Say I define a variable named var
in 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?
An oddity of gdb is that
watch
tries to respect the scope of all the constituent parts of the expression. So, if youwatch var
, andvar
goes out of scope, the watchpoint is removed. This applies to the elements of a more complicated expression as well, likewatch 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 thewatch
command. This will do what you more commonly want to do -- just want the memory referred to by the expression. So,watch -location var
.