1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2019 Facebook */ 3 4 #ifndef _GNU_SOURCE 5 #define _GNU_SOURCE 6 #endif 7 #include <errno.h> 8 #include <fcntl.h> 9 #include <linux/err.h> 10 #include <stdbool.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <unistd.h> 15 #include <linux/btf.h> 16 #include <sys/types.h> 17 #include <sys/stat.h> 18 19 #include <bpf/bpf.h> 20 #include <bpf/btf.h> 21 #include <bpf/hashmap.h> 22 #include <bpf/libbpf.h> 23 24 #include "json_writer.h" 25 #include "main.h" 26 27 #define KFUNC_DECL_TAG "bpf_kfunc" 28 #define FASTCALL_DECL_TAG "bpf_fastcall" 29 30 #define MAX_ROOT_IDS 16 31 #define MAX_BTF_FILES 64 32 33 static const char * const btf_kind_str[NR_BTF_KINDS] = { 34 [BTF_KIND_UNKN] = "UNKNOWN", 35 [BTF_KIND_INT] = "INT", 36 [BTF_KIND_PTR] = "PTR", 37 [BTF_KIND_ARRAY] = "ARRAY", 38 [BTF_KIND_STRUCT] = "STRUCT", 39 [BTF_KIND_UNION] = "UNION", 40 [BTF_KIND_ENUM] = "ENUM", 41 [BTF_KIND_FWD] = "FWD", 42 [BTF_KIND_TYPEDEF] = "TYPEDEF", 43 [BTF_KIND_VOLATILE] = "VOLATILE", 44 [BTF_KIND_CONST] = "CONST", 45 [BTF_KIND_RESTRICT] = "RESTRICT", 46 [BTF_KIND_FUNC] = "FUNC", 47 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", 48 [BTF_KIND_VAR] = "VAR", 49 [BTF_KIND_DATASEC] = "DATASEC", 50 [BTF_KIND_FLOAT] = "FLOAT", 51 [BTF_KIND_DECL_TAG] = "DECL_TAG", 52 [BTF_KIND_TYPE_TAG] = "TYPE_TAG", 53 [BTF_KIND_ENUM64] = "ENUM64", 54 }; 55 56 struct sort_datum { 57 int index; 58 int type_rank; 59 const char *sort_name; 60 const char *own_name; 61 __u64 disambig_hash; 62 }; 63 64 static const char *btf_int_enc_str(__u8 encoding) 65 { 66 switch (encoding) { 67 case 0: 68 return "(none)"; 69 case BTF_INT_SIGNED: 70 return "SIGNED"; 71 case BTF_INT_CHAR: 72 return "CHAR"; 73 case BTF_INT_BOOL: 74 return "BOOL"; 75 default: 76 return "UNKN"; 77 } 78 } 79 80 static const char *btf_var_linkage_str(__u32 linkage) 81 { 82 switch (linkage) { 83 case BTF_VAR_STATIC: 84 return "static"; 85 case BTF_VAR_GLOBAL_ALLOCATED: 86 return "global"; 87 case BTF_VAR_GLOBAL_EXTERN: 88 return "extern"; 89 default: 90 return "(unknown)"; 91 } 92 } 93 94 static const char *btf_func_linkage_str(const struct btf_type *t) 95 { 96 switch (btf_vlen(t)) { 97 case BTF_FUNC_STATIC: 98 return "static"; 99 case BTF_FUNC_GLOBAL: 100 return "global"; 101 case BTF_FUNC_EXTERN: 102 return "extern"; 103 default: 104 return "(unknown)"; 105 } 106 } 107 108 static const char *btf_str(const struct btf *btf, __u32 off) 109 { 110 if (!off) 111 return "(anon)"; 112 return btf__name_by_offset(btf, off) ? : "(invalid)"; 113 } 114 115 static int btf_kind_safe(int kind) 116 { 117 return kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN; 118 } 119 120 static int dump_btf_type(const struct btf *btf, __u32 id, 121 const struct btf_type *t) 122 { 123 json_writer_t *w = json_wtr; 124 int kind = btf_kind(t); 125 126 if (json_output) { 127 jsonw_start_object(w); 128 jsonw_uint_field(w, "id", id); 129 jsonw_string_field(w, "kind", btf_kind_str[btf_kind_safe(kind)]); 130 jsonw_string_field(w, "name", btf_str(btf, t->name_off)); 131 } else { 132 printf("[%u] %s '%s'", id, btf_kind_str[btf_kind_safe(kind)], 133 btf_str(btf, t->name_off)); 134 } 135 136 switch (kind) { 137 case BTF_KIND_INT: { 138 __u32 v = *(__u32 *)(t + 1); 139 const char *enc; 140 141 enc = btf_int_enc_str(BTF_INT_ENCODING(v)); 142 143 if (json_output) { 144 jsonw_uint_field(w, "size", t->size); 145 jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v)); 146 jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v)); 147 jsonw_string_field(w, "encoding", enc); 148 } else { 149 printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s", 150 t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v), 151 enc); 152 } 153 break; 154 } 155 case BTF_KIND_PTR: 156 case BTF_KIND_CONST: 157 case BTF_KIND_VOLATILE: 158 case BTF_KIND_RESTRICT: 159 case BTF_KIND_TYPEDEF: 160 case BTF_KIND_TYPE_TAG: 161 if (json_output) 162 jsonw_uint_field(w, "type_id", t->type); 163 else 164 printf(" type_id=%u", t->type); 165 break; 166 case BTF_KIND_ARRAY: { 167 const struct btf_array *arr = (const void *)(t + 1); 168 169 if (json_output) { 170 jsonw_uint_field(w, "type_id", arr->type); 171 jsonw_uint_field(w, "index_type_id", arr->index_type); 172 jsonw_uint_field(w, "nr_elems", arr->nelems); 173 } else { 174 printf(" type_id=%u index_type_id=%u nr_elems=%u", 175 arr->type, arr->index_type, arr->nelems); 176 } 177 break; 178 } 179 case BTF_KIND_STRUCT: 180 case BTF_KIND_UNION: { 181 const struct btf_member *m = (const void *)(t + 1); 182 __u32 i, vlen = btf_vlen(t); 183 184 if (json_output) { 185 jsonw_uint_field(w, "size", t->size); 186 jsonw_uint_field(w, "vlen", vlen); 187 jsonw_name(w, "members"); 188 jsonw_start_array(w); 189 } else { 190 printf(" size=%u vlen=%u", t->size, vlen); 191 } 192 for (i = 0; i < vlen; i++, m++) { 193 const char *name = btf_str(btf, m->name_off); 194 __u32 bit_off, bit_sz; 195 196 if (btf_kflag(t)) { 197 bit_off = BTF_MEMBER_BIT_OFFSET(m->offset); 198 bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset); 199 } else { 200 bit_off = m->offset; 201 bit_sz = 0; 202 } 203 204 if (json_output) { 205 jsonw_start_object(w); 206 jsonw_string_field(w, "name", name); 207 jsonw_uint_field(w, "type_id", m->type); 208 jsonw_uint_field(w, "bits_offset", bit_off); 209 if (bit_sz) { 210 jsonw_uint_field(w, "bitfield_size", 211 bit_sz); 212 } 213 jsonw_end_object(w); 214 } else { 215 printf("\n\t'%s' type_id=%u bits_offset=%u", 216 name, m->type, bit_off); 217 if (bit_sz) 218 printf(" bitfield_size=%u", bit_sz); 219 } 220 } 221 if (json_output) 222 jsonw_end_array(w); 223 break; 224 } 225 case BTF_KIND_ENUM: { 226 const struct btf_enum *v = (const void *)(t + 1); 227 __u32 i, vlen = btf_vlen(t); 228 const char *encoding; 229 230 encoding = btf_kflag(t) ? "SIGNED" : "UNSIGNED"; 231 if (json_output) { 232 jsonw_string_field(w, "encoding", encoding); 233 jsonw_uint_field(w, "size", t->size); 234 jsonw_uint_field(w, "vlen", vlen); 235 jsonw_name(w, "values"); 236 jsonw_start_array(w); 237 } else { 238 printf(" encoding=%s size=%u vlen=%u", encoding, t->size, vlen); 239 } 240 for (i = 0; i < vlen; i++, v++) { 241 const char *name = btf_str(btf, v->name_off); 242 243 if (json_output) { 244 jsonw_start_object(w); 245 jsonw_string_field(w, "name", name); 246 if (btf_kflag(t)) 247 jsonw_int_field(w, "val", v->val); 248 else 249 jsonw_uint_field(w, "val", v->val); 250 jsonw_end_object(w); 251 } else { 252 if (btf_kflag(t)) 253 printf("\n\t'%s' val=%d", name, v->val); 254 else 255 printf("\n\t'%s' val=%u", name, (__u32)v->val); 256 } 257 } 258 if (json_output) 259 jsonw_end_array(w); 260 break; 261 } 262 case BTF_KIND_ENUM64: { 263 const struct btf_enum64 *v = btf_enum64(t); 264 __u32 i, vlen = btf_vlen(t); 265 const char *encoding; 266 267 encoding = btf_kflag(t) ? "SIGNED" : "UNSIGNED"; 268 if (json_output) { 269 jsonw_string_field(w, "encoding", encoding); 270 jsonw_uint_field(w, "size", t->size); 271 jsonw_uint_field(w, "vlen", vlen); 272 jsonw_name(w, "values"); 273 jsonw_start_array(w); 274 } else { 275 printf(" encoding=%s size=%u vlen=%u", encoding, t->size, vlen); 276 } 277 for (i = 0; i < vlen; i++, v++) { 278 const char *name = btf_str(btf, v->name_off); 279 __u64 val = ((__u64)v->val_hi32 << 32) | v->val_lo32; 280 281 if (json_output) { 282 jsonw_start_object(w); 283 jsonw_string_field(w, "name", name); 284 if (btf_kflag(t)) 285 jsonw_int_field(w, "val", val); 286 else 287 jsonw_uint_field(w, "val", val); 288 jsonw_end_object(w); 289 } else { 290 if (btf_kflag(t)) 291 printf("\n\t'%s' val=%lldLL", name, 292 (long long)val); 293 else 294 printf("\n\t'%s' val=%lluULL", name, 295 (unsigned long long)val); 296 } 297 } 298 if (json_output) 299 jsonw_end_array(w); 300 break; 301 } 302 case BTF_KIND_FWD: { 303 const char *fwd_kind = btf_kflag(t) ? "union" : "struct"; 304 305 if (json_output) 306 jsonw_string_field(w, "fwd_kind", fwd_kind); 307 else 308 printf(" fwd_kind=%s", fwd_kind); 309 break; 310 } 311 case BTF_KIND_FUNC: { 312 const char *linkage = btf_func_linkage_str(t); 313 314 if (json_output) { 315 jsonw_uint_field(w, "type_id", t->type); 316 jsonw_string_field(w, "linkage", linkage); 317 } else { 318 printf(" type_id=%u linkage=%s", t->type, linkage); 319 } 320 break; 321 } 322 case BTF_KIND_FUNC_PROTO: { 323 const struct btf_param *p = (const void *)(t + 1); 324 __u32 i, vlen = btf_vlen(t); 325 326 if (json_output) { 327 jsonw_uint_field(w, "ret_type_id", t->type); 328 jsonw_uint_field(w, "vlen", vlen); 329 jsonw_name(w, "params"); 330 jsonw_start_array(w); 331 } else { 332 printf(" ret_type_id=%u vlen=%u", t->type, vlen); 333 } 334 for (i = 0; i < vlen; i++, p++) { 335 const char *name = btf_str(btf, p->name_off); 336 337 if (json_output) { 338 jsonw_start_object(w); 339 jsonw_string_field(w, "name", name); 340 jsonw_uint_field(w, "type_id", p->type); 341 jsonw_end_object(w); 342 } else { 343 printf("\n\t'%s' type_id=%u", name, p->type); 344 } 345 } 346 if (json_output) 347 jsonw_end_array(w); 348 break; 349 } 350 case BTF_KIND_VAR: { 351 const struct btf_var *v = (const void *)(t + 1); 352 const char *linkage; 353 354 linkage = btf_var_linkage_str(v->linkage); 355 356 if (json_output) { 357 jsonw_uint_field(w, "type_id", t->type); 358 jsonw_string_field(w, "linkage", linkage); 359 } else { 360 printf(" type_id=%u, linkage=%s", t->type, linkage); 361 } 362 break; 363 } 364 case BTF_KIND_DATASEC: { 365 const struct btf_var_secinfo *v = (const void *)(t + 1); 366 const struct btf_type *vt; 367 __u32 i, vlen = btf_vlen(t); 368 369 if (json_output) { 370 jsonw_uint_field(w, "size", t->size); 371 jsonw_uint_field(w, "vlen", vlen); 372 jsonw_name(w, "vars"); 373 jsonw_start_array(w); 374 } else { 375 printf(" size=%u vlen=%u", t->size, vlen); 376 } 377 for (i = 0; i < vlen; i++, v++) { 378 if (json_output) { 379 jsonw_start_object(w); 380 jsonw_uint_field(w, "type_id", v->type); 381 jsonw_uint_field(w, "offset", v->offset); 382 jsonw_uint_field(w, "size", v->size); 383 jsonw_end_object(w); 384 } else { 385 printf("\n\ttype_id=%u offset=%u size=%u", 386 v->type, v->offset, v->size); 387 388 if (v->type < btf__type_cnt(btf)) { 389 vt = btf__type_by_id(btf, v->type); 390 printf(" (%s '%s')", 391 btf_kind_str[btf_kind_safe(btf_kind(vt))], 392 btf_str(btf, vt->name_off)); 393 } 394 } 395 } 396 if (json_output) 397 jsonw_end_array(w); 398 break; 399 } 400 case BTF_KIND_FLOAT: { 401 if (json_output) 402 jsonw_uint_field(w, "size", t->size); 403 else 404 printf(" size=%u", t->size); 405 break; 406 } 407 case BTF_KIND_DECL_TAG: { 408 const struct btf_decl_tag *tag = (const void *)(t + 1); 409 410 if (json_output) { 411 jsonw_uint_field(w, "type_id", t->type); 412 jsonw_int_field(w, "component_idx", tag->component_idx); 413 } else { 414 printf(" type_id=%u component_idx=%d", t->type, tag->component_idx); 415 } 416 break; 417 } 418 default: 419 break; 420 } 421 422 if (json_output) 423 jsonw_end_object(json_wtr); 424 else 425 printf("\n"); 426 427 return 0; 428 } 429 430 static int dump_btf_raw(const struct btf *btf, 431 __u32 *root_type_ids, int root_type_cnt) 432 { 433 const struct btf_type *t; 434 int i; 435 436 if (json_output) { 437 jsonw_start_object(json_wtr); 438 jsonw_name(json_wtr, "types"); 439 jsonw_start_array(json_wtr); 440 } 441 442 if (root_type_cnt) { 443 for (i = 0; i < root_type_cnt; i++) { 444 t = btf__type_by_id(btf, root_type_ids[i]); 445 dump_btf_type(btf, root_type_ids[i], t); 446 } 447 } else { 448 const struct btf *base; 449 int cnt = btf__type_cnt(btf); 450 int start_id = 1; 451 452 base = btf__base_btf(btf); 453 if (base) 454 start_id = btf__type_cnt(base); 455 456 for (i = start_id; i < cnt; i++) { 457 t = btf__type_by_id(btf, i); 458 dump_btf_type(btf, i, t); 459 } 460 } 461 462 if (json_output) { 463 jsonw_end_array(json_wtr); 464 jsonw_end_object(json_wtr); 465 } 466 return 0; 467 } 468 469 struct ptr_array { 470 __u32 cnt; 471 __u32 cap; 472 const void **elems; 473 }; 474 475 static int ptr_array_push(const void *ptr, struct ptr_array *arr) 476 { 477 __u32 new_cap; 478 void *tmp; 479 480 if (arr->cnt == arr->cap) { 481 new_cap = (arr->cap ?: 16) * 2; 482 tmp = realloc(arr->elems, sizeof(*arr->elems) * new_cap); 483 if (!tmp) 484 return -ENOMEM; 485 arr->elems = tmp; 486 arr->cap = new_cap; 487 } 488 arr->elems[arr->cnt++] = ptr; 489 return 0; 490 } 491 492 static void ptr_array_free(struct ptr_array *arr) 493 { 494 free(arr->elems); 495 } 496 497 static int cmp_kfuncs(const void *pa, const void *pb, void *ctx) 498 { 499 struct btf *btf = ctx; 500 const struct btf_type *a = *(void **)pa; 501 const struct btf_type *b = *(void **)pb; 502 503 return strcmp(btf__str_by_offset(btf, a->name_off), 504 btf__str_by_offset(btf, b->name_off)); 505 } 506 507 static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf) 508 { 509 LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts); 510 __u32 cnt = btf__type_cnt(btf), i, j; 511 struct ptr_array fastcalls = {}; 512 struct ptr_array kfuncs = {}; 513 int err = 0; 514 515 printf("\n/* BPF kfuncs */\n"); 516 printf("#ifndef BPF_NO_KFUNC_PROTOTYPES\n"); 517 518 for (i = 1; i < cnt; i++) { 519 const struct btf_type *t = btf__type_by_id(btf, i); 520 const struct btf_type *ft; 521 const char *name; 522 523 if (!btf_is_decl_tag(t)) 524 continue; 525 526 if (btf_decl_tag(t)->component_idx != -1) 527 continue; 528 529 ft = btf__type_by_id(btf, t->type); 530 if (!btf_is_func(ft)) 531 continue; 532 533 name = btf__name_by_offset(btf, t->name_off); 534 if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)) == 0) { 535 err = ptr_array_push(ft, &kfuncs); 536 if (err) 537 goto out; 538 } 539 540 if (strncmp(name, FASTCALL_DECL_TAG, sizeof(FASTCALL_DECL_TAG)) == 0) { 541 err = ptr_array_push(ft, &fastcalls); 542 if (err) 543 goto out; 544 } 545 } 546 547 /* Sort kfuncs by name for improved vmlinux.h stability */ 548 qsort_r(kfuncs.elems, kfuncs.cnt, sizeof(*kfuncs.elems), cmp_kfuncs, (void *)btf); 549 for (i = 0; i < kfuncs.cnt; i++) { 550 const struct btf_type *t = kfuncs.elems[i]; 551 552 printf("extern "); 553 554 /* Assume small amount of fastcall kfuncs */ 555 for (j = 0; j < fastcalls.cnt; j++) { 556 if (fastcalls.elems[j] == t) { 557 printf("__bpf_fastcall "); 558 break; 559 } 560 } 561 562 opts.field_name = btf__name_by_offset(btf, t->name_off); 563 err = btf_dump__emit_type_decl(d, t->type, &opts); 564 if (err) 565 goto out; 566 567 printf(" __weak __ksym;\n"); 568 } 569 570 printf("#endif\n\n"); 571 572 out: 573 ptr_array_free(&fastcalls); 574 ptr_array_free(&kfuncs); 575 return err; 576 } 577 578 static void __printf(2, 0) btf_dump_printf(void *ctx, 579 const char *fmt, va_list args) 580 { 581 vfprintf(stdout, fmt, args); 582 } 583 584 static int btf_type_rank(const struct btf *btf, __u32 index, bool has_name) 585 { 586 const struct btf_type *t = btf__type_by_id(btf, index); 587 const int kind = btf_kind(t); 588 const int max_rank = 10; 589 590 if (t->name_off) 591 has_name = true; 592 593 switch (kind) { 594 case BTF_KIND_ENUM: 595 case BTF_KIND_ENUM64: 596 return has_name ? 1 : 0; 597 case BTF_KIND_INT: 598 case BTF_KIND_FLOAT: 599 return 2; 600 case BTF_KIND_STRUCT: 601 case BTF_KIND_UNION: 602 return has_name ? 3 : max_rank; 603 case BTF_KIND_FUNC_PROTO: 604 return has_name ? 4 : max_rank; 605 case BTF_KIND_ARRAY: 606 if (has_name) 607 return btf_type_rank(btf, btf_array(t)->type, has_name); 608 return max_rank; 609 case BTF_KIND_TYPE_TAG: 610 case BTF_KIND_CONST: 611 case BTF_KIND_PTR: 612 case BTF_KIND_VOLATILE: 613 case BTF_KIND_RESTRICT: 614 case BTF_KIND_TYPEDEF: 615 case BTF_KIND_DECL_TAG: 616 if (has_name) 617 return btf_type_rank(btf, t->type, has_name); 618 return max_rank; 619 default: 620 return max_rank; 621 } 622 } 623 624 static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool from_ref) 625 { 626 const struct btf_type *t = btf__type_by_id(btf, index); 627 628 switch (btf_kind(t)) { 629 case BTF_KIND_ENUM: 630 case BTF_KIND_ENUM64: { 631 int name_off = t->name_off; 632 633 if (!from_ref && !name_off && btf_vlen(t)) 634 name_off = btf_kind(t) == BTF_KIND_ENUM64 ? 635 btf_enum64(t)->name_off : 636 btf_enum(t)->name_off; 637 638 return btf__name_by_offset(btf, name_off); 639 } 640 case BTF_KIND_ARRAY: 641 return btf_type_sort_name(btf, btf_array(t)->type, true); 642 case BTF_KIND_TYPE_TAG: 643 case BTF_KIND_CONST: 644 case BTF_KIND_PTR: 645 case BTF_KIND_VOLATILE: 646 case BTF_KIND_RESTRICT: 647 case BTF_KIND_TYPEDEF: 648 case BTF_KIND_DECL_TAG: 649 return btf_type_sort_name(btf, t->type, true); 650 default: 651 return btf__name_by_offset(btf, t->name_off); 652 } 653 return NULL; 654 } 655 656 static __u64 hasher(__u64 hash, __u64 val) 657 { 658 return hash * 31 + val; 659 } 660 661 static __u64 btf_name_hasher(__u64 hash, const struct btf *btf, __u32 name_off) 662 { 663 if (!name_off) 664 return hash; 665 666 return hasher(hash, str_hash(btf__name_by_offset(btf, name_off))); 667 } 668 669 static __u64 btf_type_disambig_hash(const struct btf *btf, __u32 id, bool include_members) 670 { 671 const struct btf_type *t = btf__type_by_id(btf, id); 672 __u32 i; 673 size_t hash = 0; 674 675 hash = btf_name_hasher(hash, btf, t->name_off); 676 677 switch (btf_kind(t)) { 678 case BTF_KIND_ENUM: 679 case BTF_KIND_ENUM64: 680 for (i = 0; i < btf_vlen(t); i++) { 681 __u32 name_off = btf_is_enum(t) ? 682 btf_enum(t)[i].name_off : 683 btf_enum64(t)[i].name_off; 684 685 hash = btf_name_hasher(hash, btf, name_off); 686 } 687 break; 688 case BTF_KIND_STRUCT: 689 case BTF_KIND_UNION: 690 if (!include_members) 691 break; 692 for (i = 0; i < btf_vlen(t); i++) { 693 const struct btf_member *m = btf_members(t) + i; 694 695 hash = btf_name_hasher(hash, btf, m->name_off); 696 /* resolve field type's name and hash it as well */ 697 hash = hasher(hash, btf_type_disambig_hash(btf, m->type, false)); 698 } 699 break; 700 case BTF_KIND_TYPE_TAG: 701 case BTF_KIND_CONST: 702 case BTF_KIND_PTR: 703 case BTF_KIND_VOLATILE: 704 case BTF_KIND_RESTRICT: 705 case BTF_KIND_TYPEDEF: 706 case BTF_KIND_DECL_TAG: 707 hash = hasher(hash, btf_type_disambig_hash(btf, t->type, include_members)); 708 break; 709 case BTF_KIND_ARRAY: { 710 struct btf_array *arr = btf_array(t); 711 712 hash = hasher(hash, arr->nelems); 713 hash = hasher(hash, btf_type_disambig_hash(btf, arr->type, include_members)); 714 break; 715 } 716 default: 717 break; 718 } 719 return hash; 720 } 721 722 static int btf_type_compare(const void *left, const void *right) 723 { 724 const struct sort_datum *d1 = (const struct sort_datum *)left; 725 const struct sort_datum *d2 = (const struct sort_datum *)right; 726 int r; 727 728 r = d1->type_rank - d2->type_rank; 729 r = r ?: strcmp(d1->sort_name, d2->sort_name); 730 r = r ?: strcmp(d1->own_name, d2->own_name); 731 if (r) 732 return r; 733 734 if (d1->disambig_hash != d2->disambig_hash) 735 return d1->disambig_hash < d2->disambig_hash ? -1 : 1; 736 737 return d1->index - d2->index; 738 } 739 740 static struct sort_datum *sort_btf_c(const struct btf *btf) 741 { 742 struct sort_datum *datums; 743 int n; 744 745 n = btf__type_cnt(btf); 746 datums = malloc(sizeof(struct sort_datum) * n); 747 if (!datums) 748 return NULL; 749 750 for (int i = 0; i < n; ++i) { 751 struct sort_datum *d = datums + i; 752 const struct btf_type *t = btf__type_by_id(btf, i); 753 754 d->index = i; 755 d->type_rank = btf_type_rank(btf, i, false); 756 d->sort_name = btf_type_sort_name(btf, i, false); 757 d->own_name = btf__name_by_offset(btf, t->name_off); 758 d->disambig_hash = btf_type_disambig_hash(btf, i, true); 759 } 760 761 qsort(datums, n, sizeof(struct sort_datum), btf_type_compare); 762 763 return datums; 764 } 765 766 static int dump_btf_c(const struct btf *btf, 767 __u32 *root_type_ids, int root_type_cnt, bool sort_dump) 768 { 769 struct sort_datum *datums = NULL; 770 struct btf_dump *d; 771 int err = 0, i; 772 773 d = btf_dump__new(btf, btf_dump_printf, NULL, NULL); 774 if (!d) 775 return -errno; 776 777 printf("#ifndef __VMLINUX_H__\n"); 778 printf("#define __VMLINUX_H__\n"); 779 printf("\n"); 780 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n"); 781 printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n"); 782 printf("#endif\n\n"); 783 printf("#ifndef __ksym\n"); 784 printf("#define __ksym __attribute__((section(\".ksyms\")))\n"); 785 printf("#endif\n\n"); 786 printf("#ifndef __weak\n"); 787 printf("#define __weak __attribute__((weak))\n"); 788 printf("#endif\n\n"); 789 printf("#ifndef __bpf_fastcall\n"); 790 printf("#if __has_attribute(bpf_fastcall)\n"); 791 printf("#define __bpf_fastcall __attribute__((bpf_fastcall))\n"); 792 printf("#else\n"); 793 printf("#define __bpf_fastcall\n"); 794 printf("#endif\n"); 795 printf("#endif\n\n"); 796 797 if (root_type_cnt) { 798 for (i = 0; i < root_type_cnt; i++) { 799 err = btf_dump__dump_type(d, root_type_ids[i]); 800 if (err) 801 goto done; 802 } 803 } else { 804 int cnt = btf__type_cnt(btf); 805 806 if (sort_dump) 807 datums = sort_btf_c(btf); 808 for (i = 1; i < cnt; i++) { 809 int idx = datums ? datums[i].index : i; 810 811 err = btf_dump__dump_type(d, idx); 812 if (err) 813 goto done; 814 } 815 816 err = dump_btf_kfuncs(d, btf); 817 if (err) 818 goto done; 819 } 820 821 printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n"); 822 printf("#pragma clang attribute pop\n"); 823 printf("#endif\n"); 824 printf("\n"); 825 printf("#endif /* __VMLINUX_H__ */\n"); 826 827 done: 828 free(datums); 829 btf_dump__free(d); 830 return err; 831 } 832 833 static const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux"; 834 835 static struct btf *get_vmlinux_btf_from_sysfs(void) 836 { 837 struct btf *base; 838 839 base = btf__parse(sysfs_vmlinux, NULL); 840 if (!base) 841 p_err("failed to parse vmlinux BTF at '%s': %d\n", 842 sysfs_vmlinux, -errno); 843 844 return base; 845 } 846 847 #define BTF_NAME_BUFF_LEN 64 848 849 static bool btf_is_kernel_module(__u32 btf_id) 850 { 851 struct bpf_btf_info btf_info = {}; 852 char btf_name[BTF_NAME_BUFF_LEN]; 853 int btf_fd; 854 __u32 len; 855 int err; 856 857 btf_fd = bpf_btf_get_fd_by_id(btf_id); 858 if (btf_fd < 0) { 859 p_err("can't get BTF object by id (%u): %s", btf_id, strerror(errno)); 860 return false; 861 } 862 863 len = sizeof(btf_info); 864 btf_info.name = ptr_to_u64(btf_name); 865 btf_info.name_len = sizeof(btf_name); 866 err = bpf_btf_get_info_by_fd(btf_fd, &btf_info, &len); 867 close(btf_fd); 868 if (err) { 869 p_err("can't get BTF (ID %u) object info: %s", btf_id, strerror(errno)); 870 return false; 871 } 872 873 return btf_info.kernel_btf && strncmp(btf_name, "vmlinux", sizeof(btf_name)) != 0; 874 } 875 876 static struct btf *merge_btf_files(const char **files, int nr_files, 877 struct btf *vmlinux_base) 878 { 879 struct btf *combined, *mod; 880 int ret; 881 882 combined = btf__new_empty_split(vmlinux_base); 883 if (!combined) { 884 p_err("failed to create combined BTF: %s", strerror(errno)); 885 return NULL; 886 } 887 888 for (int j = 0; j < nr_files; j++) { 889 mod = btf__parse_split(files[j], vmlinux_base); 890 if (!mod) { 891 p_err("failed to load BTF from %s: %s", files[j], strerror(errno)); 892 btf__free(combined); 893 return NULL; 894 } 895 896 ret = btf__add_btf(combined, mod); 897 btf__free(mod); 898 if (ret < 0) { 899 p_err("failed to merge BTF from %s: %s", files[j], strerror(-ret)); 900 btf__free(combined); 901 return NULL; 902 } 903 } 904 905 ret = btf__dedup(combined, NULL); 906 if (ret) { 907 p_err("failed to dedup combined BTF: %s", strerror(-ret)); 908 btf__free(combined); 909 return NULL; 910 } 911 912 return combined; 913 } 914 915 static int do_dump(int argc, char **argv) 916 { 917 bool dump_c = false, sort_dump_c = true; 918 struct btf *btf = NULL, *base = NULL; 919 __u32 root_type_ids[MAX_ROOT_IDS]; 920 bool have_id_filtering; 921 int root_type_cnt = 0; 922 __u32 btf_id = -1; 923 const char *src; 924 int fd = -1; 925 int err = 0; 926 int i; 927 928 if (!REQ_ARGS(2)) { 929 usage(); 930 return -1; 931 } 932 src = GET_ARG(); 933 if (is_prefix(src, "map")) { 934 struct bpf_map_info info = {}; 935 __u32 len = sizeof(info); 936 937 if (!REQ_ARGS(2)) { 938 usage(); 939 return -1; 940 } 941 942 fd = map_parse_fd_and_info(&argc, &argv, &info, &len, 943 BPF_F_RDONLY); 944 if (fd < 0) 945 return -1; 946 947 btf_id = info.btf_id; 948 if (argc && is_prefix(*argv, "key")) { 949 root_type_ids[root_type_cnt++] = info.btf_key_type_id; 950 NEXT_ARG(); 951 } else if (argc && is_prefix(*argv, "value")) { 952 root_type_ids[root_type_cnt++] = info.btf_value_type_id; 953 NEXT_ARG(); 954 } else if (argc && is_prefix(*argv, "all")) { 955 NEXT_ARG(); 956 } else if (argc && is_prefix(*argv, "kv")) { 957 root_type_ids[root_type_cnt++] = info.btf_key_type_id; 958 root_type_ids[root_type_cnt++] = info.btf_value_type_id; 959 NEXT_ARG(); 960 } else { 961 root_type_ids[root_type_cnt++] = info.btf_key_type_id; 962 root_type_ids[root_type_cnt++] = info.btf_value_type_id; 963 } 964 } else if (is_prefix(src, "prog")) { 965 struct bpf_prog_info info = {}; 966 __u32 len = sizeof(info); 967 968 if (!REQ_ARGS(2)) { 969 usage(); 970 return -1; 971 } 972 973 fd = prog_parse_fd(&argc, &argv); 974 if (fd < 0) 975 return -1; 976 977 err = bpf_prog_get_info_by_fd(fd, &info, &len); 978 if (err) { 979 p_err("can't get prog info: %s", strerror(errno)); 980 goto done; 981 } 982 983 btf_id = info.btf_id; 984 } else if (is_prefix(src, "id")) { 985 char *endptr; 986 987 btf_id = strtoul(*argv, &endptr, 0); 988 if (*endptr) { 989 p_err("can't parse %s as ID", *argv); 990 return -1; 991 } 992 NEXT_ARG(); 993 } else if (is_prefix(src, "file")) { 994 const char sysfs_prefix[] = "/sys/kernel/btf/"; 995 struct btf *vmlinux_base = base_btf; 996 const char *files[MAX_BTF_FILES]; 997 int nr_files = 0; 998 999 /* First grab our argument, filtering out the sysfs_vmlinux. */ 1000 if (strcmp(*argv, sysfs_vmlinux) != 0) { 1001 files[nr_files++] = *argv; 1002 } else { 1003 p_info("skipping %s (will be loaded as base)", *argv); 1004 } 1005 NEXT_ARG(); 1006 1007 while (argc && is_prefix(*argv, "file")) { 1008 NEXT_ARG(); 1009 if (!REQ_ARGS(1)) { 1010 err = -EINVAL; 1011 goto done; 1012 } 1013 /* Filter out any sysfs vmlinux entries. */ 1014 if (strcmp(*argv, sysfs_vmlinux) == 0) { 1015 p_info("skipping %s (will be loaded as base)", *argv); 1016 NEXT_ARG(); 1017 continue; 1018 } 1019 if (nr_files >= MAX_BTF_FILES) { 1020 p_err("too many BTF files (max %d)", MAX_BTF_FILES); 1021 err = -E2BIG; 1022 goto done; 1023 } 1024 files[nr_files++] = *argv; 1025 NEXT_ARG(); 1026 } 1027 1028 /* Auto-detect vmlinux base if any file is from sysfs */ 1029 if (!vmlinux_base) { 1030 for (int j = 0; j < nr_files; j++) { 1031 if (strncmp(files[j], sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0) { 1032 base = get_vmlinux_btf_from_sysfs(); 1033 vmlinux_base = base; 1034 break; 1035 } 1036 } 1037 } 1038 1039 /* All files were the sysfs_vmlinux, handle it like we used to */ 1040 if (nr_files == 0) { 1041 nr_files = 1; 1042 files[0] = sysfs_vmlinux; 1043 } 1044 1045 if (nr_files == 1) { 1046 btf = btf__parse_split(files[0], base ?: base_btf); 1047 if (!btf) { 1048 err = -errno; 1049 p_err("failed to load BTF from %s: %s", files[0], strerror(errno)); 1050 goto done; 1051 } 1052 } else { 1053 if (!vmlinux_base) { 1054 p_err("base BTF is required when merging multiple BTF files; use -B/--base-btf or use sysfs paths"); 1055 err = -EINVAL; 1056 goto done; 1057 } 1058 1059 btf = merge_btf_files(files, nr_files, vmlinux_base); 1060 if (!btf) { 1061 err = -errno; 1062 goto done; 1063 } 1064 } 1065 } else { 1066 err = -1; 1067 p_err("unrecognized BTF source specifier: '%s'", src); 1068 goto done; 1069 } 1070 1071 have_id_filtering = !!root_type_cnt; 1072 1073 while (argc) { 1074 if (is_prefix(*argv, "format")) { 1075 NEXT_ARG(); 1076 if (argc < 1) { 1077 p_err("expecting value for 'format' option\n"); 1078 err = -EINVAL; 1079 goto done; 1080 } 1081 if (strcmp(*argv, "c") == 0) { 1082 dump_c = true; 1083 } else if (strcmp(*argv, "raw") == 0) { 1084 dump_c = false; 1085 } else { 1086 p_err("unrecognized format specifier: '%s', possible values: raw, c", 1087 *argv); 1088 err = -EINVAL; 1089 goto done; 1090 } 1091 NEXT_ARG(); 1092 } else if (is_prefix(*argv, "root_id")) { 1093 __u32 root_id; 1094 char *end; 1095 1096 if (have_id_filtering) { 1097 p_err("cannot use root_id with other type filtering"); 1098 err = -EINVAL; 1099 goto done; 1100 } else if (root_type_cnt == MAX_ROOT_IDS) { 1101 p_err("only %d root_id are supported", MAX_ROOT_IDS); 1102 err = -E2BIG; 1103 goto done; 1104 } 1105 1106 NEXT_ARG(); 1107 root_id = strtoul(*argv, &end, 0); 1108 if (*end) { 1109 err = -1; 1110 p_err("can't parse %s as root ID", *argv); 1111 goto done; 1112 } 1113 for (i = 0; i < root_type_cnt; i++) { 1114 if (root_type_ids[i] == root_id) { 1115 err = -EINVAL; 1116 p_err("duplicate root_id %u supplied", root_id); 1117 goto done; 1118 } 1119 } 1120 root_type_ids[root_type_cnt++] = root_id; 1121 NEXT_ARG(); 1122 } else if (is_prefix(*argv, "unsorted")) { 1123 sort_dump_c = false; 1124 NEXT_ARG(); 1125 } else { 1126 p_err("unrecognized option: '%s'", *argv); 1127 err = -EINVAL; 1128 goto done; 1129 } 1130 } 1131 1132 if (!btf) { 1133 if (!base_btf && btf_is_kernel_module(btf_id)) { 1134 p_info("Warning: valid base BTF was not specified with -B option, falling back to standard base BTF (%s)", 1135 sysfs_vmlinux); 1136 base_btf = get_vmlinux_btf_from_sysfs(); 1137 } 1138 1139 btf = btf__load_from_kernel_by_id_split(btf_id, base_btf); 1140 if (!btf) { 1141 err = -errno; 1142 p_err("get btf by id (%u): %s", btf_id, strerror(errno)); 1143 goto done; 1144 } 1145 } 1146 1147 /* Invalid root IDs causes half emitted boilerplate and then unclean 1148 * exit. It's an ugly user experience, so handle common error here. 1149 */ 1150 for (i = 0; i < root_type_cnt; i++) { 1151 if (root_type_ids[i] >= btf__type_cnt(btf)) { 1152 err = -EINVAL; 1153 p_err("invalid root ID: %u", root_type_ids[i]); 1154 goto done; 1155 } 1156 } 1157 1158 if (dump_c) { 1159 if (json_output) { 1160 p_err("JSON output for C-syntax dump is not supported"); 1161 err = -ENOTSUP; 1162 goto done; 1163 } 1164 err = dump_btf_c(btf, root_type_ids, root_type_cnt, sort_dump_c); 1165 } else { 1166 err = dump_btf_raw(btf, root_type_ids, root_type_cnt); 1167 } 1168 1169 done: 1170 close(fd); 1171 btf__free(btf); 1172 btf__free(base); 1173 return err; 1174 } 1175 1176 static int btf_parse_fd(int *argc, char ***argv) 1177 { 1178 unsigned int id; 1179 char *endptr; 1180 int fd; 1181 1182 if (!is_prefix(*argv[0], "id")) { 1183 p_err("expected 'id', got: '%s'?", **argv); 1184 return -1; 1185 } 1186 NEXT_ARGP(); 1187 1188 id = strtoul(**argv, &endptr, 0); 1189 if (*endptr) { 1190 p_err("can't parse %s as ID", **argv); 1191 return -1; 1192 } 1193 NEXT_ARGP(); 1194 1195 fd = bpf_btf_get_fd_by_id(id); 1196 if (fd < 0) 1197 p_err("can't get BTF object by id (%u): %s", 1198 id, strerror(errno)); 1199 1200 return fd; 1201 } 1202 1203 static int 1204 build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type, 1205 void *info, __u32 *len) 1206 { 1207 static const char * const names[] = { 1208 [BPF_OBJ_UNKNOWN] = "unknown", 1209 [BPF_OBJ_PROG] = "prog", 1210 [BPF_OBJ_MAP] = "map", 1211 }; 1212 LIBBPF_OPTS(bpf_get_fd_by_id_opts, opts_ro); 1213 __u32 btf_id, id = 0; 1214 int err; 1215 int fd; 1216 1217 opts_ro.open_flags = BPF_F_RDONLY; 1218 1219 while (true) { 1220 switch (type) { 1221 case BPF_OBJ_PROG: 1222 err = bpf_prog_get_next_id(id, &id); 1223 break; 1224 case BPF_OBJ_MAP: 1225 err = bpf_map_get_next_id(id, &id); 1226 break; 1227 default: 1228 err = -1; 1229 p_err("unexpected object type: %u", type); 1230 goto err_free; 1231 } 1232 if (err) { 1233 if (errno == ENOENT) { 1234 err = 0; 1235 break; 1236 } 1237 p_err("can't get next %s: %s%s", names[type], 1238 strerror(errno), 1239 errno == EINVAL ? " -- kernel too old?" : ""); 1240 goto err_free; 1241 } 1242 1243 switch (type) { 1244 case BPF_OBJ_PROG: 1245 fd = bpf_prog_get_fd_by_id(id); 1246 break; 1247 case BPF_OBJ_MAP: 1248 fd = bpf_map_get_fd_by_id_opts(id, &opts_ro); 1249 break; 1250 default: 1251 err = -1; 1252 p_err("unexpected object type: %u", type); 1253 goto err_free; 1254 } 1255 if (fd < 0) { 1256 if (errno == ENOENT) 1257 continue; 1258 p_err("can't get %s by id (%u): %s", names[type], id, 1259 strerror(errno)); 1260 err = -1; 1261 goto err_free; 1262 } 1263 1264 memset(info, 0, *len); 1265 if (type == BPF_OBJ_PROG) 1266 err = bpf_prog_get_info_by_fd(fd, info, len); 1267 else 1268 err = bpf_map_get_info_by_fd(fd, info, len); 1269 close(fd); 1270 if (err) { 1271 p_err("can't get %s info: %s", names[type], 1272 strerror(errno)); 1273 goto err_free; 1274 } 1275 1276 switch (type) { 1277 case BPF_OBJ_PROG: 1278 btf_id = ((struct bpf_prog_info *)info)->btf_id; 1279 break; 1280 case BPF_OBJ_MAP: 1281 btf_id = ((struct bpf_map_info *)info)->btf_id; 1282 break; 1283 default: 1284 err = -1; 1285 p_err("unexpected object type: %u", type); 1286 goto err_free; 1287 } 1288 if (!btf_id) 1289 continue; 1290 1291 err = hashmap__append(tab, btf_id, id); 1292 if (err) { 1293 p_err("failed to append entry to hashmap for BTF ID %u, object ID %u: %s", 1294 btf_id, id, strerror(-err)); 1295 goto err_free; 1296 } 1297 } 1298 1299 return 0; 1300 1301 err_free: 1302 hashmap__free(tab); 1303 return err; 1304 } 1305 1306 static int 1307 build_btf_tables(struct hashmap *btf_prog_table, 1308 struct hashmap *btf_map_table) 1309 { 1310 struct bpf_prog_info prog_info; 1311 __u32 prog_len = sizeof(prog_info); 1312 struct bpf_map_info map_info; 1313 __u32 map_len = sizeof(map_info); 1314 int err = 0; 1315 1316 err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info, 1317 &prog_len); 1318 if (err) 1319 return err; 1320 1321 err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info, 1322 &map_len); 1323 if (err) { 1324 hashmap__free(btf_prog_table); 1325 return err; 1326 } 1327 1328 return 0; 1329 } 1330 1331 static void 1332 show_btf_plain(struct bpf_btf_info *info, int fd, 1333 struct hashmap *btf_prog_table, 1334 struct hashmap *btf_map_table) 1335 { 1336 struct hashmap_entry *entry; 1337 const char *name = u64_to_ptr(info->name); 1338 int n; 1339 1340 printf("%u: ", info->id); 1341 if (info->kernel_btf) 1342 printf("name [%s] ", name); 1343 else if (name && name[0]) 1344 printf("name %s ", name); 1345 else 1346 printf("name <anon> "); 1347 printf("size %uB", info->btf_size); 1348 1349 n = 0; 1350 hashmap__for_each_key_entry(btf_prog_table, entry, info->id) { 1351 printf("%s%lu", n++ == 0 ? " prog_ids " : ",", (unsigned long)entry->value); 1352 } 1353 1354 n = 0; 1355 hashmap__for_each_key_entry(btf_map_table, entry, info->id) { 1356 printf("%s%lu", n++ == 0 ? " map_ids " : ",", (unsigned long)entry->value); 1357 } 1358 1359 emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); 1360 1361 printf("\n"); 1362 } 1363 1364 static void 1365 show_btf_json(struct bpf_btf_info *info, int fd, 1366 struct hashmap *btf_prog_table, 1367 struct hashmap *btf_map_table) 1368 { 1369 struct hashmap_entry *entry; 1370 const char *name = u64_to_ptr(info->name); 1371 1372 jsonw_start_object(json_wtr); /* btf object */ 1373 jsonw_uint_field(json_wtr, "id", info->id); 1374 jsonw_uint_field(json_wtr, "size", info->btf_size); 1375 1376 jsonw_name(json_wtr, "prog_ids"); 1377 jsonw_start_array(json_wtr); /* prog_ids */ 1378 hashmap__for_each_key_entry(btf_prog_table, entry, info->id) { 1379 jsonw_uint(json_wtr, entry->value); 1380 } 1381 jsonw_end_array(json_wtr); /* prog_ids */ 1382 1383 jsonw_name(json_wtr, "map_ids"); 1384 jsonw_start_array(json_wtr); /* map_ids */ 1385 hashmap__for_each_key_entry(btf_map_table, entry, info->id) { 1386 jsonw_uint(json_wtr, entry->value); 1387 } 1388 jsonw_end_array(json_wtr); /* map_ids */ 1389 1390 emit_obj_refs_json(refs_table, info->id, json_wtr); /* pids */ 1391 1392 jsonw_bool_field(json_wtr, "kernel", info->kernel_btf); 1393 1394 if (name && name[0]) 1395 jsonw_string_field(json_wtr, "name", name); 1396 1397 jsonw_end_object(json_wtr); /* btf object */ 1398 } 1399 1400 static int 1401 show_btf(int fd, struct hashmap *btf_prog_table, 1402 struct hashmap *btf_map_table) 1403 { 1404 struct bpf_btf_info info; 1405 __u32 len = sizeof(info); 1406 char name[64]; 1407 int err; 1408 1409 memset(&info, 0, sizeof(info)); 1410 err = bpf_btf_get_info_by_fd(fd, &info, &len); 1411 if (err) { 1412 p_err("can't get BTF object info: %s", strerror(errno)); 1413 return -1; 1414 } 1415 /* if kernel support emitting BTF object name, pass name pointer */ 1416 if (info.name_len) { 1417 memset(&info, 0, sizeof(info)); 1418 info.name_len = sizeof(name); 1419 info.name = ptr_to_u64(name); 1420 len = sizeof(info); 1421 1422 err = bpf_btf_get_info_by_fd(fd, &info, &len); 1423 if (err) { 1424 p_err("can't get BTF object info: %s", strerror(errno)); 1425 return -1; 1426 } 1427 } 1428 1429 if (json_output) 1430 show_btf_json(&info, fd, btf_prog_table, btf_map_table); 1431 else 1432 show_btf_plain(&info, fd, btf_prog_table, btf_map_table); 1433 1434 return 0; 1435 } 1436 1437 static int do_show(int argc, char **argv) 1438 { 1439 struct hashmap *btf_prog_table; 1440 struct hashmap *btf_map_table; 1441 int err, fd = -1; 1442 __u32 id = 0; 1443 1444 if (argc == 2) { 1445 fd = btf_parse_fd(&argc, &argv); 1446 if (fd < 0) 1447 return -1; 1448 } 1449 1450 if (argc) { 1451 if (fd >= 0) 1452 close(fd); 1453 return BAD_ARG(); 1454 } 1455 1456 btf_prog_table = hashmap__new(hash_fn_for_key_as_id, 1457 equal_fn_for_key_as_id, NULL); 1458 btf_map_table = hashmap__new(hash_fn_for_key_as_id, 1459 equal_fn_for_key_as_id, NULL); 1460 if (IS_ERR(btf_prog_table) || IS_ERR(btf_map_table)) { 1461 hashmap__free(btf_prog_table); 1462 hashmap__free(btf_map_table); 1463 if (fd >= 0) 1464 close(fd); 1465 p_err("failed to create hashmap for object references"); 1466 return -1; 1467 } 1468 err = build_btf_tables(btf_prog_table, btf_map_table); 1469 if (err) { 1470 if (fd >= 0) 1471 close(fd); 1472 return err; 1473 } 1474 build_obj_refs_table(&refs_table, BPF_OBJ_BTF); 1475 1476 if (fd >= 0) { 1477 err = show_btf(fd, btf_prog_table, btf_map_table); 1478 close(fd); 1479 goto exit_free; 1480 } 1481 1482 if (json_output) 1483 jsonw_start_array(json_wtr); /* root array */ 1484 1485 while (true) { 1486 err = bpf_btf_get_next_id(id, &id); 1487 if (err) { 1488 if (errno == ENOENT) { 1489 err = 0; 1490 break; 1491 } 1492 p_err("can't get next BTF object: %s%s", 1493 strerror(errno), 1494 errno == EINVAL ? " -- kernel too old?" : ""); 1495 err = -1; 1496 break; 1497 } 1498 1499 fd = bpf_btf_get_fd_by_id(id); 1500 if (fd < 0) { 1501 if (errno == ENOENT) 1502 continue; 1503 p_err("can't get BTF object by id (%u): %s", 1504 id, strerror(errno)); 1505 err = -1; 1506 break; 1507 } 1508 1509 err = show_btf(fd, btf_prog_table, btf_map_table); 1510 close(fd); 1511 if (err) 1512 break; 1513 } 1514 1515 if (json_output) 1516 jsonw_end_array(json_wtr); /* root array */ 1517 1518 exit_free: 1519 hashmap__free(btf_prog_table); 1520 hashmap__free(btf_map_table); 1521 delete_obj_refs_table(refs_table); 1522 1523 return err; 1524 } 1525 1526 static int do_help(int argc, char **argv) 1527 { 1528 if (json_output) { 1529 jsonw_null(json_wtr); 1530 return 0; 1531 } 1532 1533 fprintf(stderr, 1534 "Usage: %1$s %2$s { show | list } [id BTF_ID]\n" 1535 " %1$s %2$s dump BTF_SRC [format FORMAT] [root_id ROOT_ID]\n" 1536 " %1$s %2$s help\n" 1537 "\n" 1538 " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] |\n" 1539 " file FILE [file FILE]... }\n" 1540 " FORMAT := { raw | c [unsorted] }\n" 1541 " " HELP_SPEC_MAP "\n" 1542 " " HELP_SPEC_PROGRAM "\n" 1543 " " HELP_SPEC_OPTIONS " |\n" 1544 " {-B|--base-btf} }\n" 1545 "", 1546 bin_name, "btf"); 1547 1548 return 0; 1549 } 1550 1551 static const struct cmd cmds[] = { 1552 { "show", do_show }, 1553 { "list", do_show }, 1554 { "help", do_help }, 1555 { "dump", do_dump }, 1556 { 0 } 1557 }; 1558 1559 int do_btf(int argc, char **argv) 1560 { 1561 return cmd_select(cmds, argc, argv, do_help); 1562 } 1563