1# -*- coding: utf-8 -*-
2
3"""
4Generate .stp file that outputs simpletrace binary traces (DTrace with SystemTAP only).
5"""
6
7__author__     = "Stefan Hajnoczi <redhat.com>"
8__copyright__  = "Copyright (C) 2014, Red Hat, Inc."
9__license__    = "GPL version 2 or (at your option) any later version"
10
11__maintainer__ = "Stefan Hajnoczi"
12__email__      = "stefanha@redhat.com"
13
14
15from tracetool import out
16from tracetool.backend.dtrace import probeprefix
17from tracetool.backend.simple import is_string
18from tracetool.format.stap import stap_escape
19
20def global_var_name(name):
21    return probeprefix().replace(".", "_") + "_" + name
22
23def generate(events, backend, group):
24    out('/* This file is autogenerated by tracetool, do not edit. */',
25        '')
26
27    for event_id, e in enumerate(events):
28        if 'disable' in e.properties:
29            continue
30
31        out('probe %(probeprefix)s.simpletrace.%(name)s = %(probeprefix)s.%(name)s ?',
32            '{',
33            probeprefix=probeprefix(),
34            name=e.name)
35
36        # Calculate record size
37        sizes = ['24'] # sizeof(TraceRecord)
38        for type_, name in e.args:
39            name = stap_escape(name)
40            if is_string(type_):
41                out('    try {',
42                    '        arg%(name)s_str = %(name)s ? user_string_n(%(name)s, 512) : "<null>"',
43                    '    } catch {}',
44                    '    arg%(name)s_len = strlen(arg%(name)s_str)',
45                    name=name)
46                sizes.append('4 + arg%s_len' % name)
47            else:
48                sizes.append('8')
49        sizestr = ' + '.join(sizes)
50
51        # Generate format string and value pairs for record header and arguments
52        fields = [('8b', str(event_id)),
53                  ('8b', 'gettimeofday_ns()'),
54                  ('4b', sizestr),
55                  ('4b', 'pid()')]
56        for type_, name in e.args:
57            name = stap_escape(name)
58            if is_string(type_):
59                fields.extend([('4b', 'arg%s_len' % name),
60                               ('.*s', 'arg%s_len, arg%s_str' % (name, name))])
61            else:
62                fields.append(('8b', name))
63
64        # Emit the entire record in a single SystemTap printf()
65        fmt_str = '%'.join(fmt for fmt, _ in fields)
66        arg_str = ', '.join(arg for _, arg in fields)
67        out('    printf("%%8b%%%(fmt_str)s", 1, %(arg_str)s)',
68            fmt_str=fmt_str, arg_str=arg_str)
69
70        out('}')
71
72    out()
73