2
0

simpletrace.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #!/usr/bin/env python3
  2. #
  3. # Pretty-printer for simple trace backend binary trace files
  4. #
  5. # Copyright IBM, Corp. 2010
  6. #
  7. # This work is licensed under the terms of the GNU GPL, version 2. See
  8. # the COPYING file in the top-level directory.
  9. #
  10. # For help see docs/devel/tracing.rst
  11. import sys
  12. import struct
  13. import inspect
  14. import warnings
  15. from tracetool import read_events, Event
  16. from tracetool.backend.simple import is_string
  17. __all__ = ['Analyzer', 'Analyzer2', 'process', 'run']
  18. # This is the binary format that the QEMU "simple" trace backend
  19. # emits. There is no specification documentation because the format is
  20. # not guaranteed to be stable. Trace files must be parsed with the
  21. # same trace-events-all file and the same simpletrace.py file that
  22. # QEMU was built with.
  23. header_event_id = 0xffffffffffffffff
  24. header_magic = 0xf2b177cb0aa429b4
  25. dropped_event_id = 0xfffffffffffffffe
  26. record_type_mapping = 0
  27. record_type_event = 1
  28. log_header_fmt = '=QQQ'
  29. rec_header_fmt = '=QQII'
  30. rec_header_fmt_len = struct.calcsize(rec_header_fmt)
  31. class SimpleException(Exception):
  32. pass
  33. def read_header(fobj, hfmt):
  34. '''Read a trace record header'''
  35. hlen = struct.calcsize(hfmt)
  36. hdr = fobj.read(hlen)
  37. if len(hdr) != hlen:
  38. raise SimpleException('Error reading header. Wrong filetype provided?')
  39. return struct.unpack(hfmt, hdr)
  40. def get_mapping(fobj):
  41. (event_id, ) = struct.unpack('=Q', fobj.read(8))
  42. (len, ) = struct.unpack('=L', fobj.read(4))
  43. name = fobj.read(len).decode()
  44. return (event_id, name)
  45. def read_record(fobj):
  46. """Deserialize a trace record from a file into a tuple (event_num, timestamp, pid, args)."""
  47. event_id, timestamp_ns, record_length, record_pid = read_header(fobj, rec_header_fmt)
  48. args_payload = fobj.read(record_length - rec_header_fmt_len)
  49. return (event_id, timestamp_ns, record_pid, args_payload)
  50. def read_trace_header(fobj):
  51. """Read and verify trace file header"""
  52. _header_event_id, _header_magic, log_version = read_header(fobj, log_header_fmt)
  53. if _header_event_id != header_event_id:
  54. raise ValueError(f'Not a valid trace file, header id {_header_event_id} != {header_event_id}')
  55. if _header_magic != header_magic:
  56. raise ValueError(f'Not a valid trace file, header magic {_header_magic} != {header_magic}')
  57. if log_version not in [0, 2, 3, 4]:
  58. raise ValueError(f'Unknown version {log_version} of tracelog format!')
  59. if log_version != 4:
  60. raise ValueError(f'Log format {log_version} not supported with this QEMU release!')
  61. def read_trace_records(events, fobj, read_header):
  62. """Deserialize trace records from a file, yielding record tuples (event, event_num, timestamp, pid, arg1, ..., arg6).
  63. Args:
  64. event_mapping (str -> Event): events dict, indexed by name
  65. fobj (file): input file
  66. read_header (bool): whether headers were read from fobj
  67. """
  68. frameinfo = inspect.getframeinfo(inspect.currentframe())
  69. dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)",
  70. frameinfo.lineno + 1, frameinfo.filename)
  71. event_mapping = {e.name: e for e in events}
  72. event_mapping["dropped"] = dropped_event
  73. event_id_to_name = {dropped_event_id: "dropped"}
  74. # If there is no header assume event ID mapping matches events list
  75. if not read_header:
  76. for event_id, event in enumerate(events):
  77. event_id_to_name[event_id] = event.name
  78. while True:
  79. t = fobj.read(8)
  80. if len(t) == 0:
  81. break
  82. (rectype, ) = struct.unpack('=Q', t)
  83. if rectype == record_type_mapping:
  84. event_id, event_name = get_mapping(fobj)
  85. event_id_to_name[event_id] = event_name
  86. else:
  87. event_id, timestamp_ns, pid, args_payload = read_record(fobj)
  88. event_name = event_id_to_name[event_id]
  89. try:
  90. event = event_mapping[event_name]
  91. except KeyError as e:
  92. raise SimpleException(
  93. f'{e} event is logged but is not declared in the trace events'
  94. 'file, try using trace-events-all instead.'
  95. )
  96. offset = 0
  97. args = []
  98. for type, _ in event.args:
  99. if is_string(type):
  100. (length,) = struct.unpack_from('=L', args_payload, offset=offset)
  101. offset += 4
  102. s = args_payload[offset:offset+length]
  103. offset += length
  104. args.append(s)
  105. else:
  106. (value,) = struct.unpack_from('=Q', args_payload, offset=offset)
  107. offset += 8
  108. args.append(value)
  109. yield (event_mapping[event_name], event_name, timestamp_ns, pid) + tuple(args)
  110. class Analyzer:
  111. """[Deprecated. Refer to Analyzer2 instead.]
  112. A trace file analyzer which processes trace records.
  113. An analyzer can be passed to run() or process(). The begin() method is
  114. invoked, then each trace record is processed, and finally the end() method
  115. is invoked. When Analyzer is used as a context-manager (using the `with`
  116. statement), begin() and end() are called automatically.
  117. If a method matching a trace event name exists, it is invoked to process
  118. that trace record. Otherwise the catchall() method is invoked.
  119. Example:
  120. The following method handles the runstate_set(int new_state) trace event::
  121. def runstate_set(self, new_state):
  122. ...
  123. The method can also take a timestamp argument before the trace event
  124. arguments::
  125. def runstate_set(self, timestamp, new_state):
  126. ...
  127. Timestamps have the uint64_t type and are in nanoseconds.
  128. The pid can be included in addition to the timestamp and is useful when
  129. dealing with traces from multiple processes::
  130. def runstate_set(self, timestamp, pid, new_state):
  131. ...
  132. """
  133. def begin(self):
  134. """Called at the start of the trace."""
  135. pass
  136. def catchall(self, event, rec):
  137. """Called if no specific method for processing a trace event has been found."""
  138. pass
  139. def _build_fn(self, event):
  140. fn = getattr(self, event.name, None)
  141. if fn is None:
  142. # Return early to avoid costly call to inspect.getfullargspec
  143. return self.catchall
  144. event_argcount = len(event.args)
  145. fn_argcount = len(inspect.getfullargspec(fn)[0]) - 1
  146. if fn_argcount == event_argcount + 1:
  147. # Include timestamp as first argument
  148. return lambda _, rec: fn(*(rec[1:2] + rec[3:3 + event_argcount]))
  149. elif fn_argcount == event_argcount + 2:
  150. # Include timestamp and pid
  151. return lambda _, rec: fn(*rec[1:3 + event_argcount])
  152. else:
  153. # Just arguments, no timestamp or pid
  154. return lambda _, rec: fn(*rec[3:3 + event_argcount])
  155. def _process_event(self, rec_args, *, event, event_id, timestamp_ns, pid, **kwargs):
  156. warnings.warn(
  157. "Use of deprecated Analyzer class. Refer to Analyzer2 instead.",
  158. DeprecationWarning,
  159. )
  160. if not hasattr(self, '_fn_cache'):
  161. # NOTE: Cannot depend on downstream subclasses to have
  162. # super().__init__() because of legacy.
  163. self._fn_cache = {}
  164. rec = (event_id, timestamp_ns, pid, *rec_args)
  165. if event_id not in self._fn_cache:
  166. self._fn_cache[event_id] = self._build_fn(event)
  167. self._fn_cache[event_id](event, rec)
  168. def end(self):
  169. """Called at the end of the trace."""
  170. pass
  171. def __enter__(self):
  172. self.begin()
  173. return self
  174. def __exit__(self, exc_type, exc_val, exc_tb):
  175. if exc_type is None:
  176. self.end()
  177. return False
  178. class Analyzer2(Analyzer):
  179. """A trace file analyzer which processes trace records.
  180. An analyzer can be passed to run() or process(). The begin() method is
  181. invoked, then each trace record is processed, and finally the end() method
  182. is invoked. When Analyzer is used as a context-manager (using the `with`
  183. statement), begin() and end() are called automatically.
  184. If a method matching a trace event name exists, it is invoked to process
  185. that trace record. Otherwise the catchall() method is invoked.
  186. The methods are called with a set of keyword-arguments. These can be ignored
  187. using `**kwargs` or defined like any keyword-argument.
  188. The following keyword-arguments are available, but make sure to have an
  189. **kwargs to allow for unmatched arguments in the future:
  190. event: Event object of current trace
  191. event_id: The id of the event in the current trace file
  192. timestamp_ns: The timestamp in nanoseconds of the trace
  193. pid: The process id recorded for the given trace
  194. Example:
  195. The following method handles the runstate_set(int new_state) trace event::
  196. def runstate_set(self, new_state, **kwargs):
  197. ...
  198. The method can also explicitly take a timestamp keyword-argument with the
  199. trace event arguments::
  200. def runstate_set(self, new_state, *, timestamp_ns, **kwargs):
  201. ...
  202. Timestamps have the uint64_t type and are in nanoseconds.
  203. The pid can be included in addition to the timestamp and is useful when
  204. dealing with traces from multiple processes:
  205. def runstate_set(self, new_state, *, timestamp_ns, pid, **kwargs):
  206. ...
  207. """
  208. def catchall(self, *rec_args, event, timestamp_ns, pid, event_id, **kwargs):
  209. """Called if no specific method for processing a trace event has been found."""
  210. pass
  211. def _process_event(self, rec_args, *, event, **kwargs):
  212. fn = getattr(self, event.name, self.catchall)
  213. fn(*rec_args, event=event, **kwargs)
  214. def process(events, log, analyzer, read_header=True):
  215. """Invoke an analyzer on each event in a log.
  216. Args:
  217. events (file-object or list or str): events list or file-like object or file path as str to read event data from
  218. log (file-object or str): file-like object or file path as str to read log data from
  219. analyzer (Analyzer): Instance of Analyzer to interpret the event data
  220. read_header (bool, optional): Whether to read header data from the log data. Defaults to True.
  221. """
  222. if isinstance(events, str):
  223. with open(events, 'r') as f:
  224. events_list = read_events(f, events)
  225. elif isinstance(events, list):
  226. # Treat as a list of events already produced by tracetool.read_events
  227. events_list = events
  228. else:
  229. # Treat as an already opened file-object
  230. events_list = read_events(events, events.name)
  231. if isinstance(log, str):
  232. with open(log, 'rb') as log_fobj:
  233. _process(events_list, log_fobj, analyzer, read_header)
  234. else:
  235. # Treat `log` as an already opened file-object. We will not close it,
  236. # as we do not own it.
  237. _process(events_list, log, analyzer, read_header)
  238. def _process(events, log_fobj, analyzer, read_header=True):
  239. """Internal function for processing
  240. Args:
  241. events (list): list of events already produced by tracetool.read_events
  242. log_fobj (file): file-object to read log data from
  243. analyzer (Analyzer): the Analyzer to interpret the event data
  244. read_header (bool, optional): Whether to read header data from the log data. Defaults to True.
  245. """
  246. if read_header:
  247. read_trace_header(log_fobj)
  248. with analyzer:
  249. for event, event_id, timestamp_ns, record_pid, *rec_args in read_trace_records(events, log_fobj, read_header):
  250. analyzer._process_event(
  251. rec_args,
  252. event=event,
  253. event_id=event_id,
  254. timestamp_ns=timestamp_ns,
  255. pid=record_pid,
  256. )
  257. def run(analyzer):
  258. """Execute an analyzer on a trace file given on the command-line.
  259. This function is useful as a driver for simple analysis scripts. More
  260. advanced scripts will want to call process() instead."""
  261. try:
  262. # NOTE: See built-in `argparse` module for a more robust cli interface
  263. *no_header, trace_event_path, trace_file_path = sys.argv[1:]
  264. assert no_header == [] or no_header == ['--no-header'], 'Invalid no-header argument'
  265. except (AssertionError, ValueError):
  266. raise SimpleException(f'usage: {sys.argv[0]} [--no-header] <trace-events> <trace-file>\n')
  267. with open(trace_event_path, 'r') as events_fobj, open(trace_file_path, 'rb') as log_fobj:
  268. process(events_fobj, log_fobj, analyzer, read_header=not no_header)
  269. if __name__ == '__main__':
  270. class Formatter2(Analyzer2):
  271. def __init__(self):
  272. self.last_timestamp_ns = None
  273. def catchall(self, *rec_args, event, timestamp_ns, pid, event_id):
  274. if self.last_timestamp_ns is None:
  275. self.last_timestamp_ns = timestamp_ns
  276. delta_ns = timestamp_ns - self.last_timestamp_ns
  277. self.last_timestamp_ns = timestamp_ns
  278. fields = [
  279. f'{name}={r}' if is_string(type) else f'{name}=0x{r:x}'
  280. for r, (type, name) in zip(rec_args, event.args)
  281. ]
  282. print(f'{event.name} {delta_ns / 1000:0.3f} {pid=} ' + ' '.join(fields))
  283. try:
  284. run(Formatter2())
  285. except SimpleException as e:
  286. sys.stderr.write(str(e) + "\n")
  287. sys.exit(1)