rtl8139: fix possible out of bound access
[qemu/ar7.git] / scripts / replay-dump.py
blob5ae77c8a9262b904475226354a29838dde3a681c
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Dump the contents of a recorded execution stream
6 # Copyright (c) 2017 Alex Bennée <alex.bennee@linaro.org>
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 of the License, or (at your option) any later version.
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.
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/>.
21 from __future__ import print_function
22 import argparse
23 import struct
24 from collections import namedtuple
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.
31 class 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
39 def set_event(self, ev):
40 self.event = ev
41 self.event_count += 1
43 def get_event(self):
44 self.already_read = False
45 return self.event
47 def reuse_event(self, ev):
48 self.event = ev
49 self.already_read = True
51 def set_checkpoint(self):
52 self.checkpoint = self.event - self.checkpoint_start
54 def get_checkpoint(self):
55 return self.checkpoint
57 replay_state = ReplayState()
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.
62 def read_byte(fin):
63 "Read a single byte"
64 return struct.unpack('>B', fin.read(1))[0]
66 def 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
74 def read_word(fin):
75 "Read a 16 bit word"
76 return struct.unpack('>H', fin.read(2))[0]
78 def read_dword(fin):
79 "Read a 32 bit word"
80 return struct.unpack('>I', fin.read(4))[0]
82 def read_qword(fin):
83 "Read a 64 bit word"
84 return struct.unpack('>Q', fin.read(8))[0]
86 # Generic decoder structure
87 Decoder = namedtuple("Decoder", "eid name fn")
89 def call_decode(table, index, dumpfile):
90 "Search decode table for next step"
91 decoder = next((d for d in table if d.eid == index), None)
92 if not decoder:
93 print("Could not decode index: %d" % (index))
94 print("Entry is: %s" % (decoder))
95 print("Decode Table is:\n%s" % (table))
96 return False
97 else:
98 return decoder.fn(decoder.eid, decoder.name, dumpfile)
100 # Print event
101 def print_event(eid, name, string=None, event_count=None):
102 "Print event with count"
103 if not event_count:
104 event_count = replay_state.event_count
106 if string:
107 print("%d:%s(%d) %s" % (event_count, name, eid, string))
108 else:
109 print("%d:%s(%d)" % (event_count, name, eid))
112 # Decoders for each event type
114 def decode_unimp(eid, name, _unused_dumpfile):
115 "Unimplimented decoder, will trigger exit"
116 print("%s not handled - will now stop" % (name))
117 return False
119 # Checkpoint decoder
120 def swallow_async_qword(eid, name, dumpfile):
121 "Swallow a qword of data without looking at it"
122 step_id = read_qword(dumpfile)
123 print(" %s(%d) @ %d" % (name, eid, step_id))
124 return True
126 async_decode_table = [ Decoder(0, "REPLAY_ASYNC_EVENT_BH", swallow_async_qword),
127 Decoder(1, "REPLAY_ASYNC_INPUT", decode_unimp),
128 Decoder(2, "REPLAY_ASYNC_INPUT_SYNC", decode_unimp),
129 Decoder(3, "REPLAY_ASYNC_CHAR_READ", decode_unimp),
130 Decoder(4, "REPLAY_ASYNC_EVENT_BLOCK", decode_unimp),
131 Decoder(5, "REPLAY_ASYNC_EVENT_NET", decode_unimp),
133 # See replay_read_events/replay_read_event
134 def decode_async(eid, name, dumpfile):
135 """Decode an ASYNC event"""
137 print_event(eid, name)
139 async_event_kind = read_byte(dumpfile)
140 async_event_checkpoint = read_byte(dumpfile)
142 if async_event_checkpoint != replay_state.current_checkpoint:
143 print(" mismatch between checkpoint %d and async data %d" % (
144 replay_state.current_checkpoint, async_event_checkpoint))
145 return True
147 return call_decode(async_decode_table, async_event_kind, dumpfile)
150 def decode_instruction(eid, name, dumpfile):
151 ins_diff = read_dword(dumpfile)
152 print_event(eid, name, "0x%x" % (ins_diff))
153 return True
155 def decode_audio_out(eid, name, dumpfile):
156 audio_data = read_dword(dumpfile)
157 print_event(eid, name, "%d" % (audio_data))
158 return True
160 def decode_checkpoint(eid, name, dumpfile):
161 """Decode a checkpoint.
163 Checkpoints contain a series of async events with their own specific data.
165 replay_state.set_checkpoint()
166 # save event count as we peek ahead
167 event_number = replay_state.event_count
168 next_event = read_event(dumpfile)
170 # if the next event is EVENT_ASYNC there are a bunch of
171 # async events to read, otherwise we are done
172 if next_event != 3:
173 print_event(eid, name, "no additional data", event_number)
174 else:
175 print_event(eid, name, "more data follows", event_number)
177 replay_state.reuse_event(next_event)
178 return True
180 def decode_checkpoint_init(eid, name, dumpfile):
181 print_event(eid, name)
182 return True
184 def decode_interrupt(eid, name, dumpfile):
185 print_event(eid, name)
186 return True
188 def decode_clock(eid, name, dumpfile):
189 clock_data = read_qword(dumpfile)
190 print_event(eid, name, "0x%x" % (clock_data))
191 return True
194 # pre-MTTCG merge
195 v5_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
196 Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
197 Decoder(2, "EVENT_EXCEPTION", decode_unimp),
198 Decoder(3, "EVENT_ASYNC", decode_async),
199 Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
200 Decoder(5, "EVENT_CHAR_WRITE", decode_unimp),
201 Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
202 Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
203 Decoder(8, "EVENT_CLOCK_HOST", decode_clock),
204 Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
205 Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
206 Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
207 Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
208 Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
209 Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
210 Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
211 Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
212 Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init),
213 Decoder(18, "EVENT_CP_RESET", decode_checkpoint),
216 # post-MTTCG merge, AUDIO support added
217 v6_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
218 Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
219 Decoder(2, "EVENT_EXCEPTION", decode_unimp),
220 Decoder(3, "EVENT_ASYNC", decode_async),
221 Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
222 Decoder(5, "EVENT_CHAR_WRITE", decode_unimp),
223 Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp),
224 Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
225 Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out),
226 Decoder(9, "EVENT_AUDIO_IN", decode_unimp),
227 Decoder(10, "EVENT_CLOCK_HOST", decode_clock),
228 Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
229 Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
230 Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
231 Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
232 Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
233 Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
234 Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
235 Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
236 Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init),
237 Decoder(20, "EVENT_CP_RESET", decode_checkpoint),
240 # Shutdown cause added
241 v7_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction),
242 Decoder(1, "EVENT_INTERRUPT", decode_interrupt),
243 Decoder(2, "EVENT_EXCEPTION", decode_unimp),
244 Decoder(3, "EVENT_ASYNC", decode_async),
245 Decoder(4, "EVENT_SHUTDOWN", decode_unimp),
246 Decoder(5, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp),
247 Decoder(6, "EVENT_SHUTDOWN_HOST_QMP", decode_unimp),
248 Decoder(7, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp),
249 Decoder(8, "EVENT_SHUTDOWN_HOST_UI", decode_unimp),
250 Decoder(9, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp),
251 Decoder(10, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp),
252 Decoder(11, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp),
253 Decoder(12, "EVENT_SHUTDOWN___MAX", decode_unimp),
254 Decoder(13, "EVENT_CHAR_WRITE", decode_unimp),
255 Decoder(14, "EVENT_CHAR_READ_ALL", decode_unimp),
256 Decoder(15, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp),
257 Decoder(16, "EVENT_AUDIO_OUT", decode_audio_out),
258 Decoder(17, "EVENT_AUDIO_IN", decode_unimp),
259 Decoder(18, "EVENT_CLOCK_HOST", decode_clock),
260 Decoder(19, "EVENT_CLOCK_VIRTUAL_RT", decode_clock),
261 Decoder(20, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint),
262 Decoder(21, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint),
263 Decoder(22, "EVENT_CP_RESET_REQUESTED", decode_checkpoint),
264 Decoder(23, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint),
265 Decoder(24, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint),
266 Decoder(25, "EVENT_CP_CLOCK_HOST", decode_checkpoint),
267 Decoder(26, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint),
268 Decoder(27, "EVENT_CP_INIT", decode_checkpoint_init),
269 Decoder(28, "EVENT_CP_RESET", decode_checkpoint),
272 def parse_arguments():
273 "Grab arguments for script"
274 parser = argparse.ArgumentParser()
275 parser.add_argument("-f", "--file", help='record/replay dump to read from',
276 required=True)
277 return parser.parse_args()
279 def decode_file(filename):
280 "Decode a record/replay dump"
281 dumpfile = open(filename, "rb")
283 # read and throwaway the header
284 version = read_dword(dumpfile)
285 junk = read_qword(dumpfile)
287 print("HEADER: version 0x%x" % (version))
289 if version == 0xe02007:
290 event_decode_table = v7_event_table
291 replay_state.checkpoint_start = 12
292 elif version == 0xe02006:
293 event_decode_table = v6_event_table
294 replay_state.checkpoint_start = 12
295 else:
296 event_decode_table = v5_event_table
297 replay_state.checkpoint_start = 10
299 try:
300 decode_ok = True
301 while decode_ok:
302 event = read_event(dumpfile)
303 decode_ok = call_decode(event_decode_table, event, dumpfile)
304 finally:
305 dumpfile.close()
307 if __name__ == "__main__":
308 args = parse_arguments()
309 decode_file(args.file)