llvm-project

Форк
0
/
cmdtemplate.py 
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

12
import inspect
13
import lldb
14
import sys
15
from lldb.plugins.parsed_cmd import ParsedCommand
16

17
class FrameStatCommand(ParsedCommand):
18
    program = "framestats"
19

20
    @classmethod
21
    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 setup_command_definition(self):
29

30
        self.ov_parser.add_option(
31
            "i",
32
            "in-scope",
33
            help = "in_scope_only = True",
34
            value_type = lldb.eArgTypeBoolean,
35
            dest = "bool_arg",
36
            default = True,
37
        )
38

39
        self.ov_parser.add_option(
40
            "i",
41
            "in-scope",
42
            help = "in_scope_only = True",
43
            value_type = lldb.eArgTypeBoolean,
44
            dest = "inscope",
45
            default=True,
46
        )
47
        
48
        self.ov_parser.add_option(
49
            "a",
50
            "arguments",
51
            help = "arguments = True",
52
            value_type = lldb.eArgTypeBoolean,
53
            dest = "arguments",
54
            default = True,
55
        )
56

57
        self.ov_parser.add_option(
58
            "l",
59
            "locals",
60
            help = "locals = True",
61
            value_type = lldb.eArgTypeBoolean,
62
            dest = "locals",
63
            default = True,
64
        )
65

66
        self.ov_parser.add_option(
67
            "s",
68
            "statics",
69
            help = "statics = True",
70
            value_type = lldb.eArgTypeBoolean,
71
            dest = "statics",
72
            default = True,
73
        )
74

75
    def get_repeat_command(self, args):
76
        """As an example, make the command not auto-repeat:"""
77
        return ""
78

79
    def get_short_help(self):
80
        return "Example command for use in debugging"
81

82
    def get_long_help(self):
83
        return ("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

92
    def __init__(self, debugger, unused):
93
        super().__init__(debugger, unused)
94

95
    def __call__(self, debugger, command, exe_ctx, result):
96
        # Always get program state from the lldb.SBExecutionContext passed
97
        # in as exe_ctx
98
        frame = exe_ctx.GetFrame()
99
        if not frame.IsValid():
100
            result.SetError("invalid frame")
101
            return
102

103
        variables_list = frame.GetVariables(
104
            self.ov_parser.arguments, self.ov_parser.locals, self.ov_parser.statics, self.ov_parser.inscope
105
        )
106
        variables_count = variables_list.GetSize()
107
        if variables_count == 0:
108
            print("no variables here", file=result)
109
            return
110
        total_size = 0
111
        for i in range(0, variables_count):
112
            variable = variables_list.GetValueAtIndex(i)
113
            variable_type = variable.GetType()
114
            total_size = total_size + variable_type.GetByteSize()
115
            average_size = float(total_size) / variables_count
116
            print(
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),
120
                file=result,
121
            )
122
        # not returning anything is akin to returning success
123

124

125
def __lldb_init_module(debugger, dict):
126
    # Register all classes that have a register_lldb_command method
127
    for _name, cls in inspect.getmembers(sys.modules[__name__]):
128
        if inspect.isclass(cls) and callable(
129
            getattr(cls, "register_lldb_command", None)
130
        ):
131
            cls.register_lldb_command(debugger, __name__)
132

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.