1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5Simple built-in backend.
6"""
7
8__author__     = "Lluís Vilanova <vilanova@ac.upc.edu>"
9__copyright__  = "Copyright 2012-2017, 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
19PUBLIC = True
20
21
22def is_string(arg):
23    strtype = ('const char*', 'char*', 'const char *', 'char *')
24    arg_strip = arg.lstrip()
25    if arg_strip.startswith(strtype) and arg_strip.count('*') == 1:
26        return True
27    else:
28        return False
29
30
31def generate_h_begin(events, group):
32    for event in events:
33        out('void _simple_%(api)s(%(args)s);',
34            api=event.api(),
35            args=event.args)
36    out('')
37
38
39def generate_h(event, group):
40    out('    _simple_%(api)s(%(args)s);',
41        api=event.api(),
42        args=", ".join(event.args.names()))
43
44
45def generate_c_begin(events, group):
46    out('#include "qemu/osdep.h"',
47        '#include "trace/control.h"',
48        '#include "trace/simple.h"',
49        '')
50
51
52def generate_c(event, group):
53    out('void _simple_%(api)s(%(args)s)',
54        '{',
55        '    TraceBufferRecord rec;',
56        api=event.api(),
57        args=event.args)
58    sizes = []
59    for type_, name in event.args:
60        if is_string(type_):
61            out('    size_t arg%(name)s_len = %(name)s ? MIN(strlen(%(name)s), MAX_TRACE_STRLEN) : 0;',
62                name=name)
63            strsizeinfo = "4 + arg%s_len" % name
64            sizes.append(strsizeinfo)
65        else:
66            sizes.append("8")
67    sizestr = " + ".join(sizes)
68    if len(event.args) == 0:
69        sizestr = '0'
70
71    event_id = 'TRACE_' + event.name.upper()
72    if "vcpu" in event.properties:
73        # already checked on the generic format code
74        cond = "true"
75    else:
76        cond = "trace_event_get_state(%s)" % event_id
77
78    out('',
79        '    if (!%(cond)s) {',
80        '        return;',
81        '    }',
82        '',
83        '    if (trace_record_start(&rec, %(event_obj)s.id, %(size_str)s)) {',
84        '        return; /* Trace Buffer Full, Event Dropped ! */',
85        '    }',
86        cond=cond,
87        event_obj=event.api(event.QEMU_EVENT),
88        size_str=sizestr)
89
90    if len(event.args) > 0:
91        for type_, name in event.args:
92            # string
93            if is_string(type_):
94                out('    trace_record_write_str(&rec, %(name)s, arg%(name)s_len);',
95                    name=name)
96            # pointer var (not string)
97            elif type_.endswith('*'):
98                out('    trace_record_write_u64(&rec, (uintptr_t)(uint64_t *)%(name)s);',
99                    name=name)
100            # primitive data type
101            else:
102                out('    trace_record_write_u64(&rec, (uint64_t)%(name)s);',
103                   name=name)
104
105    out('    trace_record_finish(&rec);',
106        '}',
107        '')
108