1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
5 #include <stdio.h> /* for (FILE *) used by json_writer */
8 #include <asm/byteorder.h>
9 #include <linux/bitops.h>
10 #include <linux/btf.h>
11 #include <linux/err.h>
15 #include "json_writer.h"
18 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
19 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
20 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
21 #define BITS_ROUNDUP_BYTES(bits) \
22 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
24 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
25 __u8 bit_offset, const void *data);
27 static int btf_dump_func(const struct btf *btf, char *func_sig,
28 const struct btf_type *func_proto,
29 const struct btf_type *func, int pos, int size);
31 static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,
32 const struct btf_type *func_proto,
35 const struct btf_type *func_type;
36 int prog_fd = -1, func_sig_len;
37 struct bpf_prog_info info = {};
38 __u32 info_len = sizeof(info);
39 const char *prog_name = NULL;
40 struct btf *prog_btf = NULL;
41 struct bpf_func_info finfo;
46 /* Get the ptr's func_proto */
47 func_sig_len = btf_dump_func(d->btf, prog_str, func_proto, NULL, 0,
49 if (func_sig_len == -1)
55 /* Get the bpf_prog's name. Obtain from func_info. */
56 prog_fd = bpf_prog_get_fd_by_id(prog_id);
60 err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
64 if (!info.btf_id || !info.nr_func_info)
67 finfo_rec_size = info.func_info_rec_size;
68 memset(&info, 0, sizeof(info));
69 info.nr_func_info = 1;
70 info.func_info_rec_size = finfo_rec_size;
71 info.func_info = ptr_to_u64(&finfo);
73 err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
77 prog_btf = btf__load_from_kernel_by_id(info.btf_id);
80 func_type = btf__type_by_id(prog_btf, finfo.type_id);
81 if (!func_type || !btf_is_func(func_type))
84 prog_name = btf__name_by_offset(prog_btf, func_type->name_off);
88 snprintf(&prog_str[func_sig_len],
89 sizeof(prog_str) - func_sig_len, " 0");
91 snprintf(&prog_str[func_sig_len],
92 sizeof(prog_str) - func_sig_len,
93 " %s/prog_id:%u", prog_name, prog_id);
95 snprintf(&prog_str[func_sig_len],
96 sizeof(prog_str) - func_sig_len,
97 " <unknown_prog_name>/prog_id:%u", prog_id);
99 prog_str[sizeof(prog_str) - 1] = '\0';
100 jsonw_string(d->jw, prog_str);
107 static void btf_dumper_ptr(const struct btf_dumper *d,
108 const struct btf_type *t,
111 unsigned long value = *(unsigned long *)data;
112 const struct btf_type *ptr_type;
115 if (!d->prog_id_as_func_ptr || value > UINT32_MAX)
116 goto print_ptr_value;
118 ptr_type_id = btf__resolve_type(d->btf, t->type);
120 goto print_ptr_value;
121 ptr_type = btf__type_by_id(d->btf, ptr_type_id);
122 if (!ptr_type || !btf_is_func_proto(ptr_type))
123 goto print_ptr_value;
125 if (!dump_prog_id_as_func_ptr(d, ptr_type, value))
129 if (d->is_plain_text)
130 jsonw_printf(d->jw, "\"%p\"", (void *)value);
132 jsonw_printf(d->jw, "%lu", value);
135 static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
136 __u8 bit_offset, const void *data)
140 actual_type_id = btf__resolve_type(d->btf, type_id);
141 if (actual_type_id < 0)
142 return actual_type_id;
144 return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
147 static int btf_dumper_enum(const struct btf_dumper *d,
148 const struct btf_type *t,
151 const struct btf_enum *enums = btf_enum(t);
157 value = *(__s64 *)data;
160 value = *(__s32 *)data;
163 value = *(__s16 *)data;
166 value = *(__s8 *)data;
172 for (i = 0; i < btf_vlen(t); i++) {
173 if (value == enums[i].val) {
175 btf__name_by_offset(d->btf,
181 jsonw_int(d->jw, value);
185 static int btf_dumper_enum64(const struct btf_dumper *d,
186 const struct btf_type *t,
189 const struct btf_enum64 *enums = btf_enum64(t);
190 __u32 val_lo32, val_hi32;
194 value = *(__u64 *)data;
195 val_lo32 = (__u32)value;
196 val_hi32 = value >> 32;
198 for (i = 0; i < btf_vlen(t); i++) {
199 if (val_lo32 == enums[i].val_lo32 && val_hi32 == enums[i].val_hi32) {
201 btf__name_by_offset(d->btf,
207 jsonw_int(d->jw, value);
211 static bool is_str_array(const struct btf *btf, const struct btf_array *arr,
214 const struct btf_type *elem_type;
220 elem_type = btf__type_by_id(btf, arr->type);
221 /* Not skipping typedef. typedef to char does not count as
224 while (elem_type && btf_is_mod(elem_type))
225 elem_type = btf__type_by_id(btf, elem_type->type);
227 if (!elem_type || !btf_is_int(elem_type) || elem_type->size != 1)
230 if (btf_int_encoding(elem_type) != BTF_INT_CHAR &&
231 strcmp("char", btf__name_by_offset(btf, elem_type->name_off)))
234 end_s = s + arr->nelems;
238 if (*s <= 0x1f || *s >= 0x7f)
243 /* '\0' is not found */
247 static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
250 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
251 struct btf_array *arr = (struct btf_array *)(t + 1);
256 if (is_str_array(d->btf, arr, data)) {
257 jsonw_string(d->jw, data);
261 elem_size = btf__resolve_size(d->btf, arr->type);
265 jsonw_start_array(d->jw);
266 for (i = 0; i < arr->nelems; i++) {
267 ret = btf_dumper_do_type(d, arr->type, 0,
268 data + i * elem_size);
273 jsonw_end_array(d->jw);
277 static void btf_int128_print(json_writer_t *jw, const void *data,
280 /* data points to a __int128 number.
282 * int128_num = *(__int128 *)data;
283 * The below formulas shows what upper_num and lower_num represents:
284 * upper_num = int128_num >> 64;
285 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
287 __u64 upper_num, lower_num;
289 #ifdef __BIG_ENDIAN_BITFIELD
290 upper_num = *(__u64 *)data;
291 lower_num = *(__u64 *)(data + 8);
293 upper_num = *(__u64 *)(data + 8);
294 lower_num = *(__u64 *)data;
299 jsonw_printf(jw, "0x%llx", lower_num);
301 jsonw_printf(jw, "0x%llx%016llx", upper_num, lower_num);
304 jsonw_printf(jw, "\"0x%llx\"", lower_num);
306 jsonw_printf(jw, "\"0x%llx%016llx\"", upper_num, lower_num);
310 static void btf_int128_shift(__u64 *print_num, __u16 left_shift_bits,
311 __u16 right_shift_bits)
313 __u64 upper_num, lower_num;
315 #ifdef __BIG_ENDIAN_BITFIELD
316 upper_num = print_num[0];
317 lower_num = print_num[1];
319 upper_num = print_num[1];
320 lower_num = print_num[0];
323 /* shake out un-needed bits by shift/or operations */
324 if (left_shift_bits >= 64) {
325 upper_num = lower_num << (left_shift_bits - 64);
328 upper_num = (upper_num << left_shift_bits) |
329 (lower_num >> (64 - left_shift_bits));
330 lower_num = lower_num << left_shift_bits;
333 if (right_shift_bits >= 64) {
334 lower_num = upper_num >> (right_shift_bits - 64);
337 lower_num = (lower_num >> right_shift_bits) |
338 (upper_num << (64 - right_shift_bits));
339 upper_num = upper_num >> right_shift_bits;
342 #ifdef __BIG_ENDIAN_BITFIELD
343 print_num[0] = upper_num;
344 print_num[1] = lower_num;
346 print_num[0] = lower_num;
347 print_num[1] = upper_num;
351 static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
352 const void *data, json_writer_t *jw,
355 int left_shift_bits, right_shift_bits;
356 __u64 print_num[2] = {};
360 bits_to_copy = bit_offset + nr_bits;
361 bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
363 memcpy(print_num, data, bytes_to_copy);
364 #if defined(__BIG_ENDIAN_BITFIELD)
365 left_shift_bits = bit_offset;
366 #elif defined(__LITTLE_ENDIAN_BITFIELD)
367 left_shift_bits = 128 - bits_to_copy;
369 #error neither big nor little endian
371 right_shift_bits = 128 - nr_bits;
373 btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
374 btf_int128_print(jw, print_num, is_plain_text);
378 static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
379 const void *data, json_writer_t *jw,
382 int nr_bits = BTF_INT_BITS(int_type);
383 int total_bits_offset;
385 /* bits_offset is at most 7.
386 * BTF_INT_OFFSET() cannot exceed 128 bits.
388 total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type);
389 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
390 bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
391 btf_dumper_bitfield(nr_bits, bit_offset, data, jw,
395 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
396 const void *data, json_writer_t *jw,
402 int_type = (__u32 *)(t + 1);
403 nr_bits = BTF_INT_BITS(*int_type);
404 /* if this is bit field */
405 if (bit_offset || BTF_INT_OFFSET(*int_type) ||
406 BITS_PER_BYTE_MASKED(nr_bits)) {
407 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
412 if (nr_bits == 128) {
413 btf_int128_print(jw, data, is_plain_text);
417 switch (BTF_INT_ENCODING(*int_type)) {
419 if (BTF_INT_BITS(*int_type) == 64)
420 jsonw_printf(jw, "%llu", *(__u64 *)data);
421 else if (BTF_INT_BITS(*int_type) == 32)
422 jsonw_printf(jw, "%u", *(__u32 *)data);
423 else if (BTF_INT_BITS(*int_type) == 16)
424 jsonw_printf(jw, "%hu", *(__u16 *)data);
425 else if (BTF_INT_BITS(*int_type) == 8)
426 jsonw_printf(jw, "%hhu", *(__u8 *)data);
428 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
432 if (BTF_INT_BITS(*int_type) == 64)
433 jsonw_printf(jw, "%lld", *(long long *)data);
434 else if (BTF_INT_BITS(*int_type) == 32)
435 jsonw_printf(jw, "%d", *(int *)data);
436 else if (BTF_INT_BITS(*int_type) == 16)
437 jsonw_printf(jw, "%hd", *(short *)data);
438 else if (BTF_INT_BITS(*int_type) == 8)
439 jsonw_printf(jw, "%hhd", *(char *)data);
441 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
445 if (isprint(*(char *)data))
446 jsonw_printf(jw, "\"%c\"", *(char *)data);
449 jsonw_printf(jw, "0x%hhx", *(char *)data);
451 jsonw_printf(jw, "\"\\u00%02hhx\"",
455 jsonw_bool(jw, *(bool *)data);
458 /* shouldn't happen */
465 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
468 const struct btf_type *t;
469 struct btf_member *m;
470 const void *data_off;
475 t = btf__type_by_id(d->btf, type_id);
479 kind_flag = BTF_INFO_KFLAG(t->info);
480 vlen = BTF_INFO_VLEN(t->info);
481 jsonw_start_object(d->jw);
482 m = (struct btf_member *)(t + 1);
484 for (i = 0; i < vlen; i++) {
485 __u32 bit_offset = m[i].offset;
486 __u32 bitfield_size = 0;
489 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
490 bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
493 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
494 data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
496 btf_dumper_bitfield(bitfield_size,
497 BITS_PER_BYTE_MASKED(bit_offset),
498 data_off, d->jw, d->is_plain_text);
500 ret = btf_dumper_do_type(d, m[i].type,
501 BITS_PER_BYTE_MASKED(bit_offset),
508 jsonw_end_object(d->jw);
513 static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
514 __u8 bit_offset, const void *data)
516 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
519 jsonw_start_object(d->jw);
520 jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
521 ret = btf_dumper_do_type(d, t->type, bit_offset, data);
522 jsonw_end_object(d->jw);
527 static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
530 struct btf_var_secinfo *vsi;
531 const struct btf_type *t;
532 int ret = 0, i, vlen;
534 t = btf__type_by_id(d->btf, type_id);
538 vlen = BTF_INFO_VLEN(t->info);
539 vsi = (struct btf_var_secinfo *)(t + 1);
541 jsonw_start_object(d->jw);
542 jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
543 jsonw_start_array(d->jw);
544 for (i = 0; i < vlen; i++) {
545 ret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);
549 jsonw_end_array(d->jw);
550 jsonw_end_object(d->jw);
555 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
556 __u8 bit_offset, const void *data)
558 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
560 switch (BTF_INFO_KIND(t->info)) {
562 return btf_dumper_int(t, bit_offset, data, d->jw,
564 case BTF_KIND_STRUCT:
566 return btf_dumper_struct(d, type_id, data);
568 return btf_dumper_array(d, type_id, data);
570 return btf_dumper_enum(d, t, data);
571 case BTF_KIND_ENUM64:
572 return btf_dumper_enum64(d, t, data);
574 btf_dumper_ptr(d, t, data);
577 jsonw_printf(d->jw, "(unknown)");
580 /* map key or value can't be forward */
581 jsonw_printf(d->jw, "(fwd-kind-invalid)");
583 case BTF_KIND_TYPEDEF:
584 case BTF_KIND_VOLATILE:
586 case BTF_KIND_RESTRICT:
587 return btf_dumper_modifier(d, type_id, bit_offset, data);
589 return btf_dumper_var(d, type_id, bit_offset, data);
590 case BTF_KIND_DATASEC:
591 return btf_dumper_datasec(d, type_id, data);
593 jsonw_printf(d->jw, "(unsupported-kind");
598 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
601 return btf_dumper_do_type(d, type_id, 0, data);
604 #define BTF_PRINT_ARG(...) \
606 pos += snprintf(func_sig + pos, size - pos, \
611 #define BTF_PRINT_TYPE(type) \
613 pos = __btf_dumper_type_only(btf, type, func_sig, \
619 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
620 char *func_sig, int pos, int size)
622 const struct btf_type *proto_type;
623 const struct btf_array *array;
624 const struct btf_var *var;
625 const struct btf_type *t;
628 BTF_PRINT_ARG("void ");
632 t = btf__type_by_id(btf, type_id);
634 switch (BTF_INFO_KIND(t->info)) {
636 case BTF_KIND_TYPEDEF:
638 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
640 case BTF_KIND_STRUCT:
641 BTF_PRINT_ARG("struct %s ",
642 btf__name_by_offset(btf, t->name_off));
645 BTF_PRINT_ARG("union %s ",
646 btf__name_by_offset(btf, t->name_off));
649 case BTF_KIND_ENUM64:
650 BTF_PRINT_ARG("enum %s ",
651 btf__name_by_offset(btf, t->name_off));
654 array = (struct btf_array *)(t + 1);
655 BTF_PRINT_TYPE(array->type);
656 BTF_PRINT_ARG("[%d]", array->nelems);
659 BTF_PRINT_TYPE(t->type);
663 BTF_PRINT_ARG("%s %s ",
664 BTF_INFO_KFLAG(t->info) ? "union" : "struct",
665 btf__name_by_offset(btf, t->name_off));
667 case BTF_KIND_VOLATILE:
668 BTF_PRINT_ARG("volatile ");
669 BTF_PRINT_TYPE(t->type);
672 BTF_PRINT_ARG("const ");
673 BTF_PRINT_TYPE(t->type);
675 case BTF_KIND_RESTRICT:
676 BTF_PRINT_ARG("restrict ");
677 BTF_PRINT_TYPE(t->type);
679 case BTF_KIND_FUNC_PROTO:
680 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
685 proto_type = btf__type_by_id(btf, t->type);
686 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
691 var = (struct btf_var *)(t + 1);
692 if (var->linkage == BTF_VAR_STATIC)
693 BTF_PRINT_ARG("static ");
694 BTF_PRINT_TYPE(t->type);
696 btf__name_by_offset(btf, t->name_off));
698 case BTF_KIND_DATASEC:
699 BTF_PRINT_ARG("section (\"%s\") ",
700 btf__name_by_offset(btf, t->name_off));
710 static int btf_dump_func(const struct btf *btf, char *func_sig,
711 const struct btf_type *func_proto,
712 const struct btf_type *func, int pos, int size)
716 BTF_PRINT_TYPE(func_proto->type);
718 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
721 vlen = BTF_INFO_VLEN(func_proto->info);
722 for (i = 0; i < vlen; i++) {
723 struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i];
728 BTF_PRINT_TYPE(arg->type);
731 btf__name_by_offset(btf, arg->name_off));
732 else if (pos && func_sig[pos - 1] == ' ')
733 /* Remove unnecessary space for
734 * FUNC_PROTO that does not have
737 func_sig[--pos] = '\0';
739 BTF_PRINT_ARG("...");
747 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
756 err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
761 static const char *ltrim(const char *s)
769 void btf_dump_linfo_plain(const struct btf *btf,
770 const struct bpf_line_info *linfo,
771 const char *prefix, bool linum)
773 const char *line = btf__name_by_offset(btf, linfo->line_off);
783 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
785 /* More forgiving on file because linum option is
786 * expected to provide more info than the already
787 * available src line.
792 printf("%s%s [file:%s line_num:%u line_col:%u]\n",
794 BPF_LINE_INFO_LINE_NUM(linfo->line_col),
795 BPF_LINE_INFO_LINE_COL(linfo->line_col));
797 printf("%s%s\n", prefix, line);
801 void btf_dump_linfo_json(const struct btf *btf,
802 const struct bpf_line_info *linfo, bool linum)
804 const char *line = btf__name_by_offset(btf, linfo->line_off);
807 jsonw_string_field(json_wtr, "src", ltrim(line));
810 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
813 jsonw_string_field(json_wtr, "file", file);
815 if (BPF_LINE_INFO_LINE_NUM(linfo->line_col))
816 jsonw_int_field(json_wtr, "line_num",
817 BPF_LINE_INFO_LINE_NUM(linfo->line_col));
819 if (BPF_LINE_INFO_LINE_COL(linfo->line_col))
820 jsonw_int_field(json_wtr, "line_col",
821 BPF_LINE_INFO_LINE_COL(linfo->line_col));
825 static void dotlabel_puts(const char *s)
845 static const char *shorten_path(const char *path)
847 const unsigned int MAX_PATH_LEN = 32;
848 size_t len = strlen(path);
849 const char *shortpath;
851 if (len <= MAX_PATH_LEN)
854 /* Search for last '/' under the MAX_PATH_LEN limit */
855 shortpath = strchr(path + len - MAX_PATH_LEN, '/');
857 if (shortpath < path + strlen("..."))
858 /* We removed a very short prefix, e.g. "/w", and we'll
859 * make the path longer by prefixing with the ellipsis.
860 * Not worth it, keep initial path.
866 /* File base name length is > MAX_PATH_LEN, search for last '/' */
867 shortpath = strrchr(path, '/');
874 void btf_dump_linfo_dotlabel(const struct btf *btf,
875 const struct bpf_line_info *linfo, bool linum)
877 const char *line = btf__name_by_offset(btf, linfo->line_off);
879 if (!line || !strlen(line))
884 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
885 const char *shortfile;
887 /* More forgiving on file because linum option is
888 * expected to provide more info than the already
889 * available src line.
894 shortfile = shorten_path(file);
896 printf("; [%s", shortfile > file ? "..." : "");
897 dotlabel_puts(shortfile);
898 printf(" line:%u col:%u]\\l\\\n",
899 BPF_LINE_INFO_LINE_NUM(linfo->line_col),
900 BPF_LINE_INFO_LINE_COL(linfo->line_col));