blob: 149ef15ba3bae0d3d273ff69d424e3795f4dfff9 [file] [log] [blame]
Greg Claytonf9ab5ea2012-01-22 02:55:08 +00001<!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 Claytonf3edcc02012-01-26 00:32:22 +00006<title>LLDB Python Reference</title>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +00007</head>
8
9<body>
10 <div class="www_title">
Greg Claytonf3edcc02012-01-26 00:32:22 +000011 LLDB Python Reference
Greg Claytonf9ab5ea2012-01-22 02:55:08 +000012 </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 Ingham062a8362012-01-24 02:40:42 +000022 <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 Claytonf9ab5ea2012-01-22 02:55:08 +000029
30 </div>
31 <div class="postfooter"></div>
32
33 <div class="post">
34 <h1 class ="postheader">Embedded Python Interpreter</h1>
35 <div class="postcontent">
36
Jim Ingham062a8362012-01-24 02:40:42 +000037 <p>The embedded python interpreter can be accessed in a variety of ways from within LLDB. The
38 easiest way is to use the lldb command <b>script</b> with no arguments at the lldb command prompt:</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +000039<code><pre><tt>(lldb) <strong>script</strong>
40Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
41>>> 2+3
425
43>>> hex(12345)
44'0x3039'
45>>>
46</tt></pre></code>
47
Jim Ingham062a8362012-01-24 02:40:42 +000048 <p>This drops you into the embedded python interpreter. When running under the <b>script</b> command,
49 lldb sets some convenience variables that give you quick access to the currently selected entities that characterize
50 the program and debugger state. In each case, if there is no currently selected entity of the appropriate
51 type, the variable's <b>IsValid</b> method will return false.
52 <p>Note also, these variables hold the values
53 of the selected objects on entry to the embedded interpreter. They do not update as you use the LLDB
54 API's to change, for example, the currently selected stack frame or thread.</p>
55 These are all global variables contained in the <b>lldb</b> python namespace :</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +000056 <table class="stats" width="620" cellspacing="0">
57 <tr>
58 <td class="hed" width="20%">Variable</td>
59 <td class="hed" width="10%">Type</td>
60 <td class="hed" width="70%">Description</td>
61 </tr>
62
63 <tr>
64 <td class="content">
65 <b>lldb.debugger</b>
66 </td>
67 <td class="content">
68 <b>lldb.SBDebugger</b>
69 </td>
70 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +000071 Contains the debugger object whose <b>script</b> command was invoked.
72 The <b>lldb.SBDebugger</b> object owns the command interpreter
73 and all the targets in your debug session. There will always be a
74 Debugger in the embedded interpreter.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +000075 </td>
76 </tr>
77 <tr>
78 <td class="content">
79 <b>lldb.target</b>
80 </td>
81 <td class="content">
82 <b>lldb.SBTarget</b>
83 </td>
84 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +000085 Contains the currently selected target - for instance the one made with the
86 <b>file</b> or selected by the <b>target select &lt;target-index&gt;</b> command.
87 The <b>lldb.SBTarget</b> manages one running process, and all the executable
88 and debug files for the process.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +000089 </td>
90 </tr>
91 <tr>
92 <td class="content">
93 <b>lldb.process</b>
94 </td>
95 <td class="content">
96 <b>lldb.SBProcess</b>
97 </td>
98 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +000099 Contains the process of the currently selected target.
100 The <b>lldb.SBProcess</b> object manages the threads and allows access to
101 memory for the process.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000102 </td>
103 </tr>
104 <tr>
105 <td class="content">
106 <b>lldb.thread</b>
107 </td>
108 <td class="content">
109 <b>lldb.SBThread</b>
110 </td>
111 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +0000112 Contains the currently selected thread.
113 The <b>lldb.SBThread</b> object manages the stack frames in that thread.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000114 A thread is always selected in the command interpreter when a target stops.
115 The <b>thread select &lt;thread-index&gt;</b> commmand can be used to change the
Jim Ingham062a8362012-01-24 02:40:42 +0000116 currently selected thread. So as long as you have a stopped process, there will be
117 some selected thread.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000118 </td>
119 </tr>
120 <tr>
121 <td class="content">
122 <b>lldb.frame</b>
123 </td>
124 <td class="content">
125 <b>lldb.SBFrame</b>
126 </td>
127 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +0000128 Contains the currently selected stack frame.
129 The <b>lldb.SBFrame</b> object manage the stack locals and the register set for
130 that stack.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000131 A stack frame is always selected in the command interpreter when a target stops.
132 The <b>frame select &lt;frame-index&gt;</b> commmand can be used to change the
Jim Ingham062a8362012-01-24 02:40:42 +0000133 currently selected frame. So as long as you have a stopped process, there will
134 be some selected frame.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000135 </td>
136 </tr>
137 </table>
138
Jim Ingham062a8362012-01-24 02:40:42 +0000139 <p>Once in the embedded interpreter, these objects can be used. To get started, note that almost
140 all of the <b>lldb</b> Python objects are able to briefly describe themselves when you pass them
141 to the Python <b>print</b> function:
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000142<code><pre><tt>(lldb) <b>script</b>
143Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
144>>> <strong>print lldb.debugger</strong>
145Debugger (instance: "debugger_1", id: 1)
146>>> <strong>print lldb.target</strong>
147a.out
148>>> <strong>print lldb.process</strong>
149SBProcess: pid = 59289, state = stopped, threads = 1, executable = a.out
150>>> <strong>print lldb.thread</strong>
151SBThread: tid = 0x1f03
152>>> <strong>print lldb.frame</strong>
153frame #0: 0x0000000100000bb6 a.out main + 54 at main.c:16
154</tt></pre></code>
155
156 </div>
157 <div class="postfooter"></div>
158
159 </div>
160 <div class="post">
161 <h1 class ="postheader">Running a Python script when a breakpoint gets hit</h1>
162 <div class="postcontent">
163
Jim Ingham062a8362012-01-24 02:40:42 +0000164 <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 Claytonf9ab5ea2012-01-22 02:55:08 +0000165 scripts to breakpoints provides a way to create complex breakpoint
166 conditions and also allows for smart logging and data gathering.</p>
Jim Ingham062a8362012-01-24 02:40:42 +0000167 <p>When your process hits a breakpoint to which you have attached some python code, the code is executed as the
168 body of a function which takes two arguments:</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000169 <p>
Jim Ingham062a8362012-01-24 02:40:42 +0000170<code><pre><tt>def breakpoint_function_wrapper(<b>frame</b>, <b>bp_loc</b>):
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000171 <font color=green># Your code goes here</font>
172</tt></pre></code>
173 <p><table class="stats" width="620" cellspacing="0">
174 <tr>
175 <td class="hed" width="10%">Argument</td>
176 <td class="hed" width="10%">Type</td>
177 <td class="hed" width="80%">Description</td>
178 </tr>
179
180 <tr>
181 <td class="content">
182 <b>frame</b>
183 </td>
184 <td class="content">
185 <b>lldb.SBFrame</b>
186 </td>
187 <td class="content">
188 The current stack frame where the breakpoint got hit.
Jim Ingham062a8362012-01-24 02:40:42 +0000189 The object will always be valid.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000190 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>.
191 </td>
192 </tr>
193 <tr>
194 <td class="content">
195 <b>bp_loc</b>
196 </td>
197 <td class="content">
198 <b>lldb.SBBreakpointLocation</b>
199 </td>
200 <td class="content">
201 The breakpoint location that just got hit. Breakpoints are represented by <b>lldb.SBBreakpoint</b>
202 objects. These breakpoint objects can have one or more locations. These locations
203 are represented by <b>lldb.SBBreakpointLocation</b> objects.
204 </td>
205 </tr>
206 </table>
Jim Ingham062a8362012-01-24 02:40:42 +0000207 <p>An example will show how simple it is to write some python code and attach it to a breakpoint.
208 The following example will allow you to track the order in which the functions in a given shared library
209 are first executed during one run of your program. This is a simple method to gather an order file which
210 can be used to optimize function placement within a binary for execution locality.</p>
211 <p>We do this by setting a regular expression breakpoint
212 that will match every function in the shared library. The regular expression '.' will match
213 any string that has at least one character in it, so we will use that.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000214 This will result in one <b>lldb.SBBreakpoint</b> object
Jim Ingham062a8362012-01-24 02:40:42 +0000215 that contains an <b>lldb.SBBreakpointLocation</b> object for each function. As the breakpoint gets
216 hit, we use a counter to track the order in which the function at this particular breakpoint location got hit.
217 Since our code is passed the location that was hit, we can get the name of the function from the location,
218 disable the location so we won't count this function again; then log some info and continue the process.</p>
219 <p>Note we also have to initialize our counter, which we do with the simple one-line version of the <b>script</b>
220 command.
221 <p>Here is the code:
222
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000223<code><pre><tt>(lldb) <strong>breakpoint set --func-regex=. --shlib=libfoo.dylib</strong>
224Breakpoint created: 1: regex = '.', module = libfoo.dylib, locations = 223
Jim Ingham062a8362012-01-24 02:40:42 +0000225(lldb) <strong>script counter = 0</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000226(lldb) <strong>breakpoint command add --script-type python 1</strong>
227Enter your Python command(s). Type 'DONE' to end.
Jim Ingham062a8362012-01-24 02:40:42 +0000228> <font color=green># Increment our counter. Since we are in a function, this must be a global python variable</font>
229> <strong>global counter</strong>
230> <strong>counter += 1</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000231> <font color=green># Get the name of the function</font>
232> <strong>name = frame.GetFunctionName()</strong>
233> <font color=green># Print the order and the function name</font>
Jim Ingham062a8362012-01-24 02:40:42 +0000234> <strong>print '[%i] %s' % (counter, name)</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000235> <font color=green># Disable the current breakpoint location so it doesn't get hit again</font>
236> <strong>bp_loc.SetEnabled(False)</strong>
237> <font color=green># How continue the process</font>
238> <strong>frame.GetThread().GetProcess().Continue()</strong>
239> <strong>DONE</strong>
240</tt></pre></code>
241 <p>The <b>breakpoint command add</b> command above attaches a python script to breakpoint 1.
242 To remove the breakpoint command:
243 <p><code>(lldb) <strong>breakpoint command delete 1</strong></code>
244 </div>
245 </div>
246 <div class="post">
247 <h1 class ="postheader">Create a new LLDB command using a python function</h1>
248 <div class="postcontent">
249
Jim Ingham062a8362012-01-24 02:40:42 +0000250 <p>Python functions can be used to create new LLDB command interpreter commands, which will work
251 like all the natively defined lldb commands. This provides a very flexible and easy way to extend LLDB to meet your
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000252 debugging requirements. </p>
Jim Ingham062a8362012-01-24 02:40:42 +0000253 <p>To write a python function that implements a new LDB command define the function to take four arguments as follows:</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000254
255 <code><pre><tt>def command_function(<b>debugger</b>, <b>command</b>, <b>result</b>, <b>dict</b>):
256 <font color=green># Your code goes here</font>
257 </tt></pre></code>
258 <p><table class="stats" width="620" cellspacing="0">
259 <tr>
260 <td class="hed" width="10%">Argument</td>
261 <td class="hed" width="10%">Type</td>
262 <td class="hed" width="80%">Description</td>
263 </tr>
264
265 <tr>
266 <td class="content">
267 <b>debugger</b>
268 </td>
269 <td class="content">
270 <b>lldb.SBDebugger</b>
271 </td>
272 <td class="content">
273 The current debugger object.
274 </td>
275 </tr>
276 <tr>
277 <td class="content">
278 <b>command</b>
279 </td>
280 <td class="content">
281 <b>python string</b>
282 </td>
283 <td class="content">
284 A python string containing all arguments for your command. If you need to chop up the arguments
285 try using the <b>shlex</b> module's <code>shlex.split(command)</code> to properly extract the
286 arguments.
287 </td>
288 </tr>
289 <tr>
290 <td class="content">
291 <b>result</b>
292 </td>
293 <td class="content">
294 <b>lldb.SBCommandReturnObject</b>
295 </td>
296 <td class="content">
297 A return object where you can indicate the success or failure of your command. You can also
298 provide information for the command result by printing data into it. You can also just print
Jim Ingham062a8362012-01-24 02:40:42 +0000299 data as you normally would in a python script and the output will show up; this is useful for
300 logging, but the real output for your command should go in the result object.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000301 </td>
302 </tr>
303 <tr>
304 <td class="content">
305 <b>dict</b>
306 </td>
307 <td class="content">
308 <b>python dict object</b>
309 </td>
310 <td class="content">
311 The dictionary for the current embedded script session which contains all variables
312 and functions.
313 </td>
314 </tr>
315 </table>
Jim Ingham062a8362012-01-24 02:40:42 +0000316 <p>One other handy convenience when defining lldb command-line commands is the command
317 <b>command script import</b> which will import a module specified by file path - so you
318 don't have to change your PYTHONPATH for temporary scripts. It also has another convenience
319 that if your new script module has a function of the form:</p>
320
Greg Clayton261c9742012-01-26 05:36:07 +0000321<code><pre><tt>def __lldb_module_init(<b>debugger</b>, <b>dict</b>):
322 <font color=green># Command Initialization code goes here</font>
323</tt></pre></code>
Jim Ingham062a8362012-01-24 02:40:42 +0000324
325 <p>where <b>debugger</b> and <b>dict</b> are as above, that function will get run when the module is loaded
Greg Clayton261c9742012-01-26 05:36:07 +0000326 allowing you to add whatever commands you want into the current debugger. Note that
327 this function will only be run when using the LLDB comand <b>command script import</b>,
328 it will not get run if anyone imports your module from another module.
329 If you want to always run code when your module is loaded from LLDB
330 <u>or</u> when loaded via an <b>import</b> statement in python code
331 you can test the <b>lldb.debugger</b> object, since you imported the
332 <lldb> module at the top of the python <b>ls.py</b> module. This test
333 must be in code that isn't contained inside of any function or class,
334 just like the standard test for <b>__main__</b> like all python modules
335 usally do. Sample code would look like:
336
337<code><pre><tt>if __name__ == '__main__':
338 <font color=green># Create a new debugger instance in your module if your module
339 # can be run from the command line. When we run a script from
340 # the command line, we won't have any debugger object in
341 # lldb.debugger, so we can just create it if it will be needed</font>
342 lldb.debugger = lldb.SBDebugger.Create()
343elif lldb.debugger:
344 <font color=green># Module is being run inside the LLDB interpreter</font>
345 lldb.debugger.HandleCommand('command script add -f ls.ls ls')
346 print 'The "ls" python command has been installed and is ready for use.'
347</tt></pre></code>
348 <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 Ingham062a8362012-01-24 02:40:42 +0000349 can be used by LLDB's python command code:</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000350
351<code><pre><tt><font color=green>#!/usr/bin/python</font>
352
353import lldb
354import commands
355import optparse
356import shlex
357
358def ls(debugger, command, result, dict):
Jim Ingham062a8362012-01-24 02:40:42 +0000359 result.PutCString(commands.getoutput('/bin/ls %s' % command))
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000360
Jim Ingham062a8362012-01-24 02:40:42 +0000361<font color=green># And the initialization code to add your commands </font>
362def __lldb_module_init(debugger, dict):
363 debugger.HandleCommand('command script add -f ls.ls ls')
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000364 print 'The "ls" python command has been installed and is ready for use.'
365</tt></pre></code>
366 <p>Now we can load the module into LLDB and use it</p>
367<code><pre><tt>% lldb
Greg Clayton261c9742012-01-26 05:36:07 +0000368(lldb) <strong>command script import ~/ls.py</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000369The "ls" python command has been installed and is ready for use.
370(lldb) <strong>ls -l /tmp/</strong>
371total 365848
372-rw-r--r--@ 1 someuser wheel 6148 Jan 19 17:27 .DS_Store
373-rw------- 1 someuser wheel 7331 Jan 19 15:37 crash.log
374</tt></pre></code>
375 <p>A template has been created in the source repository that can help you to create
376 lldb command quickly:</p>
377 <a href="http://llvm.org/svn/llvm-project/lldb/trunk/examples/python/cmdtemplate.py">cmdtemplate.py</a>
378 </div>
379 <div class="post">
380 <h1 class ="postheader">Using the lldb.py module in python</h1>
381 <div class="postcontent">
382
383 <p>LLDB has all of its core code build into a shared library which gets
384 used by the <b>lldb</b> command line application. On Mac OS X this
385 shared library is a framework: <b>LLDB.framework</b> and on other
Jim Ingham062a8362012-01-24 02:40:42 +0000386 unix variants the program is a shared library: <b>lldb.so</b>. LLDB also
387 provides an lldb.py module that contains the bindings from LLDB into Python.
388 To use the
389 <b>LLDB.framework</b> to create your own stand-alone python programs, you will
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000390 need to tell python where to look in order to find this module. This
Jim Ingham062a8362012-01-24 02:40:42 +0000391 is done by setting the <b>PYTHONPATH</b> environment variable, adding
392 a path to the directory that contains the <b>lldb.py</b> python module. On
393 Mac OS X, this is contained inside the LLDB.framework, so you would do:
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000394
395 <p>For csh and tcsh:</p>
396 <p><code>% <b>setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
397 <p>For sh and bash:
398 <p><code>% <b>export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
399
Jim Ingham062a8362012-01-24 02:40:42 +0000400 <p> Alternately, you can append the LLDB Python directory to the <b>sys.path</b> list directly in
401 your Python code before importing the lldb module.</p>
402
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000403 <p>
404 Now your python scripts are ready to import the lldb module. Below is a
405 python script that will launch a program from the current working directory
406 called "a.out", set a breakpoint at "main", and then run and hit the breakpoint,
407 and print the process, thread and frame objects if the process stopped:
408
409 </p>
410<code><pre><tt><font color=green>#!/usr/bin/python</font>
411
412import lldb
413
414<font color=green># Set the path to the executable to debug</font>
415exe = "./a.out"
416
417<font color=green># Create a new debugger instance</font>
418debugger = lldb.SBDebugger.Create()
419
420<font color=green># When we step or continue, don't return from the function until the process
Jim Ingham062a8362012-01-24 02:40:42 +0000421# stops. Otherwise we would have to handle the process events ourselves which, while doable is
422#a little tricky. We do this by setting the async mode to false.</font>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000423debugger.SetAsync (False)
424
425<font color=green># Create a target from a file and arch</font>
426print "Creating a target for '%s'" % exe
427
428target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
429
430if target:
431 <font color=green># If the target is valid set a breakpoint at main</font>
432 main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
433
434 print main_bp
435
436 <font color=green># Launch the process. Since we specified synchronous mode, we won't return
437 # from this function until we hit the breakpoint at main</font>
438 process = target.LaunchSimple (None, None, os.getcwd())
439
440 <font color=green># Make sure the launch went ok</font>
441 if process:
442 <font color=green># Print some simple process info</font>
443 state = process.GetState ()
444 print process
445 if state == lldb.eStateStopped:
446 <font color=green># Get the first thread</font>
447 thread = process.GetThreadAtIndex (0)
448 if thread:
449 <font color=green># Print some simple thread info</font>
450 print thread
451 <font color=green># Get the first frame</font>
452 frame = thread.GetFrameAtIndex (0)
453 if frame:
454 <font color=green># Print some simple frame info</font>
455 print frame
456 function = frame.GetFunction()
457 <font color=green># See if we have debug info (a function)</font>
458 if function:
459 <font color=green># We do have a function, print some info for the function</font>
460 print function
461 <font color=green># Now get all instructions for this function and print them</font>
462 insts = function.GetInstructions(target)
463 disassemble_instructions (insts)
464 else:
465 <font color=green># See if we have a symbol in the symbol table for where we stopped</font>
466 symbol = frame.GetSymbol();
467 if symbol:
468 <font color=green># We do have a symbol, print some info for the symbol</font>
469 print symbol
470</tt></pre></code>
471 </div>
472 <div class="postfooter"></div>
473
474 </div>
475 </div>
476</div>
477</body>
478</html>