[lldb-dev] executing a c++ function from lldb python script

Greg Clayton gclayton at apple.com
Tue Jan 7 15:37:56 PST 2014


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
    else:
        # print error

On Jan 7, 2014, at 5:22 AM, Niels Bogaards <niels at elephantcandy.com> wrote:

> Hi,
> 
> 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,
> 
> Niels
> 
> 
> Here's the script, it outputs: 
> 
>  - DEBUG - res is No value
>  - DEBUG - fname is No value
>  - DEBUG - error is error: <NULL>
> 
> 
> 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 "
> 	description=‘''plot'''
> 	parser = optparse.OptionParser(description=description, prog='plot',usage=usage)
> 	
> 	try:
> 		(options, args) = parser.parse_args(command_args)
> 	except:
> 		print '"plot" exception'
> 		return
> 	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'
> 
> _______________________________________________
> 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