GDB: break if variable equal value

前端 未结 4 1825
既然无缘
既然无缘 2020-12-07 10:34

I like to make GDB set a break point when a variable equal some value I set, I tried this example:

#include 
main()
{ 
     int i = 0;
     fo         


        
4条回答
  •  再見小時候
    2020-12-07 10:57

    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 
    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)
    

提交回复
热议问题