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": 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 29from __future__ import print_function 30import lldb 31import optparse 32import os 33import plistlib 34import re 35import shlex 36import sys 37import time 38import uuid 39 40 41class Address: 42 """Class that represents an address that will be symbolicated""" 43 44 def __init__(self, target, load_addr): 45 self.target = target 46 self.load_addr = load_addr # The load address that this object represents 47 # the resolved lldb.SBAddress (if any), named so_addr for 48 # section/offset address 49 self.so_addr = None 50 self.sym_ctx = None # The cached symbol context for this address 51 # Any original textual description of this address to be used as a 52 # backup in case symbolication fails 53 self.description = None 54 self.symbolication = None # The cached symbolicated string that describes this address 55 self.inlined = False 56 57 def __str__(self): 58 s = "%#16.16x" % (self.load_addr) 59 if self.symbolication: 60 s += " %s" % (self.symbolication) 61 elif self.description: 62 s += " %s" % (self.description) 63 elif self.so_addr: 64 s += " %s" % (self.so_addr) 65 return s 66 67 def resolve_addr(self): 68 if self.so_addr is None: 69 self.so_addr = self.target.ResolveLoadAddress(self.load_addr) 70 return self.so_addr 71 72 def is_inlined(self): 73 return self.inlined 74 75 def get_symbol_context(self): 76 if self.sym_ctx is None: 77 sb_addr = self.resolve_addr() 78 if sb_addr: 79 self.sym_ctx = self.target.ResolveSymbolContextForAddress( 80 sb_addr, lldb.eSymbolContextEverything) 81 else: 82 self.sym_ctx = lldb.SBSymbolContext() 83 return self.sym_ctx 84 85 def get_instructions(self): 86 sym_ctx = self.get_symbol_context() 87 if sym_ctx: 88 function = sym_ctx.GetFunction() 89 if function: 90 return function.GetInstructions(self.target) 91 return sym_ctx.GetSymbol().GetInstructions(self.target) 92 return None 93 94 def symbolicate(self, verbose=False): 95 if self.symbolication is None: 96 self.symbolication = '' 97 self.inlined = False 98 sym_ctx = self.get_symbol_context() 99 if sym_ctx: 100 module = sym_ctx.GetModule() 101 if module: 102 # Print full source file path in verbose mode 103 if verbose: 104 self.symbolication += str(module.GetFileSpec()) + '`' 105 else: 106 self.symbolication += module.GetFileSpec().GetFilename() + '`' 107 function_start_load_addr = -1 108 function = sym_ctx.GetFunction() 109 block = sym_ctx.GetBlock() 110 line_entry = sym_ctx.GetLineEntry() 111 symbol = sym_ctx.GetSymbol() 112 inlined_block = block.GetContainingInlinedBlock() 113 if function: 114 self.symbolication += function.GetName() 115 116 if inlined_block: 117 self.inlined = True 118 self.symbolication += ' [inlined] ' + \ 119 inlined_block.GetInlinedName() 120 block_range_idx = inlined_block.GetRangeIndexForBlockAddress( 121 self.so_addr) 122 if block_range_idx < lldb.UINT32_MAX: 123 block_range_start_addr = inlined_block.GetRangeStartAddress( 124 block_range_idx) 125 function_start_load_addr = block_range_start_addr.GetLoadAddress( 126 self.target) 127 if function_start_load_addr == -1: 128 function_start_load_addr = function.GetStartAddress().GetLoadAddress(self.target) 129 elif symbol: 130 self.symbolication += symbol.GetName() 131 function_start_load_addr = symbol.GetStartAddress().GetLoadAddress(self.target) 132 else: 133 self.symbolication = '' 134 return False 135 136 # Dump the offset from the current function or symbol if it 137 # is non zero 138 function_offset = self.load_addr - function_start_load_addr 139 if function_offset > 0: 140 self.symbolication += " + %u" % (function_offset) 141 elif function_offset < 0: 142 self.symbolication += " %i (invalid negative offset, file a bug) " % function_offset 143 144 # Print out any line information if any is available 145 if line_entry.GetFileSpec(): 146 # Print full source file path in verbose mode 147 if verbose: 148 self.symbolication += ' at %s' % line_entry.GetFileSpec() 149 else: 150 self.symbolication += ' at %s' % line_entry.GetFileSpec().GetFilename() 151 self.symbolication += ':%u' % line_entry.GetLine() 152 column = line_entry.GetColumn() 153 if column > 0: 154 self.symbolication += ':%u' % column 155 return True 156 return False 157 158 159class Section: 160 """Class that represents an load address range""" 161 sect_info_regex = re.compile('(?P<name>[^=]+)=(?P<range>.*)') 162 addr_regex = re.compile('^\s*(?P<start>0x[0-9A-Fa-f]+)\s*$') 163 range_regex = re.compile( 164 '^\s*(?P<start>0x[0-9A-Fa-f]+)\s*(?P<op>[-+])\s*(?P<end>0x[0-9A-Fa-f]+)\s*$') 165 166 def __init__(self, start_addr=None, end_addr=None, name=None): 167 self.start_addr = start_addr 168 self.end_addr = end_addr 169 self.name = name 170 171 @classmethod 172 def InitWithSBTargetAndSBSection(cls, target, section): 173 sect_load_addr = section.GetLoadAddress(target) 174 if sect_load_addr != lldb.LLDB_INVALID_ADDRESS: 175 obj = cls( 176 sect_load_addr, 177 sect_load_addr + 178 section.size, 179 section.name) 180 return obj 181 else: 182 return None 183 184 def contains(self, addr): 185 return self.start_addr <= addr and addr < self.end_addr 186 187 def set_from_string(self, s): 188 match = self.sect_info_regex.match(s) 189 if match: 190 self.name = match.group('name') 191 range_str = match.group('range') 192 addr_match = self.addr_regex.match(range_str) 193 if addr_match: 194 self.start_addr = int(addr_match.group('start'), 16) 195 self.end_addr = None 196 return True 197 198 range_match = self.range_regex.match(range_str) 199 if range_match: 200 self.start_addr = int(range_match.group('start'), 16) 201 self.end_addr = int(range_match.group('end'), 16) 202 op = range_match.group('op') 203 if op == '+': 204 self.end_addr += self.start_addr 205 return True 206 print('error: invalid section info string "%s"' % s) 207 print('Valid section info formats are:') 208 print('Format Example Description') 209 print('--------------------- -----------------------------------------------') 210 print('<name>=<base> __TEXT=0x123000 Section from base address only') 211 print('<name>=<base>-<end> __TEXT=0x123000-0x124000 Section from base address and end address') 212 print('<name>=<base>+<size> __TEXT=0x123000+0x1000 Section from base address and size') 213 return False 214 215 def __str__(self): 216 if self.name: 217 if self.end_addr is not None: 218 if self.start_addr is not None: 219 return "%s=[0x%16.16x - 0x%16.16x)" % ( 220 self.name, self.start_addr, self.end_addr) 221 else: 222 if self.start_addr is not None: 223 return "%s=0x%16.16x" % (self.name, self.start_addr) 224 return self.name 225 return "<invalid>" 226 227 228class Image: 229 """A class that represents an executable image and any associated data""" 230 231 def __init__(self, path, uuid=None): 232 self.path = path 233 self.resolved_path = None 234 self.resolved = False 235 self.unavailable = False 236 self.uuid = uuid 237 self.section_infos = list() 238 self.identifier = None 239 self.version = None 240 self.arch = None 241 self.module = None 242 self.symfile = None 243 self.slide = None 244 245 @classmethod 246 def InitWithSBTargetAndSBModule(cls, target, module): 247 '''Initialize this Image object with a module from a target.''' 248 obj = cls(module.file.fullpath, module.uuid) 249 obj.resolved_path = module.platform_file.fullpath 250 obj.resolved = True 251 for section in module.sections: 252 symb_section = Section.InitWithSBTargetAndSBSection( 253 target, section) 254 if symb_section: 255 obj.section_infos.append(symb_section) 256 obj.arch = module.triple 257 obj.module = module 258 obj.symfile = None 259 obj.slide = None 260 return obj 261 262 def dump(self, prefix): 263 print("%s%s" % (prefix, self)) 264 265 def debug_dump(self): 266 print('path = "%s"' % (self.path)) 267 print('resolved_path = "%s"' % (self.resolved_path)) 268 print('resolved = %i' % (self.resolved)) 269 print('unavailable = %i' % (self.unavailable)) 270 print('uuid = %s' % (self.uuid)) 271 print('section_infos = %s' % (self.section_infos)) 272 print('identifier = "%s"' % (self.identifier)) 273 print('version = %s' % (self.version)) 274 print('arch = %s' % (self.arch)) 275 print('module = %s' % (self.module)) 276 print('symfile = "%s"' % (self.symfile)) 277 print('slide = %i (0x%x)' % (self.slide, self.slide)) 278 279 def __str__(self): 280 s = '' 281 if self.uuid: 282 s += "%s " % (self.get_uuid()) 283 if self.arch: 284 s += "%s " % (self.arch) 285 if self.version: 286 s += "%s " % (self.version) 287 resolved_path = self.get_resolved_path() 288 if resolved_path: 289 s += "%s " % (resolved_path) 290 for section_info in self.section_infos: 291 s += ", %s" % (section_info) 292 if self.slide is not None: 293 s += ', slide = 0x%16.16x' % self.slide 294 return s 295 296 def add_section(self, section): 297 # print "added '%s' to '%s'" % (section, self.path) 298 self.section_infos.append(section) 299 300 def get_section_containing_load_addr(self, load_addr): 301 for section_info in self.section_infos: 302 if section_info.contains(load_addr): 303 return section_info 304 return None 305 306 def get_resolved_path(self): 307 if self.resolved_path: 308 return self.resolved_path 309 elif self.path: 310 return self.path 311 return None 312 313 def get_resolved_path_basename(self): 314 path = self.get_resolved_path() 315 if path: 316 return os.path.basename(path) 317 return None 318 319 def symfile_basename(self): 320 if self.symfile: 321 return os.path.basename(self.symfile) 322 return None 323 324 def has_section_load_info(self): 325 return self.section_infos or self.slide is not None 326 327 def load_module(self, target): 328 if self.unavailable: 329 return None # We already warned that we couldn't find this module, so don't return an error string 330 # Load this module into "target" using the section infos to 331 # set the section load addresses 332 if self.has_section_load_info(): 333 if target: 334 if self.module: 335 if self.section_infos: 336 num_sections_loaded = 0 337 for section_info in self.section_infos: 338 if section_info.name: 339 section = self.module.FindSection( 340 section_info.name) 341 if section: 342 error = target.SetSectionLoadAddress( 343 section, section_info.start_addr) 344 if error.Success(): 345 num_sections_loaded += 1 346 else: 347 return 'error: %s' % error.GetCString() 348 else: 349 return 'error: unable to find the section named "%s"' % section_info.name 350 else: 351 return 'error: unable to find "%s" section in "%s"' % ( 352 range.name, self.get_resolved_path()) 353 if num_sections_loaded == 0: 354 return 'error: no sections were successfully loaded' 355 else: 356 err = target.SetModuleLoadAddress( 357 self.module, self.slide) 358 if err.Fail(): 359 return err.GetCString() 360 return None 361 else: 362 return 'error: invalid module' 363 else: 364 return 'error: invalid target' 365 else: 366 return 'error: no section infos' 367 368 def add_module(self, target): 369 '''Add the Image described in this object to "target" and load the sections if "load" is True.''' 370 if target: 371 # Try and find using UUID only first so that paths need not match 372 # up 373 uuid_str = self.get_normalized_uuid_string() 374 if uuid_str: 375 self.module = target.AddModule(None, None, uuid_str) 376 if not self.module: 377 self.locate_module_and_debug_symbols() 378 if self.unavailable: 379 return None 380 resolved_path = self.get_resolved_path() 381 self.module = target.AddModule( 382 resolved_path, str(self.arch), uuid_str, self.symfile) 383 if not self.module: 384 return 'error: unable to get module for (%s) "%s"' % ( 385 self.arch, self.get_resolved_path()) 386 if self.has_section_load_info(): 387 return self.load_module(target) 388 else: 389 return None # No sections, the module was added to the target, so success 390 else: 391 return 'error: invalid target' 392 393 def locate_module_and_debug_symbols(self): 394 # By default, just use the paths that were supplied in: 395 # self.path 396 # self.resolved_path 397 # self.module 398 # self.symfile 399 # Subclasses can inherit from this class and override this function 400 self.resolved = True 401 return True 402 403 def get_uuid(self): 404 if not self.uuid and self.module: 405 self.uuid = uuid.UUID(self.module.GetUUIDString()) 406 return self.uuid 407 408 def get_normalized_uuid_string(self): 409 if self.uuid: 410 return str(self.uuid).upper() 411 return None 412 413 def create_target(self, debugger): 414 '''Create a target using the information in this Image object.''' 415 if self.unavailable: 416 return None 417 418 if self.locate_module_and_debug_symbols(): 419 resolved_path = self.get_resolved_path() 420 path_spec = lldb.SBFileSpec(resolved_path) 421 error = lldb.SBError() 422 target = debugger.CreateTarget( 423 resolved_path, self.arch, None, False, error) 424 if target: 425 self.module = target.FindModule(path_spec) 426 if self.has_section_load_info(): 427 err = self.load_module(target) 428 if err: 429 print('ERROR: ', err) 430 return target 431 else: 432 print('error: unable to create a valid target for (%s) "%s"' % (self.arch, self.path)) 433 else: 434 print('error: unable to locate main executable (%s) "%s"' % (self.arch, self.path)) 435 return None 436 437 438class Symbolicator: 439 440 def __init__(self, debugger=None, target=None, images=list()): 441 """A class the represents the information needed to symbolicate 442 addresses in a program. 443 444 Do not call this initializer directly, but rather use the factory 445 methods. 446 """ 447 self.debugger = debugger 448 self.target = target 449 self.images = images # a list of images to be used when symbolicating 450 self.addr_mask = 0xffffffffffffffff 451 452 @classmethod 453 def InitWithSBTarget(cls, target): 454 """Initialize a new Symbolicator with an existing SBTarget.""" 455 obj = cls(target=target) 456 triple = target.triple 457 if triple: 458 arch = triple.split('-')[0] 459 if "arm" in arch: 460 obj.addr_mask = 0xfffffffffffffffe 461 462 for module in target.modules: 463 image = Image.InitWithSBTargetAndSBModule(target, module) 464 obj.images.append(image) 465 return obj 466 467 @classmethod 468 def InitWithSBDebugger(cls, debugger, images): 469 """Initialize a new Symbolicator with an existing debugger and list of 470 images. The Symbolicator will create the target.""" 471 obj = cls(debugger=debugger, images=images) 472 return obj 473 474 def __str__(self): 475 s = "Symbolicator:\n" 476 if self.target: 477 s += "Target = '%s'\n" % (self.target) 478 s += "Target modules:\n" 479 for m in self.target.modules: 480 s += str(m) + "\n" 481 s += "Images:\n" 482 for image in self.images: 483 s += ' %s\n' % (image) 484 return s 485 486 def find_images_with_identifier(self, identifier): 487 images = list() 488 for image in self.images: 489 if image.identifier == identifier: 490 images.append(image) 491 if len(images) == 0: 492 regex_text = '^.*\.%s$' % (re.escape(identifier)) 493 regex = re.compile(regex_text) 494 for image in self.images: 495 if regex.match(image.identifier): 496 images.append(image) 497 return images 498 499 def find_image_containing_load_addr(self, load_addr): 500 for image in self.images: 501 if image.get_section_containing_load_addr(load_addr): 502 return image 503 return None 504 505 def create_target(self): 506 if self.target: 507 return self.target 508 509 if self.images: 510 for image in self.images: 511 self.target = image.create_target(self.debugger) 512 if self.target: 513 if self.target.GetAddressByteSize() == 4: 514 triple = self.target.triple 515 if triple: 516 arch = triple.split('-')[0] 517 if "arm" in arch: 518 self.addr_mask = 0xfffffffffffffffe 519 return self.target 520 return None 521 522 def symbolicate(self, load_addr, verbose=False): 523 if not self.target: 524 self.create_target() 525 if self.target: 526 live_process = False 527 process = self.target.process 528 if process: 529 state = process.state 530 if state > lldb.eStateUnloaded and state < lldb.eStateDetached: 531 live_process = True 532 # If we don't have a live process, we can attempt to find the image 533 # that a load address belongs to and lazily load its module in the 534 # target, but we shouldn't do any of this if we have a live process 535 if not live_process: 536 image = self.find_image_containing_load_addr(load_addr) 537 if image: 538 image.add_module(self.target) 539 symbolicated_address = Address(self.target, load_addr) 540 if symbolicated_address.symbolicate(verbose): 541 if symbolicated_address.so_addr: 542 symbolicated_addresses = list() 543 symbolicated_addresses.append(symbolicated_address) 544 # See if we were able to reconstruct anything? 545 while True: 546 inlined_parent_so_addr = lldb.SBAddress() 547 inlined_parent_sym_ctx = symbolicated_address.sym_ctx.GetParentOfInlinedScope( 548 symbolicated_address.so_addr, inlined_parent_so_addr) 549 if not inlined_parent_sym_ctx: 550 break 551 if not inlined_parent_so_addr: 552 break 553 554 symbolicated_address = Address( 555 self.target, inlined_parent_so_addr.GetLoadAddress( 556 self.target)) 557 symbolicated_address.sym_ctx = inlined_parent_sym_ctx 558 symbolicated_address.so_addr = inlined_parent_so_addr 559 symbolicated_address.symbolicate(verbose) 560 561 # push the new frame onto the new frame stack 562 symbolicated_addresses.append(symbolicated_address) 563 564 if symbolicated_addresses: 565 return symbolicated_addresses 566 else: 567 print('error: no target in Symbolicator') 568 return None 569 570 571def disassemble_instructions( 572 target, 573 instructions, 574 pc, 575 insts_before_pc, 576 insts_after_pc, 577 non_zeroeth_frame): 578 lines = list() 579 pc_index = -1 580 comment_column = 50 581 for inst_idx, inst in enumerate(instructions): 582 inst_pc = inst.GetAddress().GetLoadAddress(target) 583 if pc == inst_pc: 584 pc_index = inst_idx 585 mnemonic = inst.GetMnemonic(target) 586 operands = inst.GetOperands(target) 587 comment = inst.GetComment(target) 588 lines.append("%#16.16x: %8s %s" % (inst_pc, mnemonic, operands)) 589 if comment: 590 line_len = len(lines[-1]) 591 if line_len < comment_column: 592 lines[-1] += ' ' * (comment_column - line_len) 593 lines[-1] += "; %s" % comment 594 595 if pc_index >= 0: 596 # If we are disassembling the non-zeroeth frame, we need to backup the 597 # PC by 1 598 if non_zeroeth_frame and pc_index > 0: 599 pc_index = pc_index - 1 600 if insts_before_pc == -1: 601 start_idx = 0 602 else: 603 start_idx = pc_index - insts_before_pc 604 if start_idx < 0: 605 start_idx = 0 606 if insts_before_pc == -1: 607 end_idx = inst_idx 608 else: 609 end_idx = pc_index + insts_after_pc 610 if end_idx > inst_idx: 611 end_idx = inst_idx 612 for i in range(start_idx, end_idx + 1): 613 if i == pc_index: 614 print(' -> ', lines[i]) 615 else: 616 print(' ', lines[i]) 617 618 619def print_module_section_data(section): 620 print(section) 621 section_data = section.GetSectionData() 622 if section_data: 623 ostream = lldb.SBStream() 624 section_data.GetDescription(ostream, section.GetFileAddress()) 625 print(ostream.GetData()) 626 627 628def print_module_section(section, depth): 629 print(section) 630 if depth > 0: 631 num_sub_sections = section.GetNumSubSections() 632 for sect_idx in range(num_sub_sections): 633 print_module_section( 634 section.GetSubSectionAtIndex(sect_idx), depth - 1) 635 636 637def print_module_sections(module, depth): 638 for sect in module.section_iter(): 639 print_module_section(sect, depth) 640 641 642def print_module_symbols(module): 643 for sym in module: 644 print(sym) 645 646 647def Symbolicate(debugger, command_args): 648 649 usage = "usage: %prog [options] <addr1> [addr2 ...]" 650 description = '''Symbolicate one or more addresses using LLDB's python scripting API..''' 651 parser = optparse.OptionParser( 652 description=description, 653 prog='crashlog.py', 654 usage=usage) 655 parser.add_option( 656 '-v', 657 '--verbose', 658 action='store_true', 659 dest='verbose', 660 help='display verbose debug info', 661 default=False) 662 parser.add_option( 663 '-p', 664 '--platform', 665 type='string', 666 metavar='platform', 667 dest='platform', 668 help='Specify the platform to use when creating the debug target. Valid values include "localhost", "darwin-kernel", "ios-simulator", "remote-freebsd", "remote-macosx", "remote-ios", "remote-linux".') 669 parser.add_option( 670 '-f', 671 '--file', 672 type='string', 673 metavar='file', 674 dest='file', 675 help='Specify a file to use when symbolicating') 676 parser.add_option( 677 '-a', 678 '--arch', 679 type='string', 680 metavar='arch', 681 dest='arch', 682 help='Specify a architecture to use when symbolicating') 683 parser.add_option( 684 '-s', 685 '--slide', 686 type='int', 687 metavar='slide', 688 dest='slide', 689 help='Specify the slide to use on the file specified with the --file option', 690 default=None) 691 parser.add_option( 692 '--section', 693 type='string', 694 action='append', 695 dest='section_strings', 696 help='specify <sect-name>=<start-addr> or <sect-name>=<start-addr>-<end-addr>') 697 try: 698 (options, args) = parser.parse_args(command_args) 699 except: 700 return 701 symbolicator = Symbolicator(debugger) 702 images = list() 703 if options.file: 704 image = Image(options.file) 705 image.arch = options.arch 706 # Add any sections that were specified with one or more --section 707 # options 708 if options.section_strings: 709 for section_str in options.section_strings: 710 section = Section() 711 if section.set_from_string(section_str): 712 image.add_section(section) 713 else: 714 sys.exit(1) 715 if options.slide is not None: 716 image.slide = options.slide 717 symbolicator.images.append(image) 718 719 target = symbolicator.create_target() 720 if options.verbose: 721 print(symbolicator) 722 if target: 723 for addr_str in args: 724 addr = int(addr_str, 0) 725 symbolicated_addrs = symbolicator.symbolicate( 726 addr, options.verbose) 727 for symbolicated_addr in symbolicated_addrs: 728 print(symbolicated_addr) 729 print() 730 else: 731 print('error: no target for %s' % (symbolicator)) 732 733if __name__ == '__main__': 734 # Create a new debugger instance 735 debugger = lldb.SBDebugger.Create() 736 Symbolicate(debugger, sys.argv[1:]) 737 SBDebugger.Destroy(debugger) 738