xref: /openbmc/qemu/scripts/tracetool.py (revision 2f95279a)
1#!/usr/bin/env python3
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-2014, 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@redhat.com"
14
15
16import sys
17import getopt
18
19from tracetool import error_write, out, out_open
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> --backends=<backends> [<options>] <trace-events> ... <output>
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-backends         Check if the given backend is valid.
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).
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
61def main(args):
62    global _SCRIPT
63    _SCRIPT = args[0]
64
65    long_opts = ["backends=", "format=", "help", "list-backends",
66                 "check-backends", "group="]
67    long_opts += ["binary=", "probe-prefix="]
68
69    try:
70        opts, args = getopt.getopt(args[1:], "", long_opts)
71    except getopt.GetoptError as err:
72        error_opt(str(err))
73
74    check_backends = False
75    arg_backends = []
76    arg_format = ""
77    arg_group = None
78    binary = None
79    probe_prefix = None
80    for opt, arg in opts:
81        if opt == "--help":
82            error_opt()
83
84        elif opt == "--backends":
85            arg_backends = arg.split(",")
86        elif opt == "--group":
87            arg_group = arg
88        elif opt == "--format":
89            arg_format = arg
90
91        elif opt == "--list-backends":
92            public_backends = tracetool.backend.get_list(only_public = True)
93            out(", ".join([ b for b,_ in public_backends ]))
94            sys.exit(0)
95        elif opt == "--check-backends":
96            check_backends = True
97
98        elif opt == "--binary":
99            binary = arg
100        elif opt == '--probe-prefix':
101            probe_prefix = arg
102
103        else:
104            error_opt("unhandled option: %s" % opt)
105
106    if len(arg_backends) == 0:
107        error_opt("no backends specified")
108
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)
114
115    if arg_group is None:
116        error_opt("group name is required")
117
118    if arg_format == "stap":
119        if binary is None:
120            error_opt("--binary is required for SystemTAP tapset generator")
121        if probe_prefix is None:
122            error_opt("--probe-prefix is required for SystemTAP tapset generator")
123
124    if len(args) < 2:
125        error_opt("missing trace-events and output filepaths")
126    events = []
127    for arg in args[:-1]:
128        with open(arg, "r") as fh:
129            events.extend(tracetool.read_events(fh, arg))
130
131    out_open(args[-1])
132
133    try:
134        tracetool.generate(events, arg_group, arg_format, arg_backends,
135                           binary=binary, probe_prefix=probe_prefix)
136    except tracetool.TracetoolError as e:
137        error_opt(str(e))
138
139if __name__ == "__main__":
140    main(sys.argv)
141