Philippe Mathieu-Daudé | 3d004a3 | 2020-01-30 17:32:25 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ |
| 5 | Command-line wrapper for the tracetool machinery. |
| 6 | """ |
| 7 | |
| 8 | __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>" |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 9 | __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>" |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 10 | __license__ = "GPL version 2 or (at your option) any later version" |
| 11 | |
| 12 | __maintainer__ = "Stefan Hajnoczi" |
Philippe Mathieu-Daudé | f892b49 | 2020-05-11 10:28:16 +0200 | [diff] [blame] | 13 | __email__ = "stefanha@redhat.com" |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 14 | |
| 15 | |
| 16 | import sys |
| 17 | import getopt |
| 18 | |
Stefan Hajnoczi | c05012a | 2020-08-27 15:29:12 +0100 | [diff] [blame] | 19 | from tracetool import error_write, out, out_open |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 20 | import tracetool.backend |
| 21 | import tracetool.format |
| 22 | |
| 23 | |
| 24 | _SCRIPT = "" |
| 25 | |
| 26 | def 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("""\ |
Stefan Hajnoczi | c05012a | 2020-08-27 15:29:12 +0100 | [diff] [blame] | 35 | Usage: %(script)s --format=<format> --backends=<backends> [<options>] <trace-events> ... <output> |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 36 | |
| 37 | Backends: |
| 38 | %(backends)s |
| 39 | |
| 40 | Formats: |
| 41 | %(formats)s |
| 42 | |
| 43 | Options: |
| 44 | --help This help message. |
| 45 | --list-backends Print list of available backends. |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 46 | --check-backends Check if the given backend is valid. |
Daniel P. Berrangé | 081340d | 2024-01-08 17:13:55 +0000 | [diff] [blame] | 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). |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 50 | """ % { |
| 51 | "script" : _SCRIPT, |
| 52 | "backends" : backend_descr, |
| 53 | "formats" : format_descr, |
| 54 | }) |
| 55 | |
| 56 | if msg is None: |
| 57 | sys.exit(0) |
| 58 | else: |
| 59 | sys.exit(1) |
| 60 | |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 61 | def main(args): |
| 62 | global _SCRIPT |
| 63 | _SCRIPT = args[0] |
| 64 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 65 | long_opts = ["backends=", "format=", "help", "list-backends", |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 66 | "check-backends", "group="] |
Daniel P. Berrangé | 081340d | 2024-01-08 17:13:55 +0000 | [diff] [blame] | 67 | long_opts += ["binary=", "probe-prefix="] |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 68 | |
| 69 | try: |
| 70 | opts, args = getopt.getopt(args[1:], "", long_opts) |
Markus Armbruster | 86b227d | 2015-12-18 08:52:43 +0100 | [diff] [blame] | 71 | except getopt.GetoptError as err: |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 72 | error_opt(str(err)) |
| 73 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 74 | check_backends = False |
| 75 | arg_backends = [] |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 76 | arg_format = "" |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 77 | arg_group = None |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 78 | binary = None |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 79 | probe_prefix = None |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 80 | for opt, arg in opts: |
| 81 | if opt == "--help": |
| 82 | error_opt() |
| 83 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 84 | elif opt == "--backends": |
| 85 | arg_backends = arg.split(",") |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 86 | elif opt == "--group": |
| 87 | arg_group = arg |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 88 | elif opt == "--format": |
| 89 | arg_format = arg |
| 90 | |
| 91 | elif opt == "--list-backends": |
Lluís Vilanova | 93fba16 | 2013-03-05 14:47:26 +0100 | [diff] [blame] | 92 | public_backends = tracetool.backend.get_list(only_public = True) |
| 93 | out(", ".join([ b for b,_ in public_backends ])) |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 94 | sys.exit(0) |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 95 | elif opt == "--check-backends": |
| 96 | check_backends = True |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 97 | |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 98 | elif opt == "--binary": |
| 99 | binary = arg |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 100 | elif opt == '--probe-prefix': |
| 101 | probe_prefix = arg |
| 102 | |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 103 | else: |
| 104 | error_opt("unhandled option: %s" % opt) |
| 105 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 106 | if len(arg_backends) == 0: |
| 107 | error_opt("no backends specified") |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 108 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 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) |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 114 | |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 115 | if arg_group is None: |
| 116 | error_opt("group name is required") |
| 117 | |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 118 | if arg_format == "stap": |
| 119 | if binary is None: |
| 120 | error_opt("--binary is required for SystemTAP tapset generator") |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 121 | if probe_prefix is None: |
Daniel P. Berrangé | 081340d | 2024-01-08 17:13:55 +0000 | [diff] [blame] | 122 | error_opt("--probe-prefix is required for SystemTAP tapset generator") |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 123 | |
Stefan Hajnoczi | c05012a | 2020-08-27 15:29:12 +0100 | [diff] [blame] | 124 | if len(args) < 2: |
| 125 | error_opt("missing trace-events and output filepaths") |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 126 | events = [] |
Stefan Hajnoczi | c05012a | 2020-08-27 15:29:12 +0100 | [diff] [blame] | 127 | for arg in args[:-1]: |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 128 | with open(arg, "r") as fh: |
Daniel P. Berrangé | 86b5aac | 2018-03-06 15:46:50 +0000 | [diff] [blame] | 129 | events.extend(tracetool.read_events(fh, arg)) |
Daniel P. Berrange | 9096b78 | 2016-10-04 14:35:57 +0100 | [diff] [blame] | 130 | |
Stefan Hajnoczi | c05012a | 2020-08-27 15:29:12 +0100 | [diff] [blame] | 131 | out_open(args[-1]) |
| 132 | |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 133 | try: |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 134 | tracetool.generate(events, arg_group, arg_format, arg_backends, |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 135 | binary=binary, probe_prefix=probe_prefix) |
Markus Armbruster | 86b227d | 2015-12-18 08:52:43 +0100 | [diff] [blame] | 136 | except tracetool.TracetoolError as e: |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 137 | error_opt(str(e)) |
| 138 | |
| 139 | if __name__ == "__main__": |
| 140 | main(sys.argv) |