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",
41 struct btf_attach_table {
42 DECLARE_HASHTABLE(table, 16);
45 struct btf_attach_point {
48 struct hlist_node hash;
51 static const char *btf_int_enc_str(__u8 encoding)
67 static const char *btf_var_linkage_str(__u32 linkage)
72 case BTF_VAR_GLOBAL_ALLOCATED:
73 return "global-alloc";
79 static const char *btf_func_linkage_str(const struct btf_type *t)
81 switch (btf_vlen(t)) {
93 static const char *btf_str(const struct btf *btf, __u32 off)
97 return btf__name_by_offset(btf, off) ? : "(invalid)";
100 static int dump_btf_type(const struct btf *btf, __u32 id,
101 const struct btf_type *t)
103 json_writer_t *w = json_wtr;
106 kind = BTF_INFO_KIND(t->info);
107 safe_kind = kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
110 jsonw_start_object(w);
111 jsonw_uint_field(w, "id", id);
112 jsonw_string_field(w, "kind", btf_kind_str[safe_kind]);
113 jsonw_string_field(w, "name", btf_str(btf, t->name_off));
115 printf("[%u] %s '%s'", id, btf_kind_str[safe_kind],
116 btf_str(btf, t->name_off));
119 switch (BTF_INFO_KIND(t->info)) {
121 __u32 v = *(__u32 *)(t + 1);
124 enc = btf_int_enc_str(BTF_INT_ENCODING(v));
127 jsonw_uint_field(w, "size", t->size);
128 jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v));
129 jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v));
130 jsonw_string_field(w, "encoding", enc);
132 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
133 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
140 case BTF_KIND_VOLATILE:
141 case BTF_KIND_RESTRICT:
142 case BTF_KIND_TYPEDEF:
144 jsonw_uint_field(w, "type_id", t->type);
146 printf(" type_id=%u", t->type);
148 case BTF_KIND_ARRAY: {
149 const struct btf_array *arr = (const void *)(t + 1);
152 jsonw_uint_field(w, "type_id", arr->type);
153 jsonw_uint_field(w, "index_type_id", arr->index_type);
154 jsonw_uint_field(w, "nr_elems", arr->nelems);
156 printf(" type_id=%u index_type_id=%u nr_elems=%u",
157 arr->type, arr->index_type, arr->nelems);
161 case BTF_KIND_STRUCT:
162 case BTF_KIND_UNION: {
163 const struct btf_member *m = (const void *)(t + 1);
164 __u16 vlen = BTF_INFO_VLEN(t->info);
168 jsonw_uint_field(w, "size", t->size);
169 jsonw_uint_field(w, "vlen", vlen);
170 jsonw_name(w, "members");
171 jsonw_start_array(w);
173 printf(" size=%u vlen=%u", t->size, vlen);
175 for (i = 0; i < vlen; i++, m++) {
176 const char *name = btf_str(btf, m->name_off);
177 __u32 bit_off, bit_sz;
179 if (BTF_INFO_KFLAG(t->info)) {
180 bit_off = BTF_MEMBER_BIT_OFFSET(m->offset);
181 bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset);
188 jsonw_start_object(w);
189 jsonw_string_field(w, "name", name);
190 jsonw_uint_field(w, "type_id", m->type);
191 jsonw_uint_field(w, "bits_offset", bit_off);
193 jsonw_uint_field(w, "bitfield_size",
198 printf("\n\t'%s' type_id=%u bits_offset=%u",
199 name, m->type, bit_off);
201 printf(" bitfield_size=%u", bit_sz);
208 case BTF_KIND_ENUM: {
209 const struct btf_enum *v = (const void *)(t + 1);
210 __u16 vlen = BTF_INFO_VLEN(t->info);
214 jsonw_uint_field(w, "size", t->size);
215 jsonw_uint_field(w, "vlen", vlen);
216 jsonw_name(w, "values");
217 jsonw_start_array(w);
219 printf(" size=%u vlen=%u", t->size, vlen);
221 for (i = 0; i < vlen; i++, v++) {
222 const char *name = btf_str(btf, v->name_off);
225 jsonw_start_object(w);
226 jsonw_string_field(w, "name", name);
227 jsonw_uint_field(w, "val", v->val);
230 printf("\n\t'%s' val=%u", name, v->val);
238 const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
242 jsonw_string_field(w, "fwd_kind", fwd_kind);
244 printf(" fwd_kind=%s", fwd_kind);
247 case BTF_KIND_FUNC: {
248 const char *linkage = btf_func_linkage_str(t);
251 jsonw_uint_field(w, "type_id", t->type);
252 jsonw_string_field(w, "linkage", linkage);
254 printf(" type_id=%u linkage=%s", t->type, linkage);
258 case BTF_KIND_FUNC_PROTO: {
259 const struct btf_param *p = (const void *)(t + 1);
260 __u16 vlen = BTF_INFO_VLEN(t->info);
264 jsonw_uint_field(w, "ret_type_id", t->type);
265 jsonw_uint_field(w, "vlen", vlen);
266 jsonw_name(w, "params");
267 jsonw_start_array(w);
269 printf(" ret_type_id=%u vlen=%u", t->type, vlen);
271 for (i = 0; i < vlen; i++, p++) {
272 const char *name = btf_str(btf, p->name_off);
275 jsonw_start_object(w);
276 jsonw_string_field(w, "name", name);
277 jsonw_uint_field(w, "type_id", p->type);
280 printf("\n\t'%s' type_id=%u", name, p->type);
288 const struct btf_var *v = (const void *)(t + 1);
291 linkage = btf_var_linkage_str(v->linkage);
294 jsonw_uint_field(w, "type_id", t->type);
295 jsonw_string_field(w, "linkage", linkage);
297 printf(" type_id=%u, linkage=%s", t->type, linkage);
301 case BTF_KIND_DATASEC: {
302 const struct btf_var_secinfo *v = (const void *)(t+1);
303 __u16 vlen = BTF_INFO_VLEN(t->info);
307 jsonw_uint_field(w, "size", t->size);
308 jsonw_uint_field(w, "vlen", vlen);
309 jsonw_name(w, "vars");
310 jsonw_start_array(w);
312 printf(" size=%u vlen=%u", t->size, vlen);
314 for (i = 0; i < vlen; i++, v++) {
316 jsonw_start_object(w);
317 jsonw_uint_field(w, "type_id", v->type);
318 jsonw_uint_field(w, "offset", v->offset);
319 jsonw_uint_field(w, "size", v->size);
322 printf("\n\ttype_id=%u offset=%u size=%u",
323 v->type, v->offset, v->size);
335 jsonw_end_object(json_wtr);
342 static int dump_btf_raw(const struct btf *btf,
343 __u32 *root_type_ids, int root_type_cnt)
345 const struct btf_type *t;
349 jsonw_start_object(json_wtr);
350 jsonw_name(json_wtr, "types");
351 jsonw_start_array(json_wtr);
355 for (i = 0; i < root_type_cnt; i++) {
356 t = btf__type_by_id(btf, root_type_ids[i]);
357 dump_btf_type(btf, root_type_ids[i], t);
360 int cnt = btf__get_nr_types(btf);
362 for (i = 1; i <= cnt; i++) {
363 t = btf__type_by_id(btf, i);
364 dump_btf_type(btf, i, t);
369 jsonw_end_array(json_wtr);
370 jsonw_end_object(json_wtr);
375 static void __printf(2, 0) btf_dump_printf(void *ctx,
376 const char *fmt, va_list args)
378 vfprintf(stdout, fmt, args);
381 static int dump_btf_c(const struct btf *btf,
382 __u32 *root_type_ids, int root_type_cnt)
387 d = btf_dump__new(btf, NULL, NULL, btf_dump_printf);
391 printf("#ifndef __VMLINUX_H__\n");
392 printf("#define __VMLINUX_H__\n");
394 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
395 printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
396 printf("#endif\n\n");
399 for (i = 0; i < root_type_cnt; i++) {
400 err = btf_dump__dump_type(d, root_type_ids[i]);
405 int cnt = btf__get_nr_types(btf);
407 for (i = 1; i <= cnt; i++) {
408 err = btf_dump__dump_type(d, i);
414 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
415 printf("#pragma clang attribute pop\n");
418 printf("#endif /* __VMLINUX_H__ */\n");
425 static struct btf *btf__parse_raw(const char *file)
435 f = fopen(file, "rb");
439 buf = malloc(st.st_size);
441 btf = ERR_PTR(-ENOMEM);
445 if ((size_t) st.st_size != fread(buf, 1, st.st_size, f)) {
446 btf = ERR_PTR(-EINVAL);
450 btf = btf__new(buf, st.st_size);
459 static bool is_btf_raw(const char *file)
464 fd = open(file, O_RDONLY);
468 nb_read = read(fd, &magic, sizeof(magic));
470 return nb_read == sizeof(magic) && magic == BTF_MAGIC;
473 static int do_dump(int argc, char **argv)
475 struct btf *btf = NULL;
476 __u32 root_type_ids[2];
477 int root_type_cnt = 0;
490 if (is_prefix(src, "map")) {
491 struct bpf_map_info info = {};
492 __u32 len = sizeof(info);
499 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
503 btf_id = info.btf_id;
504 if (argc && is_prefix(*argv, "key")) {
505 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
507 } else if (argc && is_prefix(*argv, "value")) {
508 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
510 } else if (argc && is_prefix(*argv, "all")) {
512 } else if (argc && is_prefix(*argv, "kv")) {
513 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
514 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
517 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
518 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
520 } else if (is_prefix(src, "prog")) {
521 struct bpf_prog_info info = {};
522 __u32 len = sizeof(info);
529 fd = prog_parse_fd(&argc, &argv);
533 err = bpf_obj_get_info_by_fd(fd, &info, &len);
535 p_err("can't get prog info: %s", strerror(errno));
539 btf_id = info.btf_id;
540 } else if (is_prefix(src, "id")) {
543 btf_id = strtoul(*argv, &endptr, 0);
545 p_err("can't parse %s as ID", *argv);
549 } else if (is_prefix(src, "file")) {
550 if (is_btf_raw(*argv))
551 btf = btf__parse_raw(*argv);
553 btf = btf__parse_elf(*argv, NULL);
558 p_err("failed to load BTF from %s: %s",
559 *argv, strerror(err));
565 p_err("unrecognized BTF source specifier: '%s'", src);
570 if (is_prefix(*argv, "format")) {
573 p_err("expecting value for 'format' option\n");
576 if (strcmp(*argv, "c") == 0) {
578 } else if (strcmp(*argv, "raw") == 0) {
581 p_err("unrecognized format specifier: '%s', possible values: raw, c",
587 p_err("unrecognized option: '%s'", *argv);
593 err = btf__get_from_id(btf_id, &btf);
595 p_err("get btf by id (%u): %s", btf_id, strerror(err));
600 p_err("can't find btf with ID (%u)", btf_id);
607 p_err("JSON output for C-syntax dump is not supported");
611 err = dump_btf_c(btf, root_type_ids, root_type_cnt);
613 err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
622 static int btf_parse_fd(int *argc, char ***argv)
628 if (!is_prefix(*argv[0], "id")) {
629 p_err("expected 'id', got: '%s'?", **argv);
634 id = strtoul(**argv, &endptr, 0);
636 p_err("can't parse %s as ID", **argv);
641 fd = bpf_btf_get_fd_by_id(id);
643 p_err("can't get BTF object by id (%u): %s",
644 id, strerror(errno));
649 static void delete_btf_table(struct btf_attach_table *tab)
651 struct btf_attach_point *obj;
652 struct hlist_node *tmp;
656 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
657 hash_del(&obj->hash);
663 build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type,
664 void *info, __u32 *len)
666 static const char * const names[] = {
667 [BPF_OBJ_UNKNOWN] = "unknown",
668 [BPF_OBJ_PROG] = "prog",
669 [BPF_OBJ_MAP] = "map",
671 struct btf_attach_point *obj_node;
672 __u32 btf_id, id = 0;
679 err = bpf_prog_get_next_id(id, &id);
682 err = bpf_map_get_next_id(id, &id);
686 p_err("unexpected object type: %d", type);
690 if (errno == ENOENT) {
694 p_err("can't get next %s: %s%s", names[type],
696 errno == EINVAL ? " -- kernel too old?" : "");
702 fd = bpf_prog_get_fd_by_id(id);
705 fd = bpf_map_get_fd_by_id(id);
709 p_err("unexpected object type: %d", type);
715 p_err("can't get %s by id (%u): %s", names[type], id,
721 memset(info, 0, *len);
722 err = bpf_obj_get_info_by_fd(fd, info, len);
725 p_err("can't get %s info: %s", names[type],
732 btf_id = ((struct bpf_prog_info *)info)->btf_id;
735 btf_id = ((struct bpf_map_info *)info)->btf_id;
739 p_err("unexpected object type: %d", type);
745 obj_node = calloc(1, sizeof(*obj_node));
747 p_err("failed to allocate memory: %s", strerror(errno));
751 obj_node->obj_id = id;
752 obj_node->btf_id = btf_id;
753 hash_add(tab->table, &obj_node->hash, obj_node->btf_id);
759 delete_btf_table(tab);
764 build_btf_tables(struct btf_attach_table *btf_prog_table,
765 struct btf_attach_table *btf_map_table)
767 struct bpf_prog_info prog_info;
768 __u32 prog_len = sizeof(prog_info);
769 struct bpf_map_info map_info;
770 __u32 map_len = sizeof(map_info);
773 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
778 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
781 delete_btf_table(btf_prog_table);
789 show_btf_plain(struct bpf_btf_info *info, int fd,
790 struct btf_attach_table *btf_prog_table,
791 struct btf_attach_table *btf_map_table)
793 struct btf_attach_point *obj;
796 printf("%u: ", info->id);
797 printf("size %uB", info->btf_size);
800 hash_for_each_possible(btf_prog_table->table, obj, hash, info->id) {
801 if (obj->btf_id == info->id)
802 printf("%s%u", n++ == 0 ? " prog_ids " : ",",
807 hash_for_each_possible(btf_map_table->table, obj, hash, info->id) {
808 if (obj->btf_id == info->id)
809 printf("%s%u", n++ == 0 ? " map_ids " : ",",
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;
823 jsonw_start_object(json_wtr); /* btf object */
824 jsonw_uint_field(json_wtr, "id", info->id);
825 jsonw_uint_field(json_wtr, "size", info->btf_size);
827 jsonw_name(json_wtr, "prog_ids");
828 jsonw_start_array(json_wtr); /* prog_ids */
829 hash_for_each_possible(btf_prog_table->table, obj, hash,
831 if (obj->btf_id == info->id)
832 jsonw_uint(json_wtr, obj->obj_id);
834 jsonw_end_array(json_wtr); /* prog_ids */
836 jsonw_name(json_wtr, "map_ids");
837 jsonw_start_array(json_wtr); /* map_ids */
838 hash_for_each_possible(btf_map_table->table, obj, hash,
840 if (obj->btf_id == info->id)
841 jsonw_uint(json_wtr, obj->obj_id);
843 jsonw_end_array(json_wtr); /* map_ids */
844 jsonw_end_object(json_wtr); /* btf object */
848 show_btf(int fd, struct btf_attach_table *btf_prog_table,
849 struct btf_attach_table *btf_map_table)
851 struct bpf_btf_info info = {};
852 __u32 len = sizeof(info);
855 err = bpf_obj_get_info_by_fd(fd, &info, &len);
857 p_err("can't get BTF object info: %s", strerror(errno));
862 show_btf_json(&info, fd, btf_prog_table, btf_map_table);
864 show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
869 static int do_show(int argc, char **argv)
871 struct btf_attach_table btf_prog_table;
872 struct btf_attach_table btf_map_table;
877 fd = btf_parse_fd(&argc, &argv);
888 hash_init(btf_prog_table.table);
889 hash_init(btf_map_table.table);
890 err = build_btf_tables(&btf_prog_table, &btf_map_table);
898 err = show_btf(fd, &btf_prog_table, &btf_map_table);
904 jsonw_start_array(json_wtr); /* root array */
907 err = bpf_btf_get_next_id(id, &id);
909 if (errno == ENOENT) {
913 p_err("can't get next BTF object: %s%s",
915 errno == EINVAL ? " -- kernel too old?" : "");
920 fd = bpf_btf_get_fd_by_id(id);
924 p_err("can't get BTF object by id (%u): %s",
925 id, strerror(errno));
930 err = show_btf(fd, &btf_prog_table, &btf_map_table);
937 jsonw_end_array(json_wtr); /* root array */
940 delete_btf_table(&btf_prog_table);
941 delete_btf_table(&btf_map_table);
946 static int do_help(int argc, char **argv)
949 jsonw_null(json_wtr);
954 "Usage: %s btf { show | list } [id BTF_ID]\n"
955 " %s btf dump BTF_SRC [format FORMAT]\n"
958 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
959 " FORMAT := { raw | c }\n"
960 " " HELP_SPEC_MAP "\n"
961 " " HELP_SPEC_PROGRAM "\n"
962 " " HELP_SPEC_OPTIONS "\n"
964 bin_name, bin_name, bin_name);
969 static const struct cmd cmds[] = {
977 int do_btf(int argc, char **argv)
979 return cmd_select(cmds, argc, argv, do_help);