Skip to content

Commit 3775be3

Browse files
mrpreanakryiko
authored andcommitted
bpftool: Using the right format specifiers
Fixed some formatting specifiers errors, such as using %d for int and %u for unsigned int, as well as other byte-length types. Perform type cast using the type derived from the data type itself, for example, if it's originally an int, it will be cast to unsigned int if forced to unsigned. Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250311112809.81901-3-jiayuan.chen@linux.dev
1 parent 8d86767 commit 3775be3

File tree

12 files changed

+33
-32
lines changed

12 files changed

+33
-32
lines changed

kernel/bpf/disasm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
202202
insn->dst_reg, class == BPF_ALU ? 'w' : 'r',
203203
insn->dst_reg);
204204
} else if (is_addr_space_cast(insn)) {
205-
verbose(cbs->private_data, "(%02x) r%d = addr_space_cast(r%d, %d, %d)\n",
205+
verbose(cbs->private_data, "(%02x) r%d = addr_space_cast(r%d, %u, %u)\n",
206206
insn->code, insn->dst_reg,
207207
insn->src_reg, ((u32)insn->imm) >> 16, (u16)insn->imm);
208208
} else if (is_mov_percpu_addr(insn)) {
@@ -381,7 +381,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
381381
insn->code, class == BPF_JMP32 ? 'w' : 'r',
382382
insn->dst_reg,
383383
bpf_jmp_string[BPF_OP(insn->code) >> 4],
384-
insn->imm, insn->off);
384+
(u32)insn->imm, insn->off);
385385
}
386386
} else {
387387
verbose(cbs->private_data, "(%02x) %s\n",

tools/bpf/bpftool/btf.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
253253
if (btf_kflag(t))
254254
printf("\n\t'%s' val=%d", name, v->val);
255255
else
256-
printf("\n\t'%s' val=%u", name, v->val);
256+
printf("\n\t'%s' val=%u", name, (__u32)v->val);
257257
}
258258
}
259259
if (json_output)
@@ -1022,7 +1022,7 @@ static int do_dump(int argc, char **argv)
10221022
for (i = 0; i < root_type_cnt; i++) {
10231023
if (root_type_ids[i] == root_id) {
10241024
err = -EINVAL;
1025-
p_err("duplicate root_id %d supplied", root_id);
1025+
p_err("duplicate root_id %u supplied", root_id);
10261026
goto done;
10271027
}
10281028
}
@@ -1132,7 +1132,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
11321132
break;
11331133
default:
11341134
err = -1;
1135-
p_err("unexpected object type: %d", type);
1135+
p_err("unexpected object type: %u", type);
11361136
goto err_free;
11371137
}
11381138
if (err) {
@@ -1155,7 +1155,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
11551155
break;
11561156
default:
11571157
err = -1;
1158-
p_err("unexpected object type: %d", type);
1158+
p_err("unexpected object type: %u", type);
11591159
goto err_free;
11601160
}
11611161
if (fd < 0) {
@@ -1188,7 +1188,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
11881188
break;
11891189
default:
11901190
err = -1;
1191-
p_err("unexpected object type: %d", type);
1191+
p_err("unexpected object type: %u", type);
11921192
goto err_free;
11931193
}
11941194
if (!btf_id)
@@ -1254,12 +1254,12 @@ show_btf_plain(struct bpf_btf_info *info, int fd,
12541254

12551255
n = 0;
12561256
hashmap__for_each_key_entry(btf_prog_table, entry, info->id) {
1257-
printf("%s%lu", n++ == 0 ? " prog_ids " : ",", entry->value);
1257+
printf("%s%lu", n++ == 0 ? " prog_ids " : ",", (unsigned long)entry->value);
12581258
}
12591259

12601260
n = 0;
12611261
hashmap__for_each_key_entry(btf_map_table, entry, info->id) {
1262-
printf("%s%lu", n++ == 0 ? " map_ids " : ",", entry->value);
1262+
printf("%s%lu", n++ == 0 ? " map_ids " : ",", (unsigned long)entry->value);
12631263
}
12641264

12651265
emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");

tools/bpf/bpftool/btf_dumper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
653653
case BTF_KIND_ARRAY:
654654
array = (struct btf_array *)(t + 1);
655655
BTF_PRINT_TYPE(array->type);
656-
BTF_PRINT_ARG("[%d]", array->nelems);
656+
BTF_PRINT_ARG("[%u]", array->nelems);
657657
break;
658658
case BTF_KIND_PTR:
659659
BTF_PRINT_TYPE(t->type);

tools/bpf/bpftool/cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
191191
if (attach_btf_name)
192192
printf(" %-15s", attach_btf_name);
193193
else if (info.attach_btf_id)
194-
printf(" attach_btf_obj_id=%d attach_btf_id=%d",
194+
printf(" attach_btf_obj_id=%u attach_btf_id=%u",
195195
info.attach_btf_obj_id, info.attach_btf_id);
196196
printf("\n");
197197
}

tools/bpf/bpftool/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt)
714714
int vendor_id;
715715

