Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB: Watch a variable in a given scope

Tags:

c

gdb

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?

like image 527
Randomblue Avatar asked May 08 '12 15:05

Randomblue


1 Answers

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.

like image 187
Pavan Manjunath Avatar answered Oct 04 '22 00:10

Pavan Manjunath