tracetool.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Command-line wrapper for the tracetool machinery.
  5. """
  6. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  7. __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
  8. __license__ = "GPL version 2 or (at your option) any later version"
  9. __maintainer__ = "Stefan Hajnoczi"
  10. __email__ = "stefanha@linux.vnet.ibm.com"
  11. import sys
  12. import getopt
  13. import os.path
  14. import re
  15. from tracetool import error_write, out
  16. import tracetool.backend
  17. import tracetool.format
  18. _SCRIPT = ""
  19. def error_opt(msg = None):
  20. if msg is not None:
  21. error_write("Error: " + msg + "\n")
  22. backend_descr = "\n".join([ " %-15s %s" % (n, d)
  23. for n,d in tracetool.backend.get_list() ])
  24. format_descr = "\n".join([ " %-15s %s" % (n, d)
  25. for n,d in tracetool.format.get_list() ])
  26. error_write("""\
  27. Usage: %(script)s --format=<format> --backends=<backends> [<options>]
  28. Backends:
  29. %(backends)s
  30. Formats:
  31. %(formats)s
  32. Options:
  33. --help This help message.
  34. --list-backends Print list of available backends.
  35. --check-backends Check if the given backend is valid.
  36. --binary <path> Full path to QEMU binary.
  37. --target-type <type> QEMU emulator target type ('system' or 'user').
  38. --target-name <name> QEMU emulator target name.
  39. --group <name> Name of the event group
  40. --probe-prefix <prefix> Prefix for dtrace probe names
  41. (default: qemu-<target-type>-<target-name>).\
  42. """ % {
  43. "script" : _SCRIPT,
  44. "backends" : backend_descr,
  45. "formats" : format_descr,
  46. })
  47. if msg is None:
  48. sys.exit(0)
  49. else:
  50. sys.exit(1)
  51. def main(args):
  52. global _SCRIPT
  53. _SCRIPT = args[0]
  54. long_opts = ["backends=", "format=", "help", "list-backends",
  55. "check-backends", "group="]
  56. long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="]
  57. try:
  58. opts, args = getopt.getopt(args[1:], "", long_opts)
  59. except getopt.GetoptError as err:
  60. error_opt(str(err))
  61. check_backends = False
  62. arg_backends = []
  63. arg_format = ""
  64. arg_group = None
  65. binary = None
  66. target_type = None
  67. target_name = None
  68. probe_prefix = None
  69. for opt, arg in opts:
  70. if opt == "--help":
  71. error_opt()
  72. elif opt == "--backends":
  73. arg_backends = arg.split(",")
  74. elif opt == "--group":
  75. arg_group = arg
  76. elif opt == "--format":
  77. arg_format = arg
  78. elif opt == "--list-backends":
  79. public_backends = tracetool.backend.get_list(only_public = True)
  80. out(", ".join([ b for b,_ in public_backends ]))
  81. sys.exit(0)
  82. elif opt == "--check-backends":
  83. check_backends = True
  84. elif opt == "--binary":
  85. binary = arg
  86. elif opt == '--target-type':
  87. target_type = arg
  88. elif opt == '--target-name':
  89. target_name = arg
  90. elif opt == '--probe-prefix':
  91. probe_prefix = arg
  92. else:
  93. error_opt("unhandled option: %s" % opt)
  94. if len(arg_backends) == 0:
  95. error_opt("no backends specified")
  96. if check_backends:
  97. for backend in arg_backends:
  98. if not tracetool.backend.exists(backend):
  99. sys.exit(1)
  100. sys.exit(0)
  101. if arg_group is None:
  102. error_opt("group name is required")
  103. if arg_format == "stap":
  104. if binary is None:
  105. error_opt("--binary is required for SystemTAP tapset generator")
  106. if probe_prefix is None and target_type is None:
  107. error_opt("--target-type is required for SystemTAP tapset generator")
  108. if probe_prefix is None and target_name is None:
  109. error_opt("--target-name is required for SystemTAP tapset generator")
  110. if probe_prefix is None:
  111. probe_prefix = ".".join(["qemu", target_type, target_name])
  112. if len(args) < 1:
  113. error_opt("missing trace-events filepath")
  114. events = []
  115. for arg in args:
  116. with open(arg, "r") as fh:
  117. events.extend(tracetool.read_events(fh, arg))
  118. try:
  119. tracetool.generate(events, arg_group, arg_format, arg_backends,
  120. binary=binary, probe_prefix=probe_prefix)
  121. except tracetool.TracetoolError as e:
  122. error_opt(str(e))
  123. if __name__ == "__main__":
  124. main(sys.argv)