mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00
libbpf: Introduce errstr() for stringifying errno
Add function errstr(int err) that allows converting numeric error codes into string representations. Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20241111212919.368971-2-mykyta.yatsenko5@gmail.com
This commit is contained in:
parent
213a695297
commit
1633a83bf9
2 changed files with 78 additions and 0 deletions
|
@ -5,6 +5,10 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "str_error.h"
|
#include "str_error.h"
|
||||||
|
|
||||||
|
#ifndef ENOTSUPP
|
||||||
|
#define ENOTSUPP 524
|
||||||
|
#endif
|
||||||
|
|
||||||
/* make sure libbpf doesn't use kernel-only integer typedefs */
|
/* make sure libbpf doesn't use kernel-only integer typedefs */
|
||||||
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
|
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
|
||||||
|
|
||||||
|
@ -31,3 +35,70 @@ char *libbpf_strerror_r(int err, char *dst, int len)
|
||||||
}
|
}
|
||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -6,4 +6,11 @@
|
||||||
|
|
||||||
char *libbpf_strerror_r(int err, char *dst, int len);
|
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 */
|
#endif /* __LIBBPF_STR_ERROR_H */
|
||||||
|
|
Loading…
Add table
Reference in a new issue