sysbus: Add new platform bus helper device
[qemu.git] / scripts / tracetool / __init__.py
blob3d5743f93e4a5130dfd93845a43c402e735aef6f
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """
5 Machinery for generating tracing-related intermediate files.
6 """
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"
12 __maintainer__ = "Stefan Hajnoczi"
13 __email__ = "stefanha@linux.vnet.ibm.com"
16 import re
17 import sys
18 import weakref
20 import tracetool.format
21 import tracetool.backend
22 import tracetool.transform
25 def error_write(*lines):
26 """Write a set of error lines."""
27 sys.stderr.writelines("\n".join(lines) + "\n")
29 def error(*lines):
30 """Write a set of error lines and exit."""
31 error_write(*lines)
32 sys.exit(1)
35 def out(*lines, **kwargs):
36 """Write a set of output lines.
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")
45 class Arguments:
46 """Event arguments description."""
48 def __init__(self, args):
49 """
50 Parameters
51 ----------
52 args :
53 List of (type, name) tuples.
54 """
55 self._args = args
57 def copy(self):
58 """Create a new copy."""
59 return Arguments(list(self._args))
61 @staticmethod
62 def build(arg_str):
63 """Build and Arguments instance from an argument string.
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
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)
83 res.append((arg_type, identifier))
84 return Arguments(res)
86 def __iter__(self):
87 """Iterate over the (type, name) pairs."""
88 return iter(self._args)
90 def __len__(self):
91 """Number of arguments."""
92 return len(self._args)
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 ])
101 def __repr__(self):
102 """Evaluable string representation for this object."""
103 return "Arguments(\"%s\")" % str(self)
105 def names(self):
106 """List of argument names."""
107 return [ name for _, name in self._args ]
109 def types(self):
110 """List of argument types."""
111 return [ type_ for type_, _ in self._args ]
113 def transform(self, *trans):
114 """Return a new Arguments instance with transformed types.
116 The types in the resulting Arguments instance are transformed according
117 to tracetool.transform.transform_type.
119 res = []
120 for type_, name in self._args:
121 res.append((tracetool.transform.transform_type(type_, *trans),
122 name))
123 return Arguments(res)
126 class Event(object):
127 """Event description.
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.
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*")
149 _VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec"])
151 def __init__(self, name, props, fmt, args, orig=None):
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.
167 self.name = name
168 self.properties = props
169 self.fmt = fmt
170 self.args = args
172 if orig is None:
173 self.original = weakref.ref(self)
174 else:
175 self.original = orig
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
183 def copy(self):
184 """Create a new copy."""
185 return Event(self.name, list(self.properties), self.fmt,
186 self.args.copy(), self)
188 @staticmethod
189 def build(line_str):
190 """Build an Event instance from a string.
192 Parameters
193 ----------
194 line_str : str
195 Line describing the event.
197 m = Event._CRE.match(line_str)
198 assert m is not None
199 groups = m.groupdict('')
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"])
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")
218 return Event(name, props, fmt, args)
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)
231 _FMT = re.compile("(%[\d\.]*\w+|%.*PRI\S+)")
233 def formats(self):
234 """List of argument print formats."""
235 assert not isinstance(self.fmt, list)
236 return self._FMT.findall(self.fmt)
238 QEMU_TRACE = "trace_%(name)s"
239 QEMU_TRACE_TCG = QEMU_TRACE + "_tcg"
241 def api(self, fmt=None):
242 if fmt is None:
243 fmt = Event.QEMU_TRACE
244 return fmt % {"name": self.name}
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)
255 def _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
266 class TracetoolError (Exception):
267 """Exception for calls to generate."""
268 pass
271 def try_import(mod_name, attr_name=None, attr_default=None):
272 """Try to import a module and get an attribute from it.
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.
283 Returns
284 -------
285 A pair indicating whether the module could be imported and the module or
286 object or attribute value.
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
297 def generate(fevents, format, backends,
298 binary=None, probe_prefix=None):
299 """Generate the output for the given (format, backends) pair.
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.
314 # fix strange python error (UnboundLocalError tracetool)
315 import tracetool
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)
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)
330 import tracetool.backend.dtrace
331 tracetool.backend.dtrace.BINARY = binary
332 tracetool.backend.dtrace.PROBEPREFIX = probe_prefix
334 events = _read_events(fevents)
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()
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)
361 new_event = [event_trans, event_exec]
362 event.event_trans, event.event_exec = new_event
364 new_events.extend(new_event)
365 events = new_events
367 tracetool.format.generate(events, format, backend)