Chromium Code Reviews| Index: mojo/devtools/common/android_gdb/session.py |
| diff --git a/mojo/devtools/common/android_gdb/session.py b/mojo/devtools/common/android_gdb/session.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..80786d57a7504d70b68fae9ea89b16536fdd96fe |
| --- /dev/null |
| +++ b/mojo/devtools/common/android_gdb/session.py |
| @@ -0,0 +1,241 @@ |
| +# Copyright 2015 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 gdb |
| +import glob |
| +import itertools |
| +import os |
| +import os.path |
| +import re |
|
ppi
2015/06/24 15:14:07
Unused.
etiennej
2015/06/29 15:41:42
Done.
|
| +import shutil |
| +import socket |
|
ppi
2015/06/24 15:14:08
Unused?
etiennej
2015/06/29 15:41:42
Done.
|
| +import struct |
|
ppi
2015/06/24 15:14:08
Unused.
etiennej
2015/06/29 15:41:42
Done.
|
| +import subprocess |
| +import sys |
| +import tempfile |
| +import time |
| + |
| + |
| +sys.path.append(os.path.dirname(os.path.abspath(__file__))) |
| +import config |
| +from remote_file_connection import RemoteFileConnection |
| + |
| + |
| +def _GetDirAbove(dirname): |
| + """Returns the directory "above" this file containing |dirname|.""" |
| + path = os.path.abspath(__file__) |
| + while True: |
| + path, _ = os.path.split(path) |
| + assert path |
| + if dirname in os.listdir(path): |
| + return path |
| + |
| + |
| +def gdb_execute(command): |
| + """Execute a GDB command.""" |
|
ppi
2015/06/24 15:14:08
s/Execute/Executes/
etiennej
2015/06/29 15:41:42
Done.
|
| + return gdb.execute(command, to_string=True) |
| + |
| + |
| +class Mapping(object): |
| + def __init__(self, line): |
| + self.start = int(line[0], 16) |
| + self.end = int(line[1], 16) |
| + self.size = int(line[2], 16) |
| + self.offset = int(line[3], 16) |
| + self.filename = line[4] |
| + |
| + |
| +def get_mapped_files(): |
| + mappings = [Mapping(x) for x in |
| + [x.split() for x in |
| + gdb_execute("info proc map").split('\n')] |
| + if len(x) == 5 and x[4][0] == '/'] |
| + res = {} |
| + for m in mappings: |
| + libname = m.filename[m.filename.rfind('/') + 1:] |
| + res[libname] = res.get(libname, []) + [m] |
| + return res.values() |
| + |
| + |
| +class DebugSession(object): |
| + def __init__(self, |
| + build_directory=os.path.join( |
| + _GetDirAbove('out'), 'out', 'android_Debug'), |
| + package_name='org.chromium.mojo.shell', |
| + adb='adb', |
| + third_party_dir=os.path.join( |
| + _GetDirAbove('third_party'), 'third_party')): |
| + self.build_directory = build_directory |
| + self.package_name = package_name |
| + self.adb = adb |
| + self.remote_file_cache = os.path.join(os.getenv('HOME'), '.mojosymbols') |
| + |
| + sys.path.append(os.path.join(third_party_dir, 'pyelftools')) |
|
ppi
2015/06/24 15:14:08
devtools are meant to be consumed in other reposit
etiennej
2015/06/29 15:41:42
Done.
|
| + import elftools.elf.elffile as elffile |
| + self._elffile = elffile |
| + |
| + self.libraries = self._find_libraries(build_directory) |
| + self.rfc = RemoteFileConnection('localhost', 10000) |
| + if not os.path.exists(self.remote_file_cache): |
| + os.makedirs(self.remote_file_cache) |
| + self.done_mapping = set() |
| + self._downloaded_files = [] |
| + |
| + def __del__(self): |
| + gdbserver_pid = self._get_pid('gdbserver') |
| + if gdbserver_pid is not None: |
| + subprocess.check_call([self.adb, 'shell', 'kill', gdbserver_pid]) |
| + remote_file_reader_pid = self._get_pid('remote_file_reader') |
| + if remote_file_reader_pid is not None: |
| + subprocess.check_call([self.adb, 'shell', 'kill', remote_file_reader_pid]) |
| + |
| + def _find_libraries(self, lib_dir): |
| + res = {} |
| + for fn in glob.glob('%s/*.so' % lib_dir): |
| + with open(fn, 'r') as f: |
| + s = self._get_signature(f) |
| + if s is not None: |
| + res[s] = fn |
| + return res |
| + |
| + |
|
ppi
2015/06/24 15:14:08
Single blank lines between class members, please.
etiennej
2015/06/29 15:41:42
Done.
|
| + def _get_signature(self, file_object): |
| + """Compute a unique signature of a library file.""" |
| + elf = self._elffile.ELFFile(file_object) |
| + text_section = elf.get_section_by_name('.text') |
| + file_object.seek(text_section['sh_offset']) |
| + data = file_object.read(min(4096, text_section['sh_size'])) |
| + def combine((i, c)): |
| + return i ^ ord(c) |
| + result = 16 * [ 0 ] |
|
ppi
2015/06/24 15:14:08
Remove spaces around "0".
etiennej
2015/06/29 15:41:42
Done.
|
| + for i in xrange(0, len(data), len(result)): |
| + result = map(combine, |
| + itertools.izip_longest(result, |
| + data[i:i+len(result)], |
|
ppi
2015/06/24 15:14:08
Add spaces around "+".
etiennej
2015/06/29 15:41:42
Done.
|
| + fillvalue='\0')) |
| + return ''.join(["%02x" % x for x in result]) |
| + |
| + def _associate_symbols(self, mapping, local_file): |
| + with open(local_file, "r") as f: |
| + elf = self._elffile.ELFFile(f) |
| + s = elf.get_section_by_name(".text") |
| + text_address = mapping[0].start + s['sh_offset'] |
| + gdb_execute("add-symbol-file %s 0x%x" % (local_file, text_address)) |
| + |
| + def _download_file(self, remote): |
| + """Download a remote file through GDB connection. |
|
ppi
2015/06/24 15:14:08
s/Download/Downloads/
etiennej
2015/06/29 15:41:42
Done.
|
| + |
| + Returns the filename |
|
ppi
2015/06/24 15:14:08
The format is:
"""
Returns:
What is being return
etiennej
2015/06/29 15:41:42
Done.
|
| + """ |
| + temp_file = tempfile.NamedTemporaryFile() |
| + gdb_execute("remote get %s %s" % (remote, temp_file.name)) |
| + # This allows the deletion of temporary files on disk when the debugging |
| + # session terminates. |
| + self._downloaded_files.append(temp_file) |
| + return temp_file.name |
| + |
| + def _download_and_associate_symbol(self, mapping): |
| + self._associate_symbols(mapping, self._download_file(mapping[0].filename)) |
| + |
| + def _find_mapping_for_address(self, mappings, address): |
| + for m in mappings: |
| + for s in m: |
| + if address >= s.start and address <= s.end: |
| + return m |
| + return None |
| + |
| + def _try_to_map(self, mapping): |
| + remote_file = mapping[0].filename |
| + if remote_file in self.done_mapping: |
| + return False |
| + self.done_mapping.add(remote_file) |
| + self.rfc.open(remote_file) |
| + s = self._get_signature(self.rfc) |
| + if s is not None: |
| + if s in self.libraries: |
| + self._associate_symbols(mapping, self.libraries[s]) |
| + else: |
| + local_file = os.path.join(self.remote_file_cache, s) |
| + if not os.path.exists(local_file): |
| + tmp_output = self._download_file(remote_file) |
| + shutil.move(tmp_output, local_file) |
| + self._associate_symbols(mapping, local_file) |
| + return True |
| + return False |
| + |
| + def _update_symbols(self): |
| + """Update the mapping between symbols as seen from GDB and local library |
|
ppi
2015/06/24 15:14:08
s/Update/Updates/
etiennej
2015/06/29 15:41:42
Done.
|
| + files.""" |
| + mapped_files = get_mapped_files() |
| + gdb_execute("info threads") |
| + nb_threads = len(gdb_execute("info threads").split("\n")) - 2 |
| + # Map all symbols in /data/ |
| + for m in mapped_files: |
| + filename = m[0].filename |
| + if ((filename.startswith('/data/data/') or |
| + filename.startswith('/data/app')) and |
| + not filename.endswith('.apk') and |
| + not filename.endswith('.dex')): |
| + print 'Pre-mapping: %s' % m[0].filename |
| + self._try_to_map(m) |
| + for i in xrange(nb_threads): |
| + try: |
| + gdb_execute("thread %d" % (i + 1)) |
| + frame = gdb.newest_frame() |
| + while frame and frame.is_valid(): |
| + if frame.name() is None: |
| + m = self._find_mapping_for_address(mapped_files, frame.pc()) |
| + if m is not None and self._try_to_map(m): |
| + # Force gdb to recompute its frames. |
| + gdb_execute("info threads") |
| + frame = gdb.newest_frame() |
| + assert frame.is_valid() |
| + if (frame.older() is not None and |
| + frame.older().is_valid() and |
| + frame.older().pc() != frame.pc()): |
| + frame = frame.older() |
| + else: |
| + frame = None |
| + except gdb.error as _: |
| + import traceback |
| + traceback.print_exc() |
| + |
| + |
|
ppi
2015/06/24 15:14:08
Just one blank line.
etiennej
2015/06/29 15:41:42
Done.
|
| + def _get_pid(self, application): |
| + """Get the PID of an application running on a device.""" |
|
ppi
2015/06/24 15:14:08
s/Get/Gets/
etiennej
2015/06/29 15:41:42
Done.
|
| + output = subprocess.check_output([self.adb, 'shell', 'ps']) |
| + for line in output.split('\n'): |
| + elements = line.split() |
| + if len(elements) > 0 and elements[-1] == application: |
| + return elements[1] |
| + return None |
| + |
| + def start_debug(self): |
| + """Start a debugging session.""" |
|
ppi
2015/06/24 15:14:07
s/Start/Starts/
etiennej
2015/06/29 15:41:42
Done.
|
| + gdbserver_pid = self._get_pid('gdbserver') |
| + if gdbserver_pid is not None: |
| + subprocess.check_call([self.adb, 'shell', 'kill', gdbserver_pid]) |
| + shell_pid = self._get_pid(self.package_name) |
| + if shell_pid is None: |
| + raise Exception('Unable to find a running mojo shell.') |
| + subprocess.check_call([self.adb, 'forward', 'tcp:9999', 'tcp:9999']) |
| + subprocess.Popen( |
| + [self.adb, 'shell', 'gdbserver', '--attach', ':9999', shell_pid]) |
| + time.sleep(0.1) |
| + |
| + read_process = subprocess.Popen( |
| + [self.adb, 'shell', config.REMOTE_FILE_READER_DEVICE_PATH], |
| + stdout=subprocess.PIPE) |
| + port = int(read_process.stdout.readline()) |
| + subprocess.check_call([self.adb, 'forward', 'tcp:10000', 'tcp:%d' % port]) |
| + self.rfc.connect() |
| + |
| + gdb_execute('target remote localhost:9999') |
| + |
| + self._update_symbols() |
| + def on_stop(_): |
| + self._update_symbols() |
| + gdb.events.stop.connect(on_stop) |
| + |
|
ppi
2015/06/24 15:14:08
Remove the trailing blank lines.
etiennej
2015/06/29 15:41:42
Done.
|
| + |