views:

1930

answers:

4

How do I make gdb print functions of interest as they are called, indented according to how deep in the stack they are?

I want to be able to say something like (made up):

(gdb) trace Foo* Bar* printf

And have gdb print all functions which begin with Foo or Bar, as they are called. Kind of like gnu cflow, except using the debugging symbols and only printing functions which actually get called, not all possible call flows.

Tools which won't help include cachegrind, callgrind and oprofile, which order the results by which functions were called most often. I need the order of calling preserved.

The wildcarding (or equivalent) is essential, as there are a lot of Foo and Bar funcs. Although I would settle for recording absolutely every function. Or, perhaps telling gdb to record all functions in a particular library.

Some GDB wizard must have a script for this common job!

+1  A: 

Did you see litb's excellent anser to a similar post here ?

He uses readelf to get interesting symbols, gdb commands to get the trace, and awk to glue all that.

Basically what you have to change is to modify his gdb command script to remove the 1 depth from backtrace to see the stack and filter specific functions, and reformat the output with an awk/python/(...) script to present it as a tree. (I admit I'm too lazy to do it now...)

Piotr Lesnicki
That looks like a very similar question, although more general in that I'm specifically asking about gdb (although I'd be happy with anything).All the answers seem wrong though. The one you point to breaks on a specific func of interest and finds all it's callers. I want all func invocations.
callgiraffe, no it doesn't
Johannes Schaub - litb
A: 

You may call gdb in batch mode (using -x option), break where you need and ask for backtrace (bt), then you filter the result using grep or egrep.

Indents are more difficult, however bt output is ordered so you have current function at the top of the trace and main at very bottom.

So you create file with commands:

br <function name where to break>
run
bt
kill
quit

then run gdb <program> -x<command file>

Filter strings that starts with #<digit> - you get stack trace.

qrdl
+1  A: 

Use the right tool for the job ;)

http://stackoverflow.com/questions/764382/automate-tracing-in-gdb/1123188#1123188

RandomNickName42
+2  A: 

In your case I would turn to the define command in gdb, which allows you to define a function, which can take up to 10 arguments.

You can pass in the names of functions to "trace" as arguments to the function you define, or record them all in the function itself. I'd do something like the following

define functiontrace
if $arg0
    break $arg0
    commands
        where
        continue
        end
    end

if $arg1
...

Arguments to a user-defined function in gdb are referenced as $arg0-$arg9. Alternatively, you could just record every function you wanted to trace in the function, instead of using $arg0-9.

Note: this will not indent as to depth in the stack trace, but will print the stack trace every time the function is called. I find this approach more useful than strace etc... because it will log any function you want, system, library, local, or otherwise.

alesplin