views:

1430

answers:

6

Hi,

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 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 is 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 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??

Cheers,

Sice

+5  A: 

How about using strace for system calls and ltrace for library calls to see traces instead of using gdb? strace for system calls and ltrace for library calls work with any application, even if you don't have the source code.

If you have access to the source code, then you could also instrument your own code to do traces. Gcc has a mode that allows you to do that. Here's an example.

Then you would only have to analyze the log files.

If you want to use plain GDB, then you can use Breakpoint Command Lists

You can give any breakpoint (or watchpoint or catchpoint) a series of commands to execute when your program stops due to that breakpoint. For example, you might want to print the values of certain expressions, or enable other breakpoints.

And in particular:

for example, here is how you could use breakpoint commands to print the value of x at entry to foo whenever x is positive.

break foo if x>0
commands
silent
printf "x is %d\n",x
cont
end
lothar
A: 

Thanks for the reply...unfortunately that tool will not work on existing applications where recompiling is not an option, am i right?

I would like to find a script that can automate the process of the next command in GDB, with address locations. Any ideas?

Comments to posts should be added using "add comment" and not as a reply to your own question.
X-Istence
strace amd ltrace work with any application no matter if you have the source or not.
lothar
A: 

AFAIK, It's not easy to automate gdb. You can try insight, to see if the little amount of tcl scritiability can help you.

You can try automatiing gdb/MI with some expect script.

In automatable debuggers, i'vd found Sun's dbx to be best. It has a 'ksh' integrated in it. It's available on linux

Vardhan Varma
+1  A: 

Fenris, is THE tool your looking for, not gdb.

Authored by the legondary Michal Zalewski, is exactally what your looking for.

RandomNickName42
A: 

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!

Michael Snyder
+2  A: 

There is a shell script for automatizating tracing function calls with gdb

http://blog.superadditive.com/2007/12/01/call-graphs-using-the-gnu-project-debugger/

It dumps all functions from program and generate a gdb command file with breakpoints on each function. At each breakpoint, "backtrace 2" and "continue" are executed.

This script is rather slow on big porject (~ thousands of functions), so i add a filter on function list (via egrep). It was very easy. I use this script almost evry day.

osgx