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