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