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 struct bpf_prog_info_linear
*prog_info
= NULL
;
36 const struct btf_type
*func_type
;
37 const char *prog_name
= NULL
;
38 struct bpf_func_info
*finfo
;
39 struct btf
*prog_btf
= NULL
;
40 struct bpf_prog_info
*info
;
41 int prog_fd
, func_sig_len
;
44 /* Get the ptr's func_proto */
45 func_sig_len
= btf_dump_func(d
->btf
, prog_str
, func_proto
, NULL
, 0,
47 if (func_sig_len
== -1)
53 /* Get the bpf_prog's name. Obtain from func_info. */
54 prog_fd
= bpf_prog_get_fd_by_id(prog_id
);
58 prog_info
= bpf_program__get_prog_info_linear(prog_fd
,
59 1UL << BPF_PROG_INFO_FUNC_INFO
);
61 if (IS_ERR(prog_info
)) {
65 info
= &prog_info
->info
;
67 if (!info
->btf_id
|| !info
->nr_func_info
||
68 btf__get_from_id(info
->btf_id
, &prog_btf
))
70 finfo
= u64_to_ptr(info
->func_info
);
71 func_type
= btf__type_by_id(prog_btf
, finfo
->type_id
);
72 if (!func_type
|| !btf_is_func(func_type
))
75 prog_name
= btf__name_by_offset(prog_btf
, func_type
->name_off
);
79 snprintf(&prog_str
[func_sig_len
],
80 sizeof(prog_str
) - func_sig_len
, " 0");
82 snprintf(&prog_str
[func_sig_len
],
83 sizeof(prog_str
) - func_sig_len
,
84 " %s/prog_id:%u", prog_name
, prog_id
);
86 snprintf(&prog_str
[func_sig_len
],
87 sizeof(prog_str
) - func_sig_len
,
88 " <unknown_prog_name>/prog_id:%u", prog_id
);
90 prog_str
[sizeof(prog_str
) - 1] = '\0';
91 jsonw_string(d
->jw
, prog_str
);
97 static void btf_dumper_ptr(const struct btf_dumper
*d
,
98 const struct btf_type
*t
,
101 unsigned long value
= *(unsigned long *)data
;
102 const struct btf_type
*ptr_type
;
105 if (!d
->prog_id_as_func_ptr
|| value
> UINT32_MAX
)
106 goto print_ptr_value
;
108 ptr_type_id
= btf__resolve_type(d
->btf
, t
->type
);
110 goto print_ptr_value
;
111 ptr_type
= btf__type_by_id(d
->btf
, ptr_type_id
);
112 if (!ptr_type
|| !btf_is_func_proto(ptr_type
))
113 goto print_ptr_value
;
115 if (!dump_prog_id_as_func_ptr(d
, ptr_type
, value
))
119 if (d
->is_plain_text
)
120 jsonw_printf(d
->jw
, "%p", (void *)value
);
122 jsonw_printf(d
->jw
, "%lu", value
);
125 static int btf_dumper_modifier(const struct btf_dumper
*d
, __u32 type_id
,
126 __u8 bit_offset
, const void *data
)
130 actual_type_id
= btf__resolve_type(d
->btf
, type_id
);
131 if (actual_type_id
< 0)
132 return actual_type_id
;
134 return btf_dumper_do_type(d
, actual_type_id
, bit_offset
, data
);
137 static int btf_dumper_enum(const struct btf_dumper
*d
,
138 const struct btf_type
*t
,
141 const struct btf_enum
*enums
= btf_enum(t
);
147 value
= *(__s64
*)data
;
150 value
= *(__s32
*)data
;
153 value
= *(__s16
*)data
;
156 value
= *(__s8
*)data
;
162 for (i
= 0; i
< btf_vlen(t
); i
++) {
163 if (value
== enums
[i
].val
) {
165 btf__name_by_offset(d
->btf
,
171 jsonw_int(d
->jw
, value
);
175 static bool is_str_array(const struct btf
*btf
, const struct btf_array
*arr
,
178 const struct btf_type
*elem_type
;
184 elem_type
= btf__type_by_id(btf
, arr
->type
);
185 /* Not skipping typedef. typedef to char does not count as
188 while (elem_type
&& btf_is_mod(elem_type
))
189 elem_type
= btf__type_by_id(btf
, elem_type
->type
);
191 if (!elem_type
|| !btf_is_int(elem_type
) || elem_type
->size
!= 1)
194 if (btf_int_encoding(elem_type
) != BTF_INT_CHAR
&&
195 strcmp("char", btf__name_by_offset(btf
, elem_type
->name_off
)))
198 end_s
= s
+ arr
->nelems
;
202 if (*s
<= 0x1f || *s
>= 0x7f)
207 /* '\0' is not found */
211 static int btf_dumper_array(const struct btf_dumper
*d
, __u32 type_id
,
214 const struct btf_type
*t
= btf__type_by_id(d
->btf
, type_id
);
215 struct btf_array
*arr
= (struct btf_array
*)(t
+ 1);
220 if (is_str_array(d
->btf
, arr
, data
)) {
221 jsonw_string(d
->jw
, data
);
225 elem_size
= btf__resolve_size(d
->btf
, arr
->type
);
229 jsonw_start_array(d
->jw
);
230 for (i
= 0; i
< arr
->nelems
; i
++) {
231 ret
= btf_dumper_do_type(d
, arr
->type
, 0,
232 data
+ i
* elem_size
);
237 jsonw_end_array(d
->jw
);
241 static void btf_int128_print(json_writer_t
*jw
, const void *data
,
244 /* data points to a __int128 number.
246 * int128_num = *(__int128 *)data;
247 * The below formulas shows what upper_num and lower_num represents:
248 * upper_num = int128_num >> 64;
249 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
251 __u64 upper_num
, lower_num
;
253 #ifdef __BIG_ENDIAN_BITFIELD
254 upper_num
= *(__u64
*)data
;
255 lower_num
= *(__u64
*)(data
+ 8);
257 upper_num
= *(__u64
*)(data
+ 8);
258 lower_num
= *(__u64
*)data
;
263 jsonw_printf(jw
, "0x%llx", lower_num
);
265 jsonw_printf(jw
, "0x%llx%016llx", upper_num
, lower_num
);
268 jsonw_printf(jw
, "\"0x%llx\"", lower_num
);
270 jsonw_printf(jw
, "\"0x%llx%016llx\"", upper_num
, lower_num
);
274 static void btf_int128_shift(__u64
*print_num
, __u16 left_shift_bits
,
275 __u16 right_shift_bits
)
277 __u64 upper_num
, lower_num
;
279 #ifdef __BIG_ENDIAN_BITFIELD
280 upper_num
= print_num
[0];
281 lower_num
= print_num
[1];
283 upper_num
= print_num
[1];
284 lower_num
= print_num
[0];
287 /* shake out un-needed bits by shift/or operations */
288 if (left_shift_bits
>= 64) {
289 upper_num
= lower_num
<< (left_shift_bits
- 64);
292 upper_num
= (upper_num
<< left_shift_bits
) |
293 (lower_num
>> (64 - left_shift_bits
));
294 lower_num
= lower_num
<< left_shift_bits
;
297 if (right_shift_bits
>= 64) {
298 lower_num
= upper_num
>> (right_shift_bits
- 64);
301 lower_num
= (lower_num
>> right_shift_bits
) |
302 (upper_num
<< (64 - right_shift_bits
));
303 upper_num
= upper_num
>> right_shift_bits
;
306 #ifdef __BIG_ENDIAN_BITFIELD
307 print_num
[0] = upper_num
;
308 print_num
[1] = lower_num
;
310 print_num
[0] = lower_num
;
311 print_num
[1] = upper_num
;
315 static void btf_dumper_bitfield(__u32 nr_bits
, __u8 bit_offset
,
316 const void *data
, json_writer_t
*jw
,
319 int left_shift_bits
, right_shift_bits
;
320 __u64 print_num
[2] = {};
324 bits_to_copy
= bit_offset
+ nr_bits
;
325 bytes_to_copy
= BITS_ROUNDUP_BYTES(bits_to_copy
);
327 memcpy(print_num
, data
, bytes_to_copy
);
328 #if defined(__BIG_ENDIAN_BITFIELD)
329 left_shift_bits
= bit_offset
;
330 #elif defined(__LITTLE_ENDIAN_BITFIELD)
331 left_shift_bits
= 128 - bits_to_copy
;
333 #error neither big nor little endian
335 right_shift_bits
= 128 - nr_bits
;
337 btf_int128_shift(print_num
, left_shift_bits
, right_shift_bits
);
338 btf_int128_print(jw
, print_num
, is_plain_text
);
342 static void btf_dumper_int_bits(__u32 int_type
, __u8 bit_offset
,
343 const void *data
, json_writer_t
*jw
,
346 int nr_bits
= BTF_INT_BITS(int_type
);
347 int total_bits_offset
;
349 /* bits_offset is at most 7.
350 * BTF_INT_OFFSET() cannot exceed 128 bits.
352 total_bits_offset
= bit_offset
+ BTF_INT_OFFSET(int_type
);
353 data
+= BITS_ROUNDDOWN_BYTES(total_bits_offset
);
354 bit_offset
= BITS_PER_BYTE_MASKED(total_bits_offset
);
355 btf_dumper_bitfield(nr_bits
, bit_offset
, data
, jw
,
359 static int btf_dumper_int(const struct btf_type
*t
, __u8 bit_offset
,
360 const void *data
, json_writer_t
*jw
,
366 int_type
= (__u32
*)(t
+ 1);
367 nr_bits
= BTF_INT_BITS(*int_type
);
368 /* if this is bit field */
369 if (bit_offset
|| BTF_INT_OFFSET(*int_type
) ||
370 BITS_PER_BYTE_MASKED(nr_bits
)) {
371 btf_dumper_int_bits(*int_type
, bit_offset
, data
, jw
,
376 if (nr_bits
== 128) {
377 btf_int128_print(jw
, data
, is_plain_text
);
381 switch (BTF_INT_ENCODING(*int_type
)) {
383 if (BTF_INT_BITS(*int_type
) == 64)
384 jsonw_printf(jw
, "%llu", *(__u64
*)data
);
385 else if (BTF_INT_BITS(*int_type
) == 32)
386 jsonw_printf(jw
, "%u", *(__u32
*)data
);
387 else if (BTF_INT_BITS(*int_type
) == 16)
388 jsonw_printf(jw
, "%hu", *(__u16
*)data
);
389 else if (BTF_INT_BITS(*int_type
) == 8)
390 jsonw_printf(jw
, "%hhu", *(__u8
*)data
);
392 btf_dumper_int_bits(*int_type
, bit_offset
, data
, jw
,
396 if (BTF_INT_BITS(*int_type
) == 64)
397 jsonw_printf(jw
, "%lld", *(long long *)data
);
398 else if (BTF_INT_BITS(*int_type
) == 32)
399 jsonw_printf(jw
, "%d", *(int *)data
);
400 else if (BTF_INT_BITS(*int_type
) == 16)
401 jsonw_printf(jw
, "%hd", *(short *)data
);
402 else if (BTF_INT_BITS(*int_type
) == 8)
403 jsonw_printf(jw
, "%hhd", *(char *)data
);
405 btf_dumper_int_bits(*int_type
, bit_offset
, data
, jw
,
409 if (isprint(*(char *)data
))
410 jsonw_printf(jw
, "\"%c\"", *(char *)data
);
413 jsonw_printf(jw
, "0x%hhx", *(char *)data
);
415 jsonw_printf(jw
, "\"\\u00%02hhx\"",
419 jsonw_bool(jw
, *(int *)data
);
422 /* shouldn't happen */
429 static int btf_dumper_struct(const struct btf_dumper
*d
, __u32 type_id
,
432 const struct btf_type
*t
;
433 struct btf_member
*m
;
434 const void *data_off
;
439 t
= btf__type_by_id(d
->btf
, type_id
);
443 kind_flag
= BTF_INFO_KFLAG(t
->info
);
444 vlen
= BTF_INFO_VLEN(t
->info
);
445 jsonw_start_object(d
->jw
);
446 m
= (struct btf_member
*)(t
+ 1);
448 for (i
= 0; i
< vlen
; i
++) {
449 __u32 bit_offset
= m
[i
].offset
;
450 __u32 bitfield_size
= 0;
453 bitfield_size
= BTF_MEMBER_BITFIELD_SIZE(bit_offset
);
454 bit_offset
= BTF_MEMBER_BIT_OFFSET(bit_offset
);
457 jsonw_name(d
->jw
, btf__name_by_offset(d
->btf
, m
[i
].name_off
));
458 data_off
= data
+ BITS_ROUNDDOWN_BYTES(bit_offset
);
460 btf_dumper_bitfield(bitfield_size
,
461 BITS_PER_BYTE_MASKED(bit_offset
),
462 data_off
, d
->jw
, d
->is_plain_text
);
464 ret
= btf_dumper_do_type(d
, m
[i
].type
,
465 BITS_PER_BYTE_MASKED(bit_offset
),
472 jsonw_end_object(d
->jw
);
477 static int btf_dumper_var(const struct btf_dumper
*d
, __u32 type_id
,
478 __u8 bit_offset
, const void *data
)
480 const struct btf_type
*t
= btf__type_by_id(d
->btf
, type_id
);
483 jsonw_start_object(d
->jw
);
484 jsonw_name(d
->jw
, btf__name_by_offset(d
->btf
, t
->name_off
));
485 ret
= btf_dumper_do_type(d
, t
->type
, bit_offset
, data
);
486 jsonw_end_object(d
->jw
);
491 static int btf_dumper_datasec(const struct btf_dumper
*d
, __u32 type_id
,
494 struct btf_var_secinfo
*vsi
;
495 const struct btf_type
*t
;
496 int ret
= 0, i
, vlen
;
498 t
= btf__type_by_id(d
->btf
, type_id
);
502 vlen
= BTF_INFO_VLEN(t
->info
);
503 vsi
= (struct btf_var_secinfo
*)(t
+ 1);
505 jsonw_start_object(d
->jw
);
506 jsonw_name(d
->jw
, btf__name_by_offset(d
->btf
, t
->name_off
));
507 jsonw_start_array(d
->jw
);
508 for (i
= 0; i
< vlen
; i
++) {
509 ret
= btf_dumper_do_type(d
, vsi
[i
].type
, 0, data
+ vsi
[i
].offset
);
513 jsonw_end_array(d
->jw
);
514 jsonw_end_object(d
->jw
);
519 static int btf_dumper_do_type(const struct btf_dumper
*d
, __u32 type_id
,
520 __u8 bit_offset
, const void *data
)
522 const struct btf_type
*t
= btf__type_by_id(d
->btf
, type_id
);
524 switch (BTF_INFO_KIND(t
->info
)) {
526 return btf_dumper_int(t
, bit_offset
, data
, d
->jw
,
528 case BTF_KIND_STRUCT
:
530 return btf_dumper_struct(d
, type_id
, data
);
532 return btf_dumper_array(d
, type_id
, data
);
534 return btf_dumper_enum(d
, t
, data
);
536 btf_dumper_ptr(d
, t
, data
);
539 jsonw_printf(d
->jw
, "(unknown)");
542 /* map key or value can't be forward */
543 jsonw_printf(d
->jw
, "(fwd-kind-invalid)");
545 case BTF_KIND_TYPEDEF
:
546 case BTF_KIND_VOLATILE
:
548 case BTF_KIND_RESTRICT
:
549 return btf_dumper_modifier(d
, type_id
, bit_offset
, data
);
551 return btf_dumper_var(d
, type_id
, bit_offset
, data
);
552 case BTF_KIND_DATASEC
:
553 return btf_dumper_datasec(d
, type_id
, data
);
555 jsonw_printf(d
->jw
, "(unsupported-kind");
560 int btf_dumper_type(const struct btf_dumper
*d
, __u32 type_id
,
563 return btf_dumper_do_type(d
, type_id
, 0, data
);
566 #define BTF_PRINT_ARG(...) \
568 pos += snprintf(func_sig + pos, size - pos, \
573 #define BTF_PRINT_TYPE(type) \
575 pos = __btf_dumper_type_only(btf, type, func_sig, \
581 static int __btf_dumper_type_only(const struct btf
*btf
, __u32 type_id
,
582 char *func_sig
, int pos
, int size
)
584 const struct btf_type
*proto_type
;
585 const struct btf_array
*array
;
586 const struct btf_var
*var
;
587 const struct btf_type
*t
;
590 BTF_PRINT_ARG("void ");
594 t
= btf__type_by_id(btf
, type_id
);
596 switch (BTF_INFO_KIND(t
->info
)) {
598 case BTF_KIND_TYPEDEF
:
599 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf
, t
->name_off
));
601 case BTF_KIND_STRUCT
:
602 BTF_PRINT_ARG("struct %s ",
603 btf__name_by_offset(btf
, t
->name_off
));
606 BTF_PRINT_ARG("union %s ",
607 btf__name_by_offset(btf
, t
->name_off
));
610 BTF_PRINT_ARG("enum %s ",
611 btf__name_by_offset(btf
, t
->name_off
));
614 array
= (struct btf_array
*)(t
+ 1);
615 BTF_PRINT_TYPE(array
->type
);
616 BTF_PRINT_ARG("[%d]", array
->nelems
);
619 BTF_PRINT_TYPE(t
->type
);
623 BTF_PRINT_ARG("%s %s ",
624 BTF_INFO_KFLAG(t
->info
) ? "union" : "struct",
625 btf__name_by_offset(btf
, t
->name_off
));
627 case BTF_KIND_VOLATILE
:
628 BTF_PRINT_ARG("volatile ");
629 BTF_PRINT_TYPE(t
->type
);
632 BTF_PRINT_ARG("const ");
633 BTF_PRINT_TYPE(t
->type
);
635 case BTF_KIND_RESTRICT
:
636 BTF_PRINT_ARG("restrict ");
637 BTF_PRINT_TYPE(t
->type
);
639 case BTF_KIND_FUNC_PROTO
:
640 pos
= btf_dump_func(btf
, func_sig
, t
, NULL
, pos
, size
);
645 proto_type
= btf__type_by_id(btf
, t
->type
);
646 pos
= btf_dump_func(btf
, func_sig
, proto_type
, t
, pos
, size
);
651 var
= (struct btf_var
*)(t
+ 1);
652 if (var
->linkage
== BTF_VAR_STATIC
)
653 BTF_PRINT_ARG("static ");
654 BTF_PRINT_TYPE(t
->type
);
656 btf__name_by_offset(btf
, t
->name_off
));
658 case BTF_KIND_DATASEC
:
659 BTF_PRINT_ARG("section (\"%s\") ",
660 btf__name_by_offset(btf
, t
->name_off
));
670 static int btf_dump_func(const struct btf
*btf
, char *func_sig
,
671 const struct btf_type
*func_proto
,
672 const struct btf_type
*func
, int pos
, int size
)
676 BTF_PRINT_TYPE(func_proto
->type
);
678 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf
, func
->name_off
));
681 vlen
= BTF_INFO_VLEN(func_proto
->info
);
682 for (i
= 0; i
< vlen
; i
++) {
683 struct btf_param
*arg
= &((struct btf_param
*)(func_proto
+ 1))[i
];
688 BTF_PRINT_TYPE(arg
->type
);
691 btf__name_by_offset(btf
, arg
->name_off
));
692 else if (pos
&& func_sig
[pos
- 1] == ' ')
693 /* Remove unnecessary space for
694 * FUNC_PROTO that does not have
697 func_sig
[--pos
] = '\0';
699 BTF_PRINT_ARG("...");
707 void btf_dumper_type_only(const struct btf
*btf
, __u32 type_id
, char *func_sig
,
716 err
= __btf_dumper_type_only(btf
, type_id
, func_sig
, 0, size
);
721 static const char *ltrim(const char *s
)
729 void btf_dump_linfo_plain(const struct btf
*btf
,
730 const struct bpf_line_info
*linfo
,
731 const char *prefix
, bool linum
)
733 const char *line
= btf__name_by_offset(btf
, linfo
->line_off
);
743 const char *file
= btf__name_by_offset(btf
, linfo
->file_name_off
);
745 /* More forgiving on file because linum option is
746 * expected to provide more info than the already
747 * available src line.
752 printf("%s%s [file:%s line_num:%u line_col:%u]\n",
754 BPF_LINE_INFO_LINE_NUM(linfo
->line_col
),
755 BPF_LINE_INFO_LINE_COL(linfo
->line_col
));
757 printf("%s%s\n", prefix
, line
);
761 void btf_dump_linfo_json(const struct btf
*btf
,
762 const struct bpf_line_info
*linfo
, bool linum
)
764 const char *line
= btf__name_by_offset(btf
, linfo
->line_off
);
767 jsonw_string_field(json_wtr
, "src", ltrim(line
));
770 const char *file
= btf__name_by_offset(btf
, linfo
->file_name_off
);
773 jsonw_string_field(json_wtr
, "file", file
);
775 if (BPF_LINE_INFO_LINE_NUM(linfo
->line_col
))
776 jsonw_int_field(json_wtr
, "line_num",
777 BPF_LINE_INFO_LINE_NUM(linfo
->line_col
));
779 if (BPF_LINE_INFO_LINE_COL(linfo
->line_col
))
780 jsonw_int_field(json_wtr
, "line_col",
781 BPF_LINE_INFO_LINE_COL(linfo
->line_col
));