replay-dump.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 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. from __future__ import print_function
  21. import argparse
  22. import struct
  23. from collections import namedtuple
  24. # This mirrors some of the global replay state which some of the
  25. # stream loading refers to. Some decoders may read the next event so
  26. # we need handle that case. Calling reuse_event will ensure the next
  27. # event is read from the cache rather than advancing the file.
  28. class ReplayState(object):
  29. def __init__(self):
  30. self.event = -1
  31. self.event_count = 0
  32. self.already_read = False
  33. self.current_checkpoint = 0
  34. self.checkpoint = 0
  35. def set_event(self, ev):
  36. self.event = ev
  37. self.event_count += 1
  38. def get_event(self):
  39. self.already_read = False
  40. return self.event
  41. def reuse_event(self, ev):
  42. self.event = ev
  43. self.already_read = True
  44. def set_checkpoint(self):
  45. self.checkpoint = self.event - self.checkpoint_start
  46. def get_checkpoint(self):
  47. return self.checkpoint
  48. replay_state = ReplayState()
  49. # Simple read functions that mirror replay-internal.c
  50. # The file-stream is big-endian and manually written out a byte at a time.
  51. def read_byte(fin):
  52. "Read a single byte"
  53. return struct.unpack('>B', fin.read(1))[0]
  54. def read_event(fin):
  55. "Read a single byte event, but save some state"
  56. if replay_state.already_read:
  57. return replay_state.get_event()
  58. else:
  59. replay_state.set_event(read_byte(fin))
  60. return replay_state.event
  61. def read_word(fin):
  62. "Read a 16 bit word"
  63. return struct.unpack('>H', fin.read(2))[0]
  64. def read_dword(fin):
  65. "Read a 32 bit word"
  66. return struct.unpack('>I', fin.read(4))[0]
  67. def read_qword(fin):
  68. "Read a 64 bit word"
  69. return struct.unpack('>Q', fin.read(8))[0]
  70. # Generic decoder structure
  71. Decoder = namedtuple("Decoder", "eid name fn")
  72. def call_decode(table, index, dumpfile):
  73. "Search decode table for next step"
  74. decoder = next((d for d in table if d.eid == index), None)
  75. if not decoder:
  76. print("Could not decode index: %d" % (index))
  77. print("Entry is: %s" % (decoder))
  78. print("Decode Table is:\n%s" % (table))
  79. return False
  80. else:
  81. return decoder.fn(decoder.eid, decoder.name, dumpfile)
  82. # Print event
  83. def print_event(eid, name, string=None, event_count=None):
  84. "Print event with count"
  85. if not event_count:
  86. event_count = replay_state.event_count
  87. if string:
  88. print("%d:%s(%d) %s" % (event_count, name, eid, string))
  89. else:
  90. print("%d:%s(%d)" % (event_count, name, eid))
  91. # Decoders for each event type
  92. def decode_unimp(eid, name, _unused_dumpfile):
  93. "Unimplimented decoder, will trigger exit"
  94. print("%s not handled - will now stop" % (name))
  95. return False
  96. # Checkpoint decoder
  97. def swallow_async_qword(eid, name, dumpfile):
  98. "Swallow a qword of data without looking at it"
  99. step_id = read_qword(dumpfile)
  100. print(" %s(%d) @ %d" % (name, eid, step_id))
  101. return True
  102. async_decode_table = [ Decoder(0, "REPLAY_ASYNC_EVENT_BH", swallow_async_qword),
  103. Decoder(1, "REPLAY_ASYNC_INPUT", decode_unimp),
  104. Decoder(2, "REPLAY_ASYNC_INPUT_SYNC", decode_unimp),
  105. Decoder(3, "REPLAY_ASYNC_CHAR_READ", decode_unimp),
  106. Decoder(4, "REPLAY_ASYNC_EVENT_BLOCK", decode_unimp),
  107. Decoder(5, "REPLAY_ASYNC_EVENT_NET", decode_unimp),
  108. ]
  109. # See replay_read_events/replay_read_event
  110. def decode_async(eid, name, dumpfile):
  111. """Decode an ASYNC event"""
  112. print_event(eid, name)
  113. async_event_kind = read_byte(dumpfile)
  114. async_event_checkpoint = read_byte(dumpfile)
  115. if async_event_checkpoint != replay_state.current_checkpoint:
  116. print(" mismatch between checkpoint %d and async data %d" % (
  117. replay_state.current_checkpoint, async_event_checkpoint))
  118. return True
  119. return call_decode(async_decode_table, async_event_kind, dumpfile)
  120. def decode_instruction(eid, name, dumpfile):
  121. ins_diff = read_dword(dumpfile)
  122. print_event(eid, name, "0x%x" % (ins_diff))
  123. return True
  124. def decode_audio_out(eid, name, dumpfile):
  125. audio_data = read_dword(dumpfile)
  126. print_event(eid, name, "%d" % (audio_data))
  127. return True
  128. def decode_checkpoint(eid, name, dumpfile):
  129. """Decode a checkpoint.
  130. Checkpoints contain a series of async events with their own specific data.
  131. """
  132. replay_state.set_checkpoint()
  133. # save event count as we peek ahead
  134. event_number = replay_state.event_count
  135. next_event = read_event(dumpfile)
  136. # if the next event is EVENT_ASYNC there are a bunch of
  137. # async events to read, otherwise we are done
  138. if next_event != 3:
  139. print_event(eid, name, "no additional data", event_number)
  140. else:
  141. print_event(eid, name, "more data follows", event_number)
  142. replay_state.reuse_event(next_event)
  143. return True
  144. def decode_checkpoint_init(eid, name, dumpfile):
  145. print_event(eid, name)
  146. return True
  147. def decode_interrupt(eid, name, dumpfile):
  148. print_event(eid, name)
  149. return True
  150. def decode_clock(eid, name, dumpfile):
  151. clock_data = read_qword(dumpfile)
  152. print_event(eid, name, "0x%x" % (clock_data))
  153. return True
  154. # pre-MTTCG merge
  155. v5_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
  156. Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
  157. Decoder(2, "EVENT_EXCEPTION", decode_unimp),
  158. Decoder(3, "EVENT_ASYNC", decode_async),
  159. Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
  160. Decoder(5, "EVENT_CHAR_WRITE", decode_unimp),
  161. Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
  162. Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
  163. Decoder(8, "EVENT_CLOCK_HOST", decode_clock),
  164. Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
  165. Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
  166. Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
  167. Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
  168. Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
  169. Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
  170. Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
  171. Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
  172. Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init),
  173. Decoder(18, "EVENT_CP_RESET", decode_checkpoint),
  174. ]
  175. # post-MTTCG merge, AUDIO support added
  176. v6_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
  177. Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
  178. Decoder(2, "EVENT_EXCEPTION", decode_unimp),
  179. Decoder(3, "EVENT_ASYNC", decode_async),
  180. Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
  181. Decoder(5, "EVENT_CHAR_WRITE", decode_unimp),
  182. Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
  183. Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
  184. Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out),
  185. Decoder(9, "EVENT_AUDIO_IN", decode_unimp),
  186. Decoder(10, "EVENT_CLOCK_HOST", decode_clock),
  187. Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
  188. Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
  189. Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
  190. Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
  191. Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
  192. Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
  193. Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
  194. Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
  195. Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init),
  196. Decoder(20, "EVENT_CP_RESET", decode_checkpoint),
  197. ]
  198. # Shutdown cause added
  199. v7_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
  200. Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
  201. Decoder(2, "EVENT_EXCEPTION", decode_unimp),
  202. Decoder(3, "EVENT_ASYNC", decode_async),
  203. Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
  204. Decoder(5, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
  205. Decoder(6, "EVENT_SHUTDOWN_HOST_QMP", decode_unimp),
  206. Decoder(7, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
  207. Decoder(8, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
  208. Decoder(9, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
  209. Decoder(10, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
  210. Decoder(11, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
  211. Decoder(12, "EVENT_SHUTDOWN___MAX", decode_unimp),
  212. Decoder(13, "EVENT_CHAR_WRITE", decode_unimp),
  213. Decoder(14, "EVENT_CHAR_READ_ALL", decode_unimp),
  214. Decoder(15, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
  215. Decoder(16, "EVENT_AUDIO_OUT", decode_audio_out),
  216. Decoder(17, "EVENT_AUDIO_IN", decode_unimp),
  217. Decoder(18, "EVENT_CLOCK_HOST", decode_clock),
  218. Decoder(19, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
  219. Decoder(20, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
  220. Decoder(21, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
  221. Decoder(22, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
  222. Decoder(23, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
  223. Decoder(24, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
  224. Decoder(25, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
  225. Decoder(26, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
  226. Decoder(27, "EVENT_CP_INIT", decode_checkpoint_init),
  227. Decoder(28, "EVENT_CP_RESET", decode_checkpoint),
  228. ]
  229. def parse_arguments():
  230. "Grab arguments for script"
  231. parser = argparse.ArgumentParser()
  232. parser.add_argument("-f", "--file", help='record/replay dump to read from',
  233. required=True)
  234. return parser.parse_args()
  235. def decode_file(filename):
  236. "Decode a record/replay dump"
  237. dumpfile = open(filename, "rb")
  238. # read and throwaway the header
  239. version = read_dword(dumpfile)
  240. junk = read_qword(dumpfile)
  241. print("HEADER: version 0x%x" % (version))
  242. if version == 0xe02007:
  243. event_decode_table = v7_event_table
  244. replay_state.checkpoint_start = 12
  245. elif version == 0xe02006:
  246. event_decode_table = v6_event_table
  247. replay_state.checkpoint_start = 12
  248. else:
  249. event_decode_table = v5_event_table
  250. replay_state.checkpoint_start = 10
  251. try:
  252. decode_ok = True
  253. while decode_ok:
  254. event = read_event(dumpfile)
  255. decode_ok = call_decode(event_decode_table, event, dumpfile)
  256. finally:
  257. dumpfile.close()
  258. if __name__ == "__main__":
  259. args = parse_arguments()
  260. decode_file(args.file)