Make GDB print flow control functions, as they are called

How to make interesting print functions of gdb, as they are called, indented depending on how deep they are on the stack?

I want to say something like (composed):

(gdb) trace Foo* Bar* printf 

And gdb print all the functions that start with Foo or Bar, as they are called. A view like gnu cflow, except for the use of debugging symbols and only print functions that are actually called, and not all possible call flows.

Tools that won't help include cachegrind, callgrind, and oprofile, which streamline the results by which functions are called most often. I need a call order.

A wildcard (or equivalent) is necessary since there are many Foo and Bar functions. Although I would agree to record absolutely every function. Or perhaps by pointing gdb to write all the functions in a specific library.

Some GDB masters must have a script for this general task!

+17
c debugging dynamic gdb call-graph
Nov 23 '08 at 0:25
source share
5 answers

In your case, I will move on to the define command in gdb, which will allow you to define a function that can take up to 10 arguments.

You can pass function names for "tracing" as arguments to the function you define, or write them all in the function itself. I would do something like the following

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

The arguments to a user-defined function in gdb are called $ arg0- $ arg9. In addition, you can simply write down every function that you want to track in a function, instead of using $ arg0-9.

Note. This will not be indented in depth in the stack trace, but will print a stack trace with every function call. I find this approach more useful than strace, etc., because it will register any function you want, system, library, local or other.

+8
Sep 30 '09 at 20:22
source share

Use the right tool for the job;)

How to print the following N completed lines automatically in GDB?

+2
Jul 14 '09 at 2:59
source share

There rbreak cmd takes a regex to set breakpoints. You can use:

 (gdb) rbreak Foo.* (gdb) rbreak Bar.* (gdb) break printf 

For more information about breakpoints, see this .

Then use commands to print each function on its call. For example. let α = the number of the last breakpoint (you can check it with i br if you missed), and then do:

 (gdb) commands 1-α Type commands for breakpoint(s) 1-α, one per line. End with a line saying just "end". >silent >bt 1 >c >end (gdb) 

Some development: silent suppresses unnecessary informational messages, bt 1 prints the last backtrace frame (i.e. the current function), c is a shortcut to continue , to continue execution and end is just a separator of the command list.

NB : if you are tracing library functions, you may need to wait for the lib to load. For example. set the gap to main or any other function, run the application to this point and only then set the breakpoints that you wanted.

+2
Jan 30 '13 at 5:04 on
source share

Have you seen a bright excellent link to a similar post here ?

It uses readelf to get interesting characters, gdb commands to get tracing, and awk to glue it all together.

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

+1
Nov 23 '08 at 1:29
source share

You can invoke gdb in batch mode (using the -x option), break where you need it, and request a backtrace ( bt ), then you filter the result with grep or egrep .

The indentation is more complicated, but the output of bt ordered, so you have the current function at the top of the trace and main at the very bottom.

So, you create a file with the commands:

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

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

Filter lines starting with #<digit> - you get a stack trace.

0
Nov 23 '08 at 7:53
source share



All Articles