replay-dump.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Dump the contents of a recorded execution stream
  5. #
  6. # Copyright (c) 2017 Alex Bennée <alex.bennee@linaro.org>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. import argparse
  21. import struct
  22. import os
  23. from collections import namedtuple
  24. from os import path
  25. # This mirrors some of the global replay state which some of the
  26. # stream loading refers to. Some decoders may read the next event so
  27. # we need handle that case. Calling reuse_event will ensure the next
  28. # event is read from the cache rather than advancing the file.
  29. class ReplayState(object):
  30. def __init__(self):
  31. self.event = -1
  32. self.event_count = 0
  33. self.already_read = False
  34. self.current_checkpoint = 0
  35. self.checkpoint = 0
  36. def set_event(self, ev):
  37. self.event = ev
  38. self.event_count += 1
  39. def get_event(self):
  40. self.already_read = False
  41. return self.event
  42. def reuse_event(self, ev):
  43. self.event = ev
  44. self.already_read = True
  45. def set_checkpoint(self):
  46. self.checkpoint = self.event - self.checkpoint_start
  47. def get_checkpoint(self):
  48. return self.checkpoint
  49. replay_state = ReplayState()
  50. # Simple read functions that mirror replay-internal.c
  51. # The file-stream is big-endian and manually written out a byte at a time.
  52. def read_byte(fin):
  53. "Read a single byte"
  54. return struct.unpack('>B', fin.read(1))[0]
  55. def read_event(fin):
  56. "Read a single byte event, but save some state"
  57. if replay_state.already_read:
  58. return replay_state.get_event()
  59. else:
  60. replay_state.set_event(read_byte(fin))
  61. return replay_state.event
  62. def read_word(fin):
  63. "Read a 16 bit word"
  64. return struct.unpack('>H', fin.read(2))[0]
  65. def read_dword(fin):
  66. "Read a 32 bit word"
  67. return struct.unpack('>I', fin.read(4))[0]
  68. def read_qword(fin):
  69. "Read a 64 bit word"
  70. return struct.unpack('>Q', fin.read(8))[0]
  71. def read_array(fin):
  72. "Read a sized array"
  73. size = read_dword(fin)
  74. data = fin.read(size)
  75. return data
  76. # Generic decoder structure
  77. Decoder = namedtuple("Decoder", "eid name fn")
  78. def call_decode(table, index, dumpfile):
  79. "Search decode table for next step"
  80. decoder = next((d for d in table if d.eid == index), None)
  81. if not decoder:
  82. print("Could not decode index: %d" % (index))
  83. print("Entry is: %s" % (decoder))
  84. print("Decode Table is:\n%s" % (table))
  85. return False
  86. else:
  87. return decoder.fn(decoder.eid, decoder.name, dumpfile)
  88. # Print event
  89. def print_event(eid, name, string=None, event_count=None):
  90. "Print event with count"
  91. if not event_count:
  92. event_count = replay_state.event_count
  93. if string:
  94. print("%d:%s(%d) %s" % (event_count, name, eid, string))
  95. else:
  96. print("%d:%s(%d)" % (event_count, name, eid))
  97. # Decoders for each event type
  98. def decode_unimp(eid, name, _unused_dumpfile):
  99. "Unimplemented decoder, will trigger exit"
  100. print("%s not handled - will now stop" % (name))
  101. return False
  102. def decode_plain(eid, name, _unused_dumpfile):
  103. "Plain events without additional data"
  104. print_event(eid, name, "no data")
  105. return True
  106. # Checkpoint decoder
  107. def swallow_async_qword(eid, name, dumpfile):
  108. "Swallow a qword of data without looking at it"
  109. step_id = read_qword(dumpfile)
  110. print(" %s(%d) @ %d" % (name, eid, step_id))
  111. return True
  112. def swallow_bytes(eid, name, dumpfile, nr):
  113. """Swallow nr bytes of data without looking at it"""
  114. dumpfile.seek(nr, os.SEEK_CUR)
  115. def decode_exception(eid, name, dumpfile):
  116. print_event(eid, name)
  117. return True
  118. # v12 does away with the additional event byte and encodes it in the main type
  119. # Between v8 and v9, REPLAY_ASYNC_BH_ONESHOT was added, but we don't decode
  120. # those versions so leave it out.
  121. async_decode_table = [ Decoder(0, "REPLAY_ASYNC_EVENT_BH", swallow_async_qword),
  122. Decoder(1, "REPLAY_ASYNC_INPUT", decode_unimp),
  123. Decoder(2, "REPLAY_ASYNC_INPUT_SYNC", decode_unimp),
  124. Decoder(3, "REPLAY_ASYNC_CHAR_READ", decode_unimp),
  125. Decoder(4, "REPLAY_ASYNC_EVENT_BLOCK", decode_unimp),
  126. Decoder(5, "REPLAY_ASYNC_EVENT_NET", decode_unimp),
  127. ]
  128. # See replay_read_events/replay_read_event
  129. def decode_async_old(eid, name, dumpfile):
  130. """Decode an ASYNC event (pre-v8)"""
  131. print_event(eid, name)
  132. async_event_kind = read_byte(dumpfile)
  133. async_event_checkpoint = read_byte(dumpfile)
  134. if async_event_checkpoint != replay_state.current_checkpoint:
  135. print(" mismatch between checkpoint %d and async data %d" % (
  136. replay_state.current_checkpoint, async_event_checkpoint))
  137. return True
  138. return call_decode(async_decode_table, async_event_kind, dumpfile)
  139. def decode_async_bh(eid, name, dumpfile):
  140. op_id = read_qword(dumpfile)
  141. print_event(eid, name)
  142. return True
  143. def decode_async_bh_oneshot(eid, name, dumpfile):
  144. op_id = read_qword(dumpfile)
  145. print_event(eid, name)
  146. return True
  147. def decode_async_char_read(eid, name, dumpfile):
  148. char_id = read_byte(dumpfile)
  149. size = read_dword(dumpfile)
  150. print_event(eid, name, "device:%x chars:%s" % (char_id, dumpfile.read(size)))
  151. return True
  152. def decode_async_block(eid, name, dumpfile):
  153. op_id = read_qword(dumpfile)
  154. print_event(eid, name)
  155. return True
  156. def decode_async_net(eid, name, dumpfile):
  157. net_id = read_byte(dumpfile)
  158. flags = read_dword(dumpfile)
  159. size = read_dword(dumpfile)
  160. swallow_bytes(eid, name, dumpfile, size)
  161. print_event(eid, name, "net:%x flags:%x bytes:%d" % (net_id, flags, size))
  162. return True
  163. total_insns = 0
  164. def decode_instruction(eid, name, dumpfile):
  165. global total_insns
  166. ins_diff = read_dword(dumpfile)
  167. total_insns += ins_diff
  168. print_event(eid, name, "+ %d -> %d" % (ins_diff, total_insns))
  169. return True
  170. def decode_shutdown(eid, name, dumpfile):
  171. print_event(eid, name)
  172. return True
  173. def decode_char_write(eid, name, dumpfile):
  174. res = read_dword(dumpfile)
  175. offset = read_dword(dumpfile)
  176. print_event(eid, name, "%d -> %d" % (offset, res))
  177. return True
  178. def decode_audio_out(eid, name, dumpfile):
  179. audio_data = read_dword(dumpfile)
  180. print_event(eid, name, "%d" % (audio_data))
  181. return True
  182. def __decode_checkpoint(eid, name, dumpfile, old):
  183. """Decode a checkpoint.
  184. Checkpoints contain a series of async events with their own specific data.
  185. """
  186. replay_state.set_checkpoint()
  187. # save event count as we peek ahead
  188. event_number = replay_state.event_count
  189. next_event = read_event(dumpfile)
  190. # if the next event is EVENT_ASYNC there are a bunch of
  191. # async events to read, otherwise we are done
  192. if (old and next_event == 3) or (not old and next_event >= 3 and next_event <= 9):
  193. print_event(eid, name, "more data follows", event_number)
  194. else:
  195. print_event(eid, name, "no additional data", event_number)
  196. replay_state.reuse_event(next_event)
  197. return True
  198. def decode_checkpoint_old(eid, name, dumpfile):
  199. return __decode_checkpoint(eid, name, dumpfile, False)
  200. def decode_checkpoint(eid, name, dumpfile):
  201. return __decode_checkpoint(eid, name, dumpfile, True)
  202. def decode_checkpoint_init(eid, name, dumpfile):
  203. print_event(eid, name)
  204. return True
  205. def decode_interrupt(eid, name, dumpfile):
  206. print_event(eid, name)
  207. return True
  208. def decode_clock(eid, name, dumpfile):
  209. clock_data = read_qword(dumpfile)
  210. print_event(eid, name, "0x%x" % (clock_data))
  211. return True
  212. def decode_random(eid, name, dumpfile):
  213. ret = read_dword(dumpfile)
  214. size = read_dword(dumpfile)
  215. swallow_bytes(eid, name, dumpfile, size)
  216. if (ret):
  217. print_event(eid, name, "%d bytes (getrandom failed)" % (size))
  218. else:
  219. print_event(eid, name, "%d bytes" % (size))
  220. return True
  221. def decode_end(eid, name, dumpfile):
  222. print_event(eid, name)
  223. return False
  224. # pre-MTTCG merge
  225. v5_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
  226. Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
  227. Decoder(2, "EVENT_EXCEPTION", decode_plain),
  228. Decoder(3, "EVENT_ASYNC", decode_async_old),
  229. Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
  230. Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
  231. Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
  232. Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
  233. Decoder(8, "EVENT_CLOCK_HOST", decode_clock),
  234. Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
  235. Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
  236. Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
  237. Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
  238. Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
  239. Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
  240. Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
  241. Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
  242. Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init),
  243. Decoder(18, "EVENT_CP_RESET", decode_checkpoint),
  244. ]
  245. # post-MTTCG merge, AUDIO support added
  246. v6_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
  247. Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
  248. Decoder(2, "EVENT_EXCEPTION", decode_plain),
  249. Decoder(3, "EVENT_ASYNC", decode_async_old),
  250. Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
  251. Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
  252. Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
  253. Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
  254. Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out),
  255. Decoder(9, "EVENT_AUDIO_IN", decode_unimp),
  256. Decoder(10, "EVENT_CLOCK_HOST", decode_clock),
  257. Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
  258. Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
  259. Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
  260. Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
  261. Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
  262. Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
  263. Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
  264. Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
  265. Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init),
  266. Decoder(20, "EVENT_CP_RESET", decode_checkpoint),
  267. ]
  268. # Shutdown cause added
  269. v7_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
  270. Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
  271. Decoder(2, "EVENT_EXCEPTION", decode_unimp),
  272. Decoder(3, "EVENT_ASYNC", decode_async_old),
  273. Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
  274. Decoder(5, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
  275. Decoder(6, "EVENT_SHUTDOWN_HOST_QMP", decode_unimp),
  276. Decoder(7, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
  277. Decoder(8, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
  278. Decoder(9, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
  279. Decoder(10, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
  280. Decoder(11, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
  281. Decoder(12, "EVENT_SHUTDOWN___MAX", decode_unimp),
  282. Decoder(13, "EVENT_CHAR_WRITE", decode_char_write),
  283. Decoder(14, "EVENT_CHAR_READ_ALL", decode_unimp),
  284. Decoder(15, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
  285. Decoder(16, "EVENT_AUDIO_OUT", decode_audio_out),
  286. Decoder(17, "EVENT_AUDIO_IN", decode_unimp),
  287. Decoder(18, "EVENT_CLOCK_HOST", decode_clock),
  288. Decoder(19, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
  289. Decoder(20, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
  290. Decoder(21, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
  291. Decoder(22, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
  292. Decoder(23, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
  293. Decoder(24, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
  294. Decoder(25, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
  295. Decoder(26, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
  296. Decoder(27, "EVENT_CP_INIT", decode_checkpoint_init),
  297. Decoder(28, "EVENT_CP_RESET", decode_checkpoint),
  298. ]
  299. v12_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
  300. Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
  301. Decoder(2, "EVENT_EXCEPTION", decode_exception),
  302. Decoder(3, "EVENT_ASYNC_BH", decode_async_bh),
  303. Decoder(4, "EVENT_ASYNC_BH_ONESHOT", decode_async_bh_oneshot),
  304. Decoder(5, "EVENT_ASYNC_INPUT", decode_unimp),
  305. Decoder(6, "EVENT_ASYNC_INPUT_SYNC", decode_unimp),
  306. Decoder(7, "EVENT_ASYNC_CHAR_READ", decode_async_char_read),
  307. Decoder(8, "EVENT_ASYNC_BLOCK", decode_async_block),
  308. Decoder(9, "EVENT_ASYNC_NET", decode_async_net),
  309. Decoder(10, "EVENT_SHUTDOWN", decode_shutdown),
  310. Decoder(11, "EVENT_SHUTDOWN_HOST_ERR", decode_shutdown),
  311. Decoder(12, "EVENT_SHUTDOWN_HOST_QMP_QUIT", decode_shutdown),
  312. Decoder(13, "EVENT_SHUTDOWN_HOST_QMP_RESET", decode_shutdown),
  313. Decoder(14, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_shutdown),
  314. Decoder(15, "EVENT_SHUTDOWN_HOST_UI", decode_shutdown),
  315. Decoder(16, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_shutdown),
  316. Decoder(17, "EVENT_SHUTDOWN_GUEST_RESET", decode_shutdown),
  317. Decoder(18, "EVENT_SHUTDOWN_GUEST_PANIC", decode_shutdown),
  318. Decoder(19, "EVENT_SHUTDOWN_SUBSYS_RESET", decode_shutdown),
  319. Decoder(20, "EVENT_SHUTDOWN_SNAPSHOT_LOAD", decode_shutdown),
  320. Decoder(21, "EVENT_SHUTDOWN___MAX", decode_shutdown),
  321. Decoder(22, "EVENT_CHAR_WRITE", decode_char_write),
  322. Decoder(23, "EVENT_CHAR_READ_ALL", decode_unimp),
  323. Decoder(24, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
  324. Decoder(25, "EVENT_AUDIO_OUT", decode_audio_out),
  325. Decoder(26, "EVENT_AUDIO_IN", decode_unimp),
  326. Decoder(27, "EVENT_RANDOM", decode_random),
  327. Decoder(28, "EVENT_CLOCK_HOST", decode_clock),
  328. Decoder(29, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
  329. Decoder(30, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
  330. Decoder(31, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
  331. Decoder(32, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
  332. Decoder(33, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
  333. Decoder(34, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
  334. Decoder(35, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
  335. Decoder(36, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
  336. Decoder(37, "EVENT_CP_INIT", decode_checkpoint_init),
  337. Decoder(38, "EVENT_CP_RESET", decode_checkpoint),
  338. Decoder(39, "EVENT_END", decode_end),
  339. ]
  340. def parse_arguments():
  341. "Grab arguments for script"
  342. parser = argparse.ArgumentParser()
  343. parser.add_argument("-f", "--file", help='record/replay dump to read from',
  344. required=True)
  345. return parser.parse_args()
  346. def decode_file(filename):
  347. "Decode a record/replay dump"
  348. dumpfile = open(filename, "rb")
  349. dumpsize = path.getsize(filename)
  350. # read and throwaway the header
  351. version = read_dword(dumpfile)
  352. junk = read_qword(dumpfile)
  353. # see REPLAY_VERSION
  354. print("HEADER: version 0x%x" % (version))
  355. if version == 0xe0200c:
  356. event_decode_table = v12_event_table
  357. replay_state.checkpoint_start = 30
  358. elif version == 0xe02007:
  359. event_decode_table = v7_event_table
  360. replay_state.checkpoint_start = 12
  361. elif version == 0xe02006:
  362. event_decode_table = v6_event_table
  363. replay_state.checkpoint_start = 12
  364. else:
  365. event_decode_table = v5_event_table
  366. replay_state.checkpoint_start = 10
  367. try:
  368. decode_ok = True
  369. while decode_ok:
  370. event = read_event(dumpfile)
  371. decode_ok = call_decode(event_decode_table, event,
  372. dumpfile)
  373. except Exception as inst:
  374. print(f"error {inst}")
  375. finally:
  376. print(f"Reached {dumpfile.tell()} of {dumpsize} bytes")
  377. dumpfile.close()
  378. if __name__ == "__main__":
  379. args = parse_arguments()
  380. decode_file(args.file)