blob: 83bde7bda905b5a10fc58171479ff35bd93c4121 [file] [log] [blame]
Lluís Vilanova650ab982012-04-03 20:47:39 +02001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5Command-line wrapper for the tracetool machinery.
6"""
7
8__author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
Lluís Vilanova5b808272014-05-27 15:02:14 +02009__copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
Lluís Vilanova650ab982012-04-03 20:47:39 +020010__license__ = "GPL version 2 or (at your option) any later version"
11
12__maintainer__ = "Stefan Hajnoczi"
13__email__ = "stefanha@linux.vnet.ibm.com"
14
15
16import sys
17import getopt
18
19from tracetool import error_write, out
20import tracetool.backend
21import tracetool.format
22
23
24_SCRIPT = ""
25
26def error_opt(msg = None):
27 if msg is not None:
28 error_write("Error: " + msg + "\n")
29
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("""\
Lluís Vilanova5b808272014-05-27 15:02:14 +020035Usage: %(script)s --format=<format> --backends=<backends> [<options>]
Lluís Vilanova650ab982012-04-03 20:47:39 +020036
37Backends:
38%(backends)s
39
40Formats:
41%(formats)s
42
43Options:
44 --help This help message.
45 --list-backends Print list of available backends.
Lluís Vilanova5b808272014-05-27 15:02:14 +020046 --check-backends Check if the given backend is valid.
Lluís Vilanova52ef0932012-04-03 20:48:12 +020047 --binary <path> Full path to QEMU binary.
48 --target-type <type> QEMU emulator target type ('system' or 'user').
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020049 --target-name <name> QEMU emulator target name.
Lluís Vilanova52ef0932012-04-03 20:48:12 +020050 --probe-prefix <prefix> Prefix for dtrace probe names
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020051 (default: qemu-<target-type>-<target-name>).\
Lluís Vilanova650ab982012-04-03 20:47:39 +020052""" % {
53 "script" : _SCRIPT,
54 "backends" : backend_descr,
55 "formats" : format_descr,
56 })
57
58 if msg is None:
59 sys.exit(0)
60 else:
61 sys.exit(1)
62
63
64def main(args):
65 global _SCRIPT
66 _SCRIPT = args[0]
67
Lluís Vilanova5b808272014-05-27 15:02:14 +020068 long_opts = ["backends=", "format=", "help", "list-backends",
69 "check-backends"]
70 long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="]
Lluís Vilanova650ab982012-04-03 20:47:39 +020071
72 try:
73 opts, args = getopt.getopt(args[1:], "", long_opts)
Stefan Hajnoczi662da382012-04-25 10:39:42 +010074 except getopt.GetoptError, err:
Lluís Vilanova650ab982012-04-03 20:47:39 +020075 error_opt(str(err))
76
Lluís Vilanova5b808272014-05-27 15:02:14 +020077 check_backends = False
78 arg_backends = []
Lluís Vilanova650ab982012-04-03 20:47:39 +020079 arg_format = ""
Lluís Vilanova52ef0932012-04-03 20:48:12 +020080 binary = None
81 target_type = None
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020082 target_name = None
Lluís Vilanova52ef0932012-04-03 20:48:12 +020083 probe_prefix = None
Lluís Vilanova650ab982012-04-03 20:47:39 +020084 for opt, arg in opts:
85 if opt == "--help":
86 error_opt()
87
Lluís Vilanova5b808272014-05-27 15:02:14 +020088 elif opt == "--backends":
89 arg_backends = arg.split(",")
Lluís Vilanova650ab982012-04-03 20:47:39 +020090 elif opt == "--format":
91 arg_format = arg
92
93 elif opt == "--list-backends":
Lluís Vilanova93fba162013-03-05 14:47:26 +010094 public_backends = tracetool.backend.get_list(only_public = True)
95 out(", ".join([ b for b,_ in public_backends ]))
Lluís Vilanova650ab982012-04-03 20:47:39 +020096 sys.exit(0)
Lluís Vilanova5b808272014-05-27 15:02:14 +020097 elif opt == "--check-backends":
98 check_backends = True
Lluís Vilanova650ab982012-04-03 20:47:39 +020099
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200100 elif opt == "--binary":
101 binary = arg
102 elif opt == '--target-type':
103 target_type = arg
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200104 elif opt == '--target-name':
105 target_name = arg
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200106 elif opt == '--probe-prefix':
107 probe_prefix = arg
108
Lluís Vilanova650ab982012-04-03 20:47:39 +0200109 else:
110 error_opt("unhandled option: %s" % opt)
111
Lluís Vilanova5b808272014-05-27 15:02:14 +0200112 if len(arg_backends) == 0:
113 error_opt("no backends specified")
Lluís Vilanova650ab982012-04-03 20:47:39 +0200114
Lluís Vilanova5b808272014-05-27 15:02:14 +0200115 if check_backends:
116 for backend in arg_backends:
117 if not tracetool.backend.exists(backend):
118 sys.exit(1)
119 sys.exit(0)
Lluís Vilanova650ab982012-04-03 20:47:39 +0200120
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200121 if arg_format == "stap":
122 if binary is None:
123 error_opt("--binary is required for SystemTAP tapset generator")
124 if probe_prefix is None and target_type is None:
125 error_opt("--target-type is required for SystemTAP tapset generator")
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200126 if probe_prefix is None and target_name is None:
127 error_opt("--target-name is required for SystemTAP tapset generator")
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200128
129 if probe_prefix is None:
Lluís Vilanova5b808272014-05-27 15:02:14 +0200130 probe_prefix = ".".join(["qemu", target_type, target_name])
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200131
Lluís Vilanova650ab982012-04-03 20:47:39 +0200132 try:
Lluís Vilanova5b808272014-05-27 15:02:14 +0200133 tracetool.generate(sys.stdin, arg_format, arg_backends,
134 binary=binary, probe_prefix=probe_prefix)
Stefan Hajnoczi662da382012-04-25 10:39:42 +0100135 except tracetool.TracetoolError, e:
Lluís Vilanova650ab982012-04-03 20:47:39 +0200136 error_opt(str(e))
137
138if __name__ == "__main__":
139 main(sys.argv)