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 # ---------------------------------------------------------------------
15 from lldb
.plugins
.parsed_cmd
import ParsedCommand
17 class FrameStatCommand(ParsedCommand
):
18 program
= "framestats"
21 def register_lldb_command(cls
, debugger
, module_name
):
22 ParsedCommand
.do_register_cmd(cls
, debugger
, module_name
)
24 'The "{0}" command has been installed, type "help {0}" or "{0} '
25 '--help" for detailed help.'.format(cls
.program
)
29 return lldb
.eCommandRequiresFrame | lldb
.eCommandProcessMustBePaused
31 def setup_command_definition(self
):
32 ov_parser
= self
.get_parser()
36 help = "in_scope_only = True",
37 value_type
= lldb
.eArgTypeBoolean
,
45 help = "in_scope_only = True",
46 value_type
= lldb
.eArgTypeBoolean
,
54 help = "arguments = True",
55 value_type
= lldb
.eArgTypeBoolean
,
63 help = "locals = True",
64 value_type
= lldb
.eArgTypeBoolean
,
72 help = "statics = True",
73 value_type
= lldb
.eArgTypeBoolean
,
78 def get_repeat_command(self
, args
):
79 """As an example, make the command not auto-repeat:"""
82 def get_short_help(self
):
83 return "Example command for use in debugging"
85 def get_long_help(self
):
86 return ("This command is meant to be an example of how to make "
87 "an LLDB command that does something useful, follows "
88 "best practices, and exploits the SB API. "
89 "Specifically, this command computes the aggregate "
90 "and average size of the variables in the current "
91 "frame and allows you to tweak exactly which variables "
92 "are to be accounted in the computation.")
95 def __init__(self
, debugger
, unused
):
96 super().__init
__(debugger
, unused
)
98 def __call__(self
, debugger
, command
, exe_ctx
, result
):
99 # Always get program state from the lldb.SBExecutionContext passed
101 frame
= exe_ctx
.GetFrame()
102 if not frame
.IsValid():
103 result
.SetError("invalid frame")
106 ov_parser
= self
.get_parser()
107 variables_list
= frame
.GetVariables(
108 ov_parser
.arguments
, ov_parser
.locals, ov_parser
.statics
, ov_parser
.inscope
110 variables_count
= variables_list
.GetSize()
111 if variables_count
== 0:
112 print("no variables here", file=result
)
115 for i
in range(0, variables_count
):
116 variable
= variables_list
.GetValueAtIndex(i
)
117 variable_type
= variable
.GetType()
118 total_size
= total_size
+ variable_type
.GetByteSize()
119 average_size
= float(total_size
) / variables_count
121 "Your frame has %d variables. Their total size "
122 "is %d bytes. The average size is %f bytes"
123 % (variables_count
, total_size
, average_size
),
126 # not returning anything is akin to returning success
129 def __lldb_init_module(debugger
, dict):
130 # Register all classes that have a register_lldb_command method
131 for _name
, cls
in inspect
.getmembers(sys
.modules
[__name__
]):
132 if inspect
.isclass(cls
) and callable(
133 getattr(cls
, "register_lldb_command", None)
135 cls
.register_lldb_command(debugger
, __name__
)