From 13ba5fe90ccb9f93a9e01f77ae770a462fea4483 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Thu, 18 Aug 2022 09:02:51 +0300 Subject: [PATCH] tools: add mtrace-reader.py 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. Signed-off-by: Kai Vehmanen --- tools/mtrace/mtrace-reader.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 tools/mtrace/mtrace-reader.py diff --git a/tools/mtrace/mtrace-reader.py b/tools/mtrace/mtrace-reader.py new file mode 100755 index 000000000..06b005a93 --- /dev/null +++ b/tools/mtrace/mtrace-reader.py @@ -0,0 +1,38 @@ +#!/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 + +READ_BUFFER = 16384 +MTRACE_FILE = "/sys/kernel/debug/sof/mtrace/core0" + +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] + + os.write(sys.stdout.fileno(), data)