Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
| 2 | <html xmlns="http://www.w3.org/1999/xhtml">
|
| 3 | <head>
|
| 4 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
| 5 | <link href="style.css" rel="stylesheet" type="text/css" />
|
Greg Clayton | f3edcc0 | 2012-01-26 00:32:22 +0000 | [diff] [blame] | 6 | <title>LLDB Python Reference</title>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 7 | </head>
|
| 8 |
|
| 9 | <body>
|
| 10 | <div class="www_title">
|
Greg Clayton | f3edcc0 | 2012-01-26 00:32:22 +0000 | [diff] [blame] | 11 | LLDB Python Reference
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 12 | </div>
|
| 13 |
|
| 14 | <div id="container">
|
| 15 | <div id="content">
|
| 16 | <!--#include virtual="sidebar.incl"-->
|
| 17 | <div id="middle">
|
| 18 | <div class="post">
|
| 19 | <h1 class ="postheader">Introduction</h1>
|
| 20 | <div class="postcontent">
|
| 21 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 22 | <p>The entire LLDB API is available as Python functions through a script bridging interface.
|
| 23 | This means the LLDB API's can be used directly from python either interactively or to build python apps that
|
| 24 | provide debugger features. </p>
|
| 25 | <p>Additionally, Python can be used as a programmatic interface within the
|
| 26 | lldb command interpreter (we refer to this for brevity as the embedded interpreter). Of course,
|
| 27 | in this context it has full access to the LLDB API - with some additional conveniences we will
|
| 28 | call out in the FAQ.</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 29 |
|
| 30 | </div>
|
| 31 | <div class="postfooter"></div>
|
Greg Clayton | d47c792 | 2012-06-29 16:25:05 +0000 | [diff] [blame] | 32 | <div class="post">
|
| 33 | <h1 class ="postheader">Documentation</h1>
|
| 34 | <div class="postcontent">
|
| 35 |
|
| 36 | <p>The LLDB API is contained in a python module named <b>lldb</b>. Help is available through the standard python help and documentation. To get an overview of the <b>lldb</b> python module you can execute the following command:</p>
|
| 37 | <code><pre><tt>(lldb) <b>script help(lldb)</b>
|
| 38 | Help on package lldb:
|
| 39 |
|
| 40 | NAME
|
| 41 | lldb - The lldb module contains the public APIs for Python binding.
|
| 42 |
|
| 43 | FILE
|
| 44 | /System/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/Python/lldb/__init__.py
|
| 45 |
|
| 46 | DESCRIPTION
|
| 47 | ...
|
| 48 | </tt></pre></code>
|
| 49 | <p>You can also get help using a module class name. The full API that is exposed for that class will be displayed in a man page style window. Below we want to get help on the lldb.SBFrame class:</p>
|
| 50 | <code><pre><tt>(lldb) <b>script help(lldb.SBFrame)</b>
|
| 51 | Help on class SBFrame in module lldb:
|
| 52 |
|
| 53 | class SBFrame(__builtin__.object)
|
| 54 | | Represents one of the stack frames associated with a thread.
|
| 55 | | SBThread contains SBFrame(s). For example (from test/lldbutil.py),
|
| 56 | |
|
| 57 | | def print_stacktrace(thread, string_buffer = False):
|
| 58 | | '''Prints a simple stack trace of this thread.'''
|
| 59 | |
|
| 60 | ...
|
| 61 | </tt></pre></code>
|
| 62 | <p>Or you can get help using any python object, here we use the <b>lldb.process</b> object which is a global variable in the <b>lldb</b> module which represents the currently selected process:</p>
|
| 63 | <code><pre><tt>(lldb) <b>script help(lldb.process)</b>
|
| 64 | Help on SBProcess in module lldb object:
|
| 65 |
|
| 66 | class SBProcess(__builtin__.object)
|
| 67 | | Represents the process associated with the target program.
|
| 68 | |
|
| 69 | | SBProcess supports thread iteration. For example (from test/lldbutil.py),
|
| 70 | |
|
| 71 | | # ==================================================
|
| 72 | | # Utility functions related to Threads and Processes
|
| 73 | | # ==================================================
|
| 74 | |
|
| 75 | ...
|
| 76 | </tt></pre></code>
|
| 77 |
|
| 78 | </div>
|
| 79 | <div class="postfooter"></div>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 80 |
|
| 81 | <div class="post">
|
| 82 | <h1 class ="postheader">Embedded Python Interpreter</h1>
|
| 83 | <div class="postcontent">
|
| 84 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 85 | <p>The embedded python interpreter can be accessed in a variety of ways from within LLDB. The
|
| 86 | easiest way is to use the lldb command <b>script</b> with no arguments at the lldb command prompt:</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 87 | <code><pre><tt>(lldb) <strong>script</strong>
|
| 88 | Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
|
| 89 | >>> 2+3
|
| 90 | 5
|
| 91 | >>> hex(12345)
|
| 92 | '0x3039'
|
| 93 | >>>
|
| 94 | </tt></pre></code>
|
| 95 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 96 | <p>This drops you into the embedded python interpreter. When running under the <b>script</b> command,
|
| 97 | lldb sets some convenience variables that give you quick access to the currently selected entities that characterize
|
| 98 | the program and debugger state. In each case, if there is no currently selected entity of the appropriate
|
| 99 | type, the variable's <b>IsValid</b> method will return false.
|
| 100 | <p>Note also, these variables hold the values
|
| 101 | of the selected objects on entry to the embedded interpreter. They do not update as you use the LLDB
|
| 102 | API's to change, for example, the currently selected stack frame or thread.</p>
|
| 103 | These are all global variables contained in the <b>lldb</b> python namespace :</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 104 | <table class="stats" width="620" cellspacing="0">
|
| 105 | <tr>
|
| 106 | <td class="hed" width="20%">Variable</td>
|
| 107 | <td class="hed" width="10%">Type</td>
|
| 108 | <td class="hed" width="70%">Description</td>
|
| 109 | </tr>
|
| 110 |
|
| 111 | <tr>
|
| 112 | <td class="content">
|
| 113 | <b>lldb.debugger</b>
|
| 114 | </td>
|
| 115 | <td class="content">
|
| 116 | <b>lldb.SBDebugger</b>
|
| 117 | </td>
|
| 118 | <td class="content">
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 119 | Contains the debugger object whose <b>script</b> command was invoked.
|
| 120 | The <b>lldb.SBDebugger</b> object owns the command interpreter
|
| 121 | and all the targets in your debug session. There will always be a
|
| 122 | Debugger in the embedded interpreter.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 123 | </td>
|
| 124 | </tr>
|
| 125 | <tr>
|
| 126 | <td class="content">
|
| 127 | <b>lldb.target</b>
|
| 128 | </td>
|
| 129 | <td class="content">
|
| 130 | <b>lldb.SBTarget</b>
|
| 131 | </td>
|
| 132 | <td class="content">
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 133 | Contains the currently selected target - for instance the one made with the
|
| 134 | <b>file</b> or selected by the <b>target select <target-index></b> command.
|
| 135 | The <b>lldb.SBTarget</b> manages one running process, and all the executable
|
| 136 | and debug files for the process.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 137 | </td>
|
| 138 | </tr>
|
| 139 | <tr>
|
| 140 | <td class="content">
|
| 141 | <b>lldb.process</b>
|
| 142 | </td>
|
| 143 | <td class="content">
|
| 144 | <b>lldb.SBProcess</b>
|
| 145 | </td>
|
| 146 | <td class="content">
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 147 | Contains the process of the currently selected target.
|
| 148 | The <b>lldb.SBProcess</b> object manages the threads and allows access to
|
| 149 | memory for the process.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 150 | </td>
|
| 151 | </tr>
|
| 152 | <tr>
|
| 153 | <td class="content">
|
| 154 | <b>lldb.thread</b>
|
| 155 | </td>
|
| 156 | <td class="content">
|
| 157 | <b>lldb.SBThread</b>
|
| 158 | </td>
|
| 159 | <td class="content">
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 160 | Contains the currently selected thread.
|
| 161 | The <b>lldb.SBThread</b> object manages the stack frames in that thread.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 162 | A thread is always selected in the command interpreter when a target stops.
|
| 163 | The <b>thread select <thread-index></b> commmand can be used to change the
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 164 | currently selected thread. So as long as you have a stopped process, there will be
|
| 165 | some selected thread.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 166 | </td>
|
| 167 | </tr>
|
| 168 | <tr>
|
| 169 | <td class="content">
|
| 170 | <b>lldb.frame</b>
|
| 171 | </td>
|
| 172 | <td class="content">
|
| 173 | <b>lldb.SBFrame</b>
|
| 174 | </td>
|
| 175 | <td class="content">
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 176 | Contains the currently selected stack frame.
|
| 177 | The <b>lldb.SBFrame</b> object manage the stack locals and the register set for
|
| 178 | that stack.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 179 | A stack frame is always selected in the command interpreter when a target stops.
|
| 180 | The <b>frame select <frame-index></b> commmand can be used to change the
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 181 | currently selected frame. So as long as you have a stopped process, there will
|
| 182 | be some selected frame.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 183 | </td>
|
| 184 | </tr>
|
| 185 | </table>
|
| 186 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 187 | <p>Once in the embedded interpreter, these objects can be used. To get started, note that almost
|
| 188 | all of the <b>lldb</b> Python objects are able to briefly describe themselves when you pass them
|
| 189 | to the Python <b>print</b> function:
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 190 | <code><pre><tt>(lldb) <b>script</b>
|
| 191 | Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
|
| 192 | >>> <strong>print lldb.debugger</strong>
|
| 193 | Debugger (instance: "debugger_1", id: 1)
|
| 194 | >>> <strong>print lldb.target</strong>
|
| 195 | a.out
|
| 196 | >>> <strong>print lldb.process</strong>
|
| 197 | SBProcess: pid = 59289, state = stopped, threads = 1, executable = a.out
|
| 198 | >>> <strong>print lldb.thread</strong>
|
| 199 | SBThread: tid = 0x1f03
|
| 200 | >>> <strong>print lldb.frame</strong>
|
| 201 | frame #0: 0x0000000100000bb6 a.out main + 54 at main.c:16
|
| 202 | </tt></pre></code>
|
| 203 |
|
| 204 | </div>
|
| 205 | <div class="postfooter"></div>
|
| 206 |
|
| 207 | </div>
|
| 208 | <div class="post">
|
| 209 | <h1 class ="postheader">Running a Python script when a breakpoint gets hit</h1>
|
| 210 | <div class="postcontent">
|
| 211 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 212 | <p>One very powerful use of the lldb Python API is to have a python script run when a breakpoint gets hit. Adding python
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 213 | scripts to breakpoints provides a way to create complex breakpoint
|
| 214 | conditions and also allows for smart logging and data gathering.</p>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 215 | <p>When your process hits a breakpoint to which you have attached some python code, the code is executed as the
|
| 216 | body of a function which takes two arguments:</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 217 | <p>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 218 | <code><pre><tt>def breakpoint_function_wrapper(<b>frame</b>, <b>bp_loc</b>):
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 219 | <font color=green># Your code goes here</font>
|
| 220 | </tt></pre></code>
|
| 221 | <p><table class="stats" width="620" cellspacing="0">
|
| 222 | <tr>
|
| 223 | <td class="hed" width="10%">Argument</td>
|
| 224 | <td class="hed" width="10%">Type</td>
|
| 225 | <td class="hed" width="80%">Description</td>
|
| 226 | </tr>
|
| 227 |
|
| 228 | <tr>
|
| 229 | <td class="content">
|
| 230 | <b>frame</b>
|
| 231 | </td>
|
| 232 | <td class="content">
|
| 233 | <b>lldb.SBFrame</b>
|
| 234 | </td>
|
| 235 | <td class="content">
|
| 236 | The current stack frame where the breakpoint got hit.
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 237 | The object will always be valid.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 238 | This <b>frame</b> argument might <i>not</i> match the currently selected stack frame found in the <b>lldb</b> module global variable <b>lldb.frame</b>.
|
| 239 | </td>
|
| 240 | </tr>
|
| 241 | <tr>
|
| 242 | <td class="content">
|
| 243 | <b>bp_loc</b>
|
| 244 | </td>
|
| 245 | <td class="content">
|
| 246 | <b>lldb.SBBreakpointLocation</b>
|
| 247 | </td>
|
| 248 | <td class="content">
|
| 249 | The breakpoint location that just got hit. Breakpoints are represented by <b>lldb.SBBreakpoint</b>
|
| 250 | objects. These breakpoint objects can have one or more locations. These locations
|
| 251 | are represented by <b>lldb.SBBreakpointLocation</b> objects.
|
| 252 | </td>
|
| 253 | </tr>
|
| 254 | </table>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 255 | <p>An example will show how simple it is to write some python code and attach it to a breakpoint.
|
| 256 | The following example will allow you to track the order in which the functions in a given shared library
|
| 257 | are first executed during one run of your program. This is a simple method to gather an order file which
|
| 258 | can be used to optimize function placement within a binary for execution locality.</p>
|
| 259 | <p>We do this by setting a regular expression breakpoint
|
| 260 | that will match every function in the shared library. The regular expression '.' will match
|
| 261 | any string that has at least one character in it, so we will use that.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 262 | This will result in one <b>lldb.SBBreakpoint</b> object
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 263 | that contains an <b>lldb.SBBreakpointLocation</b> object for each function. As the breakpoint gets
|
| 264 | hit, we use a counter to track the order in which the function at this particular breakpoint location got hit.
|
| 265 | Since our code is passed the location that was hit, we can get the name of the function from the location,
|
| 266 | disable the location so we won't count this function again; then log some info and continue the process.</p>
|
| 267 | <p>Note we also have to initialize our counter, which we do with the simple one-line version of the <b>script</b>
|
| 268 | command.
|
| 269 | <p>Here is the code:
|
| 270 |
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 271 | <code><pre><tt>(lldb) <strong>breakpoint set --func-regex=. --shlib=libfoo.dylib</strong>
|
| 272 | Breakpoint created: 1: regex = '.', module = libfoo.dylib, locations = 223
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 273 | (lldb) <strong>script counter = 0</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 274 | (lldb) <strong>breakpoint command add --script-type python 1</strong>
|
| 275 | Enter your Python command(s). Type 'DONE' to end.
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 276 | > <font color=green># Increment our counter. Since we are in a function, this must be a global python variable</font>
|
| 277 | > <strong>global counter</strong>
|
| 278 | > <strong>counter += 1</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 279 | > <font color=green># Get the name of the function</font>
|
| 280 | > <strong>name = frame.GetFunctionName()</strong>
|
| 281 | > <font color=green># Print the order and the function name</font>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 282 | > <strong>print '[%i] %s' % (counter, name)</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 283 | > <font color=green># Disable the current breakpoint location so it doesn't get hit again</font>
|
| 284 | > <strong>bp_loc.SetEnabled(False)</strong>
|
| 285 | > <font color=green># How continue the process</font>
|
| 286 | > <strong>frame.GetThread().GetProcess().Continue()</strong>
|
| 287 | > <strong>DONE</strong>
|
| 288 | </tt></pre></code>
|
| 289 | <p>The <b>breakpoint command add</b> command above attaches a python script to breakpoint 1.
|
| 290 | To remove the breakpoint command:
|
| 291 | <p><code>(lldb) <strong>breakpoint command delete 1</strong></code>
|
| 292 | </div>
|
| 293 | </div>
|
| 294 | <div class="post">
|
| 295 | <h1 class ="postheader">Create a new LLDB command using a python function</h1>
|
| 296 | <div class="postcontent">
|
| 297 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 298 | <p>Python functions can be used to create new LLDB command interpreter commands, which will work
|
| 299 | like all the natively defined lldb commands. This provides a very flexible and easy way to extend LLDB to meet your
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 300 | debugging requirements. </p>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 301 | <p>To write a python function that implements a new LDB command define the function to take four arguments as follows:</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 302 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 303 | <code><pre><tt>def command_function(<b>debugger</b>, <b>command</b>, <b>result</b>, <b>internal_dict</b>):
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 304 | <font color=green># Your code goes here</font>
|
| 305 | </tt></pre></code>
|
Enrico Granata | c0791aa | 2012-05-02 21:00:41 +0000 | [diff] [blame] | 306 |
|
| 307 | Optionally, you can also provide a Python docstring, and LLDB will use it when providing help for your command, as in:
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 308 | <code><pre><tt>def command_function(<b>debugger</b>, <b>command</b>, <b>result</b>, <b>internal_dict</b>):
|
Enrico Granata | c0791aa | 2012-05-02 21:00:41 +0000 | [diff] [blame] | 309 | <font color=green>"""This command takes a lot of options and does many fancy things"""</font>
|
| 310 | <font color=green># Your code goes here</font>
|
| 311 | </tt></pre></code>
|
| 312 |
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 313 | <p><table class="stats" width="620" cellspacing="0">
|
| 314 | <tr>
|
| 315 | <td class="hed" width="10%">Argument</td>
|
| 316 | <td class="hed" width="10%">Type</td>
|
| 317 | <td class="hed" width="80%">Description</td>
|
| 318 | </tr>
|
| 319 |
|
| 320 | <tr>
|
| 321 | <td class="content">
|
| 322 | <b>debugger</b>
|
| 323 | </td>
|
| 324 | <td class="content">
|
| 325 | <b>lldb.SBDebugger</b>
|
| 326 | </td>
|
| 327 | <td class="content">
|
| 328 | The current debugger object.
|
| 329 | </td>
|
| 330 | </tr>
|
| 331 | <tr>
|
| 332 | <td class="content">
|
| 333 | <b>command</b>
|
| 334 | </td>
|
| 335 | <td class="content">
|
| 336 | <b>python string</b>
|
| 337 | </td>
|
| 338 | <td class="content">
|
| 339 | A python string containing all arguments for your command. If you need to chop up the arguments
|
| 340 | try using the <b>shlex</b> module's <code>shlex.split(command)</code> to properly extract the
|
| 341 | arguments.
|
| 342 | </td>
|
| 343 | </tr>
|
| 344 | <tr>
|
| 345 | <td class="content">
|
| 346 | <b>result</b>
|
| 347 | </td>
|
| 348 | <td class="content">
|
| 349 | <b>lldb.SBCommandReturnObject</b>
|
| 350 | </td>
|
| 351 | <td class="content">
|
| 352 | A return object where you can indicate the success or failure of your command. You can also
|
| 353 | provide information for the command result by printing data into it. You can also just print
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 354 | data as you normally would in a python script and the output will show up; this is useful for
|
| 355 | logging, but the real output for your command should go in the result object.
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 356 | </td>
|
| 357 | </tr>
|
| 358 | <tr>
|
| 359 | <td class="content">
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 360 | <b>internal_dict</b>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 361 | </td>
|
| 362 | <td class="content">
|
| 363 | <b>python dict object</b>
|
| 364 | </td>
|
| 365 | <td class="content">
|
| 366 | The dictionary for the current embedded script session which contains all variables
|
| 367 | and functions.
|
| 368 | </td>
|
| 369 | </tr>
|
| 370 | </table>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 371 | <p>One other handy convenience when defining lldb command-line commands is the command
|
| 372 | <b>command script import</b> which will import a module specified by file path - so you
|
| 373 | don't have to change your PYTHONPATH for temporary scripts. It also has another convenience
|
| 374 | that if your new script module has a function of the form:</p>
|
| 375 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 376 | <code><pre><tt>def __lldb_init_module(<b>debugger</b>, <b>internal_dict</b>):
|
Greg Clayton | 261c974 | 2012-01-26 05:36:07 +0000 | [diff] [blame] | 377 | <font color=green># Command Initialization code goes here</font>
|
| 378 | </tt></pre></code>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 379 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 380 | <p>where <b>debugger</b> and <b>internal_dict</b> are as above, that function will get run when the module is loaded
|
Greg Clayton | 261c974 | 2012-01-26 05:36:07 +0000 | [diff] [blame] | 381 | allowing you to add whatever commands you want into the current debugger. Note that
|
| 382 | this function will only be run when using the LLDB comand <b>command script import</b>,
|
| 383 | it will not get run if anyone imports your module from another module.
|
| 384 | If you want to always run code when your module is loaded from LLDB
|
| 385 | <u>or</u> when loaded via an <b>import</b> statement in python code
|
| 386 | you can test the <b>lldb.debugger</b> object, since you imported the
|
| 387 | <lldb> module at the top of the python <b>ls.py</b> module. This test
|
| 388 | must be in code that isn't contained inside of any function or class,
|
| 389 | just like the standard test for <b>__main__</b> like all python modules
|
| 390 | usally do. Sample code would look like:
|
| 391 |
|
| 392 | <code><pre><tt>if __name__ == '__main__':
|
| 393 | <font color=green># Create a new debugger instance in your module if your module
|
| 394 | # can be run from the command line. When we run a script from
|
| 395 | # the command line, we won't have any debugger object in
|
| 396 | # lldb.debugger, so we can just create it if it will be needed</font>
|
| 397 | lldb.debugger = lldb.SBDebugger.Create()
|
| 398 | elif lldb.debugger:
|
| 399 | <font color=green># Module is being run inside the LLDB interpreter</font>
|
| 400 | lldb.debugger.HandleCommand('command script add -f ls.ls ls')
|
| 401 | print 'The "ls" python command has been installed and is ready for use.'
|
| 402 | </tt></pre></code>
|
| 403 | <p>Now we can create a module called <b>ls.py</b> in the file <b>~/ls.py</b> that will implement a function that
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 404 | can be used by LLDB's python command code:</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 405 |
|
| 406 | <code><pre><tt><font color=green>#!/usr/bin/python</font>
|
| 407 |
|
| 408 | import lldb
|
| 409 | import commands
|
| 410 | import optparse
|
| 411 | import shlex
|
| 412 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 413 | def ls(debugger, command, result, internal_dict):
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 414 | result.PutCString(commands.getoutput('/bin/ls %s' % command))
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 415 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 416 | <font color=green># And the initialization code to add your commands </font>
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 417 | def __lldb_init_module(debugger, internal_dict):
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 418 | debugger.HandleCommand('command script add -f ls.ls ls')
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 419 | print 'The "ls" python command has been installed and is ready for use.'
|
| 420 | </tt></pre></code>
|
| 421 | <p>Now we can load the module into LLDB and use it</p>
|
| 422 | <code><pre><tt>% lldb
|
Greg Clayton | 261c974 | 2012-01-26 05:36:07 +0000 | [diff] [blame] | 423 | (lldb) <strong>command script import ~/ls.py</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 424 | The "ls" python command has been installed and is ready for use.
|
| 425 | (lldb) <strong>ls -l /tmp/</strong>
|
| 426 | total 365848
|
| 427 | -rw-r--r--@ 1 someuser wheel 6148 Jan 19 17:27 .DS_Store
|
| 428 | -rw------- 1 someuser wheel 7331 Jan 19 15:37 crash.log
|
| 429 | </tt></pre></code>
|
| 430 | <p>A template has been created in the source repository that can help you to create
|
| 431 | lldb command quickly:</p>
|
| 432 | <a href="http://llvm.org/svn/llvm-project/lldb/trunk/examples/python/cmdtemplate.py">cmdtemplate.py</a>
|
Enrico Granata | 74566c9 | 2012-08-24 21:20:14 +0000 | [diff] [blame^] | 433 | <p>
|
| 434 | A commonly required facility is being able to create a command that does some token substitution, and then runs a different debugger command
|
| 435 | (usually, it po'es the result of an expression evaluated on its argument). For instance, given the following program:
|
| 436 | <code><pre><tt>
|
| 437 | #import <Foundation/Foundation.h>
|
| 438 | NSString*
|
| 439 | ModifyString(NSString* src)
|
| 440 | {
|
| 441 | return [src stringByAppendingString:@"foobar"];
|
| 442 | }
|
| 443 |
|
| 444 | int main()
|
| 445 | {
|
| 446 | NSString* aString = @"Hello world";
|
| 447 | NSString* anotherString = @"Let's be friends";
|
| 448 | return 1;
|
| 449 | }
|
| 450 | </tt></pre></code>
|
| 451 | you may want a pofoo X command, that equates po [ModifyString(X) capitalizedString].
|
| 452 | The following debugger interaction shows how to achieve that goal:
|
| 453 | <code><pre><tt>
|
| 454 | (lldb) <b>script</b>
|
| 455 | Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
|
| 456 | >>> <b>def pofoo_funct(debugger, command, result, internal_dict):</b>
|
| 457 | ... <b>cmd = "po [ModifyString(" + command + ") capitalizedString]"</b>
|
| 458 | ... <b>lldb.debugger.HandleCommand(cmd)</b>
|
| 459 | ...
|
| 460 | >>> ^D
|
| 461 | (lldb) <b>command script add pofoo -f pofoo_funct</b>
|
| 462 | (lldb) <b>pofoo aString</b>
|
| 463 | $1 = 0x000000010010aa00 Hello Worldfoobar
|
| 464 | (lldb) <b>pofoo anotherString</b>
|
| 465 | $2 = 0x000000010010aba0 Let's Be Friendsfoobar</tt></pre></code>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 466 | </div>
|
| 467 | <div class="post">
|
| 468 | <h1 class ="postheader">Using the lldb.py module in python</h1>
|
| 469 | <div class="postcontent">
|
| 470 |
|
| 471 | <p>LLDB has all of its core code build into a shared library which gets
|
| 472 | used by the <b>lldb</b> command line application. On Mac OS X this
|
| 473 | shared library is a framework: <b>LLDB.framework</b> and on other
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 474 | unix variants the program is a shared library: <b>lldb.so</b>. LLDB also
|
| 475 | provides an lldb.py module that contains the bindings from LLDB into Python.
|
| 476 | To use the
|
| 477 | <b>LLDB.framework</b> to create your own stand-alone python programs, you will
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 478 | need to tell python where to look in order to find this module. This
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 479 | is done by setting the <b>PYTHONPATH</b> environment variable, adding
|
| 480 | a path to the directory that contains the <b>lldb.py</b> python module. On
|
| 481 | Mac OS X, this is contained inside the LLDB.framework, so you would do:
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 482 |
|
| 483 | <p>For csh and tcsh:</p>
|
| 484 | <p><code>% <b>setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
|
| 485 | <p>For sh and bash:
|
| 486 | <p><code>% <b>export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
|
| 487 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 488 | <p> Alternately, you can append the LLDB Python directory to the <b>sys.path</b> list directly in
|
| 489 | your Python code before importing the lldb module.</p>
|
| 490 |
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 491 | <p>
|
| 492 | Now your python scripts are ready to import the lldb module. Below is a
|
| 493 | python script that will launch a program from the current working directory
|
| 494 | called "a.out", set a breakpoint at "main", and then run and hit the breakpoint,
|
| 495 | and print the process, thread and frame objects if the process stopped:
|
| 496 |
|
| 497 | </p>
|
| 498 | <code><pre><tt><font color=green>#!/usr/bin/python</font>
|
| 499 |
|
| 500 | import lldb
|
| 501 |
|
| 502 | <font color=green># Set the path to the executable to debug</font>
|
| 503 | exe = "./a.out"
|
| 504 |
|
| 505 | <font color=green># Create a new debugger instance</font>
|
| 506 | debugger = lldb.SBDebugger.Create()
|
| 507 |
|
| 508 | <font color=green># When we step or continue, don't return from the function until the process
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 509 | # stops. Otherwise we would have to handle the process events ourselves which, while doable is
|
| 510 | #a little tricky. We do this by setting the async mode to false.</font>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 511 | debugger.SetAsync (False)
|
| 512 |
|
| 513 | <font color=green># Create a target from a file and arch</font>
|
| 514 | print "Creating a target for '%s'" % exe
|
| 515 |
|
| 516 | target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
|
| 517 |
|
| 518 | if target:
|
| 519 | <font color=green># If the target is valid set a breakpoint at main</font>
|
| 520 | main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
|
| 521 |
|
| 522 | print main_bp
|
| 523 |
|
| 524 | <font color=green># Launch the process. Since we specified synchronous mode, we won't return
|
| 525 | # from this function until we hit the breakpoint at main</font>
|
| 526 | process = target.LaunchSimple (None, None, os.getcwd())
|
| 527 |
|
| 528 | <font color=green># Make sure the launch went ok</font>
|
| 529 | if process:
|
| 530 | <font color=green># Print some simple process info</font>
|
| 531 | state = process.GetState ()
|
| 532 | print process
|
| 533 | if state == lldb.eStateStopped:
|
| 534 | <font color=green># Get the first thread</font>
|
| 535 | thread = process.GetThreadAtIndex (0)
|
| 536 | if thread:
|
| 537 | <font color=green># Print some simple thread info</font>
|
| 538 | print thread
|
| 539 | <font color=green># Get the first frame</font>
|
| 540 | frame = thread.GetFrameAtIndex (0)
|
| 541 | if frame:
|
| 542 | <font color=green># Print some simple frame info</font>
|
| 543 | print frame
|
| 544 | function = frame.GetFunction()
|
| 545 | <font color=green># See if we have debug info (a function)</font>
|
| 546 | if function:
|
| 547 | <font color=green># We do have a function, print some info for the function</font>
|
| 548 | print function
|
| 549 | <font color=green># Now get all instructions for this function and print them</font>
|
| 550 | insts = function.GetInstructions(target)
|
| 551 | disassemble_instructions (insts)
|
| 552 | else:
|
| 553 | <font color=green># See if we have a symbol in the symbol table for where we stopped</font>
|
| 554 | symbol = frame.GetSymbol();
|
| 555 | if symbol:
|
| 556 | <font color=green># We do have a symbol, print some info for the symbol</font>
|
| 557 | print symbol
|
| 558 | </tt></pre></code>
|
| 559 | </div>
|
| 560 | <div class="postfooter"></div>
|
| 561 |
|
| 562 | </div>
|
| 563 | </div>
|
| 564 | </div>
|
| 565 | </body>
|
| 566 | </html>
|