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-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@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    if arg.lstrip().startswith(strtype):
25        return True
26    else:
27        return False
28
29
30def generate_h_begin(events):
31    for event in events:
32        out('void _simple_%(api)s(%(args)s);',
33            api=event.api(),
34            args=event.args)
35    out('')
36
37
38def generate_h(event):
39    out('    _simple_%(api)s(%(args)s);',
40        api=event.api(),
41        args=", ".join(event.args.names()))
42
43
44def generate_c_begin(events):
45    out('#include "qemu/osdep.h"',
46        '#include "trace.h"',
47        '#include "trace/control.h"',
48        '#include "trace/simple.h"',
49        '')
50
51
52def generate_c(event):
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
72    out('',
73        '    if (!trace_event_get_state(%(event_id)s)) {',
74        '        return;',
75        '    }',
76        '',
77        '    if (trace_record_start(&rec, %(event_id)s, %(size_str)s)) {',
78        '        return; /* Trace Buffer Full, Event Dropped ! */',
79        '    }',
80        event_id='TRACE_' + event.name.upper(),
81        size_str=sizestr)
82
83    if len(event.args) > 0:
84        for type_, name in event.args:
85            # string
86            if is_string(type_):
87                out('    trace_record_write_str(&rec, %(name)s, arg%(name)s_len);',
88                    name=name)
89            # pointer var (not string)
90            elif type_.endswith('*'):
91                out('    trace_record_write_u64(&rec, (uintptr_t)(uint64_t *)%(name)s);',
92                    name=name)
93            # primitive data type
94            else:
95                out('    trace_record_write_u64(&rec, (uint64_t)%(name)s);',
96                   name=name)
97
98    out('    trace_record_finish(&rec);',
99        '}',
100        '')
101