I'm debugging a C/C++ program using GDB. I want to print the value of a variable right after a specific statement executes, but before the current scope (e.g., function or loop body) exits.
The challenge is that sometimes the statement I care about is the last one in a block, such as:
for (int i = 0; i < 10; ++i) {
int x = i * 2; // I want to print x *after* this line, but before leaving the scope
}
Things I've tried:
next
to run: will leave the scope, cannot print x.stepi
: low level details seem to matter -- i don't know when to stop.Is there a reliable way in GDB to:
I'm open to scripting solutions or using probe, finish, stepi, etc., as long as it can work reliably across such edge cases (better can be automated).
As per @ssbssa's comment, there is nowhere in the source code to break at after the
final assignment of x
but before it is out of scope. But you can nevertheless
inspect the final value of x
before it goes out of scope using the watch -l|-location
variant of watchpoint, documented:
Ordinarily a watchpoint respects the scope of variables in expr ... The -location argument tells GDB to instead watch the memory referred to by expr. In this case, GDB will evaluate expr, take the address of the result, and watch the memory at that address.
So for the program:
$ cat main.cpp
int main()
{
for (int i = 0; i < 10; ++i) {
int x = i * 2; // I want to print x *after* this line, but before leaving the scope
}
return 0;
}
you can go about it like this:
$ gcc -g main.cpp
$ gdb -q a.out
Reading symbols from a.out...
(gdb) b main
Breakpoint 1 at 0x1131: file main.cpp, line 3.
(gdb) r
Starting program: /home/imk/develop/so/scrap2/a.out
Downloading separate debug info for system-supplied DSO at 0x7ffff7fc3000
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, main () at main.cpp:3
3 for (int i = 0; i < 10; ++i) {
(gdb) s
4 int x = i * 2; // I want to print x *after* this line, but before leaving the scope
(gdb) watch -l x if i==10
Hardware watchpoint 2: -location x
We need x
to be in scope to set a watchpoint on its location, but now we can watch
it beyond scope, and we trigger the watch only on the loop exit condition.
(gdb) c
Continuing.
warning: Watchpoint condition cannot be tested in the current scope
Download failed: Invalid argument. Continuing without source file ./stdlib/./stdlib/exit.c.
Hardware watchpoint 2: -location x
Old value = 18
New value = 32767
__run_exit_handlers (status=0, listp=0x7ffff7e03680 <__exit_funcs>,
run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true)
at ./stdlib/exit.c:38
warning: 38 ./stdlib/exit.c: No such file or directory
(gdb) quit
A debugging session is active.
Inferior 1 [process 43041] will be killed.
Quit anyway? (y or n) y
There, Old value = 18
is the final in-scope value of x
. New value = 32767
is its first out-of-scope (garbage) value. At this point we're warned that the watch condition is also out-of-scope.