trace: Add helper function to cast event arguments
[qemu/ar7.git] / scripts / tracetool / __init__.py
blob9e02f73e20d999f5e087a697be7acb01c18bf968
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-2016, 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 or Arguments objects.
54 """
55 self._args = []
56 for arg in args:
57 if isinstance(arg, Arguments):
58 self._args.extend(arg._args)
59 else:
60 self._args.append(arg)
62 def copy(self):
63 """Create a new copy."""
64 return Arguments(list(self._args))
66 @staticmethod
67 def build(arg_str):
68 """Build and Arguments instance from an argument string.
70 Parameters
71 ----------
72 arg_str : str
73 String describing the event arguments.
74 """
75 res = []
76 for arg in arg_str.split(","):
77 arg = arg.strip()
78 if arg == 'void':
79 continue
81 if '*' in arg:
82 arg_type, identifier = arg.rsplit('*', 1)
83 arg_type += '*'
84 identifier = identifier.strip()
85 else:
86 arg_type, identifier = arg.rsplit(None, 1)
88 res.append((arg_type, identifier))
89 return Arguments(res)
91 def __getitem__(self, index):
92 if isinstance(index, slice):
93 return Arguments(self._args[index])
94 else:
95 return self._args[index]
97 def __iter__(self):
98 """Iterate over the (type, name) pairs."""
99 return iter(self._args)
101 def __len__(self):
102 """Number of arguments."""
103 return len(self._args)
105 def __str__(self):
106 """String suitable for declaring function arguments."""
107 if len(self._args) == 0:
108 return "void"
109 else:
110 return ", ".join([ " ".join([t, n]) for t,n in self._args ])
112 def __repr__(self):
113 """Evaluable string representation for this object."""
114 return "Arguments(\"%s\")" % str(self)
116 def names(self):
117 """List of argument names."""
118 return [ name for _, name in self._args ]
120 def types(self):
121 """List of argument types."""
122 return [ type_ for type_, _ in self._args ]
124 def casted(self):
125 """List of argument names casted to their type."""
126 return ["(%s)%s" % (type_, name) for type_, name in self._args]
128 def transform(self, *trans):
129 """Return a new Arguments instance with transformed types.
131 The types in the resulting Arguments instance are transformed according
132 to tracetool.transform.transform_type.
134 res = []
135 for type_, name in self._args:
136 res.append((tracetool.transform.transform_type(type_, *trans),
137 name))
138 return Arguments(res)
141 class Event(object):
142 """Event description.
144 Attributes
145 ----------
146 name : str
147 The event name.
148 fmt : str
149 The event format string.
150 properties : set(str)
151 Properties of the event.
152 args : Arguments
153 The event arguments.
157 _CRE = re.compile("((?P<props>[\w\s]+)\s+)?"
158 "(?P<name>\w+)"
159 "\((?P<args>[^)]*)\)"
160 "\s*"
161 "(?:(?:(?P<fmt_trans>\".+),)?\s*(?P<fmt>\".+))?"
162 "\s*")
164 _VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec"])
166 def __init__(self, name, props, fmt, args, orig=None):
168 Parameters
169 ----------
170 name : string
171 Event name.
172 props : list of str
173 Property names.
174 fmt : str, list of str
175 Event printing format (or formats).
176 args : Arguments
177 Event arguments.
178 orig : Event or None
179 Original Event before transformation.
182 self.name = name
183 self.properties = props
184 self.fmt = fmt
185 self.args = args
187 if orig is None:
188 self.original = weakref.ref(self)
189 else:
190 self.original = orig
192 unknown_props = set(self.properties) - self._VALID_PROPS
193 if len(unknown_props) > 0:
194 raise ValueError("Unknown properties: %s"
195 % ", ".join(unknown_props))
196 assert isinstance(self.fmt, str) or len(self.fmt) == 2
198 def copy(self):
199 """Create a new copy."""
200 return Event(self.name, list(self.properties), self.fmt,
201 self.args.copy(), self)
203 @staticmethod
204 def build(line_str):
205 """Build an Event instance from a string.
207 Parameters
208 ----------
209 line_str : str
210 Line describing the event.
212 m = Event._CRE.match(line_str)
213 assert m is not None
214 groups = m.groupdict('')
216 name = groups["name"]
217 props = groups["props"].split()
218 fmt = groups["fmt"]
219 fmt_trans = groups["fmt_trans"]
220 if len(fmt_trans) > 0:
221 fmt = [fmt_trans, fmt]
222 args = Arguments.build(groups["args"])
224 if "tcg-trans" in props:
225 raise ValueError("Invalid property 'tcg-trans'")
226 if "tcg-exec" in props:
227 raise ValueError("Invalid property 'tcg-exec'")
228 if "tcg" not in props and not isinstance(fmt, str):
229 raise ValueError("Only events with 'tcg' property can have two formats")
230 if "tcg" in props and isinstance(fmt, str):
231 raise ValueError("Events with 'tcg' property must have two formats")
233 return Event(name, props, fmt, args)
235 def __repr__(self):
236 """Evaluable string representation for this object."""
237 if isinstance(self.fmt, str):
238 fmt = self.fmt
239 else:
240 fmt = "%s, %s" % (self.fmt[0], self.fmt[1])
241 return "Event('%s %s(%s) %s')" % (" ".join(self.properties),
242 self.name,
243 self.args,
244 fmt)
246 _FMT = re.compile("(%[\d\.]*\w+|%.*PRI\S+)")
248 def formats(self):
249 """List of argument print formats."""
250 assert not isinstance(self.fmt, list)
251 return self._FMT.findall(self.fmt)
253 QEMU_TRACE = "trace_%(name)s"
254 QEMU_TRACE_TCG = QEMU_TRACE + "_tcg"
256 def api(self, fmt=None):
257 if fmt is None:
258 fmt = Event.QEMU_TRACE
259 return fmt % {"name": self.name}
261 def transform(self, *trans):
262 """Return a new Event with transformed Arguments."""
263 return Event(self.name,
264 list(self.properties),
265 self.fmt,
266 self.args.transform(*trans),
267 self)
270 def _read_events(fobj):
271 events = []
272 for line in fobj:
273 if not line.strip():
274 continue
275 if line.lstrip().startswith('#'):
276 continue
278 event = Event.build(line)
280 # transform TCG-enabled events
281 if "tcg" not in event.properties:
282 events.append(event)
283 else:
284 event_trans = event.copy()
285 event_trans.name += "_trans"
286 event_trans.properties += ["tcg-trans"]
287 event_trans.fmt = event.fmt[0]
288 args_trans = []
289 for atrans, aorig in zip(
290 event_trans.transform(tracetool.transform.TCG_2_HOST).args,
291 event.args):
292 if atrans == aorig:
293 args_trans.append(atrans)
294 event_trans.args = Arguments(args_trans)
296 event_exec = event.copy()
297 event_exec.name += "_exec"
298 event_exec.properties += ["tcg-exec"]
299 event_exec.fmt = event.fmt[1]
300 event_exec.args = event_exec.args.transform(tracetool.transform.TCG_2_HOST)
302 new_event = [event_trans, event_exec]
303 event.event_trans, event.event_exec = new_event
305 events.extend(new_event)
307 return events
310 class TracetoolError (Exception):
311 """Exception for calls to generate."""
312 pass
315 def try_import(mod_name, attr_name=None, attr_default=None):
316 """Try to import a module and get an attribute from it.
318 Parameters
319 ----------
320 mod_name : str
321 Module name.
322 attr_name : str, optional
323 Name of an attribute in the module.
324 attr_default : optional
325 Default value if the attribute does not exist in the module.
327 Returns
328 -------
329 A pair indicating whether the module could be imported and the module or
330 object or attribute value.
332 try:
333 module = __import__(mod_name, globals(), locals(), ["__package__"])
334 if attr_name is None:
335 return True, module
336 return True, getattr(module, str(attr_name), attr_default)
337 except ImportError:
338 return False, None
341 def generate(fevents, format, backends,
342 binary=None, probe_prefix=None):
343 """Generate the output for the given (format, backends) pair.
345 Parameters
346 ----------
347 fevents : file
348 Event description file.
349 format : str
350 Output format name.
351 backends : list
352 Output backend names.
353 binary : str or None
354 See tracetool.backend.dtrace.BINARY.
355 probe_prefix : str or None
356 See tracetool.backend.dtrace.PROBEPREFIX.
358 # fix strange python error (UnboundLocalError tracetool)
359 import tracetool
361 format = str(format)
362 if len(format) is 0:
363 raise TracetoolError("format not set")
364 if not tracetool.format.exists(format):
365 raise TracetoolError("unknown format: %s" % format)
367 if len(backends) is 0:
368 raise TracetoolError("no backends specified")
369 for backend in backends:
370 if not tracetool.backend.exists(backend):
371 raise TracetoolError("unknown backend: %s" % backend)
372 backend = tracetool.backend.Wrapper(backends, format)
374 import tracetool.backend.dtrace
375 tracetool.backend.dtrace.BINARY = binary
376 tracetool.backend.dtrace.PROBEPREFIX = probe_prefix
378 events = _read_events(fevents)
380 tracetool.format.generate(events, format, backend)