OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import json | |
7 import logging | 6 import logging |
8 import os | 7 import os |
9 import re | 8 import re |
10 import sys | 9 import sys |
11 | 10 |
12 from parse_proc_maps import parse_proc_maps | 11 from static_symbols import StaticSymbols |
13 from procedure_boundaries import get_procedure_boundaries_from_nm_bsd | |
14 from util import executable_condition | 12 from util import executable_condition |
15 | 13 |
16 | 14 |
17 def _determine_symbol_name(address, symbol): | 15 def _determine_symbol_name(address, symbol): |
18 if symbol: | 16 if symbol: |
19 return symbol.name | 17 return symbol.name |
20 else: | 18 else: |
21 return '0x%016x' % address | 19 return '0x%016x' % address |
22 | 20 |
23 | 21 |
(...skipping 19 matching lines...) Expand all Loading... |
43 self.with_address = with_address | 41 self.with_address = with_address |
44 | 42 |
45 def output(self, address, symbol=None): | 43 def output(self, address, symbol=None): |
46 symbol_name = _determine_symbol_name(address, symbol) | 44 symbol_name = _determine_symbol_name(address, symbol) |
47 if self.with_address: | 45 if self.with_address: |
48 self.result.write('%016x %s\n' % (address, symbol_name)) | 46 self.result.write('%016x %s\n' % (address, symbol_name)) |
49 else: | 47 else: |
50 self.result.write('%s\n' % symbol_name) | 48 self.result.write('%s\n' % symbol_name) |
51 | 49 |
52 | 50 |
53 def _find_runtime_symbols( | 51 def _find_runtime_symbols(static_symbols, addresses, outputter): |
54 prepared_data_dir, addresses, outputter, loglevel=logging.WARN): | 52 maps = static_symbols.maps |
55 log = logging.getLogger('find_runtime_symbols') | 53 symbol_tables = static_symbols.procedure_boundaries |
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 | 54 |
85 for address in addresses: | 55 for address in addresses: |
86 if isinstance(address, str): | 56 if isinstance(address, str): |
87 address = int(address, 16) | 57 address = int(address, 16) |
88 is_found = False | 58 is_found = False |
89 for entry in maps.iter(executable_condition): | 59 for entry in maps.iter(executable_condition): |
90 if entry.begin <= address < entry.end: | 60 if entry.begin <= address < entry.end: |
91 if entry.name in symbol_table: | 61 if entry.name in symbol_tables: |
92 found = symbol_table[entry.name].find_procedure( | 62 found = symbol_tables[entry.name].find_procedure( |
93 address - (entry.begin - entry.offset)) | 63 address - (entry.begin - entry.offset)) |
94 outputter.output(address, found) | 64 outputter.output(address, found) |
95 else: | 65 else: |
96 outputter.output(address) | 66 outputter.output(address) |
97 is_found = True | 67 is_found = True |
98 break | 68 break |
99 if not is_found: | 69 if not is_found: |
100 outputter.output(address) | 70 outputter.output(address) |
101 | 71 |
102 return 0 | 72 return 0 |
103 | 73 |
104 | 74 |
105 def find_runtime_symbols_list(prepared_data_dir, addresses): | 75 def find_runtime_symbols_list(static_symbols, addresses): |
106 result = [] | 76 result = [] |
107 _find_runtime_symbols(prepared_data_dir, addresses, _ListOutput(result)) | 77 _find_runtime_symbols(static_symbols, addresses, _ListOutput(result)) |
108 return result | 78 return result |
109 | 79 |
110 | 80 |
111 def find_runtime_symbols_dict(prepared_data_dir, addresses): | 81 def find_runtime_symbols_dict(static_symbols, addresses): |
112 result = {} | 82 result = {} |
113 _find_runtime_symbols(prepared_data_dir, addresses, _DictOutput(result)) | 83 _find_runtime_symbols(static_symbols, addresses, _DictOutput(result)) |
114 return result | 84 return result |
115 | 85 |
116 | 86 |
117 def find_runtime_symbols_file(prepared_data_dir, addresses, f): | 87 def find_runtime_symbols_file(static_symbols, addresses, f): |
118 _find_runtime_symbols( | 88 _find_runtime_symbols( |
119 prepared_data_dir, addresses, _FileOutput(f, False)) | 89 static_symbols, addresses, _FileOutput(f, False)) |
120 | 90 |
121 | 91 |
122 def main(): | 92 def main(): |
123 # FIX: Accept only .pre data | 93 # FIX: Accept only .pre data |
124 if len(sys.argv) < 2: | 94 if len(sys.argv) < 2: |
125 sys.stderr.write("""Usage: | 95 sys.stderr.write("""Usage: |
126 %s /path/to/prepared_data_dir/ < addresses.txt | 96 %s /path/to/prepared_data_dir/ < addresses.txt |
127 """ % sys.argv[0]) | 97 """ % sys.argv[0]) |
128 return 1 | 98 return 1 |
129 | 99 |
130 return find_runtime_symbols_file(sys.argv[1], sys.stdin, sys.stdout) | 100 log = logging.getLogger('find_runtime_symbols') |
| 101 log.setLevel(logging.WARN) |
| 102 handler = logging.StreamHandler() |
| 103 handler.setLevel(logging.WARN) |
| 104 formatter = logging.Formatter('%(message)s') |
| 105 handler.setFormatter(formatter) |
| 106 log.addHandler(handler) |
| 107 |
| 108 prepared_data_dir = sys.argv[1] |
| 109 if not os.path.exists(prepared_data_dir): |
| 110 log.warn("Nothing found: %s" % prepared_data_dir) |
| 111 return 1 |
| 112 if not os.path.isdir(prepared_data_dir): |
| 113 log.warn("Not a directory: %s" % prepared_data_dir) |
| 114 return 1 |
| 115 |
| 116 static_symbols = StaticSymbols.load(prepared_data_dir) |
| 117 return find_runtime_symbols_file(static_symbols, sys.stdin, sys.stdout) |
131 | 118 |
132 | 119 |
133 if __name__ == '__main__': | 120 if __name__ == '__main__': |
134 sys.exit(main()) | 121 sys.exit(main()) |
OLD | NEW |