204 lines · python
1#!/usr/bin/env python2 3import lldb4import optparse5import shlex6import string7import sys8 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.usage21 )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_description34 35 def get_long_help(self):36 return self.help_string37 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 a44 # shell would45 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 exit51 # (courtesy of OptParse dealing with argument errors by throwing SystemExit)52 result.SetError("option parsing failed")53 return54 55 # Always get program state from the SBExecutionContext passed in as exe_ctx56 target = exe_ctx.GetTarget()57 if not target.IsValid():58 result.SetError("invalid target")59 return60 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 return66 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 continue71 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_addr78 end_file_addr = line_entry.end_addr.file_addr79 # If the two addresses are equal, this line table entry80 # is a termination entry81 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 file107for the binaries specified as arguments in the current target. Specify the108--support-files or -s option to see all file paths that a compile unit uses in109its lines tables. This is handy for troubleshooting why breakpoints aren't110working in IDEs that specify full paths to source files when setting file and111line breakpoints. Sometimes symlinks cause the debug info to contain the symlink112path and an IDE will resolve the path to the actual file and use the resolved113path 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_description137 138 def get_long_help(self):139 return self.help_string140 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 a147 # shell would148 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 exit154 # (courtesy of OptParse dealing with argument errors by throwing SystemExit)155 result.SetError("option parsing failed")156 return157 158 # Always get program state from the SBExecutionContext passed in as exe_ctx159 target = exe_ctx.GetTarget()160 if not target.IsValid():161 result.SetError("invalid target")162 return163 164 if len(args) == 0:165 result.SetError("one or more executable paths must be specified")166 return167 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 return173 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 continue178 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).fullpath186 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 interpreter191 192 # Add any commands contained in this module to LLDB193 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