xref: /qemu/scripts/replay-dump.py (revision 00140e79bb5e74c5a06f9eb0a0110f4e5ebffaec)
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
23*00140e79SNicholas Pigginimport os
24821c1130SAlex Bennéefrom collections import namedtuple
25fcc8c529SAlex Bennéefrom os import path
26821c1130SAlex Bennée
27821c1130SAlex Bennée# This mirrors some of the global replay state which some of the
28821c1130SAlex Bennée# stream loading refers to. Some decoders may read the next event so
29821c1130SAlex Bennée# we need handle that case. Calling reuse_event will ensure the next
30821c1130SAlex Bennée# event is read from the cache rather than advancing the file.
31821c1130SAlex Bennée
32821c1130SAlex Bennéeclass ReplayState(object):
33821c1130SAlex Bennée    def __init__(self):
34821c1130SAlex Bennée        self.event = -1
35821c1130SAlex Bennée        self.event_count = 0
36821c1130SAlex Bennée        self.already_read = False
37821c1130SAlex Bennée        self.current_checkpoint = 0
38821c1130SAlex Bennée        self.checkpoint = 0
39821c1130SAlex Bennée
40821c1130SAlex Bennée    def set_event(self, ev):
41821c1130SAlex Bennée        self.event = ev
42821c1130SAlex Bennée        self.event_count += 1
43821c1130SAlex Bennée
44821c1130SAlex Bennée    def get_event(self):
45821c1130SAlex Bennée        self.already_read = False
46821c1130SAlex Bennée        return self.event
47821c1130SAlex Bennée
48821c1130SAlex Bennée    def reuse_event(self, ev):
49821c1130SAlex Bennée        self.event = ev
50821c1130SAlex Bennée        self.already_read = True
51821c1130SAlex Bennée
52821c1130SAlex Bennée    def set_checkpoint(self):
53821c1130SAlex Bennée        self.checkpoint = self.event - self.checkpoint_start
54821c1130SAlex Bennée
55821c1130SAlex Bennée    def get_checkpoint(self):
56821c1130SAlex Bennée        return self.checkpoint
57821c1130SAlex Bennée
58821c1130SAlex Bennéereplay_state = ReplayState()
59821c1130SAlex Bennée
60821c1130SAlex Bennée# Simple read functions that mirror replay-internal.c
61821c1130SAlex Bennée# The file-stream is big-endian and manually written out a byte at a time.
62821c1130SAlex Bennée
63821c1130SAlex Bennéedef read_byte(fin):
64821c1130SAlex Bennée    "Read a single byte"
65821c1130SAlex Bennée    return struct.unpack('>B', fin.read(1))[0]
66821c1130SAlex Bennée
67821c1130SAlex Bennéedef read_event(fin):
68821c1130SAlex Bennée    "Read a single byte event, but save some state"
69821c1130SAlex Bennée    if replay_state.already_read:
70821c1130SAlex Bennée        return replay_state.get_event()
71821c1130SAlex Bennée    else:
72821c1130SAlex Bennée        replay_state.set_event(read_byte(fin))
73821c1130SAlex Bennée        return replay_state.event
74821c1130SAlex Bennée
75821c1130SAlex Bennéedef read_word(fin):
76821c1130SAlex Bennée    "Read a 16 bit word"
77821c1130SAlex Bennée    return struct.unpack('>H', fin.read(2))[0]
78821c1130SAlex Bennée
79821c1130SAlex Bennéedef read_dword(fin):
80821c1130SAlex Bennée    "Read a 32 bit word"
81821c1130SAlex Bennée    return struct.unpack('>I', fin.read(4))[0]
82821c1130SAlex Bennée
83821c1130SAlex Bennéedef read_qword(fin):
84821c1130SAlex Bennée    "Read a 64 bit word"
85821c1130SAlex Bennée    return struct.unpack('>Q', fin.read(8))[0]
86821c1130SAlex Bennée
87fcc8c529SAlex Bennéedef read_array(fin):
88fcc8c529SAlex Bennée    "Read a sized array"
89fcc8c529SAlex Bennée    size = read_dword(fin)
90fcc8c529SAlex Bennée    data = fin.read(size)
91fcc8c529SAlex Bennée    return data
92fcc8c529SAlex Bennée
93821c1130SAlex Bennée# Generic decoder structure
94821c1130SAlex BennéeDecoder = namedtuple("Decoder", "eid name fn")
95821c1130SAlex Bennée
96821c1130SAlex Bennéedef call_decode(table, index, dumpfile):
97821c1130SAlex Bennée    "Search decode table for next step"
98821c1130SAlex Bennée    decoder = next((d for d in table if d.eid == index), None)
99821c1130SAlex Bennée    if not decoder:
100f03868bdSEduardo Habkost        print("Could not decode index: %d" % (index))
101f03868bdSEduardo Habkost        print("Entry is: %s" % (decoder))
102f03868bdSEduardo Habkost        print("Decode Table is:\n%s" % (table))
103821c1130SAlex Bennée        return False
104821c1130SAlex Bennée    else:
105821c1130SAlex Bennée        return decoder.fn(decoder.eid, decoder.name, dumpfile)
106821c1130SAlex Bennée
107821c1130SAlex Bennée# Print event
108821c1130SAlex Bennéedef print_event(eid, name, string=None, event_count=None):
109821c1130SAlex Bennée    "Print event with count"
110821c1130SAlex Bennée    if not event_count:
111821c1130SAlex Bennée        event_count = replay_state.event_count
112821c1130SAlex Bennée
113821c1130SAlex Bennée    if string:
114f03868bdSEduardo Habkost        print("%d:%s(%d) %s" % (event_count, name, eid, string))
115821c1130SAlex Bennée    else:
116f03868bdSEduardo Habkost        print("%d:%s(%d)" % (event_count, name, eid))
117821c1130SAlex Bennée
118821c1130SAlex Bennée
119821c1130SAlex Bennée# Decoders for each event type
120821c1130SAlex Bennée
121821c1130SAlex Bennéedef decode_unimp(eid, name, _unused_dumpfile):
122d30b5bc9SMichael Tokarev    "Unimplemented decoder, will trigger exit"
123f03868bdSEduardo Habkost    print("%s not handled - will now stop" % (name))
124821c1130SAlex Bennée    return False
125821c1130SAlex Bennée
126fcc8c529SAlex Bennéedef decode_plain(eid, name, _unused_dumpfile):
127fcc8c529SAlex Bennée    "Plain events without additional data"
128fcc8c529SAlex Bennée    print_event(eid, name, "no data")
129fcc8c529SAlex Bennée    return True
130fcc8c529SAlex Bennée
131821c1130SAlex Bennée# Checkpoint decoder
132821c1130SAlex Bennéedef swallow_async_qword(eid, name, dumpfile):
133821c1130SAlex Bennée    "Swallow a qword of data without looking at it"
134821c1130SAlex Bennée    step_id = read_qword(dumpfile)
135f03868bdSEduardo Habkost    print("  %s(%d) @ %d" % (name, eid, step_id))
136821c1130SAlex Bennée    return True
137821c1130SAlex Bennée
138*00140e79SNicholas Piggindef swallow_bytes(eid, name, dumpfile, nr):
139*00140e79SNicholas Piggin    """Swallow nr bytes of data without looking at it"""
140*00140e79SNicholas Piggin    dumpfile.seek(nr, os.SEEK_CUR)
141*00140e79SNicholas Piggin
142*00140e79SNicholas Piggindef decode_exception(eid, name, dumpfile):
143*00140e79SNicholas Piggin    print_event(eid, name)
144*00140e79SNicholas Piggin    return True
145*00140e79SNicholas Piggin
146*00140e79SNicholas Piggin# v12 does away with the additional event byte and encodes it in the main type
147*00140e79SNicholas Piggin# Between v8 and v9, REPLAY_ASYNC_BH_ONESHOT was added, but we don't decode
148*00140e79SNicholas Piggin# those versions so leave it out.
149821c1130SAlex Bennéeasync_decode_table = [ Decoder(0, "REPLAY_ASYNC_EVENT_BH", swallow_async_qword),
150821c1130SAlex Bennée                       Decoder(1, "REPLAY_ASYNC_INPUT", decode_unimp),
151821c1130SAlex Bennée                       Decoder(2, "REPLAY_ASYNC_INPUT_SYNC", decode_unimp),
152821c1130SAlex Bennée                       Decoder(3, "REPLAY_ASYNC_CHAR_READ", decode_unimp),
153821c1130SAlex Bennée                       Decoder(4, "REPLAY_ASYNC_EVENT_BLOCK", decode_unimp),
154821c1130SAlex Bennée                       Decoder(5, "REPLAY_ASYNC_EVENT_NET", decode_unimp),
155821c1130SAlex Bennée]
156821c1130SAlex Bennée# See replay_read_events/replay_read_event
157*00140e79SNicholas Piggindef decode_async_old(eid, name, dumpfile):
158*00140e79SNicholas Piggin    """Decode an ASYNC event (pre-v8)"""
159821c1130SAlex Bennée
160821c1130SAlex Bennée    print_event(eid, name)
161821c1130SAlex Bennée
162821c1130SAlex Bennée    async_event_kind = read_byte(dumpfile)
163821c1130SAlex Bennée    async_event_checkpoint = read_byte(dumpfile)
164821c1130SAlex Bennée
165821c1130SAlex Bennée    if async_event_checkpoint != replay_state.current_checkpoint:
166f03868bdSEduardo Habkost        print("  mismatch between checkpoint %d and async data %d" % (
167f03868bdSEduardo Habkost            replay_state.current_checkpoint, async_event_checkpoint))
168821c1130SAlex Bennée        return True
169821c1130SAlex Bennée
170821c1130SAlex Bennée    return call_decode(async_decode_table, async_event_kind, dumpfile)
171821c1130SAlex Bennée
172*00140e79SNicholas Piggindef decode_async_bh(eid, name, dumpfile):
173*00140e79SNicholas Piggin    op_id = read_qword(dumpfile)
174*00140e79SNicholas Piggin    print_event(eid, name)
175*00140e79SNicholas Piggin    return True
176*00140e79SNicholas Piggin
177*00140e79SNicholas Piggindef decode_async_bh_oneshot(eid, name, dumpfile):
178*00140e79SNicholas Piggin    op_id = read_qword(dumpfile)
179*00140e79SNicholas Piggin    print_event(eid, name)
180*00140e79SNicholas Piggin    return True
181*00140e79SNicholas Piggin
182*00140e79SNicholas Piggindef decode_async_char_read(eid, name, dumpfile):
183*00140e79SNicholas Piggin    char_id = read_byte(dumpfile)
184*00140e79SNicholas Piggin    size = read_dword(dumpfile)
185*00140e79SNicholas Piggin    print_event(eid, name, "device:%x chars:%s" % (char_id, dumpfile.read(size)))
186*00140e79SNicholas Piggin    return True
187*00140e79SNicholas Piggin
188*00140e79SNicholas Piggindef decode_async_block(eid, name, dumpfile):
189*00140e79SNicholas Piggin    op_id = read_qword(dumpfile)
190*00140e79SNicholas Piggin    print_event(eid, name)
191*00140e79SNicholas Piggin    return True
192*00140e79SNicholas Piggin
193*00140e79SNicholas Piggindef decode_async_net(eid, name, dumpfile):
194*00140e79SNicholas Piggin    net_id = read_byte(dumpfile)
195*00140e79SNicholas Piggin    flags = read_dword(dumpfile)
196*00140e79SNicholas Piggin    size = read_dword(dumpfile)
197*00140e79SNicholas Piggin    swallow_bytes(eid, name, dumpfile, size)
198*00140e79SNicholas Piggin    print_event(eid, name, "net:%x flags:%x bytes:%d" % (net_id, flags, size))
199*00140e79SNicholas Piggin    return True
200*00140e79SNicholas Piggin
20141e17cc8SAlex Bennéetotal_insns = 0
202821c1130SAlex Bennée
203821c1130SAlex Bennéedef decode_instruction(eid, name, dumpfile):
20441e17cc8SAlex Bennée    global total_insns
205821c1130SAlex Bennée    ins_diff = read_dword(dumpfile)
20641e17cc8SAlex Bennée    total_insns += ins_diff
20741e17cc8SAlex Bennée    print_event(eid, name, "+ %d -> %d" % (ins_diff, total_insns))
208821c1130SAlex Bennée    return True
209821c1130SAlex Bennée
210*00140e79SNicholas Piggindef decode_shutdown(eid, name, dumpfile):
211*00140e79SNicholas Piggin    print_event(eid, name)
212*00140e79SNicholas Piggin    return True
213*00140e79SNicholas Piggin
214fcc8c529SAlex Bennéedef decode_char_write(eid, name, dumpfile):
215fcc8c529SAlex Bennée    res = read_dword(dumpfile)
216fcc8c529SAlex Bennée    offset = read_dword(dumpfile)
217fcc8c529SAlex Bennée    print_event(eid, name, "%d -> %d" % (offset, res))
218fcc8c529SAlex Bennée    return True
219fcc8c529SAlex Bennée
220821c1130SAlex Bennéedef decode_audio_out(eid, name, dumpfile):
221821c1130SAlex Bennée    audio_data = read_dword(dumpfile)
222821c1130SAlex Bennée    print_event(eid, name, "%d" % (audio_data))
223821c1130SAlex Bennée    return True
224821c1130SAlex Bennée
225*00140e79SNicholas Piggindef __decode_checkpoint(eid, name, dumpfile, old):
226821c1130SAlex Bennée    """Decode a checkpoint.
227821c1130SAlex Bennée
228821c1130SAlex Bennée    Checkpoints contain a series of async events with their own specific data.
229821c1130SAlex Bennée    """
230821c1130SAlex Bennée    replay_state.set_checkpoint()
231821c1130SAlex Bennée    # save event count as we peek ahead
232821c1130SAlex Bennée    event_number = replay_state.event_count
233821c1130SAlex Bennée    next_event = read_event(dumpfile)
234821c1130SAlex Bennée
235821c1130SAlex Bennée    # if the next event is EVENT_ASYNC there are a bunch of
236821c1130SAlex Bennée    # async events to read, otherwise we are done
237*00140e79SNicholas Piggin    if (old and next_event == 3) or (not old and next_event >= 3 and next_event <= 9):
238821c1130SAlex Bennée        print_event(eid, name, "more data follows", event_number)
239*00140e79SNicholas Piggin    else:
240*00140e79SNicholas Piggin        print_event(eid, name, "no additional data", event_number)
241821c1130SAlex Bennée
242821c1130SAlex Bennée    replay_state.reuse_event(next_event)
243821c1130SAlex Bennée    return True
244821c1130SAlex Bennée
245*00140e79SNicholas Piggindef decode_checkpoint_old(eid, name, dumpfile):
246*00140e79SNicholas Piggin    return __decode_checkpoint(eid, name, dumpfile, False)
247*00140e79SNicholas Piggin
248*00140e79SNicholas Piggindef decode_checkpoint(eid, name, dumpfile):
249*00140e79SNicholas Piggin    return __decode_checkpoint(eid, name, dumpfile, True)
250*00140e79SNicholas Piggin
251821c1130SAlex Bennéedef decode_checkpoint_init(eid, name, dumpfile):
252821c1130SAlex Bennée    print_event(eid, name)
253821c1130SAlex Bennée    return True
254821c1130SAlex Bennée
255821c1130SAlex Bennéedef decode_interrupt(eid, name, dumpfile):
256821c1130SAlex Bennée    print_event(eid, name)
257821c1130SAlex Bennée    return True
258821c1130SAlex Bennée
259821c1130SAlex Bennéedef decode_clock(eid, name, dumpfile):
260821c1130SAlex Bennée    clock_data = read_qword(dumpfile)
261821c1130SAlex Bennée    print_event(eid, name, "0x%x" % (clock_data))
262821c1130SAlex Bennée    return True
263821c1130SAlex Bennée
264fcc8c529SAlex Bennéedef decode_random(eid, name, dumpfile):
265fcc8c529SAlex Bennée    ret = read_dword(dumpfile)
266*00140e79SNicholas Piggin    size = read_dword(dumpfile)
267*00140e79SNicholas Piggin    swallow_bytes(eid, name, dumpfile, size)
268*00140e79SNicholas Piggin    if (ret):
269*00140e79SNicholas Piggin        print_event(eid, name, "%d bytes (getrandom failed)" % (size))
270*00140e79SNicholas Piggin    else:
271*00140e79SNicholas Piggin        print_event(eid, name, "%d bytes" % (size))
272fcc8c529SAlex Bennée    return True
273821c1130SAlex Bennée
274*00140e79SNicholas Piggindef decode_end(eid, name, dumpfile):
275*00140e79SNicholas Piggin    print_event(eid, name)
276*00140e79SNicholas Piggin    return False
277*00140e79SNicholas Piggin
278821c1130SAlex Bennée# pre-MTTCG merge
279821c1130SAlex Bennéev5_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
280821c1130SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
281fcc8c529SAlex Bennée                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
282*00140e79SNicholas Piggin                  Decoder(3, "EVENT_ASYNC", decode_async_old),
283821c1130SAlex Bennée                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
284fcc8c529SAlex Bennée                  Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
285821c1130SAlex Bennée                  Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
286821c1130SAlex Bennée                  Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
287821c1130SAlex Bennée                  Decoder(8, "EVENT_CLOCK_HOST", decode_clock),
288821c1130SAlex Bennée                  Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
289821c1130SAlex Bennée                  Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
290821c1130SAlex Bennée                  Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
291821c1130SAlex Bennée                  Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
292821c1130SAlex Bennée                  Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
293821c1130SAlex Bennée                  Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
294821c1130SAlex Bennée                  Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
295821c1130SAlex Bennée                  Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
296821c1130SAlex Bennée                  Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init),
297821c1130SAlex Bennée                  Decoder(18, "EVENT_CP_RESET", decode_checkpoint),
298821c1130SAlex Bennée]
299821c1130SAlex Bennée
300821c1130SAlex Bennée# post-MTTCG merge, AUDIO support added
301821c1130SAlex Bennéev6_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
302821c1130SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
303fcc8c529SAlex Bennée                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
304*00140e79SNicholas Piggin                  Decoder(3, "EVENT_ASYNC", decode_async_old),
305821c1130SAlex Bennée                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
306fcc8c529SAlex Bennée                  Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
307821c1130SAlex Bennée                  Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
308821c1130SAlex Bennée                  Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
309821c1130SAlex Bennée                  Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out),
310821c1130SAlex Bennée                  Decoder(9, "EVENT_AUDIO_IN", decode_unimp),
311821c1130SAlex Bennée                  Decoder(10, "EVENT_CLOCK_HOST", decode_clock),
312821c1130SAlex Bennée                  Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
313821c1130SAlex Bennée                  Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
314821c1130SAlex Bennée                  Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
315821c1130SAlex Bennée                  Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
316821c1130SAlex Bennée                  Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
317821c1130SAlex Bennée                  Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
318821c1130SAlex Bennée                  Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
319821c1130SAlex Bennée                  Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
320821c1130SAlex Bennée                  Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init),
321821c1130SAlex Bennée                  Decoder(20, "EVENT_CP_RESET", decode_checkpoint),
322821c1130SAlex Bennée]
323821c1130SAlex Bennée
324821c1130SAlex Bennée# Shutdown cause added
325821c1130SAlex Bennéev7_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
326821c1130SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
327821c1130SAlex Bennée                  Decoder(2, "EVENT_EXCEPTION", decode_unimp),
328*00140e79SNicholas Piggin                  Decoder(3, "EVENT_ASYNC", decode_async_old),
329821c1130SAlex Bennée                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
330821c1130SAlex Bennée                  Decoder(5, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
331821c1130SAlex Bennée                  Decoder(6, "EVENT_SHUTDOWN_HOST_QMP", decode_unimp),
332821c1130SAlex Bennée                  Decoder(7, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
333821c1130SAlex Bennée                  Decoder(8, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
334821c1130SAlex Bennée                  Decoder(9, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
335821c1130SAlex Bennée                  Decoder(10, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
336821c1130SAlex Bennée                  Decoder(11, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
337821c1130SAlex Bennée                  Decoder(12, "EVENT_SHUTDOWN___MAX", decode_unimp),
338fcc8c529SAlex Bennée                  Decoder(13, "EVENT_CHAR_WRITE", decode_char_write),
339821c1130SAlex Bennée                  Decoder(14, "EVENT_CHAR_READ_ALL", decode_unimp),
340821c1130SAlex Bennée                  Decoder(15, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
341821c1130SAlex Bennée                  Decoder(16, "EVENT_AUDIO_OUT", decode_audio_out),
342821c1130SAlex Bennée                  Decoder(17, "EVENT_AUDIO_IN", decode_unimp),
343821c1130SAlex Bennée                  Decoder(18, "EVENT_CLOCK_HOST", decode_clock),
344821c1130SAlex Bennée                  Decoder(19, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
345821c1130SAlex Bennée                  Decoder(20, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
346821c1130SAlex Bennée                  Decoder(21, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
347821c1130SAlex Bennée                  Decoder(22, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
348821c1130SAlex Bennée                  Decoder(23, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
349821c1130SAlex Bennée                  Decoder(24, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
350821c1130SAlex Bennée                  Decoder(25, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
351821c1130SAlex Bennée                  Decoder(26, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
352821c1130SAlex Bennée                  Decoder(27, "EVENT_CP_INIT", decode_checkpoint_init),
353821c1130SAlex Bennée                  Decoder(28, "EVENT_CP_RESET", decode_checkpoint),
354821c1130SAlex Bennée]
355821c1130SAlex Bennée
356fcc8c529SAlex Bennéev12_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
357fcc8c529SAlex Bennée                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
358*00140e79SNicholas Piggin                  Decoder(2, "EVENT_EXCEPTION", decode_exception),
359*00140e79SNicholas Piggin                  Decoder(3, "EVENT_ASYNC_BH", decode_async_bh),
360*00140e79SNicholas Piggin                  Decoder(4, "EVENT_ASYNC_BH_ONESHOT", decode_async_bh_oneshot),
361*00140e79SNicholas Piggin                  Decoder(5, "EVENT_ASYNC_INPUT", decode_unimp),
362*00140e79SNicholas Piggin                  Decoder(6, "EVENT_ASYNC_INPUT_SYNC", decode_unimp),
363*00140e79SNicholas Piggin                  Decoder(7, "EVENT_ASYNC_CHAR_READ", decode_async_char_read),
364*00140e79SNicholas Piggin                  Decoder(8, "EVENT_ASYNC_BLOCK", decode_async_block),
365*00140e79SNicholas Piggin                  Decoder(9, "EVENT_ASYNC_NET", decode_async_net),
366*00140e79SNicholas Piggin                  Decoder(10, "EVENT_SHUTDOWN", decode_shutdown),
367*00140e79SNicholas Piggin                  Decoder(11, "EVENT_SHUTDOWN_HOST_ERR", decode_shutdown),
368*00140e79SNicholas Piggin                  Decoder(12, "EVENT_SHUTDOWN_HOST_QMP_QUIT", decode_shutdown),
369*00140e79SNicholas Piggin                  Decoder(13, "EVENT_SHUTDOWN_HOST_QMP_RESET", decode_shutdown),
370*00140e79SNicholas Piggin                  Decoder(14, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_shutdown),
371*00140e79SNicholas Piggin                  Decoder(15, "EVENT_SHUTDOWN_HOST_UI", decode_shutdown),
372*00140e79SNicholas Piggin                  Decoder(16, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_shutdown),
373*00140e79SNicholas Piggin                  Decoder(17, "EVENT_SHUTDOWN_GUEST_RESET", decode_shutdown),
374*00140e79SNicholas Piggin                  Decoder(18, "EVENT_SHUTDOWN_GUEST_PANIC", decode_shutdown),
375*00140e79SNicholas Piggin                  Decoder(19, "EVENT_SHUTDOWN_SUBSYS_RESET", decode_shutdown),
376*00140e79SNicholas Piggin                  Decoder(20, "EVENT_SHUTDOWN_SNAPSHOT_LOAD", decode_shutdown),
377*00140e79SNicholas Piggin                  Decoder(21, "EVENT_SHUTDOWN___MAX", decode_shutdown),
378fcc8c529SAlex Bennée                  Decoder(22, "EVENT_CHAR_WRITE", decode_char_write),
379fcc8c529SAlex Bennée                  Decoder(23, "EVENT_CHAR_READ_ALL", decode_unimp),
380fcc8c529SAlex Bennée                  Decoder(24, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
381*00140e79SNicholas Piggin                  Decoder(25, "EVENT_AUDIO_OUT", decode_audio_out),
382*00140e79SNicholas Piggin                  Decoder(26, "EVENT_AUDIO_IN", decode_unimp),
383fcc8c529SAlex Bennée                  Decoder(27, "EVENT_RANDOM", decode_random),
384fcc8c529SAlex Bennée                  Decoder(28, "EVENT_CLOCK_HOST", decode_clock),
385fcc8c529SAlex Bennée                  Decoder(29, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
386fcc8c529SAlex Bennée                  Decoder(30, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
387fcc8c529SAlex Bennée                  Decoder(31, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
388fcc8c529SAlex Bennée                  Decoder(32, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
389fcc8c529SAlex Bennée                  Decoder(33, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
390fcc8c529SAlex Bennée                  Decoder(34, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
391fcc8c529SAlex Bennée                  Decoder(35, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
392fcc8c529SAlex Bennée                  Decoder(36, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
393fcc8c529SAlex Bennée                  Decoder(37, "EVENT_CP_INIT", decode_checkpoint_init),
394fcc8c529SAlex Bennée                  Decoder(38, "EVENT_CP_RESET", decode_checkpoint),
395*00140e79SNicholas Piggin                  Decoder(39, "EVENT_END", decode_end),
396fcc8c529SAlex Bennée]
397fcc8c529SAlex Bennée
398821c1130SAlex Bennéedef parse_arguments():
399821c1130SAlex Bennée    "Grab arguments for script"
400821c1130SAlex Bennée    parser = argparse.ArgumentParser()
401821c1130SAlex Bennée    parser.add_argument("-f", "--file", help='record/replay dump to read from',
402821c1130SAlex Bennée                        required=True)
403821c1130SAlex Bennée    return parser.parse_args()
404821c1130SAlex Bennée
405821c1130SAlex Bennéedef decode_file(filename):
406821c1130SAlex Bennée    "Decode a record/replay dump"
407821c1130SAlex Bennée    dumpfile = open(filename, "rb")
408fcc8c529SAlex Bennée    dumpsize = path.getsize(filename)
409821c1130SAlex Bennée    # read and throwaway the header
410821c1130SAlex Bennée    version = read_dword(dumpfile)
411821c1130SAlex Bennée    junk = read_qword(dumpfile)
412821c1130SAlex Bennée
413fcc8c529SAlex Bennée    # see REPLAY_VERSION
414f03868bdSEduardo Habkost    print("HEADER: version 0x%x" % (version))
415821c1130SAlex Bennée
416fcc8c529SAlex Bennée    if version == 0xe0200c:
417fcc8c529SAlex Bennée        event_decode_table = v12_event_table
418fcc8c529SAlex Bennée        replay_state.checkpoint_start = 30
419fcc8c529SAlex Bennée    elif version == 0xe02007:
420821c1130SAlex Bennée        event_decode_table = v7_event_table
421821c1130SAlex Bennée        replay_state.checkpoint_start = 12
422821c1130SAlex Bennée    elif version == 0xe02006:
423821c1130SAlex Bennée        event_decode_table = v6_event_table
424821c1130SAlex Bennée        replay_state.checkpoint_start = 12
425821c1130SAlex Bennée    else:
426821c1130SAlex Bennée        event_decode_table = v5_event_table
427821c1130SAlex Bennée        replay_state.checkpoint_start = 10
428821c1130SAlex Bennée
429821c1130SAlex Bennée    try:
430821c1130SAlex Bennée        decode_ok = True
431821c1130SAlex Bennée        while decode_ok:
432821c1130SAlex Bennée            event = read_event(dumpfile)
433fcc8c529SAlex Bennée            decode_ok = call_decode(event_decode_table, event,
434fcc8c529SAlex Bennée                                    dumpfile)
435fcc8c529SAlex Bennée    except Exception as inst:
436fcc8c529SAlex Bennée        print(f"error {inst}")
437fcc8c529SAlex Bennée
438821c1130SAlex Bennée    finally:
439fcc8c529SAlex Bennée        print(f"Reached {dumpfile.tell()} of {dumpsize} bytes")
440821c1130SAlex Bennée        dumpfile.close()
441821c1130SAlex Bennée
442821c1130SAlex Bennéeif __name__ == "__main__":
443821c1130SAlex Bennée    args = parse_arguments()
444821c1130SAlex Bennée    decode_file(args.file)
445