OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 # gdb_helper.py |
| 7 |
| 8 ''' A bunch of helper functions for querying gdb.''' |
| 9 |
| 10 import logging |
| 11 import os |
| 12 import re |
| 13 import tempfile |
| 14 |
| 15 GDB_LINE_RE = re.compile(r'Line ([0-9]*) of "([^"]*)".*') |
| 16 |
| 17 def _GdbOutputToFileLine(output_line): |
| 18 ''' Parse the gdb output line, return a pair (file, line num) ''' |
| 19 match = GDB_LINE_RE.match(output_line) |
| 20 if match: |
| 21 return match.groups()[1], match.groups()[0] |
| 22 else: |
| 23 return None |
| 24 |
| 25 def ResolveAddressesWithinABinary(binary_name, load_address, address_list): |
| 26 ''' For each address, return a pair (file, line num) ''' |
| 27 commands = tempfile.NamedTemporaryFile() |
| 28 commands.write('add-symbol-file "%s" %s\n' % (binary_name, load_address)) |
| 29 for addr in address_list: |
| 30 commands.write('info line *%s\n' % addr) |
| 31 commands.write('quit\n') |
| 32 commands.flush() |
| 33 gdb_commandline = 'gdb -batch -x %s 2>/dev/null' % commands.name |
| 34 gdb_pipe = os.popen(gdb_commandline) |
| 35 result = gdb_pipe.readlines() |
| 36 |
| 37 address_count = 0 |
| 38 ret = {} |
| 39 for line in result: |
| 40 if line.startswith('Line'): |
| 41 ret[address_list[address_count]] = _GdbOutputToFileLine(line) |
| 42 address_count += 1 |
| 43 if line.startswith('No line'): |
| 44 ret[address_list[address_count]] = (None, None) |
| 45 address_count += 1 |
| 46 gdb_pipe.close() |
| 47 commands.close() |
| 48 return ret |
| 49 |
| 50 class AddressTable(object): |
| 51 ''' Object to do batched line number lookup. ''' |
| 52 def __init__(self): |
| 53 self._load_addresses = {} |
| 54 self._binaries = {} |
| 55 self._all_resolved = False |
| 56 |
| 57 def AddBinaryAt(self, binary, load_address): |
| 58 ''' Register a new shared library or executable. ''' |
| 59 self._load_addresses[binary] = load_address |
| 60 |
| 61 def Add(self, binary, address): |
| 62 ''' Register a lookup request. ''' |
| 63 if binary == '': |
| 64 logging.warn('adding address %s in empty binary?' % address) |
| 65 if binary in self._binaries: |
| 66 self._binaries[binary].append(address) |
| 67 else: |
| 68 self._binaries[binary] = [address] |
| 69 self._all_resolved = False |
| 70 |
| 71 def ResolveAll(self): |
| 72 ''' Carry out all lookup requests. ''' |
| 73 self._translation = {} |
| 74 for binary in self._binaries.keys(): |
| 75 if binary != '' and binary in self._load_addresses: |
| 76 load_address = self._load_addresses[binary] |
| 77 addr = ResolveAddressesWithinABinary(binary, load_address, self._binarie
s[binary]) |
| 78 self._translation[binary] = addr |
| 79 self._all_resolved = True |
| 80 |
| 81 def GetFileLine(self, binary, addr): |
| 82 ''' Get the (filename, linenum) result of a previously-registered lookup req
uest. ''' |
| 83 if self._all_resolved: |
| 84 if binary in self._translation: |
| 85 if addr in self._translation[binary]: |
| 86 return self._translation[binary][addr] |
| 87 return (None, None) |
OLD | NEW |