OLD | NEW |
(Empty) | |
| 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 |
| 8 import shutil |
| 9 import tempfile |
| 10 import zipfile |
| 11 |
| 12 from devil.utils import cmd_helper |
| 13 from pylib import constants |
| 14 |
| 15 _STACK_TOOL = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', |
| 16 'third_party', 'android_platform', 'development', |
| 17 'scripts', 'stack') |
| 18 ABI_REG = re.compile('ABI: \'(.+?)\'') |
| 19 |
| 20 |
| 21 def _DeviceAbiToArch(device_abi): |
| 22 # The order of this list is significant to find the more specific match |
| 23 # (e.g., arm64) before the less specific (e.g., arm). |
| 24 arches = ['arm64', 'arm', 'x86_64', 'x86_64', 'x86', 'mips'] |
| 25 for arch in arches: |
| 26 if arch in device_abi: |
| 27 return arch |
| 28 raise RuntimeError('Unknown device ABI: %s' % device_abi) |
| 29 |
| 30 |
| 31 class Symbolizer(object): |
| 32 """A helper class to symbolize stack.""" |
| 33 |
| 34 def __init__(self, apk_under_test=None, |
| 35 enable_relocation_packing=None): |
| 36 self._apk_under_test = apk_under_test |
| 37 self._enable_relocation_packing = enable_relocation_packing |
| 38 self._libs_dir = None |
| 39 self._apk_libs = [] |
| 40 self._has_unzipped = False |
| 41 |
| 42 |
| 43 def __del__(self): |
| 44 if self._libs_dir: |
| 45 logging.warning('Please call stack_symbolizer\'s' |
| 46 ' CleanUp method before it goes out of scope.') |
| 47 self.CleanUp() |
| 48 |
| 49 |
| 50 def CleanUp(self): |
| 51 """Clean up the temporary directory of apk libs.""" |
| 52 if self._libs_dir: |
| 53 shutil.rmtree(self._libs_dir) |
| 54 self._libs_dir = None |
| 55 |
| 56 |
| 57 def UnzipAPKIfNecessary(self): |
| 58 """Unzip apk if packed relocation is enabled.""" |
| 59 if (self._has_unzipped |
| 60 or not self._enable_relocation_packing |
| 61 or not self._apk_under_test): |
| 62 return |
| 63 self._libs_dir = tempfile.mkdtemp() |
| 64 with zipfile.ZipFile(self._apk_under_test) as z: |
| 65 for name in z.namelist(): |
| 66 if name.endswith('.so'): |
| 67 self._apk_libs.append(z.extract(name, self._libs_dir)) |
| 68 |
| 69 self._has_unzipped = True |
| 70 |
| 71 |
| 72 def ExtractAndResolveNativeStackTraces(self, data_to_symbolize, |
| 73 device_abi, include_stack=True): |
| 74 """Run the stack tool for given input. |
| 75 |
| 76 Args: |
| 77 data_to_symbolize: a list of strings to symbolize. |
| 78 include_stack: boolean whether to include stack data in output. |
| 79 device_abi: the default ABI of the device which generated the tombstone. |
| 80 |
| 81 Yields: |
| 82 A string for each line of resolved stack output. |
| 83 """ |
| 84 self.UnzipAPKIfNecessary() |
| 85 arch = _DeviceAbiToArch(device_abi) |
| 86 if not arch: |
| 87 logging.warning('No device_abi can be found.') |
| 88 return |
| 89 |
| 90 cmd = [_STACK_TOOL, '--arch', arch, '--output-directory', |
| 91 constants.GetOutDirectory(), '--more-info'] |
| 92 if self._enable_relocation_packing and self._apk_libs: |
| 93 for apk_lib in self._apk_libs: |
| 94 cmd.extend(['--packed-lib', apk_lib]) |
| 95 with tempfile.NamedTemporaryFile() as f: |
| 96 f.write('\n'.join(data_to_symbolize)) |
| 97 f.flush() |
| 98 _, output = cmd_helper.GetCmdStatusAndOutput(cmd + [f.name]) |
| 99 for line in output.splitlines(): |
| 100 if not include_stack and 'Stack Data:' in line: |
| 101 break |
| 102 yield line |
OLD | NEW |