blob: c9e47371d3c6323efd3863d2f56f4dce148645ed [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
Daniel P. Berrange80dd5c42016-10-04 14:35:59 +010018import os.path
19import re
Lluís Vilanova650ab982012-04-03 20:47:39 +020020
21from tracetool import error_write, out
22import tracetool.backend
23import tracetool.format
24
25
26_SCRIPT = ""
27
28def error_opt(msg = None):
29 if msg is not None:
30 error_write("Error: " + msg + "\n")
31
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("""\
Lluís Vilanova5b808272014-05-27 15:02:14 +020037Usage: %(script)s --format=<format> --backends=<backends> [<options>]
Lluís Vilanova650ab982012-04-03 20:47:39 +020038
39Backends:
40%(backends)s
41
42Formats:
43%(formats)s
44
45Options:
46 --help This help message.
47 --list-backends Print list of available backends.
Lluís Vilanova5b808272014-05-27 15:02:14 +020048 --check-backends Check if the given backend is valid.
Lluís Vilanova52ef0932012-04-03 20:48:12 +020049 --binary <path> Full path to QEMU binary.
50 --target-type <type> QEMU emulator target type ('system' or 'user').
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020051 --target-name <name> QEMU emulator target name.
Lluís Vilanova52ef0932012-04-03 20:48:12 +020052 --probe-prefix <prefix> Prefix for dtrace probe names
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020053 (default: qemu-<target-type>-<target-name>).\
Lluís Vilanova650ab982012-04-03 20:47:39 +020054""" % {
55 "script" : _SCRIPT,
56 "backends" : backend_descr,
57 "formats" : format_descr,
58 })
59
60 if msg is None:
61 sys.exit(0)
62 else:
63 sys.exit(1)
64
Daniel P. Berrange80dd5c42016-10-04 14:35:59 +010065def 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:]
70
71 if dirname == "":
72 return "common"
Greg Kurzd4f7ca52016-11-17 14:59:13 +010073 return "_" + re.sub(r"[^A-Za-z0-9]", "_", dirname)
Lluís Vilanova650ab982012-04-03 20:47:39 +020074
75def main(args):
76 global _SCRIPT
77 _SCRIPT = args[0]
78
Lluís Vilanova5b808272014-05-27 15:02:14 +020079 long_opts = ["backends=", "format=", "help", "list-backends",
80 "check-backends"]
81 long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="]
Lluís Vilanova650ab982012-04-03 20:47:39 +020082
83 try:
84 opts, args = getopt.getopt(args[1:], "", long_opts)
Markus Armbruster86b227d2015-12-18 08:52:43 +010085 except getopt.GetoptError as err:
Lluís Vilanova650ab982012-04-03 20:47:39 +020086 error_opt(str(err))
87
Lluís Vilanova5b808272014-05-27 15:02:14 +020088 check_backends = False
89 arg_backends = []
Lluís Vilanova650ab982012-04-03 20:47:39 +020090 arg_format = ""
Lluís Vilanova52ef0932012-04-03 20:48:12 +020091 binary = None
92 target_type = None
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020093 target_name = None
Lluís Vilanova52ef0932012-04-03 20:48:12 +020094 probe_prefix = None
Lluís Vilanova650ab982012-04-03 20:47:39 +020095 for opt, arg in opts:
96 if opt == "--help":
97 error_opt()
98
Lluís Vilanova5b808272014-05-27 15:02:14 +020099 elif opt == "--backends":
100 arg_backends = arg.split(",")
Lluís Vilanova650ab982012-04-03 20:47:39 +0200101 elif opt == "--format":
102 arg_format = arg
103
104 elif opt == "--list-backends":
Lluís Vilanova93fba162013-03-05 14:47:26 +0100105 public_backends = tracetool.backend.get_list(only_public = True)
106 out(", ".join([ b for b,_ in public_backends ]))
Lluís Vilanova650ab982012-04-03 20:47:39 +0200107 sys.exit(0)
Lluís Vilanova5b808272014-05-27 15:02:14 +0200108 elif opt == "--check-backends":
109 check_backends = True
Lluís Vilanova650ab982012-04-03 20:47:39 +0200110
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200111 elif opt == "--binary":
112 binary = arg
113 elif opt == '--target-type':
114 target_type = arg
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200115 elif opt == '--target-name':
116 target_name = arg
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200117 elif opt == '--probe-prefix':
118 probe_prefix = arg
119
Lluís Vilanova650ab982012-04-03 20:47:39 +0200120 else:
121 error_opt("unhandled option: %s" % opt)
122
Lluís Vilanova5b808272014-05-27 15:02:14 +0200123 if len(arg_backends) == 0:
124 error_opt("no backends specified")
Lluís Vilanova650ab982012-04-03 20:47:39 +0200125
Lluís Vilanova5b808272014-05-27 15:02:14 +0200126 if check_backends:
127 for backend in arg_backends:
128 if not tracetool.backend.exists(backend):
129 sys.exit(1)
130 sys.exit(0)
Lluís Vilanova650ab982012-04-03 20:47:39 +0200131
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200132 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")
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200137 if probe_prefix is None and target_name is None:
138 error_opt("--target-name is required for SystemTAP tapset generator")
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200139
140 if probe_prefix is None:
Lluís Vilanova5b808272014-05-27 15:02:14 +0200141 probe_prefix = ".".join(["qemu", target_type, target_name])
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200142
Daniel P. Berrange0bc64842016-10-04 14:35:58 +0100143 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)
Daniel P. Berrange9096b782016-10-04 14:35:57 +0100147
Daniel P. Berrange80dd5c42016-10-04 14:35:59 +0100148 group = make_group_name(args[0])
149
Lluís Vilanova650ab982012-04-03 20:47:39 +0200150 try:
Daniel P. Berrange80dd5c42016-10-04 14:35:59 +0100151 tracetool.generate(events, group, arg_format, arg_backends,
Lluís Vilanova5b808272014-05-27 15:02:14 +0200152 binary=binary, probe_prefix=probe_prefix)
Markus Armbruster86b227d2015-12-18 08:52:43 +0100153 except tracetool.TracetoolError as e:
Lluís Vilanova650ab982012-04-03 20:47:39 +0200154 error_opt(str(e))
155
156if __name__ == "__main__":
157 main(sys.argv)