146 lines · python
1#!/usr/bin/env python2 3# ---------------------------------------------------------------------4# Be sure to add the python path that points to the LLDB shared library.5#6# # To use this in the embedded python interpreter using "lldb" just7# import it with the full path using the "command script import"8# command9# (lldb) command script import /path/to/cmdtemplate.py10# ---------------------------------------------------------------------11 12import inspect13import lldb14import sys15from lldb.plugins.parsed_cmd import ParsedCommand16 17class FrameStatCommand(ParsedCommand):18 program = "framestats"19 20 @classmethod21 def register_lldb_command(cls, debugger, module_name):22 ParsedCommand.do_register_cmd(cls, debugger, module_name)23 print(24 'The "{0}" command has been installed, type "help {0}" or "{0} '25 '--help" for detailed help.'.format(cls.program)26 )27 28 def get_flags(self):29 return lldb.eCommandRequiresFrame | lldb.eCommandProcessMustBePaused30 31 def setup_command_definition(self):32 ov_parser = self.get_parser()33 ov_parser.add_option(34 "i",35 "in-scope",36 help = "in_scope_only = True",37 value_type = lldb.eArgTypeBoolean,38 dest = "bool_arg",39 default = True,40 )41 42 ov_parser.add_option(43 "i",44 "in-scope",45 help = "in_scope_only = True",46 value_type = lldb.eArgTypeBoolean,47 dest = "inscope",48 default=True,49 )50 51 ov_parser.add_option(52 "a",53 "arguments",54 help = "arguments = True",55 value_type = lldb.eArgTypeBoolean,56 dest = "arguments",57 default = True,58 )59 60 ov_parser.add_option(61 "l",62 "locals",63 help = "locals = True",64 value_type = lldb.eArgTypeBoolean,65 dest = "locals",66 default = True,67 )68 69 ov_parser.add_option(70 "s",71 "statics",72 help = "statics = True",73 value_type = lldb.eArgTypeBoolean,74 dest = "statics",75 default = True,76 )77 ov_parser.add_option(78 "t",79 "test-flag",80 help="test a flag value.",81 )82 83 def get_repeat_command(self, args):84 """As an example, make the command not auto-repeat:"""85 return ""86 87 def get_short_help(self):88 return "Example command for use in debugging"89 90 def get_long_help(self):91 return ("This command is meant to be an example of how to make "92 "an LLDB command that does something useful, follows "93 "best practices, and exploits the SB API. "94 "Specifically, this command computes the aggregate "95 "and average size of the variables in the current "96 "frame and allows you to tweak exactly which variables "97 "are to be accounted in the computation.")98 99 100 def __init__(self, debugger, unused):101 super().__init__(debugger, unused)102 103 def __call__(self, debugger, command, exe_ctx, result):104 # Always get program state from the lldb.SBExecutionContext passed105 # in as exe_ctx106 frame = exe_ctx.GetFrame()107 if not frame.IsValid():108 result.SetError("invalid frame")109 return110 111 ov_parser = self.get_parser()112 variables_list = frame.GetVariables(113 ov_parser.arguments, ov_parser.locals, ov_parser.statics, ov_parser.inscope114 )115 variables_count = variables_list.GetSize()116 if variables_count == 0:117 print("no variables here", file=result)118 return119 total_size = 0120 for i in range(0, variables_count):121 variable = variables_list.GetValueAtIndex(i)122 variable_type = variable.GetType()123 total_size = total_size + variable_type.GetByteSize()124 average_size = float(total_size) / variables_count125 print(126 "Your frame has %d variables. Their total size "127 "is %d bytes. The average size is %f bytes"128 % (variables_count, total_size, average_size),129 file=result,130 )131 if ov_parser.was_set("test-flag"):132 print("Got the test flag")133 else:134 print("Got no test flag")135 136 # not returning anything is akin to returning success137 138 139def __lldb_init_module(debugger, dict):140 # Register all classes that have a register_lldb_command method141 for _name, cls in inspect.getmembers(sys.modules[__name__]):142 if inspect.isclass(cls) and callable(143 getattr(cls, "register_lldb_command", None)144 ):145 cls.register_lldb_command(debugger, __name__)146