Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to watch consistently and examine occasionally the variable outside the current frame in gdb

Tags:

gdb

watchpoint

Say I define a variable named varin the main function. And the I set a watchpoint to it. Then I enter another function called func(). At this time, the watchpoints may be deleted so that I have no access to that variable. Any method to make to possible to always keep the watchpoint whenever you are?

Also, I know I can use syntax like print main::var to print out the value of the variable. But that is not sufficient enough. Any good idea?

like image 337
walkerlala Avatar asked Feb 10 '23 19:02

walkerlala


1 Answers

An oddity of gdb is that watch tries to respect the scope of all the constituent parts of the expression. So, if you watch var, and var goes out of scope, the watchpoint is removed. This applies to the elements of a more complicated expression as well, like watch a + b.

This has a justification, of course, and is sort of cool in a way -- but it is rarely what you actually want. It's far more normal, in my experience, not to care about the scope and to just want to watch some bit of memory.

To do this, pass -location to the watch command. This will do what you more commonly want to do -- just want the memory referred to by the expression. So, watch -location var.

like image 71
Tom Tromey Avatar answered Feb 15 '23 12:02

Tom Tromey