Skip to content

Commit 946152b

Browse files
fomichevAlexei Starovoitov
authored andcommitted
selftests/bpf: test_progs: switch to open_memstream
Use open_memstream to override stdout during test execution. The copy of the original stdout is held in env.stdout and used to print subtest info and dump failed log. test_{v,}printf are now simple wrappers around stdout and will be removed in the next patch. v5: * fix -v crash by always setting env.std{in,err} (Alexei Starovoitov) * drop force_log check from stdio_hijack (Andrii Nakryiko) v4: * one field per line for stdout/stderr (Andrii Nakryiko) v3: * don't do strlen over log_buf, log_cnt has it already (Andrii Nakryiko) v2: * add ifdef __GLIBC__ around open_memstream (maybe pointless since we already depend on glibc for argp_parse) * hijack stderr as well (Andrii Nakryiko) * don't hijack for every test, do it once (Andrii Nakryiko) * log_cap -> log_size (Andrii Nakryiko) * do fseeko in a proper place (Andrii Nakryiko) * check open_memstream returned value (Andrii Nakryiko) Cc: Andrii Nakryiko <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Stanislav Fomichev <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 8c30396 commit 946152b

File tree

2 files changed

+62
-56
lines changed

2 files changed

+62
-56
lines changed

tools/testing/selftests/bpf/test_progs.c

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ static bool should_run(struct test_selector *sel, int num, const char *name)
4040

4141
static void dump_test_log(const struct prog_test_def *test, bool failed)
4242
{
43+
if (stdout == env.stdout)
44+
return;
45+
46+
fflush(stdout); /* exports env.log_buf & env.log_cnt */
47+
4348
if (env.verbose || test->force_log || failed) {
4449
if (env.log_cnt) {
45-
fprintf(stdout, "%s", env.log_buf);
50+
fprintf(env.stdout, "%s", env.log_buf);
4651
if (env.log_buf[env.log_cnt - 1] != '\n')
47-
fprintf(stdout, "\n");
52+
fprintf(env.stdout, "\n");
4853
}
4954
}
50-
env.log_cnt = 0;
55+
56+
fseeko(stdout, 0, SEEK_SET); /* rewind */
5157
}
5258

5359
void test__end_subtest()
@@ -62,7 +68,7 @@ void test__end_subtest()
6268

6369
dump_test_log(test, sub_error_cnt);
6470

65-
printf("#%d/%d %s:%s\n",
71+
fprintf(env.stdout, "#%d/%d %s:%s\n",
6672
test->test_num, test->subtest_num,
6773
test->subtest_name, sub_error_cnt ? "FAIL" : "OK");
6874
}
@@ -79,7 +85,8 @@ bool test__start_subtest(const char *name)
7985
test->subtest_num++;
8086

