qemu-trace-stap 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/python
  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. from __future__ import print_function
  21. import argparse
  22. import copy
  23. import os.path
  24. import re
  25. import subprocess
  26. import sys
  27. def probe_prefix(binary):
  28. dirname, filename = os.path.split(binary)
  29. return re.sub("-", ".", filename) + ".log"
  30. def which(binary):
  31. for path in os.environ["PATH"].split(os.pathsep):
  32. if os.path.exists(os.path.join(path, binary)):
  33. return os.path.join(path, binary)
  34. print("Unable to find '%s' in $PATH" % binary)
  35. sys.exit(1)
  36. def tapset_dir(binary):
  37. dirname, filename = os.path.split(binary)
  38. if dirname == '':
  39. thisfile = which(binary)
  40. else:
  41. thisfile = os.path.realpath(binary)
  42. if not os.path.exists(thisfile):
  43. print("Unable to find '%s'" % thisfile)
  44. sys.exit(1)
  45. basedir = os.path.split(thisfile)[0]
  46. tapset = os.path.join(basedir, "..", "share", "systemtap", "tapset")
  47. return os.path.realpath(tapset)
  48. def tapset_env(tapset_dir):
  49. tenv = copy.copy(os.environ)
  50. tenv["SYSTEMTAP_TAPSET"] = tapset_dir
  51. return tenv
  52. def cmd_run(args):
  53. prefix = probe_prefix(args.binary)
  54. tapsets = tapset_dir(args.binary)
  55. if args.verbose:
  56. print("Using tapset dir '%s' for binary '%s'" % (tapsets, args.binary))
  57. probes = []
  58. for probe in args.probes:
  59. probes.append("probe %s.%s {}" % (prefix, probe))
  60. if len(probes) == 0:
  61. print("At least one probe pattern must be specified")
  62. sys.exit(1)
  63. script = " ".join(probes)
  64. if args.verbose:
  65. print("Compiling script '%s'" % script)
  66. script = """probe begin { print("Running script, <Ctrl>-c to quit\\n") } """ + script
  67. # We request an 8MB buffer, since the stap default 1MB buffer
  68. # can be easily overflowed by frequently firing QEMU traces
  69. stapargs = ["stap", "-s", "8"]
  70. if args.pid is not None:
  71. stapargs.extend(["-x", args.pid])
  72. stapargs.extend(["-e", script])
  73. subprocess.call(stapargs, env=tapset_env(tapsets))
  74. def cmd_list(args):
  75. tapsets = tapset_dir(args.binary)
  76. if args.verbose:
  77. print("Using tapset dir '%s' for binary '%s'" % (tapsets, args.binary))
  78. def print_probes(verbose, name):
  79. prefix = probe_prefix(args.binary)
  80. offset = len(prefix) + 1
  81. script = prefix + "." + name
  82. if verbose:
  83. print("Listing probes with name '%s'" % script)
  84. proc = subprocess.Popen(["stap", "-l", script],
  85. stdout=subprocess.PIPE, env=tapset_env(tapsets))
  86. out, err = proc.communicate()
  87. if proc.returncode != 0:
  88. print("No probes found, are the tapsets installed in %s" % tapset_dir(args.binary))
  89. sys.exit(1)
  90. for line in out.splitlines():
  91. if line.startswith(prefix):
  92. print("%s" % line[offset:])
  93. if len(args.probes) == 0:
  94. print_probes(args.verbose, "*")
  95. else:
  96. for probe in args.probes:
  97. print_probes(args.verbose, probe)
  98. def main():
  99. parser = argparse.ArgumentParser(description="QEMU SystemTap trace tool")
  100. parser.add_argument("-v", "--verbose", help="Print verbose progress info",
  101. action='store_true')
  102. subparser = parser.add_subparsers(help="commands")
  103. subparser.required = True
  104. subparser.dest = "command"
  105. runparser = subparser.add_parser("run", help="Run a trace session",
  106. formatter_class=argparse.RawDescriptionHelpFormatter,
  107. epilog="""
  108. To watch all trace points on the qemu-system-x86_64 binary:
  109. %(argv0)s run qemu-system-x86_64
  110. To only watch the trace points matching the qio* and qcrypto* patterns
  111. %(argv0)s run qemu-system-x86_64 'qio*' 'qcrypto*'
  112. """ % {"argv0": sys.argv[0]})
  113. runparser.set_defaults(func=cmd_run)
  114. runparser.add_argument("--pid", "-p", dest="pid",
  115. help="Restrict tracing to a specific process ID")
  116. runparser.add_argument("binary", help="QEMU system or user emulator binary")
  117. runparser.add_argument("probes", help="Probe names or wildcards",
  118. nargs=argparse.REMAINDER)
  119. listparser = subparser.add_parser("list", help="List probe points",
  120. formatter_class=argparse.RawDescriptionHelpFormatter,
  121. epilog="""
  122. To list all trace points on the qemu-system-x86_64 binary:
  123. %(argv0)s list qemu-system-x86_64
  124. To only list the trace points matching the qio* and qcrypto* patterns
  125. %(argv0)s list qemu-system-x86_64 'qio*' 'qcrypto*'
  126. """ % {"argv0": sys.argv[0]})
  127. listparser.set_defaults(func=cmd_list)
  128. listparser.add_argument("binary", help="QEMU system or user emulator binary")
  129. listparser.add_argument("probes", help="Probe names or wildcards",
  130. nargs=argparse.REMAINDER)
  131. args = parser.parse_args()
  132. args.func(args)
  133. sys.exit(0)
  134. if __name__ == '__main__':
  135. main()