xref: /openbmc/qemu/scripts/tracetool/__init__.py (revision 931f53e1)
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5Machinery for generating tracing-related intermediate files.
6"""
7
8__author__     = "Lluís Vilanova <vilanova@ac.upc.edu>"
9__copyright__  = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
10__license__    = "GPL version 2 or (at your option) any later version"
11
12__maintainer__ = "Stefan Hajnoczi"
13__email__      = "stefanha@linux.vnet.ibm.com"
14
15
16import re
17import sys
18import weakref
19
20import tracetool.format
21import tracetool.backend
22import tracetool.transform
23
24
25def error_write(*lines):
26    """Write a set of error lines."""
27    sys.stderr.writelines("\n".join(lines) + "\n")
28
29def error(*lines):
30    """Write a set of error lines and exit."""
31    error_write(*lines)
32    sys.exit(1)
33
34
35def out(*lines, **kwargs):
36    """Write a set of output lines.
37
38    You can use kwargs as a shorthand for mapping variables when formating all
39    the strings in lines.
40    """
41    lines = [ l % kwargs for l in lines ]
42    sys.stdout.writelines("\n".join(lines) + "\n")
43
44
45class Arguments:
46    """Event arguments description."""
47
48    def __init__(self, args):
49        """
50        Parameters
51        ----------
52        args :
53            List of (type, name) tuples.
54        """
55        self._args = args
56
57    def copy(self):
58        """Create a new copy."""
59        return Arguments(list(self._args))
60
61    @staticmethod
62    def build(arg_str):
63        """Build and Arguments instance from an argument string.
64
65        Parameters
66        ----------
67        arg_str : str
68            String describing the event arguments.
69        """
70        res = []
71        for arg in arg_str.split(","):
72            arg = arg.strip()
73            if arg == 'void':
74                continue
75
76            if '*' in arg:
77                arg_type, identifier = arg.rsplit('*', 1)
78                arg_type += '*'
79                identifier = identifier.strip()
80            else:
81                arg_type, identifier = arg.rsplit(None, 1)
82
83            res.append((arg_type, identifier))
84        return Arguments(res)
85
86    def __iter__(self):
87        """Iterate over the (type, name) pairs."""
88        return iter(self._args)
89
90    def __len__(self):
91        """Number of arguments."""
92        return len(self._args)
93
94    def __str__(self):
95        """String suitable for declaring function arguments."""
96        if len(self._args) == 0:
97            return "void"
98        else:
99            return ", ".join([ " ".join([t, n]) for t,n in self._args ])
100
101    def __repr__(self):
102        """Evaluable string representation for this object."""
103        return "Arguments(\"%s\")" % str(self)
104
105    def names(self):
106        """List of argument names."""
107        return [ name for _, name in self._args ]
108
109    def types(self):
110        """List of argument types."""
111        return [ type_ for type_, _ in self._args ]
112
113    def transform(self, *trans):
114        """Return a new Arguments instance with transformed types.
115
116        The types in the resulting Arguments instance are transformed according
117        to tracetool.transform.transform_type.
118        """
119        res = []
120        for type_, name in self._args:
121            res.append((tracetool.transform.transform_type(type_, *trans),
122                        name))
123        return Arguments(res)
124
125
126class Event(object):
127    """Event description.
128
129    Attributes
130    ----------
131    name : str
132        The event name.
133    fmt : str
134        The event format string.
135    properties : set(str)
136        Properties of the event.
137    args : Arguments
138        The event arguments.
139
140    """
141
142    _CRE = re.compile("((?P<props>[\w\s]+)\s+)?"
143                      "(?P<name>\w+)"
144                      "\((?P<args>[^)]*)\)"
145                      "\s*"
146                      "(?:(?:(?P<fmt_trans>\".+),)?\s*(?P<fmt>\".+))?"
147                      "\s*")
148
149    _VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec"])
150
151    def __init__(self, name, props, fmt, args, orig=None):
152        """
153        Parameters
154        ----------
155        name : string
156            Event name.
157        props : list of str
158            Property names.
159        fmt : str, list of str
160            Event printing format (or formats).
161        args : Arguments
162            Event arguments.
163        orig : Event or None
164            Original Event before transformation.
165
166        """
167        self.name = name
168        self.properties = props
169        self.fmt = fmt
170        self.args = args
171
172        if orig is None:
173            self.original = weakref.ref(self)
174        else:
175            self.original = orig
176
177        unknown_props = set(self.properties) - self._VALID_PROPS
178        if len(unknown_props) > 0:
179            raise ValueError("Unknown properties: %s"
180                             % ", ".join(unknown_props))
181        assert isinstance(self.fmt, str) or len(self.fmt) == 2
182
183    def copy(self):
184        """Create a new copy."""
185        return Event(self.name, list(self.properties), self.fmt,
186                     self.args.copy(), self)
187
188    @staticmethod
189    def build(line_str):
190        """Build an Event instance from a string.
191
192        Parameters
193        ----------
194        line_str : str
195            Line describing the event.
196        """
197        m = Event._CRE.match(line_str)
198        assert m is not None
199        groups = m.groupdict('')
200
201        name = groups["name"]
202        props = groups["props"].split()
203        fmt = groups["fmt"]
204        fmt_trans = groups["fmt_trans"]
205        if len(fmt_trans) > 0:
206            fmt = [fmt_trans, fmt]
207        args = Arguments.build(groups["args"])
208
209        if "tcg-trans" in props:
210            raise ValueError("Invalid property 'tcg-trans'")
211        if "tcg-exec" in props:
212            raise ValueError("Invalid property 'tcg-exec'")
213        if "tcg" not in props and not isinstance(fmt, str):
214            raise ValueError("Only events with 'tcg' property can have two formats")
215        if "tcg" in props and isinstance(fmt, str):
216            raise ValueError("Events with 'tcg' property must have two formats")
217
218        return Event(name, props, fmt, args)
219
220    def __repr__(self):
221        """Evaluable string representation for this object."""
222        if isinstance(self.fmt, str):
223            fmt = self.fmt
224        else:
225            fmt = "%s, %s" % (self.fmt[0], self.fmt[1])
226        return "Event('%s %s(%s) %s')" % (" ".join(self.properties),
227                                          self.name,
228                                          self.args,
229                                          fmt)
230
231    _FMT = re.compile("(%[\d\.]*\w+|%.*PRI\S+)")
232
233    def formats(self):
234        """List of argument print formats."""
235        assert not isinstance(self.fmt, list)
236        return self._FMT.findall(self.fmt)
237
238    QEMU_TRACE               = "trace_%(name)s"
239    QEMU_TRACE_TCG           = QEMU_TRACE + "_tcg"
240
241    def api(self, fmt=None):
242        if fmt is None:
243            fmt = Event.QEMU_TRACE
244        return fmt % {"name": self.name}
245
246    def transform(self, *trans):
247        """Return a new Event with transformed Arguments."""
248        return Event(self.name,
249                     list(self.properties),
250                     self.fmt,
251                     self.args.transform(*trans),
252                     self)
253
254
255def _read_events(fobj):
256    res = []
257    for line in fobj:
258        if not line.strip():
259            continue
260        if line.lstrip().startswith('#'):
261            continue
262        res.append(Event.build(line))
263    return res
264
265
266class TracetoolError (Exception):
267    """Exception for calls to generate."""
268    pass
269
270
271def try_import(mod_name, attr_name=None, attr_default=None):
272    """Try to import a module and get an attribute from it.
273
274    Parameters
275    ----------
276    mod_name : str
277        Module name.
278    attr_name : str, optional
279        Name of an attribute in the module.
280    attr_default : optional
281        Default value if the attribute does not exist in the module.
282
283    Returns
284    -------
285    A pair indicating whether the module could be imported and the module or
286    object or attribute value.
287    """
288    try:
289        module = __import__(mod_name, globals(), locals(), ["__package__"])
290        if attr_name is None:
291            return True, module
292        return True, getattr(module, str(attr_name), attr_default)
293    except ImportError:
294        return False, None
295
296
297def generate(fevents, format, backends,
298             binary=None, probe_prefix=None):
299    """Generate the output for the given (format, backends) pair.
300
301    Parameters
302    ----------
303    fevents : file
304        Event description file.
305    format : str
306        Output format name.
307    backends : list
308        Output backend names.
309    binary : str or None
310        See tracetool.backend.dtrace.BINARY.
311    probe_prefix : str or None
312        See tracetool.backend.dtrace.PROBEPREFIX.
313    """
314    # fix strange python error (UnboundLocalError tracetool)
315    import tracetool
316
317    format = str(format)
318    if len(format) is 0:
319        raise TracetoolError("format not set")
320    if not tracetool.format.exists(format):
321        raise TracetoolError("unknown format: %s" % format)
322
323    if len(backends) is 0:
324        raise TracetoolError("no backends specified")
325    for backend in backends:
326        if not tracetool.backend.exists(backend):
327            raise TracetoolError("unknown backend: %s" % backend)
328    backend = tracetool.backend.Wrapper(backends, format)
329
330    import tracetool.backend.dtrace
331    tracetool.backend.dtrace.BINARY = binary
332    tracetool.backend.dtrace.PROBEPREFIX = probe_prefix
333
334    events = _read_events(fevents)
335
336    # transform TCG-enabled events
337    new_events = []
338    for event in events:
339        if "tcg" not in event.properties:
340            new_events.append(event)
341        else:
342            event_trans = event.copy()
343            event_trans.name += "_trans"
344            event_trans.properties += ["tcg-trans"]
345            event_trans.fmt = event.fmt[0]
346            args_trans = []
347            for atrans, aorig in zip(
348                    event_trans.transform(tracetool.transform.TCG_2_HOST).args,
349                    event.args):
350                if atrans == aorig:
351                    args_trans.append(atrans)
352            event_trans.args = Arguments(args_trans)
353            event_trans = event_trans.copy()
354
355            event_exec = event.copy()
356            event_exec.name += "_exec"
357            event_exec.properties += ["tcg-exec"]
358            event_exec.fmt = event.fmt[1]
359            event_exec = event_exec.transform(tracetool.transform.TCG_2_HOST)
360
361            new_event = [event_trans, event_exec]
362            event.event_trans, event.event_exec = new_event
363
364            new_events.extend(new_event)
365    events = new_events
366
367    tracetool.format.generate(events, format, backend)
368