[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 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 os |
| 14 | import re |
| 15 | import sys |
| 16 | |
| 17 | from pylib import constants |
| 18 | |
| 19 | # Uses symbol.py from third_party/android_platform, not python's. |
| 20 | sys.path.insert(0, |
| 21 | os.path.join(constants.DIR_SOURCE_ROOT, |
| 22 | 'third_party/android_platform/development/scripts')) |
| 23 | import symbol |
| 24 | |
| 25 | # Sample output from base/debug/stack_trace_android.cc |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 26 | #00 0x693cd34f /path/to/some/libfoo.so+0x0007434f |
jbudorick | 0f77e96 | 2014-11-19 15:41:29 | [diff] [blame] | 27 | TRACE_LINE = re.compile(r'(?P<frame>\#[0-9]+ 0x[0-9a-f]{8,8}) ' |
| 28 | r'(?P<lib>[^+]+)\+0x(?P<addr>[0-9a-f]{8,8})') |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 29 | |
| 30 | class Symbolizer(object): |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 31 | def __init__(self, output): |
| 32 | self._output = output |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 33 | |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 34 | def write(self, data): |
| 35 | while True: |
| 36 | match = re.search(TRACE_LINE, data) |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 37 | if not match: |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 38 | self._output.write(data) |
| 39 | break |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 40 | |
| 41 | frame = match.group('frame') |
| 42 | lib = match.group('lib') |
| 43 | addr = match.group('addr') |
| 44 | |
| 45 | # TODO(scherkus): Doing a single lookup per line is pretty slow, |
| 46 | # especially with larger libraries. Consider caching strategies such as: |
| 47 | # 1) Have Python load the libraries and do symbol lookups instead of |
| 48 | # calling out to addr2line each time. |
| 49 | # 2) Have Python keep multiple addr2line instances open as subprocesses, |
| 50 | # piping addresses and reading back symbols as we find them |
| 51 | # 3) Read ahead the entire stack trace until we find no more, then batch |
| 52 | # the symbol lookups. |
| 53 | # |
| 54 | # TODO(scherkus): These results are memoized, which could result in |
| 55 | # incorrect lookups when running this script on long-lived instances |
| 56 | # (e.g., adb logcat) when doing incremental development. Consider clearing |
| 57 | # the cache when modification timestamp of libraries change. |
jbudorick | 58b4d36 | 2015-09-08 16:44:59 | [diff] [blame] | 58 | # pylint: disable=no-member |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 59 | sym = symbol.SymbolInformation(lib, addr, False)[0][0] |
| 60 | |
| 61 | if not sym: |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 62 | post = match.end('addr') |
| 63 | self._output.write(data[:post]) |
| 64 | data = data[post:] |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 65 | continue |
| 66 | |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 67 | pre = match.start('frame') |
| 68 | post = match.end('addr') |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 69 | |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 70 | self._output.write(data[:pre]) |
| 71 | self._output.write(frame) |
| 72 | self._output.write(' ') |
| 73 | self._output.write(sym) |
| 74 | |
| 75 | data = data[post:] |
| 76 | |
| 77 | def flush(self): |
| 78 | self._output.flush() |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def main(): |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 82 | symbolizer = Symbolizer(sys.stdout) |
| 83 | for line in sys.stdin: |
| 84 | symbolizer.write(line) |
| 85 | symbolizer.flush() |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 86 | |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | main() |