Make GDB print control flow of functions as they are called Make GDB print control flow of functions as they are called c c

Make GDB print control flow of functions as they are called


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 functiontraceif $arg0    break $arg0    commands        where        continue        end    endif $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.


There's rbreak cmd accepting regular expression for setting breakpoints. You can use:

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

See this for details on breakpoints.

Then use commands to print every function as it's called. E.g. let α = the number of the last breakpoint (you can check it with i br if you missed), 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 elaboration: silent suppresses unnecessary informational messages, bt 1 prints the last frame of backtrace (i.e. it's the current function), c is a shortcut for continue, to continue execution, and end is just the delimiter of command list.

NB: if you trace library functions, you may want to wait for lib to get loaded. E.g. set a break to main or whatever function, run app until that point, and only then set breakpoints you wanted.