I want to set a condition on a gdb breakpoint to only break if a certain function name appears in the backtrace. What's the best way to do this?
To display the backtrace for several or all of the threads, use the command thread apply (see thread apply). For example, if you type thread apply all backtrace , gdb will display the backtrace for all the threads; this is handy when you debug a core dump of a multi-threaded program.
A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack. To print a backtrace of the entire stack, use the backtrace command, or its alias bt .
The frame command allows you to move from one stack frame to another, and to print the stack frame you select. args may be either the address of the frame or the stack frame number. Without an argument, frame prints the current stack frame.
I am not sure how to do exactly what you ask for, but a possible workaround, if you have access to the source code of the relevant function, is to set some global boolean variable to true
in the beginning of the function, and set it to false
just before the function exits. Then you could set a conditional breakpoint (using the condition
command) to stop only when this boolean variable is true
.
A simpler solution than Python scripting is using a temporary breakpoint.
It looks like this:
b ParentFunction
command 1
tb FunctionImInterestedIn
c
end
Every time you break in ParentFunction
, you'll set a one-time breakpoint on the function you're actually interested in, then continue running (presumably until you hit that breakpoint).
Since you'll break exactly once on FunctionImInterestedIn
, this won't work if FunctionImInterestedIn
is called multiple times in the context of ParentFunction
and you want to break on each invocation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With