replay-dump.py 18 KB

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