xref: /qemu/scripts/tracetool/vcpu.py (revision f03868bd)
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5Generic management for the 'vcpu' property.
6
7"""
8
9__author__     = "Lluís Vilanova <vilanova@ac.upc.edu>"
10__copyright__  = "Copyright 2016, Lluís Vilanova <vilanova@ac.upc.edu>"
11__license__    = "GPL version 2 or (at your option) any later version"
12
13__maintainer__ = "Stefan Hajnoczi"
14__email__      = "stefanha@linux.vnet.ibm.com"
15
16
17from tracetool import Arguments, try_import
18
19
20def transform_event(event):
21    """Transform event to comply with the 'vcpu' property (if present)."""
22    if "vcpu" in event.properties:
23        # events with 'tcg-trans' and 'tcg-exec' are auto-generated from
24        # already-patched events
25        assert "tcg-trans" not in event.properties
26        assert "tcg-exec" not in event.properties
27
28        event.args = Arguments([("CPUState *", "__cpu"), event.args])
29        if "tcg" in event.properties:
30            fmt = "\"cpu=%p \""
31            event.fmt = [fmt + event.fmt[0],
32                         fmt + event.fmt[1]]
33        else:
34            fmt = "\"cpu=%p \""
35            event.fmt = fmt + event.fmt
36    return event
37
38
39def transform_args(format, event, *args, **kwargs):
40    """Transforms the arguments to suit the specified format.
41
42    The format module must implement function 'vcpu_args', which receives the
43    implicit arguments added by the 'vcpu' property, and must return suitable
44    arguments for the given format.
45
46    The function is only called for events with the 'vcpu' property.
47
48    Parameters
49    ==========
50    format : str
51        Format module name.
52    event : Event
53    args, kwargs
54        Passed to 'vcpu_transform_args'.
55
56    Returns
57    =======
58    Arguments
59        The transformed arguments, including the non-implicit ones.
60
61    """
62    if "vcpu" in event.properties:
63        ok, func = try_import("tracetool.format." + format,
64                              "vcpu_transform_args")
65        assert ok
66        assert func
67        return Arguments([func(event.args[:1], *args, **kwargs),
68                          event.args[1:]])
69    else:
70        return event.args
71