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

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

Issue 886563002: Switch from nm to objdump for the cygprofile tools. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. 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',
pasko 2015/01/29 12:24:42 why not sys.path.append()? let's also not make an
Benoit L 2015/01/29 13:10:30 Because python's standard library also has a modul
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 _OBJDUMP_BINARY = symbol.ToolPath('objdump')
23
24
25 SymbolInfo = collections.namedtuple('SymbolInfo', ('name', 'offset', 'size',
26 'section'))
27
28
29 def _FromObjdumpLine(line):
30 """Create a SymbolInfo by parsing a properly formatted objdump output line.
31
32 Args:
33 line: line from objdump
34
35 Returns:
36 An instance of SymbolInfo if the line represents a symbol, None otherwise.
37 """
38 # All of the symbol lines we care about are in the form
39 # 0000000000 g F .text.foo 000000000 [.hidden] foo
40 # where g (global) might also be l (local) or w (weak).
41 parts = line.split()
42 if len(parts) < 6 or parts[2] != 'F':
43 return None
44 offset = int(parts[0], 16)
45 section = parts[3]
46 size = int(parts[4], 16)
47 name = parts[-1].rstrip('\n')
48 return SymbolInfo(name=name, offset=offset, section=section, size=size)
49
50
51 def _SymbolInfosFromStream(objdump_lines):
52 """Parses the output of objdump, and get all the symbols from a binary.
53
54 Args:
55 objdump_lines: An iterable of lines
56
57 Returns:
58 A list of SymbolInfo.
59 """
60 symbol_infos = []
61 for line in objdump_lines:
62 symbol_info = _FromObjdumpLine(line)
63 if symbol_info is not None:
64 symbol_infos.append(symbol_info)
65 return symbol_infos
66
67
68 def SymbolInfosFromBinary(binary_filename):
69 """Runs objdump to get all the symbols from a binary.
70
71 Args:
72 binary_filename: path to the binary.
73
74 Returns:
75 A list of SymbolInfo from the binary.
76 """
77 command = (_OBJDUMP_BINARY, '-t', '-w', binary_filename)
78 p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE)
79 try:
80 result = _SymbolInfosFromStream(p.stdout)
81 return result
82 finally:
83 p.wait()
84
85
86 def GroupSymbolInfosByOffset(symbol_infos):
87 """Create a dict {offset: [symbol_info1, ...], ...}.
88
89 As several symbols can be at the same offset, this is a 1-to-many
90 relationship.
91
92 Args:
93 symbol_infos: iterable of SymbolInfo instances
94
95 Returns:
96 a dict {offset: [symbol_info1, ...], ...}
97 """
98 offset_to_symbol_infos = collections.defaultdict(list)
99 for symbol_info in symbol_infos:
100 offset_to_symbol_infos[symbol_info.offset].append(symbol_info)
101 return dict(offset_to_symbol_infos)
102
103
104 def CreateNameToSymbolInfo(symbol_infos):
pasko 2015/01/29 12:24:42 this function is only used in a test, is it going
Benoit L 2015/01/29 13:10:30 Yes, it is going to be used. But I would prefer to
pasko 2015/01/29 13:25:30 since it confuses reviewers, that's usually worth
105 """Create a dict {name: symbol_info, ...}.
106
107 Args:
108 symbol_infos: iterable of SymbolInfo instances
109
110 Returns:
111 a dict {name: symbol_info, ...}
112 """
113 return {symbol_info.name: symbol_info for symbol_info in symbol_infos}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698