pcie_sriov: Ensure VF function number does not overflow
[qemu/ar7.git] / scripts / tracetool.py
blob5de9ce96d3024b99e28dc5f7c2b704f3fa7bdd1b
1 #!/usr/bin/env python3
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@redhat.com"
16 import sys
17 import getopt
19 from tracetool import error_write, out, out_open
20 import tracetool.backend
21 import tracetool.format
24 _SCRIPT = ""
26 def error_opt(msg = None):
27 if msg is not None:
28 error_write("Error: " + msg + "\n")
30 backend_descr = "\n".join([ " %-15s %s" % (n, d)
31 for n,d in tracetool.backend.get_list() ])
32 format_descr = "\n".join([ " %-15s %s" % (n, d)
33 for n,d in tracetool.format.get_list() ])
34 error_write("""\
35 Usage: %(script)s --format=<format> --backends=<backends> [<options>] <trace-events> ... <output>
37 Backends:
38 %(backends)s
40 Formats:
41 %(formats)s
43 Options:
44 --help This help message.
45 --list-backends Print list of available backends.
46 --check-backends Check if the given backend is valid.
47 --binary <path> Full path to QEMU binary (required for 'stap' backend).
48 --group <name> Name of the event group.
49 --probe-prefix <prefix> Prefix for dtrace probe names (required for 'stap' backend).
50 """ % {
51 "script" : _SCRIPT,
52 "backends" : backend_descr,
53 "formats" : format_descr,
56 if msg is None:
57 sys.exit(0)
58 else:
59 sys.exit(1)
61 def main(args):
62 global _SCRIPT
63 _SCRIPT = args[0]
65 long_opts = ["backends=", "format=", "help", "list-backends",
66 "check-backends", "group="]
67 long_opts += ["binary=", "probe-prefix="]
69 try:
70 opts, args = getopt.getopt(args[1:], "", long_opts)
71 except getopt.GetoptError as err:
72 error_opt(str(err))
74 check_backends = False
75 arg_backends = []
76 arg_format = ""
77 arg_group = None
78 binary = None
79 probe_prefix = None
80 for opt, arg in opts:
81 if opt == "--help":
82 error_opt()
84 elif opt == "--backends":
85 arg_backends = arg.split(",")
86 elif opt == "--group":
87 arg_group = arg
88 elif opt == "--format":
89 arg_format = arg
91 elif opt == "--list-backends":
92 public_backends = tracetool.backend.get_list(only_public = True)
93 out(", ".join([ b for b,_ in public_backends ]))
94 sys.exit(0)
95 elif opt == "--check-backends":
96 check_backends = True
98 elif opt == "--binary":
99 binary = arg
100 elif opt == '--probe-prefix':
101 probe_prefix = arg
103 else:
104 error_opt("unhandled option: %s" % opt)
106 if len(arg_backends) == 0:
107 error_opt("no backends specified")
109 if check_backends:
110 for backend in arg_backends:
111 if not tracetool.backend.exists(backend):
112 sys.exit(1)
113 sys.exit(0)
115 if arg_group is None:
116 error_opt("group name is required")
118 if arg_format == "stap":
119 if binary is None:
120 error_opt("--binary is required for SystemTAP tapset generator")
121 if probe_prefix is None:
122 error_opt("--probe-prefix is required for SystemTAP tapset generator")
124 if len(args) < 2:
125 error_opt("missing trace-events and output filepaths")
126 events = []
127 for arg in args[:-1]:
128 with open(arg, "r") as fh:
129 events.extend(tracetool.read_events(fh, arg))
131 out_open(args[-1])
133 try:
134 tracetool.generate(events, arg_group, arg_format, arg_backends,
135 binary=binary, probe_prefix=probe_prefix)
136 except tracetool.TracetoolError as e:
137 error_opt(str(e))
139 if __name__ == "__main__":
140 main(sys.argv)