Refactoring: combine static and dynamic types
[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 *sav_desc);
26 static
27 void tracer_print_struct_sg(const struct side_type *type_desc, void *ptr);
28 static
29 void tracer_print_array(const struct side_type *type_desc, const struct side_arg_vec *sav_desc);
30 static
31 void tracer_print_vla(const struct side_type *type_desc, const struct side_arg_vec *sav_desc);
32 static
33 void tracer_print_vla_visitor(const struct side_type *type_desc, void *app_ctx);
34 static
35 void tracer_print_array_fixint(const struct side_type *type_desc, const struct side_arg *item);
36 static
37 void tracer_print_vla_fixint(const struct side_type *type_desc, const struct side_arg *item);
38 static
39 void tracer_print_dynamic(const struct side_arg *dynamic_item);
40 static
41 void tracer_print_type(const struct side_type *type_desc, const struct side_arg *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 *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 *type_desc, const struct side_arg *item)
244 {
245 const struct side_enum_mappings *mappings = type_desc->u.side_enum.mappings;
246 const struct side_type *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.side_static.integer_value.side_u8;
257 break;
258 case SIDE_TYPE_U16:
259 {
260 uint16_t v;
261
262 v = item->u.side_static.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.side_static.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.side_static.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.side_static.integer_value.side_s8;
290 break;
291 case SIDE_TYPE_S16:
292 {
293 int16_t v;
294
295 v = item->u.side_static.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.side_static.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.side_static.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 *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 *type_desc,
375 const struct side_arg *item)
376 {
377 const struct side_type *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 *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_static.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_static.side_vla->sav;
405 nr_items = item->u.side_static.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.side_static.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.side_static.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.side_static.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.side_static.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 *type_desc, const struct side_arg *item)
716 {
717 enum side_type_label 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 case SIDE_TYPE_DYNAMIC:
800 switch (item->type) {
801 case SIDE_TYPE_DYNAMIC_NULL:
802 case SIDE_TYPE_DYNAMIC_BOOL:
803 case SIDE_TYPE_DYNAMIC_U8:
804 case SIDE_TYPE_DYNAMIC_U16:
805 case SIDE_TYPE_DYNAMIC_U32:
806 case SIDE_TYPE_DYNAMIC_U64:
807 case SIDE_TYPE_DYNAMIC_S8:
808 case SIDE_TYPE_DYNAMIC_S16:
809 case SIDE_TYPE_DYNAMIC_S32:
810 case SIDE_TYPE_DYNAMIC_S64:
811 case SIDE_TYPE_DYNAMIC_BYTE:
812 case SIDE_TYPE_DYNAMIC_POINTER32:
813 case SIDE_TYPE_DYNAMIC_POINTER64:
814 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY16:
815 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY32:
816 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY64:
817 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY128:
818 case SIDE_TYPE_DYNAMIC_STRING:
819 case SIDE_TYPE_DYNAMIC_STRUCT:
820 case SIDE_TYPE_DYNAMIC_STRUCT_VISITOR:
821 case SIDE_TYPE_DYNAMIC_VLA:
822 case SIDE_TYPE_DYNAMIC_VLA_VISITOR:
823 break;
824 default:
825 fprintf(stderr, "ERROR: Unexpected dynamic type\n");
826 abort();
827 break;
828 }
829 break;
830
831 default:
832 if (type_desc->type != item->type) {
833 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
834 abort();
835 }
836 break;
837 }
838
839 if (type_desc->type == SIDE_TYPE_ENUM || type_desc->type == SIDE_TYPE_ENUM_BITMAP)
840 type = type_desc->type;
841 else
842 type = item->type;
843
844 printf("{ ");
845 switch (type) {
846 case SIDE_TYPE_NULL:
847 tracer_print_type_header(":", type_desc->u.side_null.attr, type_desc->u.side_null.nr_attr);
848 printf("<NULL TYPE>");
849 break;
850
851 case SIDE_TYPE_BOOL:
852 tracer_print_type_header(":", type_desc->u.side_bool.attr, type_desc->u.side_bool.nr_attr);
853 printf("%s", item->u.side_static.bool_value ? "true" : "false");
854 break;
855
856 case SIDE_TYPE_U8:
857 case SIDE_TYPE_U16:
858 case SIDE_TYPE_U32:
859 case SIDE_TYPE_U64:
860 case SIDE_TYPE_S8:
861 case SIDE_TYPE_S16:
862 case SIDE_TYPE_S32:
863 case SIDE_TYPE_S64:
864 tracer_print_type_integer(":", &type_desc->u.side_integer, &item->u.side_static.integer_value, 0,
865 TRACER_DISPLAY_BASE_10);
866 break;
867
868 case SIDE_TYPE_POINTER32:
869 case SIDE_TYPE_POINTER64:
870 tracer_print_type_integer(":", &type_desc->u.side_integer, &item->u.side_static.integer_value, 0,
871 TRACER_DISPLAY_BASE_16);
872 break;
873
874 case SIDE_TYPE_BYTE:
875 tracer_print_type_header(":", type_desc->u.side_byte.attr, type_desc->u.side_byte.nr_attr);
876 printf("0x%" PRIx8, item->u.side_static.byte_value);
877 break;
878
879 case SIDE_TYPE_ENUM:
880 print_enum(type_desc, item);
881 break;
882
883 case SIDE_TYPE_ENUM_BITMAP:
884 print_enum_bitmap(type_desc, item);
885 break;
886
887 case SIDE_TYPE_FLOAT_BINARY16:
888 case SIDE_TYPE_FLOAT_BINARY32:
889 case SIDE_TYPE_FLOAT_BINARY64:
890 case SIDE_TYPE_FLOAT_BINARY128:
891 tracer_print_type_float(":", &type_desc->u.side_float, &item->u.side_static.float_value);
892 break;
893
894 case SIDE_TYPE_STRING:
895 tracer_print_type_header(":", type_desc->u.side_string.attr, type_desc->u.side_string.nr_attr);
896 printf("\"%s\"", (const char *)(uintptr_t) item->u.side_static.string_value);
897 break;
898 case SIDE_TYPE_STRUCT:
899 tracer_print_struct(type_desc, item->u.side_static.side_struct);
900 break;
901 case SIDE_TYPE_STRUCT_SG:
902 tracer_print_struct_sg(type_desc, item->u.side_static.side_struct_sg_ptr);
903 break;
904 case SIDE_TYPE_ARRAY:
905 tracer_print_array(type_desc, item->u.side_static.side_array);
906 break;
907 case SIDE_TYPE_VLA:
908 tracer_print_vla(type_desc, item->u.side_static.side_vla);
909 break;
910 case SIDE_TYPE_VLA_VISITOR:
911 tracer_print_vla_visitor(type_desc, item->u.side_static.side_vla_app_visitor_ctx);
912 break;
913 case SIDE_TYPE_ARRAY_U8:
914 case SIDE_TYPE_ARRAY_U16:
915 case SIDE_TYPE_ARRAY_U32:
916 case SIDE_TYPE_ARRAY_U64:
917 case SIDE_TYPE_ARRAY_S8:
918 case SIDE_TYPE_ARRAY_S16:
919 case SIDE_TYPE_ARRAY_S32:
920 case SIDE_TYPE_ARRAY_S64:
921 case SIDE_TYPE_ARRAY_BYTE:
922 case SIDE_TYPE_ARRAY_POINTER32:
923 case SIDE_TYPE_ARRAY_POINTER64:
924 tracer_print_array_fixint(type_desc, item);
925 break;
926 case SIDE_TYPE_VLA_U8:
927 case SIDE_TYPE_VLA_U16:
928 case SIDE_TYPE_VLA_U32:
929 case SIDE_TYPE_VLA_U64:
930 case SIDE_TYPE_VLA_S8:
931 case SIDE_TYPE_VLA_S16:
932 case SIDE_TYPE_VLA_S32:
933 case SIDE_TYPE_VLA_S64:
934 case SIDE_TYPE_VLA_BYTE:
935 case SIDE_TYPE_VLA_POINTER32:
936 case SIDE_TYPE_VLA_POINTER64:
937 tracer_print_vla_fixint(type_desc, item);
938 break;
939
940 /* Dynamic types */
941 case SIDE_TYPE_DYNAMIC_NULL:
942 case SIDE_TYPE_DYNAMIC_BOOL:
943 case SIDE_TYPE_DYNAMIC_U8:
944 case SIDE_TYPE_DYNAMIC_U16:
945 case SIDE_TYPE_DYNAMIC_U32:
946 case SIDE_TYPE_DYNAMIC_U64:
947 case SIDE_TYPE_DYNAMIC_S8:
948 case SIDE_TYPE_DYNAMIC_S16:
949 case SIDE_TYPE_DYNAMIC_S32:
950 case SIDE_TYPE_DYNAMIC_S64:
951 case SIDE_TYPE_DYNAMIC_BYTE:
952 case SIDE_TYPE_DYNAMIC_POINTER32:
953 case SIDE_TYPE_DYNAMIC_POINTER64:
954 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY16:
955 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY32:
956 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY64:
957 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY128:
958 case SIDE_TYPE_DYNAMIC_STRING:
959 case SIDE_TYPE_DYNAMIC_STRUCT:
960 case SIDE_TYPE_DYNAMIC_STRUCT_VISITOR:
961 case SIDE_TYPE_DYNAMIC_VLA:
962 case SIDE_TYPE_DYNAMIC_VLA_VISITOR:
963 tracer_print_dynamic(item);
964 break;
965 default:
966 fprintf(stderr, "<UNKNOWN TYPE>");
967 abort();
968 }
969 printf(" }");
970 }
971
972 static
973 void tracer_print_field(const struct side_event_field *item_desc, const struct side_arg *item)
974 {
975 printf("%s: ", item_desc->field_name);
976 tracer_print_type(&item_desc->side_type, item);
977 }
978
979 static
980 void tracer_print_struct(const struct side_type *type_desc, const struct side_arg_vec *sav_desc)
981 {
982 const struct side_arg *sav = sav_desc->sav;
983 uint32_t side_sav_len = sav_desc->len;
984 int i;
985
986 if (type_desc->u.side_struct->nr_fields != side_sav_len) {
987 fprintf(stderr, "ERROR: number of fields mismatch between description and arguments of structure\n");
988 abort();
989 }
990 print_attributes("attr", ":", type_desc->u.side_struct->attr, type_desc->u.side_struct->nr_attr);
991 printf("%s", type_desc->u.side_struct->nr_attr ? ", " : "");
992 printf("fields: { ");
993 for (i = 0; i < side_sav_len; i++) {
994 printf("%s", i ? ", " : "");
995 tracer_print_field(&type_desc->u.side_struct->fields[i], &sav[i]);
996 }
997 printf(" }");
998 }
999
1000 static
1001 void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *_ptr)
1002 {
1003 const char *ptr = (const char *) _ptr;
1004 union side_integer_value value;
1005
1006 printf("{ ");
1007 switch (sg_type->type) {
1008 case SIDE_TYPE_SG_NULL:
1009 tracer_print_type_header(":", sg_type->u.side_null.attr, sg_type->u.side_null.nr_attr);
1010 printf("<NULL TYPE>");
1011 break;
1012
1013 case SIDE_TYPE_SG_UNSIGNED_INT:
1014 case SIDE_TYPE_SG_SIGNED_INT:
1015 switch (sg_type->u.side_integer.type.integer_size_bits) {
1016 case 8:
1017 case 16:
1018 case 32:
1019 case 64:
1020 break;
1021 default:
1022 abort();
1023 }
1024 memcpy(&value, ptr + sg_type->offset, sg_type->u.side_integer.type.integer_size_bits >> 3);
1025 tracer_print_type_integer(":", &sg_type->u.side_integer.type, &value,
1026 sg_type->u.side_integer.offset_bits, TRACER_DISPLAY_BASE_10);
1027 break;
1028 default:
1029 fprintf(stderr, "<UNKNOWN TYPE>");
1030 abort();
1031 }
1032 printf(" }");
1033 }
1034
1035 static
1036 void tracer_print_sg_field(const struct side_struct_field_sg *field_sg, void *ptr)
1037 {
1038 printf("%s: ", field_sg->field_name);
1039 tracer_print_sg_type(&field_sg->side_type, ptr);
1040 }
1041
1042 static
1043 void tracer_print_struct_sg(const struct side_type *type_desc, void *ptr)
1044 {
1045 const struct side_type_struct_sg *struct_sg = type_desc->u.side_struct_sg;
1046 int i;
1047
1048 print_attributes("attr", ":", struct_sg->attr, struct_sg->nr_attr);
1049 printf("%s", struct_sg->nr_attr ? ", " : "");
1050 printf("fields: { ");
1051 for (i = 0; i < struct_sg->nr_fields; i++) {
1052 printf("%s", i ? ", " : "");
1053 tracer_print_sg_field(&struct_sg->fields_sg[i], ptr);
1054 }
1055 printf(" }");
1056 }
1057
1058 static
1059 void tracer_print_array(const struct side_type *type_desc, const struct side_arg_vec *sav_desc)
1060 {
1061 const struct side_arg *sav = sav_desc->sav;
1062 uint32_t side_sav_len = sav_desc->len;
1063 int i;
1064
1065 if (type_desc->u.side_array.length != side_sav_len) {
1066 fprintf(stderr, "ERROR: length mismatch between description and arguments of array\n");
1067 abort();
1068 }
1069 print_attributes("attr", ":", type_desc->u.side_array.attr, type_desc->u.side_array.nr_attr);
1070 printf("%s", type_desc->u.side_array.nr_attr ? ", " : "");
1071 printf("elements: ");
1072 printf("[ ");
1073 for (i = 0; i < side_sav_len; i++) {
1074 printf("%s", i ? ", " : "");
1075 tracer_print_type(type_desc->u.side_array.elem_type, &sav[i]);
1076 }
1077 printf(" ]");
1078 }
1079
1080 static
1081 void tracer_print_vla(const struct side_type *type_desc, const struct side_arg_vec *sav_desc)
1082 {
1083 const struct side_arg *sav = sav_desc->sav;
1084 uint32_t side_sav_len = sav_desc->len;
1085 int i;
1086
1087 print_attributes("attr", ":", type_desc->u.side_vla.attr, type_desc->u.side_vla.nr_attr);
1088 printf("%s", type_desc->u.side_vla.nr_attr ? ", " : "");
1089 printf("elements: ");
1090 printf("[ ");
1091 for (i = 0; i < side_sav_len; i++) {
1092 printf("%s", i ? ", " : "");
1093 tracer_print_type(type_desc->u.side_vla.elem_type, &sav[i]);
1094 }
1095 printf(" ]");
1096 }
1097
1098 struct tracer_visitor_priv {
1099 const struct side_type *elem_type;
1100 int i;
1101 };
1102
1103 static
1104 enum side_visitor_status tracer_write_elem_cb(const struct side_tracer_visitor_ctx *tracer_ctx,
1105 const struct side_arg *elem)
1106 {
1107 struct tracer_visitor_priv *tracer_priv = tracer_ctx->priv;
1108
1109 printf("%s", tracer_priv->i++ ? ", " : "");
1110 tracer_print_type(tracer_priv->elem_type, elem);
1111 return SIDE_VISITOR_STATUS_OK;
1112 }
1113
1114 static
1115 void tracer_print_vla_visitor(const struct side_type *type_desc, void *app_ctx)
1116 {
1117 enum side_visitor_status status;
1118 struct tracer_visitor_priv tracer_priv = {
1119 .elem_type = type_desc->u.side_vla_visitor.elem_type,
1120 .i = 0,
1121 };
1122 const struct side_tracer_visitor_ctx tracer_ctx = {
1123 .write_elem = tracer_write_elem_cb,
1124 .priv = &tracer_priv,
1125 };
1126
1127 print_attributes("attr", ":", type_desc->u.side_vla_visitor.attr, type_desc->u.side_vla_visitor.nr_attr);
1128 printf("%s", type_desc->u.side_vla_visitor.nr_attr ? ", " : "");
1129 printf("elements: ");
1130 printf("[ ");
1131 status = type_desc->u.side_vla_visitor.visitor(&tracer_ctx, app_ctx);
1132 switch (status) {
1133 case SIDE_VISITOR_STATUS_OK:
1134 break;
1135 case SIDE_VISITOR_STATUS_ERROR:
1136 fprintf(stderr, "ERROR: Visitor error\n");
1137 abort();
1138 }
1139 printf(" ]");
1140 }
1141
1142 void tracer_print_array_fixint(const struct side_type *type_desc, const struct side_arg *item)
1143 {
1144 const struct side_type *elem_type = type_desc->u.side_array.elem_type;
1145 uint32_t side_sav_len = type_desc->u.side_array.length;
1146 void *p = item->u.side_static.side_array_fixint;
1147 enum side_type_label side_type;
1148 int i;
1149
1150 print_attributes("attr", ":", type_desc->u.side_array.attr, type_desc->u.side_array.nr_attr);
1151 printf("%s", type_desc->u.side_array.nr_attr ? ", " : "");
1152 printf("elements: ");
1153 switch (item->type) {
1154 case SIDE_TYPE_ARRAY_U8:
1155 if (elem_type->type != SIDE_TYPE_U8)
1156 goto type_error;
1157 break;
1158 case SIDE_TYPE_ARRAY_U16:
1159 if (elem_type->type != SIDE_TYPE_U16)
1160 goto type_error;
1161 break;
1162 case SIDE_TYPE_ARRAY_U32:
1163 if (elem_type->type != SIDE_TYPE_U32)
1164 goto type_error;
1165 break;
1166 case SIDE_TYPE_ARRAY_U64:
1167 if (elem_type->type != SIDE_TYPE_U64)
1168 goto type_error;
1169 break;
1170 case SIDE_TYPE_ARRAY_S8:
1171 if (elem_type->type != SIDE_TYPE_S8)
1172 goto type_error;
1173 break;
1174 case SIDE_TYPE_ARRAY_S16:
1175 if (elem_type->type != SIDE_TYPE_S16)
1176 goto type_error;
1177 break;
1178 case SIDE_TYPE_ARRAY_S32:
1179 if (elem_type->type != SIDE_TYPE_S32)
1180 goto type_error;
1181 break;
1182 case SIDE_TYPE_ARRAY_S64:
1183 if (elem_type->type != SIDE_TYPE_S64)
1184 goto type_error;
1185 break;
1186 case SIDE_TYPE_ARRAY_BYTE:
1187 if (elem_type->type != SIDE_TYPE_BYTE)
1188 goto type_error;
1189 break;
1190 case SIDE_TYPE_ARRAY_POINTER32:
1191 if (elem_type->type != SIDE_TYPE_POINTER32)
1192 goto type_error;
1193 case SIDE_TYPE_ARRAY_POINTER64:
1194 if (elem_type->type != SIDE_TYPE_POINTER64)
1195 goto type_error;
1196 break;
1197 default:
1198 goto type_error;
1199 }
1200 side_type = elem_type->type;
1201
1202 printf("[ ");
1203 for (i = 0; i < side_sav_len; i++) {
1204 struct side_arg sav_elem = {
1205 .type = side_type,
1206 };
1207
1208 switch (side_type) {
1209 case SIDE_TYPE_U8:
1210 sav_elem.u.side_static.integer_value.side_u8 = ((const uint8_t *) p)[i];
1211 break;
1212 case SIDE_TYPE_S8:
1213 sav_elem.u.side_static.integer_value.side_s8 = ((const int8_t *) p)[i];
1214 break;
1215 case SIDE_TYPE_U16:
1216 sav_elem.u.side_static.integer_value.side_u16 = ((const uint16_t *) p)[i];
1217 break;
1218 case SIDE_TYPE_S16:
1219 sav_elem.u.side_static.integer_value.side_s16 = ((const int16_t *) p)[i];
1220 break;
1221 case SIDE_TYPE_U32:
1222 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1223 break;
1224 case SIDE_TYPE_S32:
1225 sav_elem.u.side_static.integer_value.side_s32 = ((const int32_t *) p)[i];
1226 break;
1227 case SIDE_TYPE_U64:
1228 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1229 break;
1230 case SIDE_TYPE_S64:
1231 sav_elem.u.side_static.integer_value.side_s64 = ((const int64_t *) p)[i];
1232 break;
1233 case SIDE_TYPE_BYTE:
1234 sav_elem.u.side_static.byte_value = ((const uint8_t *) p)[i];
1235 break;
1236 case SIDE_TYPE_POINTER32:
1237 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1238 break;
1239 case SIDE_TYPE_POINTER64:
1240 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1241 break;
1242
1243 default:
1244 fprintf(stderr, "ERROR: Unexpected type\n");
1245 abort();
1246 }
1247
1248 printf("%s", i ? ", " : "");
1249 tracer_print_type(elem_type, &sav_elem);
1250 }
1251 printf(" ]");
1252 return;
1253
1254 type_error:
1255 fprintf(stderr, "ERROR: type mismatch\n");
1256 abort();
1257 }
1258
1259 void tracer_print_vla_fixint(const struct side_type *type_desc, const struct side_arg *item)
1260 {
1261 const struct side_type *elem_type = type_desc->u.side_vla.elem_type;
1262 uint32_t side_sav_len = item->u.side_static.side_vla_fixint.length;
1263 void *p = item->u.side_static.side_vla_fixint.p;
1264 enum side_type_label side_type;
1265 int i;
1266
1267 print_attributes("attr", ":", type_desc->u.side_vla.attr, type_desc->u.side_vla.nr_attr);
1268 printf("%s", type_desc->u.side_vla.nr_attr ? ", " : "");
1269 printf("elements: ");
1270 switch (item->type) {
1271 case SIDE_TYPE_VLA_U8:
1272 if (elem_type->type != SIDE_TYPE_U8)
1273 goto type_error;
1274 break;
1275 case SIDE_TYPE_VLA_U16:
1276 if (elem_type->type != SIDE_TYPE_U16)
1277 goto type_error;
1278 break;
1279 case SIDE_TYPE_VLA_U32:
1280 if (elem_type->type != SIDE_TYPE_U32)
1281 goto type_error;
1282 break;
1283 case SIDE_TYPE_VLA_U64:
1284 if (elem_type->type != SIDE_TYPE_U64)
1285 goto type_error;
1286 break;
1287 case SIDE_TYPE_VLA_S8:
1288 if (elem_type->type != SIDE_TYPE_S8)
1289 goto type_error;
1290 break;
1291 case SIDE_TYPE_VLA_S16:
1292 if (elem_type->type != SIDE_TYPE_S16)
1293 goto type_error;
1294 break;
1295 case SIDE_TYPE_VLA_S32:
1296 if (elem_type->type != SIDE_TYPE_S32)
1297 goto type_error;
1298 break;
1299 case SIDE_TYPE_VLA_S64:
1300 if (elem_type->type != SIDE_TYPE_S64)
1301 goto type_error;
1302 break;
1303 case SIDE_TYPE_VLA_BYTE:
1304 if (elem_type->type != SIDE_TYPE_BYTE)
1305 goto type_error;
1306 break;
1307 case SIDE_TYPE_VLA_POINTER32:
1308 if (elem_type->type != SIDE_TYPE_POINTER32)
1309 goto type_error;
1310 case SIDE_TYPE_VLA_POINTER64:
1311 if (elem_type->type != SIDE_TYPE_POINTER64)
1312 goto type_error;
1313 break;
1314 default:
1315 goto type_error;
1316 }
1317 side_type = elem_type->type;
1318
1319 printf("[ ");
1320 for (i = 0; i < side_sav_len; i++) {
1321 struct side_arg sav_elem = {
1322 .type = side_type,
1323 };
1324
1325 switch (side_type) {
1326 case SIDE_TYPE_U8:
1327 sav_elem.u.side_static.integer_value.side_u8 = ((const uint8_t *) p)[i];
1328 break;
1329 case SIDE_TYPE_S8:
1330 sav_elem.u.side_static.integer_value.side_s8 = ((const int8_t *) p)[i];
1331 break;
1332 case SIDE_TYPE_U16:
1333 sav_elem.u.side_static.integer_value.side_u16 = ((const uint16_t *) p)[i];
1334 break;
1335 case SIDE_TYPE_S16:
1336 sav_elem.u.side_static.integer_value.side_s16 = ((const int16_t *) p)[i];
1337 break;
1338 case SIDE_TYPE_U32:
1339 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1340 break;
1341 case SIDE_TYPE_S32:
1342 sav_elem.u.side_static.integer_value.side_s32 = ((const int32_t *) p)[i];
1343 break;
1344 case SIDE_TYPE_U64:
1345 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1346 break;
1347 case SIDE_TYPE_S64:
1348 sav_elem.u.side_static.integer_value.side_s64 = ((const int64_t *) p)[i];
1349 break;
1350 case SIDE_TYPE_BYTE:
1351 sav_elem.u.side_static.byte_value = ((const uint8_t *) p)[i];
1352 break;
1353 case SIDE_TYPE_POINTER32:
1354 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1355 break;
1356 case SIDE_TYPE_POINTER64:
1357 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1358 break;
1359
1360 default:
1361 fprintf(stderr, "ERROR: Unexpected type\n");
1362 abort();
1363 }
1364
1365 printf("%s", i ? ", " : "");
1366 tracer_print_type(elem_type, &sav_elem);
1367 }
1368 printf(" ]");
1369 return;
1370
1371 type_error:
1372 fprintf(stderr, "ERROR: type mismatch\n");
1373 abort();
1374 }
1375
1376 static
1377 void tracer_print_dynamic_struct(const struct side_arg_dynamic_event_struct *dynamic_struct)
1378 {
1379 const struct side_arg_dynamic_event_field *fields = dynamic_struct->fields;
1380 uint32_t len = dynamic_struct->len;
1381 int i;
1382
1383 print_attributes("attr", "::", dynamic_struct->attr, dynamic_struct->nr_attr);
1384 printf("%s", dynamic_struct->nr_attr ? ", " : "");
1385 printf("fields:: ");
1386 printf("[ ");
1387 for (i = 0; i < len; i++) {
1388 printf("%s", i ? ", " : "");
1389 printf("%s:: ", fields[i].field_name);
1390 tracer_print_dynamic(&fields[i].elem);
1391 }
1392 printf(" ]");
1393 }
1394
1395 struct tracer_dynamic_struct_visitor_priv {
1396 int i;
1397 };
1398
1399 static
1400 enum side_visitor_status tracer_dynamic_struct_write_elem_cb(
1401 const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
1402 const struct side_arg_dynamic_event_field *dynamic_field)
1403 {
1404 struct tracer_dynamic_struct_visitor_priv *tracer_priv = tracer_ctx->priv;
1405
1406 printf("%s", tracer_priv->i++ ? ", " : "");
1407 printf("%s:: ", dynamic_field->field_name);
1408 tracer_print_dynamic(&dynamic_field->elem);
1409 return SIDE_VISITOR_STATUS_OK;
1410 }
1411
1412 static
1413 void tracer_print_dynamic_struct_visitor(const struct side_arg *item)
1414 {
1415 enum side_visitor_status status;
1416 struct tracer_dynamic_struct_visitor_priv tracer_priv = {
1417 .i = 0,
1418 };
1419 const struct side_tracer_dynamic_struct_visitor_ctx tracer_ctx = {
1420 .write_field = tracer_dynamic_struct_write_elem_cb,
1421 .priv = &tracer_priv,
1422 };
1423 void *app_ctx = item->u.side_dynamic.side_dynamic_struct_visitor.app_ctx;
1424
1425 print_attributes("attr", "::", item->u.side_dynamic.side_dynamic_struct_visitor.attr, item->u.side_dynamic.side_dynamic_struct_visitor.nr_attr);
1426 printf("%s", item->u.side_dynamic.side_dynamic_struct_visitor.nr_attr ? ", " : "");
1427 printf("fields:: ");
1428 printf("[ ");
1429 status = item->u.side_dynamic.side_dynamic_struct_visitor.visitor(&tracer_ctx, app_ctx);
1430 switch (status) {
1431 case SIDE_VISITOR_STATUS_OK:
1432 break;
1433 case SIDE_VISITOR_STATUS_ERROR:
1434 fprintf(stderr, "ERROR: Visitor error\n");
1435 abort();
1436 }
1437 printf(" ]");
1438 }
1439
1440 static
1441 void tracer_print_dynamic_vla(const struct side_arg_dynamic_vla *vla)
1442 {
1443 const struct side_arg *sav = vla->sav;
1444 uint32_t side_sav_len = vla->len;
1445 int i;
1446
1447 print_attributes("attr", "::", vla->attr, vla->nr_attr);
1448 printf("%s", vla->nr_attr ? ", " : "");
1449 printf("elements:: ");
1450 printf("[ ");
1451 for (i = 0; i < side_sav_len; i++) {
1452 printf("%s", i ? ", " : "");
1453 tracer_print_dynamic(&sav[i]);
1454 }
1455 printf(" ]");
1456 }
1457
1458 struct tracer_dynamic_vla_visitor_priv {
1459 int i;
1460 };
1461
1462 static
1463 enum side_visitor_status tracer_dynamic_vla_write_elem_cb(
1464 const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx,
1465 const struct side_arg *elem)
1466 {
1467 struct tracer_dynamic_vla_visitor_priv *tracer_priv = tracer_ctx->priv;
1468
1469 printf("%s", tracer_priv->i++ ? ", " : "");
1470 tracer_print_dynamic(elem);
1471 return SIDE_VISITOR_STATUS_OK;
1472 }
1473
1474 static
1475 void tracer_print_dynamic_vla_visitor(const struct side_arg *item)
1476 {
1477 enum side_visitor_status status;
1478 struct tracer_dynamic_vla_visitor_priv tracer_priv = {
1479 .i = 0,
1480 };
1481 const struct side_tracer_dynamic_vla_visitor_ctx tracer_ctx = {
1482 .write_elem = tracer_dynamic_vla_write_elem_cb,
1483 .priv = &tracer_priv,
1484 };
1485 void *app_ctx = item->u.side_dynamic.side_dynamic_vla_visitor.app_ctx;
1486
1487 print_attributes("attr", "::", item->u.side_dynamic.side_dynamic_vla_visitor.attr, item->u.side_dynamic.side_dynamic_vla_visitor.nr_attr);
1488 printf("%s", item->u.side_dynamic.side_dynamic_vla_visitor.nr_attr ? ", " : "");
1489 printf("elements:: ");
1490 printf("[ ");
1491 status = item->u.side_dynamic.side_dynamic_vla_visitor.visitor(&tracer_ctx, app_ctx);
1492 switch (status) {
1493 case SIDE_VISITOR_STATUS_OK:
1494 break;
1495 case SIDE_VISITOR_STATUS_ERROR:
1496 fprintf(stderr, "ERROR: Visitor error\n");
1497 abort();
1498 }
1499 printf(" ]");
1500 }
1501
1502 static
1503 void tracer_print_dynamic(const struct side_arg *item)
1504 {
1505 printf("{ ");
1506 switch (item->type) {
1507 case SIDE_TYPE_DYNAMIC_NULL:
1508 tracer_print_type_header("::", item->u.side_dynamic.side_null.attr, item->u.side_dynamic.side_null.nr_attr);
1509 printf("<NULL TYPE>");
1510 break;
1511 case SIDE_TYPE_DYNAMIC_BOOL:
1512 tracer_print_type_header("::", item->u.side_dynamic.side_bool.type.attr, item->u.side_dynamic.side_bool.type.nr_attr);
1513 printf("%s", item->u.side_dynamic.side_bool.value ? "true" : "false");
1514 break;
1515 case SIDE_TYPE_DYNAMIC_U8:
1516 case SIDE_TYPE_DYNAMIC_U16:
1517 case SIDE_TYPE_DYNAMIC_U32:
1518 case SIDE_TYPE_DYNAMIC_U64:
1519 case SIDE_TYPE_DYNAMIC_S8:
1520 case SIDE_TYPE_DYNAMIC_S16:
1521 case SIDE_TYPE_DYNAMIC_S32:
1522 case SIDE_TYPE_DYNAMIC_S64:
1523 tracer_print_type_integer("::", &item->u.side_dynamic.side_integer.type, &item->u.side_dynamic.side_integer.value, 0,
1524 TRACER_DISPLAY_BASE_10);
1525 break;
1526 case SIDE_TYPE_DYNAMIC_BYTE:
1527 tracer_print_type_header("::", item->u.side_dynamic.side_byte.type.attr, item->u.side_dynamic.side_byte.type.nr_attr);
1528 printf("0x%" PRIx8, item->u.side_dynamic.side_byte.value);
1529 break;
1530
1531 case SIDE_TYPE_DYNAMIC_POINTER32:
1532 case SIDE_TYPE_DYNAMIC_POINTER64:
1533 tracer_print_type_integer("::", &item->u.side_dynamic.side_integer.type, &item->u.side_dynamic.side_integer.value, 0,
1534 TRACER_DISPLAY_BASE_16);
1535 break;
1536
1537 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY16:
1538 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY32:
1539 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY64:
1540 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY128:
1541 tracer_print_type_float("::", &item->u.side_dynamic.side_float.type,
1542 &item->u.side_dynamic.side_float.value);
1543 break;
1544
1545 case SIDE_TYPE_DYNAMIC_STRING:
1546 tracer_print_type_header("::", item->u.side_dynamic.side_string.type.attr, item->u.side_dynamic.side_string.type.nr_attr);
1547 printf("\"%s\"", (const char *)(uintptr_t) item->u.side_dynamic.side_string.value);
1548 break;
1549 case SIDE_TYPE_DYNAMIC_STRUCT:
1550 tracer_print_dynamic_struct(item->u.side_dynamic.side_dynamic_struct);
1551 break;
1552 case SIDE_TYPE_DYNAMIC_STRUCT_VISITOR:
1553 tracer_print_dynamic_struct_visitor(item);
1554 break;
1555 case SIDE_TYPE_DYNAMIC_VLA:
1556 tracer_print_dynamic_vla(item->u.side_dynamic.side_dynamic_vla);
1557 break;
1558 case SIDE_TYPE_DYNAMIC_VLA_VISITOR:
1559 tracer_print_dynamic_vla_visitor(item);
1560 break;
1561 default:
1562 fprintf(stderr, "<UNKNOWN TYPE>");
1563 abort();
1564 }
1565 printf(" }");
1566 }
1567
1568 static
1569 void tracer_print_static_fields(const struct side_event_description *desc,
1570 const struct side_arg_vec *sav_desc,
1571 int *nr_items)
1572 {
1573 const struct side_arg *sav = sav_desc->sav;
1574 uint32_t side_sav_len = sav_desc->len;
1575 int i;
1576
1577 printf("provider: %s, event: %s", desc->provider_name, desc->event_name);
1578 if (desc->nr_fields != side_sav_len) {
1579 fprintf(stderr, "ERROR: number of fields mismatch between description and arguments\n");
1580 abort();
1581 }
1582 print_attributes(", attr", ":", desc->attr, desc->nr_attr);
1583 printf("%s", side_sav_len ? ", fields: [ " : "");
1584 for (i = 0; i < side_sav_len; i++) {
1585 printf("%s", i ? ", " : "");
1586 tracer_print_field(&desc->fields[i], &sav[i]);
1587 }
1588 if (nr_items)
1589 *nr_items = i;
1590 if (side_sav_len)
1591 printf(" ]");
1592 }
1593
1594 void tracer_call(const struct side_event_description *desc,
1595 const struct side_arg_vec *sav_desc,
1596 void *priv __attribute__((unused)))
1597 {
1598 int nr_fields = 0;
1599
1600 tracer_print_static_fields(desc, sav_desc, &nr_fields);
1601 printf("\n");
1602 }
1603
1604 void tracer_call_variadic(const struct side_event_description *desc,
1605 const struct side_arg_vec *sav_desc,
1606 const struct side_arg_dynamic_event_struct *var_struct,
1607 void *priv __attribute__((unused)))
1608 {
1609 uint32_t var_struct_len = var_struct->len;
1610 int nr_fields = 0, i;
1611
1612 tracer_print_static_fields(desc, sav_desc, &nr_fields);
1613
1614 if (side_unlikely(!(desc->flags & SIDE_EVENT_FLAG_VARIADIC))) {
1615 fprintf(stderr, "ERROR: unexpected non-variadic event description\n");
1616 abort();
1617 }
1618 print_attributes(", attr ", "::", var_struct->attr, var_struct->nr_attr);
1619 printf("%s", var_struct_len ? ", fields:: [ " : "");
1620 for (i = 0; i < var_struct_len; i++, nr_fields++) {
1621 printf("%s", i ? ", " : "");
1622 printf("%s:: ", var_struct->fields[i].field_name);
1623 tracer_print_dynamic(&var_struct->fields[i].elem);
1624 }
1625 if (i)
1626 printf(" ]");
1627 printf("\n");
1628 }
1629
1630 void tracer_event_notification(enum side_tracer_notification notif,
1631 struct side_event_description **events, uint32_t nr_events, void *priv)
1632 {
1633 uint32_t i;
1634 int ret;
1635
1636 printf("----------------------------------------------------------\n");
1637 printf("Tracer notified of events %s\n",
1638 notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS ? "inserted" : "removed");
1639 for (i = 0; i < nr_events; i++) {
1640 struct side_event_description *event = events[i];
1641
1642 /* Skip NULL pointers */
1643 if (!event)
1644 continue;
1645 printf("provider: %s, event: %s\n",
1646 event->provider_name, event->event_name);
1647 if (notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS) {
1648 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
1649 ret = side_tracer_callback_variadic_register(event, tracer_call_variadic, NULL);
1650 if (ret)
1651 abort();
1652 } else {
1653 ret = side_tracer_callback_register(event, tracer_call, NULL);
1654 if (ret)
1655 abort();
1656 }
1657 } else {
1658 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
1659 ret = side_tracer_callback_variadic_unregister(event, tracer_call_variadic, NULL);
1660 if (ret)
1661 abort();
1662 } else {
1663 ret = side_tracer_callback_unregister(event, tracer_call, NULL);
1664 if (ret)
1665 abort();
1666 }
1667 }
1668 }
1669 printf("----------------------------------------------------------\n");
1670 }
1671
1672 static __attribute__((constructor))
1673 void tracer_init(void);
1674 static
1675 void tracer_init(void)
1676 {
1677 tracer_handle = side_tracer_event_notification_register(tracer_event_notification, NULL);
1678 if (!tracer_handle)
1679 abort();
1680 }
1681
1682 static __attribute__((destructor))
1683 void tracer_exit(void);
1684 static
1685 void tracer_exit(void)
1686 {
1687 side_tracer_event_notification_unregister(tracer_handle);
1688 }
This page took 0.103479 seconds and 4 git commands to generate.