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.1 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/>.
25 from collections
import namedtuple
28 # This mirrors some of the global replay state which some of the
29 # stream loading refers to. Some decoders may read the next event so
30 # we need handle that case. Calling reuse_event will ensure the next
31 # event is read from the cache rather than advancing the file.
33 class ReplayState(object):
37 self
.already_read
= False
38 self
.current_checkpoint
= 0
41 def set_event(self
, ev
):
46 self
.already_read
= False
49 def reuse_event(self
, ev
):
51 self
.already_read
= True
53 def set_checkpoint(self
):
54 self
.checkpoint
= self
.event
- self
.checkpoint_start
56 def get_checkpoint(self
):
57 return self
.checkpoint
59 replay_state
= ReplayState()
61 # Simple read functions that mirror replay-internal.c
62 # The file-stream is big-endian and manually written out a byte at a time.
66 return struct
.unpack('>B', fin
.read(1))[0]
69 "Read a single byte event, but save some state"
70 if replay_state
.already_read
:
71 return replay_state
.get_event()
73 replay_state
.set_event(read_byte(fin
))
74 return replay_state
.event
78 return struct
.unpack('>H', fin
.read(2))[0]
82 return struct
.unpack('>I', fin
.read(4))[0]
86 return struct
.unpack('>Q', fin
.read(8))[0]
90 size
= read_dword(fin
)
94 # Generic decoder structure
95 Decoder
= namedtuple("Decoder", "eid name fn")
97 def call_decode(table
, index
, dumpfile
):
98 "Search decode table for next step"
99 decoder
= next((d
for d
in table
if d
.eid
== index
), None)
101 print("Could not decode index: %d" % (index
))
102 print("Entry is: %s" % (decoder
))
103 print("Decode Table is:\n%s" % (table
))
104 raise(Exception("unknown event"))
106 return decoder
.fn(decoder
.eid
, decoder
.name
, dumpfile
)
109 def print_event(eid
, name
, string
=None, event_count
=None):
110 "Print event with count"
112 event_count
= replay_state
.event_count
115 print("%d:%s(%d) %s" % (event_count
, name
, eid
, string
))
117 print("%d:%s(%d)" % (event_count
, name
, eid
))
120 # Decoders for each event type
122 def decode_unimp(eid
, name
, _unused_dumpfile
):
123 "Unimplemented decoder, will trigger exit"
124 print("%s not handled - will now stop" % (name
))
125 raise(Exception("unhandled event"))
127 def decode_plain(eid
, name
, _unused_dumpfile
):
128 "Plain events without additional data"
129 print_event(eid
, name
, "no data")
133 def swallow_async_qword(eid
, name
, dumpfile
):
134 "Swallow a qword of data without looking at it"
135 step_id
= read_qword(dumpfile
)
136 print(" %s(%d) @ %d" % (name
, eid
, step_id
))
139 def swallow_bytes(eid
, name
, dumpfile
, nr
):
140 """Swallow nr bytes of data without looking at it"""
141 dumpfile
.seek(nr
, os
.SEEK_CUR
)
145 def decode_instruction(eid
, name
, dumpfile
):
147 ins_diff
= read_dword(dumpfile
)
148 total_insns
+= ins_diff
149 print_event(eid
, name
, "+ %d -> %d" % (ins_diff
, total_insns
))
152 def decode_interrupt(eid
, name
, dumpfile
):
153 print_event(eid
, name
)
156 def decode_exception(eid
, name
, dumpfile
):
157 print_event(eid
, name
)
160 # v12 does away with the additional event byte and encodes it in the main type
161 # Between v8 and v9, REPLAY_ASYNC_BH_ONESHOT was added, but we don't decode
162 # those versions so leave it out.
163 async_decode_table
= [ Decoder(0, "REPLAY_ASYNC_EVENT_BH", swallow_async_qword
),
164 Decoder(1, "REPLAY_ASYNC_INPUT", decode_unimp
),
165 Decoder(2, "REPLAY_ASYNC_INPUT_SYNC", decode_unimp
),
166 Decoder(3, "REPLAY_ASYNC_CHAR_READ", decode_unimp
),
167 Decoder(4, "REPLAY_ASYNC_EVENT_BLOCK", decode_unimp
),
168 Decoder(5, "REPLAY_ASYNC_EVENT_NET", decode_unimp
),
170 # See replay_read_events/replay_read_event
171 def decode_async_old(eid
, name
, dumpfile
):
172 """Decode an ASYNC event (pre-v8)"""
174 print_event(eid
, name
)
176 async_event_kind
= read_byte(dumpfile
)
177 async_event_checkpoint
= read_byte(dumpfile
)
179 if async_event_checkpoint
!= replay_state
.current_checkpoint
:
180 print(" mismatch between checkpoint %d and async data %d" % (
181 replay_state
.current_checkpoint
, async_event_checkpoint
))
184 return call_decode(async_decode_table
, async_event_kind
, dumpfile
)
186 def decode_async_bh(eid
, name
, dumpfile
):
187 op_id
= read_qword(dumpfile
)
188 print_event(eid
, name
)
191 def decode_async_bh_oneshot(eid
, name
, dumpfile
):
192 op_id
= read_qword(dumpfile
)
193 print_event(eid
, name
)
196 def decode_async_char_read(eid
, name
, dumpfile
):
197 char_id
= read_byte(dumpfile
)
198 size
= read_dword(dumpfile
)
199 print_event(eid
, name
, "device:%x chars:%s" % (char_id
, dumpfile
.read(size
)))
202 def decode_async_block(eid
, name
, dumpfile
):
203 op_id
= read_qword(dumpfile
)
204 print_event(eid
, name
)
207 def decode_async_net(eid
, name
, dumpfile
):
208 net_id
= read_byte(dumpfile
)
209 flags
= read_dword(dumpfile
)
210 size
= read_dword(dumpfile
)
211 swallow_bytes(eid
, name
, dumpfile
, size
)
212 print_event(eid
, name
, "net:%x flags:%x bytes:%d" % (net_id
, flags
, size
))
215 def decode_shutdown(eid
, name
, dumpfile
):
216 print_event(eid
, name
)
219 def decode_char_write(eid
, name
, dumpfile
):
220 res
= read_dword(dumpfile
)
221 offset
= read_dword(dumpfile
)
222 print_event(eid
, name
, "%d -> %d" % (offset
, res
))
225 def decode_audio_out(eid
, name
, dumpfile
):
226 audio_data
= read_dword(dumpfile
)
227 print_event(eid
, name
, "%d" % (audio_data
))
230 def decode_random(eid
, name
, dumpfile
):
231 ret
= read_dword(dumpfile
)
232 size
= read_dword(dumpfile
)
233 swallow_bytes(eid
, name
, dumpfile
, size
)
235 print_event(eid
, name
, "%d bytes (getrandom failed)" % (size
))
237 print_event(eid
, name
, "%d bytes" % (size
))
240 def decode_clock(eid
, name
, dumpfile
):
241 clock_data
= read_qword(dumpfile
)
242 print_event(eid
, name
, "0x%x" % (clock_data
))
245 def __decode_checkpoint(eid
, name
, dumpfile
, old
):
246 """Decode a checkpoint.
248 Checkpoints contain a series of async events with their own specific data.
250 replay_state
.set_checkpoint()
251 # save event count as we peek ahead
252 event_number
= replay_state
.event_count
253 next_event
= read_event(dumpfile
)
255 # if the next event is EVENT_ASYNC there are a bunch of
256 # async events to read, otherwise we are done
257 if (old
and next_event
== 3) or (not old
and next_event
>= 3 and next_event
<= 9):
258 print_event(eid
, name
, "more data follows", event_number
)
260 print_event(eid
, name
, "no additional data", event_number
)
262 replay_state
.reuse_event(next_event
)
265 def decode_checkpoint_old(eid
, name
, dumpfile
):
266 return __decode_checkpoint(eid
, name
, dumpfile
, False)
268 def decode_checkpoint(eid
, name
, dumpfile
):
269 return __decode_checkpoint(eid
, name
, dumpfile
, True)
271 def decode_checkpoint_init(eid
, name
, dumpfile
):
272 print_event(eid
, name
)
275 def decode_end(eid
, name
, dumpfile
):
276 print_event(eid
, name
)
280 v5_event_table
= [Decoder(0, "EVENT_INSTRUCTION", decode_instruction
),
281 Decoder(1, "EVENT_INTERRUPT", decode_interrupt
),
282 Decoder(2, "EVENT_EXCEPTION", decode_plain
),
283 Decoder(3, "EVENT_ASYNC", decode_async_old
),
284 Decoder(4, "EVENT_SHUTDOWN", decode_unimp
),
285 Decoder(5, "EVENT_CHAR_WRITE", decode_char_write
),
286 Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp
),
287 Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp
),
288 Decoder(8, "EVENT_CLOCK_HOST", decode_clock
),
289 Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock
),
290 Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint
),
291 Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint
),
292 Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint
),
293 Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint
),
294 Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint
),
295 Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint
),
296 Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint
),
297 Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init
),
298 Decoder(18, "EVENT_CP_RESET", decode_checkpoint
),
301 # post-MTTCG merge, AUDIO support added
302 v6_event_table
= [Decoder(0, "EVENT_INSTRUCTION", decode_instruction
),
303 Decoder(1, "EVENT_INTERRUPT", decode_interrupt
),
304 Decoder(2, "EVENT_EXCEPTION", decode_plain
),
305 Decoder(3, "EVENT_ASYNC", decode_async_old
),
306 Decoder(4, "EVENT_SHUTDOWN", decode_unimp
),
307 Decoder(5, "EVENT_CHAR_WRITE", decode_char_write
),
308 Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp
),
309 Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp
),
310 Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out
),
311 Decoder(9, "EVENT_AUDIO_IN", decode_unimp
),
312 Decoder(10, "EVENT_CLOCK_HOST", decode_clock
),
313 Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock
),
314 Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint
),
315 Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint
),
316 Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint
),
317 Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint
),
318 Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint
),
319 Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint
),
320 Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint
),
321 Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init
),
322 Decoder(20, "EVENT_CP_RESET", decode_checkpoint
),
325 # Shutdown cause added
326 v7_event_table
= [Decoder(0, "EVENT_INSTRUCTION", decode_instruction
),
327 Decoder(1, "EVENT_INTERRUPT", decode_interrupt
),
328 Decoder(2, "EVENT_EXCEPTION", decode_unimp
),
329 Decoder(3, "EVENT_ASYNC", decode_async_old
),
330 Decoder(4, "EVENT_SHUTDOWN", decode_unimp
),
331 Decoder(5, "EVENT_SHUTDOWN_HOST_ERR", decode_unimp
),
332 Decoder(6, "EVENT_SHUTDOWN_HOST_QMP", decode_unimp
),
333 Decoder(7, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_unimp
),
334 Decoder(8, "EVENT_SHUTDOWN_HOST_UI", decode_unimp
),
335 Decoder(9, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_unimp
),
336 Decoder(10, "EVENT_SHUTDOWN_GUEST_RESET", decode_unimp
),
337 Decoder(11, "EVENT_SHUTDOWN_GUEST_PANIC", decode_unimp
),
338 Decoder(12, "EVENT_SHUTDOWN___MAX", decode_unimp
),
339 Decoder(13, "EVENT_CHAR_WRITE", decode_char_write
),
340 Decoder(14, "EVENT_CHAR_READ_ALL", decode_unimp
),
341 Decoder(15, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp
),
342 Decoder(16, "EVENT_AUDIO_OUT", decode_audio_out
),
343 Decoder(17, "EVENT_AUDIO_IN", decode_unimp
),
344 Decoder(18, "EVENT_CLOCK_HOST", decode_clock
),
345 Decoder(19, "EVENT_CLOCK_VIRTUAL_RT", decode_clock
),
346 Decoder(20, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint
),
347 Decoder(21, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint
),
348 Decoder(22, "EVENT_CP_RESET_REQUESTED", decode_checkpoint
),
349 Decoder(23, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint
),
350 Decoder(24, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint
),
351 Decoder(25, "EVENT_CP_CLOCK_HOST", decode_checkpoint
),
352 Decoder(26, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint
),
353 Decoder(27, "EVENT_CP_INIT", decode_checkpoint_init
),
354 Decoder(28, "EVENT_CP_RESET", decode_checkpoint
),
357 v12_event_table
= [Decoder(0, "EVENT_INSTRUCTION", decode_instruction
),
358 Decoder(1, "EVENT_INTERRUPT", decode_interrupt
),
359 Decoder(2, "EVENT_EXCEPTION", decode_exception
),
360 Decoder(3, "EVENT_ASYNC_BH", decode_async_bh
),
361 Decoder(4, "EVENT_ASYNC_BH_ONESHOT", decode_async_bh_oneshot
),
362 Decoder(5, "EVENT_ASYNC_INPUT", decode_unimp
),
363 Decoder(6, "EVENT_ASYNC_INPUT_SYNC", decode_unimp
),
364 Decoder(7, "EVENT_ASYNC_CHAR_READ", decode_async_char_read
),
365 Decoder(8, "EVENT_ASYNC_BLOCK", decode_async_block
),
366 Decoder(9, "EVENT_ASYNC_NET", decode_async_net
),
367 Decoder(10, "EVENT_SHUTDOWN", decode_shutdown
),
368 Decoder(11, "EVENT_SHUTDOWN_HOST_ERR", decode_shutdown
),
369 Decoder(12, "EVENT_SHUTDOWN_HOST_QMP_QUIT", decode_shutdown
),
370 Decoder(13, "EVENT_SHUTDOWN_HOST_QMP_RESET", decode_shutdown
),
371 Decoder(14, "EVENT_SHUTDOWN_HOST_SIGNAL", decode_shutdown
),
372 Decoder(15, "EVENT_SHUTDOWN_HOST_UI", decode_shutdown
),
373 Decoder(16, "EVENT_SHUTDOWN_GUEST_SHUTDOWN", decode_shutdown
),
374 Decoder(17, "EVENT_SHUTDOWN_GUEST_RESET", decode_shutdown
),
375 Decoder(18, "EVENT_SHUTDOWN_GUEST_PANIC", decode_shutdown
),
376 Decoder(19, "EVENT_SHUTDOWN_SUBSYS_RESET", decode_shutdown
),
377 Decoder(20, "EVENT_SHUTDOWN_SNAPSHOT_LOAD", decode_shutdown
),
378 Decoder(21, "EVENT_SHUTDOWN___MAX", decode_shutdown
),
379 Decoder(22, "EVENT_CHAR_WRITE", decode_char_write
),
380 Decoder(23, "EVENT_CHAR_READ_ALL", decode_unimp
),
381 Decoder(24, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp
),
382 Decoder(25, "EVENT_AUDIO_OUT", decode_audio_out
),
383 Decoder(26, "EVENT_AUDIO_IN", decode_unimp
),
384 Decoder(27, "EVENT_RANDOM", decode_random
),
385 Decoder(28, "EVENT_CLOCK_HOST", decode_clock
),
386 Decoder(29, "EVENT_CLOCK_VIRTUAL_RT", decode_clock
),
387 Decoder(30, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint
),
388 Decoder(31, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint
),
389 Decoder(32, "EVENT_CP_RESET_REQUESTED", decode_checkpoint
),
390 Decoder(33, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint
),
391 Decoder(34, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint
),
392 Decoder(35, "EVENT_CP_CLOCK_HOST", decode_checkpoint
),
393 Decoder(36, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint
),
394 Decoder(37, "EVENT_CP_INIT", decode_checkpoint_init
),
395 Decoder(38, "EVENT_CP_RESET", decode_checkpoint
),
396 Decoder(39, "EVENT_END", decode_end
),
399 def parse_arguments():
400 "Grab arguments for script"
401 parser
= argparse
.ArgumentParser()
402 parser
.add_argument("-f", "--file", help='record/replay dump to read from',
404 return parser
.parse_args()
406 def decode_file(filename
):
407 "Decode a record/replay dump"
408 dumpfile
= open(filename
, "rb")
409 dumpsize
= path
.getsize(filename
)
410 # read and throwaway the header
411 version
= read_dword(dumpfile
)
412 junk
= read_qword(dumpfile
)
415 print("HEADER: version 0x%x" % (version
))
417 if version
== 0xe0200c:
418 event_decode_table
= v12_event_table
419 replay_state
.checkpoint_start
= 30
420 elif version
== 0xe02007:
421 event_decode_table
= v7_event_table
422 replay_state
.checkpoint_start
= 12
423 elif version
== 0xe02006:
424 event_decode_table
= v6_event_table
425 replay_state
.checkpoint_start
= 12
427 event_decode_table
= v5_event_table
428 replay_state
.checkpoint_start
= 10
433 event
= read_event(dumpfile
)
434 decode_ok
= call_decode(event_decode_table
, event
,
436 except Exception as inst
:
437 print(f
"error {inst}")
441 print(f
"Reached {dumpfile.tell()} of {dumpsize} bytes")
444 if __name__
== "__main__":
445 args
= parse_arguments()
446 decode_file(args
.file)