hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 1 | # 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 | |
| 5 | import logging |
| 6 | import os |
| 7 | import re |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 8 | import tempfile |
Benjamin Pastene | b9097130 | 2017-12-20 02:41:20 | [diff] [blame] | 9 | import time |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 10 | |
| 11 | from devil.utils import cmd_helper |
| 12 | from pylib import constants |
| 13 | |
| 14 | _STACK_TOOL = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', |
| 15 | 'third_party', 'android_platform', 'development', |
| 16 | 'scripts', 'stack') |
| 17 | ABI_REG = re.compile('ABI: \'(.+?)\'') |
| 18 | |
| 19 | |
| 20 | def _DeviceAbiToArch(device_abi): |
John Budorick | 906a32fc | 2019-06-26 16:00:29 | [diff] [blame] | 21 | # 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) |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class Symbolizer(object): |
| 31 | """A helper class to symbolize stack.""" |
| 32 | |
Andrew Grieve | 7c179d7 | 2018-07-11 03:05:03 | [diff] [blame] | 33 | def __init__(self, apk_under_test=None): |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 34 | self._apk_under_test = apk_under_test |
Benjamin Pastene | b9097130 | 2017-12-20 02:41:20 | [diff] [blame] | 35 | self._time_spent_symbolizing = 0 |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def __del__(self): |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 39 | self.CleanUp() |
| 40 | |
| 41 | |
| 42 | def CleanUp(self): |
| 43 | """Clean up the temporary directory of apk libs.""" |
Benjamin Pastene | b9097130 | 2017-12-20 02:41:20 | [diff] [blame] | 44 | if self._time_spent_symbolizing > 0: |
| 45 | logging.info( |
| 46 | 'Total time spent symbolizing: %.2fs', self._time_spent_symbolizing) |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 47 | |
| 48 | |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 49 | 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 Budorick | 906a32fc | 2019-06-26 16:00:29 | [diff] [blame] | 61 | if not os.path.exists(_STACK_TOOL): |
| 62 | logging.warning('%s missing. Unable to resolve native stack traces.', |
| 63 | _STACK_TOOL) |
| 64 | return |
| 65 | |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 66 | 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 Budorick | 8ddab76 | 2017-10-09 16:03:36 | [diff] [blame] | 73 | env = dict(os.environ) |
| 74 | env['PYTHONDONTWRITEBYTECODE'] = '1' |
Egor Pasko | a61169b6 | 2021-07-05 14:16:15 | [diff] [blame^] | 75 | with tempfile.NamedTemporaryFile(mode='w') as f: |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 76 | f.write('\n'.join(data_to_symbolize)) |
| 77 | f.flush() |
Benjamin Pastene | b9097130 | 2017-12-20 02:41:20 | [diff] [blame] | 78 | 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 |
hzl | 55fc2af | 2017-07-27 08:24:50 | [diff] [blame] | 83 | for line in output.splitlines(): |
| 84 | if not include_stack and 'Stack Data:' in line: |
| 85 | break |
| 86 | yield line |