2012-04-03 22:47:39 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Machinery for generating tracing-related intermediate files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
|
2016-02-25 19:43:10 +03:00
|
|
|
__copyright__ = "Copyright 2012-2016, Lluís Vilanova <vilanova@ac.upc.edu>"
|
2012-04-03 22:47:39 +04:00
|
|
|
__license__ = "GPL version 2 or (at your option) any later version"
|
|
|
|
|
|
|
|
__maintainer__ = "Stefan Hajnoczi"
|
|
|
|
__email__ = "stefanha@linux.vnet.ibm.com"
|
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
2014-05-30 16:11:38 +04:00
|
|
|
import weakref
|
2012-04-03 22:47:39 +04:00
|
|
|
|
|
|
|
import tracetool.format
|
|
|
|
import tracetool.backend
|
2014-05-30 16:11:44 +04:00
|
|
|
import tracetool.transform
|
2012-04-03 22:47:39 +04:00
|
|
|
|
|
|
|
|
|
|
|
def error_write(*lines):
|
|
|
|
"""Write a set of error lines."""
|
|
|
|
sys.stderr.writelines("\n".join(lines) + "\n")
|
|
|
|
|
|
|
|
def error(*lines):
|
|
|
|
"""Write a set of error lines and exit."""
|
|
|
|
error_write(*lines)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def out(*lines, **kwargs):
|
|
|
|
"""Write a set of output lines.
|
|
|
|
|
|
|
|
You can use kwargs as a shorthand for mapping variables when formating all
|
|
|
|
the strings in lines.
|
|
|
|
"""
|
|
|
|
lines = [ l % kwargs for l in lines ]
|
|
|
|
sys.stdout.writelines("\n".join(lines) + "\n")
|
|
|
|
|
|
|
|
|
|
|
|
class Arguments:
|
|
|
|
"""Event arguments description."""
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
args :
|
2016-02-25 19:43:04 +03:00
|
|
|
List of (type, name) tuples or Arguments objects.
|
2012-04-03 22:47:39 +04:00
|
|
|
"""
|
2016-02-25 19:43:04 +03:00
|
|
|
self._args = []
|
|
|
|
for arg in args:
|
|
|
|
if isinstance(arg, Arguments):
|
|
|
|
self._args.extend(arg._args)
|
|
|
|
else:
|
|
|
|
self._args.append(arg)
|
2012-04-03 22:47:39 +04:00
|
|
|
|
2014-02-23 23:37:08 +04:00
|
|
|
def copy(self):
|
|
|
|
"""Create a new copy."""
|
|
|
|
return Arguments(list(self._args))
|
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
@staticmethod
|
|
|
|
def build(arg_str):
|
|
|
|
"""Build and Arguments instance from an argument string.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
arg_str : str
|
|
|
|
String describing the event arguments.
|
|
|
|
"""
|
|
|
|
res = []
|
|
|
|
for arg in arg_str.split(","):
|
|
|
|
arg = arg.strip()
|
2012-04-27 18:12:04 +04:00
|
|
|
if arg == 'void':
|
2012-04-03 22:47:39 +04:00
|
|
|
continue
|
2012-04-27 18:12:04 +04:00
|
|
|
|
|
|
|
if '*' in arg:
|
|
|
|
arg_type, identifier = arg.rsplit('*', 1)
|
|
|
|
arg_type += '*'
|
|
|
|
identifier = identifier.strip()
|
|
|
|
else:
|
|
|
|
arg_type, identifier = arg.rsplit(None, 1)
|
|
|
|
|
|
|
|
res.append((arg_type, identifier))
|
2012-04-03 22:47:39 +04:00
|
|
|
return Arguments(res)
|
|
|
|
|
2016-02-25 19:43:04 +03:00
|
|
|
def __getitem__(self, index):
|
|
|
|
if isinstance(index, slice):
|
|
|
|
return Arguments(self._args[index])
|
|
|
|
else:
|
|
|
|
return self._args[index]
|
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
def __iter__(self):
|
|
|
|
"""Iterate over the (type, name) pairs."""
|
|
|
|
return iter(self._args)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"""Number of arguments."""
|
|
|
|
return len(self._args)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""String suitable for declaring function arguments."""
|
|
|
|
if len(self._args) == 0:
|
|
|
|
return "void"
|
|
|
|
else:
|
|
|
|
return ", ".join([ " ".join([t, n]) for t,n in self._args ])
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
"""Evaluable string representation for this object."""
|
|
|
|
return "Arguments(\"%s\")" % str(self)
|
|
|
|
|
|
|
|
def names(self):
|
|
|
|
"""List of argument names."""
|
|
|
|
return [ name for _, name in self._args ]
|
|
|
|
|
|
|
|
def types(self):
|
|
|
|
"""List of argument types."""
|
|
|
|
return [ type_ for type_, _ in self._args ]
|
|
|
|
|
2016-02-25 19:43:27 +03:00
|
|
|
def casted(self):
|
|
|
|
"""List of argument names casted to their type."""
|
|
|
|
return ["(%s)%s" % (type_, name) for type_, name in self._args]
|
|
|
|
|
2014-05-30 16:11:38 +04:00
|
|
|
def transform(self, *trans):
|
|
|
|
"""Return a new Arguments instance with transformed types.
|
|
|
|
|
|
|
|
The types in the resulting Arguments instance are transformed according
|
|
|
|
to tracetool.transform.transform_type.
|
|
|
|
"""
|
|
|
|
res = []
|
|
|
|
for type_, name in self._args:
|
|
|
|
res.append((tracetool.transform.transform_type(type_, *trans),
|
|
|
|
name))
|
|
|
|
return Arguments(res)
|
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
|
|
|
|
class Event(object):
|
|
|
|
"""Event description.
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
name : str
|
|
|
|
The event name.
|
|
|
|
fmt : str
|
|
|
|
The event format string.
|
|
|
|
properties : set(str)
|
|
|
|
Properties of the event.
|
|
|
|
args : Arguments
|
|
|
|
The event arguments.
|
2014-08-18 17:02:07 +04:00
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
"""
|
|
|
|
|
trace: tighten up trace-events regex to fix bad parse
Use \w for properties and trace event names since they are both drawn
from [a-zA-Z0-9_] character sets.
The .* for matching properties was too aggressive and caused the
following failure with foo(int rc) "(this is a test)":
Traceback (most recent call last):
File "scripts/tracetool.py", line 139, in <module>
main(sys.argv)
File "scripts/tracetool.py", line 134, in main
binary=binary, probe_prefix=probe_prefix)
File "scripts/tracetool/__init__.py", line 334, in generate
events = _read_events(fevents)
File "scripts/tracetool/__init__.py", line 262, in _read_events
res.append(Event.build(line))
File "scripts/tracetool/__init__.py", line 225, in build
return Event(name, props, fmt, args, arg_fmts)
File "scripts/tracetool/__init__.py", line 185, in __init__
% ", ".join(unknown_props))
ValueError: Unknown properties: foo(int, rc)
Cc: Lluís Vilanova <vilanova@ac.upc.edu>
Reported-by: Eric Auger <eric.auger@linaro.org>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 1411468626-20450-1-git-send-email-stefanha@redhat.com
2014-09-23 14:37:06 +04:00
|
|
|
_CRE = re.compile("((?P<props>[\w\s]+)\s+)?"
|
|
|
|
"(?P<name>\w+)"
|
2014-05-30 16:11:44 +04:00
|
|
|
"\((?P<args>[^)]*)\)"
|
|
|
|
"\s*"
|
|
|
|
"(?:(?:(?P<fmt_trans>\".+),)?\s*(?P<fmt>\".+))?"
|
|
|
|
"\s*")
|
2012-04-03 22:47:39 +04:00
|
|
|
|
2016-02-25 19:43:38 +03:00
|
|
|
_VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec", "vcpu"])
|
2012-04-03 22:47:39 +04:00
|
|
|
|
2016-02-25 16:06:30 +03:00
|
|
|
def __init__(self, name, props, fmt, args, orig=None,
|
|
|
|
event_trans=None, event_exec=None):
|
2012-04-03 22:47:39 +04:00
|
|
|
"""
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
name : string
|
|
|
|
Event name.
|
|
|
|
props : list of str
|
|
|
|
Property names.
|
2014-05-30 16:11:44 +04:00
|
|
|
fmt : str, list of str
|
|
|
|
Event printing format (or formats).
|
2012-04-03 22:47:39 +04:00
|
|
|
args : Arguments
|
|
|
|
Event arguments.
|
2014-05-30 16:11:38 +04:00
|
|
|
orig : Event or None
|
2016-02-25 16:06:30 +03:00
|
|
|
Original Event before transformation/generation.
|
|
|
|
event_trans : Event or None
|
|
|
|
Generated translation-time event ("tcg" property).
|
|
|
|
event_exec : Event or None
|
|
|
|
Generated execution-time event ("tcg" property).
|
2014-08-01 20:08:56 +04:00
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.properties = props
|
|
|
|
self.fmt = fmt
|
|
|
|
self.args = args
|
2016-02-25 16:06:30 +03:00
|
|
|
self.event_trans = event_trans
|
|
|
|
self.event_exec = event_exec
|
2012-04-03 22:47:39 +04:00
|
|
|
|
2014-05-30 16:11:38 +04:00
|
|
|
if orig is None:
|
|
|
|
self.original = weakref.ref(self)
|
|
|
|
else:
|
|
|
|
self.original = orig
|
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
unknown_props = set(self.properties) - self._VALID_PROPS
|
|
|
|
if len(unknown_props) > 0:
|
2014-02-23 23:37:13 +04:00
|
|
|
raise ValueError("Unknown properties: %s"
|
|
|
|
% ", ".join(unknown_props))
|
2014-05-30 16:11:44 +04:00
|
|
|
assert isinstance(self.fmt, str) or len(self.fmt) == 2
|
2012-04-03 22:47:39 +04:00
|
|
|
|
2014-02-23 23:37:08 +04:00
|
|
|
def copy(self):
|
|
|
|
"""Create a new copy."""
|
|
|
|
return Event(self.name, list(self.properties), self.fmt,
|
2016-02-25 16:06:30 +03:00
|
|
|
self.args.copy(), self, self.event_trans, self.event_exec)
|
2014-02-23 23:37:08 +04:00
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
@staticmethod
|
|
|
|
def build(line_str):
|
|
|
|
"""Build an Event instance from a string.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
line_str : str
|
|
|
|
Line describing the event.
|
|
|
|
"""
|
|
|
|
m = Event._CRE.match(line_str)
|
|
|
|
assert m is not None
|
|
|
|
groups = m.groupdict('')
|
|
|
|
|
|
|
|
name = groups["name"]
|
|
|
|
props = groups["props"].split()
|
|
|
|
fmt = groups["fmt"]
|
2014-05-30 16:11:44 +04:00
|
|
|
fmt_trans = groups["fmt_trans"]
|
|
|
|
if len(fmt_trans) > 0:
|
|
|
|
fmt = [fmt_trans, fmt]
|
2012-04-03 22:47:39 +04:00
|
|
|
args = Arguments.build(groups["args"])
|
|
|
|
|
2014-05-30 16:11:44 +04:00
|
|
|
if "tcg-trans" in props:
|
|
|
|
raise ValueError("Invalid property 'tcg-trans'")
|
|
|
|
if "tcg-exec" in props:
|
|
|
|
raise ValueError("Invalid property 'tcg-exec'")
|
|
|
|
if "tcg" not in props and not isinstance(fmt, str):
|
|
|
|
raise ValueError("Only events with 'tcg' property can have two formats")
|
|
|
|
if "tcg" in props and isinstance(fmt, str):
|
|
|
|
raise ValueError("Events with 'tcg' property must have two formats")
|
|
|
|
|
2016-02-25 19:43:38 +03:00
|
|
|
event = Event(name, props, fmt, args)
|
|
|
|
|
|
|
|
# add implicit arguments when using the 'vcpu' property
|
|
|
|
import tracetool.vcpu
|
|
|
|
event = tracetool.vcpu.transform_event(event)
|
|
|
|
|
|
|
|
return event
|
2012-04-03 22:47:39 +04:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
"""Evaluable string representation for this object."""
|
2014-05-30 16:11:44 +04:00
|
|
|
if isinstance(self.fmt, str):
|
|
|
|
fmt = self.fmt
|
|
|
|
else:
|
|
|
|
fmt = "%s, %s" % (self.fmt[0], self.fmt[1])
|
2012-04-03 22:47:39 +04:00
|
|
|
return "Event('%s %s(%s) %s')" % (" ".join(self.properties),
|
|
|
|
self.name,
|
|
|
|
self.args,
|
2014-05-30 16:11:44 +04:00
|
|
|
fmt)
|
2012-04-03 22:47:39 +04:00
|
|
|
|
2014-09-25 13:40:14 +04:00
|
|
|
_FMT = re.compile("(%[\d\.]*\w+|%.*PRI\S+)")
|
2014-08-18 17:02:07 +04:00
|
|
|
|
|
|
|
def formats(self):
|
|
|
|
"""List of argument print formats."""
|
|
|
|
assert not isinstance(self.fmt, list)
|
|
|
|
return self._FMT.findall(self.fmt)
|
|
|
|
|
2014-02-23 23:37:02 +04:00
|
|
|
QEMU_TRACE = "trace_%(name)s"
|
2014-05-30 16:11:50 +04:00
|
|
|
QEMU_TRACE_TCG = QEMU_TRACE + "_tcg"
|
2014-02-23 23:37:02 +04:00
|
|
|
|
|
|
|
def api(self, fmt=None):
|
|
|
|
if fmt is None:
|
|
|
|
fmt = Event.QEMU_TRACE
|
|
|
|
return fmt % {"name": self.name}
|
|
|
|
|
2014-05-30 16:11:38 +04:00
|
|
|
def transform(self, *trans):
|
|
|
|
"""Return a new Event with transformed Arguments."""
|
|
|
|
return Event(self.name,
|
|
|
|
list(self.properties),
|
|
|
|
self.fmt,
|
|
|
|
self.args.transform(*trans),
|
|
|
|
self)
|
|
|
|
|
2014-02-23 23:37:02 +04:00
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
def _read_events(fobj):
|
2014-11-03 00:37:59 +03:00
|
|
|
events = []
|
2012-04-03 22:47:39 +04:00
|
|
|
for line in fobj:
|
|
|
|
if not line.strip():
|
|
|
|
continue
|
|
|
|
if line.lstrip().startswith('#'):
|
|
|
|
continue
|
2014-11-03 00:37:59 +03:00
|
|
|
|
|
|
|
event = Event.build(line)
|
|
|
|
|
|
|
|
# transform TCG-enabled events
|
|
|
|
if "tcg" not in event.properties:
|
|
|
|
events.append(event)
|
|
|
|
else:
|
|
|
|
event_trans = event.copy()
|
|
|
|
event_trans.name += "_trans"
|
|
|
|
event_trans.properties += ["tcg-trans"]
|
|
|
|
event_trans.fmt = event.fmt[0]
|
2016-02-25 19:43:38 +03:00
|
|
|
# ignore TCG arguments
|
2014-11-03 00:37:59 +03:00
|
|
|
args_trans = []
|
|
|
|
for atrans, aorig in zip(
|
|
|
|
event_trans.transform(tracetool.transform.TCG_2_HOST).args,
|
|
|
|
event.args):
|
|
|
|
if atrans == aorig:
|
|
|
|
args_trans.append(atrans)
|
|
|
|
event_trans.args = Arguments(args_trans)
|
|
|
|
|
|
|
|
event_exec = event.copy()
|
|
|
|
event_exec.name += "_exec"
|
|
|
|
event_exec.properties += ["tcg-exec"]
|
|
|
|
event_exec.fmt = event.fmt[1]
|
2016-02-25 19:43:10 +03:00
|
|
|
event_exec.args = event_exec.args.transform(tracetool.transform.TCG_2_HOST)
|
2014-11-03 00:37:59 +03:00
|
|
|
|
|
|
|
new_event = [event_trans, event_exec]
|
|
|
|
event.event_trans, event.event_exec = new_event
|
|
|
|
|
|
|
|
events.extend(new_event)
|
|
|
|
|
|
|
|
return events
|
2012-04-03 22:47:39 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TracetoolError (Exception):
|
|
|
|
"""Exception for calls to generate."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-02-23 23:37:13 +04:00
|
|
|
def try_import(mod_name, attr_name=None, attr_default=None):
|
2012-04-03 22:47:39 +04:00
|
|
|
"""Try to import a module and get an attribute from it.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
mod_name : str
|
|
|
|
Module name.
|
|
|
|
attr_name : str, optional
|
|
|
|
Name of an attribute in the module.
|
|
|
|
attr_default : optional
|
|
|
|
Default value if the attribute does not exist in the module.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
A pair indicating whether the module could be imported and the module or
|
|
|
|
object or attribute value.
|
|
|
|
"""
|
|
|
|
try:
|
2012-04-27 17:24:41 +04:00
|
|
|
module = __import__(mod_name, globals(), locals(), ["__package__"])
|
2012-04-03 22:47:39 +04:00
|
|
|
if attr_name is None:
|
|
|
|
return True, module
|
|
|
|
return True, getattr(module, str(attr_name), attr_default)
|
|
|
|
except ImportError:
|
|
|
|
return False, None
|
|
|
|
|
|
|
|
|
2014-05-27 17:02:14 +04:00
|
|
|
def generate(fevents, format, backends,
|
2014-02-23 23:37:13 +04:00
|
|
|
binary=None, probe_prefix=None):
|
2014-05-27 17:02:14 +04:00
|
|
|
"""Generate the output for the given (format, backends) pair.
|
2012-04-03 22:47:39 +04:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fevents : file
|
|
|
|
Event description file.
|
|
|
|
format : str
|
|
|
|
Output format name.
|
2014-05-27 17:02:14 +04:00
|
|
|
backends : list
|
|
|
|
Output backend names.
|
2012-04-03 22:48:12 +04:00
|
|
|
binary : str or None
|
|
|
|
See tracetool.backend.dtrace.BINARY.
|
|
|
|
probe_prefix : str or None
|
|
|
|
See tracetool.backend.dtrace.PROBEPREFIX.
|
2012-04-03 22:47:39 +04:00
|
|
|
"""
|
|
|
|
# fix strange python error (UnboundLocalError tracetool)
|
|
|
|
import tracetool
|
|
|
|
|
|
|
|
format = str(format)
|
|
|
|
if len(format) is 0:
|
|
|
|
raise TracetoolError("format not set")
|
2014-02-23 23:37:19 +04:00
|
|
|
if not tracetool.format.exists(format):
|
2012-04-03 22:47:39 +04:00
|
|
|
raise TracetoolError("unknown format: %s" % format)
|
2014-05-27 17:02:14 +04:00
|
|
|
|
|
|
|
if len(backends) is 0:
|
|
|
|
raise TracetoolError("no backends specified")
|
|
|
|
for backend in backends:
|
|
|
|
if not tracetool.backend.exists(backend):
|
|
|
|
raise TracetoolError("unknown backend: %s" % backend)
|
|
|
|
backend = tracetool.backend.Wrapper(backends, format)
|
2012-04-03 22:47:39 +04:00
|
|
|
|
2012-04-03 22:48:12 +04:00
|
|
|
import tracetool.backend.dtrace
|
|
|
|
tracetool.backend.dtrace.BINARY = binary
|
|
|
|
tracetool.backend.dtrace.PROBEPREFIX = probe_prefix
|
|
|
|
|
2012-04-03 22:47:39 +04:00
|
|
|
events = _read_events(fevents)
|
|
|
|
|
2014-02-23 23:37:40 +04:00
|
|
|
tracetool.format.generate(events, format, backend)
|