xref: /openbmc/qemu/scripts/replay-dump.py (revision 333b3e5f)
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
21import argparse
22import struct
23from collections import namedtuple
24from os import path
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
86def read_array(fin):
87    "Read a sized array"
88    size = read_dword(fin)
89    data = fin.read(size)
90    return data
91
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:
99        print("Could not decode index: %d" % (index))
100        print("Entry is: %s" % (decoder))
101        print("Decode Table is:\n%s" % (table))
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:
113        print("%d:%s(%d) %s" % (event_count, name, eid, string))
114    else:
115        print("%d:%s(%d)" % (event_count, name, eid))
116
117
118# Decoders for each event type
119
120def decode_unimp(eid, name, _unused_dumpfile):
121    "Unimplemented decoder, will trigger exit"
122    print("%s not handled - will now stop" % (name))
123    return False
124
125def decode_plain(eid, name, _unused_dumpfile):
126    "Plain events without additional data"
127    print_event(eid, name, "no data")
128    return True
129
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)
134    print("  %s(%d) @ %d" % (name, eid, step_id))
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:
154        print("  mismatch between checkpoint %d and async data %d" % (
155            replay_state.current_checkpoint, async_event_checkpoint))
156        return True
157
158    return call_decode(async_decode_table, async_event_kind, dumpfile)
159
160total_insns = 0
161
162def decode_instruction(eid, name, dumpfile):
163    global total_insns
164    ins_diff = read_dword(dumpfile)
165    total_insns += ins_diff
166    print_event(eid, name, "+ %d -> %d" % (ins_diff, total_insns))
167    return True
168
169def decode_char_write(eid, name, dumpfile):
170    res = read_dword(dumpfile)
171    offset = read_dword(dumpfile)
172    print_event(eid, name, "%d -> %d" % (offset, res))
173    return True
174
175def decode_audio_out(eid, name, dumpfile):
176    audio_data = read_dword(dumpfile)
177    print_event(eid, name, "%d" % (audio_data))
178    return True
179
180def decode_checkpoint(eid, name, dumpfile):
181    """Decode a checkpoint.
182
183    Checkpoints contain a series of async events with their own specific data.
184    """
185    replay_state.set_checkpoint()
186    # save event count as we peek ahead
187    event_number = replay_state.event_count
188    next_event = read_event(dumpfile)
189
190    # if the next event is EVENT_ASYNC there are a bunch of
191    # async events to read, otherwise we are done
192    if next_event != 3:
193        print_event(eid, name, "no additional data", event_number)
194    else:
195        print_event(eid, name, "more data follows", event_number)
196
197    replay_state.reuse_event(next_event)
198    return True
199
200def decode_checkpoint_init(eid, name, dumpfile):
201    print_event(eid, name)
202    return True
203
204def decode_interrupt(eid, name, dumpfile):
205    print_event(eid, name)
206    return True
207
208def decode_clock(eid, name, dumpfile):
209    clock_data = read_qword(dumpfile)
210    print_event(eid, name, "0x%x" % (clock_data))
211    return True
212
213def decode_random(eid, name, dumpfile):
214    ret = read_dword(dumpfile)
215    data = read_array(dumpfile)
216    print_event(eid, "%d bytes of random data" % len(data))
217    return True
218
219# pre-MTTCG merge
220v5_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
221                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
222                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
223                  Decoder(3, "EVENT_ASYNC", decode_async),
224                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
225                  Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
226                  Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
227                  Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
228                  Decoder(8, "EVENT_CLOCK_HOST", decode_clock),
229                  Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
230                  Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
231                  Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
232                  Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
233                  Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
234                  Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
235                  Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
236                  Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
237                  Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init),
238                  Decoder(18, "EVENT_CP_RESET", decode_checkpoint),
239]
240
241# post-MTTCG merge, AUDIO support added
242v6_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
243                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
244                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
245                  Decoder(3, "EVENT_ASYNC", decode_async),
246                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
247                  Decoder(5, "EVENT_CHAR_WRITE", decode_char_write),
248                  Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
249                  Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
250                  Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out),
251                  Decoder(9, "EVENT_AUDIO_IN", decode_unimp),
252                  Decoder(10, "EVENT_CLOCK_HOST", decode_clock),
253                  Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
254                  Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
255                  Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
256                  Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
257                  Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
258                  Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
259                  Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
260                  Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
261                  Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init),
262                  Decoder(20, "EVENT_CP_RESET", decode_checkpoint),
263]
264
265# Shutdown cause added
266v7_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
267                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
268                  Decoder(2, "EVENT_EXCEPTION", decode_unimp),
269                  Decoder(3, "EVENT_ASYNC", decode_async),
270                  Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
271                  Decoder(5, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
272                  Decoder(6, "EVENT_SHUTDOWN_HOST_QMP", decode_unimp),
273                  Decoder(7, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
274                  Decoder(8, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
275                  Decoder(9, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
276                  Decoder(10, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
277                  Decoder(11, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
278                  Decoder(12, "EVENT_SHUTDOWN___MAX", decode_unimp),
279                  Decoder(13, "EVENT_CHAR_WRITE", decode_char_write),
280                  Decoder(14, "EVENT_CHAR_READ_ALL", decode_unimp),
281                  Decoder(15, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
282                  Decoder(16, "EVENT_AUDIO_OUT", decode_audio_out),
283                  Decoder(17, "EVENT_AUDIO_IN", decode_unimp),
284                  Decoder(18, "EVENT_CLOCK_HOST", decode_clock),
285                  Decoder(19, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
286                  Decoder(20, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
287                  Decoder(21, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
288                  Decoder(22, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
289                  Decoder(23, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
290                  Decoder(24, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
291                  Decoder(25, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
292                  Decoder(26, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
293                  Decoder(27, "EVENT_CP_INIT", decode_checkpoint_init),
294                  Decoder(28, "EVENT_CP_RESET", decode_checkpoint),
295]
296
297v12_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
298                  Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
299                  Decoder(2, "EVENT_EXCEPTION", decode_plain),
300                  Decoder(3, "EVENT_ASYNC", decode_async),
301                  Decoder(4, "EVENT_ASYNC", decode_async),
302                  Decoder(5, "EVENT_ASYNC", decode_async),
303                  Decoder(6, "EVENT_ASYNC", decode_async),
304                  Decoder(6, "EVENT_ASYNC", decode_async),
305                  Decoder(8, "EVENT_ASYNC", decode_async),
306                  Decoder(9, "EVENT_ASYNC", decode_async),
307                  Decoder(10, "EVENT_ASYNC", decode_async),
308                  Decoder(11, "EVENT_SHUTDOWN", decode_unimp),
309                  Decoder(12, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
310                  Decoder(13, "EVENT_SHUTDOWN_HOST_QMP_QUIT", decode_unimp),
311                  Decoder(14, "EVENT_SHUTDOWN_HOST_QMP_RESET", decode_unimp),
312                  Decoder(14, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
313                  Decoder(15, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
314                  Decoder(16, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
315                  Decoder(17, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
316                  Decoder(18, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
317                  Decoder(19, "EVENT_SHUTDOWN_GUEST_SUBSYSTEM_RESET", decode_unimp),
318                  Decoder(20, "EVENT_SHUTDOWN_GUEST_SNAPSHOT_LOAD", decode_unimp),
319                  Decoder(21, "EVENT_SHUTDOWN___MAX", decode_unimp),
320                  Decoder(22, "EVENT_CHAR_WRITE", decode_char_write),
321                  Decoder(23, "EVENT_CHAR_READ_ALL", decode_unimp),
322                  Decoder(24, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
323                  Decoder(25, "EVENT_AUDIO_IN", decode_unimp),
324                  Decoder(26, "EVENT_AUDIO_OUT", decode_audio_out),
325                  Decoder(27, "EVENT_RANDOM", decode_random),
326                  Decoder(28, "EVENT_CLOCK_HOST", decode_clock),
327                  Decoder(29, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
328                  Decoder(30, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
329                  Decoder(31, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
330                  Decoder(32, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
331                  Decoder(33, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
332                  Decoder(34, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
333                  Decoder(35, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
334                  Decoder(36, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
335                  Decoder(37, "EVENT_CP_INIT", decode_checkpoint_init),
336                  Decoder(38, "EVENT_CP_RESET", decode_checkpoint),
337]
338
339def parse_arguments():
340    "Grab arguments for script"
341    parser = argparse.ArgumentParser()
342    parser.add_argument("-f", "--file", help='record/replay dump to read from',
343                        required=True)
344    return parser.parse_args()
345
346def decode_file(filename):
347    "Decode a record/replay dump"
348    dumpfile = open(filename, "rb")
349    dumpsize = path.getsize(filename)
350    # read and throwaway the header
351    version = read_dword(dumpfile)
352    junk = read_qword(dumpfile)
353
354    # see REPLAY_VERSION
355    print("HEADER: version 0x%x" % (version))
356
357    if version == 0xe0200c:
358        event_decode_table = v12_event_table
359        replay_state.checkpoint_start = 30
360    elif version == 0xe02007:
361        event_decode_table = v7_event_table
362        replay_state.checkpoint_start = 12
363    elif version == 0xe02006:
364        event_decode_table = v6_event_table
365        replay_state.checkpoint_start = 12
366    else:
367        event_decode_table = v5_event_table
368        replay_state.checkpoint_start = 10
369
370    try:
371        decode_ok = True
372        while decode_ok:
373            event = read_event(dumpfile)
374            decode_ok = call_decode(event_decode_table, event,
375                                    dumpfile)
376    except Exception as inst:
377        print(f"error {inst}")
378
379    finally:
380        print(f"Reached {dumpfile.tell()} of {dumpsize} bytes")
381        dumpfile.close()
382
383if __name__ == "__main__":
384    args = parse_arguments()
385    decode_file(args.file)
386