Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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" |
| 13 | __email__ = "stefanha@linux.vnet.ibm.com" |
| 14 | |
| 15 | |
| 16 | import sys |
| 17 | import getopt |
| 18 | |
| 19 | from tracetool import error_write, out |
| 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("""\ |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 35 | Usage: %(script)s --format=<format> --backends=<backends> [<options>] |
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. |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 47 | --binary <path> Full path to QEMU binary. |
| 48 | --target-type <type> QEMU emulator target type ('system' or 'user'). |
Paolo Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 49 | --target-name <name> QEMU emulator target name. |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 50 | --group <name> Name of the event group |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 51 | --probe-prefix <prefix> Prefix for dtrace probe names |
Paolo Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 52 | (default: qemu-<target-type>-<target-name>).\ |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 53 | """ % { |
| 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 Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 64 | def main(args): |
| 65 | global _SCRIPT |
| 66 | _SCRIPT = args[0] |
| 67 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 68 | long_opts = ["backends=", "format=", "help", "list-backends", |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 69 | "check-backends", "group="] |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 70 | long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="] |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 71 | |
| 72 | try: |
| 73 | opts, args = getopt.getopt(args[1:], "", long_opts) |
Markus Armbruster | 86b227d | 2015-12-18 08:52:43 +0100 | [diff] [blame] | 74 | except getopt.GetoptError as err: |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 75 | error_opt(str(err)) |
| 76 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 77 | check_backends = False |
| 78 | arg_backends = [] |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 79 | arg_format = "" |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 80 | arg_group = None |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 81 | binary = None |
| 82 | target_type = None |
Paolo Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 83 | target_name = None |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 84 | probe_prefix = None |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 85 | for opt, arg in opts: |
| 86 | if opt == "--help": |
| 87 | error_opt() |
| 88 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 89 | elif opt == "--backends": |
| 90 | arg_backends = arg.split(",") |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 91 | elif opt == "--group": |
| 92 | arg_group = arg |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 93 | elif opt == "--format": |
| 94 | arg_format = arg |
| 95 | |
| 96 | elif opt == "--list-backends": |
Lluís Vilanova | 93fba16 | 2013-03-05 14:47:26 +0100 | [diff] [blame] | 97 | public_backends = tracetool.backend.get_list(only_public = True) |
| 98 | out(", ".join([ b for b,_ in public_backends ])) |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 99 | sys.exit(0) |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 100 | elif opt == "--check-backends": |
| 101 | check_backends = True |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 102 | |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 103 | elif opt == "--binary": |
| 104 | binary = arg |
| 105 | elif opt == '--target-type': |
| 106 | target_type = arg |
Paolo Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 107 | elif opt == '--target-name': |
| 108 | target_name = arg |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 109 | elif opt == '--probe-prefix': |
| 110 | probe_prefix = arg |
| 111 | |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 112 | else: |
| 113 | error_opt("unhandled option: %s" % opt) |
| 114 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 115 | if len(arg_backends) == 0: |
| 116 | error_opt("no backends specified") |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 117 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 118 | 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 Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 123 | |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 124 | if arg_group is None: |
| 125 | error_opt("group name is required") |
| 126 | |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 127 | 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 Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 132 | if probe_prefix is None and target_name is None: |
| 133 | error_opt("--target-name is required for SystemTAP tapset generator") |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 134 | |
| 135 | if probe_prefix is None: |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 136 | probe_prefix = ".".join(["qemu", target_type, target_name]) |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 137 | |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 138 | if len(args) < 1: |
Daniel P. Berrange | 0bc6484 | 2016-10-04 14:35:58 +0100 | [diff] [blame] | 139 | error_opt("missing trace-events filepath") |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 140 | events = [] |
| 141 | for arg in args: |
| 142 | with open(arg, "r") as fh: |
Daniel P. Berrangé | 86b5aac | 2018-03-06 15:46:50 +0000 | [diff] [blame] | 143 | events.extend(tracetool.read_events(fh, arg)) |
Daniel P. Berrange | 9096b78 | 2016-10-04 14:35:57 +0100 | [diff] [blame] | 144 | |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 145 | try: |
Daniel P. Berrange | 2098c56 | 2017-01-25 16:14:14 +0000 | [diff] [blame] | 146 | tracetool.generate(events, arg_group, arg_format, arg_backends, |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 147 | binary=binary, probe_prefix=probe_prefix) |
Markus Armbruster | 86b227d | 2015-12-18 08:52:43 +0100 | [diff] [blame] | 148 | except tracetool.TracetoolError as e: |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 149 | error_opt(str(e)) |
| 150 | |
| 151 | if __name__ == "__main__": |
| 152 | main(sys.argv) |