blob: 3beaa66bd8b5f126f3509c45b9a107bf53bad102 [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.
Daniel P. Berrange2098c562017-01-25 16:14:14 +000050 --group <name> Name of the event group
Lluís Vilanova52ef0932012-04-03 20:48:12 +020051 --probe-prefix <prefix> Prefix for dtrace probe names
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020052 (default: qemu-<target-type>-<target-name>).\
Lluís Vilanova650ab982012-04-03 20:47:39 +020053""" % {
54 "script" : _SCRIPT,
55 "backends" : backend_descr,
56 "formats" : format_descr,
57 })
58
59 if msg is None:
60 sys.exit(0)
61 else:
62 sys.exit(1)
63
Lluís Vilanova650ab982012-04-03 20:47:39 +020064def 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",
Daniel P. Berrange2098c562017-01-25 16:14:14 +000069 "check-backends", "group="]
Lluís Vilanova5b808272014-05-27 15:02:14 +020070 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)
Markus Armbruster86b227d2015-12-18 08:52:43 +010074 except getopt.GetoptError as 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 = ""
Daniel P. Berrange2098c562017-01-25 16:14:14 +000080 arg_group = None
Lluís Vilanova52ef0932012-04-03 20:48:12 +020081 binary = None
82 target_type = None
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020083 target_name = None
Lluís Vilanova52ef0932012-04-03 20:48:12 +020084 probe_prefix = None
Lluís Vilanova650ab982012-04-03 20:47:39 +020085 for opt, arg in opts:
86 if opt == "--help":
87 error_opt()
88
Lluís Vilanova5b808272014-05-27 15:02:14 +020089 elif opt == "--backends":
90 arg_backends = arg.split(",")
Daniel P. Berrange2098c562017-01-25 16:14:14 +000091 elif opt == "--group":
92 arg_group = arg
Lluís Vilanova650ab982012-04-03 20:47:39 +020093 elif opt == "--format":
94 arg_format = arg
95
96 elif opt == "--list-backends":
Lluís Vilanova93fba162013-03-05 14:47:26 +010097 public_backends = tracetool.backend.get_list(only_public = True)
98 out(", ".join([ b for b,_ in public_backends ]))
Lluís Vilanova650ab982012-04-03 20:47:39 +020099 sys.exit(0)
Lluís Vilanova5b808272014-05-27 15:02:14 +0200100 elif opt == "--check-backends":
101 check_backends = True
Lluís Vilanova650ab982012-04-03 20:47:39 +0200102
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200103 elif opt == "--binary":
104 binary = arg
105 elif opt == '--target-type':
106 target_type = arg
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200107 elif opt == '--target-name':
108 target_name = arg
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200109 elif opt == '--probe-prefix':
110 probe_prefix = arg
111
Lluís Vilanova650ab982012-04-03 20:47:39 +0200112 else:
113 error_opt("unhandled option: %s" % opt)
114
Lluís Vilanova5b808272014-05-27 15:02:14 +0200115 if len(arg_backends) == 0:
116 error_opt("no backends specified")
Lluís Vilanova650ab982012-04-03 20:47:39 +0200117
Lluís Vilanova5b808272014-05-27 15:02:14 +0200118 if check_backends:
119 for backend in arg_backends:
120 if not tracetool.backend.exists(backend):
121 sys.exit(1)
122 sys.exit(0)
Lluís Vilanova650ab982012-04-03 20:47:39 +0200123
Daniel P. Berrange2098c562017-01-25 16:14:14 +0000124 if arg_group is None:
125 error_opt("group name is required")
126
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200127 if arg_format == "stap":
128 if binary is None:
129 error_opt("--binary is required for SystemTAP tapset generator")
130 if probe_prefix is None and target_type is None:
131 error_opt("--target-type is required for SystemTAP tapset generator")
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200132 if probe_prefix is None and target_name is None:
133 error_opt("--target-name is required for SystemTAP tapset generator")
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200134
135 if probe_prefix is None:
Lluís Vilanova5b808272014-05-27 15:02:14 +0200136 probe_prefix = ".".join(["qemu", target_type, target_name])
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200137
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +0000138 if len(args) < 1:
Daniel P. Berrange0bc64842016-10-04 14:35:58 +0100139 error_opt("missing trace-events filepath")
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +0000140 events = []
141 for arg in args:
142 with open(arg, "r") as fh:
Daniel P. Berrangé86b5aac2018-03-06 15:46:50 +0000143 events.extend(tracetool.read_events(fh, arg))
Daniel P. Berrange9096b782016-10-04 14:35:57 +0100144
Lluís Vilanova650ab982012-04-03 20:47:39 +0200145 try:
Daniel P. Berrange2098c562017-01-25 16:14:14 +0000146 tracetool.generate(events, arg_group, arg_format, arg_backends,
Lluís Vilanova5b808272014-05-27 15:02:14 +0200147 binary=binary, probe_prefix=probe_prefix)
Markus Armbruster86b227d2015-12-18 08:52:43 +0100148 except tracetool.TracetoolError as e:
Lluís Vilanova650ab982012-04-03 20:47:39 +0200149 error_opt(str(e))
150
151if __name__ == "__main__":
152 main(sys.argv)