Skip to content

Commit a8fdaad

Browse files
anakryikoAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Integrate verbose verifier log into test_progs
Add exra level of verboseness, activated by -vvv argument. When -vv is specified, verbose libbpf and verifier log (level 1) is output, even for successful tests. With -vvv, verifier log goes to level 2. This is extremely useful to debug verifier failures, as well as just see the state and flow of verification. Before this, you'd have to go and modify load_program()'s source code inside libbpf to specify extra log_level flags, which is suboptimal to say the least. Currently -vv and -vvv triggering verifier output is integrated into test_stub's bpf_prog_load as well as bpf_verif_scale.c tests. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5940c5b commit a8fdaad

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ static int libbpf_debug_print(enum libbpf_print_level level,
1515
return 0;
1616
}
1717

18+
extern int extra_prog_load_log_flags;
19+
1820
static int check_load(const char *file, enum bpf_prog_type type)
1921
{
2022
struct bpf_prog_load_attr attr;
@@ -24,7 +26,7 @@ static int check_load(const char *file, enum bpf_prog_type type)
2426
memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
2527
attr.file = file;
2628
attr.prog_type = type;
27-
attr.log_level = 4;
29+
attr.log_level = 4 | extra_prog_load_log_flags;
2830
attr.prog_flags = BPF_F_TEST_RND_HI32;
2931
err = bpf_prog_load_xattr(&attr, &obj, &prog_fd);
3032
bpf_object__close(obj);

tools/testing/selftests/bpf/test_progs.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void dump_test_log(const struct prog_test_def *test, bool failed)
4545

4646
fflush(stdout); /* exports env.log_buf & env.log_cnt */
4747

48-
if (env.verbose || test->force_log || failed) {
48+
if (env.verbosity > VERBOSE_NONE || test->force_log || failed) {
4949
if (env.log_cnt) {
5050
env.log_buf[env.log_cnt] = '\0';
5151
fprintf(env.stdout, "%s", env.log_buf);
@@ -346,14 +346,14 @@ static const struct argp_option opts[] = {
346346
{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
347347
"Output verifier statistics", },
348348
{ "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
349-
"Verbose output (use -vv for extra verbose output)" },
349+
"Verbose output (use -vv or -vvv for progressively verbose output)" },
350350
{},
351351
};
352352

353353
static int libbpf_print_fn(enum libbpf_print_level level,
354354
const char *format, va_list args)
355355
{
356-
if (!env.very_verbose && level == LIBBPF_DEBUG)
356+
if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG)
357357
return 0;
358358
vprintf(format, args);
359359
return 0;
@@ -419,6 +419,8 @@ int parse_num_list(const char *s, struct test_selector *sel)
419419
return 0;
420420
}
421421

422+
extern int extra_prog_load_log_flags;
423+
422424
static error_t parse_arg(int key, char *arg, struct argp_state *state)
423425
{
424426
struct test_env *env = state->input;
@@ -460,17 +462,21 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
460462
env->verifier_stats = true;
461463
break;
462464
case ARG_VERBOSE:
465+
env->verbosity = VERBOSE_NORMAL;
463466
if (arg) {
464467
if (strcmp(arg, "v") == 0) {
465-
env->very_verbose = true;
468+
env->verbosity = VERBOSE_VERY;
469+
extra_prog_load_log_flags = 1;
470+
} else if (strcmp(arg, "vv") == 0) {
471+
env->verbosity = VERBOSE_SUPER;
472+
extra_prog_load_log_flags = 2;
466473
} else {
467474
fprintf(stderr,
468475
"Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
469476
arg);
470477
return -EINVAL;
471478
}
472479
}
473-
env->verbose = true;
474480
break;
475481
case ARGP_KEY_ARG:
476482
argp_usage(state);
@@ -489,7 +495,7 @@ static void stdio_hijack(void)
489495
env.stdout = stdout;
490496
env.stderr = stderr;
491497

492-
if (env.verbose) {
498+
if (env.verbosity > VERBOSE_NONE) {
493499
/* nothing to do, output to stdout by default */
494500
return;
495501
}

tools/testing/selftests/bpf/test_progs.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ typedef __u16 __sum16;
3939
#include "trace_helpers.h"
4040
#include "flow_dissector_load.h"
4141

42+
enum verbosity {
43+
VERBOSE_NONE,
44+
VERBOSE_NORMAL,
45+
VERBOSE_VERY,
46+
VERBOSE_SUPER,
47+
};
48+
4249
struct test_selector {
4350
const char *name;
4451
bool *num_set;
@@ -49,8 +56,7 @@ struct test_env {
4956
struct test_selector test_selector;
5057
struct test_selector subtest_selector;
5158
bool verifier_stats;
52-
bool verbose;
53-
bool very_verbose;
59+
enum verbosity verbosity;
5460

5561
bool jit_enabled;
5662

tools/testing/selftests/bpf/test_stub.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <bpf/libbpf.h>
66
#include <string.h>
77

8+
int extra_prog_load_log_flags = 0;
9+
810
int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
911
struct bpf_object **pobj, int *prog_fd)
1012
{
@@ -15,6 +17,7 @@ int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
1517
attr.prog_type = type;
1618
attr.expected_attach_type = 0;
1719
attr.prog_flags = BPF_F_TEST_RND_HI32;
20+
attr.log_level = extra_prog_load_log_flags;
1821

1922
return bpf_prog_load_xattr(&attr, pobj, prog_fd);
2023
}
@@ -35,6 +38,7 @@ int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
3538
load_attr.license = license;
3639
load_attr.kern_version = kern_version;
3740
load_attr.prog_flags = BPF_F_TEST_RND_HI32;
41+
load_attr.log_level = extra_prog_load_log_flags;
3842

3943
return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
4044
}

0 commit comments

Comments
 (0)