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

Side by Side Diff: tools/linux/dump-static-initializers.py

Issue 9169074: Add '-d' flag to dump-static-initializers.py (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleaning up a bit Created 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Dump functions called by static intializers in a Linux Release binary. 6 """Dump functions called by static intializers in a Linux Release binary.
7 7
8 Usage example: 8 Usage example:
9 tools/linux/dump-static-intializers.py out/Release/chrome 9 tools/linux/dump-static-intializers.py out/Release/chrome
10 10
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 if candidate != filename: # More than one candidate; return bare filename. 89 if candidate != filename: # More than one candidate; return bare filename.
90 return filename 90 return filename
91 candidate = line.strip() 91 candidate = line.strip()
92 return candidate 92 return candidate
93 93
94 # Regex matching nm output for the symbols we're interested in. 94 # Regex matching nm output for the symbols we're interested in.
95 # Example line: 95 # Example line:
96 # 0000000001919920 0000000000000008 b _ZN12_GLOBAL__N_119g_nine_box_prelightE 96 # 0000000001919920 0000000000000008 b _ZN12_GLOBAL__N_119g_nine_box_prelightE
97 nm_re = re.compile(r'(\S+) (\S+) t _GLOBAL__I_(.*)') 97 nm_re = re.compile(r'(\S+) (\S+) t _GLOBAL__I_(.*)')
98 def ParseNm(binary): 98 def ParseNm(binary):
99 """Given a binary, yield static initializers as (start, size, file) pairs.""" 99 """Given a binary, yield static initializers as (start, size, file) tuples."""
100 100
101 nm = subprocess.Popen(['nm', '-S', binary], stdout=subprocess.PIPE) 101 nm = subprocess.Popen(['nm', '-S', binary], stdout=subprocess.PIPE)
102 files = []
102 for line in nm.stdout: 103 for line in nm.stdout:
103 match = nm_re.match(line) 104 match = nm_re.match(line)
104 if match: 105 if match:
105 addr, size, filename = match.groups() 106 addr, size, filename = match.groups()
106 yield int(addr, 16), int(size, 16), filename 107 files.append((filename, int(addr, 16), int(size, 16)))
108
109 for f in sorted(files):
110 yield f
107 111
108 112
109 # Regex matching objdump output for the symbols we're interested in. 113 # Regex matching objdump output for the symbols we're interested in.
110 # Example line: 114 # Example line:
111 # 12354ab: (disassembly, including <FunctionReference>) 115 # 12354ab: (disassembly, including <FunctionReference>)
112 disassembly_re = re.compile(r'^\s+[0-9a-f]+:.*<(\S+)>') 116 disassembly_re = re.compile(r'^\s+[0-9a-f]+:.*<(\S+)>')
113 def ExtractSymbolReferences(binary, start, end): 117 def ExtractSymbolReferences(binary, start, end):
114 """Given a span of addresses, yields symbol references from disassembly.""" 118 """Given a span of addresses, yields symbol references from disassembly."""
115 cmd = ['objdump', binary, '--disassemble', 119 cmd = ['objdump', binary, '--disassemble',
116 '--start-address=0x%x' % start, '--stop-address=0x%x' % end] 120 '--start-address=0x%x' % start, '--stop-address=0x%x' % end]
117 objdump = subprocess.Popen(cmd, stdout=subprocess.PIPE) 121 objdump = subprocess.Popen(cmd, stdout=subprocess.PIPE)
118 122
119 refs = set() 123 refs = set()
120 for line in objdump.stdout: 124 for line in objdump.stdout:
121 if '__static_initialization_and_destruction' in line: 125 if '__static_initialization_and_destruction' in line:
122 raise RuntimeError, ('code mentions ' 126 raise RuntimeError, ('code mentions '
123 '__static_initialization_and_destruction; ' 127 '__static_initialization_and_destruction; '
124 'did you accidentally run this on a Debug binary?') 128 'did you accidentally run this on a Debug binary?')
125 match = disassembly_re.search(line) 129 match = disassembly_re.search(line)
126 if match: 130 if match:
127 (ref,) = match.groups() 131 (ref,) = match.groups()
128 if ref.startswith('.LC') or ref.startswith('_DYNAMIC'): 132 if ref.startswith('.LC') or ref.startswith('_DYNAMIC'):
129 # Ignore these, they are uninformative. 133 # Ignore these, they are uninformative.
130 continue 134 continue
131 if ref.startswith('_GLOBAL__I_'): 135 if ref.startswith('_GLOBAL__I_'):
132 # Probably a relative jump within this function. 136 # Probably a relative jump within this function.
133 continue 137 continue
134 refs.add(ref) 138 refs.add(ref)
135 continue
Tyler Breisacher (Chromium) 2012/01/26 22:36:33 I'm pretty sure this doesn't actually do anything.
136 139
137 for ref in sorted(refs): 140 for ref in sorted(refs):
138 yield ref 141 yield ref
139 142
140
141 def main(): 143 def main():
142 parser = optparse.OptionParser(usage='%prog filename') 144 parser = optparse.OptionParser(usage='%prog [option] filename')
143 parser.add_option('-i', '--instances', dest='calculate_instances', 145 parser.add_option('-f', '--files', dest='count_files',
144 action='store_true', default=False, 146 action='store_true', default=False,
145 help='Only print out the number of static initializers') 147 help='Print out the number of files containing static '
148 'initializers')
149 parser.add_option('-i', '--instances', dest='count_initializers',
150 action='store_true', default=False,
151 help='Print out the number of static initializers')
152 parser.add_option('-d', '--diffable', dest='diffable',
153 action='store_true', default=False,
154 help='Prints the filename on each line, for more easily '
155 'diff-able output.')
146 opts, args = parser.parse_args() 156 opts, args = parser.parse_args()
147 if len(args) != 1: 157 if len(args) != 1:
148 parser.error('missing filename argument') 158 parser.error('missing filename argument')
149 return 1 159 return 1
150 binary = args[0] 160 binary = args[0]
151 161
162 if opts.count_files and opts.count_initializers:
163 parser.error('-f and -i are mutually exclusive')
164 return 1
165
166 if opts.diffable and (opts.count_initializers or opts.count_files):
167 parser.error('-d cannot be used with -f or -i')
168 return 1
169
152 demangler = Demangler() 170 demangler = Demangler()
153 static_initializers_count = 0 171 file_count = 0
154 for addr, size, filename in ParseNm(binary): 172 initializer_count = 0
155 if size == 2: 173 for filename, addr, size in ParseNm(binary):
156 # gcc generates a two-byte 'repz retq' initializer when there is nothing 174 if opts.count_files:
157 # to do. jyasskin tells me this is fixed in gcc 4.6. 175 file_count += 1
158 # Two bytes is too small to do anything, so just ignore it.
159 continue 176 continue
160 177
161 if (opts.calculate_instances): 178 ref_output = []
162 static_initializers_count += 1
163 continue
164
165 ref_output = ''
166 qualified_filename = QualifyFilenameAsProto(filename) 179 qualified_filename = QualifyFilenameAsProto(filename)
167 for ref in ExtractSymbolReferences(binary, addr, addr+size): 180 for ref in ExtractSymbolReferences(binary, addr, addr+size):
181 if opts.count_initializers:
182 initializer_count += 1
183 continue
184
168 ref = demangler.Demangle(ref) 185 ref = demangler.Demangle(ref)
169 if qualified_filename == filename: 186 if qualified_filename == filename:
170 qualified_filename = QualifyFilename(filename, ref) 187 qualified_filename = QualifyFilename(filename, ref)
171 if ref in NOTES: 188 if ref in NOTES:
172 ref_output = ref_output + ' %s [%s]\n' % (ref, NOTES[ref]) 189 ref_output.append(' %s [%s]' % (ref, NOTES[ref]))
173 else: 190 else:
174 ref_output = ref_output + ' ' + ref + '\n' 191 ref_output.append(' ' + ref)
175 print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename,
176 addr, size)
177 print ref_output
178 192
179 if opts.calculate_instances: 193 if opts.count_initializers:
180 print static_initializers_count 194 continue
195
196 if opts.diffable:
197 print '\n'.join(qualified_filename + r for r in ref_output)
198 else:
199 print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename,
200 addr, size)
201 print '\n'.join(ref_output) + '\n'
202
203 if opts.count_files:
204 print file_count
205
206 if opts.count_initializers:
207 print initializer_count
181 return 0 208 return 0
182 209
183
184 if '__main__' == __name__: 210 if '__main__' == __name__:
185 sys.exit(main()) 211 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698