- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathgprof2html.py
executable file
·79 lines (72 loc) · 2.12 KB
/
gprof2html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /usr/bin/env python
"""Transform gprof(1) output into useful HTML."""
importre, os, sys, cgi, webbrowser
header="""\
<html>
<head>
<title>gprof output (%s)</title>
</head>
<body>
<pre>
"""
trailer="""\
</pre>
</body>
</html>
"""
defadd_escapes(input):
forlineininput:
yieldcgi.escape(line)
defmain():
filename="gprof.out"
ifsys.argv[1:]:
filename=sys.argv[1]
outputfilename=filename+".html"
input=add_escapes(file(filename))
output=file(outputfilename, "w")
output.write(header%filename)
forlineininput:
output.write(line)
ifline.startswith(" time"):
break
labels= {}
forlineininput:
m=re.match(r"(.* )(\w+)\n", line)
ifnotm:
output.write(line)
break
stuff, fname=m.group(1, 2)
labels[fname] =fname
output.write('%s<a name="flat:%s" href="#call:%s">%s</a>\n'%
(stuff, fname, fname, fname))
forlineininput:
output.write(line)
ifline.startswith("index % time"):
break
forlineininput:
m=re.match(r"(.* )(\w+)(( <cycle.*>)? \[\d+\])\n", line)
ifnotm:
output.write(line)
ifline.startswith("Index by function name"):
break
continue
prefix, fname, suffix=m.group(1, 2, 3)
iffnamenotinlabels:
output.write(line)
continue
ifline.startswith("["):
output.write('%s<a name="call:%s" href="#flat:%s">%s</a>%s\n'%
(prefix, fname, fname, fname, suffix))
else:
output.write('%s<a href="#call:%s">%s</a>%s\n'%
(prefix, fname, fname, suffix))
forlineininput:
forpartinre.findall(r"(\w+(?:\.c)?|\W+)", line):
ifpartinlabels:
part='<a href="#call:%s">%s</a>'% (part, part)
output.write(part)
output.write(trailer)
output.close()
webbrowser.open("file:"+os.path.abspath(outputfilename))
if__name__=='__main__':
main()