Linux Audio

Check our new training course

Loading...
v6.13.7
  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 <unistd.h>
  8#include <asm/byteorder.h>
  9#include <linux/bitops.h>
 10#include <linux/btf.h>
 11#include <linux/err.h>
 12#include <bpf/btf.h>
 13#include <bpf/bpf.h>
 14
 
 15#include "json_writer.h"
 16#include "main.h"
 17
 18#define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
 19#define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
 20#define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 21#define BITS_ROUNDUP_BYTES(bits) \
 22	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
 23
 24static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
 25			      __u8 bit_offset, const void *data);
 26
 27static int btf_dump_func(const struct btf *btf, char *func_sig,
 28			 const struct btf_type *func_proto,
 29			 const struct btf_type *func, int pos, int size);
 30
 31static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,
 32				    const struct btf_type *func_proto,
 33				    __u32 prog_id)
 34{
 35	const struct btf_type *func_type;
 36	int prog_fd = -1, func_sig_len;
 37	struct bpf_prog_info info = {};
 38	__u32 info_len = sizeof(info);
 39	const char *prog_name = NULL;
 40	struct btf *prog_btf = NULL;
 41	struct bpf_func_info finfo;
 42	__u32 finfo_rec_size;
 43	char prog_str[1024];
 44	int err;
 45
 46	/* Get the ptr's func_proto */
 47	func_sig_len = btf_dump_func(d->btf, prog_str, func_proto, NULL, 0,
 48				     sizeof(prog_str));
 49	if (func_sig_len == -1)
 50		return -1;
 51
 52	if (!prog_id)
 53		goto print;
 54
 55	/* Get the bpf_prog's name.  Obtain from func_info. */
 56	prog_fd = bpf_prog_get_fd_by_id(prog_id);
 57	if (prog_fd < 0)
 58		goto print;
 59
 60	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
 61	if (err)
 62		goto print;
 63
 64	if (!info.btf_id || !info.nr_func_info)
 65		goto print;
 66
 67	finfo_rec_size = info.func_info_rec_size;
 68	memset(&info, 0, sizeof(info));
 69	info.nr_func_info = 1;
 70	info.func_info_rec_size = finfo_rec_size;
 71	info.func_info = ptr_to_u64(&finfo);
 72
 73	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
 74	if (err)
 75		goto print;
 76
 77	prog_btf = btf__load_from_kernel_by_id(info.btf_id);
 78	if (!prog_btf)
 79		goto print;
 80	func_type = btf__type_by_id(prog_btf, finfo.type_id);
 81	if (!func_type || !btf_is_func(func_type))
 82		goto print;
 83
 84	prog_name = btf__name_by_offset(prog_btf, func_type->name_off);
 85
 86print:
 87	if (!prog_id)
 88		snprintf(&prog_str[func_sig_len],
 89			 sizeof(prog_str) - func_sig_len, " 0");
 90	else if (prog_name)
 91		snprintf(&prog_str[func_sig_len],
 92			 sizeof(prog_str) - func_sig_len,
 93			 " %s/prog_id:%u", prog_name, prog_id);
 94	else
 95		snprintf(&prog_str[func_sig_len],
 96			 sizeof(prog_str) - func_sig_len,
 97			 " <unknown_prog_name>/prog_id:%u", prog_id);
 98
 99	prog_str[sizeof(prog_str) - 1] = '\0';
100	jsonw_string(d->jw, prog_str);
101	btf__free(prog_btf);
102	if (prog_fd >= 0)
103		close(prog_fd);
104	return 0;
105}
106
107static void btf_dumper_ptr(const struct btf_dumper *d,
108			   const struct btf_type *t,
109			   const void *data)
110{
111	unsigned long value = *(unsigned long *)data;
112	const struct btf_type *ptr_type;
113	__s32 ptr_type_id;
114
115	if (!d->prog_id_as_func_ptr || value > UINT32_MAX)
116		goto print_ptr_value;
117
118	ptr_type_id = btf__resolve_type(d->btf, t->type);
119	if (ptr_type_id < 0)
120		goto print_ptr_value;
121	ptr_type = btf__type_by_id(d->btf, ptr_type_id);
122	if (!ptr_type || !btf_is_func_proto(ptr_type))
123		goto print_ptr_value;
124
125	if (!dump_prog_id_as_func_ptr(d, ptr_type, value))
126		return;
127
128print_ptr_value:
129	if (d->is_plain_text)
130		jsonw_printf(d->jw, "\"%p\"", (void *)value);
131	else
132		jsonw_printf(d->jw, "%lu", value);
133}
134
135static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
136			       __u8 bit_offset, const void *data)
137{
138	int actual_type_id;
139
140	actual_type_id = btf__resolve_type(d->btf, type_id);
141	if (actual_type_id < 0)
142		return actual_type_id;
143
144	return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
145}
146
147static int btf_dumper_enum(const struct btf_dumper *d,
148			    const struct btf_type *t,
149			    const void *data)
150{
151	const struct btf_enum *enums = btf_enum(t);
152	__s64 value;
153	__u16 i;
154
155	switch (t->size) {
156	case 8:
157		value = *(__s64 *)data;
158		break;
159	case 4:
160		value = *(__s32 *)data;
161		break;
162	case 2:
163		value = *(__s16 *)data;
164		break;
165	case 1:
166		value = *(__s8 *)data;
167		break;
168	default:
169		return -EINVAL;
170	}
171
172	for (i = 0; i < btf_vlen(t); i++) {
173		if (value == enums[i].val) {
174			jsonw_string(d->jw,
175				     btf__name_by_offset(d->btf,
176							 enums[i].name_off));
177			return 0;
178		}
179	}
180
181	jsonw_int(d->jw, value);
182	return 0;
183}
184
185static int btf_dumper_enum64(const struct btf_dumper *d,
186			     const struct btf_type *t,
187			     const void *data)
188{
189	const struct btf_enum64 *enums = btf_enum64(t);
190	__u32 val_lo32, val_hi32;
191	__u64 value;
192	__u16 i;
193
194	value = *(__u64 *)data;
195	val_lo32 = (__u32)value;
196	val_hi32 = value >> 32;
197
198	for (i = 0; i < btf_vlen(t); i++) {
199		if (val_lo32 == enums[i].val_lo32 && val_hi32 == enums[i].val_hi32) {
200			jsonw_string(d->jw,
201				     btf__name_by_offset(d->btf,
202							 enums[i].name_off));
203			return 0;
204		}
205	}
206
207	jsonw_int(d->jw, value);
208	return 0;
209}
210
211static bool is_str_array(const struct btf *btf, const struct btf_array *arr,
212			 const char *s)
213{
214	const struct btf_type *elem_type;
215	const char *end_s;
216
217	if (!arr->nelems)
218		return false;
219
220	elem_type = btf__type_by_id(btf, arr->type);
221	/* Not skipping typedef.  typedef to char does not count as
222	 * a string now.
223	 */
224	while (elem_type && btf_is_mod(elem_type))
225		elem_type = btf__type_by_id(btf, elem_type->type);
226
227	if (!elem_type || !btf_is_int(elem_type) || elem_type->size != 1)
228		return false;
229
230	if (btf_int_encoding(elem_type) != BTF_INT_CHAR &&
231	    strcmp("char", btf__name_by_offset(btf, elem_type->name_off)))
232		return false;
233
234	end_s = s + arr->nelems;
235	while (s < end_s) {
236		if (!*s)
237			return true;
238		if (*s <= 0x1f || *s >= 0x7f)
239			return false;
240		s++;
241	}
242
243	/* '\0' is not found */
244	return false;
245}
246
247static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
248			    const void *data)
249{
250	const struct btf_type *t = btf__type_by_id(d->btf, type_id);
251	struct btf_array *arr = (struct btf_array *)(t + 1);
252	long long elem_size;
253	int ret = 0;
254	__u32 i;
255
256	if (is_str_array(d->btf, arr, data)) {
257		jsonw_string(d->jw, data);
258		return 0;
259	}
260
261	elem_size = btf__resolve_size(d->btf, arr->type);
262	if (elem_size < 0)
263		return elem_size;
264
265	jsonw_start_array(d->jw);
266	for (i = 0; i < arr->nelems; i++) {
267		ret = btf_dumper_do_type(d, arr->type, 0,
268					 data + i * elem_size);
269		if (ret)
270			break;
271	}
272
273	jsonw_end_array(d->jw);
274	return ret;
275}
276
277static void btf_int128_print(json_writer_t *jw, const void *data,
278			     bool is_plain_text)
279{
280	/* data points to a __int128 number.
281	 * Suppose
282	 *     int128_num = *(__int128 *)data;
283	 * The below formulas shows what upper_num and lower_num represents:
284	 *     upper_num = int128_num >> 64;
285	 *     lower_num = int128_num & 0xffffffffFFFFFFFFULL;
286	 */
287	__u64 upper_num, lower_num;
288
289#ifdef __BIG_ENDIAN_BITFIELD
290	upper_num = *(__u64 *)data;
291	lower_num = *(__u64 *)(data + 8);
292#else
293	upper_num = *(__u64 *)(data + 8);
294	lower_num = *(__u64 *)data;
295#endif
296
297	if (is_plain_text) {
298		if (upper_num == 0)
299			jsonw_printf(jw, "0x%llx", lower_num);
300		else
301			jsonw_printf(jw, "0x%llx%016llx", upper_num, lower_num);
302	} else {
303		if (upper_num == 0)
304			jsonw_printf(jw, "\"0x%llx\"", lower_num);
305		else
306			jsonw_printf(jw, "\"0x%llx%016llx\"", upper_num, lower_num);
307	}
308}
309
310static void btf_int128_shift(__u64 *print_num, __u16 left_shift_bits,
311			     __u16 right_shift_bits)
312{
313	__u64 upper_num, lower_num;
314
315#ifdef __BIG_ENDIAN_BITFIELD
316	upper_num = print_num[0];
317	lower_num = print_num[1];
318#else
319	upper_num = print_num[1];
320	lower_num = print_num[0];
321#endif
322
323	/* shake out un-needed bits by shift/or operations */
324	if (left_shift_bits >= 64) {
325		upper_num = lower_num << (left_shift_bits - 64);
326		lower_num = 0;
327	} else {
328		upper_num = (upper_num << left_shift_bits) |
329			    (lower_num >> (64 - left_shift_bits));
330		lower_num = lower_num << left_shift_bits;
331	}
332
333	if (right_shift_bits >= 64) {
334		lower_num = upper_num >> (right_shift_bits - 64);
335		upper_num = 0;
336	} else {
337		lower_num = (lower_num >> right_shift_bits) |
338			    (upper_num << (64 - right_shift_bits));
339		upper_num = upper_num >> right_shift_bits;
340	}
341
342#ifdef __BIG_ENDIAN_BITFIELD
343	print_num[0] = upper_num;
344	print_num[1] = lower_num;
345#else
346	print_num[0] = lower_num;
347	print_num[1] = upper_num;
348#endif
349}
350
351static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
352				const void *data, json_writer_t *jw,
353				bool is_plain_text)
354{
355	int left_shift_bits, right_shift_bits;
356	__u64 print_num[2] = {};
357	int bytes_to_copy;
358	int bits_to_copy;
359
360	bits_to_copy = bit_offset + nr_bits;
361	bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
362
363	memcpy(print_num, data, bytes_to_copy);
364#if defined(__BIG_ENDIAN_BITFIELD)
365	left_shift_bits = bit_offset;
366#elif defined(__LITTLE_ENDIAN_BITFIELD)
367	left_shift_bits = 128 - bits_to_copy;
368#else
369#error neither big nor little endian
370#endif
371	right_shift_bits = 128 - nr_bits;
372
373	btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
374	btf_int128_print(jw, print_num, is_plain_text);
375}
376
377
378static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
379				const void *data, json_writer_t *jw,
380				bool is_plain_text)
381{
382	int nr_bits = BTF_INT_BITS(int_type);
383	int total_bits_offset;
384
385	/* bits_offset is at most 7.
386	 * BTF_INT_OFFSET() cannot exceed 128 bits.
387	 */
388	total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type);
389	data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
390	bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
391	btf_dumper_bitfield(nr_bits, bit_offset, data, jw,
392			    is_plain_text);
393}
394
395static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
396			  const void *data, json_writer_t *jw,
397			  bool is_plain_text)
398{
399	__u32 *int_type;
400	__u32 nr_bits;
401
402	int_type = (__u32 *)(t + 1);
403	nr_bits = BTF_INT_BITS(*int_type);
404	/* if this is bit field */
405	if (bit_offset || BTF_INT_OFFSET(*int_type) ||
406	    BITS_PER_BYTE_MASKED(nr_bits)) {
407		btf_dumper_int_bits(*int_type, bit_offset, data, jw,
408				    is_plain_text);
409		return 0;
410	}
411
412	if (nr_bits == 128) {
413		btf_int128_print(jw, data, is_plain_text);
414		return 0;
415	}
416
417	switch (BTF_INT_ENCODING(*int_type)) {
418	case 0:
419		if (BTF_INT_BITS(*int_type) == 64)
420			jsonw_printf(jw, "%llu", *(__u64 *)data);
421		else if (BTF_INT_BITS(*int_type) == 32)
422			jsonw_printf(jw, "%u", *(__u32 *)data);
423		else if (BTF_INT_BITS(*int_type) == 16)
424			jsonw_printf(jw, "%hu", *(__u16 *)data);
425		else if (BTF_INT_BITS(*int_type) == 8)
426			jsonw_printf(jw, "%hhu", *(__u8 *)data);
427		else
428			btf_dumper_int_bits(*int_type, bit_offset, data, jw,
429					    is_plain_text);
430		break;
431	case BTF_INT_SIGNED:
432		if (BTF_INT_BITS(*int_type) == 64)
433			jsonw_printf(jw, "%lld", *(long long *)data);
434		else if (BTF_INT_BITS(*int_type) == 32)
435			jsonw_printf(jw, "%d", *(int *)data);
436		else if (BTF_INT_BITS(*int_type) == 16)
437			jsonw_printf(jw, "%hd", *(short *)data);
438		else if (BTF_INT_BITS(*int_type) == 8)
439			jsonw_printf(jw, "%hhd", *(char *)data);
440		else
441			btf_dumper_int_bits(*int_type, bit_offset, data, jw,
442					    is_plain_text);
443		break;
444	case BTF_INT_CHAR:
445		if (isprint(*(char *)data))
446			jsonw_printf(jw, "\"%c\"", *(char *)data);
447		else
448			if (is_plain_text)
449				jsonw_printf(jw, "0x%hhx", *(char *)data);
450			else
451				jsonw_printf(jw, "\"\\u00%02hhx\"",
452					     *(char *)data);
453		break;
454	case BTF_INT_BOOL:
455		jsonw_bool(jw, *(bool *)data);
456		break;
457	default:
458		/* shouldn't happen */
459		return -EINVAL;
460	}
461
462	return 0;
463}
464
465static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
466			     const void *data)
467{
468	const struct btf_type *t;
469	struct btf_member *m;
470	const void *data_off;
471	int kind_flag;
472	int ret = 0;
473	int i, vlen;
474
475	t = btf__type_by_id(d->btf, type_id);
476	if (!t)
477		return -EINVAL;
478
479	kind_flag = BTF_INFO_KFLAG(t->info);
480	vlen = BTF_INFO_VLEN(t->info);
481	jsonw_start_object(d->jw);
482	m = (struct btf_member *)(t + 1);
483
484	for (i = 0; i < vlen; i++) {
485		__u32 bit_offset = m[i].offset;
486		__u32 bitfield_size = 0;
487
488		if (kind_flag) {
489			bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
490			bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
491		}
492
493		jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
494		data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
495		if (bitfield_size) {
496			btf_dumper_bitfield(bitfield_size,
497					    BITS_PER_BYTE_MASKED(bit_offset),
498					    data_off, d->jw, d->is_plain_text);
499		} else {
500			ret = btf_dumper_do_type(d, m[i].type,
501						 BITS_PER_BYTE_MASKED(bit_offset),
502						 data_off);
503			if (ret)
504				break;
505		}
506	}
507
508	jsonw_end_object(d->jw);
509
510	return ret;
511}
512
513static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
514			  __u8 bit_offset, const void *data)
515{
516	const struct btf_type *t = btf__type_by_id(d->btf, type_id);
517	int ret;
518
519	jsonw_start_object(d->jw);
520	jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
521	ret = btf_dumper_do_type(d, t->type, bit_offset, data);
522	jsonw_end_object(d->jw);
523
524	return ret;
525}
526
527static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
528			      const void *data)
529{
530	struct btf_var_secinfo *vsi;
531	const struct btf_type *t;
532	int ret = 0, i, vlen;
533
534	t = btf__type_by_id(d->btf, type_id);
535	if (!t)
536		return -EINVAL;
537
538	vlen = BTF_INFO_VLEN(t->info);
539	vsi = (struct btf_var_secinfo *)(t + 1);
540
541	jsonw_start_object(d->jw);
542	jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
543	jsonw_start_array(d->jw);
544	for (i = 0; i < vlen; i++) {
545		ret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);
546		if (ret)
547			break;
548	}
549	jsonw_end_array(d->jw);
550	jsonw_end_object(d->jw);
551
552	return ret;
553}
554
555static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
556			      __u8 bit_offset, const void *data)
557{
558	const struct btf_type *t = btf__type_by_id(d->btf, type_id);
559
560	switch (BTF_INFO_KIND(t->info)) {
561	case BTF_KIND_INT:
562		return btf_dumper_int(t, bit_offset, data, d->jw,
563				     d->is_plain_text);
564	case BTF_KIND_STRUCT:
565	case BTF_KIND_UNION:
566		return btf_dumper_struct(d, type_id, data);
567	case BTF_KIND_ARRAY:
568		return btf_dumper_array(d, type_id, data);
569	case BTF_KIND_ENUM:
570		return btf_dumper_enum(d, t, data);
571	case BTF_KIND_ENUM64:
572		return btf_dumper_enum64(d, t, data);
573	case BTF_KIND_PTR:
574		btf_dumper_ptr(d, t, data);
575		return 0;
576	case BTF_KIND_UNKN:
577		jsonw_printf(d->jw, "(unknown)");
578		return 0;
579	case BTF_KIND_FWD:
580		/* map key or value can't be forward */
581		jsonw_printf(d->jw, "(fwd-kind-invalid)");
582		return -EINVAL;
583	case BTF_KIND_TYPEDEF:
584	case BTF_KIND_VOLATILE:
585	case BTF_KIND_CONST:
586	case BTF_KIND_RESTRICT:
587		return btf_dumper_modifier(d, type_id, bit_offset, data);
588	case BTF_KIND_VAR:
589		return btf_dumper_var(d, type_id, bit_offset, data);
590	case BTF_KIND_DATASEC:
591		return btf_dumper_datasec(d, type_id, data);
592	default:
593		jsonw_printf(d->jw, "(unsupported-kind");
594		return -EINVAL;
595	}
596}
597
598int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
599		    const void *data)
600{
601	return btf_dumper_do_type(d, type_id, 0, data);
602}
603
604#define BTF_PRINT_ARG(...)						\
605	do {								\
606		pos += snprintf(func_sig + pos, size - pos,		\
607				__VA_ARGS__);				\
608		if (pos >= size)					\
609			return -1;					\
610	} while (0)
611#define BTF_PRINT_TYPE(type)					\
612	do {								\
613		pos = __btf_dumper_type_only(btf, type, func_sig,	\
614					     pos, size);		\
615		if (pos == -1)						\
616			return -1;					\
617	} while (0)
618
 
 
 
 
619static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
620				  char *func_sig, int pos, int size)
621{
622	const struct btf_type *proto_type;
623	const struct btf_array *array;
624	const struct btf_var *var;
625	const struct btf_type *t;
626
627	if (!type_id) {
628		BTF_PRINT_ARG("void ");
629		return pos;
630	}
631
632	t = btf__type_by_id(btf, type_id);
633
634	switch (BTF_INFO_KIND(t->info)) {
635	case BTF_KIND_INT:
636	case BTF_KIND_TYPEDEF:
637	case BTF_KIND_FLOAT:
638		BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
639		break;
640	case BTF_KIND_STRUCT:
641		BTF_PRINT_ARG("struct %s ",
642			      btf__name_by_offset(btf, t->name_off));
643		break;
644	case BTF_KIND_UNION:
645		BTF_PRINT_ARG("union %s ",
646			      btf__name_by_offset(btf, t->name_off));
647		break;
648	case BTF_KIND_ENUM:
649	case BTF_KIND_ENUM64:
650		BTF_PRINT_ARG("enum %s ",
651			      btf__name_by_offset(btf, t->name_off));
652		break;
653	case BTF_KIND_ARRAY:
654		array = (struct btf_array *)(t + 1);
655		BTF_PRINT_TYPE(array->type);
656		BTF_PRINT_ARG("[%d]", array->nelems);
657		break;
658	case BTF_KIND_PTR:
659		BTF_PRINT_TYPE(t->type);
660		BTF_PRINT_ARG("* ");
661		break;
662	case BTF_KIND_FWD:
663		BTF_PRINT_ARG("%s %s ",
664			      BTF_INFO_KFLAG(t->info) ? "union" : "struct",
665			      btf__name_by_offset(btf, t->name_off));
666		break;
667	case BTF_KIND_VOLATILE:
668		BTF_PRINT_ARG("volatile ");
669		BTF_PRINT_TYPE(t->type);
670		break;
671	case BTF_KIND_CONST:
672		BTF_PRINT_ARG("const ");
673		BTF_PRINT_TYPE(t->type);
674		break;
675	case BTF_KIND_RESTRICT:
676		BTF_PRINT_ARG("restrict ");
677		BTF_PRINT_TYPE(t->type);
678		break;
679	case BTF_KIND_FUNC_PROTO:
680		pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
681		if (pos == -1)
682			return -1;
683		break;
684	case BTF_KIND_FUNC:
685		proto_type = btf__type_by_id(btf, t->type);
686		pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
687		if (pos == -1)
688			return -1;
689		break;
690	case BTF_KIND_VAR:
691		var = (struct btf_var *)(t + 1);
692		if (var->linkage == BTF_VAR_STATIC)
693			BTF_PRINT_ARG("static ");
694		BTF_PRINT_TYPE(t->type);
695		BTF_PRINT_ARG(" %s",
696			      btf__name_by_offset(btf, t->name_off));
697		break;
698	case BTF_KIND_DATASEC:
699		BTF_PRINT_ARG("section (\"%s\") ",
700			      btf__name_by_offset(btf, t->name_off));
701		break;
702	case BTF_KIND_UNKN:
703	default:
704		return -1;
705	}
706
707	return pos;
708}
709
710static int btf_dump_func(const struct btf *btf, char *func_sig,
711			 const struct btf_type *func_proto,
712			 const struct btf_type *func, int pos, int size)
713{
714	int i, vlen;
715
716	BTF_PRINT_TYPE(func_proto->type);
717	if (func)
718		BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
719	else
720		BTF_PRINT_ARG("(");
721	vlen = BTF_INFO_VLEN(func_proto->info);
722	for (i = 0; i < vlen; i++) {
723		struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i];
724
725		if (i)
726			BTF_PRINT_ARG(", ");
727		if (arg->type) {
728			BTF_PRINT_TYPE(arg->type);
729			if (arg->name_off)
730				BTF_PRINT_ARG("%s",
731					      btf__name_by_offset(btf, arg->name_off));
732			else if (pos && func_sig[pos - 1] == ' ')
733				/* Remove unnecessary space for
734				 * FUNC_PROTO that does not have
735				 * arg->name_off
736				 */
737				func_sig[--pos] = '\0';
738		} else {
739			BTF_PRINT_ARG("...");
740		}
741	}
742	BTF_PRINT_ARG(")");
743
744	return pos;
745}
746
747void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
748			  int size)
749{
750	int err;
751
752	func_sig[0] = '\0';
753	if (!btf)
754		return;
755
756	err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
757	if (err < 0)
758		func_sig[0] = '\0';
759}
760
761static const char *ltrim(const char *s)
762{
763	while (isspace(*s))
764		s++;
765
766	return s;
767}
768
769void btf_dump_linfo_plain(const struct btf *btf,
770			  const struct bpf_line_info *linfo,
771			  const char *prefix, bool linum)
772{
773	const char *line = btf__name_by_offset(btf, linfo->line_off);
774
775	if (!line)
776		return;
777	line = ltrim(line);
778
779	if (!prefix)
780		prefix = "";
781
782	if (linum) {
783		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
784
785		/* More forgiving on file because linum option is
786		 * expected to provide more info than the already
787		 * available src line.
788		 */
789		if (!file)
790			file = "";
791
792		printf("%s%s [file:%s line_num:%u line_col:%u]\n",
793		       prefix, line, file,
794		       BPF_LINE_INFO_LINE_NUM(linfo->line_col),
795		       BPF_LINE_INFO_LINE_COL(linfo->line_col));
796	} else {
797		printf("%s%s\n", prefix, line);
798	}
799}
800
801void btf_dump_linfo_json(const struct btf *btf,
802			 const struct bpf_line_info *linfo, bool linum)
803{
804	const char *line = btf__name_by_offset(btf, linfo->line_off);
805
806	if (line)
807		jsonw_string_field(json_wtr, "src", ltrim(line));
808
809	if (linum) {
810		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
811
812		if (file)
813			jsonw_string_field(json_wtr, "file", file);
814
815		if (BPF_LINE_INFO_LINE_NUM(linfo->line_col))
816			jsonw_int_field(json_wtr, "line_num",
817					BPF_LINE_INFO_LINE_NUM(linfo->line_col));
818
819		if (BPF_LINE_INFO_LINE_COL(linfo->line_col))
820			jsonw_int_field(json_wtr, "line_col",
821					BPF_LINE_INFO_LINE_COL(linfo->line_col));
822	}
823}
824
825static void dotlabel_puts(const char *s)
826{
827	for (; *s; ++s) {
828		switch (*s) {
829		case '\\':
830		case '"':
831		case '{':
832		case '}':
833		case '<':
834		case '>':
835		case '|':
836		case ' ':
837			putchar('\\');
838			fallthrough;
839		default:
840			putchar(*s);
841		}
842	}
843}
844
845static const char *shorten_path(const char *path)
846{
847	const unsigned int MAX_PATH_LEN = 32;
848	size_t len = strlen(path);
849	const char *shortpath;
850
851	if (len <= MAX_PATH_LEN)
852		return path;
853
854	/* Search for last '/' under the MAX_PATH_LEN limit */
855	shortpath = strchr(path + len - MAX_PATH_LEN, '/');
856	if (shortpath) {
857		if (shortpath < path + strlen("..."))
858			/* We removed a very short prefix, e.g. "/w", and we'll
859			 * make the path longer by prefixing with the ellipsis.
860			 * Not worth it, keep initial path.
861			 */
862			return path;
863		return shortpath;
864	}
865
866	/* File base name length is > MAX_PATH_LEN, search for last '/' */
867			shortpath = strrchr(path, '/');
868	if (shortpath)
869		return shortpath;
870
871	return path;
872}
873
874void btf_dump_linfo_dotlabel(const struct btf *btf,
875			     const struct bpf_line_info *linfo, bool linum)
876{
877	const char *line = btf__name_by_offset(btf, linfo->line_off);
878
879	if (!line || !strlen(line))
880		return;
881	line = ltrim(line);
882
883	if (linum) {
884		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
885		const char *shortfile;
886
887		/* More forgiving on file because linum option is
888		 * expected to provide more info than the already
889		 * available src line.
890		 */
891		if (!file)
892			shortfile = "";
893		else
894			shortfile = shorten_path(file);
895
896		printf("; [%s", shortfile > file ? "..." : "");
897		dotlabel_puts(shortfile);
898		printf(" line:%u col:%u]\\l\\\n",
899		       BPF_LINE_INFO_LINE_NUM(linfo->line_col),
900		       BPF_LINE_INFO_LINE_COL(linfo->line_col));
901	}
902
903	printf("; ");
904	dotlabel_puts(line);
905	printf("\\l\\\n");
906}
v5.4
  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
 22static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
 23			      __u8 bit_offset, const void *data);
 24
 25static 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", data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 30	else
 31		jsonw_printf(jw, "%lu", *(unsigned long *)data);
 32}
 33
 34static 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
 46static void btf_dumper_enum(const void *data, json_writer_t *jw)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 47{
 48	jsonw_printf(jw, "%d", *(int *)data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 49}
 50
 51static 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
 76static void btf_int128_print(json_writer_t *jw, const void *data,
 77			     bool is_plain_text)
 78{
 79	/* data points to a __int128 number.
 80	 * Suppose
 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;
 85	 */
 86	__u64 upper_num, lower_num;
 87
 88#ifdef __BIG_ENDIAN_BITFIELD
 89	upper_num = *(__u64 *)data;
 90	lower_num = *(__u64 *)(data + 8);
 91#else
 92	upper_num = *(__u64 *)(data + 8);
 93	lower_num = *(__u64 *)data;
 94#endif
 95
 96	if (is_plain_text) {
 97		if (upper_num == 0)
 98			jsonw_printf(jw, "0x%llx", lower_num);
 99		else
100			jsonw_printf(jw, "0x%llx%016llx", upper_num, lower_num);
101	} else {
102		if (upper_num == 0)
103			jsonw_printf(jw, "\"0x%llx\"", lower_num);
104		else
105			jsonw_printf(jw, "\"0x%llx%016llx\"", upper_num, lower_num);
106	}
107}
108
109static void btf_int128_shift(__u64 *print_num, u16 left_shift_bits,
110			     u16 right_shift_bits)
111{
112	__u64 upper_num, lower_num;
113
114#ifdef __BIG_ENDIAN_BITFIELD
115	upper_num = print_num[0];
116	lower_num = print_num[1];
117#else
118	upper_num = print_num[1];
119	lower_num = print_num[0];
120#endif
121
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);
125		lower_num = 0;
126	} else {
127		upper_num = (upper_num << left_shift_bits) |
128			    (lower_num >> (64 - left_shift_bits));
129		lower_num = lower_num << left_shift_bits;
130	}
131
132	if (right_shift_bits >= 64) {
133		lower_num = upper_num >> (right_shift_bits - 64);
134		upper_num = 0;
135	} else {
136		lower_num = (lower_num >> right_shift_bits) |
137			    (upper_num << (64 - right_shift_bits));
138		upper_num = upper_num >> right_shift_bits;
139	}
140
141#ifdef __BIG_ENDIAN_BITFIELD
142	print_num[0] = upper_num;
143	print_num[1] = lower_num;
144#else
145	print_num[0] = lower_num;
146	print_num[1] = upper_num;
147#endif
148}
149
150static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset,
151				const void *data, json_writer_t *jw,
152				bool is_plain_text)
153{
154	int left_shift_bits, right_shift_bits;
155	__u64 print_num[2] = {};
156	int bytes_to_copy;
157	int bits_to_copy;
158
159	bits_to_copy = bit_offset + nr_bits;
160	bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy);
161
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;
167#else
168#error neither big nor little endian
169#endif
170	right_shift_bits = 128 - nr_bits;
171
172	btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
173	btf_int128_print(jw, print_num, is_plain_text);
174}
175
176
177static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset,
178				const void *data, json_writer_t *jw,
179				bool is_plain_text)
180{
181	int nr_bits = BTF_INT_BITS(int_type);
182	int total_bits_offset;
183
184	/* bits_offset is at most 7.
185	 * BTF_INT_OFFSET() cannot exceed 128 bits.
186	 */
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,
191			    is_plain_text);
192}
193
194static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
195			  const void *data, json_writer_t *jw,
196			  bool is_plain_text)
197{
198	__u32 *int_type;
199	__u32 nr_bits;
200
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,
207				    is_plain_text);
208		return 0;
209	}
210
211	if (nr_bits == 128) {
212		btf_int128_print(jw, data, is_plain_text);
213		return 0;
214	}
215
216	switch (BTF_INT_ENCODING(*int_type)) {
217	case 0:
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);
226		else
227			btf_dumper_int_bits(*int_type, bit_offset, data, jw,
228					    is_plain_text);
229		break;
230	case BTF_INT_SIGNED:
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);
239		else
240			btf_dumper_int_bits(*int_type, bit_offset, data, jw,
241					    is_plain_text);
242		break;
243	case BTF_INT_CHAR:
244		if (isprint(*(char *)data))
245			jsonw_printf(jw, "\"%c\"", *(char *)data);
246		else
247			if (is_plain_text)
248				jsonw_printf(jw, "0x%hhx", *(char *)data);
249			else
250				jsonw_printf(jw, "\"\\u00%02hhx\"",
251					     *(char *)data);
252		break;
253	case BTF_INT_BOOL:
254		jsonw_bool(jw, *(int *)data);
255		break;
256	default:
257		/* shouldn't happen */
258		return -EINVAL;
259	}
260
261	return 0;
262}
263
264static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id,
265			     const void *data)
266{
267	const struct btf_type *t;
268	struct btf_member *m;
269	const void *data_off;
270	int kind_flag;
271	int ret = 0;
272	int i, vlen;
273
274	t = btf__type_by_id(d->btf, type_id);
275	if (!t)
276		return -EINVAL;
277
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);
282
283	for (i = 0; i < vlen; i++) {
284		__u32 bit_offset = m[i].offset;
285		__u32 bitfield_size = 0;
286
287		if (kind_flag) {
288			bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset);
289			bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset);
290		}
291
292		jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off));
293		data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset);
294		if (bitfield_size) {
295			btf_dumper_bitfield(bitfield_size,
296					    BITS_PER_BYTE_MASKED(bit_offset),
297					    data_off, d->jw, d->is_plain_text);
298		} else {
299			ret = btf_dumper_do_type(d, m[i].type,
300						 BITS_PER_BYTE_MASKED(bit_offset),
301						 data_off);
302			if (ret)
303				break;
304		}
305	}
306
307	jsonw_end_object(d->jw);
308
309	return ret;
310}
311
312static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
313			  __u8 bit_offset, const void *data)
314{
315	const struct btf_type *t = btf__type_by_id(d->btf, type_id);
316	int ret;
317
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);
322
323	return ret;
324}
325
326static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
327			      const void *data)
328{
329	struct btf_var_secinfo *vsi;
330	const struct btf_type *t;
331	int ret = 0, i, vlen;
332
333	t = btf__type_by_id(d->btf, type_id);
334	if (!t)
335		return -EINVAL;
336
337	vlen = BTF_INFO_VLEN(t->info);
338	vsi = (struct btf_var_secinfo *)(t + 1);
339
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);
345		if (ret)
346			break;
347	}
348	jsonw_end_array(d->jw);
349	jsonw_end_object(d->jw);
350
351	return ret;
352}
353
354static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
355			      __u8 bit_offset, const void *data)
356{
357	const struct btf_type *t = btf__type_by_id(d->btf, type_id);
358
359	switch (BTF_INFO_KIND(t->info)) {
360	case BTF_KIND_INT:
361		return btf_dumper_int(t, bit_offset, data, d->jw,
362				     d->is_plain_text);
363	case BTF_KIND_STRUCT:
364	case BTF_KIND_UNION:
365		return btf_dumper_struct(d, type_id, data);
366	case BTF_KIND_ARRAY:
367		return btf_dumper_array(d, type_id, data);
368	case BTF_KIND_ENUM:
369		btf_dumper_enum(data, d->jw);
370		return 0;
 
371	case BTF_KIND_PTR:
372		btf_dumper_ptr(data, d->jw, d->is_plain_text);
373		return 0;
374	case BTF_KIND_UNKN:
375		jsonw_printf(d->jw, "(unknown)");
376		return 0;
377	case BTF_KIND_FWD:
378		/* map key or value can't be forward */
379		jsonw_printf(d->jw, "(fwd-kind-invalid)");
380		return -EINVAL;
381	case BTF_KIND_TYPEDEF:
382	case BTF_KIND_VOLATILE:
383	case BTF_KIND_CONST:
384	case BTF_KIND_RESTRICT:
385		return btf_dumper_modifier(d, type_id, bit_offset, data);
386	case BTF_KIND_VAR:
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);
390	default:
391		jsonw_printf(d->jw, "(unsupported-kind");
392		return -EINVAL;
393	}
394}
395
396int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
397		    const void *data)
398{
399	return btf_dumper_do_type(d, type_id, 0, data);
400}
401
402#define BTF_PRINT_ARG(...)						\
403	do {								\
404		pos += snprintf(func_sig + pos, size - pos,		\
405				__VA_ARGS__);				\
406		if (pos >= size)					\
407			return -1;					\
408	} while (0)
409#define BTF_PRINT_TYPE(type)					\
410	do {								\
411		pos = __btf_dumper_type_only(btf, type, func_sig,	\
412					     pos, size);		\
413		if (pos == -1)						\
414			return -1;					\
415	} while (0)
416
417static 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);
420
421static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
422				  char *func_sig, int pos, int size)
423{
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;
428
429	if (!type_id) {
430		BTF_PRINT_ARG("void ");
431		return pos;
432	}
433
434	t = btf__type_by_id(btf, type_id);
435
436	switch (BTF_INFO_KIND(t->info)) {
437	case BTF_KIND_INT:
438	case BTF_KIND_TYPEDEF:
 
439		BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
440		break;
441	case BTF_KIND_STRUCT:
442		BTF_PRINT_ARG("struct %s ",
443			      btf__name_by_offset(btf, t->name_off));
444		break;
445	case BTF_KIND_UNION:
446		BTF_PRINT_ARG("union %s ",
447			      btf__name_by_offset(btf, t->name_off));
448		break;
449	case BTF_KIND_ENUM:
 
450		BTF_PRINT_ARG("enum %s ",
451			      btf__name_by_offset(btf, t->name_off));
452		break;
453	case BTF_KIND_ARRAY:
454		array = (struct btf_array *)(t + 1);
455		BTF_PRINT_TYPE(array->type);
456		BTF_PRINT_ARG("[%d]", array->nelems);
457		break;
458	case BTF_KIND_PTR:
459		BTF_PRINT_TYPE(t->type);
460		BTF_PRINT_ARG("* ");
461		break;
462	case BTF_KIND_FWD:
463		BTF_PRINT_ARG("%s %s ",
464			      BTF_INFO_KFLAG(t->info) ? "union" : "struct",
465			      btf__name_by_offset(btf, t->name_off));
466		break;
467	case BTF_KIND_VOLATILE:
468		BTF_PRINT_ARG("volatile ");
469		BTF_PRINT_TYPE(t->type);
470		break;
471	case BTF_KIND_CONST:
472		BTF_PRINT_ARG("const ");
473		BTF_PRINT_TYPE(t->type);
474		break;
475	case BTF_KIND_RESTRICT:
476		BTF_PRINT_ARG("restrict ");
477		BTF_PRINT_TYPE(t->type);
478		break;
479	case BTF_KIND_FUNC_PROTO:
480		pos = btf_dump_func(btf, func_sig, t, NULL, pos, size);
481		if (pos == -1)
482			return -1;
483		break;
484	case BTF_KIND_FUNC:
485		proto_type = btf__type_by_id(btf, t->type);
486		pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size);
487		if (pos == -1)
488			return -1;
489		break;
490	case BTF_KIND_VAR:
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);
495		BTF_PRINT_ARG(" %s",
496			      btf__name_by_offset(btf, t->name_off));
497		break;
498	case BTF_KIND_DATASEC:
499		BTF_PRINT_ARG("section (\"%s\") ",
500			      btf__name_by_offset(btf, t->name_off));
501		break;
502	case BTF_KIND_UNKN:
503	default:
504		return -1;
505	}
506
507	return pos;
508}
509
510static 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)
513{
514	int i, vlen;
515
516	BTF_PRINT_TYPE(func_proto->type);
517	if (func)
518		BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off));
519	else
520		BTF_PRINT_ARG("(");
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];
524
525		if (i)
526			BTF_PRINT_ARG(", ");
527		if (arg->type) {
528			BTF_PRINT_TYPE(arg->type);
529			BTF_PRINT_ARG("%s",
530				      btf__name_by_offset(btf, arg->name_off));
 
 
 
 
 
 
 
531		} else {
532			BTF_PRINT_ARG("...");
533		}
534	}
535	BTF_PRINT_ARG(")");
536
537	return pos;
538}
539
540void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig,
541			  int size)
542{
543	int err;
544
545	func_sig[0] = '\0';
546	if (!btf)
547		return;
548
549	err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size);
550	if (err < 0)
551		func_sig[0] = '\0';
552}
553
554static const char *ltrim(const char *s)
555{
556	while (isspace(*s))
557		s++;
558
559	return s;
560}
561
562void btf_dump_linfo_plain(const struct btf *btf,
563			  const struct bpf_line_info *linfo,
564			  const char *prefix, bool linum)
565{
566	const char *line = btf__name_by_offset(btf, linfo->line_off);
567
568	if (!line)
569		return;
570	line = ltrim(line);
571
572	if (!prefix)
573		prefix = "";
574
575	if (linum) {
576		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
577
578		/* More forgiving on file because linum option is
579		 * expected to provide more info than the already
580		 * available src line.
581		 */
582		if (!file)
583			file = "";
584
585		printf("%s%s [file:%s line_num:%u line_col:%u]\n",
586		       prefix, line, file,
587		       BPF_LINE_INFO_LINE_NUM(linfo->line_col),
588		       BPF_LINE_INFO_LINE_COL(linfo->line_col));
589	} else {
590		printf("%s%s\n", prefix, line);
591	}
592}
593
594void btf_dump_linfo_json(const struct btf *btf,
595			 const struct bpf_line_info *linfo, bool linum)
596{
597	const char *line = btf__name_by_offset(btf, linfo->line_off);
598
599	if (line)
600		jsonw_string_field(json_wtr, "src", ltrim(line));
601
602	if (linum) {
603		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
604
605		if (file)
606			jsonw_string_field(json_wtr, "file", file);
607
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));
611
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));
615	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
616}