b071ee0fbc569091f55e442a0c903c3c1ac53e45
[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 uint32_t tracer_print_sg_integer_type(const struct side_type_sg *type_sg, const void *_ptr)
1054 {
1055 const char *ptr = (const char *) _ptr;
1056 union side_integer_value value;
1057 uint32_t integer_size_bytes = type_sg->u.side_integer.type.integer_size_bits >> 3;
1058
1059 switch (type_sg->u.side_integer.type.integer_size_bits) {
1060 case 8:
1061 case 16:
1062 case 32:
1063 case 64:
1064 break;
1065 default:
1066 abort();
1067 }
1068 memcpy(&value, ptr + type_sg->offset, integer_size_bytes);
1069 tracer_print_type_integer(":", &type_sg->u.side_integer.type, &value,
1070 type_sg->u.side_integer.offset_bits, TRACER_DISPLAY_BASE_10);
1071 return integer_size_bytes;
1072 }
1073
1074 static
1075 uint32_t tracer_print_sg_float_type(const struct side_type_sg *type_sg, const void *_ptr)
1076 {
1077 const char *ptr = (const char *) _ptr;
1078 union side_float_value value;
1079 uint32_t float_size_bytes = type_sg->u.side_float.float_size_bits >> 3;
1080
1081 switch (type_sg->u.side_float.float_size_bits) {
1082 case 16:
1083 case 32:
1084 case 64:
1085 case 128:
1086 break;
1087 default:
1088 abort();
1089 }
1090 memcpy(&value, ptr + type_sg->offset, float_size_bytes);
1091 tracer_print_type_float(":", &type_sg->u.side_float, &value);
1092 return float_size_bytes;
1093 }
1094
1095 static
1096 uint32_t tracer_print_sg_type(const struct side_type *type_desc, void *ptr)
1097 {
1098 uint32_t len;
1099
1100 printf("{ ");
1101 switch (type_desc->type) {
1102 case SIDE_TYPE_SG_UNSIGNED_INT:
1103 case SIDE_TYPE_SG_SIGNED_INT:
1104 len = tracer_print_sg_integer_type(&type_desc->u.side_sg, ptr);
1105 break;
1106 case SIDE_TYPE_SG_FLOAT:
1107 len = tracer_print_sg_float_type(&type_desc->u.side_sg, ptr);
1108 break;
1109 case SIDE_TYPE_SG_STRUCT:
1110 len = tracer_print_sg_struct(&type_desc->u.side_sg, ptr);
1111 break;
1112 case SIDE_TYPE_SG_ARRAY:
1113 len = tracer_print_sg_array(&type_desc->u.side_sg, ptr);
1114 break;
1115 default:
1116 fprintf(stderr, "<UNKNOWN SCATTER-GATHER TYPE>");
1117 abort();
1118 }
1119 printf(" }");
1120 return len;
1121 }
1122
1123 static
1124 void tracer_print_sg_field(const struct side_event_field *field, void *ptr)
1125 {
1126 printf("%s: ", field->field_name);
1127 (void) tracer_print_sg_type(&field->side_type, ptr);
1128 }
1129
1130 static
1131 uint32_t tracer_print_sg_struct(const struct side_type_sg *type_sg, const void *_ptr)
1132 {
1133 char *ptr = (char *) _ptr;
1134 uint32_t i;
1135
1136 memcpy(&ptr, ptr + type_sg->offset, sizeof(ptr));
1137 print_attributes("attr", ":", type_sg->u.side_struct->attr, type_sg->u.side_struct->nr_attr);
1138 printf("%s", type_sg->u.side_struct->nr_attr ? ", " : "");
1139 printf("fields: { ");
1140 for (i = 0; i < type_sg->u.side_struct->nr_fields; i++) {
1141 printf("%s", i ? ", " : "");
1142 tracer_print_sg_field(&type_sg->u.side_struct->fields[i], ptr);
1143 }
1144 printf(" }");
1145 return sizeof(void *);
1146 }
1147
1148 static
1149 uint32_t tracer_print_sg_array(const struct side_type_sg *type_sg, const void *_ptr)
1150 {
1151 char *ptr = (char *) _ptr;
1152 uint32_t i;
1153
1154 memcpy(&ptr, ptr + type_sg->offset, sizeof(ptr));
1155 print_attributes("attr", ":", type_sg->u.side_array.attr, type_sg->u.side_array.nr_attr);
1156 printf("%s", type_sg->u.side_array.nr_attr ? ", " : "");
1157 printf("elements: ");
1158 printf("[ ");
1159 for (i = 0; i < type_sg->u.side_array.length; i++) {
1160 printf("%s", i ? ", " : "");
1161 ptr += tracer_print_sg_type(type_sg->u.side_array.elem_type, ptr);
1162 }
1163 printf(" ]");
1164 return sizeof(void *);
1165 }
1166
1167 struct tracer_visitor_priv {
1168 const struct side_type *elem_type;
1169 int i;
1170 };
1171
1172 static
1173 enum side_visitor_status tracer_write_elem_cb(const struct side_tracer_visitor_ctx *tracer_ctx,
1174 const struct side_arg *elem)
1175 {
1176 struct tracer_visitor_priv *tracer_priv = (struct tracer_visitor_priv *) tracer_ctx->priv;
1177
1178 printf("%s", tracer_priv->i++ ? ", " : "");
1179 tracer_print_type(tracer_priv->elem_type, elem);
1180 return SIDE_VISITOR_STATUS_OK;
1181 }
1182
1183 static
1184 void tracer_print_vla_visitor(const struct side_type *type_desc, void *app_ctx)
1185 {
1186 enum side_visitor_status status;
1187 struct tracer_visitor_priv tracer_priv = {
1188 .elem_type = type_desc->u.side_vla_visitor.elem_type,
1189 .i = 0,
1190 };
1191 const struct side_tracer_visitor_ctx tracer_ctx = {
1192 .write_elem = tracer_write_elem_cb,
1193 .priv = &tracer_priv,
1194 };
1195
1196 print_attributes("attr", ":", type_desc->u.side_vla_visitor.attr, type_desc->u.side_vla_visitor.nr_attr);
1197 printf("%s", type_desc->u.side_vla_visitor.nr_attr ? ", " : "");
1198 printf("elements: ");
1199 printf("[ ");
1200 status = type_desc->u.side_vla_visitor.visitor(&tracer_ctx, app_ctx);
1201 switch (status) {
1202 case SIDE_VISITOR_STATUS_OK:
1203 break;
1204 case SIDE_VISITOR_STATUS_ERROR:
1205 fprintf(stderr, "ERROR: Visitor error\n");
1206 abort();
1207 }
1208 printf(" ]");
1209 }
1210
1211 void tracer_print_array_fixint(const struct side_type *type_desc, const struct side_arg *item)
1212 {
1213 const struct side_type *elem_type = type_desc->u.side_array.elem_type;
1214 uint32_t i, side_sav_len = type_desc->u.side_array.length;
1215 void *p = item->u.side_static.side_array_fixint;
1216 enum side_type_label side_type;
1217
1218 print_attributes("attr", ":", type_desc->u.side_array.attr, type_desc->u.side_array.nr_attr);
1219 printf("%s", type_desc->u.side_array.nr_attr ? ", " : "");
1220 printf("elements: ");
1221 switch (item->type) {
1222 case SIDE_TYPE_ARRAY_U8:
1223 if (elem_type->type != SIDE_TYPE_U8)
1224 goto type_error;
1225 break;
1226 case SIDE_TYPE_ARRAY_U16:
1227 if (elem_type->type != SIDE_TYPE_U16)
1228 goto type_error;
1229 break;
1230 case SIDE_TYPE_ARRAY_U32:
1231 if (elem_type->type != SIDE_TYPE_U32)
1232 goto type_error;
1233 break;
1234 case SIDE_TYPE_ARRAY_U64:
1235 if (elem_type->type != SIDE_TYPE_U64)
1236 goto type_error;
1237 break;
1238 case SIDE_TYPE_ARRAY_S8:
1239 if (elem_type->type != SIDE_TYPE_S8)
1240 goto type_error;
1241 break;
1242 case SIDE_TYPE_ARRAY_S16:
1243 if (elem_type->type != SIDE_TYPE_S16)
1244 goto type_error;
1245 break;
1246 case SIDE_TYPE_ARRAY_S32:
1247 if (elem_type->type != SIDE_TYPE_S32)
1248 goto type_error;
1249 break;
1250 case SIDE_TYPE_ARRAY_S64:
1251 if (elem_type->type != SIDE_TYPE_S64)
1252 goto type_error;
1253 break;
1254 case SIDE_TYPE_ARRAY_BYTE:
1255 if (elem_type->type != SIDE_TYPE_BYTE)
1256 goto type_error;
1257 break;
1258 case SIDE_TYPE_ARRAY_POINTER32:
1259 if (elem_type->type != SIDE_TYPE_POINTER32)
1260 goto type_error;
1261 case SIDE_TYPE_ARRAY_POINTER64:
1262 if (elem_type->type != SIDE_TYPE_POINTER64)
1263 goto type_error;
1264 break;
1265 default:
1266 goto type_error;
1267 }
1268 side_type = (enum side_type_label) elem_type->type;
1269
1270 printf("[ ");
1271 for (i = 0; i < side_sav_len; i++) {
1272 struct side_arg sav_elem = {
1273 .type = side_type,
1274 };
1275
1276 switch (side_type) {
1277 case SIDE_TYPE_U8:
1278 sav_elem.u.side_static.integer_value.side_u8 = ((const uint8_t *) p)[i];
1279 break;
1280 case SIDE_TYPE_S8:
1281 sav_elem.u.side_static.integer_value.side_s8 = ((const int8_t *) p)[i];
1282 break;
1283 case SIDE_TYPE_U16:
1284 sav_elem.u.side_static.integer_value.side_u16 = ((const uint16_t *) p)[i];
1285 break;
1286 case SIDE_TYPE_S16:
1287 sav_elem.u.side_static.integer_value.side_s16 = ((const int16_t *) p)[i];
1288 break;
1289 case SIDE_TYPE_U32:
1290 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1291 break;
1292 case SIDE_TYPE_S32:
1293 sav_elem.u.side_static.integer_value.side_s32 = ((const int32_t *) p)[i];
1294 break;
1295 case SIDE_TYPE_U64:
1296 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1297 break;
1298 case SIDE_TYPE_S64:
1299 sav_elem.u.side_static.integer_value.side_s64 = ((const int64_t *) p)[i];
1300 break;
1301 case SIDE_TYPE_BYTE:
1302 sav_elem.u.side_static.byte_value = ((const uint8_t *) p)[i];
1303 break;
1304 case SIDE_TYPE_POINTER32:
1305 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1306 break;
1307 case SIDE_TYPE_POINTER64:
1308 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1309 break;
1310
1311 default:
1312 fprintf(stderr, "ERROR: Unexpected type\n");
1313 abort();
1314 }
1315
1316 printf("%s", i ? ", " : "");
1317 tracer_print_type(elem_type, &sav_elem);
1318 }
1319 printf(" ]");
1320 return;
1321
1322 type_error:
1323 fprintf(stderr, "ERROR: type mismatch\n");
1324 abort();
1325 }
1326
1327 void tracer_print_vla_fixint(const struct side_type *type_desc, const struct side_arg *item)
1328 {
1329 const struct side_type *elem_type = type_desc->u.side_vla.elem_type;
1330 uint32_t i, side_sav_len = item->u.side_static.side_vla_fixint.length;
1331 void *p = item->u.side_static.side_vla_fixint.p;
1332 enum side_type_label side_type;
1333
1334 print_attributes("attr", ":", type_desc->u.side_vla.attr, type_desc->u.side_vla.nr_attr);
1335 printf("%s", type_desc->u.side_vla.nr_attr ? ", " : "");
1336 printf("elements: ");
1337 switch (item->type) {
1338 case SIDE_TYPE_VLA_U8:
1339 if (elem_type->type != SIDE_TYPE_U8)
1340 goto type_error;
1341 break;
1342 case SIDE_TYPE_VLA_U16:
1343 if (elem_type->type != SIDE_TYPE_U16)
1344 goto type_error;
1345 break;
1346 case SIDE_TYPE_VLA_U32:
1347 if (elem_type->type != SIDE_TYPE_U32)
1348 goto type_error;
1349 break;
1350 case SIDE_TYPE_VLA_U64:
1351 if (elem_type->type != SIDE_TYPE_U64)
1352 goto type_error;
1353 break;
1354 case SIDE_TYPE_VLA_S8:
1355 if (elem_type->type != SIDE_TYPE_S8)
1356 goto type_error;
1357 break;
1358 case SIDE_TYPE_VLA_S16:
1359 if (elem_type->type != SIDE_TYPE_S16)
1360 goto type_error;
1361 break;
1362 case SIDE_TYPE_VLA_S32:
1363 if (elem_type->type != SIDE_TYPE_S32)
1364 goto type_error;
1365 break;
1366 case SIDE_TYPE_VLA_S64:
1367 if (elem_type->type != SIDE_TYPE_S64)
1368 goto type_error;
1369 break;
1370 case SIDE_TYPE_VLA_BYTE:
1371 if (elem_type->type != SIDE_TYPE_BYTE)
1372 goto type_error;
1373 break;
1374 case SIDE_TYPE_VLA_POINTER32:
1375 if (elem_type->type != SIDE_TYPE_POINTER32)
1376 goto type_error;
1377 case SIDE_TYPE_VLA_POINTER64:
1378 if (elem_type->type != SIDE_TYPE_POINTER64)
1379 goto type_error;
1380 break;
1381 default:
1382 goto type_error;
1383 }
1384 side_type = (enum side_type_label) elem_type->type;
1385
1386 printf("[ ");
1387 for (i = 0; i < side_sav_len; i++) {
1388 struct side_arg sav_elem = {
1389 .type = side_type,
1390 };
1391
1392 switch (side_type) {
1393 case SIDE_TYPE_U8:
1394 sav_elem.u.side_static.integer_value.side_u8 = ((const uint8_t *) p)[i];
1395 break;
1396 case SIDE_TYPE_S8:
1397 sav_elem.u.side_static.integer_value.side_s8 = ((const int8_t *) p)[i];
1398 break;
1399 case SIDE_TYPE_U16:
1400 sav_elem.u.side_static.integer_value.side_u16 = ((const uint16_t *) p)[i];
1401 break;
1402 case SIDE_TYPE_S16:
1403 sav_elem.u.side_static.integer_value.side_s16 = ((const int16_t *) p)[i];
1404 break;
1405 case SIDE_TYPE_U32:
1406 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1407 break;
1408 case SIDE_TYPE_S32:
1409 sav_elem.u.side_static.integer_value.side_s32 = ((const int32_t *) p)[i];
1410 break;
1411 case SIDE_TYPE_U64:
1412 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1413 break;
1414 case SIDE_TYPE_S64:
1415 sav_elem.u.side_static.integer_value.side_s64 = ((const int64_t *) p)[i];
1416 break;
1417 case SIDE_TYPE_BYTE:
1418 sav_elem.u.side_static.byte_value = ((const uint8_t *) p)[i];
1419 break;
1420 case SIDE_TYPE_POINTER32:
1421 sav_elem.u.side_static.integer_value.side_u32 = ((const uint32_t *) p)[i];
1422 break;
1423 case SIDE_TYPE_POINTER64:
1424 sav_elem.u.side_static.integer_value.side_u64 = ((const uint64_t *) p)[i];
1425 break;
1426
1427 default:
1428 fprintf(stderr, "ERROR: Unexpected type\n");
1429 abort();
1430 }
1431
1432 printf("%s", i ? ", " : "");
1433 tracer_print_type(elem_type, &sav_elem);
1434 }
1435 printf(" ]");
1436 return;
1437
1438 type_error:
1439 fprintf(stderr, "ERROR: type mismatch\n");
1440 abort();
1441 }
1442
1443 static
1444 void tracer_print_dynamic_struct(const struct side_arg_dynamic_struct *dynamic_struct)
1445 {
1446 const struct side_arg_dynamic_field *fields = dynamic_struct->fields;
1447 uint32_t i, len = dynamic_struct->len;
1448
1449 print_attributes("attr", "::", dynamic_struct->attr, dynamic_struct->nr_attr);
1450 printf("%s", dynamic_struct->nr_attr ? ", " : "");
1451 printf("fields:: ");
1452 printf("[ ");
1453 for (i = 0; i < len; i++) {
1454 printf("%s", i ? ", " : "");
1455 printf("%s:: ", fields[i].field_name);
1456 tracer_print_dynamic(&fields[i].elem);
1457 }
1458 printf(" ]");
1459 }
1460
1461 struct tracer_dynamic_struct_visitor_priv {
1462 int i;
1463 };
1464
1465 static
1466 enum side_visitor_status tracer_dynamic_struct_write_elem_cb(
1467 const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
1468 const struct side_arg_dynamic_field *dynamic_field)
1469 {
1470 struct tracer_dynamic_struct_visitor_priv *tracer_priv =
1471 (struct tracer_dynamic_struct_visitor_priv *) tracer_ctx->priv;
1472
1473 printf("%s", tracer_priv->i++ ? ", " : "");
1474 printf("%s:: ", dynamic_field->field_name);
1475 tracer_print_dynamic(&dynamic_field->elem);
1476 return SIDE_VISITOR_STATUS_OK;
1477 }
1478
1479 static
1480 void tracer_print_dynamic_struct_visitor(const struct side_arg *item)
1481 {
1482 enum side_visitor_status status;
1483 struct tracer_dynamic_struct_visitor_priv tracer_priv = {
1484 .i = 0,
1485 };
1486 const struct side_tracer_dynamic_struct_visitor_ctx tracer_ctx = {
1487 .write_field = tracer_dynamic_struct_write_elem_cb,
1488 .priv = &tracer_priv,
1489 };
1490 void *app_ctx = item->u.side_dynamic.side_dynamic_struct_visitor.app_ctx;
1491
1492 print_attributes("attr", "::", item->u.side_dynamic.side_dynamic_struct_visitor.attr, item->u.side_dynamic.side_dynamic_struct_visitor.nr_attr);
1493 printf("%s", item->u.side_dynamic.side_dynamic_struct_visitor.nr_attr ? ", " : "");
1494 printf("fields:: ");
1495 printf("[ ");
1496 status = item->u.side_dynamic.side_dynamic_struct_visitor.visitor(&tracer_ctx, app_ctx);
1497 switch (status) {
1498 case SIDE_VISITOR_STATUS_OK:
1499 break;
1500 case SIDE_VISITOR_STATUS_ERROR:
1501 fprintf(stderr, "ERROR: Visitor error\n");
1502 abort();
1503 }
1504 printf(" ]");
1505 }
1506
1507 static
1508 void tracer_print_dynamic_vla(const struct side_arg_dynamic_vla *vla)
1509 {
1510 const struct side_arg *sav = vla->sav;
1511 uint32_t i, side_sav_len = vla->len;
1512
1513 print_attributes("attr", "::", vla->attr, vla->nr_attr);
1514 printf("%s", vla->nr_attr ? ", " : "");
1515 printf("elements:: ");
1516 printf("[ ");
1517 for (i = 0; i < side_sav_len; i++) {
1518 printf("%s", i ? ", " : "");
1519 tracer_print_dynamic(&sav[i]);
1520 }
1521 printf(" ]");
1522 }
1523
1524 struct tracer_dynamic_vla_visitor_priv {
1525 int i;
1526 };
1527
1528 static
1529 enum side_visitor_status tracer_dynamic_vla_write_elem_cb(
1530 const struct side_tracer_visitor_ctx *tracer_ctx,
1531 const struct side_arg *elem)
1532 {
1533 struct tracer_dynamic_vla_visitor_priv *tracer_priv =
1534 (struct tracer_dynamic_vla_visitor_priv *) tracer_ctx->priv;
1535
1536 printf("%s", tracer_priv->i++ ? ", " : "");
1537 tracer_print_dynamic(elem);
1538 return SIDE_VISITOR_STATUS_OK;
1539 }
1540
1541 static
1542 void tracer_print_dynamic_vla_visitor(const struct side_arg *item)
1543 {
1544 enum side_visitor_status status;
1545 struct tracer_dynamic_vla_visitor_priv tracer_priv = {
1546 .i = 0,
1547 };
1548 const struct side_tracer_visitor_ctx tracer_ctx = {
1549 .write_elem = tracer_dynamic_vla_write_elem_cb,
1550 .priv = &tracer_priv,
1551 };
1552 void *app_ctx = item->u.side_dynamic.side_dynamic_vla_visitor.app_ctx;
1553
1554 print_attributes("attr", "::", item->u.side_dynamic.side_dynamic_vla_visitor.attr, item->u.side_dynamic.side_dynamic_vla_visitor.nr_attr);
1555 printf("%s", item->u.side_dynamic.side_dynamic_vla_visitor.nr_attr ? ", " : "");
1556 printf("elements:: ");
1557 printf("[ ");
1558 status = item->u.side_dynamic.side_dynamic_vla_visitor.visitor(&tracer_ctx, app_ctx);
1559 switch (status) {
1560 case SIDE_VISITOR_STATUS_OK:
1561 break;
1562 case SIDE_VISITOR_STATUS_ERROR:
1563 fprintf(stderr, "ERROR: Visitor error\n");
1564 abort();
1565 }
1566 printf(" ]");
1567 }
1568
1569 static
1570 void tracer_print_dynamic(const struct side_arg *item)
1571 {
1572 printf("{ ");
1573 switch (item->type) {
1574 case SIDE_TYPE_DYNAMIC_NULL:
1575 tracer_print_type_header("::", item->u.side_dynamic.side_null.attr, item->u.side_dynamic.side_null.nr_attr);
1576 printf("<NULL TYPE>");
1577 break;
1578 case SIDE_TYPE_DYNAMIC_BOOL:
1579 tracer_print_type_header("::", item->u.side_dynamic.side_bool.type.attr, item->u.side_dynamic.side_bool.type.nr_attr);
1580 printf("%s", item->u.side_dynamic.side_bool.value ? "true" : "false");
1581 break;
1582 case SIDE_TYPE_DYNAMIC_U8:
1583 case SIDE_TYPE_DYNAMIC_U16:
1584 case SIDE_TYPE_DYNAMIC_U32:
1585 case SIDE_TYPE_DYNAMIC_U64:
1586 case SIDE_TYPE_DYNAMIC_S8:
1587 case SIDE_TYPE_DYNAMIC_S16:
1588 case SIDE_TYPE_DYNAMIC_S32:
1589 case SIDE_TYPE_DYNAMIC_S64:
1590 tracer_print_type_integer("::", &item->u.side_dynamic.side_integer.type, &item->u.side_dynamic.side_integer.value, 0,
1591 TRACER_DISPLAY_BASE_10);
1592 break;
1593 case SIDE_TYPE_DYNAMIC_BYTE:
1594 tracer_print_type_header("::", item->u.side_dynamic.side_byte.type.attr, item->u.side_dynamic.side_byte.type.nr_attr);
1595 printf("0x%" PRIx8, item->u.side_dynamic.side_byte.value);
1596 break;
1597
1598 case SIDE_TYPE_DYNAMIC_POINTER32:
1599 case SIDE_TYPE_DYNAMIC_POINTER64:
1600 tracer_print_type_integer("::", &item->u.side_dynamic.side_integer.type, &item->u.side_dynamic.side_integer.value, 0,
1601 TRACER_DISPLAY_BASE_16);
1602 break;
1603
1604 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY16:
1605 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY32:
1606 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY64:
1607 case SIDE_TYPE_DYNAMIC_FLOAT_BINARY128:
1608 tracer_print_type_float("::", &item->u.side_dynamic.side_float.type,
1609 &item->u.side_dynamic.side_float.value);
1610 break;
1611
1612 case SIDE_TYPE_DYNAMIC_STRING:
1613 tracer_print_type_header("::", item->u.side_dynamic.side_string.type.attr, item->u.side_dynamic.side_string.type.nr_attr);
1614 printf("\"%s\"", (const char *)(uintptr_t) item->u.side_dynamic.side_string.value);
1615 break;
1616 case SIDE_TYPE_DYNAMIC_STRUCT:
1617 tracer_print_dynamic_struct(item->u.side_dynamic.side_dynamic_struct);
1618 break;
1619 case SIDE_TYPE_DYNAMIC_STRUCT_VISITOR:
1620 tracer_print_dynamic_struct_visitor(item);
1621 break;
1622 case SIDE_TYPE_DYNAMIC_VLA:
1623 tracer_print_dynamic_vla(item->u.side_dynamic.side_dynamic_vla);
1624 break;
1625 case SIDE_TYPE_DYNAMIC_VLA_VISITOR:
1626 tracer_print_dynamic_vla_visitor(item);
1627 break;
1628 default:
1629 fprintf(stderr, "<UNKNOWN TYPE>");
1630 abort();
1631 }
1632 printf(" }");
1633 }
1634
1635 static
1636 void tracer_print_static_fields(const struct side_event_description *desc,
1637 const struct side_arg_vec *side_arg_vec,
1638 uint32_t *nr_items)
1639 {
1640 const struct side_arg *sav = side_arg_vec->sav;
1641 uint32_t i, side_sav_len = side_arg_vec->len;
1642
1643 printf("provider: %s, event: %s", desc->provider_name, desc->event_name);
1644 if (desc->nr_fields != side_sav_len) {
1645 fprintf(stderr, "ERROR: number of fields mismatch between description and arguments\n");
1646 abort();
1647 }
1648 print_attributes(", attr", ":", desc->attr, desc->nr_attr);
1649 printf("%s", side_sav_len ? ", fields: [ " : "");
1650 for (i = 0; i < side_sav_len; i++) {
1651 printf("%s", i ? ", " : "");
1652 tracer_print_field(&desc->fields[i], &sav[i]);
1653 }
1654 if (nr_items)
1655 *nr_items = i;
1656 if (side_sav_len)
1657 printf(" ]");
1658 }
1659
1660 void tracer_call(const struct side_event_description *desc,
1661 const struct side_arg_vec *side_arg_vec,
1662 void *priv __attribute__((unused)))
1663 {
1664 uint32_t nr_fields = 0;
1665
1666 tracer_print_static_fields(desc, side_arg_vec, &nr_fields);
1667 printf("\n");
1668 }
1669
1670 void tracer_call_variadic(const struct side_event_description *desc,
1671 const struct side_arg_vec *side_arg_vec,
1672 const struct side_arg_dynamic_struct *var_struct,
1673 void *priv __attribute__((unused)))
1674 {
1675 uint32_t nr_fields = 0, i, var_struct_len = var_struct->len;
1676
1677 tracer_print_static_fields(desc, side_arg_vec, &nr_fields);
1678
1679 if (side_unlikely(!(desc->flags & SIDE_EVENT_FLAG_VARIADIC))) {
1680 fprintf(stderr, "ERROR: unexpected non-variadic event description\n");
1681 abort();
1682 }
1683 print_attributes(", attr ", "::", var_struct->attr, var_struct->nr_attr);
1684 printf("%s", var_struct_len ? ", fields:: [ " : "");
1685 for (i = 0; i < var_struct_len; i++, nr_fields++) {
1686 printf("%s", i ? ", " : "");
1687 printf("%s:: ", var_struct->fields[i].field_name);
1688 tracer_print_dynamic(&var_struct->fields[i].elem);
1689 }
1690 if (i)
1691 printf(" ]");
1692 printf("\n");
1693 }
1694
1695 void tracer_event_notification(enum side_tracer_notification notif,
1696 struct side_event_description **events, uint32_t nr_events, void *priv)
1697 {
1698 uint32_t i;
1699 int ret;
1700
1701 printf("----------------------------------------------------------\n");
1702 printf("Tracer notified of events %s\n",
1703 notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS ? "inserted" : "removed");
1704 for (i = 0; i < nr_events; i++) {
1705 struct side_event_description *event = events[i];
1706
1707 /* Skip NULL pointers */
1708 if (!event)
1709 continue;
1710 printf("provider: %s, event: %s\n",
1711 event->provider_name, event->event_name);
1712 if (notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS) {
1713 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
1714 ret = side_tracer_callback_variadic_register(event, tracer_call_variadic, NULL);
1715 if (ret)
1716 abort();
1717 } else {
1718 ret = side_tracer_callback_register(event, tracer_call, NULL);
1719 if (ret)
1720 abort();
1721 }
1722 } else {
1723 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
1724 ret = side_tracer_callback_variadic_unregister(event, tracer_call_variadic, NULL);
1725 if (ret)
1726 abort();
1727 } else {
1728 ret = side_tracer_callback_unregister(event, tracer_call, NULL);
1729 if (ret)
1730 abort();
1731 }
1732 }
1733 }
1734 printf("----------------------------------------------------------\n");
1735 }
1736
1737 static __attribute__((constructor))
1738 void tracer_init(void);
1739 static
1740 void tracer_init(void)
1741 {
1742 tracer_handle = side_tracer_event_notification_register(tracer_event_notification, NULL);
1743 if (!tracer_handle)
1744 abort();
1745 }
1746
1747 static __attribute__((destructor))
1748 void tracer_exit(void);
1749 static
1750 void tracer_exit(void)
1751 {
1752 side_tracer_event_notification_unregister(tracer_handle);
1753 }
This page took 0.064241 seconds and 4 git commands to generate.