libbpf: Stringify errno in log messages in btf*.c
authorMykyta Yatsenko <yatsenko@meta.com>
Mon, 11 Nov 2024 21:29:18 +0000 (21:29 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 12 Nov 2024 04:29:45 +0000 (20:29 -0800)
Convert numeric error codes into the string representations in log
messages in btf.c and btf_dump.c.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20241111212919.368971-4-mykyta.yatsenko5@gmail.com
tools/lib/bpf/btf.c
tools/lib/bpf/btf_dump.c

index e9673c0..8befb81 100644 (file)
@@ -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 @@ retry_load:
                        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;
 
index 468392f..a3fc690 100644 (file)
@@ -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;
                }