1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5DTrace/SystemTAP backend.
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
16from tracetool import out
17
18
19PROBEPREFIX = None
20
21def _probeprefix():
22    if PROBEPREFIX is None:
23        raise ValueError("you must set PROBEPREFIX")
24    return PROBEPREFIX
25
26
27BINARY = None
28
29def _binary():
30    if BINARY is None:
31        raise ValueError("you must set BINARY")
32    return BINARY
33
34
35def c(events):
36    pass
37
38
39def h(events):
40    out('#include "trace-dtrace.h"',
41        '')
42
43    for e in events:
44        out('static inline void trace_%(name)s(%(args)s) {',
45            '    QEMU_%(uppername)s(%(argnames)s);',
46            '}',
47            name = e.name,
48            args = e.args,
49            uppername = e.name.upper(),
50            argnames = ", ".join(e.args.names()),
51            )
52
53
54def d(events):
55    out('provider qemu {')
56
57    for e in events:
58        args = str(e.args)
59
60        # DTrace provider syntax expects foo() for empty
61        # params, not foo(void)
62        if args == 'void':
63            args = ''
64
65        # Define prototype for probe arguments
66        out('',
67            'probe %(name)s(%(args)s);',
68            name = e.name,
69            args = args,
70            )
71
72    out('',
73        '};')
74
75
76# Technically 'self' is not used by systemtap yet, but
77# they recommended we keep it in the reserved list anyway
78RESERVED_WORDS = (
79    'break', 'catch', 'continue', 'delete', 'else', 'for',
80    'foreach', 'function', 'global', 'if', 'in', 'limit',
81    'long', 'next', 'probe', 'return', 'self', 'string',
82    'try', 'while'
83    )
84
85def stap(events):
86    for e in events:
87        # Define prototype for probe arguments
88        out('probe %(probeprefix)s.%(name)s = process("%(binary)s").mark("%(name)s")',
89            '{',
90            probeprefix = _probeprefix(),
91            name = e.name,
92            binary = _binary(),
93            )
94
95        i = 1
96        if len(e.args) > 0:
97            for name in e.args.names():
98                # Append underscore to reserved keywords
99                if name in RESERVED_WORDS:
100                    name += '_'
101                out('  %s = $arg%d;' % (name, i))
102                i += 1
103
104        out('}')
105
106    out()
107