2013-07-11 20:17:56 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2013 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""Symbolizes stack traces generated by Chromium for Android.
|
|
|
|
|
|
|
|
Sample usage:
|
|
|
|
adb logcat chromium:V | symbolize.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from pylib import constants
|
|
|
|
|
|
|
|
# Uses symbol.py from third_party/android_platform, not python's.
|
|
|
|
sys.path.insert(0,
|
|
|
|
os.path.join(constants.DIR_SOURCE_ROOT,
|
|
|
|
'third_party/android_platform/development/scripts'))
|
|
|
|
import symbol
|
|
|
|
|
|
|
|
# Sample output from base/debug/stack_trace_android.cc
|
2013-07-13 00:17:54 +04:00
|
|
|
#00 0x693cd34f /path/to/some/libfoo.so+0x0007434f
|
2014-11-19 18:41:29 +03:00
|
|
|
TRACE_LINE = re.compile(r'(?P<frame>\#[0-9]+ 0x[0-9a-f]{8,8}) '
|
|
|
|
r'(?P<lib>[^+]+)\+0x(?P<addr>[0-9a-f]{8,8})')
|
2013-07-11 20:17:56 +04:00
|
|
|
|
|
|
|
class Symbolizer(object):
|
2013-07-19 01:37:45 +04:00
|
|
|
def __init__(self, output):
|
|
|
|
self._output = output
|
2013-07-11 20:17:56 +04:00
|
|
|
|
2013-07-19 01:37:45 +04:00
|
|
|
def write(self, data):
|
|
|
|
while True:
|
|
|
|
match = re.search(TRACE_LINE, data)
|
2013-07-11 20:17:56 +04:00
|
|
|
if not match:
|
2013-07-19 01:37:45 +04:00
|
|
|
self._output.write(data)
|
|
|
|
break
|
2013-07-11 20:17:56 +04:00
|
|
|
|
|
|
|
frame = match.group('frame')
|
|
|
|
lib = match.group('lib')
|
|
|
|
addr = match.group('addr')
|
|
|
|
|
|
|
|
# TODO(scherkus): Doing a single lookup per line is pretty slow,
|
|
|
|
# especially with larger libraries. Consider caching strategies such as:
|
|
|
|
# 1) Have Python load the libraries and do symbol lookups instead of
|
|
|
|
# calling out to addr2line each time.
|
|
|
|
# 2) Have Python keep multiple addr2line instances open as subprocesses,
|
|
|
|
# piping addresses and reading back symbols as we find them
|
|
|
|
# 3) Read ahead the entire stack trace until we find no more, then batch
|
|
|
|
# the symbol lookups.
|
|
|
|
#
|
|
|
|
# TODO(scherkus): These results are memoized, which could result in
|
|
|
|
# incorrect lookups when running this script on long-lived instances
|
|
|
|
# (e.g., adb logcat) when doing incremental development. Consider clearing
|
|
|
|
# the cache when modification timestamp of libraries change.
|
|
|
|
sym = symbol.SymbolInformation(lib, addr, False)[0][0]
|
|
|
|
|
|
|
|
if not sym:
|
2013-07-19 01:37:45 +04:00
|
|
|
post = match.end('addr')
|
|
|
|
self._output.write(data[:post])
|
|
|
|
data = data[post:]
|
2013-07-11 20:17:56 +04:00
|
|
|
continue
|
|
|
|
|
2013-07-19 01:37:45 +04:00
|
|
|
pre = match.start('frame')
|
|
|
|
post = match.end('addr')
|
|
|
|
|
|
|
|
self._output.write(data[:pre])
|
|
|
|
self._output.write(frame)
|
|
|
|
self._output.write(' ')
|
|
|
|
self._output.write(sym)
|
|
|
|
|
|
|
|
data = data[post:]
|
2013-07-11 20:17:56 +04:00
|
|
|
|
2013-07-19 01:37:45 +04:00
|
|
|
def flush(self):
|
|
|
|
self._output.flush()
|
2013-07-11 20:17:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-07-19 01:37:45 +04:00
|
|
|
symbolizer = Symbolizer(sys.stdout)
|
|
|
|
for line in sys.stdin:
|
|
|
|
symbolizer.write(line)
|
|
|
|
symbolizer.flush()
|
2013-07-11 20:17:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|