Reputation: 38619
I would like to trace the execution of a function, by printing out each line it has executed - in gdb
, I could use dprintf
for this. Since it's a big function (from line 113 to line 200 in myFile.cpp), I came up with the following loop to set up dprintf
type breakpoints on each line of the function:
set $n=113
while ($n<200)
dprintf myFile.cpp:$n, " myFile:testF %d\n", $n
set $n=$n+1
end
This actually works, when it comes to setting the dprintf
style breakpoints:
(gdb) info br
Num Type Disp Enb Address What
1 dprintf keep y 0x080a7272 in namespace::myFile::testFunc(int&)
at /path/to/myFile.cpp:113
printf " myFile:testF %d\n", $n
2 dprintf keep y 0x080a727d in namespace::myFile::testFunc(int&)
at /path/to/myFile.cpp:114
printf " myFile:testF %d\n", $n
...
... however, if doesn't work for the respective dprintf
format string, as the gdb
variable $n
is not expanded into a number string, which is what I wanted.
So is there any way to expand the $n
variable, such that the dprintf
s end up like: printf " myFile:testF %d\n", 113
, printf " myFile:testF %d\n", 114
, and so on?
Upvotes: 3
Views: 343
Reputation: 38619
Well, turns out there's an eval
function (https://unix.stackexchange.com/questions/151502/how-to-save-the-result-of-printf-to-a-variable-in-gdb) which helps with this - the loop should be re-written like this:
set $n=113
while ($n<200)
eval "dprintf myFile.cpp:%d, \" myFile:testF %d\"", $n, $n
set $n=$n+1
end
Running this in gdb
will produce dprintf
breakpoints like:
1 dprintf keep y 0x080a7272 in namespace::myFile::testFunc(int&)
at /path/to/myFile.cpp:113
printf " myFile:testF 113"
... and so on, which - I guess - is what I needed...
Upvotes: 3