__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Machinery for generating tracing-related intermediate files.
  5. """
  6. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  7. __copyright__ = "Copyright 2012-2017, Lluís Vilanova <vilanova@ac.upc.edu>"
  8. __license__ = "GPL version 2 or (at your option) any later version"
  9. __maintainer__ = "Stefan Hajnoczi"
  10. __email__ = "stefanha@linux.vnet.ibm.com"
  11. import re
  12. import sys
  13. import weakref
  14. import tracetool.format
  15. import tracetool.backend
  16. import tracetool.transform
  17. def error_write(*lines):
  18. """Write a set of error lines."""
  19. sys.stderr.writelines("\n".join(lines) + "\n")
  20. def error(*lines):
  21. """Write a set of error lines and exit."""
  22. error_write(*lines)
  23. sys.exit(1)
  24. def out(*lines, **kwargs):
  25. """Write a set of output lines.
  26. You can use kwargs as a shorthand for mapping variables when formating all
  27. the strings in lines.
  28. """
  29. lines = [ l % kwargs for l in lines ]
  30. sys.stdout.writelines("\n".join(lines) + "\n")
  31. class Arguments:
  32. """Event arguments description."""
  33. def __init__(self, args):
  34. """
  35. Parameters
  36. ----------
  37. args :
  38. List of (type, name) tuples or Arguments objects.
  39. """
  40. self._args = []
  41. for arg in args:
  42. if isinstance(arg, Arguments):
  43. self._args.extend(arg._args)
  44. else:
  45. self._args.append(arg)
  46. def copy(self):
  47. """Create a new copy."""
  48. return Arguments(list(self._args))
  49. @staticmethod
  50. def build(arg_str):
  51. """Build and Arguments instance from an argument string.
  52. Parameters
  53. ----------
  54. arg_str : str
  55. String describing the event arguments.
  56. """
  57. res = []
  58. for arg in arg_str.split(","):
  59. arg = arg.strip()
  60. if arg == 'void':
  61. continue
  62. if '*' in arg:
  63. arg_type, identifier = arg.rsplit('*', 1)
  64. arg_type += '*'
  65. identifier = identifier.strip()
  66. else:
  67. arg_type, identifier = arg.rsplit(None, 1)
  68. res.append((arg_type, identifier))
  69. return Arguments(res)
  70. def __getitem__(self, index):
  71. if isinstance(index, slice):
  72. return Arguments(self._args[index])
  73. else:
  74. return self._args[index]
  75. def __iter__(self):
  76. """Iterate over the (type, name) pairs."""
  77. return iter(self._args)
  78. def __len__(self):
  79. """Number of arguments."""
  80. return len(self._args)
  81. def __str__(self):
  82. """String suitable for declaring function arguments."""
  83. if len(self._args) == 0:
  84. return "void"
  85. else:
  86. return ", ".join([ " ".join([t, n]) for t,n in self._args ])
  87. def __repr__(self):
  88. """Evaluable string representation for this object."""
  89. return "Arguments(\"%s\")" % str(self)
  90. def names(self):
  91. """List of argument names."""
  92. return [ name for _, name in self._args ]
  93. def types(self):
  94. """List of argument types."""
  95. return [ type_ for type_, _ in self._args ]
  96. def casted(self):
  97. """List of argument names casted to their type."""
  98. return ["(%s)%s" % (type_, name) for type_, name in self._args]
  99. def transform(self, *trans):
  100. """Return a new Arguments instance with transformed types.
  101. The types in the resulting Arguments instance are transformed according
  102. to tracetool.transform.transform_type.
  103. """
  104. res = []
  105. for type_, name in self._args:
  106. res.append((tracetool.transform.transform_type(type_, *trans),
  107. name))
  108. return Arguments(res)
  109. class Event(object):
  110. """Event description.
  111. Attributes
  112. ----------
  113. name : str
  114. The event name.
  115. fmt : str
  116. The event format string.
  117. properties : set(str)
  118. Properties of the event.
  119. args : Arguments
  120. The event arguments.
  121. """
  122. _CRE = re.compile("((?P<props>[\w\s]+)\s+)?"
  123. "(?P<name>\w+)"
  124. "\((?P<args>[^)]*)\)"
  125. "\s*"
  126. "(?:(?:(?P<fmt_trans>\".+),)?\s*(?P<fmt>\".+))?"
  127. "\s*")
  128. _VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec", "vcpu"])
  129. def __init__(self, name, props, fmt, args, orig=None,
  130. event_trans=None, event_exec=None):
  131. """
  132. Parameters
  133. ----------
  134. name : string
  135. Event name.
  136. props : list of str
  137. Property names.
  138. fmt : str, list of str
  139. Event printing format (or formats).
  140. args : Arguments
  141. Event arguments.
  142. orig : Event or None
  143. Original Event before transformation/generation.
  144. event_trans : Event or None
  145. Generated translation-time event ("tcg" property).
  146. event_exec : Event or None
  147. Generated execution-time event ("tcg" property).
  148. """
  149. self.name = name
  150. self.properties = props
  151. self.fmt = fmt
  152. self.args = args
  153. self.event_trans = event_trans
  154. self.event_exec = event_exec
  155. if len(args) > 10:
  156. raise ValueError("Event '%s' has more than maximum permitted "
  157. "argument count" % name)
  158. if orig is None:
  159. self.original = weakref.ref(self)
  160. else:
  161. self.original = orig
  162. unknown_props = set(self.properties) - self._VALID_PROPS
  163. if len(unknown_props) > 0:
  164. raise ValueError("Unknown properties: %s"
  165. % ", ".join(unknown_props))
  166. assert isinstance(self.fmt, str) or len(self.fmt) == 2
  167. def copy(self):
  168. """Create a new copy."""
  169. return Event(self.name, list(self.properties), self.fmt,
  170. self.args.copy(), self, self.event_trans, self.event_exec)
  171. @staticmethod
  172. def build(line_str):
  173. """Build an Event instance from a string.
  174. Parameters
  175. ----------
  176. line_str : str
  177. Line describing the event.
  178. """
  179. m = Event._CRE.match(line_str)
  180. assert m is not None
  181. groups = m.groupdict('')
  182. name = groups["name"]
  183. props = groups["props"].split()
  184. fmt = groups["fmt"]
  185. fmt_trans = groups["fmt_trans"]
  186. if len(fmt_trans) > 0:
  187. fmt = [fmt_trans, fmt]
  188. args = Arguments.build(groups["args"])
  189. if "tcg-trans" in props:
  190. raise ValueError("Invalid property 'tcg-trans'")
  191. if "tcg-exec" in props:
  192. raise ValueError("Invalid property 'tcg-exec'")
  193. if "tcg" not in props and not isinstance(fmt, str):
  194. raise ValueError("Only events with 'tcg' property can have two formats")
  195. if "tcg" in props and isinstance(fmt, str):
  196. raise ValueError("Events with 'tcg' property must have two formats")
  197. event = Event(name, props, fmt, args)
  198. # add implicit arguments when using the 'vcpu' property
  199. import tracetool.vcpu
  200. event = tracetool.vcpu.transform_event(event)
  201. return event
  202. def __repr__(self):
  203. """Evaluable string representation for this object."""
  204. if isinstance(self.fmt, str):
  205. fmt = self.fmt
  206. else:
  207. fmt = "%s, %s" % (self.fmt[0], self.fmt[1])
  208. return "Event('%s %s(%s) %s')" % (" ".join(self.properties),
  209. self.name,
  210. self.args,
  211. fmt)
  212. _FMT = re.compile("(%[\d\.]*\w+|%.*PRI\S+)")
  213. def formats(self):
  214. """List of argument print formats."""
  215. assert not isinstance(self.fmt, list)
  216. return self._FMT.findall(self.fmt)
  217. QEMU_TRACE = "trace_%(name)s"
  218. QEMU_TRACE_NOCHECK = "_nocheck__" + QEMU_TRACE
  219. QEMU_TRACE_TCG = QEMU_TRACE + "_tcg"
  220. QEMU_DSTATE = "_TRACE_%(NAME)s_DSTATE"
  221. QEMU_BACKEND_DSTATE = "TRACE_%(NAME)s_BACKEND_DSTATE"
  222. QEMU_EVENT = "_TRACE_%(NAME)s_EVENT"
  223. def api(self, fmt=None):
  224. if fmt is None:
  225. fmt = Event.QEMU_TRACE
  226. return fmt % {"name": self.name, "NAME": self.name.upper()}
  227. def transform(self, *trans):
  228. """Return a new Event with transformed Arguments."""
  229. return Event(self.name,
  230. list(self.properties),
  231. self.fmt,
  232. self.args.transform(*trans),
  233. self)
  234. def read_events(fobj):
  235. """Generate the output for the given (format, backends) pair.
  236. Parameters
  237. ----------
  238. fobj : file
  239. Event description file.
  240. Returns a list of Event objects
  241. """
  242. events = []
  243. for line in fobj:
  244. if not line.strip():
  245. continue
  246. if line.lstrip().startswith('#'):
  247. continue
  248. event = Event.build(line)
  249. # transform TCG-enabled events
  250. if "tcg" not in event.properties:
  251. events.append(event)
  252. else:
  253. event_trans = event.copy()
  254. event_trans.name += "_trans"
  255. event_trans.properties += ["tcg-trans"]
  256. event_trans.fmt = event.fmt[0]
  257. # ignore TCG arguments
  258. args_trans = []
  259. for atrans, aorig in zip(
  260. event_trans.transform(tracetool.transform.TCG_2_HOST).args,
  261. event.args):
  262. if atrans == aorig:
  263. args_trans.append(atrans)
  264. event_trans.args = Arguments(args_trans)
  265. event_exec = event.copy()
  266. event_exec.name += "_exec"
  267. event_exec.properties += ["tcg-exec"]
  268. event_exec.fmt = event.fmt[1]
  269. event_exec.args = event_exec.args.transform(tracetool.transform.TCG_2_HOST)
  270. new_event = [event_trans, event_exec]
  271. event.event_trans, event.event_exec = new_event
  272. events.extend(new_event)
  273. return events
  274. class TracetoolError (Exception):
  275. """Exception for calls to generate."""
  276. pass
  277. def try_import(mod_name, attr_name=None, attr_default=None):
  278. """Try to import a module and get an attribute from it.
  279. Parameters
  280. ----------
  281. mod_name : str
  282. Module name.
  283. attr_name : str, optional
  284. Name of an attribute in the module.
  285. attr_default : optional
  286. Default value if the attribute does not exist in the module.
  287. Returns
  288. -------
  289. A pair indicating whether the module could be imported and the module or
  290. object or attribute value.
  291. """
  292. try:
  293. module = __import__(mod_name, globals(), locals(), ["__package__"])
  294. if attr_name is None:
  295. return True, module
  296. return True, getattr(module, str(attr_name), attr_default)
  297. except ImportError:
  298. return False, None
  299. def generate(events, group, format, backends,
  300. binary=None, probe_prefix=None):
  301. """Generate the output for the given (format, backends) pair.
  302. Parameters
  303. ----------
  304. events : list
  305. list of Event objects to generate for
  306. group: str
  307. Name of the tracing group
  308. format : str
  309. Output format name.
  310. backends : list
  311. Output backend names.
  312. binary : str or None
  313. See tracetool.backend.dtrace.BINARY.
  314. probe_prefix : str or None
  315. See tracetool.backend.dtrace.PROBEPREFIX.
  316. """
  317. # fix strange python error (UnboundLocalError tracetool)
  318. import tracetool
  319. format = str(format)
  320. if len(format) is 0:
  321. raise TracetoolError("format not set")
  322. if not tracetool.format.exists(format):
  323. raise TracetoolError("unknown format: %s" % format)
  324. if len(backends) is 0:
  325. raise TracetoolError("no backends specified")
  326. for backend in backends:
  327. if not tracetool.backend.exists(backend):
  328. raise TracetoolError("unknown backend: %s" % backend)
  329. backend = tracetool.backend.Wrapper(backends, format)
  330. import tracetool.backend.dtrace
  331. tracetool.backend.dtrace.BINARY = binary
  332. tracetool.backend.dtrace.PROBEPREFIX = probe_prefix
  333. tracetool.format.generate(events, format, backend, group)