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