xref: /qemu/scripts/replay-dump.py (revision 41e17cc8)
13d004a37SPhilippe Mathieu-Daudé#!/usr/bin/env python3
2821c1130SAlex Bennée# -*- coding: utf-8 -*-
3821c1130SAlex Bennée#
4821c1130SAlex Bennée# Dump the contents of a recorded execution stream
5821c1130SAlex Bennée#
649ebe9b1SAlex Bennée#  Copyright (c) 2017 Alex Bennée <alex.bennee@linaro.org>
7821c1130SAlex Bennée#
8821c1130SAlex Bennée# This library is free software; you can redistribute it and/or
9821c1130SAlex Bennée# modify it under the terms of the GNU Lesser General Public
10821c1130SAlex Bennée# License as published by the Free Software Foundation; either
1161f3c91aSChetan Pant# version 2.1 of the License, or (at your option) any later version.
12821c1130SAlex Bennée#
13821c1130SAlex Bennée# This library is distributed in the hope that it will be useful,
14821c1130SAlex Bennée# but WITHOUT ANY WARRANTY; without even the implied warranty of
15821c1130SAlex Bennée# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16821c1130SAlex Bennée# Lesser General Public License for more details.
17821c1130SAlex Bennée#
18821c1130SAlex Bennée# You should have received a copy of the GNU Lesser General Public
19821c1130SAlex Bennée# License along with this library; if not, see <http://www.gnu.org/licenses/>.
20821c1130SAlex Bennée
21821c1130SAlex Bennéeimport argparse
22821c1130SAlex Bennéeimport struct
23821c1130SAlex Bennéefrom collections import namedtuple
24fcc8c529SAlex Bennéefrom os import path
25821c1130SAlex Bennée
26821c1130SAlex Bennée# This mirrors some of the global replay state which some of the
27821c1130SAlex Bennée# stream loading refers to. Some decoders may read the next event so
28821c1130SAlex Bennée# we need handle that case. Calling reuse_event will ensure the next
29821c1130SAlex Bennée# event is read from the cache rather than advancing the file.
30821c1130SAlex Bennée
31821c1130SAlex Bennéeclass ReplayState(object):
32821c1130SAlex Bennée    def __init__(self):
33821c1130SAlex Bennée        self.event = -1
34821c1130SAlex Bennée        self.event_count = 0
35821c1130SAlex Bennée        self.already_read = False
36821c1130SAlex Bennée        self.current_checkpoint = 0
37821c1130SAlex Bennée        self.checkpoint = 0
38821c1130SAlex Bennée
39821c1130SAlex Bennée    def set_event(self, ev):
40821c1130SAlex Bennée        self.event = ev
41821c1130SAlex Bennée        self.event_count += 1
42821c1130SAlex Bennée
43821c1130SAlex Bennée    def get_event(self):
44821c1130SAlex Bennée        self.already_read = False
45821c1130SAlex Bennée        return self.event
46821c1130SAlex Bennée
47821c1130SAlex Bennée    def reuse_event(self, ev):
48821c1130SAlex Bennée        self.event = ev
49821c1130SAlex Bennée        self.already_read = True
50821c1130SAlex Bennée
51821c1130SAlex Bennée    def set_checkpoint(self):
52821c1130SAlex Bennée        self.checkpoint = self.event - self.checkpoint_start
53821c1130SAlex Bennée
54821c1130SAlex Bennée    def get_checkpoint(self):
55821c1130SAlex Bennée        return self.checkpoint
56821c1130SAlex Bennée
57821c1130SAlex Bennéereplay_state = ReplayState()
58821c1130SAlex Bennée
59821c1130SAlex Bennée# Simple read functions that mirror replay-internal.c
60821c1130SAlex Bennée# The file-stream is big-endian and manually written out a byte at a time.
61821c1130SAlex Bennée
62821c1130SAlex Bennéedef read_byte(fin):
63821c1130SAlex Bennée    "Read a single byte"
64821c1130SAlex Bennée    return struct.unpack('>B', fin.read(1))[0]
65821c1130SAlex Bennée
66821c1130SAlex Bennéedef read_event(fin):
67821c1130SAlex Bennée    "Read a single byte event, but save some state"
68821c1130SAlex Bennée    if replay_state.already_read:
69821c1130SAlex Bennée        return replay_state.get_event()
70821c1130SAlex Bennée    else:
71821c1130SAlex Bennée        replay_state.set_event(read_byte(fin))
72821c1130SAlex Bennée        return replay_state.event
73821c1130SAlex Bennée
74821c1130SAlex Bennéedef read_word(fin):
75821c1130SAlex Bennée    "Read a 16 bit word"
76821c1130SAlex Bennée    return struct.unpack('>H', fin.read(2))[0]
77821c1130SAlex Bennée
78821c1130SAlex Bennéedef read_dword(fin):
79821c1130SAlex Bennée    "Read a 32 bit word"
80821c1130SAlex Bennée    return struct.unpack('>I', fin.read(4))[0]
81821c1130SAlex Bennée
82821c1130SAlex Bennéedef read_qword(fin):
83821c1130SAlex Bennée    "Read a 64 bit word"
84821c1130SAlex Bennée    return struct.unpack('>Q', fin.read(8))[0]
85821c1130SAlex Bennée
86fcc8c529SAlex Bennéedef read_array(fin):
87fcc8c529SAlex Bennée    "Read a sized array"
88fcc8c529SAlex Bennée    size = read_dword(fin)
89fcc8c529SAlex Bennée    data = fin.read(size)
90fcc8c529SAlex Bennée    return data
91fcc8c529SAlex Bennée
92821c1130SAlex Bennée# Generic decoder structure
93821c1130SAlex BennéeDecoder = namedtuple("Decoder", "eid name fn")
94821c1130SAlex Bennée
95821c1130SAlex Bennéedef call_decode(table, index, dumpfile):
96821c1130SAlex Bennée    "Search decode table for next step"
97821c1130SAlex Bennée    decoder = next((d for d in table if d.eid == index), None)
98821c1130SAlex Bennée    if not decoder:
99f03868bdSEduardo Habkost        print("Could not decode index: %d" % (index))
100f03868bdSEduardo Habkost        print("Entry is: %s" % (decoder))
101f03868bdSEduardo Habkost        print("Decode Table is:\n%s" % (table))
102821c1130SAlex Bennée        return False
103821c1130SAlex Bennée    else:
104821c1130SAlex Bennée        return decoder.fn(decoder.eid, decoder.name, dumpfile)
105821c1130SAlex Bennée
106821c1130SAlex Bennée# Print event
107821c1130SAlex Bennéedef print_event(eid, name, string=None, event_count=None):
108821c1130SAlex Bennée    "Print event with count"
109821c1130SAlex Bennée    if not event_count:
110821c1130SAlex Bennée        event_count = replay_state.event_count
111821c1130SAlex Bennée
112821c1130SAlex Bennée    if string:
113f03868bdSEduardo Habkost        print("%d:%s(%d) %s" % (event_count, name, eid, string))
114821c1130SAlex Bennée    else:
115f03868bdSEduardo Habkost        print("%d:%s(%d)" % (event_count, name, eid))
116821c1130SAlex Bennée
117821c1130SAlex Bennée
118821c1130SAlex Bennée# Decoders for each event type
119821c1130SAlex Bennée
120821c1130SAlex Bennéedef decode_unimp(eid, name, _unused_dumpfile):
121d30b5bc9SMichael Tokarev    "Unimplemented decoder, will trigger exit"
122f03868bdSEduardo Habkost    print("%s not handled - will now stop" % (name))
123821c1130SAlex Bennée    return False
124821c1130SAlex Bennée
125fcc8c529SAlex Bennéedef decode_plain(eid, name, _unused_dumpfile):
126fcc8c529SAlex Bennée    "Plain events without additional data"
127fcc8c529SAlex Bennée    print_event(eid, name, "no data")
128fcc8c529SAlex Bennée    return True
129fcc8c529SAlex Bennée
130821c1130SAlex Bennée# Checkpoint decoder
131821c1130SAlex Bennéedef swallow_async_qword(eid, name, dumpfile):
132821c1130SAlex Bennée    "Swallow a qword of data without looking at it"
133821c1130SAlex Bennée    step_id = read_qword(dumpfile)
134f03868bdSEduardo Habkost    print("  %s(%d) @ %d" % (name, eid, step_id))
135821c1130SAlex Bennée    return True
136821c1130SAlex Bennée
137821c1130SAlex Bennéeasync_decode_table = [ Decoder(0, "REPLAY_ASYNC_EVENT_BH", swallow_async_qword),
138821c1130SAlex Bennée                       Decoder(1, "REPLAY_ASYNC_INPUT", decode_unimp),
139821c1130SAlex Bennée                       Decoder(2, "REPLAY_ASYNC_INPUT_SYNC", decode_unimp),
140821c1130SAlex Bennée                       Decoder(3, "REPLAY_ASYNC_CHAR_READ", decode_unimp),
141821c1130SAlex Bennée                       Decoder(4, "REPLAY_ASYNC_EVENT_BLOCK", decode_unimp),
142821c1130SAlex Bennée                       Decoder(5, "REPLAY_ASYNC_EVENT_NET", decode_unimp),
143821c1130SAlex Bennée]
144821c1130SAlex Bennée# See replay_read_events/replay_read_event
145821c1130SAlex Bennéedef decode_async(eid, name, dumpfile):
146821c1130SAlex Bennée    """Decode an ASYNC event"""
147821c1130SAlex Bennée
148821c1130SAlex Bennée    print_event(eid, name)
149821c1130SAlex Bennée
150821c1130SAlex Bennée    async_event_kind = read_byte(dumpfile)
151821c1130SAlex Bennée    async_event_checkpoint = read_byte(dumpfile)
152821c1130SAlex Bennée
153821c1130SAlex Bennée    if async_event_checkpoint != replay_state.current_checkpoint:
154f03868bdSEduardo Habkost        print("  mismatch between checkpoint %d and async data %d" % (
155f03868bdSEduardo Habkost            replay_state.current_checkpoint, async_event_checkpoint))
156821c1130SAlex Bennée        return True
157821c1130SAlex Bennée
158821c1130SAlex Bennée    return call_decode(async_decode_table, async_event_kind, dumpfile)
159821c1130SAlex Bennée
160*41e17cc8SAlex Bennéetotal_insns = 0
161821c1130SAlex Bennée
162821c1130SAlex Bennéedef decode_instruction(eid, name, dumpfile):
163*41e17cc8SAlex Bennée    global total_insns
164821c1130SAlex Bennée    ins_diff = read_dword(dumpfile)
165*41e17cc8SAlex Bennée    total_insns += ins_diff
166*41e17cc8SAlex Bennée    print_event(eid, name, "+ %d -> %d" % (ins_diff, total_insns))
167821c1130SAlex Bennée    return True
168821c1130SAlex Bennée
169fcc8c529SAlex Bennéedef decode_char_write(eid, name, dumpfile):
170fcc8c529SAlex Bennée    res = read_dword(dumpfile)
171fcc8c529SAlex Bennée    offset = read_dword(dumpfile)
172fcc8c529SAlex Bennée    print_event(eid, name, "%d -> %d" % (offset, res))
173fcc8c529SAlex Bennée    return True
174fcc8c529SAlex Bennée
175821c1130SAlex Bennéedef decode_audio_out(eid, name, dumpfile):
176821c1130SAlex Bennée    audio_data = read_dword(dumpfile)
177821c1130SAlex Bennée    print_event(eid, name, "%d" % (audio_data))
178821c1130SAlex Bennée    return True
179821c1130SAlex Bennée
180821c1130SAlex Bennéedef decode_checkpoint(eid, name, dumpfile):
181821c1130SAlex Bennée    """Decode a checkpoint.
182821c1130SAlex Bennée
183821c1130SAlex Bennée    Checkpoints contain a series of async events with their own specific data.
184821c1130SAlex Bennée    """
185821c1130SAlex Bennée    replay_state.set_checkpoint()
186821c1130SAlex Bennée    # save event count as we peek ahead
187821c1130SAlex Bennée    event_number = replay_state.event_count
188821c1130SAlex Bennée    next_event = read_event(dumpfile)
189821c1130SAlex Bennée
190821c1130SAlex Bennée    # if the next event is EVENT_ASYNC there are a bunch of
191821c1130SAlex Bennée    # async events to read, otherwise we are done
192821c1130SAlex Bennée    if next_event != 3:
193821c1130SAlex Bennée        print_event(eid, name, "no additional data", event_number)
194821c1130SAlex Bennée    else:
195821c1130SAlex Bennée        print_event(eid, name, "more data follows", event_number)
196821c1130SAlex Bennée
197821c1130SAlex Bennée    replay_state.reuse_event(next_event)
198821c1130SAlex Bennée    return True
199821c1130SAlex Bennée
200821c1130SAlex Bennéedef decode_checkpoint_init(eid, name, dumpfile):
201821c1130SAlex Bennée    print_event(eid, name)
202821c1130SAlex Bennée    return True
203821c1130SAlex Bennée
204821c1130SAlex Bennéedef decode_interrupt(eid, name, dumpfile):
205821c1130SAlex Bennée    print_event(eid, name)
206821c1130SAlex Bennée    return True
207821c1130SAlex Bennée
208821c1130SAlex Bennéedef decode_clock(eid, name, dumpfile):
209821c1130SAlex Bennée    clock_data = read_qword(dumpfile)
210821c1130SAlex Bennée    print_event(eid, name, "0x%x" % (clock_data))
211821c1130SAlex Bennée    return True
212821c1130SAlex Bennée
213fcc8c529SAlex Bennéedef decode_random(eid, name, dumpfile):
214fcc8c529SAlex Bennée    ret = read_dword(dumpfile)
215fcc8c529SAlex Bennée    data = read_array(dumpfile)
216fcc8c529SAlex Bennée    print_event(eid, "%d bytes of random data" % len(data))
217fcc8c529SAlex Bennée    return True
218821c1130SAlex Bennée
219821c1130SAlex Bennée# pre-MTTCG merge
220821c1130SAlex Bennéev5_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
221821c1130SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
222fcc8c529SAlex Bennée                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
223821c1130SAlex Bennée                  Decoder(3, "EVENT_ASYNC", decode_async),
224821c1130SAlex Bennée                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
225fcc8c529SAlex Bennée                  Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
226821c1130SAlex Bennée                  Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
227821c1130SAlex Bennée                  Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
228821c1130SAlex Bennée                  Decoder(8, "EVENT_CLOCK_HOST", decode_clock),
229821c1130SAlex Bennée                  Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
230821c1130SAlex Bennée                  Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
231821c1130SAlex Bennée                  Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
232821c1130SAlex Bennée                  Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
233821c1130SAlex Bennée                  Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
234821c1130SAlex Bennée                  Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
235821c1130SAlex Bennée                  Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
236821c1130SAlex Bennée                  Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
237821c1130SAlex Bennée                  Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init),
238821c1130SAlex Bennée                  Decoder(18, "EVENT_CP_RESET", decode_checkpoint),
239821c1130SAlex Bennée]
240821c1130SAlex Bennée
241821c1130SAlex Bennée# post-MTTCG merge, AUDIO support added
242821c1130SAlex Bennéev6_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
243821c1130SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
244fcc8c529SAlex Bennée                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
245821c1130SAlex Bennée                  Decoder(3, "EVENT_ASYNC", decode_async),
246821c1130SAlex Bennée                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
247fcc8c529SAlex Bennée                  Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
248821c1130SAlex Bennée                  Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
249821c1130SAlex Bennée                  Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
250821c1130SAlex Bennée                  Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out),
251821c1130SAlex Bennée                  Decoder(9, "EVENT_AUDIO_IN", decode_unimp),
252821c1130SAlex Bennée                  Decoder(10, "EVENT_CLOCK_HOST", decode_clock),
253821c1130SAlex Bennée                  Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
254821c1130SAlex Bennée                  Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
255821c1130SAlex Bennée                  Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
256821c1130SAlex Bennée                  Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
257821c1130SAlex Bennée                  Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
258821c1130SAlex Bennée                  Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
259821c1130SAlex Bennée                  Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
260821c1130SAlex Bennée                  Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
261821c1130SAlex Bennée                  Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init),
262821c1130SAlex Bennée                  Decoder(20, "EVENT_CP_RESET", decode_checkpoint),
263821c1130SAlex Bennée]
264821c1130SAlex Bennée
265821c1130SAlex Bennée# Shutdown cause added
266821c1130SAlex Bennéev7_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
267821c1130SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
268821c1130SAlex Bennée                  Decoder(2, "EVENT_EXCEPTION", decode_unimp),
269821c1130SAlex Bennée                  Decoder(3, "EVENT_ASYNC", decode_async),
270821c1130SAlex Bennée                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
271821c1130SAlex Bennée                  Decoder(5, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
272821c1130SAlex Bennée                  Decoder(6, "EVENT_SHUTDOWN_HOST_QMP", decode_unimp),
273821c1130SAlex Bennée                  Decoder(7, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
274821c1130SAlex Bennée                  Decoder(8, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
275821c1130SAlex Bennée                  Decoder(9, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
276821c1130SAlex Bennée                  Decoder(10, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
277821c1130SAlex Bennée                  Decoder(11, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
278821c1130SAlex Bennée                  Decoder(12, "EVENT_SHUTDOWN___MAX", decode_unimp),
279fcc8c529SAlex Bennée                  Decoder(13, "EVENT_CHAR_WRITE", decode_char_write),
280821c1130SAlex Bennée                  Decoder(14, "EVENT_CHAR_READ_ALL", decode_unimp),
281821c1130SAlex Bennée                  Decoder(15, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
282821c1130SAlex Bennée                  Decoder(16, "EVENT_AUDIO_OUT", decode_audio_out),
283821c1130SAlex Bennée                  Decoder(17, "EVENT_AUDIO_IN", decode_unimp),
284821c1130SAlex Bennée                  Decoder(18, "EVENT_CLOCK_HOST", decode_clock),
285821c1130SAlex Bennée                  Decoder(19, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
286821c1130SAlex Bennée                  Decoder(20, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
287821c1130SAlex Bennée                  Decoder(21, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
288821c1130SAlex Bennée                  Decoder(22, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
289821c1130SAlex Bennée                  Decoder(23, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
290821c1130SAlex Bennée                  Decoder(24, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
291821c1130SAlex Bennée                  Decoder(25, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
292821c1130SAlex Bennée                  Decoder(26, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
293821c1130SAlex Bennée                  Decoder(27, "EVENT_CP_INIT", decode_checkpoint_init),
294821c1130SAlex Bennée                  Decoder(28, "EVENT_CP_RESET", decode_checkpoint),
295821c1130SAlex Bennée]
296821c1130SAlex Bennée
297fcc8c529SAlex Bennéev12_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
298fcc8c529SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
299fcc8c529SAlex Bennée                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
300fcc8c529SAlex Bennée                  Decoder(3, "EVENT_ASYNC", decode_async),
301fcc8c529SAlex Bennée                  Decoder(4, "EVENT_ASYNC", decode_async),
302fcc8c529SAlex Bennée                  Decoder(5, "EVENT_ASYNC", decode_async),
303fcc8c529SAlex Bennée                  Decoder(6, "EVENT_ASYNC", decode_async),
304fcc8c529SAlex Bennée                  Decoder(6, "EVENT_ASYNC", decode_async),
305fcc8c529SAlex Bennée                  Decoder(8, "EVENT_ASYNC", decode_async),
306fcc8c529SAlex Bennée                  Decoder(9, "EVENT_ASYNC", decode_async),
307fcc8c529SAlex Bennée                  Decoder(10, "EVENT_ASYNC", decode_async),
308fcc8c529SAlex Bennée                  Decoder(11, "EVENT_SHUTDOWN", decode_unimp),
309fcc8c529SAlex Bennée                  Decoder(12, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
310fcc8c529SAlex Bennée                  Decoder(13, "EVENT_SHUTDOWN_HOST_QMP_QUIT", decode_unimp),
311fcc8c529SAlex Bennée                  Decoder(14, "EVENT_SHUTDOWN_HOST_QMP_RESET", decode_unimp),
312fcc8c529SAlex Bennée                  Decoder(14, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
313fcc8c529SAlex Bennée                  Decoder(15, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
314fcc8c529SAlex Bennée                  Decoder(16, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
315fcc8c529SAlex Bennée                  Decoder(17, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
316fcc8c529SAlex Bennée                  Decoder(18, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
317fcc8c529SAlex Bennée                  Decoder(19, "EVENT_SHUTDOWN_GUEST_SUBSYSTEM_RESET", decode_unimp),
318fcc8c529SAlex Bennée                  Decoder(20, "EVENT_SHUTDOWN_GUEST_SNAPSHOT_LOAD", decode_unimp),
319fcc8c529SAlex Bennée                  Decoder(21, "EVENT_SHUTDOWN___MAX", decode_unimp),
320fcc8c529SAlex Bennée                  Decoder(22, "EVENT_CHAR_WRITE", decode_char_write),
321fcc8c529SAlex Bennée                  Decoder(23, "EVENT_CHAR_READ_ALL", decode_unimp),
322fcc8c529SAlex Bennée                  Decoder(24, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
323fcc8c529SAlex Bennée                  Decoder(25, "EVENT_AUDIO_IN", decode_unimp),
324fcc8c529SAlex Bennée                  Decoder(26, "EVENT_AUDIO_OUT", decode_audio_out),
325fcc8c529SAlex Bennée                  Decoder(27, "EVENT_RANDOM", decode_random),
326fcc8c529SAlex Bennée                  Decoder(28, "EVENT_CLOCK_HOST", decode_clock),
327fcc8c529SAlex Bennée                  Decoder(29, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
328fcc8c529SAlex Bennée                  Decoder(30, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
329fcc8c529SAlex Bennée                  Decoder(31, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
330fcc8c529SAlex Bennée                  Decoder(32, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
331fcc8c529SAlex Bennée                  Decoder(33, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
332fcc8c529SAlex Bennée                  Decoder(34, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
333fcc8c529SAlex Bennée                  Decoder(35, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
334fcc8c529SAlex Bennée                  Decoder(36, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
335fcc8c529SAlex Bennée                  Decoder(37, "EVENT_CP_INIT", decode_checkpoint_init),
336fcc8c529SAlex Bennée                  Decoder(38, "EVENT_CP_RESET", decode_checkpoint),
337fcc8c529SAlex Bennée]
338fcc8c529SAlex Bennée
339821c1130SAlex Bennéedef parse_arguments():
340821c1130SAlex Bennée    "Grab arguments for script"
341821c1130SAlex Bennée    parser = argparse.ArgumentParser()
342821c1130SAlex Bennée    parser.add_argument("-f", "--file", help='record/replay dump to read from',
343821c1130SAlex Bennée                        required=True)
344821c1130SAlex Bennée    return parser.parse_args()
345821c1130SAlex Bennée
346821c1130SAlex Bennéedef decode_file(filename):
347821c1130SAlex Bennée    "Decode a record/replay dump"
348821c1130SAlex Bennée    dumpfile = open(filename, "rb")
349fcc8c529SAlex Bennée    dumpsize = path.getsize(filename)
350821c1130SAlex Bennée    # read and throwaway the header
351821c1130SAlex Bennée    version = read_dword(dumpfile)
352821c1130SAlex Bennée    junk = read_qword(dumpfile)
353821c1130SAlex Bennée
354fcc8c529SAlex Bennée    # see REPLAY_VERSION
355f03868bdSEduardo Habkost    print("HEADER: version 0x%x" % (version))
356821c1130SAlex Bennée
357fcc8c529SAlex Bennée    if version == 0xe0200c:
358fcc8c529SAlex Bennée        event_decode_table = v12_event_table
359fcc8c529SAlex Bennée        replay_state.checkpoint_start = 30
360fcc8c529SAlex Bennée    elif version == 0xe02007:
361821c1130SAlex Bennée        event_decode_table = v7_event_table
362821c1130SAlex Bennée        replay_state.checkpoint_start = 12
363821c1130SAlex Bennée    elif version == 0xe02006:
364821c1130SAlex Bennée        event_decode_table = v6_event_table
365821c1130SAlex Bennée        replay_state.checkpoint_start = 12
366821c1130SAlex Bennée    else:
367821c1130SAlex Bennée        event_decode_table = v5_event_table
368821c1130SAlex Bennée        replay_state.checkpoint_start = 10
369821c1130SAlex Bennée
370821c1130SAlex Bennée    try:
371821c1130SAlex Bennée        decode_ok = True
372821c1130SAlex Bennée        while decode_ok:
373821c1130SAlex Bennée            event = read_event(dumpfile)
374fcc8c529SAlex Bennée            decode_ok = call_decode(event_decode_table, event,
375fcc8c529SAlex Bennée                                    dumpfile)
376fcc8c529SAlex Bennée    except Exception as inst:
377fcc8c529SAlex Bennée        print(f"error {inst}")
378fcc8c529SAlex Bennée
379821c1130SAlex Bennée    finally:
380fcc8c529SAlex Bennée        print(f"Reached {dumpfile.tell()} of {dumpsize} bytes")
381821c1130SAlex Bennée        dumpfile.close()
382821c1130SAlex Bennée
383821c1130SAlex Bennéeif __name__ == "__main__":
384821c1130SAlex Bennée    args = parse_arguments()
385821c1130SAlex Bennée    decode_file(args.file)
386