Skip to content

Commit 47e2c45

Browse files
committed
Merge branch 'libbpf-stringify-error-codes-in-log-messages'
Mykyta Yatsenko says: ==================== libbpf: stringify error codes in log messages From: Mykyta Yatsenko <[email protected]> Libbpf may report error in 2 ways: 1. Numeric errno 2. Errno's text representation, returned by strerror Both ways may be confusing for users: numeric code requires people to know how to find its meaning and strerror may be too generic and unclear. These patches modify libbpf error reporting by swapping numeric codes and strerror with the standard short error name, for example: "failed to attach: -22" becomes "failed to attach: -EINVAL". ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 213a695 + 4ce16dd commit 47e2c45

File tree

11 files changed

+306
-266
lines changed

11 files changed

+306
-266
lines changed

tools/lib/bpf/btf.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "libbpf_internal.h"
2323
#include "hashmap.h"
2424
#include "strset.h"
25+
#include "str_error.h"
2526

2627
#define BTF_MAX_NR_TYPES 0x7fffffffU
2728
#define BTF_MAX_STR_OFFSET 0x7fffffffU
@@ -1179,7 +1180,7 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
11791180
fd = open(path, O_RDONLY | O_CLOEXEC);
11801181
if (fd < 0) {
11811182
err = -errno;
1182-
pr_warn("failed to open %s: %s\n", path, strerror(errno));
1183+
pr_warn("failed to open %s: %s\n", path, errstr(err));
11831184
return ERR_PTR(err);
11841185
}
11851186

@@ -1445,7 +1446,7 @@ int btf_load_into_kernel(struct btf *btf,
14451446
goto retry_load;
14461447

14471448
err = -errno;
1448-
pr_warn("BTF loading error: %d\n", err);
1449+
pr_warn("BTF loading error: %s\n", errstr(err));
14491450
/* don't print out contents of custom log_buf */
14501451
if (!log_buf && buf[0])
14511452
pr_warn("-- BEGIN BTF LOAD LOG ---\n%s\n-- END BTF LOAD LOG --\n", buf);
@@ -3464,42 +3465,42 @@ int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts)
34643465

34653466
err = btf_dedup_prep(d);
34663467
if (err) {
3467-
pr_debug("btf_dedup_prep failed:%d\n", err);
3468+
pr_debug("btf_dedup_prep failed: %s\n", errstr(err));
34683469
goto done;
34693470
}
34703471
err = btf_dedup_strings(d);
34713472
if (err < 0) {
3472-
pr_debug("btf_dedup_strings failed:%d\n", err);
3473+
pr_debug("btf_dedup_strings failed: %s\n", errstr(err));
34733474
goto done;
34743475
}
34753476
err = btf_dedup_prim_types(d);
34763477
if (err < 0) {
3477-
pr_debug("btf_dedup_prim_types failed:%d\n", err);
3478+
pr_debug("btf_dedup_prim_types failed: %s\n", errstr(err));
34783479
goto done;
34793480
}
34803481
err = btf_dedup_struct_types(d);
34813482
if (err < 0) {
3482-
pr_debug("btf_dedup_struct_types failed:%d\n", err);
3483+
pr_debug("btf_dedup_struct_types failed: %s\n", errstr(err));
34833484
goto done;
34843485
}
34853486
err = btf_dedup_resolve_fwds(d);
34863487
if (err < 0) {
3487-
pr_debug("btf_dedup_resolve_fwds failed:%d\n", err);
3488+
pr_debug("btf_dedup_resolve_fwds failed: %s\n", errstr(err));
34883489
goto done;
34893490
}
34903491
err = btf_dedup_ref_types(d);
34913492
if (err < 0) {
3492-
pr_debug("btf_dedup_ref_types failed:%d\n", err);
3493+
pr_debug("btf_dedup_ref_types failed: %s\n", errstr(err));
34933494
goto done;
34943495
}
34953496
err = btf_dedup_compact_types(d);
34963497
if (err < 0) {
3497-
pr_debug("btf_dedup_compact_types failed:%d\n", err);
3498+
pr_debug("btf_dedup_compact_types failed: %s\n", errstr(err));
34983499
goto done;
34993500
}
35003501
err = btf_dedup_remap_types(d);
35013502
if (err < 0) {
3502-
pr_debug("btf_dedup_remap_types failed:%d\n", err);
3503+
pr_debug("btf_dedup_remap_types failed: %s\n", errstr(err));
35033504
goto done;
35043505
}
35053506

