symbolication.py revision 1dae6f39248e38ac84fc20c8b4c26e11bfcc19b7
1#!/usr/bin/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": 7# 8# cd /path/containing/crashlog.py 9# lldb 10# (lldb) script import crashlog 11# "crashlog" command installed, type "crashlog --help" for detailed help 12# (lldb) crashlog ~/Library/Logs/DiagnosticReports/a.crash 13# 14# The benefit of running the crashlog command inside lldb in the 15# embedded python interpreter is when the command completes, there 16# will be a target with all of the files loaded at the locations 17# described in the crash log. Only the files that have stack frames 18# in the backtrace will be loaded unless the "--load-all" option 19# has been specified. This allows users to explore the program in the 20# state it was in right at crash time. 21# 22# On MacOSX csh, tcsh: 23# ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash ) 24# 25# On MacOSX sh, bash: 26# PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash 27#---------------------------------------------------------------------- 28 29import lldb 30import commands 31import optparse 32import os 33import plistlib 34import re 35import shlex 36import sys 37import time 38import uuid 39 40class Address: 41 """Class that represents an address that will be symbolicated""" 42 def __init__(self, target, load_addr): 43 self.target = target 44 self.load_addr = load_addr # The load address that this object represents 45 self.so_addr = None # the resolved lldb.SBAddress (if any), named so_addr for section/offset address 46 self.sym_ctx = None # The cached symbol context for this address 47 self.description = None # Any original textual description of this address to be used as a backup in case symbolication fails 48 self.symbolication = None # The cached symbolicated string that describes this address 49 self.inlined = False 50 def __str__(self): 51 s = "%#16.16x" % (self.load_addr) 52 if self.symbolication: 53 s += " %s" % (self.symbolication) 54 elif self.description: 55 s += " %s" % (self.description) 56 elif self.so_addr: 57 s += " %s" % (self.so_addr) 58 return s 59 60 def resolve_addr(self): 61 if self.so_addr == None: 62 self.so_addr = self.target.ResolveLoadAddress (self.load_addr) 63 return self.so_addr 64 65 def is_inlined(self): 66 return self.inlined 67 68 def get_symbol_context(self): 69 if self.sym_ctx == None: 70 sb_addr = self.resolve_addr() 71 if sb_addr: 72 self.sym_ctx = self.target.ResolveSymbolContextForAddress (sb_addr, lldb.eSymbolContextEverything) 73 else: 74 self.sym_ctx = lldb.SBSymbolContext() 75 return self.sym_ctx 76 77 def get_instructions(self): 78 sym_ctx = self.get_symbol_context() 79 if sym_ctx: 80 function = sym_ctx.GetFunction() 81 if function: 82 return function.GetInstructions(self.target) 83 return sym_ctx.GetSymbol().GetInstructions(self.target) 84 return None 85 86 def symbolicate(self): 87 if self.symbolication == None: 88 self.symbolication = '' 89 self.inlined = False 90 sym_ctx = self.get_symbol_context() 91 if sym_ctx: 92 module = sym_ctx.GetModule() 93 if module: 94 self.symbolication += module.GetFileSpec().GetFilename() + '`' 95 function_start_load_addr = -1 96 function = sym_ctx.GetFunction() 97 block = sym_ctx.GetBlock() 98 line_entry = sym_ctx.GetLineEntry() 99 symbol = sym_ctx.GetSymbol() 100 inlined_block = block.GetContainingInlinedBlock(); 101 if function: 102 self.symbolication += function.GetName() 103 104 if inlined_block: 105 self.inlined = True 106 self.symbolication += ' [inlined] ' + inlined_block.GetInlinedName(); 107 block_range_idx = inlined_block.GetRangeIndexForBlockAddress (self.so_addr) 108 if block_range_idx < lldb.UINT32_MAX: 109 block_range_start_addr = inlined_block.GetRangeStartAddress (block_range_idx) 110 function_start_load_addr = block_range_start_addr.GetLoadAddress (self.target) 111 if function_start_load_addr == -1: 112 function_start_load_addr = function.GetStartAddress().GetLoadAddress (self.target) 113 elif symbol: 114 self.symbolication += symbol.GetName() 115 function_start_load_addr = symbol.GetStartAddress().GetLoadAddress (self.target) 116 else: 117 self.symbolication = '' 118 return False 119 120 # Dump the offset from the current function or symbol if it is non zero 121 function_offset = self.load_addr - function_start_load_addr 122 if function_offset > 0: 123 self.symbolication += " + %u" % (function_offset) 124 elif function_offset < 0: 125 self.symbolication += " %i (invalid negative offset, file a bug) " % function_offset 126 127 # Print out any line information if any is available 128 if line_entry.GetFileSpec(): 129 self.symbolication += ' at %s' % line_entry.GetFileSpec().GetFilename() 130 self.symbolication += ':%u' % line_entry.GetLine () 131 column = line_entry.GetColumn() 132 if column > 0: 133 self.symbolication += ':%u' % column 134 return True 135 return False 136 137class Section: 138 """Class that represents an load address range""" 139 sect_info_regex = re.compile('(?P<name>[^=]+)=(?P<range>.*)') 140 addr_regex = re.compile('^\s*(?P<start>0x[0-9A-Fa-f]+)\s*$') 141 range_regex = re.compile('^\s*(?P<start>0x[0-9A-Fa-f]+)\s*(?P<op>[-+])\s*(?P<end>0x[0-9A-Fa-f]+)\s*$') 142 143 def __init__(self, start_addr = None, end_addr = None, name = None): 144 self.start_addr = start_addr 145 self.end_addr = end_addr 146 self.name = name 147 148 def contains(self, addr): 149 return self.start_addr <= addr and addr < self.end_addr; 150 151 def set_from_string(self, s): 152 match = self.sect_info_regex.match (s) 153 if match: 154 self.name = match.group('name') 155 range_str = match.group('range') 156 addr_match = self.addr_regex.match(range_str) 157 if addr_match: 158 self.start_addr = int(addr_match.group('start'), 16) 159 self.end_addr = None 160 return True 161 162 range_match = self.range_regex.match(range_str) 163 if range_match: 164 self.start_addr = int(range_match.group('start'), 16) 165 self.end_addr = int(range_match.group('end'), 16) 166 op = range_match.group('op') 167 if op == '+': 168 self.end_addr += self.start_addr 169 return True 170 print 'error: invalid section info string "%s"' % s 171 print 'Valid section info formats are:' 172 print 'Format Example Description' 173 print '--------------------- -----------------------------------------------' 174 print '<name>=<base> __TEXT=0x123000 Section from base address only' 175 print '<name>=<base>-<end> __TEXT=0x123000-0x124000 Section from base address and end address' 176 print '<name>=<base>+<size> __TEXT=0x123000+0x1000 Section from base address and size' 177 return False 178 179 def __str__(self): 180 if self.name: 181 if self.end_addr != None: 182 if self.start_addr != None: 183 return "%s=[0x%16.16x - 0x%16.16x)" % (self.name, self.start_addr, self.end_addr) 184 else: 185 if self.start_addr != None: 186 return "%s=0x%16.16x" % (self.name, self.start_addr) 187 return self.name 188 return "<invalid>" 189 190class Image: 191 """A class that represents an executable image and any associated data""" 192 193 def __init__(self, path, uuid = None): 194 self.path = path 195 self.resolved_path = None 196 self.uuid = uuid 197 self.section_infos = list() 198 self.identifier = None 199 self.version = None 200 self.arch = None 201 self.module = None 202 self.symfile = None 203 self.slide = None 204 205 def dump(self, prefix): 206 print "%s%s" % (prefix, self) 207 208 def __str__(self): 209 s = "%s %s" % (self.get_uuid(), self.get_resolved_path()) 210 for section_info in self.section_infos: 211 s += ", %s" % (section_info) 212 if self.slide != None: 213 s += ', slide = 0x%16.16x' % self.slide 214 return s 215 216 def add_section(self, section): 217 #print "added '%s' to '%s'" % (section, self.path) 218 self.section_infos.append (section) 219 220 def get_section_containing_load_addr (self, load_addr): 221 for section_info in self.section_infos: 222 if section_info.contains(load_addr): 223 return section_info 224 return None 225 226 def get_resolved_path(self): 227 if self.resolved_path: 228 return self.resolved_path 229 elif self.path: 230 return self.path 231 return None 232 233 def get_resolved_path_basename(self): 234 path = self.get_resolved_path() 235 if path: 236 return os.path.basename(path) 237 return None 238 239 def symfile_basename(self): 240 if self.symfile: 241 return os.path.basename(self.symfile) 242 return None 243 244 def has_section_load_info(self): 245 return self.section_infos or self.slide != None 246 247 def load_module(self, target): 248 # Load this module into "target" using the section infos to 249 # set the section load addresses 250 if self.has_section_load_info(): 251 if target: 252 if self.module: 253 if self.section_infos: 254 num_sections_loaded = 0 255 for section_info in self.section_infos: 256 if section_info.name: 257 section = self.module.FindSection (section_info.name) 258 if section: 259 error = target.SetSectionLoadAddress (section, section_info.start_addr) 260 if error.Success(): 261 num_sections_loaded += 1 262 else: 263 return 'error: %s' % error.GetCString() 264 else: 265 return 'error: unable to find the section named "%s"' % section_info.name 266 else: 267 return 'error: unable to find "%s" section in "%s"' % (range.name, self.get_resolved_path()) 268 if num_sections_loaded == 0: 269 return 'error: no sections were successfully loaded' 270 else: 271 err = target.SetModuleLoadAddress(self.module, self.slide) 272 if err.Fail(): 273 return err.GetCString() 274 return None 275 else: 276 return 'error: invalid module' 277 else: 278 return 'error: invalid target' 279 else: 280 return 'error: no section infos' 281 282 def add_module(self, target): 283 '''Add the Image described in this object to "target" and load the sections if "load" is True.''' 284 if target: 285 # Try and find using UUID only first so that paths need not match up 286 if self.uuid: 287 self.module = target.AddModule (None, None, str(self.uuid)) 288 if not self.module: 289 self.locate_module_and_debug_symbols () 290 resolved_path = self.get_resolved_path() 291 self.module = target.AddModule (resolved_path, self.arch, self.uuid)#, self.symfile) 292 if not self.module: 293 return 'error: unable to get module for (%s) "%s"' % (self.arch, self.get_resolved_path()) 294 if self.has_section_load_info(): 295 return self.load_module(target) 296 else: 297 return None # No sections, the module was added to the target, so success 298 else: 299 return 'error: invalid target' 300 301 def locate_module_and_debug_symbols (self): 302 # By default, just use the paths that were supplied in: 303 # self.path 304 # self.resolved_path 305 # self.module 306 # self.symfile 307 # Subclasses can inherit from this class and override this function 308 return True 309 310 def get_uuid(self): 311 if not self.uuid: 312 self.uuid = uuid.UUID(self.module.GetUUIDString()) 313 return self.uuid 314 315 def create_target(self): 316 '''Create a target using the information in this Image object.''' 317 if self.locate_module_and_debug_symbols (): 318 resolved_path = self.get_resolved_path(); 319 path_spec = lldb.SBFileSpec (resolved_path) 320 #result.PutCString ('plist[%s] = %s' % (uuid, self.plist)) 321 error = lldb.SBError() 322 target = lldb.debugger.CreateTarget (resolved_path, self.arch, None, False, error); 323 if target: 324 self.module = target.FindModule(path_spec) 325 if self.has_section_load_info(): 326 err = self.load_module(target) 327 if err: 328 print 'ERROR: ', err 329 return target 330 else: 331 print 'error: unable to create a valid target for (%s) "%s"' % (self.arch, self.path) 332 else: 333 print 'error: unable to locate main executable (%s) "%s"' % (self.arch, self.path) 334 return None 335 336class Symbolicator: 337 338 def __init__(self): 339 """A class the represents the information needed to symbolicate addresses in a program""" 340 self.target = None 341 self.images = list() # a list of images to be used when symbolicating 342 343 344 def __str__(self): 345 s = "Symbolicator:\n" 346 if self.target: 347 s += "Target = '%s'\n" % (self.target) 348 s += "Target modules:'\n" 349 for m in self.target.modules: 350 print m 351 s += "Images:\n" 352 for image in self.images: 353 s += ' %s\n' % (image) 354 return s 355 356 def find_images_with_identifier(self, identifier): 357 images = list() 358 for image in self.images: 359 if image.identifier == identifier: 360 images.append(image) 361 return images 362 363 def find_image_containing_load_addr(self, load_addr): 364 for image in self.images: 365 if image.contains_addr (load_addr): 366 return image 367 return None 368 369 def create_target(self): 370 if self.target: 371 return self.target 372 373 if self.images: 374 for image in self.images: 375 self.target = image.create_target () 376 if self.target: 377 return self.target 378 return None 379 380 def symbolicate(self, load_addr): 381 if self.target: 382 symbolicated_address = Address(self.target, load_addr) 383 if symbolicated_address.symbolicate (): 384 385 if symbolicated_address.so_addr: 386 symbolicated_addresses = list() 387 symbolicated_addresses.append(symbolicated_address) 388 # See if we were able to reconstruct anything? 389 while 1: 390 inlined_parent_so_addr = lldb.SBAddress() 391 inlined_parent_sym_ctx = symbolicated_address.sym_ctx.GetParentOfInlinedScope (symbolicated_address.so_addr, inlined_parent_so_addr) 392 if not inlined_parent_sym_ctx: 393 break 394 if not inlined_parent_so_addr: 395 break 396 397 symbolicated_address = Address(self.target, inlined_parent_so_addr.GetLoadAddress(self.target)) 398 symbolicated_address.sym_ctx = inlined_parent_sym_ctx 399 symbolicated_address.so_addr = inlined_parent_so_addr 400 symbolicated_address.symbolicate () 401 402 # push the new frame onto the new frame stack 403 symbolicated_addresses.append (symbolicated_address) 404 405 if symbolicated_addresses: 406 return symbolicated_addresses 407 return None 408 409 410def disassemble_instructions (target, instructions, pc, insts_before_pc, insts_after_pc, non_zeroeth_frame): 411 lines = list() 412 pc_index = -1 413 comment_column = 50 414 for inst_idx, inst in enumerate(instructions): 415 inst_pc = inst.GetAddress().GetLoadAddress(target); 416 if pc == inst_pc: 417 pc_index = inst_idx 418 mnemonic = inst.GetMnemonic (target) 419 operands = inst.GetOperands (target) 420 comment = inst.GetComment (target) 421 #data = inst.GetData (target) 422 lines.append ("%#16.16x: %8s %s" % (inst_pc, mnemonic, operands)) 423 if comment: 424 line_len = len(lines[-1]) 425 if line_len < comment_column: 426 lines[-1] += ' ' * (comment_column - line_len) 427 lines[-1] += "; %s" % comment 428 429 if pc_index >= 0: 430 # If we are disassembling the non-zeroeth frame, we need to backup the PC by 1 431 if non_zeroeth_frame and pc_index > 0: 432 pc_index = pc_index - 1 433 if insts_before_pc == -1: 434 start_idx = 0 435 else: 436 start_idx = pc_index - insts_before_pc 437 if start_idx < 0: 438 start_idx = 0 439 if insts_before_pc == -1: 440 end_idx = inst_idx 441 else: 442 end_idx = pc_index + insts_after_pc 443 if end_idx > inst_idx: 444 end_idx = inst_idx 445 for i in range(start_idx, end_idx+1): 446 if i == pc_index: 447 print ' -> ', lines[i] 448 else: 449 print ' ', lines[i] 450 451def print_module_section_data (section): 452 print section 453 section_data = section.GetSectionData() 454 if section_data: 455 ostream = lldb.SBStream() 456 section_data.GetDescription (ostream, section.GetFileAddress()) 457 print ostream.GetData() 458 459def print_module_section (section, depth): 460 print section 461 if depth > 0: 462 num_sub_sections = section.GetNumSubSections() 463 for sect_idx in range(num_sub_sections): 464 print_module_section (section.GetSubSectionAtIndex(sect_idx), depth - 1) 465 466def print_module_sections (module, depth): 467 for sect in module.section_iter(): 468 print_module_section (sect, depth) 469 470def print_module_symbols (module): 471 for sym in module: 472 print sym 473 474def Symbolicate(command_args): 475 476 usage = "usage: %prog [options] <addr1> [addr2 ...]" 477 description='''Symbolicate one or more addresses using LLDB's python scripting API..''' 478 parser = optparse.OptionParser(description=description, prog='crashlog.py',usage=usage) 479 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) 480 parser.add_option('-p', '--platform', type='string', metavar='platform', dest='platform', help='specify one platform by name') 481 parser.add_option('-f', '--file', type='string', metavar='file', dest='file', help='Specify a file to use when symbolicating') 482 parser.add_option('-a', '--arch', type='string', metavar='arch', dest='arch', help='Specify a architecture to use when symbolicating') 483 parser.add_option('-s', '--slide', type='int', metavar='slide', dest='slide', help='Specify the slide to use on the file specified with the --file option', default=None) 484 parser.add_option('--section', type='string', action='append', dest='section_strings', help='specify <sect-name>=<start-addr> or <sect-name>=<start-addr>-<end-addr>') 485 try: 486 (options, args) = parser.parse_args(command_args) 487 except: 488 return 489 symbolicator = Symbolicator() 490 images = list(); 491 if options.file: 492 image = Image(options.file); 493 image.arch = options.arch 494 # Add any sections that were specified with one or more --section options 495 if options.section_strings: 496 for section_str in options.section_strings: 497 section = Section() 498 if section.set_from_string (section_str): 499 image.add_section (section) 500 else: 501 sys.exit(1) 502 if options.slide != None: 503 image.slide = options.slide 504 symbolicator.images.append(image) 505 506 target = symbolicator.create_target() 507 if options.verbose: 508 print symbolicator 509 if target: 510 for addr_str in args: 511 addr = int(addr_str, 0) 512 symbolicated_addrs = symbolicator.symbolicate(addr) 513 for symbolicated_addr in symbolicated_addrs: 514 print symbolicated_addr 515 print 516 else: 517 print 'error: no target for %s' % (symbolicator) 518 519if __name__ == '__main__': 520 # Create a new debugger instance 521 lldb.debugger = lldb.SBDebugger.Create() 522 Symbolicate (sys.argv[1:]) 523