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. |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 50 | --probe-prefix <prefix> Prefix for dtrace probe names |
Paolo Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 51 | (default: qemu-<target-type>-<target-name>).\ |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 52 | """ % { |
| 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 | |
| 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", |
| 69 | "check-backends"] |
| 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) |
Stefan Hajnoczi | 662da38 | 2012-04-25 10:39:42 +0100 | [diff] [blame] | 74 | except getopt.GetoptError, 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 = "" |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 80 | binary = None |
| 81 | target_type = None |
Paolo Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 82 | target_name = None |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 83 | probe_prefix = None |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 84 | for opt, arg in opts: |
| 85 | if opt == "--help": |
| 86 | error_opt() |
| 87 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 88 | elif opt == "--backends": |
| 89 | arg_backends = arg.split(",") |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 90 | elif opt == "--format": |
| 91 | arg_format = arg |
| 92 | |
| 93 | elif opt == "--list-backends": |
Lluís Vilanova | 93fba16 | 2013-03-05 14:47:26 +0100 | [diff] [blame] | 94 | public_backends = tracetool.backend.get_list(only_public = True) |
| 95 | out(", ".join([ b for b,_ in public_backends ])) |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 96 | sys.exit(0) |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 97 | elif opt == "--check-backends": |
| 98 | check_backends = True |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 99 | |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 100 | elif opt == "--binary": |
| 101 | binary = arg |
| 102 | elif opt == '--target-type': |
| 103 | target_type = arg |
Paolo Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 104 | elif opt == '--target-name': |
| 105 | target_name = arg |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 106 | elif opt == '--probe-prefix': |
| 107 | probe_prefix = arg |
| 108 | |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 109 | else: |
| 110 | error_opt("unhandled option: %s" % opt) |
| 111 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 112 | if len(arg_backends) == 0: |
| 113 | error_opt("no backends specified") |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 114 | |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 115 | 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 Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 120 | |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 121 | 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 Bonzini | b9a7b74 | 2013-06-04 14:45:26 +0200 | [diff] [blame] | 126 | if probe_prefix is None and target_name is None: |
| 127 | error_opt("--target-name is required for SystemTAP tapset generator") |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 128 | |
| 129 | if probe_prefix is None: |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 130 | probe_prefix = ".".join(["qemu", target_type, target_name]) |
Lluís Vilanova | 52ef093 | 2012-04-03 20:48:12 +0200 | [diff] [blame] | 131 | |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 132 | try: |
Lluís Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 133 | tracetool.generate(sys.stdin, arg_format, arg_backends, |
| 134 | binary=binary, probe_prefix=probe_prefix) |
Stefan Hajnoczi | 662da38 | 2012-04-25 10:39:42 +0100 | [diff] [blame] | 135 | except tracetool.TracetoolError, e: |
Lluís Vilanova | 650ab98 | 2012-04-03 20:47:39 +0200 | [diff] [blame] | 136 | error_opt(str(e)) |
| 137 | |
| 138 | if __name__ == "__main__": |
| 139 | main(sys.argv) |