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