Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: move libbpf_errstr() into libbpf_utils.c

Get rid of str_err.{c,h} by moving implementation of libbpf_errstr()
into libbpf_utils.c and declarations into libbpf_internal.h.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20251001171326.3883055-4-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
c68b6fdc d05ab618

+84 -107
+1 -1
tools/lib/bpf/Build
··· 1 - libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o str_error.o \ 1 + libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o \ 2 2 netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \ 3 3 btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \ 4 4 usdt.o zip.o elf.o features.o btf_iter.o btf_relocate.o
-1
tools/lib/bpf/btf.c
··· 23 23 #include "libbpf_internal.h" 24 24 #include "hashmap.h" 25 25 #include "strset.h" 26 - #include "str_error.h" 27 26 28 27 #define BTF_MAX_NR_TYPES 0x7fffffffU 29 28 #define BTF_MAX_STR_OFFSET 0x7fffffffU
-1
tools/lib/bpf/btf_dump.c
··· 21 21 #include "hashmap.h" 22 22 #include "libbpf.h" 23 23 #include "libbpf_internal.h" 24 - #include "str_error.h" 25 24 26 25 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t"; 27 26 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
-1
tools/lib/bpf/elf.c
··· 9 9 #include <linux/kernel.h> 10 10 11 11 #include "libbpf_internal.h" 12 - #include "str_error.h" 13 12 14 13 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if 15 14 * the symbol is hidden and can only be seen when referenced using an
-1
tools/lib/bpf/features.c
··· 6 6 #include "libbpf.h" 7 7 #include "libbpf_common.h" 8 8 #include "libbpf_internal.h" 9 - #include "str_error.h" 10 9 11 10 static inline __u64 ptr_to_u64(const void *ptr) 12 11 {
+1 -2
tools/lib/bpf/gen_loader.c
··· 4 4 #include <stdlib.h> 5 5 #include <string.h> 6 6 #include <errno.h> 7 + #include <asm/byteorder.h> 7 8 #include <linux/filter.h> 8 9 #include <sys/param.h> 9 10 #include "btf.h" ··· 14 13 #include "hashmap.h" 15 14 #include "bpf_gen_internal.h" 16 15 #include "skel_internal.h" 17 - #include <asm/byteorder.h> 18 - #include "str_error.h" 19 16 20 17 #define MAX_USED_MAPS 64 21 18 #define MAX_USED_PROGS 32
-1
tools/lib/bpf/libbpf.c
··· 51 51 #include "libbpf.h" 52 52 #include "bpf.h" 53 53 #include "btf.h" 54 - #include "str_error.h" 55 54 #include "libbpf_internal.h" 56 55 #include "hashmap.h" 57 56 #include "bpf_gen_internal.h"
+10
tools/lib/bpf/libbpf_internal.h
··· 172 172 #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__) 173 173 #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__) 174 174 175 + /** 176 + * @brief **libbpf_errstr()** returns string corresponding to numeric errno 177 + * @param err negative numeric errno 178 + * @return pointer to string representation of the errno, that is invalidated 179 + * upon the next call. 180 + */ 181 + const char *libbpf_errstr(int err); 182 + 183 + #define errstr(err) libbpf_errstr(err) 184 + 175 185 #ifndef __has_builtin 176 186 #define __has_builtin(x) 0 177 187 #endif
+72
tools/lib/bpf/libbpf_utils.c
··· 10 10 #undef _GNU_SOURCE 11 11 #include <stdio.h> 12 12 #include <string.h> 13 + #include <errno.h> 13 14 14 15 #include "libbpf.h" 15 16 #include "libbpf_internal.h" 17 + 18 + #ifndef ENOTSUPP 19 + #define ENOTSUPP 524 20 + #endif 16 21 17 22 /* make sure libbpf doesn't use kernel-only integer typedefs */ 18 23 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 ··· 77 72 if (ret >= size) 78 73 return libbpf_err(-ERANGE); 79 74 return libbpf_err(-ENOENT); 75 + } 76 + 77 + const char *libbpf_errstr(int err) 78 + { 79 + static __thread char buf[12]; 80 + 81 + if (err > 0) 82 + err = -err; 83 + 84 + switch (err) { 85 + case -E2BIG: return "-E2BIG"; 86 + case -EACCES: return "-EACCES"; 87 + case -EADDRINUSE: return "-EADDRINUSE"; 88 + case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL"; 89 + case -EAGAIN: return "-EAGAIN"; 90 + case -EALREADY: return "-EALREADY"; 91 + case -EBADF: return "-EBADF"; 92 + case -EBADFD: return "-EBADFD"; 93 + case -EBUSY: return "-EBUSY"; 94 + case -ECANCELED: return "-ECANCELED"; 95 + case -ECHILD: return "-ECHILD"; 96 + case -EDEADLK: return "-EDEADLK"; 97 + case -EDOM: return "-EDOM"; 98 + case -EEXIST: return "-EEXIST"; 99 + case -EFAULT: return "-EFAULT"; 100 + case -EFBIG: return "-EFBIG"; 101 + case -EILSEQ: return "-EILSEQ"; 102 + case -EINPROGRESS: return "-EINPROGRESS"; 103 + case -EINTR: return "-EINTR"; 104 + case -EINVAL: return "-EINVAL"; 105 + case -EIO: return "-EIO"; 106 + case -EISDIR: return "-EISDIR"; 107 + case -ELOOP: return "-ELOOP"; 108 + case -EMFILE: return "-EMFILE"; 109 + case -EMLINK: return "-EMLINK"; 110 + case -EMSGSIZE: return "-EMSGSIZE"; 111 + case -ENAMETOOLONG: return "-ENAMETOOLONG"; 112 + case -ENFILE: return "-ENFILE"; 113 + case -ENODATA: return "-ENODATA"; 114 + case -ENODEV: return "-ENODEV"; 115 + case -ENOENT: return "-ENOENT"; 116 + case -ENOEXEC: return "-ENOEXEC"; 117 + case -ENOLINK: return "-ENOLINK"; 118 + case -ENOMEM: return "-ENOMEM"; 119 + case -ENOSPC: return "-ENOSPC"; 120 + case -ENOTBLK: return "-ENOTBLK"; 121 + case -ENOTDIR: return "-ENOTDIR"; 122 + case -ENOTSUPP: return "-ENOTSUPP"; 123 + case -ENOTTY: return "-ENOTTY"; 124 + case -ENXIO: return "-ENXIO"; 125 + case -EOPNOTSUPP: return "-EOPNOTSUPP"; 126 + case -EOVERFLOW: return "-EOVERFLOW"; 127 + case -EPERM: return "-EPERM"; 128 + case -EPIPE: return "-EPIPE"; 129 + case -EPROTO: return "-EPROTO"; 130 + case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT"; 131 + case -ERANGE: return "-ERANGE"; 132 + case -EROFS: return "-EROFS"; 133 + case -ESPIPE: return "-ESPIPE"; 134 + case -ESRCH: return "-ESRCH"; 135 + case -ETXTBSY: return "-ETXTBSY"; 136 + case -EUCLEAN: return "-EUCLEAN"; 137 + case -EXDEV: return "-EXDEV"; 138 + default: 139 + snprintf(buf, sizeof(buf), "%d", err); 140 + return buf; 141 + } 80 142 }
-1
tools/lib/bpf/linker.c
··· 25 25 #include "btf.h" 26 26 #include "libbpf_internal.h" 27 27 #include "strset.h" 28 - #include "str_error.h" 29 28 30 29 #define BTF_EXTERN_SEC ".extern" 31 30
-1
tools/lib/bpf/relo_core.c
··· 64 64 #include "libbpf.h" 65 65 #include "bpf.h" 66 66 #include "btf.h" 67 - #include "str_error.h" 68 67 #include "libbpf_internal.h" 69 68 #endif 70 69
-1
tools/lib/bpf/ringbuf.c
··· 21 21 #include "libbpf.h" 22 22 #include "libbpf_internal.h" 23 23 #include "bpf.h" 24 - #include "str_error.h" 25 24 26 25 struct ring { 27 26 ring_buffer_sample_fn sample_cb;
-80
tools/lib/bpf/str_error.c
··· 1 - // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 - #undef _GNU_SOURCE 3 - #include <string.h> 4 - #include <stdio.h> 5 - #include <errno.h> 6 - #include "str_error.h" 7 - 8 - #ifndef ENOTSUPP 9 - #define ENOTSUPP 524 10 - #endif 11 - 12 - /* make sure libbpf doesn't use kernel-only integer typedefs */ 13 - #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 14 - 15 - const char *libbpf_errstr(int err) 16 - { 17 - static __thread char buf[12]; 18 - 19 - if (err > 0) 20 - err = -err; 21 - 22 - switch (err) { 23 - case -E2BIG: return "-E2BIG"; 24 - case -EACCES: return "-EACCES"; 25 - case -EADDRINUSE: return "-EADDRINUSE"; 26 - case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL"; 27 - case -EAGAIN: return "-EAGAIN"; 28 - case -EALREADY: return "-EALREADY"; 29 - case -EBADF: return "-EBADF"; 30 - case -EBADFD: return "-EBADFD"; 31 - case -EBUSY: return "-EBUSY"; 32 - case -ECANCELED: return "-ECANCELED"; 33 - case -ECHILD: return "-ECHILD"; 34 - case -EDEADLK: return "-EDEADLK"; 35 - case -EDOM: return "-EDOM"; 36 - case -EEXIST: return "-EEXIST"; 37 - case -EFAULT: return "-EFAULT"; 38 - case -EFBIG: return "-EFBIG"; 39 - case -EILSEQ: return "-EILSEQ"; 40 - case -EINPROGRESS: return "-EINPROGRESS"; 41 - case -EINTR: return "-EINTR"; 42 - case -EINVAL: return "-EINVAL"; 43 - case -EIO: return "-EIO"; 44 - case -EISDIR: return "-EISDIR"; 45 - case -ELOOP: return "-ELOOP"; 46 - case -EMFILE: return "-EMFILE"; 47 - case -EMLINK: return "-EMLINK"; 48 - case -EMSGSIZE: return "-EMSGSIZE"; 49 - case -ENAMETOOLONG: return "-ENAMETOOLONG"; 50 - case -ENFILE: return "-ENFILE"; 51 - case -ENODATA: return "-ENODATA"; 52 - case -ENODEV: return "-ENODEV"; 53 - case -ENOENT: return "-ENOENT"; 54 - case -ENOEXEC: return "-ENOEXEC"; 55 - case -ENOLINK: return "-ENOLINK"; 56 - case -ENOMEM: return "-ENOMEM"; 57 - case -ENOSPC: return "-ENOSPC"; 58 - case -ENOTBLK: return "-ENOTBLK"; 59 - case -ENOTDIR: return "-ENOTDIR"; 60 - case -ENOTSUPP: return "-ENOTSUPP"; 61 - case -ENOTTY: return "-ENOTTY"; 62 - case -ENXIO: return "-ENXIO"; 63 - case -EOPNOTSUPP: return "-EOPNOTSUPP"; 64 - case -EOVERFLOW: return "-EOVERFLOW"; 65 - case -EPERM: return "-EPERM"; 66 - case -EPIPE: return "-EPIPE"; 67 - case -EPROTO: return "-EPROTO"; 68 - case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT"; 69 - case -ERANGE: return "-ERANGE"; 70 - case -EROFS: return "-EROFS"; 71 - case -ESPIPE: return "-ESPIPE"; 72 - case -ESRCH: return "-ESRCH"; 73 - case -ETXTBSY: return "-ETXTBSY"; 74 - case -EUCLEAN: return "-EUCLEAN"; 75 - case -EXDEV: return "-EXDEV"; 76 - default: 77 - snprintf(buf, sizeof(buf), "%d", err); 78 - return buf; 79 - } 80 - }
-15
tools/lib/bpf/str_error.h
··· 1 - /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 - #ifndef __LIBBPF_STR_ERROR_H 3 - #define __LIBBPF_STR_ERROR_H 4 - 5 - /** 6 - * @brief **libbpf_errstr()** returns string corresponding to numeric errno 7 - * @param err negative numeric errno 8 - * @return pointer to string representation of the errno, that is invalidated 9 - * upon the next call. 10 - */ 11 - const char *libbpf_errstr(int err); 12 - 13 - #define errstr(err) libbpf_errstr(err) 14 - 15 - #endif /* __LIBBPF_STR_ERROR_H */
-1
tools/lib/bpf/usdt.c
··· 20 20 #include "libbpf_common.h" 21 21 #include "libbpf_internal.h" 22 22 #include "hashmap.h" 23 - #include "str_error.h" 24 23 25 24 /* libbpf's USDT support consists of BPF-side state/code and user-space 26 25 * state/code working together in concert. BPF-side parts are defined in