You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.9 KiB

35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
  1. #! /usr/bin/env python
  2. # objgraph
  3. #
  4. # Read "nm -o" input (on IRIX: "nm -Bo") of a set of libraries or modules
  5. # and print various interesting listings, such as:
  6. #
  7. # - which names are used but not defined in the set (and used where),
  8. # - which names are defined in the set (and where),
  9. # - which modules use which other modules,
  10. # - which modules are used by which other modules.
  11. #
  12. # Usage: objgraph [-cdu] [file] ...
  13. # -c: print callers per objectfile
  14. # -d: print callees per objectfile
  15. # -u: print usage of undefined symbols
  16. # If none of -cdu is specified, all are assumed.
  17. # Use "nm -o" to generate the input (on IRIX: "nm -Bo"),
  18. # e.g.: nm -o /lib/libc.a | objgraph
  19. import sys
  20. import os
  21. import getopt
  22. import re
  23. # Types of symbols.
  24. #
  25. definitions = 'TRGDSBAEC'
  26. externals = 'UV'
  27. ignore = 'Nntrgdsbavuc'
  28. # Regular expression to parse "nm -o" output.
  29. #
  30. matcher = re.compile('(.*):\t?........ (.) (.*)$')
  31. # Store "item" in "dict" under "key".
  32. # The dictionary maps keys to lists of items.
  33. # If there is no list for the key yet, it is created.
  34. #
  35. def store(dict, key, item):
  36. if dict.has_key(key):
  37. dict[key].append(item)
  38. else:
  39. dict[key] = [item]
  40. # Return a flattened version of a list of strings: the concatenation
  41. # of its elements with intervening spaces.
  42. #
  43. def flat(list):
  44. s = ''
  45. for item in list:
  46. s = s + ' ' + item
  47. return s[1:]
  48. # Global variables mapping defined/undefined names to files and back.
  49. #
  50. file2undef = {}
  51. def2file = {}
  52. file2def = {}
  53. undef2file = {}
  54. # Read one input file and merge the data into the tables.
  55. # Argument is an open file.
  56. #
  57. def readinput(fp):
  58. while 1:
  59. s = fp.readline()
  60. if not s:
  61. break
  62. # If you get any output from this line,
  63. # it is probably caused by an unexpected input line:
  64. if matcher.search(s) < 0: s; continue # Shouldn't happen
  65. (ra, rb), (r1a, r1b), (r2a, r2b), (r3a, r3b) = matcher.regs[:4]
  66. fn, name, type = s[r1a:r1b], s[r3a:r3b], s[r2a:r2b]
  67. if type in definitions:
  68. store(def2file, name, fn)
  69. store(file2def, fn, name)
  70. elif type in externals:
  71. store(file2undef, fn, name)
  72. store(undef2file, name, fn)
  73. elif not type in ignore:
  74. print fn + ':' + name + ': unknown type ' + type
  75. # Print all names that were undefined in some module and where they are
  76. # defined.
  77. #
  78. def printcallee():
  79. flist = file2undef.keys()
  80. flist.sort()
  81. for filename in flist:
  82. print filename + ':'
  83. elist = file2undef[filename]
  84. elist.sort()
  85. for ext in elist:
  86. if len(ext) >= 8:
  87. tabs = '\t'
  88. else:
  89. tabs = '\t\t'
  90. if not def2file.has_key(ext):
  91. print '\t' + ext + tabs + ' *undefined'
  92. else:
  93. print '\t' + ext + tabs + flat(def2file[ext])
  94. # Print for each module the names of the other modules that use it.
  95. #
  96. def printcaller():
  97. files = file2def.keys()
  98. files.sort()
  99. for filename in files:
  100. callers = []
  101. for label in file2def[filename]:
  102. if undef2file.has_key(label):
  103. callers = callers + undef2file[label]
  104. if callers:
  105. callers.sort()
  106. print filename + ':'
  107. lastfn = ''
  108. for fn in callers:
  109. if fn <> lastfn:
  110. print '\t' + fn
  111. lastfn = fn
  112. else:
  113. print filename + ': unused'
  114. # Print undefined names and where they are used.
  115. #
  116. def printundef():
  117. undefs = {}
  118. for filename in file2undef.keys():
  119. for ext in file2undef[filename]:
  120. if not def2file.has_key(ext):
  121. store(undefs, ext, filename)
  122. elist = undefs.keys()
  123. elist.sort()
  124. for ext in elist:
  125. print ext + ':'
  126. flist = undefs[ext]
  127. flist.sort()
  128. for filename in flist:
  129. print '\t' + filename
  130. # Print warning messages about names defined in more than one file.
  131. #
  132. def warndups():
  133. savestdout = sys.stdout
  134. sys.stdout = sys.stderr
  135. names = def2file.keys()
  136. names.sort()
  137. for name in names:
  138. if len(def2file[name]) > 1:
  139. print 'warning:', name, 'multiply defined:',
  140. print flat(def2file[name])
  141. sys.stdout = savestdout
  142. # Main program
  143. #
  144. def main():
  145. try:
  146. optlist, args = getopt.getopt(sys.argv[1:], 'cdu')
  147. except getopt.error:
  148. sys.stdout = sys.stderr
  149. print 'Usage:', os.path.basename(sys.argv[0]),
  150. print '[-cdu] [file] ...'
  151. print '-c: print callers per objectfile'
  152. print '-d: print callees per objectfile'
  153. print '-u: print usage of undefined symbols'
  154. print 'If none of -cdu is specified, all are assumed.'
  155. print 'Use "nm -o" to generate the input (on IRIX: "nm -Bo"),'
  156. print 'e.g.: nm -o /lib/libc.a | objgraph'
  157. return 1
  158. optu = optc = optd = 0
  159. for opt, void in optlist:
  160. if opt == '-u':
  161. optu = 1
  162. elif opt == '-c':
  163. optc = 1
  164. elif opt == '-d':
  165. optd = 1
  166. if optu == optc == optd == 0:
  167. optu = optc = optd = 1
  168. if not args:
  169. args = ['-']
  170. for filename in args:
  171. if filename == '-':
  172. readinput(sys.stdin)
  173. else:
  174. readinput(open(filename, 'r'))
  175. #
  176. warndups()
  177. #
  178. more = (optu + optc + optd > 1)
  179. if optd:
  180. if more:
  181. print '---------------All callees------------------'
  182. printcallee()
  183. if optu:
  184. if more:
  185. print '---------------Undefined callees------------'
  186. printundef()
  187. if optc:
  188. if more:
  189. print '---------------All Callers------------------'
  190. printcaller()
  191. return 0
  192. # Call the main program.
  193. # Use its return value as exit status.
  194. # Catch interrupts to avoid stack trace.
  195. #
  196. if __name__ == '__main__':
  197. try:
  198. sys.exit(main())
  199. except KeyboardInterrupt:
  200. sys.exit(1)