[lldb-dev] writing an extension command in Python

Greg Clayton gclayton at apple.com
Tue Jan 4 16:09:20 PST 2011


You could currently do this by writing a ShowImage() function in python and then dropping into the embedded python interpreter within LLDB:

(lldb) breakpoint set --file make_image.c --line 123
(lldb) run
stop at breakpoint...
(lldb) script

Now you are in the script interpreter with access to the current program. You should be able to get ahold of the current process and thread and then evaluate an expression:

debugger = lldb.SBDebugger().FindDebuggerWithID(lldb.debugger_unique_id)
target = debugger.GetCurrentTarget()
process = target.GetProcess()
thread = process.GetThreadAtIndex (0)

if thread.IsValid():
    frame = thread.GetFrameAtIndex (0)
    if frame.IsValid():
        image_data_expr = frame.EvaluateExpression ("my_struct->image_data");
        image_size_expr = frame.EvaluateExpression ("my_struct->image_size");

	if image_data_expr.GetError().Success() && image_size_expr.GetError().Success():
		image_addr = int(image_data_expr.GetValue(frame), 16);
		image_size = int(image_size_expr.GetValue(frame), 16);
		lldb::SBError error;
		process.ReadMemory (image_addr, image_bytes, image_size, error);

		... then save the bytes to a file and then call some display functions....


We need to make convenience variables in the embedded interpreter to make getting the current target/process/thread/frame much easier, but this should at least be a start. The one issue is that in our SBProcess::ReadMemory() function call the C++ API looks like:

    size_t
    SBProcess::ReadMemory (addr_t addr, void *buf, size_t size, lldb::SBError &error);

I am not sure how SWIG will handle the "void *buf". We might need to add some conversion functions so that any functions that take a "void *buf, size_t size" pair of arguments, can take a reference to a python array that can be filled in.

If wrote the above code in a function, you could them import your module and just call your function with a few parameters.

Greg Clayton





            


On Jan 4, 2011, at 3:11 PM, Cameron McCormack wrote:

> Hi.
> 
> I guess this is more of a user question than a dev question, but since
> there is no user mailing list I hope that it’s OK to post here!
> 
> I want to write an extension command in Python that would work something
> like this:
> 
>  (lldb) showimage 300 200 <some-expr-that-evaluates-to-a-pointer>
> 
> which grabs the data at the pointer and goes off and writes it to a file
> as a PNG, or pops up a window showing the image, or something like that.
> 
> How do I go about writing the extension?  I see there is this disasm.py
> example, but that seems to be controlling lldb from Python rather than
> registering an extension to be called from the lldb prompt.
> 
> Also, is there documentation yet on how to use the API?  The C++ headers
> in include/lldb/API don’t seem to have doxygen comments.  (I can
> probably fumble my way through working out the Python API from the C++
> one, if it is documented.)
> 
> Thanks!
> 
> Cameron
> 
> -- 
> Cameron McCormack ≝ http://mcc.id.au/
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev





More information about the lldb-dev mailing list