conditional breakpoint on an indirect addressing mode in gdb - gdb

I would like to set up a conditional breakpoint on an instruction that is like add [ebp+0xc], 1 but I can't figure out the right expression to give to gdb's shell.
I've tried that one yet it doesn't seem to work:
b *0xdeadbeef if ($ebp+0x0c) == 0xf00
But I think it only breaks when $ebp = 0xf0c (0xf00 + 0x0c) and this is not the intented result.
How could I perform a conditional breakpoint in that the memory location pointed by [ebp+0x0c] contains any specific value?

Try
b *0xdeadbeef if *(int*)($ebp+0x0c) == 0xf00

Related

Is it possible to name a break point in GDB?

Is there any way to label/name breakpoints in GDB, in order to more easily identify them in, for instance, info b? And if so, how?
No, there is no way to do this.
http://users.ece.utexas.edu/~adnan/gdb-refcard.pdf
While this doesn't help with the output of info b, for other purposes where you need to reference the breakpoint later in commands you can store the breakpoint number of the last breakpoint from $bpnum in another convenience variable, e.g.:
b foo.c:123
set $im_a_breakpoint = $bpnum
# ... set some more breakpoints and do some other stuff ...
disable $im_a_breakpoint
As a further example, I'm using this pattern right now for a task: I'm using normal gdb breakpoints in code emitted by a JIT, which requires setting the breakpoint on the address after the code has actually been emitted as gdb modifies the code. For this purpose I actually disable the initial set-up breakpoint from within its own commands block:
set $cur_stop_point = 0x41aaa
b basic_jit_cache::copy_block if ((uint32_t)this->code_ptr()) > ($gencode + $cur_stop_point)
set $cur_stop_point_setup_bp = $bpnum
commands
b *($gencode + $cur_stop_point)
disable $cur_stop_point_setup_bp
cont
end

Gdb conditional step based on memory address?

I 'm wondering if it 's possible to create a script that will continue the program 's execution (after a break) step by step based on the memory address value.
So, if I 'm tracing a function and it goes into a high memory value, I 'd call the gdb script until the memory value is below a set value - then it would break again.
I 'm very new to gdb and still reading the manual/tutorials, but I 'd like to know if my goal is possible :) - and if you could bump me to the proper direction, even better ;)
Thanks!
Edit, updated with pseudocode:
while (1) {
cma = getMemoryAddressForCurrentInstruction();
if (cma > 0xdeadbeef) {
stepi;
} else {
break;
}
}
You're talking about the Program Counter (sometimes called the instruction pointer). It's available in gdb as $pc. Your pseudocode can be translated into this actual gdb command:
while $pc <= 0xdeadbeef
stepi
It'll be slow, since it's starting and stopping the program for every instruction, but as far as I know there's no fast way to do it if you don't know exactly what address you're looking for. If you do, then you can just set a breakpoint there:
break *0xf0abcdef
cont
will run until the program counter hits 0xf0abcdef

How to make GDB substitute variables with their current value when a conditional breakpoint is created

I would like GDB to perform variable substitution when I create a conditional breakpoint. For example:
set variable $my_value = 1
b my_function if my_param == $my_value
set variable $my_value = 5
b my_function if my_param == $my_value
This actually creates 2 identical breakpoints which break in my_function() when my_param equals the current value of $my_value. Hence when running my program a breakpoint is only triggered when my_param is equal to 5. What I actually wanted was two different conditional breakpoints, for the values 1 and 5.
Is there any way to make GDB set conditional breakpoints like this using the current value of a convenience variable instead of the variable itself?
I ask this question because I'm trying to create a GDB script to track memory deallocation which will automatically set conditional breakpoints, e.g.
# set breakpoint after malloc() statement of interest
b some_file.c:2238
# define commands to execute when the above breakpoint is hit
commands
# $last is set to the allocated memory address
set variable $last = new_pointer
# set conditional breakpoint in free() to check when allocated pointer is released
b free if ptr == $last
continue
end
But of course I find that this only works for the last pointer value because all my auto generated breakpoints are identical!
I am going to investigate the use of Python scripting to see if this could solve my problem, but as I have no experience of Python I wanted to post this question first! I feel sure that it should be possible to do what I am trying to achive and any help or suggestions would be much appreciated.
For completness here is how to use the eval command with my original example:
set variable $my_value = 1
eval "b my_function if my_param == %d", $my_value
set variable $my_value = 5
eval "b my_function if my_param == %d", $my_value
This generates two breakpoints for the values 1 and 5 as desired!
Use the eval command (apparently in gdb 7.2 and later)

