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", *(unsigned long *)data);
31 jsonw_printf(jw, "%u", *(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_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
77 const void *data, json_writer_t *jw,
80 int left_shift_bits, right_shift_bits;
85 bits_to_copy = bit_offset + nr_bits;
86 bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
89 memcpy(&print_num, data, bytes_to_copy);
90 #if defined(__BIG_ENDIAN_BITFIELD)
91 left_shift_bits = bit_offset;
92 #elif defined(__LITTLE_ENDIAN_BITFIELD)
93 left_shift_bits = 64 - bits_to_copy;
95 #error neither big nor little endian
97 right_shift_bits = 64 - nr_bits;
99 print_num <<= left_shift_bits;
100 print_num >>= right_shift_bits;
102 jsonw_printf(jw, "0x%llx", print_num);
104 jsonw_printf(jw, "%llu", print_num);
108 static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
109 const void *data, json_writer_t *jw,
112 int nr_bits = BTF_INT_BITS(int_type);
113 int total_bits_offset;
115 /* bits_offset is at most 7.
116 * BTF_INT_OFFSET() cannot exceed 64 bits.
118 total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type);
119 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
120 bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
121 btf_dumper_bitfield(nr_bits, bit_offset, data, jw,
125 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
126 const void *data, json_writer_t *jw,
132 int_type = (__u32 *)(t + 1);
133 nr_bits = BTF_INT_BITS(*int_type);
134 /* if this is bit field */
135 if (bit_offset || BTF_INT_OFFSET(*int_type) ||
136 BITS_PER_BYTE_MASKED(nr_bits)) {
137 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
142 switch (BTF_INT_ENCODING(*int_type)) {
144 if (BTF_INT_BITS(*int_type) == 64)
145 jsonw_printf(jw, "%lu", *(__u64 *)data);
146 else if (BTF_INT_BITS(*int_type) == 32)
147 jsonw_printf(jw, "%u", *(__u32 *)data);
148 else if (BTF_INT_BITS(*int_type) == 16)
149 jsonw_printf(jw, "%hu", *(__u16 *)data);
150 else if (BTF_INT_BITS(*int_type) == 8)
151 jsonw_printf(jw, "%hhu", *(__u8 *)data);
153 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
157 if (BTF_INT_BITS(*int_type) == 64)
158 jsonw_printf(jw, "%ld", *(long long *)data);
159 else if (BTF_INT_BITS(*int_type) == 32)
160 jsonw_printf(jw, "%d", *(int *)data);
161 else if (BTF_INT_BITS(*int_type) == 16)
162 jsonw_printf(jw, "%hd", *(short *)data);
163 else if (BTF_INT_BITS(*int_type) == 8)
164 jsonw_printf(jw, "%hhd", *(char *)data);
166 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
170 if (isprint(*(char *)data))
171 jsonw_printf(jw, "\"%c\"", *(char *)data);
174 jsonw_printf(jw, "0x%hhx", *(char *)data);
176 jsonw_printf(jw, "\"\\u00%02hhx\"",
180 jsonw_bool(jw, *(int *)data);
183 /* shouldn't happen */
190 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
193 const struct btf_type *t;
194 struct btf_member *m;
195 const void *data_off;
200 t = btf__type_by_id(d->btf, type_id);
204 kind_flag = BTF_INFO_KFLAG(t->info);
205 vlen = BTF_INFO_VLEN(t->info);
206 jsonw_start_object(d->jw);
207 m = (struct btf_member *)(t + 1);
209 for (i = 0; i < vlen; i++) {
210 __u32 bit_offset = m[i].offset;
211 __u32 bitfield_size = 0;
214 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
215 bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
218 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
219 data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
221 btf_dumper_bitfield(bitfield_size,
222 BITS_PER_BYTE_MASKED(bit_offset),
223 data_off, d->jw, d->is_plain_text);
225 ret = btf_dumper_do_type(d, m[i].type,
226 BITS_PER_BYTE_MASKED(bit_offset),
233 jsonw_end_object(d->jw);
238 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
239 __u8 bit_offset, const void *data)
241 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
243 switch (BTF_INFO_KIND(t->info)) {
245 return btf_dumper_int(t, bit_offset, data, d->jw,
247 case BTF_KIND_STRUCT:
249 return btf_dumper_struct(d, type_id, data);
251 return btf_dumper_array(d, type_id, data);
253 btf_dumper_enum(data, d->jw);
256 btf_dumper_ptr(data, d->jw, d->is_plain_text);
259 jsonw_printf(d->jw, "(unknown)");
262 /* map key or value can't be forward */
263 jsonw_printf(d->jw, "(fwd-kind-invalid)");
265 case BTF_KIND_TYPEDEF:
266 case BTF_KIND_VOLATILE:
268 case BTF_KIND_RESTRICT:
269 return btf_dumper_modifier(d, type_id, bit_offset, data);
271 jsonw_printf(d->jw, "(unsupported-kind");
276 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
279 return btf_dumper_do_type(d, type_id, 0, data);
282 #define BTF_PRINT_ARG(...) \
284 pos += snprintf(func_sig + pos, size - pos, \
289 #define BTF_PRINT_TYPE(type) \
291 pos = __btf_dumper_type_only(btf, type, func_sig, \
297 static int btf_dump_func(const struct btf *btf, char *func_sig,
298 const struct btf_type *func_proto,
299 const struct btf_type *func, int pos, int size);
301 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
302 char *func_sig, int pos, int size)
304 const struct btf_type *proto_type;
305 const struct btf_array *array;
306 const struct btf_type *t;
309 BTF_PRINT_ARG("void ");
313 t = btf__type_by_id(btf, type_id);
315 switch (BTF_INFO_KIND(t->info)) {
317 case BTF_KIND_TYPEDEF:
318 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
320 case BTF_KIND_STRUCT:
321 BTF_PRINT_ARG("struct %s ",
322 btf__name_by_offset(btf, t->name_off));
325 BTF_PRINT_ARG("union %s ",
326 btf__name_by_offset(btf, t->name_off));
329 BTF_PRINT_ARG("enum %s ",
330 btf__name_by_offset(btf, t->name_off));
333 array = (struct btf_array *)(t + 1);
334 BTF_PRINT_TYPE(array->type);
335 BTF_PRINT_ARG("[%d]", array->nelems);
338 BTF_PRINT_TYPE(t->type);
342 BTF_PRINT_ARG("%s %s ",
343 BTF_INFO_KFLAG(t->info) ? "union" : "struct",
344 btf__name_by_offset(btf, t->name_off));
346 case BTF_KIND_VOLATILE:
347 BTF_PRINT_ARG("volatile ");
348 BTF_PRINT_TYPE(t->type);
351 BTF_PRINT_ARG("const ");
352 BTF_PRINT_TYPE(t->type);
354 case BTF_KIND_RESTRICT:
355 BTF_PRINT_ARG("restrict ");
356 BTF_PRINT_TYPE(t->type);
358 case BTF_KIND_FUNC_PROTO:
359 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
364 proto_type = btf__type_by_id(btf, t->type);
365 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
377 static int btf_dump_func(const struct btf *btf, char *func_sig,
378 const struct btf_type *func_proto,
379 const struct btf_type *func, int pos, int size)
383 BTF_PRINT_TYPE(func_proto->type);
385 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
388 vlen = BTF_INFO_VLEN(func_proto->info);
389 for (i = 0; i < vlen; i++) {
390 struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i];
395 BTF_PRINT_TYPE(arg->type);
397 btf__name_by_offset(btf, arg->name_off));
399 BTF_PRINT_ARG("...");
407 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
416 err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
421 static const char *ltrim(const char *s)
429 void btf_dump_linfo_plain(const struct btf *btf,
430 const struct bpf_line_info *linfo,
431 const char *prefix, bool linum)
433 const char *line = btf__name_by_offset(btf, linfo->line_off);
443 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
445 /* More forgiving on file because linum option is
446 * expected to provide more info than the already
447 * available src line.
452 printf("%s%s [file:%s line_num:%u line_col:%u]\n",
454 BPF_LINE_INFO_LINE_NUM(linfo->line_col),
455 BPF_LINE_INFO_LINE_COL(linfo->line_col));
457 printf("%s%s\n", prefix, line);
461 void btf_dump_linfo_json(const struct btf *btf,
462 const struct bpf_line_info *linfo, bool linum)
464 const char *line = btf__name_by_offset(btf, linfo->line_off);
467 jsonw_string_field(json_wtr, "src", ltrim(line));
470 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
473 jsonw_string_field(json_wtr, "file", file);
475 if (BPF_LINE_INFO_LINE_NUM(linfo->line_col))
476 jsonw_int_field(json_wtr, "line_num",
477 BPF_LINE_INFO_LINE_NUM(linfo->line_col));
479 if (BPF_LINE_INFO_LINE_COL(linfo->line_col))
480 jsonw_int_field(json_wtr, "line_col",
481 BPF_LINE_INFO_LINE_COL(linfo->line_col));