diff mbox series

[bpf-next,v3,1/4] libbpf: introduce errstr() for stringifying errno

Message ID 20241111212919.368971-2-mykyta.yatsenko5@gmail.com (mailing list archive)
State Accepted
Commit 1633a83bf993fa1eb8df26ec6043f77a2fe03245
Delegated to: BPF
Headers show
Series libbpf: stringify error codes in log messages | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/build_tools success Errors and warnings before: 2 (+0) this patch: 2 (+0)
netdev/cc_maintainers warning 9 maintainers not CCed: kpsingh@kernel.org jolsa@kernel.org eddyz87@gmail.com song@kernel.org haoluo@google.com john.fastabend@gmail.com yonghong.song@linux.dev martin.lau@linux.dev sdf@fomichev.me
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 3 this patch: 3
netdev/checkpatch warning WARNING: ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc fail Errors and warnings before: 0 this patch: 4
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-39 fail Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-7 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-15 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release

Commit Message

Mykyta Yatsenko Nov. 11, 2024, 9:29 p.m. UTC
From: Mykyta Yatsenko <yatsenko@meta.com>

Add function errstr(int err) that allows converting numeric error codes
into string representations.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 tools/lib/bpf/str_error.c | 59 +++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/str_error.h |  7 +++++
 2 files changed, 66 insertions(+)

Comments

Andrii Nakryiko Nov. 12, 2024, 4:35 a.m. UTC | #1
On Mon, Nov 11, 2024 at 1:29 PM Mykyta Yatsenko
<mykyta.yatsenko5@gmail.com> wrote:
>
> From: Mykyta Yatsenko <yatsenko@meta.com>
>
> Add function errstr(int err) that allows converting numeric error codes
> into string representations.
>
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---
>  tools/lib/bpf/str_error.c | 59 +++++++++++++++++++++++++++++++++++++++
>  tools/lib/bpf/str_error.h |  7 +++++
>  2 files changed, 66 insertions(+)
>
> diff --git a/tools/lib/bpf/str_error.c b/tools/lib/bpf/str_error.c
> index 5e6a1e27ddf9..cf817c0c7ddd 100644
> --- a/tools/lib/bpf/str_error.c
> +++ b/tools/lib/bpf/str_error.c
> @@ -5,6 +5,10 @@
>  #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
>
> @@ -31,3 +35,58 @@ char *libbpf_strerror_r(int err, char *dst, int len)
>         }
>         return dst;
>  }
> +
> +const char *errstr(int err)
> +{
> +       static __thread char buf[12];
> +
> +       if (err > 0)
> +               err = -err;
> +
> +       switch (err) {
> +       case -EINVAL: return "-EINVAL";
> +       case -EPERM: return "-EPERM";
> +       case -ENXIO: return "-ENXIO";
> +       case -ENOMEM: return "-ENOMEM";
> +       case -ENOENT: return "-ENOENT";
> +       case -E2BIG: return "-E2BIG";
> +       case -EEXIST: return "-EEXIST";
> +       case -EFAULT: return "-EFAULT";
> +       case -ENOSPC: return "-ENOSPC";
> +       case -EACCES: return "-EACCES";
> +       case -EAGAIN: return "-EAGAIN";
> +       case -EBADF: return "-EBADF";
> +       case -ENAMETOOLONG: return "-ENAMETOOLONG";
> +       case -ESRCH: return "-ESRCH";
> +       case -EBUSY: return "-EBUSY";
> +       case -ENOTSUPP: return "-ENOTSUPP";
> +       case -EPROTO: return "-EPROTO";
> +       case -ERANGE: return "-ERANGE";
> +       case -EMSGSIZE: return "-EMSGSIZE";
> +       case -EINTR: return "-EINTR";
> +       case -ENODATA: return "-ENODATA";
> +       case -ENODEV: return "-ENODEV";
> +       case -ENOLINK:return "-ENOLINK";
> +       case -EIO: return "-EIO";
> +       case -EUCLEAN: return "-EUCLEAN";
> +       case -EDOM: return "-EDOM";
> +       case -ELOOP: return "-ELOOP";
> +       case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT";
> +       case -EDEADLK: return "-EDEADLK";
> +       case -EOVERFLOW: return "-EOVERFLOW";
> +       case -EOPNOTSUPP: return "-EOPNOTSUPP";
> +       case -EINPROGRESS: return "-EINPROGRESS";
> +       case -EBADFD: return "-EBADFD";
> +       case -EADDRINUSE: return "-EADDRINUSE";
> +       case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL";
> +       case -ECANCELED: return "-ECANCELED";
> +       case -EILSEQ: return "-EILSEQ";
> +       case -EMFILE: return "-EMFILE";
> +       case -ENOTTY: return "-ENOTTY";
> +       case -EALREADY: return "-EALREADY";
> +       case -ECHILD: return "-ECHILD";

I added a few error code from include/uapi/asm-generic/errno-base.h
that were missing (just for completeness, as we already had absolute
majority of them), reordered alphabetically and reformatted to be a
bit more table-like. Applied to bpf-next.

Thanks a lot for this massive clean up, it's great!

> +       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
> index 626d7ffb03d6..66ffebde0684 100644
> --- a/tools/lib/bpf/str_error.h
> +++ b/tools/lib/bpf/str_error.h
> @@ -6,4 +6,11 @@
>
>  char *libbpf_strerror_r(int err, char *dst, int len);
>
> +/**
> + * @brief **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 *errstr(int err);
>  #endif /* __LIBBPF_STR_ERROR_H */
> --
> 2.47.0
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/str_error.c b/tools/lib/bpf/str_error.c
index 5e6a1e27ddf9..cf817c0c7ddd 100644
--- a/tools/lib/bpf/str_error.c
+++ b/tools/lib/bpf/str_error.c
@@ -5,6 +5,10 @@ 
 #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
 
@@ -31,3 +35,58 @@  char *libbpf_strerror_r(int err, char *dst, int len)
 	}
 	return dst;
 }
