I like to make GDB set a break point when a variable equal some value I set, I tried this example:
#include <stdio.h> main() { int i = 0; for(i=0;i<7;++i) printf("%d\n", i); return 0; }
Output from GDB:
(gdb) break if ((int)i == 5) No default breakpoint address now. (gdb) run Starting program: /home/SIFE/run 0 1 2 3 4 5 6 Program exited normally. (gdb)
Like you see, GDB didn't make any break point, is this possible with GDB?
A conditional breakpoint insertion can be done by using one single command by using the breakpoint command followed by the word if followed by a condition. In the previous example, the breakpoint can be inserted with the line b 29 if (y == 999).
Conditional breakpoints can help with this. Here [CONDITION] is a boolean expression, which, in GDB is true if the result is nonzero, otherwise it is false. The condition can include a function call, the value of a variable or the result of any GDB expression. Type help condition at the GDB prompt for more.
Conditional breakpoints allow you to break inside a code block when a defined expression evaluates to true. Conditional breakpoints highlight as orange instead of blue. Add a conditional breakpoint by right clicking a line number, selecting Add Conditional Breakpoint , and entering an expression.
When specified, the break command will set a breakpoint at a given location inside the specified file. If no file is specified, the current source file will be used. When specified, the break command will set a breakpoint at a given address.
in addition to a watchpoint nested inside a breakpoint you can also set a single breakpoint on the 'filename:line_number' and use a condition. I find it sometimes easier.
(gdb) break iter.c:6 if i == 5 Breakpoint 2 at 0x4004dc: file iter.c, line 6. (gdb) c Continuing. 0 1 2 3 4 Breakpoint 2, main () at iter.c:6 6 printf("%d\n", i);
If like me you get tired of line numbers changing, you can add a label then set the breakpoint on the label like so:
#include <stdio.h> main() { int i = 0; for(i=0;i<7;++i) { looping: printf("%d\n", i); } return 0; } (gdb) break main:looping if i == 5
You can use a watchpoint for this (A breakpoint on data instead of code).
You can start by using watch i
.
Then set a condition for it using condition <breakpoint num> i == 5
You can get the breakpoint number by using info watch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With