1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """Transform gprof(1) output into useful HTML.""" |
---|
4 | n/a | |
---|
5 | n/a | import html |
---|
6 | n/a | import os |
---|
7 | n/a | import re |
---|
8 | n/a | import sys |
---|
9 | n/a | import webbrowser |
---|
10 | n/a | |
---|
11 | n/a | header = """\ |
---|
12 | n/a | <html> |
---|
13 | n/a | <head> |
---|
14 | n/a | <title>gprof output (%s)</title> |
---|
15 | n/a | </head> |
---|
16 | n/a | <body> |
---|
17 | n/a | <pre> |
---|
18 | n/a | """ |
---|
19 | n/a | |
---|
20 | n/a | trailer = """\ |
---|
21 | n/a | </pre> |
---|
22 | n/a | </body> |
---|
23 | n/a | </html> |
---|
24 | n/a | """ |
---|
25 | n/a | |
---|
26 | n/a | def add_escapes(filename): |
---|
27 | n/a | with open(filename) as fp: |
---|
28 | n/a | for line in fp: |
---|
29 | n/a | yield html.escape(line) |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | def main(): |
---|
33 | n/a | filename = "gprof.out" |
---|
34 | n/a | if sys.argv[1:]: |
---|
35 | n/a | filename = sys.argv[1] |
---|
36 | n/a | outputfilename = filename + ".html" |
---|
37 | n/a | input = add_escapes(filename) |
---|
38 | n/a | output = open(outputfilename, "w") |
---|
39 | n/a | output.write(header % filename) |
---|
40 | n/a | for line in input: |
---|
41 | n/a | output.write(line) |
---|
42 | n/a | if line.startswith(" time"): |
---|
43 | n/a | break |
---|
44 | n/a | labels = {} |
---|
45 | n/a | for line in input: |
---|
46 | n/a | m = re.match(r"(.* )(\w+)\n", line) |
---|
47 | n/a | if not m: |
---|
48 | n/a | output.write(line) |
---|
49 | n/a | break |
---|
50 | n/a | stuff, fname = m.group(1, 2) |
---|
51 | n/a | labels[fname] = fname |
---|
52 | n/a | output.write('%s<a name="flat:%s" href="#call:%s">%s</a>\n' % |
---|
53 | n/a | (stuff, fname, fname, fname)) |
---|
54 | n/a | for line in input: |
---|
55 | n/a | output.write(line) |
---|
56 | n/a | if line.startswith("index % time"): |
---|
57 | n/a | break |
---|
58 | n/a | for line in input: |
---|
59 | n/a | m = re.match(r"(.* )(\w+)(( <cycle.*>)? \[\d+\])\n", line) |
---|
60 | n/a | if not m: |
---|
61 | n/a | output.write(line) |
---|
62 | n/a | if line.startswith("Index by function name"): |
---|
63 | n/a | break |
---|
64 | n/a | continue |
---|
65 | n/a | prefix, fname, suffix = m.group(1, 2, 3) |
---|
66 | n/a | if fname not in labels: |
---|
67 | n/a | output.write(line) |
---|
68 | n/a | continue |
---|
69 | n/a | if line.startswith("["): |
---|
70 | n/a | output.write('%s<a name="call:%s" href="#flat:%s">%s</a>%s\n' % |
---|
71 | n/a | (prefix, fname, fname, fname, suffix)) |
---|
72 | n/a | else: |
---|
73 | n/a | output.write('%s<a href="#call:%s">%s</a>%s\n' % |
---|
74 | n/a | (prefix, fname, fname, suffix)) |
---|
75 | n/a | for line in input: |
---|
76 | n/a | for part in re.findall(r"(\w+(?:\.c)?|\W+)", line): |
---|
77 | n/a | if part in labels: |
---|
78 | n/a | part = '<a href="#call:%s">%s</a>' % (part, part) |
---|
79 | n/a | output.write(part) |
---|
80 | n/a | output.write(trailer) |
---|
81 | n/a | output.close() |
---|
82 | n/a | webbrowser.open("file:" + os.path.abspath(outputfilename)) |
---|
83 | n/a | |
---|
84 | n/a | if __name__ == '__main__': |
---|
85 | n/a | main() |
---|