Commit 8461ef8b authored by Yonghong Song's avatar Yonghong Song Committed by Alexei Starovoitov

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: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent cc733578
This diff is collapsed.
...@@ -55,11 +55,8 @@ struct btf_ext_header { ...@@ -55,11 +55,8 @@ struct btf_ext_header {
__u32 line_info_len; __u32 line_info_len;
}; };
typedef int (*btf_print_fn_t)(const char *, ...)
__attribute__((format(printf, 1, 2)));
LIBBPF_API void btf__free(struct btf *btf); LIBBPF_API void btf__free(struct btf *btf);
LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log); LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size);
LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
const char *type_name); const char *type_name);
LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf, LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
...@@ -70,7 +67,7 @@ LIBBPF_API int btf__fd(const struct btf *btf); ...@@ -70,7 +67,7 @@ LIBBPF_API int btf__fd(const struct btf *btf);
LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log); struct btf_ext *btf_ext__new(__u8 *data, __u32 size);
void btf_ext__free(struct btf_ext *btf_ext); void btf_ext__free(struct btf_ext *btf_ext);
int btf_ext__reloc_func_info(const struct btf *btf, int btf_ext__reloc_func_info(const struct btf *btf,
const struct btf_ext *btf_ext, const struct btf_ext *btf_ext,
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
#include "bpf.h" #include "bpf.h"
#include "btf.h" #include "btf.h"
#include "str_error.h" #include "str_error.h"
#include "libbpf_util.h"
#ifndef EM_BPF #ifndef EM_BPF
#define EM_BPF 247 #define EM_BPF 247
...@@ -69,16 +70,6 @@ static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr; ...@@ -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_info = __base_pr;
static __printf(1, 2) libbpf_print_fn_t __pr_debug; 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, void libbpf_set_print(libbpf_print_fn_t warn,
libbpf_print_fn_t info, libbpf_print_fn_t info,
libbpf_print_fn_t debug) libbpf_print_fn_t debug)
...@@ -88,6 +79,35 @@ void libbpf_set_print(libbpf_print_fn_t warn, ...@@ -88,6 +79,35 @@ void libbpf_set_print(libbpf_print_fn_t warn,
__pr_debug = debug; __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 STRERR_BUFSIZE 128
#define CHECK_ERR(action, err, out) do { \ #define CHECK_ERR(action, err, out) do { \
...@@ -839,8 +859,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) ...@@ -839,8 +859,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
else if (strcmp(name, "maps") == 0) else if (strcmp(name, "maps") == 0)
obj->efile.maps_shndx = idx; obj->efile.maps_shndx = idx;
else if (strcmp(name, BTF_ELF_SEC) == 0) { else if (strcmp(name, BTF_ELF_SEC) == 0) {
obj->btf = btf__new(data->d_buf, data->d_size, obj->btf = btf__new(data->d_buf, data->d_size);
__pr_debug);
if (IS_ERR(obj->btf)) { if (IS_ERR(obj->btf)) {
pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n", pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
BTF_ELF_SEC, PTR_ERR(obj->btf)); BTF_ELF_SEC, PTR_ERR(obj->btf));
...@@ -915,8 +934,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) ...@@ -915,8 +934,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
BTF_EXT_ELF_SEC, BTF_ELF_SEC); BTF_EXT_ELF_SEC, BTF_ELF_SEC);
} else { } else {
obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
btf_ext_data->d_size, btf_ext_data->d_size);
__pr_debug);
if (IS_ERR(obj->btf_ext)) { if (IS_ERR(obj->btf_ext)) {
pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n", pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
BTF_EXT_ELF_SEC, BTF_EXT_ELF_SEC,
......
...@@ -47,6 +47,12 @@ enum libbpf_errno { ...@@ -47,6 +47,12 @@ enum libbpf_errno {
LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size); LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
enum libbpf_print_level {
LIBBPF_WARN,
LIBBPF_INFO,
LIBBPF_DEBUG,
};
/* /*
* __printf is defined in include/linux/compiler-gcc.h. However, * __printf is defined in include/linux/compiler-gcc.h. However,
* it would be better if libbpf.h didn't depend on Linux header files. * it would be better if libbpf.h didn't depend on Linux header files.
......
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* Copyright (c) 2019 Facebook */
#ifndef __LIBBPF_LIBBPF_UTIL_H
#define __LIBBPF_LIBBPF_UTIL_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
extern void libbpf_print(enum libbpf_print_level level,
const char *format, ...)
__attribute__((format(printf, 2, 3)));
extern bool libbpf_print_level_available(enum libbpf_print_level level);
#define __pr(level, fmt, ...) \
do { \
libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__); \
} while (0)
#define pr_warning(fmt, ...) __pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
#define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
#define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
...@@ -14,5 +14,5 @@ int main(int argc, char *argv[]) ...@@ -14,5 +14,5 @@ int main(int argc, char *argv[])
bpf_prog_get_fd_by_id(0); bpf_prog_get_fd_by_id(0);
/* btf.h */ /* btf.h */
btf__new(NULL, 0, NULL); btf__new(NULL, 0);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment