Chromium Code Reviews| 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 z.extract(name, self._libs_dir) | |
| 68 | |
| 69 self._has_unzipped = True | |
| 70 if os.path.exists(os.path.join(self._libs_dir, 'lib')): | |
| 71 for root, _, files in os.walk(os.path.join(self._libs_dir, | |
| 72 'lib')): | |
| 73 for file_name in files: | |
| 74 if file_name.endswith('.so'): | |
| 75 self._apk_libs.append(os.path.join(root, file_name)) | |
|
agrieve
2017/07/26 17:50:38
nit: shouldn't need to do a separate tree walk her
BigBossZhiling
2017/07/26 18:36:58
Done.
| |
| 76 | |
| 77 | |
| 78 def ExtractAndResolveNativeStackTraces(self, data_to_symbolize, | |
| 79 device_abi, include_stack=True): | |
| 80 """Run the stack tool for given input. | |
| 81 | |
| 82 Args: | |
| 83 data_to_symbolize: a list of strings to symbolize. | |
| 84 include_stack: boolean whether to include stack data in output. | |
| 85 device_abi: the default ABI of the device which generated the tombstone. | |
| 86 | |
| 87 Yields: | |
| 88 A string for each line of resolved stack output. | |
| 89 """ | |
| 90 self.UnzipAPKIfNecessary() | |
| 91 arch = _DeviceAbiToArch(device_abi) | |
| 92 if not arch: | |
| 93 logging.warning('No device_abi can be found.') | |
| 94 return | |
| 95 | |
| 96 cmd = [_STACK_TOOL, '--arch', arch, '--output-directory', | |
| 97 constants.GetOutDirectory()] | |
| 98 if self._enable_relocation_packing and self._apk_libs: | |
| 99 for apk_lib in self._apk_libs: | |
| 100 cmd.extend(['--packed-lib', apk_lib]) | |
| 101 with tempfile.NamedTemporaryFile() as f: | |
| 102 f.write('\n'.join(data_to_symbolize)) | |
| 103 f.flush() | |
| 104 _, output = cmd_helper.GetCmdStatusAndOutput(cmd + [f.name]) | |
| 105 for line in output.splitlines(): | |
| 106 if not include_stack and 'Stack Data:' in line: | |
| 107 break | |
| 108 yield line | |
| OLD | NEW |