libbpf: move libbpf_errstr() into libbpf_utils.c
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 1 Oct 2025 17:13:24 +0000 (10:13 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 1 Oct 2025 22:27:25 +0000 (15:27 -0700)
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>
15 files changed:
tools/lib/bpf/Build
tools/lib/bpf/btf.c
tools/lib/bpf/btf_dump.c
tools/lib/bpf/elf.c
tools/lib/bpf/features.c
tools/lib/bpf/gen_loader.c
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf_internal.h
tools/lib/bpf/libbpf_utils.c
tools/lib/bpf/linker.c
tools/lib/bpf/relo_core.c
tools/lib/bpf/ringbuf.c
tools/lib/bpf/str_error.c [deleted file]
tools/lib/bpf/str_error.h [deleted file]
tools/lib/bpf/usdt.c

index c309271..c80204b 100644 (file)
@@ -1,4 +1,4 @@
-libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o str_error.o \
+libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o \
            netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \
            btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \
            usdt.o zip.o elf.o features.o btf_iter.o btf_relocate.o
index 3768290..18907f0 100644 (file)
@@ -23,7 +23,6 @@
 #include "libbpf_internal.h"
 #include "hashmap.h"
 #include "strset.h"
-#include "str_error.h"
 
 #define BTF_MAX_NR_TYPES 0x7fffffffU
 #define BTF_MAX_STR_OFFSET 0x7fffffffU
index f09f25e..6388392 100644 (file)
@@ -21,7 +21,6 @@
 #include "hashmap.h"
 #include "libbpf.h"
 #include "libbpf_internal.h"
-#include "str_error.h"
 
 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
index 823f83a..295dbda 100644 (file)
@@ -9,7 +9,6 @@
 #include <linux/kernel.h>
 
 #include "libbpf_internal.h"
-#include "str_error.h"
 
 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if
  * the symbol is hidden and can only be seen when referenced using an
index 760657f..b842b83 100644 (file)
@@ -6,7 +6,6 @@
 #include "libbpf.h"
 #include "libbpf_common.h"
 #include "libbpf_internal.h"
-#include "str_error.h"
 
 static inline __u64 ptr_to_u64(const void *ptr)
 {
index 6945dd9..cd5c254 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <asm/byteorder.h>
 #include <linux/filter.h>
 #include <sys/param.h>
 #include "btf.h"
@@ -13,8 +14,6 @@
 #include "hashmap.h"
 #include "bpf_gen_internal.h"
 #include "skel_internal.h"
-#include <asm/byteorder.h>
-#include "str_error.h"
 
 #define MAX_USED_MAPS  64
 #define MAX_USED_PROGS 32
index c21bc61..6d19e0d 100644 (file)
@@ -51,7 +51,6 @@
 #include "libbpf.h"
 #include "bpf.h"
 #include "btf.h"
-#include "str_error.h"
 #include "libbpf_internal.h"
 #include "hashmap.h"
 #include "bpf_gen_internal.h"
index c93797d..a8f2041 100644 (file)
@@ -172,6 +172,16 @@ do {                               \
 #define pr_info(fmt, ...)      __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
 #define pr_debug(fmt, ...)     __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
 
+/**
+ * @brief **libbpf_errstr()** returns string corresponding to numeric errno
+ * @param err negative numeric errno
+ * @return pointer to string representation of the errno, that is invalidated
+ * upon the next call.
+ */
+const char *libbpf_errstr(int err);
+
+#define errstr(err) libbpf_errstr(err)
+
 #ifndef __has_builtin
 #define __has_builtin(x) 0
 #endif
index 6b18017..ee3013e 100644 (file)
 #undef _GNU_SOURCE
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 
 #include "libbpf.h"
 #include "libbpf_internal.h"
 
+#ifndef ENOTSUPP
+#define ENOTSUPP       524
+#endif
+
 /* make sure libbpf doesn't use kernel-only integer typedefs */
 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
 
@@ -73,3 +78,70 @@ int libbpf_strerror(int err, char *buf, size_t size)
                return libbpf_err(-ERANGE);
        return libbpf_err(-ENOENT);
 }
+
+const char *libbpf_errstr(int err)
+{
+       static __thread char buf[12];
+
+       if (err > 0)
+               err = -err;
+
+       switch (err) {
+       case -E2BIG:            return "-E2BIG";
+       case -EACCES:           return "-EACCES";
+       case -EADDRINUSE:       return "-EADDRINUSE";
+       case -EADDRNOTAVAIL:    return "-EADDRNOTAVAIL";
+       case -EAGAIN:           return "-EAGAIN";
+       case -EALREADY:         return "-EALREADY";
+       case -EBADF:            return "-EBADF";
+       case -EBADFD:           return "-EBADFD";
+       case -EBUSY:            return "-EBUSY";
+       case -ECANCELED:        return "-ECANCELED";
+       case -ECHILD:           return "-ECHILD";
+       case -EDEADLK:          return "-EDEADLK";
+       case -EDOM:             return "-EDOM";
+       case -EEXIST:           return "-EEXIST";
+       case -EFAULT:           return "-EFAULT";
+       case -EFBIG:            return "-EFBIG";
+       case -EILSEQ:           return "-EILSEQ";
+       case -EINPROGRESS:      return "-EINPROGRESS";
+       case -EINTR:            return "-EINTR";
+       case -EINVAL:           return "-EINVAL";
+       case -EIO:              return "-EIO";
+       case -EISDIR:           return "-EISDIR";
+       case -ELOOP:            return "-ELOOP";
+       case -EMFILE:           return "-EMFILE";
+       case -EMLINK:           return "-EMLINK";
+       case -EMSGSIZE:         return "-EMSGSIZE";
+       case -ENAMETOOLONG:     return "-ENAMETOOLONG";
+       case -ENFILE:           return "-ENFILE";
+       case -ENODATA:          return "-ENODATA";
+       case -ENODEV:           return "-ENODEV";
+       case -ENOENT:           return "-ENOENT";
+       case -ENOEXEC:          return "-ENOEXEC";
+       case -ENOLINK:          return "-ENOLINK";
+       case -ENOMEM:           return "-ENOMEM";
+       case -ENOSPC:           return "-ENOSPC";
+       case -ENOTBLK:          return "-ENOTBLK";
+       case -ENOTDIR:          return "-ENOTDIR";
+       case -ENOTSUPP:         return "-ENOTSUPP";
+       case -ENOTTY:           return "-ENOTTY";
+       case -ENXIO:            return "-ENXIO";
+       case -EOPNOTSUPP:       return "-EOPNOTSUPP";
+       case -EOVERFLOW:        return "-EOVERFLOW";
+       case -EPERM:            return "-EPERM";
+       case -EPIPE:            return "-EPIPE";
+       case -EPROTO:           return "-EPROTO";
+       case -EPROTONOSUPPORT:  return "-EPROTONOSUPPORT";
+       case -ERANGE:           return "-ERANGE";
+       case -EROFS:            return "-EROFS";
+       case -ESPIPE:           return "-ESPIPE";
+       case -ESRCH:            return "-ESRCH";
+       case -ETXTBSY:          return "-ETXTBSY";
+       case -EUCLEAN:          return "-EUCLEAN";
+       case -EXDEV:            return "-EXDEV";
+       default:
+               snprintf(buf, sizeof(buf), "%d", err);
+               return buf;
+       }
+}
index a469e5d..56ae770 100644 (file)
@@ -25,7 +25,6 @@
 #include "btf.h"
 #include "libbpf_internal.h"
 #include "strset.h"
-#include "str_error.h"
 
 #define BTF_EXTERN_SEC ".extern"
 
index 2b83c98..6eea5ed 100644 (file)
@@ -64,7 +64,6 @@ enum libbpf_print_level {
 #include "libbpf.h"
 #include "bpf.h"
 #include "btf.h"
-#include "str_error.h"
 #include "libbpf_internal.h"
 #endif
 
index 9702b70..00ec483 100644 (file)
@@ -21,7 +21,6 @@
 #include "libbpf.h"
 #include "libbpf_internal.h"
 #include "bpf.h"
-#include "str_error.h"
 
 struct ring {
        ring_buffer_sample_fn sample_cb;
diff --git a/tools/lib/bpf/str_error.c b/tools/lib/bpf/str_error.c
deleted file mode 100644 (file)
index 92dbd80..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
-#undef _GNU_SOURCE
-#include <string.h>
-#include <stdio.h>
-#include <errno.h>
-#include "str_error.h"
-
-#ifndef ENOTSUPP
-#define ENOTSUPP       524
-#endif
-
-/* make sure libbpf doesn't use kernel-only integer typedefs */
-#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
-
-const char *libbpf_errstr(int err)
-{
-       static __thread char buf[12];
-
-       if (err > 0)
-               err = -err;
-
-       switch (err) {
-       case -E2BIG:            return "-E2BIG";
-       case -EACCES:           return "-EACCES";
-       case -EADDRINUSE:       return "-EADDRINUSE";
-       case -EADDRNOTAVAIL:    return "-EADDRNOTAVAIL";
-       case -EAGAIN:           return "-EAGAIN";
-       case -EALREADY:         return "-EALREADY";
-       case -EBADF:            return "-EBADF";
-       case -EBADFD:           return "-EBADFD";
-       case -EBUSY:            return "-EBUSY";
-       case -ECANCELED:        return "-ECANCELED";
-       case -ECHILD:           return "-ECHILD";
-       case -EDEADLK:          return "-EDEADLK";
-       case -EDOM:             return "-EDOM";
-       case -EEXIST:           return "-EEXIST";
-       case -EFAULT:           return "-EFAULT";
-       case -EFBIG:            return "-EFBIG";
-       case -EILSEQ:           return "-EILSEQ";
-       case -EINPROGRESS:      return "-EINPROGRESS";
-       case -EINTR:            return "-EINTR";
-       case -EINVAL:           return "-EINVAL";
-       case -EIO:              return "-EIO";
-       case -EISDIR:           return "-EISDIR";
-       case -ELOOP:            return "-ELOOP";
-       case -EMFILE:           return "-EMFILE";
-       case -EMLINK:           return "-EMLINK";
-       case -EMSGSIZE:         return "-EMSGSIZE";
-       case -ENAMETOOLONG:     return "-ENAMETOOLONG";
-       case -ENFILE:           return "-ENFILE";
-       case -ENODATA:          return "-ENODATA";
-       case -ENODEV:           return "-ENODEV";
-       case -ENOENT:           return "-ENOENT";
-       case -ENOEXEC:          return "-ENOEXEC";
-       case -ENOLINK:          return "-ENOLINK";
-       case -ENOMEM:           return "-ENOMEM";
-       case -ENOSPC:           return "-ENOSPC";
-       case -ENOTBLK:          return "-ENOTBLK";
-       case -ENOTDIR:          return "-ENOTDIR";
-       case -ENOTSUPP:         return "-ENOTSUPP";
-       case -ENOTTY:           return "-ENOTTY";
-       case -ENXIO:            return "-ENXIO";
-       case -EOPNOTSUPP:       return "-EOPNOTSUPP";
-       case -EOVERFLOW:        return "-EOVERFLOW";
-       case -EPERM:            return "-EPERM";
-       case -EPIPE:            return "-EPIPE";
-       case -EPROTO:           return "-EPROTO";
-       case -EPROTONOSUPPORT:  return "-EPROTONOSUPPORT";
-       case -ERANGE:           return "-ERANGE";
-       case -EROFS:            return "-EROFS";
-       case -ESPIPE:           return "-ESPIPE";
-       case -ESRCH:            return "-ESRCH";
-       case -ETXTBSY:          return "-ETXTBSY";
-       case -EUCLEAN:          return "-EUCLEAN";
-       case -EXDEV:            return "-EXDEV";
-       default:
-               snprintf(buf, sizeof(buf), "%d", err);
-               return buf;
-       }
-}
diff --git a/tools/lib/bpf/str_error.h b/tools/lib/bpf/str_error.h
deleted file mode 100644 (file)
index d4c82ee..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
-#ifndef __LIBBPF_STR_ERROR_H
-#define __LIBBPF_STR_ERROR_H
-
-/**
- * @brief **libbpf_errstr()** returns string corresponding to numeric errno
- * @param err negative numeric errno
- * @return pointer to string representation of the errno, that is invalidated
- * upon the next call.
- */
-const char *libbpf_errstr(int err);
-
-#define errstr(err) libbpf_errstr(err)
-
-#endif /* __LIBBPF_STR_ERROR_H */
index fc2785e..c174b40 100644 (file)
@@ -20,7 +20,6 @@
 #include "libbpf_common.h"
 #include "libbpf_internal.h"
 #include "hashmap.h"
-#include "str_error.h"
 
 /* libbpf's USDT support consists of BPF-side state/code and user-space
  * state/code working together in concert. BPF-side parts are defined in