Is it possible to break a breakpoint only when a certain method is present in the call stack?

Suppose I have a method foothat is called by various methods when viewing an object hierarchy.

Is it possible to split a method fooinside only when it was called by the method bar(so it is barpresent in the call stack)?

Does LLDB or GDB support this use case?

+4
source share
3 answers

gdb- , Python . $_caller_is . (FWIW , Python gdb...)

:

(gdb) break foo if $_any_caller_matches("bar")
+3

GDB , bar() foo() , foo() . bar() foo() foo().

:

int bar()
{
    foo() ;     // Add breakpoint with command list here to set breakpoint in foo()
    return 0 ;  // Add breakpoint command list here to clear breakpoint in foo()
}

, , .

foo() bar() , , foo() bar(), ; , bar() .

; , bar(), foo() , .

+2

The way you do this in lldb is to write a Python-based breakpoint command that checks the stack of the thread that hits the breakpoint and continues if it does not contain a frame with this function. Stop commands tell lldb to stop or not, returning True or False respectively from the function. Therefore, a very simple way to do this is to make a Python file (break_here.py):

import lldb
desired_func = "some_func"
def break_here(frame, bp_loc, dict):
    thread = frame.thread
    for frame in thread.frames:
        if frame.name == desired_func:
            return True
    return False

Suppose this file is located in / tmp. Then in lldb you will do:

(lldb) com scr imp /tmp/break_here.py
(lldb) br s -n whatever
(lldb) br com add --python-function break_here.break_here
+2
source

Source: https://habr.com/ru/post/1621083/


All Articles