| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2013 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """stack symbolizes native crash dumps.""" |
| 18 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 19 | import os |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 20 | import re |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 21 | import subprocess |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 22 | import symbol |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 23 | import tempfile |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 24 | import unittest |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 25 | |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 26 | import example_crashes |
| 27 | |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 28 | def ConvertTrace(lines): |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 29 | tracer = TraceConverter() |
| 30 | print "Reading symbols from", symbol.SYMBOLS_DIR |
| 31 | tracer.ConvertTrace(lines) |
| 32 | |
| 33 | class TraceConverter: |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 34 | process_info_line = re.compile("(pid: [0-9]+, tid: [0-9]+.*)") |
| Brigid Smith | 45a46c6 | 2014-06-10 17:31:32 -0700 | [diff] [blame] | 35 | abi_line = re.compile("(ABI: \'(.*)\')") |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 36 | revision_line = re.compile("(Revision: \'(.*)\')") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 37 | signal_line = re.compile("(signal [0-9]+ \(.*\).*)") |
| Elliott Hughes | d2471c8 | 2014-06-17 16:55:10 -0700 | [diff] [blame] | 38 | abort_message_line = re.compile("(Abort message: '.*')") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 39 | thread_line = re.compile("(.*)(\-\-\- ){15}\-\-\-") |
| 40 | dalvik_jni_thread_line = re.compile("(\".*\" prio=[0-9]+ tid=[0-9]+ NATIVE.*)") |
| 41 | dalvik_native_thread_line = re.compile("(\".*\" sysTid=[0-9]+ nice=[0-9]+.*)") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 42 | register_line = re.compile("$a") |
| 43 | trace_line = re.compile("$a") |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 44 | sanitizer_trace_line = re.compile("$a") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 45 | value_line = re.compile("$a") |
| 46 | code_line = re.compile("$a") |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 47 | unzip_line = re.compile("\s*(\d+)\s+\S+\s+\S+\s+(\S+)") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 48 | trace_lines = [] |
| 49 | value_lines = [] |
| 50 | last_frame = -1 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 51 | width = "{8}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 52 | spacing = "" |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 53 | apk_info = dict() |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 54 | |
| Brigid Smith | 15142f7 | 2014-07-15 13:47:07 -0700 | [diff] [blame] | 55 | def __init__(self): |
| 56 | self.UpdateAbiRegexes() |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 57 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 58 | register_names = { |
| 59 | "arm": "r0|r1|r2|r3|r4|r5|r6|r7|r8|r9|sl|fp|ip|sp|lr|pc|cpsr", |
| Elliott Hughes | be4de46 | 2014-07-14 17:15:41 -0700 | [diff] [blame] | 60 | "arm64": "x0|x1|x2|x3|x4|x5|x6|x7|x8|x9|x10|x11|x12|x13|x14|x15|x16|x17|x18|x19|x20|x21|x22|x23|x24|x25|x26|x27|x28|x29|x30|sp|pc|pstate", |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 61 | "mips": "zr|at|v0|v1|a0|a1|a2|a3|t0|t1|t2|t3|t4|t5|t6|t7|s0|s1|s2|s3|s4|s5|s6|s7|t8|t9|k0|k1|gp|sp|s8|ra|hi|lo|bva|epc", |
| Andreas Gampe | 5521841 | 2015-05-21 14:44:21 -0700 | [diff] [blame] | 62 | "mips64": "zr|at|v0|v1|a0|a1|a2|a3|a4|a5|a6|a7|t0|t1|t2|t3|s0|s1|s2|s3|s4|s5|s6|s7|t8|t9|k0|k1|gp|sp|s8|ra|hi|lo|bva|epc", |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 63 | "x86": "eax|ebx|ecx|edx|esi|edi|x?cs|x?ds|x?es|x?fs|x?ss|eip|ebp|esp|flags", |
| 64 | "x86_64": "rax|rbx|rcx|rdx|rsi|rdi|r8|r9|r10|r11|r12|r13|r14|r15|cs|ss|rip|rbp|rsp|eflags", |
| 65 | } |
| 66 | |
| 67 | def UpdateAbiRegexes(self): |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 68 | if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64": |
| 69 | self.width = "{16}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 70 | self.spacing = " " |
| Brigid Smith | 15142f7 | 2014-07-15 13:47:07 -0700 | [diff] [blame] | 71 | else: |
| 72 | self.width = "{8}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 73 | self.spacing = "" |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 74 | |
| Elliott Hughes | be4de46 | 2014-07-14 17:15:41 -0700 | [diff] [blame] | 75 | self.register_line = re.compile("(([ ]*\\b(" + self.register_names[symbol.ARCH] + ")\\b +[0-9a-f]" + self.width + "){2,5})") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 76 | |
| 77 | # Note that both trace and value line matching allow for variable amounts of |
| 78 | # whitespace (e.g. \t). This is because the we want to allow for the stack |
| 79 | # tool to operate on AndroidFeedback provided system logs. AndroidFeedback |
| 80 | # strips out double spaces that are found in tombsone files and logcat output. |
| 81 | # |
| 82 | # Examples of matched trace lines include lines from tombstone files like: |
| 83 | # #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so |
| 84 | # |
| 85 | # Or lines from AndroidFeedback crash report system logs like: |
| 86 | # 03-25 00:51:05.520 I/DEBUG ( 65): #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so |
| 87 | # Please note the spacing differences. |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 88 | self.trace_line = re.compile( |
| 89 | ".*" # Random start stuff. |
| 90 | "\#(?P<frame>[0-9]+)" # Frame number. |
| 91 | "[ \t]+..[ \t]+" # (space)pc(space). |
| 92 | "(?P<offset>[0-9a-f]" + self.width + ")[ \t]+" # Offset (hex number given without |
| 93 | # 0x prefix). |
| Christopher Ferris | c14b612 | 2015-11-30 16:29:57 -0800 | [diff] [blame] | 94 | "(?P<dso>\[[^\]]+\]|[^\r\n \t]*)" # Library name. |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 95 | "( \(offset (?P<so_offset>0x[0-9a-fA-F]+)\))?" # Offset into the file to find the start of the shared so. |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 96 | "(?P<symbolpresent> \((?P<symbol>.*)\))?") # Is the symbol there? |
| 97 | # pylint: disable-msg=C6310 |
| 98 | # Sanitizer output. This is different from debuggerd output, and it is easier to handle this as |
| 99 | # its own regex. Example: |
| 100 | # 08-19 05:29:26.283 397 403 I : #0 0xb6a15237 (/system/lib/libclang_rt.asan-arm-android.so+0x4f237) |
| 101 | self.sanitizer_trace_line = re.compile( |
| 102 | ".*" # Random start stuff. |
| 103 | "\#(?P<frame>[0-9]+)" # Frame number. |
| 104 | "[ \t]+0x[0-9a-f]+[ \t]+" # PC, not interesting to us. |
| 105 | "\(" # Opening paren. |
| 106 | "(?P<dso>[^+]+)" # Library name. |
| 107 | "\+" # '+' |
| 108 | "0x(?P<offset>[0-9a-f]+)" # Offset (hex number given with |
| 109 | # 0x prefix). |
| 110 | "\)") # Closin paren. |
| 111 | # pylint: disable-msg=C6310 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 112 | # Examples of matched value lines include: |
| 113 | # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so |
| 114 | # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so (symbol) |
| 115 | # 03-25 00:51:05.530 I/DEBUG ( 65): bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so |
| 116 | # Again, note the spacing differences. |
| 117 | self.value_line = re.compile("(.*)([0-9a-f]" + self.width + ")[ \t]+([0-9a-f]" + self.width + ")[ \t]+([^\r\n \t]*)( \((.*)\))?") |
| 118 | # Lines from 'code around' sections of the output will be matched before |
| 119 | # value lines because otheriwse the 'code around' sections will be confused as |
| 120 | # value lines. |
| 121 | # |
| 122 | # Examples include: |
| 123 | # 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8 |
| 124 | # 03-25 00:51:05.530 I/DEBUG ( 65): 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8 |
| 125 | self.code_line = re.compile("(.*)[ \t]*[a-f0-9]" + self.width + |
| 126 | "[ \t]*[a-f0-9]" + self.width + |
| 127 | "[ \t]*[a-f0-9]" + self.width + |
| 128 | "[ \t]*[a-f0-9]" + self.width + |
| 129 | "[ \t]*[a-f0-9]" + self.width + |
| 130 | "[ \t]*[ \r\n]") # pylint: disable-msg=C6310 |
| 131 | |
| 132 | def CleanLine(self, ln): |
| 133 | # AndroidFeedback adds zero width spaces into its crash reports. These |
| 134 | # should be removed or the regular expresssions will fail to match. |
| 135 | return unicode(ln, errors='ignore') |
| 136 | |
| 137 | def PrintTraceLines(self, trace_lines): |
| 138 | """Print back trace.""" |
| 139 | maxlen = max(map(lambda tl: len(tl[1]), trace_lines)) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 140 | print |
| 141 | print "Stack Trace:" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 142 | print " RELADDR " + self.spacing + "FUNCTION".ljust(maxlen) + " FILE:LINE" |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 143 | for tl in self.trace_lines: |
| 144 | (addr, symbol_with_offset, location) = tl |
| 145 | print " %8s %s %s" % (addr, symbol_with_offset.ljust(maxlen), location) |
| 146 | return |
| 147 | |
| 148 | def PrintValueLines(self, value_lines): |
| 149 | """Print stack data values.""" |
| 150 | maxlen = max(map(lambda tl: len(tl[2]), self.value_lines)) |
| 151 | print |
| 152 | print "Stack Data:" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 153 | print " ADDR " + self.spacing + "VALUE " + "FUNCTION".ljust(maxlen) + " FILE:LINE" |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 154 | for vl in self.value_lines: |
| 155 | (addr, value, symbol_with_offset, location) = vl |
| 156 | print " %8s %8s %s %s" % (addr, value, symbol_with_offset.ljust(maxlen), location) |
| 157 | return |
| 158 | |
| 159 | def PrintOutput(self, trace_lines, value_lines): |
| 160 | if self.trace_lines: |
| 161 | self.PrintTraceLines(self.trace_lines) |
| 162 | if self.value_lines: |
| 163 | self.PrintValueLines(self.value_lines) |
| 164 | |
| 165 | def PrintDivider(self): |
| 166 | print |
| 167 | print "-----------------------------------------------------\n" |
| 168 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 169 | def DeleteApkTmpFiles(self): |
| 170 | for _, offset_list in self.apk_info.values(): |
| 171 | for _, _, tmp_file in offset_list: |
| 172 | if tmp_file: |
| 173 | os.unlink(tmp_file) |
| 174 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 175 | def ConvertTrace(self, lines): |
| 176 | lines = map(self.CleanLine, lines) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 177 | try: |
| 178 | for line in lines: |
| 179 | self.ProcessLine(line) |
| 180 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 181 | finally: |
| 182 | # Delete any temporary files created while processing the lines. |
| 183 | self.DeleteApkTmpFiles() |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 184 | |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 185 | def MatchTraceLine(self, line): |
| 186 | if self.trace_line.match(line): |
| 187 | match = self.trace_line.match(line) |
| 188 | return {"frame": match.group("frame"), |
| 189 | "offset": match.group("offset"), |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 190 | "so_offset": match.group("so_offset"), |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 191 | "dso": match.group("dso"), |
| 192 | "symbol_present": bool(match.group("symbolpresent")), |
| 193 | "symbol_name": match.group("symbol")} |
| 194 | if self.sanitizer_trace_line.match(line): |
| 195 | match = self.sanitizer_trace_line.match(line) |
| 196 | return {"frame": match.group("frame"), |
| 197 | "offset": match.group("offset"), |
| Andreas Gampe | 57acd5f | 2015-09-17 11:44:21 -0700 | [diff] [blame] | 198 | "so_offset": None, |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 199 | "dso": match.group("dso"), |
| 200 | "symbol_present": False, |
| 201 | "symbol_name": None} |
| 202 | return None |
| 203 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 204 | def ExtractLibFromApk(self, apk, shared_lib_name): |
| 205 | # Create a temporary file containing the shared library from the apk. |
| 206 | tmp_file = None |
| 207 | try: |
| 208 | tmp_fd, tmp_file = tempfile.mkstemp() |
| 209 | if subprocess.call(["unzip", "-p", apk, shared_lib_name], stdout=tmp_fd) == 0: |
| 210 | os.close(tmp_fd) |
| 211 | shared_file = tmp_file |
| 212 | tmp_file = None |
| 213 | return shared_file |
| 214 | finally: |
| 215 | if tmp_file: |
| 216 | os.close(tmp_fd) |
| 217 | os.unlink(tmp_file) |
| 218 | return None |
| 219 | |
| 220 | def GetLibFromApk(self, apk, offset): |
| 221 | # Convert the string to hex. |
| 222 | offset = int(offset, 16) |
| 223 | |
| 224 | # Check if we already have information about this offset. |
| 225 | if apk in self.apk_info: |
| 226 | apk_full_path, offset_list = self.apk_info[apk] |
| 227 | for current_offset, file_name, tmp_file in offset_list: |
| 228 | if offset <= current_offset: |
| 229 | if tmp_file: |
| 230 | return file_name, tmp_file |
| 231 | # This modifies the value in offset_list. |
| 232 | tmp_file = self.ExtractLibFromApk(apk_full_path, file_name) |
| 233 | if tmp_file: |
| 234 | return file_name, tmp_file |
| 235 | break |
| 236 | return None, None |
| 237 | |
| 238 | if not "ANDROID_PRODUCT_OUT" in os.environ: |
| 239 | print "ANDROID_PRODUCT_OUT environment variable not set." |
| 240 | return None, None |
| 241 | out_dir = os.environ["ANDROID_PRODUCT_OUT"] |
| 242 | if not os.path.exists(out_dir): |
| 243 | print "ANDROID_PRODUCT_OUT " + out_dir + " does not exist." |
| 244 | return None, None |
| 245 | if apk.startswith("/"): |
| 246 | apk_full_path = out_dir + apk |
| 247 | else: |
| 248 | apk_full_path = os.path.join(out_dir, apk) |
| 249 | if not os.path.exists(apk_full_path): |
| 250 | print "Cannot find apk " + apk; |
| 251 | return None, None |
| 252 | |
| 253 | cmd = subprocess.Popen(["unzip", "-lqq", apk_full_path], stdout=subprocess.PIPE) |
| 254 | current_offset = 0 |
| 255 | file_entry = None |
| 256 | offset_list = [] |
| 257 | for line in cmd.stdout: |
| 258 | match = self.unzip_line.match(line) |
| 259 | if match: |
| 260 | # Round the size up to a page boundary. |
| 261 | current_offset += (int(match.group(1), 10) + 0x1000) & ~0xfff |
| 262 | offset_entry = [current_offset - 1, match.group(2), None] |
| 263 | offset_list.append(offset_entry) |
| 264 | if offset < current_offset and not file_entry: |
| 265 | file_entry = offset_entry |
| 266 | |
| 267 | # Save the information from the zip. |
| 268 | self.apk_info[apk] = [apk_full_path, offset_list] |
| 269 | if not file_entry: |
| 270 | return None, None |
| 271 | tmp_shared_lib = self.ExtractLibFromApk(apk_full_path, file_entry[1]) |
| 272 | if tmp_shared_lib: |
| 273 | file_entry[2] = tmp_shared_lib |
| 274 | return file_entry[1], file_entry[2] |
| 275 | return None, None |
| 276 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 277 | def ProcessLine(self, line): |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 278 | ret = False |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 279 | process_header = self.process_info_line.search(line) |
| 280 | signal_header = self.signal_line.search(line) |
| 281 | abort_message_header = self.abort_message_line.search(line) |
| 282 | thread_header = self.thread_line.search(line) |
| 283 | register_header = self.register_line.search(line) |
| 284 | abi_header = self.abi_line.search(line) |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 285 | revision_header = self.revision_line.search(line) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 286 | dalvik_jni_thread_header = self.dalvik_jni_thread_line.search(line) |
| 287 | dalvik_native_thread_header = self.dalvik_native_thread_line.search(line) |
| Elliott Hughes | d2471c8 | 2014-06-17 16:55:10 -0700 | [diff] [blame] | 288 | if process_header or signal_header or abort_message_header or thread_header or abi_header or \ |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 289 | register_header or dalvik_jni_thread_header or dalvik_native_thread_header or revision_header: |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 290 | ret = True |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 291 | if self.trace_lines or self.value_lines: |
| 292 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 293 | self.PrintDivider() |
| 294 | self.trace_lines = [] |
| 295 | self.value_lines = [] |
| 296 | self.last_frame = -1 |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 297 | if process_header: |
| 298 | print process_header.group(1) |
| 299 | if signal_header: |
| 300 | print signal_header.group(1) |
| Elliott Hughes | d2471c8 | 2014-06-17 16:55:10 -0700 | [diff] [blame] | 301 | if abort_message_header: |
| 302 | print abort_message_header.group(1) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 303 | if register_header: |
| 304 | print register_header.group(1) |
| 305 | if thread_header: |
| 306 | print thread_header.group(1) |
| 307 | if dalvik_jni_thread_header: |
| 308 | print dalvik_jni_thread_header.group(1) |
| 309 | if dalvik_native_thread_header: |
| 310 | print dalvik_native_thread_header.group(1) |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 311 | if revision_header: |
| 312 | print revision_header.group(1) |
| Brigid Smith | 45a46c6 | 2014-06-10 17:31:32 -0700 | [diff] [blame] | 313 | if abi_header: |
| 314 | print abi_header.group(1) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 315 | symbol.ARCH = abi_header.group(2) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 316 | self.UpdateAbiRegexes() |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 317 | return ret |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 318 | trace_line_dict = self.MatchTraceLine(line) |
| 319 | if trace_line_dict is not None: |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 320 | ret = True |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 321 | frame = trace_line_dict["frame"] |
| 322 | code_addr = trace_line_dict["offset"] |
| 323 | area = trace_line_dict["dso"] |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 324 | so_offset = trace_line_dict["so_offset"] |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 325 | symbol_present = trace_line_dict["symbol_present"] |
| 326 | symbol_name = trace_line_dict["symbol_name"] |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 327 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 328 | if frame <= self.last_frame and (self.trace_lines or self.value_lines): |
| 329 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 330 | self.PrintDivider() |
| 331 | self.trace_lines = [] |
| 332 | self.value_lines = [] |
| 333 | self.last_frame = frame |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 334 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 335 | if area == "<unknown>" or area == "[heap]" or area == "[stack]": |
| 336 | self.trace_lines.append((code_addr, "", area)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 337 | else: |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 338 | # If this is an apk, it usually means that there is actually |
| 339 | # a shared so that was loaded directly out of it. In that case, |
| 340 | # extract the shared library and the name of the shared library. |
| 341 | lib = None |
| 342 | if area.endswith(".apk") and so_offset: |
| 343 | lib_name, lib = self.GetLibFromApk(area, so_offset) |
| 344 | if not lib: |
| 345 | lib = area |
| 346 | lib_name = None |
| 347 | |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 348 | # If a calls b which further calls c and c is inlined to b, we want to |
| 349 | # display "a -> b -> c" in the stack trace instead of just "a -> c" |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 350 | info = symbol.SymbolInformation(lib, code_addr) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 351 | nest_count = len(info) - 1 |
| 352 | for (source_symbol, source_location, object_symbol_with_offset) in info: |
| 353 | if not source_symbol: |
| 354 | if symbol_present: |
| 355 | source_symbol = symbol.CallCppFilt(symbol_name) |
| 356 | else: |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 357 | source_symbol = "<unknown>" |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 358 | if not source_location: |
| 359 | source_location = area |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 360 | if lib_name: |
| 361 | source_location += "(" + lib_name + ")" |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 362 | if nest_count > 0: |
| 363 | nest_count = nest_count - 1 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 364 | arrow = "v------>" |
| 365 | if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64": |
| 366 | arrow = "v-------------->" |
| 367 | self.trace_lines.append((arrow, source_symbol, source_location)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 368 | else: |
| 369 | if not object_symbol_with_offset: |
| 370 | object_symbol_with_offset = source_symbol |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 371 | self.trace_lines.append((code_addr, |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 372 | object_symbol_with_offset, |
| 373 | source_location)) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 374 | if self.code_line.match(line): |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 375 | # Code lines should be ignored. If this were exluded the 'code around' |
| 376 | # sections would trigger value_line matches. |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 377 | return ret |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 378 | if self.value_line.match(line): |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 379 | ret = True |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 380 | match = self.value_line.match(line) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 381 | (unused_, addr, value, area, symbol_present, symbol_name) = match.groups() |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 382 | if area == "<unknown>" or area == "[heap]" or area == "[stack]" or not area: |
| 383 | self.value_lines.append((addr, value, "", area)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 384 | else: |
| 385 | info = symbol.SymbolInformation(area, value) |
| 386 | (source_symbol, source_location, object_symbol_with_offset) = info.pop() |
| 387 | if not source_symbol: |
| 388 | if symbol_present: |
| 389 | source_symbol = symbol.CallCppFilt(symbol_name) |
| 390 | else: |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 391 | source_symbol = "<unknown>" |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 392 | if not source_location: |
| 393 | source_location = area |
| 394 | if not object_symbol_with_offset: |
| 395 | object_symbol_with_offset = source_symbol |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 396 | self.value_lines.append((addr, |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 397 | value, |
| 398 | object_symbol_with_offset, |
| 399 | source_location)) |
| 400 | |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 401 | return ret |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 402 | |
| 403 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 404 | class RegisterPatternTests(unittest.TestCase): |
| 405 | def assert_register_matches(self, abi, example_crash, stupid_pattern): |
| 406 | tc = TraceConverter() |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 407 | for line in example_crash.split('\n'): |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 408 | tc.ProcessLine(line) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 409 | is_register = (re.search(stupid_pattern, line) is not None) |
| 410 | matched = (tc.register_line.search(line) is not None) |
| 411 | self.assertEquals(matched, is_register, line) |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 412 | tc.PrintOutput(tc.trace_lines, tc.value_lines) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 413 | |
| 414 | def test_arm_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 415 | self.assert_register_matches("arm", example_crashes.arm, '\\b(r0|r4|r8|ip)\\b') |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 416 | |
| 417 | def test_arm64_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 418 | self.assert_register_matches("arm64", example_crashes.arm64, '\\b(x0|x4|x8|x12|x16|x20|x24|x28|sp)\\b') |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 419 | |
| 420 | def test_mips_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 421 | self.assert_register_matches("mips", example_crashes.mips, '\\b(zr|a0|t0|t4|s0|s4|t8|gp|hi)\\b') |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 422 | |
| Andreas Gampe | 820ca72 | 2015-06-01 15:43:52 -0700 | [diff] [blame] | 423 | def test_mips64_registers(self): |
| 424 | self.assert_register_matches("mips64", example_crashes.mips64, '\\b(zr|a0|a4|t0|s0|s4|t8|gp|hi)\\b') |
| 425 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 426 | def test_x86_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 427 | self.assert_register_matches("x86", example_crashes.x86, '\\b(eax|esi|xcs|eip)\\b') |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 428 | |
| 429 | def test_x86_64_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 430 | self.assert_register_matches("x86_64", example_crashes.x86_64, '\\b(rax|rsi|r8|r12|cs|rip)\\b') |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 431 | |
| 432 | |
| 433 | if __name__ == '__main__': |
| 434 | unittest.main() |