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