2013-04-20 18:07:43 -03:00
|
|
|
#! /usr/bin/env python3
|
2002-08-09 17:07:34 -03:00
|
|
|
|
|
|
|
"""Transform gprof(1) output into useful HTML."""
|
|
|
|
|
2014-12-01 04:50:33 -04:00
|
|
|
import html
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import webbrowser
|
2002-08-09 17:07:34 -03:00
|
|
|
|
|
|
|
header = """\
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>gprof output (%s)</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<pre>
|
|
|
|
"""
|
|
|
|
|
|
|
|
trailer = """\
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2012-04-11 16:16:38 -03:00
|
|
|
def add_escapes(filename):
|
|
|
|
with open(filename) as fp:
|
|
|
|
for line in fp:
|
2014-12-01 04:50:33 -04:00
|
|
|
yield html.escape(line)
|
2012-04-11 16:16:38 -03:00
|
|
|
|
2019-03-30 03:32:18 -03:00
|
|
|
def gprof2html(input, output, filename):
|
2002-08-09 17:07:34 -03:00
|
|
|
output.write(header % filename)
|
|
|
|
for line in input:
|
|
|
|
output.write(line)
|
|
|
|
if line.startswith(" time"):
|
|
|
|
break
|
|
|
|
labels = {}
|
|
|
|
for line in input:
|
|
|
|
m = re.match(r"(.* )(\w+)\n", line)
|
|
|
|
if not m:
|
|
|
|
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))
|
|
|
|
for line in input:
|
|
|
|
output.write(line)
|
|
|
|
if line.startswith("index % time"):
|
|
|
|
break
|
|
|
|
for line in input:
|
|
|
|
m = re.match(r"(.* )(\w+)(( <cycle.*>)? \[\d+\])\n", line)
|
|
|
|
if not m:
|
|
|
|
output.write(line)
|
|
|
|
if line.startswith("Index by function name"):
|
|
|
|
break
|
|
|
|
continue
|
|
|
|
prefix, fname, suffix = m.group(1, 2, 3)
|
|
|
|
if fname not in labels:
|
|
|
|
output.write(line)
|
|
|
|
continue
|
|
|
|
if line.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))
|
|
|
|
for line in input:
|
|
|
|
for part in re.findall(r"(\w+(?:\.c)?|\W+)", line):
|
|
|
|
if part in labels:
|
|
|
|
part = '<a href="#call:%s">%s</a>' % (part, part)
|
|
|
|
output.write(part)
|
|
|
|
output.write(trailer)
|
2019-03-30 03:32:18 -03:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
filename = "gprof.out"
|
|
|
|
if sys.argv[1:]:
|
|
|
|
filename = sys.argv[1]
|
|
|
|
outputfilename = filename + ".html"
|
|
|
|
input = add_escapes(filename)
|
|
|
|
with open(outputfilename, "w") as output:
|
|
|
|
gprof2html(input, output, filename)
|
2002-08-09 17:07:34 -03:00
|
|
|
webbrowser.open("file:" + os.path.abspath(outputfilename))
|
|
|
|
|
2004-08-09 14:27:55 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|