llvm-project
131 строка · 4.3 Кб
1#!/usr/bin/env python
2
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" 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# ---------------------------------------------------------------------
11
12import inspect13import lldb14import sys15from lldb.plugins.parsed_cmd import ParsedCommand16
17class FrameStatCommand(ParsedCommand):18program = "framestats"19
20@classmethod21def register_lldb_command(cls, debugger, module_name):22ParsedCommand.do_register_cmd(cls, debugger, module_name)23print(24'The "{0}" command has been installed, type "help {0}" or "{0} '25'--help" for detailed help.'.format(cls.program)26)27
28def setup_command_definition(self):29
30self.ov_parser.add_option(31"i",32"in-scope",33help = "in_scope_only = True",34value_type = lldb.eArgTypeBoolean,35dest = "bool_arg",36default = True,37)38
39self.ov_parser.add_option(40"i",41"in-scope",42help = "in_scope_only = True",43value_type = lldb.eArgTypeBoolean,44dest = "inscope",45default=True,46)47
48self.ov_parser.add_option(49"a",50"arguments",51help = "arguments = True",52value_type = lldb.eArgTypeBoolean,53dest = "arguments",54default = True,55)56
57self.ov_parser.add_option(58"l",59"locals",60help = "locals = True",61value_type = lldb.eArgTypeBoolean,62dest = "locals",63default = True,64)65
66self.ov_parser.add_option(67"s",68"statics",69help = "statics = True",70value_type = lldb.eArgTypeBoolean,71dest = "statics",72default = True,73)74
75def get_repeat_command(self, args):76"""As an example, make the command not auto-repeat:"""77return ""78
79def get_short_help(self):80return "Example command for use in debugging"81
82def get_long_help(self):83return ("This command is meant to be an example of how to make "84"an LLDB command that does something useful, follows "85"best practices, and exploits the SB API. "86"Specifically, this command computes the aggregate "87"and average size of the variables in the current "88"frame and allows you to tweak exactly which variables "89"are to be accounted in the computation.")90
91
92def __init__(self, debugger, unused):93super().__init__(debugger, unused)94
95def __call__(self, debugger, command, exe_ctx, result):96# Always get program state from the lldb.SBExecutionContext passed97# in as exe_ctx98frame = exe_ctx.GetFrame()99if not frame.IsValid():100result.SetError("invalid frame")101return102
103variables_list = frame.GetVariables(104self.ov_parser.arguments, self.ov_parser.locals, self.ov_parser.statics, self.ov_parser.inscope105)106variables_count = variables_list.GetSize()107if variables_count == 0:108print("no variables here", file=result)109return110total_size = 0111for i in range(0, variables_count):112variable = variables_list.GetValueAtIndex(i)113variable_type = variable.GetType()114total_size = total_size + variable_type.GetByteSize()115average_size = float(total_size) / variables_count116print(117"Your frame has %d variables. Their total size "118"is %d bytes. The average size is %f bytes"119% (variables_count, total_size, average_size),120file=result,121)122# not returning anything is akin to returning success123
124
125def __lldb_init_module(debugger, dict):126# Register all classes that have a register_lldb_command method127for _name, cls in inspect.getmembers(sys.modules[__name__]):128if inspect.isclass(cls) and callable(129getattr(cls, "register_lldb_command", None)130):131cls.register_lldb_command(debugger, __name__)132