3 # ---------------------------------------------------------------------
4 # Be sure to add the python path that points to the LLDB shared library.
6 # # To use this in the embedded python interpreter using "lldb" just
7 # import it with the full path using the "command script import"
9 # (lldb) command script import /path/to/cmdtemplate.py
10 # ---------------------------------------------------------------------
19 class FrameStatCommand
:
20 program
= "framestats"
23 def register_lldb_command(cls
, debugger
, module_name
):
24 parser
= cls
.create_options()
25 cls
.__doc
__ = parser
.format_help()
26 # Add any commands contained in this module to LLDB
27 command
= "command script add -o -c %s.%s %s" % (
32 debugger
.HandleCommand(command
)
34 'The "{0}" command has been installed, type "help {0}" or "{0} '
35 '--help" for detailed help.'.format(cls
.program
)
39 def create_options(cls
):
40 usage
= "usage: %prog [options]"
42 "This command is meant to be an example of how to make "
43 "an LLDB command that does something useful, follows "
44 "best practices, and exploits the SB API. "
45 "Specifically, this command computes the aggregate "
46 "and average size of the variables in the current "
47 "frame and allows you to tweak exactly which variables "
48 "are to be accounted in the computation."
51 # Pass add_help_option = False, since this keeps the command in line
52 # with lldb commands, and we wire up "help command" to work by
53 # providing the long & short help methods below.
54 parser
= optparse
.OptionParser(
55 description
=description
,
58 add_help_option
=False,
66 help="in_scope_only = True",
75 help="arguments = True",
93 help="statics = True",
99 def get_short_help(self
):
100 return "Example command for use in debugging"
102 def get_long_help(self
):
103 return self
.help_string
105 def __init__(self
, debugger
, unused
):
106 self
.parser
= self
.create_options()
107 self
.help_string
= self
.parser
.format_help()
109 def __call__(self
, debugger
, command
, exe_ctx
, result
):
110 # Use the Shell Lexer to properly parse up command options just like a
112 command_args
= shlex
.split(command
)
115 (options
, args
) = self
.parser
.parse_args(command_args
)
117 # if you don't handle exceptions, passing an incorrect argument to
118 # the OptionParser will cause LLDB to exit (courtesy of OptParse
119 # dealing with argument errors by throwing SystemExit)
120 result
.SetError("option parsing failed")
123 # Always get program state from the lldb.SBExecutionContext passed
125 frame
= exe_ctx
.GetFrame()
126 if not frame
.IsValid():
127 result
.SetError("invalid frame")
130 variables_list
= frame
.GetVariables(
131 options
.arguments
, options
.locals, options
.statics
, options
.inscope
133 variables_count
= variables_list
.GetSize()
134 if variables_count
== 0:
135 print("no variables here", file=result
)
138 for i
in range(0, variables_count
):
139 variable
= variables_list
.GetValueAtIndex(i
)
140 variable_type
= variable
.GetType()
141 total_size
= total_size
+ variable_type
.GetByteSize()
142 average_size
= float(total_size
) / variables_count
144 "Your frame has %d variables. Their total size "
145 "is %d bytes. The average size is %f bytes"
146 % (variables_count
, total_size
, average_size
),
149 # not returning anything is akin to returning success
152 def __lldb_init_module(debugger
, dict):
153 # Register all classes that have a register_lldb_command method
154 for _name
, cls
in inspect
.getmembers(sys
.modules
[__name__
]):
155 if inspect
.isclass(cls
) and callable(
156 getattr(cls
, "register_lldb_command", None)
158 cls
.register_lldb_command(debugger
, __name__
)