Skip to content

Commit d3d2a63

Browse files
anakryikoKernel Patches Daemon
authored andcommitted
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 <[email protected]>
1 parent ac1bcae commit d3d2a63

File tree

15 files changed

+84
-107
lines changed

15 files changed

+84
-107
lines changed

tools/lib/bpf/Build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 \
22
netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \
33
btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \
44
usdt.o zip.o elf.o features.o btf_iter.o btf_relocate.o

tools/lib/bpf/btf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "libbpf_internal.h"
2424
#include "hashmap.h"
2525
#include "strset.h"
26-
#include "str_error.h"
2726

2827
#define BTF_MAX_NR_TYPES 0x7fffffffU
2928
#define BTF_MAX_STR_OFFSET 0x7fffffffU

tools/lib/bpf/btf_dump.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "hashmap.h"
2222
#include "libbpf.h"
2323
#include "libbpf_internal.h"
24-
#include "str_error.h"
2524

2625
static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
2726
static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;

tools/lib/bpf/elf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <linux/kernel.h>
1010

1111
#include "libbpf_internal.h"
12-
#include "str_error.h"
1312

1413
/* A SHT_GNU_versym section holds 16-bit words. This bit is set if
1514
* the symbol is hidden and can only be seen when referenced using an

tools/lib/bpf/features.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "libbpf.h"
77
#include "libbpf_common.h"
88
#include "libbpf_internal.h"
9-
#include "str_error.h"
109

1110
static inline __u64 ptr_to_u64(const void *ptr)
1211
{

tools/lib/bpf/gen_loader.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <string.h>
66
#include <errno.h>
7+
#include <asm/byteorder.h>
78
#include <linux/filter.h>
89
#include <sys/param.h>
910
#include "btf.h"
@@ -13,8 +14,6 @@
1314
#include "hashmap.h"
1415
#include "bpf_gen_internal.h"
1516
#include "skel_internal.h"
16-
#include <asm/byteorder.h>
17-
#include "str_error.h"
1817

1918
#define MAX_USED_MAPS 64
2019
#define MAX_USED_PROGS 32

tools/lib/bpf/libbpf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include "libbpf.h"
5252
#include "bpf.h"
5353
#include "btf.h"
54-
#include "str_error.h"
5554
#include "libbpf_internal.h"
5655
#include "hashmap.h"
5756
#include "bpf_gen_internal.h"

tools/lib/bpf/libbpf_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ do { \
172172
#define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
173173
#define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
174174

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+
175185
#ifndef __has_builtin
176186
#define __has_builtin(x) 0
177187
#endif

tools/lib/bpf/libbpf_utils.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
#undef _GNU_SOURCE
1111
#include <stdio.h>
1212
#include <string.h>
13+
#include <errno.h>
1314

1415
#include "libbpf.h"
1516
#include "libbpf_internal.h"
1617

18+
#ifndef ENOTSUPP
19+
#define ENOTSUPP 524
20+
#endif
21+
1722
/* make sure libbpf doesn't use kernel-only integer typedefs */
1823
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
1924

@@ -73,3 +78,70 @@ int libbpf_strerror(int err, char *buf, size_t size)
7378
return libbpf_err(-ERANGE);
7479
return libbpf_err(-ENOENT);
7580
}
81+
82+
const char *libbpf_errstr(int err)
83+
{
84+
static __thread char buf[12];
85+
86+
if (err > 0)
87+
err = -err;
88+
89+
switch (err) {
90+
case -E2BIG: return "-E2BIG";
91+
case -EACCES: return "-EACCES";
92+
case -EADDRINUSE: return "-EADDRINUSE";
93+
case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL";
94+
case -EAGAIN: return "-EAGAIN";
95+
case -EALREADY: return "-EALREADY";
96+
case -EBADF: return "-EBADF";
97+
case -EBADFD: return "-EBADFD";
98+
case -EBUSY: return "-EBUSY";
99+
case -ECANCELED: return "-ECANCELED";
100+
case -ECHILD: return "-ECHILD";
101+
case -EDEADLK: return "-EDEADLK";
102+
case -EDOM: return "-EDOM";
103+
case -EEXIST: return "-EEXIST";
104+
case -EFAULT: return "-EFAULT";
105+
case -EFBIG: return "-EFBIG";
106+
case -EILSEQ: return "-EILSEQ";
107+
case -EINPROGRESS: return "-EINPROGRESS";
108+
case -EINTR: return "-EINTR";
109+
case -EINVAL: return "-EINVAL";
110+
case -EIO: return "-EIO";
111+
case -EISDIR: return "-EISDIR";
112+
case -ELOOP: return "-ELOOP";
113+
case -EMFILE: return "-EMFILE";
114+
case -EMLINK: return "-EMLINK";
115+
case -EMSGSIZE: return "-EMSGSIZE";
116+
case -ENAMETOOLONG: return "-ENAMETOOLONG";
117+
case -ENFILE: return "-ENFILE";
118+
case -ENODATA: return "-ENODATA";
119+
case -ENODEV: return "-ENODEV";
120+
case -ENOENT: return "-ENOENT";
121+
case -ENOEXEC: return "-ENOEXEC";
122+
case -ENOLINK: return "-ENOLINK";
123+
case -ENOMEM: return "-ENOMEM";
124+
case -ENOSPC: return "-ENOSPC";
125+
case -ENOTBLK: return "-ENOTBLK";
126+
case -ENOTDIR: return "-ENOTDIR";
127+
case -ENOTSUPP: return "-ENOTSUPP";
128+
case -ENOTTY: return "-ENOTTY";
129+
case -ENXIO: return "-ENXIO";
130+
case -EOPNOTSUPP: return "-EOPNOTSUPP";
131+
case -EOVERFLOW: return "-EOVERFLOW";
132+
case -EPERM: return "-EPERM";
133+
case -EPIPE: return "-EPIPE";
134+
case -EPROTO: return "-EPROTO";
135+
case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT";
136+
case -ERANGE: return "-ERANGE";
137+
case -EROFS: return "-EROFS";
138+
case -ESPIPE: return "-ESPIPE";
139+
case -ESRCH: return "-ESRCH";
140+
case -ETXTBSY: return "-ETXTBSY";
141+
case -EUCLEAN: return "-EUCLEAN";
142+
case -EXDEV: return "-EXDEV";
143+
default:
144+
snprintf(buf, sizeof(buf), "%d", err);
145+
return buf;
146+
}
147+
}

tools/lib/bpf/linker.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "btf.h"
2626
#include "libbpf_internal.h"
2727
#include "strset.h"
28-
#include "str_error.h"
2928

3029
#define BTF_EXTERN_SEC ".extern"
3130

0 commit comments

Comments
 (0)