1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Facebook */
15 #include <linux/btf.h>
16 #include <sys/types.h>
21 #include <bpf/hashmap.h>
22 #include <bpf/libbpf.h>
24 #include "json_writer.h"
27 #define KFUNC_DECL_TAG "bpf_kfunc"
28 #define FASTCALL_DECL_TAG "bpf_fastcall"
30 static const char * const btf_kind_str
[NR_BTF_KINDS
] = {
31 [BTF_KIND_UNKN
] = "UNKNOWN",
32 [BTF_KIND_INT
] = "INT",
33 [BTF_KIND_PTR
] = "PTR",
34 [BTF_KIND_ARRAY
] = "ARRAY",
35 [BTF_KIND_STRUCT
] = "STRUCT",
36 [BTF_KIND_UNION
] = "UNION",
37 [BTF_KIND_ENUM
] = "ENUM",
38 [BTF_KIND_FWD
] = "FWD",
39 [BTF_KIND_TYPEDEF
] = "TYPEDEF",
40 [BTF_KIND_VOLATILE
] = "VOLATILE",
41 [BTF_KIND_CONST
] = "CONST",
42 [BTF_KIND_RESTRICT
] = "RESTRICT",
43 [BTF_KIND_FUNC
] = "FUNC",
44 [BTF_KIND_FUNC_PROTO
] = "FUNC_PROTO",
45 [BTF_KIND_VAR
] = "VAR",
46 [BTF_KIND_DATASEC
] = "DATASEC",
47 [BTF_KIND_FLOAT
] = "FLOAT",
48 [BTF_KIND_DECL_TAG
] = "DECL_TAG",
49 [BTF_KIND_TYPE_TAG
] = "TYPE_TAG",
50 [BTF_KIND_ENUM64
] = "ENUM64",
56 const char *sort_name
;
61 static const char *btf_int_enc_str(__u8 encoding
)
77 static const char *btf_var_linkage_str(__u32 linkage
)
82 case BTF_VAR_GLOBAL_ALLOCATED
:
84 case BTF_VAR_GLOBAL_EXTERN
:
91 static const char *btf_func_linkage_str(const struct btf_type
*t
)
93 switch (btf_vlen(t
)) {
105 static const char *btf_str(const struct btf
*btf
, __u32 off
)
109 return btf__name_by_offset(btf
, off
) ? : "(invalid)";
112 static int btf_kind_safe(int kind
)
114 return kind
<= BTF_KIND_MAX
? kind
: BTF_KIND_UNKN
;
117 static int dump_btf_type(const struct btf
*btf
, __u32 id
,
118 const struct btf_type
*t
)
120 json_writer_t
*w
= json_wtr
;
121 int kind
= btf_kind(t
);
124 jsonw_start_object(w
);
125 jsonw_uint_field(w
, "id", id
);
126 jsonw_string_field(w
, "kind", btf_kind_str
[btf_kind_safe(kind
)]);
127 jsonw_string_field(w
, "name", btf_str(btf
, t
->name_off
));
129 printf("[%u] %s '%s'", id
, btf_kind_str
[btf_kind_safe(kind
)],
130 btf_str(btf
, t
->name_off
));
135 __u32 v
= *(__u32
*)(t
+ 1);
138 enc
= btf_int_enc_str(BTF_INT_ENCODING(v
));
141 jsonw_uint_field(w
, "size", t
->size
);
142 jsonw_uint_field(w
, "bits_offset", BTF_INT_OFFSET(v
));
143 jsonw_uint_field(w
, "nr_bits", BTF_INT_BITS(v
));
144 jsonw_string_field(w
, "encoding", enc
);
146 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
147 t
->size
, BTF_INT_OFFSET(v
), BTF_INT_BITS(v
),
154 case BTF_KIND_VOLATILE
:
155 case BTF_KIND_RESTRICT
:
156 case BTF_KIND_TYPEDEF
:
157 case BTF_KIND_TYPE_TAG
:
159 jsonw_uint_field(w
, "type_id", t
->type
);
161 printf(" type_id=%u", t
->type
);
163 case BTF_KIND_ARRAY
: {
164 const struct btf_array
*arr
= (const void *)(t
+ 1);
167 jsonw_uint_field(w
, "type_id", arr
->type
);
168 jsonw_uint_field(w
, "index_type_id", arr
->index_type
);
169 jsonw_uint_field(w
, "nr_elems", arr
->nelems
);
171 printf(" type_id=%u index_type_id=%u nr_elems=%u",
172 arr
->type
, arr
->index_type
, arr
->nelems
);
176 case BTF_KIND_STRUCT
:
177 case BTF_KIND_UNION
: {
178 const struct btf_member
*m
= (const void *)(t
+ 1);
179 __u16 vlen
= BTF_INFO_VLEN(t
->info
);
183 jsonw_uint_field(w
, "size", t
->size
);
184 jsonw_uint_field(w
, "vlen", vlen
);
185 jsonw_name(w
, "members");
186 jsonw_start_array(w
);
188 printf(" size=%u vlen=%u", t
->size
, vlen
);
190 for (i
= 0; i
< vlen
; i
++, m
++) {
191 const char *name
= btf_str(btf
, m
->name_off
);
192 __u32 bit_off
, bit_sz
;
194 if (BTF_INFO_KFLAG(t
->info
)) {
195 bit_off
= BTF_MEMBER_BIT_OFFSET(m
->offset
);
196 bit_sz
= BTF_MEMBER_BITFIELD_SIZE(m
->offset
);
203 jsonw_start_object(w
);
204 jsonw_string_field(w
, "name", name
);
205 jsonw_uint_field(w
, "type_id", m
->type
);
206 jsonw_uint_field(w
, "bits_offset", bit_off
);
208 jsonw_uint_field(w
, "bitfield_size",
213 printf("\n\t'%s' type_id=%u bits_offset=%u",
214 name
, m
->type
, bit_off
);
216 printf(" bitfield_size=%u", bit_sz
);
223 case BTF_KIND_ENUM
: {
224 const struct btf_enum
*v
= (const void *)(t
+ 1);
225 __u16 vlen
= BTF_INFO_VLEN(t
->info
);
226 const char *encoding
;
229 encoding
= btf_kflag(t
) ? "SIGNED" : "UNSIGNED";
231 jsonw_string_field(w
, "encoding", encoding
);
232 jsonw_uint_field(w
, "size", t
->size
);
233 jsonw_uint_field(w
, "vlen", vlen
);
234 jsonw_name(w
, "values");
235 jsonw_start_array(w
);
237 printf(" encoding=%s size=%u vlen=%u", encoding
, t
->size
, vlen
);
239 for (i
= 0; i
< vlen
; i
++, v
++) {
240 const char *name
= btf_str(btf
, v
->name_off
);
243 jsonw_start_object(w
);
244 jsonw_string_field(w
, "name", name
);
246 jsonw_int_field(w
, "val", v
->val
);
248 jsonw_uint_field(w
, "val", v
->val
);
252 printf("\n\t'%s' val=%d", name
, v
->val
);
254 printf("\n\t'%s' val=%u", name
, v
->val
);
261 case BTF_KIND_ENUM64
: {
262 const struct btf_enum64
*v
= btf_enum64(t
);
263 __u16 vlen
= btf_vlen(t
);
264 const char *encoding
;
267 encoding
= btf_kflag(t
) ? "SIGNED" : "UNSIGNED";
269 jsonw_string_field(w
, "encoding", encoding
);
270 jsonw_uint_field(w
, "size", t
->size
);
271 jsonw_uint_field(w
, "vlen", vlen
);
272 jsonw_name(w
, "values");
273 jsonw_start_array(w
);
275 printf(" encoding=%s size=%u vlen=%u", encoding
, t
->size
, vlen
);
277 for (i
= 0; i
< vlen
; i
++, v
++) {
278 const char *name
= btf_str(btf
, v
->name_off
);
279 __u64 val
= ((__u64
)v
->val_hi32
<< 32) | v
->val_lo32
;
282 jsonw_start_object(w
);
283 jsonw_string_field(w
, "name", name
);
285 jsonw_int_field(w
, "val", val
);
287 jsonw_uint_field(w
, "val", val
);
291 printf("\n\t'%s' val=%lldLL", name
,
294 printf("\n\t'%s' val=%lluULL", name
,
295 (unsigned long long)val
);
303 const char *fwd_kind
= BTF_INFO_KFLAG(t
->info
) ? "union"
307 jsonw_string_field(w
, "fwd_kind", fwd_kind
);
309 printf(" fwd_kind=%s", fwd_kind
);
312 case BTF_KIND_FUNC
: {
313 const char *linkage
= btf_func_linkage_str(t
);
316 jsonw_uint_field(w
, "type_id", t
->type
);
317 jsonw_string_field(w
, "linkage", linkage
);
319 printf(" type_id=%u linkage=%s", t
->type
, linkage
);
323 case BTF_KIND_FUNC_PROTO
: {
324 const struct btf_param
*p
= (const void *)(t
+ 1);
325 __u16 vlen
= BTF_INFO_VLEN(t
->info
);
329 jsonw_uint_field(w
, "ret_type_id", t
->type
);
330 jsonw_uint_field(w
, "vlen", vlen
);
331 jsonw_name(w
, "params");
332 jsonw_start_array(w
);
334 printf(" ret_type_id=%u vlen=%u", t
->type
, vlen
);
336 for (i
= 0; i
< vlen
; i
++, p
++) {
337 const char *name
= btf_str(btf
, p
->name_off
);
340 jsonw_start_object(w
);
341 jsonw_string_field(w
, "name", name
);
342 jsonw_uint_field(w
, "type_id", p
->type
);
345 printf("\n\t'%s' type_id=%u", name
, p
->type
);
353 const struct btf_var
*v
= (const void *)(t
+ 1);
356 linkage
= btf_var_linkage_str(v
->linkage
);
359 jsonw_uint_field(w
, "type_id", t
->type
);
360 jsonw_string_field(w
, "linkage", linkage
);
362 printf(" type_id=%u, linkage=%s", t
->type
, linkage
);
366 case BTF_KIND_DATASEC
: {
367 const struct btf_var_secinfo
*v
= (const void *)(t
+ 1);
368 const struct btf_type
*vt
;
369 __u16 vlen
= BTF_INFO_VLEN(t
->info
);
373 jsonw_uint_field(w
, "size", t
->size
);
374 jsonw_uint_field(w
, "vlen", vlen
);
375 jsonw_name(w
, "vars");
376 jsonw_start_array(w
);
378 printf(" size=%u vlen=%u", t
->size
, vlen
);
380 for (i
= 0; i
< vlen
; i
++, v
++) {
382 jsonw_start_object(w
);
383 jsonw_uint_field(w
, "type_id", v
->type
);
384 jsonw_uint_field(w
, "offset", v
->offset
);
385 jsonw_uint_field(w
, "size", v
->size
);
388 printf("\n\ttype_id=%u offset=%u size=%u",
389 v
->type
, v
->offset
, v
->size
);
391 if (v
->type
< btf__type_cnt(btf
)) {
392 vt
= btf__type_by_id(btf
, v
->type
);
394 btf_kind_str
[btf_kind_safe(btf_kind(vt
))],
395 btf_str(btf
, vt
->name_off
));
403 case BTF_KIND_FLOAT
: {
405 jsonw_uint_field(w
, "size", t
->size
);
407 printf(" size=%u", t
->size
);
410 case BTF_KIND_DECL_TAG
: {
411 const struct btf_decl_tag
*tag
= (const void *)(t
+ 1);
414 jsonw_uint_field(w
, "type_id", t
->type
);
415 jsonw_int_field(w
, "component_idx", tag
->component_idx
);
417 printf(" type_id=%u component_idx=%d", t
->type
, tag
->component_idx
);
426 jsonw_end_object(json_wtr
);
433 static int dump_btf_raw(const struct btf
*btf
,
434 __u32
*root_type_ids
, int root_type_cnt
)
436 const struct btf_type
*t
;
440 jsonw_start_object(json_wtr
);
441 jsonw_name(json_wtr
, "types");
442 jsonw_start_array(json_wtr
);
446 for (i
= 0; i
< root_type_cnt
; i
++) {
447 t
= btf__type_by_id(btf
, root_type_ids
[i
]);
448 dump_btf_type(btf
, root_type_ids
[i
], t
);
451 const struct btf
*base
;
452 int cnt
= btf__type_cnt(btf
);
455 base
= btf__base_btf(btf
);
457 start_id
= btf__type_cnt(base
);
459 for (i
= start_id
; i
< cnt
; i
++) {
460 t
= btf__type_by_id(btf
, i
);
461 dump_btf_type(btf
, i
, t
);
466 jsonw_end_array(json_wtr
);
467 jsonw_end_object(json_wtr
);
478 static int ptr_array_push(const void *ptr
, struct ptr_array
*arr
)
483 if (arr
->cnt
== arr
->cap
) {
484 new_cap
= (arr
->cap
?: 16) * 2;
485 tmp
= realloc(arr
->elems
, sizeof(*arr
->elems
) * new_cap
);
491 arr
->elems
[arr
->cnt
++] = ptr
;
495 static void ptr_array_free(struct ptr_array
*arr
)
500 static int cmp_kfuncs(const void *pa
, const void *pb
, void *ctx
)
502 struct btf
*btf
= ctx
;
503 const struct btf_type
*a
= *(void **)pa
;
504 const struct btf_type
*b
= *(void **)pb
;
506 return strcmp(btf__str_by_offset(btf
, a
->name_off
),
507 btf__str_by_offset(btf
, b
->name_off
));
510 static int dump_btf_kfuncs(struct btf_dump
*d
, const struct btf
*btf
)
512 LIBBPF_OPTS(btf_dump_emit_type_decl_opts
, opts
);
513 __u32 cnt
= btf__type_cnt(btf
), i
, j
;
514 struct ptr_array fastcalls
= {};
515 struct ptr_array kfuncs
= {};
518 printf("\n/* BPF kfuncs */\n");
519 printf("#ifndef BPF_NO_KFUNC_PROTOTYPES\n");
521 for (i
= 1; i
< cnt
; i
++) {
522 const struct btf_type
*t
= btf__type_by_id(btf
, i
);
523 const struct btf_type
*ft
;
526 if (!btf_is_decl_tag(t
))
529 if (btf_decl_tag(t
)->component_idx
!= -1)
532 ft
= btf__type_by_id(btf
, t
->type
);
533 if (!btf_is_func(ft
))
536 name
= btf__name_by_offset(btf
, t
->name_off
);
537 if (strncmp(name
, KFUNC_DECL_TAG
, sizeof(KFUNC_DECL_TAG
)) == 0) {
538 err
= ptr_array_push(ft
, &kfuncs
);
543 if (strncmp(name
, FASTCALL_DECL_TAG
, sizeof(FASTCALL_DECL_TAG
)) == 0) {
544 err
= ptr_array_push(ft
, &fastcalls
);
550 /* Sort kfuncs by name for improved vmlinux.h stability */
551 qsort_r(kfuncs
.elems
, kfuncs
.cnt
, sizeof(*kfuncs
.elems
), cmp_kfuncs
, (void *)btf
);
552 for (i
= 0; i
< kfuncs
.cnt
; i
++) {
553 const struct btf_type
*t
= kfuncs
.elems
[i
];
557 /* Assume small amount of fastcall kfuncs */
558 for (j
= 0; j
< fastcalls
.cnt
; j
++) {
559 if (fastcalls
.elems
[j
] == t
) {
560 printf("__bpf_fastcall ");
565 opts
.field_name
= btf__name_by_offset(btf
, t
->name_off
);
566 err
= btf_dump__emit_type_decl(d
, t
->type
, &opts
);
570 printf(" __weak __ksym;\n");
573 printf("#endif\n\n");
576 ptr_array_free(&fastcalls
);
577 ptr_array_free(&kfuncs
);
581 static void __printf(2, 0) btf_dump_printf(void *ctx
,
582 const char *fmt
, va_list args
)
584 vfprintf(stdout
, fmt
, args
);
587 static int btf_type_rank(const struct btf
*btf
, __u32 index
, bool has_name
)
589 const struct btf_type
*t
= btf__type_by_id(btf
, index
);
590 const int kind
= btf_kind(t
);
591 const int max_rank
= 10;
598 case BTF_KIND_ENUM64
:
599 return has_name
? 1 : 0;
603 case BTF_KIND_STRUCT
:
605 return has_name
? 3 : max_rank
;
606 case BTF_KIND_FUNC_PROTO
:
607 return has_name
? 4 : max_rank
;
610 return btf_type_rank(btf
, btf_array(t
)->type
, has_name
);
612 case BTF_KIND_TYPE_TAG
:
615 case BTF_KIND_VOLATILE
:
616 case BTF_KIND_RESTRICT
:
617 case BTF_KIND_TYPEDEF
:
618 case BTF_KIND_DECL_TAG
:
620 return btf_type_rank(btf
, t
->type
, has_name
);
627 static const char *btf_type_sort_name(const struct btf
*btf
, __u32 index
, bool from_ref
)
629 const struct btf_type
*t
= btf__type_by_id(btf
, index
);
631 switch (btf_kind(t
)) {
633 case BTF_KIND_ENUM64
: {
634 int name_off
= t
->name_off
;
636 if (!from_ref
&& !name_off
&& btf_vlen(t
))
637 name_off
= btf_kind(t
) == BTF_KIND_ENUM64
?
638 btf_enum64(t
)->name_off
:
639 btf_enum(t
)->name_off
;
641 return btf__name_by_offset(btf
, name_off
);
644 return btf_type_sort_name(btf
, btf_array(t
)->type
, true);
645 case BTF_KIND_TYPE_TAG
:
648 case BTF_KIND_VOLATILE
:
649 case BTF_KIND_RESTRICT
:
650 case BTF_KIND_TYPEDEF
:
651 case BTF_KIND_DECL_TAG
:
652 return btf_type_sort_name(btf
, t
->type
, true);
654 return btf__name_by_offset(btf
, t
->name_off
);
659 static __u64
hasher(__u64 hash
, __u64 val
)
661 return hash
* 31 + val
;
664 static __u64
btf_name_hasher(__u64 hash
, const struct btf
*btf
, __u32 name_off
)
669 return hasher(hash
, str_hash(btf__name_by_offset(btf
, name_off
)));
672 static __u64
btf_type_disambig_hash(const struct btf
*btf
, __u32 id
, bool include_members
)
674 const struct btf_type
*t
= btf__type_by_id(btf
, id
);
678 hash
= btf_name_hasher(hash
, btf
, t
->name_off
);
680 switch (btf_kind(t
)) {
682 case BTF_KIND_ENUM64
:
683 for (i
= 0; i
< btf_vlen(t
); i
++) {
684 __u32 name_off
= btf_is_enum(t
) ?
685 btf_enum(t
)[i
].name_off
:
686 btf_enum64(t
)[i
].name_off
;
688 hash
= btf_name_hasher(hash
, btf
, name_off
);
691 case BTF_KIND_STRUCT
:
693 if (!include_members
)
695 for (i
= 0; i
< btf_vlen(t
); i
++) {
696 const struct btf_member
*m
= btf_members(t
) + i
;
698 hash
= btf_name_hasher(hash
, btf
, m
->name_off
);
699 /* resolve field type's name and hash it as well */
700 hash
= hasher(hash
, btf_type_disambig_hash(btf
, m
->type
, false));
703 case BTF_KIND_TYPE_TAG
:
706 case BTF_KIND_VOLATILE
:
707 case BTF_KIND_RESTRICT
:
708 case BTF_KIND_TYPEDEF
:
709 case BTF_KIND_DECL_TAG
:
710 hash
= hasher(hash
, btf_type_disambig_hash(btf
, t
->type
, include_members
));
712 case BTF_KIND_ARRAY
: {
713 struct btf_array
*arr
= btf_array(t
);
715 hash
= hasher(hash
, arr
->nelems
);
716 hash
= hasher(hash
, btf_type_disambig_hash(btf
, arr
->type
, include_members
));
725 static int btf_type_compare(const void *left
, const void *right
)
727 const struct sort_datum
*d1
= (const struct sort_datum
*)left
;
728 const struct sort_datum
*d2
= (const struct sort_datum
*)right
;
731 r
= d1
->type_rank
- d2
->type_rank
;
732 r
= r
?: strcmp(d1
->sort_name
, d2
->sort_name
);
733 r
= r
?: strcmp(d1
->own_name
, d2
->own_name
);
737 if (d1
->disambig_hash
!= d2
->disambig_hash
)
738 return d1
->disambig_hash
< d2
->disambig_hash
? -1 : 1;
740 return d1
->index
- d2
->index
;
743 static struct sort_datum
*sort_btf_c(const struct btf
*btf
)
745 struct sort_datum
*datums
;
748 n
= btf__type_cnt(btf
);
749 datums
= malloc(sizeof(struct sort_datum
) * n
);
753 for (int i
= 0; i
< n
; ++i
) {
754 struct sort_datum
*d
= datums
+ i
;
755 const struct btf_type
*t
= btf__type_by_id(btf
, i
);
758 d
->type_rank
= btf_type_rank(btf
, i
, false);
759 d
->sort_name
= btf_type_sort_name(btf
, i
, false);
760 d
->own_name
= btf__name_by_offset(btf
, t
->name_off
);
761 d
->disambig_hash
= btf_type_disambig_hash(btf
, i
, true);
764 qsort(datums
, n
, sizeof(struct sort_datum
), btf_type_compare
);
769 static int dump_btf_c(const struct btf
*btf
,
770 __u32
*root_type_ids
, int root_type_cnt
, bool sort_dump
)
772 struct sort_datum
*datums
= NULL
;
776 d
= btf_dump__new(btf
, btf_dump_printf
, NULL
, NULL
);
780 printf("#ifndef __VMLINUX_H__\n");
781 printf("#define __VMLINUX_H__\n");
783 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
784 printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
785 printf("#endif\n\n");
786 printf("#ifndef __ksym\n");
787 printf("#define __ksym __attribute__((section(\".ksyms\")))\n");
788 printf("#endif\n\n");
789 printf("#ifndef __weak\n");
790 printf("#define __weak __attribute__((weak))\n");
791 printf("#endif\n\n");
792 printf("#ifndef __bpf_fastcall\n");
793 printf("#if __has_attribute(bpf_fastcall)\n");
794 printf("#define __bpf_fastcall __attribute__((bpf_fastcall))\n");
796 printf("#define __bpf_fastcall\n");
798 printf("#endif\n\n");
801 for (i
= 0; i
< root_type_cnt
; i
++) {
802 err
= btf_dump__dump_type(d
, root_type_ids
[i
]);
807 int cnt
= btf__type_cnt(btf
);
810 datums
= sort_btf_c(btf
);
811 for (i
= 1; i
< cnt
; i
++) {
812 int idx
= datums
? datums
[i
].index
: i
;
814 err
= btf_dump__dump_type(d
, idx
);
819 err
= dump_btf_kfuncs(d
, btf
);
824 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
825 printf("#pragma clang attribute pop\n");
828 printf("#endif /* __VMLINUX_H__ */\n");
836 static const char sysfs_vmlinux
[] = "/sys/kernel/btf/vmlinux";
838 static struct btf
*get_vmlinux_btf_from_sysfs(void)
842 base
= btf__parse(sysfs_vmlinux
, NULL
);
844 p_err("failed to parse vmlinux BTF at '%s': %d\n",
845 sysfs_vmlinux
, -errno
);
850 #define BTF_NAME_BUFF_LEN 64
852 static bool btf_is_kernel_module(__u32 btf_id
)
854 struct bpf_btf_info btf_info
= {};
855 char btf_name
[BTF_NAME_BUFF_LEN
];
860 btf_fd
= bpf_btf_get_fd_by_id(btf_id
);
862 p_err("can't get BTF object by id (%u): %s", btf_id
, strerror(errno
));
866 len
= sizeof(btf_info
);
867 btf_info
.name
= ptr_to_u64(btf_name
);
868 btf_info
.name_len
= sizeof(btf_name
);
869 err
= bpf_btf_get_info_by_fd(btf_fd
, &btf_info
, &len
);
872 p_err("can't get BTF (ID %u) object info: %s", btf_id
, strerror(errno
));
876 return btf_info
.kernel_btf
&& strncmp(btf_name
, "vmlinux", sizeof(btf_name
)) != 0;
879 static int do_dump(int argc
, char **argv
)
881 bool dump_c
= false, sort_dump_c
= true;
882 struct btf
*btf
= NULL
, *base
= NULL
;
883 __u32 root_type_ids
[2];
884 int root_type_cnt
= 0;
895 if (is_prefix(src
, "map")) {
896 struct bpf_map_info info
= {};
897 __u32 len
= sizeof(info
);
904 fd
= map_parse_fd_and_info(&argc
, &argv
, &info
, &len
);
908 btf_id
= info
.btf_id
;
909 if (argc
&& is_prefix(*argv
, "key")) {
910 root_type_ids
[root_type_cnt
++] = info
.btf_key_type_id
;
912 } else if (argc
&& is_prefix(*argv
, "value")) {
913 root_type_ids
[root_type_cnt
++] = info
.btf_value_type_id
;
915 } else if (argc
&& is_prefix(*argv
, "all")) {
917 } else if (argc
&& is_prefix(*argv
, "kv")) {
918 root_type_ids
[root_type_cnt
++] = info
.btf_key_type_id
;
919 root_type_ids
[root_type_cnt
++] = info
.btf_value_type_id
;
922 root_type_ids
[root_type_cnt
++] = info
.btf_key_type_id
;
923 root_type_ids
[root_type_cnt
++] = info
.btf_value_type_id
;
925 } else if (is_prefix(src
, "prog")) {
926 struct bpf_prog_info info
= {};
927 __u32 len
= sizeof(info
);
934 fd
= prog_parse_fd(&argc
, &argv
);
938 err
= bpf_prog_get_info_by_fd(fd
, &info
, &len
);
940 p_err("can't get prog info: %s", strerror(errno
));
944 btf_id
= info
.btf_id
;
945 } else if (is_prefix(src
, "id")) {
948 btf_id
= strtoul(*argv
, &endptr
, 0);
950 p_err("can't parse %s as ID", *argv
);
954 } else if (is_prefix(src
, "file")) {
955 const char sysfs_prefix
[] = "/sys/kernel/btf/";
958 strncmp(*argv
, sysfs_prefix
, sizeof(sysfs_prefix
) - 1) == 0 &&
959 strcmp(*argv
, sysfs_vmlinux
) != 0)
960 base
= get_vmlinux_btf_from_sysfs();
962 btf
= btf__parse_split(*argv
, base
?: base_btf
);
965 p_err("failed to load BTF from %s: %s",
966 *argv
, strerror(errno
));
972 p_err("unrecognized BTF source specifier: '%s'", src
);
977 if (is_prefix(*argv
, "format")) {
980 p_err("expecting value for 'format' option\n");
984 if (strcmp(*argv
, "c") == 0) {
986 } else if (strcmp(*argv
, "raw") == 0) {
989 p_err("unrecognized format specifier: '%s', possible values: raw, c",
995 } else if (is_prefix(*argv
, "unsorted")) {
999 p_err("unrecognized option: '%s'", *argv
);
1006 if (!base_btf
&& btf_is_kernel_module(btf_id
)) {
1007 p_info("Warning: valid base BTF was not specified with -B option, falling back to standard base BTF (%s)",
1009 base_btf
= get_vmlinux_btf_from_sysfs();
1012 btf
= btf__load_from_kernel_by_id_split(btf_id
, base_btf
);
1015 p_err("get btf by id (%u): %s", btf_id
, strerror(errno
));
1022 p_err("JSON output for C-syntax dump is not supported");
1026 err
= dump_btf_c(btf
, root_type_ids
, root_type_cnt
, sort_dump_c
);
1028 err
= dump_btf_raw(btf
, root_type_ids
, root_type_cnt
);
1038 static int btf_parse_fd(int *argc
, char ***argv
)
1044 if (!is_prefix(*argv
[0], "id")) {
1045 p_err("expected 'id', got: '%s'?", **argv
);
1050 id
= strtoul(**argv
, &endptr
, 0);
1052 p_err("can't parse %s as ID", **argv
);
1057 fd
= bpf_btf_get_fd_by_id(id
);
1059 p_err("can't get BTF object by id (%u): %s",
1060 id
, strerror(errno
));
1066 build_btf_type_table(struct hashmap
*tab
, enum bpf_obj_type type
,
1067 void *info
, __u32
*len
)
1069 static const char * const names
[] = {
1070 [BPF_OBJ_UNKNOWN
] = "unknown",
1071 [BPF_OBJ_PROG
] = "prog",
1072 [BPF_OBJ_MAP
] = "map",
1074 __u32 btf_id
, id
= 0;
1081 err
= bpf_prog_get_next_id(id
, &id
);
1084 err
= bpf_map_get_next_id(id
, &id
);
1088 p_err("unexpected object type: %d", type
);
1092 if (errno
== ENOENT
) {
1096 p_err("can't get next %s: %s%s", names
[type
],
1098 errno
== EINVAL
? " -- kernel too old?" : "");
1104 fd
= bpf_prog_get_fd_by_id(id
);
1107 fd
= bpf_map_get_fd_by_id(id
);
1111 p_err("unexpected object type: %d", type
);
1115 if (errno
== ENOENT
)
1117 p_err("can't get %s by id (%u): %s", names
[type
], id
,
1123 memset(info
, 0, *len
);
1124 if (type
== BPF_OBJ_PROG
)
1125 err
= bpf_prog_get_info_by_fd(fd
, info
, len
);
1127 err
= bpf_map_get_info_by_fd(fd
, info
, len
);
1130 p_err("can't get %s info: %s", names
[type
],
1137 btf_id
= ((struct bpf_prog_info
*)info
)->btf_id
;
1140 btf_id
= ((struct bpf_map_info
*)info
)->btf_id
;
1144 p_err("unexpected object type: %d", type
);
1150 err
= hashmap__append(tab
, btf_id
, id
);
1152 p_err("failed to append entry to hashmap for BTF ID %u, object ID %u: %s",
1153 btf_id
, id
, strerror(-err
));
1166 build_btf_tables(struct hashmap
*btf_prog_table
,
1167 struct hashmap
*btf_map_table
)
1169 struct bpf_prog_info prog_info
;
1170 __u32 prog_len
= sizeof(prog_info
);
1171 struct bpf_map_info map_info
;
1172 __u32 map_len
= sizeof(map_info
);
1175 err
= build_btf_type_table(btf_prog_table
, BPF_OBJ_PROG
, &prog_info
,
1180 err
= build_btf_type_table(btf_map_table
, BPF_OBJ_MAP
, &map_info
,
1183 hashmap__free(btf_prog_table
);
1191 show_btf_plain(struct bpf_btf_info
*info
, int fd
,
1192 struct hashmap
*btf_prog_table
,
1193 struct hashmap
*btf_map_table
)
1195 struct hashmap_entry
*entry
;
1196 const char *name
= u64_to_ptr(info
->name
);
1199 printf("%u: ", info
->id
);
1200 if (info
->kernel_btf
)
1201 printf("name [%s] ", name
);
1202 else if (name
&& name
[0])
1203 printf("name %s ", name
);
1205 printf("name <anon> ");
1206 printf("size %uB", info
->btf_size
);
1209 hashmap__for_each_key_entry(btf_prog_table
, entry
, info
->id
) {
1210 printf("%s%lu", n
++ == 0 ? " prog_ids " : ",", entry
->value
);
1214 hashmap__for_each_key_entry(btf_map_table
, entry
, info
->id
) {
1215 printf("%s%lu", n
++ == 0 ? " map_ids " : ",", entry
->value
);
1218 emit_obj_refs_plain(refs_table
, info
->id
, "\n\tpids ");
1224 show_btf_json(struct bpf_btf_info
*info
, int fd
,
1225 struct hashmap
*btf_prog_table
,
1226 struct hashmap
*btf_map_table
)
1228 struct hashmap_entry
*entry
;
1229 const char *name
= u64_to_ptr(info
->name
);
1231 jsonw_start_object(json_wtr
); /* btf object */
1232 jsonw_uint_field(json_wtr
, "id", info
->id
);
1233 jsonw_uint_field(json_wtr
, "size", info
->btf_size
);
1235 jsonw_name(json_wtr
, "prog_ids");
1236 jsonw_start_array(json_wtr
); /* prog_ids */
1237 hashmap__for_each_key_entry(btf_prog_table
, entry
, info
->id
) {
1238 jsonw_uint(json_wtr
, entry
->value
);
1240 jsonw_end_array(json_wtr
); /* prog_ids */
1242 jsonw_name(json_wtr
, "map_ids");
1243 jsonw_start_array(json_wtr
); /* map_ids */
1244 hashmap__for_each_key_entry(btf_map_table
, entry
, info
->id
) {
1245 jsonw_uint(json_wtr
, entry
->value
);
1247 jsonw_end_array(json_wtr
); /* map_ids */
1249 emit_obj_refs_json(refs_table
, info
->id
, json_wtr
); /* pids */
1251 jsonw_bool_field(json_wtr
, "kernel", info
->kernel_btf
);
1253 if (name
&& name
[0])
1254 jsonw_string_field(json_wtr
, "name", name
);
1256 jsonw_end_object(json_wtr
); /* btf object */
1260 show_btf(int fd
, struct hashmap
*btf_prog_table
,
1261 struct hashmap
*btf_map_table
)
1263 struct bpf_btf_info info
;
1264 __u32 len
= sizeof(info
);
1268 memset(&info
, 0, sizeof(info
));
1269 err
= bpf_btf_get_info_by_fd(fd
, &info
, &len
);
1271 p_err("can't get BTF object info: %s", strerror(errno
));
1274 /* if kernel support emitting BTF object name, pass name pointer */
1275 if (info
.name_len
) {
1276 memset(&info
, 0, sizeof(info
));
1277 info
.name_len
= sizeof(name
);
1278 info
.name
= ptr_to_u64(name
);
1281 err
= bpf_btf_get_info_by_fd(fd
, &info
, &len
);
1283 p_err("can't get BTF object info: %s", strerror(errno
));
1289 show_btf_json(&info
, fd
, btf_prog_table
, btf_map_table
);
1291 show_btf_plain(&info
, fd
, btf_prog_table
, btf_map_table
);
1296 static int do_show(int argc
, char **argv
)
1298 struct hashmap
*btf_prog_table
;
1299 struct hashmap
*btf_map_table
;
1304 fd
= btf_parse_fd(&argc
, &argv
);
1315 btf_prog_table
= hashmap__new(hash_fn_for_key_as_id
,
1316 equal_fn_for_key_as_id
, NULL
);
1317 btf_map_table
= hashmap__new(hash_fn_for_key_as_id
,
1318 equal_fn_for_key_as_id
, NULL
);
1319 if (IS_ERR(btf_prog_table
) || IS_ERR(btf_map_table
)) {
1320 hashmap__free(btf_prog_table
);
1321 hashmap__free(btf_map_table
);
1324 p_err("failed to create hashmap for object references");
1327 err
= build_btf_tables(btf_prog_table
, btf_map_table
);
1333 build_obj_refs_table(&refs_table
, BPF_OBJ_BTF
);
1336 err
= show_btf(fd
, btf_prog_table
, btf_map_table
);
1342 jsonw_start_array(json_wtr
); /* root array */
1345 err
= bpf_btf_get_next_id(id
, &id
);
1347 if (errno
== ENOENT
) {
1351 p_err("can't get next BTF object: %s%s",
1353 errno
== EINVAL
? " -- kernel too old?" : "");
1358 fd
= bpf_btf_get_fd_by_id(id
);
1360 if (errno
== ENOENT
)
1362 p_err("can't get BTF object by id (%u): %s",
1363 id
, strerror(errno
));
1368 err
= show_btf(fd
, btf_prog_table
, btf_map_table
);
1375 jsonw_end_array(json_wtr
); /* root array */
1378 hashmap__free(btf_prog_table
);
1379 hashmap__free(btf_map_table
);
1380 delete_obj_refs_table(refs_table
);
1385 static int do_help(int argc
, char **argv
)
1388 jsonw_null(json_wtr
);
1393 "Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
1394 " %1$s %2$s dump BTF_SRC [format FORMAT]\n"
1397 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
1398 " FORMAT := { raw | c [unsorted] }\n"
1399 " " HELP_SPEC_MAP
"\n"
1400 " " HELP_SPEC_PROGRAM
"\n"
1401 " " HELP_SPEC_OPTIONS
" |\n"
1402 " {-B|--base-btf} }\n"
1409 static const struct cmd cmds
[] = {
1410 { "show", do_show
},
1411 { "list", do_show
},
1412 { "help", do_help
},
1413 { "dump", do_dump
},
1417 int do_btf(int argc
, char **argv
)
1419 return cmd_select(cmds
, argc
, argv
, do_help
);