2
0

qemu-trace-stap 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. prefix = probe_prefix(args.binary)
  49. tapsets = tapset_dir(args.binary)
  50. if args.verbose:
  51. print("Using tapset dir '%s' for binary '%s'" % (tapsets, args.binary))
  52. probes = []
  53. for probe in args.probes:
  54. probes.append("probe %s.%s {}" % (prefix, probe))
  55. if len(probes) == 0:
  56. print("At least one probe pattern must be specified")
  57. sys.exit(1)
  58. script = " ".join(probes)
  59. if args.verbose:
  60. print("Compiling script '%s'" % script)
  61. script = """probe begin { print("Running script, <Ctrl>-c to quit\\n") } """ + script
  62. # We request an 8MB buffer, since the stap default 1MB buffer
  63. # can be easily overflowed by frequently firing QEMU traces
  64. stapargs = ["stap", "-s", "8", "-I", tapsets ]
  65. if args.pid is not None:
  66. stapargs.extend(["-x", args.pid])
  67. stapargs.extend(["-e", script])
  68. subprocess.call(stapargs)
  69. def cmd_list(args):
  70. tapsets = tapset_dir(args.binary)
  71. if args.verbose:
  72. print("Using tapset dir '%s' for binary '%s'" % (tapsets, args.binary))
  73. def print_probes(verbose, name):
  74. prefix = probe_prefix(args.binary)
  75. offset = len(prefix) + 1
  76. script = prefix + "." + name
  77. if verbose:
  78. print("Listing probes with name '%s'" % script)
  79. proc = subprocess.Popen(["stap", "-I", tapsets, "-l", script],
  80. stdout=subprocess.PIPE,
  81. universal_newlines=True)
  82. out, err = proc.communicate()
  83. if proc.returncode != 0:
  84. print("No probes found, are the tapsets installed in %s" % tapset_dir(args.binary))
  85. sys.exit(1)
  86. for line in out.splitlines():
  87. if line.startswith(prefix):
  88. print("%s" % line[offset:])
  89. if len(args.probes) == 0:
  90. print_probes(args.verbose, "*")
  91. else:
  92. for probe in args.probes:
  93. print_probes(args.verbose, probe)
  94. def main():
  95. parser = argparse.ArgumentParser(description="QEMU SystemTap trace tool")
  96. parser.add_argument("-v", "--verbose", help="Print verbose progress info",
  97. action='store_true')
  98. subparser = parser.add_subparsers(help="commands")
  99. subparser.required = True
  100. subparser.dest = "command"
  101. runparser = subparser.add_parser("run", help="Run a trace session",
  102. formatter_class=argparse.RawDescriptionHelpFormatter,
  103. epilog="""
  104. To watch all trace points on the qemu-system-x86_64 binary:
  105. %(argv0)s run qemu-system-x86_64
  106. To only watch the trace points matching the qio* and qcrypto* patterns
  107. %(argv0)s run qemu-system-x86_64 'qio*' 'qcrypto*'
  108. """ % {"argv0": sys.argv[0]})
  109. runparser.set_defaults(func=cmd_run)
  110. runparser.add_argument("--pid", "-p", dest="pid",
  111. help="Restrict tracing to a specific process ID")
  112. runparser.add_argument("binary", help="QEMU system or user emulator binary")
  113. runparser.add_argument("probes", help="Probe names or wildcards",
  114. nargs=argparse.REMAINDER)
  115. listparser = subparser.add_parser("list", help="List probe points",
  116. formatter_class=argparse.RawDescriptionHelpFormatter,
  117. epilog="""
  118. To list all trace points on the qemu-system-x86_64 binary:
  119. %(argv0)s list qemu-system-x86_64
  120. To only list the trace points matching the qio* and qcrypto* patterns
  121. %(argv0)s list qemu-system-x86_64 'qio*' 'qcrypto*'
  122. """ % {"argv0": sys.argv[0]})
  123. listparser.set_defaults(func=cmd_list)
  124. listparser.add_argument("binary", help="QEMU system or user emulator binary")
  125. listparser.add_argument("probes", help="Probe names or wildcards",
  126. nargs=argparse.REMAINDER)
  127. args = parser.parse_args()
  128. args.func(args)
  129. sys.exit(0)
  130. if __name__ == '__main__':
  131. main()