1#!/usr/bin/python 2 3#---------------------------------------------------------------------- 4# Be sure to add the python path that points to the LLDB shared library. 5# 6# # To use this in the embedded python interpreter using "lldb" just 7# import it with the full path using the "command script import" 8# command 9# (lldb) command script import /path/to/cmdtemplate.py 10#---------------------------------------------------------------------- 11 12import lldb 13import commands 14import optparse 15import shlex 16 17def create_framestats_options(): 18 usage = "usage: %prog [options]" 19 description='''This command is meant to be an example of how to make an LLDB command that 20does something useful, follows best practices, and exploits the SB API. 21Specifically, this command computes the aggregate and average size of the variables in the current frame 22and allows you to tweak exactly which variables are to be accounted in the computation. 23''' 24 parser = optparse.OptionParser(description=description, prog='framestats',usage=usage) 25 parser.add_option('-i', '--in-scope', action='store_true', dest='inscope', help='in_scope_only = True', default=False) 26 parser.add_option('-a', '--arguments', action='store_true', dest='arguments', help='arguments = True', default=False) 27 parser.add_option('-l', '--locals', action='store_true', dest='locals', help='locals = True', default=False) 28 parser.add_option('-s', '--statics', action='store_true', dest='statics', help='statics = True', default=False) 29 return parser 30 31def the_framestats_command(debugger, command, result, dict): 32 # Use the Shell Lexer to properly parse up command options just like a 33 # shell would 34 command_args = shlex.split(command) 35 parser = create_framestats_options() 36 try: 37 (options, args) = parser.parse_args(command_args) 38 except: 39 # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit 40 # (courtesy of OptParse dealing with argument errors by throwing SystemExit) 41 result.SetError ("option parsing failed") 42 return 43 44 # in a command - the lldb.* convenience variables are not to be used 45 # and their values (if any) are undefined 46 # this is the best practice to access those objects from within a command 47 target = debugger.GetSelectedTarget() 48 process = target.GetProcess() 49 thread = process.GetSelectedThread() 50 frame = thread.GetSelectedFrame() 51 if not frame.IsValid(): 52 return "no frame here" 53 # from now on, replace lldb.<thing>.whatever with <thing>.whatever 54 variables_list = frame.GetVariables(options.arguments, options.locals, options.statics, options.inscope) 55 variables_count = variables_list.GetSize() 56 if variables_count == 0: 57 print >> result, "no variables here" 58 return 59 total_size = 0 60 for i in range(0,variables_count): 61 variable = variables_list.GetValueAtIndex(i) 62 variable_type = variable.GetType() 63 total_size = total_size + variable_type.GetByteSize() 64 average_size = float(total_size) / variables_count 65 print >>result, "Your frame has %d variables. Their total size is %d bytes. The average size is %f bytes" % (variables_count,total_size,average_size) 66 # not returning anything is akin to returning success 67 68def __lldb_init_module (debugger, dict): 69 # This initializer is being run from LLDB in the embedded command interpreter 70 # Make the options so we can generate the help text for the new LLDB 71 # command line command prior to registering it with LLDB below 72 parser = create_framestats_options() 73 the_framestats_command.__doc__ = parser.format_help() 74 # Add any commands contained in this module to LLDB 75 debugger.HandleCommand('command script add -f cmdtemplate.the_framestats_command framestats') 76 print 'The "framestats" command has been installed, type "help framestats" or "framestats --help" for detailed help.' 77