Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I constantly watch variables in GDB?

Tags:

Many IDEs have a "variables" pane where you can add/remove variables to watch. These variables value are updated every time you step the code.

Is there such an option in GDB?

like image 875
Strudle Avatar asked Feb 12 '18 07:02

Strudle


1 Answers

If you want a graphical version of GDB, then you can look at DDD. It has the exact option you're looking for.

GDB - not being graphical - cannot deliver the kind of functionality in a pane, but you can use the display command to show a variable after each step.

I think the most practical way to achieve this (and what I do in practice) is to use watch. From the prompt: watch varname will break whenever a variable is written to. For global variables, sometimes it's best to get the address of the variable with print &varname and then set a watch on the address watch (int)0xDEADBEEF, replacing DEADBEEF with the output of the print command, and using something other than int if it isn't an int.

There is also GDB's TUI mode. I don't believe you can tell it to watch a specific variable, but you can have it display the registers (or the source code, or other things) as you step through. In the prompt (after launching gdb -tui) enter: tui reg general. This will show the variable after each step while it is in a register. More TUI options.

like image 200
dbeer Avatar answered Sep 23 '22 11:09

dbeer