| 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 | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 35 | revision_line = re.compile("(Revision: \'(.*)\')") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 36 | signal_line = re.compile("(signal [0-9]+ \(.*\).*)") |
| Elliott Hughes | d2471c8 | 2014-06-17 16:55:10 -0700 | [diff] [blame] | 37 | abort_message_line = re.compile("(Abort message: '.*')") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 38 | thread_line = re.compile("(.*)(\-\-\- ){15}\-\-\-") |
| 39 | dalvik_jni_thread_line = re.compile("(\".*\" prio=[0-9]+ tid=[0-9]+ NATIVE.*)") |
| 40 | dalvik_native_thread_line = re.compile("(\".*\" sysTid=[0-9]+ nice=[0-9]+.*)") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 41 | register_line = re.compile("$a") |
| 42 | trace_line = re.compile("$a") |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 43 | sanitizer_trace_line = re.compile("$a") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 44 | value_line = re.compile("$a") |
| 45 | code_line = re.compile("$a") |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 46 | zipinfo_central_directory_line = re.compile("Central\s+directory\s+entry") |
| 47 | zipinfo_central_info_match = re.compile( |
| 48 | "^\s*(\S+)$\s*offset of local header from start of archive:\s*(\d+)" |
| 49 | ".*^\s*compressed size:\s+(\d+)", re.M | re.S) |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 50 | unreachable_line = re.compile("((\d+ bytes in \d+ unreachable allocations)|"+\ |
| 51 | "(\d+ bytes unreachable at [0-9a-f]+)|"+\ |
| 52 | "(referencing \d+ unreachable bytes in \d+ allocation(s)?)|"+\ |
| 53 | "(and \d+ similar unreachable bytes in \d+ allocation(s)?))") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 54 | trace_lines = [] |
| 55 | value_lines = [] |
| 56 | last_frame = -1 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 57 | width = "{8}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 58 | spacing = "" |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 59 | apk_info = dict() |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 60 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 61 | register_names = { |
| 62 | "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] | 63 | "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] | 64 | "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] | 65 | "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] | 66 | "x86": "eax|ebx|ecx|edx|esi|edi|x?cs|x?ds|x?es|x?fs|x?ss|eip|ebp|esp|flags", |
| 67 | "x86_64": "rax|rbx|rcx|rdx|rsi|rdi|r8|r9|r10|r11|r12|r13|r14|r15|cs|ss|rip|rbp|rsp|eflags", |
| 68 | } |
| 69 | |
| 70 | def UpdateAbiRegexes(self): |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 71 | if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64": |
| 72 | self.width = "{16}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 73 | self.spacing = " " |
| Brigid Smith | 15142f7 | 2014-07-15 13:47:07 -0700 | [diff] [blame] | 74 | else: |
| 75 | self.width = "{8}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 76 | self.spacing = "" |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 77 | |
| Elliott Hughes | be4de46 | 2014-07-14 17:15:41 -0700 | [diff] [blame] | 78 | 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] | 79 | |
| 80 | # Note that both trace and value line matching allow for variable amounts of |
| 81 | # whitespace (e.g. \t). This is because the we want to allow for the stack |
| 82 | # tool to operate on AndroidFeedback provided system logs. AndroidFeedback |
| 83 | # strips out double spaces that are found in tombsone files and logcat output. |
| 84 | # |
| 85 | # Examples of matched trace lines include lines from tombstone files like: |
| 86 | # #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so |
| 87 | # |
| 88 | # Or lines from AndroidFeedback crash report system logs like: |
| 89 | # 03-25 00:51:05.520 I/DEBUG ( 65): #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so |
| 90 | # Please note the spacing differences. |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 91 | self.trace_line = re.compile( |
| 92 | ".*" # Random start stuff. |
| 93 | "\#(?P<frame>[0-9]+)" # Frame number. |
| 94 | "[ \t]+..[ \t]+" # (space)pc(space). |
| 95 | "(?P<offset>[0-9a-f]" + self.width + ")[ \t]+" # Offset (hex number given without |
| 96 | # 0x prefix). |
| Christopher Ferris | c14b612 | 2015-11-30 16:29:57 -0800 | [diff] [blame] | 97 | "(?P<dso>\[[^\]]+\]|[^\r\n \t]*)" # Library name. |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 98 | "( \(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] | 99 | "(?P<symbolpresent> \((?P<symbol>.*)\))?") # Is the symbol there? |
| 100 | # pylint: disable-msg=C6310 |
| 101 | # Sanitizer output. This is different from debuggerd output, and it is easier to handle this as |
| 102 | # its own regex. Example: |
| 103 | # 08-19 05:29:26.283 397 403 I : #0 0xb6a15237 (/system/lib/libclang_rt.asan-arm-android.so+0x4f237) |
| 104 | self.sanitizer_trace_line = re.compile( |
| 105 | ".*" # Random start stuff. |
| 106 | "\#(?P<frame>[0-9]+)" # Frame number. |
| 107 | "[ \t]+0x[0-9a-f]+[ \t]+" # PC, not interesting to us. |
| 108 | "\(" # Opening paren. |
| 109 | "(?P<dso>[^+]+)" # Library name. |
| 110 | "\+" # '+' |
| 111 | "0x(?P<offset>[0-9a-f]+)" # Offset (hex number given with |
| 112 | # 0x prefix). |
| 113 | "\)") # Closin paren. |
| 114 | # pylint: disable-msg=C6310 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 115 | # Examples of matched value lines include: |
| 116 | # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so |
| 117 | # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so (symbol) |
| 118 | # 03-25 00:51:05.530 I/DEBUG ( 65): bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so |
| 119 | # Again, note the spacing differences. |
| 120 | self.value_line = re.compile("(.*)([0-9a-f]" + self.width + ")[ \t]+([0-9a-f]" + self.width + ")[ \t]+([^\r\n \t]*)( \((.*)\))?") |
| 121 | # Lines from 'code around' sections of the output will be matched before |
| 122 | # value lines because otheriwse the 'code around' sections will be confused as |
| 123 | # value lines. |
| 124 | # |
| 125 | # Examples include: |
| 126 | # 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8 |
| 127 | # 03-25 00:51:05.530 I/DEBUG ( 65): 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8 |
| 128 | self.code_line = re.compile("(.*)[ \t]*[a-f0-9]" + self.width + |
| 129 | "[ \t]*[a-f0-9]" + self.width + |
| 130 | "[ \t]*[a-f0-9]" + self.width + |
| 131 | "[ \t]*[a-f0-9]" + self.width + |
| 132 | "[ \t]*[a-f0-9]" + self.width + |
| 133 | "[ \t]*[ \r\n]") # pylint: disable-msg=C6310 |
| 134 | |
| 135 | def CleanLine(self, ln): |
| 136 | # AndroidFeedback adds zero width spaces into its crash reports. These |
| 137 | # should be removed or the regular expresssions will fail to match. |
| 138 | return unicode(ln, errors='ignore') |
| 139 | |
| 140 | def PrintTraceLines(self, trace_lines): |
| 141 | """Print back trace.""" |
| 142 | maxlen = max(map(lambda tl: len(tl[1]), trace_lines)) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 143 | print |
| 144 | print "Stack Trace:" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 145 | print " RELADDR " + self.spacing + "FUNCTION".ljust(maxlen) + " FILE:LINE" |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 146 | for tl in self.trace_lines: |
| 147 | (addr, symbol_with_offset, location) = tl |
| 148 | print " %8s %s %s" % (addr, symbol_with_offset.ljust(maxlen), location) |
| 149 | return |
| 150 | |
| 151 | def PrintValueLines(self, value_lines): |
| 152 | """Print stack data values.""" |
| 153 | maxlen = max(map(lambda tl: len(tl[2]), self.value_lines)) |
| 154 | print |
| 155 | print "Stack Data:" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 156 | print " ADDR " + self.spacing + "VALUE " + "FUNCTION".ljust(maxlen) + " FILE:LINE" |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 157 | for vl in self.value_lines: |
| 158 | (addr, value, symbol_with_offset, location) = vl |
| 159 | print " %8s %8s %s %s" % (addr, value, symbol_with_offset.ljust(maxlen), location) |
| 160 | return |
| 161 | |
| 162 | def PrintOutput(self, trace_lines, value_lines): |
| 163 | if self.trace_lines: |
| 164 | self.PrintTraceLines(self.trace_lines) |
| 165 | if self.value_lines: |
| 166 | self.PrintValueLines(self.value_lines) |
| 167 | |
| 168 | def PrintDivider(self): |
| 169 | print |
| 170 | print "-----------------------------------------------------\n" |
| 171 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 172 | def DeleteApkTmpFiles(self): |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 173 | for _, _, tmp_files in self.apk_info.values(): |
| 174 | for tmp_file in tmp_files.values(): |
| 175 | os.unlink(tmp_file) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 176 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 177 | def ConvertTrace(self, lines): |
| 178 | lines = map(self.CleanLine, lines) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 179 | try: |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 180 | if not symbol.ARCH: |
| 181 | symbol.SetAbi(lines) |
| 182 | self.UpdateAbiRegexes() |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 183 | for line in lines: |
| 184 | self.ProcessLine(line) |
| 185 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 186 | finally: |
| 187 | # Delete any temporary files created while processing the lines. |
| 188 | self.DeleteApkTmpFiles() |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 189 | |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 190 | def MatchTraceLine(self, line): |
| 191 | if self.trace_line.match(line): |
| 192 | match = self.trace_line.match(line) |
| 193 | return {"frame": match.group("frame"), |
| 194 | "offset": match.group("offset"), |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 195 | "so_offset": match.group("so_offset"), |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 196 | "dso": match.group("dso"), |
| 197 | "symbol_present": bool(match.group("symbolpresent")), |
| 198 | "symbol_name": match.group("symbol")} |
| 199 | if self.sanitizer_trace_line.match(line): |
| 200 | match = self.sanitizer_trace_line.match(line) |
| 201 | return {"frame": match.group("frame"), |
| 202 | "offset": match.group("offset"), |
| Andreas Gampe | 57acd5f | 2015-09-17 11:44:21 -0700 | [diff] [blame] | 203 | "so_offset": None, |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 204 | "dso": match.group("dso"), |
| 205 | "symbol_present": False, |
| 206 | "symbol_name": None} |
| 207 | return None |
| 208 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 209 | def ExtractLibFromApk(self, apk, shared_lib_name): |
| 210 | # Create a temporary file containing the shared library from the apk. |
| 211 | tmp_file = None |
| 212 | try: |
| 213 | tmp_fd, tmp_file = tempfile.mkstemp() |
| 214 | if subprocess.call(["unzip", "-p", apk, shared_lib_name], stdout=tmp_fd) == 0: |
| 215 | os.close(tmp_fd) |
| 216 | shared_file = tmp_file |
| 217 | tmp_file = None |
| 218 | return shared_file |
| 219 | finally: |
| 220 | if tmp_file: |
| 221 | os.close(tmp_fd) |
| 222 | os.unlink(tmp_file) |
| 223 | return None |
| 224 | |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 225 | def ProcessCentralInfo(self, offset_list, central_info): |
| 226 | match = self.zipinfo_central_info_match.search(central_info) |
| 227 | if not match: |
| 228 | raise Exception("Cannot find all info from zipinfo\n" + central_info) |
| 229 | name = match.group(1) |
| 230 | start = int(match.group(2)) |
| 231 | end = start + int(match.group(3)) |
| 232 | |
| 233 | offset_list.append([name, start, end]) |
| 234 | return name, start, end |
| 235 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 236 | def GetLibFromApk(self, apk, offset): |
| 237 | # Convert the string to hex. |
| 238 | offset = int(offset, 16) |
| 239 | |
| 240 | # Check if we already have information about this offset. |
| 241 | if apk in self.apk_info: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 242 | apk_full_path, offset_list, tmp_files = self.apk_info[apk] |
| 243 | for file_name, start, end in offset_list: |
| 244 | if offset >= start and offset < end: |
| 245 | if file_name in tmp_files: |
| 246 | return file_name, tmp_files[file_name] |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 247 | tmp_file = self.ExtractLibFromApk(apk_full_path, file_name) |
| 248 | if tmp_file: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 249 | tmp_files[file_name] = tmp_file |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 250 | return file_name, tmp_file |
| 251 | break |
| 252 | return None, None |
| 253 | |
| 254 | if not "ANDROID_PRODUCT_OUT" in os.environ: |
| 255 | print "ANDROID_PRODUCT_OUT environment variable not set." |
| 256 | return None, None |
| 257 | out_dir = os.environ["ANDROID_PRODUCT_OUT"] |
| 258 | if not os.path.exists(out_dir): |
| 259 | print "ANDROID_PRODUCT_OUT " + out_dir + " does not exist." |
| 260 | return None, None |
| 261 | if apk.startswith("/"): |
| 262 | apk_full_path = out_dir + apk |
| 263 | else: |
| 264 | apk_full_path = os.path.join(out_dir, apk) |
| 265 | if not os.path.exists(apk_full_path): |
| 266 | print "Cannot find apk " + apk; |
| 267 | return None, None |
| 268 | |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 269 | cmd = subprocess.Popen(["zipinfo", "-v", apk_full_path], stdout=subprocess.PIPE) |
| 270 | # Find the first central info marker. |
| 271 | for line in cmd.stdout: |
| 272 | if self.zipinfo_central_directory_line.search(line): |
| 273 | break |
| 274 | |
| 275 | central_info = "" |
| 276 | file_name = None |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 277 | offset_list = [] |
| 278 | for line in cmd.stdout: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 279 | match = self.zipinfo_central_directory_line.search(line) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 280 | if match: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 281 | cur_name, start, end = self.ProcessCentralInfo(offset_list, central_info) |
| 282 | if not file_name and offset >= start and offset < end: |
| 283 | file_name = cur_name |
| 284 | central_info = "" |
| 285 | else: |
| 286 | central_info += line |
| 287 | if central_info: |
| 288 | cur_name, start, end = self.ProcessCentralInfo(offset_list, central_info) |
| 289 | if not file_name and offset >= start and offset < end: |
| 290 | file_name = cur_name |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 291 | |
| 292 | # Save the information from the zip. |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 293 | tmp_files = dict() |
| 294 | self.apk_info[apk] = [apk_full_path, offset_list, tmp_files] |
| 295 | if not file_name: |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 296 | return None, None |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 297 | tmp_shared_lib = self.ExtractLibFromApk(apk_full_path, file_name) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 298 | if tmp_shared_lib: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 299 | tmp_files[file_name] = tmp_shared_lib |
| 300 | return file_name, tmp_shared_lib |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 301 | return None, None |
| 302 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 303 | def ProcessLine(self, line): |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 304 | ret = False |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 305 | process_header = self.process_info_line.search(line) |
| 306 | signal_header = self.signal_line.search(line) |
| 307 | abort_message_header = self.abort_message_line.search(line) |
| 308 | thread_header = self.thread_line.search(line) |
| 309 | register_header = self.register_line.search(line) |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 310 | revision_header = self.revision_line.search(line) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 311 | dalvik_jni_thread_header = self.dalvik_jni_thread_line.search(line) |
| 312 | dalvik_native_thread_header = self.dalvik_native_thread_line.search(line) |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 313 | unreachable_header = self.unreachable_line.search(line) |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 314 | if process_header or signal_header or abort_message_header or thread_header or \ |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 315 | register_header or dalvik_jni_thread_header or dalvik_native_thread_header or \ |
| 316 | revision_header or unreachable_header: |
| 317 | ret = True |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 318 | if self.trace_lines or self.value_lines: |
| 319 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 320 | self.PrintDivider() |
| 321 | self.trace_lines = [] |
| 322 | self.value_lines = [] |
| 323 | self.last_frame = -1 |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 324 | if process_header: |
| 325 | print process_header.group(1) |
| 326 | if signal_header: |
| 327 | print signal_header.group(1) |
| Elliott Hughes | d2471c8 | 2014-06-17 16:55:10 -0700 | [diff] [blame] | 328 | if abort_message_header: |
| 329 | print abort_message_header.group(1) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 330 | if register_header: |
| 331 | print register_header.group(1) |
| 332 | if thread_header: |
| 333 | print thread_header.group(1) |
| 334 | if dalvik_jni_thread_header: |
| 335 | print dalvik_jni_thread_header.group(1) |
| 336 | if dalvik_native_thread_header: |
| 337 | print dalvik_native_thread_header.group(1) |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 338 | if revision_header: |
| 339 | print revision_header.group(1) |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 340 | if unreachable_header: |
| 341 | print unreachable_header.group(1) |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 342 | return True |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 343 | trace_line_dict = self.MatchTraceLine(line) |
| 344 | if trace_line_dict is not None: |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 345 | ret = True |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 346 | frame = trace_line_dict["frame"] |
| 347 | code_addr = trace_line_dict["offset"] |
| 348 | area = trace_line_dict["dso"] |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 349 | so_offset = trace_line_dict["so_offset"] |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 350 | symbol_present = trace_line_dict["symbol_present"] |
| 351 | symbol_name = trace_line_dict["symbol_name"] |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 352 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 353 | if frame <= self.last_frame and (self.trace_lines or self.value_lines): |
| 354 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 355 | self.PrintDivider() |
| 356 | self.trace_lines = [] |
| 357 | self.value_lines = [] |
| 358 | self.last_frame = frame |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 359 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 360 | if area == "<unknown>" or area == "[heap]" or area == "[stack]": |
| 361 | self.trace_lines.append((code_addr, "", area)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 362 | else: |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 363 | # If this is an apk, it usually means that there is actually |
| 364 | # a shared so that was loaded directly out of it. In that case, |
| 365 | # extract the shared library and the name of the shared library. |
| 366 | lib = None |
| 367 | if area.endswith(".apk") and so_offset: |
| 368 | lib_name, lib = self.GetLibFromApk(area, so_offset) |
| 369 | if not lib: |
| 370 | lib = area |
| 371 | lib_name = None |
| 372 | |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 373 | # If a calls b which further calls c and c is inlined to b, we want to |
| 374 | # 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] | 375 | info = symbol.SymbolInformation(lib, code_addr) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 376 | nest_count = len(info) - 1 |
| 377 | for (source_symbol, source_location, object_symbol_with_offset) in info: |
| 378 | if not source_symbol: |
| 379 | if symbol_present: |
| 380 | source_symbol = symbol.CallCppFilt(symbol_name) |
| 381 | else: |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 382 | source_symbol = "<unknown>" |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 383 | if not source_location: |
| 384 | source_location = area |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 385 | if lib_name: |
| 386 | source_location += "(" + lib_name + ")" |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 387 | if nest_count > 0: |
| 388 | nest_count = nest_count - 1 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 389 | arrow = "v------>" |
| 390 | if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64": |
| 391 | arrow = "v-------------->" |
| 392 | self.trace_lines.append((arrow, source_symbol, source_location)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 393 | else: |
| 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.trace_lines.append((code_addr, |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 397 | object_symbol_with_offset, |
| 398 | source_location)) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 399 | if self.code_line.match(line): |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 400 | # Code lines should be ignored. If this were exluded the 'code around' |
| 401 | # sections would trigger value_line matches. |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 402 | return ret |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 403 | if self.value_line.match(line): |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 404 | ret = True |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 405 | match = self.value_line.match(line) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 406 | (unused_, addr, value, area, symbol_present, symbol_name) = match.groups() |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 407 | if area == "<unknown>" or area == "[heap]" or area == "[stack]" or not area: |
| 408 | self.value_lines.append((addr, value, "", area)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 409 | else: |
| 410 | info = symbol.SymbolInformation(area, value) |
| 411 | (source_symbol, source_location, object_symbol_with_offset) = info.pop() |
| 412 | if not source_symbol: |
| 413 | if symbol_present: |
| 414 | source_symbol = symbol.CallCppFilt(symbol_name) |
| 415 | else: |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 416 | source_symbol = "<unknown>" |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 417 | if not source_location: |
| 418 | source_location = area |
| 419 | if not object_symbol_with_offset: |
| 420 | object_symbol_with_offset = source_symbol |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 421 | self.value_lines.append((addr, |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 422 | value, |
| 423 | object_symbol_with_offset, |
| 424 | source_location)) |
| 425 | |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 426 | return ret |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 427 | |
| 428 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 429 | class RegisterPatternTests(unittest.TestCase): |
| 430 | def assert_register_matches(self, abi, example_crash, stupid_pattern): |
| 431 | tc = TraceConverter() |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 432 | lines = example_crash.split('\n') |
| 433 | symbol.SetAbi(lines) |
| 434 | tc.UpdateAbiRegexes() |
| 435 | for line in lines: |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 436 | tc.ProcessLine(line) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 437 | is_register = (re.search(stupid_pattern, line) is not None) |
| 438 | matched = (tc.register_line.search(line) is not None) |
| 439 | self.assertEquals(matched, is_register, line) |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 440 | tc.PrintOutput(tc.trace_lines, tc.value_lines) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 441 | |
| 442 | def test_arm_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 443 | 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] | 444 | |
| 445 | def test_arm64_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 446 | 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] | 447 | |
| 448 | def test_mips_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 449 | 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] | 450 | |
| Andreas Gampe | 820ca72 | 2015-06-01 15:43:52 -0700 | [diff] [blame] | 451 | def test_mips64_registers(self): |
| 452 | self.assert_register_matches("mips64", example_crashes.mips64, '\\b(zr|a0|a4|t0|s0|s4|t8|gp|hi)\\b') |
| 453 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 454 | def test_x86_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 455 | 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] | 456 | |
| 457 | def test_x86_64_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 458 | 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] | 459 | |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 460 | class LibmemunreachablePatternTests(unittest.TestCase): |
| 461 | def test_libmemunreachable(self): |
| 462 | tc = TraceConverter() |
| 463 | lines = example_crashes.libmemunreachable.split('\n') |
| 464 | |
| 465 | symbol.SetAbi(lines) |
| 466 | self.assertEquals(symbol.ARCH, "arm") |
| 467 | |
| 468 | tc.UpdateAbiRegexes() |
| 469 | header_lines = 0 |
| Colin Cross | 1127df9 | 2016-07-26 10:15:01 -0700 | [diff] [blame^] | 470 | trace_lines = 0 |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 471 | for line in lines: |
| 472 | tc.ProcessLine(line) |
| 473 | if re.search(tc.unreachable_line, line) is not None: |
| 474 | header_lines += 1 |
| Colin Cross | 1127df9 | 2016-07-26 10:15:01 -0700 | [diff] [blame^] | 475 | if tc.MatchTraceLine(line) is not None: |
| 476 | trace_lines += 1 |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 477 | self.assertEquals(header_lines, 3) |
| Colin Cross | 1127df9 | 2016-07-26 10:15:01 -0700 | [diff] [blame^] | 478 | self.assertEquals(trace_lines, 2) |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 479 | tc.PrintOutput(tc.trace_lines, tc.value_lines) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 480 | |
| 481 | if __name__ == '__main__': |
| 482 | unittest.main() |