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. |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 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 | # |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 22 | # On MacOSX csh, tcsh: |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 23 | # ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash ) |
| 24 | # |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 25 | # On MacOSX sh, bash: |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 26 | # PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 27 | #---------------------------------------------------------------------- |
| 28 | |
| 29 | import lldb |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 30 | import commands |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 31 | import cmd |
| 32 | import glob |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 33 | import optparse |
| 34 | import os |
| 35 | import plistlib |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 36 | import pprint # pp = pprint.PrettyPrinter(indent=4); pp.pprint(command_args) |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 37 | import re |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 38 | import shlex |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 39 | import string |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 40 | import sys |
| 41 | import time |
Greg Clayton | cd79312 | 2012-01-20 06:12:47 +0000 | [diff] [blame] | 42 | import uuid |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 43 | from lldb.utils import symbolication |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 44 | |
| 45 | PARSE_MODE_NORMAL = 0 |
| 46 | PARSE_MODE_THREAD = 1 |
| 47 | PARSE_MODE_IMAGES = 2 |
| 48 | PARSE_MODE_THREGS = 3 |
| 49 | PARSE_MODE_SYSTEM = 4 |
| 50 | |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 51 | class CrashLog(symbolication.Symbolicator): |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 52 | """Class that does parses darwin crash logs""" |
| 53 | thread_state_regex = re.compile('^Thread ([0-9]+) crashed with') |
| 54 | thread_regex = re.compile('^Thread ([0-9]+)([^:]*):(.*)') |
Greg Clayton | 8077a53 | 2012-01-20 03:32:35 +0000 | [diff] [blame] | 55 | frame_regex = re.compile('^([0-9]+) +([^ ]+) *\t(0x[0-9a-fA-F]+) +(.*)') |
| 56 | image_regex_uuid = re.compile('(0x[0-9a-fA-F]+)[- ]+(0x[0-9a-fA-F]+) +[+]?([^ ]+) +([^<]+)<([-0-9a-fA-F]+)> (.*)'); |
| 57 | image_regex_no_uuid = re.compile('(0x[0-9a-fA-F]+)[- ]+(0x[0-9a-fA-F]+) +[+]?([^ ]+) +([^/]+)/(.*)'); |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 58 | empty_line_regex = re.compile('^$') |
| 59 | |
| 60 | class Thread: |
| 61 | """Class that represents a thread in a darwin crash log""" |
| 62 | def __init__(self, index): |
| 63 | self.index = index |
| 64 | self.frames = list() |
| 65 | self.registers = dict() |
| 66 | self.reason = None |
| 67 | self.queue = None |
| 68 | |
| 69 | def dump(self, prefix): |
| 70 | print "%sThread[%u] %s" % (prefix, self.index, self.reason) |
| 71 | if self.frames: |
| 72 | print "%s Frames:" % (prefix) |
| 73 | for frame in self.frames: |
| 74 | frame.dump(prefix + ' ') |
| 75 | if self.registers: |
| 76 | print "%s Registers:" % (prefix) |
| 77 | for reg in self.registers.keys(): |
| 78 | print "%s %-5s = %#16.16x" % (prefix, reg, self.registers[reg]) |
| 79 | |
| 80 | def did_crash(self): |
| 81 | return self.reason != None |
| 82 | |
| 83 | def __str__(self): |
| 84 | s = "Thread[%u]" % self.index |
| 85 | if self.reason: |
| 86 | s += ' %s' % self.reason |
| 87 | return s |
| 88 | |
| 89 | |
| 90 | class Frame: |
| 91 | """Class that represents a stack frame in a thread in a darwin crash log""" |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 92 | def __init__(self, index, pc, description): |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 93 | self.pc = pc |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 94 | self.description = description |
| 95 | self.index = index |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 96 | |
| 97 | def __str__(self): |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 98 | if self.description: |
| 99 | return "[%3u] 0x%16.16x %s" % (self.index, self.pc, self.description) |
| 100 | else: |
Johnny Chen | 4e46867 | 2012-05-03 18:46:28 +0000 | [diff] [blame] | 101 | return "[%3u] 0x%16.16x" % (self.index, self.pc) |
| 102 | |
| 103 | def dump(self, prefix): |
| 104 | print "%s%s" % (prefix, str(self)) |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 105 | |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 106 | class DarwinImage(symbolication.Image): |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 107 | """Class that represents a binary images in a darwin crash log""" |
Greg Clayton | 8077a53 | 2012-01-20 03:32:35 +0000 | [diff] [blame] | 108 | dsymForUUIDBinary = os.path.expanduser('~rc/bin/dsymForUUID') |
Greg Clayton | cd79312 | 2012-01-20 06:12:47 +0000 | [diff] [blame] | 109 | if not os.path.exists(dsymForUUIDBinary): |
| 110 | dsymForUUIDBinary = commands.getoutput('which dsymForUUID') |
| 111 | |
| 112 | dwarfdump_uuid_regex = re.compile('UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*') |
Greg Clayton | 8077a53 | 2012-01-20 03:32:35 +0000 | [diff] [blame] | 113 | |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 114 | def __init__(self, text_addr_lo, text_addr_hi, identifier, version, uuid, path): |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 115 | symbolication.Image.__init__(self, path, uuid); |
| 116 | self.add_section (symbolication.Section(text_addr_lo, text_addr_hi, "__TEXT")) |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 117 | self.identifier = identifier |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 118 | self.version = version |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 119 | |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 120 | def locate_module_and_debug_symbols(self): |
Greg Clayton | cd79312 | 2012-01-20 06:12:47 +0000 | [diff] [blame] | 121 | if self.resolved_path: |
| 122 | # Don't load a module twice... |
Greg Clayton | 4c983c8 | 2012-04-20 23:31:27 +0000 | [diff] [blame] | 123 | return True |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 124 | print 'Getting symbols for %s %s...' % (self.uuid, self.path), |
Greg Clayton | 8077a53 | 2012-01-20 03:32:35 +0000 | [diff] [blame] | 125 | if os.path.exists(self.dsymForUUIDBinary): |
| 126 | dsym_for_uuid_command = '%s %s' % (self.dsymForUUIDBinary, self.uuid) |
Greg Clayton | 8077a53 | 2012-01-20 03:32:35 +0000 | [diff] [blame] | 127 | s = commands.getoutput(dsym_for_uuid_command) |
| 128 | if s: |
| 129 | plist_root = plistlib.readPlistFromString (s) |
| 130 | if plist_root: |
Greg Clayton | cd79312 | 2012-01-20 06:12:47 +0000 | [diff] [blame] | 131 | plist = plist_root[self.uuid] |
| 132 | if plist: |
| 133 | if 'DBGArchitecture' in plist: |
| 134 | self.arch = plist['DBGArchitecture'] |
| 135 | if 'DBGDSYMPath' in plist: |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 136 | self.symfile = os.path.realpath(plist['DBGDSYMPath']) |
Greg Clayton | cd79312 | 2012-01-20 06:12:47 +0000 | [diff] [blame] | 137 | if 'DBGSymbolRichExecutable' in plist: |
| 138 | self.resolved_path = os.path.expanduser (plist['DBGSymbolRichExecutable']) |
| 139 | if not self.resolved_path and os.path.exists(self.path): |
| 140 | dwarfdump_cmd_output = commands.getoutput('dwarfdump --uuid "%s"' % self.path) |
| 141 | self_uuid = uuid.UUID(self.uuid) |
| 142 | for line in dwarfdump_cmd_output.splitlines(): |
| 143 | match = self.dwarfdump_uuid_regex.search (line) |
| 144 | if match: |
| 145 | dwarf_uuid_str = match.group(1) |
| 146 | dwarf_uuid = uuid.UUID(dwarf_uuid_str) |
| 147 | if self_uuid == dwarf_uuid: |
| 148 | self.resolved_path = self.path |
| 149 | self.arch = match.group(2) |
| 150 | break; |
| 151 | if not self.resolved_path: |
| 152 | print "error: file %s '%s' doesn't match the UUID in the installed file" % (self.uuid, self.path) |
Greg Clayton | 4c983c8 | 2012-04-20 23:31:27 +0000 | [diff] [blame] | 153 | return False |
Greg Clayton | cd79312 | 2012-01-20 06:12:47 +0000 | [diff] [blame] | 154 | if (self.resolved_path and os.path.exists(self.resolved_path)) or (self.path and os.path.exists(self.path)): |
| 155 | print 'ok' |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 156 | # if self.resolved_path: |
| 157 | # print ' exe = "%s"' % self.resolved_path |
| 158 | # if self.symfile: |
| 159 | # print ' dsym = "%s"' % self.symfile |
Greg Clayton | 4c983c8 | 2012-04-20 23:31:27 +0000 | [diff] [blame] | 160 | return True |
| 161 | return False |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 162 | |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 163 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 164 | |
| 165 | def __init__(self, path): |
| 166 | """CrashLog constructor that take a path to a darwin crash log file""" |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 167 | symbolication.Symbolicator.__init__(self); |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 168 | self.path = os.path.expanduser(path); |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 169 | self.info_lines = list() |
| 170 | self.system_profile = list() |
| 171 | self.threads = list() |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 172 | self.idents = list() # A list of the required identifiers for doing all stack backtraces |
| 173 | self.crashed_thread_idx = -1 |
| 174 | self.version = -1 |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 175 | self.error = None |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 176 | # With possible initial component of ~ or ~user replaced by that user's home directory. |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 177 | try: |
| 178 | f = open(self.path) |
| 179 | except IOError: |
| 180 | self.error = 'error: cannot open "%s"' % self.path |
| 181 | return |
| 182 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 183 | self.file_lines = f.read().splitlines() |
| 184 | parse_mode = PARSE_MODE_NORMAL |
| 185 | thread = None |
| 186 | for line in self.file_lines: |
| 187 | # print line |
| 188 | line_len = len(line) |
| 189 | if line_len == 0: |
| 190 | if thread: |
| 191 | if parse_mode == PARSE_MODE_THREAD: |
| 192 | if thread.index == self.crashed_thread_idx: |
| 193 | thread.reason = '' |
| 194 | if self.thread_exception: |
| 195 | thread.reason += self.thread_exception |
| 196 | if self.thread_exception_data: |
| 197 | thread.reason += " (%s)" % self.thread_exception_data |
| 198 | self.threads.append(thread) |
| 199 | thread = None |
| 200 | else: |
| 201 | # only append an extra empty line if the previous line |
| 202 | # in the info_lines wasn't empty |
| 203 | if len(self.info_lines) > 0 and len(self.info_lines[-1]): |
| 204 | self.info_lines.append(line) |
| 205 | parse_mode = PARSE_MODE_NORMAL |
| 206 | # print 'PARSE_MODE_NORMAL' |
| 207 | elif parse_mode == PARSE_MODE_NORMAL: |
| 208 | if line.startswith ('Process:'): |
| 209 | (self.process_name, pid_with_brackets) = line[8:].strip().split() |
| 210 | self.process_id = pid_with_brackets.strip('[]') |
| 211 | elif line.startswith ('Path:'): |
| 212 | self.process_path = line[5:].strip() |
| 213 | elif line.startswith ('Identifier:'): |
| 214 | self.process_identifier = line[11:].strip() |
| 215 | elif line.startswith ('Version:'): |
| 216 | (self.process_version, compatability_version) = line[8:].strip().split() |
| 217 | self.process_compatability_version = compatability_version.strip('()') |
| 218 | elif line.startswith ('Parent Process:'): |
| 219 | (self.parent_process_name, pid_with_brackets) = line[15:].strip().split() |
| 220 | self.parent_process_id = pid_with_brackets.strip('[]') |
| 221 | elif line.startswith ('Exception Type:'): |
| 222 | self.thread_exception = line[15:].strip() |
| 223 | continue |
| 224 | elif line.startswith ('Exception Codes:'): |
| 225 | self.thread_exception_data = line[16:].strip() |
| 226 | continue |
| 227 | elif line.startswith ('Crashed Thread:'): |
| 228 | self.crashed_thread_idx = int(line[15:].strip().split()[0]) |
| 229 | continue |
| 230 | elif line.startswith ('Report Version:'): |
| 231 | self.version = int(line[15:].strip()) |
| 232 | continue |
| 233 | elif line.startswith ('System Profile:'): |
| 234 | parse_mode = PARSE_MODE_SYSTEM |
| 235 | continue |
| 236 | elif (line.startswith ('Interval Since Last Report:') or |
| 237 | line.startswith ('Crashes Since Last Report:') or |
| 238 | line.startswith ('Per-App Interval Since Last Report:') or |
| 239 | line.startswith ('Per-App Crashes Since Last Report:') or |
| 240 | line.startswith ('Sleep/Wake UUID:') or |
| 241 | line.startswith ('Anonymous UUID:')): |
| 242 | # ignore these |
| 243 | continue |
| 244 | elif line.startswith ('Thread'): |
| 245 | thread_state_match = self.thread_state_regex.search (line) |
| 246 | if thread_state_match: |
| 247 | thread_state_match = self.thread_regex.search (line) |
| 248 | thread_idx = int(thread_state_match.group(1)) |
| 249 | parse_mode = PARSE_MODE_THREGS |
| 250 | thread = self.threads[thread_idx] |
| 251 | else: |
| 252 | thread_match = self.thread_regex.search (line) |
| 253 | if thread_match: |
| 254 | # print 'PARSE_MODE_THREAD' |
| 255 | parse_mode = PARSE_MODE_THREAD |
| 256 | thread_idx = int(thread_match.group(1)) |
| 257 | thread = CrashLog.Thread(thread_idx) |
| 258 | continue |
| 259 | elif line.startswith ('Binary Images:'): |
| 260 | parse_mode = PARSE_MODE_IMAGES |
| 261 | continue |
| 262 | self.info_lines.append(line.strip()) |
| 263 | elif parse_mode == PARSE_MODE_THREAD: |
| 264 | frame_match = self.frame_regex.search(line) |
| 265 | if frame_match: |
| 266 | ident = frame_match.group(2) |
| 267 | if not ident in self.idents: |
| 268 | self.idents.append(ident) |
| 269 | thread.frames.append (CrashLog.Frame(int(frame_match.group(1)), int(frame_match.group(3), 0), frame_match.group(4))) |
| 270 | else: |
Greg Clayton | 8077a53 | 2012-01-20 03:32:35 +0000 | [diff] [blame] | 271 | print 'error: frame regex failed for line: "%s"' % line |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 272 | elif parse_mode == PARSE_MODE_IMAGES: |
| 273 | image_match = self.image_regex_uuid.search (line) |
| 274 | if image_match: |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 275 | image = CrashLog.DarwinImage (int(image_match.group(1),0), |
| 276 | int(image_match.group(2),0), |
| 277 | image_match.group(3).strip(), |
| 278 | image_match.group(4).strip(), |
| 279 | image_match.group(5), |
| 280 | image_match.group(6)) |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 281 | self.images.append (image) |
| 282 | else: |
| 283 | image_match = self.image_regex_no_uuid.search (line) |
| 284 | if image_match: |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 285 | image = CrashLog.DarwinImage (int(image_match.group(1),0), |
| 286 | int(image_match.group(2),0), |
| 287 | image_match.group(3).strip(), |
| 288 | image_match.group(4).strip(), |
| 289 | None, |
| 290 | image_match.group(5)) |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 291 | self.images.append (image) |
| 292 | else: |
| 293 | print "error: image regex failed for: %s" % line |
| 294 | |
| 295 | elif parse_mode == PARSE_MODE_THREGS: |
| 296 | stripped_line = line.strip() |
| 297 | reg_values = stripped_line.split(' ') |
| 298 | for reg_value in reg_values: |
| 299 | (reg, value) = reg_value.split(': ') |
| 300 | thread.registers[reg.strip()] = int(value, 0) |
| 301 | elif parse_mode == PARSE_MODE_SYSTEM: |
| 302 | self.system_profile.append(line) |
| 303 | f.close() |
| 304 | |
| 305 | def dump(self): |
| 306 | print "Crash Log File: %s" % (self.path) |
| 307 | print "\nThreads:" |
| 308 | for thread in self.threads: |
| 309 | thread.dump(' ') |
| 310 | print "\nImages:" |
| 311 | for image in self.images: |
| 312 | image.dump(' ') |
| 313 | |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 314 | def find_image_with_identifier(self, identifier): |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 315 | for image in self.images: |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 316 | if image.identifier == identifier: |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 317 | return image |
| 318 | return None |
| 319 | |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 320 | def create_target(self): |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 321 | #print 'crashlog.create_target()...' |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 322 | target = symbolication.Symbolicator.create_target(self) |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 323 | if target: |
| 324 | return target |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 325 | # We weren't able to open the main executable as, but we can still symbolicate |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 326 | print 'crashlog.create_target()...2' |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 327 | if self.idents: |
Sean Callanan | f7fb733 | 2012-01-20 19:27:48 +0000 | [diff] [blame] | 328 | for ident in self.idents: |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 329 | image = self.find_image_with_identifier (ident) |
| 330 | if image: |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 331 | target = image.create_target () |
| 332 | if target: |
| 333 | return target # success |
| 334 | print 'crashlog.create_target()...3' |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 335 | for image in self.images: |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 336 | target = image.create_target () |
| 337 | if target: |
| 338 | return target # success |
| 339 | print 'crashlog.create_target()...4' |
| 340 | print 'error: unable to locate any executables from the crash log' |
| 341 | return None |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 342 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 343 | |
| 344 | def usage(): |
| 345 | print "Usage: lldb-symbolicate.py [-n name] executable-image" |
| 346 | sys.exit(0) |
| 347 | |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 348 | class Interactive(cmd.Cmd): |
| 349 | '''Interactive prompt for analyzing one or more Darwin crash logs, type "help" to see a list of supported commands.''' |
| 350 | image_option_parser = None |
| 351 | |
| 352 | def __init__(self, crash_logs): |
| 353 | cmd.Cmd.__init__(self) |
| 354 | self.intro = 'Interactive crashlogs prompt, type "help" to see a list of supported commands.' |
| 355 | self.crash_logs = crash_logs |
| 356 | self.prompt = '% ' |
| 357 | |
| 358 | def default(self, line): |
| 359 | '''Catch all for unknown command, which will exit the interpreter.''' |
| 360 | print "uknown command: %s" % line |
| 361 | return True |
| 362 | |
| 363 | def do_q(self, line): |
| 364 | '''Quit command''' |
| 365 | return True |
| 366 | |
| 367 | def do_quit(self, line): |
| 368 | '''Quit command''' |
| 369 | return True |
| 370 | |
| 371 | def do_list(self, line=None): |
| 372 | '''Dump a list of all crash logs that are currently loaded. |
| 373 | |
| 374 | USAGE: list''' |
| 375 | print '%u crash logs are loaded:' % len(self.crash_logs) |
| 376 | for (crash_log_idx, crash_log) in enumerate(self.crash_logs): |
| 377 | print '[%u] = %s' % (crash_log_idx, crash_log.path) |
| 378 | |
| 379 | def do_image(self, line): |
| 380 | '''Dump information about an image in the crash log given an image basename. |
| 381 | |
| 382 | USAGE: image <basename>''' |
| 383 | usage = "usage: %prog [options] <PATH> [PATH ...]" |
| 384 | description='''Dump information about one or more images in all crash logs. The <PATH> |
| 385 | can be a full path or a image basename.''' |
| 386 | command_args = shlex.split(line) |
| 387 | if not self.image_option_parser: |
| 388 | self.image_option_parser = optparse.OptionParser(description=description, prog='image',usage=usage) |
| 389 | self.image_option_parser.add_option('-a', '--all', action='store_true', help='show all images', default=False) |
| 390 | try: |
| 391 | (options, args) = self.image_option_parser.parse_args(command_args) |
| 392 | except: |
| 393 | return |
| 394 | |
| 395 | for image_path in args: |
| 396 | fullpath_search = image_path[0] == '/' |
| 397 | for crash_log in self.crash_logs: |
| 398 | matches_found = 0 |
| 399 | for (image_idx, image) in enumerate(crash_log.images): |
| 400 | if fullpath_search: |
| 401 | if image.get_resolved_path() == image_path: |
| 402 | matches_found += 1 |
| 403 | print image |
| 404 | else: |
| 405 | image_basename = image.get_resolved_path_basename() |
| 406 | if image_basename == image_path: |
| 407 | matches_found += 1 |
| 408 | print image |
| 409 | if matches_found == 0: |
| 410 | for (image_idx, image) in enumerate(crash_log.images): |
| 411 | if string.find(image.get_resolved_path(), image_path) >= 0: |
| 412 | print image |
| 413 | return False |
| 414 | |
| 415 | |
| 416 | def interactive_crashlogs(options, args): |
| 417 | crash_log_files = list() |
| 418 | for arg in args: |
| 419 | for resolved_path in glob.glob(arg): |
| 420 | crash_log_files.append(resolved_path) |
| 421 | |
| 422 | crash_logs = list(); |
| 423 | for crash_log_file in crash_log_files: |
| 424 | #print 'crash_log_file = "%s"' % crash_log_file |
| 425 | crash_log = CrashLog(crash_log_file) |
| 426 | if crash_log.error: |
| 427 | print crash_log.error |
| 428 | continue |
| 429 | if options.verbose: |
| 430 | crash_log.dump() |
| 431 | if not crash_log.images: |
| 432 | print 'error: no images in crash log "%s"' % (crash_log) |
| 433 | continue |
| 434 | else: |
| 435 | crash_logs.append(crash_log) |
| 436 | |
| 437 | interpreter = Interactive(crash_logs) |
| 438 | # List all crash logs that were imported |
| 439 | interpreter.do_list() |
| 440 | interpreter.cmdloop() |
| 441 | |
| 442 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 443 | def Symbolicate(debugger, command, result, dict): |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 444 | try: |
| 445 | SymbolicateCrashLog (shlex.split(command)) |
| 446 | except: |
| 447 | result.PutCString ("error: python exception %s" % sys.exc_info()[0]) |
| 448 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 449 | def SymbolicateCrashLog(command_args): |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 450 | usage = "usage: %prog [options] <FILE> [FILE ...]" |
| 451 | description='''Symbolicate one or more darwin crash log files to provide source file and line information, |
| 452 | inlined stack frames back to the concrete functions, and disassemble the location of the crash |
| 453 | for the first frame of the crashed thread. |
| 454 | If this script is imported into the LLDB command interpreter, a "crashlog" command will be added to the interpreter |
| 455 | for use at the LLDB command line. After a crash log has been parsed and symbolicated, a target will have been |
| 456 | created that has all of the shared libraries loaded at the load addresses found in the crash log file. This allows |
| 457 | you to explore the program as if it were stopped at the locations described in the crash log and functions can |
| 458 | be disassembled and lookups can be performed using the addresses found in the crash log.''' |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 459 | parser = optparse.OptionParser(description=description, prog='crashlog',usage=usage) |
Greg Clayton | 2c1fdd0 | 2012-01-21 05:10:20 +0000 | [diff] [blame] | 460 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) |
Greg Clayton | 2c1fdd0 | 2012-01-21 05:10:20 +0000 | [diff] [blame] | 461 | parser.add_option('-a', '--load-all', action='store_true', dest='load_all_images', help='load all executable images, not just the images found in the crashed stack frames', default=False) |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 462 | parser.add_option('--images', action='store_true', dest='dump_image_list', help='show image list', default=False) |
Greg Clayton | 2c1fdd0 | 2012-01-21 05:10:20 +0000 | [diff] [blame] | 463 | parser.add_option('-g', '--debug-delay', type='int', dest='debug_delay', metavar='NSEC', help='pause for NSEC seconds for debugger', default=0) |
| 464 | parser.add_option('-c', '--crashed-only', action='store_true', dest='crashed_only', help='only symbolicate the crashed thread', default=False) |
| 465 | parser.add_option('-d', '--disasm-depth', type='int', dest='disassemble_depth', help='set the depth in stack frames that should be disassembled (default is 1)', default=1) |
| 466 | parser.add_option('-D', '--disasm-all', action='store_true', dest='disassemble_all_threads', help='enabled disassembly of frames on all threads (not just the crashed thread)', default=False) |
| 467 | parser.add_option('-B', '--disasm-before', type='int', dest='disassemble_before', help='the number of instructions to disassemble before the frame PC', default=4) |
| 468 | parser.add_option('-A', '--disasm-after', type='int', dest='disassemble_after', help='the number of instructions to disassemble after the frame PC', default=4) |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 469 | parser.add_option('-i', '--interactive', action='store_true', help='parse all crash logs and enter interactive mode', default=False) |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 470 | try: |
| 471 | (options, args) = parser.parse_args(command_args) |
| 472 | except: |
| 473 | return |
| 474 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 475 | if options.verbose: |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 476 | print 'command_args = %s' % command_args |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 477 | print 'options', options |
Greg Clayton | 223e808 | 2012-01-21 04:26:24 +0000 | [diff] [blame] | 478 | print 'args', args |
| 479 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 480 | if options.debug_delay > 0: |
| 481 | print "Waiting %u seconds for debugger to attach..." % options.debug_delay |
| 482 | time.sleep(options.debug_delay) |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 483 | error = lldb.SBError() |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 484 | |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 485 | if args: |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 486 | if options.interactive: |
| 487 | interactive_crashlogs(options, args) |
| 488 | else: |
| 489 | for crash_log_file in args: |
| 490 | crash_log = CrashLog(crash_log_file) |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 491 | |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 492 | #pp = pprint.PrettyPrinter(indent=4); pp.pprint(args) |
| 493 | if crash_log.error: |
| 494 | print crash_log.error |
| 495 | return |
| 496 | if options.verbose: |
| 497 | crash_log.dump() |
| 498 | if not crash_log.images: |
| 499 | print 'error: no images in crash log' |
| 500 | return |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 501 | |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 502 | target = crash_log.create_target () |
| 503 | if not target: |
| 504 | return |
| 505 | exe_module = target.GetModuleAtIndex(0) |
| 506 | images_to_load = list() |
| 507 | loaded_images = list() |
| 508 | if options.load_all_images: |
| 509 | # --load-all option was specified, load everything up |
| 510 | for image in crash_log.images: |
| 511 | images_to_load.append(image) |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 512 | else: |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 513 | # Only load the images found in stack frames for the crashed threads |
| 514 | for ident in crash_log.idents: |
| 515 | images = crash_log.find_images_with_identifier (ident) |
| 516 | if images: |
| 517 | for image in images: |
| 518 | images_to_load.append(image) |
| 519 | else: |
| 520 | print 'error: can\'t find image for identifier "%s"' % ident |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 521 | |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 522 | for image in images_to_load: |
| 523 | if image in loaded_images: |
| 524 | print "warning: skipping %s loaded at %#16.16x duplicate entry (probably commpage)" % (image.path, image.text_addr_lo) |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 525 | else: |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 526 | err = image.add_module (target) |
| 527 | if err: |
| 528 | print err |
| 529 | else: |
| 530 | #print 'loaded %s' % image |
| 531 | loaded_images.append(image) |
| 532 | |
| 533 | for thread in crash_log.threads: |
| 534 | this_thread_crashed = thread.did_crash() |
| 535 | if options.crashed_only and this_thread_crashed == False: |
| 536 | continue |
| 537 | print "%s" % thread |
| 538 | #prev_frame_index = -1 |
| 539 | for frame_idx, frame in enumerate(thread.frames): |
| 540 | disassemble = (this_thread_crashed or options.disassemble_all_threads) and frame_idx < options.disassemble_depth; |
| 541 | symbolicated_frame_addresses = crash_log.symbolicate (frame.pc) |
| 542 | if symbolicated_frame_addresses: |
| 543 | symbolicated_frame_address_idx = 0 |
| 544 | for symbolicated_frame_address in symbolicated_frame_addresses: |
| 545 | print '[%3u] %s' % (frame_idx, symbolicated_frame_address) |
| 546 | |
| 547 | if symbolicated_frame_address_idx == 0: |
| 548 | if disassemble: |
| 549 | instructions = symbolicated_frame_address.get_instructions() |
| 550 | if instructions: |
| 551 | print |
| 552 | symbolication.disassemble_instructions (target, |
| 553 | instructions, |
| 554 | frame.pc, |
| 555 | options.disassemble_before, |
| 556 | options.disassemble_after, frame.index > 0) |
| 557 | print |
| 558 | symbolicated_frame_address_idx += 1 |
| 559 | else: |
| 560 | print frame |
| 561 | print |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 562 | |
Greg Clayton | 9d01042 | 2012-05-04 20:44:14 +0000 | [diff] [blame^] | 563 | if options.dump_image_list: |
| 564 | print "Binary Images:" |
| 565 | for image in crash_log.images: |
| 566 | print image |
Greg Clayton | a3698c6 | 2012-01-21 00:37:19 +0000 | [diff] [blame] | 567 | |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 568 | if __name__ == '__main__': |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 569 | # Create a new debugger instance |
| 570 | lldb.debugger = lldb.SBDebugger.Create() |
Greg Clayton | 3d39f83 | 2012-04-03 21:35:43 +0000 | [diff] [blame] | 571 | SymbolicateCrashLog (sys.argv[1:]) |
Johnny Chen | a889aee | 2012-05-03 22:31:30 +0000 | [diff] [blame] | 572 | elif getattr(lldb, 'debugger', None): |
Greg Clayton | 6f2f0ab | 2012-04-25 01:49:50 +0000 | [diff] [blame] | 573 | lldb.debugger.HandleCommand('command script add -f lldb.macosx.crashlog.Symbolicate crashlog') |
Greg Clayton | e9ee550 | 2012-01-20 19:25:32 +0000 | [diff] [blame] | 574 | print '"crashlog" command installed, type "crashlog --help" for detailed help' |
Greg Clayton | 01f7c96 | 2012-01-20 03:15:45 +0000 | [diff] [blame] | 575 | |