+
+const char *errstr(int err)
+{
+	static __thread char buf[12];
+
+	if (err > 0)
+		err = -err;
+
+	switch (err) {
+	case -EINVAL: return "-EINVAL";
+	case -EPERM: return "-EPERM";
+	case -ENXIO: return "-ENXIO";
+	case -ENOMEM: return "-ENOMEM";
+	case -ENOENT: return "-ENOENT";
+	case -E2BIG: return "-E2BIG";
+	case -EEXIST: return "-EEXIST";
+	case -EFAULT: return "-EFAULT";
+	case -ENOSPC: return "-ENOSPC";
+	case -EACCES: return "-EACCES";
+	case -EAGAIN: return "-EAGAIN";
+	case -EBADF: return "-EBADF";
+	case -ENAMETOOLONG: return "-ENAMETOOLONG";
+	case -ESRCH: return "-ESRCH";
+	case -EBUSY: return "-EBUSY";
+	case -ENOTSUPP: return "-ENOTSUPP";
+	case -EPROTO: return "-EPROTO";
+	case -ERANGE: return "-ERANGE";
+	case -EMSGSIZE: return "-EMSGSIZE";
+	case -EINTR: return "-EINTR";
+	case -ENODATA: return "-ENODATA";
+	case -ENODEV: return "-ENODEV";
+	case -ENOLINK:return "-ENOLINK";
+	case -EIO: return "-EIO";
+	case -EUCLEAN: return "-EUCLEAN";
+	case -EDOM: return "-EDOM";
+	case -ELOOP: return "-ELOOP";
+	case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT";
+	case -EDEADLK: return "-EDEADLK";
+	case -EOVERFLOW: return "-EOVERFLOW";
+	case -EOPNOTSUPP: return "-EOPNOTSUPP";
+	case -EINPROGRESS: return "-EINPROGRESS";
+	case -EBADFD: return "-EBADFD";
+	case -EADDRINUSE: return "-EADDRINUSE";
+	case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL";
+	case -ECANCELED: return "-ECANCELED";
+	case -EILSEQ: return "-EILSEQ";
+	case -EMFILE: return "-EMFILE";
+	case -ENOTTY: return "-ENOTTY";
+	case -EALREADY: return "-EALREADY";
+	case -ECHILD: return "-ECHILD";
+	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
index 626d7ffb03d6..66ffebde0684 100644
--- a/tools/lib/bpf/str_error.h
+++ b/tools/lib/bpf/str_error.h
@@ -6,4 +6,11 @@ 
 
 char *libbpf_strerror_r(int err, char *dst, int len);
 
+/**
+ * @brief **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 *errstr(int err);
 #endif /* __LIBBPF_STR_ERROR_H */