forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathcmdtemplate.py
135 lines (116 loc) · 4.48 KB
/
cmdtemplate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# ---------------------------------------------------------------------
# Be sure to add the python path that points to the LLDB shared library.
#
# # To use this in the embedded python interpreter using "lldb" just
# import it with the full path using the "command script import"
# command
# (lldb) command script import /path/to/cmdtemplate.py
# ---------------------------------------------------------------------
importinspect
importlldb
importsys
fromlldb.plugins.parsed_cmdimportParsedCommand
classFrameStatCommand(ParsedCommand):
program="framestats"
@classmethod
defregister_lldb_command(cls, debugger, module_name):
ParsedCommand.do_register_cmd(cls, debugger, module_name)
print(
'The "{0}" command has been installed, type "help {0}" or "{0} '
'--help" for detailed help.'.format(cls.program)
)
defget_flags(self):
returnlldb.eCommandRequiresFrame|lldb.eCommandProcessMustBePaused
defsetup_command_definition(self):
ov_parser=self.get_parser()
ov_parser.add_option(
"i",
"in-scope",
help="in_scope_only = True",
value_type=lldb.eArgTypeBoolean,
dest="bool_arg",
default=True,
)
ov_parser.add_option(
"i",
"in-scope",
help="in_scope_only = True",
value_type=lldb.eArgTypeBoolean,
dest="inscope",
default=True,
)
ov_parser.add_option(
"a",
"arguments",
help="arguments = True",
value_type=lldb.eArgTypeBoolean,
dest="arguments",
default=True,
)
ov_parser.add_option(
"l",
"locals",
help="locals = True",
value_type=lldb.eArgTypeBoolean,
dest="locals",
default=True,
)
ov_parser.add_option(
"s",
"statics",
help="statics = True",
value_type=lldb.eArgTypeBoolean,
dest="statics",
default=True,
)
defget_repeat_command(self, args):
"""As an example, make the command not auto-repeat:"""
return""
defget_short_help(self):
return"Example command for use in debugging"
defget_long_help(self):
return ("This command is meant to be an example of how to make "
"an LLDB command that does something useful, follows "
"best practices, and exploits the SB API. "
"Specifically, this command computes the aggregate "
"and average size of the variables in the current "
"frame and allows you to tweak exactly which variables "
"are to be accounted in the computation.")
def__init__(self, debugger, unused):
super().__init__(debugger, unused)
def__call__(self, debugger, command, exe_ctx, result):
# Always get program state from the lldb.SBExecutionContext passed
# in as exe_ctx
frame=exe_ctx.GetFrame()
ifnotframe.IsValid():
result.SetError("invalid frame")
return
ov_parser=self.get_parser()
variables_list=frame.GetVariables(
ov_parser.arguments, ov_parser.locals, ov_parser.statics, ov_parser.inscope
)
variables_count=variables_list.GetSize()
ifvariables_count==0:
print("no variables here", file=result)
return
total_size=0
foriinrange(0, variables_count):
variable=variables_list.GetValueAtIndex(i)
variable_type=variable.GetType()
total_size=total_size+variable_type.GetByteSize()
average_size=float(total_size) /variables_count
print(
"Your frame has %d variables. Their total size "
"is %d bytes. The average size is %f bytes"
% (variables_count, total_size, average_size),
file=result,
)
# not returning anything is akin to returning success
def__lldb_init_module(debugger, dict):
# Register all classes that have a register_lldb_command method
for_name, clsininspect.getmembers(sys.modules[__name__]):
ifinspect.isclass(cls) andcallable(
getattr(cls, "register_lldb_command", None)
):
cls.register_lldb_command(debugger, __name__)