Merge remote-tracking branch 'public/tags/block-pull-request' into staging
[qemu.git] / scripts / tracetool.py
blobfe9c9e904b0304195c9d7a1c4d734f1e97cb3ded
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """
5 Command-line wrapper for the tracetool machinery.
6 """
8 __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
9 __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
10 __license__ = "GPL version 2 or (at your option) any later version"
12 __maintainer__ = "Stefan Hajnoczi"
13 __email__ = "stefanha@linux.vnet.ibm.com"
16 import sys
17 import getopt
18 import os.path
19 import re
21 from tracetool import error_write, out
22 import tracetool.backend
23 import tracetool.format
26 _SCRIPT = ""
28 def error_opt(msg = None):
29 if msg is not None:
30 error_write("Error: " + msg + "\n")
32 backend_descr = "\n".join([ " %-15s %s" % (n, d)
33 for n,d in tracetool.backend.get_list() ])
34 format_descr = "\n".join([ " %-15s %s" % (n, d)
35 for n,d in tracetool.format.get_list() ])
36 error_write("""\
37 Usage: %(script)s --format=<format> --backends=<backends> [<options>]
39 Backends:
40 %(backends)s
42 Formats:
43 %(formats)s
45 Options:
46 --help This help message.
47 --list-backends Print list of available backends.
48 --check-backends Check if the given backend is valid.
49 --binary <path> Full path to QEMU binary.
50 --target-type <type> QEMU emulator target type ('system' or 'user').
51 --target-name <name> QEMU emulator target name.
52 --probe-prefix <prefix> Prefix for dtrace probe names
53 (default: qemu-<target-type>-<target-name>).\
54 """ % {
55 "script" : _SCRIPT,
56 "backends" : backend_descr,
57 "formats" : format_descr,
60 if msg is None:
61 sys.exit(0)
62 else:
63 sys.exit(1)
65 def make_group_name(filename):
66 dirname = os.path.realpath(os.path.dirname(filename))
67 basedir = os.path.join(os.path.dirname(__file__), os.pardir)
68 basedir = os.path.realpath(os.path.abspath(basedir))
69 dirname = dirname[len(basedir) + 1:]
71 if dirname == "":
72 return "common"
73 return re.sub(r"[^A-Za-z0-9]", "_", dirname)
75 def main(args):
76 global _SCRIPT
77 _SCRIPT = args[0]
79 long_opts = ["backends=", "format=", "help", "list-backends",
80 "check-backends"]
81 long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="]
83 try:
84 opts, args = getopt.getopt(args[1:], "", long_opts)
85 except getopt.GetoptError as err:
86 error_opt(str(err))
88 check_backends = False
89 arg_backends = []
90 arg_format = ""
91 binary = None
92 target_type = None
93 target_name = None
94 probe_prefix = None
95 for opt, arg in opts:
96 if opt == "--help":
97 error_opt()
99 elif opt == "--backends":
100 arg_backends = arg.split(",")
101 elif opt == "--format":
102 arg_format = arg
104 elif opt == "--list-backends":
105 public_backends = tracetool.backend.get_list(only_public = True)
106 out(", ".join([ b for b,_ in public_backends ]))
107 sys.exit(0)
108 elif opt == "--check-backends":
109 check_backends = True
111 elif opt == "--binary":
112 binary = arg
113 elif opt == '--target-type':
114 target_type = arg
115 elif opt == '--target-name':
116 target_name = arg
117 elif opt == '--probe-prefix':
118 probe_prefix = arg
120 else:
121 error_opt("unhandled option: %s" % opt)
123 if len(arg_backends) == 0:
124 error_opt("no backends specified")
126 if check_backends:
127 for backend in arg_backends:
128 if not tracetool.backend.exists(backend):
129 sys.exit(1)
130 sys.exit(0)
132 if arg_format == "stap":
133 if binary is None:
134 error_opt("--binary is required for SystemTAP tapset generator")
135 if probe_prefix is None and target_type is None:
136 error_opt("--target-type is required for SystemTAP tapset generator")
137 if probe_prefix is None and target_name is None:
138 error_opt("--target-name is required for SystemTAP tapset generator")
140 if probe_prefix is None:
141 probe_prefix = ".".join(["qemu", target_type, target_name])
143 if len(args) != 1:
144 error_opt("missing trace-events filepath")
145 with open(args[0], "r") as fh:
146 events = tracetool.read_events(fh)
148 group = make_group_name(args[0])
150 try:
151 tracetool.generate(events, group, arg_format, arg_backends,
152 binary=binary, probe_prefix=probe_prefix)
153 except tracetool.TracetoolError as e:
154 error_opt(str(e))
156 if __name__ == "__main__":
157 main(sys.argv)