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 subprocess | |
11 import sys | |
12 | |
13 sys.path.append(os.path.join(sys.path[0], '..', '..', 'build', 'android')) | |
14 from pylib import constants | |
15 | |
16 | |
17 # TODO(lizeb): Fix this when the hardcoded assumptions (ARM 32bits, linux | |
pasko
2015/01/28 14:37:01
Can this be trivially reused via ToolPath() from t
Benoit L
2015/01/28 15:20:41
Thank you for the tip !
Done.
| |
18 # x86_64) are no longer true. Even better, encapsulate the | |
19 # architecture-dependent bits. | |
20 _NM_BINARY = os.path.join(constants.ANDROID_NDK_ROOT, 'toolchains', | |
21 'arm-linux-androideabi-4.9', 'prebuilt', | |
22 'linux-x86_64', 'bin', 'arm-linux-androideabi-nm') | |
23 | |
24 __SymbolInfo = collections.namedtuple( | |
pasko
2015/01/28 14:37:01
Two blank lines between top-level definitions:
htt
Benoit L
2015/01/28 15:20:41
Done.
| |
25 '__SymbolInfo', ('name', 'offset', 'size')) | |
26 | |
27 class SymbolInfo(__SymbolInfo): | |
pasko
2015/01/28 14:37:01
Making this a class complicates reading without cl
Benoit L
2015/01/28 15:20:41
Done.
| |
28 """Represent the information gathered from a symbol. | |
29 """ | |
30 __slots__ = () | |
31 | |
32 @classmethod | |
33 def fromNmLine(cls, line): | |
34 """Create a SymbolInfo by parsing a properly formatted nm output line. | |
35 | |
36 Args: | |
37 line: line from nm | |
38 | |
39 Returns: | |
40 An instance of SymbolInfo if the line represent a symbol, None otherwise. | |
pasko
2015/01/28 14:37:01
s/represent/represents/
Benoit L
2015/01/28 15:20:41
Done.
| |
41 """ | |
42 # We are interested in two types of lines: | |
43 # This: | |
44 # 00210d59 00000002 t _ZN34BrowserPluginHostMsg_Attach_ParamsD2Ev | |
45 # offset size <symbol_type> symbol_name | |
46 # And that: | |
47 # 0070ee8c T WebRtcSpl_ComplexBitReverse | |
48 # In the second case we don't have a size, so use -1 as a sentinel | |
49 if not any(x in line for x in (' t ', ' W ', ' T ')): | |
pasko
2015/01/28 14:37:02
I realize it was easy to write this line, but read
Benoit L
2015/01/28 15:20:41
Done.
| |
50 return None | |
51 parts = line.split() | |
52 if len(parts) == 4: | |
53 return SymbolInfo( | |
54 offset=int(parts[0], 16), size=int(parts[1], 16), name=parts[3]) | |
55 elif len(parts) == 3: | |
56 return SymbolInfo( | |
57 offset=int(parts[0], 16), size=-1, name=parts[2]) | |
58 else: | |
59 return None | |
60 | |
61 | |
62 def SymbolInfosFromStream(nm_lines): | |
63 """Parses the output of nm, and get all the symbols from a binary. | |
64 | |
65 Args: | |
66 nm_lines: An iterable of lines | |
67 | |
68 Returns: | |
69 The same output as GetSymbolsFromBinary. | |
pasko
2015/01/28 14:37:01
did you mean SymbolInfosFromBinary? Saying that it
Benoit L
2015/01/28 15:20:41
Done.
| |
70 """ | |
71 # TODO(lizeb): Consider switching to objdump to simplify parsing. | |
72 symbol_infos = [] | |
73 for line in nm_lines: | |
74 if not any(x in line for x in (' t ', ' W ', ' T ')): | |
pasko
2015/01/28 14:37:02
why checking it again here? the checkign for None
Benoit L
2015/01/28 15:20:41
Done.
| |
75 continue | |
76 symbol_info = SymbolInfo.fromNmLine(line) | |
77 if symbol_info: | |
78 symbol_infos.append(symbol_info) | |
79 return symbol_infos | |
80 | |
81 | |
82 def SymbolInfosFromBinary(binary_filename): | |
83 """Runs nm to get all the symbols from a binary. | |
84 | |
85 Args: | |
86 binary_filename: path to the binary. | |
87 | |
88 Returns: | |
89 A list of symbols from the binary. | |
90 """ | |
91 command = (_NM_BINARY, '-S', '-n', binary_filename) | |
92 p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE) | |
93 try: | |
94 result = SymbolInfosFromStream(p.stdout) | |
95 return result | |
96 finally: | |
97 p.wait() | |
98 | |
99 | |
100 def OffsetToSymbolInfos(symbol_infos): | |
pasko
2015/01/28 14:37:02
GroupSymbolInfosByOffset? GroupSymbolsByOffset? Gr
Benoit L
2015/01/28 15:20:41
Done.
| |
101 """Create a dict {offset: [symbol_info1, ...], ...}. | |
102 | |
103 As several symbols can be at the same offset, this is a 1-to-many | |
104 relationship. | |
105 | |
106 Args: | |
107 symbol_infos: iterable of SymbolInfo instances | |
108 | |
109 Returns: | |
110 a dict {offset: [symbol_info1, ...], ...} | |
111 """ | |
112 offset_to_symbol_infos = collections.defaultdict(list) | |
113 for symbol_info in symbol_infos: | |
114 offset_to_symbol_infos[symbol_info.offset].append(symbol_info) | |
115 return dict(offset_to_symbol_infos) | |
116 | |
117 | |
118 def NameToSymbolInfo(symbol_infos): | |
pasko
2015/01/28 14:37:01
CreateNameToSymbolInfo?
Benoit L
2015/01/28 15:20:41
Done.
| |
119 """Create a dict {name: symbol_info, ...}. | |
120 | |
121 Args: | |
122 symbol_infos: iterable of SymbolInfo instances | |
123 | |
124 Returns: | |
125 a dict {name: symbol_info, ...} | |
126 """ | |
127 return {symbol_info.name: symbol_info for symbol_info in symbol_infos} | |
OLD | NEW |