summaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/libbpf.c
diff options
context:
space:
mode:
authorYonghong Song2019-02-02 01:14:14 +0100
committerAlexei Starovoitov2019-02-04 18:40:58 +0100
commit8461ef8b7ef286212ca954d8b82dac3ceecb219d (patch)
tree3bcd5c6a65655d88808ed72815e0e68f8cf02dd9 /tools/lib/bpf/libbpf.c
parentsocket: fix for Add SO_TIMESTAMP[NS]_NEW (diff)
downloadkernel-qcow2-linux-8461ef8b7ef286212ca954d8b82dac3ceecb219d.tar.gz
kernel-qcow2-linux-8461ef8b7ef286212ca954d8b82dac3ceecb219d.tar.xz
kernel-qcow2-linux-8461ef8b7ef286212ca954d8b82dac3ceecb219d.zip
tools/bpf: move libbpf pr_* debug print functions to headers
A global function libbpf_print, which is invisible outside the shared library, is defined to print based on levels. The pr_warning, pr_info and pr_debug macros are moved into the newly created header common.h. So any .c file including common.h can use these macros directly. Currently btf__new and btf_ext__new API has an argument getting __pr_debug function pointer into btf.c so the debugging information can be printed there. This patch removed this parameter from btf__new and btf_ext__new and directly using pr_debug in btf.c. Another global function libbpf_print_level_available, also invisible outside the shared library, can test whether a particular level debug printing is available or not. It is used in btf.c to test whether DEBUG level debug printing is availabl or not, based on which the log buffer will be allocated when loading btf to the kernel. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r--tools/lib/bpf/libbpf.c46
1 files changed, 32 insertions, 14 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 03bc01ca2577..eeba77b695ad 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -42,6 +42,7 @@
#include "bpf.h"
#include "btf.h"
#include "str_error.h"
+#include "libbpf_util.h"
#ifndef EM_BPF
#define EM_BPF 247
@@ -69,16 +70,6 @@ static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
static __printf(1, 2) libbpf_print_fn_t __pr_debug;
-#define __pr(func, fmt, ...) \
-do { \
- if ((func)) \
- (func)("libbpf: " fmt, ##__VA_ARGS__); \
-} while (0)
-
-#define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
-#define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
-#define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
-
void libbpf_set_print(libbpf_print_fn_t warn,
libbpf_print_fn_t info,
libbpf_print_fn_t debug)
@@ -88,6 +79,35 @@ void libbpf_set_print(libbpf_print_fn_t warn,
__pr_debug = debug;
}
+__printf(2, 3)
+void libbpf_print(enum libbpf_print_level level, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ if (level == LIBBPF_WARN) {
+ if (__pr_warning)
+ __pr_warning(format, args);
+ } else if (level == LIBBPF_INFO) {
+ if (__pr_info)
+ __pr_info(format, args);
+ } else {
+ if (__pr_debug)
+ __pr_debug(format, args);
+ }
+ va_end(args);
+}
+
+bool libbpf_print_level_available(enum libbpf_print_level level)
+{
+ if (level == LIBBPF_WARN)
+ return !!__pr_warning;
+ else if (level == LIBBPF_INFO)
+ return !!__pr_info;
+ else
+ return !!__pr_debug;
+}
+
#define STRERR_BUFSIZE 128
#define CHECK_ERR(action, err, out) do { \
@@ -839,8 +859,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
else if (strcmp(name, "maps") == 0)
obj->efile.maps_shndx = idx;
else if (strcmp(name, BTF_ELF_SEC) == 0) {
- obj->btf = btf__new(data->d_buf, data->d_size,
- __pr_debug);
+ obj->btf = btf__new(data->d_buf, data->d_size);
if (IS_ERR(obj->btf)) {
pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
BTF_ELF_SEC, PTR_ERR(obj->btf));
@@ -915,8 +934,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
BTF_EXT_ELF_SEC, BTF_ELF_SEC);
} else {
obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
- btf_ext_data->d_size,
- __pr_debug);
+ btf_ext_data->d_size);
if (IS_ERR(obj->btf_ext)) {
pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
BTF_EXT_ELF_SEC,