Remove useless SIDE_ATTR_TYPE_POINTER
[libside.git] / src / tracer.c
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
6 #include <stdint.h>
7 #include <inttypes.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <stdbool.h>
11 #include <string.h>
12
13 #include <side/trace.h>
14
15 enum tracer_display_base {
16 TRACER_DISPLAY_BASE_2,
17 TRACER_DISPLAY_BASE_8,
18 TRACER_DISPLAY_BASE_10,
19 TRACER_DISPLAY_BASE_16,
20 };
21
22 static struct side_tracer_handle *tracer_handle;
23
24 static
25 void tracer_print_struct(const struct side_type *type_desc, const struct side_arg_vec *side_arg_vec);
26 static
27 void tracer_print_array(const struct side_type *type_desc, const struct side_arg_vec *side_arg_vec);
28 static
29 void tracer_print_vla(const struct side_type *type_desc, const struct side_arg_vec *side_arg_vec);
30 static
31 void tracer_print_vla_visitor(const struct side_type *type_desc, void *app_ctx);
32 static
33 void tracer_print_dynamic(const struct side_arg *dynamic_item);
34 static
35 uint32_t tracer_print_gather_bool_type(const struct side_type_gather *type_gather, const void *_ptr);
36 static
37 uint32_t tracer_print_gather_byte_type(const struct side_type_gather *type_gather, const void *_ptr);
38 static
39 uint32_t tracer_print_gather_integer_type(const struct side_type_gather *type_gather, const void *_ptr,
40 enum tracer_display_base default_base);
41 static
42 uint32_t tracer_print_gather_float_type(const struct side_type_gather *type_gather, const void *_ptr);
43 static
44 uint32_t tracer_print_gather_struct(const struct side_type_gather *type_gather, const void *_ptr);
45 static
46 uint32_t tracer_print_gather_array(const struct side_type_gather *type_gather, const void *_ptr);
47 static
48 uint32_t tracer_print_gather_vla(const struct side_type_gather *type_gather, const void *_ptr,
49 const void *_length_ptr);
50 static
51 void tracer_print_type(const struct side_type *type_desc, const struct side_arg *item);
52
53 static
54 int64_t get_attr_integer_value(const struct side_attr *attr)
55 {
56 int64_t val;
57
58 switch (attr->value.type) {
59 case SIDE_ATTR_TYPE_U8:
60 val = attr->value.u.integer_value.side_u8;
61 break;
62 case SIDE_ATTR_TYPE_U16:
63 val = attr->value.u.integer_value.side_u16;
64 break;
65 case SIDE_ATTR_TYPE_U32:
66 val = attr->value.u.integer_value.side_u32;
67 break;
68 case SIDE_ATTR_TYPE_U64:
69 val = attr->value.u.integer_value.side_u64;
70 break;
71 case SIDE_ATTR_TYPE_S8:
72 val = attr->value.u.integer_value.side_s8;
73 break;
74 case SIDE_ATTR_TYPE_S16:
75 val = attr->value.u.integer_value.side_s16;
76 break;
77 case SIDE_ATTR_TYPE_S32:
78 val = attr->value.u.integer_value.side_s32;
79 break;
80 case SIDE_ATTR_TYPE_S64:
81 val = attr->value.u.integer_value.side_s64;
82 break;
83 default:
84 fprintf(stderr, "Unexpected attribute type\n");
85 abort();
86 }
87 return val;
88 }
89
90 static
91 enum tracer_display_base get_attr_display_base(const struct side_attr *_attr, uint32_t nr_attr,
92 enum tracer_display_base default_base)
93 {
94 uint32_t i;
95
96 for (i = 0; i < nr_attr; i++) {
97 const struct side_attr *attr = &_attr[i];
98
99 if (!strcmp(attr->key, "std.integer.base")) {
100 int64_t val = get_attr_integer_value(attr);
101
102 switch (val) {
103 case 2:
104 return TRACER_DISPLAY_BASE_2;
105 case 8:
106 return TRACER_DISPLAY_BASE_8;
107 case 10:
108 return TRACER_DISPLAY_BASE_10;
109 case 16:
110 return TRACER_DISPLAY_BASE_16;
111 default:
112 fprintf(stderr, "Unexpected integer display base: %" PRId64 "\n", val);
113 abort();
114 }
115 }
116 }
117 return default_base; /* Default */
118 }
119
120 static
121 bool type_to_host_reverse_bo(const struct side_type *type_desc)
122 {
123 switch (type_desc->type) {
124 case SIDE_TYPE_U8:
125 case SIDE_TYPE_S8:
126 case SIDE_TYPE_BYTE:
127 return false;
128 case SIDE_TYPE_U16:
129 case SIDE_TYPE_U32:
130 case SIDE_TYPE_U64:
131 case SIDE_TYPE_S16:
132 case SIDE_TYPE_S32:
133 case SIDE_TYPE_S64:
134 case SIDE_TYPE_POINTER:
135 if (type_desc->u.side_integer.byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
136 return true;
137 else
138 return false;
139 break;
140 case SIDE_TYPE_FLOAT_BINARY16:
141 case SIDE_TYPE_FLOAT_BINARY32:
142 case SIDE_TYPE_FLOAT_BINARY64:
143 case SIDE_TYPE_FLOAT_BINARY128:
144 if (type_desc->u.side_float.byte_order != SIDE_TYPE_FLOAT_WORD_ORDER_HOST)
145 return true;
146 else
147 return false;
148 break;
149 default:
150 fprintf(stderr, "Unexpected type\n");
151 abort();
152 }
153 }
154
155 static
156 void tracer_print_attr_type(const char *separator, const struct side_attr *attr)
157 {
158 printf("{ key%s \"%s\", value%s ", separator, attr->key, separator);
159 switch (attr->value.type) {
160 case SIDE_ATTR_TYPE_BOOL:
161 printf("%s", attr->value.u.bool_value ? "true" : "false");
162 break;
163 case SIDE_ATTR_TYPE_U8:
164 printf("%" PRIu8, attr->value.u.integer_value.side_u8);
165 break;
166 case SIDE_ATTR_TYPE_U16:
167 printf("%" PRIu16, attr->value.u.integer_value.side_u16);
168 break;
169 case SIDE_ATTR_TYPE_U32:
170 printf("%" PRIu32, attr->value.u.integer_value.side_u32);
171 break;
172 case SIDE_ATTR_TYPE_U64:
173 printf("%" PRIu64, attr->value.u.integer_value.side_u64);
174 break;
175 case SIDE_ATTR_TYPE_S8:
176 printf("%" PRId8, attr->value.u.integer_value.side_s8);
177 break;
178 case SIDE_ATTR_TYPE_S16:
179 printf("%" PRId16, attr->value.u.integer_value.side_s16);
180 break;
181 case SIDE_ATTR_TYPE_S32:
182 printf("%" PRId32, attr->value.u.integer_value.side_s32);
183 break;
184 case SIDE_ATTR_TYPE_S64:
185 printf("%" PRId64, attr->value.u.integer_value.side_s64);
186 break;
187 case SIDE_ATTR_TYPE_FLOAT_BINARY16:
188 #if __HAVE_FLOAT16
189 printf("%g", (double) attr->value.u.float_value.side_float_binary16);
190 break;
191 #else
192 fprintf(stderr, "ERROR: Unsupported binary16 float type\n");
193 abort();
194 #endif
195 case SIDE_ATTR_TYPE_FLOAT_BINARY32:
196 #if __HAVE_FLOAT32
197 printf("%g", (double) attr->value.u.float_value.side_float_binary32);
198 break;
199 #else
200 fprintf(stderr, "ERROR: Unsupported binary32 float type\n");
201 abort();
202 #endif
203 case SIDE_ATTR_TYPE_FLOAT_BINARY64:
204 #if __HAVE_FLOAT64
205 printf("%g", (double) attr->value.u.float_value.side_float_binary64);
206 break;
207 #else
208 fprintf(stderr, "ERROR: Unsupported binary64 float type\n");
209 abort();
210 #endif
211 case SIDE_ATTR_TYPE_FLOAT_BINARY128:
212 #if __HAVE_FLOAT128
213 printf("%Lg", (long double) attr->value.u.float_value.side_float_binary128);
214 break;
215 #else
216 fprintf(stderr, "ERROR: Unsupported binary128 float type\n");
217 abort();
218 #endif
219 case SIDE_ATTR_TYPE_STRING:
220 printf("\"%s\"", (const char *)(uintptr_t) attr->value.u.string_value);
221 break;
222 default:
223 fprintf(stderr, "ERROR: <UNKNOWN ATTRIBUTE TYPE>");
224 abort();
225 }
226 printf(" }");
227 }
228
229 static
230 void print_attributes(const char *prefix_str, const char *separator,
231 const struct side_attr *attr, uint32_t nr_attr)
232 {
233 uint32_t i;
234
235 if (!nr_attr)
236 return;
237 printf("%s%s [ ", prefix_str, separator);
238 for (i = 0; i < nr_attr; i++) {
239 printf("%s", i ? ", " : "");
240 tracer_print_attr_type(separator, &attr[i]);
241 }
242 printf(" ]");
243 }
244
245 static
246 void print_enum(const struct side_type *type_desc, const struct side_arg *item)
247 {
248 const struct side_enum_mappings *mappings = type_desc->u.side_enum.mappings;
249 const struct side_type *elem_type = type_desc->u.side_enum.elem_type;
250 uint32_t i, print_count = 0;
251 int64_t value;
252
253 if (elem_type->type != item->type) {
254 fprintf(stderr, "ERROR: Unexpected enum element type\n");
255 abort();
256 }
257 switch (item->type) {
258 case SIDE_TYPE_U8:
259 value = (int64_t) item->u.side_static.integer_value.side_u8;
260 break;
261 case SIDE_TYPE_U16:
262 {
263 uint16_t v;
264
265 v = item->u.side_static.integer_value.side_u16;
266 if (type_to_host_reverse_bo(elem_type))
267 v = side_bswap_16(v);
268 value = (int64_t) v;
269 break;
270 }
271 case SIDE_TYPE_U32:
272 {
273 uint32_t v;
274
275 v = item->u.side_static.integer_value.side_u32;
276 if (type_to_host_reverse_bo(elem_type))
277 v = side_bswap_32(v);
278 value = (int64_t) v;
279 break;
280 }
281 case SIDE_TYPE_U64:
282 {
283 uint64_t v;
284
285 v = item->u.side_static.integer_value.side_u64;
286 if (type_to_host_reverse_bo(elem_type))
287 v = side_bswap_64(v);
288 value = (int64_t) v;
289 break;
290 }
291 case SIDE_TYPE_S8:
292 value = (int64_t) item->u.side_static.integer_value.side_s8;
293 break;
294 case SIDE_TYPE_S16:
295 {
296 int16_t v;
297
298 v = item->u.side_static.integer_value.side_s16;
299 if (type_to_host_reverse_bo(elem_type))
300 v = side_bswap_16(v);
301 value = (int64_t) v;
302 break;
303 }
304 case SIDE_TYPE_S32:
305 {
306 int32_t v;
307
308 v = item->u.side_static.integer_value.side_s32;
309 if (type_to_host_reverse_bo(elem_type))
310 v = side_bswap_32(v);
311 value = (int64_t) v;
312 break;
313 }
314 case SIDE_TYPE_S64:
315 {
316 int64_t v;
317
318 v = item->u.side_static.integer_value.side_s64;
319 if (type_to_host_reverse_bo(elem_type))
320 v = side_bswap_64(v);
321 value = v;
322 break;
323 }
324 default:
325 fprintf(stderr, "ERROR: Unexpected enum element type\n");
326 abort();
327 }
328 print_attributes("attr", ":", mappings->attr, mappings->nr_attr);
329 printf("%s", mappings->nr_attr ? ", " : "");
330 tracer_print_type(type_desc->u.side_enum.elem_type, item);
331 printf(", labels: [ ");
332 for (i = 0; i < mappings->nr_mappings; i++) {
333 const struct side_enum_mapping *mapping = &mappings->mappings[i];
334
335 if (mapping->range_end < mapping->range_begin) {
336 fprintf(stderr, "ERROR: Unexpected enum range: %" PRIu64 "-%" PRIu64 "\n",
337 mapping->range_begin, mapping->range_end);
338 abort();
339 }
340 if (value >= mapping->range_begin && value <= mapping->range_end) {
341 printf("%s", print_count++ ? ", " : "");
342 printf("\"%s\"", mapping->label);
343 }
344 }
345 if (!print_count)
346 printf("<NO LABEL>");
347 printf(" ]");
348 }
349
350 static
351 uint32_t enum_elem_type_to_stride(const struct side_type *elem_type)
352 {
353 uint32_t stride_bit;
354
355 switch (elem_type->type) {
356 case SIDE_TYPE_U8: /* Fall-through */
357 case SIDE_TYPE_BYTE:
358 stride_bit = 8;
359 break;
360 case SIDE_TYPE_U16:
361 stride_bit = 16;
362 break;
363 case SIDE_TYPE_U32:
364 stride_bit = 32;
365 break;
366 case SIDE_TYPE_U64:
367 stride_bit = 64;
368 break;
369 default:
370 fprintf(stderr, "ERROR: Unexpected enum element type\n");
371 abort();
372 }
373 return stride_bit;
374 }
375
376 static
377 void print_enum_bitmap(const struct side_type *type_desc,
378 const struct side_arg *item)
379 {
380 const struct side_type *elem_type = type_desc->u.side_enum_bitmap.elem_type;
381 const struct side_enum_bitmap_mappings *side_enum_mappings = type_desc->u.side_enum_bitmap.mappings;
382 uint32_t i, print_count = 0, stride_bit, nr_items;
383 bool reverse_byte_order = false;
384 const struct side_arg *array_item;
385
386 switch (elem_type->type) {
387 case SIDE_TYPE_U8: /* Fall-through */
388 case SIDE_TYPE_BYTE: /* Fall-through */
389 case SIDE_TYPE_U16: /* Fall-through */
390 case SIDE_TYPE_U32: /* Fall-through */
391 case SIDE_TYPE_U64:
392 stride_bit = enum_elem_type_to_stride(elem_type);
393 reverse_byte_order = type_to_host_reverse_bo(elem_type);
394 array_item = item;
395 nr_items = 1;
396 break;
397 case SIDE_TYPE_ARRAY:
398 stride_bit = enum_elem_type_to_stride(elem_type->u.side_array.elem_type);
399 reverse_byte_order = type_to_host_reverse_bo(elem_type->u.side_array.elem_type);
400 array_item = item->u.side_static.side_array->sav;
401 nr_items = type_desc->u.side_array.length;
402 break;
403 case SIDE_TYPE_VLA:
404 stride_bit = enum_elem_type_to_stride(elem_type->u.side_vla.elem_type);
405 reverse_byte_order = type_to_host_reverse_bo(elem_type->u.side_vla.elem_type);
406 array_item = item->u.side_static.side_vla->sav;
407 nr_items = item->u.side_static.side_vla->len;
408 break;
409 default:
410 fprintf(stderr, "ERROR: Unexpected enum element type\n");
411 abort();
412 }
413
414 print_attributes("attr", ":", side_enum_mappings->attr, side_enum_mappings->nr_attr);
415 printf("%s", side_enum_mappings->nr_attr ? ", " : "");
416 printf("labels: [ ");
417 for (i = 0; i < side_enum_mappings->nr_mappings; i++) {
418 const struct side_enum_bitmap_mapping *mapping = &side_enum_mappings->mappings[i];
419 bool match = false;
420 uint64_t bit;
421
422 if (mapping->range_end < mapping->range_begin) {
423 fprintf(stderr, "ERROR: Unexpected enum bitmap range: %" PRIu64 "-%" PRIu64 "\n",
424 mapping->range_begin, mapping->range_end);
425 abort();
426 }
427 for (bit = mapping->range_begin; bit <= mapping->range_end; bit++) {
428 if (bit > (nr_items * stride_bit) - 1)
429 break;
430 switch (stride_bit) {
431 case 8:
432 {
433 uint8_t v = array_item[bit / 8].u.side_static.integer_value.side_u8;
434 if (v & (1ULL << (bit % 8))) {
435 match = true;
436 goto match;
437 }
438 break;
439 }
440 case 16:
441 {
442 uint16_t v = array_item[bit / 16].u.side_static.integer_value.side_u16;
443 if (reverse_byte_order)
444 v = side_bswap_16(v);
445 if (v & (1ULL << (bit % 16))) {
446 match = true;
447 goto match;
448 }
449 break;
450 }
451 case 32:
452 {
453 uint32_t v = array_item[bit / 32].u.side_static.integer_value.side_u32;
454 if (reverse_byte_order)
455 v = side_bswap_32(v);
456 if (v & (1ULL << (bit % 32))) {
457 match = true;
458 goto match;
459 }
460 break;
461 }
462 case 64:
463 {
464 uint64_t v = array_item[bit / 64].u.side_static.integer_value.side_u64;
465 if (reverse_byte_order)
466 v = side_bswap_64(v);
467 if (v & (1ULL << (bit % 64))) {
468 match = true;
469 goto match;
470 }
471 break;
472 }
473 default:
474 abort();
475 }
476 }
477 match:
478 if (match) {
479 printf("%s", print_count++ ? ", " : "");
480 printf("\"%s\"", mapping->label);
481 }
482 }
483 if (!print_count)
484 printf("<NO LABEL>");
485 printf(" ]");
486 }
487
488 static
489 void print_integer_binary(uint64_t v, int bits)
490 {
491 int i;
492
493 printf("0b");
494 v <<= 64 - bits;
495 for (i = 0; i < bits; i++) {
496 printf("%c", v & (1ULL << 63) ? '1' : '0');
497 v <<= 1;
498 }
499 }
500
501 static
502 void tracer_print_type_header(const char *separator,
503 const struct side_attr *attr, uint32_t nr_attr)
504 {
505 print_attributes("attr", separator, attr, nr_attr);
506 printf("%s", nr_attr ? ", " : "");
507 printf("value%s ", separator);
508 }
509
510 static
511 void tracer_print_type_bool(const char *separator,
512 const struct side_type_bool *type_bool,
513 const union side_bool_value *value,
514 uint16_t offset_bits)
515 {
516 uint32_t len_bits;
517 bool reverse_bo;
518 uint64_t v;
519
520 if (!type_bool->len_bits)
521 len_bits = type_bool->bool_size * CHAR_BIT;
522 else
523 len_bits = type_bool->len_bits;
524 if (len_bits + offset_bits > type_bool->bool_size * CHAR_BIT)
525 abort();
526 reverse_bo = type_bool->byte_order != SIDE_TYPE_BYTE_ORDER_HOST;
527 switch (type_bool->bool_size) {
528 case 1:
529 v = value->side_bool8;
530 break;
531 case 2:
532 {
533 uint16_t side_u16;
534
535 side_u16 = value->side_bool16;
536 if (reverse_bo)
537 side_u16 = side_bswap_16(side_u16);
538 v = side_u16;
539 break;
540 }
541 case 4:
542 {
543 uint32_t side_u32;
544
545 side_u32 = value->side_bool32;
546 if (reverse_bo)
547 side_u32 = side_bswap_32(side_u32);
548 v = side_u32;
549 break;
550 }
551 case 8:
552 {
553 uint64_t side_u64;
554
555 side_u64 = value->side_bool64;
556 if (reverse_bo)
557 side_u64 = side_bswap_64(side_u64);
558 v = side_u64;
559 break;
560 }
561 default:
562 abort();
563 }
564 v >>= offset_bits;
565 if (len_bits < 64)
566 v &= (1ULL << len_bits) - 1;
567 tracer_print_type_header(separator, type_bool->attr, type_bool->nr_attr);
568 printf("%s", v ? "true" : "false");
569 }
570
571 static
572 void tracer_print_type_integer(const char *separator,
573 const struct side_type_integer *type_integer,
574 const union side_integer_value *value,
575 uint16_t offset_bits,
576 enum tracer_display_base default_base)
577 {
578 enum tracer_display_base base;
579 uint32_t len_bits;
580 bool reverse_bo;
581 union {
582 uint64_t v_unsigned;
583 int64_t v_signed;
584 } v;
585
586 if (!type_integer->len_bits)
587 len_bits = type_integer->integer_size * CHAR_BIT;
588 else
589 len_bits = type_integer->len_bits;
590 if (len_bits + offset_bits > type_integer->integer_size * CHAR_BIT)
591 abort();
592 reverse_bo = type_integer->byte_order != SIDE_TYPE_BYTE_ORDER_HOST;
593 base = get_attr_display_base(type_integer->attr,
594 type_integer->nr_attr,
595 default_base);
596 switch (type_integer->integer_size) {
597 case 1:
598 if (type_integer->signedness)
599 v.v_signed = value->side_s8;
600 else
601 v.v_unsigned = value->side_u8;
602 break;
603 case 2:
604 if (type_integer->signedness) {
605 int16_t side_s16;
606
607 side_s16 = value->side_s16;
608 if (reverse_bo)
609 side_s16 = side_bswap_16(side_s16);
610 v.v_signed = side_s16;
611 } else {
612 uint16_t side_u16;
613
614 side_u16 = value->side_u16;
615 if (reverse_bo)
616 side_u16 = side_bswap_16(side_u16);
617 v.v_unsigned = side_u16;
618 }
619 break;
620 case 4:
621 if (type_integer->signedness) {
622 int32_t side_s32;
623
624 side_s32 = value->side_s32;
625 if (reverse_bo)
626 side_s32 = side_bswap_32(side_s32);
627 v.v_signed = side_s32;
628 } else {
629 uint32_t side_u32;
630
631 side_u32 = value->side_u32;
632 if (reverse_bo)
633 side_u32 = side_bswap_32(side_u32);
634 v.v_unsigned = side_u32;
635 }
636 break;
637 case 8:
638 if (type_integer->signedness) {
639 int64_t side_s64;
640
641 side_s64 = value->side_s64;
642 if (reverse_bo)
643 side_s64 = side_bswap_64(side_s64);
644 v.v_signed = side_s64;
645 } else {
646 uint64_t side_u64;
647
648 side_u64 = value->side_u64;
649 if (reverse_bo)
650 side_u64 = side_bswap_64(side_u64);
651 v.v_unsigned = side_u64;
652 }
653 break;
654 default:
655 abort();
656 }
657 v.v_unsigned >>= offset_bits;
658 if (len_bits < 64)
659 v.v_unsigned &= (1ULL << len_bits) - 1;
660 tracer_print_type_header(separator, type_integer->attr, type_integer->nr_attr);
661 switch (base) {
662 case TRACER_DISPLAY_BASE_2:
663 print_integer_binary(v.v_unsigned, len_bits);
664 break;
665 case TRACER_DISPLAY_BASE_8:
666 printf("0%" PRIo64, v.v_unsigned);
667 break;
668 case TRACER_DISPLAY_BASE_10:
669 if (type_integer->signedness) {
670 /* Sign-extend. */
671 if (len_bits < 64) {
672 if (v.v_unsigned & (1ULL << (len_bits - 1)))
673 v.v_unsigned |= ~((1ULL << len_bits) - 1);
674 }
675 printf("%" PRId64, v.v_signed);
676 } else {
677 printf("%" PRIu64, v.v_unsigned);
678 }
679 break;
680 case TRACER_DISPLAY_BASE_16:
681 printf("0x%" PRIx64, v.v_unsigned);
682 break;
683 default:
684 abort();
685 }
686 }
687
688 static
689 void tracer_print_type_float(const char *separator,
690 const struct side_type_float *type_float,
691 const union side_float_value *value)
692 {
693 bool reverse_bo;
694
695 reverse_bo = type_float->byte_order != SIDE_TYPE_FLOAT_WORD_ORDER_HOST;
696 tracer_print_type_header(separator, type_float->attr, type_float->nr_attr);
697 switch (type_float->float_size) {
698 case 2:
699 {
700 #if __HAVE_FLOAT16
701 union {
702 _Float16 f;
703 uint16_t u;
704 } float16 = {
705 .f = value->side_float_binary16,
706 };
707
708 if (reverse_bo)
709 float16.u = side_bswap_16(float16.u);
710 tracer_print_type_header(":", type_desc->u.side_float.attr, type_desc->u.side_float.nr_attr);
711 printf("%g", (double) float16.f);
712 break;
713 #else
714 fprintf(stderr, "ERROR: Unsupported binary16 float type\n");
715 abort();
716 #endif
717 }
718 case 4:
719 {
720 #if __HAVE_FLOAT32
721 union {
722 _Float32 f;
723 uint32_t u;
724 } float32 = {
725 .f = value->side_float_binary32,
726 };
727
728 if (reverse_bo)
729 float32.u = side_bswap_32(float32.u);
730 printf("%g", (double) float32.f);
731 break;
732 #else
733 fprintf(stderr, "ERROR: Unsupported binary32 float type\n");
734 abort();
735 #endif
736 }
737 case 8:
738 {
739 #if __HAVE_FLOAT64
740 union {
741 _Float64 f;
742 uint64_t u;
743 } float64 = {
744 .f = value->side_float_binary64,
745 };
746
747 if (reverse_bo)
748 float64.u = side_bswap_64(float64.u);
749 printf("%g", (double) float64.f);
750 break;
751 #else
752 fprintf(stderr, "ERROR: Unsupported binary64 float type\n");
753 abort();
754 #endif
755 }
756 case 16:
757 {
758 #if __HAVE_FLOAT128
759 union {
760 _Float128 f;
761 char arr[16];
762 } float128 = {
763 .f = value->side_float_binary128,
764 };
765
766 if (reverse_bo)
767 side_bswap_128p(float128.arr);
768 printf("%Lg", (long double) float128.f);
769 break;
770 #else
771 fprintf(stderr, "ERROR: Unsupported binary128 float type\n");
772 abort();
773 #endif
774 }
775 default:
776 fprintf(stderr, "ERROR: Unknown float size\n");
777 abort();
778 }
779 }
780
781 static
782 void tracer_print_type(const struct side_type *type_desc, const struct side_arg *item)
783 {
784 enum side_type_label type;
785
786 switch (type_desc->type) {
787 case SIDE_TYPE_ENUM:
788 switch (item->type) {
789 case SIDE_TYPE_U8:
790 case SIDE_TYPE_U16:
791 case SIDE_TYPE_U32:
792 case SIDE_TYPE_U64:
793 case SIDE_TYPE_S8:
794 case SIDE_TYPE_S16:
795 case SIDE_TYPE_S32:
796 case SIDE_TYPE_S64:
797 break;
798 default:
799 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
800 abort();
801 break;
802 }
803 break;
804
805 case SIDE_TYPE_ENUM_BITMAP:
806 switch (item->type) {
807 case SIDE_TYPE_U8:
808 case SIDE_TYPE_BYTE:
809 case SIDE_TYPE_U16:
810 case SIDE_TYPE_U32:
811 case SIDE_TYPE_U64:
812 case SIDE_TYPE_ARRAY:
813 case SIDE_TYPE_VLA:
814 break;
815 default:
816 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
817 abort();
818 break;
819 }
820 break;
821
822 case SIDE_TYPE_DYNAMIC:
823 switch (item->type) {
824 case SIDE_TYPE_DYNAMIC_NULL:
825 case SIDE_TYPE_DYNAMIC_BOOL:
826 case SIDE_TYPE_DYNAMIC_U8:
827 case SIDE_TYPE_DYNAMIC_U16:
828 case SIDE_TYPE_DYNAMIC_U32:
829 case SIDE_TYPE_DYNAMIC_U64:
830 case SIDE_TYPE_DYNAMIC_S8:
831 case SIDE_TYPE_DYNAMIC_S16:
832 case SIDE_TYPE_DYNAMIC_S32:
833 case SIDE_TYPE_DYNAMIC_S64:
834 case SIDE_TYPE_DYNAMIC_BYTE:
835 case SIDE_TYPE_DYNAMIC_POINTER:
836 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY16:
837 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY32:
838 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY64:
839 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY128:
840 case SIDE_TYPE_DYNAMIC_STRING:
841 case SIDE_TYPE_DYNAMIC_STRUCT:
842 case SIDE_TYPE_DYNAMIC_STRUCT_VISITOR:
843 case SIDE_TYPE_DYNAMIC_VLA:
844 case SIDE_TYPE_DYNAMIC_VLA_VISITOR:
845 break;
846 default:
847 fprintf(stderr, "ERROR: Unexpected dynamic type\n");
848 abort();
849 break;
850 }
851 break;
852
853 default:
854 if (type_desc->type != item->type) {
855 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
856 abort();
857 }
858 break;
859 }
860
861 if (type_desc->type == SIDE_TYPE_ENUM || type_desc->type == SIDE_TYPE_ENUM_BITMAP)
862 type = (enum side_type_label) type_desc->type;
863 else
864 type = (enum side_type_label) item->type;
865
866 printf("{ ");
867 switch (type) {
868 case SIDE_TYPE_NULL:
869 tracer_print_type_header(":", type_desc->u.side_null.attr, type_desc->u.side_null.nr_attr);
870 printf("<NULL TYPE>");
871 break;
872
873 case SIDE_TYPE_BOOL:
874 tracer_print_type_bool(":", &type_desc->u.side_bool, &item->u.side_static.bool_value, 0);
875 break;
876
877 case SIDE_TYPE_U8:
878 case SIDE_TYPE_U16:
879 case SIDE_TYPE_U32:
880 case SIDE_TYPE_U64:
881 case SIDE_TYPE_S8:
882 case SIDE_TYPE_S16:
883 case SIDE_TYPE_S32:
884 case SIDE_TYPE_S64:
885 tracer_print_type_integer(":", &type_desc->u.side_integer, &item->u.side_static.integer_value, 0,
886 TRACER_DISPLAY_BASE_10);
887 break;
888
889 case SIDE_TYPE_POINTER:
890 tracer_print_type_integer(":", &type_desc->u.side_integer, &item->u.side_static.integer_value, 0,
891 TRACER_DISPLAY_BASE_16);
892 break;
893
894 case SIDE_TYPE_BYTE:
895 tracer_print_type_header(":", type_desc->u.side_byte.attr, type_desc->u.side_byte.nr_attr);
896 printf("0x%" PRIx8, item->u.side_static.byte_value);
897 break;
898
899 case SIDE_TYPE_ENUM:
900 print_enum(type_desc, item);
901 break;
902
903 case SIDE_TYPE_ENUM_BITMAP:
904 print_enum_bitmap(type_desc, item);
905 break;
906
907 case SIDE_TYPE_FLOAT_BINARY16:
908 case SIDE_TYPE_FLOAT_BINARY32:
909 case SIDE_TYPE_FLOAT_BINARY64:
910 case SIDE_TYPE_FLOAT_BINARY128:
911 tracer_print_type_float(":", &type_desc->u.side_float, &item->u.side_static.float_value);
912 break;
913
914 case SIDE_TYPE_STRING:
915 tracer_print_type_header(":", type_desc->u.side_string.attr, type_desc->u.side_string.nr_attr);
916 printf("\"%s\"", (const char *)(uintptr_t) item->u.side_static.string_value);
917 break;
918 case SIDE_TYPE_STRUCT:
919 tracer_print_struct(type_desc, item->u.side_static.side_struct);
920 break;
921 case SIDE_TYPE_GATHER_STRUCT:
922 (void) tracer_print_gather_struct(&type_desc->u.side_gather, item->u.side_static.side_struct_gather_ptr);
923 break;
924 case SIDE_TYPE_GATHER_ARRAY:
925 (void) tracer_print_gather_array(&type_desc->u.side_gather, item->u.side_static.side_array_gather_ptr);
926 break;
927 case SIDE_TYPE_GATHER_VLA:
928 (void) tracer_print_gather_vla(&type_desc->u.side_gather, item->u.side_static.side_vla_gather.ptr,
929 item->u.side_static.side_vla_gather.length_ptr);
930 break;
931 case SIDE_TYPE_GATHER_BOOL:
932 (void) tracer_print_gather_bool_type(&type_desc->u.side_gather, item->u.side_static.side_bool_gather_ptr);
933 break;
934 case SIDE_TYPE_GATHER_BYTE:
935 (void) tracer_print_gather_byte_type(&type_desc->u.side_gather, item->u.side_static.side_byte_gather_ptr);
936 break;
937 case SIDE_TYPE_GATHER_POINTER:
938 (void) tracer_print_gather_integer_type(&type_desc->u.side_gather, item->u.side_static.side_integer_gather_ptr,
939 TRACER_DISPLAY_BASE_16);
940 break;
941 case SIDE_TYPE_GATHER_UNSIGNED_INT:
942 case SIDE_TYPE_GATHER_SIGNED_INT:
943 (void) tracer_print_gather_integer_type(&type_desc->u.side_gather, item->u.side_static.side_integer_gather_ptr,
944 TRACER_DISPLAY_BASE_10);
945 break;
946 case SIDE_TYPE_GATHER_FLOAT:
947 (void) tracer_print_gather_float_type(&type_desc->u.side_gather, item->u.side_static.side_float_gather_ptr);
948 break;
949 case SIDE_TYPE_ARRAY:
950 tracer_print_array(type_desc, item->u.side_static.side_array);
951 break;
952 case SIDE_TYPE_VLA:
953 tracer_print_vla(type_desc, item->u.side_static.side_vla);
954 break;
955 case SIDE_TYPE_VLA_VISITOR:
956 tracer_print_vla_visitor(type_desc, item->u.side_static.side_vla_app_visitor_ctx);
957 break;
958
959 /* Dynamic types */
960 case SIDE_TYPE_DYNAMIC_NULL:
961 case SIDE_TYPE_DYNAMIC_BOOL:
962 case SIDE_TYPE_DYNAMIC_U8:
963 case SIDE_TYPE_DYNAMIC_U16:
964 case SIDE_TYPE_DYNAMIC_U32:
965 case SIDE_TYPE_DYNAMIC_U64:
966 case SIDE_TYPE_DYNAMIC_S8:
967 case SIDE_TYPE_DYNAMIC_S16:
968 case SIDE_TYPE_DYNAMIC_S32:
969 case SIDE_TYPE_DYNAMIC_S64:
970 case SIDE_TYPE_DYNAMIC_BYTE:
971 case SIDE_TYPE_DYNAMIC_POINTER:
972 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY16:
973 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY32:
974 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY64:
975 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY128:
976 case SIDE_TYPE_DYNAMIC_STRING:
977 case SIDE_TYPE_DYNAMIC_STRUCT:
978 case SIDE_TYPE_DYNAMIC_STRUCT_VISITOR:
979 case SIDE_TYPE_DYNAMIC_VLA:
980 case SIDE_TYPE_DYNAMIC_VLA_VISITOR:
981 tracer_print_dynamic(item);
982 break;
983 default:
984 fprintf(stderr, "<UNKNOWN TYPE>\n");
985 abort();
986 }
987 printf(" }");
988 }
989
990 static
991 void tracer_print_field(const struct side_event_field *item_desc, const struct side_arg *item)
992 {
993 printf("%s: ", item_desc->field_name);
994 tracer_print_type(&item_desc->side_type, item);
995 }
996
997 static
998 void tracer_print_struct(const struct side_type *type_desc, const struct side_arg_vec *side_arg_vec)
999 {
1000 const struct side_arg *sav = side_arg_vec->sav;
1001 uint32_t i, side_sav_len = side_arg_vec->len;
1002
1003 if (type_desc->u.side_struct->nr_fields != side_sav_len) {
1004 fprintf(stderr, "ERROR: number of fields mismatch between description and arguments of structure\n");
1005 abort();
1006 }
1007 print_attributes("attr", ":", type_desc->u.side_struct->attr, type_desc->u.side_struct->nr_attr);
1008 printf("%s", type_desc->u.side_struct->nr_attr ? ", " : "");
1009 printf("fields: { ");
1010 for (i = 0; i < side_sav_len; i++) {
1011 printf("%s", i ? ", " : "");
1012 tracer_print_field(&type_desc->u.side_struct->fields[i], &sav[i]);
1013 }
1014 printf(" }");
1015 }
1016
1017 static
1018 void tracer_print_array(const struct side_type *type_desc, const struct side_arg_vec *side_arg_vec)
1019 {
1020 const struct side_arg *sav = side_arg_vec->sav;
1021 uint32_t i, side_sav_len = side_arg_vec->len;
1022
1023 if (type_desc->u.side_array.length != side_sav_len) {
1024 fprintf(stderr, "ERROR: length mismatch between description and arguments of array\n");
1025 abort();
1026 }
1027 print_attributes("attr", ":", type_desc->u.side_array.attr, type_desc->u.side_array.nr_attr);
1028 printf("%s", type_desc->u.side_array.nr_attr ? ", " : "");
1029 printf("elements: ");
1030 printf("[ ");
1031 for (i = 0; i < side_sav_len; i++) {
1032 printf("%s", i ? ", " : "");
1033 tracer_print_type(type_desc->u.side_array.elem_type, &sav[i]);
1034 }
1035 printf(" ]");
1036 }
1037
1038 static
1039 void tracer_print_vla(const struct side_type *type_desc, const struct side_arg_vec *side_arg_vec)
1040 {
1041 const struct side_arg *sav = side_arg_vec->sav;
1042 uint32_t i, side_sav_len = side_arg_vec->len;
1043
1044 print_attributes("attr", ":", type_desc->u.side_vla.attr, type_desc->u.side_vla.nr_attr);
1045 printf("%s", type_desc->u.side_vla.nr_attr ? ", " : "");
1046 printf("elements: ");
1047 printf("[ ");
1048 for (i = 0; i < side_sav_len; i++) {
1049 printf("%s", i ? ", " : "");
1050 tracer_print_type(type_desc->u.side_vla.elem_type, &sav[i]);
1051 }
1052 printf(" ]");
1053 }
1054
1055 static
1056 const char *tracer_gather_access(enum side_type_gather_access_mode access_mode, const char *ptr)
1057 {
1058 switch (access_mode) {
1059 case SIDE_TYPE_GATHER_ACCESS_DIRECT:
1060 return ptr;
1061 case SIDE_TYPE_GATHER_ACCESS_POINTER:
1062 /* Dereference pointer */
1063 memcpy(&ptr, ptr, sizeof(ptr));
1064 return ptr;
1065 default:
1066 abort();
1067 }
1068 }
1069
1070 static
1071 uint32_t tracer_gather_size(enum side_type_gather_access_mode access_mode, uint32_t len)
1072 {
1073 switch (access_mode) {
1074 case SIDE_TYPE_GATHER_ACCESS_DIRECT:
1075 return len;
1076 case SIDE_TYPE_GATHER_ACCESS_POINTER:
1077 return sizeof(void *);
1078 default:
1079 abort();
1080 }
1081 }
1082
1083 static
1084 uint64_t tracer_load_gather_integer_type(const struct side_type_gather *type_gather, const void *_ptr)
1085 {
1086 enum side_type_gather_access_mode access_mode = type_gather->u.side_integer.access_mode;
1087 uint32_t integer_size_bytes = type_gather->u.side_integer.type.integer_size;
1088 const char *ptr = (const char *) _ptr;
1089 union side_integer_value value;
1090
1091 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_integer.offset);
1092 memcpy(&value, ptr, integer_size_bytes);
1093 switch (type_gather->u.side_integer.type.integer_size) {
1094 case 1:
1095 return (uint64_t) value.side_u8;
1096 case 2:
1097 return (uint64_t) value.side_u16;
1098 case 4:
1099 return (uint64_t) value.side_u32;
1100 case 8:
1101 return (uint64_t) value.side_u64;
1102 default:
1103 abort();
1104 }
1105 }
1106
1107 static
1108 uint32_t tracer_print_gather_bool_type(const struct side_type_gather *type_gather, const void *_ptr)
1109 {
1110 enum side_type_gather_access_mode access_mode = type_gather->u.side_bool.access_mode;
1111 uint32_t bool_size_bytes = type_gather->u.side_bool.type.bool_size;
1112 const char *ptr = (const char *) _ptr;
1113 union side_bool_value value;
1114
1115 switch (bool_size_bytes) {
1116 case 1:
1117 case 2:
1118 case 4:
1119 case 8:
1120 break;
1121 default:
1122 abort();
1123 }
1124 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_bool.offset);
1125 memcpy(&value, ptr, bool_size_bytes);
1126 tracer_print_type_bool(":", &type_gather->u.side_bool.type, &value,
1127 type_gather->u.side_bool.offset_bits);
1128 return tracer_gather_size(access_mode, bool_size_bytes);
1129 }
1130
1131 static
1132 uint32_t tracer_print_gather_byte_type(const struct side_type_gather *type_gather, const void *_ptr)
1133 {
1134 enum side_type_gather_access_mode access_mode = type_gather->u.side_byte.access_mode;
1135 const char *ptr = (const char *) _ptr;
1136 uint8_t value;
1137
1138 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_byte.offset);
1139 memcpy(&value, ptr, 1);
1140 tracer_print_type_header(":", type_gather->u.side_byte.type.attr,
1141 type_gather->u.side_byte.type.nr_attr);
1142 printf("0x%" PRIx8, value);
1143 return tracer_gather_size(access_mode, 1);
1144 }
1145
1146 static
1147 uint32_t tracer_print_gather_integer_type(const struct side_type_gather *type_gather, const void *_ptr,
1148 enum tracer_display_base default_base)
1149 {
1150 enum side_type_gather_access_mode access_mode = type_gather->u.side_integer.access_mode;
1151 uint32_t integer_size_bytes = type_gather->u.side_integer.type.integer_size;
1152 const char *ptr = (const char *) _ptr;
1153 union side_integer_value value;
1154
1155 switch (integer_size_bytes) {
1156 case 1:
1157 case 2:
1158 case 4:
1159 case 8:
1160 break;
1161 default:
1162 abort();
1163 }
1164 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_integer.offset);
1165 memcpy(&value, ptr, integer_size_bytes);
1166 tracer_print_type_integer(":", &type_gather->u.side_integer.type, &value,
1167 type_gather->u.side_integer.offset_bits, default_base);
1168 return tracer_gather_size(access_mode, integer_size_bytes);
1169 }
1170
1171 static
1172 uint32_t tracer_print_gather_float_type(const struct side_type_gather *type_gather, const void *_ptr)
1173 {
1174 enum side_type_gather_access_mode access_mode = type_gather->u.side_float.access_mode;
1175 uint32_t float_size_bytes = type_gather->u.side_float.type.float_size;
1176 const char *ptr = (const char *) _ptr;
1177 union side_float_value value;
1178
1179 switch (float_size_bytes) {
1180 case 2:
1181 case 4:
1182 case 8:
1183 case 16:
1184 break;
1185 default:
1186 abort();
1187 }
1188 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_float.offset);
1189 memcpy(&value, ptr, float_size_bytes);
1190 tracer_print_type_float(":", &type_gather->u.side_float.type, &value);
1191 return tracer_gather_size(access_mode, float_size_bytes);
1192 }
1193
1194 static
1195 uint32_t tracer_print_gather_type(const struct side_type *type_desc, const void *ptr)
1196 {
1197 uint32_t len;
1198
1199 printf("{ ");
1200 switch (type_desc->type) {
1201 case SIDE_TYPE_GATHER_BOOL:
1202 len = tracer_print_gather_bool_type(&type_desc->u.side_gather, ptr);
1203 break;
1204 case SIDE_TYPE_GATHER_BYTE:
1205 len = tracer_print_gather_byte_type(&type_desc->u.side_gather, ptr);
1206 break;
1207 case SIDE_TYPE_GATHER_POINTER:
1208 len = tracer_print_gather_integer_type(&type_desc->u.side_gather, ptr,
1209 TRACER_DISPLAY_BASE_16);
1210 break;
1211 case SIDE_TYPE_GATHER_UNSIGNED_INT:
1212 case SIDE_TYPE_GATHER_SIGNED_INT:
1213 len = tracer_print_gather_integer_type(&type_desc->u.side_gather, ptr,
1214 TRACER_DISPLAY_BASE_10);
1215 break;
1216 case SIDE_TYPE_GATHER_FLOAT:
1217 len = tracer_print_gather_float_type(&type_desc->u.side_gather, ptr);
1218 break;
1219 case SIDE_TYPE_GATHER_STRUCT:
1220 len = tracer_print_gather_struct(&type_desc->u.side_gather, ptr);
1221 break;
1222 case SIDE_TYPE_GATHER_ARRAY:
1223 len = tracer_print_gather_array(&type_desc->u.side_gather, ptr);
1224 break;
1225 case SIDE_TYPE_GATHER_VLA:
1226 len = tracer_print_gather_vla(&type_desc->u.side_gather, ptr, ptr);
1227 break;
1228 default:
1229 fprintf(stderr, "<UNKNOWN GATHER TYPE>");
1230 abort();
1231 }
1232 printf(" }");
1233 return len;
1234 }
1235
1236 static
1237 void tracer_print_gather_field(const struct side_event_field *field, const void *ptr)
1238 {
1239 printf("%s: ", field->field_name);
1240 (void) tracer_print_gather_type(&field->side_type, ptr);
1241 }
1242
1243 static
1244 uint32_t tracer_print_gather_struct(const struct side_type_gather *type_gather, const void *_ptr)
1245 {
1246 enum side_type_gather_access_mode access_mode = type_gather->u.side_struct.access_mode;
1247 const char *ptr = (const char *) _ptr;
1248 uint32_t i;
1249
1250 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_struct.offset);
1251 print_attributes("attr", ":", type_gather->u.side_struct.type->attr, type_gather->u.side_struct.type->nr_attr);
1252 printf("%s", type_gather->u.side_struct.type->nr_attr ? ", " : "");
1253 printf("fields: { ");
1254 for (i = 0; i < type_gather->u.side_struct.type->nr_fields; i++) {
1255 printf("%s", i ? ", " : "");
1256 tracer_print_gather_field(&type_gather->u.side_struct.type->fields[i], ptr);
1257 }
1258 printf(" }");
1259 return tracer_gather_size(access_mode, type_gather->u.side_struct.size);
1260 }
1261
1262 static
1263 uint32_t tracer_print_gather_array(const struct side_type_gather *type_gather, const void *_ptr)
1264 {
1265 enum side_type_gather_access_mode access_mode = type_gather->u.side_array.access_mode;
1266 const char *ptr = (const char *) _ptr, *orig_ptr;
1267 uint32_t i;
1268
1269 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_array.offset);
1270 orig_ptr = ptr;
1271 print_attributes("attr", ":", type_gather->u.side_array.type.attr, type_gather->u.side_array.type.nr_attr);
1272 printf("%s", type_gather->u.side_array.type.nr_attr ? ", " : "");
1273 printf("elements: ");
1274 printf("[ ");
1275 for (i = 0; i < type_gather->u.side_array.type.length; i++) {
1276 switch (type_gather->u.side_array.type.elem_type->type) {
1277 case SIDE_TYPE_GATHER_VLA:
1278 fprintf(stderr, "<gather VLA only supported within gather structures>\n");
1279 abort();
1280 default:
1281 break;
1282 }
1283 printf("%s", i ? ", " : "");
1284 ptr += tracer_print_gather_type(type_gather->u.side_array.type.elem_type, ptr);
1285 }
1286 printf(" ]");
1287 return tracer_gather_size(access_mode, ptr - orig_ptr);
1288 }
1289
1290 static
1291 uint32_t tracer_print_gather_vla(const struct side_type_gather *type_gather, const void *_ptr,
1292 const void *_length_ptr)
1293 {
1294 enum side_type_gather_access_mode access_mode = type_gather->u.side_vla.access_mode;
1295 const char *ptr = (const char *) _ptr, *orig_ptr;
1296 const char *length_ptr = (const char *) _length_ptr;
1297 uint32_t i, length;
1298
1299 /* Access length */
1300 switch (type_gather->u.side_vla.length_type->type) {
1301 case SIDE_TYPE_GATHER_UNSIGNED_INT:
1302 case SIDE_TYPE_GATHER_SIGNED_INT:
1303 break;
1304 default:
1305 fprintf(stderr, "<gather VLA expects integer gather length type>\n");
1306 abort();
1307 }
1308 length = (uint32_t) tracer_load_gather_integer_type(&type_gather->u.side_vla.length_type->u.side_gather, length_ptr);
1309 ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_vla.offset);
1310 orig_ptr = ptr;
1311 print_attributes("attr", ":", type_gather->u.side_vla.type.attr, type_gather->u.side_vla.type.nr_attr);
1312 printf("%s", type_gather->u.side_vla.type.nr_attr ? ", " : "");
1313 printf("elements: ");
1314 printf("[ ");
1315 for (i = 0; i < length; i++) {
1316 switch (type_gather->u.side_vla.type.elem_type->type) {
1317 case SIDE_TYPE_GATHER_VLA:
1318 fprintf(stderr, "<gather VLA only supported within gather structures>\n");
1319 abort();
1320 default:
1321 break;
1322 }
1323 printf("%s", i ? ", " : "");
1324 ptr += tracer_print_gather_type(type_gather->u.side_vla.type.elem_type, ptr);
1325 }
1326 printf(" ]");
1327 return tracer_gather_size(access_mode, ptr - orig_ptr);
1328 }
1329
1330 struct tracer_visitor_priv {
1331 const struct side_type *elem_type;
1332 int i;
1333 };
1334
1335 static
1336 enum side_visitor_status tracer_write_elem_cb(const struct side_tracer_visitor_ctx *tracer_ctx,
1337 const struct side_arg *elem)
1338 {
1339 struct tracer_visitor_priv *tracer_priv = (struct tracer_visitor_priv *) tracer_ctx->priv;
1340
1341 printf("%s", tracer_priv->i++ ? ", " : "");
1342 tracer_print_type(tracer_priv->elem_type, elem);
1343 return SIDE_VISITOR_STATUS_OK;
1344 }
1345
1346 static
1347 void tracer_print_vla_visitor(const struct side_type *type_desc, void *app_ctx)
1348 {
1349 enum side_visitor_status status;
1350 struct tracer_visitor_priv tracer_priv = {
1351 .elem_type = type_desc->u.side_vla_visitor.elem_type,
1352 .i = 0,
1353 };
1354 const struct side_tracer_visitor_ctx tracer_ctx = {
1355 .write_elem = tracer_write_elem_cb,
1356 .priv = &tracer_priv,
1357 };
1358
1359 print_attributes("attr", ":", type_desc->u.side_vla_visitor.attr, type_desc->u.side_vla_visitor.nr_attr);
1360 printf("%s", type_desc->u.side_vla_visitor.nr_attr ? ", " : "");
1361 printf("elements: ");
1362 printf("[ ");
1363 status = type_desc->u.side_vla_visitor.visitor(&tracer_ctx, app_ctx);
1364 switch (status) {
1365 case SIDE_VISITOR_STATUS_OK:
1366 break;
1367 case SIDE_VISITOR_STATUS_ERROR:
1368 fprintf(stderr, "ERROR: Visitor error\n");
1369 abort();
1370 }
1371 printf(" ]");
1372 }
1373
1374 static
1375 void tracer_print_dynamic_struct(const struct side_arg_dynamic_struct *dynamic_struct)
1376 {
1377 const struct side_arg_dynamic_field *fields = dynamic_struct->fields;
1378 uint32_t i, len = dynamic_struct->len;
1379
1380 print_attributes("attr", "::", dynamic_struct->attr, dynamic_struct->nr_attr);
1381 printf("%s", dynamic_struct->nr_attr ? ", " : "");
1382 printf("fields:: ");
1383 printf("[ ");
1384 for (i = 0; i < len; i++) {
1385 printf("%s", i ? ", " : "");
1386 printf("%s:: ", fields[i].field_name);
1387 tracer_print_dynamic(&fields[i].elem);
1388 }
1389 printf(" ]");
1390 }
1391
1392 struct tracer_dynamic_struct_visitor_priv {
1393 int i;
1394 };
1395
1396 static
1397 enum side_visitor_status tracer_dynamic_struct_write_elem_cb(
1398 const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
1399 const struct side_arg_dynamic_field *dynamic_field)
1400 {
1401 struct tracer_dynamic_struct_visitor_priv *tracer_priv =
1402 (struct tracer_dynamic_struct_visitor_priv *) tracer_ctx->priv;
1403
1404 printf("%s", tracer_priv->i++ ? ", " : "");
1405 printf("%s:: ", dynamic_field->field_name);
1406 tracer_print_dynamic(&dynamic_field->elem);
1407 return SIDE_VISITOR_STATUS_OK;
1408 }
1409
1410 static
1411 void tracer_print_dynamic_struct_visitor(const struct side_arg *item)
1412 {
1413 enum side_visitor_status status;
1414 struct tracer_dynamic_struct_visitor_priv tracer_priv = {
1415 .i = 0,
1416 };
1417 const struct side_tracer_dynamic_struct_visitor_ctx tracer_ctx = {
1418 .write_field = tracer_dynamic_struct_write_elem_cb,
1419 .priv = &tracer_priv,
1420 };
1421 void *app_ctx = item->u.side_dynamic.side_dynamic_struct_visitor.app_ctx;
1422
1423 print_attributes("attr", "::", item->u.side_dynamic.side_dynamic_struct_visitor.attr, item->u.side_dynamic.side_dynamic_struct_visitor.nr_attr);
1424 printf("%s", item->u.side_dynamic.side_dynamic_struct_visitor.nr_attr ? ", " : "");
1425 printf("fields:: ");
1426 printf("[ ");
1427 status = item->u.side_dynamic.side_dynamic_struct_visitor.visitor(&tracer_ctx, app_ctx);
1428 switch (status) {
1429 case SIDE_VISITOR_STATUS_OK:
1430 break;
1431 case SIDE_VISITOR_STATUS_ERROR:
1432 fprintf(stderr, "ERROR: Visitor error\n");
1433 abort();
1434 }
1435 printf(" ]");
1436 }
1437
1438 static
1439 void tracer_print_dynamic_vla(const struct side_arg_dynamic_vla *vla)
1440 {
1441 const struct side_arg *sav = vla->sav;
1442 uint32_t i, side_sav_len = vla->len;
1443
1444 print_attributes("attr", "::", vla->attr, vla->nr_attr);
1445 printf("%s", vla->nr_attr ? ", " : "");
1446 printf("elements:: ");
1447 printf("[ ");
1448 for (i = 0; i < side_sav_len; i++) {
1449 printf("%s", i ? ", " : "");
1450 tracer_print_dynamic(&sav[i]);
1451 }
1452 printf(" ]");
1453 }
1454
1455 struct tracer_dynamic_vla_visitor_priv {
1456 int i;
1457 };
1458
1459 static
1460 enum side_visitor_status tracer_dynamic_vla_write_elem_cb(
1461 const struct side_tracer_visitor_ctx *tracer_ctx,
1462 const struct side_arg *elem)
1463 {
1464 struct tracer_dynamic_vla_visitor_priv *tracer_priv =
1465 (struct tracer_dynamic_vla_visitor_priv *) tracer_ctx->priv;
1466
1467 printf("%s", tracer_priv->i++ ? ", " : "");
1468 tracer_print_dynamic(elem);
1469 return SIDE_VISITOR_STATUS_OK;
1470 }
1471
1472 static
1473 void tracer_print_dynamic_vla_visitor(const struct side_arg *item)
1474 {
1475 enum side_visitor_status status;
1476 struct tracer_dynamic_vla_visitor_priv tracer_priv = {
1477 .i = 0,
1478 };
1479 const struct side_tracer_visitor_ctx tracer_ctx = {
1480 .write_elem = tracer_dynamic_vla_write_elem_cb,
1481 .priv = &tracer_priv,
1482 };
1483 void *app_ctx = item->u.side_dynamic.side_dynamic_vla_visitor.app_ctx;
1484
1485 print_attributes("attr", "::", item->u.side_dynamic.side_dynamic_vla_visitor.attr, item->u.side_dynamic.side_dynamic_vla_visitor.nr_attr);
1486 printf("%s", item->u.side_dynamic.side_dynamic_vla_visitor.nr_attr ? ", " : "");
1487 printf("elements:: ");
1488 printf("[ ");
1489 status = item->u.side_dynamic.side_dynamic_vla_visitor.visitor(&tracer_ctx, app_ctx);
1490 switch (status) {
1491 case SIDE_VISITOR_STATUS_OK:
1492 break;
1493 case SIDE_VISITOR_STATUS_ERROR:
1494 fprintf(stderr, "ERROR: Visitor error\n");
1495 abort();
1496 }
1497 printf(" ]");
1498 }
1499
1500 static
1501 void tracer_print_dynamic(const struct side_arg *item)
1502 {
1503 printf("{ ");
1504 switch (item->type) {
1505 case SIDE_TYPE_DYNAMIC_NULL:
1506 tracer_print_type_header("::", item->u.side_dynamic.side_null.attr, item->u.side_dynamic.side_null.nr_attr);
1507 printf("<NULL TYPE>");
1508 break;
1509
1510 case SIDE_TYPE_DYNAMIC_BOOL:
1511 tracer_print_type_bool("::", &item->u.side_dynamic.side_bool.type, &item->u.side_dynamic.side_bool.value, 0);
1512 break;
1513
1514 case SIDE_TYPE_DYNAMIC_U8:
1515 case SIDE_TYPE_DYNAMIC_U16:
1516 case SIDE_TYPE_DYNAMIC_U32:
1517 case SIDE_TYPE_DYNAMIC_U64:
1518 case SIDE_TYPE_DYNAMIC_S8:
1519 case SIDE_TYPE_DYNAMIC_S16:
1520 case SIDE_TYPE_DYNAMIC_S32:
1521 case SIDE_TYPE_DYNAMIC_S64:
1522 tracer_print_type_integer("::", &item->u.side_dynamic.side_integer.type, &item->u.side_dynamic.side_integer.value, 0,
1523 TRACER_DISPLAY_BASE_10);
1524 break;
1525 case SIDE_TYPE_DYNAMIC_BYTE:
1526 tracer_print_type_header("::", item->u.side_dynamic.side_byte.type.attr, item->u.side_dynamic.side_byte.type.nr_attr);
1527 printf("0x%" PRIx8, item->u.side_dynamic.side_byte.value);
1528 break;
1529
1530 case SIDE_TYPE_DYNAMIC_POINTER:
1531 tracer_print_type_integer("::", &item->u.side_dynamic.side_integer.type, &item->u.side_dynamic.side_integer.value, 0,
1532 TRACER_DISPLAY_BASE_16);
1533 break;
1534
1535 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY16:
1536 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY32:
1537 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY64:
1538 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY128:
1539 tracer_print_type_float("::", &item->u.side_dynamic.side_float.type,
1540 &item->u.side_dynamic.side_float.value);
1541 break;
1542
1543 case SIDE_TYPE_DYNAMIC_STRING:
1544 tracer_print_type_header("::", item->u.side_dynamic.side_string.type.attr, item->u.side_dynamic.side_string.type.nr_attr);
1545 printf("\"%s\"", (const char *)(uintptr_t) item->u.side_dynamic.side_string.value);
1546 break;
1547 case SIDE_TYPE_DYNAMIC_STRUCT:
1548 tracer_print_dynamic_struct(item->u.side_dynamic.side_dynamic_struct);
1549 break;
1550 case SIDE_TYPE_DYNAMIC_STRUCT_VISITOR:
1551 tracer_print_dynamic_struct_visitor(item);
1552 break;
1553 case SIDE_TYPE_DYNAMIC_VLA:
1554 tracer_print_dynamic_vla(item->u.side_dynamic.side_dynamic_vla);
1555 break;
1556 case SIDE_TYPE_DYNAMIC_VLA_VISITOR:
1557 tracer_print_dynamic_vla_visitor(item);
1558 break;
1559 default:
1560 fprintf(stderr, "<UNKNOWN TYPE>\n");
1561 abort();
1562 }
1563 printf(" }");
1564 }
1565
1566 static
1567 void tracer_print_static_fields(const struct side_event_description *desc,
1568 const struct side_arg_vec *side_arg_vec,
1569 uint32_t *nr_items)
1570 {
1571 const struct side_arg *sav = side_arg_vec->sav;
1572 uint32_t i, side_sav_len = side_arg_vec->len;
1573
1574 printf("provider: %s, event: %s", desc->provider_name, desc->event_name);
1575 if (desc->nr_fields != side_sav_len) {
1576 fprintf(stderr, "ERROR: number of fields mismatch between description and arguments\n");
1577 abort();
1578 }
1579 print_attributes(", attr", ":", desc->attr, desc->nr_attr);
1580 printf("%s", side_sav_len ? ", fields: [ " : "");
1581 for (i = 0; i < side_sav_len; i++) {
1582 printf("%s", i ? ", " : "");
1583 tracer_print_field(&desc->fields[i], &sav[i]);
1584 }
1585 if (nr_items)
1586 *nr_items = i;
1587 if (side_sav_len)
1588 printf(" ]");
1589 }
1590
1591 void tracer_call(const struct side_event_description *desc,
1592 const struct side_arg_vec *side_arg_vec,
1593 void *priv __attribute__((unused)))
1594 {
1595 uint32_t nr_fields = 0;
1596
1597 tracer_print_static_fields(desc, side_arg_vec, &nr_fields);
1598 printf("\n");
1599 }
1600
1601 void tracer_call_variadic(const struct side_event_description *desc,
1602 const struct side_arg_vec *side_arg_vec,
1603 const struct side_arg_dynamic_struct *var_struct,
1604 void *priv __attribute__((unused)))
1605 {
1606 uint32_t nr_fields = 0, i, var_struct_len = var_struct->len;
1607
1608 tracer_print_static_fields(desc, side_arg_vec, &nr_fields);
1609
1610 if (side_unlikely(!(desc->flags & SIDE_EVENT_FLAG_VARIADIC))) {
1611 fprintf(stderr, "ERROR: unexpected non-variadic event description\n");
1612 abort();
1613 }
1614 print_attributes(", attr ", "::", var_struct->attr, var_struct->nr_attr);
1615 printf("%s", var_struct_len ? ", fields:: [ " : "");
1616 for (i = 0; i < var_struct_len; i++, nr_fields++) {
1617 printf("%s", i ? ", " : "");
1618 printf("%s:: ", var_struct->fields[i].field_name);
1619 tracer_print_dynamic(&var_struct->fields[i].elem);
1620 }
1621 if (i)
1622 printf(" ]");
1623 printf("\n");
1624 }
1625
1626 void tracer_event_notification(enum side_tracer_notification notif,
1627 struct side_event_description **events, uint32_t nr_events, void *priv)
1628 {
1629 uint32_t i;
1630 int ret;
1631
1632 printf("----------------------------------------------------------\n");
1633 printf("Tracer notified of events %s\n",
1634 notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS ? "inserted" : "removed");
1635 for (i = 0; i < nr_events; i++) {
1636 struct side_event_description *event = events[i];
1637
1638 /* Skip NULL pointers */
1639 if (!event)
1640 continue;
1641 printf("provider: %s, event: %s\n",
1642 event->provider_name, event->event_name);
1643 if (notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS) {
1644 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
1645 ret = side_tracer_callback_variadic_register(event, tracer_call_variadic, NULL);
1646 if (ret)
1647 abort();
1648 } else {
1649 ret = side_tracer_callback_register(event, tracer_call, NULL);
1650 if (ret)
1651 abort();
1652 }
1653 } else {
1654 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
1655 ret = side_tracer_callback_variadic_unregister(event, tracer_call_variadic, NULL);
1656 if (ret)
1657 abort();
1658 } else {
1659 ret = side_tracer_callback_unregister(event, tracer_call, NULL);
1660 if (ret)
1661 abort();
1662 }
1663 }
1664 }
1665 printf("----------------------------------------------------------\n");
1666 }
1667
1668 static __attribute__((constructor))
1669 void tracer_init(void);
1670 static
1671 void tracer_init(void)
1672 {
1673 tracer_handle = side_tracer_event_notification_register(tracer_event_notification, NULL);
1674 if (!tracer_handle)
1675 abort();
1676 }
1677
1678 static __attribute__((destructor))
1679 void tracer_exit(void);
1680 static
1681 void tracer_exit(void)
1682 {
1683 side_tracer_event_notification_unregister(tracer_handle);
1684 }
This page took 0.282136 seconds and 5 git commands to generate.