I need to estimate the exact starting location of some hotspot in a program, in terms of x86 machine instruction count (so that it can later be run in some emulator/simulator). Is there a way to use gdb to count the number of machine instructions being executed up to a breakpoint?
There are other alternatives of course, I could use a emulation / binary instrumentation tool (like Pin), and track the run while counting instructions, but that would require installing this tool on every platform I work on - not always possible. I need some tool that's available on pretty much any linux machine.
With gdb, I guess it's also possible to run stepi X
over large strides as some sort of coarse grained search until we hit the breakpoint, then repeat with reduced the resolution, but that would be excruciatingly slow. Is there another way to do this?
This is actually only a slight improvement of the usability of Mark's solution.
We can define a function
do_count
:and then this function can be reused for counting the number of steps over and over again:
One can even put this definition into
.gdbinit
(on Linux, on Windows it should be calledgdb.ini
) in the home-folder, so it becomes available automatically after the start of the gdb (useshow user
to see, whether the function was loaded).If you are running on bare metal ARM, you might be able to read the cycle counter, see for example Cycle counter on ARM Cortex M4 (or M3)?
In your scenario, I would try Process Record and Replay to obtain the elapsed instruction count (available since GDB 7.0 and improved afterwards):
record btrace
(orrecord full
if the former is not available).continue
execution (until a breakpoint, or usenext
or other commands to step through).info record
record stop
(recommended as the buffer is of limited size).Example:
Limitations:
set record btrace pt buffer-size <size>
for the BTS format above, see the documentation for other types).record full
, not all instructions can be captured. Notably, SSE and AVX instructions are unsupported and will cause gdb to pause execution.Try this:
Then go get a cup of coffee. Or a long lunch.