716716
if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
717-
p_err("Can't get net device name for ifindex %d: %s", ifindex,
717+
p_err("Can't get net device name for ifindex %u: %s", ifindex,
718718
strerror(errno));
719719
return NULL;
720720
}
@@ -739,7 +739,7 @@ ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt)
739739
/* No NFP support in LLVM, we have no valid triple to return. */
740740
default:
741741
p_err("Can't get arch name for device vendor id 0x%04x",
742-
vendor_id);
742+
(unsigned int)vendor_id);
743743
return NULL;
744744
}
745745
}

tools/bpf/bpftool/jit_disasm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
343343
{
344344
const struct bpf_line_info *linfo = NULL;
345345
unsigned int nr_skip = 0;
346-
int count, i, pc = 0;
346+
int count, i;
347+
unsigned int pc = 0;
347348
disasm_ctx_t ctx;
348349

349350
if (!len)

tools/bpf/bpftool/map_perf_ring.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ print_bpf_output(void *private_data, int cpu, struct perf_event_header *event)
9191
jsonw_end_object(json_wtr);
9292
} else {
9393
if (e->header.type == PERF_RECORD_SAMPLE) {
94-
printf("== @%lld.%09lld CPU: %d index: %d =====\n",
94+
printf("== @%llu.%09llu CPU: %d index: %d =====\n",
9595
e->time / 1000000000ULL, e->time % 1000000000ULL,
9696
cpu, idx);
9797
fprint_hex(stdout, e->data, e->size, " ");
9898
printf("\n");
9999
} else if (e->header.type == PERF_RECORD_LOST) {
100-
printf("lost %lld events\n", lost->lost);
100+
printf("lost %llu events\n", lost->lost);
101101
} else {
102-
printf("unknown event type=%d size=%d\n",
102+
printf("unknown event type=%u size=%u\n",
103103
e->header.type, e->header.size);
104104
}
105105
}

tools/bpf/bpftool/net.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
476476
for (i = 0; i < optq.count; i++) {
477477
NET_START_OBJECT;
478478
NET_DUMP_STR("devname", "%s", dev->devname);
479-
NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex);
479+
NET_DUMP_UINT("ifindex", "(%u)", (unsigned int)dev->ifindex);
480480
NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
481481
ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name,
482482
sizeof(prog_name));
@@ -831,7 +831,7 @@ static void show_link_netfilter(void)
831831
if (err) {
832832
if (errno == ENOENT)
833833
break;
834-
p_err("can't get next link: %s (id %d)", strerror(errno), id);
834+
p_err("can't get next link: %s (id %u)", strerror(errno), id);
835835
break;
836836
}
837837

tools/bpf/bpftool/netlink_dumper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static int do_xdp_dump_one(struct nlattr *attr, unsigned int ifindex,
4545
NET_START_OBJECT;
4646
if (name)
4747
NET_DUMP_STR("devname", "%s", name);
48-
NET_DUMP_UINT("ifindex", "(%d)", ifindex);
48+
NET_DUMP_UINT("ifindex", "(%u)", ifindex);
4949

5050
if (mode == XDP_ATTACHED_MULTI) {
5151
if (json_output) {
@@ -74,7 +74,7 @@ int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb)
7474
if (!tb[IFLA_XDP])
7575
return 0;
7676

77-
return do_xdp_dump_one(tb[IFLA_XDP], ifinfo->ifi_index,
77+
return do_xdp_dump_one(tb[IFLA_XDP], (unsigned int)ifinfo->ifi_index,
7878
libbpf_nla_getattr_str(tb[IFLA_IFNAME]));
7979
}
8080

@@ -168,7 +168,7 @@ int do_filter_dump(struct tcmsg *info, struct nlattr **tb, const char *kind,
168168
NET_START_OBJECT;
169169
if (devname[0] != '\0')
170170
NET_DUMP_STR("devname", "%s", devname);
171-
NET_DUMP_UINT("ifindex", "(%u)", ifindex);
171+
NET_DUMP_UINT("ifindex", "(%u)", (unsigned int)ifindex);
172172
NET_DUMP_STR("kind", " %s", kind);
173173
ret = do_bpf_filter_dump(tb[TCA_OPTIONS]);
174174
NET_END_OBJECT_FINAL;

tools/bpf/bpftool/prog.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,10 @@ static void print_prog_header_plain(struct bpf_prog_info *info, int fd)
521521
print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
522522
printf("%s", info->gpl_compatible ? " gpl" : "");
523523
if (info->run_time_ns)
524-
printf(" run_time_ns %lld run_cnt %lld",
524+
printf(" run_time_ns %llu run_cnt %llu",
525525
info->run_time_ns, info->run_cnt);
526526
if (info->recursion_misses)
527-
printf(" recursion_misses %lld", info->recursion_misses);
527+
printf(" recursion_misses %llu", info->recursion_misses);
528528
printf("\n");
529529
}
530530

@@ -569,7 +569,7 @@ static void print_prog_plain(struct bpf_prog_info *info, int fd, bool orphaned)
569569
}
570570

571571
if (info->btf_id)
572-
printf("\n\tbtf_id %d", info->btf_id);
572+
printf("\n\tbtf_id %u", info->btf_id);
573573

574574
emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
575575

@@ -1164,7 +1164,7 @@ static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
11641164
}
11651165
if (nb_read > buf_size - block_size) {
11661166
if (buf_size == UINT32_MAX) {
1167-
p_err("data_in/ctx_in is too long (max: %d)",
1167+
p_err("data_in/ctx_in is too long (max: %u)",
11681168
UINT32_MAX);
11691169
goto err_free;
11701170
}
@@ -2252,7 +2252,7 @@ static char *profile_target_name(int tgt_fd)
22522252

22532253
t = btf__type_by_id(btf, func_info.type_id);
22542254
if (!t) {
2255-
p_err("btf %d doesn't have type %d",
2255+
p_err("btf %u doesn't have type %u",
22562256
info.btf_id, func_info.type_id);
22572257
goto out;
22582258
}
@@ -2330,7 +2330,7 @@ static int profile_open_perf_events(struct profiler_bpf *obj)
23302330
continue;
23312331
for (cpu = 0; cpu < obj->rodata->num_cpu; cpu++) {
23322332
if (profile_open_perf_event(m, cpu, map_fd)) {
2333-
p_err("failed to create event %s on cpu %d",
2333+
p_err("failed to create event %s on cpu %u",
23342334
metrics[m].name, cpu);
23352335
return -1;
23362336
}

0 commit comments

Comments
 (0)