blob: a62afd923d215c0a5138de90022b747e8c477b06 [file] [log] [blame]
Ben Chengb42dad02013-04-25 15:14:04 -07001#!/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 Ferrisece64c42015-08-20 20:09:09 -070019import os
Ben Chengb42dad02013-04-25 15:14:04 -070020import re
Christopher Ferrisece64c42015-08-20 20:09:09 -070021import subprocess
Ben Chengb42dad02013-04-25 15:14:04 -070022import symbol
Christopher Ferrisece64c42015-08-20 20:09:09 -070023import tempfile
Elliott Hughesa9e34172014-07-01 14:56:22 -070024import unittest
Ben Chengb42dad02013-04-25 15:14:04 -070025
Elliott Hughesc3166be2014-07-07 15:06:28 -070026import example_crashes
27
Ben Chengb42dad02013-04-25 15:14:04 -070028def ConvertTrace(lines):
Brigid Smithea0a8352014-06-30 16:01:40 -070029 tracer = TraceConverter()
30 print "Reading symbols from", symbol.SYMBOLS_DIR
31 tracer.ConvertTrace(lines)
32
33class TraceConverter:
Ben Chengb42dad02013-04-25 15:14:04 -070034 process_info_line = re.compile("(pid: [0-9]+, tid: [0-9]+.*)")
Brigid Smith0b309402014-07-07 14:34:00 -070035 revision_line = re.compile("(Revision: \'(.*)\')")
Ben Chengb42dad02013-04-25 15:14:04 -070036 signal_line = re.compile("(signal [0-9]+ \(.*\).*)")
Elliott Hughesd2471c82014-06-17 16:55:10 -070037 abort_message_line = re.compile("(Abort message: '.*')")
Ben Chengb42dad02013-04-25 15:14:04 -070038 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 Smithea0a8352014-06-30 16:01:40 -070041 register_line = re.compile("$a")
42 trace_line = re.compile("$a")
Andreas Gamped900d082015-08-21 15:25:03 -070043 sanitizer_trace_line = re.compile("$a")
Brigid Smithea0a8352014-06-30 16:01:40 -070044 value_line = re.compile("$a")
45 code_line = re.compile("$a")
Christopher Ferrisece64c42015-08-20 20:09:09 -070046 unzip_line = re.compile("\s*(\d+)\s+\S+\s+\S+\s+(\S+)")
Ben Chengb42dad02013-04-25 15:14:04 -070047 trace_lines = []
48 value_lines = []
49 last_frame = -1
Brigid Smithea0a8352014-06-30 16:01:40 -070050 width = "{8}"
Elliott Hughesc3c86192014-08-29 13:49:57 -070051 spacing = ""
Christopher Ferrisece64c42015-08-20 20:09:09 -070052 apk_info = dict()
Ben Chengb42dad02013-04-25 15:14:04 -070053
Elliott Hughesa9e34172014-07-01 14:56:22 -070054 register_names = {
55 "arm": "r0|r1|r2|r3|r4|r5|r6|r7|r8|r9|sl|fp|ip|sp|lr|pc|cpsr",
Elliott Hughesbe4de462014-07-14 17:15:41 -070056 "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 Hughesa9e34172014-07-01 14:56:22 -070057 "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 Gampe55218412015-05-21 14:44:21 -070058 "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 Hughesa9e34172014-07-01 14:56:22 -070059 "x86": "eax|ebx|ecx|edx|esi|edi|x?cs|x?ds|x?es|x?fs|x?ss|eip|ebp|esp|flags",
60 "x86_64": "rax|rbx|rcx|rdx|rsi|rdi|r8|r9|r10|r11|r12|r13|r14|r15|cs|ss|rip|rbp|rsp|eflags",
61 }
62
63 def UpdateAbiRegexes(self):
Brigid Smithea0a8352014-06-30 16:01:40 -070064 if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64":
65 self.width = "{16}"
Elliott Hughesc3c86192014-08-29 13:49:57 -070066 self.spacing = " "
Brigid Smith15142f72014-07-15 13:47:07 -070067 else:
68 self.width = "{8}"
Elliott Hughesc3c86192014-08-29 13:49:57 -070069 self.spacing = ""
Brigid Smithea0a8352014-06-30 16:01:40 -070070
Elliott Hughesbe4de462014-07-14 17:15:41 -070071 self.register_line = re.compile("(([ ]*\\b(" + self.register_names[symbol.ARCH] + ")\\b +[0-9a-f]" + self.width + "){2,5})")
Brigid Smithea0a8352014-06-30 16:01:40 -070072
73 # Note that both trace and value line matching allow for variable amounts of
74 # whitespace (e.g. \t). This is because the we want to allow for the stack
75 # tool to operate on AndroidFeedback provided system logs. AndroidFeedback
76 # strips out double spaces that are found in tombsone files and logcat output.
77 #
78 # Examples of matched trace lines include lines from tombstone files like:
79 # #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so
80 #
81 # Or lines from AndroidFeedback crash report system logs like:
82 # 03-25 00:51:05.520 I/DEBUG ( 65): #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so
83 # Please note the spacing differences.
Andreas Gamped900d082015-08-21 15:25:03 -070084 self.trace_line = re.compile(
85 ".*" # Random start stuff.
86 "\#(?P<frame>[0-9]+)" # Frame number.
87 "[ \t]+..[ \t]+" # (space)pc(space).
88 "(?P<offset>[0-9a-f]" + self.width + ")[ \t]+" # Offset (hex number given without
89 # 0x prefix).
Christopher Ferrisc14b6122015-11-30 16:29:57 -080090 "(?P<dso>\[[^\]]+\]|[^\r\n \t]*)" # Library name.
Christopher Ferrisece64c42015-08-20 20:09:09 -070091 "( \(offset (?P<so_offset>0x[0-9a-fA-F]+)\))?" # Offset into the file to find the start of the shared so.
Andreas Gamped900d082015-08-21 15:25:03 -070092 "(?P<symbolpresent> \((?P<symbol>.*)\))?") # Is the symbol there?
93 # pylint: disable-msg=C6310
94 # Sanitizer output. This is different from debuggerd output, and it is easier to handle this as
95 # its own regex. Example:
96 # 08-19 05:29:26.283 397 403 I : #0 0xb6a15237 (/system/lib/libclang_rt.asan-arm-android.so+0x4f237)
97 self.sanitizer_trace_line = re.compile(
98 ".*" # Random start stuff.
99 "\#(?P<frame>[0-9]+)" # Frame number.
100 "[ \t]+0x[0-9a-f]+[ \t]+" # PC, not interesting to us.
101 "\(" # Opening paren.
102 "(?P<dso>[^+]+)" # Library name.
103 "\+" # '+'
104 "0x(?P<offset>[0-9a-f]+)" # Offset (hex number given with
105 # 0x prefix).
106 "\)") # Closin paren.
107 # pylint: disable-msg=C6310
Brigid Smithea0a8352014-06-30 16:01:40 -0700108 # Examples of matched value lines include:
109 # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so
110 # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so (symbol)
111 # 03-25 00:51:05.530 I/DEBUG ( 65): bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so
112 # Again, note the spacing differences.
113 self.value_line = re.compile("(.*)([0-9a-f]" + self.width + ")[ \t]+([0-9a-f]" + self.width + ")[ \t]+([^\r\n \t]*)( \((.*)\))?")
114 # Lines from 'code around' sections of the output will be matched before
115 # value lines because otheriwse the 'code around' sections will be confused as
116 # value lines.
117 #
118 # Examples include:
119 # 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8
120 # 03-25 00:51:05.530 I/DEBUG ( 65): 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8
121 self.code_line = re.compile("(.*)[ \t]*[a-f0-9]" + self.width +
122 "[ \t]*[a-f0-9]" + self.width +
123 "[ \t]*[a-f0-9]" + self.width +
124 "[ \t]*[a-f0-9]" + self.width +
125 "[ \t]*[a-f0-9]" + self.width +
126 "[ \t]*[ \r\n]") # pylint: disable-msg=C6310
127
128 def CleanLine(self, ln):
129 # AndroidFeedback adds zero width spaces into its crash reports. These
130 # should be removed or the regular expresssions will fail to match.
131 return unicode(ln, errors='ignore')
132
133 def PrintTraceLines(self, trace_lines):
134 """Print back trace."""
135 maxlen = max(map(lambda tl: len(tl[1]), trace_lines))
Brigid Smithea0a8352014-06-30 16:01:40 -0700136 print
137 print "Stack Trace:"
Elliott Hughesc3c86192014-08-29 13:49:57 -0700138 print " RELADDR " + self.spacing + "FUNCTION".ljust(maxlen) + " FILE:LINE"
Brigid Smithea0a8352014-06-30 16:01:40 -0700139 for tl in self.trace_lines:
140 (addr, symbol_with_offset, location) = tl
141 print " %8s %s %s" % (addr, symbol_with_offset.ljust(maxlen), location)
142 return
143
144 def PrintValueLines(self, value_lines):
145 """Print stack data values."""
146 maxlen = max(map(lambda tl: len(tl[2]), self.value_lines))
147 print
148 print "Stack Data:"
Elliott Hughesc3c86192014-08-29 13:49:57 -0700149 print " ADDR " + self.spacing + "VALUE " + "FUNCTION".ljust(maxlen) + " FILE:LINE"
Brigid Smithea0a8352014-06-30 16:01:40 -0700150 for vl in self.value_lines:
151 (addr, value, symbol_with_offset, location) = vl
152 print " %8s %8s %s %s" % (addr, value, symbol_with_offset.ljust(maxlen), location)
153 return
154
155 def PrintOutput(self, trace_lines, value_lines):
156 if self.trace_lines:
157 self.PrintTraceLines(self.trace_lines)
158 if self.value_lines:
159 self.PrintValueLines(self.value_lines)
160
161 def PrintDivider(self):
162 print
163 print "-----------------------------------------------------\n"
164
Christopher Ferrisece64c42015-08-20 20:09:09 -0700165 def DeleteApkTmpFiles(self):
166 for _, offset_list in self.apk_info.values():
167 for _, _, tmp_file in offset_list:
168 if tmp_file:
169 os.unlink(tmp_file)
170
Brigid Smithea0a8352014-06-30 16:01:40 -0700171 def ConvertTrace(self, lines):
172 lines = map(self.CleanLine, lines)
Christopher Ferrisece64c42015-08-20 20:09:09 -0700173 try:
Christopher Ferrisbf8a9402016-03-11 15:50:46 -0800174 if not symbol.ARCH:
175 symbol.SetAbi(lines)
176 self.UpdateAbiRegexes()
Christopher Ferrisece64c42015-08-20 20:09:09 -0700177 for line in lines:
178 self.ProcessLine(line)
179 self.PrintOutput(self.trace_lines, self.value_lines)
180 finally:
181 # Delete any temporary files created while processing the lines.
182 self.DeleteApkTmpFiles()
Brigid Smithea0a8352014-06-30 16:01:40 -0700183
Andreas Gamped900d082015-08-21 15:25:03 -0700184 def MatchTraceLine(self, line):
185 if self.trace_line.match(line):
186 match = self.trace_line.match(line)
187 return {"frame": match.group("frame"),
188 "offset": match.group("offset"),
Christopher Ferrisece64c42015-08-20 20:09:09 -0700189 "so_offset": match.group("so_offset"),
Andreas Gamped900d082015-08-21 15:25:03 -0700190 "dso": match.group("dso"),
191 "symbol_present": bool(match.group("symbolpresent")),
192 "symbol_name": match.group("symbol")}
193 if self.sanitizer_trace_line.match(line):
194 match = self.sanitizer_trace_line.match(line)
195 return {"frame": match.group("frame"),
196 "offset": match.group("offset"),
Andreas Gampe57acd5f2015-09-17 11:44:21 -0700197 "so_offset": None,
Andreas Gamped900d082015-08-21 15:25:03 -0700198 "dso": match.group("dso"),
199 "symbol_present": False,
200 "symbol_name": None}
201 return None
202
Christopher Ferrisece64c42015-08-20 20:09:09 -0700203 def ExtractLibFromApk(self, apk, shared_lib_name):
204 # Create a temporary file containing the shared library from the apk.
205 tmp_file = None
206 try:
207 tmp_fd, tmp_file = tempfile.mkstemp()
208 if subprocess.call(["unzip", "-p", apk, shared_lib_name], stdout=tmp_fd) == 0:
209 os.close(tmp_fd)
210 shared_file = tmp_file
211 tmp_file = None
212 return shared_file
213 finally:
214 if tmp_file:
215 os.close(tmp_fd)
216 os.unlink(tmp_file)
217 return None
218
219 def GetLibFromApk(self, apk, offset):
220 # Convert the string to hex.
221 offset = int(offset, 16)
222
223 # Check if we already have information about this offset.
224 if apk in self.apk_info:
225 apk_full_path, offset_list = self.apk_info[apk]
226 for current_offset, file_name, tmp_file in offset_list:
227 if offset <= current_offset:
228 if tmp_file:
229 return file_name, tmp_file
230 # This modifies the value in offset_list.
231 tmp_file = self.ExtractLibFromApk(apk_full_path, file_name)
232 if tmp_file:
233 return file_name, tmp_file
234 break
235 return None, None
236
237 if not "ANDROID_PRODUCT_OUT" in os.environ:
238 print "ANDROID_PRODUCT_OUT environment variable not set."
239 return None, None
240 out_dir = os.environ["ANDROID_PRODUCT_OUT"]
241 if not os.path.exists(out_dir):
242 print "ANDROID_PRODUCT_OUT " + out_dir + " does not exist."
243 return None, None
244 if apk.startswith("/"):
245 apk_full_path = out_dir + apk
246 else:
247 apk_full_path = os.path.join(out_dir, apk)
248 if not os.path.exists(apk_full_path):
249 print "Cannot find apk " + apk;
250 return None, None
251
252 cmd = subprocess.Popen(["unzip", "-lqq", apk_full_path], stdout=subprocess.PIPE)
253 current_offset = 0
254 file_entry = None
255 offset_list = []
256 for line in cmd.stdout:
257 match = self.unzip_line.match(line)
258 if match:
259 # Round the size up to a page boundary.
260 current_offset += (int(match.group(1), 10) + 0x1000) & ~0xfff
261 offset_entry = [current_offset - 1, match.group(2), None]
262 offset_list.append(offset_entry)
263 if offset < current_offset and not file_entry:
264 file_entry = offset_entry
265
266 # Save the information from the zip.
267 self.apk_info[apk] = [apk_full_path, offset_list]
268 if not file_entry:
269 return None, None
270 tmp_shared_lib = self.ExtractLibFromApk(apk_full_path, file_entry[1])
271 if tmp_shared_lib:
272 file_entry[2] = tmp_shared_lib
273 return file_entry[1], file_entry[2]
274 return None, None
275
Brigid Smithea0a8352014-06-30 16:01:40 -0700276 def ProcessLine(self, line):
Brigid Smith9c2192a2014-07-07 10:33:21 -0700277 ret = False
Brigid Smithea0a8352014-06-30 16:01:40 -0700278 process_header = self.process_info_line.search(line)
279 signal_header = self.signal_line.search(line)
280 abort_message_header = self.abort_message_line.search(line)
281 thread_header = self.thread_line.search(line)
282 register_header = self.register_line.search(line)
Brigid Smith0b309402014-07-07 14:34:00 -0700283 revision_header = self.revision_line.search(line)
Brigid Smithea0a8352014-06-30 16:01:40 -0700284 dalvik_jni_thread_header = self.dalvik_jni_thread_line.search(line)
285 dalvik_native_thread_header = self.dalvik_native_thread_line.search(line)
Christopher Ferrisbf8a9402016-03-11 15:50:46 -0800286 if process_header or signal_header or abort_message_header or thread_header or \
Brigid Smith0b309402014-07-07 14:34:00 -0700287 register_header or dalvik_jni_thread_header or dalvik_native_thread_header or revision_header:
Brigid Smithea0a8352014-06-30 16:01:40 -0700288 if self.trace_lines or self.value_lines:
289 self.PrintOutput(self.trace_lines, self.value_lines)
290 self.PrintDivider()
291 self.trace_lines = []
292 self.value_lines = []
293 self.last_frame = -1
Ben Chengb42dad02013-04-25 15:14:04 -0700294 if process_header:
295 print process_header.group(1)
296 if signal_header:
297 print signal_header.group(1)
Elliott Hughesd2471c82014-06-17 16:55:10 -0700298 if abort_message_header:
299 print abort_message_header.group(1)
Ben Chengb42dad02013-04-25 15:14:04 -0700300 if register_header:
301 print register_header.group(1)
302 if thread_header:
303 print thread_header.group(1)
304 if dalvik_jni_thread_header:
305 print dalvik_jni_thread_header.group(1)
306 if dalvik_native_thread_header:
307 print dalvik_native_thread_header.group(1)
Brigid Smith0b309402014-07-07 14:34:00 -0700308 if revision_header:
309 print revision_header.group(1)
Christopher Ferrisbf8a9402016-03-11 15:50:46 -0800310 return True
Andreas Gamped900d082015-08-21 15:25:03 -0700311 trace_line_dict = self.MatchTraceLine(line)
312 if trace_line_dict is not None:
Brigid Smith9c2192a2014-07-07 10:33:21 -0700313 ret = True
Andreas Gamped900d082015-08-21 15:25:03 -0700314 frame = trace_line_dict["frame"]
315 code_addr = trace_line_dict["offset"]
316 area = trace_line_dict["dso"]
Christopher Ferrisece64c42015-08-20 20:09:09 -0700317 so_offset = trace_line_dict["so_offset"]
Andreas Gamped900d082015-08-21 15:25:03 -0700318 symbol_present = trace_line_dict["symbol_present"]
319 symbol_name = trace_line_dict["symbol_name"]
Ben Chengb42dad02013-04-25 15:14:04 -0700320
Brigid Smithea0a8352014-06-30 16:01:40 -0700321 if frame <= self.last_frame and (self.trace_lines or self.value_lines):
322 self.PrintOutput(self.trace_lines, self.value_lines)
323 self.PrintDivider()
324 self.trace_lines = []
325 self.value_lines = []
326 self.last_frame = frame
Ben Chengb42dad02013-04-25 15:14:04 -0700327
Brigid Smithea0a8352014-06-30 16:01:40 -0700328 if area == "<unknown>" or area == "[heap]" or area == "[stack]":
329 self.trace_lines.append((code_addr, "", area))
Ben Chengb42dad02013-04-25 15:14:04 -0700330 else:
Christopher Ferrisece64c42015-08-20 20:09:09 -0700331 # If this is an apk, it usually means that there is actually
332 # a shared so that was loaded directly out of it. In that case,
333 # extract the shared library and the name of the shared library.
334 lib = None
335 if area.endswith(".apk") and so_offset:
336 lib_name, lib = self.GetLibFromApk(area, so_offset)
337 if not lib:
338 lib = area
339 lib_name = None
340
Ben Chengb42dad02013-04-25 15:14:04 -0700341 # If a calls b which further calls c and c is inlined to b, we want to
342 # display "a -> b -> c" in the stack trace instead of just "a -> c"
Christopher Ferrisece64c42015-08-20 20:09:09 -0700343 info = symbol.SymbolInformation(lib, code_addr)
Ben Chengb42dad02013-04-25 15:14:04 -0700344 nest_count = len(info) - 1
345 for (source_symbol, source_location, object_symbol_with_offset) in info:
346 if not source_symbol:
347 if symbol_present:
348 source_symbol = symbol.CallCppFilt(symbol_name)
349 else:
Brigid Smithea0a8352014-06-30 16:01:40 -0700350 source_symbol = "<unknown>"
Ben Chengb42dad02013-04-25 15:14:04 -0700351 if not source_location:
352 source_location = area
Christopher Ferrisece64c42015-08-20 20:09:09 -0700353 if lib_name:
354 source_location += "(" + lib_name + ")"
Ben Chengb42dad02013-04-25 15:14:04 -0700355 if nest_count > 0:
356 nest_count = nest_count - 1
Brigid Smithea0a8352014-06-30 16:01:40 -0700357 arrow = "v------>"
358 if symbol.ARCH == "arm64" or symbol.ARCH == "mips64" or symbol.ARCH == "x86_64":
359 arrow = "v-------------->"
360 self.trace_lines.append((arrow, source_symbol, source_location))
Ben Chengb42dad02013-04-25 15:14:04 -0700361 else:
362 if not object_symbol_with_offset:
363 object_symbol_with_offset = source_symbol
Brigid Smithea0a8352014-06-30 16:01:40 -0700364 self.trace_lines.append((code_addr,
Ben Chengb42dad02013-04-25 15:14:04 -0700365 object_symbol_with_offset,
366 source_location))
Brigid Smithea0a8352014-06-30 16:01:40 -0700367 if self.code_line.match(line):
Ben Chengb42dad02013-04-25 15:14:04 -0700368 # Code lines should be ignored. If this were exluded the 'code around'
369 # sections would trigger value_line matches.
Brigid Smith9c2192a2014-07-07 10:33:21 -0700370 return ret
Brigid Smithea0a8352014-06-30 16:01:40 -0700371 if self.value_line.match(line):
Brigid Smith9c2192a2014-07-07 10:33:21 -0700372 ret = True
Brigid Smithea0a8352014-06-30 16:01:40 -0700373 match = self.value_line.match(line)
Ben Chengb42dad02013-04-25 15:14:04 -0700374 (unused_, addr, value, area, symbol_present, symbol_name) = match.groups()
Brigid Smithea0a8352014-06-30 16:01:40 -0700375 if area == "<unknown>" or area == "[heap]" or area == "[stack]" or not area:
376 self.value_lines.append((addr, value, "", area))
Ben Chengb42dad02013-04-25 15:14:04 -0700377 else:
378 info = symbol.SymbolInformation(area, value)
379 (source_symbol, source_location, object_symbol_with_offset) = info.pop()
380 if not source_symbol:
381 if symbol_present:
382 source_symbol = symbol.CallCppFilt(symbol_name)
383 else:
Brigid Smithea0a8352014-06-30 16:01:40 -0700384 source_symbol = "<unknown>"
Ben Chengb42dad02013-04-25 15:14:04 -0700385 if not source_location:
386 source_location = area
387 if not object_symbol_with_offset:
388 object_symbol_with_offset = source_symbol
Brigid Smithea0a8352014-06-30 16:01:40 -0700389 self.value_lines.append((addr,
Ben Chengb42dad02013-04-25 15:14:04 -0700390 value,
391 object_symbol_with_offset,
392 source_location))
393
Brigid Smith9c2192a2014-07-07 10:33:21 -0700394 return ret
Elliott Hughesa9e34172014-07-01 14:56:22 -0700395
396
Elliott Hughesa9e34172014-07-01 14:56:22 -0700397class RegisterPatternTests(unittest.TestCase):
398 def assert_register_matches(self, abi, example_crash, stupid_pattern):
399 tc = TraceConverter()
Christopher Ferrisbf8a9402016-03-11 15:50:46 -0800400 lines = example_crash.split('\n')
401 symbol.SetAbi(lines)
402 tc.UpdateAbiRegexes()
403 for line in lines:
Elliott Hughesc3c86192014-08-29 13:49:57 -0700404 tc.ProcessLine(line)
Elliott Hughesa9e34172014-07-01 14:56:22 -0700405 is_register = (re.search(stupid_pattern, line) is not None)
406 matched = (tc.register_line.search(line) is not None)
407 self.assertEquals(matched, is_register, line)
Elliott Hughesc3c86192014-08-29 13:49:57 -0700408 tc.PrintOutput(tc.trace_lines, tc.value_lines)
Elliott Hughesa9e34172014-07-01 14:56:22 -0700409
410 def test_arm_registers(self):
Elliott Hughesc3166be2014-07-07 15:06:28 -0700411 self.assert_register_matches("arm", example_crashes.arm, '\\b(r0|r4|r8|ip)\\b')
Elliott Hughesa9e34172014-07-01 14:56:22 -0700412
413 def test_arm64_registers(self):
Elliott Hughesc3166be2014-07-07 15:06:28 -0700414 self.assert_register_matches("arm64", example_crashes.arm64, '\\b(x0|x4|x8|x12|x16|x20|x24|x28|sp)\\b')
Elliott Hughesa9e34172014-07-01 14:56:22 -0700415
416 def test_mips_registers(self):
Elliott Hughesc3166be2014-07-07 15:06:28 -0700417 self.assert_register_matches("mips", example_crashes.mips, '\\b(zr|a0|t0|t4|s0|s4|t8|gp|hi)\\b')
Elliott Hughesa9e34172014-07-01 14:56:22 -0700418
Andreas Gampe820ca722015-06-01 15:43:52 -0700419 def test_mips64_registers(self):
420 self.assert_register_matches("mips64", example_crashes.mips64, '\\b(zr|a0|a4|t0|s0|s4|t8|gp|hi)\\b')
421
Elliott Hughesa9e34172014-07-01 14:56:22 -0700422 def test_x86_registers(self):
Elliott Hughesc3166be2014-07-07 15:06:28 -0700423 self.assert_register_matches("x86", example_crashes.x86, '\\b(eax|esi|xcs|eip)\\b')
Elliott Hughesa9e34172014-07-01 14:56:22 -0700424
425 def test_x86_64_registers(self):
Elliott Hughesc3166be2014-07-07 15:06:28 -0700426 self.assert_register_matches("x86_64", example_crashes.x86_64, '\\b(rax|rsi|r8|r12|cs|rip)\\b')
Elliott Hughesa9e34172014-07-01 14:56:22 -0700427
428
429if __name__ == '__main__':
430 unittest.main()