lldb python handle breakpoint hit
Question: I was looking through this huge LLDB python reference manual, and can't find
still how to handle breakpoint hit with my code. I mean, I want to run my
foo() when some breakpoint was hit.

Please help me with some code snippets!

Or point me the needed class in Python reference manual.

Thanx!


Answer: On the page:

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

there is a section titled:

RUNNING A PYTHON SCRIPT WHEN A BREAKPOINT GETS HIT

which has some useful info.

What you are doing is "adding a command to your breakpoint". The lldb command
for this is "breakpoint command add". All the basic lldb commands are in the
form "noun [sub-noun [...]] verb options"; knowing that might help translate
from the thing you want to do to where in the command set the command for that
operation lives. Anyway, the help on "breakpoint command add" has other
examples that might be useful.

Then condensed version is, make a python module containing a function with
this signature:

    
    
    breakpoint_function (frame, bp_loc, dict)
    

Bring that module into lldb with the command:

    
    
    (lldb) command script import "path to your .py file"
    

You can also use the module name in "command script import" if you've set up
the PYTHONPATH to point to it, but unlike Python's "import" you don't have to,
the command will take care of that for you.

Then make a breakpoint, and use "br com a" to add your function to it:

(lldb) br com a -F MyModule.breakpoint_function

Now when a breakpoint gets hit, your function will get called with the
following arguments:

The "frame" argument is the frame that hit the breakpoint. You can get the
thread from the frame & thus the complete stack if you need it.

The "bploc" argument is the "Breakpoint Location" that hit the breakpoint. In
lldb one "breakpoint specification" (which is what you are setting with "break
set") can resolve to many locations. For instance, a "source pattern"
breakpoint might match many source patterns in your code. So you might want to
know which one was actually hit.

The "dict" option is so we can squirrel some stuff away and pass it to Python,
it should be left alone.

One other thing to keep in mind is that though the script interpreter
(accessible with the "script" command) defines lldb.thread, lldb.frame etc.
convenience variables, these variables are NOT set up when your breakpoint
command is running. So if you've used these variables in the script
interpreter while prototyping your command, you'll have to find them from the
frame you were passed in if you need them in the breakpoint command.

Note, Python breakpoint commands don't currently work in Xcode 6, though that
should be fixed by the time it is done.

