btf_dumper.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (c) 2018 Facebook */
  3. #include <ctype.h>
  4. #include <stdio.h> /* for (FILE *) used by json_writer */
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <asm/byteorder.h>
  8. #include <linux/bitops.h>
  9. #include <linux/btf.h>
  10. #include <linux/err.h>
  11. #include <bpf/btf.h>
  12. #include <bpf/bpf.h>
  13. #include "json_writer.h"
  14. #include "main.h"
  15. #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
  16. #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
  17. #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
  18. #define BITS_ROUNDUP_BYTES(bits) \
  19. (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
  20. static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
  21. __u8 bit_offset, const void *data);
  22. static int btf_dump_func(const struct btf *btf, char *func_sig,
  23. const struct btf_type *func_proto,
  24. const struct btf_type *func, int pos, int size);
  25. static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,
  26. const struct btf_type *func_proto,
  27. __u32 prog_id)
  28. {
  29. const struct btf_type *func_type;
  30. int prog_fd = -1, func_sig_len;
  31. struct bpf_prog_info info = {};
  32. __u32 info_len = sizeof(info);
  33. const char *prog_name = NULL;
  34. struct btf *prog_btf = NULL;
  35. struct bpf_func_info finfo;
  36. __u32 finfo_rec_size;
  37. char prog_str[1024];
  38. int err;
  39. /* Get the ptr's func_proto */
  40. func_sig_len = btf_dump_func(d->btf, prog_str, func_proto, NULL, 0,
  41. sizeof(prog_str));
  42. if (func_sig_len == -1)
  43. return -1;
  44. if (!prog_id)
  45. goto print;
  46. /* Get the bpf_prog's name. Obtain from func_info. */
  47. prog_fd = bpf_prog_get_fd_by_id(prog_id);
  48. if (prog_fd < 0)
  49. goto print;
  50. err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
  51. if (err)
  52. goto print;
  53. if (!info.btf_id || !info.nr_func_info)
  54. goto print;
  55. finfo_rec_size = info.func_info_rec_size;
  56. memset(&info, 0, sizeof(info));
  57. info.nr_func_info = 1;
  58. info.func_info_rec_size = finfo_rec_size;
  59. info.func_info = ptr_to_u64(&finfo);
  60. err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
  61. if (err)
  62. goto print;
  63. prog_btf = btf__load_from_kernel_by_id(info.btf_id);
  64. if (libbpf_get_error(prog_btf))
  65. goto print;
  66. func_type = btf__type_by_id(prog_btf, finfo.type_id);
  67. if (!func_type || !btf_is_func(func_type))
  68. goto print;
  69. prog_name = btf__name_by_offset(prog_btf, func_type->name_off);
  70. print:
  71. if (!prog_id)
  72. snprintf(&prog_str[func_sig_len],
  73. sizeof(prog_str) - func_sig_len, " 0");
  74. else if (prog_name)
  75. snprintf(&prog_str[func_sig_len],
  76. sizeof(prog_str) - func_sig_len,
  77. " %s/prog_id:%u", prog_name, prog_id);
  78. else
  79. snprintf(&prog_str[func_sig_len],
  80. sizeof(prog_str) - func_sig_len,
  81. " <unknown_prog_name>/prog_id:%u", prog_id);
  82. prog_str[sizeof(prog_str) - 1] = '\0';
  83. jsonw_string(d->jw, prog_str);
  84. btf__free(prog_btf);
  85. if (prog_fd >= 0)
  86. close(prog_fd);
  87. return 0;
  88. }
  89. static void btf_dumper_ptr(const struct btf_dumper *d,
  90. const struct btf_type *t,
  91. const void *data)
  92. {
  93. unsigned long value = *(unsigned long *)data;
  94. const struct btf_type *ptr_type;
  95. __s32 ptr_type_id;
  96. if (!d->prog_id_as_func_ptr || value > UINT32_MAX)
  97. goto print_ptr_value;
  98. ptr_type_id = btf__resolve_type(d->btf, t->type);
  99. if (ptr_type_id < 0)
  100. goto print_ptr_value;
  101. ptr_type = btf__type_by_id(d->btf, ptr_type_id);
  102. if (!ptr_type || !btf_is_func_proto(ptr_type))
  103. goto print_ptr_value;
  104. if (!dump_prog_id_as_func_ptr(d, ptr_type, value))
  105. return;
  106. print_ptr_value:
  107. if (d->is_plain_text)
  108. jsonw_printf(d->jw, "%p", (void *)value);
  109. else
  110. jsonw_printf(d->jw, "%lu", value);
  111. }
  112. static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
  113. __u8 bit_offset, const void *data)
  114. {
  115. int actual_type_id;
  116. actual_type_id = btf__resolve_type(d->btf, type_id);
  117. if (actual_type_id < 0)
  118. return actual_type_id;
  119. return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
  120. }
  121. static int btf_dumper_enum(const struct btf_dumper *d,
  122. const struct btf_type *t,
  123. const void *data)
  124. {
  125. const struct btf_enum *enums = btf_enum(t);
  126. __s64 value;
  127. __u16 i;
  128. switch (t->size) {
  129. case 8:
  130. value = *(__s64 *)data;
  131. break;
  132. case 4:
  133. value = *(__s32 *)data;
  134. break;
  135. case 2:
  136. value = *(__s16 *)data;
  137. break;
  138. case 1:
  139. value = *(__s8 *)data;
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. for (i = 0; i < btf_vlen(t); i++) {
  145. if (value == enums[i].val) {
  146. jsonw_string(d->jw,
  147. btf__name_by_offset(d->btf,
  148. enums[i].name_off));
  149. return 0;
  150. }
  151. }
  152. jsonw_int(d->jw, value);
  153. return 0;
  154. }
  155. static int btf_dumper_enum64(const struct btf_dumper *d,
  156. const struct btf_type *t,
  157. const void *data)
  158. {
  159. const struct btf_enum64 *enums = btf_enum64(t);
  160. __u32 val_lo32, val_hi32;
  161. __u64 value;
  162. __u16 i;
  163. value = *(__u64 *)data;
  164. val_lo32 = (__u32)value;
  165. val_hi32 = value >> 32;
  166. for (i = 0; i < btf_vlen(t); i++) {
  167. if (val_lo32 == enums[i].val_lo32 && val_hi32 == enums[i].val_hi32) {
  168. jsonw_string(d->jw,
  169. btf__name_by_offset(d->btf,
  170. enums[i].name_off));
  171. return 0;
  172. }
  173. }
  174. jsonw_int(d->jw, value);
  175. return 0;
  176. }
  177. static bool is_str_array(const struct btf *btf, const struct btf_array *arr,
  178. const char *s)
  179. {
  180. const struct btf_type *elem_type;
  181. const char *end_s;
  182. if (!arr->nelems)
  183. return false;
  184. elem_type = btf__type_by_id(btf, arr->type);
  185. /* Not skipping typedef. typedef to char does not count as
  186. * a string now.
  187. */
  188. while (elem_type && btf_is_mod(elem_type))
  189. elem_type = btf__type_by_id(btf, elem_type->type);
  190. if (!elem_type || !btf_is_int(elem_type) || elem_type->size != 1)
  191. return false;
  192. if (btf_int_encoding(elem_type) != BTF_INT_CHAR &&
  193. strcmp("char", btf__name_by_offset(btf, elem_type->name_off)))
  194. return false;
  195. end_s = s + arr->nelems;
  196. while (s < end_s) {
  197. if (!*s)
  198. return true;
  199. if (*s <= 0x1f || *s >= 0x7f)
  200. return false;
  201. s++;
  202. }
  203. /* '\0' is not found */
  204. return false;
  205. }
  206. static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
  207. const void *data)
  208. {
  209. const struct btf_type *t = btf__type_by_id(d->btf, type_id);
  210. struct btf_array *arr = (struct btf_array *)(t + 1);
  211. long long elem_size;
  212. int ret = 0;
  213. __u32 i;
  214. if (is_str_array(d->btf, arr, data)) {
  215. jsonw_string(d->jw, data);
  216. return 0;
  217. }
  218. elem_size = btf__resolve_size(d->btf, arr->type);
  219. if (elem_size < 0)
  220. return elem_size;
  221. jsonw_start_array(d->jw);
  222. for (i = 0; i < arr->nelems; i++) {
  223. ret = btf_dumper_do_type(d, arr->type, 0,
  224. data + i * elem_size);
  225. if (ret)
  226. break;
  227. }
  228. jsonw_end_array(d->jw);
  229. return ret;
  230. }
  231. static void btf_int128_print(json_writer_t *jw, const void *data,
  232. bool is_plain_text)
  233. {
  234. /* data points to a __int128 number.
  235. * Suppose
  236. * int128_num = *(__int128 *)data;
  237. * The below formulas shows what upper_num and lower_num represents:
  238. * upper_num = int128_num >> 64;
  239. * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
  240. */
  241. __u64 upper_num, lower_num;
  242. #ifdef __BIG_ENDIAN_BITFIELD
  243. upper_num = *(__u64 *)data;
  244. lower_num = *(__u64 *)(data + 8);
  245. #else
  246. upper_num = *(__u64 *)(data + 8);
  247. lower_num = *(__u64 *)data;
  248. #endif
  249. if (is_plain_text) {
  250. if (upper_num == 0)
  251. jsonw_printf(jw, "0x%llx", lower_num);
  252. else
  253. jsonw_printf(jw, "0x%llx%016llx", upper_num, lower_num);
  254. } else {
  255. if (upper_num == 0)
  256. jsonw_printf(jw, "\"0x%llx\"", lower_num);
  257. else
  258. jsonw_printf(jw, "\"0x%llx%016llx\"", upper_num, lower_num);
  259. }
  260. }
  261. static void btf_int128_shift(__u64 *print_num, __u16 left_shift_bits,
  262. __u16 right_shift_bits)
  263. {
  264. __u64 upper_num, lower_num;
  265. #ifdef __BIG_ENDIAN_BITFIELD
  266. upper_num = print_num[0];
  267. lower_num = print_num[1];
  268. #else
  269. upper_num = print_num[1];
  270. lower_num = print_num[0];
  271. #endif
  272. /* shake out un-needed bits by shift/or operations */
  273. if (left_shift_bits >= 64) {
  274. upper_num = lower_num << (left_shift_bits - 64);
  275. lower_num = 0;
  276. } else {
  277. upper_num = (upper_num << left_shift_bits) |
  278. (lower_num >> (64 - left_shift_bits));
  279. lower_num = lower_num << left_shift_bits;
  280. }
  281. if (right_shift_bits >= 64) {
  282. lower_num = upper_num >> (right_shift_bits - 64);
  283. upper_num = 0;
  284. } else {
  285. lower_num = (lower_num >> right_shift_bits) |
  286. (upper_num << (64 - right_shift_bits));
  287. upper_num = upper_num >> right_shift_bits;
  288. }
  289. #ifdef __BIG_ENDIAN_BITFIELD
  290. print_num[0] = upper_num;
  291. print_num[1] = lower_num;
  292. #else
  293. print_num[0] = lower_num;
  294. print_num[1] = upper_num;
  295. #endif
  296. }
  297. static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
  298. const void *data, json_writer_t *jw,
  299. bool is_plain_text)
  300. {
  301. int left_shift_bits, right_shift_bits;
  302. __u64 print_num[2] = {};
  303. int bytes_to_copy;
  304. int bits_to_copy;
  305. bits_to_copy = bit_offset + nr_bits;
  306. bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
  307. memcpy(print_num, data, bytes_to_copy);
  308. #if defined(__BIG_ENDIAN_BITFIELD)
  309. left_shift_bits = bit_offset;
  310. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  311. left_shift_bits = 128 - bits_to_copy;
  312. #else
  313. #error neither big nor little endian
  314. #endif
  315. right_shift_bits = 128 - nr_bits;
  316. btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
  317. btf_int128_print(jw, print_num, is_plain_text);
  318. }
  319. static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
  320. const void *data, json_writer_t *jw,
  321. bool is_plain_text)
  322. {
  323. int nr_bits = BTF_INT_BITS(int_type);
  324. int total_bits_offset;
  325. /* bits_offset is at most 7.
  326. * BTF_INT_OFFSET() cannot exceed 128 bits.
  327. */
  328. total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type);
  329. data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
  330. bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
  331. btf_dumper_bitfield(nr_bits, bit_offset, data, jw,
  332. is_plain_text);
  333. }
  334. static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
  335. const void *data, json_writer_t *jw,
  336. bool is_plain_text)
  337. {
  338. __u32 *int_type;
  339. __u32 nr_bits;
  340. int_type = (__u32 *)(t + 1);
  341. nr_bits = BTF_INT_BITS(*int_type);
  342. /* if this is bit field */
  343. if (bit_offset || BTF_INT_OFFSET(*int_type) ||
  344. BITS_PER_BYTE_MASKED(nr_bits)) {
  345. btf_dumper_int_bits(*int_type, bit_offset, data, jw,
  346. is_plain_text);
  347. return 0;
  348. }
  349. if (nr_bits == 128) {
  350. btf_int128_print(jw, data, is_plain_text);
  351. return 0;
  352. }
  353. switch (BTF_INT_ENCODING(*int_type)) {
  354. case 0:
  355. if (BTF_INT_BITS(*int_type) == 64)
  356. jsonw_printf(jw, "%llu", *(__u64 *)data);
  357. else if (BTF_INT_BITS(*int_type) == 32)
  358. jsonw_printf(jw, "%u", *(__u32 *)data);
  359. else if (BTF_INT_BITS(*int_type) == 16)
  360. jsonw_printf(jw, "%hu", *(__u16 *)data);
  361. else if (BTF_INT_BITS(*int_type) == 8)
  362. jsonw_printf(jw, "%hhu", *(__u8 *)data);
  363. else
  364. btf_dumper_int_bits(*int_type, bit_offset, data, jw,
  365. is_plain_text);
  366. break;
  367. case BTF_INT_SIGNED:
  368. if (BTF_INT_BITS(*int_type) == 64)
  369. jsonw_printf(jw, "%lld", *(long long *)data);
  370. else if (BTF_INT_BITS(*int_type) == 32)
  371. jsonw_printf(jw, "%d", *(int *)data);
  372. else if (BTF_INT_BITS(*int_type) == 16)
  373. jsonw_printf(jw, "%hd", *(short *)data);
  374. else if (BTF_INT_BITS(*int_type) == 8)
  375. jsonw_printf(jw, "%hhd", *(char *)data);
  376. else
  377. btf_dumper_int_bits(*int_type, bit_offset, data, jw,
  378. is_plain_text);
  379. break;
  380. case BTF_INT_CHAR:
  381. if (isprint(*(char *)data))
  382. jsonw_printf(jw, "\"%c\"", *(char *)data);
  383. else
  384. if (is_plain_text)
  385. jsonw_printf(jw, "0x%hhx", *(char *)data);
  386. else
  387. jsonw_printf(jw, "\"\\u00%02hhx\"",
  388. *(char *)data);
  389. break;
  390. case BTF_INT_BOOL:
  391. jsonw_bool(jw, *(bool *)data);
  392. break;
  393. default:
  394. /* shouldn't happen */
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
  400. const void *data)
  401. {
  402. const struct btf_type *t;
  403. struct btf_member *m;
  404. const void *data_off;
  405. int kind_flag;
  406. int ret = 0;
  407. int i, vlen;
  408. t = btf__type_by_id(d->btf, type_id);
  409. if (!t)
  410. return -EINVAL;
  411. kind_flag = BTF_INFO_KFLAG(t->info);
  412. vlen = BTF_INFO_VLEN(t->info);
  413. jsonw_start_object(d->jw);
  414. m = (struct btf_member *)(t + 1);
  415. for (i = 0; i < vlen; i++) {
  416. __u32 bit_offset = m[i].offset;
  417. __u32 bitfield_size = 0;
  418. if (kind_flag) {
  419. bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
  420. bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
  421. }
  422. jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
  423. data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
  424. if (bitfield_size) {
  425. btf_dumper_bitfield(bitfield_size,
  426. BITS_PER_BYTE_MASKED(bit_offset),
  427. data_off, d->jw, d->is_plain_text);
  428. } else {
  429. ret = btf_dumper_do_type(d, m[i].type,
  430. BITS_PER_BYTE_MASKED(bit_offset),
  431. data_off);
  432. if (ret)
  433. break;
  434. }
  435. }
  436. jsonw_end_object(d->jw);
  437. return ret;
  438. }
  439. static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
  440. __u8 bit_offset, const void *data)
  441. {
  442. const struct btf_type *t = btf__type_by_id(d->btf, type_id);
  443. int ret;
  444. jsonw_start_object(d->jw);
  445. jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
  446. ret = btf_dumper_do_type(d, t->type, bit_offset, data);
  447. jsonw_end_object(d->jw);
  448. return ret;
  449. }
  450. static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
  451. const void *data)
  452. {
  453. struct btf_var_secinfo *vsi;
  454. const struct btf_type *t;
  455. int ret = 0, i, vlen;
  456. t = btf__type_by_id(d->btf, type_id);
  457. if (!t)
  458. return -EINVAL;
  459. vlen = BTF_INFO_VLEN(t->info);
  460. vsi = (struct btf_var_secinfo *)(t + 1);
  461. jsonw_start_object(d->jw);
  462. jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
  463. jsonw_start_array(d->jw);
  464. for (i = 0; i < vlen; i++) {
  465. ret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);
  466. if (ret)
  467. break;
  468. }
  469. jsonw_end_array(d->jw);
  470. jsonw_end_object(d->jw);
  471. return ret;
  472. }
  473. static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
  474. __u8 bit_offset, const void *data)
  475. {
  476. const struct btf_type *t = btf__type_by_id(d->btf, type_id);
  477. switch (BTF_INFO_KIND(t->info)) {
  478. case BTF_KIND_INT:
  479. return btf_dumper_int(t, bit_offset, data, d->jw,
  480. d->is_plain_text);
  481. case BTF_KIND_STRUCT:
  482. case BTF_KIND_UNION:
  483. return btf_dumper_struct(d, type_id, data);
  484. case BTF_KIND_ARRAY:
  485. return btf_dumper_array(d, type_id, data);
  486. case BTF_KIND_ENUM:
  487. return btf_dumper_enum(d, t, data);
  488. case BTF_KIND_ENUM64:
  489. return btf_dumper_enum64(d, t, data);
  490. case BTF_KIND_PTR:
  491. btf_dumper_ptr(d, t, data);
  492. return 0;
  493. case BTF_KIND_UNKN:
  494. jsonw_printf(d->jw, "(unknown)");
  495. return 0;
  496. case BTF_KIND_FWD:
  497. /* map key or value can't be forward */
  498. jsonw_printf(d->jw, "(fwd-kind-invalid)");
  499. return -EINVAL;
  500. case BTF_KIND_TYPEDEF:
  501. case BTF_KIND_VOLATILE:
  502. case BTF_KIND_CONST:
  503. case BTF_KIND_RESTRICT:
  504. return btf_dumper_modifier(d, type_id, bit_offset, data);
  505. case BTF_KIND_VAR:
  506. return btf_dumper_var(d, type_id, bit_offset, data);
  507. case BTF_KIND_DATASEC:
  508. return btf_dumper_datasec(d, type_id, data);
  509. default:
  510. jsonw_printf(d->jw, "(unsupported-kind");
  511. return -EINVAL;
  512. }
  513. }
  514. int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
  515. const void *data)
  516. {
  517. return btf_dumper_do_type(d, type_id, 0, data);
  518. }
  519. #define BTF_PRINT_ARG(...) \
  520. do { \
  521. pos += snprintf(func_sig + pos, size - pos, \
  522. __VA_ARGS__); \
  523. if (pos >= size) \
  524. return -1; \
  525. } while (0)
  526. #define BTF_PRINT_TYPE(type) \
  527. do { \
  528. pos = __btf_dumper_type_only(btf, type, func_sig, \
  529. pos, size); \
  530. if (pos == -1) \
  531. return -1; \
  532. } while (0)
  533. static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
  534. char *func_sig, int pos, int size)
  535. {
  536. const struct btf_type *proto_type;
  537. const struct btf_array *array;
  538. const struct btf_var *var;
  539. const struct btf_type *t;
  540. if (!type_id) {
  541. BTF_PRINT_ARG("void ");
  542. return pos;
  543. }
  544. t = btf__type_by_id(btf, type_id);
  545. switch (BTF_INFO_KIND(t->info)) {
  546. case BTF_KIND_INT:
  547. case BTF_KIND_TYPEDEF:
  548. case BTF_KIND_FLOAT:
  549. BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
  550. break;
  551. case BTF_KIND_STRUCT:
  552. BTF_PRINT_ARG("struct %s ",
  553. btf__name_by_offset(btf, t->name_off));
  554. break;
  555. case BTF_KIND_UNION:
  556. BTF_PRINT_ARG("union %s ",
  557. btf__name_by_offset(btf, t->name_off));
  558. break;
  559. case BTF_KIND_ENUM:
  560. case BTF_KIND_ENUM64:
  561. BTF_PRINT_ARG("enum %s ",
  562. btf__name_by_offset(btf, t->name_off));
  563. break;
  564. case BTF_KIND_ARRAY:
  565. array = (struct btf_array *)(t + 1);
  566. BTF_PRINT_TYPE(array->type);
  567. BTF_PRINT_ARG("[%d]", array->nelems);
  568. break;
  569. case BTF_KIND_PTR:
  570. BTF_PRINT_TYPE(t->type);
  571. BTF_PRINT_ARG("* ");
  572. break;
  573. case BTF_KIND_FWD:
  574. BTF_PRINT_ARG("%s %s ",
  575. BTF_INFO_KFLAG(t->info) ? "union" : "struct",
  576. btf__name_by_offset(btf, t->name_off));
  577. break;
  578. case BTF_KIND_VOLATILE:
  579. BTF_PRINT_ARG("volatile ");
  580. BTF_PRINT_TYPE(t->type);
  581. break;
  582. case BTF_KIND_CONST:
  583. BTF_PRINT_ARG("const ");
  584. BTF_PRINT_TYPE(t->type);
  585. break;
  586. case BTF_KIND_RESTRICT:
  587. BTF_PRINT_ARG("restrict ");
  588. BTF_PRINT_TYPE(t->type);
  589. break;
  590. case BTF_KIND_FUNC_PROTO:
  591. pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
  592. if (pos == -1)
  593. return -1;
  594. break;
  595. case BTF_KIND_FUNC:
  596. proto_type = btf__type_by_id(btf, t->type);
  597. pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
  598. if (pos == -1)
  599. return -1;
  600. break;
  601. case BTF_KIND_VAR:
  602. var = (struct btf_var *)(t + 1);
  603. if (var->linkage == BTF_VAR_STATIC)
  604. BTF_PRINT_ARG("static ");
  605. BTF_PRINT_TYPE(t->type);
  606. BTF_PRINT_ARG(" %s",
  607. btf__name_by_offset(btf, t->name_off));
  608. break;
  609. case BTF_KIND_DATASEC:
  610. BTF_PRINT_ARG("section (\"%s\") ",
  611. btf__name_by_offset(btf, t->name_off));
  612. break;
  613. case BTF_KIND_UNKN:
  614. default:
  615. return -1;
  616. }
  617. return pos;
  618. }
  619. static int btf_dump_func(const struct btf *btf, char *func_sig,
  620. const struct btf_type *func_proto,
  621. const struct btf_type *func, int pos, int size)
  622. {
  623. int i, vlen;
  624. BTF_PRINT_TYPE(func_proto->type);
  625. if (func)
  626. BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
  627. else
  628. BTF_PRINT_ARG("(");
  629. vlen = BTF_INFO_VLEN(func_proto->info);
  630. for (i = 0; i < vlen; i++) {
  631. struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i];
  632. if (i)
  633. BTF_PRINT_ARG(", ");
  634. if (arg->type) {
  635. BTF_PRINT_TYPE(arg->type);
  636. if (arg->name_off)
  637. BTF_PRINT_ARG("%s",
  638. btf__name_by_offset(btf, arg->name_off));
  639. else if (pos && func_sig[pos - 1] == ' ')
  640. /* Remove unnecessary space for
  641. * FUNC_PROTO that does not have
  642. * arg->name_off
  643. */
  644. func_sig[--pos] = '\0';
  645. } else {
  646. BTF_PRINT_ARG("...");
  647. }
  648. }
  649. BTF_PRINT_ARG(")");
  650. return pos;
  651. }
  652. void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
  653. int size)
  654. {
  655. int err;
  656. func_sig[0] = '\0';
  657. if (!btf)
  658. return;
  659. err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
  660. if (err < 0)
  661. func_sig[0] = '\0';
  662. }
  663. static const char *ltrim(const char *s)
  664. {
  665. while (isspace(*s))
  666. s++;
  667. return s;
  668. }
  669. void btf_dump_linfo_plain(const struct btf *btf,
  670. const struct bpf_line_info *linfo,
  671. const char *prefix, bool linum)
  672. {
  673. const char *line = btf__name_by_offset(btf, linfo->line_off);
  674. if (!line)
  675. return;
  676. line = ltrim(line);
  677. if (!prefix)
  678. prefix = "";
  679. if (linum) {
  680. const char *file = btf__name_by_offset(btf, linfo->file_name_off);
  681. /* More forgiving on file because linum option is
  682. * expected to provide more info than the already
  683. * available src line.
  684. */
  685. if (!file)
  686. file = "";
  687. printf("%s%s [file:%s line_num:%u line_col:%u]\n",
  688. prefix, line, file,
  689. BPF_LINE_INFO_LINE_NUM(linfo->line_col),
  690. BPF_LINE_INFO_LINE_COL(linfo->line_col));
  691. } else {
  692. printf("%s%s\n", prefix, line);
  693. }
  694. }
  695. void btf_dump_linfo_json(const struct btf *btf,
  696. const struct bpf_line_info *linfo, bool linum)
  697. {
  698. const char *line = btf__name_by_offset(btf, linfo->line_off);
  699. if (line)
  700. jsonw_string_field(json_wtr, "src", ltrim(line));
  701. if (linum) {
  702. const char *file = btf__name_by_offset(btf, linfo->file_name_off);
  703. if (file)
  704. jsonw_string_field(json_wtr, "file", file);
  705. if (BPF_LINE_INFO_LINE_NUM(linfo->line_col))
  706. jsonw_int_field(json_wtr, "line_num",
  707. BPF_LINE_INFO_LINE_NUM(linfo->line_col));
  708. if (BPF_LINE_INFO_LINE_COL(linfo->line_col))
  709. jsonw_int_field(json_wtr, "line_col",
  710. BPF_LINE_INFO_LINE_COL(linfo->line_col));
  711. }
  712. }