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_h_backend_dstate(event, group):
46    out('    trace_event_get_state_dynamic_by_id(%(event_id)s) || \\',
47        event_id="TRACE_" + event.name.upper())
48
49
50def generate_c_begin(events, group):
51    out('#include "qemu/osdep.h"',
52        '#include "trace/control.h"',
53        '#include "trace/simple.h"',
54        '')
55
56
57def generate_c(event, group):
58    out('void _simple_%(api)s(%(args)s)',
59        '{',
60        '    TraceBufferRecord rec;',
61        api=event.api(),
62        args=event.args)
63    sizes = []
64    for type_, name in event.args:
65        if is_string(type_):
66            out('    size_t arg%(name)s_len = %(name)s ? MIN(strlen(%(name)s), MAX_TRACE_STRLEN) : 0;',
67                name=name)
68            strsizeinfo = "4 + arg%s_len" % name
69            sizes.append(strsizeinfo)
70        else:
71            sizes.append("8")
72    sizestr = " + ".join(sizes)
73    if len(event.args) == 0:
74        sizestr = '0'
75
76    event_id = 'TRACE_' + event.name.upper()
77    if "vcpu" in event.properties:
78        # already checked on the generic format code
79        cond = "true"
80    else:
81        cond = "trace_event_get_state(%s)" % event_id
82
83    out('',
84        '    if (!%(cond)s) {',
85        '        return;',
86        '    }',
87        '',
88        '    if (trace_record_start(&rec, %(event_obj)s.id, %(size_str)s)) {',
89        '        return; /* Trace Buffer Full, Event Dropped ! */',
90        '    }',
91        cond=cond,
92        event_obj=event.api(event.QEMU_EVENT),
93        size_str=sizestr)
94
95    if len(event.args) > 0:
96        for type_, name in event.args:
97            # string
98            if is_string(type_):
99                out('    trace_record_write_str(&rec, %(name)s, arg%(name)s_len);',
100                    name=name)
101            # pointer var (not string)
102            elif type_.endswith('*'):
103                out('    trace_record_write_u64(&rec, (uintptr_t)(uint64_t *)%(name)s);',
104                    name=name)
105            # primitive data type
106            else:
107                out('    trace_record_write_u64(&rec, (uint64_t)%(name)s);',
108                   name=name)
109
110    out('    trace_record_finish(&rec);',
111        '}',
112        '')
113