trace: add tracetool simpletrace_stap format
[qemu.git] / scripts / tracetool / format / simpletrace_stap.py
blob7e44bc1811387ad8c9f084423e750ff2aaf5f976
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """
5 Generate .stp file that outputs simpletrace binary traces (DTrace with SystemTAP only).
6 """
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"
12 __maintainer__ = "Stefan Hajnoczi"
13 __email__ = "stefanha@redhat.com"
16 from tracetool import out
17 from tracetool.backend.dtrace import binary, probeprefix
18 from tracetool.backend.simple import is_string
19 from tracetool.format.stap import stap_escape
22 def generate(events, backend):
23 out('/* This file is autogenerated by tracetool, do not edit. */',
24 '')
26 for event_id, e in enumerate(events):
27 if 'disable' in e.properties:
28 continue
30 out('probe %(probeprefix)s.simpletrace.%(name)s = %(probeprefix)s.%(name)s ?',
31 '{',
32 probeprefix=probeprefix(),
33 name=e.name)
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)
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))
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)
69 out('}')
71 out()