I am trying program using Lldb Python API to get an output exactly same when I run this command “image lookup --address 0x0000000405a6 --verbose”. But when I print return value of GetSymbolContext(lldb.eSymbolContextEverything), it doesnt contain the decoding of local variables which the above commands can print out local variables.

I have attached a simple that I have developed. It is not possible to print out local variables using the APIs or I am missing something out?

I am looking forward to hear from you soon.


script.txt (2.91 KB)

The variables are available through the frame in your symbol context. You have a line of code commented out in your script:

                    #variable = frame.GetVariables(target,True,True,True)

Change it to:

get_arguments = True # Get argument variables
get_locals = True # Get local variables
get_statics = True # Get globals and static variables
get_in_scope_only = True # only get variables that are in scope
use_dynamic = lldb.eDynamicDontRunTarget # Get dynamic types for variables
variables = frame.GetVariables (get_arguments, get_locals, get_statics, get_in_scope_only, use_dynamic)
print variables

This output will look different from the output in "image lookup --address 0x... --verbose" because we have an actual frame here so we can dump the variable value itself because we have a stack frame that allows us to have variable values. If you want the location of the variable you can also print that in a for loop using "variables" from above:

for variable in variables:
    print str(variable)
    print "Location = %s" % (variable.GetLocation())

Each "variable" object is a lldb.SBValue type. There are many API calls on these that you can call manually depending on what you want. Let me know if you have any questions.

Greg Clayton

Hi Greg,

Thanks for your reply. I did try the method you mention above but variable.GetLocation() only provide me the memory address of the variable. What exactly I am looking for was the register offset that stores variable i here. Example, I am looking for method able to output the one highlighted in yellow:

Variable: id = {0x000000a2}, name = “i”, type= “int”, location = DW_OP_fbreg(-564), decl = ivm_demo.c:6

Basically, I am working on a binary instrumentation research tool, that will inject code to check changes on selected variable. Therefore, using register offset, I am able to inject check for instruction using this register with this offset.

Please let me know if you have any better method to grab register offset for particular variable.



We currently don't expose this information through the API, though we could. You could add a new method to SBValue:

namespace lldb
  class SBValue
    SBData GetDWARFLocation();

This could return the DWARF location as a SBData object. Then you could consume the data by parsing the DWARF DW_OP enumerations. Otherwise you can parse the textual output of the "image lookup -va 0x123" command:

result = lldb.SBCommandReturnObject()
ci = debugger.GetCommandInterpreter()
ci.HandleCommand("image lookup -va %#x" % (frame.GetPC()), result, False)

# Now all output from the above command is in "result"
output = result.GetOutput()

# Parse "output" for variable locations

Thanks a lot Greg for your input. I have tried the 2nd option to parse the out of image lookup command and it works perfectly fine for me.