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
|
Greg Clayton | b34ca85 | 2012-10-26 17:53:21 +0000 | [diff] [blame^] | 216 | body of a function which takes three arguments:</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 217 | <p>
|
Greg Clayton | b34ca85 | 2012-10-26 17:53:21 +0000 | [diff] [blame^] | 218 | <code><pre><tt>def breakpoint_function_wrapper(<b>frame</b>, <b>bp_loc</b>, <b>dict</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>
|
Greg Clayton | b34ca85 | 2012-10-26 17:53:21 +0000 | [diff] [blame^] | 254 | <tr>
|
| 255 | <td class="content">
|
| 256 | <b>dict</b>
|
| 257 | </td>
|
| 258 | <td class="content">
|
| 259 | <b>dict</b>
|
| 260 | </td>
|
| 261 | <td class="content">
|
| 262 | The python session dictionary as a standard python dictionary object.
|
| 263 | </td>
|
| 264 | </tr>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 265 | </table>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 266 | <p>An example will show how simple it is to write some python code and attach it to a breakpoint.
|
| 267 | The following example will allow you to track the order in which the functions in a given shared library
|
| 268 | are first executed during one run of your program. This is a simple method to gather an order file which
|
| 269 | can be used to optimize function placement within a binary for execution locality.</p>
|
| 270 | <p>We do this by setting a regular expression breakpoint
|
| 271 | that will match every function in the shared library. The regular expression '.' will match
|
| 272 | 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] | 273 | This will result in one <b>lldb.SBBreakpoint</b> object
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 274 | that contains an <b>lldb.SBBreakpointLocation</b> object for each function. As the breakpoint gets
|
| 275 | hit, we use a counter to track the order in which the function at this particular breakpoint location got hit.
|
| 276 | Since our code is passed the location that was hit, we can get the name of the function from the location,
|
| 277 | disable the location so we won't count this function again; then log some info and continue the process.</p>
|
| 278 | <p>Note we also have to initialize our counter, which we do with the simple one-line version of the <b>script</b>
|
| 279 | command.
|
| 280 | <p>Here is the code:
|
| 281 |
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 282 | <code><pre><tt>(lldb) <strong>breakpoint set --func-regex=. --shlib=libfoo.dylib</strong>
|
| 283 | Breakpoint created: 1: regex = '.', module = libfoo.dylib, locations = 223
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 284 | (lldb) <strong>script counter = 0</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 285 | (lldb) <strong>breakpoint command add --script-type python 1</strong>
|
| 286 | Enter your Python command(s). Type 'DONE' to end.
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 287 | > <font color=green># Increment our counter. Since we are in a function, this must be a global python variable</font>
|
| 288 | > <strong>global counter</strong>
|
| 289 | > <strong>counter += 1</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 290 | > <font color=green># Get the name of the function</font>
|
| 291 | > <strong>name = frame.GetFunctionName()</strong>
|
| 292 | > <font color=green># Print the order and the function name</font>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 293 | > <strong>print '[%i] %s' % (counter, name)</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 294 | > <font color=green># Disable the current breakpoint location so it doesn't get hit again</font>
|
| 295 | > <strong>bp_loc.SetEnabled(False)</strong>
|
| 296 | > <font color=green># How continue the process</font>
|
| 297 | > <strong>frame.GetThread().GetProcess().Continue()</strong>
|
| 298 | > <strong>DONE</strong>
|
| 299 | </tt></pre></code>
|
| 300 | <p>The <b>breakpoint command add</b> command above attaches a python script to breakpoint 1.
|
| 301 | To remove the breakpoint command:
|
| 302 | <p><code>(lldb) <strong>breakpoint command delete 1</strong></code>
|
| 303 | </div>
|
| 304 | </div>
|
| 305 | <div class="post">
|
| 306 | <h1 class ="postheader">Create a new LLDB command using a python function</h1>
|
| 307 | <div class="postcontent">
|
| 308 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 309 | <p>Python functions can be used to create new LLDB command interpreter commands, which will work
|
| 310 | 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] | 311 | debugging requirements. </p>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 312 | <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] | 313 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 314 | <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] | 315 | <font color=green># Your code goes here</font>
|
| 316 | </tt></pre></code>
|
Enrico Granata | c0791aa | 2012-05-02 21:00:41 +0000 | [diff] [blame] | 317 |
|
| 318 | 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] | 319 | <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] | 320 | <font color=green>"""This command takes a lot of options and does many fancy things"""</font>
|
| 321 | <font color=green># Your code goes here</font>
|
| 322 | </tt></pre></code>
|
| 323 |
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 324 | <p><table class="stats" width="620" cellspacing="0">
|
| 325 | <tr>
|
| 326 | <td class="hed" width="10%">Argument</td>
|
| 327 | <td class="hed" width="10%">Type</td>
|
| 328 | <td class="hed" width="80%">Description</td>
|
| 329 | </tr>
|
| 330 |
|
| 331 | <tr>
|
| 332 | <td class="content">
|
| 333 | <b>debugger</b>
|
| 334 | </td>
|
| 335 | <td class="content">
|
| 336 | <b>lldb.SBDebugger</b>
|
| 337 | </td>
|
| 338 | <td class="content">
|
| 339 | The current debugger object.
|
| 340 | </td>
|
| 341 | </tr>
|
| 342 | <tr>
|
| 343 | <td class="content">
|
| 344 | <b>command</b>
|
| 345 | </td>
|
| 346 | <td class="content">
|
| 347 | <b>python string</b>
|
| 348 | </td>
|
| 349 | <td class="content">
|
| 350 | A python string containing all arguments for your command. If you need to chop up the arguments
|
| 351 | try using the <b>shlex</b> module's <code>shlex.split(command)</code> to properly extract the
|
| 352 | arguments.
|
| 353 | </td>
|
| 354 | </tr>
|
| 355 | <tr>
|
| 356 | <td class="content">
|
| 357 | <b>result</b>
|
| 358 | </td>
|
| 359 | <td class="content">
|
| 360 | <b>lldb.SBCommandReturnObject</b>
|
| 361 | </td>
|
| 362 | <td class="content">
|
| 363 | A return object where you can indicate the success or failure of your command. You can also
|
| 364 | 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] | 365 | data as you normally would in a python script and the output will show up; this is useful for
|
| 366 | 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] | 367 | </td>
|
| 368 | </tr>
|
| 369 | <tr>
|
| 370 | <td class="content">
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 371 | <b>internal_dict</b>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 372 | </td>
|
| 373 | <td class="content">
|
| 374 | <b>python dict object</b>
|
| 375 | </td>
|
| 376 | <td class="content">
|
| 377 | The dictionary for the current embedded script session which contains all variables
|
| 378 | and functions.
|
| 379 | </td>
|
| 380 | </tr>
|
| 381 | </table>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 382 | <p>One other handy convenience when defining lldb command-line commands is the command
|
| 383 | <b>command script import</b> which will import a module specified by file path - so you
|
| 384 | don't have to change your PYTHONPATH for temporary scripts. It also has another convenience
|
| 385 | that if your new script module has a function of the form:</p>
|
| 386 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 387 | <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] | 388 | <font color=green># Command Initialization code goes here</font>
|
| 389 | </tt></pre></code>
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 390 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 391 | <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] | 392 | allowing you to add whatever commands you want into the current debugger. Note that
|
| 393 | this function will only be run when using the LLDB comand <b>command script import</b>,
|
| 394 | it will not get run if anyone imports your module from another module.
|
| 395 | If you want to always run code when your module is loaded from LLDB
|
| 396 | <u>or</u> when loaded via an <b>import</b> statement in python code
|
| 397 | you can test the <b>lldb.debugger</b> object, since you imported the
|
| 398 | <lldb> module at the top of the python <b>ls.py</b> module. This test
|
| 399 | must be in code that isn't contained inside of any function or class,
|
| 400 | just like the standard test for <b>__main__</b> like all python modules
|
| 401 | usally do. Sample code would look like:
|
| 402 |
|
| 403 | <code><pre><tt>if __name__ == '__main__':
|
| 404 | <font color=green># Create a new debugger instance in your module if your module
|
| 405 | # can be run from the command line. When we run a script from
|
| 406 | # the command line, we won't have any debugger object in
|
| 407 | # lldb.debugger, so we can just create it if it will be needed</font>
|
| 408 | lldb.debugger = lldb.SBDebugger.Create()
|
| 409 | elif lldb.debugger:
|
| 410 | <font color=green># Module is being run inside the LLDB interpreter</font>
|
| 411 | lldb.debugger.HandleCommand('command script add -f ls.ls ls')
|
| 412 | print 'The "ls" python command has been installed and is ready for use.'
|
| 413 | </tt></pre></code>
|
| 414 | <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] | 415 | can be used by LLDB's python command code:</p>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 416 |
|
| 417 | <code><pre><tt><font color=green>#!/usr/bin/python</font>
|
| 418 |
|
| 419 | import lldb
|
| 420 | import commands
|
| 421 | import optparse
|
| 422 | import shlex
|
| 423 |
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 424 | def ls(debugger, command, result, internal_dict):
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 425 | result.PutCString(commands.getoutput('/bin/ls %s' % command))
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 426 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 427 | <font color=green># And the initialization code to add your commands </font>
|
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 428 | def __lldb_init_module(debugger, internal_dict):
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 429 | debugger.HandleCommand('command script add -f ls.ls ls')
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 430 | print 'The "ls" python command has been installed and is ready for use.'
|
| 431 | </tt></pre></code>
|
| 432 | <p>Now we can load the module into LLDB and use it</p>
|
| 433 | <code><pre><tt>% lldb
|
Greg Clayton | 261c974 | 2012-01-26 05:36:07 +0000 | [diff] [blame] | 434 | (lldb) <strong>command script import ~/ls.py</strong>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 435 | The "ls" python command has been installed and is ready for use.
|
| 436 | (lldb) <strong>ls -l /tmp/</strong>
|
| 437 | total 365848
|
| 438 | -rw-r--r--@ 1 someuser wheel 6148 Jan 19 17:27 .DS_Store
|
| 439 | -rw------- 1 someuser wheel 7331 Jan 19 15:37 crash.log
|
| 440 | </tt></pre></code>
|
| 441 | <p>A template has been created in the source repository that can help you to create
|
| 442 | lldb command quickly:</p>
|
| 443 | <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] | 444 | <p>
|
| 445 | A commonly required facility is being able to create a command that does some token substitution, and then runs a different debugger command
|
| 446 | (usually, it po'es the result of an expression evaluated on its argument). For instance, given the following program:
|
| 447 | <code><pre><tt>
|
| 448 | #import <Foundation/Foundation.h>
|
| 449 | NSString*
|
| 450 | ModifyString(NSString* src)
|
| 451 | {
|
| 452 | return [src stringByAppendingString:@"foobar"];
|
| 453 | }
|
| 454 |
|
| 455 | int main()
|
| 456 | {
|
| 457 | NSString* aString = @"Hello world";
|
| 458 | NSString* anotherString = @"Let's be friends";
|
| 459 | return 1;
|
| 460 | }
|
| 461 | </tt></pre></code>
|
| 462 | you may want a pofoo X command, that equates po [ModifyString(X) capitalizedString].
|
| 463 | The following debugger interaction shows how to achieve that goal:
|
| 464 | <code><pre><tt>
|
| 465 | (lldb) <b>script</b>
|
| 466 | Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
|
| 467 | >>> <b>def pofoo_funct(debugger, command, result, internal_dict):</b>
|
| 468 | ... <b>cmd = "po [ModifyString(" + command + ") capitalizedString]"</b>
|
| 469 | ... <b>lldb.debugger.HandleCommand(cmd)</b>
|
| 470 | ...
|
| 471 | >>> ^D
|
| 472 | (lldb) <b>command script add pofoo -f pofoo_funct</b>
|
| 473 | (lldb) <b>pofoo aString</b>
|
| 474 | $1 = 0x000000010010aa00 Hello Worldfoobar
|
| 475 | (lldb) <b>pofoo anotherString</b>
|
| 476 | $2 = 0x000000010010aba0 Let's Be Friendsfoobar</tt></pre></code>
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 477 | </div>
|
| 478 | <div class="post">
|
| 479 | <h1 class ="postheader">Using the lldb.py module in python</h1>
|
| 480 | <div class="postcontent">
|
| 481 |
|
| 482 | <p>LLDB has all of its core code build into a shared library which gets
|
| 483 | used by the <b>lldb</b> command line application. On Mac OS X this
|
| 484 | shared library is a framework: <b>LLDB.framework</b> and on other
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 485 | unix variants the program is a shared library: <b>lldb.so</b>. LLDB also
|
| 486 | provides an lldb.py module that contains the bindings from LLDB into Python.
|
| 487 | To use the
|
| 488 | <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] | 489 | 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] | 490 | is done by setting the <b>PYTHONPATH</b> environment variable, adding
|
| 491 | a path to the directory that contains the <b>lldb.py</b> python module. On
|
| 492 | 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] | 493 |
|
| 494 | <p>For csh and tcsh:</p>
|
| 495 | <p><code>% <b>setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
|
| 496 | <p>For sh and bash:
|
| 497 | <p><code>% <b>export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
|
| 498 |
|
Jim Ingham | 062a836 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 499 | <p> Alternately, you can append the LLDB Python directory to the <b>sys.path</b> list directly in
|
| 500 | your Python code before importing the lldb module.</p>
|
| 501 |
|
Greg Clayton | f9ab5ea | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 502 | <p>
|
| 503 | Now your python scripts are ready to import the lldb module. Below is a
|
| 504 | python script that will launch a program from the current working directory
|
| 505 | called "a.out", set a breakpoint at "main", and then run and hit the breakpoint,
|
| 506 | and print the process, thread and frame objects if the process stopped:
|
| 507 |
|
| 508 | </p>
|
| 509 | <code><pre><tt><font color=green>#!/usr/bin/python</font>
|
| 510 |
|
| 511 | import lldb
|
| 512 |
|
| 513 | <font color=green># Set the path to the executable to debug</font>
|
| 514 | exe = "./a.out"
|
| 515 |
|
| 516 | <font color=green># Create a new debugger instance</font>
|
| 517 | debugger = lldb.SBDebugger.Create()
|
| 518 |
|
| 519 | <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] | 520 | # stops. Otherwise we would have to handle the process events ourselves which, while doable is
|
| 521 | #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] | 522 | debugger.SetAsync (False)
|
| 523 |
|
| 524 | <font color=green># Create a target from a file and arch</font>
|
| 525 | print "Creating a target for '%s'" % exe
|
| 526 |
|
| 527 | target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
|
| 528 |
|
| 529 | if target:
|
| 530 | <font color=green># If the target is valid set a breakpoint at main</font>
|
| 531 | main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
|
| 532 |
|
| 533 | print main_bp
|
| 534 |
|
| 535 | <font color=green># Launch the process. Since we specified synchronous mode, we won't return
|
| 536 | # from this function until we hit the breakpoint at main</font>
|
| 537 | process = target.LaunchSimple (None, None, os.getcwd())
|
| 538 |
|
| 539 | <font color=green># Make sure the launch went ok</font>
|
| 540 | if process:
|
| 541 | <font color=green># Print some simple process info</font>
|
| 542 | state = process.GetState ()
|
| 543 | print process
|
| 544 | if state == lldb.eStateStopped:
|
| 545 | <font color=green># Get the first thread</font>
|
| 546 | thread = process.GetThreadAtIndex (0)
|
| 547 | if thread:
|
| 548 | <font color=green># Print some simple thread info</font>
|
| 549 | print thread
|
| 550 | <font color=green># Get the first frame</font>
|
| 551 | frame = thread.GetFrameAtIndex (0)
|
| 552 | if frame:
|
| 553 | <font color=green># Print some simple frame info</font>
|
| 554 | print frame
|
| 555 | function = frame.GetFunction()
|
| 556 | <font color=green># See if we have debug info (a function)</font>
|
| 557 | if function:
|
| 558 | <font color=green># We do have a function, print some info for the function</font>
|
| 559 | print function
|
| 560 | <font color=green># Now get all instructions for this function and print them</font>
|
| 561 | insts = function.GetInstructions(target)
|
| 562 | disassemble_instructions (insts)
|
| 563 | else:
|
| 564 | <font color=green># See if we have a symbol in the symbol table for where we stopped</font>
|
| 565 | symbol = frame.GetSymbol();
|
| 566 | if symbol:
|
| 567 | <font color=green># We do have a symbol, print some info for the symbol</font>
|
| 568 | print symbol
|
| 569 | </tt></pre></code>
|
| 570 | </div>
|
| 571 | <div class="postfooter"></div>
|
| 572 |
|
| 573 | </div>
|
| 574 | </div>
|
| 575 | </div>
|
| 576 | </body>
|
| 577 | </html>
|