Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a watch point in GDB

I am operating a huge code base and want to monitor a value of a particular variable (which is buried deep down inside one of the files)especially when it gets set to zero.

1) Variable does not belong to global scope .Is there a better option than to first set breakpoint into the function where it is defined and then set the watch point?
2) After trying the option in 1 I see that watch point gets deleted after a while saying its out of frame which used this .This way it adds to the tediousness of the procedure since I have to add it again and again?Any workarounds?
3) Is there a way to check ie watch if a particular variable is equal to 0( or any specific constant)?

like image 306
Manish Avatar asked Sep 13 '11 01:09

Manish


People also ask

How do I add a watchpoint in GDB?

If GDB cannot set a hardware watchpoint, it sets a software watchpoint, which executes more slowly and reports the change in value at the next statement, not the instruction, after the change occurs. You can force GDB to use only software watchpoints with the set can-use-hw-watchpoints 0 command.

How do I set a breakpoint in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

How does watch work in GDB?

watch allows us to stop the execution every time the value of a variable changes. display prints variables every time the program's execution stops (i.e. at a watchpoint, breakpoint, etc…) Using both allows us to automatically stop at various points throughout a loop, and print all the relevant variables.


1 Answers

want to monitor a value of a particular variable

Often this is not the best approach, especially in large codebases.

What you really likely want to do is understand the invariants, and assert that they are true on entry and exit to various parts of the code.

1) Variable does not belong to global scope .Is there a better option than to first set breakpoint into the function where it is defined and then set the watch point?

No. For automatic (stack) variables you have to be in the scope where the variable is "active".

What you can do is set a breakpoint on some line, and attach commands to that breakpoint that will set the watchpoint automatically, e.g.

(gdb) break foo.c:123
(gdb) commands 1
      silent
      watch some_local
      continue
      end

3) Is there a way to check ie watch if a particular variable is equal to 0

You can't do that with a watchpoint, but you can with a conditional breakpoint:

(gdb) break foo.c:234 if some_local == 0
like image 106
Employed Russian Avatar answered Sep 22 '22 17:09

Employed Russian