Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lldb: how to get calling function(s)?

I'm wondering if one could get selected items from the bt command. E.g. to find the calling functions for a certain function. The idea is basically to add a non-stop breakpoint at the function in question and then print the callstack e.g. calling function, maybe the calling function of that as well. In the end this would likely boil down to filtering the result of the bt command. The filtering would be necessariy to exclude the framework and runtime methods in between.

like image 248
angerman Avatar asked Oct 26 '25 03:10

angerman


1 Answers

You need to drop into a little Python scripting to do this today - it's not bad.

(lldb) br se -n mach_msg
(lldb) br comm add -s python 1
Enter your Python command(s). Type 'DONE' to end.
> thread = frame.GetThread()
> frnum = 0
> for fr in thread.frames:
>   print '% 2d %s' % (frnum, fr.GetFunctionName())
>   frnum = frnum + 1
> frame.GetThread().GetProcess().Continue()
> DONE

I set my breakpoint (on mach_msg() in this example), I add a command to my breakpoint (-s python means it is written in the scripting language python; I'm adding this command to breakpoint #1).

The python code is supplied with the current breakpoint object and frame object automatically (see "help break command add" in lldb). I get the current thread from the frame object, then iterate over the stack frames for that thread.

lldb has lots of built in information about what operations you can do on these python objects. e.g.

(lldb) script help (lldb.SBFrame)
(lldb) script help (lldb.SBThread)

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

like image 59
Jason Molenda Avatar answered Oct 27 '25 23:10

Jason Molenda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!