bcattle
bcattle

Reputation: 12819

XCode/LLDB: Can LLDB break on the calling function?

I'm setting a symbolic breakpoint on -[CALayer setSpeed:], and I'd like the breakpoint to only be triggered when the function is called by a specific function

-[UIPercentDrivenInteractiveTransition _updateInteractiveTransition:percent:isFinished:didComplete:]

Is there a way to do this?

I can see the value of the calling function manually, by doing bt 2. Is there perhaps some way to perform a string comparison with this output in a breakpoint conditional?

Thanks!

Upvotes: 1

Views: 1095

Answers (1)

Jason Molenda
Jason Molenda

Reputation: 15395

You can do this with a bit of python scripting on the breakpoint. It means that lldb will stop the process every time the breakpoint is hit and resume it -- for a really hot function like objc_msgSend, this will impact performance dramatically.

Create a python function in your homedir like ~/lldb/stopifcaller.py with these contents

import lldb
def stop_if_caller(current_frame, function_of_interest):
  thread = current_frame.GetThread()
  if thread.GetNumFrames() > 1:
    if thread.GetFrameAtIndex(1).GetFunctionName() != function_of_interest:
      thread.GetProcess().Continue()

Then put

command script import ~/lldb/stopifcaller.py

in your ~/.lldbinit file.

Use it like this in lldb:

(lldb) br s -n bar
Breakpoint 1: where = a.out`bar + 15 at a.c:5, address = 0x0000000100000e7f
(lldb) br comm add --script-type python -o "stopifcaller.stop_if_caller(frame, 'foo')" 1

and you're done - breakpoint 1 (on bar()) will only stop if the caller frame is foo(). Or to put it another way, it will continue if the caller frame is not foo().

Upvotes: 4

Related Questions