6

With GDB I can watch i, to break whenever i changes. The problem is that I have multiple functions using the name i, so GDB breaks inside of all those functions.

Is it possible to break whenever i changes, but only inside a given function?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 1
    Please show us a transcript of your interaction with GDB. When I `watch i` inside of a function, GDB (correctly) deletes the watchpoint as soon as I return from that function. – Employed Russian May 08 '12 at 15:39
  • Possible duplicate of [How do I set persistent and conditional watchpoints on locally scoped variables?](http://stackoverflow.com/questions/1354637/how-do-i-set-persistent-and-conditional-watchpoints-on-locally-scoped-variables) – Ciro Santilli OurBigBook.com Apr 19 '17 at 08:59

1 Answers1

6

I guess you are watching a global variable and hence it stops in all functions where this variable is modified which is the logical and expected behaviour. If you want to break only inside a given function whenever i changes, dont set the breakpoint for i. Instead set a breakpoint for the desired function. When this breakpoint is hit, now set the watch for i so that you know for sure that the next breakpoint will be hit when i is modified in the desired function (Ignoring possible recursions and the like)

I guessed that you may not be watching a local variable inside every function with the same name because you cannot set a breakpoint on a local variable until you are in the scope of that particular function.

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125