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-2016, 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 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) 61 62 def copy(self): 63 """Create a new copy.""" 64 return Arguments(list(self._args)) 65 66 @staticmethod 67 def build(arg_str): 68 """Build and Arguments instance from an argument string. 69 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 80 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) 87 88 res.append((arg_type, identifier)) 89 return Arguments(res) 90 91 def __getitem__(self, index): 92 if isinstance(index, slice): 93 return Arguments(self._args[index]) 94 else: 95 return self._args[index] 96 97 def __iter__(self): 98 """Iterate over the (type, name) pairs.""" 99 return iter(self._args) 100 101 def __len__(self): 102 """Number of arguments.""" 103 return len(self._args) 104 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 ]) 111 112 def __repr__(self): 113 """Evaluable string representation for this object.""" 114 return "Arguments(\"%s\")" % str(self) 115 116 def names(self): 117 """List of argument names.""" 118 return [ name for _, name in self._args ] 119 120 def types(self): 121 """List of argument types.""" 122 return [ type_ for type_, _ in self._args ] 123 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] 127 128 def transform(self, *trans): 129 """Return a new Arguments instance with transformed types. 130 131 The types in the resulting Arguments instance are transformed according 132 to tracetool.transform.transform_type. 133 """ 134 res = [] 135 for type_, name in self._args: 136 res.append((tracetool.transform.transform_type(type_, *trans), 137 name)) 138 return Arguments(res) 139 140 141class Event(object): 142 """Event description. 143 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. 154 155 """ 156 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*") 163 164 _VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec", "vcpu"]) 165 166 def __init__(self, name, props, fmt, args, orig=None, 167 event_trans=None, event_exec=None): 168 """ 169 Parameters 170 ---------- 171 name : string 172 Event name. 173 props : list of str 174 Property names. 175 fmt : str, list of str 176 Event printing format (or formats). 177 args : Arguments 178 Event arguments. 179 orig : Event or None 180 Original Event before transformation/generation. 181 event_trans : Event or None 182 Generated translation-time event ("tcg" property). 183 event_exec : Event or None 184 Generated execution-time event ("tcg" property). 185 186 """ 187 self.name = name 188 self.properties = props 189 self.fmt = fmt 190 self.args = args 191 self.event_trans = event_trans 192 self.event_exec = event_exec 193 194 if len(args) > 10: 195 raise ValueError("Event '%s' has more than maximum permitted " 196 "argument count" % name) 197 198 if orig is None: 199 self.original = weakref.ref(self) 200 else: 201 self.original = orig 202 203 unknown_props = set(self.properties) - self._VALID_PROPS 204 if len(unknown_props) > 0: 205 raise ValueError("Unknown properties: %s" 206 % ", ".join(unknown_props)) 207 assert isinstance(self.fmt, str) or len(self.fmt) == 2 208 209 def copy(self): 210 """Create a new copy.""" 211 return Event(self.name, list(self.properties), self.fmt, 212 self.args.copy(), self, self.event_trans, self.event_exec) 213 214 @staticmethod 215 def build(line_str): 216 """Build an Event instance from a string. 217 218 Parameters 219 ---------- 220 line_str : str 221 Line describing the event. 222 """ 223 m = Event._CRE.match(line_str) 224 assert m is not None 225 groups = m.groupdict('') 226 227 name = groups["name"] 228 props = groups["props"].split() 229 fmt = groups["fmt"] 230 fmt_trans = groups["fmt_trans"] 231 if len(fmt_trans) > 0: 232 fmt = [fmt_trans, fmt] 233 args = Arguments.build(groups["args"]) 234 235 if "tcg-trans" in props: 236 raise ValueError("Invalid property 'tcg-trans'") 237 if "tcg-exec" in props: 238 raise ValueError("Invalid property 'tcg-exec'") 239 if "tcg" not in props and not isinstance(fmt, str): 240 raise ValueError("Only events with 'tcg' property can have two formats") 241 if "tcg" in props and isinstance(fmt, str): 242 raise ValueError("Events with 'tcg' property must have two formats") 243 244 event = Event(name, props, fmt, args) 245 246 # add implicit arguments when using the 'vcpu' property 247 import tracetool.vcpu 248 event = tracetool.vcpu.transform_event(event) 249 250 return event 251 252 def __repr__(self): 253 """Evaluable string representation for this object.""" 254 if isinstance(self.fmt, str): 255 fmt = self.fmt 256 else: 257 fmt = "%s, %s" % (self.fmt[0], self.fmt[1]) 258 return "Event('%s %s(%s) %s')" % (" ".join(self.properties), 259 self.name, 260 self.args, 261 fmt) 262 263 _FMT = re.compile("(%[\d\.]*\w+|%.*PRI\S+)") 264 265 def formats(self): 266 """List of argument print formats.""" 267 assert not isinstance(self.fmt, list) 268 return self._FMT.findall(self.fmt) 269 270 QEMU_TRACE = "trace_%(name)s" 271 QEMU_TRACE_TCG = QEMU_TRACE + "_tcg" 272 QEMU_DSTATE = "_TRACE_%(NAME)s_DSTATE" 273 QEMU_EVENT = "_TRACE_%(NAME)s_EVENT" 274 275 def api(self, fmt=None): 276 if fmt is None: 277 fmt = Event.QEMU_TRACE 278 return fmt % {"name": self.name, "NAME": self.name.upper()} 279 280 def transform(self, *trans): 281 """Return a new Event with transformed Arguments.""" 282 return Event(self.name, 283 list(self.properties), 284 self.fmt, 285 self.args.transform(*trans), 286 self) 287 288 289def read_events(fobj): 290 """Generate the output for the given (format, backends) pair. 291 292 Parameters 293 ---------- 294 fobj : file 295 Event description file. 296 297 Returns a list of Event objects 298 """ 299 300 events = [] 301 for line in fobj: 302 if not line.strip(): 303 continue 304 if line.lstrip().startswith('#'): 305 continue 306 307 event = Event.build(line) 308 309 # transform TCG-enabled events 310 if "tcg" not in event.properties: 311 events.append(event) 312 else: 313 event_trans = event.copy() 314 event_trans.name += "_trans" 315 event_trans.properties += ["tcg-trans"] 316 event_trans.fmt = event.fmt[0] 317 # ignore TCG arguments 318 args_trans = [] 319 for atrans, aorig in zip( 320 event_trans.transform(tracetool.transform.TCG_2_HOST).args, 321 event.args): 322 if atrans == aorig: 323 args_trans.append(atrans) 324 event_trans.args = Arguments(args_trans) 325 326 event_exec = event.copy() 327 event_exec.name += "_exec" 328 event_exec.properties += ["tcg-exec"] 329 event_exec.fmt = event.fmt[1] 330 event_exec.args = event_exec.args.transform(tracetool.transform.TCG_2_HOST) 331 332 new_event = [event_trans, event_exec] 333 event.event_trans, event.event_exec = new_event 334 335 events.extend(new_event) 336 337 return events 338 339 340class TracetoolError (Exception): 341 """Exception for calls to generate.""" 342 pass 343 344 345def try_import(mod_name, attr_name=None, attr_default=None): 346 """Try to import a module and get an attribute from it. 347 348 Parameters 349 ---------- 350 mod_name : str 351 Module name. 352 attr_name : str, optional 353 Name of an attribute in the module. 354 attr_default : optional 355 Default value if the attribute does not exist in the module. 356 357 Returns 358 ------- 359 A pair indicating whether the module could be imported and the module or 360 object or attribute value. 361 """ 362 try: 363 module = __import__(mod_name, globals(), locals(), ["__package__"]) 364 if attr_name is None: 365 return True, module 366 return True, getattr(module, str(attr_name), attr_default) 367 except ImportError: 368 return False, None 369 370 371def generate(events, group, format, backends, 372 binary=None, probe_prefix=None): 373 """Generate the output for the given (format, backends) pair. 374 375 Parameters 376 ---------- 377 events : list 378 list of Event objects to generate for 379 group: str 380 Name of the tracing group 381 format : str 382 Output format name. 383 backends : list 384 Output backend names. 385 binary : str or None 386 See tracetool.backend.dtrace.BINARY. 387 probe_prefix : str or None 388 See tracetool.backend.dtrace.PROBEPREFIX. 389 """ 390 # fix strange python error (UnboundLocalError tracetool) 391 import tracetool 392 393 format = str(format) 394 if len(format) is 0: 395 raise TracetoolError("format not set") 396 if not tracetool.format.exists(format): 397 raise TracetoolError("unknown format: %s" % format) 398 399 if len(backends) is 0: 400 raise TracetoolError("no backends specified") 401 for backend in backends: 402 if not tracetool.backend.exists(backend): 403 raise TracetoolError("unknown backend: %s" % backend) 404 backend = tracetool.backend.Wrapper(backends, format) 405 406 import tracetool.backend.dtrace 407 tracetool.backend.dtrace.BINARY = binary 408 tracetool.backend.dtrace.PROBEPREFIX = probe_prefix 409 410 tracetool.format.generate(events, format, backend, group) 411