@@ -5218,7 +5219,8 @@ struct btf *btf__load_vmlinux_btf(void)
52185219
btf = btf__parse(sysfs_btf_path, NULL);
52195220
if (!btf) {
52205221
err = -errno;
5221-
pr_warn("failed to read kernel BTF from '%s': %d\n", sysfs_btf_path, err);
5222+
pr_warn("failed to read kernel BTF from '%s': %s\n",
5223+
sysfs_btf_path, errstr(err));
52225224
return libbpf_err_ptr(err);
52235225
}
52245226
pr_debug("loaded kernel BTF from '%s'\n", sysfs_btf_path);
@@ -5235,7 +5237,7 @@ struct btf *btf__load_vmlinux_btf(void)
52355237

52365238
btf = btf__parse(path, NULL);
52375239
err = libbpf_get_error(btf);
5238-
pr_debug("loading kernel BTF '%s': %d\n", path, err);
5240+
pr_debug("loading kernel BTF '%s': %s\n", path, errstr(err));
52395241
if (err)
52405242
continue;
52415243

tools/lib/bpf/btf_dump.c

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

2526
static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
2627
static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
@@ -1304,7 +1305,7 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
13041305
* chain, restore stack, emit warning, and try to
13051306
* proceed nevertheless
13061307
*/
1307-
pr_warn("not enough memory for decl stack: %d\n", err);
1308+
pr_warn("not enough memory for decl stack: %s\n", errstr(err));
13081309
d->decl_stack_cnt = stack_start;
13091310
return;
13101311
}

tools/lib/bpf/elf.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
int elf_open(const char *binary_path, struct elf_fd *elf_fd)
2626
{
27-
char errmsg[STRERR_BUFSIZE];
2827
int fd, ret;
2928
Elf *elf;
3029

@@ -38,8 +37,7 @@ int elf_open(const char *binary_path, struct elf_fd *elf_fd)
3837
fd = open(binary_path, O_RDONLY | O_CLOEXEC);
3938
if (fd < 0) {
4039
ret = -errno;
41-
pr_warn("elf: failed to open %s: %s\n", binary_path,
42-
libbpf_strerror_r(ret, errmsg, sizeof(errmsg)));
40+
pr_warn("elf: failed to open %s: %s\n", binary_path, errstr(ret));
4341
return ret;
4442
}
4543
elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);

tools/lib/bpf/features.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ static int probe_kern_prog_name(int token_fd)
4747

4848
static int probe_kern_global_data(int token_fd)
4949
{
50-
char *cp, errmsg[STRERR_BUFSIZE];
5150
struct bpf_insn insns[] = {
5251
BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
5352
BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
@@ -67,9 +66,8 @@ static int probe_kern_global_data(int token_fd)
6766
map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, &map_opts);
6867
if (map < 0) {
6968
ret = -errno;
70-
cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
71-
pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
72-
__func__, cp, -ret);
69+
pr_warn("Error in %s(): %s. Couldn't create simple array map.\n",
70+
__func__, errstr(ret));
7371
return ret;
7472
}
7573

@@ -267,7 +265,6 @@ static int probe_kern_probe_read_kernel(int token_fd)
267265

268266
static int probe_prog_bind_map(int token_fd)
269267
{
270-
char *cp, errmsg[STRERR_BUFSIZE];
271268
struct bpf_insn insns[] = {
272269
BPF_MOV64_IMM(BPF_REG_0, 0),
273270
BPF_EXIT_INSN(),
@@ -285,9 +282,8 @@ static int probe_prog_bind_map(int token_fd)
285282
map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, &map_opts);
286283
if (map < 0) {
287284
ret = -errno;
288-
cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
289-
pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
290-
__func__, cp, -ret);
285+
pr_warn("Error in %s(): %s. Couldn't create simple array map.\n",
286+
__func__, errstr(ret));
291287
return ret;
292288
}
293289

@@ -604,7 +600,8 @@ bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_
604600
} else if (ret == 0) {
605601
WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
606602
} else {
607-
pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret);
603+
pr_warn("Detection of kernel %s support failed: %s\n",
604+
feat->desc, errstr(ret));
608605
WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
609606
}
610607
}

tools/lib/bpf/gen_loader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "bpf_gen_internal.h"
1515
#include "skel_internal.h"
1616
#include <asm/byteorder.h>
17+
#include "str_error.h"
1718

1819
#define MAX_USED_MAPS 64
1920
#define MAX_USED_PROGS 32
@@ -393,7 +394,7 @@ int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
393394
blob_fd_array_off(gen, i));
394395
emit(gen, BPF_MOV64_IMM(BPF_REG_0, 0));
395396
emit(gen, BPF_EXIT_INSN());
396-
pr_debug("gen: finish %d\n", gen->error);
397+
pr_debug("gen: finish %s\n", errstr(gen->error));
397398
if (!gen->error) {
398399
struct gen_loader_opts *opts = gen->opts;
399400

0 commit comments

Comments
 (0)