1

Is there a way possible to set a watchpoint for a particular variable, but to only watch whether it's value is changing within a particular function? I have the function which is changing the variable value narrowed down, so only want to "watch" it's value within the function instead of just doing

(gdb) watch <variable-name>

as that would take a substantially longer time to run.

Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40

1 Answers1

0

I have the function which is changing the variable value narrowed down, so only want to "watch" it's value within the function instead of just doing watch <variable-name>.

If you are on a system with hardware watchpoint support, and if the variable is global, then there is no advantage to watching the variable only while in the function of interest -- the watchpoint has no overhead, so you might as well keep it in place.

But let's assume that either the variable is not global, or your platform has only software watchpoints (which are incredibly slow).

In that case, what you want to do is set breakpoints on entry to and return from the function of interest (see this answer).

Once you have these breakpoints, attach commands to them. The entry breakpoint command will set a watchpoint, and the return breakpoint will remove it.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362