| 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 |
| 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 def _DeviceAbiToArch(device_abi): |
| 20 # The order of this list is significant to find the more specific match |
| 21 # (e.g., arm64) before the less specific (e.g., arm). |
| 22 arches = ['arm64', 'arm', 'x86_64', 'x86_64', 'x86', 'mips'] |
| 23 for arch in arches: |
| 24 if arch in device_abi: |
| 25 return arch |
| 26 raise RuntimeError('Unknown device ABI: %s' % device_abi) |
| 27 |
| 28 |
| 29 class Symbolizer(object): |
| 30 """ A helper class to symbolize stack. """ |
| 31 |
| 32 def __init__(self, apk_under_test=None, |
| 33 enable_relocation_packing=None): |
| 34 self.apk_under_test = apk_under_test |
| 35 self.enable_relocation_packing = enable_relocation_packing |
| 36 self.libs_dir = None |
| 37 self.apk_libs = [] |
| 38 self.has_unzipped = False |
| 39 |
| 40 |
| 41 def __del__(self): |
| 42 self.CleanUp() |
| 43 |
| 44 |
| 45 def CleanUp(self): |
| 46 """ Clean up the temporary directory of apk libs. """ |
| 47 if self.libs_dir: |
| 48 shutil.rmtree(self.libs_dir) |
| 49 self.libs_dir = None |
| 50 |
| 51 |
| 52 def UnzipAPKIfNecessary(self): |
| 53 """ Unzip apk if packed relocation is enabled. """ |
| 54 if (self.has_unzipped |
| 55 or not self.enable_relocation_packing |
| 56 or not self.apk_under_test): |
| 57 return |
| 58 self.libs_dir = tempfile.mkdtemp() |
| 59 cmd_helper.GetCmdOutput( |
| 60 ['unzip', '-o', self.apk_under_test, '-d', self.libs_dir]) |
| 61 self.has_unzipped = True |
| 62 if os.path.exists(os.path.join(self.libs_dir, 'lib')): |
| 63 for root, _, files in os.walk(os.path.join(self.libs_dir, |
| 64 'lib')): |
| 65 for file_name in files: |
| 66 if file_name.endswith('.so'): |
| 67 self.apk_libs.append(os.path.join(root, file_name)) |
| 68 |
| 69 |
| 70 def ResolveSymbols(self, data_to_symbolize, device_abi, include_stack=True): |
| 71 """Run the stack tool for given input. |
| 72 |
| 73 Args: |
| 74 data_to_symbolize: a list of strings to symbolize. |
| 75 include_stack: boolean whether to include stack data in output. |
| 76 device_abi: the default ABI of the device which generated the tombstone. |
| 77 |
| 78 Yields: |
| 79 A string for each line of resolved stack output. |
| 80 """ |
| 81 self.UnzipAPKIfNecessary() |
| 82 # Check if the data_to_symbolize has an ABI listed, |
| 83 # if so use this in preference to the device's default ABI. |
| 84 for line in data_to_symbolize: |
| 85 found_abi = ABI_REG.match(line) |
| 86 if found_abi: |
| 87 device_abi = found_abi.group(1) |
| 88 arch = _DeviceAbiToArch(device_abi) |
| 89 if not arch: |
| 90 logging.warning('No device_abi can be found.') |
| 91 return |
| 92 |
| 93 cmd = [STACK_TOOL, '--arch', arch, '--output-directory', |
| 94 constants.GetOutDirectory()] |
| 95 if self.enable_relocation_packing and self.apk_libs: |
| 96 for apk_lib in self.apk_libs: |
| 97 cmd.extend(['--packed-lib', apk_lib]) |
| 98 with tempfile.NamedTemporaryFile() as f: |
| 99 f.write('\n'.join(data_to_symbolize)) |
| 100 f.flush() |
| 101 _, output = cmd_helper.GetCmdStatusAndOutput(cmd + [f.name]) |
| 102 for line in output.splitlines(): |
| 103 if not include_stack and 'Stack Data:' in line: |
| 104 break |
| 105 yield line |
| OLD | NEW |