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