binarysize.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """A tool to inspect the binary size of a built binary file.
  2. This script prints out a tree of symbols and their corresponding sizes, using
  3. Linux's nm functionality.
  4. Usage:
  5. python binary_size.py -- \
  6. --target=/path/to/your/target/binary \
  7. [--nm_command=/path/to/your/custom/nm] \
  8. [--max_depth=10] [--min_size=1024] \
  9. [--color] \
  10. To assist visualization, pass in '--color' to make the symbols color coded to
  11. green, assuming that you have a xterm connection that supports color.
  12. """
  13. import argparse
  14. import subprocess
  15. import sys
  16. class Trie(object):
  17. """A simple class that represents a Trie."""
  18. def __init__(self, name):
  19. """Initializes a Trie object."""
  20. self.name = name
  21. self.size = 0
  22. self.dictionary = {}
  23. def GetSymbolTrie(target, nm_command, max_depth):
  24. """Gets a symbol trie with the passed in target.
  25. Args:
  26. target: the target binary to inspect.
  27. nm_command: the command to run nm.
  28. max_depth: the maximum depth to create the trie.
  29. """
  30. # Run nm to get a dump on the strings.
  31. proc = subprocess.Popen(
  32. [nm_command, '--radix=d', '--size-sort', '--print-size', target],
  33. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  34. nm_out, _ = proc.communicate()
  35. if proc.returncode != 0:
  36. print('NM command failed. Output is as follows:')
  37. print(nm_out)
  38. sys.exit(1)
  39. # Run c++filt to get proper symbols.
  40. proc = subprocess.Popen(['c++filt'],
  41. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  42. stderr=subprocess.STDOUT)
  43. out, _ = proc.communicate(input=nm_out)
  44. if proc.returncode != 0:
  45. print('c++filt failed. Output is as follows:')
  46. print(out)
  47. sys.exit(1)
  48. # Splits the output to size and function name.
  49. data = []
  50. for line in out.split('\n'):
  51. if line:
  52. content = line.split(' ')
  53. if len(content) < 4:
  54. # This is a line not representing symbol sizes. skip.
  55. continue
  56. data.append([int(content[1]), ' '.join(content[3:])])
  57. symbol_trie = Trie('')
  58. for size, name in data:
  59. curr = symbol_trie
  60. for c in name:
  61. if c not in curr.dictionary:
  62. curr.dictionary[c] = Trie(curr.name + c)
  63. curr = curr.dictionary[c]
  64. curr.size += size
  65. if len(curr.name) > max_depth:
  66. break
  67. symbol_trie.size = sum(t.size for t in symbol_trie.dictionary.values())
  68. return symbol_trie
  69. def MaybeAddColor(s, color):
  70. """Wrap the input string to the xterm green color, if color is set.
  71. """
  72. if color:
  73. return '\033[92m{0}\033[0m'.format(s)
  74. else:
  75. return s
  76. def ReadableSize(num):
  77. """Get a human-readable size."""
  78. for unit in ['B', 'KB', 'MB', 'GB']:
  79. if abs(num) <= 1024.0:
  80. return '%3.2f%s' % (num, unit)
  81. num /= 1024.0
  82. return '%.1f TB' % (num,)
  83. # Note(jiayq): I know, I know, this is a recursive function, but it is
  84. # convenient to write.
  85. def PrintTrie(trie, prefix, max_depth, min_size, color):
  86. """Prints the symbol trie in a readable manner.
  87. """
  88. if len(trie.name) == max_depth or not trie.dictionary.keys():
  89. # If we are reaching a leaf node or the maximum depth, we will print the
  90. # result.
  91. if trie.size > min_size:
  92. print('{0}{1} {2}'.format(
  93. prefix,
  94. MaybeAddColor(trie.name, color),
  95. ReadableSize(trie.size)))
  96. elif len(trie.dictionary.keys()) == 1:
  97. # There is only one child in this dictionary, so we will just delegate
  98. # to the downstream trie to print stuff.
  99. PrintTrie(
  100. trie.dictionary.values()[0], prefix, max_depth, min_size, color)
  101. elif trie.size > min_size:
  102. print('{0}{1} {2}'.format(
  103. prefix,
  104. MaybeAddColor(trie.name, color),
  105. ReadableSize(trie.size)))
  106. keys_with_sizes = [
  107. (k, trie.dictionary[k].size) for k in trie.dictionary.keys()]
  108. keys_with_sizes.sort(key=lambda x: x[1])
  109. for k, _ in keys_with_sizes[::-1]:
  110. PrintTrie(
  111. trie.dictionary[k], prefix + ' |', max_depth, min_size, color)
  112. def main(argv):
  113. if not sys.platform.startswith('linux'):
  114. raise RuntimeError('Currently this tool only supports Linux.')
  115. parser = argparse.ArgumentParser(
  116. description="Tool to inspect binary size.")
  117. parser.add_argument(
  118. '--max_depth', type=int, default=10,
  119. help='The maximum depth to print the symbol tree.')
  120. parser.add_argument(
  121. '--min_size', type=int, default=1024,
  122. help='The mininum symbol size to print.')
  123. parser.add_argument(
  124. '--nm_command', type=str, default='nm',
  125. help='The path to the nm command that the tool needs.')
  126. parser.add_argument(
  127. '--color', action='store_true',
  128. help='If set, use ascii color for output.')
  129. parser.add_argument(
  130. '--target', type=str,
  131. help='The binary target to inspect.')
  132. args = parser.parse_args(argv)
  133. if not args.target:
  134. raise RuntimeError('You must specify a target to inspect.')
  135. symbol_trie = GetSymbolTrie(
  136. args.target, args.nm_command, args.max_depth)
  137. PrintTrie(symbol_trie, '', args.max_depth, args.min_size, args.color)
  138. if __name__ == '__main__':
  139. main(sys.argv[1:])