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