Embedded Python Interpreter#
The embedded python interpreter can be accessed in a variety of ways from within LLDB. The easiest way is to use the lldb command script with no arguments at the lldb command prompt:
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> 2+3
5
>>> hex(12345)
'0x3039'
>>>
This drops you into the embedded python interpreter. When running under the script command, lldb sets some convenience variables that give you quick access to the currently selected entities that characterize the program and debugger state. In each case, if there is no currently selected entity of the appropriate type, the variableās IsValid method will return false. These variables are:
Variable |
Type |
Equivalent |
Description |
---|---|---|---|
|
|
|
Contains the debugger object whose |
|
|
|
Contains the currently selected target - for instance the one made with the |
|
|
|
Contains the process of the currently selected target. The |
|
|
|
Contains the currently selected thread. The |
|
|
|
Contains the currently selected stack frame. The |
While extremely convenient, these variables have a couple caveats that you should be aware of. First of all, they hold the values of the selected objects on entry to the embedded interpreter. They do not update as you use the LLDB APIās to change, for example, the currently selected stack frame or thread.
Moreover, they are only defined and meaningful while in the interactive Python
interpreter. There is no guarantee on their value in any other situation, hence
you should not use them when defining Python formatters, breakpoint scripts and
commands (or any other Python extension point that LLDB provides). For the
latter youāll be passed an SBDebugger
, SBTarget
, SBProcess
, SBThread
or
SBFrame
instance and you can use the functions from the āEquivalentā column
to navigate between them.
As a rationale for such behavior, consider that lldb can run in a multithreaded environment, and another thread might call the āscriptā command, changing the value out from under you.
To get started with these objects and LLDB scripting, please note that almost all of the lldb Python objects are able to briefly describe themselves when you pass them to the Python print function:
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> print(lldb.debugger)
Debugger (instance: "debugger_1", id: 1)
>>> print(lldb.target)
a.out
>>> print(lldb.process)
SBProcess: pid = 58842, state = stopped, threads = 1, executable = a.out
>>> print(lldb.thread)
thread #1: tid = 0x2265ce3, 0x0000000100000334 a.out`main at t.c:2:3, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
>>> print(lldb.frame)
frame #0: 0x0000000100000334 a.out`main at t.c:2:3