qemu-trace-stap 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python3
  2. # -*- python -*-
  3. #
  4. # Copyright (C) 2019 Red Hat, Inc
  5. #
  6. # QEMU SystemTap Trace Tool
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import argparse
  21. import copy
  22. import os.path
  23. import re
  24. import subprocess
  25. import sys
  26. def probe_prefix(binary):
  27. dirname, filename = os.path.split(binary)
  28. return re.sub("-", ".", filename) + ".log"
  29. def which(binary):
  30. for path in os.environ["PATH"].split(os.pathsep):
  31. if os.path.exists(os.path.join(path, binary)):
  32. return os.path.join(path, binary)
  33. print("Unable to find '%s' in $PATH" % binary)
  34. sys.exit(1)
  35. def tapset_dir(binary):
  36. dirname, filename = os.path.split(binary)
  37. if dirname == '':
  38. thisfile = which(binary)
  39. else:
  40. thisfile = os.path.realpath(binary)
  41. if not os.path.exists(thisfile):
  42. print("Unable to find '%s'" % thisfile)
  43. sys.exit(1)
  44. basedir = os.path.split(thisfile)[0]
  45. tapset = os.path.join(basedir, "..", "share", "systemtap", "tapset")
  46. return os.path.realpath(tapset)
  47. def cmd_run(args):
  48. stap = which("stap")
  49. prefix = probe_prefix(args.binary)
  50. tapsets = tapset_dir(args.binary)
  51. if args.verbose:
  52. print("Using tapset dir '%s' for binary '%s'" % (tapsets, args.binary))
  53. probes = []
  54. for probe in args.probes:
  55. probes.append("probe %s.%s {}" % (prefix, probe))
  56. if len(probes) == 0:
  57. print("At least one probe pattern must be specified")
  58. sys.exit(1)
  59. script = " ".join(probes)
  60. if args.verbose:
  61. print("Compiling script '%s'" % script)
  62. script = """probe begin { print("Running script, <Ctrl>-c to quit\\n") } """ + script
  63. # We request an 8MB buffer, since the stap default 1MB buffer
  64. # can be easily overflowed by frequently firing QEMU traces
  65. stapargs = [stap, "-s", "8", "-I", tapsets ]
  66. if args.pid is not None:
  67. stapargs.extend(["-x", args.pid])
  68. stapargs.extend(["-e", script])
  69. subprocess.call(stapargs)
  70. def cmd_list(args):
  71. stap = which("stap")
  72. tapsets = tapset_dir(args.binary)
  73. if args.verbose:
  74. print("Using tapset dir '%s' for binary '%s'" % (tapsets, args.binary))
  75. def print_probes(verbose, name):
  76. prefix = probe_prefix(args.binary)
  77. offset = len(prefix) + 1
  78. script = prefix + "." + name
  79. if verbose:
  80. print("Listing probes with name '%s'" % script)
  81. proc = subprocess.Popen([stap, "-I", tapsets, "-l", script],
  82. stdout=subprocess.PIPE,
  83. universal_newlines=True)
  84. out, err = proc.communicate()
  85. if proc.returncode != 0:
  86. print("No probes found, are the tapsets installed in %s" % tapset_dir(args.binary))
  87. sys.exit(1)
  88. for line in out.splitlines():
  89. if line.startswith(prefix):
  90. print("%s" % line[offset:])
  91. if len(args.probes) == 0:
  92. print_probes(args.verbose, "*")
  93. else:
  94. for probe in args.probes:
  95. print_probes(args.verbose, probe)
  96. def main():
  97. parser = argparse.ArgumentParser(description="QEMU SystemTap trace tool")
  98. parser.add_argument("-v", "--verbose", help="Print verbose progress info",
  99. action='store_true')
  100. subparser = parser.add_subparsers(help="commands")
  101. subparser.required = True
  102. subparser.dest = "command"
  103. runparser = subparser.add_parser("run", help="Run a trace session",
  104. formatter_class=argparse.RawDescriptionHelpFormatter,
  105. epilog="""
  106. To watch all trace points on the qemu-system-x86_64 binary:
  107. %(argv0)s run qemu-system-x86_64
  108. To only watch the trace points matching the qio* and qcrypto* patterns
  109. %(argv0)s run qemu-system-x86_64 'qio*' 'qcrypto*'
  110. """ % {"argv0": sys.argv[0]})
  111. runparser.set_defaults(func=cmd_run)
  112. runparser.add_argument("--pid", "-p", dest="pid",
  113. help="Restrict tracing to a specific process ID")
  114. runparser.add_argument("binary", help="QEMU system or user emulator binary")
  115. runparser.add_argument("probes", help="Probe names or wildcards",
  116. nargs=argparse.REMAINDER)
  117. listparser = subparser.add_parser("list", help="List probe points",
  118. formatter_class=argparse.RawDescriptionHelpFormatter,
  119. epilog="""
  120. To list all trace points on the qemu-system-x86_64 binary:
  121. %(argv0)s list qemu-system-x86_64
  122. To only list the trace points matching the qio* and qcrypto* patterns
  123. %(argv0)s list qemu-system-x86_64 'qio*' 'qcrypto*'
  124. """ % {"argv0": sys.argv[0]})
  125. listparser.set_defaults(func=cmd_list)
  126. listparser.add_argument("binary", help="QEMU system or user emulator binary")
  127. listparser.add_argument("probes", help="Probe names or wildcards",
  128. nargs=argparse.REMAINDER)
  129. args = parser.parse_args()
  130. args.func(args)
  131. sys.exit(0)
  132. if __name__ == '__main__':
  133. main()