| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 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 | |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 19 | import collections |
| 20 | import functools |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 21 | import os |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 22 | import pathlib |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 23 | import re |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 24 | import subprocess |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 25 | import symbol |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 26 | import tempfile |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 27 | import unittest |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 28 | |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 29 | import example_crashes |
| 30 | |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 31 | def ConvertTrace(lines): |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 32 | tracer = TraceConverter() |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 33 | print("Reading symbols from", symbol.SYMBOLS_DIR) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 34 | tracer.ConvertTrace(lines) |
| 35 | |
| 36 | class TraceConverter: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 37 | process_info_line = re.compile(r"(pid: [0-9]+, tid: [0-9]+.*)") |
| 38 | revision_line = re.compile(r"(Revision: '(.*)')") |
| 39 | signal_line = re.compile(r"(signal [0-9]+ \(.*\).*)") |
| 40 | abort_message_line = re.compile(r"(Abort message: '.*')") |
| 41 | thread_line = re.compile(r"(.*)(--- ){15}---") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 42 | dalvik_jni_thread_line = re.compile("(\".*\" prio=[0-9]+ tid=[0-9]+ NATIVE.*)") |
| 43 | dalvik_native_thread_line = re.compile("(\".*\" sysTid=[0-9]+ nice=[0-9]+.*)") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 44 | register_line = re.compile("$a") |
| 45 | trace_line = re.compile("$a") |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 46 | sanitizer_trace_line = re.compile("$a") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 47 | value_line = re.compile("$a") |
| 48 | code_line = re.compile("$a") |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 49 | zipinfo_central_directory_line = re.compile(r"Central\s+directory\s+entry") |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 50 | zipinfo_central_info_match = re.compile( |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 51 | r"^\s*(\S+)$\s*offset of local header from start of archive:\s*(\d+)" |
| 52 | r".*^\s*compressed size:\s+(\d+)", re.M | re.S) |
| 53 | unreachable_line = re.compile(r"((\d+ bytes in \d+ unreachable allocations)|" |
| 54 | r"(\d+ bytes unreachable at [0-9a-f]+)|" |
| 55 | r"(referencing \d+ unreachable bytes in \d+ allocation(s)?)|" |
| 56 | r"(and \d+ similar unreachable bytes in \d+ allocation(s)?))") |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 57 | trace_lines = [] |
| 58 | value_lines = [] |
| 59 | last_frame = -1 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 60 | width = "{8}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 61 | spacing = "" |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 62 | apk_info = dict() |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 63 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 64 | register_names = { |
| 65 | "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] | 66 | "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] | 67 | "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] | 68 | "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] | 69 | "x86": "eax|ebx|ecx|edx|esi|edi|x?cs|x?ds|x?es|x?fs|x?ss|eip|ebp|esp|flags", |
| 70 | "x86_64": "rax|rbx|rcx|rdx|rsi|rdi|r8|r9|r10|r11|r12|r13|r14|r15|cs|ss|rip|rbp|rsp|eflags", |
| 71 | } |
| 72 | |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 73 | # We use the "file" command line tool to extract BuildId from ELF files. |
| 74 | ElfInfo = collections.namedtuple("ElfInfo", ["bitness", "build_id"]) |
| 75 | file_tool_output = re.compile(r"ELF (?P<bitness>32|64)-bit .*" |
| 76 | r"BuildID(\[.*\])?=(?P<build_id>[0-9a-f]+)") |
| 77 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 78 | def UpdateAbiRegexes(self): |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 79 | if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64": |
| 80 | self.width = "{16}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 81 | self.spacing = " " |
| Brigid Smith | 15142f7 | 2014-07-15 13:47:07 -0700 | [diff] [blame] | 82 | else: |
| 83 | self.width = "{8}" |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 84 | self.spacing = "" |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 85 | |
| Elliott Hughes | be4de46 | 2014-07-14 17:15:41 -0700 | [diff] [blame] | 86 | 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] | 87 | |
| 88 | # Note that both trace and value line matching allow for variable amounts of |
| 89 | # whitespace (e.g. \t). This is because the we want to allow for the stack |
| 90 | # tool to operate on AndroidFeedback provided system logs. AndroidFeedback |
| 91 | # strips out double spaces that are found in tombsone files and logcat output. |
| 92 | # |
| 93 | # Examples of matched trace lines include lines from tombstone files like: |
| 94 | # #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so |
| 95 | # |
| 96 | # Or lines from AndroidFeedback crash report system logs like: |
| 97 | # 03-25 00:51:05.520 I/DEBUG ( 65): #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so |
| 98 | # Please note the spacing differences. |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 99 | self.trace_line = re.compile( |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 100 | r".*" # Random start stuff. |
| 101 | r"\#(?P<frame>[0-9]+)" # Frame number. |
| 102 | r"[ \t]+..[ \t]+" # (space)pc(space). |
| 103 | r"(?P<offset>[0-9a-f]" + self.width + ")[ \t]+" # Offset (hex number given without |
| 104 | # 0x prefix). |
| 105 | r"(?P<dso>\[[^\]]+\]|[^\r\n \t]*)" # Library name. |
| 106 | r"( \(offset (?P<so_offset>0x[0-9a-fA-F]+)\))?" # Offset into the file to find the start of the shared so. |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 107 | r"(?P<symbolpresent> \((?P<symbol>.*?)\))?" # Is the symbol there? (non-greedy) |
| 108 | r"( \(BuildId: (?P<build_id>.*)\))?" # Optional build-id of the ELF file. |
| 109 | r"[ \t]*$") # End of line (to expand non-greedy match). |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 110 | # pylint: disable-msg=C6310 |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 111 | # Sanitizer output. This is different from debuggerd output, and it is easier to handle this as |
| 112 | # its own regex. Example: |
| 113 | # 08-19 05:29:26.283 397 403 I : #0 0xb6a15237 (/system/lib/libclang_rt.asan-arm-android.so+0x4f237) |
| 114 | self.sanitizer_trace_line = re.compile( |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 115 | r".*" # Random start stuff. |
| 116 | r"\#(?P<frame>[0-9]+)" # Frame number. |
| 117 | r"[ \t]+0x[0-9a-f]+[ \t]+" # PC, not interesting to us. |
| 118 | r"\(" # Opening paren. |
| 119 | r"(?P<dso>[^+]+)" # Library name. |
| 120 | r"\+" # '+' |
| 121 | r"0x(?P<offset>[0-9a-f]+)" # Offset (hex number given with |
| 122 | # 0x prefix). |
| 123 | r"\)") # Closing paren. |
| 124 | # pylint: disable-msg=C6310 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 125 | # Examples of matched value lines include: |
| 126 | # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so |
| 127 | # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so (symbol) |
| 128 | # 03-25 00:51:05.530 I/DEBUG ( 65): bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so |
| 129 | # Again, note the spacing differences. |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 130 | self.value_line = re.compile(r"(.*)([0-9a-f]" + self.width + r")[ \t]+([0-9a-f]" + self.width + r")[ \t]+([^\r\n \t]*)( \((.*)\))?") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 131 | # Lines from 'code around' sections of the output will be matched before |
| 132 | # value lines because otheriwse the 'code around' sections will be confused as |
| 133 | # value lines. |
| 134 | # |
| 135 | # Examples include: |
| 136 | # 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8 |
| 137 | # 03-25 00:51:05.530 I/DEBUG ( 65): 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8 |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 138 | self.code_line = re.compile(r"(.*)[ \t]*[a-f0-9]" + self.width + |
| 139 | r"[ \t]*[a-f0-9]" + self.width + |
| 140 | r"[ \t]*[a-f0-9]" + self.width + |
| 141 | r"[ \t]*[a-f0-9]" + self.width + |
| 142 | r"[ \t]*[a-f0-9]" + self.width + |
| 143 | r"[ \t]*[ \r\n]") # pylint: disable-msg=C6310 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 144 | |
| 145 | def CleanLine(self, ln): |
| 146 | # AndroidFeedback adds zero width spaces into its crash reports. These |
| 147 | # should be removed or the regular expresssions will fail to match. |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 148 | return ln.encode().decode(encoding='utf8', errors='ignore') |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 149 | |
| 150 | def PrintTraceLines(self, trace_lines): |
| 151 | """Print back trace.""" |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 152 | maxlen = max(len(tl[1]) for tl in trace_lines) |
| 153 | print("\nStack Trace:") |
| 154 | print(" RELADDR " + self.spacing + "FUNCTION".ljust(maxlen) + " FILE:LINE") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 155 | for tl in self.trace_lines: |
| 156 | (addr, symbol_with_offset, location) = tl |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 157 | print(" %8s %s %s" % (addr, symbol_with_offset.ljust(maxlen), location)) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 158 | |
| 159 | def PrintValueLines(self, value_lines): |
| 160 | """Print stack data values.""" |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 161 | maxlen = max(len(tl[2]) for tl in self.value_lines) |
| 162 | print("\nStack Data:") |
| 163 | print(" ADDR " + self.spacing + "VALUE " + "FUNCTION".ljust(maxlen) + " FILE:LINE") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 164 | for vl in self.value_lines: |
| 165 | (addr, value, symbol_with_offset, location) = vl |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 166 | print(" %8s %8s %s %s" % (addr, value, symbol_with_offset.ljust(maxlen), location)) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 167 | |
| 168 | def PrintOutput(self, trace_lines, value_lines): |
| 169 | if self.trace_lines: |
| 170 | self.PrintTraceLines(self.trace_lines) |
| 171 | if self.value_lines: |
| 172 | self.PrintValueLines(self.value_lines) |
| 173 | |
| 174 | def PrintDivider(self): |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 175 | print("\n-----------------------------------------------------\n") |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 176 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 177 | def DeleteApkTmpFiles(self): |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 178 | for _, _, tmp_files in self.apk_info.values(): |
| 179 | for tmp_file in tmp_files.values(): |
| 180 | os.unlink(tmp_file) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 181 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 182 | def ConvertTrace(self, lines): |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 183 | lines = [self.CleanLine(line) for line in lines] |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 184 | try: |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 185 | if not symbol.ARCH: |
| 186 | symbol.SetAbi(lines) |
| 187 | self.UpdateAbiRegexes() |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 188 | for line in lines: |
| 189 | self.ProcessLine(line) |
| 190 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 191 | finally: |
| 192 | # Delete any temporary files created while processing the lines. |
| 193 | self.DeleteApkTmpFiles() |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 194 | |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 195 | def MatchTraceLine(self, line): |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 196 | match = self.trace_line.match(line) |
| 197 | if match: |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 198 | return {"frame": match.group("frame"), |
| 199 | "offset": match.group("offset"), |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 200 | "so_offset": match.group("so_offset"), |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 201 | "dso": match.group("dso"), |
| 202 | "symbol_present": bool(match.group("symbolpresent")), |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 203 | "symbol_name": match.group("symbol"), |
| 204 | "build_id": match.group("build_id")} |
| 205 | match = self.sanitizer_trace_line.match(line) |
| 206 | if match: |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 207 | return {"frame": match.group("frame"), |
| 208 | "offset": match.group("offset"), |
| Andreas Gampe | 57acd5f | 2015-09-17 11:44:21 -0700 | [diff] [blame] | 209 | "so_offset": None, |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 210 | "dso": match.group("dso"), |
| 211 | "symbol_present": False, |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 212 | "symbol_name": None, |
| 213 | "build_id": None} |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 214 | return None |
| 215 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 216 | def ExtractLibFromApk(self, apk, shared_lib_name): |
| 217 | # Create a temporary file containing the shared library from the apk. |
| 218 | tmp_file = None |
| 219 | try: |
| 220 | tmp_fd, tmp_file = tempfile.mkstemp() |
| 221 | if subprocess.call(["unzip", "-p", apk, shared_lib_name], stdout=tmp_fd) == 0: |
| 222 | os.close(tmp_fd) |
| 223 | shared_file = tmp_file |
| 224 | tmp_file = None |
| 225 | return shared_file |
| 226 | finally: |
| 227 | if tmp_file: |
| 228 | os.close(tmp_fd) |
| 229 | os.unlink(tmp_file) |
| 230 | return None |
| 231 | |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 232 | def ProcessCentralInfo(self, offset_list, central_info): |
| 233 | match = self.zipinfo_central_info_match.search(central_info) |
| 234 | if not match: |
| 235 | raise Exception("Cannot find all info from zipinfo\n" + central_info) |
| 236 | name = match.group(1) |
| 237 | start = int(match.group(2)) |
| 238 | end = start + int(match.group(3)) |
| 239 | |
| 240 | offset_list.append([name, start, end]) |
| 241 | return name, start, end |
| 242 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 243 | def GetLibFromApk(self, apk, offset): |
| 244 | # Convert the string to hex. |
| 245 | offset = int(offset, 16) |
| 246 | |
| 247 | # Check if we already have information about this offset. |
| 248 | if apk in self.apk_info: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 249 | apk_full_path, offset_list, tmp_files = self.apk_info[apk] |
| 250 | for file_name, start, end in offset_list: |
| 251 | if offset >= start and offset < end: |
| 252 | if file_name in tmp_files: |
| 253 | return file_name, tmp_files[file_name] |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 254 | tmp_file = self.ExtractLibFromApk(apk_full_path, file_name) |
| 255 | if tmp_file: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 256 | tmp_files[file_name] = tmp_file |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 257 | return file_name, tmp_file |
| 258 | break |
| 259 | return None, None |
| 260 | |
| 261 | if not "ANDROID_PRODUCT_OUT" in os.environ: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 262 | print("ANDROID_PRODUCT_OUT environment variable not set.") |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 263 | return None, None |
| 264 | out_dir = os.environ["ANDROID_PRODUCT_OUT"] |
| 265 | if not os.path.exists(out_dir): |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 266 | print("ANDROID_PRODUCT_OUT", out_dir, "does not exist.") |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 267 | return None, None |
| 268 | if apk.startswith("/"): |
| 269 | apk_full_path = out_dir + apk |
| 270 | else: |
| 271 | apk_full_path = os.path.join(out_dir, apk) |
| 272 | if not os.path.exists(apk_full_path): |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 273 | print("Cannot find apk", apk) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 274 | return None, None |
| 275 | |
| Christopher Ferris | 7ea56f0 | 2021-10-21 00:20:18 +0000 | [diff] [blame] | 276 | cmd = subprocess.Popen(["zipinfo", "-v", apk_full_path], stdout=subprocess.PIPE, |
| 277 | encoding='utf8') |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 278 | # Find the first central info marker. |
| 279 | for line in cmd.stdout: |
| 280 | if self.zipinfo_central_directory_line.search(line): |
| 281 | break |
| 282 | |
| 283 | central_info = "" |
| 284 | file_name = None |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 285 | offset_list = [] |
| 286 | for line in cmd.stdout: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 287 | match = self.zipinfo_central_directory_line.search(line) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 288 | if match: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 289 | cur_name, start, end = self.ProcessCentralInfo(offset_list, central_info) |
| 290 | if not file_name and offset >= start and offset < end: |
| 291 | file_name = cur_name |
| 292 | central_info = "" |
| 293 | else: |
| 294 | central_info += line |
| 295 | if central_info: |
| 296 | cur_name, start, end = self.ProcessCentralInfo(offset_list, central_info) |
| 297 | if not file_name and offset >= start and offset < end: |
| 298 | file_name = cur_name |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 299 | |
| Christopher Ferris | 7ea56f0 | 2021-10-21 00:20:18 +0000 | [diff] [blame] | 300 | # Make sure the offset_list is sorted, the zip file does not guarantee |
| 301 | # that the entries are in order. |
| 302 | offset_list = sorted(offset_list, key=lambda entry: entry[1]) |
| 303 | |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 304 | # Save the information from the zip. |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 305 | tmp_files = dict() |
| 306 | self.apk_info[apk] = [apk_full_path, offset_list, tmp_files] |
| 307 | if not file_name: |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 308 | return None, None |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 309 | tmp_shared_lib = self.ExtractLibFromApk(apk_full_path, file_name) |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 310 | if tmp_shared_lib: |
| Christopher Ferris | abe22f4 | 2016-03-16 12:17:59 -0700 | [diff] [blame] | 311 | tmp_files[file_name] = tmp_shared_lib |
| 312 | return file_name, tmp_shared_lib |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 313 | return None, None |
| 314 | |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 315 | # Find all files in the symbols directory and group them by basename (without directory). |
| David Srbecky | df6d482 | 2021-10-29 11:35:18 +0100 | [diff] [blame] | 316 | @functools.lru_cache(maxsize=None) |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 317 | def GlobSymbolsDir(self, symbols_dir): |
| 318 | files_by_basename = {} |
| 319 | for path in sorted(pathlib.Path(symbols_dir).glob("**/*")): |
| 320 | files_by_basename.setdefault(path.name, []).append(path) |
| 321 | return files_by_basename |
| 322 | |
| 323 | # Use the "file" command line tool to find the bitness and build_id of given ELF file. |
| David Srbecky | df6d482 | 2021-10-29 11:35:18 +0100 | [diff] [blame] | 324 | @functools.lru_cache(maxsize=None) |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 325 | def GetLibraryInfo(self, lib): |
| 326 | stdout = subprocess.check_output(["file", lib], text=True) |
| 327 | match = self.file_tool_output.search(stdout) |
| 328 | if match: |
| 329 | return self.ElfInfo(bitness=match.group("bitness"), build_id=match.group("build_id")) |
| 330 | return None |
| 331 | |
| 332 | # Search for a library with the given basename and build_id anywhere in the symbols directory. |
| David Srbecky | df6d482 | 2021-10-29 11:35:18 +0100 | [diff] [blame] | 333 | @functools.lru_cache(maxsize=None) |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 334 | def GetLibraryByBuildId(self, symbols_dir, basename, build_id): |
| Christopher Ferris | 27bee5a | 2022-01-25 13:05:53 -0800 | [diff] [blame] | 335 | for candidate in self.GlobSymbolsDir(symbols_dir).get(basename, []): |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 336 | info = self.GetLibraryInfo(candidate) |
| 337 | if info and info.build_id == build_id: |
| 338 | return "/" + str(candidate.relative_to(symbols_dir)) |
| 339 | return None |
| 340 | |
| Christopher Ferris | 1f2051d | 2021-09-10 14:41:00 -0700 | [diff] [blame] | 341 | def GetLibPath(self, lib): |
| 342 | symbol_dir = symbol.SYMBOLS_DIR |
| 343 | if os.path.isfile(symbol_dir + lib): |
| 344 | return lib |
| 345 | |
| Christopher Ferris | 598cc36 | 2022-04-22 14:37:56 -0700 | [diff] [blame] | 346 | # Try and rewrite any apex files if not found in symbols. |
| 347 | # For some reason, the directory in symbols does not match |
| 348 | # the path on system. |
| 349 | # The path is com.android.<directory> on device, but |
| 350 | # com.google.android.<directory> in symbols. |
| 351 | new_lib = lib.replace("/com.android.", "/com.google.android.") |
| 352 | if os.path.isfile(symbol_dir + new_lib): |
| 353 | return new_lib |
| 354 | |
| Christopher Ferris | 1f2051d | 2021-09-10 14:41:00 -0700 | [diff] [blame] | 355 | # When using atest, test paths are different between the out/ directory |
| 356 | # and device. Apply fixups. |
| 357 | if not lib.startswith("/data/local/tests/") and not lib.startswith("/data/local/tmp/"): |
| 358 | print("WARNING: Cannot find %s in symbol directory" % lib) |
| 359 | return lib |
| 360 | |
| 361 | test_name = lib.rsplit("/", 1)[-1] |
| 362 | test_dir = "/data/nativetest" |
| 363 | test_dir_bitness = "" |
| 364 | if symbol.ARCH.endswith("64"): |
| 365 | bitness = "64" |
| 366 | test_dir_bitness = "64" |
| 367 | else: |
| 368 | bitness = "32" |
| 369 | |
| 370 | # Unfortunately, the location of the real symbol file is not |
| 371 | # standardized, so we need to go hunting for it. |
| 372 | |
| 373 | # This is in vendor, look for the value in: |
| 374 | # /data/nativetest{64}/vendor/test_name/test_name |
| 375 | if lib.startswith("/data/local/tests/vendor/"): |
| 376 | lib_path = os.path.join(test_dir + test_dir_bitness, "vendor", test_name, test_name) |
| 377 | if os.path.isfile(symbol_dir + lib_path): |
| 378 | return lib_path |
| 379 | |
| 380 | # Look for the path in: |
| 381 | # /data/nativetest{64}/test_name/test_name |
| 382 | lib_path = os.path.join(test_dir + test_dir_bitness, test_name, test_name) |
| 383 | if os.path.isfile(symbol_dir + lib_path): |
| 384 | return lib_path |
| 385 | |
| 386 | # CtsXXX tests are in really non-standard locations try: |
| 387 | # /data/nativetest/{test_name} |
| 388 | lib_path = os.path.join(test_dir, test_name) |
| 389 | if os.path.isfile(symbol_dir + lib_path): |
| 390 | return lib_path |
| 391 | # Try: |
| 392 | # /data/nativetest/{test_name}{32|64} |
| 393 | lib_path += bitness |
| 394 | if os.path.isfile(symbol_dir + lib_path): |
| 395 | return lib_path |
| 396 | |
| 397 | # Cannot find location, give up and return the original path |
| 398 | print("WARNING: Cannot find %s in symbol directory" % lib) |
| 399 | return lib |
| 400 | |
| 401 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 402 | def ProcessLine(self, line): |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 403 | ret = False |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 404 | process_header = self.process_info_line.search(line) |
| 405 | signal_header = self.signal_line.search(line) |
| 406 | abort_message_header = self.abort_message_line.search(line) |
| 407 | thread_header = self.thread_line.search(line) |
| 408 | register_header = self.register_line.search(line) |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 409 | revision_header = self.revision_line.search(line) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 410 | dalvik_jni_thread_header = self.dalvik_jni_thread_line.search(line) |
| 411 | dalvik_native_thread_header = self.dalvik_native_thread_line.search(line) |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 412 | unreachable_header = self.unreachable_line.search(line) |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 413 | 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] | 414 | register_header or dalvik_jni_thread_header or dalvik_native_thread_header or \ |
| 415 | revision_header or unreachable_header: |
| 416 | ret = True |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 417 | if self.trace_lines or self.value_lines: |
| 418 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 419 | self.PrintDivider() |
| 420 | self.trace_lines = [] |
| 421 | self.value_lines = [] |
| 422 | self.last_frame = -1 |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 423 | if process_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 424 | print(process_header.group(1)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 425 | if signal_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 426 | print(signal_header.group(1)) |
| Elliott Hughes | d2471c8 | 2014-06-17 16:55:10 -0700 | [diff] [blame] | 427 | if abort_message_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 428 | print(abort_message_header.group(1)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 429 | if register_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 430 | print(register_header.group(1)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 431 | if thread_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 432 | print(thread_header.group(1)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 433 | if dalvik_jni_thread_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 434 | print(dalvik_jni_thread_header.group(1)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 435 | if dalvik_native_thread_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 436 | print(dalvik_native_thread_header.group(1)) |
| Brigid Smith | 0b30940 | 2014-07-07 14:34:00 -0700 | [diff] [blame] | 437 | if revision_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 438 | print(revision_header.group(1)) |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 439 | if unreachable_header: |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 440 | print(unreachable_header.group(1)) |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 441 | return True |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 442 | trace_line_dict = self.MatchTraceLine(line) |
| 443 | if trace_line_dict is not None: |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 444 | ret = True |
| Andreas Gampe | 48068ac | 2016-07-25 21:07:27 -0700 | [diff] [blame] | 445 | frame = int(trace_line_dict["frame"]) |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 446 | code_addr = trace_line_dict["offset"] |
| 447 | area = trace_line_dict["dso"] |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 448 | so_offset = trace_line_dict["so_offset"] |
| Andreas Gampe | d900d08 | 2015-08-21 15:25:03 -0700 | [diff] [blame] | 449 | symbol_present = trace_line_dict["symbol_present"] |
| 450 | symbol_name = trace_line_dict["symbol_name"] |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 451 | build_id = trace_line_dict["build_id"] |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 452 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 453 | if frame <= self.last_frame and (self.trace_lines or self.value_lines): |
| 454 | self.PrintOutput(self.trace_lines, self.value_lines) |
| 455 | self.PrintDivider() |
| 456 | self.trace_lines = [] |
| 457 | self.value_lines = [] |
| 458 | self.last_frame = frame |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 459 | |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 460 | if area == "<unknown>" or area == "[heap]" or area == "[stack]": |
| 461 | self.trace_lines.append((code_addr, "", area)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 462 | else: |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 463 | # If this is an apk, it usually means that there is actually |
| 464 | # a shared so that was loaded directly out of it. In that case, |
| 465 | # extract the shared library and the name of the shared library. |
| 466 | lib = None |
| Christopher Ferris | 55a9699 | 2019-03-13 15:37:31 -0700 | [diff] [blame] | 467 | # The format of the map name: |
| 468 | # Some.apk!libshared.so |
| 469 | # or |
| 470 | # Some.apk |
| 471 | if so_offset: |
| 472 | # If it ends in apk, we are done. |
| 473 | apk = None |
| 474 | if area.endswith(".apk"): |
| 475 | apk = area |
| 476 | else: |
| Christopher Ferris | 5e4b372 | 2020-01-31 14:38:07 -0800 | [diff] [blame] | 477 | index = area.rfind(".so!") |
| Christopher Ferris | 55a9699 | 2019-03-13 15:37:31 -0700 | [diff] [blame] | 478 | if index != -1: |
| Christopher Ferris | 5e4b372 | 2020-01-31 14:38:07 -0800 | [diff] [blame] | 479 | # Sometimes we'll see something like: |
| 480 | # #01 pc abcd libart.so!libart.so (offset 0x134000) |
| 481 | # Remove everything after the ! and zero the offset value. |
| 482 | area = area[0:index + 3] |
| 483 | so_offset = 0 |
| 484 | else: |
| 485 | index = area.rfind(".apk!") |
| 486 | if index != -1: |
| 487 | apk = area[0:index + 4] |
| Christopher Ferris | ac90d1d | 2019-03-15 12:21:57 -0700 | [diff] [blame] | 488 | if apk: |
| Christopher Ferris | 55a9699 | 2019-03-13 15:37:31 -0700 | [diff] [blame] | 489 | lib_name, lib = self.GetLibFromApk(apk, so_offset) |
| Christopher Ferris | 7d4d527 | 2022-05-05 15:04:58 -0700 | [diff] [blame] | 490 | else: |
| 491 | # Sometimes we'll see something like: |
| 492 | # #01 pc abcd libart.so!libart.so |
| 493 | # Remove everything after the !. |
| 494 | index = area.rfind(".so!") |
| 495 | if index != -1: |
| 496 | area = area[0:index + 3] |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 497 | if not lib: |
| 498 | lib = area |
| 499 | lib_name = None |
| 500 | |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 501 | if build_id: |
| 502 | # If we have the build_id, do a brute-force search of the symbols directory. |
| David Srbecky | df6d482 | 2021-10-29 11:35:18 +0100 | [diff] [blame] | 503 | basename = os.path.basename(lib) |
| 504 | lib = self.GetLibraryByBuildId(symbol.SYMBOLS_DIR, basename, build_id) |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 505 | if not lib: |
| 506 | print("WARNING: Cannot find {} with build id {} in symbols directory." |
| David Srbecky | df6d482 | 2021-10-29 11:35:18 +0100 | [diff] [blame] | 507 | .format(basename, build_id)) |
| David Srbecky | 3a3349e | 2021-10-28 13:08:10 +0100 | [diff] [blame] | 508 | else: |
| 509 | # When using atest, test paths are different between the out/ directory |
| 510 | # and device. Apply fixups. |
| 511 | lib = self.GetLibPath(lib) |
| Krzysztof Kosiński | dd45e18 | 2021-02-24 15:21:50 -0800 | [diff] [blame] | 512 | |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 513 | # If a calls b which further calls c and c is inlined to b, we want to |
| 514 | # 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] | 515 | info = symbol.SymbolInformation(lib, code_addr) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 516 | nest_count = len(info) - 1 |
| David Srbecky | 80547ae | 2021-11-01 21:59:59 +0000 | [diff] [blame] | 517 | for (source_symbol, source_location, symbol_with_offset) in info: |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 518 | if not source_symbol: |
| 519 | if symbol_present: |
| 520 | source_symbol = symbol.CallCppFilt(symbol_name) |
| 521 | else: |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 522 | source_symbol = "<unknown>" |
| David Srbecky | 80547ae | 2021-11-01 21:59:59 +0000 | [diff] [blame] | 523 | if not symbol.VERBOSE: |
| 524 | source_symbol = symbol.FormatSymbolWithoutParameters(source_symbol) |
| 525 | symbol_with_offset = symbol.FormatSymbolWithoutParameters(symbol_with_offset) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 526 | if not source_location: |
| 527 | source_location = area |
| Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 528 | if lib_name: |
| 529 | source_location += "(" + lib_name + ")" |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 530 | if nest_count > 0: |
| 531 | nest_count = nest_count - 1 |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 532 | arrow = "v------>" |
| 533 | if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64": |
| 534 | arrow = "v-------------->" |
| 535 | self.trace_lines.append((arrow, source_symbol, source_location)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 536 | else: |
| David Srbecky | 80547ae | 2021-11-01 21:59:59 +0000 | [diff] [blame] | 537 | if not symbol_with_offset: |
| 538 | symbol_with_offset = source_symbol |
| 539 | self.trace_lines.append((code_addr, symbol_with_offset, source_location)) |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 540 | if self.code_line.match(line): |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 541 | # Code lines should be ignored. If this were exluded the 'code around' |
| 542 | # sections would trigger value_line matches. |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 543 | return ret |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 544 | if self.value_line.match(line): |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 545 | ret = True |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 546 | match = self.value_line.match(line) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 547 | (unused_, addr, value, area, symbol_present, symbol_name) = match.groups() |
| Brigid Smith | ea0a835 | 2014-06-30 16:01:40 -0700 | [diff] [blame] | 548 | if area == "<unknown>" or area == "[heap]" or area == "[stack]" or not area: |
| 549 | self.value_lines.append((addr, value, "", area)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 550 | else: |
| 551 | info = symbol.SymbolInformation(area, value) |
| 552 | (source_symbol, source_location, object_symbol_with_offset) = info.pop() |
| Christopher Ferris | 5f1b4f0 | 2016-09-19 13:24:37 -0700 | [diff] [blame] | 553 | # If there is no information, skip this. |
| 554 | if source_symbol or source_location or object_symbol_with_offset: |
| 555 | if not source_symbol: |
| 556 | if symbol_present: |
| 557 | source_symbol = symbol.CallCppFilt(symbol_name) |
| 558 | else: |
| 559 | source_symbol = "<unknown>" |
| 560 | if not source_location: |
| 561 | source_location = area |
| 562 | if not object_symbol_with_offset: |
| 563 | object_symbol_with_offset = source_symbol |
| 564 | self.value_lines.append((addr, |
| 565 | value, |
| 566 | object_symbol_with_offset, |
| 567 | source_location)) |
| Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 568 | |
| Brigid Smith | 9c2192a | 2014-07-07 10:33:21 -0700 | [diff] [blame] | 569 | return ret |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 570 | |
| 571 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 572 | class RegisterPatternTests(unittest.TestCase): |
| 573 | def assert_register_matches(self, abi, example_crash, stupid_pattern): |
| 574 | tc = TraceConverter() |
| Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 575 | lines = example_crash.split('\n') |
| 576 | symbol.SetAbi(lines) |
| 577 | tc.UpdateAbiRegexes() |
| 578 | for line in lines: |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 579 | tc.ProcessLine(line) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 580 | is_register = (re.search(stupid_pattern, line) is not None) |
| 581 | matched = (tc.register_line.search(line) is not None) |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 582 | self.assertEqual(matched, is_register, line) |
| Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 583 | tc.PrintOutput(tc.trace_lines, tc.value_lines) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 584 | |
| 585 | def test_arm_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 586 | 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] | 587 | |
| 588 | def test_arm64_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 589 | 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] | 590 | |
| 591 | def test_mips_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 592 | 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] | 593 | |
| Andreas Gampe | 820ca72 | 2015-06-01 15:43:52 -0700 | [diff] [blame] | 594 | def test_mips64_registers(self): |
| 595 | self.assert_register_matches("mips64", example_crashes.mips64, '\\b(zr|a0|a4|t0|s0|s4|t8|gp|hi)\\b') |
| 596 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 597 | def test_x86_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 598 | 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] | 599 | |
| 600 | def test_x86_64_registers(self): |
| Elliott Hughes | c3166be | 2014-07-07 15:06:28 -0700 | [diff] [blame] | 601 | 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] | 602 | |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 603 | class LibmemunreachablePatternTests(unittest.TestCase): |
| 604 | def test_libmemunreachable(self): |
| 605 | tc = TraceConverter() |
| 606 | lines = example_crashes.libmemunreachable.split('\n') |
| 607 | |
| 608 | symbol.SetAbi(lines) |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 609 | self.assertEqual(symbol.ARCH, "arm") |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 610 | |
| 611 | tc.UpdateAbiRegexes() |
| 612 | header_lines = 0 |
| Colin Cross | 1127df9 | 2016-07-26 10:15:01 -0700 | [diff] [blame] | 613 | trace_lines = 0 |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 614 | for line in lines: |
| 615 | tc.ProcessLine(line) |
| 616 | if re.search(tc.unreachable_line, line) is not None: |
| 617 | header_lines += 1 |
| Colin Cross | 1127df9 | 2016-07-26 10:15:01 -0700 | [diff] [blame] | 618 | if tc.MatchTraceLine(line) is not None: |
| 619 | trace_lines += 1 |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 620 | self.assertEqual(header_lines, 3) |
| 621 | self.assertEqual(trace_lines, 2) |
| Colin Cross | 807ec0e | 2016-03-04 17:29:01 -0800 | [diff] [blame] | 622 | tc.PrintOutput(tc.trace_lines, tc.value_lines) |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 623 | |
| Andreas Gampe | 48068ac | 2016-07-25 21:07:27 -0700 | [diff] [blame] | 624 | class LongASANStackTests(unittest.TestCase): |
| 625 | # Test that a long ASAN-style (non-padded frame numbers) stack trace is not split into two |
| 626 | # when the frame number becomes two digits. This happened before as the frame number was |
| 627 | # handled as a string and not converted to an integral. |
| 628 | def test_long_asan_crash(self): |
| 629 | tc = TraceConverter() |
| 630 | lines = example_crashes.long_asan_crash.splitlines() |
| 631 | symbol.SetAbi(lines) |
| 632 | tc.UpdateAbiRegexes() |
| 633 | # Test by making sure trace_line_count is monotonically non-decreasing. If the stack trace |
| 634 | # is split, a separator is printed and trace_lines is flushed. |
| 635 | trace_line_count = 0 |
| 636 | for line in lines: |
| 637 | tc.ProcessLine(line) |
| 638 | self.assertLessEqual(trace_line_count, len(tc.trace_lines)) |
| 639 | trace_line_count = len(tc.trace_lines) |
| 640 | # The split happened at transition of frame #9 -> #10. Make sure we have parsed (and stored) |
| 641 | # more than ten frames. |
| 642 | self.assertGreater(trace_line_count, 10) |
| 643 | tc.PrintOutput(tc.trace_lines, tc.value_lines) |
| 644 | |
| Christopher Ferris | 5f1b4f0 | 2016-09-19 13:24:37 -0700 | [diff] [blame] | 645 | class ValueLinesTest(unittest.TestCase): |
| 646 | def test_value_line_skipped(self): |
| 647 | tc = TraceConverter() |
| 648 | symbol.SetAbi(["ABI: 'arm'"]) |
| 649 | tc.UpdateAbiRegexes() |
| 650 | tc.ProcessLine(" 12345678 00001000 .") |
| 651 | self.assertEqual([], tc.value_lines) |
| 652 | |
| Elliott Hughes | a9e3417 | 2014-07-01 14:56:22 -0700 | [diff] [blame] | 653 | if __name__ == '__main__': |
| Krzysztof Kosiński | b136111 | 2021-03-11 18:05:01 -0800 | [diff] [blame] | 654 | unittest.main(verbosity=2) |