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