Linux Trace Development
 help / color / mirror / Atom feed
* [PATCH 0/2] libtraceevent: Fix BTF utest host dependencies
@ 2026-06-01 10:37 Cao Ruichuang
  2026-06-01 10:37 ` [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF Cao Ruichuang
  2026-06-01 10:37 ` [PATCH 2/2] libtraceevent utest: Avoid host-specific getname_flags output Cao Ruichuang
  0 siblings, 2 replies; 4+ messages in thread
From: Cao Ruichuang @ 2026-06-01 10:37 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: linux-trace-devel, Tzvetomir Stoyanov, Emil Thorsoe,
	Cao Ruichuang

Fix two independent host-dependent failures in the BTF utest.

The first patch handles short reads from /sys/kernel/btf/vmlinux after
mmap() fails. On a Rocky Linux 9.7 system, mmap() fails with ENODEV and a
single read() returns 4096 bytes from a 5150407-byte BTF file, while a read
loop returns the full file.

The second patch avoids failing the exact getname_flags() output check when
the host kernel BTF has extra arguments appended after the expected
arguments. On the same system, getname_flags() has a third parameter named
"empty", so tep_btf_print_args() correctly prints:

  getname_flags(filename=0x7ffe7d33f3d0, flags=0, empty=0x0)

Validation was done on x86_64 Rocky Linux 9.7, kernel
5.14.0-570.42.2.el9_6.x86_64:

  make -j$(nproc)
  make test
  ./utest/trace-utest -r traceevent

All commands completed successfully after this series.

Cao Ruichuang (2):
  libtraceevent utest: Handle short reads when reading BTF
  libtraceevent utest: Avoid host-specific getname_flags output

 utest/traceevent-utest.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)

-- 
2.39.5 (Apple Git-154)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF
  2026-06-01 10:37 [PATCH 0/2] libtraceevent: Fix BTF utest host dependencies Cao Ruichuang
