GDB: break if variable equal value

前端 未结 4 1824
既然无缘
既然无缘 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:59

    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 
    main()
    { 
         int i = 0;
         for(i=0;i<7;++i) {
           looping:
            printf("%d\n", i);
         }
         return 0;
    }
    
    (gdb) break main:looping if i == 5
    

提交回复
热议问题