OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 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 import json |
| 7 import logging |
| 8 import os |
| 9 import re |
| 10 import sys |
| 11 |
| 12 from parse_proc_maps import parse_proc_maps |
| 13 from procedure_boundaries import get_procedure_boundaries_from_nm_bsd |
| 14 from util import executable_condition |
| 15 |
| 16 |
| 17 def _determine_symbol_name(address, symbol): |
| 18 if symbol: |
| 19 return symbol.name |
| 20 else: |
| 21 return '0x%016x' % address |
| 22 |
| 23 |
| 24 class _ListOutput(object): |
| 25 def __init__(self, result): |
| 26 self.result = result |
| 27 |
| 28 def output(self, address, symbol=None): |
| 29 self.result.append(_determine_symbol_name(address, symbol)) |
| 30 |
| 31 |
| 32 class _DictOutput(object): |
| 33 def __init__(self, result): |
| 34 self.result = result |
| 35 |
| 36 def output(self, address, symbol=None): |
| 37 self.result[address] = _determine_symbol_name(address, symbol) |
| 38 |
| 39 |
| 40 class _FileOutput(object): |
| 41 def __init__(self, result, with_address): |
| 42 self.result = result |
| 43 self.with_address = with_address |
| 44 |
| 45 def output(self, address, symbol=None): |
| 46 symbol_name = _determine_symbol_name(address, symbol) |
| 47 if self.with_address: |
| 48 self.result.write('%016x %s\n' % (address, symbol_name)) |
| 49 else: |
| 50 self.result.write('%s\n' % symbol_name) |
| 51 |
| 52 |
| 53 def _find_runtime_symbols( |
| 54 prepared_data_dir, addresses, outputter, loglevel=logging.WARN): |
| 55 log = logging.getLogger('find_runtime_symbols') |
| 56 log.setLevel(loglevel) |
| 57 handler = logging.StreamHandler() |
| 58 handler.setLevel(loglevel) |
| 59 formatter = logging.Formatter('%(message)s') |
| 60 handler.setFormatter(formatter) |
| 61 log.addHandler(handler) |
| 62 |
| 63 if not os.path.exists(prepared_data_dir): |
| 64 log.warn("Nothing found: %s" % prepared_data_dir) |
| 65 return 1 |
| 66 if not os.path.isdir(prepared_data_dir): |
| 67 log.warn("Not a directory: %s" % prepared_data_dir) |
| 68 return 1 |
| 69 |
| 70 with open(os.path.join(prepared_data_dir, 'maps'), mode='r') as f: |
| 71 maps = parse_proc_maps(f) |
| 72 |
| 73 with open(os.path.join(prepared_data_dir, 'nm.json'), mode='r') as f: |
| 74 nm_files = json.load(f) |
| 75 |
| 76 symbol_table = {} |
| 77 for entry in maps.iter(executable_condition): |
| 78 if nm_files.has_key(entry.name): |
| 79 if nm_files[entry.name]['format'] == 'bsd': |
| 80 with open(os.path.join(prepared_data_dir, |
| 81 nm_files[entry.name]['file']), mode='r') as f: |
| 82 symbol_table[entry.name] = get_procedure_boundaries_from_nm_bsd( |
| 83 f, nm_files[entry.name]['mangled']) |
| 84 |
| 85 for address in addresses: |
| 86 if isinstance(address, str): |
| 87 address = int(address, 16) |
| 88 is_found = False |
| 89 for entry in maps.iter(executable_condition): |
| 90 if entry.begin <= address < entry.end: |
| 91 if entry.name in symbol_table: |
| 92 found = symbol_table[entry.name].find_procedure( |
| 93 address - (entry.begin - entry.offset)) |
| 94 outputter.output(address, found) |
| 95 else: |
| 96 outputter.output(address) |
| 97 is_found = True |
| 98 break |
| 99 if not is_found: |
| 100 outputter.output(address) |
| 101 |
| 102 return 0 |
| 103 |
| 104 |
| 105 def find_runtime_symbols_list(prepared_data_dir, addresses): |
| 106 result = [] |
| 107 _find_runtime_symbols(prepared_data_dir, addresses, _ListOutput(result)) |
| 108 return result |
| 109 |
| 110 |
| 111 def find_runtime_symbols_dict(prepared_data_dir, addresses): |
| 112 result = {} |
| 113 _find_runtime_symbols(prepared_data_dir, addresses, _DictOutput(result)) |
| 114 return result |
| 115 |
| 116 |
| 117 def find_runtime_symbols_file(prepared_data_dir, addresses, f): |
| 118 _find_runtime_symbols( |
| 119 prepared_data_dir, addresses, _FileOutput(f, False)) |
| 120 |
| 121 |
| 122 def main(): |
| 123 # FIX: Accept only .pre data |
| 124 if len(sys.argv) < 2: |
| 125 sys.stderr.write("""Usage: |
| 126 %s /path/to/prepared_data_dir/ < addresses.txt |
| 127 """ % sys.argv[0]) |
| 128 return 1 |
| 129 |
| 130 return find_runtime_symbols_file(sys.argv[1], sys.stdin, sys.stdout) |
| 131 |
| 132 |
| 133 if __name__ == '__main__': |
| 134 sys.exit(main()) |
OLD | NEW |