gdb can use display
to print an expression each step. I want to format the expression, using gdb's printf
. How can I tell gdb to run a printf
each step, similar to display?
You can use hook-next
. Documentation.
Example:
// t.c
int main()
{
int x = 2;
x++;
x += 3;
x += 5;
return x - 11;
}
gcc -g t.c && gdb -q ./a.out
(gdb) start
(gdb) define hook-next
Type commands for definition of "hook-next".
End with a line saying just "end".
>printf "%08d\n", x
>end
(gdb) next
00032767 # <<-- x hasn't been initialized yet.
4 x++;
(gdb) next
00000002
5 x += 3;
(gdb)
00000003
6 x += 5;
(gdb)
00000006
7 return x - 11;
(gdb)
00000011
8 }
(gdb) q