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

Side by Side Diff: tools/cygprofile/symbol_extractor.py

Issue 884113002: Refactor the symbol parsing, and move to NDK's nm. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Missing blank line. Created 5 years, 10 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
OLDNEW
(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
28 def FromNmLine(line):
29 """Create a SymbolInfo by parsing a properly formatted nm output line.
30
31 Args:
32 line: line from nm
33
34 Returns:
35 An instance of SymbolInfo if the line represents a symbol, None otherwise.
36 """
37 # We are interested in two types of lines:
38 # This:
39 # 00210d59 00000002 t _ZN34BrowserPluginHostMsg_Attach_ParamsD2Ev
40 # offset size <symbol_type> symbol_name
41 # And that:
42 # 0070ee8c T WebRtcSpl_ComplexBitReverse
43 # In the second case we don't have a size, so use -1 as a sentinel
44 if not re.search(' (t|W|T) ', line):
45 return None
46 parts = line.split()
47 if len(parts) == 4:
48 return SymbolInfo(
49 offset=int(parts[0], 16), size=int(parts[1], 16), name=parts[3])
50 elif len(parts) == 3:
51 return SymbolInfo(
52 offset=int(parts[0], 16), size=-1, name=parts[2])
53 else:
54 return None
55
56
57 def SymbolInfosFromStream(nm_lines):
58 """Parses the output of nm, and get all the symbols from a binary.
59
60 Args:
61 nm_lines: An iterable of lines
62
63 Returns:
64 A list of SymbolInfo.
65 """
66 # TODO(lizeb): Consider switching to objdump to simplify parsing.
67 symbol_infos = []
68 for line in nm_lines:
69 symbol_info = FromNmLine(line)
70 if symbol_info is not None:
71 symbol_infos.append(symbol_info)
72 return symbol_infos
73
74
75 def SymbolInfosFromBinary(binary_filename):
76 """Runs nm to get all the symbols from a binary.
77
78 Args:
79 binary_filename: path to the binary.
80
81 Returns:
82 A list of SymbolInfo from the binary.
83 """
84 command = (_NM_BINARY, '-S', '-n', binary_filename)
85 p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE)
86 try:
87 result = SymbolInfosFromStream(p.stdout)
88 return result
89 finally:
90 p.wait()
91
92
93 def GroupSymbolInfosByOffset(symbol_infos):
94 """Create a dict {offset: [symbol_info1, ...], ...}.
95
96 As several symbols can be at the same offset, this is a 1-to-many
97 relationship.
98
99 Args:
100 symbol_infos: iterable of SymbolInfo instances
101
102 Returns:
103 a dict {offset: [symbol_info1, ...], ...}
104 """
105 offset_to_symbol_infos = collections.defaultdict(list)
106 for symbol_info in symbol_infos:
107 offset_to_symbol_infos[symbol_info.offset].append(symbol_info)
108 return dict(offset_to_symbol_infos)
109
110
111 def CreateNameToSymbolInfo(symbol_infos):
112 """Create a dict {name: symbol_info, ...}.
113
114 Args:
115 symbol_infos: iterable of SymbolInfo instances
116
117 Returns:
118 a dict {name: symbol_info, ...}
119 """
120 return {symbol_info.name: symbol_info for symbol_info in symbol_infos}
OLDNEW
« no previous file with comments | « tools/cygprofile/patch_orderfile_unittest.py ('k') | tools/cygprofile/symbol_extractor_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698