1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Facebook */
13 #include <linux/btf.h>
14 #include <linux/hashtable.h>
15 #include <sys/types.h>
20 #include "json_writer.h"
23 static const char * const btf_kind_str[NR_BTF_KINDS] = {
24 [BTF_KIND_UNKN] = "UNKNOWN",
25 [BTF_KIND_INT] = "INT",
26 [BTF_KIND_PTR] = "PTR",
27 [BTF_KIND_ARRAY] = "ARRAY",
28 [BTF_KIND_STRUCT] = "STRUCT",
29 [BTF_KIND_UNION] = "UNION",
30 [BTF_KIND_ENUM] = "ENUM",
31 [BTF_KIND_FWD] = "FWD",
32 [BTF_KIND_TYPEDEF] = "TYPEDEF",
33 [BTF_KIND_VOLATILE] = "VOLATILE",
34 [BTF_KIND_CONST] = "CONST",
35 [BTF_KIND_RESTRICT] = "RESTRICT",
36 [BTF_KIND_FUNC] = "FUNC",
37 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
38 [BTF_KIND_VAR] = "VAR",
39 [BTF_KIND_DATASEC] = "DATASEC",
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_str(const struct btf *btf, __u32 off)
84 return btf__name_by_offset(btf, off) ? : "(invalid)";
87 static int dump_btf_type(const struct btf *btf, __u32 id,
88 const struct btf_type *t)
90 json_writer_t *w = json_wtr;
93 kind = BTF_INFO_KIND(t->info);
94 safe_kind = kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
97 jsonw_start_object(w);
98 jsonw_uint_field(w, "id", id);
99 jsonw_string_field(w, "kind", btf_kind_str[safe_kind]);
100 jsonw_string_field(w, "name", btf_str(btf, t->name_off));
102 printf("[%u] %s '%s'", id, btf_kind_str[safe_kind],
103 btf_str(btf, t->name_off));
106 switch (BTF_INFO_KIND(t->info)) {
108 __u32 v = *(__u32 *)(t + 1);
111 enc = btf_int_enc_str(BTF_INT_ENCODING(v));
114 jsonw_uint_field(w, "size", t->size);
115 jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v));
116 jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v));
117 jsonw_string_field(w, "encoding", enc);
119 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
120 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
127 case BTF_KIND_VOLATILE:
128 case BTF_KIND_RESTRICT:
129 case BTF_KIND_TYPEDEF:
131 jsonw_uint_field(w, "type_id", t->type);
133 printf(" type_id=%u", t->type);
135 case BTF_KIND_ARRAY: {
136 const struct btf_array *arr = (const void *)(t + 1);
139 jsonw_uint_field(w, "type_id", arr->type);
140 jsonw_uint_field(w, "index_type_id", arr->index_type);
141 jsonw_uint_field(w, "nr_elems", arr->nelems);
143 printf(" type_id=%u index_type_id=%u nr_elems=%u",
144 arr->type, arr->index_type, arr->nelems);
148 case BTF_KIND_STRUCT:
149 case BTF_KIND_UNION: {
150 const struct btf_member *m = (const void *)(t + 1);
151 __u16 vlen = BTF_INFO_VLEN(t->info);
155 jsonw_uint_field(w, "size", t->size);
156 jsonw_uint_field(w, "vlen", vlen);
157 jsonw_name(w, "members");
158 jsonw_start_array(w);
160 printf(" size=%u vlen=%u", t->size, vlen);
162 for (i = 0; i < vlen; i++, m++) {
163 const char *name = btf_str(btf, m->name_off);
164 __u32 bit_off, bit_sz;
166 if (BTF_INFO_KFLAG(t->info)) {
167 bit_off = BTF_MEMBER_BIT_OFFSET(m->offset);
168 bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset);
175 jsonw_start_object(w);
176 jsonw_string_field(w, "name", name);
177 jsonw_uint_field(w, "type_id", m->type);
178 jsonw_uint_field(w, "bits_offset", bit_off);
180 jsonw_uint_field(w, "bitfield_size",
185 printf("\n\t'%s' type_id=%u bits_offset=%u",
186 name, m->type, bit_off);
188 printf(" bitfield_size=%u", bit_sz);
195 case BTF_KIND_ENUM: {
196 const struct btf_enum *v = (const void *)(t + 1);
197 __u16 vlen = BTF_INFO_VLEN(t->info);
201 jsonw_uint_field(w, "size", t->size);
202 jsonw_uint_field(w, "vlen", vlen);
203 jsonw_name(w, "values");
204 jsonw_start_array(w);
206 printf(" size=%u vlen=%u", t->size, vlen);
208 for (i = 0; i < vlen; i++, v++) {
209 const char *name = btf_str(btf, v->name_off);
212 jsonw_start_object(w);
213 jsonw_string_field(w, "name", name);
214 jsonw_uint_field(w, "val", v->val);
217 printf("\n\t'%s' val=%u", name, v->val);
225 const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
229 jsonw_string_field(w, "fwd_kind", fwd_kind);
231 printf(" fwd_kind=%s", fwd_kind);
236 jsonw_uint_field(w, "type_id", t->type);
238 printf(" type_id=%u", t->type);
240 case BTF_KIND_FUNC_PROTO: {
241 const struct btf_param *p = (const void *)(t + 1);
242 __u16 vlen = BTF_INFO_VLEN(t->info);
246 jsonw_uint_field(w, "ret_type_id", t->type);
247 jsonw_uint_field(w, "vlen", vlen);
248 jsonw_name(w, "params");
249 jsonw_start_array(w);
251 printf(" ret_type_id=%u vlen=%u", t->type, vlen);
253 for (i = 0; i < vlen; i++, p++) {
254 const char *name = btf_str(btf, p->name_off);
257 jsonw_start_object(w);
258 jsonw_string_field(w, "name", name);
259 jsonw_uint_field(w, "type_id", p->type);
262 printf("\n\t'%s' type_id=%u", name, p->type);
270 const struct btf_var *v = (const void *)(t + 1);
273 linkage = btf_var_linkage_str(v->linkage);
276 jsonw_uint_field(w, "type_id", t->type);
277 jsonw_string_field(w, "linkage", linkage);
279 printf(" type_id=%u, linkage=%s", t->type, linkage);
283 case BTF_KIND_DATASEC: {
284 const struct btf_var_secinfo *v = (const void *)(t+1);
285 __u16 vlen = BTF_INFO_VLEN(t->info);
289 jsonw_uint_field(w, "size", t->size);
290 jsonw_uint_field(w, "vlen", vlen);
291 jsonw_name(w, "vars");
292 jsonw_start_array(w);
294 printf(" size=%u vlen=%u", t->size, vlen);
296 for (i = 0; i < vlen; i++, v++) {
298 jsonw_start_object(w);
299 jsonw_uint_field(w, "type_id", v->type);
300 jsonw_uint_field(w, "offset", v->offset);
301 jsonw_uint_field(w, "size", v->size);
304 printf("\n\ttype_id=%u offset=%u size=%u",
305 v->type, v->offset, v->size);
317 jsonw_end_object(json_wtr);
324 static int dump_btf_raw(const struct btf *btf,
325 __u32 *root_type_ids, int root_type_cnt)
327 const struct btf_type *t;
331 jsonw_start_object(json_wtr);
332 jsonw_name(json_wtr, "types");
333 jsonw_start_array(json_wtr);
337 for (i = 0; i < root_type_cnt; i++) {
338 t = btf__type_by_id(btf, root_type_ids[i]);
339 dump_btf_type(btf, root_type_ids[i], t);
342 int cnt = btf__get_nr_types(btf);
344 for (i = 1; i <= cnt; i++) {
345 t = btf__type_by_id(btf, i);
346 dump_btf_type(btf, i, t);
351 jsonw_end_array(json_wtr);
352 jsonw_end_object(json_wtr);
357 static void __printf(2, 0) btf_dump_printf(void *ctx,
358 const char *fmt, va_list args)
360 vfprintf(stdout, fmt, args);
363 static int dump_btf_c(const struct btf *btf,
364 __u32 *root_type_ids, int root_type_cnt)
369 d = btf_dump__new(btf, NULL, NULL, btf_dump_printf);
374 for (i = 0; i < root_type_cnt; i++) {
375 err = btf_dump__dump_type(d, root_type_ids[i]);
380 int cnt = btf__get_nr_types(btf);
382 for (i = 1; i <= cnt; i++) {
383 err = btf_dump__dump_type(d, i);
394 static struct btf *btf__parse_raw(const char *file)
404 f = fopen(file, "rb");
408 buf = malloc(st.st_size);
410 btf = ERR_PTR(-ENOMEM);
414 if ((size_t) st.st_size != fread(buf, 1, st.st_size, f)) {
415 btf = ERR_PTR(-EINVAL);
419 btf = btf__new(buf, st.st_size);
428 static bool is_btf_raw(const char *file)
433 fd = open(file, O_RDONLY);
437 nb_read = read(fd, &magic, sizeof(magic));
439 return nb_read == sizeof(magic) && magic == BTF_MAGIC;
442 static int do_dump(int argc, char **argv)
444 struct btf *btf = NULL;
445 __u32 root_type_ids[2];
446 int root_type_cnt = 0;
459 if (is_prefix(src, "map")) {
460 struct bpf_map_info info = {};
461 __u32 len = sizeof(info);
468 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
472 btf_id = info.btf_id;
473 if (argc && is_prefix(*argv, "key")) {
474 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
476 } else if (argc && is_prefix(*argv, "value")) {
477 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
479 } else if (argc && is_prefix(*argv, "all")) {
481 } else if (argc && is_prefix(*argv, "kv")) {
482 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
483 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
486 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
487 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
489 } else if (is_prefix(src, "prog")) {
490 struct bpf_prog_info info = {};
491 __u32 len = sizeof(info);
498 fd = prog_parse_fd(&argc, &argv);
502 err = bpf_obj_get_info_by_fd(fd, &info, &len);
504 p_err("can't get prog info: %s", strerror(errno));
508 btf_id = info.btf_id;
509 } else if (is_prefix(src, "id")) {
512 btf_id = strtoul(*argv, &endptr, 0);
514 p_err("can't parse %s as ID", *argv);
518 } else if (is_prefix(src, "file")) {
519 if (is_btf_raw(*argv))
520 btf = btf__parse_raw(*argv);
522 btf = btf__parse_elf(*argv, NULL);
527 p_err("failed to load BTF from %s: %s",
528 *argv, strerror(err));
534 p_err("unrecognized BTF source specifier: '%s'", src);
539 if (is_prefix(*argv, "format")) {
542 p_err("expecting value for 'format' option\n");
545 if (strcmp(*argv, "c") == 0) {
547 } else if (strcmp(*argv, "raw") == 0) {
550 p_err("unrecognized format specifier: '%s', possible values: raw, c",
556 p_err("unrecognized option: '%s'", *argv);
562 err = btf__get_from_id(btf_id, &btf);
564 p_err("get btf by id (%u): %s", btf_id, strerror(err));
569 p_err("can't find btf with ID (%u)", btf_id);
576 p_err("JSON output for C-syntax dump is not supported");
580 err = dump_btf_c(btf, root_type_ids, root_type_cnt);
582 err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
591 static int btf_parse_fd(int *argc, char ***argv)
597 if (!is_prefix(*argv[0], "id")) {
598 p_err("expected 'id', got: '%s'?", **argv);
603 id = strtoul(**argv, &endptr, 0);
605 p_err("can't parse %s as ID", **argv);
610 fd = bpf_btf_get_fd_by_id(id);
612 p_err("can't get BTF object by id (%u): %s",
613 id, strerror(errno));
618 static void delete_btf_table(struct btf_attach_table *tab)
620 struct btf_attach_point *obj;
621 struct hlist_node *tmp;
625 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
626 hash_del(&obj->hash);
632 build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type,
633 void *info, __u32 *len)
635 static const char * const names[] = {
636 [BPF_OBJ_UNKNOWN] = "unknown",
637 [BPF_OBJ_PROG] = "prog",
638 [BPF_OBJ_MAP] = "map",
640 struct btf_attach_point *obj_node;
641 __u32 btf_id, id = 0;
648 err = bpf_prog_get_next_id(id, &id);
651 err = bpf_map_get_next_id(id, &id);
655 p_err("unexpected object type: %d", type);
659 if (errno == ENOENT) {
663 p_err("can't get next %s: %s%s", names[type],
665 errno == EINVAL ? " -- kernel too old?" : "");
671 fd = bpf_prog_get_fd_by_id(id);
674 fd = bpf_map_get_fd_by_id(id);
678 p_err("unexpected object type: %d", type);
684 p_err("can't get %s by id (%u): %s", names[type], id,
690 memset(info, 0, *len);
691 err = bpf_obj_get_info_by_fd(fd, info, len);
694 p_err("can't get %s info: %s", names[type],
701 btf_id = ((struct bpf_prog_info *)info)->btf_id;
704 btf_id = ((struct bpf_map_info *)info)->btf_id;
708 p_err("unexpected object type: %d", type);
714 obj_node = calloc(1, sizeof(*obj_node));
716 p_err("failed to allocate memory: %s", strerror(errno));
720 obj_node->obj_id = id;
721 obj_node->btf_id = btf_id;
722 hash_add(tab->table, &obj_node->hash, obj_node->btf_id);
728 delete_btf_table(tab);
733 build_btf_tables(struct btf_attach_table *btf_prog_table,
734 struct btf_attach_table *btf_map_table)
736 struct bpf_prog_info prog_info;
737 __u32 prog_len = sizeof(prog_info);
738 struct bpf_map_info map_info;
739 __u32 map_len = sizeof(map_info);
742 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
747 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
750 delete_btf_table(btf_prog_table);
758 show_btf_plain(struct bpf_btf_info *info, int fd,
759 struct btf_attach_table *btf_prog_table,
760 struct btf_attach_table *btf_map_table)
762 struct btf_attach_point *obj;
765 printf("%u: ", info->id);
766 printf("size %uB", info->btf_size);
769 hash_for_each_possible(btf_prog_table->table, obj, hash, info->id) {
770 if (obj->btf_id == info->id)
771 printf("%s%u", n++ == 0 ? " prog_ids " : ",",
776 hash_for_each_possible(btf_map_table->table, obj, hash, info->id) {
777 if (obj->btf_id == info->id)
778 printf("%s%u", n++ == 0 ? " map_ids " : ",",
786 show_btf_json(struct bpf_btf_info *info, int fd,
787 struct btf_attach_table *btf_prog_table,
788 struct btf_attach_table *btf_map_table)
790 struct btf_attach_point *obj;
792 jsonw_start_object(json_wtr); /* btf object */
793 jsonw_uint_field(json_wtr, "id", info->id);
794 jsonw_uint_field(json_wtr, "size", info->btf_size);
796 jsonw_name(json_wtr, "prog_ids");
797 jsonw_start_array(json_wtr); /* prog_ids */
798 hash_for_each_possible(btf_prog_table->table, obj, hash,
800 if (obj->btf_id == info->id)
801 jsonw_uint(json_wtr, obj->obj_id);
803 jsonw_end_array(json_wtr); /* prog_ids */
805 jsonw_name(json_wtr, "map_ids");
806 jsonw_start_array(json_wtr); /* map_ids */
807 hash_for_each_possible(btf_map_table->table, obj, hash,
809 if (obj->btf_id == info->id)
810 jsonw_uint(json_wtr, obj->obj_id);
812 jsonw_end_array(json_wtr); /* map_ids */
813 jsonw_end_object(json_wtr); /* btf object */
817 show_btf(int fd, struct btf_attach_table *btf_prog_table,
818 struct btf_attach_table *btf_map_table)
820 struct bpf_btf_info info = {};
821 __u32 len = sizeof(info);
824 err = bpf_obj_get_info_by_fd(fd, &info, &len);
826 p_err("can't get BTF object info: %s", strerror(errno));
831 show_btf_json(&info, fd, btf_prog_table, btf_map_table);
833 show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
838 static int do_show(int argc, char **argv)
840 struct btf_attach_table btf_prog_table;
841 struct btf_attach_table btf_map_table;
846 fd = btf_parse_fd(&argc, &argv);
857 hash_init(btf_prog_table.table);
858 hash_init(btf_map_table.table);
859 err = build_btf_tables(&btf_prog_table, &btf_map_table);
867 err = show_btf(fd, &btf_prog_table, &btf_map_table);
873 jsonw_start_array(json_wtr); /* root array */
876 err = bpf_btf_get_next_id(id, &id);
878 if (errno == ENOENT) {
882 p_err("can't get next BTF object: %s%s",
884 errno == EINVAL ? " -- kernel too old?" : "");
889 fd = bpf_btf_get_fd_by_id(id);
893 p_err("can't get BTF object by id (%u): %s",
894 id, strerror(errno));
899 err = show_btf(fd, &btf_prog_table, &btf_map_table);
906 jsonw_end_array(json_wtr); /* root array */
909 delete_btf_table(&btf_prog_table);
910 delete_btf_table(&btf_map_table);
915 static int do_help(int argc, char **argv)
918 jsonw_null(json_wtr);
923 "Usage: %s btf { show | list } [id BTF_ID]\n"
924 " %s btf dump BTF_SRC [format FORMAT]\n"
927 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
928 " FORMAT := { raw | c }\n"
929 " " HELP_SPEC_MAP "\n"
930 " " HELP_SPEC_PROGRAM "\n"
931 " " HELP_SPEC_OPTIONS "\n"
933 bin_name, bin_name, bin_name);
938 static const struct cmd cmds[] = {
946 int do_btf(int argc, char **argv)
948 return cmd_select(cmds, argc, argv, do_help);