aboutsummaryrefslogtreecommitdiff
path: root/scripts/tracetool/backend/simple.py
blob: c7e47d6d72d2cbb3beafc9dd8285afca20b53f4c (plain)
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Simple built-in backend.
"""

__author__     = "Lluís Vilanova <vilanova@ac.upc.edu>"
__copyright__  = "Copyright 2012, Lluís Vilanova <vilanova@ac.upc.edu>"
__license__    = "GPL version 2 or (at your option) any later version"

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


from tracetool import out

def is_string(arg):
    strtype = ('const char*', 'char*', 'const char *', 'char *')
    if arg.lstrip().startswith(strtype):
        return True
    else:
        return False

def c(events):
    out('#include "trace.h"',
        '#include "trace/simple.h"',
        '',
        'TraceEvent trace_list[] = {')

    for e in events:
        out('{.tp_name = "%(name)s", .state=0},',
            name = e.name,
            )

    out('};',
        '')

    for num, event in enumerate(events):
        out('void trace_%(name)s(%(args)s)',
            '{',
            '    TraceBufferRecord rec;',
            name = event.name,
            args = event.args,
            )
        sizes = []
        for type_, name in event.args:
            if is_string(type_):
                out('    size_t arg%(name)s_len = %(name)s ? MIN(strlen(%(name)s), MAX_TRACE_STRLEN) : 0;',
                    name = name,
                   )
                strsizeinfo = "4 + arg%s_len" % name
                sizes.append(strsizeinfo)
            else:
                sizes.append("8")
        sizestr = " + ".join(sizes)
        if len(event.args) == 0:
            sizestr = '0'


        out('',
            '    if (!trace_list[%(event_id)s].state) {',
            '        return;',
            '    }',
            '',
            '    if (trace_record_start(&rec, %(event_id)s, %(size_str)s)) {',
            '        return; /* Trace Buffer Full, Event Dropped ! */',
            '    }',
            event_id = num,
            size_str = sizestr,
            )

        if len(event.args) > 0:
            for type_, name in event.args:
                # string
                if is_string(type_):
                    out('    trace_record_write_str(&rec, %(name)s, arg%(name)s_len);',
                        name = name,
                       )
                # pointer var (not string)
                elif type_.endswith('*'):
                    out('    trace_record_write_u64(&rec, (uint64_t)(uint64_t *)%(name)s);',
                        name = name,
                       )
                # primitive data type
                else:
                    out('    trace_record_write_u64(&rec, (uint64_t)%(name)s);',
                       name = name,
                       )

        out('    trace_record_finish(&rec);',
            '}',
            '')


def h(events):
    out('#include "trace/simple.h"',
        '')

    for event in events:
        out('void trace_%(name)s(%(args)s);',
            name = event.name,
            args = event.args,
            )
    out('')
    out('#define NR_TRACE_EVENTS %d' % len(events))
    out('extern TraceEvent trace_list[NR_TRACE_EVENTS];')