1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Facebook */
11 #include <linux/btf.h>
12 #include <sys/types.h>
17 #include <bpf/hashmap.h>
18 #include <bpf/libbpf.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",
40 [BTF_KIND_FLOAT] = "FLOAT",
41 [BTF_KIND_DECL_TAG] = "DECL_TAG",
44 struct btf_attach_point {
49 static const char *btf_int_enc_str(__u8 encoding)
65 static const char *btf_var_linkage_str(__u32 linkage)
70 case BTF_VAR_GLOBAL_ALLOCATED:
72 case BTF_VAR_GLOBAL_EXTERN:
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 btf_kind_safe(int kind)
102 return kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
105 static int dump_btf_type(const struct btf *btf, __u32 id,
106 const struct btf_type *t)
108 json_writer_t *w = json_wtr;
109 int kind = btf_kind(t);
112 jsonw_start_object(w);
113 jsonw_uint_field(w, "id", id);
114 jsonw_string_field(w, "kind", btf_kind_str[btf_kind_safe(kind)]);
115 jsonw_string_field(w, "name", btf_str(btf, t->name_off));
117 printf("[%u] %s '%s'", id, btf_kind_str[btf_kind_safe(kind)],
118 btf_str(btf, t->name_off));
123 __u32 v = *(__u32 *)(t + 1);
126 enc = btf_int_enc_str(BTF_INT_ENCODING(v));
129 jsonw_uint_field(w, "size", t->size);
130 jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v));
131 jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v));
132 jsonw_string_field(w, "encoding", enc);
134 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
135 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
142 case BTF_KIND_VOLATILE:
143 case BTF_KIND_RESTRICT:
144 case BTF_KIND_TYPEDEF:
146 jsonw_uint_field(w, "type_id", t->type);
148 printf(" type_id=%u", t->type);
150 case BTF_KIND_ARRAY: {
151 const struct btf_array *arr = (const void *)(t + 1);
154 jsonw_uint_field(w, "type_id", arr->type);
155 jsonw_uint_field(w, "index_type_id", arr->index_type);
156 jsonw_uint_field(w, "nr_elems", arr->nelems);
158 printf(" type_id=%u index_type_id=%u nr_elems=%u",
159 arr->type, arr->index_type, arr->nelems);
163 case BTF_KIND_STRUCT:
164 case BTF_KIND_UNION: {
165 const struct btf_member *m = (const void *)(t + 1);
166 __u16 vlen = BTF_INFO_VLEN(t->info);
170 jsonw_uint_field(w, "size", t->size);
171 jsonw_uint_field(w, "vlen", vlen);
172 jsonw_name(w, "members");
173 jsonw_start_array(w);
175 printf(" size=%u vlen=%u", t->size, vlen);
177 for (i = 0; i < vlen; i++, m++) {
178 const char *name = btf_str(btf, m->name_off);
179 __u32 bit_off, bit_sz;
181 if (BTF_INFO_KFLAG(t->info)) {
182 bit_off = BTF_MEMBER_BIT_OFFSET(m->offset);
183 bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset);
190 jsonw_start_object(w);
191 jsonw_string_field(w, "name", name);
192 jsonw_uint_field(w, "type_id", m->type);
193 jsonw_uint_field(w, "bits_offset", bit_off);
195 jsonw_uint_field(w, "bitfield_size",
200 printf("\n\t'%s' type_id=%u bits_offset=%u",
201 name, m->type, bit_off);
203 printf(" bitfield_size=%u", bit_sz);
210 case BTF_KIND_ENUM: {
211 const struct btf_enum *v = (const void *)(t + 1);
212 __u16 vlen = BTF_INFO_VLEN(t->info);
216 jsonw_uint_field(w, "size", t->size);
217 jsonw_uint_field(w, "vlen", vlen);
218 jsonw_name(w, "values");
219 jsonw_start_array(w);
221 printf(" size=%u vlen=%u", t->size, vlen);
223 for (i = 0; i < vlen; i++, v++) {
224 const char *name = btf_str(btf, v->name_off);
227 jsonw_start_object(w);
228 jsonw_string_field(w, "name", name);
229 jsonw_uint_field(w, "val", v->val);
232 printf("\n\t'%s' val=%u", name, v->val);
240 const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
244 jsonw_string_field(w, "fwd_kind", fwd_kind);
246 printf(" fwd_kind=%s", fwd_kind);
249 case BTF_KIND_FUNC: {
250 const char *linkage = btf_func_linkage_str(t);
253 jsonw_uint_field(w, "type_id", t->type);
254 jsonw_string_field(w, "linkage", linkage);
256 printf(" type_id=%u linkage=%s", t->type, linkage);
260 case BTF_KIND_FUNC_PROTO: {
261 const struct btf_param *p = (const void *)(t + 1);
262 __u16 vlen = BTF_INFO_VLEN(t->info);
266 jsonw_uint_field(w, "ret_type_id", t->type);
267 jsonw_uint_field(w, "vlen", vlen);
268 jsonw_name(w, "params");
269 jsonw_start_array(w);
271 printf(" ret_type_id=%u vlen=%u", t->type, vlen);
273 for (i = 0; i < vlen; i++, p++) {
274 const char *name = btf_str(btf, p->name_off);
277 jsonw_start_object(w);
278 jsonw_string_field(w, "name", name);
279 jsonw_uint_field(w, "type_id", p->type);
282 printf("\n\t'%s' type_id=%u", name, p->type);
290 const struct btf_var *v = (const void *)(t + 1);
293 linkage = btf_var_linkage_str(v->linkage);
296 jsonw_uint_field(w, "type_id", t->type);
297 jsonw_string_field(w, "linkage", linkage);
299 printf(" type_id=%u, linkage=%s", t->type, linkage);
303 case BTF_KIND_DATASEC: {
304 const struct btf_var_secinfo *v = (const void *)(t + 1);
305 const struct btf_type *vt;
306 __u16 vlen = BTF_INFO_VLEN(t->info);
310 jsonw_uint_field(w, "size", t->size);
311 jsonw_uint_field(w, "vlen", vlen);
312 jsonw_name(w, "vars");
313 jsonw_start_array(w);
315 printf(" size=%u vlen=%u", t->size, vlen);
317 for (i = 0; i < vlen; i++, v++) {
319 jsonw_start_object(w);
320 jsonw_uint_field(w, "type_id", v->type);
321 jsonw_uint_field(w, "offset", v->offset);
322 jsonw_uint_field(w, "size", v->size);
325 printf("\n\ttype_id=%u offset=%u size=%u",
326 v->type, v->offset, v->size);
328 if (v->type < btf__type_cnt(btf)) {
329 vt = btf__type_by_id(btf, v->type);
331 btf_kind_str[btf_kind_safe(btf_kind(vt))],
332 btf_str(btf, vt->name_off));
340 case BTF_KIND_FLOAT: {
342 jsonw_uint_field(w, "size", t->size);
344 printf(" size=%u", t->size);
347 case BTF_KIND_DECL_TAG: {
348 const struct btf_decl_tag *tag = (const void *)(t + 1);
351 jsonw_uint_field(w, "type_id", t->type);
352 jsonw_int_field(w, "component_idx", tag->component_idx);
354 printf(" type_id=%u component_idx=%d", t->type, tag->component_idx);
363 jsonw_end_object(json_wtr);
370 static int dump_btf_raw(const struct btf *btf,
371 __u32 *root_type_ids, int root_type_cnt)
373 const struct btf_type *t;
377 jsonw_start_object(json_wtr);
378 jsonw_name(json_wtr, "types");
379 jsonw_start_array(json_wtr);
383 for (i = 0; i < root_type_cnt; i++) {
384 t = btf__type_by_id(btf, root_type_ids[i]);
385 dump_btf_type(btf, root_type_ids[i], t);
388 const struct btf *base;
389 int cnt = btf__type_cnt(btf);
392 base = btf__base_btf(btf);
394 start_id = btf__type_cnt(base);
396 for (i = start_id; i < cnt; i++) {
397 t = btf__type_by_id(btf, i);
398 dump_btf_type(btf, i, t);
403 jsonw_end_array(json_wtr);
404 jsonw_end_object(json_wtr);
409 static void __printf(2, 0) btf_dump_printf(void *ctx,
410 const char *fmt, va_list args)
412 vfprintf(stdout, fmt, args);
415 static int dump_btf_c(const struct btf *btf,
416 __u32 *root_type_ids, int root_type_cnt)
421 d = btf_dump__new(btf, NULL, NULL, btf_dump_printf);
425 printf("#ifndef __VMLINUX_H__\n");
426 printf("#define __VMLINUX_H__\n");
428 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
429 printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
430 printf("#endif\n\n");
433 for (i = 0; i < root_type_cnt; i++) {
434 err = btf_dump__dump_type(d, root_type_ids[i]);
439 int cnt = btf__type_cnt(btf);
441 for (i = 1; i < cnt; i++) {
442 err = btf_dump__dump_type(d, i);
448 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
449 printf("#pragma clang attribute pop\n");
452 printf("#endif /* __VMLINUX_H__ */\n");
459 static int do_dump(int argc, char **argv)
461 struct btf *btf = NULL, *base = NULL;
462 __u32 root_type_ids[2];
463 int root_type_cnt = 0;
475 if (is_prefix(src, "map")) {
476 struct bpf_map_info info = {};
477 __u32 len = sizeof(info);
484 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
488 btf_id = info.btf_id;
489 if (argc && is_prefix(*argv, "key")) {
490 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
492 } else if (argc && is_prefix(*argv, "value")) {
493 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
495 } else if (argc && is_prefix(*argv, "all")) {
497 } else if (argc && is_prefix(*argv, "kv")) {
498 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
499 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
502 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
503 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
505 } else if (is_prefix(src, "prog")) {
506 struct bpf_prog_info info = {};
507 __u32 len = sizeof(info);
514 fd = prog_parse_fd(&argc, &argv);
518 err = bpf_obj_get_info_by_fd(fd, &info, &len);
520 p_err("can't get prog info: %s", strerror(errno));
524 btf_id = info.btf_id;
525 } else if (is_prefix(src, "id")) {
528 btf_id = strtoul(*argv, &endptr, 0);
530 p_err("can't parse %s as ID", *argv);
534 } else if (is_prefix(src, "file")) {
535 const char sysfs_prefix[] = "/sys/kernel/btf/";
536 const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
539 strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
540 strcmp(*argv, sysfs_vmlinux) != 0) {
541 base = btf__parse(sysfs_vmlinux, NULL);
542 if (libbpf_get_error(base)) {
543 p_err("failed to parse vmlinux BTF at '%s': %ld\n",
544 sysfs_vmlinux, libbpf_get_error(base));
549 btf = btf__parse_split(*argv, base ?: base_btf);
553 p_err("failed to load BTF from %s: %s",
554 *argv, strerror(err));
560 p_err("unrecognized BTF source specifier: '%s'", src);
565 if (is_prefix(*argv, "format")) {
568 p_err("expecting value for 'format' option\n");
572 if (strcmp(*argv, "c") == 0) {
574 } else if (strcmp(*argv, "raw") == 0) {
577 p_err("unrecognized format specifier: '%s', possible values: raw, c",
584 p_err("unrecognized option: '%s'", *argv);
591 btf = btf__load_from_kernel_by_id_split(btf_id, base_btf);
592 err = libbpf_get_error(btf);
594 p_err("get btf by id (%u): %s", btf_id, strerror(err));
601 p_err("JSON output for C-syntax dump is not supported");
605 err = dump_btf_c(btf, root_type_ids, root_type_cnt);
607 err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
617 static int btf_parse_fd(int *argc, char ***argv)
623 if (!is_prefix(*argv[0], "id")) {
624 p_err("expected 'id', got: '%s'?", **argv);
629 id = strtoul(**argv, &endptr, 0);
631 p_err("can't parse %s as ID", **argv);
636 fd = bpf_btf_get_fd_by_id(id);
638 p_err("can't get BTF object by id (%u): %s",
639 id, strerror(errno));
645 build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
646 void *info, __u32 *len)
648 static const char * const names[] = {
649 [BPF_OBJ_UNKNOWN] = "unknown",
650 [BPF_OBJ_PROG] = "prog",
651 [BPF_OBJ_MAP] = "map",
653 __u32 btf_id, id = 0;
660 err = bpf_prog_get_next_id(id, &id);
663 err = bpf_map_get_next_id(id, &id);
667 p_err("unexpected object type: %d", type);
671 if (errno == ENOENT) {
675 p_err("can't get next %s: %s%s", names[type],
677 errno == EINVAL ? " -- kernel too old?" : "");
683 fd = bpf_prog_get_fd_by_id(id);
686 fd = bpf_map_get_fd_by_id(id);
690 p_err("unexpected object type: %d", type);
696 p_err("can't get %s by id (%u): %s", names[type], id,
702 memset(info, 0, *len);
703 err = bpf_obj_get_info_by_fd(fd, info, len);
706 p_err("can't get %s info: %s", names[type],
713 btf_id = ((struct bpf_prog_info *)info)->btf_id;
716 btf_id = ((struct bpf_map_info *)info)->btf_id;
720 p_err("unexpected object type: %d", type);
726 err = hashmap__append(tab, u32_as_hash_field(btf_id),
727 u32_as_hash_field(id));
729 p_err("failed to append entry to hashmap for BTF ID %u, object ID %u: %s",
730 btf_id, id, strerror(errno));
743 build_btf_tables(struct hashmap *btf_prog_table,
744 struct hashmap *btf_map_table)
746 struct bpf_prog_info prog_info;
747 __u32 prog_len = sizeof(prog_info);
748 struct bpf_map_info map_info;
749 __u32 map_len = sizeof(map_info);
752 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
757 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
760 hashmap__free(btf_prog_table);
768 show_btf_plain(struct bpf_btf_info *info, int fd,
769 struct hashmap *btf_prog_table,
770 struct hashmap *btf_map_table)
772 struct hashmap_entry *entry;
773 const char *name = u64_to_ptr(info->name);
776 printf("%u: ", info->id);
777 if (info->kernel_btf)
778 printf("name [%s] ", name);
779 else if (name && name[0])
780 printf("name %s ", name);
782 printf("name <anon> ");
783 printf("size %uB", info->btf_size);
786 hashmap__for_each_key_entry(btf_prog_table, entry,
787 u32_as_hash_field(info->id)) {
788 printf("%s%u", n++ == 0 ? " prog_ids " : ",",
789 hash_field_as_u32(entry->value));
793 hashmap__for_each_key_entry(btf_map_table, entry,
794 u32_as_hash_field(info->id)) {
795 printf("%s%u", n++ == 0 ? " map_ids " : ",",
796 hash_field_as_u32(entry->value));
799 emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
805 show_btf_json(struct bpf_btf_info *info, int fd,
806 struct hashmap *btf_prog_table,
807 struct hashmap *btf_map_table)
809 struct hashmap_entry *entry;
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 hashmap__for_each_key_entry(btf_prog_table, entry,
819 u32_as_hash_field(info->id)) {
820 jsonw_uint(json_wtr, hash_field_as_u32(entry->value));
822 jsonw_end_array(json_wtr); /* prog_ids */
824 jsonw_name(json_wtr, "map_ids");
825 jsonw_start_array(json_wtr); /* map_ids */
826 hashmap__for_each_key_entry(btf_map_table, entry,
827 u32_as_hash_field(info->id)) {
828 jsonw_uint(json_wtr, hash_field_as_u32(entry->value));
830 jsonw_end_array(json_wtr); /* map_ids */
832 emit_obj_refs_json(refs_table, info->id, json_wtr); /* pids */
834 jsonw_bool_field(json_wtr, "kernel", info->kernel_btf);
837 jsonw_string_field(json_wtr, "name", name);
839 jsonw_end_object(json_wtr); /* btf object */
843 show_btf(int fd, struct hashmap *btf_prog_table,
844 struct hashmap *btf_map_table)
846 struct bpf_btf_info info;
847 __u32 len = sizeof(info);
851 memset(&info, 0, sizeof(info));
852 err = bpf_obj_get_info_by_fd(fd, &info, &len);
854 p_err("can't get BTF object info: %s", strerror(errno));
857 /* if kernel support emitting BTF object name, pass name pointer */
859 memset(&info, 0, sizeof(info));
860 info.name_len = sizeof(name);
861 info.name = ptr_to_u64(name);
864 err = bpf_obj_get_info_by_fd(fd, &info, &len);
866 p_err("can't get BTF object info: %s", strerror(errno));
872 show_btf_json(&info, fd, btf_prog_table, btf_map_table);
874 show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
879 static int do_show(int argc, char **argv)
881 struct hashmap *btf_prog_table;
882 struct hashmap *btf_map_table;
887 fd = btf_parse_fd(&argc, &argv);
898 btf_prog_table = hashmap__new(hash_fn_for_key_as_id,
899 equal_fn_for_key_as_id, NULL);
900 btf_map_table = hashmap__new(hash_fn_for_key_as_id,
901 equal_fn_for_key_as_id, NULL);
902 if (!btf_prog_table || !btf_map_table) {
903 hashmap__free(btf_prog_table);
904 hashmap__free(btf_map_table);
907 p_err("failed to create hashmap for object references");
910 err = build_btf_tables(btf_prog_table, btf_map_table);
916 build_obj_refs_table(&refs_table, BPF_OBJ_BTF);
919 err = show_btf(fd, btf_prog_table, btf_map_table);
925 jsonw_start_array(json_wtr); /* root array */
928 err = bpf_btf_get_next_id(id, &id);
930 if (errno == ENOENT) {
934 p_err("can't get next BTF object: %s%s",
936 errno == EINVAL ? " -- kernel too old?" : "");
941 fd = bpf_btf_get_fd_by_id(id);
945 p_err("can't get BTF object by id (%u): %s",
946 id, strerror(errno));
951 err = show_btf(fd, btf_prog_table, btf_map_table);
958 jsonw_end_array(json_wtr); /* root array */
961 hashmap__free(btf_prog_table);
962 hashmap__free(btf_map_table);
963 delete_obj_refs_table(refs_table);
968 static int do_help(int argc, char **argv)
971 jsonw_null(json_wtr);
976 "Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
977 " %1$s %2$s dump BTF_SRC [format FORMAT]\n"
980 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
981 " FORMAT := { raw | c }\n"
982 " " HELP_SPEC_MAP "\n"
983 " " HELP_SPEC_PROGRAM "\n"
984 " " HELP_SPEC_OPTIONS " |\n"
985 " {-B|--base-btf} }\n"
992 static const struct cmd cmds[] = {
1000 int do_btf(int argc, char **argv)
1002 return cmd_select(cmds, argc, argv, do_help);