blob: 5f4890f3ab0655f044ff0b24b2cdbf525eb70ee1 [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>"
9__copyright__ = "Copyright 2012, Lluís Vilanova <vilanova@ac.upc.edu>"
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
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("""\
35Usage: %(script)s --format=<format> --backend=<backend> [<options>]
36
37Backends:
38%(backends)s
39
40Formats:
41%(formats)s
42
43Options:
44 --help This help message.
45 --list-backends Print list of available backends.
46 --check-backend 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.
Lluís Vilanova52ef0932012-04-03 20:48:12 +020050 --probe-prefix <prefix> Prefix for dtrace probe names
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020051 (default: qemu-<target-type>-<target-name>).\
Lluís Vilanova650ab982012-04-03 20:47:39 +020052""" % {
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
64def main(args):
65 global _SCRIPT
66 _SCRIPT = args[0]
67
68 long_opts = [ "backend=", "format=", "help", "list-backends", "check-backend" ]
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020069 long_opts += [ "binary=", "target-type=", "target-name=", "probe-prefix=" ]
Lluís Vilanova650ab982012-04-03 20:47:39 +020070
71 try:
72 opts, args = getopt.getopt(args[1:], "", long_opts)
Stefan Hajnoczi662da382012-04-25 10:39:42 +010073 except getopt.GetoptError, err:
Lluís Vilanova650ab982012-04-03 20:47:39 +020074 error_opt(str(err))
75
76 check_backend = False
77 arg_backend = ""
78 arg_format = ""
Lluís Vilanova52ef0932012-04-03 20:48:12 +020079 binary = None
80 target_type = None
Paolo Bonzinib9a7b742013-06-04 14:45:26 +020081 target_name = None
Lluís Vilanova52ef0932012-04-03 20:48:12 +020082 probe_prefix = None
Lluís Vilanova650ab982012-04-03 20:47:39 +020083 for opt, arg in opts:
84 if opt == "--help":
85 error_opt()
86
87 elif opt == "--backend":
88 arg_backend = arg
89 elif opt == "--format":
90 arg_format = arg
91
92 elif opt == "--list-backends":
Lluís Vilanova93fba162013-03-05 14:47:26 +010093 public_backends = tracetool.backend.get_list(only_public = True)
94 out(", ".join([ b for b,_ in public_backends ]))
Lluís Vilanova650ab982012-04-03 20:47:39 +020095 sys.exit(0)
96 elif opt == "--check-backend":
97 check_backend = True
98
Lluís Vilanova52ef0932012-04-03 20:48:12 +020099 elif opt == "--binary":
100 binary = arg
101 elif opt == '--target-type':
102 target_type = arg
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200103 elif opt == '--target-name':
104 target_name = arg
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200105 elif opt == '--probe-prefix':
106 probe_prefix = arg
107
Lluís Vilanova650ab982012-04-03 20:47:39 +0200108 else:
109 error_opt("unhandled option: %s" % opt)
110
111 if arg_backend is None:
112 error_opt("backend not set")
113
114 if check_backend:
115 if tracetool.backend.exists(arg_backend):
116 sys.exit(0)
117 else:
118 sys.exit(1)
119
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200120 if arg_format == "stap":
121 if binary is None:
122 error_opt("--binary is required for SystemTAP tapset generator")
123 if probe_prefix is None and target_type is None:
124 error_opt("--target-type is required for SystemTAP tapset generator")
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200125 if probe_prefix is None and target_name is None:
126 error_opt("--target-name is required for SystemTAP tapset generator")
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200127
128 if probe_prefix is None:
Paolo Bonzinib9a7b742013-06-04 14:45:26 +0200129 probe_prefix = ".".join([ "qemu", target_type, target_name ])
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200130
Lluís Vilanova650ab982012-04-03 20:47:39 +0200131 try:
Lluís Vilanova52ef0932012-04-03 20:48:12 +0200132 tracetool.generate(sys.stdin, arg_format, arg_backend,
133 binary = binary, probe_prefix = probe_prefix)
Stefan Hajnoczi662da382012-04-25 10:39:42 +0100134 except tracetool.TracetoolError, e:
Lluís Vilanova650ab982012-04-03 20:47:39 +0200135 error_opt(str(e))
136
137if __name__ == "__main__":
138 main(sys.argv)