btf.c 33 KB

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