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:
74 return "global-alloc";
80 static const char *btf_func_linkage_str(const struct btf_type *t)
82 switch (btf_vlen(t)) {
94 static const char *btf_str(const struct btf *btf, __u32 off)
98 return btf__name_by_offset(btf, off) ? : "(invalid)";
101 static int dump_btf_type(const struct btf *btf, __u32 id,
102 const struct btf_type *t)
104 json_writer_t *w = json_wtr;
107 kind = BTF_INFO_KIND(t->info);
108 safe_kind = kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
111 jsonw_start_object(w);
112 jsonw_uint_field(w, "id", id);
113 jsonw_string_field(w, "kind", btf_kind_str[safe_kind]);
114 jsonw_string_field(w, "name", btf_str(btf, t->name_off));
116 printf("[%u] %s '%s'", id, btf_kind_str[safe_kind],
117 btf_str(btf, t->name_off));
120 switch (BTF_INFO_KIND(t->info)) {
122 __u32 v = *(__u32 *)(t + 1);
125 enc = btf_int_enc_str(BTF_INT_ENCODING(v));
128 jsonw_uint_field(w, "size", t->size);
129 jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v));
130 jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v));
131 jsonw_string_field(w, "encoding", enc);
133 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
134 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
141 case BTF_KIND_VOLATILE:
142 case BTF_KIND_RESTRICT:
143 case BTF_KIND_TYPEDEF:
145 jsonw_uint_field(w, "type_id", t->type);
147 printf(" type_id=%u", t->type);
149 case BTF_KIND_ARRAY: {
150 const struct btf_array *arr = (const void *)(t + 1);
153 jsonw_uint_field(w, "type_id", arr->type);
154 jsonw_uint_field(w, "index_type_id", arr->index_type);
155 jsonw_uint_field(w, "nr_elems", arr->nelems);
157 printf(" type_id=%u index_type_id=%u nr_elems=%u",
158 arr->type, arr->index_type, arr->nelems);
162 case BTF_KIND_STRUCT:
163 case BTF_KIND_UNION: {
164 const struct btf_member *m = (const void *)(t + 1);
165 __u16 vlen = BTF_INFO_VLEN(t->info);
169 jsonw_uint_field(w, "size", t->size);
170 jsonw_uint_field(w, "vlen", vlen);
171 jsonw_name(w, "members");
172 jsonw_start_array(w);
174 printf(" size=%u vlen=%u", t->size, vlen);
176 for (i = 0; i < vlen; i++, m++) {
177 const char *name = btf_str(btf, m->name_off);
178 __u32 bit_off, bit_sz;
180 if (BTF_INFO_KFLAG(t->info)) {
181 bit_off = BTF_MEMBER_BIT_OFFSET(m->offset);
182 bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset);
189 jsonw_start_object(w);
190 jsonw_string_field(w, "name", name);
191 jsonw_uint_field(w, "type_id", m->type);
192 jsonw_uint_field(w, "bits_offset", bit_off);
194 jsonw_uint_field(w, "bitfield_size",
199 printf("\n\t'%s' type_id=%u bits_offset=%u",
200 name, m->type, bit_off);
202 printf(" bitfield_size=%u", bit_sz);
209 case BTF_KIND_ENUM: {
210 const struct btf_enum *v = (const void *)(t + 1);
211 __u16 vlen = BTF_INFO_VLEN(t->info);
215 jsonw_uint_field(w, "size", t->size);
216 jsonw_uint_field(w, "vlen", vlen);
217 jsonw_name(w, "values");
218 jsonw_start_array(w);
220 printf(" size=%u vlen=%u", t->size, vlen);
222 for (i = 0; i < vlen; i++, v++) {
223 const char *name = btf_str(btf, v->name_off);
226 jsonw_start_object(w);
227 jsonw_string_field(w, "name", name);
228 jsonw_uint_field(w, "val", v->val);
231 printf("\n\t'%s' val=%u", name, v->val);
239 const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
243 jsonw_string_field(w, "fwd_kind", fwd_kind);
245 printf(" fwd_kind=%s", fwd_kind);
248 case BTF_KIND_FUNC: {
249 const char *linkage = btf_func_linkage_str(t);
252 jsonw_uint_field(w, "type_id", t->type);
253 jsonw_string_field(w, "linkage", linkage);
255 printf(" type_id=%u linkage=%s", t->type, linkage);
259 case BTF_KIND_FUNC_PROTO: {
260 const struct btf_param *p = (const void *)(t + 1);
261 __u16 vlen = BTF_INFO_VLEN(t->info);
265 jsonw_uint_field(w, "ret_type_id", t->type);
266 jsonw_uint_field(w, "vlen", vlen);
267 jsonw_name(w, "params");
268 jsonw_start_array(w);
270 printf(" ret_type_id=%u vlen=%u", t->type, vlen);
272 for (i = 0; i < vlen; i++, p++) {
273 const char *name = btf_str(btf, p->name_off);
276 jsonw_start_object(w);
277 jsonw_string_field(w, "name", name);
278 jsonw_uint_field(w, "type_id", p->type);
281 printf("\n\t'%s' type_id=%u", name, p->type);
289 const struct btf_var *v = (const void *)(t + 1);
292 linkage = btf_var_linkage_str(v->linkage);
295 jsonw_uint_field(w, "type_id", t->type);
296 jsonw_string_field(w, "linkage", linkage);
298 printf(" type_id=%u, linkage=%s", t->type, linkage);
302 case BTF_KIND_DATASEC: {
303 const struct btf_var_secinfo *v = (const void *)(t+1);
304 __u16 vlen = BTF_INFO_VLEN(t->info);
308 jsonw_uint_field(w, "size", t->size);
309 jsonw_uint_field(w, "vlen", vlen);
310 jsonw_name(w, "vars");
311 jsonw_start_array(w);
313 printf(" size=%u vlen=%u", t->size, vlen);
315 for (i = 0; i < vlen; i++, v++) {
317 jsonw_start_object(w);
318 jsonw_uint_field(w, "type_id", v->type);
319 jsonw_uint_field(w, "offset", v->offset);
320 jsonw_uint_field(w, "size", v->size);
323 printf("\n\ttype_id=%u offset=%u size=%u",
324 v->type, v->offset, v->size);
331 case BTF_KIND_FLOAT: {
333 jsonw_uint_field(w, "size", t->size);
335 printf(" size=%u", t->size);
343 jsonw_end_object(json_wtr);
350 static int dump_btf_raw(const struct btf *btf,
351 __u32 *root_type_ids, int root_type_cnt)
353 const struct btf_type *t;
357 jsonw_start_object(json_wtr);
358 jsonw_name(json_wtr, "types");
359 jsonw_start_array(json_wtr);
363 for (i = 0; i < root_type_cnt; i++) {
364 t = btf__type_by_id(btf, root_type_ids[i]);
365 dump_btf_type(btf, root_type_ids[i], t);
368 const struct btf *base;
369 int cnt = btf__get_nr_types(btf);
372 base = btf__base_btf(btf);
374 start_id = btf__get_nr_types(base) + 1;
376 for (i = start_id; i <= cnt; i++) {
377 t = btf__type_by_id(btf, i);
378 dump_btf_type(btf, i, t);
383 jsonw_end_array(json_wtr);
384 jsonw_end_object(json_wtr);
389 static void __printf(2, 0) btf_dump_printf(void *ctx,
390 const char *fmt, va_list args)
392 vfprintf(stdout, fmt, args);
395 static int dump_btf_c(const struct btf *btf,
396 __u32 *root_type_ids, int root_type_cnt)
401 d = btf_dump__new(btf, NULL, NULL, btf_dump_printf);
405 printf("#ifndef __VMLINUX_H__\n");
406 printf("#define __VMLINUX_H__\n");
408 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
409 printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
410 printf("#endif\n\n");
413 for (i = 0; i < root_type_cnt; i++) {
414 err = btf_dump__dump_type(d, root_type_ids[i]);
419 int cnt = btf__get_nr_types(btf);
421 for (i = 1; i <= cnt; i++) {
422 err = btf_dump__dump_type(d, i);
428 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
429 printf("#pragma clang attribute pop\n");
432 printf("#endif /* __VMLINUX_H__ */\n");
439 static int do_dump(int argc, char **argv)
441 struct btf *btf = NULL, *base = NULL;
442 __u32 root_type_ids[2];
443 int root_type_cnt = 0;
455 if (is_prefix(src, "map")) {
456 struct bpf_map_info info = {};
457 __u32 len = sizeof(info);
464 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
468 btf_id = info.btf_id;
469 if (argc && is_prefix(*argv, "key")) {
470 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
472 } else if (argc && is_prefix(*argv, "value")) {
473 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
475 } else if (argc && is_prefix(*argv, "all")) {
477 } else if (argc && is_prefix(*argv, "kv")) {
478 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
479 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
482 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
483 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
485 } else if (is_prefix(src, "prog")) {
486 struct bpf_prog_info info = {};
487 __u32 len = sizeof(info);
494 fd = prog_parse_fd(&argc, &argv);
498 err = bpf_obj_get_info_by_fd(fd, &info, &len);
500 p_err("can't get prog info: %s", strerror(errno));
504 btf_id = info.btf_id;
505 } else if (is_prefix(src, "id")) {
508 btf_id = strtoul(*argv, &endptr, 0);
510 p_err("can't parse %s as ID", *argv);
514 } else if (is_prefix(src, "file")) {
515 const char sysfs_prefix[] = "/sys/kernel/btf/";
516 const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
519 strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
520 strcmp(*argv, sysfs_vmlinux) != 0) {
521 base = btf__parse(sysfs_vmlinux, NULL);
522 if (libbpf_get_error(base)) {
523 p_err("failed to parse vmlinux BTF at '%s': %ld\n",
524 sysfs_vmlinux, libbpf_get_error(base));
529 btf = btf__parse_split(*argv, base ?: base_btf);
533 p_err("failed to load BTF from %s: %s",
534 *argv, strerror(err));
540 p_err("unrecognized BTF source specifier: '%s'", src);
545 if (is_prefix(*argv, "format")) {
548 p_err("expecting value for 'format' option\n");
552 if (strcmp(*argv, "c") == 0) {
554 } else if (strcmp(*argv, "raw") == 0) {
557 p_err("unrecognized format specifier: '%s', possible values: raw, c",
564 p_err("unrecognized option: '%s'", *argv);
571 err = btf__get_from_id(btf_id, &btf);
573 p_err("get btf by id (%u): %s", btf_id, strerror(err));
578 p_err("can't find btf with ID (%u)", btf_id);
585 p_err("JSON output for C-syntax dump is not supported");
589 err = dump_btf_c(btf, root_type_ids, root_type_cnt);
591 err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
601 static int btf_parse_fd(int *argc, char ***argv)
607 if (!is_prefix(*argv[0], "id")) {
608 p_err("expected 'id', got: '%s'?", **argv);
613 id = strtoul(**argv, &endptr, 0);
615 p_err("can't parse %s as ID", **argv);
620 fd = bpf_btf_get_fd_by_id(id);
622 p_err("can't get BTF object by id (%u): %s",
623 id, strerror(errno));
628 static void delete_btf_table(struct btf_attach_table *tab)
630 struct btf_attach_point *obj;
631 struct hlist_node *tmp;
635 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
636 hash_del(&obj->hash);
642 build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type,
643 void *info, __u32 *len)
645 static const char * const names[] = {
646 [BPF_OBJ_UNKNOWN] = "unknown",
647 [BPF_OBJ_PROG] = "prog",
648 [BPF_OBJ_MAP] = "map",
650 struct btf_attach_point *obj_node;
651 __u32 btf_id, id = 0;
658 err = bpf_prog_get_next_id(id, &id);
661 err = bpf_map_get_next_id(id, &id);
665 p_err("unexpected object type: %d", type);
669 if (errno == ENOENT) {
673 p_err("can't get next %s: %s%s", names[type],
675 errno == EINVAL ? " -- kernel too old?" : "");
681 fd = bpf_prog_get_fd_by_id(id);
684 fd = bpf_map_get_fd_by_id(id);
688 p_err("unexpected object type: %d", type);
694 p_err("can't get %s by id (%u): %s", names[type], id,
700 memset(info, 0, *len);
701 err = bpf_obj_get_info_by_fd(fd, info, len);
704 p_err("can't get %s info: %s", names[type],
711 btf_id = ((struct bpf_prog_info *)info)->btf_id;
714 btf_id = ((struct bpf_map_info *)info)->btf_id;
718 p_err("unexpected object type: %d", type);
724 obj_node = calloc(1, sizeof(*obj_node));
726 p_err("failed to allocate memory: %s", strerror(errno));
731 obj_node->obj_id = id;
732 obj_node->btf_id = btf_id;
733 hash_add(tab->table, &obj_node->hash, obj_node->btf_id);
739 delete_btf_table(tab);
744 build_btf_tables(struct btf_attach_table *btf_prog_table,
745 struct btf_attach_table *btf_map_table)
747 struct bpf_prog_info prog_info;
748 __u32 prog_len = sizeof(prog_info);
749 struct bpf_map_info map_info;
750 __u32 map_len = sizeof(map_info);
753 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
758 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
761 delete_btf_table(btf_prog_table);
769 show_btf_plain(struct bpf_btf_info *info, int fd,
770 struct btf_attach_table *btf_prog_table,
771 struct btf_attach_table *btf_map_table)
773 struct btf_attach_point *obj;
774 const char *name = u64_to_ptr(info->name);
777 printf("%u: ", info->id);
778 if (info->kernel_btf)
779 printf("name [%s] ", name);
780 else if (name && name[0])
781 printf("name %s ", name);
783 printf("name <anon> ");
784 printf("size %uB", info->btf_size);
787 hash_for_each_possible(btf_prog_table->table, obj, hash, info->id) {
788 if (obj->btf_id == info->id)
789 printf("%s%u", n++ == 0 ? " prog_ids " : ",",
794 hash_for_each_possible(btf_map_table->table, obj, hash, info->id) {
795 if (obj->btf_id == info->id)
796 printf("%s%u", n++ == 0 ? " map_ids " : ",",
799 emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
805 show_btf_json(struct bpf_btf_info *info, int fd,
806 struct btf_attach_table *btf_prog_table,
807 struct btf_attach_table *btf_map_table)
809 struct btf_attach_point *obj;
810 const char *name = u64_to_ptr(info->name);
812 jsonw_start_object(json_wtr); /* btf object */
813 jsonw_uint_field(json_wtr, "id", info->id);
814 jsonw_uint_field(json_wtr, "size", info->btf_size);
816 jsonw_name(json_wtr, "prog_ids");
817 jsonw_start_array(json_wtr); /* prog_ids */
818 hash_for_each_possible(btf_prog_table->table, obj, hash,
820 if (obj->btf_id == info->id)
821 jsonw_uint(json_wtr, obj->obj_id);
823 jsonw_end_array(json_wtr); /* prog_ids */
825 jsonw_name(json_wtr, "map_ids");
826 jsonw_start_array(json_wtr); /* map_ids */
827 hash_for_each_possible(btf_map_table->table, obj, hash,
829 if (obj->btf_id == info->id)
830 jsonw_uint(json_wtr, obj->obj_id);
832 jsonw_end_array(json_wtr); /* map_ids */
834 emit_obj_refs_json(&refs_table, info->id, json_wtr); /* pids */
836 jsonw_bool_field(json_wtr, "kernel", info->kernel_btf);
839 jsonw_string_field(json_wtr, "name", name);
841 jsonw_end_object(json_wtr); /* btf object */
845 show_btf(int fd, struct btf_attach_table *btf_prog_table,
846 struct btf_attach_table *btf_map_table)
848 struct bpf_btf_info info;
849 __u32 len = sizeof(info);
853 memset(&info, 0, sizeof(info));
854 err = bpf_obj_get_info_by_fd(fd, &info, &len);
856 p_err("can't get BTF object info: %s", strerror(errno));
859 /* if kernel support emitting BTF object name, pass name pointer */
861 memset(&info, 0, sizeof(info));
862 info.name_len = sizeof(name);
863 info.name = ptr_to_u64(name);
866 err = bpf_obj_get_info_by_fd(fd, &info, &len);
868 p_err("can't get BTF object info: %s", strerror(errno));
874 show_btf_json(&info, fd, btf_prog_table, btf_map_table);
876 show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
881 static int do_show(int argc, char **argv)
883 struct btf_attach_table btf_prog_table;
884 struct btf_attach_table btf_map_table;
889 fd = btf_parse_fd(&argc, &argv);
900 hash_init(btf_prog_table.table);
901 hash_init(btf_map_table.table);
902 err = build_btf_tables(&btf_prog_table, &btf_map_table);
908 build_obj_refs_table(&refs_table, BPF_OBJ_BTF);
911 err = show_btf(fd, &btf_prog_table, &btf_map_table);
917 jsonw_start_array(json_wtr); /* root array */
920 err = bpf_btf_get_next_id(id, &id);
922 if (errno == ENOENT) {
926 p_err("can't get next BTF object: %s%s",
928 errno == EINVAL ? " -- kernel too old?" : "");
933 fd = bpf_btf_get_fd_by_id(id);
937 p_err("can't get BTF object by id (%u): %s",
938 id, strerror(errno));
943 err = show_btf(fd, &btf_prog_table, &btf_map_table);
950 jsonw_end_array(json_wtr); /* root array */
953 delete_btf_table(&btf_prog_table);
954 delete_btf_table(&btf_map_table);
955 delete_obj_refs_table(&refs_table);
960 static int do_help(int argc, char **argv)
963 jsonw_null(json_wtr);
968 "Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
969 " %1$s %2$s dump BTF_SRC [format FORMAT]\n"
972 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
973 " FORMAT := { raw | c }\n"
974 " " HELP_SPEC_MAP "\n"
975 " " HELP_SPEC_PROGRAM "\n"
976 " " HELP_SPEC_OPTIONS "\n"
983 static const struct cmd cmds[] = {
991 int do_btf(int argc, char **argv)
993 return cmd_select(cmds, argc, argv, do_help);