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