I’ve just discovered another neat feature of gdb: command.
I was in a situation where I needed to watch a certain variable on each breakpoint hit.
Generally, this is pretty easy in gdb using its watch-feature. Unfortunately, the variable under consideration was a QString.
Now, we already have some experience with those little brothers. If we only would have a way to call functions on each breakpoint hit…
… and here it is: command:
Once you have a breakpoint, you can assign a command to it, which will be executed each time the breakpoint is hit. For example:
1 2 3 4 5 6 7 8 9
(gdb) b main Breakpoint 1 at 0x804db90: file main.cpp, line 11. (gdb) command 1 >p argc >end (gdb) r [Thread debugging using libthread_db enabled] Breakpoint 1, main (argc=1, argv=0xbffff184) at main.cpp:11 $1 = 1
In the example above, the command p argc is assigned to breakpoint #1, which will simply print the number of arguments. If a QString variable is considered, one would’ve to replace the p with some gdb macro that is able to print a QString. If no breakpoint number is given, gdb uses the number of the last created one. To tell gdb to leave the command mode, just type end