109

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?

abelenky
  • 63,815
  • 23
  • 109
  • 159
SIFE
  • 5,567
  • 7
  • 32
  • 46

4 Answers4

149

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
matt
  • 5,364
  • 1
  • 25
  • 25
37

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

Community
  • 1
  • 1
imreal
  • 10,178
  • 2
  • 32
  • 48
  • 3
    `(gdb) watch i No symbol "i" in current context.` – SIFE Jan 18 '13 at 00:12
  • 2
    You have to be at a place in the code where `i` exists. Try `break main`, `run`, `c`, `s` (step to make sure you get past the declaration), and then the commands on the answer. Be sure to compile your program with the `-g` flag. (i.e. with debug information) – imreal Jan 18 '13 at 00:14
  • Before execution starts, other compilation units / files linked against your main executable might not be loaded yet. A nifty option is to then use `start `, which is like `tb main`, `run `. This will start the program, allowing you to set break/watch points more-easily. – JWCS Oct 15 '20 at 14:07
10

First, you need to compile your code with appropriate flags, enabling debug into code.

$ gcc -Wall -g -ggdb -o ex1 ex1.c

then just run you code with your favourite debugger

$ gdb ./ex1

show me the code.

(gdb) list
1   #include <stdio.h>
2   int main(void)
3   { 
4     int i = 0;
5     for(i=0;i<7;++i)
6       printf("%d\n", i);
7   
8     return 0;
9   }

break on lines 5 and looks if i == 5.

(gdb) b 5
Breakpoint 1 at 0x4004fb: file ex1.c, line 5.
(gdb) rwatch i if i==5
Hardware read watchpoint 5: i

checking breakpoints

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004fb in main at ex1.c:5
    breakpoint already hit 1 time
5       read watchpoint keep y                      i
    stop only if i==5

running the program

(gdb) c
Continuing.
0
1
2
3
4
Hardware read watchpoint 5: i

Value = 5
0x0000000000400523 in main () at ex1.c:5
5     for(i=0;i<7;++i)
Alex
  • 450
  • 4
  • 14
4

There are hardware and software watchpoints. They are for reading and for writing a variable. You need to consult a tutorial:

http://www.unknownroad.com/rtfm/gdbtut/gdbwatch.html

To set a watchpoint, first you need to break the code into a place where the varianle i is present in the environment, and set the watchpoint.

watch command is used to set a watchpoit for writing, while rwatch for reading, and awatch for reading/writing.

alinsoar
  • 15,386
  • 4
  • 57
  • 74