Loading...
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2019 Facebook */
3
4#include <errno.h>
5#include <fcntl.h>
6#include <linux/err.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <string.h>
10#include <unistd.h>
11#include <bpf/bpf.h>
12#include <bpf/btf.h>
13#include <bpf/libbpf.h>
14#include <linux/btf.h>
15#include <linux/hashtable.h>
16#include <sys/types.h>
17#include <sys/stat.h>
18
19#include "json_writer.h"
20#include "main.h"
21
22static 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",
40};
41
42struct btf_attach_table {
43 DECLARE_HASHTABLE(table, 16);
44};
45
46struct btf_attach_point {
47 __u32 obj_id;
48 __u32 btf_id;
49 struct hlist_node hash;
50};
51
52static const char *btf_int_enc_str(__u8 encoding)
53{
54 switch (encoding) {
55 case 0:
56 return "(none)";
57 case BTF_INT_SIGNED:
58 return "SIGNED";
59 case BTF_INT_CHAR:
60 return "CHAR";
61 case BTF_INT_BOOL:
62 return "BOOL";
63 default:
64 return "UNKN";
65 }
66}
67
68static const char *btf_var_linkage_str(__u32 linkage)
69{
70 switch (linkage) {
71 case BTF_VAR_STATIC:
72 return "static";
73 case BTF_VAR_GLOBAL_ALLOCATED:
74 return "global";
75 case BTF_VAR_GLOBAL_EXTERN:
76 return "extern";
77 default:
78 return "(unknown)";
79 }
80}
81
82static const char *btf_func_linkage_str(const struct btf_type *t)
83{
84 switch (btf_vlen(t)) {
85 case BTF_FUNC_STATIC:
86 return "static";
87 case BTF_FUNC_GLOBAL:
88 return "global";
89 case BTF_FUNC_EXTERN:
90 return "extern";
91 default:
92 return "(unknown)";
93 }
94}
95
96static const char *btf_str(const struct btf *btf, __u32 off)
97{
98 if (!off)
99 return "(anon)";
100 return btf__name_by_offset(btf, off) ? : "(invalid)";
101}
102
103static int btf_kind_safe(int kind)
104{
105 return kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
106}
107
108static int dump_btf_type(const struct btf *btf, __u32 id,
109 const struct btf_type *t)
110{
111 json_writer_t *w = json_wtr;
112 int kind = btf_kind(t);
113
114 if (json_output) {
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));
119 } else {
120 printf("[%u] %s '%s'", id, btf_kind_str[btf_kind_safe(kind)],
121 btf_str(btf, t->name_off));
122 }
123
124 switch (kind) {
125 case BTF_KIND_INT: {
126 __u32 v = *(__u32 *)(t + 1);
127 const char *enc;
128
129 enc = btf_int_enc_str(BTF_INT_ENCODING(v));
130
131 if (json_output) {
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);
136 } else {
137 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
138 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
139 enc);
140 }
141 break;
142 }
143 case BTF_KIND_PTR:
144 case BTF_KIND_CONST:
145 case BTF_KIND_VOLATILE:
146 case BTF_KIND_RESTRICT:
147 case BTF_KIND_TYPEDEF:
148 if (json_output)
149 jsonw_uint_field(w, "type_id", t->type);
150 else
151 printf(" type_id=%u", t->type);
152 break;
153 case BTF_KIND_ARRAY: {
154 const struct btf_array *arr = (const void *)(t + 1);
155
156 if (json_output) {
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);
160 } else {
161 printf(" type_id=%u index_type_id=%u nr_elems=%u",
162 arr->type, arr->index_type, arr->nelems);
163 }
164 break;
165 }
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);
170 int i;
171
172 if (json_output) {
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);
177 } else {
178 printf(" size=%u vlen=%u", t->size, vlen);
179 }
180 for (i = 0; i < vlen; i++, m++) {
181 const char *name = btf_str(btf, m->name_off);
182 __u32 bit_off, bit_sz;
183
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);
187 } else {
188 bit_off = m->offset;
189 bit_sz = 0;
190 }
191
192 if (json_output) {
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);
197 if (bit_sz) {
198 jsonw_uint_field(w, "bitfield_size",
199 bit_sz);
200 }
201 jsonw_end_object(w);
202 } else {
203 printf("\n\t'%s' type_id=%u bits_offset=%u",
204 name, m->type, bit_off);
205 if (bit_sz)
206 printf(" bitfield_size=%u", bit_sz);
207 }
208 }
209 if (json_output)
210 jsonw_end_array(w);
211 break;
212 }
213 case BTF_KIND_ENUM: {
214 const struct btf_enum *v = (const void *)(t + 1);
215 __u16 vlen = BTF_INFO_VLEN(t->info);
216 int i;
217
218 if (json_output) {
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);
223 } else {
224 printf(" size=%u vlen=%u", t->size, vlen);
225 }
226 for (i = 0; i < vlen; i++, v++) {
227 const char *name = btf_str(btf, v->name_off);
228
229 if (json_output) {
230 jsonw_start_object(w);
231 jsonw_string_field(w, "name", name);
232 jsonw_uint_field(w, "val", v->val);
233 jsonw_end_object(w);
234 } else {
235 printf("\n\t'%s' val=%u", name, v->val);
236 }
237 }
238 if (json_output)
239 jsonw_end_array(w);
240 break;
241 }
242 case BTF_KIND_FWD: {
243 const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
244 : "struct";
245
246 if (json_output)
247 jsonw_string_field(w, "fwd_kind", fwd_kind);
248 else
249 printf(" fwd_kind=%s", fwd_kind);
250 break;
251 }
252 case BTF_KIND_FUNC: {
253 const char *linkage = btf_func_linkage_str(t);
254
255 if (json_output) {
256 jsonw_uint_field(w, "type_id", t->type);
257 jsonw_string_field(w, "linkage", linkage);
258 } else {
259 printf(" type_id=%u linkage=%s", t->type, linkage);
260 }
261 break;
262 }
263 case BTF_KIND_FUNC_PROTO: {
264 const struct btf_param *p = (const void *)(t + 1);
265 __u16 vlen = BTF_INFO_VLEN(t->info);
266 int i;
267
268 if (json_output) {
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);
273 } else {
274 printf(" ret_type_id=%u vlen=%u", t->type, vlen);
275 }
276 for (i = 0; i < vlen; i++, p++) {
277 const char *name = btf_str(btf, p->name_off);
278
279 if (json_output) {
280 jsonw_start_object(w);
281 jsonw_string_field(w, "name", name);
282 jsonw_uint_field(w, "type_id", p->type);
283 jsonw_end_object(w);
284 } else {
285 printf("\n\t'%s' type_id=%u", name, p->type);
286 }
287 }
288 if (json_output)
289 jsonw_end_array(w);
290 break;
291 }
292 case BTF_KIND_VAR: {
293 const struct btf_var *v = (const void *)(t + 1);
294 const char *linkage;
295
296 linkage = btf_var_linkage_str(v->linkage);
297
298 if (json_output) {
299 jsonw_uint_field(w, "type_id", t->type);
300 jsonw_string_field(w, "linkage", linkage);
301 } else {
302 printf(" type_id=%u, linkage=%s", t->type, linkage);
303 }
304 break;
305 }
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);
310 int i;
311
312 if (json_output) {
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);
317 } else {
318 printf(" size=%u vlen=%u", t->size, vlen);
319 }
320 for (i = 0; i < vlen; i++, v++) {
321 if (json_output) {
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);
326 jsonw_end_object(w);
327 } else {
328 printf("\n\ttype_id=%u offset=%u size=%u",
329 v->type, v->offset, v->size);
330
331 if (v->type <= btf__get_nr_types(btf)) {
332 vt = btf__type_by_id(btf, v->type);
333 printf(" (%s '%s')",
334 btf_kind_str[btf_kind_safe(btf_kind(vt))],
335 btf_str(btf, vt->name_off));
336 }
337 }
338 }
339 if (json_output)
340 jsonw_end_array(w);
341 break;
342 }
343 case BTF_KIND_FLOAT: {
344 if (json_output)
345 jsonw_uint_field(w, "size", t->size);
346 else
347 printf(" size=%u", t->size);
348 break;
349 }
350 default:
351 break;
352 }
353
354 if (json_output)
355 jsonw_end_object(json_wtr);
356 else
357 printf("\n");
358
359 return 0;
360}
361
362static int dump_btf_raw(const struct btf *btf,
363 __u32 *root_type_ids, int root_type_cnt)
364{
365 const struct btf_type *t;
366 int i;
367
368 if (json_output) {
369 jsonw_start_object(json_wtr);
370 jsonw_name(json_wtr, "types");
371 jsonw_start_array(json_wtr);
372 }
373
374 if (root_type_cnt) {
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);
378 }
379 } else {
380 const struct btf *base;
381 int cnt = btf__get_nr_types(btf);
382 int start_id = 1;
383
384 base = btf__base_btf(btf);
385 if (base)
386 start_id = btf__get_nr_types(base) + 1;
387
388 for (i = start_id; i <= cnt; i++) {
389 t = btf__type_by_id(btf, i);
390 dump_btf_type(btf, i, t);
391 }
392 }
393
394 if (json_output) {
395 jsonw_end_array(json_wtr);
396 jsonw_end_object(json_wtr);
397 }
398 return 0;
399}
400
401static void __printf(2, 0) btf_dump_printf(void *ctx,
402 const char *fmt, va_list args)
403{
404 vfprintf(stdout, fmt, args);
405}
406
407static int dump_btf_c(const struct btf *btf,
408 __u32 *root_type_ids, int root_type_cnt)
409{
410 struct btf_dump *d;
411 int err = 0, i;
412
413 d = btf_dump__new(btf, NULL, NULL, btf_dump_printf);
414 if (IS_ERR(d))
415 return PTR_ERR(d);
416
417 printf("#ifndef __VMLINUX_H__\n");
418 printf("#define __VMLINUX_H__\n");
419 printf("\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");
423
424 if (root_type_cnt) {
425 for (i = 0; i < root_type_cnt; i++) {
426 err = btf_dump__dump_type(d, root_type_ids[i]);
427 if (err)
428 goto done;
429 }
430 } else {
431 int cnt = btf__get_nr_types(btf);
432
433 for (i = 1; i <= cnt; i++) {
434 err = btf_dump__dump_type(d, i);
435 if (err)
436 goto done;
437 }
438 }
439
440 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
441 printf("#pragma clang attribute pop\n");
442 printf("#endif\n");
443 printf("\n");
444 printf("#endif /* __VMLINUX_H__ */\n");
445
446done:
447 btf_dump__free(d);
448 return err;
449}
450
451static int do_dump(int argc, char **argv)
452{
453 struct btf *btf = NULL, *base = NULL;
454 __u32 root_type_ids[2];
455 int root_type_cnt = 0;
456 bool dump_c = false;
457 __u32 btf_id = -1;
458 const char *src;
459 int fd = -1;
460 int err;
461
462 if (!REQ_ARGS(2)) {
463 usage();
464 return -1;
465 }
466 src = GET_ARG();
467 if (is_prefix(src, "map")) {
468 struct bpf_map_info info = {};
469 __u32 len = sizeof(info);
470
471 if (!REQ_ARGS(2)) {
472 usage();
473 return -1;
474 }
475
476 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
477 if (fd < 0)
478 return -1;
479
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;
483 NEXT_ARG();
484 } else if (argc && is_prefix(*argv, "value")) {
485 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
486 NEXT_ARG();
487 } else if (argc && is_prefix(*argv, "all")) {
488 NEXT_ARG();
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;
492 NEXT_ARG();
493 } else {
494 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
495 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
496 }
497 } else if (is_prefix(src, "prog")) {
498 struct bpf_prog_info info = {};
499 __u32 len = sizeof(info);
500
501 if (!REQ_ARGS(2)) {
502 usage();
503 return -1;
504 }
505
506 fd = prog_parse_fd(&argc, &argv);
507 if (fd < 0)
508 return -1;
509
510 err = bpf_obj_get_info_by_fd(fd, &info, &len);
511 if (err) {
512 p_err("can't get prog info: %s", strerror(errno));
513 goto done;
514 }
515
516 btf_id = info.btf_id;
517 } else if (is_prefix(src, "id")) {
518 char *endptr;
519
520 btf_id = strtoul(*argv, &endptr, 0);
521 if (*endptr) {
522 p_err("can't parse %s as ID", *argv);
523 return -1;
524 }
525 NEXT_ARG();
526 } else if (is_prefix(src, "file")) {
527 const char sysfs_prefix[] = "/sys/kernel/btf/";
528 const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
529
530 if (!base_btf &&
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));
537 base = NULL;
538 }
539 }
540
541 btf = btf__parse_split(*argv, base ?: base_btf);
542 if (IS_ERR(btf)) {
543 err = -PTR_ERR(btf);
544 btf = NULL;
545 p_err("failed to load BTF from %s: %s",
546 *argv, strerror(err));
547 goto done;
548 }
549 NEXT_ARG();
550 } else {
551 err = -1;
552 p_err("unrecognized BTF source specifier: '%s'", src);
553 goto done;
554 }
555
556 while (argc) {
557 if (is_prefix(*argv, "format")) {
558 NEXT_ARG();
559 if (argc < 1) {
560 p_err("expecting value for 'format' option\n");
561 err = -EINVAL;
562 goto done;
563 }
564 if (strcmp(*argv, "c") == 0) {
565 dump_c = true;
566 } else if (strcmp(*argv, "raw") == 0) {
567 dump_c = false;
568 } else {
569 p_err("unrecognized format specifier: '%s', possible values: raw, c",
570 *argv);
571 err = -EINVAL;
572 goto done;
573 }
574 NEXT_ARG();
575 } else {
576 p_err("unrecognized option: '%s'", *argv);
577 err = -EINVAL;
578 goto done;
579 }
580 }
581
582 if (!btf) {
583 err = btf__get_from_id(btf_id, &btf);
584 if (err) {
585 p_err("get btf by id (%u): %s", btf_id, strerror(err));
586 goto done;
587 }
588 if (!btf) {
589 err = -ENOENT;
590 p_err("can't find btf with ID (%u)", btf_id);
591 goto done;
592 }
593 }
594
595 if (dump_c) {
596 if (json_output) {
597 p_err("JSON output for C-syntax dump is not supported");
598 err = -ENOTSUP;
599 goto done;
600 }
601 err = dump_btf_c(btf, root_type_ids, root_type_cnt);
602 } else {
603 err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
604 }
605
606done:
607 close(fd);
608 btf__free(btf);
609 btf__free(base);
610 return err;
611}
612
613static int btf_parse_fd(int *argc, char ***argv)
614{
615 unsigned int id;
616 char *endptr;
617 int fd;
618
619 if (!is_prefix(*argv[0], "id")) {
620 p_err("expected 'id', got: '%s'?", **argv);
621 return -1;
622 }
623 NEXT_ARGP();
624
625 id = strtoul(**argv, &endptr, 0);
626 if (*endptr) {
627 p_err("can't parse %s as ID", **argv);
628 return -1;
629 }
630 NEXT_ARGP();
631
632 fd = bpf_btf_get_fd_by_id(id);
633 if (fd < 0)
634 p_err("can't get BTF object by id (%u): %s",
635 id, strerror(errno));
636
637 return fd;
638}
639
640static void delete_btf_table(struct btf_attach_table *tab)
641{
642 struct btf_attach_point *obj;
643 struct hlist_node *tmp;
644
645 unsigned int bkt;
646
647 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
648 hash_del(&obj->hash);
649 free(obj);
650 }
651}
652
653static int
654build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type,
655 void *info, __u32 *len)
656{
657 static const char * const names[] = {
658 [BPF_OBJ_UNKNOWN] = "unknown",
659 [BPF_OBJ_PROG] = "prog",
660 [BPF_OBJ_MAP] = "map",
661 };
662 struct btf_attach_point *obj_node;
663 __u32 btf_id, id = 0;
664 int err;
665 int fd;
666
667 while (true) {
668 switch (type) {
669 case BPF_OBJ_PROG:
670 err = bpf_prog_get_next_id(id, &id);
671 break;
672 case BPF_OBJ_MAP:
673 err = bpf_map_get_next_id(id, &id);
674 break;
675 default:
676 err = -1;
677 p_err("unexpected object type: %d", type);
678 goto err_free;
679 }
680 if (err) {
681 if (errno == ENOENT) {
682 err = 0;
683 break;
684 }
685 p_err("can't get next %s: %s%s", names[type],
686 strerror(errno),
687 errno == EINVAL ? " -- kernel too old?" : "");
688 goto err_free;
689 }
690
691 switch (type) {
692 case BPF_OBJ_PROG:
693 fd = bpf_prog_get_fd_by_id(id);
694 break;
695 case BPF_OBJ_MAP:
696 fd = bpf_map_get_fd_by_id(id);
697 break;
698 default:
699 err = -1;
700 p_err("unexpected object type: %d", type);
701 goto err_free;
702 }
703 if (fd < 0) {
704 if (errno == ENOENT)
705 continue;
706 p_err("can't get %s by id (%u): %s", names[type], id,
707 strerror(errno));
708 err = -1;
709 goto err_free;
710 }
711
712 memset(info, 0, *len);
713 err = bpf_obj_get_info_by_fd(fd, info, len);
714 close(fd);
715 if (err) {
716 p_err("can't get %s info: %s", names[type],
717 strerror(errno));
718 goto err_free;
719 }
720
721 switch (type) {
722 case BPF_OBJ_PROG:
723 btf_id = ((struct bpf_prog_info *)info)->btf_id;
724 break;
725 case BPF_OBJ_MAP:
726 btf_id = ((struct bpf_map_info *)info)->btf_id;
727 break;
728 default:
729 err = -1;
730 p_err("unexpected object type: %d", type);
731 goto err_free;
732 }
733 if (!btf_id)
734 continue;
735
736 obj_node = calloc(1, sizeof(*obj_node));
737 if (!obj_node) {
738 p_err("failed to allocate memory: %s", strerror(errno));
739 err = -ENOMEM;
740 goto err_free;
741 }
742
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);
746 }
747
748 return 0;
749
750err_free:
751 delete_btf_table(tab);
752 return err;
753}
754
755static int
756build_btf_tables(struct btf_attach_table *btf_prog_table,
757 struct btf_attach_table *btf_map_table)
758{
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);
763 int err = 0;
764
765 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
766 &prog_len);
767 if (err)
768 return err;
769
770 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
771 &map_len);
772 if (err) {
773 delete_btf_table(btf_prog_table);
774 return err;
775 }
776
777 return 0;
778}
779
780static void
781show_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)
784{
785 struct btf_attach_point *obj;
786 const char *name = u64_to_ptr(info->name);
787 int n;
788
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);
794 else
795 printf("name <anon> ");
796 printf("size %uB", info->btf_size);
797
798 n = 0;
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 " : ",",
802 obj->obj_id);
803 }
804
805 n = 0;
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 " : ",",
809 obj->obj_id);
810 }
811 emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
812
813 printf("\n");
814}
815
816static void
817show_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)
820{
821 struct btf_attach_point *obj;
822 const char *name = u64_to_ptr(info->name);
823
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);
827
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,
831 info->id) {
832 if (obj->btf_id == info->id)
833 jsonw_uint(json_wtr, obj->obj_id);
834 }
835 jsonw_end_array(json_wtr); /* prog_ids */
836
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,
840 info->id) {
841 if (obj->btf_id == info->id)
842 jsonw_uint(json_wtr, obj->obj_id);
843 }
844 jsonw_end_array(json_wtr); /* map_ids */
845
846 emit_obj_refs_json(&refs_table, info->id, json_wtr); /* pids */
847
848 jsonw_bool_field(json_wtr, "kernel", info->kernel_btf);
849
850 if (name && name[0])
851 jsonw_string_field(json_wtr, "name", name);
852
853 jsonw_end_object(json_wtr); /* btf object */
854}
855
856static int
857show_btf(int fd, struct btf_attach_table *btf_prog_table,
858 struct btf_attach_table *btf_map_table)
859{
860 struct bpf_btf_info info;
861 __u32 len = sizeof(info);
862 char name[64];
863 int err;
864
865 memset(&info, 0, sizeof(info));
866 err = bpf_obj_get_info_by_fd(fd, &info, &len);
867 if (err) {
868 p_err("can't get BTF object info: %s", strerror(errno));
869 return -1;
870 }
871 /* if kernel support emitting BTF object name, pass name pointer */
872 if (info.name_len) {
873 memset(&info, 0, sizeof(info));
874 info.name_len = sizeof(name);
875 info.name = ptr_to_u64(name);
876 len = sizeof(info);
877
878 err = bpf_obj_get_info_by_fd(fd, &info, &len);
879 if (err) {
880 p_err("can't get BTF object info: %s", strerror(errno));
881 return -1;
882 }
883 }
884
885 if (json_output)
886 show_btf_json(&info, fd, btf_prog_table, btf_map_table);
887 else
888 show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
889
890 return 0;
891}
892
893static int do_show(int argc, char **argv)
894{
895 struct btf_attach_table btf_prog_table;
896 struct btf_attach_table btf_map_table;
897 int err, fd = -1;
898 __u32 id = 0;
899
900 if (argc == 2) {
901 fd = btf_parse_fd(&argc, &argv);
902 if (fd < 0)
903 return -1;
904 }
905
906 if (argc) {
907 if (fd >= 0)
908 close(fd);
909 return BAD_ARG();
910 }
911
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);
915 if (err) {
916 if (fd >= 0)
917 close(fd);
918 return err;
919 }
920 build_obj_refs_table(&refs_table, BPF_OBJ_BTF);
921
922 if (fd >= 0) {
923 err = show_btf(fd, &btf_prog_table, &btf_map_table);
924 close(fd);
925 goto exit_free;
926 }
927
928 if (json_output)
929 jsonw_start_array(json_wtr); /* root array */
930
931 while (true) {
932 err = bpf_btf_get_next_id(id, &id);
933 if (err) {
934 if (errno == ENOENT) {
935 err = 0;
936 break;
937 }
938 p_err("can't get next BTF object: %s%s",
939 strerror(errno),
940 errno == EINVAL ? " -- kernel too old?" : "");
941 err = -1;
942 break;
943 }
944
945 fd = bpf_btf_get_fd_by_id(id);
946 if (fd < 0) {
947 if (errno == ENOENT)
948 continue;
949 p_err("can't get BTF object by id (%u): %s",
950 id, strerror(errno));
951 err = -1;
952 break;
953 }
954
955 err = show_btf(fd, &btf_prog_table, &btf_map_table);
956 close(fd);
957 if (err)
958 break;
959 }
960
961 if (json_output)
962 jsonw_end_array(json_wtr); /* root array */
963
964exit_free:
965 delete_btf_table(&btf_prog_table);
966 delete_btf_table(&btf_map_table);
967 delete_obj_refs_table(&refs_table);
968
969 return err;
970}
971
972static int do_help(int argc, char **argv)
973{
974 if (json_output) {
975 jsonw_null(json_wtr);
976 return 0;
977 }
978
979 fprintf(stderr,
980 "Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
981 " %1$s %2$s dump BTF_SRC [format FORMAT]\n"
982 " %1$s %2$s help\n"
983 "\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"
989 "",
990 bin_name, "btf");
991
992 return 0;
993}
994
995static const struct cmd cmds[] = {
996 { "show", do_show },
997 { "list", do_show },
998 { "help", do_help },
999 { "dump", do_dump },
1000 { 0 }
1001};
1002
1003int do_btf(int argc, char **argv)
1004{
1005 return cmd_select(cmds, argc, argv, do_help);
1006}
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2019 Facebook */
3
4#include <errno.h>
5#include <fcntl.h>
6#include <linux/err.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <string.h>
10#include <unistd.h>
11#include <linux/btf.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14
15#include <bpf/bpf.h>
16#include <bpf/btf.h>
17#include <bpf/hashmap.h>
18#include <bpf/libbpf.h>
19
20#include "json_writer.h"
21#include "main.h"
22
23static 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",
42 [BTF_KIND_TYPE_TAG] = "TYPE_TAG",
43 [BTF_KIND_ENUM64] = "ENUM64",
44};
45
46static const char *btf_int_enc_str(__u8 encoding)
47{
48 switch (encoding) {
49 case 0:
50 return "(none)";
51 case BTF_INT_SIGNED:
52 return "SIGNED";
53 case BTF_INT_CHAR:
54 return "CHAR";
55 case BTF_INT_BOOL:
56 return "BOOL";
57 default:
58 return "UNKN";
59 }
60}
61
62static const char *btf_var_linkage_str(__u32 linkage)
63{
64 switch (linkage) {
65 case BTF_VAR_STATIC:
66 return "static";
67 case BTF_VAR_GLOBAL_ALLOCATED:
68 return "global";
69 case BTF_VAR_GLOBAL_EXTERN:
70 return "extern";
71 default:
72 return "(unknown)";
73 }
74}
75
76static const char *btf_func_linkage_str(const struct btf_type *t)
77{
78 switch (btf_vlen(t)) {
79 case BTF_FUNC_STATIC:
80 return "static";
81 case BTF_FUNC_GLOBAL:
82 return "global";
83 case BTF_FUNC_EXTERN:
84 return "extern";
85 default:
86 return "(unknown)";
87 }
88}
89
90static const char *btf_str(const struct btf *btf, __u32 off)
91{
92 if (!off)
93 return "(anon)";
94 return btf__name_by_offset(btf, off) ? : "(invalid)";
95}
96
97static int btf_kind_safe(int kind)
98{
99 return kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
100}
101
102static int dump_btf_type(const struct btf *btf, __u32 id,
103 const struct btf_type *t)
104{
105 json_writer_t *w = json_wtr;
106 int kind = btf_kind(t);
107
108 if (json_output) {
109 jsonw_start_object(w);
110 jsonw_uint_field(w, "id", id);
111 jsonw_string_field(w, "kind", btf_kind_str[btf_kind_safe(kind)]);
112 jsonw_string_field(w, "name", btf_str(btf, t->name_off));
113 } else {
114 printf("[%u] %s '%s'", id, btf_kind_str[btf_kind_safe(kind)],
115 btf_str(btf, t->name_off));
116 }
117
118 switch (kind) {
119 case BTF_KIND_INT: {
120 __u32 v = *(__u32 *)(t + 1);
121 const char *enc;
122
123 enc = btf_int_enc_str(BTF_INT_ENCODING(v));
124
125 if (json_output) {
126 jsonw_uint_field(w, "size", t->size);
127 jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v));
128 jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v));
129 jsonw_string_field(w, "encoding", enc);
130 } else {
131 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
132 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
133 enc);
134 }
135 break;
136 }
137 case BTF_KIND_PTR:
138 case BTF_KIND_CONST:
139 case BTF_KIND_VOLATILE:
140 case BTF_KIND_RESTRICT:
141 case BTF_KIND_TYPEDEF:
142 case BTF_KIND_TYPE_TAG:
143 if (json_output)
144 jsonw_uint_field(w, "type_id", t->type);
145 else
146 printf(" type_id=%u", t->type);
147 break;
148 case BTF_KIND_ARRAY: {
149 const struct btf_array *arr = (const void *)(t + 1);
150
151 if (json_output) {
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);
155 } else {
156 printf(" type_id=%u index_type_id=%u nr_elems=%u",
157 arr->type, arr->index_type, arr->nelems);
158 }
159 break;
160 }
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);
165 int i;
166
167 if (json_output) {
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);
172 } else {
173 printf(" size=%u vlen=%u", t->size, vlen);
174 }
175 for (i = 0; i < vlen; i++, m++) {
176 const char *name = btf_str(btf, m->name_off);
177 __u32 bit_off, bit_sz;
178
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);
182 } else {
183 bit_off = m->offset;
184 bit_sz = 0;
185 }
186
187 if (json_output) {
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);
192 if (bit_sz) {
193 jsonw_uint_field(w, "bitfield_size",
194 bit_sz);
195 }
196 jsonw_end_object(w);
197 } else {
198 printf("\n\t'%s' type_id=%u bits_offset=%u",
199 name, m->type, bit_off);
200 if (bit_sz)
201 printf(" bitfield_size=%u", bit_sz);
202 }
203 }
204 if (json_output)
205 jsonw_end_array(w);
206 break;
207 }
208 case BTF_KIND_ENUM: {
209 const struct btf_enum *v = (const void *)(t + 1);
210 __u16 vlen = BTF_INFO_VLEN(t->info);
211 const char *encoding;
212 int i;
213
214 encoding = btf_kflag(t) ? "SIGNED" : "UNSIGNED";
215 if (json_output) {
216 jsonw_string_field(w, "encoding", encoding);
217 jsonw_uint_field(w, "size", t->size);
218 jsonw_uint_field(w, "vlen", vlen);
219 jsonw_name(w, "values");
220 jsonw_start_array(w);
221 } else {
222 printf(" encoding=%s size=%u vlen=%u", encoding, t->size, vlen);
223 }
224 for (i = 0; i < vlen; i++, v++) {
225 const char *name = btf_str(btf, v->name_off);
226
227 if (json_output) {
228 jsonw_start_object(w);
229 jsonw_string_field(w, "name", name);
230 if (btf_kflag(t))
231 jsonw_int_field(w, "val", v->val);
232 else
233 jsonw_uint_field(w, "val", v->val);
234 jsonw_end_object(w);
235 } else {
236 if (btf_kflag(t))
237 printf("\n\t'%s' val=%d", name, v->val);
238 else
239 printf("\n\t'%s' val=%u", name, v->val);
240 }
241 }
242 if (json_output)
243 jsonw_end_array(w);
244 break;
245 }
246 case BTF_KIND_ENUM64: {
247 const struct btf_enum64 *v = btf_enum64(t);
248 __u16 vlen = btf_vlen(t);
249 const char *encoding;
250 int i;
251
252 encoding = btf_kflag(t) ? "SIGNED" : "UNSIGNED";
253 if (json_output) {
254 jsonw_string_field(w, "encoding", encoding);
255 jsonw_uint_field(w, "size", t->size);
256 jsonw_uint_field(w, "vlen", vlen);
257 jsonw_name(w, "values");
258 jsonw_start_array(w);
259 } else {
260 printf(" encoding=%s size=%u vlen=%u", encoding, t->size, vlen);
261 }
262 for (i = 0; i < vlen; i++, v++) {
263 const char *name = btf_str(btf, v->name_off);
264 __u64 val = ((__u64)v->val_hi32 << 32) | v->val_lo32;
265
266 if (json_output) {
267 jsonw_start_object(w);
268 jsonw_string_field(w, "name", name);
269 if (btf_kflag(t))
270 jsonw_int_field(w, "val", val);
271 else
272 jsonw_uint_field(w, "val", val);
273 jsonw_end_object(w);
274 } else {
275 if (btf_kflag(t))
276 printf("\n\t'%s' val=%lldLL", name,
277 (unsigned long long)val);
278 else
279 printf("\n\t'%s' val=%lluULL", name,
280 (unsigned long long)val);
281 }
282 }
283 if (json_output)
284 jsonw_end_array(w);
285 break;
286 }
287 case BTF_KIND_FWD: {
288 const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
289 : "struct";
290
291 if (json_output)
292 jsonw_string_field(w, "fwd_kind", fwd_kind);
293 else
294 printf(" fwd_kind=%s", fwd_kind);
295 break;
296 }
297 case BTF_KIND_FUNC: {
298 const char *linkage = btf_func_linkage_str(t);
299
300 if (json_output) {
301 jsonw_uint_field(w, "type_id", t->type);
302 jsonw_string_field(w, "linkage", linkage);
303 } else {
304 printf(" type_id=%u linkage=%s", t->type, linkage);
305 }
306 break;
307 }
308 case BTF_KIND_FUNC_PROTO: {
309 const struct btf_param *p = (const void *)(t + 1);
310 __u16 vlen = BTF_INFO_VLEN(t->info);
311 int i;
312
313 if (json_output) {
314 jsonw_uint_field(w, "ret_type_id", t->type);
315 jsonw_uint_field(w, "vlen", vlen);
316 jsonw_name(w, "params");
317 jsonw_start_array(w);
318 } else {
319 printf(" ret_type_id=%u vlen=%u", t->type, vlen);
320 }
321 for (i = 0; i < vlen; i++, p++) {
322 const char *name = btf_str(btf, p->name_off);
323
324 if (json_output) {
325 jsonw_start_object(w);
326 jsonw_string_field(w, "name", name);
327 jsonw_uint_field(w, "type_id", p->type);
328 jsonw_end_object(w);
329 } else {
330 printf("\n\t'%s' type_id=%u", name, p->type);
331 }
332 }
333 if (json_output)
334 jsonw_end_array(w);
335 break;
336 }
337 case BTF_KIND_VAR: {
338 const struct btf_var *v = (const void *)(t + 1);
339 const char *linkage;
340
341 linkage = btf_var_linkage_str(v->linkage);
342
343 if (json_output) {
344 jsonw_uint_field(w, "type_id", t->type);
345 jsonw_string_field(w, "linkage", linkage);
346 } else {
347 printf(" type_id=%u, linkage=%s", t->type, linkage);
348 }
349 break;
350 }
351 case BTF_KIND_DATASEC: {
352 const struct btf_var_secinfo *v = (const void *)(t + 1);
353 const struct btf_type *vt;
354 __u16 vlen = BTF_INFO_VLEN(t->info);
355 int i;
356
357 if (json_output) {
358 jsonw_uint_field(w, "size", t->size);
359 jsonw_uint_field(w, "vlen", vlen);
360 jsonw_name(w, "vars");
361 jsonw_start_array(w);
362 } else {
363 printf(" size=%u vlen=%u", t->size, vlen);
364 }
365 for (i = 0; i < vlen; i++, v++) {
366 if (json_output) {
367 jsonw_start_object(w);
368 jsonw_uint_field(w, "type_id", v->type);
369 jsonw_uint_field(w, "offset", v->offset);
370 jsonw_uint_field(w, "size", v->size);
371 jsonw_end_object(w);
372 } else {
373 printf("\n\ttype_id=%u offset=%u size=%u",
374 v->type, v->offset, v->size);
375
376 if (v->type < btf__type_cnt(btf)) {
377 vt = btf__type_by_id(btf, v->type);
378 printf(" (%s '%s')",
379 btf_kind_str[btf_kind_safe(btf_kind(vt))],
380 btf_str(btf, vt->name_off));
381 }
382 }
383 }
384 if (json_output)
385 jsonw_end_array(w);
386 break;
387 }
388 case BTF_KIND_FLOAT: {
389 if (json_output)
390 jsonw_uint_field(w, "size", t->size);
391 else
392 printf(" size=%u", t->size);
393 break;
394 }
395 case BTF_KIND_DECL_TAG: {
396 const struct btf_decl_tag *tag = (const void *)(t + 1);
397
398 if (json_output) {
399 jsonw_uint_field(w, "type_id", t->type);
400 jsonw_int_field(w, "component_idx", tag->component_idx);
401 } else {
402 printf(" type_id=%u component_idx=%d", t->type, tag->component_idx);
403 }
404 break;
405 }
406 default:
407 break;
408 }
409
410 if (json_output)
411 jsonw_end_object(json_wtr);
412 else
413 printf("\n");
414
415 return 0;
416}
417
418static int dump_btf_raw(const struct btf *btf,
419 __u32 *root_type_ids, int root_type_cnt)
420{
421 const struct btf_type *t;
422 int i;
423
424 if (json_output) {
425 jsonw_start_object(json_wtr);
426 jsonw_name(json_wtr, "types");
427 jsonw_start_array(json_wtr);
428 }
429
430 if (root_type_cnt) {
431 for (i = 0; i < root_type_cnt; i++) {
432 t = btf__type_by_id(btf, root_type_ids[i]);
433 dump_btf_type(btf, root_type_ids[i], t);
434 }
435 } else {
436 const struct btf *base;
437 int cnt = btf__type_cnt(btf);
438 int start_id = 1;
439
440 base = btf__base_btf(btf);
441 if (base)
442 start_id = btf__type_cnt(base);
443
444 for (i = start_id; i < cnt; i++) {
445 t = btf__type_by_id(btf, i);
446 dump_btf_type(btf, i, t);
447 }
448 }
449
450 if (json_output) {
451 jsonw_end_array(json_wtr);
452 jsonw_end_object(json_wtr);
453 }
454 return 0;
455}
456
457static void __printf(2, 0) btf_dump_printf(void *ctx,
458 const char *fmt, va_list args)
459{
460 vfprintf(stdout, fmt, args);
461}
462
463static int dump_btf_c(const struct btf *btf,
464 __u32 *root_type_ids, int root_type_cnt)
465{
466 struct btf_dump *d;
467 int err = 0, i;
468
469 d = btf_dump__new(btf, btf_dump_printf, NULL, NULL);
470 if (!d)
471 return -errno;
472
473 printf("#ifndef __VMLINUX_H__\n");
474 printf("#define __VMLINUX_H__\n");
475 printf("\n");
476 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
477 printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
478 printf("#endif\n\n");
479
480 if (root_type_cnt) {
481 for (i = 0; i < root_type_cnt; i++) {
482 err = btf_dump__dump_type(d, root_type_ids[i]);
483 if (err)
484 goto done;
485 }
486 } else {
487 int cnt = btf__type_cnt(btf);
488
489 for (i = 1; i < cnt; i++) {
490 err = btf_dump__dump_type(d, i);
491 if (err)
492 goto done;
493 }
494 }
495
496 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
497 printf("#pragma clang attribute pop\n");
498 printf("#endif\n");
499 printf("\n");
500 printf("#endif /* __VMLINUX_H__ */\n");
501
502done:
503 btf_dump__free(d);
504 return err;
505}
506
507static const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
508
509static struct btf *get_vmlinux_btf_from_sysfs(void)
510{
511 struct btf *base;
512
513 base = btf__parse(sysfs_vmlinux, NULL);
514 if (!base)
515 p_err("failed to parse vmlinux BTF at '%s': %d\n",
516 sysfs_vmlinux, -errno);
517
518 return base;
519}
520
521#define BTF_NAME_BUFF_LEN 64
522
523static bool btf_is_kernel_module(__u32 btf_id)
524{
525 struct bpf_btf_info btf_info = {};
526 char btf_name[BTF_NAME_BUFF_LEN];
527 int btf_fd;
528 __u32 len;
529 int err;
530
531 btf_fd = bpf_btf_get_fd_by_id(btf_id);
532 if (btf_fd < 0) {
533 p_err("can't get BTF object by id (%u): %s", btf_id, strerror(errno));
534 return false;
535 }
536
537 len = sizeof(btf_info);
538 btf_info.name = ptr_to_u64(btf_name);
539 btf_info.name_len = sizeof(btf_name);
540 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
541 close(btf_fd);
542 if (err) {
543 p_err("can't get BTF (ID %u) object info: %s", btf_id, strerror(errno));
544 return false;
545 }
546
547 return btf_info.kernel_btf && strncmp(btf_name, "vmlinux", sizeof(btf_name)) != 0;
548}
549
550static int do_dump(int argc, char **argv)
551{
552 struct btf *btf = NULL, *base = NULL;
553 __u32 root_type_ids[2];
554 int root_type_cnt = 0;
555 bool dump_c = false;
556 __u32 btf_id = -1;
557 const char *src;
558 int fd = -1;
559 int err = 0;
560
561 if (!REQ_ARGS(2)) {
562 usage();
563 return -1;
564 }
565 src = GET_ARG();
566 if (is_prefix(src, "map")) {
567 struct bpf_map_info info = {};
568 __u32 len = sizeof(info);
569
570 if (!REQ_ARGS(2)) {
571 usage();
572 return -1;
573 }
574
575 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
576 if (fd < 0)
577 return -1;
578
579 btf_id = info.btf_id;
580 if (argc && is_prefix(*argv, "key")) {
581 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
582 NEXT_ARG();
583 } else if (argc && is_prefix(*argv, "value")) {
584 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
585 NEXT_ARG();
586 } else if (argc && is_prefix(*argv, "all")) {
587 NEXT_ARG();
588 } else if (argc && is_prefix(*argv, "kv")) {
589 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
590 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
591 NEXT_ARG();
592 } else {
593 root_type_ids[root_type_cnt++] = info.btf_key_type_id;
594 root_type_ids[root_type_cnt++] = info.btf_value_type_id;
595 }
596 } else if (is_prefix(src, "prog")) {
597 struct bpf_prog_info info = {};
598 __u32 len = sizeof(info);
599
600 if (!REQ_ARGS(2)) {
601 usage();
602 return -1;
603 }
604
605 fd = prog_parse_fd(&argc, &argv);
606 if (fd < 0)
607 return -1;
608
609 err = bpf_obj_get_info_by_fd(fd, &info, &len);
610 if (err) {
611 p_err("can't get prog info: %s", strerror(errno));
612 goto done;
613 }
614
615 btf_id = info.btf_id;
616 } else if (is_prefix(src, "id")) {
617 char *endptr;
618
619 btf_id = strtoul(*argv, &endptr, 0);
620 if (*endptr) {
621 p_err("can't parse %s as ID", *argv);
622 return -1;
623 }
624 NEXT_ARG();
625 } else if (is_prefix(src, "file")) {
626 const char sysfs_prefix[] = "/sys/kernel/btf/";
627
628 if (!base_btf &&
629 strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
630 strcmp(*argv, sysfs_vmlinux) != 0)
631 base = get_vmlinux_btf_from_sysfs();
632
633 btf = btf__parse_split(*argv, base ?: base_btf);
634 if (!btf) {
635 err = -errno;
636 p_err("failed to load BTF from %s: %s",
637 *argv, strerror(errno));
638 goto done;
639 }
640 NEXT_ARG();
641 } else {
642 err = -1;
643 p_err("unrecognized BTF source specifier: '%s'", src);
644 goto done;
645 }
646
647 while (argc) {
648 if (is_prefix(*argv, "format")) {
649 NEXT_ARG();
650 if (argc < 1) {
651 p_err("expecting value for 'format' option\n");
652 err = -EINVAL;
653 goto done;
654 }
655 if (strcmp(*argv, "c") == 0) {
656 dump_c = true;
657 } else if (strcmp(*argv, "raw") == 0) {
658 dump_c = false;
659 } else {
660 p_err("unrecognized format specifier: '%s', possible values: raw, c",
661 *argv);
662 err = -EINVAL;
663 goto done;
664 }
665 NEXT_ARG();
666 } else {
667 p_err("unrecognized option: '%s'", *argv);
668 err = -EINVAL;
669 goto done;
670 }
671 }
672
673 if (!btf) {
674 if (!base_btf && btf_is_kernel_module(btf_id)) {
675 p_info("Warning: valid base BTF was not specified with -B option, falling back to standard base BTF (%s)",
676 sysfs_vmlinux);
677 base_btf = get_vmlinux_btf_from_sysfs();
678 }
679
680 btf = btf__load_from_kernel_by_id_split(btf_id, base_btf);
681 if (!btf) {
682 err = -errno;
683 p_err("get btf by id (%u): %s", btf_id, strerror(errno));
684 goto done;
685 }
686 }
687
688 if (dump_c) {
689 if (json_output) {
690 p_err("JSON output for C-syntax dump is not supported");
691 err = -ENOTSUP;
692 goto done;
693 }
694 err = dump_btf_c(btf, root_type_ids, root_type_cnt);
695 } else {
696 err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
697 }
698
699done:
700 close(fd);
701 btf__free(btf);
702 btf__free(base);
703 return err;
704}
705
706static int btf_parse_fd(int *argc, char ***argv)
707{
708 unsigned int id;
709 char *endptr;
710 int fd;
711
712 if (!is_prefix(*argv[0], "id")) {
713 p_err("expected 'id', got: '%s'?", **argv);
714 return -1;
715 }
716 NEXT_ARGP();
717
718 id = strtoul(**argv, &endptr, 0);
719 if (*endptr) {
720 p_err("can't parse %s as ID", **argv);
721 return -1;
722 }
723 NEXT_ARGP();
724
725 fd = bpf_btf_get_fd_by_id(id);
726 if (fd < 0)
727 p_err("can't get BTF object by id (%u): %s",
728 id, strerror(errno));
729
730 return fd;
731}
732
733static int
734build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
735 void *info, __u32 *len)
736{
737 static const char * const names[] = {
738 [BPF_OBJ_UNKNOWN] = "unknown",
739 [BPF_OBJ_PROG] = "prog",
740 [BPF_OBJ_MAP] = "map",
741 };
742 __u32 btf_id, id = 0;
743 int err;
744 int fd;
745
746 while (true) {
747 switch (type) {
748 case BPF_OBJ_PROG:
749 err = bpf_prog_get_next_id(id, &id);
750 break;
751 case BPF_OBJ_MAP:
752 err = bpf_map_get_next_id(id, &id);
753 break;
754 default:
755 err = -1;
756 p_err("unexpected object type: %d", type);
757 goto err_free;
758 }
759 if (err) {
760 if (errno == ENOENT) {
761 err = 0;
762 break;
763 }
764 p_err("can't get next %s: %s%s", names[type],
765 strerror(errno),
766 errno == EINVAL ? " -- kernel too old?" : "");
767 goto err_free;
768 }
769
770 switch (type) {
771 case BPF_OBJ_PROG:
772 fd = bpf_prog_get_fd_by_id(id);
773 break;
774 case BPF_OBJ_MAP:
775 fd = bpf_map_get_fd_by_id(id);
776 break;
777 default:
778 err = -1;
779 p_err("unexpected object type: %d", type);
780 goto err_free;
781 }
782 if (fd < 0) {
783 if (errno == ENOENT)
784 continue;
785 p_err("can't get %s by id (%u): %s", names[type], id,
786 strerror(errno));
787 err = -1;
788 goto err_free;
789 }
790
791 memset(info, 0, *len);
792 err = bpf_obj_get_info_by_fd(fd, info, len);
793 close(fd);
794 if (err) {
795 p_err("can't get %s info: %s", names[type],
796 strerror(errno));
797 goto err_free;
798 }
799
800 switch (type) {
801 case BPF_OBJ_PROG:
802 btf_id = ((struct bpf_prog_info *)info)->btf_id;
803 break;
804 case BPF_OBJ_MAP:
805 btf_id = ((struct bpf_map_info *)info)->btf_id;
806 break;
807 default:
808 err = -1;
809 p_err("unexpected object type: %d", type);
810 goto err_free;
811 }
812 if (!btf_id)
813 continue;
814
815 err = hashmap__append(tab, btf_id, id);
816 if (err) {
817 p_err("failed to append entry to hashmap for BTF ID %u, object ID %u: %s",
818 btf_id, id, strerror(-err));
819 goto err_free;
820 }
821 }
822
823 return 0;
824
825err_free:
826 hashmap__free(tab);
827 return err;
828}
829
830static int
831build_btf_tables(struct hashmap *btf_prog_table,
832 struct hashmap *btf_map_table)
833{
834 struct bpf_prog_info prog_info;
835 __u32 prog_len = sizeof(prog_info);
836 struct bpf_map_info map_info;
837 __u32 map_len = sizeof(map_info);
838 int err = 0;
839
840 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
841 &prog_len);
842 if (err)
843 return err;
844
845 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
846 &map_len);
847 if (err) {
848 hashmap__free(btf_prog_table);
849 return err;
850 }
851
852 return 0;
853}
854
855static void
856show_btf_plain(struct bpf_btf_info *info, int fd,
857 struct hashmap *btf_prog_table,
858 struct hashmap *btf_map_table)
859{
860 struct hashmap_entry *entry;
861 const char *name = u64_to_ptr(info->name);
862 int n;
863
864 printf("%u: ", info->id);
865 if (info->kernel_btf)
866 printf("name [%s] ", name);
867 else if (name && name[0])
868 printf("name %s ", name);
869 else
870 printf("name <anon> ");
871 printf("size %uB", info->btf_size);
872
873 n = 0;
874 hashmap__for_each_key_entry(btf_prog_table, entry, info->id) {
875 printf("%s%lu", n++ == 0 ? " prog_ids " : ",", entry->value);
876 }
877
878 n = 0;
879 hashmap__for_each_key_entry(btf_map_table, entry, info->id) {
880 printf("%s%lu", n++ == 0 ? " map_ids " : ",", entry->value);
881 }
882
883 emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
884
885 printf("\n");
886}
887
888static void
889show_btf_json(struct bpf_btf_info *info, int fd,
890 struct hashmap *btf_prog_table,
891 struct hashmap *btf_map_table)
892{
893 struct hashmap_entry *entry;
894 const char *name = u64_to_ptr(info->name);
895
896 jsonw_start_object(json_wtr); /* btf object */
897 jsonw_uint_field(json_wtr, "id", info->id);
898 jsonw_uint_field(json_wtr, "size", info->btf_size);
899
900 jsonw_name(json_wtr, "prog_ids");
901 jsonw_start_array(json_wtr); /* prog_ids */
902 hashmap__for_each_key_entry(btf_prog_table, entry, info->id) {
903 jsonw_uint(json_wtr, entry->value);
904 }
905 jsonw_end_array(json_wtr); /* prog_ids */
906
907 jsonw_name(json_wtr, "map_ids");
908 jsonw_start_array(json_wtr); /* map_ids */
909 hashmap__for_each_key_entry(btf_map_table, entry, info->id) {
910 jsonw_uint(json_wtr, entry->value);
911 }
912 jsonw_end_array(json_wtr); /* map_ids */
913
914 emit_obj_refs_json(refs_table, info->id, json_wtr); /* pids */
915
916 jsonw_bool_field(json_wtr, "kernel", info->kernel_btf);
917
918 if (name && name[0])
919 jsonw_string_field(json_wtr, "name", name);
920
921 jsonw_end_object(json_wtr); /* btf object */
922}
923
924static int
925show_btf(int fd, struct hashmap *btf_prog_table,
926 struct hashmap *btf_map_table)
927{
928 struct bpf_btf_info info;
929 __u32 len = sizeof(info);
930 char name[64];
931 int err;
932
933 memset(&info, 0, sizeof(info));
934 err = bpf_obj_get_info_by_fd(fd, &info, &len);
935 if (err) {
936 p_err("can't get BTF object info: %s", strerror(errno));
937 return -1;
938 }
939 /* if kernel support emitting BTF object name, pass name pointer */
940 if (info.name_len) {
941 memset(&info, 0, sizeof(info));
942 info.name_len = sizeof(name);
943 info.name = ptr_to_u64(name);
944 len = sizeof(info);
945
946 err = bpf_obj_get_info_by_fd(fd, &info, &len);
947 if (err) {
948 p_err("can't get BTF object info: %s", strerror(errno));
949 return -1;
950 }
951 }
952
953 if (json_output)
954 show_btf_json(&info, fd, btf_prog_table, btf_map_table);
955 else
956 show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
957
958 return 0;
959}
960
961static int do_show(int argc, char **argv)
962{
963 struct hashmap *btf_prog_table;
964 struct hashmap *btf_map_table;
965 int err, fd = -1;
966 __u32 id = 0;
967
968 if (argc == 2) {
969 fd = btf_parse_fd(&argc, &argv);
970 if (fd < 0)
971 return -1;
972 }
973
974 if (argc) {
975 if (fd >= 0)
976 close(fd);
977 return BAD_ARG();
978 }
979
980 btf_prog_table = hashmap__new(hash_fn_for_key_as_id,
981 equal_fn_for_key_as_id, NULL);
982 btf_map_table = hashmap__new(hash_fn_for_key_as_id,
983 equal_fn_for_key_as_id, NULL);
984 if (IS_ERR(btf_prog_table) || IS_ERR(btf_map_table)) {
985 hashmap__free(btf_prog_table);
986 hashmap__free(btf_map_table);
987 if (fd >= 0)
988 close(fd);
989 p_err("failed to create hashmap for object references");
990 return -1;
991 }
992 err = build_btf_tables(btf_prog_table, btf_map_table);
993 if (err) {
994 if (fd >= 0)
995 close(fd);
996 return err;
997 }
998 build_obj_refs_table(&refs_table, BPF_OBJ_BTF);
999
1000 if (fd >= 0) {
1001 err = show_btf(fd, btf_prog_table, btf_map_table);
1002 close(fd);
1003 goto exit_free;
1004 }
1005
1006 if (json_output)
1007 jsonw_start_array(json_wtr); /* root array */
1008
1009 while (true) {
1010 err = bpf_btf_get_next_id(id, &id);
1011 if (err) {
1012 if (errno == ENOENT) {
1013 err = 0;
1014 break;
1015 }
1016 p_err("can't get next BTF object: %s%s",
1017 strerror(errno),
1018 errno == EINVAL ? " -- kernel too old?" : "");
1019 err = -1;
1020 break;
1021 }
1022
1023 fd = bpf_btf_get_fd_by_id(id);
1024 if (fd < 0) {
1025 if (errno == ENOENT)
1026 continue;
1027 p_err("can't get BTF object by id (%u): %s",
1028 id, strerror(errno));
1029 err = -1;
1030 break;
1031 }
1032
1033 err = show_btf(fd, btf_prog_table, btf_map_table);
1034 close(fd);
1035 if (err)
1036 break;
1037 }
1038
1039 if (json_output)
1040 jsonw_end_array(json_wtr); /* root array */
1041
1042exit_free:
1043 hashmap__free(btf_prog_table);
1044 hashmap__free(btf_map_table);
1045 delete_obj_refs_table(refs_table);
1046
1047 return err;
1048}
1049
1050static int do_help(int argc, char **argv)
1051{
1052 if (json_output) {
1053 jsonw_null(json_wtr);
1054 return 0;
1055 }
1056
1057 fprintf(stderr,
1058 "Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
1059 " %1$s %2$s dump BTF_SRC [format FORMAT]\n"
1060 " %1$s %2$s help\n"
1061 "\n"
1062 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
1063 " FORMAT := { raw | c }\n"
1064 " " HELP_SPEC_MAP "\n"
1065 " " HELP_SPEC_PROGRAM "\n"
1066 " " HELP_SPEC_OPTIONS " |\n"
1067 " {-B|--base-btf} }\n"
1068 "",
1069 bin_name, "btf");
1070
1071 return 0;
1072}
1073
1074static const struct cmd cmds[] = {
1075 { "show", do_show },
1076 { "list", do_show },
1077 { "help", do_help },
1078 { "dump", do_dump },
1079 { 0 }
1080};
1081
1082int do_btf(int argc, char **argv)
1083{
1084 return cmd_select(cmds, argc, argv, do_help);
1085}