ioquatix
ioquatix

Reputation: 1476

How to set a breakpoint that only triggers when within a specific parent function?

I would like to add a breakpoint to a method rb_vm_check_ints but only when it's called from within rb_ary_collect_bang. There are several threads executing.

Upvotes: 0

Views: 228

Answers (2)

Dave Lee
Dave Lee

Reputation: 6489

As a follow up to Jim's answer, this can also be done as a one liner, without creating a named function:

breakpoint command add -s python -o 'return frame.parent.name == "rb_ary_collect_bang"'

lldb creates a wrapper function for you (which has a frame parameter), and the key is to return the result of the comparison, because as Jim said, lldb will stop if the result is true, and keep going if the result is false.

This can be extended to looking at any calling function in the stack:

br c add -s python -o 'return any(f.name == "rb_ary_collect_bang" for f in frame.thread)'

This one is a bit more opaque. The expression frame.thread is an iterator of all frames on the current thread's stack. The expression [f.name for f in frame.thread] would give you a list of all function names on the stack. The expression any(f.name == "abc" for f in frame.thread) returns true if the function "abc" is anywhere in the stack.

GDB has some helper functions for these cases, and I wrote a similar set functions for lldb. https://github.com/kastiglione/lldb-helpers. Using these functions you could write:

break com add -F 'caller_is("rb_ary_collect_bang")'

Upvotes: 2

Jim Ingham
Jim Ingham

Reputation: 27118

You need to write a Python breakpoint callback. That's described here:

http://lldb.llvm.org/python-reference.html

in the section on "Running a Python Script when a breakpoint gets hit".

One thing you'll find in the docs is that if the callback returns False, then lldb won't stop for that breakpoint hit.

Also, one of the arguments passed to the callback is the frame containing the code that just hit the breakpoint. The frame object is actually an lldb.SBFrame object. The docs for SBFrame are here:

http://lldb.llvm.org/python_reference/lldb.SBFrame-class.html

The parent property of SBFrame returns the caller frame. The name property returns the function name. So you want to do something like:

def MyCallback(frame, bp_loc, dict):
    if frame.parent.name == "rb_ary_collect_bang":
       return True
    else
       return False

Upvotes: 1

Related Questions