Skip to content

Commit 70f68db

Browse files
kv2019ilgirdwood
authored andcommitted
tools: mtrace-reader.c: add C version of mtrace-reader.py
Add a C implementation of mtrace-reader.py for use in environments where Python is not available. No Cmake rules added as the mtrace-reader.py remains the preferred tool to use. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent b9e6e14 commit 70f68db

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tools/mtrace/mtrace-reader.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
//
3+
// Copyright(c) 2025 Intel Corporation.
4+
//
5+
// Author: Kai Vehmanen <kai.vehmanen@linux.intel.com>
6+
//
7+
// Compile instructions:
8+
// gcc -o mtrace-reader mtrace-reader.c -Wall -O2
9+
//
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <fcntl.h>
14+
#include <unistd.h>
15+
#include <sys/types.h>
16+
#include <sys/stat.h>
17+
#include <stdint.h>
18+
19+
#define READ_BUFFER 16384
20+
#define MTRACE_FILE "/sys/kernel/debug/sof/mtrace/core0"
21+
22+
uint8_t buffer[READ_BUFFER];
23+
24+
int main(void)
25+
{
26+
ssize_t read_bytes;
27+
uint32_t data_len;
28+
uint32_t header;
29+
int fd;
30+
31+
fd = open(MTRACE_FILE, O_RDONLY);
32+
if (fd < 0) {
33+
perror("open");
34+
return 1;
35+
}
36+
37+
while (1) {
38+
read_bytes = read(fd, buffer, READ_BUFFER);
39+
40+
/* handle end-of-file */
41+
if (read_bytes == 0)
42+
continue;
43+
44+
if (read_bytes <= 4)
45+
continue;
46+
47+
header = *(uint32_t *)buffer;
48+
data_len = header;
49+
if (data_len > read_bytes - 4)
50+
continue;
51+
52+
if (write(STDOUT_FILENO, buffer + 4, data_len) < 0) {
53+
perror("write");
54+
return -1;
55+
}
56+
}
57+
58+
close(fd);
59+
60+
return 0;
61+
}

0 commit comments

Comments
 (0)