1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
tools / unused-symbols-report.py [blame]
#!/usr/bin/env python
# Copyright 2011 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Prints a report of symbols stripped by the linker due to being unused.
To use, build with these linker flags:
-Wl,--gc-sections
-Wl,--print-gc-sections
the first one is the default in Release; search build/common.gypi for it
and to see where to add the other.
Then build, saving the output into a file:
make chrome 2>&1 | tee buildlog
and run this script on it:
./tools/unused-symbols-report.py buildlog > report.html
"""
from __future__ import print_function
import cgi
import optparse
import os
import re
import subprocess
import sys
cppfilt_proc = None
def Demangle(sym):
"""Demangle a C++ symbol by passing it through c++filt."""
global cppfilt_proc
if cppfilt_proc is None:
cppfilt_proc = subprocess.Popen(['c++filt'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
print(sym, file=cppfilt_proc.stdin)
return cppfilt_proc.stdout.readline().strip()
def Unyuck(sym):
"""Attempt to prettify a C++ symbol by some basic heuristics."""
sym = sym.replace('std::basic_string<char, std::char_traits<char>, '
'std::allocator<char> >', 'std::string')
sym = sym.replace('std::basic_string<wchar_t, std::char_traits<wchar_t>, '
'std::allocator<wchar_t> >', 'std::wstring')
sym = sym.replace(
'std::basic_string<char16_t, std::char_traits<char16_t>, '
'std::allocator<char16_t> >', 'std::u16string')
sym = re.sub(r', std::allocator<\S+\s+>', '', sym)
return sym
def Parse(input, skip_paths=None, only_paths=None):
"""Parse the --print-gc-sections build output.
Args:
input: iterable over the lines of the build output
Yields:
(target name, path to .o file, demangled symbol)
"""
symbol_re = re.compile(r"'\.text\.(\S+)' in file '(\S+)'$")
path_re = re.compile(r"^out/[^/]+/[^/]+/([^/]+)/(.*)$")
for line in input:
match = symbol_re.search(line)
if not match:
continue
symbol, path = match.groups()
symbol = Unyuck(Demangle(symbol))
path = os.path.normpath(path)
if skip_paths and skip_paths in path:
continue
if only_paths and only_paths not in path:
continue
match = path_re.match(path)
if not match:
print("Skipping weird path", path, file=sys.stderr)
continue
target, path = match.groups()
yield target, path, symbol
# HTML header for our output page.
TEMPLATE_HEADER = """<!DOCTYPE html>
<head>
<style>
body {
font-family: sans-serif;
font-size: 0.8em;
}
h1, h2 {
font-weight: normal;
margin: 0.5em 0;
}
h2 {
margin-top: 1em;
}
tr:hover {
background: #eee;
}
.permalink {
padding-left: 1ex;
font-size: 80%;
text-decoration: none;
color: #ccc;
}
.symbol {
font-family: WebKitWorkAround, monospace;
margin-left: 4ex;
text-indent: -4ex;
padding: 0.5ex 1ex;
}
.file {
padding: 0.5ex 1ex;
padding-left: 2ex;
font-family: WebKitWorkAround, monospace;
font-size: 90%;
color: #777;
}
</style>
</head>
<body>
<h1>chrome symbols deleted at link time</h1>
"""
def Output(iter):
"""Print HTML given an iterable of (target, path, symbol) tuples."""
targets = {}
for target, path, symbol in iter:
entries = targets.setdefault(target, [])
entries.append((symbol, path))
print(TEMPLATE_HEADER)
print("<p>jump to target:")
print("<select onchange='document.location.hash = this.value'>")
for target in sorted(targets.keys()):
print("<option>%s</option>" % target)
print("</select></p>")
for target in sorted(targets.keys()):
print("<h2>%s" % target)
print("<a class=permalink href='#%s' name='%s'>#</a>" % (target, target))
print("</h2>")
print("<table width=100% cellspacing=0>")
for symbol, path in sorted(targets[target]):
htmlsymbol = cgi.escape(symbol).replace('::', '::<wbr>')
print("<tr><td><div class=symbol>%s</div></td>" % htmlsymbol)
print("<td valign=top><div class=file>%s</div></td></tr>" % path)
print("</table>")
def main():
parser = optparse.OptionParser(usage='%prog [options] buildoutput\n\n' +
__doc__)
parser.add_option("--skip-paths", metavar="STR", default="third_party",
help="skip paths matching STR [default=%default]")
parser.add_option("--only-paths", metavar="STR",
help="only include paths matching STR [default=%default]")
opts, args = parser.parse_args()
if len(args) < 1:
parser.print_help()
sys.exit(1)
iter = Parse(open(args[0]),
skip_paths=opts.skip_paths,
only_paths=opts.only_paths)
Output(iter)
if __name__ == '__main__':
main()