mirror of
https://github.com/netdata/libbpf.git
synced 2026-11-09 04:10:13 +08:00
libbpf: fix -Wformat warnings from format/argument type mismatches
Building libbpf with -Wall (as happens via bpftool's bootstrap build)
surfaces ~120 -Wformat warnings where pr_warn/pr_debug format specifiers
don't match their argument types: %d for __u32/Elf64_Word, %u for signed
ints, %zd for size_t, %ld for unsigned long, and %x/%lx/%llx applied to
signed values.
Match each specifier to its argument's type where a correctly-signed
specifier exists (%d<->%u, %ld->%lu, %zd->%zu). For hex conversions,
which have no signed form, cast the argument instead (%x->(unsigned),
%lx->(unsigned long), %llx->(unsigned long long)). No functional change.
Note, the fdinfo map_flags sscanf used %i into a __u32 *, which warns.
The kernel prints map_flags as hex ("map_flags:\t%#x\n" in
bpf_map_show_fdinfo(), unchanged since the field was added to fdinfo), so
switch the conversion to %x: it parses the 0x-prefixed value and expects
unsigned int *, matching the destination, so the warning is gone with no
cast.
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20260624204946.2901178-1-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
1 parent
8dae7836fb
commit
a205255dda
9 files changed
+89
-86
No files matched your search
+39
-39
@@ -1486,7 +1486,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
|
||||
type->size);
|
||||
st_ops->type_id = type_id;
|
||||
|
||||
pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
|
||||
pr_debug("struct_ops init: struct %s(type_id=%d) %s found at offset %u\n",
|
||||
tname, type_id, var_name, vsi->offset);
|
||||
}
|
||||
|
||||
@@ -2626,7 +2626,7 @@ int parse_btf_map_def(const char *map_name, struct btf *btf,
|
||||
|
||||
t = btf__type_by_id(btf, m->type);
|
||||
if (!t) {
|
||||
pr_warn("map '%s': key type [%d] not found.\n",
|
||||
pr_warn("map '%s': key type [%u] not found.\n",
|
||||
map_name, m->type);
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -2666,7 +2666,7 @@ int parse_btf_map_def(const char *map_name, struct btf *btf,
|
||||
|
||||
t = btf__type_by_id(btf, m->type);
|
||||
if (!t) {
|
||||
pr_warn("map '%s': value type [%d] not found.\n",
|
||||
pr_warn("map '%s': value type [%u] not found.\n",
|
||||
map_name, m->type);
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -2720,7 +2720,7 @@ int parse_btf_map_def(const char *map_name, struct btf *btf,
|
||||
map_def->value_size = 4;
|
||||
t = btf__type_by_id(btf, m->type);
|
||||
if (!t) {
|
||||
pr_warn("map '%s': %s type [%d] not found.\n",
|
||||
pr_warn("map '%s': %s type [%u] not found.\n",
|
||||
map_name, desc, m->type);
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -3476,7 +3476,7 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
|
||||
|
||||
var_name = btf__name_by_offset(btf, t_var->name_off);
|
||||
if (!var_name) {
|
||||
pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n",
|
||||
pr_debug("sec '%s': failed to find name of DATASEC's member #%u\n",
|
||||
sec_name, i);
|
||||
return -ENOENT;
|
||||
}
|
||||
@@ -3971,7 +3971,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
|
||||
if (!data)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
|
||||
pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
|
||||
pr_debug("elf: section(%d) %s, size %lu, link %d, flags %lx, type=%d\n",
|
||||
idx, name, (unsigned long)data->d_size,
|
||||
(int)sh->sh_link, (unsigned long)sh->sh_flags,
|
||||
(int)sh->sh_type);
|
||||
@@ -4494,7 +4494,7 @@ static int bpf_object__collect_externs(struct bpf_object *obj)
|
||||
|
||||
ext->kcfg.data_off = roundup(off, ext->kcfg.align);
|
||||
off = ext->kcfg.data_off + ext->kcfg.sz;
|
||||
pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
|
||||
pr_debug("extern (kcfg) #%d: symbol %d, off %d, name %s\n",
|
||||
i, ext->sym_idx, ext->kcfg.data_off, ext->name);
|
||||
}
|
||||
sec->size = off;
|
||||
@@ -4626,7 +4626,7 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
|
||||
struct bpf_map *map;
|
||||
|
||||
if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) {
|
||||
pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
|
||||
pr_warn("prog '%s': invalid relo against '%s' for insns[%u].code 0x%x\n",
|
||||
prog->name, sym_name, insn_idx, insn->code);
|
||||
return -LIBBPF_ERRNO__RELOC;
|
||||
}
|
||||
@@ -4749,7 +4749,7 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
|
||||
map->sec_idx != sym->st_shndx ||
|
||||
map->sec_offset != sym->st_value)
|
||||
continue;
|
||||
pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
|
||||
pr_debug("prog '%s': found map %zu (%s, sec %d, off %zu) for insn #%u\n",
|
||||
prog->name, map_idx, map->name, map->sec_idx,
|
||||
map->sec_offset, insn_idx);
|
||||
break;
|
||||
@@ -4776,7 +4776,7 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
|
||||
map = &obj->maps[map_idx];
|
||||
if (map->libbpf_type != type || map->sec_idx != sym->st_shndx)
|
||||
continue;
|
||||
pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
|
||||
pr_debug("prog '%s': found data map %zu (%s, sec %d, off %zu) for insn %u\n",
|
||||
prog->name, map_idx, map->name, map->sec_idx,
|
||||
map->sec_offset, insn_idx);
|
||||
break;
|
||||
@@ -4985,7 +4985,7 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
|
||||
info->value_size = val;
|
||||
else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
|
||||
info->max_entries = val;
|
||||
else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
|
||||
else if (sscanf(buff, "map_flags:\t%x", &val) == 1)
|
||||
info->map_flags = val;
|
||||
}
|
||||
|
||||
@@ -5521,11 +5521,11 @@ static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map)
|
||||
}
|
||||
if (err) {
|
||||
err = -errno;
|
||||
pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %s\n",
|
||||
pr_warn("map '%s': failed to initialize slot [%u] to map '%s' fd=%d: %s\n",
|
||||
map->name, i, targ_map->name, fd, errstr(err));
|
||||
return err;
|
||||
}
|
||||
pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
|
||||
pr_debug("map '%s': slot [%u] set to map '%s' fd=%d\n",
|
||||
map->name, i, targ_map->name, fd);
|
||||
}
|
||||
|
||||
@@ -5554,11 +5554,11 @@ static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map)
|
||||
err = bpf_map_update_elem(map->fd, &i, &fd, 0);
|
||||
if (err) {
|
||||
err = -errno;
|
||||
pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %s\n",
|
||||
pr_warn("map '%s': failed to initialize slot [%u] to prog '%s' fd=%d: %s\n",
|
||||
map->name, i, targ_prog->name, fd, errstr(err));
|
||||
return err;
|
||||
}
|
||||
pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n",
|
||||
pr_debug("map '%s': slot [%u] set to prog '%s' fd=%d\n",
|
||||
map->name, i, targ_prog->name, fd);
|
||||
}
|
||||
|
||||
@@ -5788,7 +5788,7 @@ int bpf_core_add_cands(struct bpf_core_cand *local_cand,
|
||||
if (strncmp(local_name, targ_name, local_essent_len) != 0)
|
||||
continue;
|
||||
|
||||
pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
|
||||
pr_debug("CO-RE relocating [%u] %s %s: found target candidate [%d] %s %s in [%s]\n",
|
||||
local_cand->id, btf_kind_str(local_t),
|
||||
local_name, i, btf_kind_str(t), targ_name,
|
||||
targ_btf_name);
|
||||
@@ -5848,7 +5848,7 @@ static int load_module_btfs(struct bpf_object *obj)
|
||||
if (errno == ENOENT)
|
||||
continue; /* expected race: BTF was unloaded */
|
||||
err = -errno;
|
||||
pr_warn("failed to get BTF object #%d FD: %s\n", id, errstr(err));
|
||||
pr_warn("failed to get BTF object #%u FD: %s\n", id, errstr(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -5861,7 +5861,7 @@ static int load_module_btfs(struct bpf_object *obj)
|
||||
err = bpf_btf_get_info_by_fd(fd, &info, &len);
|
||||
if (err) {
|
||||
err = -errno;
|
||||
pr_warn("failed to get BTF object #%d info: %s\n", id, errstr(err));
|
||||
pr_warn("failed to get BTF object #%u info: %s\n", id, errstr(err));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -5874,7 +5874,7 @@ static int load_module_btfs(struct bpf_object *obj)
|
||||
btf = btf_get_from_fd(fd, obj->btf_vmlinux);
|
||||
err = libbpf_get_error(btf);
|
||||
if (err) {
|
||||
pr_warn("failed to load module [%s]'s BTF object #%d: %s\n",
|
||||
pr_warn("failed to load module [%s]'s BTF object #%u: %s\n",
|
||||
name, id, errstr(err));
|
||||
break;
|
||||
}
|
||||
@@ -6067,7 +6067,7 @@ static int bpf_core_resolve_relo(struct bpf_program *prog,
|
||||
!hashmap__find(cand_cache, local_id, &cands)) {
|
||||
cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
|
||||
if (IS_ERR(cands)) {
|
||||
pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
|
||||
pr_warn("prog '%s': relo #%d: target candidate search failed for [%u] %s %s: %ld\n",
|
||||
prog_name, relo_idx, local_id, btf_kind_str(local_type),
|
||||
local_name, PTR_ERR(cands));
|
||||
return PTR_ERR(cands);
|
||||
@@ -6127,7 +6127,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
|
||||
goto out;
|
||||
}
|
||||
|
||||
pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info);
|
||||
pr_debug("sec '%s': found %u CO-RE relocations\n", sec_name, sec->num_info);
|
||||
|
||||
for_each_btf_ext_rec(seg, sec, i, rec) {
|
||||
if (rec->insn_off % BPF_INSN_SZ)
|
||||
@@ -6181,7 +6181,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
|
||||
|
||||
err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res);
|
||||
if (err) {
|
||||
pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %s\n",
|
||||
pr_warn("prog '%s': relo #%d: failed to patch insn #%d: %s\n",
|
||||
prog->name, i, insn_idx, errstr(err));
|
||||
goto out;
|
||||
}
|
||||
@@ -6346,7 +6346,7 @@ static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struc
|
||||
goto err_close;
|
||||
}
|
||||
if (sym_off + jt_size > obj->jumptables_data_sz) {
|
||||
pr_warn("map '.jumptables': jumptables_data size is %zd, trying to access %d\n",
|
||||
pr_warn("map '.jumptables': jumptables_data size is %zu, trying to access %u\n",
|
||||
obj->jumptables_data_sz, sym_off + jt_size);
|
||||
err = -EINVAL;
|
||||
goto err_close;
|
||||
@@ -6381,7 +6381,7 @@ static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struc
|
||||
*/
|
||||
if (insn_off > UINT32_MAX) {
|
||||
pr_warn("map '.jumptables': invalid jump table value 0x%llx at offset %u\n",
|
||||
(long long)jt[i], sym_off + i * jt_entry_size);
|
||||
(unsigned long long)jt[i], sym_off + i * jt_entry_size);
|
||||
err = -EINVAL;
|
||||
goto err_close;
|
||||
}
|
||||
@@ -6517,7 +6517,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
|
||||
}
|
||||
break;
|
||||
default:
|
||||
pr_warn("prog '%s': relo #%d: bad relo type %d\n",
|
||||
pr_warn("prog '%s': relo #%d: bad relo type %u\n",
|
||||
prog->name, i, relo->type);
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -6797,7 +6797,7 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
|
||||
*/
|
||||
continue;
|
||||
if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) {
|
||||
pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
|
||||
pr_warn("prog '%s': unexpected relo for insn #%zu, type %u\n",
|
||||
prog->name, insn_idx, relo->type);
|
||||
return -LIBBPF_ERRNO__RELOC;
|
||||
}
|
||||
@@ -7587,7 +7587,7 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
|
||||
}
|
||||
name = elf_sym_str(obj, sym->st_name) ?: "<?>";
|
||||
|
||||
pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n",
|
||||
pr_debug(".maps relo #%d: for %zd value %zu rel->r_offset %zu name %u ('%s')\n",
|
||||
i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value,
|
||||
(size_t)rel->r_offset, sym->st_name, name);
|
||||
|
||||
@@ -7678,7 +7678,7 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
|
||||
}
|
||||
map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog;
|
||||
|
||||
pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n",
|
||||
pr_debug(".maps relo #%d: map '%s' slot [%u] points to %s '%s'\n",
|
||||
i, map->name, moff, type, name);
|
||||
}
|
||||
|
||||
@@ -8738,7 +8738,7 @@ static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,
|
||||
local_name = btf__name_by_offset(obj->btf, local_type->name_off);
|
||||
targ_name = btf__name_by_offset(btf, targ_type->name_off);
|
||||
|
||||
pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
|
||||
pr_warn("extern (var ksym) '%s': incompatible types, expected [%u] %s %s, but kernel has [%u] %s %s\n",
|
||||
ext->name, local_type_id,
|
||||
btf_kind_str(local_type), local_name, targ_type_id,
|
||||
btf_kind_str(targ_type), targ_name);
|
||||
@@ -8915,7 +8915,7 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
|
||||
if (err)
|
||||
return err;
|
||||
pr_debug("extern (kcfg) '%s': set to 0x%llx\n",
|
||||
ext->name, (long long)value);
|
||||
ext->name, (unsigned long long)value);
|
||||
} else {
|
||||
pr_warn("extern '%s': unrecognized extern kind\n", ext->name);
|
||||
return -EINVAL;
|
||||
@@ -10494,7 +10494,7 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
|
||||
moff = rel->r_offset - map->sec_offset;
|
||||
shdr_idx = sym->st_shndx;
|
||||
st_ops = map->st_ops;
|
||||
pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel->r_offset %zu map->sec_offset %zu name %d (\'%s\')\n",
|
||||
pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel->r_offset %zu map->sec_offset %zu name %u (\'%s\')\n",
|
||||
map->name,
|
||||
(long long)(rel->r_info >> 32),
|
||||
(long long)sym->st_value,
|
||||
@@ -10643,7 +10643,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int t
|
||||
memset(&info, 0, info_len);
|
||||
err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len);
|
||||
if (err) {
|
||||
pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %s\n",
|
||||
pr_warn("failed bpf_prog_get_info_by_fd for FD %u: %s\n",
|
||||
attach_prog_fd, errstr(err));
|
||||
return err;
|
||||
}
|
||||
@@ -10656,7 +10656,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int t
|
||||
btf = btf_load_from_kernel(info.btf_id, NULL, token_fd);
|
||||
err = libbpf_get_error(btf);
|
||||
if (err) {
|
||||
pr_warn("Failed to get BTF %d of the program: %s\n", info.btf_id, errstr(err));
|
||||
pr_warn("Failed to get BTF %u of the program: %s\n", info.btf_id, errstr(err));
|
||||
goto out;
|
||||
}
|
||||
err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
|
||||
@@ -10738,7 +10738,7 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attac
|
||||
}
|
||||
err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd, prog->obj->token_fd);
|
||||
if (err < 0) {
|
||||
pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %s\n",
|
||||
pr_warn("prog '%s': failed to find BPF program (FD %u) BTF ID for '%s': %s\n",
|
||||
prog->name, attach_prog_fd, attach_name, errstr(err));
|
||||
return err;
|
||||
}
|
||||
@@ -11233,7 +11233,7 @@ static int validate_map_op(const struct bpf_map *map, size_t key_sz,
|
||||
}
|
||||
|
||||
if (value_sz != num_cpu * elem_sz) {
|
||||
pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
|
||||
pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zu\n",
|
||||
map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -11774,7 +11774,7 @@ static void gen_probe_legacy_event_name(char *buf, size_t buf_sz,
|
||||
static int index = 0;
|
||||
int i;
|
||||
|
||||
snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
|
||||
snprintf(buf, buf_sz, "libbpf_%d_%d_%s_0x%zx", getpid(),
|
||||
__sync_fetch_and_add(&index, 1), name, offset);
|
||||
|
||||
/* sanitize name in the probe name */
|
||||
@@ -12924,8 +12924,8 @@ static long elf_find_func_offset_from_archive(const char *archive_path, const ch
|
||||
ret = elf_find_func_offset(elf, file_name, func_name);
|
||||
if (ret > 0) {
|
||||
pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n",
|
||||
func_name, file_name, archive_path, entry.data_offset, ret,
|
||||
ret + entry.data_offset);
|
||||
func_name, file_name, archive_path, entry.data_offset, (unsigned long)ret,
|
||||
(unsigned long)(ret + entry.data_offset));
|
||||
ret += entry.data_offset;
|
||||
}
|
||||
elf_end(elf);
|
||||
@@ -14570,7 +14570,7 @@ perf_buffer__process_record(struct perf_event_header *e, void *ctx)
|
||||
break;
|
||||
}
|
||||
default:
|
||||
pr_warn("unknown perf sample type %d\n", e->type);
|
||||
pr_warn("unknown perf sample type %u\n", e->type);
|
||||
return LIBBPF_PERF_EVENT_ERROR;
|
||||
}
|
||||
return LIBBPF_PERF_EVENT_CONT;
|
||||
|
||||
Reference in new issue
Block a user