| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 3 # Copyright 2013 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 """Symbolizes stack traces generated by Chromium for Android. | |
| 8 | |
| 9 Sample usage: | |
| 10 adb logcat chromium:V | symbolize.py | |
| 11 """ | |
| 12 | |
| 13 import re | |
| 14 import sys | |
| 15 | |
| 16 from pylib.constants import host_paths | |
| 17 | |
| 18 # Uses symbol.py from third_party/android_platform, not python's. | |
| 19 with host_paths.SysPath( | |
| 20 host_paths.ANDROID_PLATFORM_DEVELOPMENT_SCRIPTS_PATH, | |
| 21 position=0): | |
| 22 import symbol | |
| 23 | |
| 24 # Sample output from base/debug/stack_trace_android.cc | |
| 25 #00 0x693cd34f /path/to/some/libfoo.so+0x0007434f | |
| 26 TRACE_LINE = re.compile(r'(?P<frame>\#[0-9]+ 0x[0-9a-f]{8,8}) ' | |
| 27 r'(?P<lib>[^+]+)\+0x(?P<addr>[0-9a-f]{8,8})') | |
| 28 | |
| 29 class Symbolizer(object): | |
| 30 def __init__(self, output): | |
| 31 self._output = output | |
| 32 | |
| 33 def write(self, data): | |
| 34 while True: | |
| 35 match = re.search(TRACE_LINE, data) | |
| 36 if not match: | |
| 37 self._output.write(data) | |
| 38 break | |
| 39 | |
| 40 frame = match.group('frame') | |
| 41 lib = match.group('lib') | |
| 42 addr = match.group('addr') | |
| 43 | |
| 44 # TODO(scherkus): Doing a single lookup per line is pretty slow, | |
| 45 # especially with larger libraries. Consider caching strategies such as: | |
| 46 # 1) Have Python load the libraries and do symbol lookups instead of | |
| 47 # calling out to addr2line each time. | |
| 48 # 2) Have Python keep multiple addr2line instances open as subprocesses, | |
| 49 # piping addresses and reading back symbols as we find them | |
| 50 # 3) Read ahead the entire stack trace until we find no more, then batch | |
| 51 # the symbol lookups. | |
| 52 # | |
| 53 # TODO(scherkus): These results are memoized, which could result in | |
| 54 # incorrect lookups when running this script on long-lived instances | |
| 55 # (e.g., adb logcat) when doing incremental development. Consider clearing | |
| 56 # the cache when modification timestamp of libraries change. | |
| 57 # pylint: disable=no-member | |
| 58 sym = symbol.SymbolInformation(lib, addr, False)[0][0] | |
| 59 | |
| 60 if not sym: | |
| 61 post = match.end('addr') | |
| 62 self._output.write(data[:post]) | |
| 63 data = data[post:] | |
| 64 continue | |
| 65 | |
| 66 pre = match.start('frame') | |
| 67 post = match.end('addr') | |
| 68 | |
| 69 self._output.write(data[:pre]) | |
| 70 self._output.write(frame) | |
| 71 self._output.write(' ') | |
| 72 self._output.write(sym) | |
| 73 | |
| 74 data = data[post:] | |
| 75 | |
| 76 def flush(self): | |
| 77 self._output.flush() | |
| 78 | |
| 79 | |
| 80 def main(): | |
| 81 symbolizer = Symbolizer(sys.stdout) | |
| 82 for line in sys.stdin: | |
| 83 symbolizer.write(line) | |
| 84 symbolizer.flush() | |
| 85 | |
| 86 | |
| 87 if __name__ == '__main__': | |
| 88 main() | |
| OLD | NEW |