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.

79 lines
2.1 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(input):
  18. for line in input:
  19. yield cgi.escape(line)
  20. def main():
  21. filename = "gprof.out"
  22. if sys.argv[1:]:
  23. filename = sys.argv[1]
  24. outputfilename = filename + ".html"
  25. input = add_escapes(file(filename))
  26. output = file(outputfilename, "w")
  27. output.write(header % filename)
  28. for line in input:
  29. output.write(line)
  30. if line.startswith(" time"):
  31. break
  32. labels = {}
  33. for line in input:
  34. m = re.match(r"(.* )(\w+)\n", line)
  35. if not m:
  36. output.write(line)
  37. break
  38. stuff, fname = m.group(1, 2)
  39. labels[fname] = fname
  40. output.write('%s<a name="flat:%s" href="#call:%s">%s</a>\n' %
  41. (stuff, fname, fname, fname))
  42. for line in input:
  43. output.write(line)
  44. if line.startswith("index % time"):
  45. break
  46. for line in input:
  47. m = re.match(r"(.* )(\w+)(( &lt;cycle.*&gt;)? \[\d+\])\n", line)
  48. if not m:
  49. output.write(line)
  50. if line.startswith("Index by function name"):
  51. break
  52. continue
  53. prefix, fname, suffix = m.group(1, 2, 3)
  54. if fname not in labels:
  55. output.write(line)
  56. continue
  57. if line.startswith("["):
  58. output.write('%s<a name="call:%s" href="#flat:%s">%s</a>%s\n' %
  59. (prefix, fname, fname, fname, suffix))
  60. else:
  61. output.write('%s<a href="#call:%s">%s</a>%s\n' %
  62. (prefix, fname, fname, suffix))
  63. for line in input:
  64. for part in re.findall(r"(\w+(?:\.c)?|\W+)", line):
  65. if part in labels:
  66. part = '<a href="#call:%s">%s</a>' % (part, part)
  67. output.write(part)
  68. output.write(trailer)
  69. output.close()
  70. webbrowser.open("file:" + os.path.abspath(outputfilename))
  71. if __name__ == '__main__':
  72. main()