OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # Copyright 2015 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 """Utilities to get and manipulate symbols from a binary.""" | |
7 | |
8 import collections | |
9 import os | |
10 import re | |
11 import subprocess | |
12 import sys | |
13 | |
14 sys.path.insert( | |
15 0, os.path.join(sys.path[0], '..', '..', 'third_party', 'android_platform', | |
16 'development', 'scripts')) | |
17 import symbol | |
18 | |
19 | |
20 # TODO(lizeb): Change symbol.ARCH to the proper value when "arm" is no longer | |
21 # the only possible value. | |
22 _NM_BINARY = symbol.ToolPath('nm') | |
23 | |
24 | |
25 SymbolInfo = collections.namedtuple('SymbolInfo', ('name', 'offset', 'size')) | |
26 | |
27 def FromNmLine(line): | |
pasko
2015/01/28 15:57:24
nit: add an extra newline above
Benoit L
2015/01/28 16:03:53
Done.
| |
28 """Create a SymbolInfo by parsing a properly formatted nm output line. | |
29 | |
30 Args: | |
31 line: line from nm | |
32 | |
33 Returns: | |
34 An instance of SymbolInfo if the line represents a symbol, None otherwise. | |
35 """ | |
36 # We are interested in two types of lines: | |
37 # This: | |
38 # 00210d59 00000002 t _ZN34BrowserPluginHostMsg_Attach_ParamsD2Ev | |
39 # offset size <symbol_type> symbol_name | |
40 # And that: | |
41 # 0070ee8c T WebRtcSpl_ComplexBitReverse | |
42 # In the second case we don't have a size, so use -1 as a sentinel | |
43 if not re.search(' (t|W|T) ', line): | |
pasko
2015/01/28 15:57:24
I meant rather two regexps:
'([0-9a-f]+)\s+([0-9a-
Benoit L
2015/01/28 16:03:53
Acknowledged.
| |
44 return None | |
45 parts = line.split() | |
46 if len(parts) == 4: | |
47 return SymbolInfo( | |
48 offset=int(parts[0], 16), size=int(parts[1], 16), name=parts[3]) | |
49 elif len(parts) == 3: | |
50 return SymbolInfo( | |
51 offset=int(parts[0], 16), size=-1, name=parts[2]) | |
52 else: | |
53 return None | |
54 | |
55 | |
56 def SymbolInfosFromStream(nm_lines): | |
57 """Parses the output of nm, and get all the symbols from a binary. | |
58 | |
59 Args: | |
60 nm_lines: An iterable of lines | |
61 | |
62 Returns: | |
63 A list of SymbolInfo. | |
64 """ | |
65 # TODO(lizeb): Consider switching to objdump to simplify parsing. | |
66 symbol_infos = [] | |
67 for line in nm_lines: | |
68 symbol_info = FromNmLine(line) | |
69 if symbol_info is not None: | |
70 symbol_infos.append(symbol_info) | |
71 return symbol_infos | |
72 | |
73 | |
74 def SymbolInfosFromBinary(binary_filename): | |
75 """Runs nm to get all the symbols from a binary. | |
76 | |
77 Args: | |
78 binary_filename: path to the binary. | |
79 | |
80 Returns: | |
81 A list of SymbolInfo from the binary. | |
82 """ | |
83 command = (_NM_BINARY, '-S', '-n', binary_filename) | |
84 p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE) | |
85 try: | |
86 result = SymbolInfosFromStream(p.stdout) | |
87 return result | |
88 finally: | |
89 p.wait() | |
90 | |
91 | |
92 def GroupSymbolInfosByOffset(symbol_infos): | |
93 """Create a dict {offset: [symbol_info1, ...], ...}. | |
94 | |
95 As several symbols can be at the same offset, this is a 1-to-many | |
96 relationship. | |
97 | |
98 Args: | |
99 symbol_infos: iterable of SymbolInfo instances | |
100 | |
101 Returns: | |
102 a dict {offset: [symbol_info1, ...], ...} | |
103 """ | |
104 offset_to_symbol_infos = collections.defaultdict(list) | |
105 for symbol_info in symbol_infos: | |
106 offset_to_symbol_infos[symbol_info.offset].append(symbol_info) | |
107 return dict(offset_to_symbol_infos) | |
108 | |
109 | |
110 def CreateNameToSymbolInfo(symbol_infos): | |
111 """Create a dict {name: symbol_info, ...}. | |
112 | |
113 Args: | |
114 symbol_infos: iterable of SymbolInfo instances | |
115 | |
116 Returns: | |
117 a dict {name: symbol_info, ...} | |
118 """ | |
119 return {symbol_info.name: symbol_info for symbol_info in symbol_infos} | |
OLD | NEW |