[PATCH v2] libtraceevent: read btf file in utest if mmap fails

Emil Thorsoe <[email protected]> Wed, 6 May 2026 22:11:51 +0300
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
When mmap of the btf file fails in utest, fall back to malloc and
read.

This mmap fails with ENODEV on i686 nix build on nixos-unstable.

Signed-off-by: Emil Thorsoe <[email protected]>
---
 utest/traceevent-utest.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/utest/traceevent-utest.c b/utest/traceevent-utest.c
index b62411c..7d09bf2 100644
--- a/utest/traceevent-utest.c
+++ b/utest/traceevent-utest.c
@@ -388,6 +388,7 @@ static void test_btf_read(void)
 	struct stat st;
 	void *buf;
 	int fd, nr = 6;
+	bool malloced = false;
 
 	fd = open("/sys/kernel/btf/vmlinux", O_RDONLY);
 	if (fd < 0) {
@@ -397,11 +398,23 @@ static void test_btf_read(void)
 	CU_TEST(fstat(fd, &st) == 0);
 
 	buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-	CU_TEST(buf != MAP_FAILED);
-
+	if (buf == MAP_FAILED) {
+		malloced = true;
+		buf = malloc(st.st_size);
+		if (buf == NULL) {
+			printf("[FAILED TO ALLOCATE MEMORY FOR BTF FILE CONTENTS] ...");
+			close(fd);
+			return;
+		}
+		CU_TEST(read(fd, buf, st.st_size) == st.st_size);
+	}
 	CU_TEST(tep_load_btf(test_tep, buf, st.st_size) == 0);
 
-	munmap(buf, st.st_size);
+	if (malloced) {
+		free(buf);
+	} else {
+		munmap(buf, st.st_size);
+	}
 	close(fd);
 
 	trace_seq_init(s);
-- 
2.53.0