tracetool.py 4.1 KB
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Command-line wrapper for the tracetool machinery.
"""

__author__     = "Lluís Vilanova <vilanova@ac.upc.edu>"
L
Lluís Vilanova 已提交
9
__copyright__  = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
__license__    = "GPL version 2 or (at your option) any later version"

__maintainer__ = "Stefan Hajnoczi"
__email__      = "stefanha@linux.vnet.ibm.com"


import sys
import getopt

from tracetool import error_write, out
import tracetool.backend
import tracetool.format


_SCRIPT = ""

def error_opt(msg = None):
    if msg is not None:
        error_write("Error: " + msg + "\n")

    backend_descr = "\n".join([ "    %-15s %s" % (n, d)
                                for n,d in tracetool.backend.get_list() ])
    format_descr = "\n".join([ "    %-15s %s" % (n, d)
                               for n,d in tracetool.format.get_list() ])
    error_write("""\
L
Lluís Vilanova 已提交
35
Usage: %(script)s --format=<format> --backends=<backends> [<options>]
36 37 38 39 40 41 42 43 44 45

Backends:
%(backends)s

Formats:
%(formats)s

Options:
    --help                   This help message.
    --list-backends          Print list of available backends.
L
Lluís Vilanova 已提交
46
    --check-backends         Check if the given backend is valid.
47 48
    --binary <path>          Full path to QEMU binary.
    --target-type <type>     QEMU emulator target type ('system' or 'user').
P
Paolo Bonzini 已提交
49
    --target-name <name>     QEMU emulator target name.
50
    --probe-prefix <prefix>  Prefix for dtrace probe names
P
Paolo Bonzini 已提交
51
                             (default: qemu-<target-type>-<target-name>).\
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
""" % {
            "script" : _SCRIPT,
            "backends" : backend_descr,
            "formats" : format_descr,
            })

    if msg is None:
        sys.exit(0)
    else:
        sys.exit(1)


def main(args):
    global _SCRIPT
    _SCRIPT = args[0]

L
Lluís Vilanova 已提交
68 69 70
    long_opts = ["backends=", "format=", "help", "list-backends",
                 "check-backends"]
    long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="]
71 72 73

    try:
        opts, args = getopt.getopt(args[1:], "", long_opts)
74
    except getopt.GetoptError as err:
75 76
        error_opt(str(err))

L
Lluís Vilanova 已提交
77 78
    check_backends = False
    arg_backends = []
79
    arg_format = ""
80 81
    binary = None
    target_type = None
P
Paolo Bonzini 已提交
82
    target_name = None
83
    probe_prefix = None
84 85 86 87
    for opt, arg in opts:
        if opt == "--help":
            error_opt()

L
Lluís Vilanova 已提交
88 89
        elif opt == "--backends":
            arg_backends = arg.split(",")
90 91 92 93
        elif opt == "--format":
            arg_format = arg

        elif opt == "--list-backends":
94 95
            public_backends = tracetool.backend.get_list(only_public = True)
            out(", ".join([ b for b,_ in public_backends ]))
96
            sys.exit(0)
L
Lluís Vilanova 已提交
97 98
        elif opt == "--check-backends":
            check_backends = True
99

100 101 102 103
        elif opt == "--binary":
            binary = arg
        elif opt == '--target-type':
            target_type = arg
P
Paolo Bonzini 已提交
104 105
        elif opt == '--target-name':
            target_name = arg
106 107 108
        elif opt == '--probe-prefix':
            probe_prefix = arg

109 110 111
        else:
            error_opt("unhandled option: %s" % opt)

L
Lluís Vilanova 已提交
112 113
    if len(arg_backends) == 0:
        error_opt("no backends specified")
114

L
Lluís Vilanova 已提交
115 116 117 118 119
    if check_backends:
        for backend in arg_backends:
            if not tracetool.backend.exists(backend):
                sys.exit(1)
        sys.exit(0)
120

121 122 123 124 125
    if arg_format == "stap":
        if binary is None:
            error_opt("--binary is required for SystemTAP tapset generator")
        if probe_prefix is None and target_type is None:
            error_opt("--target-type is required for SystemTAP tapset generator")
P
Paolo Bonzini 已提交
126 127
        if probe_prefix is None and target_name is None:
            error_opt("--target-name is required for SystemTAP tapset generator")
128 129

        if probe_prefix is None:
L
Lluís Vilanova 已提交
130
            probe_prefix = ".".join(["qemu", target_type, target_name])
131

132 133 134 135
    if len(args) != 1:
        error_opt("missing trace-events filepath")
    with open(args[0], "r") as fh:
        events = tracetool.read_events(fh)
136

137
    try:
138
        tracetool.generate(events, arg_format, arg_backends,
L
Lluís Vilanova 已提交
139
                           binary=binary, probe_prefix=probe_prefix)
140
    except tracetool.TracetoolError as e:
141 142 143 144
        error_opt(str(e))

if __name__ == "__main__":
    main(sys.argv)