new file mode 100644
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "bpfdump_test_kern.skel.h"
+
+void test_bpfdump_test(void)
+{
+ int err, prog_fd, dumper_fd, duration = 0;
+ struct bpfdump_test_kern *skel;
+ char buf[16] = {};
+ const char *expected = "0A1B2C3D";
+
+ skel = bpfdump_test_kern__open_and_load();
+ if (CHECK(!skel, "skel_open_and_load",
+ "skeleton open_and_load failed\n"))
+ return;
+
+ prog_fd = bpf_program__fd(skel->progs.dump_tasks);
+ dumper_fd = bpf_raw_tracepoint_open(NULL, prog_fd);
+ if (CHECK(dumper_fd < 0, "bpf_raw_tracepoint_open",
+ "anonymous dumper creation failed\n"))
+ goto destroy_skel;
+
+ err = -EINVAL;
+ while (read(dumper_fd, buf, sizeof(buf)) > 0) {
+ if (CHECK(!err, "read", "unexpected extra read\n"))
+ goto close_fd;
+
+ err = strcmp(buf, expected) != 0;
+ if (CHECK(err, "read",
+ "read failed: buf %s, expected %s\n", buf,
+ expected))
+ goto close_fd;
+ }
+
+ CHECK(err, "read", "real failed: no read, expected %s\n",
+ expected);
+
+close_fd:
+ close(dumper_fd);
+destroy_skel:
+ bpfdump_test_kern__destroy(skel);
+}
new file mode 100644
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+int count = 0;
+
+SEC("dump//sys/kernel/bpfdump/task")
+int dump_tasks(struct bpfdump__task *ctx)
+{
+ struct seq_file *seq = ctx->meta->seq;
+ struct task_struct *task = ctx->task;
+ static char fmt[] = "%d";
+ char c;
+
+ if (task == (void *)0)
+ return 0;
+
+ if (count < 4) {
+ bpf_seq_printf(seq, fmt, sizeof(fmt), count);
+ c = 'A' + count;
+ bpf_seq_write(seq, &c, sizeof(c));
+ count++;
+ }
+
+ return 0;
+}
The selftest creates a anonymous dumper for the /sys/kernel/bpfdump/task/ target and ensure the user space got the expected contents. Both bpf_seq_printf() and bpf_seq_write() helpers are tested in this selftest. $ test_progs -n 2 #2 bpfdump_test:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yonghong Song <yhs@fb.com> --- .../selftests/bpf/prog_tests/bpfdump_test.c | 42 +++++++++++++++++++ .../selftests/bpf/progs/bpfdump_test_kern.c | 31 ++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpfdump_test.c create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_test_kern.c