How do I set persistent and conditional watchpoints on locally scoped variables?

If I set a watchpoint for a variable local to the current scope, it will be auto deleted when going out of the scope. Is there any way to set it once and keep it auto alive whenever entering the same scope?
Is there anyway to set conditional watchpoint, like watch var1 if var1==0? In my case, the condition does't work. gdb stops whenever var1's value is changed, instead of untill var1 == 0 is true. My gdb is GNU gdb 6.8-debian.
I agree with Dave that a conditional breakpoint is the way to go.
However, to do what you asked, you can use GDB's commands command to set a list of GDB commands to execute whenever a breakpoint is hit. I find this incredibly useful.
I suggest writing your GDB commands into a file so that they are easy to edit and easy to reload with the source command. Or you can specify command files to load on the GDB command line or use .gdbinit to make them load automatically.
An example of a good use of commands:
Suppose that I have a function format that is called by a lot of other functions. I want to break on it, but only after function do_step_3 has been called.
break do_step_3
commands
break format
continue
end
You could use this for your problem with something like:
break func
commands
watch var
continue
end
You can set conditions on watchpoints in the same way that you do with breakpoints. This is in the documentation but admittedly it hardly calls attention to itself.
So watch my_var if my_var > 3 works just fine, as does the condition command.
To recreate the watchpoint if the variable it is watching goes out of scope, have gdb do this automatically using a breakpoint at the start of the function as Zan has described.
You can set a watchpoint that does not go out of scope by setting it to the memory address.
(gdb) p &var1
$1 = (int *) 0x41523c0
(gdb) watch *(int *)0x41523c0
Hardware watchpoint 1: *(int *)0x41523c0
This also works for other data types and pointers.
I'm not sure which language us are using, so the exact answer will vary, but could you change the variable to either be static, global, or dynamically allocated (and don't free it when the function returns?). This way it's raw address won't change, and gdb will be able breakpoint on it.
Instead of watching the value whe it equals a specific value; you should set a conditional break point on the line where you want to check the value of var1. This should effectively have the same effect
e.g.
(gdb) break main.c:123 if (var1 == 0)

Set Visual Studio (conditional) breakpoint on local variable value

I'm trying to debug a method which among other things, adds items to a list which is local to the method.
However, every so often the list size gets set to zero "midstream". I would like to set the debugger to break when the list size becomes zero, but I don't know how to, and would appreciate any pointers on how to do this.
Thanks.
Why not use conditional breakpoints?
http://blogs.msdn.com/saraford/archive/2008/06/17/did-you-know-you-can-set-conditional-breakpoints-239.aspx
in C#
if(theList.Count == 0){
//do something meaningless here .e.g.
int i = 1; // << set your breakpoint here
}
in VB.NET
If theList.Count = 0 Then
'do something meaningless here .e.g.
Dim i = 1; ' << set your breakpoint here
End If
For completeness sake, here's the C++ version:
if(theList->Count == 0){
//do something meaningless here .e.g.
int i = 1; // << set your breakpoint here
}
I can give a partial answer for Visual Studio 2005. If you open the "Breakpoints" window (Alt + F9) you get a list of breakpoints. Right-click on the breakpoint you want, and choose "Condition." Then put in the condition you want.
You have already got both major options suggested:
1. Conditional breakpoints
2. Code to check for the wrong value, and with a breakpoint if so happens
The first option is the easiest and best, but on large loops it is unfortunately really slow! If you loop 100's of thousands iterations the only real option is #2. In option #1 the cpu break into the debugger on each iteration, then it evaluates the condition and if the condition for breaking is false it just continiues execution of the program. This is slow when it happens thousands of times, it is actually slow if you loop just 1000 times (depending on hardware of course)
As I suspect you really want an "global" breakpoint condition that should break the program if a certain condition is met (array size == 0), unfortunately that does not exist to my knowledge. I have made a debugging function that checks the condition, and if it is true it does something meaningless that I have a breakpoint set to (i.e. option 2), then I call that function frequently where I suspect the original fails. When the system breaks you can use the call stack to identify the faulty location.