-
Notifications
You must be signed in to change notification settings - Fork 314
/
mtrace-reader.py
executable file
·53 lines (40 loc) · 1.38 KB
/
mtrace-reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright (c) 2022, Intel Corporation. All rights reserved.
#pylint:disable=mixed-indentation
# Tool to stream data from Linux SOF driver "mtrace" debugfs
# interface to standard output. Plain "cat" is not sufficient
# as each read() syscall returns log data with a 32bit binary
# header, containing the payload length.
import struct
import os
import sys
import argparse
READ_BUFFER = 16384
MTRACE_FILE = "/sys/kernel/debug/sof/mtrace/core0"
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--mark-chunks',
action='store_true')
args = parser.parse_args()
chunk_idx = 0
fd = os.open(MTRACE_FILE, os.O_RDONLY)
while fd >= 0:
# direct unbuffered os.read() must be used to comply with
# debugfs protocol used. each non-zero read will return
# a buffer containing a 32bit header and a payload
read_bytes = os.read(fd, READ_BUFFER)
# handle end-of-file
if len(read_bytes) == 0:
continue
if len(read_bytes) <= 4:
continue
header = struct.unpack('I', read_bytes[0:4])
data_len = header[0]
data = read_bytes[4:4+data_len]
if (args.mark_chunks):
chunk_msg = "\n--- Chunk #{} start (size: {}) ---\n" .format(chunk_idx, data_len)
sys.stdout.write(chunk_msg)
sys.stdout.buffer.write(data)
sys.stdout.flush()
chunk_idx += 1