blob: dface4871f3359d798d869e67c92be3e2117b8c6 [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>
Greg Claytond47c7922012-06-29 16:25:05 +000032 <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 Claytonf9ab5ea2012-01-22 02:55:08 +000080
81 <div class="post">
82 <h1 class ="postheader">Embedded Python Interpreter</h1>
83 <div class="postcontent">
84
Jim Ingham062a8362012-01-24 02:40:42 +000085 <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 Claytonf9ab5ea2012-01-22 02:55:08 +000087<code><pre><tt>(lldb) <strong>script</strong>
88Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
89>>> 2+3
905
91>>> hex(12345)
92'0x3039'
93>>>
94</tt></pre></code>
95
Jim Ingham062a8362012-01-24 02:40:42 +000096 <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
Jim Ingham8e013df2012-12-12 20:23:52 +000099 type, the variable's <b>IsValid</b> method will return false.
Jim Ingham062a8362012-01-24 02:40:42 +0000100 <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
Jim Ingham8e013df2012-12-12 20:23:52 +0000102 API's to change, for example, the currently selected stack frame or thread.
103 <p>As a corollary to this, because they get reset every time the script interpreter is entered, you should not
104 use these variables in general purpose python code that you write using the lldb module. After all, lldb can
105 run in a multithreaded environment, and another thread might call the "script" command, changing the value out
106 from under you.</p>
Jim Ingham062a8362012-01-24 02:40:42 +0000107 These are all global variables contained in the <b>lldb</b> python namespace :</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000108 <table class="stats" width="620" cellspacing="0">
109 <tr>
110 <td class="hed" width="20%">Variable</td>
111 <td class="hed" width="10%">Type</td>
112 <td class="hed" width="70%">Description</td>
113 </tr>
114
115 <tr>
116 <td class="content">
117 <b>lldb.debugger</b>
118 </td>
119 <td class="content">
120 <b>lldb.SBDebugger</b>
121 </td>
122 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +0000123 Contains the debugger object whose <b>script</b> command was invoked.
124 The <b>lldb.SBDebugger</b> object owns the command interpreter
125 and all the targets in your debug session. There will always be a
126 Debugger in the embedded interpreter.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000127 </td>
128 </tr>
129 <tr>
130 <td class="content">
131 <b>lldb.target</b>
132 </td>
133 <td class="content">
134 <b>lldb.SBTarget</b>
135 </td>
136 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +0000137 Contains the currently selected target - for instance the one made with the
138 <b>file</b> or selected by the <b>target select &lt;target-index&gt;</b> command.
139 The <b>lldb.SBTarget</b> manages one running process, and all the executable
140 and debug files for the process.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000141 </td>
142 </tr>
143 <tr>
144 <td class="content">
145 <b>lldb.process</b>
146 </td>
147 <td class="content">
148 <b>lldb.SBProcess</b>
149 </td>
150 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +0000151 Contains the process of the currently selected target.
152 The <b>lldb.SBProcess</b> object manages the threads and allows access to
153 memory for the process.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000154 </td>
155 </tr>
156 <tr>
157 <td class="content">
158 <b>lldb.thread</b>
159 </td>
160 <td class="content">
161 <b>lldb.SBThread</b>
162 </td>
163 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +0000164 Contains the currently selected thread.
165 The <b>lldb.SBThread</b> object manages the stack frames in that thread.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000166 A thread is always selected in the command interpreter when a target stops.
167 The <b>thread select &lt;thread-index&gt;</b> commmand can be used to change the
Jim Ingham062a8362012-01-24 02:40:42 +0000168 currently selected thread. So as long as you have a stopped process, there will be
169 some selected thread.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000170 </td>
171 </tr>
172 <tr>
173 <td class="content">
174 <b>lldb.frame</b>
175 </td>
176 <td class="content">
177 <b>lldb.SBFrame</b>
178 </td>
179 <td class="content">
Jim Ingham062a8362012-01-24 02:40:42 +0000180 Contains the currently selected stack frame.
181 The <b>lldb.SBFrame</b> object manage the stack locals and the register set for
182 that stack.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000183 A stack frame is always selected in the command interpreter when a target stops.
184 The <b>frame select &lt;frame-index&gt;</b> commmand can be used to change the
Jim Ingham062a8362012-01-24 02:40:42 +0000185 currently selected frame. So as long as you have a stopped process, there will
186 be some selected frame.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000187 </td>
188 </tr>
189 </table>
190
Jim Ingham062a8362012-01-24 02:40:42 +0000191 <p>Once in the embedded interpreter, these objects can be used. To get started, note that almost
192 all of the <b>lldb</b> Python objects are able to briefly describe themselves when you pass them
193 to the Python <b>print</b> function:
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000194<code><pre><tt>(lldb) <b>script</b>
195Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
196>>> <strong>print lldb.debugger</strong>
197Debugger (instance: "debugger_1", id: 1)
198>>> <strong>print lldb.target</strong>
199a.out
200>>> <strong>print lldb.process</strong>
201SBProcess: pid = 59289, state = stopped, threads = 1, executable = a.out
202>>> <strong>print lldb.thread</strong>
203SBThread: tid = 0x1f03
204>>> <strong>print lldb.frame</strong>
205frame #0: 0x0000000100000bb6 a.out main + 54 at main.c:16
206</tt></pre></code>
207
208 </div>
209 <div class="postfooter"></div>
210
211 </div>
212 <div class="post">
213 <h1 class ="postheader">Running a Python script when a breakpoint gets hit</h1>
214 <div class="postcontent">
215
Jim Ingham062a8362012-01-24 02:40:42 +0000216 <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 +0000217 scripts to breakpoints provides a way to create complex breakpoint
218 conditions and also allows for smart logging and data gathering.</p>
Jim Ingham062a8362012-01-24 02:40:42 +0000219 <p>When your process hits a breakpoint to which you have attached some python code, the code is executed as the
Greg Claytonb34ca852012-10-26 17:53:21 +0000220 body of a function which takes three arguments:</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000221 <p>
Greg Claytonb34ca852012-10-26 17:53:21 +0000222<code><pre><tt>def breakpoint_function_wrapper(<b>frame</b>, <b>bp_loc</b>, <b>dict</b>):
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000223 <font color=green># Your code goes here</font>
224</tt></pre></code>
225 <p><table class="stats" width="620" cellspacing="0">
226 <tr>
227 <td class="hed" width="10%">Argument</td>
228 <td class="hed" width="10%">Type</td>
229 <td class="hed" width="80%">Description</td>
230 </tr>
231
232 <tr>
233 <td class="content">
234 <b>frame</b>
235 </td>
236 <td class="content">
237 <b>lldb.SBFrame</b>
238 </td>
239 <td class="content">
240 The current stack frame where the breakpoint got hit.
Jim Ingham062a8362012-01-24 02:40:42 +0000241 The object will always be valid.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000242 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>.
243 </td>
244 </tr>
245 <tr>
246 <td class="content">
247 <b>bp_loc</b>
248 </td>
249 <td class="content">
250 <b>lldb.SBBreakpointLocation</b>
251 </td>
252 <td class="content">
253 The breakpoint location that just got hit. Breakpoints are represented by <b>lldb.SBBreakpoint</b>
254 objects. These breakpoint objects can have one or more locations. These locations
255 are represented by <b>lldb.SBBreakpointLocation</b> objects.
256 </td>
257 </tr>
Greg Claytonb34ca852012-10-26 17:53:21 +0000258 <tr>
259 <td class="content">
260 <b>dict</b>
261 </td>
262 <td class="content">
263 <b>dict</b>
264 </td>
265 <td class="content">
266 The python session dictionary as a standard python dictionary object.
267 </td>
268 </tr>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000269 </table>
Jim Ingham062a8362012-01-24 02:40:42 +0000270 <p>An example will show how simple it is to write some python code and attach it to a breakpoint.
271 The following example will allow you to track the order in which the functions in a given shared library
272 are first executed during one run of your program. This is a simple method to gather an order file which
273 can be used to optimize function placement within a binary for execution locality.</p>
274 <p>We do this by setting a regular expression breakpoint
275 that will match every function in the shared library. The regular expression '.' will match
276 any string that has at least one character in it, so we will use that.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000277 This will result in one <b>lldb.SBBreakpoint</b> object
Jim Ingham062a8362012-01-24 02:40:42 +0000278 that contains an <b>lldb.SBBreakpointLocation</b> object for each function. As the breakpoint gets
279 hit, we use a counter to track the order in which the function at this particular breakpoint location got hit.
280 Since our code is passed the location that was hit, we can get the name of the function from the location,
281 disable the location so we won't count this function again; then log some info and continue the process.</p>
282 <p>Note we also have to initialize our counter, which we do with the simple one-line version of the <b>script</b>
283 command.
284 <p>Here is the code:
285
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000286<code><pre><tt>(lldb) <strong>breakpoint set --func-regex=. --shlib=libfoo.dylib</strong>
287Breakpoint created: 1: regex = '.', module = libfoo.dylib, locations = 223
Jim Ingham062a8362012-01-24 02:40:42 +0000288(lldb) <strong>script counter = 0</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000289(lldb) <strong>breakpoint command add --script-type python 1</strong>
290Enter your Python command(s). Type 'DONE' to end.
Jim Ingham062a8362012-01-24 02:40:42 +0000291> <font color=green># Increment our counter. Since we are in a function, this must be a global python variable</font>
292> <strong>global counter</strong>
293> <strong>counter += 1</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000294> <font color=green># Get the name of the function</font>
295> <strong>name = frame.GetFunctionName()</strong>
296> <font color=green># Print the order and the function name</font>
Jim Ingham062a8362012-01-24 02:40:42 +0000297> <strong>print '[%i] %s' % (counter, name)</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000298> <font color=green># Disable the current breakpoint location so it doesn't get hit again</font>
299> <strong>bp_loc.SetEnabled(False)</strong>
300> <font color=green># How continue the process</font>
301> <strong>frame.GetThread().GetProcess().Continue()</strong>
302> <strong>DONE</strong>
303</tt></pre></code>
304 <p>The <b>breakpoint command add</b> command above attaches a python script to breakpoint 1.
305 To remove the breakpoint command:
306 <p><code>(lldb) <strong>breakpoint command delete 1</strong></code>
307 </div>
308 </div>
309 <div class="post">
310 <h1 class ="postheader">Create a new LLDB command using a python function</h1>
311 <div class="postcontent">
312
Jim Ingham062a8362012-01-24 02:40:42 +0000313 <p>Python functions can be used to create new LLDB command interpreter commands, which will work
314 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 +0000315 debugging requirements. </p>
Jim Ingham062a8362012-01-24 02:40:42 +0000316 <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 +0000317
Enrico Granatac1ca9dc2012-08-08 02:06:30 +0000318 <code><pre><tt>def command_function(<b>debugger</b>, <b>command</b>, <b>result</b>, <b>internal_dict</b>):
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000319 <font color=green># Your code goes here</font>
320 </tt></pre></code>
Enrico Granatac0791aa2012-05-02 21:00:41 +0000321
322 Optionally, you can also provide a Python docstring, and LLDB will use it when providing help for your command, as in:
Enrico Granatac1ca9dc2012-08-08 02:06:30 +0000323 <code><pre><tt>def command_function(<b>debugger</b>, <b>command</b>, <b>result</b>, <b>internal_dict</b>):
Enrico Granatac0791aa2012-05-02 21:00:41 +0000324 <font color=green>"""This command takes a lot of options and does many fancy things"""</font>
325 <font color=green># Your code goes here</font>
326 </tt></pre></code>
327
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000328 <p><table class="stats" width="620" cellspacing="0">
329 <tr>
330 <td class="hed" width="10%">Argument</td>
331 <td class="hed" width="10%">Type</td>
332 <td class="hed" width="80%">Description</td>
333 </tr>
334
335 <tr>
336 <td class="content">
337 <b>debugger</b>
338 </td>
339 <td class="content">
340 <b>lldb.SBDebugger</b>
341 </td>
342 <td class="content">
343 The current debugger object.
344 </td>
345 </tr>
346 <tr>
347 <td class="content">
348 <b>command</b>
349 </td>
350 <td class="content">
351 <b>python string</b>
352 </td>
353 <td class="content">
354 A python string containing all arguments for your command. If you need to chop up the arguments
355 try using the <b>shlex</b> module's <code>shlex.split(command)</code> to properly extract the
356 arguments.
357 </td>
358 </tr>
359 <tr>
360 <td class="content">
361 <b>result</b>
362 </td>
363 <td class="content">
364 <b>lldb.SBCommandReturnObject</b>
365 </td>
366 <td class="content">
367 A return object where you can indicate the success or failure of your command. You can also
368 provide information for the command result by printing data into it. You can also just print
Jim Ingham062a8362012-01-24 02:40:42 +0000369 data as you normally would in a python script and the output will show up; this is useful for
370 logging, but the real output for your command should go in the result object.
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000371 </td>
372 </tr>
373 <tr>
374 <td class="content">
Enrico Granatac1ca9dc2012-08-08 02:06:30 +0000375 <b>internal_dict</b>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000376 </td>
377 <td class="content">
378 <b>python dict object</b>
379 </td>
380 <td class="content">
381 The dictionary for the current embedded script session which contains all variables
382 and functions.
383 </td>
384 </tr>
385 </table>
Jim Ingham062a8362012-01-24 02:40:42 +0000386 <p>One other handy convenience when defining lldb command-line commands is the command
387 <b>command script import</b> which will import a module specified by file path - so you
388 don't have to change your PYTHONPATH for temporary scripts. It also has another convenience
389 that if your new script module has a function of the form:</p>
390
Enrico Granatac1ca9dc2012-08-08 02:06:30 +0000391<code><pre><tt>def __lldb_init_module(<b>debugger</b>, <b>internal_dict</b>):
Greg Clayton261c9742012-01-26 05:36:07 +0000392 <font color=green># Command Initialization code goes here</font>
393</tt></pre></code>
Jim Ingham062a8362012-01-24 02:40:42 +0000394
Enrico Granatac1ca9dc2012-08-08 02:06:30 +0000395 <p>where <b>debugger</b> and <b>internal_dict</b> are as above, that function will get run when the module is loaded
Greg Clayton261c9742012-01-26 05:36:07 +0000396 allowing you to add whatever commands you want into the current debugger. Note that
397 this function will only be run when using the LLDB comand <b>command script import</b>,
398 it will not get run if anyone imports your module from another module.
399 If you want to always run code when your module is loaded from LLDB
400 <u>or</u> when loaded via an <b>import</b> statement in python code
401 you can test the <b>lldb.debugger</b> object, since you imported the
402 <lldb> module at the top of the python <b>ls.py</b> module. This test
403 must be in code that isn't contained inside of any function or class,
404 just like the standard test for <b>__main__</b> like all python modules
405 usally do. Sample code would look like:
406
407<code><pre><tt>if __name__ == '__main__':
408 <font color=green># Create a new debugger instance in your module if your module
409 # can be run from the command line. When we run a script from
410 # the command line, we won't have any debugger object in
411 # lldb.debugger, so we can just create it if it will be needed</font>
412 lldb.debugger = lldb.SBDebugger.Create()
413elif lldb.debugger:
414 <font color=green># Module is being run inside the LLDB interpreter</font>
415 lldb.debugger.HandleCommand('command script add -f ls.ls ls')
416 print 'The "ls" python command has been installed and is ready for use.'
417</tt></pre></code>
418 <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 +0000419 can be used by LLDB's python command code:</p>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000420
421<code><pre><tt><font color=green>#!/usr/bin/python</font>
422
423import lldb
424import commands
425import optparse
426import shlex
427
Enrico Granatac1ca9dc2012-08-08 02:06:30 +0000428def ls(debugger, command, result, internal_dict):
Jim Ingham062a8362012-01-24 02:40:42 +0000429 result.PutCString(commands.getoutput('/bin/ls %s' % command))
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000430
Jim Ingham062a8362012-01-24 02:40:42 +0000431<font color=green># And the initialization code to add your commands </font>
Enrico Granatac1ca9dc2012-08-08 02:06:30 +0000432def __lldb_init_module(debugger, internal_dict):
Jim Ingham062a8362012-01-24 02:40:42 +0000433 debugger.HandleCommand('command script add -f ls.ls ls')
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000434 print 'The "ls" python command has been installed and is ready for use.'
435</tt></pre></code>
436 <p>Now we can load the module into LLDB and use it</p>
437<code><pre><tt>% lldb
Greg Clayton261c9742012-01-26 05:36:07 +0000438(lldb) <strong>command script import ~/ls.py</strong>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000439The "ls" python command has been installed and is ready for use.
440(lldb) <strong>ls -l /tmp/</strong>
441total 365848
442-rw-r--r--@ 1 someuser wheel 6148 Jan 19 17:27 .DS_Store
443-rw------- 1 someuser wheel 7331 Jan 19 15:37 crash.log
444</tt></pre></code>
445 <p>A template has been created in the source repository that can help you to create
446 lldb command quickly:</p>
447 <a href="http://llvm.org/svn/llvm-project/lldb/trunk/examples/python/cmdtemplate.py">cmdtemplate.py</a>
Enrico Granata74566c92012-08-24 21:20:14 +0000448 <p>
449 A commonly required facility is being able to create a command that does some token substitution, and then runs a different debugger command
450 (usually, it po'es the result of an expression evaluated on its argument). For instance, given the following program:
451 <code><pre><tt>
452#import &lt;Foundation/Foundation.h&gt;
453NSString*
454ModifyString(NSString* src)
455{
456 return [src stringByAppendingString:@"foobar"];
457}
458
459int main()
460{
461 NSString* aString = @"Hello world";
462 NSString* anotherString = @"Let's be friends";
463 return 1;
464}
465 </tt></pre></code>
466 you may want a pofoo X command, that equates po [ModifyString(X) capitalizedString].
467 The following debugger interaction shows how to achieve that goal:
468 <code><pre><tt>
469(lldb) <b>script</b>
470Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
471>>> <b>def pofoo_funct(debugger, command, result, internal_dict):</b>
472... <b>cmd = "po [ModifyString(" + command + ") capitalizedString]"</b>
473... <b>lldb.debugger.HandleCommand(cmd)</b>
474...
475>>> ^D
476(lldb) <b>command script add pofoo -f pofoo_funct</b>
477(lldb) <b>pofoo aString</b>
478$1 = 0x000000010010aa00 Hello Worldfoobar
479(lldb) <b>pofoo anotherString</b>
480$2 = 0x000000010010aba0 Let's Be Friendsfoobar</tt></pre></code>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000481 </div>
482 <div class="post">
483 <h1 class ="postheader">Using the lldb.py module in python</h1>
484 <div class="postcontent">
485
486 <p>LLDB has all of its core code build into a shared library which gets
487 used by the <b>lldb</b> command line application. On Mac OS X this
488 shared library is a framework: <b>LLDB.framework</b> and on other
Jim Ingham062a8362012-01-24 02:40:42 +0000489 unix variants the program is a shared library: <b>lldb.so</b>. LLDB also
490 provides an lldb.py module that contains the bindings from LLDB into Python.
491 To use the
492 <b>LLDB.framework</b> to create your own stand-alone python programs, you will
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000493 need to tell python where to look in order to find this module. This
Jim Ingham062a8362012-01-24 02:40:42 +0000494 is done by setting the <b>PYTHONPATH</b> environment variable, adding
495 a path to the directory that contains the <b>lldb.py</b> python module. On
496 Mac OS X, this is contained inside the LLDB.framework, so you would do:
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000497
498 <p>For csh and tcsh:</p>
499 <p><code>% <b>setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
500 <p>For sh and bash:
501 <p><code>% <b>export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python</b></code></p>
502
Jim Ingham062a8362012-01-24 02:40:42 +0000503 <p> Alternately, you can append the LLDB Python directory to the <b>sys.path</b> list directly in
504 your Python code before importing the lldb module.</p>
505
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000506 <p>
507 Now your python scripts are ready to import the lldb module. Below is a
508 python script that will launch a program from the current working directory
509 called "a.out", set a breakpoint at "main", and then run and hit the breakpoint,
510 and print the process, thread and frame objects if the process stopped:
511
512 </p>
513<code><pre><tt><font color=green>#!/usr/bin/python</font>
514
515import lldb
516
517<font color=green># Set the path to the executable to debug</font>
518exe = "./a.out"
519
520<font color=green># Create a new debugger instance</font>
521debugger = lldb.SBDebugger.Create()
522
523<font color=green># When we step or continue, don't return from the function until the process
Jim Ingham062a8362012-01-24 02:40:42 +0000524# stops. Otherwise we would have to handle the process events ourselves which, while doable is
525#a little tricky. We do this by setting the async mode to false.</font>
Greg Claytonf9ab5ea2012-01-22 02:55:08 +0000526debugger.SetAsync (False)
527
528<font color=green># Create a target from a file and arch</font>
529print "Creating a target for '%s'" % exe
530
531target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
532
533if target:
534 <font color=green># If the target is valid set a breakpoint at main</font>
535 main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
536
537 print main_bp
538
539 <font color=green># Launch the process. Since we specified synchronous mode, we won't return
540 # from this function until we hit the breakpoint at main</font>
541 process = target.LaunchSimple (None, None, os.getcwd())
542
543 <font color=green># Make sure the launch went ok</font>
544 if process:
545 <font color=green># Print some simple process info</font>
546 state = process.GetState ()
547 print process
548 if state == lldb.eStateStopped:
549 <font color=green># Get the first thread</font>
550 thread = process.GetThreadAtIndex (0)
551 if thread:
552 <font color=green># Print some simple thread info</font>
553 print thread
554 <font color=green># Get the first frame</font>
555 frame = thread.GetFrameAtIndex (0)
556 if frame:
557 <font color=green># Print some simple frame info</font>
558 print frame
559 function = frame.GetFunction()
560 <font color=green># See if we have debug info (a function)</font>
561 if function:
562 <font color=green># We do have a function, print some info for the function</font>
563 print function
564 <font color=green># Now get all instructions for this function and print them</font>
565 insts = function.GetInstructions(target)
566 disassemble_instructions (insts)
567 else:
568 <font color=green># See if we have a symbol in the symbol table for where we stopped</font>
569 symbol = frame.GetSymbol();
570 if symbol:
571 <font color=green># We do have a symbol, print some info for the symbol</font>
572 print symbol
573</tt></pre></code>
574 </div>
575 <div class="postfooter"></div>
576
577 </div>
578 </div>
579</div>
580</body>
581</html>