Index: tools/linux/dump-static-initializers.py |
diff --git a/tools/linux/dump-static-initializers.py b/tools/linux/dump-static-initializers.py |
index 14fe46994a89a4aa3d017e174ccbce290a1484b9..7fe5d8696f1c4beca1739479e2ae64f1ee8466b9 100755 |
--- a/tools/linux/dump-static-initializers.py |
+++ b/tools/linux/dump-static-initializers.py |
@@ -96,14 +96,18 @@ def QualifyFilename(filename, symbol): |
# 0000000001919920 0000000000000008 b _ZN12_GLOBAL__N_119g_nine_box_prelightE |
nm_re = re.compile(r'(\S+) (\S+) t _GLOBAL__I_(.*)') |
def ParseNm(binary): |
- """Given a binary, yield static initializers as (start, size, file) pairs.""" |
+ """Given a binary, yield static initializers as (start, size, file) tuples.""" |
nm = subprocess.Popen(['nm', '-S', binary], stdout=subprocess.PIPE) |
+ files = [] |
for line in nm.stdout: |
match = nm_re.match(line) |
if match: |
addr, size, filename = match.groups() |
- yield int(addr, 16), int(size, 16), filename |
+ files.append((filename, int(addr, 16), int(size, 16))) |
+ |
+ for f in sorted(files): |
+ yield f |
# Regex matching objdump output for the symbols we're interested in. |
@@ -132,54 +136,76 @@ def ExtractSymbolReferences(binary, start, end): |
# Probably a relative jump within this function. |
continue |
refs.add(ref) |
- continue |
Tyler Breisacher (Chromium)
2012/01/26 22:36:33
I'm pretty sure this doesn't actually do anything.
|
for ref in sorted(refs): |
yield ref |
- |
def main(): |
- parser = optparse.OptionParser(usage='%prog filename') |
- parser.add_option('-i', '--instances', dest='calculate_instances', |
+ parser = optparse.OptionParser(usage='%prog [option] filename') |
+ parser.add_option('-f', '--files', dest='count_files', |
+ action='store_true', default=False, |
+ help='Print out the number of files containing static ' |
+ 'initializers') |
+ parser.add_option('-i', '--instances', dest='count_initializers', |
+ action='store_true', default=False, |
+ help='Print out the number of static initializers') |
+ parser.add_option('-d', '--diffable', dest='diffable', |
action='store_true', default=False, |
- help='Only print out the number of static initializers') |
+ help='Prints the filename on each line, for more easily ' |
+ 'diff-able output.') |
opts, args = parser.parse_args() |
if len(args) != 1: |
parser.error('missing filename argument') |
return 1 |
binary = args[0] |
- demangler = Demangler() |
- static_initializers_count = 0 |
- for addr, size, filename in ParseNm(binary): |
- if size == 2: |
- # gcc generates a two-byte 'repz retq' initializer when there is nothing |
- # to do. jyasskin tells me this is fixed in gcc 4.6. |
- # Two bytes is too small to do anything, so just ignore it. |
- continue |
+ if opts.count_files and opts.count_initializers: |
+ parser.error('-f and -i are mutually exclusive') |
+ return 1 |
+ |
+ if opts.diffable and (opts.count_initializers or opts.count_files): |
+ parser.error('-d cannot be used with -f or -i') |
+ return 1 |
- if (opts.calculate_instances): |
- static_initializers_count += 1 |
+ demangler = Demangler() |
+ file_count = 0 |
+ initializer_count = 0 |
+ for filename, addr, size in ParseNm(binary): |
+ if opts.count_files: |
+ file_count += 1 |
continue |
- ref_output = '' |
+ ref_output = [] |
qualified_filename = QualifyFilenameAsProto(filename) |
for ref in ExtractSymbolReferences(binary, addr, addr+size): |
+ if opts.count_initializers: |
+ initializer_count += 1 |
+ continue |
+ |
ref = demangler.Demangle(ref) |
if qualified_filename == filename: |
qualified_filename = QualifyFilename(filename, ref) |
if ref in NOTES: |
- ref_output = ref_output + ' %s [%s]\n' % (ref, NOTES[ref]) |
+ ref_output.append(' %s [%s]' % (ref, NOTES[ref])) |
else: |
- ref_output = ref_output + ' ' + ref + '\n' |
- print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename, |
- addr, size) |
- print ref_output |
+ ref_output.append(' ' + ref) |
- if opts.calculate_instances: |
- print static_initializers_count |
- return 0 |
+ if opts.count_initializers: |
+ continue |
+ |
+ if opts.diffable: |
+ print '\n'.join(qualified_filename + r for r in ref_output) |
+ else: |
+ print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename, |
+ addr, size) |
+ print '\n'.join(ref_output) + '\n' |
+ if opts.count_files: |
+ print file_count |
+ |
+ if opts.count_initializers: |
+ print initializer_count |
+ return 0 |
if '__main__' == __name__: |
sys.exit(main()) |