| 1 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | |
| 3 | """ |
| 4 | Generate .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 | |
| 15 | from tracetool import out |
| 16 | from tracetool.backend.dtrace import probeprefix |
| 17 | from tracetool.backend.simple import is_string |
| 18 | from tracetool.format.stap import stap_escape |
| 19 | |
| 20 | |
| 21 | def generate(events, backend, group): |
| 22 | out('/* This file is autogenerated by tracetool, do not edit. */', |
| 23 | '/* SPDX-License-Identifier: GPL-2.0-or-later */', |
| 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("%%8b%%%(fmt_str)s", 1, %(arg_str)s)', |
| 67 | fmt_str=fmt_str, arg_str=arg_str) |
| 68 | |
| 69 | out('}') |
| 70 | |
| 71 | out() |