Skip to content

Commit a8a1f7d

Browse files
fomichevAlexei Starovoitov
authored andcommitted
libbpf: fix libbpf_print
With the recent print rework we now have the following problem: pr_{warning,info,debug} expand to __pr which calls libbpf_print. libbpf_print does va_start and calls __libbpf_pr with va_list argument. In __base_pr we again do va_start. Because the next argument is a va_list, we don't get correct pointer to the argument (and print noting in my case, I don't know why it doesn't crash tbh). Fix this by changing libbpf_print_fn_t signature to accept va_list and remove unneeded calls to va_start in the existing users. Alternatively, this can we solved by exporting __libbpf_pr and changing __pr macro to (and killing libbpf_print): { if (__libbpf_pr) __libbpf_pr(level, "libbpf: " fmt, ##__VA_ARGS__) } Signed-off-by: Stanislav Fomichev <[email protected]> Acked-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1728b11 commit a8a1f7d

File tree

6 files changed

+13
-47
lines changed

6 files changed

+13
-47
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,16 @@
5454

5555
#define __printf(a, b) __attribute__((format(printf, a, b)))
5656

57-
__printf(2, 3)
58-
static int __base_pr(enum libbpf_print_level level, const char *format, ...)
57+
static int __base_pr(enum libbpf_print_level level, const char *format,
58+
va_list args)
5959
{
60-
va_list args;
61-
int err;
62-
6360
if (level == LIBBPF_DEBUG)
6461
return 0;
6562

66-
va_start(args, format);
67-
err = vfprintf(stderr, format, args);
68-
va_end(args);
69-
return err;
63+
return vfprintf(stderr, format, args);
7064
}
7165

72-
static __printf(2, 3) libbpf_print_fn_t __libbpf_pr = __base_pr;
66+
static libbpf_print_fn_t __libbpf_pr = __base_pr;
7367

7468
void libbpf_set_print(libbpf_print_fn_t fn)
7569
{

tools/lib/bpf/libbpf.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ enum libbpf_print_level {
5454
};
5555

5656
typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
57-
const char *, ...)
58-
__attribute__((format(printf, 2, 3)));
57+
const char *, va_list ap);
5958

6059
LIBBPF_API void libbpf_set_print(libbpf_print_fn_t fn);
6160

tools/perf/util/bpf-loader.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@
2525
#include "c++/clang-c.h"
2626

2727
static int libbpf_perf_print(enum libbpf_print_level level __attribute__((unused)),
28-
const char *fmt, ...)
28+
const char *fmt, va_list args)
2929
{
30-
va_list args;
31-
int ret;
32-
33-
va_start(args, fmt);
34-
ret = veprintf(1, verbose, pr_fmt(fmt), args);
35-
va_end(args);
36-
return ret;
30+
return veprintf(1, verbose, pr_fmt(fmt), args);
3731
}
3832

3933
struct bpf_prog_priv {

tools/testing/selftests/bpf/test_btf.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,10 @@ static int count_result(int err)
5252
return err;
5353
}
5454

55-
#define __printf(a, b) __attribute__((format(printf, a, b)))
56-
57-
__printf(2, 3)
5855
static int __base_pr(enum libbpf_print_level level __attribute__((unused)),
59-
const char *format, ...)
56+
const char *format, va_list args)
6057
{
61-
va_list args;
62-
int err;
63-
64-
va_start(args, format);
65-
err = vfprintf(stderr, format, args);
66-
va_end(args);
67-
return err;
58+
return vfprintf(stderr, format, args);
6859
}
6960

7061
#define BTF_INFO_ENC(kind, kind_flag, vlen) \

tools/testing/selftests/bpf/test_libbpf_open.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,13 @@ static void usage(char *argv[])
3636

3737
static bool debug = 0;
3838
static int libbpf_debug_print(enum libbpf_print_level level,
39-
const char *fmt, ...)
39+
const char *fmt, va_list args)
4040
{
41-
va_list args;
42-
int ret;
43-
4441
if (level == LIBBPF_DEBUG && !debug)
4542
return 0;
4643

47-
va_start(args, fmt);
4844
fprintf(stderr, "[%d] ", level);
49-
ret = vfprintf(stderr, fmt, args);
50-
va_end(args);
51-
return ret;
45+
return vfprintf(stderr, fmt, args);
5246
}
5347

5448
#define EXIT_FAIL_LIBBPF EXIT_FAILURE

tools/testing/selftests/bpf/test_progs.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,18 +1785,12 @@ static void test_task_fd_query_tp(void)
17851785
}
17861786

17871787
static int libbpf_debug_print(enum libbpf_print_level level,
1788-
const char *format, ...)
1788+
const char *format, va_list args)
17891789
{
1790-
va_list args;
1791-
int ret;
1792-
17931790
if (level == LIBBPF_DEBUG)
17941791
return 0;
17951792

1796-
va_start(args, format);
1797-
ret = vfprintf(stderr, format, args);
1798-
va_end(args);
1799-
return ret;
1793+
return vfprintf(stderr, format, args);
18001794
}
18011795

18021796
static void test_reference_tracking()

0 commit comments

Comments
 (0)