Debian Codegraph @

ostra/ostra-cg

raw
1 #!/usr/bin/python3
2 # ostra-cg - generate callgraphs from encoded trace
3 #
4 # Arnaldo Carvalho de Melo <[email protected]>
5 # <[email protected]>
6 #
7 # Copyright (C) 2005, 2006, 2007 Arnaldo Carvalho de Melo
8 #
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms of version 2 of the GNU General Public License as
11 # published by the Free Software Foundation.
12
13 import sys, datetime, os, ostra
14
15 class_def = None
16
17 ident = 0
18
19 verbose = False
20 valid_html = False
21 print_exits = True
22 print_exit_details = False
23 print_function_times = True
24 gen_html = True
25 html_file_seq = 0
26 nr_lines_per_page = 256
27 output_file = None
28 callgraph = None
29 print_nr_exit_points = False
30 first_table_row = True
31 plot_min_samples = 4
32 plot = False
33 tab_space = 10
34 my_object = None
35
36 # @import url(file:///home/acme/git/ostra/ostra.css);
37 html_style_import='''
38 <style type="text/css">
39 @import url(http://vger.kernel.org/~acme/ostra.css);
40 </style>
41 '''
42
43 def emit_html_page_sequence_links(page):
44 global output_file
45
46 output_file.write("<div class=\"page_links\">")
47 if page != 1:
48 if page == 2:
49 prev = "index"
50 else:
51 prev = str(page - 2)
52 output_file.write("<a href=\"index.html\">Index</a> | ")
53 output_file.write("<a href=\"%s.html\">Previous</a> | " % prev)
54 output_file.write("<a href=\"%d.html\">Next</a> | " % page)
55 output_file.write("<a href=\"changes.html\">Where fields changed</a> | ")
56 output_file.write("<a href=\"methods/index.html\">Methods statistics</a> | ")
57 output_file.write("<a href=\"last.html\">Last</a>\n")
58 output_file.write("</div>")
59
60 def close_callgraph_file():
61 if gen_html:
62 output_file.write("</td></tr></table>\n")
63 emit_html_page_sequence_links(html_file_seq)
64 if valid_html:
65 output_file.write('''
66 <p>
67 <a href="http://validator.w3.org/check?uri=referer"><img border="0"
68 src="http://www.w3.org/Icons/valid-html401"
69 alt="Valid HTML 4.01!" height="31" width="88"></a>
70 </p>
71 ''')
72 output_file.write("</body>\n</html>\n")
73
74 output_file.close()
75
76 def new_callgraph_file(traced_class):
77 global html_file_seq, output_file, first_table_row
78
79 if not gen_html:
80 if output_file == None:
81 output_file = file("%s.txt" % callgraph, "w")
82 return
83
84 first_table_row = True
85
86 if html_file_seq == 0:
87 os.mkdir(callgraph)
88 if output_file != None:
89 output_file.close()
90 filename = "index"
91 help = '''
92 <h3>Tracing struct %s methods (functions with a struct %s * argument)</h3>
93 <h3>Click on the timestamps to see the object state</h3>
94 <h3>Click on the functions to go to its definition in LXR (http://lxr.linux.no/)</h3>
95 <h3 style=\"color:red;\">Red timestamps means the state changed</h3>
96 ''' % (traced_class, traced_class)
97 else:
98 close_callgraph_file()
99 filename = str(html_file_seq)
100 help = " "
101
102 output_file = file("%s/%s.html" % (callgraph, filename), "w")
103 output_file.write('''
104 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
105 "http://www.w3.org/TR/html4/loose.dtd">
106 <html>
107 <head>
108 <title>OSTRA Callgraph: %s, file %d</title>
109 %s
110 </head>
111 <body>
112 ''' % (callgraph, html_file_seq, html_style_import))
113
114 html_file_seq += 1
115 emit_html_page_sequence_links(html_file_seq)
116
117 output_file.write("\n%s\n<table class=\"listing\" cellspacing=\"0\" border=\"0\">\n" % help)
118
119 def trim_tstamp(tstamp):
120 return str(tstamp).strip().lstrip('0').lstrip(':').lstrip('0').lstrip(':').lstrip('0').lstrip('.').lstrip('0')
121
122 def object_state():
123 output = "<table class=\"state\" cellspacing=\"0\">"
124 state_changed = False
125 for field in class_def.fields.values():
126 if not field.cg:
127 continue
128 value_changed_or_not_zero = False
129 value = field.value
130 if field.changed():
131 state_changed = True
132 last_value = field.last_value
133 if field.table and last_value and field.table.has_key(int(last_value)):
134 last_value = field.table[int(last_value)]
135 transition = "%s -> " % last_value
136 color = " class=\"odd\""
137 value_changed_or_not_zero = True
138 else:
139 field_changed = False
140 transition = ""
141 color = ""
142 if value != "0" and value != None:
143 value_changed_or_not_zero = True
144
145 if value_changed_or_not_zero:
146 if field.table and value and field.table.has_key(int(value)):
147 value = field.table[int(value)]
148 output = output.strip() + "<tr%s><td>%s</td><td class=\"right\">%s%s</td></tr>" % \
149 (color, field, transition, value)
150 output += "</table>"
151 return (output, state_changed)
152
153 total_lines = 0
154
155 def tstamp_str():
156 global total_lines, first_table_row
157
158 total_lines += 1
159
160 if gen_html:
161 state, changed = object_state()
162 if changed:
163 anchor = "%d.%d" % (class_def.tstamp.seconds, class_def.tstamp.microseconds)
164 anchor_color = " class=\"red\""
165 else:
166 anchor = ""
167 anchor_color = ""
168 if total_lines % 2 == 1:
169 row_color = "odd"
170 else:
171 row_color = "evn"
172 if first_table_row:
173 close_last_tr = ""
174 first_table_row = False
175 else:
176 close_last_tr = "</td></tr>\n"
177
178 return "%s<tr class=\"%s\"><td class=\"state\"><a name=\"%s\"%s>%04d.%06d<span>%s</span></a></td>" % \
179 (close_last_tr, row_color, anchor, anchor_color,
180 class_def.tstamp.seconds, class_def.tstamp.microseconds, state)
181 else:
182 return "%06d.%06d" % (class_def.tstamp.seconds, class_def.tstamp.microseconds)
183
184 def indent_str(indent, text):
185 if gen_html:
186 method = class_def.current_method()
187 time_so_far = method.total_time.seconds * 10000 + method.total_time.microseconds
188 tooltip = "%s: calls=%d, total time=%dus" % (method.name, method.calls, time_so_far)
189 if class_def.fields["action"].value[0] == 'o':
190 if class_def.fields.has_key("exit_point"):
191 tooltip += ", exit point=%d" % (int(class_def.fields["exit_point"].value) + 1)
192 else:
193 text = "<a class=\"lxr\" href=\"http://lxr.linux.no/ident?i=%s\">%s</a>" % (method.name, text)
194
195 return "<td title=\"%s\">%s%s" % (tooltip, "&nbsp;" * tab_space * indent, text)
196 else:
197 return "%s%s" % ("\t" * ident, text)
198
199 def function_time_str(time):
200 if gen_html:
201 if class_def.current_method().print_return_value:
202 ret_value = "%s" % class_def.fields["return_value"].value
203 else:
204 ret_value = "0"
205 if ret_value == "0":
206 ret_value = ""
207 else:
208 ret_value=" title=\"returned %s\"" % ret_value
209 return "</td><td%s class=\"time\">%sus" % (ret_value, time)
210 else:
211 return " %sus\n" % time
212
213 previous_was_entry = False
214 nr_lines = 0
215
216 def process_record():
217 global ident, previous_was_entry, nr_lines
218
219 if gen_html:
220 nr_lines += 1
221 if nr_lines > nr_lines_per_page:
222 if ident == 0 or nr_lines > nr_lines_per_page * 5:
223 new_callgraph_file(traced_class)
224 nr_lines = 0
225
226 method = class_def.current_method()
227
228 if class_def.fields["action"].value[0] == 'i':
229 output = "%s()" % method.name
230
231 if print_exits and previous_was_entry:
232 if gen_html:
233 last_open = " {</td><td>&nbsp;"
234 else:
235 last_open = " {\n"
236 else:
237 last_open = ""
238 output_file.write("%s%s %s" % (last_open, tstamp_str(), indent_str(ident, output.strip())))
239 if not print_exits:
240 output_file.write("\n")
241
242 ident += 1
243
244 method.calls += 1
245 method.last_tstamp = class_def.tstamp
246 previous_was_entry = True
247 else:
248 if not method.last_tstamp:
249 method.last_tstamp = class_def.tstamp
250 tstamp_delta = class_def.tstamp - method.last_tstamp
251 if tstamp_delta < datetime.timedelta():
252 tstamp_delta = datetime.timedelta()
253 method.total_time += tstamp_delta
254
255
256 if ident > 0:
257 ident -= 1
258 if print_exits:
259 if print_exit_details:
260 exit_point = int(class_def.fields["exit_point"].value) + 1
261 if class_def.last_method.name != method.name:
262 output_file.write("%s %s" % (tstamp_str(), indent_str(ident, "}")))
263 if print_exit_details:
264 output_file.write(" EXIT #%d (%s)" % (exit_point, method.name))
265 else:
266 if print_exit_details:
267 output_file.write("EXIT #%d" % exit_point)
268
269 function_time = trim_tstamp(tstamp_delta)
270 if len(function_time) == 0:
271 function_time = "0"
272 if print_exits:
273 if print_function_times:
274 output_file.write(function_time_str(function_time))
275 else:
276 output_file.write("\n")
277
278 if print_nr_exit_points:
279 if method.exits.has_key(exit_point):
280 method.exits[exit_point] += 1
281 else:
282 method.exits[exit_point] = 1
283
284 previous_was_entry = False
285
286 return html_file_seq - 1
287
288 def print_where_fields_changed():
289 f = file("%s/changes.html" % callgraph, "w")
290 f.write('''
291 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
292 "http://www.w3.org/TR/html4/loose.dtd">
293 <html>
294 <head>
295 <title>OSTRA Callgraph: %s, Where the Fields Changed</title>
296 %s
297 </head>
298 <body>
299 <h3>Click on the values to go to where it was changed</h3>
300 <h3>Click on the field names to see a plotting of its value over time</h3>
301 ''' % (callgraph, html_style_import))
302 output_file.write("<div class=\"page_links\">")
303 f.write("<a href=\"index.html\">Index</a>\n")
304 f.write("<a href=\"%d.html\">Last</a>\n" % (html_file_seq - 1))
305 f.write("<table border=\"1\">")
306
307 max_samples = 50
308
309 for key in class_def.fields.keys():
310 fields = class_def.fields[key]
311 changes = fields.changes
312
313 changes_str=""
314 link_pre=""
315 link_pos=""
316 if len(changes) == 0:
317 changes_str="Unchanged</td></tr>\n"
318 elif plot and len(changes) >= plot_min_samples and fields.plot_fmt != "dev_null":
319 link_pre="<a href=\"%s.png\">" % key
320 link_pos="</a>"
321
322 f.write("<tr><td valign=\"top\">%s%s%s</td><td>%s" % (link_pre, key, link_pos, changes_str))
323 if len(changes) == 0:
324 continue
325
326 f.write("<table border=\"0\">\n")
327 nr_samples = 0
328 for change in changes:
329 nr_samples += 1
330 if nr_samples <= max_samples:
331 if change.seq == 0:
332 filename="index"
333 else:
334 filename = str(change.seq)
335 f.write("<tr><td><a href=%s.html#%d.%d>%s</td></tr>" % \
336 (filename, change.tstamp.seconds, change.tstamp.microseconds, change.value))
337
338 if nr_samples > max_samples:
339 f.write("<tr><td>Only %d samples out of %d were printed</td></tr>" % (max_samples, nr_samples))
340
341 f.write("</table>\n</td></tr>\n")
342
343 f.write("</table>")
344 output_file.write("</div>")
345 f.write("</body>\n</html>\n")
346 f.close()
347 os.symlink("changes.html", "%s/%d.html" % (callgraph, html_file_seq))
348 os.symlink("%d.html" % (html_file_seq - 1), "%s/last.html" % callgraph)
349
350
351 def method_stats(class_def, callgraph):
352 os.mkdir("%s/methods" % callgraph)
353 f = file("%s/methods/index.html" % callgraph, "w")
354 f.write('''
355 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
356 "http://www.w3.org/TR/html4/loose.dtd">
357 <html>
358 <head>
359 <title>OSTRA Callgraph: %s, Methods Statistics</title>
360 %s
361 </head>
362 <body>
363 <h3>Click on the methods names to see a plotting of the times for each call</h3>
364 ''' % (callgraph, html_style_import))
365
366 if plot:
367 class_def.plot_methods(callgraph)
368 f.write("<table border=\"1\">")
369 for method in class_def.methods.values():
370 changes_str=""
371 link_pre=""
372 link_pos=""
373 if len(method.times) < 4:
374 changes_str="Less than 4 calls</td></tr>\n"
375 else:
376 if plot:
377 link_pre="<a href=\"%s.png\">" % method.name
378 link_pos="</a>"
379 changes_str="%d calls</td></tr>\n" % len(method.times)
380
381 f.write("<tr><td valign=\"top\">%s%s%s</td><td>%s" % \
382 (link_pre, method.name, link_pos, changes_str))
383 f.write("</table>")
384 f.write("</body>\n</html>\n")
385 f.close()
386
387 if __name__ == '__main__':
388 if len(sys.argv) not in [ 3, 4 ]:
389 print("usage: ostra-cg <traced_class> <encoded_trace> [object]")
390 sys.exit(1)
391
392 gen_html = True
393 traced_class = sys.argv[1]
394 callgraph = "%s.callgraph" % traced_class
395 encoded_trace = sys.argv[2]
396 if len(sys.argv) == 4:
397 my_object = sys.argv[3]
398 if my_object == "none":
399 my_object = None
400 plot = True
401
402 class_def = ostra.class_definition(class_def_file = "%s.fields" % traced_class,
403 class_methods_file = "%s.functions" % traced_class)
404 new_callgraph_file(traced_class)
405 class_def.parse_file(encoded_trace, verbose = verbose,
406 process_record = process_record,
407 my_object = my_object)
408 if gen_html:
409 print_where_fields_changed()
410 close_callgraph_file()
411 method_stats(class_def, callgraph)
412 if plot:
413 ostra.plot(class_def, callgraph)