How to print the next N executed lines automatically in GDB?

sice picture sice · Apr 19, 2009 · Viewed 17.6k times · Source

I have been trying to find a way for some time to automate the progress in GDB of tracing the control flow of a program.

Even just a simple way of automating the n command so you can see in what order routines are called.

I realise that you can issues n x where x is the number of times GDB steps through, but the trouble with that is that it shows the command but not the address of the routine! But if you press n manually in GDB (then press return to issue the previous command) it shows the address.

I have tried the following in GDB:

(after setting a breakpoint at say 0x0123456)
b *0x0123456

GDB says <breakpoint 1 at 0x123456>. I type:

commands 1
n 1000
c
end

but it doesn't loop as expected, and it doesn't show the address location :-(.

Any help would be appreciated! Surely it must be simple to automatically log the order routines are called??

Answer

Michael Snyder picture Michael Snyder · Oct 9, 2009

This is easy, actually. I'll give you the bare bones, and you can modify to suit.

(gdb) define nstep
> set $foo = $arg0
> while ($foo--)
>  step
>  end
> end
(gdb) nstep 100

I've done this many times. Hope this helps!