Skip to content

Commit e12873e

Browse files
mykyta5anakryiko
authored andcommitted
selftests/bpf: Add BPF program dump in veristat
Add the ability to dump BPF program instructions directly from veristat. Previously, inspecting a program required separate bpftool invocations: one to load and another to dump it, which meant running multiple commands. During active development, it's common for developers to use veristat for testing verification. Integrating instruction dumping into veristat reduces the need to switch tools and simplifies the workflow. By making this information more readily accessible, this change aims to streamline the BPF development cycle and improve usability for developers. This implementation leverages bpftool, by running it directly via popen to avoid any code duplication and keep veristat simple. Signed-off-by: Mykyta Yatsenko <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9621eb6 commit e12873e

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

tools/testing/selftests/bpf/veristat.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ struct var_preset {
181181
bool applied;
182182
};
183183

184+
enum dump_mode {
185+
DUMP_NONE = 0,
186+
DUMP_XLATED = 1,
187+
DUMP_JITED = 2,
188+
};
189+
184190
static struct env {
185191
char **filenames;
186192
int filename_cnt;
@@ -227,6 +233,7 @@ static struct env {
227233
char orig_cgroup[PATH_MAX];
228234
char stat_cgroup[PATH_MAX];
229235
int memory_peak_fd;
236+
__u32 dump_mode;
230237
} env;
231238

232239
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
@@ -271,6 +278,7 @@ const char argp_program_doc[] =
271278
enum {
272279
OPT_LOG_FIXED = 1000,
273280
OPT_LOG_SIZE = 1001,
281+
OPT_DUMP = 1002,
274282
};
275283

276284
static const struct argp_option opts[] = {
@@ -295,6 +303,7 @@ static const struct argp_option opts[] = {
295303
"Force BPF verifier failure on register invariant violation (BPF_F_TEST_REG_INVARIANTS program flag)" },
296304
{ "top-src-lines", 'S', "N", 0, "Emit N most frequent source code lines" },
297305
{ "set-global-vars", 'G', "GLOBAL", 0, "Set global variables provided in the expression, for example \"var1 = 1\"" },
306+
{ "dump", OPT_DUMP, "DUMP_MODE", OPTION_ARG_OPTIONAL, "Print BPF program dump (xlated, jited)" },
298307
{},
299308
};
300309

@@ -427,6 +436,16 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
427436
return err;
428437
}
429438
break;
439+
case OPT_DUMP:
440+
if (!arg || strcasecmp(arg, "xlated") == 0) {
441+
env.dump_mode |= DUMP_XLATED;
442+
} else if (strcasecmp(arg, "jited") == 0) {
443+
env.dump_mode |= DUMP_JITED;
444+
} else {
445+
fprintf(stderr, "Unrecognized dump mode '%s'\n", arg);
446+
return -EINVAL;
447+
}
448+
break;
430449
default:
431450
return ARGP_ERR_UNKNOWN;
432451
}
@@ -1554,6 +1573,36 @@ static int parse_rvalue(const char *val, struct rvalue *rvalue)
15541573
return 0;
15551574
}
15561575

1576+
static void dump(__u32 prog_id, enum dump_mode mode, const char *file_name, const char *prog_name)
1577+
{
1578+
char command[64], buf[4096];
1579+
FILE *fp;
1580+
int status;
1581+
1582+
status = system("command -v bpftool > /dev/null 2>&1");
1583+
if (status != 0) {
1584+
fprintf(stderr, "bpftool is not available, can't print program dump\n");
1585+
return;
1586+
}
1587+
snprintf(command, sizeof(command), "bpftool prog dump %s id %u",
1588+
mode == DUMP_JITED ? "jited" : "xlated", prog_id);
1589+
fp = popen(command, "r");
1590+
if (!fp) {
1591+
fprintf(stderr, "bpftool failed with error: %d\n", errno);
1592+
return;
1593+
}
1594+
1595+
printf("DUMP (%s) %s/%s:\n", mode == DUMP_JITED ? "JITED" : "XLATED", file_name, prog_name);
1596+
while (fgets(buf, sizeof(buf), fp))
1597+
fputs(buf, stdout);
1598+
fprintf(stdout, "\n");
1599+
1600+
if (ferror(fp))
1601+
fprintf(stderr, "Failed to dump BPF prog with error: %d\n", errno);
1602+
1603+
pclose(fp);
1604+
}
1605+
15571606
static int process_prog(const char *filename, struct bpf_object *obj, struct bpf_program *prog)
15581607
{
15591608
const char *base_filename = basename(strdupa(filename));
@@ -1630,8 +1679,13 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
16301679

16311680
memset(&info, 0, info_len);
16321681
fd = bpf_program__fd(prog);
1633-
if (fd > 0 && bpf_prog_get_info_by_fd(fd, &info, &info_len) == 0)
1682+
if (fd > 0 && bpf_prog_get_info_by_fd(fd, &info, &info_len) == 0) {
16341683
stats->stats[JITED_SIZE] = info.jited_prog_len;
1684+
if (env.dump_mode & DUMP_JITED)
1685+
dump(info.id, DUMP_JITED, base_filename, prog_name);
1686+
if (env.dump_mode & DUMP_XLATED)
1687+
dump(info.id, DUMP_XLATED, base_filename, prog_name);
1688+
}
16351689

16361690
parse_verif_log(buf, buf_sz, stats);
16371691

0 commit comments

Comments
 (0)