1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
5 #include <stdio.h> /* for (FILE *) used by json_writer */
7 #include <asm/byteorder.h>
8 #include <linux/bitops.h>
10 #include <linux/err.h>
13 #include "json_writer.h"
16 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
17 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
18 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
19 #define BITS_ROUNDUP_BYTES(bits) \
20 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
22 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
23 __u8 bit_offset, const void *data);
25 static void btf_dumper_ptr(const void *data, json_writer_t *jw,
29 jsonw_printf(jw, "%p", data);
31 jsonw_printf(jw, "%lu", *(unsigned long *)data);
34 static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
35 __u8 bit_offset, const void *data)
39 actual_type_id = btf__resolve_type(d->btf, type_id);
40 if (actual_type_id < 0)
41 return actual_type_id;
43 return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
46 static void btf_dumper_enum(const void *data, json_writer_t *jw)
48 jsonw_printf(jw, "%d", *(int *)data);
51 static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
54 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
55 struct btf_array *arr = (struct btf_array *)(t + 1);
60 elem_size = btf__resolve_size(d->btf, arr->type);
64 jsonw_start_array(d->jw);
65 for (i = 0; i < arr->nelems; i++) {
66 ret = btf_dumper_do_type(d, arr->type, 0,
67 data + i * elem_size);
72 jsonw_end_array(d->jw);
76 static void btf_int128_print(json_writer_t *jw, const void *data,
79 /* data points to a __int128 number.
81 * int128_num = *(__int128 *)data;
82 * The below formulas shows what upper_num and lower_num represents:
83 * upper_num = int128_num >> 64;
84 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
86 __u64 upper_num, lower_num;
88 #ifdef __BIG_ENDIAN_BITFIELD
89 upper_num = *(__u64 *)data;
90 lower_num = *(__u64 *)(data + 8);
92 upper_num = *(__u64 *)(data + 8);
93 lower_num = *(__u64 *)data;
98 jsonw_printf(jw, "0x%llx", lower_num);
100 jsonw_printf(jw, "0x%llx%016llx", upper_num, lower_num);
103 jsonw_printf(jw, "\"0x%llx\"", lower_num);
105 jsonw_printf(jw, "\"0x%llx%016llx\"", upper_num, lower_num);
109 static void btf_int128_shift(__u64 *print_num, u16 left_shift_bits,
110 u16 right_shift_bits)
112 __u64 upper_num, lower_num;
114 #ifdef __BIG_ENDIAN_BITFIELD
115 upper_num = print_num[0];
116 lower_num = print_num[1];
118 upper_num = print_num[1];
119 lower_num = print_num[0];
122 /* shake out un-needed bits by shift/or operations */
123 if (left_shift_bits >= 64) {
124 upper_num = lower_num << (left_shift_bits - 64);
127 upper_num = (upper_num << left_shift_bits) |
128 (lower_num >> (64 - left_shift_bits));
129 lower_num = lower_num << left_shift_bits;
132 if (right_shift_bits >= 64) {
133 lower_num = upper_num >> (right_shift_bits - 64);
136 lower_num = (lower_num >> right_shift_bits) |
137 (upper_num << (64 - right_shift_bits));
138 upper_num = upper_num >> right_shift_bits;
141 #ifdef __BIG_ENDIAN_BITFIELD
142 print_num[0] = upper_num;
143 print_num[1] = lower_num;
145 print_num[0] = lower_num;
146 print_num[1] = upper_num;
150 static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
151 const void *data, json_writer_t *jw,
154 int left_shift_bits, right_shift_bits;
155 __u64 print_num[2] = {};
159 bits_to_copy = bit_offset + nr_bits;
160 bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
162 memcpy(print_num, data, bytes_to_copy);
163 #if defined(__BIG_ENDIAN_BITFIELD)
164 left_shift_bits = bit_offset;
165 #elif defined(__LITTLE_ENDIAN_BITFIELD)
166 left_shift_bits = 128 - bits_to_copy;
168 #error neither big nor little endian
170 right_shift_bits = 128 - nr_bits;
172 btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
173 btf_int128_print(jw, print_num, is_plain_text);
177 static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
178 const void *data, json_writer_t *jw,
181 int nr_bits = BTF_INT_BITS(int_type);
182 int total_bits_offset;
184 /* bits_offset is at most 7.
185 * BTF_INT_OFFSET() cannot exceed 128 bits.
187 total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type);
188 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
189 bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
190 btf_dumper_bitfield(nr_bits, bit_offset, data, jw,
194 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
195 const void *data, json_writer_t *jw,
201 int_type = (__u32 *)(t + 1);
202 nr_bits = BTF_INT_BITS(*int_type);
203 /* if this is bit field */
204 if (bit_offset || BTF_INT_OFFSET(*int_type) ||
205 BITS_PER_BYTE_MASKED(nr_bits)) {
206 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
211 if (nr_bits == 128) {
212 btf_int128_print(jw, data, is_plain_text);
216 switch (BTF_INT_ENCODING(*int_type)) {
218 if (BTF_INT_BITS(*int_type) == 64)
219 jsonw_printf(jw, "%llu", *(__u64 *)data);
220 else if (BTF_INT_BITS(*int_type) == 32)
221 jsonw_printf(jw, "%u", *(__u32 *)data);
222 else if (BTF_INT_BITS(*int_type) == 16)
223 jsonw_printf(jw, "%hu", *(__u16 *)data);
224 else if (BTF_INT_BITS(*int_type) == 8)
225 jsonw_printf(jw, "%hhu", *(__u8 *)data);
227 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
231 if (BTF_INT_BITS(*int_type) == 64)
232 jsonw_printf(jw, "%lld", *(long long *)data);
233 else if (BTF_INT_BITS(*int_type) == 32)
234 jsonw_printf(jw, "%d", *(int *)data);
235 else if (BTF_INT_BITS(*int_type) == 16)
236 jsonw_printf(jw, "%hd", *(short *)data);
237 else if (BTF_INT_BITS(*int_type) == 8)
238 jsonw_printf(jw, "%hhd", *(char *)data);
240 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
244 if (isprint(*(char *)data))
245 jsonw_printf(jw, "\"%c\"", *(char *)data);
248 jsonw_printf(jw, "0x%hhx", *(char *)data);
250 jsonw_printf(jw, "\"\\u00%02hhx\"",
254 jsonw_bool(jw, *(int *)data);
257 /* shouldn't happen */
264 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
267 const struct btf_type *t;
268 struct btf_member *m;
269 const void *data_off;
274 t = btf__type_by_id(d->btf, type_id);
278 kind_flag = BTF_INFO_KFLAG(t->info);
279 vlen = BTF_INFO_VLEN(t->info);
280 jsonw_start_object(d->jw);
281 m = (struct btf_member *)(t + 1);
283 for (i = 0; i < vlen; i++) {
284 __u32 bit_offset = m[i].offset;
285 __u32 bitfield_size = 0;
288 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
289 bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
292 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
293 data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
295 btf_dumper_bitfield(bitfield_size,
296 BITS_PER_BYTE_MASKED(bit_offset),
297 data_off, d->jw, d->is_plain_text);
299 ret = btf_dumper_do_type(d, m[i].type,
300 BITS_PER_BYTE_MASKED(bit_offset),
307 jsonw_end_object(d->jw);
312 static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
313 __u8 bit_offset, const void *data)
315 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
318 jsonw_start_object(d->jw);
319 jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
320 ret = btf_dumper_do_type(d, t->type, bit_offset, data);
321 jsonw_end_object(d->jw);
326 static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
329 struct btf_var_secinfo *vsi;
330 const struct btf_type *t;
331 int ret = 0, i, vlen;
333 t = btf__type_by_id(d->btf, type_id);
337 vlen = BTF_INFO_VLEN(t->info);
338 vsi = (struct btf_var_secinfo *)(t + 1);
340 jsonw_start_object(d->jw);
341 jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
342 jsonw_start_array(d->jw);
343 for (i = 0; i < vlen; i++) {
344 ret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);
348 jsonw_end_array(d->jw);
349 jsonw_end_object(d->jw);
354 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
355 __u8 bit_offset, const void *data)
357 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
359 switch (BTF_INFO_KIND(t->info)) {
361 return btf_dumper_int(t, bit_offset, data, d->jw,
363 case BTF_KIND_STRUCT:
365 return btf_dumper_struct(d, type_id, data);
367 return btf_dumper_array(d, type_id, data);
369 btf_dumper_enum(data, d->jw);
372 btf_dumper_ptr(data, d->jw, d->is_plain_text);
375 jsonw_printf(d->jw, "(unknown)");
378 /* map key or value can't be forward */
379 jsonw_printf(d->jw, "(fwd-kind-invalid)");
381 case BTF_KIND_TYPEDEF:
382 case BTF_KIND_VOLATILE:
384 case BTF_KIND_RESTRICT:
385 return btf_dumper_modifier(d, type_id, bit_offset, data);
387 return btf_dumper_var(d, type_id, bit_offset, data);
388 case BTF_KIND_DATASEC:
389 return btf_dumper_datasec(d, type_id, data);
391 jsonw_printf(d->jw, "(unsupported-kind");
396 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
399 return btf_dumper_do_type(d, type_id, 0, data);
402 #define BTF_PRINT_ARG(...) \
404 pos += snprintf(func_sig + pos, size - pos, \
409 #define BTF_PRINT_TYPE(type) \
411 pos = __btf_dumper_type_only(btf, type, func_sig, \
417 static int btf_dump_func(const struct btf *btf, char *func_sig,
418 const struct btf_type *func_proto,
419 const struct btf_type *func, int pos, int size);
421 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
422 char *func_sig, int pos, int size)
424 const struct btf_type *proto_type;
425 const struct btf_array *array;
426 const struct btf_var *var;
427 const struct btf_type *t;
430 BTF_PRINT_ARG("void ");
434 t = btf__type_by_id(btf, type_id);
436 switch (BTF_INFO_KIND(t->info)) {
438 case BTF_KIND_TYPEDEF:
439 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
441 case BTF_KIND_STRUCT:
442 BTF_PRINT_ARG("struct %s ",
443 btf__name_by_offset(btf, t->name_off));
446 BTF_PRINT_ARG("union %s ",
447 btf__name_by_offset(btf, t->name_off));
450 BTF_PRINT_ARG("enum %s ",
451 btf__name_by_offset(btf, t->name_off));
454 array = (struct btf_array *)(t + 1);
455 BTF_PRINT_TYPE(array->type);
456 BTF_PRINT_ARG("[%d]", array->nelems);
459 BTF_PRINT_TYPE(t->type);
463 BTF_PRINT_ARG("%s %s ",
464 BTF_INFO_KFLAG(t->info) ? "union" : "struct",
465 btf__name_by_offset(btf, t->name_off));
467 case BTF_KIND_VOLATILE:
468 BTF_PRINT_ARG("volatile ");
469 BTF_PRINT_TYPE(t->type);
472 BTF_PRINT_ARG("const ");
473 BTF_PRINT_TYPE(t->type);
475 case BTF_KIND_RESTRICT:
476 BTF_PRINT_ARG("restrict ");
477 BTF_PRINT_TYPE(t->type);
479 case BTF_KIND_FUNC_PROTO:
480 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
485 proto_type = btf__type_by_id(btf, t->type);
486 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
491 var = (struct btf_var *)(t + 1);
492 if (var->linkage == BTF_VAR_STATIC)
493 BTF_PRINT_ARG("static ");
494 BTF_PRINT_TYPE(t->type);
496 btf__name_by_offset(btf, t->name_off));
498 case BTF_KIND_DATASEC:
499 BTF_PRINT_ARG("section (\"%s\") ",
500 btf__name_by_offset(btf, t->name_off));
510 static int btf_dump_func(const struct btf *btf, char *func_sig,
511 const struct btf_type *func_proto,
512 const struct btf_type *func, int pos, int size)
516 BTF_PRINT_TYPE(func_proto->type);
518 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
521 vlen = BTF_INFO_VLEN(func_proto->info);
522 for (i = 0; i < vlen; i++) {
523 struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i];
528 BTF_PRINT_TYPE(arg->type);
530 btf__name_by_offset(btf, arg->name_off));
532 BTF_PRINT_ARG("...");
540 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
549 err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
554 static const char *ltrim(const char *s)
562 void btf_dump_linfo_plain(const struct btf *btf,
563 const struct bpf_line_info *linfo,
564 const char *prefix, bool linum)
566 const char *line = btf__name_by_offset(btf, linfo->line_off);
576 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
578 /* More forgiving on file because linum option is
579 * expected to provide more info than the already
580 * available src line.
585 printf("%s%s [file:%s line_num:%u line_col:%u]\n",
587 BPF_LINE_INFO_LINE_NUM(linfo->line_col),
588 BPF_LINE_INFO_LINE_COL(linfo->line_col));
590 printf("%s%s\n", prefix, line);
594 void btf_dump_linfo_json(const struct btf *btf,
595 const struct bpf_line_info *linfo, bool linum)
597 const char *line = btf__name_by_offset(btf, linfo->line_off);
600 jsonw_string_field(json_wtr, "src", ltrim(line));
603 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
606 jsonw_string_field(json_wtr, "file", file);
608 if (BPF_LINE_INFO_LINE_NUM(linfo->line_col))
609 jsonw_int_field(json_wtr, "line_num",
610 BPF_LINE_INFO_LINE_NUM(linfo->line_col));
612 if (BPF_LINE_INFO_LINE_COL(linfo->line_col))
613 jsonw_int_field(json_wtr, "line_col",
614 BPF_LINE_INFO_LINE_COL(linfo->line_col));