aboutsummaryrefslogtreecommitdiff
path: root/scripts/tracetool/backend
diff options
context:
space:
mode:
authorLluís Vilanova <vilanova@ac.upc.edu>2012-04-03 20:47:39 +0200
committerStefan Hajnoczi <stefanha@linux.vnet.ibm.com>2012-04-18 14:02:59 +0100
commit650ab98d1d9551f0ca2180c0d88427acfcb081cf (patch)
treefe5dc59af037fa671f4e767323fc8cd1b5ba9703 /scripts/tracetool/backend
parent6e7a7f3d9bc2031b4c93c05400b18775ba1b1f55 (diff)
tracetool: Rewrite infrastructure as python modules
The tracetool script is written in shell and has hit several portability problems due to shell quirks or external tools across host platforms. Additionally the amount of string processing and lack of real data structures makes it tough to implement code generator backends for tracers that are more complex. This patch replaces the shell version of tracetool with a Python version. The new tracetool design is: scripts/tracetool.py - top-level script scripts/tracetool/backend/ - tracer backends live here (simple, ust) scripts/tracetool/format/ - output formats live here (.c, .h) There is common code for trace-events definition parsing so that backends can focus on generating code rather than parsing input. Support for all existing backends (nop, stderr, simple, ust, and dtrace) is added back in follow-up patches. [Commit description written by Stefan Hajnoczi] Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu> Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Diffstat (limited to 'scripts/tracetool/backend')
-rw-r--r--scripts/tracetool/backend/__init__.py111
1 files changed, 111 insertions, 0 deletions
diff --git a/scripts/tracetool/backend/__init__.py b/scripts/tracetool/backend/__init__.py
new file mode 100644
index 0000000000..34b7ed8081
--- /dev/null
+++ b/scripts/tracetool/backend/__init__.py
@@ -0,0 +1,111 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Backend management.
+
+
+Creating new backends
+---------------------
+
+A new backend named 'foo-bar' corresponds to Python module
+'tracetool/backend/foo_bar.py'.
+
+A backend module should provide a docstring, whose first non-empty line will be
+considered its short description.
+
+All backends must generate their contents through the 'tracetool.out' routine.
+
+
+Backend functions
+-----------------
+
+======== =======================================================================
+Function Description
+======== =======================================================================
+<format> Called to generate the format- and backend-specific code for each of
+ the specified events. If the function does not exist, the backend is
+ considered not compatible with the given format.
+======== =======================================================================
+"""
+
+__author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
+__copyright__ = "Copyright 2012, Lluís Vilanova <vilanova@ac.upc.edu>"
+__license__ = "GPL version 2 or (at your option) any later version"
+
+__maintainer__ = "Stefan Hajnoczi"
+__email__ = "stefanha@linux.vnet.ibm.com"
+
+
+import pkgutil
+
+import tracetool
+
+
+def get_list():
+ """Get a list of (name, description) pairs."""
+ res = [("nop", "Tracing disabled.")]
+ for _, modname, _ in pkgutil.iter_modules(tracetool.backend.__path__):
+ module = tracetool.try_import("tracetool.backend." + modname)
+
+ # just in case; should never fail unless non-module files are put there
+ if not module[0]:
+ continue
+ module = module[1]
+
+ doc = module.__doc__
+ if doc is None:
+ doc = ""
+ doc = doc.strip().split("\n")[0]
+
+ name = modname.replace("_", "-")
+ res.append((name, doc))
+ return res
+
+
+def exists(name):
+ """Return whether the given backend exists."""
+ if len(name) == 0:
+ return False
+ if name == "nop":
+ return True
+ name = name.replace("-", "_")
+ return tracetool.try_import("tracetool.backend." + name)[1]
+
+
+def compatible(backend, format):
+ """Whether a backend is compatible with the given format."""
+ if not exists(backend):
+ raise ValueError("unknown backend: %s" % backend)
+
+ backend = backend.replace("-", "_")
+ format = format.replace("-", "_")
+
+ if backend == "nop":
+ return True
+ else:
+ func = tracetool.try_import("tracetool.backend." + backend,
+ format, None)[1]
+ return func is not None
+
+
+def _empty(events):
+ pass
+
+def generate(backend, format, events):
+ """Generate the per-event output for the given (backend, format) pair."""
+ if not compatible(backend, format):
+ raise ValueError("backend '%s' not compatible with format '%s'" %
+ (backend, format))
+
+ backend = backend.replace("-", "_")
+ format = format.replace("-", "_")
+
+ if backend == "nop":
+ func = tracetool.try_import("tracetool.format." + format,
+ "nop", _empty)[1]
+ else:
+ func = tracetool.try_import("tracetool.backend." + backend,
+ format, None)[1]
+
+ func(events)