btf_helpers.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2020 Facebook */
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <bpf/btf.h>
  6. #include <bpf/libbpf.h>
  7. #include "test_progs.h"
  8. static const char * const btf_kind_str_mapping[] = {
  9. [BTF_KIND_UNKN] = "UNKNOWN",
  10. [BTF_KIND_INT] = "INT",
  11. [BTF_KIND_PTR] = "PTR",
  12. [BTF_KIND_ARRAY] = "ARRAY",
  13. [BTF_KIND_STRUCT] = "STRUCT",
  14. [BTF_KIND_UNION] = "UNION",
  15. [BTF_KIND_ENUM] = "ENUM",
  16. [BTF_KIND_FWD] = "FWD",
  17. [BTF_KIND_TYPEDEF] = "TYPEDEF",
  18. [BTF_KIND_VOLATILE] = "VOLATILE",
  19. [BTF_KIND_CONST] = "CONST",
  20. [BTF_KIND_RESTRICT] = "RESTRICT",
  21. [BTF_KIND_FUNC] = "FUNC",
  22. [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
  23. [BTF_KIND_VAR] = "VAR",
  24. [BTF_KIND_DATASEC] = "DATASEC",
  25. [BTF_KIND_FLOAT] = "FLOAT",
  26. [BTF_KIND_DECL_TAG] = "DECL_TAG",
  27. [BTF_KIND_TYPE_TAG] = "TYPE_TAG",
  28. [BTF_KIND_ENUM64] = "ENUM64",
  29. };
  30. static const char *btf_kind_str(__u16 kind)
  31. {
  32. if (kind > BTF_KIND_ENUM64)
  33. return "UNKNOWN";
  34. return btf_kind_str_mapping[kind];
  35. }
  36. static const char *btf_int_enc_str(__u8 encoding)
  37. {
  38. switch (encoding) {
  39. case 0:
  40. return "(none)";
  41. case BTF_INT_SIGNED:
  42. return "SIGNED";
  43. case BTF_INT_CHAR:
  44. return "CHAR";
  45. case BTF_INT_BOOL:
  46. return "BOOL";
  47. default:
  48. return "UNKN";
  49. }
  50. }
  51. static const char *btf_var_linkage_str(__u32 linkage)
  52. {
  53. switch (linkage) {
  54. case BTF_VAR_STATIC:
  55. return "static";
  56. case BTF_VAR_GLOBAL_ALLOCATED:
  57. return "global-alloc";
  58. default:
  59. return "(unknown)";
  60. }
  61. }
  62. static const char *btf_func_linkage_str(const struct btf_type *t)
  63. {
  64. switch (btf_vlen(t)) {
  65. case BTF_FUNC_STATIC:
  66. return "static";
  67. case BTF_FUNC_GLOBAL:
  68. return "global";
  69. case BTF_FUNC_EXTERN:
  70. return "extern";
  71. default:
  72. return "(unknown)";
  73. }
  74. }
  75. static const char *btf_str(const struct btf *btf, __u32 off)
  76. {
  77. if (!off)
  78. return "(anon)";
  79. return btf__str_by_offset(btf, off) ?: "(invalid)";
  80. }
  81. int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
  82. {
  83. const struct btf_type *t;
  84. int kind, i;
  85. __u32 vlen;
  86. t = btf__type_by_id(btf, id);
  87. if (!t)
  88. return -EINVAL;
  89. vlen = btf_vlen(t);
  90. kind = btf_kind(t);
  91. fprintf(out, "[%u] %s '%s'", id, btf_kind_str(kind), btf_str(btf, t->name_off));
  92. switch (kind) {
  93. case BTF_KIND_INT:
  94. fprintf(out, " size=%u bits_offset=%u nr_bits=%u encoding=%s",
  95. t->size, btf_int_offset(t), btf_int_bits(t),
  96. btf_int_enc_str(btf_int_encoding(t)));
  97. break;
  98. case BTF_KIND_PTR:
  99. case BTF_KIND_CONST:
  100. case BTF_KIND_VOLATILE:
  101. case BTF_KIND_RESTRICT:
  102. case BTF_KIND_TYPEDEF:
  103. case BTF_KIND_TYPE_TAG:
  104. fprintf(out, " type_id=%u", t->type);
  105. break;
  106. case BTF_KIND_ARRAY: {
  107. const struct btf_array *arr = btf_array(t);
  108. fprintf(out, " type_id=%u index_type_id=%u nr_elems=%u",
  109. arr->type, arr->index_type, arr->nelems);
  110. break;
  111. }
  112. case BTF_KIND_STRUCT:
  113. case BTF_KIND_UNION: {
  114. const struct btf_member *m = btf_members(t);
  115. fprintf(out, " size=%u vlen=%u", t->size, vlen);
  116. for (i = 0; i < vlen; i++, m++) {
  117. __u32 bit_off, bit_sz;
  118. bit_off = btf_member_bit_offset(t, i);
  119. bit_sz = btf_member_bitfield_size(t, i);
  120. fprintf(out, "\n\t'%s' type_id=%u bits_offset=%u",
  121. btf_str(btf, m->name_off), m->type, bit_off);
  122. if (bit_sz)
  123. fprintf(out, " bitfield_size=%u", bit_sz);
  124. }
  125. break;
  126. }
  127. case BTF_KIND_ENUM: {
  128. const struct btf_enum *v = btf_enum(t);
  129. const char *fmt_str;
  130. fmt_str = btf_kflag(t) ? "\n\t'%s' val=%d" : "\n\t'%s' val=%u";
  131. fprintf(out, " encoding=%s size=%u vlen=%u",
  132. btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);
  133. for (i = 0; i < vlen; i++, v++) {
  134. fprintf(out, fmt_str,
  135. btf_str(btf, v->name_off), v->val);
  136. }
  137. break;
  138. }
  139. case BTF_KIND_ENUM64: {
  140. const struct btf_enum64 *v = btf_enum64(t);
  141. const char *fmt_str;
  142. fmt_str = btf_kflag(t) ? "\n\t'%s' val=%lld" : "\n\t'%s' val=%llu";
  143. fprintf(out, " encoding=%s size=%u vlen=%u",
  144. btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);
  145. for (i = 0; i < vlen; i++, v++) {
  146. fprintf(out, fmt_str,
  147. btf_str(btf, v->name_off),
  148. ((__u64)v->val_hi32 << 32) | v->val_lo32);
  149. }
  150. break;
  151. }
  152. case BTF_KIND_FWD:
  153. fprintf(out, " fwd_kind=%s", btf_kflag(t) ? "union" : "struct");
  154. break;
  155. case BTF_KIND_FUNC:
  156. fprintf(out, " type_id=%u linkage=%s", t->type, btf_func_linkage_str(t));
  157. break;
  158. case BTF_KIND_FUNC_PROTO: {
  159. const struct btf_param *p = btf_params(t);
  160. fprintf(out, " ret_type_id=%u vlen=%u", t->type, vlen);
  161. for (i = 0; i < vlen; i++, p++) {
  162. fprintf(out, "\n\t'%s' type_id=%u",
  163. btf_str(btf, p->name_off), p->type);
  164. }
  165. break;
  166. }
  167. case BTF_KIND_VAR:
  168. fprintf(out, " type_id=%u, linkage=%s",
  169. t->type, btf_var_linkage_str(btf_var(t)->linkage));
  170. break;
  171. case BTF_KIND_DATASEC: {
  172. const struct btf_var_secinfo *v = btf_var_secinfos(t);
  173. fprintf(out, " size=%u vlen=%u", t->size, vlen);
  174. for (i = 0; i < vlen; i++, v++) {
  175. fprintf(out, "\n\ttype_id=%u offset=%u size=%u",
  176. v->type, v->offset, v->size);
  177. }
  178. break;
  179. }
  180. case BTF_KIND_FLOAT:
  181. fprintf(out, " size=%u", t->size);
  182. break;
  183. case BTF_KIND_DECL_TAG:
  184. fprintf(out, " type_id=%u component_idx=%d",
  185. t->type, btf_decl_tag(t)->component_idx);
  186. break;
  187. default:
  188. break;
  189. }
  190. return 0;
  191. }
  192. /* Print raw BTF type dump into a local buffer and return string pointer back.
  193. * Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls
  194. */
  195. const char *btf_type_raw_dump(const struct btf *btf, int type_id)
  196. {
  197. static char buf[16 * 1024];
  198. FILE *buf_file;
  199. buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
  200. if (!buf_file) {
  201. fprintf(stderr, "Failed to open memstream: %d\n", errno);
  202. return NULL;
  203. }
  204. fprintf_btf_type_raw(buf_file, btf, type_id);
  205. fflush(buf_file);
  206. fclose(buf_file);
  207. return buf;
  208. }
  209. int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[])
  210. {
  211. int i;
  212. bool ok = true;
  213. ASSERT_EQ(btf__type_cnt(btf) - 1, nr_types, "btf_nr_types");
  214. for (i = 1; i <= nr_types; i++) {
  215. if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump"))
  216. ok = false;
  217. }
  218. return ok;
  219. }
  220. static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
  221. {
  222. vfprintf(ctx, fmt, args);
  223. }
  224. /* Print BTF-to-C dump into a local buffer and return string pointer back.
  225. * Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls
  226. */
  227. const char *btf_type_c_dump(const struct btf *btf)
  228. {
  229. static char buf[16 * 1024];
  230. FILE *buf_file;
  231. struct btf_dump *d = NULL;
  232. int err, i;
  233. buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
  234. if (!buf_file) {
  235. fprintf(stderr, "Failed to open memstream: %d\n", errno);
  236. return NULL;
  237. }
  238. d = btf_dump__new(btf, btf_dump_printf, buf_file, NULL);
  239. if (libbpf_get_error(d)) {
  240. fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));
  241. goto err_out;
  242. }
  243. for (i = 1; i < btf__type_cnt(btf); i++) {
  244. err = btf_dump__dump_type(d, i);
  245. if (err) {
  246. fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
  247. goto err_out;
  248. }
  249. }
  250. btf_dump__free(d);
  251. fflush(buf_file);
  252. fclose(buf_file);
  253. return buf;
  254. err_out:
  255. btf_dump__free(d);
  256. fclose(buf_file);
  257. return NULL;
  258. }