Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tools/find_runtime_symbols/find_runtime_symbols.py

Issue 10825075: Classify memory usage by allocated type in Deep Memory Profiler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
6 import logging 7 import logging
7 import os 8 import os
8 import re 9 import re
9 import sys 10 import sys
10 11
11 from static_symbols import StaticSymbols 12 from static_symbols import StaticSymbolsInFile
12 from util import executable_condition 13 from proc_maps import ProcMaps
13 14
14 15
15 def _determine_symbol_name(address, symbol): 16 _MAPS_FILENAME = 'maps'
16 if symbol: 17 _FILES_FILENAME = 'files.json'
17 return symbol.name
18 else:
19 return '0x%016x' % address
20 18
21 19
22 class _ListOutput(object): 20 class _ListOutput(object):
23 def __init__(self, result): 21 def __init__(self, result):
24 self.result = result 22 self.result = result
25 23
26 def output(self, address, symbol=None): 24 def output(self, address, symbol):
27 self.result.append(_determine_symbol_name(address, symbol)) 25 self.result.append(symbol)
28 26
29 27
30 class _DictOutput(object): 28 class _DictOutput(object):
31 def __init__(self, result): 29 def __init__(self, result):
32 self.result = result 30 self.result = result
33 31
34 def output(self, address, symbol=None): 32 def output(self, address, symbol):
35 self.result[address] = _determine_symbol_name(address, symbol) 33 self.result[address] = symbol
36 34
37 35
38 class _FileOutput(object): 36 class _FileOutput(object):
39 def __init__(self, result, with_address): 37 def __init__(self, result, with_address):
40 self.result = result 38 self.result = result
41 self.with_address = with_address 39 self.with_address = with_address
42 40
43 def output(self, address, symbol=None): 41 def output(self, address, symbol):
44 symbol_name = _determine_symbol_name(address, symbol)
45 if self.with_address: 42 if self.with_address:
46 self.result.write('%016x %s\n' % (address, symbol_name)) 43 self.result.write('%016x %s\n' % (address, symbol))
47 else: 44 else:
48 self.result.write('%s\n' % symbol_name) 45 self.result.write('%s\n' % symbol)
49 46
50 47
51 def _find_runtime_symbols(static_symbols, addresses, outputter): 48 class RuntimeSymbolsInProcess(object):
52 maps = static_symbols.maps 49 def __init__(self):
53 symbol_tables = static_symbols.procedure_boundaries 50 self._maps = None
51 self._static_symbols_in_filse = {}
54 52
53 def find_procedure(self, runtime_address):
54 for vma in self._maps.iter(ProcMaps.executable):
55 if vma.begin <= runtime_address < vma.end:
56 static_symbols = self._static_symbols_in_filse.get(vma.name)
57 if static_symbols:
58 return static_symbols.find_procedure_by_runtime_address(
59 runtime_address, vma)
60 else:
61 return None
62 return None
63
64 def find_typeinfo(self, runtime_address):
65 for vma in self._maps.iter(ProcMaps.constants):
66 if vma.begin <= runtime_address < vma.end:
67 static_symbols = self._static_symbols_in_filse.get(vma.name)
68 if static_symbols:
69 return static_symbols.find_typeinfo_by_runtime_address(
70 runtime_address, vma)
71 else:
72 return None
73 return None
74
75 @staticmethod
76 def load(prepared_data_dir):
77 symbols_in_process = RuntimeSymbolsInProcess()
78
79 with open(os.path.join(prepared_data_dir, _MAPS_FILENAME), mode='r') as f:
80 symbols_in_process._maps = ProcMaps.load(f)
81 with open(os.path.join(prepared_data_dir, _FILES_FILENAME), mode='r') as f:
82 files = json.load(f)
83
84 for vma in symbols_in_process._maps.iter(ProcMaps.executable_and_constants):
85 file_entry = files.get(vma.name)
86 if not file_entry:
87 continue
88
89 static_symbols = StaticSymbolsInFile(vma.name)
90
91 nm_entry = file_entry.get('nm')
92 if nm_entry and nm_entry['format'] == 'bsd':
93 with open(os.path.join(prepared_data_dir, nm_entry['file']), 'r') as f:
94 static_symbols.load_nm_bsd(f, nm_entry['mangled'])
95
96 readelf_entry = file_entry.get('readelf-e')
97 if readelf_entry:
98 with open(os.path.join(prepared_data_dir, readelf_entry['file']),
99 'r') as f:
100 static_symbols.load_readelf_ew(f)
101
102 symbols_in_process._static_symbols_in_filse[vma.name] = static_symbols
103
104 return symbols_in_process
105
106
107 def _find_runtime_symbols(symbols_in_process, addresses, outputter):
55 for address in addresses: 108 for address in addresses:
56 if isinstance(address, str): 109 if isinstance(address, basestring):
57 address = int(address, 16) 110 address = int(address, 16)
58 is_found = False 111 found = symbols_in_process.find_procedure(address)
59 for entry in maps.iter(executable_condition): 112 if found:
60 if entry.begin <= address < entry.end: 113 outputter.output(address, found.name)
61 if entry.name in symbol_tables: 114 else:
62 found = symbol_tables[entry.name].find_procedure( 115 outputter.output(address, '0x%016x' % address)
63 address - (entry.begin - entry.offset)) 116
117
118 def _find_runtime_typeinfo_symbols(symbols_in_process, addresses, outputter):
119 for address in addresses:
120 if isinstance(address, basestring):
121 address = int(address, 16)
122 if address == 0:
123 outputter.output(address, 'no typeinfo')
124 else:
125 found = symbols_in_process.find_typeinfo(address)
126 if found:
127 if found.startswith('typeinfo for '):
128 outputter.output(address, found[13:])
129 else:
64 outputter.output(address, found) 130 outputter.output(address, found)
65 else: 131 else:
66 outputter.output(address) 132 outputter.output(address, '0x%016x' % address)
67 is_found = True
68 break
69 if not is_found:
70 outputter.output(address)
71 133
72 return 0 134
135 def find_runtime_typeinfo_symbols_list(static_symbols, addresses):
136 result = []
137 _find_runtime_typeinfo_symbols(static_symbols, addresses, _ListOutput(result))
138 return result
139
140
141 def find_runtime_typeinfo_symbols_dict(static_symbols, addresses):
142 result = {}
143 _find_runtime_typeinfo_symbols(static_symbols, addresses, _DictOutput(result))
144 return result
145
146
147 def find_runtime_typeinfo_symbols_file(static_symbols, addresses, f):
148 _find_runtime_typeinfo_symbols(
149 static_symbols, addresses, _FileOutput(f, False))
73 150
74 151
75 def find_runtime_symbols_list(static_symbols, addresses): 152 def find_runtime_symbols_list(static_symbols, addresses):
76 result = [] 153 result = []
77 _find_runtime_symbols(static_symbols, addresses, _ListOutput(result)) 154 _find_runtime_symbols(static_symbols, addresses, _ListOutput(result))
78 return result 155 return result
79 156
80 157
81 def find_runtime_symbols_dict(static_symbols, addresses): 158 def find_runtime_symbols_dict(static_symbols, addresses):
82 result = {} 159 result = {}
(...skipping 23 matching lines...) Expand all
106 log.addHandler(handler) 183 log.addHandler(handler)
107 184
108 prepared_data_dir = sys.argv[1] 185 prepared_data_dir = sys.argv[1]
109 if not os.path.exists(prepared_data_dir): 186 if not os.path.exists(prepared_data_dir):
110 log.warn("Nothing found: %s" % prepared_data_dir) 187 log.warn("Nothing found: %s" % prepared_data_dir)
111 return 1 188 return 1
112 if not os.path.isdir(prepared_data_dir): 189 if not os.path.isdir(prepared_data_dir):
113 log.warn("Not a directory: %s" % prepared_data_dir) 190 log.warn("Not a directory: %s" % prepared_data_dir)
114 return 1 191 return 1
115 192
116 static_symbols = StaticSymbols.load(prepared_data_dir) 193 symbols_in_process = RuntimeSymbolsInProcess.load(prepared_data_dir)
117 return find_runtime_symbols_file(static_symbols, sys.stdin, sys.stdout) 194 return find_runtime_symbols_file(static_symbols, sys.stdin, sys.stdout)
118 195
119 196
120 if __name__ == '__main__': 197 if __name__ == '__main__':
121 sys.exit(main()) 198 sys.exit(main())
OLDNEW
« no previous file with comments | « tools/deep_memory_profiler/policy.t0.json ('k') | tools/find_runtime_symbols/parse_proc_maps.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698