Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | #---------------------------------------------------------------------- |
| 4 | # Be sure to add the python path that points to the LLDB shared library. |
| 5 | # On MacOSX csh, tcsh: |
| 6 | # setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python |
| 7 | # On MacOSX sh, bash: |
| 8 | # export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python |
| 9 | #---------------------------------------------------------------------- |
| 10 | |
| 11 | import lldb |
| 12 | import commands # commands.getoutput ('/bin/ls /tmp') |
| 13 | import optparse |
| 14 | import os |
| 15 | import plistlib |
| 16 | import pprint |
| 17 | import re |
| 18 | import sys |
| 19 | import time |
| 20 | |
| 21 | PARSE_MODE_NORMAL = 0 |
| 22 | PARSE_MODE_THREAD = 1 |
| 23 | PARSE_MODE_IMAGES = 2 |
| 24 | PARSE_MODE_THREGS = 3 |
| 25 | PARSE_MODE_SYSTEM = 4 |
| 26 | |
| 27 | class CrashLog: |
| 28 | """Class that does parses darwin crash logs""" |
| 29 | thread_state_regex = re.compile('^Thread ([0-9]+) crashed with') |
| 30 | thread_regex = re.compile('^Thread ([0-9]+)([^:]*):(.*)') |
| 31 | frame_regex = re.compile('^([0-9]+) +([^ ]+) +\t(0x[0-9a-fA-F]+) +(.*)') |
| 32 | image_regex_uuid = re.compile('(0x[0-9a-fA-F]+)[- ]+(0x[0-9a-fA-F]+) +([^ ]+) +([^<]+)<([-0-9a-fA-F]+)> (.*)'); |
| 33 | image_regex_no_uuid = re.compile('(0x[0-9a-fA-F]+)[- ]+(0x[0-9a-fA-F]+) +([^ ]+) +([^/]+)/(.*)'); |
| 34 | empty_line_regex = re.compile('^$') |
| 35 | |
| 36 | class Thread: |
| 37 | """Class that represents a thread in a darwin crash log""" |
| 38 | def __init__(self, index): |
| 39 | self.index = index |
| 40 | self.frames = list() |
| 41 | self.registers = dict() |
| 42 | self.reason = None |
| 43 | self.queue = None |
| 44 | |
| 45 | def dump(self, prefix): |
| 46 | print "%sThread[%u] %s" % (prefix, self.index, self.reason) |
| 47 | if self.frames: |
| 48 | print "%s Frames:" % (prefix) |
| 49 | for frame in self.frames: |
| 50 | frame.dump(prefix + ' ') |
| 51 | if self.registers: |
| 52 | print "%s Registers:" % (prefix) |
| 53 | for reg in self.registers.keys(): |
| 54 | print "%s %-5s = %#16.16x" % (prefix, reg, self.registers[reg]) |
| 55 | |
| 56 | def did_crash(self): |
| 57 | return self.reason != None |
| 58 | |
| 59 | def __str__(self): |
| 60 | s = "Thread[%u]" % self.index |
| 61 | if self.reason: |
| 62 | s += ' %s' % self.reason |
| 63 | return s |
| 64 | |
| 65 | |
| 66 | class Frame: |
| 67 | """Class that represents a stack frame in a thread in a darwin crash log""" |
| 68 | def __init__(self, index, pc, details): |
| 69 | self.index = index |
| 70 | self.pc = pc |
| 71 | self.sym_ctx = None |
| 72 | self.details = details |
| 73 | |
| 74 | def __str__(self): |
| 75 | return "[%2u] %#16.16x %s" % (self.index, self.pc, self.details) |
| 76 | |
| 77 | def dump(self, prefix): |
| 78 | print "%s%s" % (prefix, self) |
| 79 | |
| 80 | |
| 81 | class Image: |
| 82 | """Class that represents a binary images in a darwin crash log""" |
| 83 | def __init__(self, text_addr_lo, text_addr_hi, ident, version, uuid, path): |
| 84 | self.text_addr_lo = text_addr_lo |
| 85 | self.text_addr_hi = text_addr_hi |
| 86 | self.ident = ident |
| 87 | self.version = version |
| 88 | self.uuid = uuid |
| 89 | self.path = path |
| 90 | self.target = None |
| 91 | self.module = None |
| 92 | self.plist = None |
| 93 | |
| 94 | def dump(self, prefix): |
| 95 | print "%s%s" % (prefix, self) |
| 96 | |
| 97 | def __str__(self): |
| 98 | return "%#16.16x %s %s" % (self.text_addr_lo, self.uuid, self.path) |
| 99 | |
| 100 | def basename(self): |
| 101 | if self.path: |
| 102 | return os.path.basename(self.path) |
| 103 | return None |
| 104 | |
| 105 | def fetch_symboled_executable_and_dsym(self): |
| 106 | dsym_for_uuid_command = ('~rc/bin/dsymForUUID %s' % self.uuid) |
| 107 | print 'Fetching %s %s...' % (self.uuid, self.path), |
| 108 | s = commands.getoutput(dsym_for_uuid_command) |
| 109 | if s: |
| 110 | plist_root = plistlib.readPlistFromString (s) |
| 111 | if plist_root: |
| 112 | # pp = pprint.PrettyPrinter(indent=4) |
| 113 | # pp.pprint(plist_root) |
| 114 | self.plist = plist_root[self.uuid] |
| 115 | if self.plist: |
| 116 | if 'DBGError' in self.plist: |
| 117 | err = self.plist['DBGError'] |
| 118 | if isinstance(err, unicode): |
| 119 | err = err.encode('utf-8') |
| 120 | print 'error: %s' % err |
| 121 | elif 'DBGSymbolRichExecutable' in self.plist: |
| 122 | self.path = os.path.expanduser (self.plist['DBGSymbolRichExecutable']) |
| 123 | #print 'success: symboled exe is "%s"' % self.path |
| 124 | print 'ok' |
| 125 | return 1 |
| 126 | print 'error: failed to get plist dictionary entry for UUID %s: %s' % (uuid, plist_root) |
| 127 | else: |
| 128 | print 'error: failed to extract plist from "%s"' % (s) |
| 129 | else: |
| 130 | print 'error: %s failed...' % (dsym_for_uuid_command) |
| 131 | return 0 |
| 132 | |
| 133 | def load_module(self): |
| 134 | if self.module: |
| 135 | text_section = self.module.FindSection ("__TEXT") |
| 136 | if text_section: |
| 137 | error = self.target.SetSectionLoadAddress (text_section, self.text_addr_lo) |
| 138 | if error.Success(): |
| 139 | print 'Success: loaded %s.__TEXT = 0x%x' % (self.basename(), self.text_addr_lo) |
| 140 | return None |
| 141 | else: |
| 142 | return 'error: %s' % error.GetCString() |
| 143 | else: |
| 144 | return 'error: unable to find "__TEXT" section in "%s"' % self.path |
| 145 | else: |
| 146 | return 'error: invalid module' |
| 147 | |
| 148 | def create_target(self, debugger): |
| 149 | if self.fetch_symboled_executable_and_dsym (): |
| 150 | path_spec = lldb.SBFileSpec (self.path) |
| 151 | arch = self.plist['DBGArchitecture'] |
| 152 | #print 'debugger.CreateTarget (path="%s", arch="%s") uuid=%s' % (self.path, arch, self.uuid) |
| 153 | #result.PutCString ('plist[%s] = %s' % (uuid, self.plist)) |
| 154 | error = lldb.SBError() |
| 155 | self.target = debugger.CreateTarget (self.path, arch, None, False, error); |
| 156 | if self.target: |
| 157 | self.module = self.target.FindModule (path_spec) |
| 158 | if self.module: |
| 159 | err = self.load_module() |
| 160 | if err: |
| 161 | print err |
| 162 | else: |
| 163 | return None |
| 164 | else: |
| 165 | return 'error: unable to get module for (%s) "%s"' % (arch, self.path) |
| 166 | else: |
| 167 | return 'error: unable to create target for (%s) "%s"' % (arch, self.path) |
| 168 | |
| 169 | def add_target_module(self, target): |
| 170 | if target: |
| 171 | self.target = target |
| 172 | if self.fetch_symboled_executable_and_dsym (): |
| 173 | path_spec = lldb.SBFileSpec (self.path) |
| 174 | arch = self.plist['DBGArchitecture'] |
| 175 | #result.PutCString ('plist[%s] = %s' % (uuid, self.plist)) |
| 176 | #print 'target.AddModule (path="%s", arch="%s", uuid=%s)' % (self.path, arch, self.uuid) |
| 177 | self.module = target.AddModule (self.path, arch, self.uuid) |
| 178 | if self.module: |
| 179 | err = self.load_module() |
| 180 | if err: |
| 181 | print err; |
| 182 | else: |
| 183 | return None |
| 184 | else: |
| 185 | return 'error: unable to get module for (%s) "%s"' % (arch, self.path) |
| 186 | else: |
| 187 | return 'error: invalid target' |
| 188 | |
| 189 | def __init__(self, path): |
| 190 | """CrashLog constructor that take a path to a darwin crash log file""" |
| 191 | self.path = path; |
| 192 | self.info_lines = list() |
| 193 | self.system_profile = list() |
| 194 | self.threads = list() |
| 195 | self.images = list() |
| 196 | self.idents = list() # A list of the required identifiers for doing all stack backtraces |
| 197 | self.crashed_thread_idx = -1 |
| 198 | self.version = -1 |
| 199 | # With possible initial component of ~ or ~user replaced by that user's home directory. |
| 200 | f = open(os.path.expanduser(self.path)) |
| 201 | self.file_lines = f.read().splitlines() |
| 202 | parse_mode = PARSE_MODE_NORMAL |
| 203 | thread = None |
| 204 | for line in self.file_lines: |
| 205 | # print line |
| 206 | line_len = len(line) |
| 207 | if line_len == 0: |
| 208 | if thread: |
| 209 | if parse_mode == PARSE_MODE_THREAD: |
| 210 | if thread.index == self.crashed_thread_idx: |
| 211 | thread.reason = '' |
| 212 | if self.thread_exception: |
| 213 | thread.reason += self.thread_exception |
| 214 | if self.thread_exception_data: |
| 215 | thread.reason += " (%s)" % self.thread_exception_data |
| 216 | self.threads.append(thread) |
| 217 | thread = None |
| 218 | else: |
| 219 | # only append an extra empty line if the previous line |
| 220 | # in the info_lines wasn't empty |
| 221 | if len(self.info_lines) > 0 and len(self.info_lines[-1]): |
| 222 | self.info_lines.append(line) |
| 223 | parse_mode = PARSE_MODE_NORMAL |
| 224 | # print 'PARSE_MODE_NORMAL' |
| 225 | elif parse_mode == PARSE_MODE_NORMAL: |
| 226 | if line.startswith ('Process:'): |
| 227 | (self.process_name, pid_with_brackets) = line[8:].strip().split() |
| 228 | self.process_id = pid_with_brackets.strip('[]') |
| 229 | elif line.startswith ('Path:'): |
| 230 | self.process_path = line[5:].strip() |
| 231 | elif line.startswith ('Identifier:'): |
| 232 | self.process_identifier = line[11:].strip() |
| 233 | elif line.startswith ('Version:'): |
| 234 | (self.process_version, compatability_version) = line[8:].strip().split() |
| 235 | self.process_compatability_version = compatability_version.strip('()') |
| 236 | elif line.startswith ('Parent Process:'): |
| 237 | (self.parent_process_name, pid_with_brackets) = line[15:].strip().split() |
| 238 | self.parent_process_id = pid_with_brackets.strip('[]') |
| 239 | elif line.startswith ('Exception Type:'): |
| 240 | self.thread_exception = line[15:].strip() |
| 241 | continue |
| 242 | elif line.startswith ('Exception Codes:'): |
| 243 | self.thread_exception_data = line[16:].strip() |
| 244 | continue |
| 245 | elif line.startswith ('Crashed Thread:'): |
| 246 | self.crashed_thread_idx = int(line[15:].strip().split()[0]) |
| 247 | continue |
| 248 | elif line.startswith ('Report Version:'): |
| 249 | self.version = int(line[15:].strip()) |
| 250 | continue |
| 251 | elif line.startswith ('System Profile:'): |
| 252 | parse_mode = PARSE_MODE_SYSTEM |
| 253 | continue |
| 254 | elif (line.startswith ('Interval Since Last Report:') or |
| 255 | line.startswith ('Crashes Since Last Report:') or |
| 256 | line.startswith ('Per-App Interval Since Last Report:') or |
| 257 | line.startswith ('Per-App Crashes Since Last Report:') or |
| 258 | line.startswith ('Sleep/Wake UUID:') or |
| 259 | line.startswith ('Anonymous UUID:')): |
| 260 | # ignore these |
| 261 | continue |
| 262 | elif line.startswith ('Thread'): |
| 263 | thread_state_match = self.thread_state_regex.search (line) |
| 264 | if thread_state_match: |
| 265 | thread_state_match = self.thread_regex.search (line) |
| 266 | thread_idx = int(thread_state_match.group(1)) |
| 267 | parse_mode = PARSE_MODE_THREGS |
| 268 | thread = self.threads[thread_idx] |
| 269 | else: |
| 270 | thread_match = self.thread_regex.search (line) |
| 271 | if thread_match: |
| 272 | # print 'PARSE_MODE_THREAD' |
| 273 | parse_mode = PARSE_MODE_THREAD |
| 274 | thread_idx = int(thread_match.group(1)) |
| 275 | thread = CrashLog.Thread(thread_idx) |
| 276 | continue |
| 277 | elif line.startswith ('Binary Images:'): |
| 278 | parse_mode = PARSE_MODE_IMAGES |
| 279 | continue |
| 280 | self.info_lines.append(line.strip()) |
| 281 | elif parse_mode == PARSE_MODE_THREAD: |
| 282 | frame_match = self.frame_regex.search(line) |
| 283 | if frame_match: |
| 284 | ident = frame_match.group(2) |
| 285 | if not ident in self.idents: |
| 286 | self.idents.append(ident) |
| 287 | thread.frames.append (CrashLog.Frame(int(frame_match.group(1)), int(frame_match.group(3), 0), frame_match.group(4))) |
| 288 | else: |
| 289 | print "error: frame regex failed" |
| 290 | elif parse_mode == PARSE_MODE_IMAGES: |
| 291 | image_match = self.image_regex_uuid.search (line) |
| 292 | if image_match: |
| 293 | image = CrashLog.Image (int(image_match.group(1),0), |
| 294 | int(image_match.group(2),0), |
| 295 | image_match.group(3).strip(), |
| 296 | image_match.group(4).strip(), |
| 297 | image_match.group(5), |
| 298 | image_match.group(6)) |
| 299 | self.images.append (image) |
| 300 | else: |
| 301 | image_match = self.image_regex_no_uuid.search (line) |
| 302 | if image_match: |
| 303 | image = CrashLog.Image (int(image_match.group(1),0), |
| 304 | int(image_match.group(2),0), |
| 305 | image_match.group(3).strip(), |
| 306 | image_match.group(4).strip(), |
| 307 | None, |
| 308 | image_match.group(5)) |
| 309 | self.images.append (image) |
| 310 | else: |
| 311 | print "error: image regex failed for: %s" % line |
| 312 | |
| 313 | elif parse_mode == PARSE_MODE_THREGS: |
| 314 | stripped_line = line.strip() |
| 315 | reg_values = stripped_line.split(' ') |
| 316 | for reg_value in reg_values: |
| 317 | (reg, value) = reg_value.split(': ') |
| 318 | thread.registers[reg.strip()] = int(value, 0) |
| 319 | elif parse_mode == PARSE_MODE_SYSTEM: |
| 320 | self.system_profile.append(line) |
| 321 | f.close() |
| 322 | |
| 323 | def dump(self): |
| 324 | print "Crash Log File: %s" % (self.path) |
| 325 | print "\nThreads:" |
| 326 | for thread in self.threads: |
| 327 | thread.dump(' ') |
| 328 | print "\nImages:" |
| 329 | for image in self.images: |
| 330 | image.dump(' ') |
| 331 | |
| 332 | def find_image_with_identifier(self, ident): |
| 333 | for image in self.images: |
| 334 | if image.ident == ident: |
| 335 | return image |
| 336 | return None |
| 337 | |
| 338 | def disassemble_instructions (target, instructions, pc, insts_before_pc, insts_after_pc): |
| 339 | lines = list() |
| 340 | pc_index = -1 |
| 341 | comment_column = 50 |
| 342 | for inst_idx, inst in enumerate(instructions): |
| 343 | inst_pc = inst.GetAddress().GetLoadAddress(target); |
| 344 | if pc == inst_pc: |
| 345 | pc_index = inst_idx |
| 346 | mnemonic = inst.GetMnemonic (target) |
| 347 | operands = inst.GetOperands (target) |
| 348 | comment = inst.GetComment (target) |
| 349 | #data = inst.GetData (target) |
| 350 | lines.append ("%#16.16x: %8s %s" % (inst_pc, mnemonic, operands)) |
| 351 | if comment: |
| 352 | line_len = len(lines[-1]) |
| 353 | if line_len < comment_column: |
| 354 | lines[-1] += ' ' * (comment_column - line_len) |
| 355 | lines[-1] += "; %s" % comment |
| 356 | |
| 357 | if pc_index >= 0: |
| 358 | if pc_index >= insts_before_pc: |
| 359 | start_idx = pc_index - insts_before_pc |
| 360 | else: |
| 361 | start_idx = 0 |
| 362 | end_idx = pc_index + insts_after_pc |
| 363 | if end_idx > inst_idx: |
| 364 | end_idx = inst_idx |
| 365 | for i in range(start_idx, end_idx+1): |
| 366 | if i == pc_index: |
| 367 | print ' -> ', lines[i] |
| 368 | else: |
| 369 | print ' ', lines[i] |
| 370 | |
| 371 | def print_module_section_data (section): |
| 372 | print section |
| 373 | section_data = section.GetSectionData() |
| 374 | if section_data: |
| 375 | ostream = lldb.SBStream() |
| 376 | section_data.GetDescription (ostream, section.GetFileAddress()) |
| 377 | print ostream.GetData() |
| 378 | |
| 379 | def print_module_section (section, depth): |
| 380 | print section |
| 381 | |
| 382 | if depth > 0: |
| 383 | num_sub_sections = section.GetNumSubSections() |
| 384 | for sect_idx in range(num_sub_sections): |
| 385 | print_module_section (section.GetSubSectionAtIndex(sect_idx), depth - 1) |
| 386 | |
| 387 | def print_module_sections (module, depth): |
| 388 | for sect in module.section_iter(): |
| 389 | print_module_section (sect, depth) |
| 390 | |
| 391 | def print_module_symbols (module): |
| 392 | for sym in module: |
| 393 | print sym |
| 394 | |
| 395 | def usage(): |
| 396 | print "Usage: lldb-symbolicate.py [-n name] executable-image" |
| 397 | sys.exit(0) |
| 398 | |
| 399 | def Symbolicate(debugger, command, result, dict): |
| 400 | SymbolicateCrashLog (command.split()) |
| 401 | |
| 402 | def SymbolicateCrashLog(command_args): |
| 403 | |
| 404 | parser = optparse.OptionParser(description='A script that parses skinny and universal mach-o files.') |
| 405 | parser.add_option('--arch', type='string', metavar='arch', dest='triple', help='specify one architecture or target triple') |
| 406 | parser.add_option('--platform', type='string', metavar='platform', dest='platform', help='specify one platform by name') |
| 407 | parser.add_option('--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) |
| 408 | parser.add_option('--interactive', action='store_true', dest='interactive', help='enable interactive mode', default=False) |
| 409 | parser.add_option('--no-images', action='store_false', dest='show_images', help='don\'t show images in stack frames', default=True) |
| 410 | parser.add_option('--no-dependents', action='store_false', dest='dependents', help='skip loading dependent modules', default=True) |
| 411 | parser.add_option('--sections', action='store_true', dest='dump_sections', help='show module sections', default=False) |
| 412 | parser.add_option('--symbols', action='store_true', dest='dump_symbols', help='show module symbols', default=False) |
| 413 | parser.add_option('--image-list', action='store_true', dest='dump_image_list', help='show image list', default=False) |
| 414 | parser.add_option('--debug-delay', type='int', dest='debug_delay', metavar='NSEC', help='pause for NSEC seconds for debugger', default=0) |
| 415 | parser.add_option('--section-depth', type='int', dest='section_depth', help='set the section depth to use when showing sections', default=0) |
| 416 | parser.add_option('--section-data', type='string', action='append', dest='sect_data_names', help='specify sections by name to display data for') |
| 417 | parser.add_option('--address', type='int', action='append', dest='addresses', help='specify addresses to lookup') |
| 418 | parser.add_option('--crash-log', type='string', action='append', dest='crash_log_files', help='specify crash log files to symbolicate') |
| 419 | parser.add_option('--crashed-only', action='store_true', dest='crashed_only', help='only show the crashed thread', default=False) |
| 420 | loaded_addresses = False |
| 421 | (options, args) = parser.parse_args(command_args) |
| 422 | if options.verbose: |
| 423 | print 'options', options |
| 424 | |
| 425 | if options.debug_delay > 0: |
| 426 | print "Waiting %u seconds for debugger to attach..." % options.debug_delay |
| 427 | time.sleep(options.debug_delay) |
| 428 | |
| 429 | # Create a new debugger instance |
| 430 | debugger = lldb.SBDebugger.Create() |
| 431 | |
| 432 | error = lldb.SBError() |
| 433 | |
| 434 | |
| 435 | if options.crash_log_files: |
| 436 | options.dependents = False |
| 437 | for crash_log_file in options.crash_log_files: |
| 438 | crash_log = CrashLog(crash_log_file) |
| 439 | crash_log.dump() |
| 440 | if crash_log.images: |
| 441 | err = crash_log.images[0].create_target (debugger) |
| 442 | if err: |
| 443 | print err |
| 444 | else: |
| 445 | target = crash_log.images[0].target |
| 446 | exe_module = target.GetModuleAtIndex(0) |
| 447 | image_paths = list() |
| 448 | # for i in range (1, len(crash_log.images)): |
| 449 | # image = crash_log.images[i] |
| 450 | for ident in crash_log.idents: |
| 451 | image = crash_log.find_image_with_identifier (ident) |
| 452 | if image: |
| 453 | if image.path in image_paths: |
| 454 | print "warning: skipping %s loaded at %#16.16x duplicate entry (probably commpage)" % (image.path, image.text_addr_lo) |
| 455 | else: |
| 456 | err = image.add_target_module (target) |
| 457 | if err: |
| 458 | print err |
| 459 | else: |
| 460 | image_paths.append(image.path) |
| 461 | else: |
| 462 | print 'error: can\'t find image for identifier "%s"' % ident |
| 463 | |
| 464 | for line in crash_log.info_lines: |
| 465 | print line |
| 466 | |
| 467 | # Reconstruct inlined frames for all threads for anything that has debug info |
| 468 | for thread in crash_log.threads: |
| 469 | if options.crashed_only and thread.did_crash() == False: |
| 470 | continue |
| 471 | # start a new frame list that we will fixup for each thread |
| 472 | new_thread_frames = list() |
| 473 | # Iterate through all concrete frames for a thread and resolve |
| 474 | # any parent frames of inlined functions |
| 475 | for frame_idx, frame in enumerate(thread.frames): |
| 476 | # Resolve the frame's pc into a section + offset address 'pc_addr' |
| 477 | pc_addr = target.ResolveLoadAddress (frame.pc) |
| 478 | # Check to see if we were able to resolve the address |
| 479 | if pc_addr: |
| 480 | # We were able to resolve the frame's PC into a section offset |
| 481 | # address. |
| 482 | |
| 483 | # Resolve the frame's PC value into a symbol context. A symbol |
| 484 | # context can resolve a module, compile unit, function, block, |
| 485 | # line table entry and/or symbol. If the frame has a block, then |
| 486 | # we can look for inlined frames, which are represented by blocks |
| 487 | # that have inlined information in them |
| 488 | frame.sym_ctx = target.ResolveSymbolContextForAddress (pc_addr, lldb.eSymbolContextEverything); |
| 489 | |
| 490 | # dump if the verbose option was specified |
| 491 | if options.verbose: |
| 492 | print "frame.pc = %#16.16x (file_addr = %#16.16x)" % (frame.pc, pc_addr.GetFileAddress()) |
| 493 | print "frame.pc_addr = ", pc_addr |
| 494 | print "frame.sym_ctx = " |
| 495 | print frame.sym_ctx |
| 496 | print |
| 497 | |
| 498 | # Append the frame we already had from the crash log to the new |
| 499 | # frames list |
| 500 | new_thread_frames.append(frame) |
| 501 | |
| 502 | new_frame = CrashLog.Frame (frame.index, -1, None) |
| 503 | |
| 504 | # Try and use the current frame's symbol context to calculate a |
| 505 | # parent frame for an inlined function. If the curent frame is |
| 506 | # inlined, it will return a valid symbol context for the parent |
| 507 | # frame of the current inlined function |
| 508 | parent_pc_addr = lldb.SBAddress() |
| 509 | new_frame.sym_ctx = frame.sym_ctx.GetParentOfInlinedScope (pc_addr, parent_pc_addr) |
| 510 | |
| 511 | # See if we were able to reconstruct anything? |
| 512 | while new_frame.sym_ctx: |
| 513 | # We have a parent frame of an inlined frame, create a new frame |
| 514 | # Convert the section + offset 'parent_pc_addr' to a load address |
| 515 | new_frame.pc = parent_pc_addr.GetLoadAddress(target) |
| 516 | # push the new frame onto the new frame stack |
| 517 | new_thread_frames.append (new_frame) |
| 518 | # dump if the verbose option was specified |
| 519 | if options.verbose: |
| 520 | print "new_frame.pc = %#16.16x (%s)" % (new_frame.pc, parent_pc_addr) |
| 521 | print "new_frame.sym_ctx = " |
| 522 | print new_frame.sym_ctx |
| 523 | print |
| 524 | # Create another new frame in case we have multiple inlined frames |
| 525 | prev_new_frame = new_frame |
| 526 | new_frame = CrashLog.Frame (frame.index, -1, None) |
| 527 | # Swap the addresses so we can try another inlined lookup |
| 528 | pc_addr = parent_pc_addr; |
| 529 | new_frame.sym_ctx = prev_new_frame.sym_ctx.GetParentOfInlinedScope (pc_addr, parent_pc_addr) |
| 530 | # Replace our thread frames with our new list that includes parent |
| 531 | # frames for inlined functions |
| 532 | thread.frames = new_thread_frames |
| 533 | # Now iterate through all threads and display our richer stack backtraces |
| 534 | for thread in crash_log.threads: |
| 535 | this_thread_crashed = thread.did_crash() |
| 536 | if options.crashed_only and this_thread_crashed == False: |
| 537 | continue |
| 538 | print "%s" % thread |
| 539 | prev_frame_index = -1 |
| 540 | for frame_idx, frame in enumerate(thread.frames): |
| 541 | details = ' %s' % frame.details |
| 542 | module = frame.sym_ctx.GetModule() |
| 543 | instructions = None |
| 544 | if module: |
| 545 | module_basename = module.GetFileSpec().GetFilename(); |
| 546 | function_start_load_addr = -1 |
| 547 | function_name = None |
| 548 | function = frame.sym_ctx.GetFunction() |
| 549 | block = frame.sym_ctx.GetBlock() |
| 550 | line_entry = frame.sym_ctx.GetLineEntry() |
| 551 | symbol = frame.sym_ctx.GetSymbol() |
| 552 | inlined_block = block.GetContainingInlinedBlock(); |
| 553 | if inlined_block: |
| 554 | function_name = inlined_block.GetInlinedName(); |
| 555 | block_range_idx = inlined_block.GetRangeIndexForBlockAddress (target.ResolveLoadAddress (frame.pc)) |
| 556 | if block_range_idx < lldb.UINT32_MAX: |
| 557 | block_range_start_addr = inlined_block.GetRangeStartAddress (block_range_idx) |
| 558 | function_start_load_addr = block_range_start_addr.GetLoadAddress (target) |
| 559 | else: |
| 560 | function_start_load_addr = frame.pc |
| 561 | if this_thread_crashed and frame_idx == 0: |
| 562 | instructions = function.GetInstructions(target) |
| 563 | elif function: |
| 564 | function_name = function.GetName() |
| 565 | function_start_load_addr = function.GetStartAddress().GetLoadAddress (target) |
| 566 | if this_thread_crashed and frame_idx == 0: |
| 567 | instructions = function.GetInstructions(target) |
| 568 | elif symbol: |
| 569 | function_name = symbol.GetName() |
| 570 | function_start_load_addr = symbol.GetStartAddress().GetLoadAddress (target) |
| 571 | if this_thread_crashed and frame_idx == 0: |
| 572 | instructions = symbol.GetInstructions(target) |
| 573 | |
| 574 | if function_name: |
| 575 | # Print the function or symbol name and annotate if it was inlined |
| 576 | inline_suffix = '' |
| 577 | if inlined_block: |
| 578 | inline_suffix = '[inlined] ' |
| 579 | else: |
| 580 | inline_suffix = ' ' |
| 581 | if options.show_images: |
| 582 | details = "%s%s`%s" % (inline_suffix, module_basename, function_name) |
| 583 | else: |
| 584 | details = "%s" % (function_name) |
| 585 | # Dump the offset from the current function or symbol if it is non zero |
| 586 | function_offset = frame.pc - function_start_load_addr |
| 587 | if function_offset > 0: |
| 588 | details += " + %u" % (function_offset) |
| 589 | elif function_offset < 0: |
| 590 | defaults += " %i (invalid negative offset, file a bug) " % function_offset |
| 591 | # Print out any line information if any is available |
| 592 | if line_entry.GetFileSpec(): |
| 593 | details += ' at %s' % line_entry.GetFileSpec().GetFilename() |
| 594 | details += ':%u' % line_entry.GetLine () |
| 595 | column = line_entry.GetColumn() |
| 596 | if column > 0: |
| 597 | details += ':%u' % column |
| 598 | |
| 599 | |
| 600 | # Only print out the concrete frame index if it changes. |
| 601 | # if prev_frame_index != frame.index: |
| 602 | # print "[%2u] %#16.16x %s" % (frame.index, frame.pc, details) |
| 603 | # else: |
| 604 | # print " %#16.16x %s" % (frame.pc, details) |
| 605 | print "[%2u] %#16.16x %s" % (frame.index, frame.pc, details) |
| 606 | prev_frame_index = frame.index |
| 607 | if instructions: |
| 608 | print |
| 609 | disassemble_instructions (target, instructions, frame.pc, 4, 4) |
| 610 | print |
| 611 | |
| 612 | print |
| 613 | |
| 614 | if options.dump_image_list: |
| 615 | print "Binary Images:" |
| 616 | for image in crash_log.images: |
| 617 | print image |
| 618 | |
| 619 | |
| 620 | if __name__ == '__main__': |
| 621 | SymbolicateCrashLog (args) |
| 622 | |