@ 2026-06-01 10:37 ` Cao Ruichuang
  2026-06-01 14:11   ` Steven Rostedt
  2026-06-01 10:37 ` [PATCH 2/2] libtraceevent utest: Avoid host-specific getname_flags output Cao Ruichuang
  1 sibling, 1 reply; 4+ messages in thread
From: Cao Ruichuang @ 2026-06-01 10:37 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: linux-trace-devel, Tzvetomir Stoyanov, Emil Thorsoe,
	Cao Ruichuang

test_btf_read() falls back to malloc() and read() when mmap() of
/sys/kernel/btf/vmlinux fails. The fallback currently assumes that one
read(fd, buf, st.st_size) call returns the whole file.

That is not guaranteed. On a Rocky Linux 9.7 system with a readable
/sys/kernel/btf/vmlinux, mmap() fails with ENODEV and a single read()
returns only 4096 bytes from a 5150407-byte file. Reading in a loop
returns the complete file.

Read the BTF file in a loop so the fallback path handles short reads
correctly and avoids passing partial BTF data to tep_load_btf().

Fixes: 5b44859765fc ("libtraceevent utest: Read btf file in utest if mmap fails")
Assisted-by: Codex:gpt-5.5
Signed-off-by: Cao Ruichuang <create0818@163.com>
---
 utest/traceevent-utest.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/utest/traceevent-utest.c b/utest/traceevent-utest.c
index 1c3a4c2..b93d686 100644
--- a/utest/traceevent-utest.c
+++ b/utest/traceevent-utest.c
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <errno.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -506,6 +507,28 @@ static void test_parse_dma_addr(void)
 			    data, sizeof(data), "dma=0x1234abcd");
 }
 
+static int read_full(int fd, void *buf, size_t size)
+{
+	char *data = buf;
+	size_t total = 0;
+
+	while (total < size) {
+		ssize_t ret;
+
+		ret = read(fd, data + total, size - total);
+		if (ret < 0) {
+			if (errno == EINTR)
+				continue;
+			return -1;
+		}
+		if (!ret)
+			return -1;
+		total += ret;
+	}
+
+	return 0;
+}
+
 static void test_btf_read(void)
 {
 	uint64_t args[] = {0x7ffe7d33f3d0, 0, 0, 0, 0, 0};
@@ -525,6 +548,8 @@ static void test_btf_read(void)
 
 	buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 	if (buf == MAP_FAILED) {
+		int ret;
+
 		malloced = true;
 		buf = malloc(st.st_size);
 		if (buf == NULL) {
@@ -532,7 +557,13 @@ static void test_btf_read(void)
 			close(fd);
 			return;
 		}
-		CU_TEST(read(fd, buf, st.st_size) == st.st_size);
+		ret = read_full(fd, buf, st.st_size);
+		CU_TEST(ret == 0);
+		if (ret < 0) {
+			free(buf);
+			close(fd);
+			return;
+		}
 	}
 	CU_TEST(tep_load_btf(test_tep, buf, st.st_size) == 0);
 
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] libtraceevent utest: Avoid host-specific getname_flags output
  2026-06-01 10:37 [PATCH 0/2] libtraceevent: Fix BTF utest host dependencies Cao Ruichuang
  2026-06-01 10:37 ` [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF Cao Ruichuang
@ 2026-06-01 10:37 ` Cao Ruichuang
  1 sibling, 0 replies; 4+ messages in thread
From: Cao Ruichuang @ 2026-06-01 10:37 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: linux-trace-devel, Tzvetomir Stoyanov, Emil Thorsoe,
	Cao Ruichuang

test_btf_read() loads /sys/kernel/btf/vmlinux and expects
getname_flags() to print exactly two arguments:

  getname_flags(filename=0x7ffe7d33f3d0, flags=0)

That prototype is not stable across kernels. On a Rocky Linux 9.7
system, the kernel BTF for getname_flags() has a third parameter named
empty, and tep_btf_print_args() correctly prints it.

Keep the exact output check for hosts that match the expected prototype.
For hosts that append extra getname_flags() BTF parameters after the
expected arguments, treat that host-specific suffix as an acceptable
variant only if the output still has the expected argument prefix and a
complete closing suffix.

Fixes: 0294b73f17e9 ("libtraceevent utest: Add simple test to test BTF parsing")
Assisted-by: Codex:gpt-5.5
Signed-off-by: Cao Ruichuang <create0818@163.com>
---
 utest/traceevent-utest.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/utest/traceevent-utest.c b/utest/traceevent-utest.c
index b93d686..04f6726 100644
--- a/utest/traceevent-utest.c
+++ b/utest/traceevent-utest.c
@@ -16,6 +16,7 @@
 #include <time.h>
 #include <dirent.h>
 #include <ftw.h>
+#include <string.h>
 
 #include <sys/mman.h>
 #include <stdint.h>
@@ -529,10 +530,21 @@ static int read_full(int fd, void *buf, size_t size)
 	return 0;
 }
 
+static bool output_has_extra_args(const char *output, const char *prefix)
+{
+	size_t len = strlen(output);
+	size_t plen = strlen(prefix);
+
+	return len > plen + 3 && strncmp(output, prefix, plen) == 0 &&
+	       output[plen] == ',' && strcmp(output + len - 2, ")\n") == 0;
+}
+
 static void test_btf_read(void)
 {
 	uint64_t args[] = {0x7ffe7d33f3d0, 0, 0, 0, 0, 0};
 	const char *func = "getname_flags";
+	const char *expected = "getname_flags(filename=0x7ffe7d33f3d0, flags=0)\n";
+	const char *expected_prefix = "getname_flags(filename=0x7ffe7d33f3d0, flags=0";
 	struct trace_seq *s = test_seq;
 	struct stat st;
 	void *buf;
@@ -582,7 +594,13 @@ static void test_btf_read(void)
 	trace_seq_puts(s, ")\n");
 	trace_seq_terminate(s);
 
-	CU_TEST(strcmp(s->buffer, "getname_flags(filename=0x7ffe7d33f3d0, flags=0)\n") == 0);
+	if (strcmp(s->buffer, expected) != 0 &&
+	    output_has_extra_args(s->buffer, expected_prefix)) {
+		printf("[KERNEL BTF HAS EXTRA getname_flags ARGS] ...");
+		return;
+	}
+
+	CU_TEST(strcmp(s->buffer, expected) == 0);
 }
 
 static int test_suite_destroy(void)
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF
  2026-06-01 10:37 ` [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF Cao Ruichuang
@ 2026-06-01 14:11   ` Steven Rostedt
  0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2026-06-01 14:11 UTC (permalink / raw)
  To: Cao Ruichuang; +Cc: linux-trace-devel, Tzvetomir Stoyanov, Emil Thorsoe

On Mon,  1 Jun 2026 18:37:49 +0800
Cao Ruichuang <create0818@163.com> wrote:

> @@ -532,7 +557,13 @@ static void test_btf_read(void)
>  			close(fd);
>  			return;
>  		}
> -		CU_TEST(read(fd, buf, st.st_size) == st.st_size);
> +		ret = read_full(fd, buf, st.st_size);
> +		CU_TEST(ret == 0);

Nit, can you change this to:

		CU_TEST((ret = read_full(fd, buf, st.st_size)) == 0);

so that if it fails, it doesn't just report "ret == 0" in the failure.

I'm working to make the CU_TEST() more informative on failures.

Thanks,

-- Steve


> +		if (ret < 0) {
> +			free(buf);
> +			close(fd);
> +			return;
> +		}
>  	}

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-01 14:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-01 10:37 [PATCH 0/2] libtraceevent: Fix BTF utest host dependencies Cao Ruichuang
2026-06-01 10:37 ` [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF Cao Ruichuang
2026-06-01 14:11   ` Steven Rostedt
2026-06-01 10:37 ` [PATCH 2/2] libtraceevent utest: Avoid host-specific getname_flags output Cao Ruichuang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox