Skip to content

Commit 02d3ecd

Browse files
mrpreqmonnet
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. Signed-off-by: Jiayuan Chen <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 3df6202 commit 02d3ecd

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

src/gen.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
670670
continue;
671671
if (bpf_map__is_internal(map) &&
672672
(bpf_map__map_flags(map) & BPF_F_MMAPABLE))
673-
printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
673+
printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zu);\n",
674674
ident, bpf_map_mmap_sz(map));
675675
codegen("\
676676
\n\
@@ -984,7 +984,7 @@ static int walk_st_ops_shadow_vars(struct btf *btf, const char *ident,
984984

985985
offset = m->offset / 8;
986986
if (next_offset < offset)
987-
printf("\t\t\tchar __padding_%d[%d];\n", i, offset - next_offset);
987+
printf("\t\t\tchar __padding_%d[%u];\n", i, offset - next_offset);
988988

989989
switch (btf_kind(member_type)) {
990990
case BTF_KIND_INT:
@@ -1052,7 +1052,7 @@ static int walk_st_ops_shadow_vars(struct btf *btf, const char *ident,
10521052
/* Cannot fail since it must be a struct type */
10531053
size = btf__resolve_size(btf, map_type_id);
10541054
if (next_offset < (__u32)size)
1055-
printf("\t\t\tchar __padding_end[%d];\n", size - next_offset);
1055+
printf("\t\t\tchar __padding_end[%u];\n", size - next_offset);
10561056

10571057
out:
10581058
btf_dump__free(d);
@@ -2095,7 +2095,7 @@ btfgen_mark_type(struct btfgen_info *info, unsigned int type_id, bool follow_poi
20952095
break;
20962096
/* tells if some other type needs to be handled */
20972097
default:
2098-
p_err("unsupported kind: %s (%d)", btf_kind_str(btf_type), type_id);
2098+
p_err("unsupported kind: %s (%u)", btf_kind_str(btf_type), type_id);
20992099
return -EINVAL;
21002100
}
21012101

@@ -2147,7 +2147,7 @@ static int btfgen_record_field_relo(struct btfgen_info *info, struct bpf_core_sp
21472147
btf_type = btf__type_by_id(btf, type_id);
21482148
break;
21492149
default:
2150-
p_err("unsupported kind: %s (%d)",
2150+
p_err("unsupported kind: %s (%u)",
21512151
btf_kind_str(btf_type), btf_type->type);
21522152
return -EINVAL;
21532153
}
@@ -2246,7 +2246,7 @@ static int btfgen_mark_type_match(struct btfgen_info *info, __u32 type_id, bool
22462246
}
22472247
/* tells if some other type needs to be handled */
22482248
default:
2249-
p_err("unsupported kind: %s (%d)", btf_kind_str(btf_type), type_id);
2249+
p_err("unsupported kind: %s (%u)", btf_kind_str(btf_type), type_id);
22502250
return -EINVAL;
22512251
}
22522252

src/link.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static int link_parse_fd(int *argc, char ***argv)
107107

108108
fd = bpf_link_get_fd_by_id(id);
109109
if (fd < 0)
110-
p_err("failed to get link with ID %d: %s", id, strerror(errno));
110+
p_err("failed to get link with ID %u: %s", id, strerror(errno));
111111
return fd;
112112
} else if (is_prefix(**argv, "pinned")) {
113113
char *path;
@@ -404,23 +404,23 @@ static char *perf_config_hw_cache_str(__u64 config)
404404
if (hw_cache)
405405
snprintf(str, PERF_HW_CACHE_LEN, "%s-", hw_cache);
406406
else
407-
snprintf(str, PERF_HW_CACHE_LEN, "%lld-", config & 0xff);
407+
snprintf(str, PERF_HW_CACHE_LEN, "%llu-", config & 0xff);
408408

409409
op = perf_event_name(evsel__hw_cache_op, (config >> 8) & 0xff);
410410
if (op)
411411
snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
412412
"%s-", op);
413413
else
414414
snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
415-
"%lld-", (config >> 8) & 0xff);
415+
"%llu-", (config >> 8) & 0xff);
416416

417417
result = perf_event_name(evsel__hw_cache_result, config >> 16);
418418
if (result)
419419
snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
420420
"%s", result);
421421
else
422422
snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
423-
"%lld", config >> 16);
423+
"%llu", config >> 16);
424424
return str;
425425
}
426426

@@ -623,7 +623,7 @@ static void show_link_ifindex_plain(__u32 ifindex)
623623
else
624624
snprintf(devname, sizeof(devname), "(detached)");
625625
if (ret)
626-
snprintf(devname, sizeof(devname), "%s(%d)",
626+
snprintf(devname, sizeof(devname), "%s(%u)",
627627
tmpname, ifindex);
628628
printf("ifindex %s ", devname);
629629
}
@@ -699,7 +699,7 @@ void netfilter_dump_plain(const struct bpf_link_info *info)
699699
if (pfname)
700700
printf("\n\t%s", pfname);
701701
else
702-
printf("\n\tpf: %d", pf);
702+
printf("\n\tpf: %u", pf);
703703

704704
if (hookname)
705705
printf(" %s", hookname);
@@ -773,7 +773,7 @@ static void show_uprobe_multi_plain(struct bpf_link_info *info)
773773
printf("func_cnt %u ", info->uprobe_multi.count);
774774

775775
if (info->uprobe_multi.pid)
776-
printf("pid %d ", info->uprobe_multi.pid);
776+
printf("pid %u ", info->uprobe_multi.pid);
777777

778778
printf("\n\t%-16s %-16s %-16s", "offset", "ref_ctr_offset", "cookies");
779779
for (i = 0; i < info->uprobe_multi.count; i++) {

src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static int do_version(int argc, char **argv)
152152
BPFTOOL_MINOR_VERSION, BPFTOOL_PATCH_VERSION);
153153
#endif
154154
jsonw_name(json_wtr, "libbpf_version");
155-
jsonw_printf(json_wtr, "\"%d.%d\"",
155+
jsonw_printf(json_wtr, "\"%u.%u\"",
156156
libbpf_major_version(), libbpf_minor_version());
157157

158158
jsonw_name(json_wtr, "features");
@@ -370,7 +370,7 @@ static int do_batch(int argc, char **argv)
370370
while ((cp = strstr(buf, "\\\n")) != NULL) {
371371
if (!fgets(contline, sizeof(contline), fp) ||
372372
strlen(contline) == 0) {
373-
p_err("missing continuation line on command %d",
373+
p_err("missing continuation line on command %u",
374374
lines);
375375
err = -1;
376376
goto err_close;
@@ -381,7 +381,7 @@ static int do_batch(int argc, char **argv)
381381
*cp = '\0';
382382

383383
if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
384-
p_err("command %d is too long", lines);
384+
p_err("command %u is too long", lines);
385385
err = -1;
386386
goto err_close;
387387
}
@@ -423,7 +423,7 @@ static int do_batch(int argc, char **argv)
423423
err = -1;
424424
} else {
425425
if (!json_output)
426-
printf("processed %d commands\n", lines);
426+
printf("processed %u commands\n", lines);
427427
}
428428
err_close:
429429
if (fp != stdin)

src/map.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
285285
}
286286
if (info->value_size) {
287287
for (i = 0; i < n; i++) {
288-
printf("value (CPU %02d):%c",
288+
printf("value (CPU %02u):%c",
289289
i, info->value_size > 16 ? '\n' : ' ');
290290
fprint_hex(stdout, value + i * step,
291291
info->value_size, " ");
@@ -316,7 +316,7 @@ static char **parse_bytes(char **argv, const char *name, unsigned char *val,
316316
}
317317

318318
if (i != n) {
319-
p_err("%s expected %d bytes got %d", name, n, i);
319+
p_err("%s expected %u bytes got %u", name, n, i);
320320
return NULL;
321321
}
322322

@@ -462,7 +462,7 @@ static void show_map_header_json(struct bpf_map_info *info, json_writer_t *wtr)
462462
jsonw_string_field(wtr, "name", info->name);
463463

464464
jsonw_name(wtr, "flags");
465-
jsonw_printf(wtr, "%d", info->map_flags);
465+
jsonw_printf(wtr, "%u", info->map_flags);
466466
}
467467

468468
static int show_map_close_json(int fd, struct bpf_map_info *info)
@@ -588,7 +588,7 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info)
588588
if (prog_type_str)
589589
printf("owner_prog_type %s ", prog_type_str);
590590
else
591-
printf("owner_prog_type %d ", prog_type);
591+
printf("owner_prog_type %u ", prog_type);
592592
}
593593
if (owner_jited)
594594
printf("owner%s jited",
@@ -615,7 +615,7 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info)
615615
printf("\n\t");
616616

617617
if (info->btf_id)
618-
printf("btf_id %d", info->btf_id);
618+
printf("btf_id %u", info->btf_id);
619619

620620
if (frozen)
621621
printf("%sfrozen", info->btf_id ? " " : "");

0 commit comments

Comments
 (0)