blob: fdd47780f7880676322bbfa7b433229c753c3f53 [file] [log] [blame]
hzl55fc2af2017-07-27 08:24:501# Copyright 2017 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7import re
hzl55fc2af2017-07-27 08:24:508import tempfile
Benjamin Pasteneb90971302017-12-20 02:41:209import time
hzl55fc2af2017-07-27 08:24:5010
11from devil.utils import cmd_helper
12from pylib import constants
13
14_STACK_TOOL = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..',
15 'third_party', 'android_platform', 'development',
16 'scripts', 'stack')
17ABI_REG = re.compile('ABI: \'(.+?)\'')
18
19
20def _DeviceAbiToArch(device_abi):
John Budorick906a32fc2019-06-26 16:00:2921 # The order of this list is significant to find the more specific match
22 # (e.g., arm64) before the less specific (e.g., arm).
23 arches = ['arm64', 'arm', 'x86_64', 'x86_64', 'x86', 'mips']
24 for arch in arches:
25 if arch in device_abi:
26 return arch
27 raise RuntimeError('Unknown device ABI: %s' % device_abi)
hzl55fc2af2017-07-27 08:24:5028
29
30class Symbolizer(object):
31 """A helper class to symbolize stack."""
32
Andrew Grieve7c179d72018-07-11 03:05:0333 def __init__(self, apk_under_test=None):
hzl55fc2af2017-07-27 08:24:5034 self._apk_under_test = apk_under_test
Benjamin Pasteneb90971302017-12-20 02:41:2035 self._time_spent_symbolizing = 0
hzl55fc2af2017-07-27 08:24:5036
37
38 def __del__(self):
hzl55fc2af2017-07-27 08:24:5039 self.CleanUp()
40
41
42 def CleanUp(self):
43 """Clean up the temporary directory of apk libs."""
Benjamin Pasteneb90971302017-12-20 02:41:2044 if self._time_spent_symbolizing > 0:
45 logging.info(
46 'Total time spent symbolizing: %.2fs', self._time_spent_symbolizing)
hzl55fc2af2017-07-27 08:24:5047
48
hzl55fc2af2017-07-27 08:24:5049 def ExtractAndResolveNativeStackTraces(self, data_to_symbolize,
50 device_abi, include_stack=True):
51 """Run the stack tool for given input.
52
53 Args:
54 data_to_symbolize: a list of strings to symbolize.
55 include_stack: boolean whether to include stack data in output.
56 device_abi: the default ABI of the device which generated the tombstone.
57
58 Yields:
59 A string for each line of resolved stack output.
60 """
John Budorick906a32fc2019-06-26 16:00:2961 if not os.path.exists(_STACK_TOOL):
62 logging.warning('%s missing. Unable to resolve native stack traces.',
63 _STACK_TOOL)
64 return
65
hzl55fc2af2017-07-27 08:24:5066 arch = _DeviceAbiToArch(device_abi)
67 if not arch:
68 logging.warning('No device_abi can be found.')
69 return
70
71 cmd = [_STACK_TOOL, '--arch', arch, '--output-directory',
72 constants.GetOutDirectory(), '--more-info']
John Budorick8ddab762017-10-09 16:03:3673 env = dict(os.environ)
74 env['PYTHONDONTWRITEBYTECODE'] = '1'
Egor Paskoa61169b62021-07-05 14:16:1575 with tempfile.NamedTemporaryFile(mode='w') as f:
hzl55fc2af2017-07-27 08:24:5076 f.write('\n'.join(data_to_symbolize))
77 f.flush()
Benjamin Pasteneb90971302017-12-20 02:41:2078 start = time.time()
79 try:
80 _, output = cmd_helper.GetCmdStatusAndOutput(cmd + [f.name], env=env)
81 finally:
82 self._time_spent_symbolizing += time.time() - start
hzl55fc2af2017-07-27 08:24:5083 for line in output.splitlines():
84 if not include_stack and 'Stack Data:' in line:
85 break
86 yield line