ac864f38ce2500ff4802c59bde8f409ca62578e2
[sdk/emulator/qemu.git] / scripts / tracetool / backend / simple.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Simple built-in 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
16 from tracetool import out
17
18
19 PUBLIC = True
20
21
22 def 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 def c(events):
30     out('#include "trace.h"',
31         '#include "trace/simple.h"',
32         '',
33         'TraceEvent trace_list[] = {')
34
35     for e in events:
36         out('{.tp_name = "%(name)s", .state=0},',
37             name = e.name,
38             )
39
40     out('};',
41         '')
42
43     for num, event in enumerate(events):
44         out('void trace_%(name)s(%(args)s)',
45             '{',
46             '    TraceBufferRecord rec;',
47             name = event.name,
48             args = event.args,
49             )
50         sizes = []
51         for type_, name in event.args:
52             if is_string(type_):
53                 out('    size_t arg%(name)s_len = %(name)s ? MIN(strlen(%(name)s), MAX_TRACE_STRLEN) : 0;',
54                     name = name,
55                    )
56                 strsizeinfo = "4 + arg%s_len" % name
57                 sizes.append(strsizeinfo)
58             else:
59                 sizes.append("8")
60         sizestr = " + ".join(sizes)
61         if len(event.args) == 0:
62             sizestr = '0'
63
64
65         out('',
66             '    if (!trace_list[%(event_id)s].state) {',
67             '        return;',
68             '    }',
69             '',
70             '    if (trace_record_start(&rec, %(event_id)s, %(size_str)s)) {',
71             '        return; /* Trace Buffer Full, Event Dropped ! */',
72             '    }',
73             event_id = num,
74             size_str = sizestr,
75             )
76
77         if len(event.args) > 0:
78             for type_, name in event.args:
79                 # string
80                 if is_string(type_):
81                     out('    trace_record_write_str(&rec, %(name)s, arg%(name)s_len);',
82                         name = name,
83                        )
84                 # pointer var (not string)
85                 elif type_.endswith('*'):
86                     out('    trace_record_write_u64(&rec, (uintptr_t)(uint64_t *)%(name)s);',
87                         name = name,
88                        )
89                 # primitive data type
90                 else:
91                     out('    trace_record_write_u64(&rec, (uint64_t)%(name)s);',
92                        name = name,
93                        )
94
95         out('    trace_record_finish(&rec);',
96             '}',
97             '')
98
99
100 def h(events):
101     out('#include "trace/simple.h"',
102         '')
103
104     for event in events:
105         out('void trace_%(name)s(%(args)s);',
106             name = event.name,
107             args = event.args,
108             )
109     out('')
110     out('#define NR_TRACE_EVENTS %d' % len(events))
111     out('extern TraceEvent trace_list[NR_TRACE_EVENTS];')