1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Facebook */
13 #include <bpf/libbpf.h>
14 #include <linux/btf.h>
15 #include <linux/hashtable.h>
16 #include <sys/types.h>
19 #include "json_writer.h"
22 static const char * const btf_kind_str[NR_BTF_KINDS] = {
23 [BTF_KIND_UNKN] = "UNKNOWN",
24 [BTF_KIND_INT] = "INT",
25 [BTF_KIND_PTR] = "PTR",
26 [BTF_KIND_ARRAY] = "ARRAY",
27 [BTF_KIND_STRUCT] = "STRUCT",
28 [BTF_KIND_UNION] = "UNION",
29 [BTF_KIND_ENUM] = "ENUM",
30 [BTF_KIND_FWD] = "FWD",
31 [BTF_KIND_TYPEDEF] = "TYPEDEF",
32 [BTF_KIND_VOLATILE] = "VOLATILE",
33 [BTF_KIND_CONST] = "CONST",
34 [BTF_KIND_RESTRICT] = "RESTRICT",
35 [BTF_KIND_FUNC] = "FUNC",
36 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
37 [BTF_KIND_VAR] = "VAR",
38 [BTF_KIND_DATASEC] = "DATASEC",
39 [BTF_KIND_FLOAT] = "FLOAT",
42 struct btf_attach_table {
43 DECLARE_HASHTABLE(table, 16);
46 struct btf_attach_point {
49 struct hlist_node hash;
52 static const char *btf_int_enc_str(__u8 encoding)
68 static const char *btf_var_linkage_str(__u32 linkage)
73 case BTF_VAR_GLOBAL_ALLOCATED:
75 case BTF_VAR_GLOBAL_EXTERN:
82 static const char *btf_func_linkage_str(const struct btf_type *t)
84 switch (btf_vlen(t)) {
96 static const char *btf_str(const struct btf *btf, __u32 off)
100 return btf__name_by_offset(btf, off) ? : "(invalid)";
103 static int btf_kind_safe(int kind)
105 return kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
108 static int dump_btf_type(const struct btf *btf, __u32 id,
109 const struct btf_type *t)
111 json_writer_t *w = json_wtr;
112 int kind = btf_kind(t);
115 jsonw_start_object(w);
116 jsonw_uint_field(w, "id", id);
117 jsonw_string_field(w, "kind", btf_kind_str[btf_kind_safe(kind)]);
118 jsonw_string_field(w, "name", btf_str(btf, t->name_off));
120 printf("[%u] %s '%s'", id, btf_kind_str[btf_kind_safe(kind)],
121 btf_str(btf, t->name_off));
126 __u32 v = *(__u32 *)(t + 1);
129 enc = btf_int_enc_str(BTF_INT_ENCODING(v));
132 jsonw_uint_field(w, "size", t->size);
133 jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v));
134 jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v));
135 jsonw_string_field(w, "encoding", enc);
137 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
138 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
145 case BTF_KIND_VOLATILE:
146 case BTF_KIND_RESTRICT:
147 case BTF_KIND_TYPEDEF:
149 jsonw_uint_field(w, "type_id", t->type);
151 printf(" type_id=%u", t->type);
153 case BTF_KIND_ARRAY: {
154 const struct btf_array *arr = (const void *)(t + 1);
157 jsonw_uint_field(w, "type_id", arr->type);
158 jsonw_uint_field(w, "index_type_id", arr->index_type);
159 jsonw_uint_field(w, "nr_elems", arr->nelems);
161 printf(" type_id=%u index_type_id=%u nr_elems=%u",
162 arr->type, arr->index_type, arr->nelems);
166 case BTF_KIND_STRUCT:
167 case BTF_KIND_UNION: {
168 const struct btf_member *m = (const void *)(t + 1);
169 __u16 vlen = BTF_INFO_VLEN(t->info);
173 jsonw_uint_field(w, "size", t->size);
174 jsonw_uint_field(w, "vlen", vlen);
175 jsonw_name(w, "members");
176 jsonw_start_array(w);
178 printf(" size=%u vlen=%u", t->size, vlen);
180 for (i = 0; i < vlen; i++, m++) {
181 const char *name = btf_str(btf, m->name_off);
182 __u32 bit_off, bit_sz;
184 if (BTF_INFO_KFLAG(t->info)) {
185 bit_off = BTF_MEMBER_BIT_OFFSET(m->offset);
186 bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset);
193 jsonw_start_object(w);
194 jsonw_string_field(w, "name", name);
195 jsonw_uint_field(w, "type_id", m->type);
196 jsonw_uint_field(w, "bits_offset", bit_off);
198 jsonw_uint_field(w, "bitfield_size",
203 printf("\n\t'%s' type_id=%u bits_offset=%u",
204 name, m->type, bit_off);
206 printf(" bitfield_size=%u", bit_sz);
213 case BTF_KIND_ENUM: {
214 const struct btf_enum *v = (const void *)(t + 1);
215 __u16 vlen = BTF_INFO_VLEN(t->info);
219 jsonw_uint_field(w, "size", t->size);
220 jsonw_uint_field(w, "vlen", vlen);
221 jsonw_name(w, "values");
222 jsonw_start_array(w);
224 printf(" size=%u vlen=%u", t->size, vlen);
226 for (i = 0; i < vlen; i++, v++) {
227 const char *name = btf_str(btf, v->name_off);
230 jsonw_start_object(w);
231 jsonw_string_field(w, "name", name);
232 jsonw_uint_field(w, "val", v->val);
235 printf("\n\t'%s' val=%u", name, v->val);
243 const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
247 jsonw_string_field(w, "fwd_kind", fwd_kind);
249 printf(" fwd_kind=%s", fwd_kind);
252 case BTF_KIND_FUNC: {
253 const char *linkage = btf_func_linkage_str(t);
256 jsonw_uint_field(w, "type_id", t->type);
257 jsonw_string_field(w, "linkage", linkage);
259 printf(" type_id=%u linkage=%s", t->type, linkage);
263 case BTF_KIND_FUNC_PROTO: {
264 const struct btf_param *p = (const void *)(t + 1);
265 __u16 vlen = BTF_INFO_VLEN(t->info);
269 jsonw_uint_field(w, "ret_type_id", t->type);
270 jsonw_uint_field(w, "vlen", vlen);
271 jsonw_name(w, "params");
272 jsonw_start_array(w);
274 printf(" ret_type_id=%u vlen=%u", t->type, vlen);
276 for (i = 0; i < vlen; i++, p++) {
277 const char *name = btf_str(btf, p->name_off);
280 jsonw_start_object(w);
281 jsonw_string_field(w, "name", name);
282 jsonw_uint_field(w, "type_id", p->type);
285 printf("\n\t'%s' type_id=%u", name, p->type);
293 const struct btf_var *v = (const void *)(t + 1);
296 linkage = btf_var_linkage_str(v->linkage);
299 jsonw_uint_field(w, "type_id", t->type);
300 jsonw_string_field(w, "linkage", linkage);
302 printf(" type_id=%u, linkage=%s", t->type, linkage);
306 case BTF_KIND_DATASEC: {
307 const struct btf_var_secinfo *v = (const void *)(t + 1);
308 const struct btf_type *vt;
309 __u16 vlen = BTF_INFO_VLEN(t->info);
313 jsonw_uint_field(w, "size", t->size);
314 jsonw_uint_field(w, "vlen", vlen);
315 jsonw_name(w, "vars");
316 jsonw_start_array(w);
318 printf(" size=%u vlen=%u", t->size, vlen);
320 for (i = 0; i < vlen; i++, v++) {
322 jsonw_start_object(w);
323 jsonw_uint_field(w, "type_id", v->type);
324 jsonw_uint_field(w, "offset", v->offset);
325 jsonw_uint_field(w, "size", v->size);
328 printf("\n\ttype_id=%u offset=%u size=%u",
329 v->type, v->offset, v->size);
331 if (v->type <= btf__get_nr_types(btf)) {
332 vt = btf__type_by_id(btf, v->type);
334 btf_kind_str[btf_kind_safe(btf_kind(vt))],
335 btf_str(btf, vt->name_off));
343 case BTF_KIND_FLOAT: {
345 jsonw_uint_field(w, "size", t->size);
347 printf(" size=%u", t->size);
355 jsonw_end_object(json_wtr);
362 static int dump_btf_raw(const struct btf *btf,
363 __u32 *root_type_ids, int root_type_cnt)
365 const struct btf_type *t;
369 jsonw_start_object(json_wtr);
370 jsonw_name(json_wtr, "types");
371 jsonw_start_array(json_wtr);
375 for (i = 0; i < root_type_cnt; i++) {
376 t = btf__type_by_id(btf, root_type_ids[i]);
377 dump_btf_type(btf, root_type_ids[i], t);
380 const struct btf *base;
381 int cnt = btf__get_nr_types(btf);
384 base = btf__base_btf(btf);
386 start_id = btf__get_nr_types(base) + 1;
388 for (i = start_id; i <= cnt; i++) {
389 t = btf__type_by_id(btf, i);
390 dump_btf_type(btf, i, t);
395 jsonw_end_array(json_wtr);
396 jsonw_end_object(json_wtr);
401 static void __printf(2, 0) btf_dump_printf(void *ctx,
402 const char *fmt, va_list args)
404 vfprintf(stdout, fmt, args);
407 static int dump_btf_c(const struct btf *btf,
408 __u32 *root_type_ids, int root_type_cnt)
413 d = btf_dump__new(btf, NULL, NULL, btf_dump_printf);
417 printf("#ifndef __VMLINUX_H__\n");
418 printf("#define __VMLINUX_H__\n");
420 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
421 printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
422 printf("#endif\n\n");
425 for (i = 0; i < root_type_cnt; i++) {
426 err = btf_dump__dump_type(d, root_type_ids[i]);
431 int cnt = btf__get_nr_types(btf);
433 for (i = 1; i <= cnt; i++) {
434 err = btf_dump__dump_type(d, i);
440 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
441 printf("#pragma clang attribute pop\n");
444 printf("#endif /* __VMLINUX_H__ */\n");
451 static int do_dump(int argc, char **argv)
453 struct btf *btf = NULL, *base = NULL;
454 __u32 root_type_ids[2];
455 int root_type_cnt = 0;
467 if (is_prefix(src, "map")) {
468 struct bpf_map_info info = {};
469 __u32 len = sizeof(info);
476 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
480 btf_id = info.btf_id;
481 if (argc && is_prefix(*argv, "key")) {
482 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
484 } else if (argc && is_prefix(*argv, "value")) {
485 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
487 } else if (argc && is_prefix(*argv, "all")) {
489 } else if (argc && is_prefix(*argv, "kv")) {
490 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
491 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
494 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
495 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
497 } else if (is_prefix(src, "prog")) {
498 struct bpf_prog_info info = {};
499 __u32 len = sizeof(info);
506 fd = prog_parse_fd(&argc, &argv);
510 err = bpf_obj_get_info_by_fd(fd, &info, &len);
512 p_err("can't get prog info: %s", strerror(errno));
516 btf_id = info.btf_id;
517 } else if (is_prefix(src, "id")) {
520 btf_id = strtoul(*argv, &endptr, 0);
522 p_err("can't parse %s as ID", *argv);
526 } else if (is_prefix(src, "file")) {
527 const char sysfs_prefix[] = "/sys/kernel/btf/";
528 const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
531 strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
532 strcmp(*argv, sysfs_vmlinux) != 0) {
533 base = btf__parse(sysfs_vmlinux, NULL);
534 if (libbpf_get_error(base)) {
535 p_err("failed to parse vmlinux BTF at '%s': %ld\n",
536 sysfs_vmlinux, libbpf_get_error(base));
541 btf = btf__parse_split(*argv, base ?: base_btf);
545 p_err("failed to load BTF from %s: %s",
546 *argv, strerror(err));
552 p_err("unrecognized BTF source specifier: '%s'", src);
557 if (is_prefix(*argv, "format")) {
560 p_err("expecting value for 'format' option\n");
564 if (strcmp(*argv, "c") == 0) {
566 } else if (strcmp(*argv, "raw") == 0) {
569 p_err("unrecognized format specifier: '%s', possible values: raw, c",
576 p_err("unrecognized option: '%s'", *argv);
583 err = btf__get_from_id(btf_id, &btf);
585 p_err("get btf by id (%u): %s", btf_id, strerror(err));
590 p_err("can't find btf with ID (%u)", btf_id);
597 p_err("JSON output for C-syntax dump is not supported");
601 err = dump_btf_c(btf, root_type_ids, root_type_cnt);
603 err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
613 static int btf_parse_fd(int *argc, char ***argv)
619 if (!is_prefix(*argv[0], "id")) {
620 p_err("expected 'id', got: '%s'?", **argv);
625 id = strtoul(**argv, &endptr, 0);
627 p_err("can't parse %s as ID", **argv);
632 fd = bpf_btf_get_fd_by_id(id);
634 p_err("can't get BTF object by id (%u): %s",
635 id, strerror(errno));
640 static void delete_btf_table(struct btf_attach_table *tab)
642 struct btf_attach_point *obj;
643 struct hlist_node *tmp;
647 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
648 hash_del(&obj->hash);
654 build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type,
655 void *info, __u32 *len)
657 static const char * const names[] = {
658 [BPF_OBJ_UNKNOWN] = "unknown",
659 [BPF_OBJ_PROG] = "prog",
660 [BPF_OBJ_MAP] = "map",
662 struct btf_attach_point *obj_node;
663 __u32 btf_id, id = 0;
670 err = bpf_prog_get_next_id(id, &id);
673 err = bpf_map_get_next_id(id, &id);
677 p_err("unexpected object type: %d", type);
681 if (errno == ENOENT) {
685 p_err("can't get next %s: %s%s", names[type],
687 errno == EINVAL ? " -- kernel too old?" : "");
693 fd = bpf_prog_get_fd_by_id(id);
696 fd = bpf_map_get_fd_by_id(id);
700 p_err("unexpected object type: %d", type);
706 p_err("can't get %s by id (%u): %s", names[type], id,
712 memset(info, 0, *len);
713 err = bpf_obj_get_info_by_fd(fd, info, len);
716 p_err("can't get %s info: %s", names[type],
723 btf_id = ((struct bpf_prog_info *)info)->btf_id;
726 btf_id = ((struct bpf_map_info *)info)->btf_id;
730 p_err("unexpected object type: %d", type);
736 obj_node = calloc(1, sizeof(*obj_node));
738 p_err("failed to allocate memory: %s", strerror(errno));
743 obj_node->obj_id = id;
744 obj_node->btf_id = btf_id;
745 hash_add(tab->table, &obj_node->hash, obj_node->btf_id);
751 delete_btf_table(tab);
756 build_btf_tables(struct btf_attach_table *btf_prog_table,
757 struct btf_attach_table *btf_map_table)
759 struct bpf_prog_info prog_info;
760 __u32 prog_len = sizeof(prog_info);
761 struct bpf_map_info map_info;
762 __u32 map_len = sizeof(map_info);
765 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
770 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
773 delete_btf_table(btf_prog_table);
781 show_btf_plain(struct bpf_btf_info *info, int fd,
782 struct btf_attach_table *btf_prog_table,
783 struct btf_attach_table *btf_map_table)
785 struct btf_attach_point *obj;
786 const char *name = u64_to_ptr(info->name);
789 printf("%u: ", info->id);
790 if (info->kernel_btf)
791 printf("name [%s] ", name);
792 else if (name && name[0])
793 printf("name %s ", name);
795 printf("name <anon> ");
796 printf("size %uB", info->btf_size);
799 hash_for_each_possible(btf_prog_table->table, obj, hash, info->id) {
800 if (obj->btf_id == info->id)
801 printf("%s%u", n++ == 0 ? " prog_ids " : ",",
806 hash_for_each_possible(btf_map_table->table, obj, hash, info->id) {
807 if (obj->btf_id == info->id)
808 printf("%s%u", n++ == 0 ? " map_ids " : ",",
811 emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
817 show_btf_json(struct bpf_btf_info *info, int fd,
818 struct btf_attach_table *btf_prog_table,
819 struct btf_attach_table *btf_map_table)
821 struct btf_attach_point *obj;
822 const char *name = u64_to_ptr(info->name);
824 jsonw_start_object(json_wtr); /* btf object */
825 jsonw_uint_field(json_wtr, "id", info->id);
826 jsonw_uint_field(json_wtr, "size", info->btf_size);
828 jsonw_name(json_wtr, "prog_ids");
829 jsonw_start_array(json_wtr); /* prog_ids */
830 hash_for_each_possible(btf_prog_table->table, obj, hash,
832 if (obj->btf_id == info->id)
833 jsonw_uint(json_wtr, obj->obj_id);
835 jsonw_end_array(json_wtr); /* prog_ids */
837 jsonw_name(json_wtr, "map_ids");
838 jsonw_start_array(json_wtr); /* map_ids */
839 hash_for_each_possible(btf_map_table->table, obj, hash,
841 if (obj->btf_id == info->id)
842 jsonw_uint(json_wtr, obj->obj_id);
844 jsonw_end_array(json_wtr); /* map_ids */
846 emit_obj_refs_json(&refs_table, info->id, json_wtr); /* pids */
848 jsonw_bool_field(json_wtr, "kernel", info->kernel_btf);
851 jsonw_string_field(json_wtr, "name", name);
853 jsonw_end_object(json_wtr); /* btf object */
857 show_btf(int fd, struct btf_attach_table *btf_prog_table,
858 struct btf_attach_table *btf_map_table)
860 struct bpf_btf_info info;
861 __u32 len = sizeof(info);
865 memset(&info, 0, sizeof(info));
866 err = bpf_obj_get_info_by_fd(fd, &info, &len);
868 p_err("can't get BTF object info: %s", strerror(errno));
871 /* if kernel support emitting BTF object name, pass name pointer */
873 memset(&info, 0, sizeof(info));
874 info.name_len = sizeof(name);
875 info.name = ptr_to_u64(name);
878 err = bpf_obj_get_info_by_fd(fd, &info, &len);
880 p_err("can't get BTF object info: %s", strerror(errno));
886 show_btf_json(&info, fd, btf_prog_table, btf_map_table);
888 show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
893 static int do_show(int argc, char **argv)
895 struct btf_attach_table btf_prog_table;
896 struct btf_attach_table btf_map_table;
901 fd = btf_parse_fd(&argc, &argv);
912 hash_init(btf_prog_table.table);
913 hash_init(btf_map_table.table);
914 err = build_btf_tables(&btf_prog_table, &btf_map_table);
920 build_obj_refs_table(&refs_table, BPF_OBJ_BTF);
923 err = show_btf(fd, &btf_prog_table, &btf_map_table);
929 jsonw_start_array(json_wtr); /* root array */
932 err = bpf_btf_get_next_id(id, &id);
934 if (errno == ENOENT) {
938 p_err("can't get next BTF object: %s%s",
940 errno == EINVAL ? " -- kernel too old?" : "");
945 fd = bpf_btf_get_fd_by_id(id);
949 p_err("can't get BTF object by id (%u): %s",
950 id, strerror(errno));
955 err = show_btf(fd, &btf_prog_table, &btf_map_table);
962 jsonw_end_array(json_wtr); /* root array */
965 delete_btf_table(&btf_prog_table);
966 delete_btf_table(&btf_map_table);
967 delete_obj_refs_table(&refs_table);
972 static int do_help(int argc, char **argv)
975 jsonw_null(json_wtr);
980 "Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
981 " %1$s %2$s dump BTF_SRC [format FORMAT]\n"
984 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
985 " FORMAT := { raw | c }\n"
986 " " HELP_SPEC_MAP "\n"
987 " " HELP_SPEC_PROGRAM "\n"
988 " " HELP_SPEC_OPTIONS "\n"
995 static const struct cmd cmds[] = {
1003 int do_btf(int argc, char **argv)
1005 return cmd_select(cmds, argc, argv, do_help);