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 arg_fmts : str 140 The format strings for each argument. 141 """ 142 143 _CRE = re.compile("((?P<props>.*)\s+)?" 144 "(?P<name>[^(\s]+)" 145 "\((?P<args>[^)]*)\)" 146 "\s*" 147 "(?:(?:(?P<fmt_trans>\".+),)?\s*(?P<fmt>\".+))?" 148 "\s*") 149 _FMT = re.compile("(%\w+|%.*PRI\S+)") 150 151 _VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec"]) 152 153 def __init__(self, name, props, fmt, args, arg_fmts, orig=None): 154 """ 155 Parameters 156 ---------- 157 name : string 158 Event name. 159 props : list of str 160 Property names. 161 fmt : str, list of str 162 Event printing format (or formats). 163 args : Arguments 164 Event arguments. 165 arg_fmts : list of str 166 Format strings for each argument. 167 orig : Event or None 168 Original Event before transformation. 169 170 """ 171 self.name = name 172 self.properties = props 173 self.fmt = fmt 174 self.args = args 175 self.arg_fmts = arg_fmts 176 177 if orig is None: 178 self.original = weakref.ref(self) 179 else: 180 self.original = orig 181 182 unknown_props = set(self.properties) - self._VALID_PROPS 183 if len(unknown_props) > 0: 184 raise ValueError("Unknown properties: %s" 185 % ", ".join(unknown_props)) 186 assert isinstance(self.fmt, str) or len(self.fmt) == 2 187 188 def copy(self): 189 """Create a new copy.""" 190 return Event(self.name, list(self.properties), self.fmt, 191 self.args.copy(), self) 192 193 @staticmethod 194 def build(line_str): 195 """Build an Event instance from a string. 196 197 Parameters 198 ---------- 199 line_str : str 200 Line describing the event. 201 """ 202 m = Event._CRE.match(line_str) 203 assert m is not None 204 groups = m.groupdict('') 205 206 name = groups["name"] 207 props = groups["props"].split() 208 fmt = groups["fmt"] 209 fmt_trans = groups["fmt_trans"] 210 if len(fmt_trans) > 0: 211 fmt = [fmt_trans, fmt] 212 args = Arguments.build(groups["args"]) 213 arg_fmts = Event._FMT.findall(fmt) 214 215 if "tcg-trans" in props: 216 raise ValueError("Invalid property 'tcg-trans'") 217 if "tcg-exec" in props: 218 raise ValueError("Invalid property 'tcg-exec'") 219 if "tcg" not in props and not isinstance(fmt, str): 220 raise ValueError("Only events with 'tcg' property can have two formats") 221 if "tcg" in props and isinstance(fmt, str): 222 raise ValueError("Events with 'tcg' property must have two formats") 223 224 return Event(name, props, fmt, args, arg_fmts) 225 226 def __repr__(self): 227 """Evaluable string representation for this object.""" 228 if isinstance(self.fmt, str): 229 fmt = self.fmt 230 else: 231 fmt = "%s, %s" % (self.fmt[0], self.fmt[1]) 232 return "Event('%s %s(%s) %s')" % (" ".join(self.properties), 233 self.name, 234 self.args, 235 fmt) 236 237 QEMU_TRACE = "trace_%(name)s" 238 QEMU_TRACE_TCG = QEMU_TRACE + "_tcg" 239 240 def api(self, fmt=None): 241 if fmt is None: 242 fmt = Event.QEMU_TRACE 243 return fmt % {"name": self.name} 244 245 def transform(self, *trans): 246 """Return a new Event with transformed Arguments.""" 247 return Event(self.name, 248 list(self.properties), 249 self.fmt, 250 self.args.transform(*trans), 251 self) 252 253 254def _read_events(fobj): 255 res = [] 256 for line in fobj: 257 if not line.strip(): 258 continue 259 if line.lstrip().startswith('#'): 260 continue 261 res.append(Event.build(line)) 262 return res 263 264 265class TracetoolError (Exception): 266 """Exception for calls to generate.""" 267 pass 268 269 270def try_import(mod_name, attr_name=None, attr_default=None): 271 """Try to import a module and get an attribute from it. 272 273 Parameters 274 ---------- 275 mod_name : str 276 Module name. 277 attr_name : str, optional 278 Name of an attribute in the module. 279 attr_default : optional 280 Default value if the attribute does not exist in the module. 281 282 Returns 283 ------- 284 A pair indicating whether the module could be imported and the module or 285 object or attribute value. 286 """ 287 try: 288 module = __import__(mod_name, globals(), locals(), ["__package__"]) 289 if attr_name is None: 290 return True, module 291 return True, getattr(module, str(attr_name), attr_default) 292 except ImportError: 293 return False, None 294 295 296def generate(fevents, format, backends, 297 binary=None, probe_prefix=None): 298 """Generate the output for the given (format, backends) pair. 299 300 Parameters 301 ---------- 302 fevents : file 303 Event description file. 304 format : str 305 Output format name. 306 backends : list 307 Output backend names. 308 binary : str or None 309 See tracetool.backend.dtrace.BINARY. 310 probe_prefix : str or None 311 See tracetool.backend.dtrace.PROBEPREFIX. 312 """ 313 # fix strange python error (UnboundLocalError tracetool) 314 import tracetool 315 316 format = str(format) 317 if len(format) is 0: 318 raise TracetoolError("format not set") 319 if not tracetool.format.exists(format): 320 raise TracetoolError("unknown format: %s" % format) 321 322 if len(backends) is 0: 323 raise TracetoolError("no backends specified") 324 for backend in backends: 325 if not tracetool.backend.exists(backend): 326 raise TracetoolError("unknown backend: %s" % backend) 327 backend = tracetool.backend.Wrapper(backends, format) 328 329 import tracetool.backend.dtrace 330 tracetool.backend.dtrace.BINARY = binary 331 tracetool.backend.dtrace.PROBEPREFIX = probe_prefix 332 333 events = _read_events(fevents) 334 335 # transform TCG-enabled events 336 new_events = [] 337 for event in events: 338 if "tcg" not in event.properties: 339 new_events.append(event) 340 else: 341 event_trans = event.copy() 342 event_trans.name += "_trans" 343 event_trans.properties += ["tcg-trans"] 344 event_trans.fmt = event.fmt[0] 345 args_trans = [] 346 for atrans, aorig in zip( 347 event_trans.transform(tracetool.transform.TCG_2_HOST).args, 348 event.args): 349 if atrans == aorig: 350 args_trans.append(atrans) 351 event_trans.args = Arguments(args_trans) 352 event_trans = event_trans.copy() 353 354 event_exec = event.copy() 355 event_exec.name += "_exec" 356 event_exec.properties += ["tcg-exec"] 357 event_exec.fmt = event.fmt[1] 358 event_exec = event_exec.transform(tracetool.transform.TCG_2_HOST) 359 360 new_event = [event_trans, event_exec] 361 event.event_trans, event.event_exec = new_event 362 363 new_events.extend(new_event) 364 events = new_events 365 366 tracetool.format.generate(events, format, backend) 367