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 dprintfs end up like: printf " myFile:testF %d\n", 113, printf " myFile:testF %d\n", 114, and so on?
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With