Skip to content

Commit 191598f

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
Merge branch 'veristat-files-list-txt-notation-for-object-files-list'
Eduard Zingerman says: ==================== veristat: @files-list.txt notation for object files list A few small veristat improvements: - It is possible to hit command line parameters number limit, e.g. when running veristat for all object files generated for test_progs. This patch-set adds an option to read objects files list from a file. - Correct usage of strerror() function. - Avoid printing log lines to CSV output. Changelog: - v1 -> v2: - replace strerror(errno) with strerror(-err) in patch #2 (Andrii) v1: https://lore.kernel.org/bpf/[email protected]/T/ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents a752ba4 + 346e4ca commit 191598f

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

tools/testing/selftests/bpf/veristat.c

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,11 @@ static int append_filter(struct filter **filters, int *cnt, const char *str);
268268
static int append_filter_file(const char *path);
269269
static int append_var_preset(struct var_preset **presets, int *cnt, const char *expr);
270270
static int append_var_preset_file(const char *filename);
271+
static int append_file(const char *path);
272+
static int append_file_from_file(const char *path);
271273

272274
static error_t parse_arg(int key, char *arg, struct argp_state *state)
273275
{
274-
void *tmp;
275276
int err;
276277

277278
switch (key) {
@@ -381,14 +382,14 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
381382
break;
382383
}
383384
case ARGP_KEY_ARG:
384-
tmp = realloc(env.filenames, (env.filename_cnt + 1) * sizeof(*env.filenames));
385-
if (!tmp)
386-
return -ENOMEM;
387-
env.filenames = tmp;
388-
env.filenames[env.filename_cnt] = strdup(arg);
389-
if (!env.filenames[env.filename_cnt])
390-
return -ENOMEM;
391-
env.filename_cnt++;
385+
if (arg[0] == '@')
386+
err = append_file_from_file(arg + 1);
387+
else
388+
err = append_file(arg);
389+
if (err) {
390+
fprintf(stderr, "Failed to collect BPF object files: %d\n", err);
391+
return err;
392+
}
392393
break;
393394
default:
394395
return ARGP_ERR_UNKNOWN;
@@ -659,7 +660,7 @@ static int append_filter_file(const char *path)
659660
f = fopen(path, "r");
660661
if (!f) {
661662
err = -errno;
662-
fprintf(stderr, "Failed to open filters in '%s': %s\n", path, strerror(err));
663+
fprintf(stderr, "Failed to open filters in '%s': %s\n", path, strerror(-err));
663664
return err;
664665
}
665666

@@ -689,6 +690,49 @@ static const struct stat_specs default_output_spec = {
689690
},
690691
};
691692

693+
static int append_file(const char *path)
694+
{
695+
void *tmp;
696+
697+
tmp = realloc(env.filenames, (env.filename_cnt + 1) * sizeof(*env.filenames));
698+
if (!tmp)
699+
return -ENOMEM;
700+
env.filenames = tmp;
701+
env.filenames[env.filename_cnt] = strdup(path);
702+
if (!env.filenames[env.filename_cnt])
703+
return -ENOMEM;
704+
env.filename_cnt++;
705+
return 0;
706+
}
707+
708+
static int append_file_from_file(const char *path)
709+
{
710+
char buf[1024];
711+
int err = 0;
712+
FILE *f;
713+
714+
f = fopen(path, "r");
715+
if (!f) {
716+
err = -errno;
717+
fprintf(stderr, "Failed to open object files list in '%s': %s\n",
718+
path, strerror(errno));
719+
return err;
720+
}
721+
722+
while (fscanf(f, " %1023[^\n]\n", buf) == 1) {
723+
/* lines starting with # are comments, skip them */
724+
if (buf[0] == '\0' || buf[0] == '#')
725+
continue;
726+
err = append_file(buf);
727+
if (err)
728+
goto cleanup;
729+
}
730+
731+
cleanup:
732+
fclose(f);
733+
return err;
734+
}
735+
692736
static const struct stat_specs default_csv_output_spec = {
693737
.spec_cnt = 14,
694738
.ids = {
@@ -1190,13 +1234,13 @@ static void fixup_obj(struct bpf_object *obj, struct bpf_program *prog, const ch
11901234
bpf_program__set_expected_attach_type(prog, attach_type);
11911235

11921236
if (!env.quiet) {
1193-
printf("Using guessed program type '%s' for %s/%s...\n",
1237+
fprintf(stderr, "Using guessed program type '%s' for %s/%s...\n",
11941238
libbpf_bpf_prog_type_str(prog_type),
11951239
filename, prog_name);
11961240
}
11971241
} else {
11981242
if (!env.quiet) {
1199-
printf("Failed to guess program type for freplace program with context type name '%s' for %s/%s. Consider using canonical type names to help veristat...\n",
1243+
fprintf(stderr, "Failed to guess program type for freplace program with context type name '%s' for %s/%s. Consider using canonical type names to help veristat...\n",
12001244
ctx_name, filename, prog_name);
12011245
}
12021246
}
@@ -1378,7 +1422,7 @@ static int append_var_preset_file(const char *filename)
13781422
f = fopen(filename, "rt");
13791423
if (!f) {
13801424
err = -errno;
1381-
fprintf(stderr, "Failed to open presets in '%s': %s\n", filename, strerror(err));
1425+
fprintf(stderr, "Failed to open presets in '%s': %s\n", filename, strerror(-err));
13821426
return -EINVAL;
13831427
}
13841428

0 commit comments

Comments
 (0)