Skip to content

Commit c8617e8

Browse files
eddyz87anakryiko
authored andcommitted
selftests/bpf: Utility functions to capture libbpf log in test_progs
Several test_progs tests already capture libbpf log in order to check for some expected output, e.g bpf_tcp_ca.c, kfunc_dynptr_param.c, log_buf.c and a few others. This commit provides a, hopefully, simple API to capture libbpf log w/o necessity to define new print callback in each test: /* Creates a global memstream capturing INFO and WARN level output * passed to libbpf_print_fn. * Returns 0 on success, negative value on failure. * On failure the description is printed using PRINT_FAIL and * current test case is marked as fail. */ int start_libbpf_log_capture(void) /* Destroys global memstream created by start_libbpf_log_capture(). * Returns a pointer to captured data which has to be freed. * Returned buffer is null terminated. */ char *stop_libbpf_log_capture(void) The intended usage is as follows: if (start_libbpf_log_capture()) return; use_libbpf(); char *log = stop_libbpf_log_capture(); ASSERT_HAS_SUBSTR(log, "... expected ...", "expected some message"); free(log); As a safety measure, free(start_libbpf_log_capture()) is invoked in the epilogue of the test_progs.c:run_one_test(). Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5bab7a2 commit c8617e8

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

tools/testing/selftests/bpf/test_progs.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,69 @@ static const struct argp_option opts[] = {
683683
{},
684684
};
685685

686+
static FILE *libbpf_capture_stream;
687+
688+
static struct {
689+
char *buf;
690+
size_t buf_sz;
691+
} libbpf_output_capture;
692+
693+
/* Creates a global memstream capturing INFO and WARN level output
694+
* passed to libbpf_print_fn.
695+
* Returns 0 on success, negative value on failure.
696+
* On failure the description is printed using PRINT_FAIL and
697+
* current test case is marked as fail.
698+
*/
699+
int start_libbpf_log_capture(void)
700+
{
701+
if (libbpf_capture_stream) {
702+
PRINT_FAIL("%s: libbpf_capture_stream != NULL\n", __func__);
703+
return -EINVAL;
704+
}
705+
706+
libbpf_capture_stream = open_memstream(&libbpf_output_capture.buf,
707+
&libbpf_output_capture.buf_sz);
708+
if (!libbpf_capture_stream) {
709+
PRINT_FAIL("%s: open_memstream failed errno=%d\n", __func__, errno);
710+
return -EINVAL;
711+
}
712+
713+
return 0;
714+
}
715+
716+
/* Destroys global memstream created by start_libbpf_log_capture().
717+
* Returns a pointer to captured data which has to be freed.
718+
* Returned buffer is null terminated.
719+
*/
720+
char *stop_libbpf_log_capture(void)
721+
{
722+
char *buf;
723+
724+
if (!libbpf_capture_stream)
725+
return NULL;
726+
727+
fputc(0, libbpf_capture_stream);
728+
fclose(libbpf_capture_stream);
729+
libbpf_capture_stream = NULL;
730+
/* get 'buf' after fclose(), see open_memstream() documentation */
731+
buf = libbpf_output_capture.buf;
732+
memset(&libbpf_output_capture, 0, sizeof(libbpf_output_capture));
733+
return buf;
734+
}
735+
686736
static int libbpf_print_fn(enum libbpf_print_level level,
687737
const char *format, va_list args)
688738
{
739+
if (libbpf_capture_stream && level != LIBBPF_DEBUG) {
740+
va_list args2;
741+
742+
va_copy(args2, args);
743+
vfprintf(libbpf_capture_stream, format, args2);
744+
}
745+
689746
if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG)
690747
return 0;
748+
691749
vfprintf(stdout, format, args);
692750
return 0;
693751
}
@@ -1081,6 +1139,7 @@ static void run_one_test(int test_num)
10811139
cleanup_cgroup_environment();
10821140

10831141
stdio_restore();
1142+
free(stop_libbpf_log_capture());
10841143

10851144
dump_test_log(test, state, false, false, NULL);
10861145
}

tools/testing/selftests/bpf/test_progs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ int test__join_cgroup(const char *path);
397397
system(cmd); \
398398
})
399399

400+
int start_libbpf_log_capture(void);
401+
char *stop_libbpf_log_capture(void);
402+
400403
static inline __u64 ptr_to_u64(const void *ptr)
401404
{
402405
return (__u64) (unsigned long) ptr;

0 commit comments

Comments
 (0)