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

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: addressing review comments 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 (file, start, size) tuples."""
100 100
101 nm = subprocess.Popen(['nm', '-S', binary], stdout=subprocess.PIPE) 101 nm = subprocess.Popen(['nm', '-S', binary], stdout=subprocess.PIPE)
102 for line in nm.stdout: 102 for line in nm.stdout:
103 match = nm_re.match(line) 103 match = nm_re.match(line)
104 if match: 104 if match:
105 addr, size, filename = match.groups() 105 addr, size, filename = match.groups()
106 yield int(addr, 16), int(size, 16), filename 106 yield filename, int(addr, 16), int(size, 16)
107
108 107
109 # Regex matching objdump output for the symbols we're interested in. 108 # Regex matching objdump output for the symbols we're interested in.
110 # Example line: 109 # Example line:
111 # 12354ab: (disassembly, including <FunctionReference>) 110 # 12354ab: (disassembly, including <FunctionReference>)
112 disassembly_re = re.compile(r'^\s+[0-9a-f]+:.*<(\S+)>') 111 disassembly_re = re.compile(r'^\s+[0-9a-f]+:.*<(\S+)>')
113 def ExtractSymbolReferences(binary, start, end): 112 def ExtractSymbolReferences(binary, start, end):
114 """Given a span of addresses, yields symbol references from disassembly.""" 113 """Given a span of addresses, yields symbol references from disassembly."""
115 cmd = ['objdump', binary, '--disassemble', 114 cmd = ['objdump', binary, '--disassemble',
116 '--start-address=0x%x' % start, '--stop-address=0x%x' % end] 115 '--start-address=0x%x' % start, '--stop-address=0x%x' % end]
117 objdump = subprocess.Popen(cmd, stdout=subprocess.PIPE) 116 objdump = subprocess.Popen(cmd, stdout=subprocess.PIPE)
118 117
119 refs = set() 118 refs = set()
120 for line in objdump.stdout: 119 for line in objdump.stdout:
121 if '__static_initialization_and_destruction' in line: 120 if '__static_initialization_and_destruction' in line:
122 raise RuntimeError, ('code mentions ' 121 raise RuntimeError, ('code mentions '
123 '__static_initialization_and_destruction; ' 122 '__static_initialization_and_destruction; '
124 'did you accidentally run this on a Debug binary?') 123 'did you accidentally run this on a Debug binary?')
125 match = disassembly_re.search(line) 124 match = disassembly_re.search(line)
126 if match: 125 if match:
127 (ref,) = match.groups() 126 (ref,) = match.groups()
128 if ref.startswith('.LC') or ref.startswith('_DYNAMIC'): 127 if ref.startswith('.LC') or ref.startswith('_DYNAMIC'):
129 # Ignore these, they are uninformative. 128 # Ignore these, they are uninformative.
130 continue 129 continue
131 if ref.startswith('_GLOBAL__I_'): 130 if ref.startswith('_GLOBAL__I_'):
132 # Probably a relative jump within this function. 131 # Probably a relative jump within this function.
133 continue 132 continue
134 refs.add(ref) 133 refs.add(ref)
135 continue
136 134
137 for ref in sorted(refs): 135 return sorted(refs)
Evan Martin 2012/01/30 23:42:25 Can you fix the function-level comment to not say
Tyler Breisacher (Chromium) 2012/01/30 23:53:45 Done.
138 yield ref
Evan Martin 2012/01/30 23:30:57 I'm confused, is this code in the current checked-
Tyler Breisacher (Chromium) 2012/01/30 23:35:50 Yes. There are typically only a few refs per file,
139
140 136
141 def main(): 137 def main():
142 parser = optparse.OptionParser(usage='%prog filename') 138 parser = optparse.OptionParser(usage='%prog [option] filename')
143 parser.add_option('-i', '--instances', dest='calculate_instances', 139 parser.add_option('-f', '--files', dest='count_files',
144 action='store_true', default=False, 140 action='store_true', default=False,
145 help='Only print out the number of static initializers') 141 help='Print out the number of files containing static '
142 'initializers')
143 parser.add_option('-i', '--instances', dest='count_initializers',
144 action='store_true', default=False,
145 help='Print out the number of static initializers')
Evan Martin 2012/01/30 23:42:25 Why not just always print these things?
Tyler Breisacher (Chromium) 2012/01/30 23:53:45 I'm not opposed to always printing them, but I thi
146 parser.add_option('-d', '--diffable', dest='diffable',
147 action='store_true', default=False,
148 help='Prints the filename on each line, for more easily '
149 'diff-able output.')
146 opts, args = parser.parse_args() 150 opts, args = parser.parse_args()
147 if len(args) != 1: 151 if len(args) != 1:
148 parser.error('missing filename argument') 152 parser.error('missing filename argument')
149 return 1 153 return 1
150 binary = args[0] 154 binary = args[0]
151 155
156 if opts.count_files and opts.count_initializers:
157 parser.error('-f and -i are mutually exclusive')
158 return 1
159
160 if opts.diffable and (opts.count_initializers or opts.count_files):
161 parser.error('-d cannot be used with -f or -i')
162 return 1
163
152 demangler = Demangler() 164 demangler = Demangler()
153 static_initializers_count = 0 165 file_count = 0
154 for addr, size, filename in ParseNm(binary): 166 initializer_count = 0
167
168 files = ParseNm(binary)
169 if (opts.diffable):
170 files = sorted(files)
Tyler Breisacher (Chromium) 2012/01/30 23:28:16 The only thing that's weird about this, is we're s
171 for filename, addr, size in files:
155 if size == 2: 172 if size == 2:
156 # gcc generates a two-byte 'repz retq' initializer when there is nothing 173 # gcc generates a two-byte 'repz retq' initializer when there is nothing
157 # to do. jyasskin tells me this is fixed in gcc 4.6. 174 # to do. jyasskin tells me this is fixed in gcc 4.6.
158 # Two bytes is too small to do anything, so just ignore it.
159 continue 175 continue
160 176
161 if (opts.calculate_instances): 177 if opts.count_files:
162 static_initializers_count += 1 178 file_count += 1
163 continue 179 continue
164 180
165 ref_output = '' 181 ref_output = []
166 qualified_filename = QualifyFilenameAsProto(filename) 182 qualified_filename = QualifyFilenameAsProto(filename)
167 for ref in ExtractSymbolReferences(binary, addr, addr+size): 183 for ref in ExtractSymbolReferences(binary, addr, addr+size):
184 if opts.count_initializers:
185 initializer_count += 1
186 continue
187
168 ref = demangler.Demangle(ref) 188 ref = demangler.Demangle(ref)
169 if qualified_filename == filename: 189 if qualified_filename == filename:
170 qualified_filename = QualifyFilename(filename, ref) 190 qualified_filename = QualifyFilename(filename, ref)
171 if ref in NOTES: 191 if ref in NOTES:
172 ref_output = ref_output + ' %s [%s]\n' % (ref, NOTES[ref]) 192 ref_output.append(' %s [%s]' % (ref, NOTES[ref]))
173 else: 193 else:
174 ref_output = ref_output + ' ' + ref + '\n' 194 ref_output.append(' ' + ref)
175 print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename,
176 addr, size)
177 print ref_output
178 195
179 if opts.calculate_instances: 196 if opts.count_initializers:
180 print static_initializers_count 197 continue
198
199 if opts.diffable:
200 print '\n'.join(qualified_filename + r for r in ref_output)
201 else:
202 print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename,
203 addr, size)
204 print '\n'.join(ref_output) + '\n'
205
206 if opts.count_files:
207 print file_count
208
209 if opts.count_initializers:
210 print initializer_count
181 return 0 211 return 0
182 212
183
184 if '__main__' == __name__: 213 if '__main__' == __name__:
185 sys.exit(main()) 214 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