blob: ca575362f9b7eb2b9a95d7afeb3bb1dc0f45a2ce [file] [log] [blame]
Greg Claytond879a632012-01-22 02:55:08 +00001#!/usr/bin/python
2
3#----------------------------------------------------------------------
4# Be sure to add the python path that points to the LLDB shared library.
5#
Greg Claytone2841632012-01-26 02:56:24 +00006# # 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
Greg Claytond879a632012-01-22 02:55:08 +000010#----------------------------------------------------------------------
11
12import lldb
13import commands
14import optparse
15import shlex
16
Enrico Granata083fcdb2013-05-09 01:32:24 +000017def 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.
Greg Claytonf4539072012-09-13 05:35:34 +000023'''
Enrico Granata083fcdb2013-05-09 01:32:24 +000024 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)
Greg Claytonf4539072012-09-13 05:35:34 +000029 return parser
30
Enrico Granata083fcdb2013-05-09 01:32:24 +000031def the_framestats_command(debugger, command, result, dict):
Greg Claytonf4539072012-09-13 05:35:34 +000032 # Use the Shell Lexer to properly parse up command options just like a
33 # shell would
34 command_args = shlex.split(command)
Enrico Granata083fcdb2013-05-09 01:32:24 +000035 parser = create_framestats_options()
Greg Claytond879a632012-01-22 02:55:08 +000036 try:
37 (options, args) = parser.parse_args(command_args)
38 except:
Enrico Granata6308f582013-02-25 23:01:08 +000039 # 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)
Enrico Granatada504ff2013-07-11 21:49:38 +000041 result.SetError ("option parsing failed")
42 return
Greg Claytond879a632012-01-22 02:55:08 +000043
Enrico Granata083fcdb2013-05-09 01:32:24 +000044 # 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:
Enrico Granatada504ff2013-07-11 21:49:38 +000057 print >> result, "no variables here"
Enrico Granata083fcdb2013-05-09 01:32:24 +000058 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
Enrico Granatada504ff2013-07-11 21:49:38 +000065 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
Greg Claytond879a632012-01-22 02:55:08 +000067
Jim Ingham87a59362012-01-24 02:40:42 +000068def __lldb_init_module (debugger, dict):
Greg Claytonf4539072012-09-13 05:35:34 +000069 # 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
Enrico Granata083fcdb2013-05-09 01:32:24 +000072 parser = create_framestats_options()
73 the_framestats_command.__doc__ = parser.format_help()
Greg Claytond879a632012-01-22 02:55:08 +000074 # Add any commands contained in this module to LLDB
Enrico Granata083fcdb2013-05-09 01:32:24 +000075 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.'