8187
if (!name || !name[0]) {
82-
fprintf(stderr, "Subtest #%d didn't provide sub-test name!\n",
88+
fprintf(env.stderr,
89+
"Subtest #%d didn't provide sub-test name!\n",
8390
test->subtest_num);
8491
return false;
8592
}
@@ -100,53 +107,7 @@ void test__force_log() {
100107

101108
void test__vprintf(const char *fmt, va_list args)
102109
{
103-
size_t rem_sz;
104-
int ret = 0;
105-
106-
if (env.verbose || (env.test && env.test->force_log)) {
107-
vfprintf(stderr, fmt, args);
108-
return;
109-
}
110-
111-
try_again:
112-
rem_sz = env.log_cap - env.log_cnt;
113-
if (rem_sz) {
114-
va_list ap;
115-
116-
va_copy(ap, args);
117-
/* we reserved extra byte for \0 at the end */
118-
ret = vsnprintf(env.log_buf + env.log_cnt, rem_sz + 1, fmt, ap);
119-
va_end(ap);
120-
121-
if (ret < 0) {
122-
env.log_buf[env.log_cnt] = '\0';
123-
fprintf(stderr, "failed to log w/ fmt '%s'\n", fmt);
124-
return;
125-
}
126-
}
127-
128-
if (!rem_sz || ret > rem_sz) {
129-
size_t new_sz = env.log_cap * 3 / 2;
130-
char *new_buf;
131-
132-
if (new_sz < 4096)
133-
new_sz = 4096;
134-
if (new_sz < ret + env.log_cnt)
135-
new_sz = ret + env.log_cnt;
136-
137-
/* +1 for guaranteed space for terminating \0 */
138-
new_buf = realloc(env.log_buf, new_sz + 1);
139-
if (!new_buf) {
140-
fprintf(stderr, "failed to realloc log buffer: %d\n",
141-
errno);
142-
return;
143-
}
144-
env.log_buf = new_buf;
145-
env.log_cap = new_sz;
146-
goto try_again;
147-
}
148-
149-
env.log_cnt += ret;
110+
vprintf(fmt, args);
150111
}
151112

152113
void test__printf(const char *fmt, ...)
@@ -477,6 +438,48 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
477438
return 0;
478439
}
479440

441+
static void stdio_hijack(void)
442+
{
443+
#ifdef __GLIBC__
444+
env.stdout = stdout;
445+
env.stderr = stderr;
446+
447+
if (env.verbose) {
448+
/* nothing to do, output to stdout by default */
449+
return;
450+
}
451+
452+
/* stdout and stderr -> buffer */
453+
fflush(stdout);
454+
455+
stdout = open_memstream(&env.log_buf, &env.log_cnt);
456+
if (!stdout) {
457+
stdout = env.stdout;
458+
perror("open_memstream");
459+
return;
460+
}
461+
462+
stderr = stdout;
463+
#endif
464+
}
465+
466+
static void stdio_restore(void)
467+
{
468+
#ifdef __GLIBC__
469+
if (stdout == env.stdout)
470+
return;
471+
472+
fclose(stdout);
473+
free(env.log_buf);
474+
475+
env.log_buf = NULL;
476+
env.log_cnt = 0;
477+
478+
stdout = env.stdout;
479+
stderr = env.stderr;
480+
#endif
481+
}
482+
480483
int main(int argc, char **argv)
481484
{
482485
static const struct argp argp = {
@@ -496,6 +499,7 @@ int main(int argc, char **argv)
496499

497500
env.jit_enabled = is_jit_enabled();
498501

502+
stdio_hijack();
499503
for (i = 0; i < prog_test_cnt; i++) {
500504
struct prog_test_def *test = &prog_test_defs[i];
501505
int old_pass_cnt = pass_cnt;
@@ -523,13 +527,14 @@ int main(int argc, char **argv)
523527

524528
dump_test_log(test, test->error_cnt);
525529

526-
printf("#%d %s:%s\n", test->test_num, test->test_name,
527-
test->error_cnt ? "FAIL" : "OK");
530+
fprintf(env.stdout, "#%d %s:%s\n",
531+
test->test_num, test->test_name,
532+
test->error_cnt ? "FAIL" : "OK");
528533
}
534+
stdio_restore();
529535
printf("Summary: %d/%d PASSED, %d FAILED\n",
530536
env.succ_cnt, env.sub_succ_cnt, env.fail_cnt);
531537

532-
free(env.log_buf);
533538
free(env.test_selector.num_set);
534539
free(env.subtest_selector.num_set);
535540

tools/testing/selftests/bpf/test_progs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ struct test_env {
5656
bool jit_enabled;
5757

5858
struct prog_test_def *test;
59+
FILE *stdout;
60+
FILE *stderr;
5961
char *log_buf;
6062
size_t log_cnt;
61-
size_t log_cap;
6263

6364
int succ_cnt; /* successful tests */
6465
int sub_succ_cnt; /* successful sub-tests */

0 commit comments

Comments
 (0)