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