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