GDB: break if variable equal value

匿名 (未验证) 提交于 2019-12-03 01:55:01

问题:

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;      for(i=0;i

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?

回答1:

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


回答2:

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 i == 5

You can get the breakpoint number by using info watch



回答3:

  $ gcc -Wall -g -ggdb -o ex1 ex1.c    $ gdb ./ex1    (gdb) list   1 #include    2 int main(void)   3 {    4   int i = 0;   5   for(i=0;i


回答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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!