xref: /llvm-project/lldb/examples/python/lldb_module_utils.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1#!/usr/bin/env python
2
3import lldb
4import optparse
5import shlex
6import string
7import sys
8
9
10class DumpLineTables:
11    command_name = "dump-line-tables"
12    short_description = (
13        "Dumps full paths to compile unit files and optionally all line table files."
14    )
15    description = "Dumps all line tables from all compile units for any modules specified as arguments. Specifying the --verbose flag will output address ranges for each line entry."
16    usage = "usage: %prog [options] MODULE1 [MODULE2 ...]"
17
18    def create_options(self):
19        self.parser = optparse.OptionParser(
20            description=self.description, prog=self.command_name, usage=self.usage
21        )
22
23        self.parser.add_option(
24            "-v",
25            "--verbose",
26            action="store_true",
27            dest="verbose",
28            help="Display verbose output.",
29            default=False,
30        )
31
32    def get_short_help(self):
33        return self.short_description
34
35    def get_long_help(self):
36        return self.help_string
37
38    def __init__(self, debugger, unused):
39        self.create_options()
40        self.help_string = self.parser.format_help()
41
42    def __call__(self, debugger, command, exe_ctx, result):
43        # Use the Shell Lexer to properly parse up command options just like a
44        # shell would
45        command_args = shlex.split(command)
46
47        try:
48            (options, args) = self.parser.parse_args(command_args)
49        except:
50            # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit
51            # (courtesy of OptParse dealing with argument errors by throwing SystemExit)
52            result.SetError("option parsing failed")
53            return
54
55        # Always get program state from the SBExecutionContext passed in as exe_ctx
56        target = exe_ctx.GetTarget()
57        if not target.IsValid():
58            result.SetError("invalid target")
59            return
60
61        for module_path in args:
62            module = target.module[module_path]
63            if not module:
64                result.SetError('no module found that matches "%s".' % (module_path))
65                return
66            num_cus = module.GetNumCompileUnits()
67            print('Module: "%s"' % (module.file.fullpath), end=" ", file=result)
68            if num_cus == 0:
69                print("no debug info.", file=result)
70                continue
71            print("has %u compile units:" % (num_cus), file=result)
72            for cu_idx in range(num_cus):
73                cu = module.GetCompileUnitAtIndex(cu_idx)
74                print("  Compile Unit: %s" % (cu.file.fullpath), file=result)
75                for line_idx in range(cu.GetNumLineEntries()):
76                    line_entry = cu.GetLineEntryAtIndex(line_idx)
77                    start_file_addr = line_entry.addr.file_addr
78                    end_file_addr = line_entry.end_addr.file_addr
79                    # If the two addresses are equal, this line table entry
80                    # is a termination entry
81                    if options.verbose:
82                        if start_file_addr != end_file_addr:
83                            result.PutCString(
84                                "    [%#x - %#x): %s"
85                                % (start_file_addr, end_file_addr, line_entry)
86                            )
87                    else:
88                        if start_file_addr == end_file_addr:
89                            result.PutCString("    %#x: END" % (start_file_addr))
90                        else:
91                            result.PutCString(
92                                "    %#x: %s" % (start_file_addr, line_entry)
93                            )
94                    if start_file_addr == end_file_addr:
95                        result.PutCString("\n")
96
97
98class DumpFiles:
99    command_name = "dump-files"
100    short_description = (
101        "Dumps full paths to compile unit files and optionally all line table files."
102    )
103    usage = "usage: %prog [options] MODULE1 [MODULE2 ...]"
104    description = """This class adds a dump-files command to the LLDB interpreter.
105
106This command will dump all compile unit file paths found for each source file
107for the binaries specified as arguments in the current target. Specify the
108--support-files or -s option to see all file paths that a compile unit uses in
109its lines tables. This is handy for troubleshooting why breakpoints aren't
110working in IDEs that specify full paths to source files when setting file and
111line breakpoints. Sometimes symlinks cause the debug info to contain the symlink
112path and an IDE will resolve the path to the actual file and use the resolved
113path when setting breakpoints.
114"""
115
116    def create_options(self):
117        # Pass add_help_option = False, since this keeps the command in line with lldb commands,
118        # and we wire up "help command" to work by providing the long & short help methods below.
119        self.parser = optparse.OptionParser(
120            description=self.description,
121            prog=self.command_name,
122            usage=self.usage,
123            add_help_option=False,
124        )
125
126        self.parser.add_option(
127            "-s",
128            "--support-files",
129            action="store_true",
130            dest="support_files",
131            help="Dumps full paths to all files used in a compile unit.",
132            default=False,
133        )
134
135    def get_short_help(self):
136        return self.short_description
137
138    def get_long_help(self):
139        return self.help_string
140
141    def __init__(self, debugger, unused):
142        self.create_options()
143        self.help_string = self.parser.format_help()
144
145    def __call__(self, debugger, command, exe_ctx, result):
146        # Use the Shell Lexer to properly parse up command options just like a
147        # shell would
148        command_args = shlex.split(command)
149
150        try:
151            (options, args) = self.parser.parse_args(command_args)
152        except:
153            # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit
154            # (courtesy of OptParse dealing with argument errors by throwing SystemExit)
155            result.SetError("option parsing failed")
156            return
157
158        # Always get program state from the SBExecutionContext passed in as exe_ctx
159        target = exe_ctx.GetTarget()
160        if not target.IsValid():
161            result.SetError("invalid target")
162            return
163
164        if len(args) == 0:
165            result.SetError("one or more executable paths must be specified")
166            return
167
168        for module_path in args:
169            module = target.module[module_path]
170            if not module:
171                result.SetError('no module found that matches "%s".' % (module_path))
172                return
173            num_cus = module.GetNumCompileUnits()
174            print('Module: "%s"' % (module.file.fullpath), end=" ", file=result)
175            if num_cus == 0:
176                print("no debug info.", file=result)
177                continue
178            print("has %u compile units:" % (num_cus), file=result)
179            for i in range(num_cus):
180                cu = module.GetCompileUnitAtIndex(i)
181                print("  Compile Unit: %s" % (cu.file.fullpath), file=result)
182                if options.support_files:
183                    num_support_files = cu.GetNumSupportFiles()
184                    for j in range(num_support_files):
185                        path = cu.GetSupportFileAtIndex(j).fullpath
186                        print("    file[%u]: %s" % (j, path), file=result)
187
188
189def __lldb_init_module(debugger, dict):
190    # This initializer is being run from LLDB in the embedded command interpreter
191
192    # Add any commands contained in this module to LLDB
193    debugger.HandleCommand(
194        "command script add -o -c %s.DumpLineTables %s"
195        % (__name__, DumpLineTables.command_name)
196    )
197    debugger.HandleCommand(
198        "command script add -o -c %s.DumpFiles %s" % (__name__, DumpFiles.command_name)
199    )
200    print(
201        'The "%s" and "%s" commands have been installed.'
202        % (DumpLineTables.command_name, DumpFiles.command_name)
203    )
204