Commit 47e2c45c authored by Andrii Nakryiko's avatar Andrii Nakryiko
Browse files

Merge branch 'libbpf-stringify-error-codes-in-log-messages'

Mykyta Yatsenko says:

====================
libbpf: stringify error codes in log messages

From: Mykyta Yatsenko <yatsenko@meta.com>

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/20241111212919.368971-1-mykyta.yatsenko5@gmail.com


Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents 213a6952 4ce16ddd
Loading
Loading
Loading
Loading
+14 −12
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include "libbpf_internal.h"
#include "hashmap.h"
#include "strset.h"
#include "str_error.h"

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

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

		err = -errno;
		pr_warn("BTF loading error: %d\n", err);
		pr_warn("BTF loading error: %s\n", errstr(err));
		/* don't print out contents of custom log_buf */
		if (!log_buf && buf[0])
			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)

	err = btf_dedup_prep(d);
	if (err) {
		pr_debug("btf_dedup_prep failed:%d\n", err);
		pr_debug("btf_dedup_prep failed: %s\n", errstr(err));
		goto done;
	}
	err = btf_dedup_strings(d);
	if (err < 0) {
		pr_debug("btf_dedup_strings failed:%d\n", err);
		pr_debug("btf_dedup_strings failed: %s\n", errstr(err));
		goto done;
	}
	err = btf_dedup_prim_types(d);
	if (err < 0) {
		pr_debug("btf_dedup_prim_types failed:%d\n", err);
		pr_debug("btf_dedup_prim_types failed: %s\n", errstr(err));
		goto done;
	}
	err = btf_dedup_struct_types(d);
	if (err < 0) {
		pr_debug("btf_dedup_struct_types failed:%d\n", err);
		pr_debug("btf_dedup_struct_types failed: %s\n", errstr(err));
		goto done;
	}
	err = btf_dedup_resolve_fwds(d);
	if (err < 0) {
		pr_debug("btf_dedup_resolve_fwds failed:%d\n", err);
		pr_debug("btf_dedup_resolve_fwds failed: %s\n", errstr(err));
		goto done;
	}
	err = btf_dedup_ref_types(d);
	if (err < 0) {
		pr_debug("btf_dedup_ref_types failed:%d\n", err);
		pr_debug("btf_dedup_ref_types failed: %s\n", errstr(err));
		goto done;
	}
	err = btf_dedup_compact_types(d);
	if (err < 0) {
		pr_debug("btf_dedup_compact_types failed:%d\n", err);
		pr_debug("btf_dedup_compact_types failed: %s\n", errstr(err));
		goto done;
	}
	err = btf_dedup_remap_types(d);
	if (err < 0) {
		pr_debug("btf_dedup_remap_types failed:%d\n", err);
		pr_debug("btf_dedup_remap_types failed: %s\n", errstr(err));
		goto done;
	}

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

		btf = btf__parse(path, NULL);
		err = libbpf_get_error(btf);
		pr_debug("loading kernel BTF '%s': %d\n", path, err);
		pr_debug("loading kernel BTF '%s': %s\n", path, errstr(err));
		if (err)
			continue;

+2 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "hashmap.h"
#include "libbpf.h"
#include "libbpf_internal.h"
#include "str_error.h"

static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
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,
			 * chain, restore stack, emit warning, and try to
			 * proceed nevertheless
			 */
			pr_warn("not enough memory for decl stack: %d\n", err);
			pr_warn("not enough memory for decl stack: %s\n", errstr(err));
			d->decl_stack_cnt = stack_start;
			return;
		}
+1 −3
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@

int elf_open(const char *binary_path, struct elf_fd *elf_fd)
{
	char errmsg[STRERR_BUFSIZE];
	int fd, ret;
	Elf *elf;

@@ -38,8 +37,7 @@ int elf_open(const char *binary_path, struct elf_fd *elf_fd)
	fd = open(binary_path, O_RDONLY | O_CLOEXEC);
	if (fd < 0) {
		ret = -errno;
		pr_warn("elf: failed to open %s: %s\n", binary_path,
			libbpf_strerror_r(ret, errmsg, sizeof(errmsg)));
		pr_warn("elf: failed to open %s: %s\n", binary_path, errstr(ret));
		return ret;
	}
	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
+6 −9
Original line number Diff line number Diff line
@@ -47,7 +47,6 @@ static int probe_kern_prog_name(int token_fd)

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

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

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

@@ -604,7 +600,8 @@ bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_
		} else if (ret == 0) {
			WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
		} else {
			pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret);
			pr_warn("Detection of kernel %s support failed: %s\n",
				feat->desc, errstr(ret));
			WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
		}
	}
+2 −1
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#include "bpf_gen_internal.h"
#include "skel_internal.h"
#include <asm/byteorder.h>
#include "str_error.h"

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

Loading