Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / examples / python / cmdtemplate.py
bloba3c30f30afea08b829c1f9dd4f5e9dbdd03c9459
1 #!/usr/bin/env python
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"
8 # command
9 # (lldb) command script import /path/to/cmdtemplate.py
10 # ---------------------------------------------------------------------
12 import inspect
13 import lldb
14 import optparse
15 import shlex
16 import sys
19 class FrameStatCommand:
20 program = "framestats"
22 @classmethod
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" % (
28 module_name,
29 cls.__name__,
30 cls.program,
32 debugger.HandleCommand(command)
33 print(
34 'The "{0}" command has been installed, type "help {0}" or "{0} '
35 '--help" for detailed help.'.format(cls.program)
38 @classmethod
39 def create_options(cls):
40 usage = "usage: %prog [options]"
41 description = (
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,
56 prog=cls.program,
57 usage=usage,
58 add_help_option=False,
61 parser.add_option(
62 "-i",
63 "--in-scope",
64 action="store_true",
65 dest="inscope",
66 help="in_scope_only = True",
67 default=True,
70 parser.add_option(
71 "-a",
72 "--arguments",
73 action="store_true",
74 dest="arguments",
75 help="arguments = True",
76 default=True,
79 parser.add_option(
80 "-l",
81 "--locals",
82 action="store_true",
83 dest="locals",
84 help="locals = True",
85 default=True,
88 parser.add_option(
89 "-s",
90 "--statics",
91 action="store_true",
92 dest="statics",
93 help="statics = True",
94 default=True,
97 return parser
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
111 # shell would
112 command_args = shlex.split(command)
114 try:
115 (options, args) = self.parser.parse_args(command_args)
116 except:
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")
121 return
123 # Always get program state from the lldb.SBExecutionContext passed
124 # in as exe_ctx
125 frame = exe_ctx.GetFrame()
126 if not frame.IsValid():
127 result.SetError("invalid frame")
128 return
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)
136 return
137 total_size = 0
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
143 print(
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),
147 file=result,
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__)