Skip to content

Commit 1633a83

Browse files
mykyta5anakryiko
authored andcommitted
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 213a695 commit 1633a83

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

tools/lib/bpf/str_error.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <errno.h>
66
#include "str_error.h"
77

8+
#ifndef ENOTSUPP
9+
#define ENOTSUPP 524
10+
#endif
11+
812
/* make sure libbpf doesn't use kernel-only integer typedefs */
913
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
1014

@@ -31,3 +35,70 @@ char *libbpf_strerror_r(int err, char *dst, int len)
3135
}
3236
return dst;
3337
}
38+
39+
const char *errstr(int err)
40+
{
41+
static __thread char buf[12];
42+
43+
if (err > 0)
44+
err = -err;
45+
46+
switch (err) {
47+
case -E2BIG: return "-E2BIG";
48+
case -EACCES: return "-EACCES";
49+
case -EADDRINUSE: return "-EADDRINUSE";
50+
case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL";
51+
case -EAGAIN: return "-EAGAIN";
52+
case -EALREADY: return "-EALREADY";
53+
case -EBADF: return "-EBADF";
54+
case -EBADFD: return "-EBADFD";
55+
case -EBUSY: return "-EBUSY";
56+
case -ECANCELED: return "-ECANCELED";
57+
case -ECHILD: return "-ECHILD";
58+
case -EDEADLK: return "-EDEADLK";
59+
case -EDOM: return "-EDOM";
60+
case -EEXIST: return "-EEXIST";
61+
case -EFAULT: return "-EFAULT";
62+
case -EFBIG: return "-EFBIG";
63+
case -EILSEQ: return "-EILSEQ";
64+
case -EINPROGRESS: return "-EINPROGRESS";
65+
case -EINTR: return "-EINTR";
66+
case -EINVAL: return "-EINVAL";
67+
case -EIO: return "-EIO";
68+
case -EISDIR: return "-EISDIR";
69+
case -ELOOP: return "-ELOOP";
70+
case -EMFILE: return "-EMFILE";
71+
case -EMLINK: return "-EMLINK";
72+
case -EMSGSIZE: return "-EMSGSIZE";
73+
case -ENAMETOOLONG: return "-ENAMETOOLONG";
74+
case -ENFILE: return "-ENFILE";
75+
case -ENODATA: return "-ENODATA";
76+
case -ENODEV: return "-ENODEV";
77+
case -ENOENT: return "-ENOENT";
78+
case -ENOEXEC: return "-ENOEXEC";
79+
case -ENOLINK: return "-ENOLINK";
80+
case -ENOMEM: return "-ENOMEM";
81+
case -ENOSPC: return "-ENOSPC";
82+
case -ENOTBLK: return "-ENOTBLK";
83+
case -ENOTDIR: return "-ENOTDIR";
84+
case -ENOTSUPP: return "-ENOTSUPP";
85+
case -ENOTTY: return "-ENOTTY";
86+
case -ENXIO: return "-ENXIO";
87+
case -EOPNOTSUPP: return "-EOPNOTSUPP";
88+
case -EOVERFLOW: return "-EOVERFLOW";
89+
case -EPERM: return "-EPERM";
90+
case -EPIPE: return "-EPIPE";
91+
case -EPROTO: return "-EPROTO";
92+
case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT";
93+
case -ERANGE: return "-ERANGE";
94+
case -EROFS: return "-EROFS";
95+
case -ESPIPE: return "-ESPIPE";
96+
case -ESRCH: return "-ESRCH";
97+
case -ETXTBSY: return "-ETXTBSY";
98+
case -EUCLEAN: return "-EUCLEAN";
99+
case -EXDEV: return "-EXDEV";
100+
default:
101+
snprintf(buf, sizeof(buf), "%d", err);
102+
return buf;
103+
}
104+
}

tools/lib/bpf/str_error.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@
66

77
char *libbpf_strerror_r(int err, char *dst, int len);
88

9+
/**
10+
* @brief **errstr()** returns string corresponding to numeric errno
11+
* @param err negative numeric errno
12+
* @return pointer to string representation of the errno, that is invalidated
13+
* upon the next call.
14+
*/
15+
const char *errstr(int err);
916
#endif /* __LIBBPF_STR_ERROR_H */

0 commit comments

Comments
 (0)