executing a c++ function from lldb python script


I made a script for lldb last year that allowed me to plot a custom object as a curve from Xcode’s lldb prompt. Worked very well for a year, but no more. Has anything changed in the EvaluateExpression syntax? Are there linker options I need to activate in my target c++ program?

Thanks for any help,


Here’s the script, it outputs:

  • DEBUG - res is No value
  • DEBUG - fname is No value
  • DEBUG - error is error:

import lldb
import commands
import optparse
import shlex
import logging

def plot(debugger, command, result, dict):
logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s - %(levelname)s - %(message)s’)
command_args = shlex.split(command)
usage = "usage: %prog objectName "
parser = optparse.OptionParser(description=description, prog=‘plot’,usage=usage)

(options, args) = parser.parse_args(command_args)
print ‘“plot” exception’
la = len(args)
print ‘len args = %d’ % la
if len(args) == 1:
ecom = “MyNamespace::MyClass::MyStaticPlotFunction(%s)" % args[0]
res = lldb.frame.EvaluateExpression(ecom, 1)
fname = lldb.frame.EvaluateExpression(“MyNamespace::MyClass::GetStaticPlotFileName()”)
error = lldb.SBError()
logging.debug(“res is %s”, res);
logging.debug(“fname is %s”, fname);
logging.debug(“error is %s”, error);

if name == ‘main’:

This script is being run from the command line, create a debugger in case we are

going to use any debugger functions in our function.

lldb.debugger = lldb.SBDebugger.Create()
plot (sys.argv)

def __lldb_init_module (debugger, dict):

This initializer is being run from LLDB in the embedded command interpreter

Add any commands contained in this module to LLDB

debugger.HandleCommand(‘command script add -f plot.plot plot’)
print ‘“plot” command installed, type “plot --help” for detailed help’

Yes, "lldb.target", "lldb.process", "lldb.thread" and "lldb.frame" are no longer set as they are global values that can be changed by code that re-enters python. We are need people to not use them anymore except when using the "script" command.

So to fix this you will want to grab your frame from the debugger object that is passed in:

    target = debugger.GetSelectedTarget()
    process = target.GetProcess()
    thread = process.GetSelectedThread()
    frame = thread.GetSelectedFrame()

Then use "frame" instead of "lldb.frame" when calling EvaluateExpression. Also instead of using magic values, you can use the enums for the 2nd param to EvaluateExpression:

    res = frame.EvaluateExpression(ecom, lldb.eDynamicCanRunTarget)

Then be sure to set your error correctly instead of initializing it with a default constructed value:

    error = res.GetError()

You might also want to make sure your process is stopped before trying to run the expression:

    if process.GetState() == eStateStopped:
        # Evaluate expression
        # print error

Thanks a lot, works like a charm!