GDB Break if frame is in backtrace
Asked Answered
N

3

13

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?

Nostomania answered 12/11, 2010 at 5:23 Comment(1)
Possible duplicate of Is there any way to set a breakpoint in gdb that is conditional on the call stack?Nardi
A
3

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.

Albaugh answered 14/11, 2010 at 13:0 Comment(1)
That's what I ended up doing but I would of liked to know how to do it without recompiling.Nostomania
F
4

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.

Flunk answered 2/5, 2016 at 13:29 Comment(0)
A
3

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.

Albaugh answered 14/11, 2010 at 13:0 Comment(1)
That's what I ended up doing but I would of liked to know how to do it without recompiling.Nostomania
M
0

Alternative to rix0rrr's answer:

b main
commands
set $inParentFunction = 0
c
end

b ParentFunction
commands
set $inParentFunction = 1
c
end

b FunctionImInterestedIn if ($inParentFunction)
Mansur answered 15/8, 2017 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.