Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB Break if frame is in backtrace

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?

like image 410
Chazz Avatar asked Nov 12 '10 05:11

Chazz


People also ask

How do you analyze a GDB backtrace?

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.

How do you analyze backtrace?

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 .

What does frame do in GDB?

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.


2 Answers

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.

like image 137
Itamar Katz Avatar answered Sep 30 '22 16:09

Itamar Katz


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.

like image 23
rix0rrr Avatar answered Sep 30 '22 15:09

rix0rrr