1 // SPDX-License-Identifier: GPL-2.0
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_int_bits(__u32 int_type, __u8 bit_offset,
77 const void *data, json_writer_t *jw,
80 int left_shift_bits, right_shift_bits;
81 int nr_bits = BTF_INT_BITS(int_type);
82 int total_bits_offset;
87 total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type);
88 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
89 bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
90 bits_to_copy = bit_offset + nr_bits;
91 bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
94 memcpy(&print_num, data, bytes_to_copy);
95 #if defined(__BIG_ENDIAN_BITFIELD)
96 left_shift_bits = bit_offset;
97 #elif defined(__LITTLE_ENDIAN_BITFIELD)
98 left_shift_bits = 64 - bits_to_copy;
100 #error neither big nor little endian
102 right_shift_bits = 64 - nr_bits;
104 print_num <<= left_shift_bits;
105 print_num >>= right_shift_bits;
107 jsonw_printf(jw, "0x%llx", print_num);
109 jsonw_printf(jw, "%llu", print_num);
112 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
113 const void *data, json_writer_t *jw,
119 int_type = (__u32 *)(t + 1);
120 nr_bits = BTF_INT_BITS(*int_type);
121 /* if this is bit field */
122 if (bit_offset || BTF_INT_OFFSET(*int_type) ||
123 BITS_PER_BYTE_MASKED(nr_bits)) {
124 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
129 switch (BTF_INT_ENCODING(*int_type)) {
131 if (BTF_INT_BITS(*int_type) == 64)
132 jsonw_printf(jw, "%lu", *(__u64 *)data);
133 else if (BTF_INT_BITS(*int_type) == 32)
134 jsonw_printf(jw, "%u", *(__u32 *)data);
135 else if (BTF_INT_BITS(*int_type) == 16)
136 jsonw_printf(jw, "%hu", *(__u16 *)data);
137 else if (BTF_INT_BITS(*int_type) == 8)
138 jsonw_printf(jw, "%hhu", *(__u8 *)data);
140 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
144 if (BTF_INT_BITS(*int_type) == 64)
145 jsonw_printf(jw, "%ld", *(long long *)data);
146 else if (BTF_INT_BITS(*int_type) == 32)
147 jsonw_printf(jw, "%d", *(int *)data);
148 else if (BTF_INT_BITS(*int_type) == 16)
149 jsonw_printf(jw, "%hd", *(short *)data);
150 else if (BTF_INT_BITS(*int_type) == 8)
151 jsonw_printf(jw, "%hhd", *(char *)data);
153 btf_dumper_int_bits(*int_type, bit_offset, data, jw,
157 if (isprint(*(char *)data))
158 jsonw_printf(jw, "\"%c\"", *(char *)data);
161 jsonw_printf(jw, "0x%hhx", *(char *)data);
163 jsonw_printf(jw, "\"\\u00%02hhx\"",
167 jsonw_bool(jw, *(int *)data);
170 /* shouldn't happen */
177 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
180 const struct btf_type *t;
181 struct btf_member *m;
182 const void *data_off;
186 t = btf__type_by_id(d->btf, type_id);
190 vlen = BTF_INFO_VLEN(t->info);
191 jsonw_start_object(d->jw);
192 m = (struct btf_member *)(t + 1);
194 for (i = 0; i < vlen; i++) {
195 data_off = data + BITS_ROUNDDOWN_BYTES(m[i].offset);
196 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
197 ret = btf_dumper_do_type(d, m[i].type,
198 BITS_PER_BYTE_MASKED(m[i].offset),
204 jsonw_end_object(d->jw);
209 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
210 __u8 bit_offset, const void *data)
212 const struct btf_type *t = btf__type_by_id(d->btf, type_id);
214 switch (BTF_INFO_KIND(t->info)) {
216 return btf_dumper_int(t, bit_offset, data, d->jw,
218 case BTF_KIND_STRUCT:
220 return btf_dumper_struct(d, type_id, data);
222 return btf_dumper_array(d, type_id, data);
224 btf_dumper_enum(data, d->jw);
227 btf_dumper_ptr(data, d->jw, d->is_plain_text);
230 jsonw_printf(d->jw, "(unknown)");
233 /* map key or value can't be forward */
234 jsonw_printf(d->jw, "(fwd-kind-invalid)");
236 case BTF_KIND_TYPEDEF:
237 case BTF_KIND_VOLATILE:
239 case BTF_KIND_RESTRICT:
240 return btf_dumper_modifier(d, type_id, bit_offset, data);
242 jsonw_printf(d->jw, "(unsupported-kind");
247 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
250 return btf_dumper_do_type(d, type_id, 0, data);
253 #define BTF_PRINT_ARG(...) \
255 pos += snprintf(func_sig + pos, size - pos, \
260 #define BTF_PRINT_TYPE(type) \
262 pos = __btf_dumper_type_only(btf, type, func_sig, \
268 static int btf_dump_func(const struct btf *btf, char *func_sig,
269 const struct btf_type *func_proto,
270 const struct btf_type *func, int pos, int size);
272 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
273 char *func_sig, int pos, int size)
275 const struct btf_type *proto_type;
276 const struct btf_array *array;
277 const struct btf_type *t;
280 BTF_PRINT_ARG("void ");
284 t = btf__type_by_id(btf, type_id);
286 switch (BTF_INFO_KIND(t->info)) {
288 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
290 case BTF_KIND_STRUCT:
291 BTF_PRINT_ARG("struct %s ",
292 btf__name_by_offset(btf, t->name_off));
295 BTF_PRINT_ARG("union %s ",
296 btf__name_by_offset(btf, t->name_off));
299 BTF_PRINT_ARG("enum %s ",
300 btf__name_by_offset(btf, t->name_off));
303 array = (struct btf_array *)(t + 1);
304 BTF_PRINT_TYPE(array->type);
305 BTF_PRINT_ARG("[%d]", array->nelems);
308 BTF_PRINT_TYPE(t->type);
313 case BTF_KIND_TYPEDEF:
315 case BTF_KIND_VOLATILE:
316 BTF_PRINT_ARG("volatile ");
317 BTF_PRINT_TYPE(t->type);
320 BTF_PRINT_ARG("const ");
321 BTF_PRINT_TYPE(t->type);
323 case BTF_KIND_RESTRICT:
324 BTF_PRINT_ARG("restrict ");
325 BTF_PRINT_TYPE(t->type);
327 case BTF_KIND_FUNC_PROTO:
328 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
333 proto_type = btf__type_by_id(btf, t->type);
334 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
345 static int btf_dump_func(const struct btf *btf, char *func_sig,
346 const struct btf_type *func_proto,
347 const struct btf_type *func, int pos, int size)
351 BTF_PRINT_TYPE(func_proto->type);
353 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
356 vlen = BTF_INFO_VLEN(func_proto->info);
357 for (i = 0; i < vlen; i++) {
358 struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i];
363 BTF_PRINT_TYPE(arg->type);
365 btf__name_by_offset(btf, arg->name_off));
367 BTF_PRINT_ARG("...");
375 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
384 err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
389 static const char *ltrim(const char *s)
397 void btf_dump_linfo_plain(const struct btf *btf,
398 const struct bpf_line_info *linfo,
399 const char *prefix, bool linum)
401 const char *line = btf__name_by_offset(btf, linfo->line_off);
411 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
413 /* More forgiving on file because linum option is
414 * expected to provide more info than the already
415 * available src line.
420 printf("%s%s [file:%s line_num:%u line_col:%u]\n",
422 BPF_LINE_INFO_LINE_NUM(linfo->line_col),
423 BPF_LINE_INFO_LINE_COL(linfo->line_col));
425 printf("%s%s\n", prefix, line);
429 void btf_dump_linfo_json(const struct btf *btf,
430 const struct bpf_line_info *linfo, bool linum)
432 const char *line = btf__name_by_offset(btf, linfo->line_off);
435 jsonw_string_field(json_wtr, "src", ltrim(line));
438 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
441 jsonw_string_field(json_wtr, "file", file);
443 if (BPF_LINE_INFO_LINE_NUM(linfo->line_col))
444 jsonw_int_field(json_wtr, "line_num",
445 BPF_LINE_INFO_LINE_NUM(linfo->line_col));
447 if (BPF_LINE_INFO_LINE_COL(linfo->line_col))
448 jsonw_int_field(json_wtr, "line_col",
449 BPF_LINE_INFO_LINE_COL(linfo->line_col));