Rewrite tracetool using python modules
v5 cover Rewrite tracetool using python modules
A full rewrite of the tracetool script using per-format and per-backend modules, so that it's easier to read and extend it in the future.
---
NOTE: This series applies in current master, ignoring the "Rewrite tracetool
using python" series.
Changes in v5:
* De-uglify the uses of 'tracetool.out' by adding support for mapping variables.
* Remove stale PUBLIC variables in backend modules.
Changes in v4:
* Some code cleanups according to comments from Stefan Hajnoczi:
** Removed the "public backend" concept (will be introduced only when necessary). ** Moved 'tracetool.generate' from "kwargs" to explicitly named arguments.
Changes in v3:
* Some minor fixes according to comments from Alon Levy:
** Fixed some rogue tabs.
** Fixed definition of '_SCRIPT' in "tracetool.py".
** Fixed some documentation.
** Defensively treat results from 'try_import' in 'get_list'. ** Fixed the no-arguments case in 'tracetool.backend.dtrace.d'. ** Fixed the commandline checks for the "stap" format. ** Added '__repr__' methods for both Event and Arguments objects.
Changes in v2:
* Fixed a strange import error.
* Add a pointer to 'tracetool.out' in the format and backend documentation.
Lluís Vilanova (8):
tracetool: Rewrite infrastructure as python modules
tracetool: Add module for the 'c' format
tracetool: Add module for the 'h' format
tracetool: Add support for the 'stderr' backend
tracetool: Add support for the 'simple' backend
tracetool: Add support for the 'ust' backend
tracetool: Add support for the 'dtrace' backend
tracetool: Add MAINTAINERS info
MAINTAINERS | 2 Makefile.objs | 6 Makefile.target | 13 - configure | 4 scripts/tracetool | 648 --------------------------------- scripts/tracetool.py | 138 +++++++ scripts/tracetool/__init__.py | 236 ++++++++++++ scripts/tracetool/backend/__init__.py | 105 +++++ scripts/tracetool/backend/dtrace.py | 97 +++++ scripts/tracetool/backend/simple.py | 55 +++ scripts/tracetool/backend/stderr.py | 56 +++ scripts/tracetool/backend/ust.py | 90 +++++ scripts/tracetool/format/__init__.py | 96 +++++ scripts/tracetool/format/c.py | 20 + scripts/tracetool/format/d.py | 20 + scripts/tracetool/format/h.py | 45 ++ scripts/tracetool/format/stap.py | 20 + 17 files changed, 992 insertions(+), 659 deletions(-) delete mode 100755 scripts/tracetool create mode 100755 scripts/tracetool.py create mode 100644 scripts/tracetool/__init__.py create mode 100644 scripts/tracetool/backend/__init__.py create mode 100644 scripts/tracetool/backend/dtrace.py create mode 100644 scripts/tracetool/backend/simple.py create mode 100644 scripts/tracetool/backend/stderr.py create mode 100644 scripts/tracetool/backend/ust.py create mode 100644 scripts/tracetool/format/__init__.py create mode 100644 scripts/tracetool/format/c.py create mode 100644 scripts/tracetool/format/d.py create mode 100644 scripts/tracetool/format/h.py create mode 100644 scripts/tracetool/format/stap.py
PATCH v5 1/8 tracetool: Rewrite infrastructure as python modules
--- Makefile.objs | 6 Makefile.target | 13 - configure | 4 scripts/tracetool | 648 --------------------------------- scripts/tracetool.py | 108 ++++++ scripts/tracetool/__init__.py | 227 ++++++++++++ scripts/tracetool/backend/__init__.py | 105 +++++ scripts/tracetool/format/__init__.py | 96 +++++ 8 files changed, 548 insertions(+), 659 deletions(-) delete mode 100755 scripts/tracetool create mode 100755 scripts/tracetool.py create mode 100644 scripts/tracetool/__init__.py create mode 100644 scripts/tracetool/backend/__init__.py create mode 100644 scripts/tracetool/format/__init__.py
Makefile.objs +3 -3
@@ -357,12 +357,12 @@ else trace.h: trace.h-timestamp endif trace.h-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak - $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -h < $< > $@," GEN trace.h") + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/tracetool.py --format=h --backend=$(TRACE_BACKEND) < $< > $@," GEN trace.h") @cmp -s $@ trace.h || cp $@ trace.h trace.c: trace.c-timestamp trace.c-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak - $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -c < $< > $@," GEN trace.c") + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/tracetool.py --format=c --backend=$(TRACE_BACKEND) < $< > $@," GEN trace.c") @cmp -s $@ trace.c || cp $@ trace.c trace.o: trace.c $(GENERATED_HEADERS)
@@ -375,7 +375,7 @@ trace-dtrace.h: trace-dtrace.dtrace # rule file. So we use '.dtrace' instead trace-dtrace.dtrace: trace-dtrace.dtrace-timestamp trace-dtrace.dtrace-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak - $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -d < $< > $@," GEN trace-dtrace.dtrace") + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/tracetool.py --format=d --backend=$(TRACE_BACKEND) < $< > $@," GEN trace-dtrace.dtrace") @cmp -s $@ trace-dtrace.dtrace || cp $@ trace-dtrace.dtrace trace-dtrace.o: trace-dtrace.dtrace $(GENERATED_HEADERS)
Makefile.target +7 -6
@@ -59,12 +59,13 @@ TARGET_TYPE=system endif $(QEMU_PROG).stp: - $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool \ - --$(TRACE_BACKEND) \ - --binary $(bindir)/$(QEMU_PROG) \ - --target-arch $(TARGET_ARCH) \ - --target-type $(TARGET_TYPE) \ - --stap < $(SRC_PATH)/trace-events > $(QEMU_PROG).stp," GEN $(QEMU_PROG).stp") + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/tracetool.py \ + --format=stap \ + --backend=$(TRACE_BACKEND) \ + --binary=$(bindir)/$(QEMU_PROG) \ + --target-arch=$(TARGET_ARCH) \ + --target-type=$(TARGET_TYPE) \ + < $(SRC_PATH)/trace-events > $(QEMU_PROG).stp," GEN $(QEMU_PROG).stp") else stap: endif
configure +2 -2
@@ -1097,7 +1097,7 @@ echo " --disable-docs disable documentation build" echo " --disable-vhost-net disable vhost-net acceleration support" echo " --enable-vhost-net enable vhost-net acceleration support" echo " --enable-trace-backend=B Set trace backend" -echo " Available backends:" $("$source_path"/scripts/tracetool --list-backends) +echo " Available backends:" $($python "$source_path"/scripts/tracetool.py --list-backends) echo " --with-trace-file=NAME Full PATH,NAME of file to store traces" echo " Default:trace-<pid>" echo " --disable-spice disable spice"
@@ -2654,7 +2654,7 @@ fi ########################################## # check if trace backend exists -sh "$source_path/scripts/tracetool" "--$trace_backend" --check-backend > /dev/null 2> /dev/null +$python "$source_path/scripts/tracetool.py" "--backend=$trace_backend" --check-backend > /dev/null 2> /dev/null if test "$?" -ne 0 ; then echo echo "Error: invalid trace backend"
scripts/tracetool deleted +0 -648
648 lines not on this page · raw message
scripts/tracetool.py new file +108 -0
108 lines not on this page · raw message
scripts/tracetool/__init__.py new file +227 -0
227 lines not on this page · raw message
scripts/tracetool/backend/__init__.py new file +105 -0
105 lines not on this page · raw message
scripts/tracetool/format/__init__.py new file +96 -0
96 lines not on this page · raw message
PATCH v5 2/8 tracetool: Add module for the 'c' format
In reply to Lluís Vilanova--- scripts/tracetool/format/c.py | 20 ++++++++++++++++++++ 1 files changed, 20 insertions(+), 0 deletions(-) create mode 100644 scripts/tracetool/format/c.py
scripts/tracetool/format/c.py new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Generate .c file. +""" + +__author__ = "Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__license__ = "GPL version 2 or (at your option) any later version" + +__maintainer__ = "Stefan Hajnoczi" +__email__ = "[email protected]" + + +from tracetool import out + + +def begin(events): + out('/* This file is autogenerated by tracetool, do not edit. */')
PATCH v5 3/8 tracetool: Add module for the 'h' format
In reply to Lluís Vilanova--- scripts/tracetool/format/h.py | 45 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 45 insertions(+), 0 deletions(-) create mode 100644 scripts/tracetool/format/h.py
scripts/tracetool/format/h.py new file +45 -0
@@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Generate .h file. +""" + +__author__ = "Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__license__ = "GPL version 2 or (at your option) any later version" + +__maintainer__ = "Stefan Hajnoczi" +__email__ = "[email protected]" + + +from tracetool import out + + +def begin(events): + out('/* This file is autogenerated by tracetool, do not edit. */', + '', + '#ifndef TRACE_H', + '#define TRACE_H', + '', + '#include "qemu-common.h"') + +def end(events): + for e in events: + if "disable" in e.properties: + enabled = 0 + else: + enabled = 1 + out('#define TRACE_%s_ENABLED %d' % (e.name.upper(), enabled)) + out('', + '#endif /* TRACE_H */') + +def nop(events): + for e in events: + out('', + 'static inline void trace_%(name)s(%(args)s)' % { + '{', + '}', + name = e.name, + args = e.args, + )
PATCH v5 4/8 tracetool: Add support for the 'stderr' backend
In reply to Lluís Vilanova--- scripts/tracetool/backend/stderr.py | 56 +++++++++++++++++++++++++++++++++++ 1 files changed, 56 insertions(+), 0 deletions(-) create mode 100644 scripts/tracetool/backend/stderr.py
scripts/tracetool/backend/stderr.py new file +56 -0
@@ -0,0 +1,56 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Stderr built-in backend. +""" + +__author__ = "Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__license__ = "GPL version 2 or (at your option) any later version" + +__maintainer__ = "Stefan Hajnoczi" +__email__ = "[email protected]" + + +from tracetool import out + + +def c(events): + out('#include "trace.h"', + '', + 'TraceEvent trace_list[] = {') + + for e in events: + out('{.tp_name = "%(name)s", .state=0},', + name = e.name, + ) + + out('};') + +def h(events): + out('#include <stdio.h>', + '#include "trace/stderr.h"', + '', + 'extern TraceEvent trace_list[];') + + for num, e in enumerate(events): + argnames = ", ".join(e.args.names()) + if len(e.args) > 0: + argnames = ", " + argnames + + out('static inline void trace_%(name)s(%(args)s)', + '{', + ' if (trace_list[%(event_num)s].state != 0) {', + ' fprintf(stderr, "%(name)s " %(fmt)s "\\n" %(argnames)s);', + ' }', + '}', + name = e.name, + args = e.args, + event_num = num, + fmt = e.fmt, + argnames = argnames, + ) + + out('', + '#define NR_TRACE_EVENTS %d' % len(events))
PATCH v5 5/8 tracetool: Add support for the 'simple' backend
In reply to Lluís Vilanova--- scripts/tracetool/backend/simple.py | 55 +++++++++++++++++++++++++++++++++++ 1 files changed, 55 insertions(+), 0 deletions(-) create mode 100644 scripts/tracetool/backend/simple.py
scripts/tracetool/backend/simple.py new file +55 -0
@@ -0,0 +1,55 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Simple built-in backend. +""" + +__author__ = "Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__license__ = "GPL version 2 or (at your option) any later version" + +__maintainer__ = "Stefan Hajnoczi" +__email__ = "[email protected]" + + +from tracetool import out + + +def c(events): + out('#include "trace.h"', + '', + 'TraceEvent trace_list[] = {') + + for e in events: + out('{.tp_name = "%(name)s", .state=0},', + name = e.name, + ) + + out('};') + +def h(events): + out('#include "trace/simple.h"', + '') + + for num, e in enumerate(events): + if len(e.args): + argstr = e.args.names() + arg_prefix = ', (uint64_t)(uintptr_t)' + cast_args = arg_prefix + arg_prefix.join(argstr) + simple_args = (str(num) + cast_args) + else: + simple_args = str(num) + + out('static inline void trace_%(name)s(%(args)s)', + '{', + ' trace%(argc)d(%(trace_args)s);', + '}', + name = e.name, + args = e.args, + argc = len(e.args), + trace_args = simple_args, + ) + + out('#define NR_TRACE_EVENTS %d' % len(events)) + out('extern TraceEvent trace_list[NR_TRACE_EVENTS];')
PATCH v5 6/8 tracetool: Add support for the 'ust' backend
In reply to Lluís Vilanova--- scripts/tracetool/backend/ust.py | 90 ++++++++++++++++++++++++++++++++++++++ 1 files changed, 90 insertions(+), 0 deletions(-) create mode 100644 scripts/tracetool/backend/ust.py
scripts/tracetool/backend/ust.py new file +90 -0
@@ -0,0 +1,90 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +LTTng User Space Tracing backend. +""" + +__author__ = "Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__license__ = "GPL version 2 or (at your option) any later version" + +__maintainer__ = "Stefan Hajnoczi" +__email__ = "[email protected]" + + +from tracetool import out + + +def c(events): + out('#include <ust/marker.h>', + '#undef mutex_lock', + '#undef mutex_unlock', + '#undef inline', + '#undef wmb', + '#include "trace.h"') + + for e in events: + argnames = ", ".join(e.args.names()) + if len(e.args) > 0: + argnames = ', ' + argnames + + out('DEFINE_TRACE(ust_%(name)s);', + '', + 'static void ust_%(name)s_probe(%(args)s)', + '{', + ' trace_mark(ust, %(name)s, %(fmt)s%(argnames)s);', + '}', + name = e.name, + args = e.args, + fmt = e.fmt, + argnames = argnames, + ) + + else: + out('DEFINE_TRACE(ust_%(name)s);', + '', + 'static void ust_%(name)s_probe(%(args)s)', + '{', + ' trace_mark(ust, %(name)s, UST_MARKER_NOARGS);', + '}', + name = e.name, + args = e.args, + ) + + # register probes + out('', + 'static void __attribute__((constructor)) trace_init(void)', + '{') + + for e in events: + out(' register_trace_ust_%(name)s(ust_%(name)s_probe);', + name = e.name, + ) + + out('}') + + +def h(events): + out('#include <ust/tracepoint.h>', + '#undef mutex_lock', + '#undef mutex_unlock', + '#undef inline', + '#undef wmb') + + for e in events: + if len(e.args) > 0: + out('DECLARE_TRACE(ust_%(name)s, TP_PROTO(%(args)s), TP_ARGS(%(argnames)s));', + '#define trace_%(name)s trace_ust_%(name)s', + name = e.name, + args = e.args, + argnames = ", ".join(e.args.names()), + ) + + else: + out('_DECLARE_TRACEPOINT_NOARGS(ust_%(name)s);', + '#define trace_%(name)s trace_ust_%(name)s', + name = e.name, + ) + + out()
PATCH v5 7/8 tracetool: Add support for the 'dtrace' backend
In reply to Lluís Vilanova--- scripts/tracetool.py | 32 +++++++++++- scripts/tracetool/__init__.py | 11 ++++ scripts/tracetool/backend/dtrace.py | 97 +++++++++++++++++++++++++++++++++++ scripts/tracetool/format/d.py | 20 +++++++ scripts/tracetool/format/stap.py | 20 +++++++ 5 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 scripts/tracetool/backend/dtrace.py create mode 100644 scripts/tracetool/format/d.py create mode 100644 scripts/tracetool/format/stap.py
scripts/tracetool.py +31 -1
@@ -44,6 +44,11 @@ Options: --help This help message. --list-backends Print list of available backends. --check-backend Check if the given backend is valid. + --binary <path> Full path to QEMU binary. + --target-type <type> QEMU emulator target type ('system' or 'user'). + --target-arch <arch> QEMU emulator target arch. + --probe-prefix <prefix> Prefix for dtrace probe names + (default: qemu-<target-type>-<target-arch>).\ """ % { "script" : _SCRIPT, "backends" : backend_descr,
@@ -71,6 +76,10 @@ def main(args): check_backend = False arg_backend = "" arg_format = "" + binary = None + target_type = None + target_arch = None + probe_prefix = None for opt, arg in opts: if opt == "--help": error_opt()
@@ -87,6 +96,15 @@ def main(args): elif opt == "--check-backend": check_backend = True + elif opt == "--binary": + binary = arg + elif opt == '--target-type': + target_type = arg + elif opt == '--target-arch': + target_arch = arg + elif opt == '--probe-prefix': + probe_prefix = arg + else: error_opt("unhandled option: %s" % opt)
@@ -99,8 +117,20 @@ def main(args): else: sys.exit(1) + if arg_format == "stap": + if binary is None: + error_opt("--binary is required for SystemTAP tapset generator") + if probe_prefix is None and target_type is None: + error_opt("--target-type is required for SystemTAP tapset generator") + if probe_prefix is None and target_arch is None: + error_opt("--target-arch is required for SystemTAP tapset generator") + + if probe_prefix is None: + probe_prefix = ".".join([ "qemu", target_type, target_arch ]) + try: - tracetool.generate(sys.stdin, arg_format, arg_backend) + tracetool.generate(sys.stdin, arg_format, arg_backend, + binary = binary, probe_prefix = probe_prefix) except tracetool.TracetoolError as e: error_opt(str(e))
scripts/tracetool/__init__.py +10 -1
@@ -177,7 +177,8 @@ def try_import(mod_name, attr_name = None, attr_default = None): return False, None -def generate(fevents, format, backend): +def generate(fevents, format, backend, + binary = None, probe_prefix = None): """Generate the output for the given (format, backend) pair. Parameters
@@ -188,6 +189,10 @@ def generate(fevents, format, backend): Output format name. backend : str Output backend name. + binary : str or None + See tracetool.backend.dtrace.BINARY. + probe_prefix : str or None + See tracetool.backend.dtrace.PROBEPREFIX. """ # fix strange python error (UnboundLocalError tracetool) import tracetool
@@ -210,6 +215,10 @@ def generate(fevents, format, backend): raise TracetoolError("backend '%s' not compatible with format '%s'" % (backend, format)) + import tracetool.backend.dtrace + tracetool.backend.dtrace.BINARY = binary + tracetool.backend.dtrace.PROBEPREFIX = probe_prefix + events = _read_events(fevents) if backend == "nop":
scripts/tracetool/backend/dtrace.py new file +97 -0
97 lines not on this page · raw message
scripts/tracetool/format/d.py new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Generate .d file (DTrace only). +""" + +__author__ = "Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__license__ = "GPL version 2 or (at your option) any later version" + +__maintainer__ = "Stefan Hajnoczi" +__email__ = "[email protected]" + + +from tracetool import out + + +def begin(events): + out('/* This file is autogenerated by tracetool, do not edit. */')
scripts/tracetool/format/stap.py new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Generate .stp file (DTrace with SystemTAP only). +""" + +__author__ = "Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__license__ = "GPL version 2 or (at your option) any later version" + +__maintainer__ = "Stefan Hajnoczi" +__email__ = "[email protected]" + + +from tracetool import out + + +def begin(events): + out('/* This file is autogenerated by tracetool, do not edit. */')
PATCH v5 8/8 tracetool: Add MAINTAINERS info
In reply to Lluís Vilanova--- MAINTAINERS | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
MAINTAINERS +2 -0
@@ -548,6 +548,8 @@ Tracing M: Stefan Hajnoczi <[email protected]> S: Maintained F: trace/ +F: scripts/tracetool.py +F: scripts/tracetool/ F: docs/tracing.txt T: git://github.com/stefanha/qemu.git tracing