You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

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