| Index: build/android/pylib/symbols/stack_symbolizer.py
|
| diff --git a/build/android/pylib/symbols/stack_symbolizer.py b/build/android/pylib/symbols/stack_symbolizer.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f293ead00df3cc6af454f61e9a92c48d38ef97c9
|
| --- /dev/null
|
| +++ b/build/android/pylib/symbols/stack_symbolizer.py
|
| @@ -0,0 +1,105 @@
|
| +# Copyright 2017 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import logging
|
| +import os
|
| +import re
|
| +import shutil
|
| +import tempfile
|
| +
|
| +from devil.utils import cmd_helper
|
| +from pylib import constants
|
| +
|
| +STACK_TOOL = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..',
|
| + 'third_party', 'android_platform', 'development',
|
| + 'scripts', 'stack')
|
| +ABI_REG = re.compile('ABI: \'(.+?)\'')
|
| +
|
| +def _DeviceAbiToArch(device_abi):
|
| + # The order of this list is significant to find the more specific match
|
| + # (e.g., arm64) before the less specific (e.g., arm).
|
| + arches = ['arm64', 'arm', 'x86_64', 'x86_64', 'x86', 'mips']
|
| + for arch in arches:
|
| + if arch in device_abi:
|
| + return arch
|
| + raise RuntimeError('Unknown device ABI: %s' % device_abi)
|
| +
|
| +
|
| +class Symbolizer(object):
|
| + """ A helper class to symbolize stack. """
|
| +
|
| + def __init__(self, apk_under_test=None,
|
| + enable_relocation_packing=None):
|
| + self.apk_under_test = apk_under_test
|
| + self.enable_relocation_packing = enable_relocation_packing
|
| + self.libs_dir = None
|
| + self.apk_libs = []
|
| + self.has_unzipped = False
|
| +
|
| +
|
| + def __del__(self):
|
| + self.CleanUp()
|
| +
|
| +
|
| + def CleanUp(self):
|
| + """ Clean up the temporary directory of apk libs. """
|
| + if self.libs_dir:
|
| + shutil.rmtree(self.libs_dir)
|
| + self.libs_dir = None
|
| +
|
| +
|
| + def UnzipAPKIfNecessary(self):
|
| + """ Unzip apk if packed relocation is enabled. """
|
| + if (self.has_unzipped
|
| + or not self.enable_relocation_packing
|
| + or not self.apk_under_test):
|
| + return
|
| + self.libs_dir = tempfile.mkdtemp()
|
| + cmd_helper.GetCmdOutput(
|
| + ['unzip', '-o', self.apk_under_test, '-d', self.libs_dir])
|
| + self.has_unzipped = True
|
| + if os.path.exists(os.path.join(self.libs_dir, 'lib')):
|
| + for root, _, files in os.walk(os.path.join(self.libs_dir,
|
| + 'lib')):
|
| + for file_name in files:
|
| + if file_name.endswith('.so'):
|
| + self.apk_libs.append(os.path.join(root, file_name))
|
| +
|
| +
|
| + def ResolveSymbols(self, data_to_symbolize, device_abi, include_stack=True):
|
| + """Run the stack tool for given input.
|
| +
|
| + Args:
|
| + data_to_symbolize: a list of strings to symbolize.
|
| + include_stack: boolean whether to include stack data in output.
|
| + device_abi: the default ABI of the device which generated the tombstone.
|
| +
|
| + Yields:
|
| + A string for each line of resolved stack output.
|
| + """
|
| + self.UnzipAPKIfNecessary()
|
| + # Check if the data_to_symbolize has an ABI listed,
|
| + # if so use this in preference to the device's default ABI.
|
| + for line in data_to_symbolize:
|
| + found_abi = ABI_REG.match(line)
|
| + if found_abi:
|
| + device_abi = found_abi.group(1)
|
| + arch = _DeviceAbiToArch(device_abi)
|
| + if not arch:
|
| + logging.warning('No device_abi can be found.')
|
| + return
|
| +
|
| + cmd = [STACK_TOOL, '--arch', arch, '--output-directory',
|
| + constants.GetOutDirectory()]
|
| + if self.enable_relocation_packing and self.apk_libs:
|
| + for apk_lib in self.apk_libs:
|
| + cmd.extend(['--packed-lib', apk_lib])
|
| + with tempfile.NamedTemporaryFile() as f:
|
| + f.write('\n'.join(data_to_symbolize))
|
| + f.flush()
|
| + _, output = cmd_helper.GetCmdStatusAndOutput(cmd + [f.name])
|
| + for line in output.splitlines():
|
| + if not include_stack and 'Stack Data:' in line:
|
| + break
|
| + yield line
|
|
|