OSDN Git Service

Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[uclinux-h8/linux.git] / tools / bpf / bpftool / btf_dumper.c
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
3
4 #include <ctype.h>
5 #include <stdio.h> /* for (FILE *) used by json_writer */
6 #include <string.h>
7 #include <asm/byteorder.h>
8 #include <linux/bitops.h>
9 #include <linux/btf.h>
10 #include <linux/err.h>
11
12 #include "btf.h"
13 #include "json_writer.h"
14 #include "main.h"
15
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))
21
22 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
23                               __u8 bit_offset, const void *data);
24
25 static void btf_dumper_ptr(const void *data, json_writer_t *jw,
26                            bool is_plain_text)
27 {
28         if (is_plain_text)
29                 jsonw_printf(jw, "%p", *(unsigned long *)data);
30         else
31                 jsonw_printf(jw, "%u", *(unsigned long *)data);
32 }
33
34 static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
35                                __u8 bit_offset, const void *data)
36 {
37         int actual_type_id;
38
39         actual_type_id = btf__resolve_type(d->btf, type_id);
40         if (actual_type_id < 0)
41                 return actual_type_id;
42
43         return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
44 }
45
46 static void btf_dumper_enum(const void *data, json_writer_t *jw)
47 {
48         jsonw_printf(jw, "%d", *(int *)data);
49 }
50
51 static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
52                             const void *data)
53 {
54         const struct btf_type *t = btf__type_by_id(d->btf, type_id);
55         struct btf_array *arr = (struct btf_array *)(t + 1);
56         long long elem_size;
57         int ret = 0;
58         __u32 i;
59
60         elem_size = btf__resolve_size(d->btf, arr->type);
61         if (elem_size < 0)
62                 return elem_size;
63
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);
68                 if (ret)
69                         break;
70         }
71
72         jsonw_end_array(d->jw);
73         return ret;
74 }
75
76 static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
77                                 const void *data, json_writer_t *jw,
78                                 bool is_plain_text)
79 {
80         int left_shift_bits, right_shift_bits;
81         int bytes_to_copy;
82         int bits_to_copy;
83         __u64 print_num;
84
85         bits_to_copy = bit_offset + nr_bits;
86         bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
87
88         print_num = 0;
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;
94 #else
95 #error neither big nor little endian
96 #endif
97         right_shift_bits = 64 - nr_bits;
98
99         print_num <<= left_shift_bits;
100         print_num >>= right_shift_bits;
101         if (is_plain_text)
102                 jsonw_printf(jw, "0x%llx", print_num);
103         else
104                 jsonw_printf(jw, "%llu", print_num);
105 }
106
107
108 static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
109                                 const void *data, json_writer_t *jw,
110                                 bool is_plain_text)
111 {
112         int nr_bits = BTF_INT_BITS(int_type);
113         int total_bits_offset;
114
115         /* bits_offset is at most 7.
116          * BTF_INT_OFFSET() cannot exceed 64 bits.
117          */
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,
122                             is_plain_text);
123 }
124
125 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
126                           const void *data, json_writer_t *jw,
127                           bool is_plain_text)
128 {
129         __u32 *int_type;
130         __u32 nr_bits;
131
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,
138                                     is_plain_text);
139                 return 0;
140         }
141
142         switch (BTF_INT_ENCODING(*int_type)) {
143         case 0:
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);
152                 else
153                         btf_dumper_int_bits(*int_type, bit_offset, data, jw,
154                                             is_plain_text);
155                 break;
156         case BTF_INT_SIGNED:
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);
165                 else
166                         btf_dumper_int_bits(*int_type, bit_offset, data, jw,
167                                             is_plain_text);
168                 break;
169         case BTF_INT_CHAR:
170                 if (isprint(*(char *)data))
171                         jsonw_printf(jw, "\"%c\"", *(char *)data);
172                 else
173                         if (is_plain_text)
174                                 jsonw_printf(jw, "0x%hhx", *(char *)data);
175                         else
176                                 jsonw_printf(jw, "\"\\u00%02hhx\"",
177                                              *(char *)data);
178                 break;
179         case BTF_INT_BOOL:
180                 jsonw_bool(jw, *(int *)data);
181                 break;
182         default:
183                 /* shouldn't happen */
184                 return -EINVAL;
185         }
186
187         return 0;
188 }
189
190 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
191                              const void *data)
192 {
193         const struct btf_type *t;
194         struct btf_member *m;
195         const void *data_off;
196         int kind_flag;
197         int ret = 0;
198         int i, vlen;
199
200         t = btf__type_by_id(d->btf, type_id);
201         if (!t)
202                 return -EINVAL;
203
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);
208
209         for (i = 0; i < vlen; i++) {
210                 __u32 bit_offset = m[i].offset;
211                 __u32 bitfield_size = 0;
212
213                 if (kind_flag) {
214                         bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
215                         bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
216                 }
217
218                 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
219                 data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
220                 if (bitfield_size) {
221                         btf_dumper_bitfield(bitfield_size,
222                                             BITS_PER_BYTE_MASKED(bit_offset),
223                                             data_off, d->jw, d->is_plain_text);
224                 } else {
225                         ret = btf_dumper_do_type(d, m[i].type,
226                                                  BITS_PER_BYTE_MASKED(bit_offset),
227                                                  data_off);
228                         if (ret)
229                                 break;
230                 }
231         }
232
233         jsonw_end_object(d->jw);
234
235         return ret;
236 }
237
238 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
239                               __u8 bit_offset, const void *data)
240 {
241         const struct btf_type *t = btf__type_by_id(d->btf, type_id);
242
243         switch (BTF_INFO_KIND(t->info)) {
244         case BTF_KIND_INT:
245                 return btf_dumper_int(t, bit_offset, data, d->jw,
246                                      d->is_plain_text);
247         case BTF_KIND_STRUCT:
248         case BTF_KIND_UNION:
249                 return btf_dumper_struct(d, type_id, data);
250         case BTF_KIND_ARRAY:
251                 return btf_dumper_array(d, type_id, data);
252         case BTF_KIND_ENUM:
253                 btf_dumper_enum(data, d->jw);
254                 return 0;
255         case BTF_KIND_PTR:
256                 btf_dumper_ptr(data, d->jw, d->is_plain_text);
257                 return 0;
258         case BTF_KIND_UNKN:
259                 jsonw_printf(d->jw, "(unknown)");
260                 return 0;
261         case BTF_KIND_FWD:
262                 /* map key or value can't be forward */
263                 jsonw_printf(d->jw, "(fwd-kind-invalid)");
264                 return -EINVAL;
265         case BTF_KIND_TYPEDEF:
266         case BTF_KIND_VOLATILE:
267         case BTF_KIND_CONST:
268         case BTF_KIND_RESTRICT:
269                 return btf_dumper_modifier(d, type_id, bit_offset, data);
270         default:
271                 jsonw_printf(d->jw, "(unsupported-kind");
272                 return -EINVAL;
273         }
274 }
275
276 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
277                     const void *data)
278 {
279         return btf_dumper_do_type(d, type_id, 0, data);
280 }
281
282 #define BTF_PRINT_ARG(...)                                              \
283         do {                                                            \
284                 pos += snprintf(func_sig + pos, size - pos,             \
285                                 __VA_ARGS__);                           \
286                 if (pos >= size)                                        \
287                         return -1;                                      \
288         } while (0)
289 #define BTF_PRINT_TYPE(type)                                    \
290         do {                                                            \
291                 pos = __btf_dumper_type_only(btf, type, func_sig,       \
292                                              pos, size);                \
293                 if (pos == -1)                                          \
294                         return -1;                                      \
295         } while (0)
296
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);
300
301 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
302                                   char *func_sig, int pos, int size)
303 {
304         const struct btf_type *proto_type;
305         const struct btf_array *array;
306         const struct btf_type *t;
307
308         if (!type_id) {
309                 BTF_PRINT_ARG("void ");
310                 return pos;
311         }
312
313         t = btf__type_by_id(btf, type_id);
314
315         switch (BTF_INFO_KIND(t->info)) {
316         case BTF_KIND_INT:
317         case BTF_KIND_TYPEDEF:
318                 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
319                 break;
320         case BTF_KIND_STRUCT:
321                 BTF_PRINT_ARG("struct %s ",
322                               btf__name_by_offset(btf, t->name_off));
323                 break;
324         case BTF_KIND_UNION:
325                 BTF_PRINT_ARG("union %s ",
326                               btf__name_by_offset(btf, t->name_off));
327                 break;
328         case BTF_KIND_ENUM:
329                 BTF_PRINT_ARG("enum %s ",
330                               btf__name_by_offset(btf, t->name_off));
331                 break;
332         case BTF_KIND_ARRAY:
333                 array = (struct btf_array *)(t + 1);
334                 BTF_PRINT_TYPE(array->type);
335                 BTF_PRINT_ARG("[%d]", array->nelems);
336                 break;
337         case BTF_KIND_PTR:
338                 BTF_PRINT_TYPE(t->type);
339                 BTF_PRINT_ARG("* ");
340                 break;
341         case BTF_KIND_FWD:
342                 BTF_PRINT_ARG("%s %s ",
343                               BTF_INFO_KFLAG(t->info) ? "union" : "struct",
344                               btf__name_by_offset(btf, t->name_off));
345                 break;
346         case BTF_KIND_VOLATILE:
347                 BTF_PRINT_ARG("volatile ");
348                 BTF_PRINT_TYPE(t->type);
349                 break;
350         case BTF_KIND_CONST:
351                 BTF_PRINT_ARG("const ");
352                 BTF_PRINT_TYPE(t->type);
353                 break;
354         case BTF_KIND_RESTRICT:
355                 BTF_PRINT_ARG("restrict ");
356                 BTF_PRINT_TYPE(t->type);
357                 break;
358         case BTF_KIND_FUNC_PROTO:
359                 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
360                 if (pos == -1)
361                         return -1;
362                 break;
363         case BTF_KIND_FUNC:
364                 proto_type = btf__type_by_id(btf, t->type);
365                 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
366                 if (pos == -1)
367                         return -1;
368                 break;
369         case BTF_KIND_UNKN:
370         default:
371                 return -1;
372         }
373
374         return pos;
375 }
376
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)
380 {
381         int i, vlen;
382
383         BTF_PRINT_TYPE(func_proto->type);
384         if (func)
385                 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
386         else
387                 BTF_PRINT_ARG("(");
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];
391
392                 if (i)
393                         BTF_PRINT_ARG(", ");
394                 if (arg->type) {
395                         BTF_PRINT_TYPE(arg->type);
396                         BTF_PRINT_ARG("%s",
397                                       btf__name_by_offset(btf, arg->name_off));
398                 } else {
399                         BTF_PRINT_ARG("...");
400                 }
401         }
402         BTF_PRINT_ARG(")");
403
404         return pos;
405 }
406
407 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
408                           int size)
409 {
410         int err;
411
412         func_sig[0] = '\0';
413         if (!btf)
414                 return;
415
416         err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
417         if (err < 0)
418                 func_sig[0] = '\0';
419 }
420
421 static const char *ltrim(const char *s)
422 {
423         while (isspace(*s))
424                 s++;
425
426         return s;
427 }
428
429 void btf_dump_linfo_plain(const struct btf *btf,
430                           const struct bpf_line_info *linfo,
431                           const char *prefix, bool linum)
432 {
433         const char *line = btf__name_by_offset(btf, linfo->line_off);
434
435         if (!line)
436                 return;
437         line = ltrim(line);
438
439         if (!prefix)
440                 prefix = "";
441
442         if (linum) {
443                 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
444
445                 /* More forgiving on file because linum option is
446                  * expected to provide more info than the already
447                  * available src line.
448                  */
449                 if (!file)
450                         file = "";
451
452                 printf("%s%s [file:%s line_num:%u line_col:%u]\n",
453                        prefix, line, file,
454                        BPF_LINE_INFO_LINE_NUM(linfo->line_col),
455                        BPF_LINE_INFO_LINE_COL(linfo->line_col));
456         } else {
457                 printf("%s%s\n", prefix, line);
458         }
459 }
460
461 void btf_dump_linfo_json(const struct btf *btf,
462                          const struct bpf_line_info *linfo, bool linum)
463 {
464         const char *line = btf__name_by_offset(btf, linfo->line_off);
465
466         if (line)
467                 jsonw_string_field(json_wtr, "src", ltrim(line));
468
469         if (linum) {
470                 const char *file = btf__name_by_offset(btf, linfo->file_name_off);
471
472                 if (file)
473                         jsonw_string_field(json_wtr, "file", file);
474
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));
478
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));
482         }
483 }