2
0

replay-dump.py 12 KB

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