Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set persistent and conditional watchpoints on locally scoped variables?

Tags:

gdb

watchpoint

  1. If I set a watchpoint for a variable local to the current scope, it will be auto deleted when going out of the scope. Is there any way to set it once and keep it auto alive whenever entering the same scope?

  2. Is there anyway to set conditional watchpoint, like watch var1 if var1==0? In my case, the condition does't work. gdb stops whenever var1's value is changed, instead of untill var1 == 0 is true. My gdb is GNU gdb 6.8-debian.

like image 233
Tim Avatar asked Aug 30 '09 19:08

Tim


People also ask

How to set watchpoints in GDB?

For example, you can set two watchpoints with watch commands, two with rwatch commands, or two with awatch commands, but you cannot set one watchpoint with one command and the other with a different command. GDB will reject the command if you try to mix watchpoints.

How to set breakpoints in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]".

What does GDB's watch command do?

Able to set a watchpoint on a variable in order to break a program when a variable changes. Use display to automatically print how variables change throughout the program's execution. watch allows us to stop the execution every time the value of a variable changes.

How to remove break point in GDB?

Deleting breakpointsWith the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers.


2 Answers

I agree with Dave that a conditional breakpoint is the way to go.

However, to do what you asked, you can use GDB's commands command to set a list of GDB commands to execute whenever a breakpoint is hit. I find this incredibly useful.

I suggest writing your GDB commands into a file so that they are easy to edit and easy to reload with the source command. Or you can specify command files to load on the GDB command line or use .gdbinit to make them load automatically.

An example of a good use of commands:
Suppose that I have a function format that is called by a lot of other functions. I want to break on it, but only after function do_step_3 has been called.

break do_step_3 commands   break format   continue end 

You could use this for your problem with something like:

break func commands   watch var   continue end 
like image 168
Zan Lynx Avatar answered Sep 21 '22 18:09

Zan Lynx


You can set conditions on watchpoints in the same way that you do with breakpoints. This is in the documentation but admittedly it hardly calls attention to itself.

So watch my_var if my_var > 3 works just fine, as does the condition command.

To recreate the watchpoint if the variable it is watching goes out of scope, have gdb do this automatically using a breakpoint at the start of the function as Zan has described.

like image 26
Robie Basak Avatar answered Sep 21 '22 18:09

Robie Basak