0001
0002
0003
0004 #include <ctype.h>
0005 #include <stdio.h> /* for (FILE *) used by json_writer */
0006 #include <string.h>
0007 #include <unistd.h>
0008 #include <asm/byteorder.h>
0009 #include <linux/bitops.h>
0010 #include <linux/btf.h>
0011 #include <linux/err.h>
0012 #include <bpf/btf.h>
0013 #include <bpf/bpf.h>
0014
0015 #include "json_writer.h"
0016 #include "main.h"
0017
0018 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
0019 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
0020 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
0021 #define BITS_ROUNDUP_BYTES(bits) \
0022 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
0023
0024 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
0025 __u8 bit_offset, const void *data);
0026
0027 static int btf_dump_func(const struct btf *btf, char *func_sig,
0028 const struct btf_type *func_proto,
0029 const struct btf_type *func, int pos, int size);
0030
0031 static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,
0032 const struct btf_type *func_proto,
0033 __u32 prog_id)
0034 {
0035 const struct btf_type *func_type;
0036 int prog_fd = -1, func_sig_len;
0037 struct bpf_prog_info info = {};
0038 __u32 info_len = sizeof(info);
0039 const char *prog_name = NULL;
0040 struct btf *prog_btf = NULL;
0041 struct bpf_func_info finfo;
0042 __u32 finfo_rec_size;
0043 char prog_str[1024];
0044 int err;
0045
0046
0047 func_sig_len = btf_dump_func(d->btf, prog_str, func_proto, NULL, 0,
0048 sizeof(prog_str));
0049 if (func_sig_len == -1)
0050 return -1;
0051
0052 if (!prog_id)
0053 goto print;
0054
0055
0056 prog_fd = bpf_prog_get_fd_by_id(prog_id);
0057 if (prog_fd < 0)
0058 goto print;
0059
0060 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
0061 if (err)
0062 goto print;
0063
0064 if (!info.btf_id || !info.nr_func_info)
0065 goto print;
0066
0067 finfo_rec_size = info.func_info_rec_size;
0068 memset(&info, 0, sizeof(info));
0069 info.nr_func_info = 1;
0070 info.func_info_rec_size = finfo_rec_size;
0071 info.func_info = ptr_to_u64(&finfo);
0072
0073 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
0074 if (err)
0075 goto print;
0076
0077 prog_btf = btf__load_from_kernel_by_id(info.btf_id);
0078 if (libbpf_get_error(prog_btf))
0079 goto print;
0080 func_type = btf__type_by_id(prog_btf, finfo.type_id);
0081 if (!func_type || !btf_is_func(func_type))
0082 goto print;
0083
0084 prog_name = btf__name_by_offset(prog_btf, func_type->name_off);
0085
0086 print:
0087 if (!prog_id)
0088 snprintf(&prog_str[func_sig_len],
0089 sizeof(prog_str) - func_sig_len, " 0");
0090 else if (prog_name)
0091 snprintf(&prog_str[func_sig_len],
0092 sizeof(prog_str) - func_sig_len,
0093 " %s/prog_id:%u", prog_name, prog_id);
0094 else
0095 snprintf(&prog_str[func_sig_len],
0096 sizeof(prog_str) - func_sig_len,
0097 " <unknown_prog_name>/prog_id:%u", prog_id);
0098
0099 prog_str[sizeof(prog_str) - 1] = '\0';
0100 jsonw_string(d->jw, prog_str);
0101 btf__free(prog_btf);
0102 if (prog_fd >= 0)
0103 close(prog_fd);
0104 return 0;
0105 }
0106
0107 static void btf_dumper_ptr(const struct btf_dumper *d,
0108 const struct btf_type *t,
0109 const void *data)
0110 {
0111 unsigned long value = *(unsigned long *)data;
0112 const struct btf_type *ptr_type;
0113 __s32 ptr_type_id;
0114
0115 if (!d->prog_id_as_func_ptr || value > UINT32_MAX)
0116 goto print_ptr_value;
0117
0118 ptr_type_id = btf__resolve_type(d->btf, t->type);
0119 if (ptr_type_id < 0)
0120 goto print_ptr_value;
0121 ptr_type = btf__type_by_id(d->btf, ptr_type_id);
0122 if (!ptr_type || !btf_is_func_proto(ptr_type))
0123 goto print_ptr_value;
0124
0125 if (!dump_prog_id_as_func_ptr(d, ptr_type, value))
0126 return;
0127
0128 print_ptr_value:
0129 if (d->is_plain_text)
0130 jsonw_printf(d->jw, "%p", (void *)value);
0131 else
0132 jsonw_printf(d->jw, "%lu", value);
0133 }
0134
0135 static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
0136 __u8 bit_offset, const void *data)
0137 {
0138 int actual_type_id;
0139
0140 actual_type_id = btf__resolve_type(d->btf, type_id);
0141 if (actual_type_id < 0)
0142 return actual_type_id;
0143
0144 return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
0145 }
0146
0147 static int btf_dumper_enum(const struct btf_dumper *d,
0148 const struct btf_type *t,
0149 const void *data)
0150 {
0151 const struct btf_enum *enums = btf_enum(t);
0152 __s64 value;
0153 __u16 i;
0154
0155 switch (t->size) {
0156 case 8:
0157 value = *(__s64 *)data;
0158 break;
0159 case 4:
0160 value = *(__s32 *)data;
0161 break;
0162 case 2:
0163 value = *(__s16 *)data;
0164 break;
0165 case 1:
0166 value = *(__s8 *)data;
0167 break;
0168 default:
0169 return -EINVAL;
0170 }
0171
0172 for (i = 0; i < btf_vlen(t); i++) {
0173 if (value == enums[i].val) {
0174 jsonw_string(d->jw,
0175 btf__name_by_offset(d->btf,
0176 enums[i].name_off));
0177 return 0;
0178 }
0179 }
0180
0181 jsonw_int(d->jw, value);
0182 return 0;
0183 }
0184
0185 static int btf_dumper_enum64(const struct btf_dumper *d,
0186 const struct btf_type *t,
0187 const void *data)
0188 {
0189 const struct btf_enum64 *enums = btf_enum64(t);
0190 __u32 val_lo32, val_hi32;
0191 __u64 value;
0192 __u16 i;
0193
0194 value = *(__u64 *)data;
0195 val_lo32 = (__u32)value;
0196 val_hi32 = value >> 32;
0197
0198 for (i = 0; i < btf_vlen(t); i++) {
0199 if (val_lo32 == enums[i].val_lo32 && val_hi32 == enums[i].val_hi32) {
0200 jsonw_string(d->jw,
0201 btf__name_by_offset(d->btf,
0202 enums[i].name_off));
0203 return 0;
0204 }
0205 }
0206
0207 jsonw_int(d->jw, value);
0208 return 0;
0209 }
0210
0211 static bool is_str_array(const struct btf *btf, const struct btf_array *arr,
0212 const char *s)
0213 {
0214 const struct btf_type *elem_type;
0215 const char *end_s;
0216
0217 if (!arr->nelems)
0218 return false;
0219
0220 elem_type = btf__type_by_id(btf, arr->type);
0221
0222
0223
0224 while (elem_type && btf_is_mod(elem_type))
0225 elem_type = btf__type_by_id(btf, elem_type->type);
0226
0227 if (!elem_type || !btf_is_int(elem_type) || elem_type->size != 1)
0228 return false;
0229
0230 if (btf_int_encoding(elem_type) != BTF_INT_CHAR &&
0231 strcmp("char", btf__name_by_offset(btf, elem_type->name_off)))
0232 return false;
0233
0234 end_s = s + arr->nelems;
0235 while (s < end_s) {
0236 if (!*s)
0237 return true;
0238 if (*s <= 0x1f || *s >= 0x7f)
0239 return false;
0240 s++;
0241 }
0242
0243
0244 return false;
0245 }
0246
0247 static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
0248 const void *data)
0249 {
0250 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
0251 struct btf_array *arr = (struct btf_array *)(t + 1);
0252 long long elem_size;
0253 int ret = 0;
0254 __u32 i;
0255
0256 if (is_str_array(d->btf, arr, data)) {
0257 jsonw_string(d->jw, data);
0258 return 0;
0259 }
0260
0261 elem_size = btf__resolve_size(d->btf, arr->type);
0262 if (elem_size < 0)
0263 return elem_size;
0264
0265 jsonw_start_array(d->jw);
0266 for (i = 0; i < arr->nelems; i++) {
0267 ret = btf_dumper_do_type(d, arr->type, 0,
0268 data + i * elem_size);
0269 if (ret)
0270 break;
0271 }
0272
0273 jsonw_end_array(d->jw);
0274 return ret;
0275 }
0276
0277 static void btf_int128_print(json_writer_t *jw, const void *data,
0278 bool is_plain_text)
0279 {
0280
0281
0282
0283
0284
0285
0286
0287 __u64 upper_num, lower_num;
0288
0289 #ifdef __BIG_ENDIAN_BITFIELD
0290 upper_num = *(__u64 *)data;
0291 lower_num = *(__u64 *)(data + 8);
0292 #else
0293 upper_num = *(__u64 *)(data + 8);
0294 lower_num = *(__u64 *)data;
0295 #endif
0296
0297 if (is_plain_text) {
0298 if (upper_num == 0)
0299 jsonw_printf(jw, "0x%llx", lower_num);
0300 else
0301 jsonw_printf(jw, "0x%llx%016llx", upper_num, lower_num);
0302 } else {
0303 if (upper_num == 0)
0304 jsonw_printf(jw, "\"0x%llx\"", lower_num);
0305 else
0306 jsonw_printf(jw, "\"0x%llx%016llx\"", upper_num, lower_num);
0307 }
0308 }
0309
0310 static void btf_int128_shift(__u64 *print_num, __u16 left_shift_bits,
0311 __u16 right_shift_bits)
0312 {
0313 __u64 upper_num, lower_num;
0314
0315 #ifdef __BIG_ENDIAN_BITFIELD
0316 upper_num = print_num[0];
0317 lower_num = print_num[1];
0318 #else
0319 upper_num = print_num[1];
0320 lower_num = print_num[0];
0321 #endif
0322
0323
0324 if (left_shift_bits >= 64) {
0325 upper_num = lower_num << (left_shift_bits - 64);
0326 lower_num = 0;
0327 } else {
0328 upper_num = (upper_num << left_shift_bits) |
0329 (lower_num >> (64 - left_shift_bits));
0330 lower_num = lower_num << left_shift_bits;
0331 }
0332
0333 if (right_shift_bits >= 64) {
0334 lower_num = upper_num >> (right_shift_bits - 64);
0335 upper_num = 0;
0336 } else {
0337 lower_num = (lower_num >> right_shift_bits) |
0338 (upper_num << (64 - right_shift_bits));
0339 upper_num = upper_num >> right_shift_bits;
0340 }
0341
0342 #ifdef __BIG_ENDIAN_BITFIELD
0343 print_num[0] = upper_num;
0344 print_num[1] = lower_num;
0345 #else
0346 print_num[0] = lower_num;
0347 print_num[1] = upper_num;
0348 #endif
0349 }
0350
0351 static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
0352 const void *data, json_writer_t *jw,
0353 bool is_plain_text)
0354 {
0355 int left_shift_bits, right_shift_bits;
0356 __u64 print_num[2] = {};
0357 int bytes_to_copy;
0358 int bits_to_copy;
0359
0360 bits_to_copy = bit_offset + nr_bits;
0361 bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
0362
0363 memcpy(print_num, data, bytes_to_copy);
0364 #if defined(__BIG_ENDIAN_BITFIELD)
0365 left_shift_bits = bit_offset;
0366 #elif defined(__LITTLE_ENDIAN_BITFIELD)
0367 left_shift_bits = 128 - bits_to_copy;
0368 #else
0369 #error neither big nor little endian
0370 #endif
0371 right_shift_bits = 128 - nr_bits;
0372
0373 btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
0374 btf_int128_print(jw, print_num, is_plain_text);
0375 }
0376
0377
0378 static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
0379 const void *data, json_writer_t *jw,
0380 bool is_plain_text)
0381 {
0382 int nr_bits = BTF_INT_BITS(int_type);
0383 int total_bits_offset;
0384
0385
0386
0387
0388 total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type);
0389 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
0390 bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
0391 btf_dumper_bitfield(nr_bits, bit_offset, data, jw,
0392 is_plain_text);
0393 }
0394
0395 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
0396 const void *data, json_writer_t *jw,
0397 bool is_plain_text)
0398 {
0399 __u32 *int_type;
0400 __u32 nr_bits;
0401
0402 int_type = (__u32 *)(t + 1);
0403 nr_bits = BTF_INT_BITS(*int_type);
0404
0405 if (bit_offset || BTF_INT_OFFSET(*int_type) ||
0406 BITS_PER_BYTE_MASKED(nr_bits)) {
0407 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
0408 is_plain_text);
0409 return 0;
0410 }
0411
0412 if (nr_bits == 128) {
0413 btf_int128_print(jw, data, is_plain_text);
0414 return 0;
0415 }
0416
0417 switch (BTF_INT_ENCODING(*int_type)) {
0418 case 0:
0419 if (BTF_INT_BITS(*int_type) == 64)
0420 jsonw_printf(jw, "%llu", *(__u64 *)data);
0421 else if (BTF_INT_BITS(*int_type) == 32)
0422 jsonw_printf(jw, "%u", *(__u32 *)data);
0423 else if (BTF_INT_BITS(*int_type) == 16)
0424 jsonw_printf(jw, "%hu", *(__u16 *)data);
0425 else if (BTF_INT_BITS(*int_type) == 8)
0426 jsonw_printf(jw, "%hhu", *(__u8 *)data);
0427 else
0428 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
0429 is_plain_text);
0430 break;
0431 case BTF_INT_SIGNED:
0432 if (BTF_INT_BITS(*int_type) == 64)
0433 jsonw_printf(jw, "%lld", *(long long *)data);
0434 else if (BTF_INT_BITS(*int_type) == 32)
0435 jsonw_printf(jw, "%d", *(int *)data);
0436 else if (BTF_INT_BITS(*int_type) == 16)
0437 jsonw_printf(jw, "%hd", *(short *)data);
0438 else if (BTF_INT_BITS(*int_type) == 8)
0439 jsonw_printf(jw, "%hhd", *(char *)data);
0440 else
0441 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
0442 is_plain_text);
0443 break;
0444 case BTF_INT_CHAR:
0445 if (isprint(*(char *)data))
0446 jsonw_printf(jw, "\"%c\"", *(char *)data);
0447 else
0448 if (is_plain_text)
0449 jsonw_printf(jw, "0x%hhx", *(char *)data);
0450 else
0451 jsonw_printf(jw, "\"\\u00%02hhx\"",
0452 *(char *)data);
0453 break;
0454 case BTF_INT_BOOL:
0455 jsonw_bool(jw, *(int *)data);
0456 break;
0457 default:
0458
0459 return -EINVAL;
0460 }
0461
0462 return 0;
0463 }
0464
0465 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
0466 const void *data)
0467 {
0468 const struct btf_type *t;
0469 struct btf_member *m;
0470 const void *data_off;
0471 int kind_flag;
0472 int ret = 0;
0473 int i, vlen;
0474
0475 t = btf__type_by_id(d->btf, type_id);
0476 if (!t)
0477 return -EINVAL;
0478
0479 kind_flag = BTF_INFO_KFLAG(t->info);
0480 vlen = BTF_INFO_VLEN(t->info);
0481 jsonw_start_object(d->jw);
0482 m = (struct btf_member *)(t + 1);
0483
0484 for (i = 0; i < vlen; i++) {
0485 __u32 bit_offset = m[i].offset;
0486 __u32 bitfield_size = 0;
0487
0488 if (kind_flag) {
0489 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
0490 bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
0491 }
0492
0493 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
0494 data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
0495 if (bitfield_size) {
0496 btf_dumper_bitfield(bitfield_size,
0497 BITS_PER_BYTE_MASKED(bit_offset),
0498 data_off, d->jw, d->is_plain_text);
0499 } else {
0500 ret = btf_dumper_do_type(d, m[i].type,
0501 BITS_PER_BYTE_MASKED(bit_offset),
0502 data_off);
0503 if (ret)
0504 break;
0505 }
0506 }
0507
0508 jsonw_end_object(d->jw);
0509
0510 return ret;
0511 }
0512
0513 static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
0514 __u8 bit_offset, const void *data)
0515 {
0516 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
0517 int ret;
0518
0519 jsonw_start_object(d->jw);
0520 jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
0521 ret = btf_dumper_do_type(d, t->type, bit_offset, data);
0522 jsonw_end_object(d->jw);
0523
0524 return ret;
0525 }
0526
0527 static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
0528 const void *data)
0529 {
0530 struct btf_var_secinfo *vsi;
0531 const struct btf_type *t;
0532 int ret = 0, i, vlen;
0533
0534 t = btf__type_by_id(d->btf, type_id);
0535 if (!t)
0536 return -EINVAL;
0537
0538 vlen = BTF_INFO_VLEN(t->info);
0539 vsi = (struct btf_var_secinfo *)(t + 1);
0540
0541 jsonw_start_object(d->jw);
0542 jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
0543 jsonw_start_array(d->jw);
0544 for (i = 0; i < vlen; i++) {
0545 ret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);
0546 if (ret)
0547 break;
0548 }
0549 jsonw_end_array(d->jw);
0550 jsonw_end_object(d->jw);
0551
0552 return ret;
0553 }
0554
0555 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
0556 __u8 bit_offset, const void *data)
0557 {
0558 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
0559
0560 switch (BTF_INFO_KIND(t->info)) {
0561 case BTF_KIND_INT:
0562 return btf_dumper_int(t, bit_offset, data, d->jw,
0563 d->is_plain_text);
0564 case BTF_KIND_STRUCT:
0565 case BTF_KIND_UNION:
0566 return btf_dumper_struct(d, type_id, data);
0567 case BTF_KIND_ARRAY:
0568 return btf_dumper_array(d, type_id, data);
0569 case BTF_KIND_ENUM:
0570 return btf_dumper_enum(d, t, data);
0571 case BTF_KIND_ENUM64:
0572 return btf_dumper_enum64(d, t, data);
0573 case BTF_KIND_PTR:
0574 btf_dumper_ptr(d, t, data);
0575 return 0;
0576 case BTF_KIND_UNKN:
0577 jsonw_printf(d->jw, "(unknown)");
0578 return 0;
0579 case BTF_KIND_FWD:
0580
0581 jsonw_printf(d->jw, "(fwd-kind-invalid)");
0582 return -EINVAL;
0583 case BTF_KIND_TYPEDEF:
0584 case BTF_KIND_VOLATILE:
0585 case BTF_KIND_CONST:
0586 case BTF_KIND_RESTRICT:
0587 return btf_dumper_modifier(d, type_id, bit_offset, data);
0588 case BTF_KIND_VAR:
0589 return btf_dumper_var(d, type_id, bit_offset, data);
0590 case BTF_KIND_DATASEC:
0591 return btf_dumper_datasec(d, type_id, data);
0592 default:
0593 jsonw_printf(d->jw, "(unsupported-kind");
0594 return -EINVAL;
0595 }
0596 }
0597
0598 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
0599 const void *data)
0600 {
0601 return btf_dumper_do_type(d, type_id, 0, data);
0602 }
0603
0604 #define BTF_PRINT_ARG(...) \
0605 do { \
0606 pos += snprintf(func_sig + pos, size - pos, \
0607 __VA_ARGS__); \
0608 if (pos >= size) \
0609 return -1; \
0610 } while (0)
0611 #define BTF_PRINT_TYPE(type) \
0612 do { \
0613 pos = __btf_dumper_type_only(btf, type, func_sig, \
0614 pos, size); \
0615 if (pos == -1) \
0616 return -1; \
0617 } while (0)
0618
0619 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
0620 char *func_sig, int pos, int size)
0621 {
0622 const struct btf_type *proto_type;
0623 const struct btf_array *array;
0624 const struct btf_var *var;
0625 const struct btf_type *t;
0626
0627 if (!type_id) {
0628 BTF_PRINT_ARG("void ");
0629 return pos;
0630 }
0631
0632 t = btf__type_by_id(btf, type_id);
0633
0634 switch (BTF_INFO_KIND(t->info)) {
0635 case BTF_KIND_INT:
0636 case BTF_KIND_TYPEDEF:
0637 case BTF_KIND_FLOAT:
0638 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
0639 break;
0640 case BTF_KIND_STRUCT:
0641 BTF_PRINT_ARG("struct %s ",
0642 btf__name_by_offset(btf, t->name_off));
0643 break;
0644 case BTF_KIND_UNION:
0645 BTF_PRINT_ARG("union %s ",
0646 btf__name_by_offset(btf, t->name_off));
0647 break;
0648 case BTF_KIND_ENUM:
0649 case BTF_KIND_ENUM64:
0650 BTF_PRINT_ARG("enum %s ",
0651 btf__name_by_offset(btf, t->name_off));
0652 break;
0653 case BTF_KIND_ARRAY:
0654 array = (struct btf_array *)(t + 1);
0655 BTF_PRINT_TYPE(array->type);
0656 BTF_PRINT_ARG("[%d]", array->nelems);
0657 break;
0658 case BTF_KIND_PTR:
0659 BTF_PRINT_TYPE(t->type);
0660 BTF_PRINT_ARG("* ");
0661 break;
0662 case BTF_KIND_FWD:
0663 BTF_PRINT_ARG("%s %s ",
0664 BTF_INFO_KFLAG(t->info) ? "union" : "struct",
0665 btf__name_by_offset(btf, t->name_off));
0666 break;
0667 case BTF_KIND_VOLATILE:
0668 BTF_PRINT_ARG("volatile ");
0669 BTF_PRINT_TYPE(t->type);
0670 break;
0671 case BTF_KIND_CONST:
0672 BTF_PRINT_ARG("const ");
0673 BTF_PRINT_TYPE(t->type);
0674 break;
0675 case BTF_KIND_RESTRICT:
0676 BTF_PRINT_ARG("restrict ");
0677 BTF_PRINT_TYPE(t->type);
0678 break;
0679 case BTF_KIND_FUNC_PROTO:
0680 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
0681 if (pos == -1)
0682 return -1;
0683 break;
0684 case BTF_KIND_FUNC:
0685 proto_type = btf__type_by_id(btf, t->type);
0686 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
0687 if (pos == -1)
0688 return -1;
0689 break;
0690 case BTF_KIND_VAR:
0691 var = (struct btf_var *)(t + 1);
0692 if (var->linkage == BTF_VAR_STATIC)
0693 BTF_PRINT_ARG("static ");
0694 BTF_PRINT_TYPE(t->type);
0695 BTF_PRINT_ARG(" %s",
0696 btf__name_by_offset(btf, t->name_off));
0697 break;
0698 case BTF_KIND_DATASEC:
0699 BTF_PRINT_ARG("section (\"%s\") ",
0700 btf__name_by_offset(btf, t->name_off));
0701 break;
0702 case BTF_KIND_UNKN:
0703 default:
0704 return -1;
0705 }
0706
0707 return pos;
0708 }
0709
0710 static int btf_dump_func(const struct btf *btf, char *func_sig,
0711 const struct btf_type *func_proto,
0712 const struct btf_type *func, int pos, int size)
0713 {
0714 int i, vlen;
0715
0716 BTF_PRINT_TYPE(func_proto->type);
0717 if (func)
0718 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
0719 else
0720 BTF_PRINT_ARG("(");
0721 vlen = BTF_INFO_VLEN(func_proto->info);
0722 for (i = 0; i < vlen; i++) {
0723 struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i];
0724
0725 if (i)
0726 BTF_PRINT_ARG(", ");
0727 if (arg->type) {
0728 BTF_PRINT_TYPE(arg->type);
0729 if (arg->name_off)
0730 BTF_PRINT_ARG("%s",
0731 btf__name_by_offset(btf, arg->name_off));
0732 else if (pos && func_sig[pos - 1] == ' ')
0733
0734
0735
0736
0737 func_sig[--pos] = '\0';
0738 } else {
0739 BTF_PRINT_ARG("...");
0740 }
0741 }
0742 BTF_PRINT_ARG(")");
0743
0744 return pos;
0745 }
0746
0747 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
0748 int size)
0749 {
0750 int err;
0751
0752 func_sig[0] = '\0';
0753 if (!btf)
0754 return;
0755
0756 err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
0757 if (err < 0)
0758 func_sig[0] = '\0';
0759 }
0760
0761 static const char *ltrim(const char *s)
0762 {
0763 while (isspace(*s))
0764 s++;
0765
0766 return s;
0767 }
0768
0769 void btf_dump_linfo_plain(const struct btf *btf,
0770 const struct bpf_line_info *linfo,
0771 const char *prefix, bool linum)
0772 {
0773 const char *line = btf__name_by_offset(btf, linfo->line_off);
0774
0775 if (!line)
0776 return;
0777 line = ltrim(line);
0778
0779 if (!prefix)
0780 prefix = "";
0781
0782 if (linum) {
0783 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
0784
0785
0786
0787
0788
0789 if (!file)
0790 file = "";
0791
0792 printf("%s%s [file:%s line_num:%u line_col:%u]\n",
0793 prefix, line, file,
0794 BPF_LINE_INFO_LINE_NUM(linfo->line_col),
0795 BPF_LINE_INFO_LINE_COL(linfo->line_col));
0796 } else {
0797 printf("%s%s\n", prefix, line);
0798 }
0799 }
0800
0801 void btf_dump_linfo_json(const struct btf *btf,
0802 const struct bpf_line_info *linfo, bool linum)
0803 {
0804 const char *line = btf__name_by_offset(btf, linfo->line_off);
0805
0806 if (line)
0807 jsonw_string_field(json_wtr, "src", ltrim(line));
0808
0809 if (linum) {
0810 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
0811
0812 if (file)
0813 jsonw_string_field(json_wtr, "file", file);
0814
0815 if (BPF_LINE_INFO_LINE_NUM(linfo->line_col))
0816 jsonw_int_field(json_wtr, "line_num",
0817 BPF_LINE_INFO_LINE_NUM(linfo->line_col));
0818
0819 if (BPF_LINE_INFO_LINE_COL(linfo->line_col))
0820 jsonw_int_field(json_wtr, "line_col",
0821 BPF_LINE_INFO_LINE_COL(linfo->line_col));
0822 }
0823 }