Refactor tracer_print_type_integer for re-use
[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 {
83 uint32_t i;
84
85 for (i = 0; i < nr_attr; i++) {
86 const struct side_attr *attr = &_attr[i];
87
88 if (!strcmp(attr->key, "std.integer.base")) {
89 int64_t val = get_attr_integer_value(attr);
90
91 switch (val) {
92 case 2:
93 return TRACER_DISPLAY_BASE_2;
94 case 8:
95 return TRACER_DISPLAY_BASE_8;
96 case 10:
97 return TRACER_DISPLAY_BASE_10;
98 case 16:
99 return TRACER_DISPLAY_BASE_16;
100 default:
101 fprintf(stderr, "Unexpected integer display base: %" PRId64 "\n", val);
102 abort();
103 }
104 }
105 }
106 return TRACER_DISPLAY_BASE_10; /* Default */
107 }
108
109 static
110 bool type_to_host_reverse_bo(const struct side_type_description *type_desc)
111 {
112 switch (type_desc->type) {
113 case SIDE_TYPE_U8:
114 case SIDE_TYPE_S8:
115 case SIDE_TYPE_BYTE:
116 return false;
117 case SIDE_TYPE_U16:
118 case SIDE_TYPE_U32:
119 case SIDE_TYPE_U64:
120 case SIDE_TYPE_S16:
121 case SIDE_TYPE_S32:
122 case SIDE_TYPE_S64:
123 if (type_desc->u.side_integer.byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
124 return true;
125 else
126 return false;
127 break;
128 case SIDE_TYPE_POINTER32:
129 case SIDE_TYPE_POINTER64:
130 if (type_desc->u.side_integer.byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
131 return true;
132 else
133 return false;
134 break;
135 case SIDE_TYPE_FLOAT_BINARY16:
136 case SIDE_TYPE_FLOAT_BINARY32:
137 case SIDE_TYPE_FLOAT_BINARY64:
138 case SIDE_TYPE_FLOAT_BINARY128:
139 if (type_desc->u.side_float.byte_order != SIDE_TYPE_FLOAT_WORD_ORDER_HOST)
140 return true;
141 else
142 return false;
143 break;
144 default:
145 fprintf(stderr, "Unexpected type\n");
146 abort();
147 }
148 }
149
150 static
151 bool sg_type_to_host_reverse_bo(const struct side_type_sg_description *sg_type)
152 {
153 switch (sg_type->type) {
154 case SIDE_TYPE_SG_UNSIGNED_INT:
155 case SIDE_TYPE_SG_SIGNED_INT:
156 if (sg_type->u.side_basic.byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
157 return true;
158 else
159 return false;
160 break;
161 default:
162 fprintf(stderr, "Unexpected type\n");
163 abort();
164 }
165 }
166
167 static
168 bool dynamic_type_to_host_reverse_bo(const struct side_arg_dynamic_vec *item)
169 {
170 switch (item->dynamic_type) {
171 case SIDE_DYNAMIC_TYPE_U8:
172 case SIDE_DYNAMIC_TYPE_S8:
173 case SIDE_DYNAMIC_TYPE_BYTE:
174 return false;
175 case SIDE_DYNAMIC_TYPE_U16:
176 case SIDE_DYNAMIC_TYPE_U32:
177 case SIDE_DYNAMIC_TYPE_U64:
178 case SIDE_DYNAMIC_TYPE_S16:
179 case SIDE_DYNAMIC_TYPE_S32:
180 case SIDE_DYNAMIC_TYPE_S64:
181 case SIDE_DYNAMIC_TYPE_POINTER32:
182 case SIDE_DYNAMIC_TYPE_POINTER64:
183 if (item->u.side_integer.type.byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
184 return true;
185 else
186 return false;
187 break;
188 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY16:
189 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY32:
190 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY64:
191 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY128:
192 if (item->u.side_float.type.byte_order != SIDE_TYPE_FLOAT_WORD_ORDER_HOST)
193 return true;
194 else
195 return false;
196 break;
197 default:
198 fprintf(stderr, "Unexpected type\n");
199 abort();
200 }
201 }
202
203 static
204 void tracer_print_attr_type(const char *separator, const struct side_attr *attr)
205 {
206 printf("{ key%s \"%s\", value%s ", separator, attr->key, separator);
207 switch (attr->value.type) {
208 case SIDE_ATTR_TYPE_BOOL:
209 printf("%s", attr->value.u.side_bool ? "true" : "false");
210 break;
211 case SIDE_ATTR_TYPE_U8:
212 printf("%" PRIu8, attr->value.u.integer_value.side_u8);
213 break;
214 case SIDE_ATTR_TYPE_U16:
215 printf("%" PRIu16, attr->value.u.integer_value.side_u16);
216 break;
217 case SIDE_ATTR_TYPE_U32:
218 printf("%" PRIu32, attr->value.u.integer_value.side_u32);
219 break;
220 case SIDE_ATTR_TYPE_U64:
221 printf("%" PRIu64, attr->value.u.integer_value.side_u64);
222 break;
223 case SIDE_ATTR_TYPE_S8:
224 printf("%" PRId8, attr->value.u.integer_value.side_s8);
225 break;
226 case SIDE_ATTR_TYPE_S16:
227 printf("%" PRId16, attr->value.u.integer_value.side_s16);
228 break;
229 case SIDE_ATTR_TYPE_S32:
230 printf("%" PRId32, attr->value.u.integer_value.side_s32);
231 break;
232 case SIDE_ATTR_TYPE_S64:
233 printf("%" PRId64, attr->value.u.integer_value.side_s64);
234 break;
235 case SIDE_ATTR_TYPE_POINTER32:
236 printf("0x%" PRIx32, attr->value.u.integer_value.side_u32);
237 break;
238 case SIDE_ATTR_TYPE_POINTER64:
239 printf("0x%" PRIx64, attr->value.u.integer_value.side_u64);
240 break;
241 case SIDE_ATTR_TYPE_FLOAT_BINARY16:
242 #if __HAVE_FLOAT16
243 printf("%g", (double) attr->value.u.float_value.side_float_binary16);
244 break;
245 #else
246 fprintf(stderr, "ERROR: Unsupported binary16 float type\n");
247 abort();
248 #endif
249 case SIDE_ATTR_TYPE_FLOAT_BINARY32:
250 #if __HAVE_FLOAT32
251 printf("%g", (double) attr->value.u.float_value.side_float_binary32);
252 break;
253 #else
254 fprintf(stderr, "ERROR: Unsupported binary32 float type\n");
255 abort();
256 #endif
257 case SIDE_ATTR_TYPE_FLOAT_BINARY64:
258 #if __HAVE_FLOAT64
259 printf("%g", (double) attr->value.u.float_value.side_float_binary64);
260 break;
261 #else
262 fprintf(stderr, "ERROR: Unsupported binary64 float type\n");
263 abort();
264 #endif
265 case SIDE_ATTR_TYPE_FLOAT_BINARY128:
266 #if __HAVE_FLOAT128
267 printf("%Lg", (long double) attr->value.u.float_value.side_float_binary128);
268 break;
269 #else
270 fprintf(stderr, "ERROR: Unsupported binary128 float type\n");
271 abort();
272 #endif
273 case SIDE_ATTR_TYPE_STRING:
274 printf("\"%s\"", (const char *)(uintptr_t) attr->value.u.string);
275 break;
276 default:
277 fprintf(stderr, "ERROR: <UNKNOWN ATTRIBUTE TYPE>");
278 abort();
279 }
280 printf(" }");
281 }
282
283 static
284 void print_attributes(const char *prefix_str, const char *separator,
285 const struct side_attr *attr, uint32_t nr_attr)
286 {
287 int i;
288
289 if (!nr_attr)
290 return;
291 printf("%s%s [ ", prefix_str, separator);
292 for (i = 0; i < nr_attr; i++) {
293 printf("%s", i ? ", " : "");
294 tracer_print_attr_type(separator, &attr[i]);
295 }
296 printf(" ]");
297 }
298
299 static
300 void print_enum(const struct side_type_description *type_desc, const struct side_arg_vec *item)
301 {
302 const struct side_enum_mappings *mappings = type_desc->u.side_enum.mappings;
303 const struct side_type_description *elem_type = type_desc->u.side_enum.elem_type;
304 int i, print_count = 0;
305 int64_t value;
306
307 if (elem_type->type != item->type) {
308 fprintf(stderr, "ERROR: Unexpected enum element type\n");
309 abort();
310 }
311 switch (item->type) {
312 case SIDE_TYPE_U8:
313 value = (int64_t) item->u.integer_value.side_u8;
314 break;
315 case SIDE_TYPE_U16:
316 {
317 uint16_t v;
318
319 v = item->u.integer_value.side_u16;
320 if (type_to_host_reverse_bo(elem_type))
321 v = side_bswap_16(v);
322 value = (int64_t) v;
323 break;
324 }
325 case SIDE_TYPE_U32:
326 {
327 uint32_t v;
328
329 v = item->u.integer_value.side_u32;
330 if (type_to_host_reverse_bo(elem_type))
331 v = side_bswap_32(v);
332 value = (int64_t) v;
333 break;
334 }
335 case SIDE_TYPE_U64:
336 {
337 uint64_t v;
338
339 v = item->u.integer_value.side_u64;
340 if (type_to_host_reverse_bo(elem_type))
341 v = side_bswap_64(v);
342 value = (int64_t) v;
343 break;
344 }
345 case SIDE_TYPE_S8:
346 value = (int64_t) item->u.integer_value.side_s8;
347 break;
348 case SIDE_TYPE_S16:
349 {
350 int16_t v;
351
352 v = item->u.integer_value.side_s16;
353 if (type_to_host_reverse_bo(elem_type))
354 v = side_bswap_16(v);
355 value = (int64_t) v;
356 break;
357 }
358 case SIDE_TYPE_S32:
359 {
360 int32_t v;
361
362 v = item->u.integer_value.side_s32;
363 if (type_to_host_reverse_bo(elem_type))
364 v = side_bswap_32(v);
365 value = (int64_t) v;
366 break;
367 }
368 case SIDE_TYPE_S64:
369 {
370 int64_t v;
371
372 v = item->u.integer_value.side_s64;
373 if (type_to_host_reverse_bo(elem_type))
374 v = side_bswap_64(v);
375 value = v;
376 break;
377 }
378 default:
379 fprintf(stderr, "ERROR: Unexpected enum element type\n");
380 abort();
381 }
382 print_attributes("attr", ":", mappings->attr, mappings->nr_attr);
383 printf("%s", mappings->nr_attr ? ", " : "");
384 tracer_print_type(type_desc->u.side_enum.elem_type, item);
385 printf(", labels: [ ");
386 for (i = 0; i < mappings->nr_mappings; i++) {
387 const struct side_enum_mapping *mapping = &mappings->mappings[i];
388
389 if (mapping->range_end < mapping->range_begin) {
390 fprintf(stderr, "ERROR: Unexpected enum range: %" PRIu64 "-%" PRIu64 "\n",
391 mapping->range_begin, mapping->range_end);
392 abort();
393 }
394 if (value >= mapping->range_begin && value <= mapping->range_end) {
395 printf("%s", print_count++ ? ", " : "");
396 printf("\"%s\"", mapping->label);
397 }
398 }
399 if (!print_count)
400 printf("<NO LABEL>");
401 printf(" ]");
402 }
403
404 static
405 uint32_t enum_elem_type_to_stride(const struct side_type_description *elem_type)
406 {
407 uint32_t stride_bit;
408
409 switch (elem_type->type) {
410 case SIDE_TYPE_U8: /* Fall-through */
411 case SIDE_TYPE_BYTE:
412 stride_bit = 8;
413 break;
414 case SIDE_TYPE_U16:
415 stride_bit = 16;
416 break;
417 case SIDE_TYPE_U32:
418 stride_bit = 32;
419 break;
420 case SIDE_TYPE_U64:
421 stride_bit = 64;
422 break;
423 default:
424 fprintf(stderr, "ERROR: Unexpected enum element type\n");
425 abort();
426 }
427 return stride_bit;
428 }
429
430 static
431 void print_enum_bitmap(const struct side_type_description *type_desc,
432 const struct side_arg_vec *item)
433 {
434 const struct side_type_description *elem_type = type_desc->u.side_enum_bitmap.elem_type;
435 const struct side_enum_bitmap_mappings *side_enum_mappings = type_desc->u.side_enum_bitmap.mappings;
436 int i, print_count = 0;
437 uint32_t stride_bit, nr_items;
438 bool reverse_byte_order = false;
439 const struct side_arg_vec *array_item;
440
441 switch (elem_type->type) {
442 case SIDE_TYPE_U8: /* Fall-through */
443 case SIDE_TYPE_BYTE: /* Fall-through */
444 case SIDE_TYPE_U16: /* Fall-through */
445 case SIDE_TYPE_U32: /* Fall-through */
446 case SIDE_TYPE_U64:
447 stride_bit = enum_elem_type_to_stride(elem_type);
448 reverse_byte_order = type_to_host_reverse_bo(elem_type);
449 array_item = item;
450 nr_items = 1;
451 break;
452 case SIDE_TYPE_ARRAY:
453 stride_bit = enum_elem_type_to_stride(elem_type->u.side_array.elem_type);
454 reverse_byte_order = type_to_host_reverse_bo(elem_type->u.side_array.elem_type);
455 array_item = item->u.side_array->sav;
456 nr_items = type_desc->u.side_array.length;
457 break;
458 case SIDE_TYPE_VLA:
459 stride_bit = enum_elem_type_to_stride(elem_type->u.side_vla.elem_type);
460 reverse_byte_order = type_to_host_reverse_bo(elem_type->u.side_vla.elem_type);
461 array_item = item->u.side_vla->sav;
462 nr_items = item->u.side_vla->len;
463 break;
464 default:
465 fprintf(stderr, "ERROR: Unexpected enum element type\n");
466 abort();
467 }
468
469 print_attributes("attr", ":", side_enum_mappings->attr, side_enum_mappings->nr_attr);
470 printf("%s", side_enum_mappings->nr_attr ? ", " : "");
471 printf("labels: [ ");
472 for (i = 0; i < side_enum_mappings->nr_mappings; i++) {
473 const struct side_enum_bitmap_mapping *mapping = &side_enum_mappings->mappings[i];
474 bool match = false;
475 uint64_t bit;
476
477 if (mapping->range_end < mapping->range_begin) {
478 fprintf(stderr, "ERROR: Unexpected enum bitmap range: %" PRIu64 "-%" PRIu64 "\n",
479 mapping->range_begin, mapping->range_end);
480 abort();
481 }
482 for (bit = mapping->range_begin; bit <= mapping->range_end; bit++) {
483 if (bit > (nr_items * stride_bit) - 1)
484 break;
485 switch (stride_bit) {
486 case 8:
487 {
488 uint8_t v = array_item[bit / 8].u.integer_value.side_u8;
489 if (v & (1ULL << (bit % 8))) {
490 match = true;
491 goto match;
492 }
493 break;
494 }
495 case 16:
496 {
497 uint16_t v = array_item[bit / 16].u.integer_value.side_u16;
498 if (reverse_byte_order)
499 v = side_bswap_16(v);
500 if (v & (1ULL << (bit % 16))) {
501 match = true;
502 goto match;
503 }
504 break;
505 }
506 case 32:
507 {
508 uint32_t v = array_item[bit / 32].u.integer_value.side_u32;
509 if (reverse_byte_order)
510 v = side_bswap_32(v);
511 if (v & (1ULL << (bit % 32))) {
512 match = true;
513 goto match;
514 }
515 break;
516 }
517 case 64:
518 {
519 uint64_t v = array_item[bit / 64].u.integer_value.side_u64;
520 if (reverse_byte_order)
521 v = side_bswap_64(v);
522 if (v & (1ULL << (bit % 64))) {
523 match = true;
524 goto match;
525 }
526 break;
527 }
528 default:
529 abort();
530 }
531 }
532 match:
533 if (match) {
534 printf("%s", print_count++ ? ", " : "");
535 printf("\"%s\"", mapping->label);
536 }
537 }
538 if (!print_count)
539 printf("<NO LABEL>");
540 printf(" ]");
541 }
542
543 static
544 void print_integer_binary(uint64_t v, int bits)
545 {
546 int i;
547
548 printf("0b");
549 v <<= 64 - bits;
550 for (i = 0; i < bits; i++) {
551 printf("%c", v & (1ULL << 63) ? '1' : '0');
552 v <<= 1;
553 }
554 }
555
556 static
557 void tracer_print_type_header(const char *separator,
558 const struct side_attr *attr, uint32_t nr_attr)
559 {
560 print_attributes("attr", separator, attr, nr_attr);
561 printf("%s", nr_attr ? ", " : "");
562 printf("value%s ", separator);
563 }
564
565 static
566 void tracer_print_type_integer(const char *separator,
567 const struct side_type_integer *type_integer,
568 const union side_integer_value *value)
569 {
570 enum tracer_display_base base;
571 union {
572 uint64_t v_unsigned;
573 int64_t v_signed;
574 } v;
575
576 if (!type_integer->len_bits ||
577 type_integer->len_bits > type_integer->integer_size_bits)
578 abort();
579 base = get_attr_display_base(type_integer->attr,
580 type_integer->nr_attr);
581 switch (type_integer->integer_size_bits) {
582 case 8:
583 if (type_integer->signedness)
584 v.v_signed = value->side_s8;
585 else
586 v.v_unsigned = value->side_u8;
587 break;
588 case 16:
589 if (type_integer->signedness) {
590 int16_t side_s16;
591
592 side_s16 = value->side_s16;
593 if (type_integer->byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
594 side_s16 = side_bswap_16(side_s16);
595 v.v_signed = side_s16;
596 } else {
597 uint16_t side_u16;
598
599 side_u16 = value->side_u16;
600 if (type_integer->byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
601 side_u16 = side_bswap_16(side_u16);
602 v.v_unsigned = side_u16;
603 }
604 break;
605 case 32:
606 if (type_integer->signedness) {
607 int32_t side_s32;
608
609 side_s32 = value->side_s32;
610 if (type_integer->byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
611 side_s32 = side_bswap_32(side_s32);
612 v.v_signed = side_s32;
613 } else {
614 uint32_t side_u32;
615
616 side_u32 = value->side_u32;
617 if (type_integer->byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
618 side_u32 = side_bswap_32(side_u32);
619 v.v_unsigned = side_u32;
620 }
621 break;
622 case 64:
623 if (type_integer->signedness) {
624 int64_t side_s64;
625
626 side_s64 = value->side_s64;
627 if (type_integer->byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
628 side_s64 = side_bswap_64(side_s64);
629 v.v_signed = side_s64;
630 } else {
631 uint64_t side_u64;
632
633 side_u64 = value->side_u64;
634 if (type_integer->byte_order != SIDE_TYPE_BYTE_ORDER_HOST)
635 side_u64 = side_bswap_64(side_u64);
636 v.v_unsigned = side_u64;
637 }
638 break;
639 default:
640 abort();
641 }
642 if (type_integer->len_bits < 64)
643 v.v_unsigned &= (1ULL << type_integer->len_bits) - 1;
644 tracer_print_type_header(separator, type_integer->attr, type_integer->nr_attr);
645 switch (base) {
646 case TRACER_DISPLAY_BASE_2:
647 print_integer_binary(v.v_unsigned, type_integer->len_bits);
648 break;
649 case TRACER_DISPLAY_BASE_8:
650 printf("0%" PRIo64, v.v_unsigned);
651 break;
652 case TRACER_DISPLAY_BASE_10:
653 if (type_integer->signedness) {
654 /* Sign-extend. */
655 if (type_integer->len_bits < 64) {
656 if (v.v_unsigned & (1ULL << (type_integer->len_bits - 1)))
657 v.v_unsigned |= ~((1ULL << type_integer->len_bits) - 1);
658 }
659 printf("%" PRId64, v.v_signed);
660 } else {
661 printf("%" PRIu64, v.v_unsigned);
662 }
663 break;
664 case TRACER_DISPLAY_BASE_16:
665 printf("0x%" PRIx64, v.v_unsigned);
666 break;
667 default:
668 abort();
669 }
670 }
671
672 static
673 void tracer_print_type(const struct side_type_description *type_desc, const struct side_arg_vec *item)
674 {
675 enum side_type type;
676
677 switch (type_desc->type) {
678 case SIDE_TYPE_ARRAY:
679 switch (item->type) {
680 case SIDE_TYPE_ARRAY_U8:
681 case SIDE_TYPE_ARRAY_U16:
682 case SIDE_TYPE_ARRAY_U32:
683 case SIDE_TYPE_ARRAY_U64:
684 case SIDE_TYPE_ARRAY_S8:
685 case SIDE_TYPE_ARRAY_S16:
686 case SIDE_TYPE_ARRAY_S32:
687 case SIDE_TYPE_ARRAY_S64:
688 case SIDE_TYPE_ARRAY_POINTER32:
689 case SIDE_TYPE_ARRAY_POINTER64:
690 case SIDE_TYPE_ARRAY_BYTE:
691 case SIDE_TYPE_ARRAY:
692 break;
693 default:
694 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
695 abort();
696 break;
697 }
698 break;
699
700 case SIDE_TYPE_VLA:
701 switch (item->type) {
702 case SIDE_TYPE_VLA_U8:
703 case SIDE_TYPE_VLA_U16:
704 case SIDE_TYPE_VLA_U32:
705 case SIDE_TYPE_VLA_U64:
706 case SIDE_TYPE_VLA_S8:
707 case SIDE_TYPE_VLA_S16:
708 case SIDE_TYPE_VLA_S32:
709 case SIDE_TYPE_VLA_S64:
710 case SIDE_TYPE_VLA_BYTE:
711 case SIDE_TYPE_VLA_POINTER32:
712 case SIDE_TYPE_VLA_POINTER64:
713 case SIDE_TYPE_VLA:
714 break;
715 default:
716 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
717 abort();
718 break;
719 }
720 break;
721
722 case SIDE_TYPE_ENUM:
723 switch (item->type) {
724 case SIDE_TYPE_U8:
725 case SIDE_TYPE_U16:
726 case SIDE_TYPE_U32:
727 case SIDE_TYPE_U64:
728 case SIDE_TYPE_S8:
729 case SIDE_TYPE_S16:
730 case SIDE_TYPE_S32:
731 case SIDE_TYPE_S64:
732 break;
733 default:
734 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
735 abort();
736 break;
737 }
738 break;
739
740 case SIDE_TYPE_ENUM_BITMAP:
741 switch (item->type) {
742 case SIDE_TYPE_U8:
743 case SIDE_TYPE_BYTE:
744 case SIDE_TYPE_U16:
745 case SIDE_TYPE_U32:
746 case SIDE_TYPE_U64:
747 case SIDE_TYPE_ARRAY:
748 case SIDE_TYPE_VLA:
749 break;
750 default:
751 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
752 abort();
753 break;
754 }
755 break;
756
757 default:
758 if (type_desc->type != item->type) {
759 fprintf(stderr, "ERROR: type mismatch between description and arguments\n");
760 abort();
761 }
762 break;
763 }
764
765 if (type_desc->type == SIDE_TYPE_ENUM || type_desc->type == SIDE_TYPE_ENUM_BITMAP)
766 type = type_desc->type;
767 else
768 type = item->type;
769
770 printf("{ ");
771 switch (type) {
772 case SIDE_TYPE_BOOL:
773 tracer_print_type_header(":", type_desc->u.side_basic.attr, type_desc->u.side_basic.nr_attr);
774 printf("%s", item->u.side_bool ? "true" : "false");
775 break;
776
777 case SIDE_TYPE_U8:
778 case SIDE_TYPE_U16:
779 case SIDE_TYPE_U32:
780 case SIDE_TYPE_U64:
781 case SIDE_TYPE_S8:
782 case SIDE_TYPE_S16:
783 case SIDE_TYPE_S32:
784 case SIDE_TYPE_S64:
785 tracer_print_type_integer(":", &type_desc->u.side_integer,
786 &item->u.integer_value);
787 break;
788
789 case SIDE_TYPE_POINTER32:
790 {
791 uint32_t v;
792
793 v = item->u.integer_value.side_u32;
794 if (type_to_host_reverse_bo(type_desc))
795 v = side_bswap_32(v);
796 tracer_print_type_header(":", type_desc->u.side_integer.attr, type_desc->u.side_integer.nr_attr);
797 printf("0x%" PRIx32, v);
798 break;
799 }
800 case SIDE_TYPE_POINTER64:
801 {
802 uint64_t v;
803
804 v = item->u.integer_value.side_u64;
805 if (type_to_host_reverse_bo(type_desc))
806 v = side_bswap_64(v);
807 tracer_print_type_header(":", type_desc->u.side_integer.attr, type_desc->u.side_integer.nr_attr);
808 printf("0x%" PRIx64, v);
809 break;
810 }
811 case SIDE_TYPE_BYTE:
812 tracer_print_type_header(":", type_desc->u.side_basic.attr, type_desc->u.side_basic.nr_attr);
813 printf("0x%" PRIx8, item->u.side_byte);
814 break;
815
816 case SIDE_TYPE_ENUM:
817 print_enum(type_desc, item);
818 break;
819
820 case SIDE_TYPE_ENUM_BITMAP:
821 print_enum_bitmap(type_desc, item);
822 break;
823
824 case SIDE_TYPE_FLOAT_BINARY16:
825 {
826 #if __HAVE_FLOAT16
827 union {
828 _Float16 f;
829 uint16_t u;
830 } float16 = {
831 .f = item->u.float_value.side_float_binary16,
832 };
833
834 if (type_to_host_reverse_bo(type_desc))
835 float16.u = side_bswap_16(float16.u);
836 tracer_print_type_header(":", type_desc->u.side_float.attr, type_desc->u.side_float.nr_attr);
837 printf("%g", (double) float16.f);
838 break;
839 #else
840 fprintf(stderr, "ERROR: Unsupported binary16 float type\n");
841 abort();
842 #endif
843 }
844 case SIDE_TYPE_FLOAT_BINARY32:
845 {
846 #if __HAVE_FLOAT32
847 union {
848 _Float32 f;
849 uint32_t u;
850 } float32 = {
851 .f = item->u.float_value.side_float_binary32,
852 };
853
854 if (type_to_host_reverse_bo(type_desc))
855 float32.u = side_bswap_32(float32.u);
856 tracer_print_type_header(":", type_desc->u.side_float.attr, type_desc->u.side_float.nr_attr);
857 printf("%g", (double) float32.f);
858 break;
859 #else
860 fprintf(stderr, "ERROR: Unsupported binary32 float type\n");
861 abort();
862 #endif
863 }
864 case SIDE_TYPE_FLOAT_BINARY64:
865 {
866 #if __HAVE_FLOAT64
867 union {
868 _Float64 f;
869 uint64_t u;
870 } float64 = {
871 .f = item->u.float_value.side_float_binary64,
872 };
873
874 if (type_to_host_reverse_bo(type_desc))
875 float64.u = side_bswap_64(float64.u);
876 tracer_print_type_header(":", type_desc->u.side_float.attr, type_desc->u.side_float.nr_attr);
877 printf("%g", (double) float64.f);
878 break;
879 #else
880 fprintf(stderr, "ERROR: Unsupported binary64 float type\n");
881 abort();
882 #endif
883 }
884 case SIDE_TYPE_FLOAT_BINARY128:
885 {
886 #if __HAVE_FLOAT128
887 union {
888 _Float128 f;
889 char arr[16];
890 } float128 = {
891 .f = item->u.float_value.side_float_binary128,
892 };
893
894 if (type_to_host_reverse_bo(type_desc))
895 side_bswap_128p(float128.arr);
896 tracer_print_type_header(":", type_desc->u.side_float.attr, type_desc->u.side_float.nr_attr);
897 printf("%Lg", (long double) float128.f);
898 break;
899 #else
900 fprintf(stderr, "ERROR: Unsupported binary128 float type\n");
901 abort();
902 #endif
903 }
904 case SIDE_TYPE_STRING:
905 tracer_print_type_header(":", type_desc->u.side_basic.attr, type_desc->u.side_basic.nr_attr);
906 printf("\"%s\"", (const char *)(uintptr_t) item->u.string);
907 break;
908 case SIDE_TYPE_STRUCT:
909 tracer_print_struct(type_desc, item->u.side_struct);
910 break;
911 case SIDE_TYPE_STRUCT_SG:
912 tracer_print_struct_sg(type_desc, item->u.side_struct_sg_ptr);
913 break;
914 case SIDE_TYPE_ARRAY:
915 tracer_print_array(type_desc, item->u.side_array);
916 break;
917 case SIDE_TYPE_VLA:
918 tracer_print_vla(type_desc, item->u.side_vla);
919 break;
920 case SIDE_TYPE_VLA_VISITOR:
921 tracer_print_vla_visitor(type_desc, item->u.side_vla_app_visitor_ctx);
922 break;
923 case SIDE_TYPE_ARRAY_U8:
924 case SIDE_TYPE_ARRAY_U16:
925 case SIDE_TYPE_ARRAY_U32:
926 case SIDE_TYPE_ARRAY_U64:
927 case SIDE_TYPE_ARRAY_S8:
928 case SIDE_TYPE_ARRAY_S16:
929 case SIDE_TYPE_ARRAY_S32:
930 case SIDE_TYPE_ARRAY_S64:
931 case SIDE_TYPE_ARRAY_BYTE:
932 case SIDE_TYPE_ARRAY_POINTER32:
933 case SIDE_TYPE_ARRAY_POINTER64:
934 tracer_print_array_fixint(type_desc, item);
935 break;
936 case SIDE_TYPE_VLA_U8:
937 case SIDE_TYPE_VLA_U16:
938 case SIDE_TYPE_VLA_U32:
939 case SIDE_TYPE_VLA_U64:
940 case SIDE_TYPE_VLA_S8:
941 case SIDE_TYPE_VLA_S16:
942 case SIDE_TYPE_VLA_S32:
943 case SIDE_TYPE_VLA_S64:
944 case SIDE_TYPE_VLA_BYTE:
945 case SIDE_TYPE_VLA_POINTER32:
946 case SIDE_TYPE_VLA_POINTER64:
947 tracer_print_vla_fixint(type_desc, item);
948 break;
949 case SIDE_TYPE_DYNAMIC:
950 tracer_print_type_header(":", type_desc->u.side_basic.attr, type_desc->u.side_basic.nr_attr);
951 tracer_print_dynamic(&item->u.dynamic);
952 break;
953 default:
954 fprintf(stderr, "<UNKNOWN TYPE>");
955 abort();
956 }
957 printf(" }");
958 }
959
960 static
961 void tracer_print_field(const struct side_event_field *item_desc, const struct side_arg_vec *item)
962 {
963 printf("%s: ", item_desc->field_name);
964 tracer_print_type(&item_desc->side_type, item);
965 }
966
967 static
968 void tracer_print_struct(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc)
969 {
970 const struct side_arg_vec *sav = sav_desc->sav;
971 uint32_t side_sav_len = sav_desc->len;
972 int i;
973
974 if (type_desc->u.side_struct->nr_fields != side_sav_len) {
975 fprintf(stderr, "ERROR: number of fields mismatch between description and arguments of structure\n");
976 abort();
977 }
978 print_attributes("attr", ":", type_desc->u.side_struct->attr, type_desc->u.side_struct->nr_attr);
979 printf("%s", type_desc->u.side_struct->nr_attr ? ", " : "");
980 printf("fields: { ");
981 for (i = 0; i < side_sav_len; i++) {
982 printf("%s", i ? ", " : "");
983 tracer_print_field(&type_desc->u.side_struct->fields[i], &sav[i]);
984 }
985 printf(" }");
986 }
987
988 static
989 void tracer_print_sg_integer_type_header(const struct side_type_sg_description *sg_type)
990 {
991 print_attributes("attr", ":", sg_type->u.side_basic.attr, sg_type->u.side_basic.nr_attr);
992 printf("%s", sg_type->u.side_basic.nr_attr ? ", " : "");
993 printf("value: ");
994 }
995
996 static
997 void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *_ptr)
998 {
999 enum tracer_display_base base = TRACER_DISPLAY_BASE_10;
1000 const char *ptr = (const char *) _ptr;
1001
1002 switch (sg_type->type) {
1003 case SIDE_TYPE_SG_UNSIGNED_INT:
1004 case SIDE_TYPE_SG_SIGNED_INT:
1005 base = get_attr_display_base(sg_type->u.side_basic.attr,
1006 sg_type->u.side_basic.nr_attr);
1007 break;
1008 default:
1009 break;
1010 }
1011
1012 printf("{ ");
1013 switch (sg_type->type) {
1014 case SIDE_TYPE_SG_UNSIGNED_INT:
1015 {
1016 tracer_print_sg_integer_type_header(sg_type);
1017 switch (sg_type->u.side_basic.integer_size_bits) {
1018 case 8:
1019 {
1020 uint8_t v;
1021
1022 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 8)
1023 abort();
1024 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1025 v >>= sg_type->u.side_basic.offset_bits;
1026 if (sg_type->u.side_basic.len_bits < 8)
1027 v &= (1U << sg_type->u.side_basic.len_bits) - 1;
1028 switch (base) {
1029 case TRACER_DISPLAY_BASE_2:
1030 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1031 break;
1032 case TRACER_DISPLAY_BASE_8:
1033 printf("0%" PRIo8, v);
1034 break;
1035 case TRACER_DISPLAY_BASE_10:
1036 printf("%" PRIu8, v);
1037 break;
1038 case TRACER_DISPLAY_BASE_16:
1039 printf("0x%" PRIx8, v);
1040 break;
1041 default:
1042 abort();
1043 }
1044 break;
1045 }
1046 case 16:
1047 {
1048 uint16_t v;
1049
1050 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 16)
1051 abort();
1052 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1053 if (sg_type_to_host_reverse_bo(sg_type))
1054 v = side_bswap_16(v);
1055 v >>= sg_type->u.side_basic.offset_bits;
1056 if (sg_type->u.side_basic.len_bits < 16)
1057 v &= (1U << sg_type->u.side_basic.len_bits) - 1;
1058 switch (base) {
1059 case TRACER_DISPLAY_BASE_2:
1060 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1061 break;
1062 case TRACER_DISPLAY_BASE_8:
1063 printf("0%" PRIo16, v);
1064 break;
1065 case TRACER_DISPLAY_BASE_10:
1066 printf("%" PRIu16, v);
1067 break;
1068 case TRACER_DISPLAY_BASE_16:
1069 printf("0x%" PRIx16, v);
1070 break;
1071 default:
1072 abort();
1073 }
1074 break;
1075 }
1076 case 32:
1077 {
1078 uint32_t v;
1079
1080 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 32)
1081 abort();
1082 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1083 if (sg_type_to_host_reverse_bo(sg_type))
1084 v = side_bswap_32(v);
1085 v >>= sg_type->u.side_basic.offset_bits;
1086 if (sg_type->u.side_basic.len_bits < 32)
1087 v &= (1U << sg_type->u.side_basic.len_bits) - 1;
1088 switch (base) {
1089 case TRACER_DISPLAY_BASE_2:
1090 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1091 break;
1092 case TRACER_DISPLAY_BASE_8:
1093 printf("0%" PRIo32, v);
1094 break;
1095 case TRACER_DISPLAY_BASE_10:
1096 printf("%" PRIu32, v);
1097 break;
1098 case TRACER_DISPLAY_BASE_16:
1099 printf("0x%" PRIx32, v);
1100 break;
1101 default:
1102 abort();
1103 }
1104 break;
1105 }
1106 case 64:
1107 {
1108 uint64_t v;
1109
1110 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 64)
1111 abort();
1112 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1113 if (sg_type_to_host_reverse_bo(sg_type))
1114 v = side_bswap_64(v);
1115 v >>= sg_type->u.side_basic.offset_bits;
1116 if (sg_type->u.side_basic.len_bits < 64)
1117 v &= (1ULL << sg_type->u.side_basic.len_bits) - 1;
1118 switch (base) {
1119 case TRACER_DISPLAY_BASE_2:
1120 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1121 break;
1122 case TRACER_DISPLAY_BASE_8:
1123 printf("0%" PRIo64, v);
1124 break;
1125 case TRACER_DISPLAY_BASE_10:
1126 printf("%" PRIu64, v);
1127 break;
1128 case TRACER_DISPLAY_BASE_16:
1129 printf("0x%" PRIx64, v);
1130 break;
1131 default:
1132 abort();
1133 }
1134 break;
1135 }
1136 default:
1137 fprintf(stderr, "<UNKNOWN SCATTER-GATHER INTEGER SIZE>");
1138 abort();
1139 }
1140 break;
1141 }
1142 case SIDE_TYPE_SG_SIGNED_INT:
1143 {
1144 tracer_print_sg_integer_type_header(sg_type);
1145 switch (sg_type->u.side_basic.integer_size_bits) {
1146 case 8:
1147 {
1148 int8_t v;
1149
1150 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 8)
1151 abort();
1152 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1153 v >>= sg_type->u.side_basic.offset_bits;
1154 if (sg_type->u.side_basic.len_bits < 8)
1155 v &= (1U << sg_type->u.side_basic.len_bits) - 1;
1156 switch (base) {
1157 case TRACER_DISPLAY_BASE_2:
1158 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1159 break;
1160 case TRACER_DISPLAY_BASE_8:
1161 printf("0%" PRIo8, v);
1162 break;
1163 case TRACER_DISPLAY_BASE_10:
1164 /* Sign-extend. */
1165 if (sg_type->u.side_basic.len_bits < 8) {
1166 if (v & (1U << (sg_type->u.side_basic.len_bits - 1)))
1167 v |= ~((1U << sg_type->u.side_basic.len_bits) - 1);
1168 }
1169 printf("%" PRId8, v);
1170 break;
1171 case TRACER_DISPLAY_BASE_16:
1172 printf("0x%" PRIx8, v);
1173 break;
1174 default:
1175 abort();
1176 }
1177 break;
1178 }
1179 case 16:
1180 {
1181 int16_t v;
1182
1183 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 16)
1184 abort();
1185 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1186 if (sg_type_to_host_reverse_bo(sg_type))
1187 v = side_bswap_16(v);
1188 v >>= sg_type->u.side_basic.offset_bits;
1189 if (sg_type->u.side_basic.len_bits < 16)
1190 v &= (1U << sg_type->u.side_basic.len_bits) - 1;
1191 switch (base) {
1192 case TRACER_DISPLAY_BASE_2:
1193 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1194 break;
1195 case TRACER_DISPLAY_BASE_8:
1196 printf("0%" PRIo16, v);
1197 break;
1198 case TRACER_DISPLAY_BASE_10:
1199 /* Sign-extend. */
1200 if (sg_type->u.side_basic.len_bits < 16) {
1201 if (v & (1U << (sg_type->u.side_basic.len_bits - 1)))
1202 v |= ~((1U << sg_type->u.side_basic.len_bits) - 1);
1203 }
1204 printf("%" PRId16, v);
1205 break;
1206 case TRACER_DISPLAY_BASE_16:
1207 printf("0x%" PRIx16, v);
1208 break;
1209 default:
1210 abort();
1211 }
1212 break;
1213 }
1214 case 32:
1215 {
1216 uint32_t v;
1217
1218 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 32)
1219 abort();
1220 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1221 if (sg_type_to_host_reverse_bo(sg_type))
1222 v = side_bswap_32(v);
1223 v >>= sg_type->u.side_basic.offset_bits;
1224 if (sg_type->u.side_basic.len_bits < 32)
1225 v &= (1U << sg_type->u.side_basic.len_bits) - 1;
1226 switch (base) {
1227 case TRACER_DISPLAY_BASE_2:
1228 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1229 break;
1230 case TRACER_DISPLAY_BASE_8:
1231 printf("0%" PRIo32, v);
1232 break;
1233 case TRACER_DISPLAY_BASE_10:
1234 /* Sign-extend. */
1235 if (sg_type->u.side_basic.len_bits < 32) {
1236 if (v & (1U << (sg_type->u.side_basic.len_bits - 1)))
1237 v |= ~((1U << sg_type->u.side_basic.len_bits) - 1);
1238 }
1239 printf("%" PRId32, v);
1240 break;
1241 case TRACER_DISPLAY_BASE_16:
1242 printf("0x%" PRIx32, v);
1243 break;
1244 default:
1245 abort();
1246 }
1247 break;
1248 }
1249 case 64:
1250 {
1251 uint64_t v;
1252
1253 if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 64)
1254 abort();
1255 memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
1256 if (sg_type_to_host_reverse_bo(sg_type))
1257 v = side_bswap_64(v);
1258 v >>= sg_type->u.side_basic.offset_bits;
1259 if (sg_type->u.side_basic.len_bits < 64)
1260 v &= (1ULL << sg_type->u.side_basic.len_bits) - 1;
1261 switch (base) {
1262 case TRACER_DISPLAY_BASE_2:
1263 print_integer_binary(v, sg_type->u.side_basic.len_bits);
1264 break;
1265 case TRACER_DISPLAY_BASE_8:
1266 printf("0%" PRIo64, v);
1267 break;
1268 case TRACER_DISPLAY_BASE_10:
1269 /* Sign-extend. */
1270 if (sg_type->u.side_basic.len_bits < 64) {
1271 if (v & (1ULL << (sg_type->u.side_basic.len_bits - 1)))
1272 v |= ~((1ULL << sg_type->u.side_basic.len_bits) - 1);
1273 }
1274 printf("%" PRId64, v);
1275 break;
1276 case TRACER_DISPLAY_BASE_16:
1277 printf("0x%" PRIx64, v);
1278 break;
1279 default:
1280 abort();
1281 }
1282 break;
1283 }
1284 default:
1285 fprintf(stderr, "<UNKNOWN SCATTER-GATHER INTEGER SIZE>");
1286 abort();
1287 }
1288 break;
1289 }
1290 default:
1291 fprintf(stderr, "<UNKNOWN SCATTER-GATHER TYPE>");
1292 abort();
1293 }
1294 printf(" }");
1295 }
1296
1297 static
1298 void tracer_print_sg_field(const struct side_struct_field_sg *field_sg, void *ptr)
1299 {
1300 printf("%s: ", field_sg->field_name);
1301 tracer_print_sg_type(&field_sg->side_type, ptr);
1302 }
1303
1304 static
1305 void tracer_print_struct_sg(const struct side_type_description *type_desc, void *ptr)
1306 {
1307 const struct side_type_struct_sg *struct_sg = type_desc->u.side_struct_sg;
1308 int i;
1309
1310 print_attributes("attr", ":", struct_sg->attr, struct_sg->nr_attr);
1311 printf("%s", struct_sg->nr_attr ? ", " : "");
1312 printf("fields: { ");
1313 for (i = 0; i < struct_sg->nr_fields; i++) {
1314 printf("%s", i ? ", " : "");
1315 tracer_print_sg_field(&struct_sg->fields_sg[i], ptr);
1316 }
1317 printf(" }");
1318 }
1319
1320 static
1321 void tracer_print_array(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc)
1322 {
1323 const struct side_arg_vec *sav = sav_desc->sav;
1324 uint32_t side_sav_len = sav_desc->len;
1325 int i;
1326
1327 if (type_desc->u.side_array.length != side_sav_len) {
1328 fprintf(stderr, "ERROR: length mismatch between description and arguments of array\n");
1329 abort();
1330 }
1331 print_attributes("attr", ":", type_desc->u.side_array.attr, type_desc->u.side_array.nr_attr);
1332 printf("%s", type_desc->u.side_array.nr_attr ? ", " : "");
1333 printf("elements: ");
1334 printf("[ ");
1335 for (i = 0; i < side_sav_len; i++) {
1336 printf("%s", i ? ", " : "");
1337 tracer_print_type(type_desc->u.side_array.elem_type, &sav[i]);
1338 }
1339 printf(" ]");
1340 }
1341
1342 static
1343 void tracer_print_vla(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc)
1344 {
1345 const struct side_arg_vec *sav = sav_desc->sav;
1346 uint32_t side_sav_len = sav_desc->len;
1347 int i;
1348
1349 print_attributes("attr", ":", type_desc->u.side_vla.attr, type_desc->u.side_vla.nr_attr);
1350 printf("%s", type_desc->u.side_vla.nr_attr ? ", " : "");
1351 printf("elements: ");
1352 printf("[ ");
1353 for (i = 0; i < side_sav_len; i++) {
1354 printf("%s", i ? ", " : "");
1355 tracer_print_type(type_desc->u.side_vla.elem_type, &sav[i]);
1356 }
1357 printf(" ]");
1358 }
1359
1360 struct tracer_visitor_priv {
1361 const struct side_type_description *elem_type;
1362 int i;
1363 };
1364
1365 static
1366 enum side_visitor_status tracer_write_elem_cb(const struct side_tracer_visitor_ctx *tracer_ctx,
1367 const struct side_arg_vec *elem)
1368 {
1369 struct tracer_visitor_priv *tracer_priv = tracer_ctx->priv;
1370
1371 printf("%s", tracer_priv->i++ ? ", " : "");
1372 tracer_print_type(tracer_priv->elem_type, elem);
1373 return SIDE_VISITOR_STATUS_OK;
1374 }
1375
1376 static
1377 void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *app_ctx)
1378 {
1379 enum side_visitor_status status;
1380 struct tracer_visitor_priv tracer_priv = {
1381 .elem_type = type_desc->u.side_vla_visitor.elem_type,
1382 .i = 0,
1383 };
1384 const struct side_tracer_visitor_ctx tracer_ctx = {
1385 .write_elem = tracer_write_elem_cb,
1386 .priv = &tracer_priv,
1387 };
1388
1389 print_attributes("attr", ":", type_desc->u.side_vla_visitor.attr, type_desc->u.side_vla_visitor.nr_attr);
1390 printf("%s", type_desc->u.side_vla_visitor.nr_attr ? ", " : "");
1391 printf("elements: ");
1392 printf("[ ");
1393 status = type_desc->u.side_vla_visitor.visitor(&tracer_ctx, app_ctx);
1394 switch (status) {
1395 case SIDE_VISITOR_STATUS_OK:
1396 break;
1397 case SIDE_VISITOR_STATUS_ERROR:
1398 fprintf(stderr, "ERROR: Visitor error\n");
1399 abort();
1400 }
1401 printf(" ]");
1402 }
1403
1404 void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
1405 {
1406 const struct side_type_description *elem_type = type_desc->u.side_array.elem_type;
1407 uint32_t side_sav_len = type_desc->u.side_array.length;
1408 void *p = item->u.side_array_fixint;
1409 enum side_type side_type;
1410 int i;
1411
1412 print_attributes("attr", ":", type_desc->u.side_array.attr, type_desc->u.side_array.nr_attr);
1413 printf("%s", type_desc->u.side_array.nr_attr ? ", " : "");
1414 printf("elements: ");
1415 switch (item->type) {
1416 case SIDE_TYPE_ARRAY_U8:
1417 if (elem_type->type != SIDE_TYPE_U8)
1418 goto type_error;
1419 break;
1420 case SIDE_TYPE_ARRAY_U16:
1421 if (elem_type->type != SIDE_TYPE_U16)
1422 goto type_error;
1423 break;
1424 case SIDE_TYPE_ARRAY_U32:
1425 if (elem_type->type != SIDE_TYPE_U32)
1426 goto type_error;
1427 break;
1428 case SIDE_TYPE_ARRAY_U64:
1429 if (elem_type->type != SIDE_TYPE_U64)
1430 goto type_error;
1431 break;
1432 case SIDE_TYPE_ARRAY_S8:
1433 if (elem_type->type != SIDE_TYPE_S8)
1434 goto type_error;
1435 break;
1436 case SIDE_TYPE_ARRAY_S16:
1437 if (elem_type->type != SIDE_TYPE_S16)
1438 goto type_error;
1439 break;
1440 case SIDE_TYPE_ARRAY_S32:
1441 if (elem_type->type != SIDE_TYPE_S32)
1442 goto type_error;
1443 break;
1444 case SIDE_TYPE_ARRAY_S64:
1445 if (elem_type->type != SIDE_TYPE_S64)
1446 goto type_error;
1447 break;
1448 case SIDE_TYPE_ARRAY_BYTE:
1449 if (elem_type->type != SIDE_TYPE_BYTE)
1450 goto type_error;
1451 break;
1452 case SIDE_TYPE_ARRAY_POINTER32:
1453 if (elem_type->type != SIDE_TYPE_POINTER32)
1454 goto type_error;
1455 case SIDE_TYPE_ARRAY_POINTER64:
1456 if (elem_type->type != SIDE_TYPE_POINTER64)
1457 goto type_error;
1458 break;
1459 default:
1460 goto type_error;
1461 }
1462 side_type = elem_type->type;
1463
1464 printf("[ ");
1465 for (i = 0; i < side_sav_len; i++) {
1466 struct side_arg_vec sav_elem = {
1467 .type = side_type,
1468 };
1469
1470 switch (side_type) {
1471 case SIDE_TYPE_U8:
1472 sav_elem.u.integer_value.side_u8 = ((const uint8_t *) p)[i];
1473 break;
1474 case SIDE_TYPE_S8:
1475 sav_elem.u.integer_value.side_s8 = ((const int8_t *) p)[i];
1476 break;
1477 case SIDE_TYPE_U16:
1478 sav_elem.u.integer_value.side_u16 = ((const uint16_t *) p)[i];
1479 break;
1480 case SIDE_TYPE_S16:
1481 sav_elem.u.integer_value.side_s16 = ((const int16_t *) p)[i];
1482 break;
1483 case SIDE_TYPE_U32:
1484 sav_elem.u.integer_value.side_u32 = ((const uint32_t *) p)[i];
1485 break;
1486 case SIDE_TYPE_S32:
1487 sav_elem.u.integer_value.side_s32 = ((const int32_t *) p)[i];
1488 break;
1489 case SIDE_TYPE_U64:
1490 sav_elem.u.integer_value.side_u64 = ((const uint64_t *) p)[i];
1491 break;
1492 case SIDE_TYPE_S64:
1493 sav_elem.u.integer_value.side_s64 = ((const int64_t *) p)[i];
1494 break;
1495 case SIDE_TYPE_BYTE:
1496 sav_elem.u.side_byte = ((const uint8_t *) p)[i];
1497 break;
1498 case SIDE_TYPE_POINTER32:
1499 sav_elem.u.integer_value.side_u32 = ((const uint32_t *) p)[i];
1500 break;
1501 case SIDE_TYPE_POINTER64:
1502 sav_elem.u.integer_value.side_u64 = ((const uint64_t *) p)[i];
1503 break;
1504
1505 default:
1506 fprintf(stderr, "ERROR: Unexpected type\n");
1507 abort();
1508 }
1509
1510 printf("%s", i ? ", " : "");
1511 tracer_print_type(elem_type, &sav_elem);
1512 }
1513 printf(" ]");
1514 return;
1515
1516 type_error:
1517 fprintf(stderr, "ERROR: type mismatch\n");
1518 abort();
1519 }
1520
1521 void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
1522 {
1523 const struct side_type_description *elem_type = type_desc->u.side_vla.elem_type;
1524 uint32_t side_sav_len = item->u.side_vla_fixint.length;
1525 void *p = item->u.side_vla_fixint.p;
1526 enum side_type side_type;
1527 int i;
1528
1529 print_attributes("attr", ":", type_desc->u.side_vla.attr, type_desc->u.side_vla.nr_attr);
1530 printf("%s", type_desc->u.side_vla.nr_attr ? ", " : "");
1531 printf("elements: ");
1532 switch (item->type) {
1533 case SIDE_TYPE_VLA_U8:
1534 if (elem_type->type != SIDE_TYPE_U8)
1535 goto type_error;
1536 break;
1537 case SIDE_TYPE_VLA_U16:
1538 if (elem_type->type != SIDE_TYPE_U16)
1539 goto type_error;
1540 break;
1541 case SIDE_TYPE_VLA_U32:
1542 if (elem_type->type != SIDE_TYPE_U32)
1543 goto type_error;
1544 break;
1545 case SIDE_TYPE_VLA_U64:
1546 if (elem_type->type != SIDE_TYPE_U64)
1547 goto type_error;
1548 break;
1549 case SIDE_TYPE_VLA_S8:
1550 if (elem_type->type != SIDE_TYPE_S8)
1551 goto type_error;
1552 break;
1553 case SIDE_TYPE_VLA_S16:
1554 if (elem_type->type != SIDE_TYPE_S16)
1555 goto type_error;
1556 break;
1557 case SIDE_TYPE_VLA_S32:
1558 if (elem_type->type != SIDE_TYPE_S32)
1559 goto type_error;
1560 break;
1561 case SIDE_TYPE_VLA_S64:
1562 if (elem_type->type != SIDE_TYPE_S64)
1563 goto type_error;
1564 break;
1565 case SIDE_TYPE_VLA_BYTE:
1566 if (elem_type->type != SIDE_TYPE_BYTE)
1567 goto type_error;
1568 break;
1569 case SIDE_TYPE_VLA_POINTER32:
1570 if (elem_type->type != SIDE_TYPE_POINTER32)
1571 goto type_error;
1572 case SIDE_TYPE_VLA_POINTER64:
1573 if (elem_type->type != SIDE_TYPE_POINTER64)
1574 goto type_error;
1575 break;
1576 default:
1577 goto type_error;
1578 }
1579 side_type = elem_type->type;
1580
1581 printf("[ ");
1582 for (i = 0; i < side_sav_len; i++) {
1583 struct side_arg_vec sav_elem = {
1584 .type = side_type,
1585 };
1586
1587 switch (side_type) {
1588 case SIDE_TYPE_U8:
1589 sav_elem.u.integer_value.side_u8 = ((const uint8_t *) p)[i];
1590 break;
1591 case SIDE_TYPE_S8:
1592 sav_elem.u.integer_value.side_s8 = ((const int8_t *) p)[i];
1593 break;
1594 case SIDE_TYPE_U16:
1595 sav_elem.u.integer_value.side_u16 = ((const uint16_t *) p)[i];
1596 break;
1597 case SIDE_TYPE_S16:
1598 sav_elem.u.integer_value.side_s16 = ((const int16_t *) p)[i];
1599 break;
1600 case SIDE_TYPE_U32:
1601 sav_elem.u.integer_value.side_u32 = ((const uint32_t *) p)[i];
1602 break;
1603 case SIDE_TYPE_S32:
1604 sav_elem.u.integer_value.side_s32 = ((const int32_t *) p)[i];
1605 break;
1606 case SIDE_TYPE_U64:
1607 sav_elem.u.integer_value.side_u64 = ((const uint64_t *) p)[i];
1608 break;
1609 case SIDE_TYPE_S64:
1610 sav_elem.u.integer_value.side_s64 = ((const int64_t *) p)[i];
1611 break;
1612 case SIDE_TYPE_BYTE:
1613 sav_elem.u.side_byte = ((const uint8_t *) p)[i];
1614 break;
1615 case SIDE_TYPE_POINTER32:
1616 sav_elem.u.integer_value.side_u32 = ((const uint32_t *) p)[i];
1617 break;
1618 case SIDE_TYPE_POINTER64:
1619 sav_elem.u.integer_value.side_u64 = ((const uint64_t *) p)[i];
1620 break;
1621
1622 default:
1623 fprintf(stderr, "ERROR: Unexpected type\n");
1624 abort();
1625 }
1626
1627 printf("%s", i ? ", " : "");
1628 tracer_print_type(elem_type, &sav_elem);
1629 }
1630 printf(" ]");
1631 return;
1632
1633 type_error:
1634 fprintf(stderr, "ERROR: type mismatch\n");
1635 abort();
1636 }
1637
1638 static
1639 void tracer_print_dynamic_struct(const struct side_arg_dynamic_event_struct *dynamic_struct)
1640 {
1641 const struct side_arg_dynamic_event_field *fields = dynamic_struct->fields;
1642 uint32_t len = dynamic_struct->len;
1643 int i;
1644
1645 print_attributes("attr", "::", dynamic_struct->attr, dynamic_struct->nr_attr);
1646 printf("%s", dynamic_struct->nr_attr ? ", " : "");
1647 printf("fields:: ");
1648 printf("[ ");
1649 for (i = 0; i < len; i++) {
1650 printf("%s", i ? ", " : "");
1651 printf("%s:: ", fields[i].field_name);
1652 tracer_print_dynamic(&fields[i].elem);
1653 }
1654 printf(" ]");
1655 }
1656
1657 struct tracer_dynamic_struct_visitor_priv {
1658 int i;
1659 };
1660
1661 static
1662 enum side_visitor_status tracer_dynamic_struct_write_elem_cb(
1663 const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
1664 const struct side_arg_dynamic_event_field *dynamic_field)
1665 {
1666 struct tracer_dynamic_struct_visitor_priv *tracer_priv = tracer_ctx->priv;
1667
1668 printf("%s", tracer_priv->i++ ? ", " : "");
1669 printf("%s:: ", dynamic_field->field_name);
1670 tracer_print_dynamic(&dynamic_field->elem);
1671 return SIDE_VISITOR_STATUS_OK;
1672 }
1673
1674 static
1675 void tracer_print_dynamic_struct_visitor(const struct side_arg_dynamic_vec *item)
1676 {
1677 enum side_visitor_status status;
1678 struct tracer_dynamic_struct_visitor_priv tracer_priv = {
1679 .i = 0,
1680 };
1681 const struct side_tracer_dynamic_struct_visitor_ctx tracer_ctx = {
1682 .write_field = tracer_dynamic_struct_write_elem_cb,
1683 .priv = &tracer_priv,
1684 };
1685 void *app_ctx = item->u.side_dynamic_struct_visitor.app_ctx;
1686
1687 print_attributes("attr", "::", item->u.side_dynamic_struct_visitor.attr, item->u.side_dynamic_struct_visitor.nr_attr);
1688 printf("%s", item->u.side_dynamic_struct_visitor.nr_attr ? ", " : "");
1689 printf("fields:: ");
1690 printf("[ ");
1691 status = item->u.side_dynamic_struct_visitor.visitor(&tracer_ctx, app_ctx);
1692 switch (status) {
1693 case SIDE_VISITOR_STATUS_OK:
1694 break;
1695 case SIDE_VISITOR_STATUS_ERROR:
1696 fprintf(stderr, "ERROR: Visitor error\n");
1697 abort();
1698 }
1699 printf(" ]");
1700 }
1701
1702 static
1703 void tracer_print_dynamic_vla(const struct side_arg_dynamic_vec_vla *vla)
1704 {
1705 const struct side_arg_dynamic_vec *sav = vla->sav;
1706 uint32_t side_sav_len = vla->len;
1707 int i;
1708
1709 print_attributes("attr", "::", vla->attr, vla->nr_attr);
1710 printf("%s", vla->nr_attr ? ", " : "");
1711 printf("elements:: ");
1712 printf("[ ");
1713 for (i = 0; i < side_sav_len; i++) {
1714 printf("%s", i ? ", " : "");
1715 tracer_print_dynamic(&sav[i]);
1716 }
1717 printf(" ]");
1718 }
1719
1720 struct tracer_dynamic_vla_visitor_priv {
1721 int i;
1722 };
1723
1724 static
1725 enum side_visitor_status tracer_dynamic_vla_write_elem_cb(
1726 const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx,
1727 const struct side_arg_dynamic_vec *elem)
1728 {
1729 struct tracer_dynamic_vla_visitor_priv *tracer_priv = tracer_ctx->priv;
1730
1731 printf("%s", tracer_priv->i++ ? ", " : "");
1732 tracer_print_dynamic(elem);
1733 return SIDE_VISITOR_STATUS_OK;
1734 }
1735
1736 static
1737 void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item)
1738 {
1739 enum side_visitor_status status;
1740 struct tracer_dynamic_vla_visitor_priv tracer_priv = {
1741 .i = 0,
1742 };
1743 const struct side_tracer_dynamic_vla_visitor_ctx tracer_ctx = {
1744 .write_elem = tracer_dynamic_vla_write_elem_cb,
1745 .priv = &tracer_priv,
1746 };
1747 void *app_ctx = item->u.side_dynamic_vla_visitor.app_ctx;
1748
1749 print_attributes("attr", "::", item->u.side_dynamic_vla_visitor.attr, item->u.side_dynamic_vla_visitor.nr_attr);
1750 printf("%s", item->u.side_dynamic_vla_visitor.nr_attr ? ", " : "");
1751 printf("elements:: ");
1752 printf("[ ");
1753 status = item->u.side_dynamic_vla_visitor.visitor(&tracer_ctx, app_ctx);
1754 switch (status) {
1755 case SIDE_VISITOR_STATUS_OK:
1756 break;
1757 case SIDE_VISITOR_STATUS_ERROR:
1758 fprintf(stderr, "ERROR: Visitor error\n");
1759 abort();
1760 }
1761 printf(" ]");
1762 }
1763
1764 static
1765 void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
1766 {
1767 enum tracer_display_base base = TRACER_DISPLAY_BASE_10;
1768
1769 switch (item->dynamic_type) {
1770 case SIDE_DYNAMIC_TYPE_U8:
1771 case SIDE_DYNAMIC_TYPE_U16:
1772 case SIDE_DYNAMIC_TYPE_U32:
1773 case SIDE_DYNAMIC_TYPE_U64:
1774 case SIDE_DYNAMIC_TYPE_S8:
1775 case SIDE_DYNAMIC_TYPE_S16:
1776 case SIDE_DYNAMIC_TYPE_S32:
1777 case SIDE_DYNAMIC_TYPE_S64:
1778 base = get_attr_display_base(item->u.side_integer.type.attr,
1779 item->u.side_integer.type.nr_attr);
1780 break;
1781 default:
1782 break;
1783 }
1784
1785 printf("{ ");
1786 switch (item->dynamic_type) {
1787 case SIDE_DYNAMIC_TYPE_NULL:
1788 tracer_print_type_header("::", item->u.side_basic.attr, item->u.side_basic.nr_attr);
1789 printf("<NULL TYPE>");
1790 break;
1791 case SIDE_DYNAMIC_TYPE_BOOL:
1792 tracer_print_type_header("::", item->u.side_basic.attr, item->u.side_basic.nr_attr);
1793 printf("%s", item->u.side_basic.u.side_bool ? "true" : "false");
1794 break;
1795 case SIDE_DYNAMIC_TYPE_U8:
1796 {
1797 uint8_t v;
1798
1799 v = item->u.side_integer.value.side_u8;
1800 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1801 switch (base) {
1802 case TRACER_DISPLAY_BASE_2:
1803 print_integer_binary(v, 8);
1804 break;
1805 case TRACER_DISPLAY_BASE_8:
1806 printf("0%" PRIo8, v);
1807 break;
1808 case TRACER_DISPLAY_BASE_10:
1809 printf("%" PRIu8, v);
1810 break;
1811 case TRACER_DISPLAY_BASE_16:
1812 printf("0x%" PRIx8, v);
1813 break;
1814 default:
1815 abort();
1816 }
1817 break;
1818 }
1819 case SIDE_DYNAMIC_TYPE_U16:
1820 {
1821 uint16_t v;
1822
1823 v = item->u.side_integer.value.side_u16;
1824 if (dynamic_type_to_host_reverse_bo(item))
1825 v = side_bswap_16(v);
1826 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1827 switch (base) {
1828 case TRACER_DISPLAY_BASE_2:
1829 print_integer_binary(v, 16);
1830 break;
1831 case TRACER_DISPLAY_BASE_8:
1832 printf("0%" PRIo16, v);
1833 break;
1834 case TRACER_DISPLAY_BASE_10:
1835 printf("%" PRIu16, v);
1836 break;
1837 case TRACER_DISPLAY_BASE_16:
1838 printf("0x%" PRIx16, v);
1839 break;
1840 default:
1841 abort();
1842 }
1843 break;
1844 }
1845 case SIDE_DYNAMIC_TYPE_U32:
1846 {
1847 uint32_t v;
1848
1849 v = item->u.side_integer.value.side_u32;
1850 if (dynamic_type_to_host_reverse_bo(item))
1851 v = side_bswap_32(v);
1852 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1853 switch (base) {
1854 case TRACER_DISPLAY_BASE_2:
1855 print_integer_binary(v, 32);
1856 break;
1857 case TRACER_DISPLAY_BASE_8:
1858 printf("0%" PRIo32, v);
1859 break;
1860 case TRACER_DISPLAY_BASE_10:
1861 printf("%" PRIu32, v);
1862 break;
1863 case TRACER_DISPLAY_BASE_16:
1864 printf("0x%" PRIx32, v);
1865 break;
1866 default:
1867 abort();
1868 }
1869 break;
1870 }
1871 case SIDE_DYNAMIC_TYPE_U64:
1872 {
1873 uint64_t v;
1874
1875 v = item->u.side_integer.value.side_u64;
1876 if (dynamic_type_to_host_reverse_bo(item))
1877 v = side_bswap_64(v);
1878 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1879 switch (base) {
1880 case TRACER_DISPLAY_BASE_2:
1881 print_integer_binary(v, 64);
1882 break;
1883 case TRACER_DISPLAY_BASE_8:
1884 printf("0%" PRIo64, v);
1885 break;
1886 case TRACER_DISPLAY_BASE_10:
1887 printf("%" PRIu64, v);
1888 break;
1889 case TRACER_DISPLAY_BASE_16:
1890 printf("0x%" PRIx64, v);
1891 break;
1892 default:
1893 abort();
1894 }
1895 break;
1896 }
1897 case SIDE_DYNAMIC_TYPE_S8:
1898 {
1899 int8_t v;
1900
1901 v = item->u.side_integer.value.side_s8;
1902 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1903 switch (base) {
1904 case TRACER_DISPLAY_BASE_2:
1905 print_integer_binary(v, 8);
1906 break;
1907 case TRACER_DISPLAY_BASE_8:
1908 printf("0%" PRIo8, v);
1909 break;
1910 case TRACER_DISPLAY_BASE_10:
1911 printf("%" PRId8, v);
1912 break;
1913 case TRACER_DISPLAY_BASE_16:
1914 printf("0x%" PRIx8, v);
1915 break;
1916 default:
1917 abort();
1918 }
1919 break;
1920 }
1921 case SIDE_DYNAMIC_TYPE_S16:
1922 {
1923 int16_t v;
1924
1925 v = item->u.side_integer.value.side_s16;
1926 if (dynamic_type_to_host_reverse_bo(item))
1927 v = side_bswap_16(v);
1928 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1929 switch (base) {
1930 case TRACER_DISPLAY_BASE_2:
1931 print_integer_binary(v, 16);
1932 break;
1933 case TRACER_DISPLAY_BASE_8:
1934 printf("0%" PRIo16, v);
1935 break;
1936 case TRACER_DISPLAY_BASE_10:
1937 printf("%" PRId16, v);
1938 break;
1939 case TRACER_DISPLAY_BASE_16:
1940 printf("0x%" PRIx16, v);
1941 break;
1942 default:
1943 abort();
1944 }
1945 break;
1946 }
1947 case SIDE_DYNAMIC_TYPE_S32:
1948 {
1949 int32_t v;
1950
1951 v = item->u.side_integer.value.side_s32;
1952 if (dynamic_type_to_host_reverse_bo(item))
1953 v = side_bswap_32(v);
1954 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1955 switch (base) {
1956 case TRACER_DISPLAY_BASE_2:
1957 print_integer_binary(v, 32);
1958 break;
1959 case TRACER_DISPLAY_BASE_8:
1960 printf("0%" PRIo32, v);
1961 break;
1962 case TRACER_DISPLAY_BASE_10:
1963 printf("%" PRId32, v);
1964 break;
1965 case TRACER_DISPLAY_BASE_16:
1966 printf("0x%" PRIx32, v);
1967 break;
1968 default:
1969 abort();
1970 }
1971 break;
1972 }
1973 case SIDE_DYNAMIC_TYPE_S64:
1974 {
1975 int64_t v;
1976
1977 v = item->u.side_integer.value.side_s64;
1978 if (dynamic_type_to_host_reverse_bo(item))
1979 v = side_bswap_64(v);
1980 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
1981 switch (base) {
1982 case TRACER_DISPLAY_BASE_2:
1983 print_integer_binary(v, 64);
1984 break;
1985 case TRACER_DISPLAY_BASE_8:
1986 printf("0%" PRIo64, v);
1987 break;
1988 case TRACER_DISPLAY_BASE_10:
1989 printf("%" PRId64, v);
1990 break;
1991 case TRACER_DISPLAY_BASE_16:
1992 printf("0x%" PRIx64, v);
1993 break;
1994 default:
1995 abort();
1996 }
1997 break;
1998 }
1999 case SIDE_DYNAMIC_TYPE_BYTE:
2000 tracer_print_type_header("::", item->u.side_basic.attr, item->u.side_basic.nr_attr);
2001 printf("0x%" PRIx8, item->u.side_basic.u.side_byte);
2002 break;
2003 case SIDE_DYNAMIC_TYPE_POINTER32:
2004 {
2005 uint32_t v;
2006
2007 v = item->u.side_integer.value.side_u32;
2008 if (dynamic_type_to_host_reverse_bo(item))
2009 v = side_bswap_32(v);
2010 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
2011 printf("0x%" PRIx32, v);
2012 break;
2013 }
2014
2015 case SIDE_DYNAMIC_TYPE_POINTER64:
2016 {
2017 uint64_t v;
2018
2019 v = item->u.side_integer.value.side_u64;
2020 if (dynamic_type_to_host_reverse_bo(item))
2021 v = side_bswap_64(v);
2022 tracer_print_type_header("::", item->u.side_integer.type.attr, item->u.side_integer.type.nr_attr);
2023 printf("0x%" PRIx64, v);
2024 break;
2025 }
2026
2027 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY16:
2028 {
2029 #if __HAVE_FLOAT16
2030 union {
2031 _Float16 f;
2032 uint16_t u;
2033 } float16 = {
2034 .f = item->u.side_float.value.side_float_binary16,
2035 };
2036
2037 if (dynamic_type_to_host_reverse_bo(item))
2038 float16.u = side_bswap_16(float16.u);
2039 tracer_print_type_header("::", item->u.side_float.type.attr, item->u.side_float.type.nr_attr);
2040 printf("%g", (double) float16.f);
2041 break;
2042 #else
2043 fprintf(stderr, "ERROR: Unsupported binary16 float type\n");
2044 abort();
2045 #endif
2046 }
2047 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY32:
2048 {
2049 #if __HAVE_FLOAT32
2050 union {
2051 _Float32 f;
2052 uint32_t u;
2053 } float32 = {
2054 .f = item->u.side_float.value.side_float_binary32,
2055 };
2056
2057 if (dynamic_type_to_host_reverse_bo(item))
2058 float32.u = side_bswap_32(float32.u);
2059 tracer_print_type_header("::", item->u.side_float.type.attr, item->u.side_float.type.nr_attr);
2060 printf("%g", (double) float32.f);
2061 break;
2062 #else
2063 fprintf(stderr, "ERROR: Unsupported binary32 float type\n");
2064 abort();
2065 #endif
2066 }
2067 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY64:
2068 {
2069 #if __HAVE_FLOAT64
2070 union {
2071 _Float64 f;
2072 uint64_t u;
2073 } float64 = {
2074 .f = item->u.side_float.value.side_float_binary64,
2075 };
2076
2077 if (dynamic_type_to_host_reverse_bo(item))
2078 float64.u = side_bswap_64(float64.u);
2079 tracer_print_type_header("::", item->u.side_float.type.attr, item->u.side_float.type.nr_attr);
2080 printf("%g", (double) float64.f);
2081 break;
2082 #else
2083 fprintf(stderr, "ERROR: Unsupported binary64 float type\n");
2084 abort();
2085 #endif
2086 }
2087 case SIDE_DYNAMIC_TYPE_FLOAT_BINARY128:
2088 {
2089 #if __HAVE_FLOAT128
2090 union {
2091 _Float128 f;
2092 char arr[16];
2093 } float128 = {
2094 .f = item->u.side_float.value.side_float_binary128,
2095 };
2096
2097 if (dynamic_type_to_host_reverse_bo(item))
2098 side_bswap_128p(float128.arr);
2099 tracer_print_type_header("::", item->u.side_float.type.attr, item->u.side_float.type.nr_attr);
2100 printf("%Lg", (long double) float128.f);
2101 break;
2102 #else
2103 fprintf(stderr, "ERROR: Unsupported binary128 float type\n");
2104 abort();
2105 #endif
2106 }
2107 case SIDE_DYNAMIC_TYPE_STRING:
2108 tracer_print_type_header("::", item->u.side_basic.attr, item->u.side_basic.nr_attr);
2109 printf("\"%s\"", (const char *)(uintptr_t) item->u.side_basic.u.string);
2110 break;
2111 case SIDE_DYNAMIC_TYPE_STRUCT:
2112 tracer_print_dynamic_struct(item->u.side_dynamic_struct);
2113 break;
2114 case SIDE_DYNAMIC_TYPE_STRUCT_VISITOR:
2115 tracer_print_dynamic_struct_visitor(item);
2116 break;
2117 case SIDE_DYNAMIC_TYPE_VLA:
2118 tracer_print_dynamic_vla(item->u.side_dynamic_vla);
2119 break;
2120 case SIDE_DYNAMIC_TYPE_VLA_VISITOR:
2121 tracer_print_dynamic_vla_visitor(item);
2122 break;
2123 default:
2124 fprintf(stderr, "<UNKNOWN TYPE>");
2125 abort();
2126 }
2127 printf(" }");
2128 }
2129
2130 static
2131 void tracer_print_static_fields(const struct side_event_description *desc,
2132 const struct side_arg_vec_description *sav_desc,
2133 int *nr_items)
2134 {
2135 const struct side_arg_vec *sav = sav_desc->sav;
2136 uint32_t side_sav_len = sav_desc->len;
2137 int i;
2138
2139 printf("provider: %s, event: %s", desc->provider_name, desc->event_name);
2140 if (desc->nr_fields != side_sav_len) {
2141 fprintf(stderr, "ERROR: number of fields mismatch between description and arguments\n");
2142 abort();
2143 }
2144 print_attributes(", attr", ":", desc->attr, desc->nr_attr);
2145 printf("%s", side_sav_len ? ", fields: [ " : "");
2146 for (i = 0; i < side_sav_len; i++) {
2147 printf("%s", i ? ", " : "");
2148 tracer_print_field(&desc->fields[i], &sav[i]);
2149 }
2150 if (nr_items)
2151 *nr_items = i;
2152 if (side_sav_len)
2153 printf(" ]");
2154 }
2155
2156 void tracer_call(const struct side_event_description *desc,
2157 const struct side_arg_vec_description *sav_desc,
2158 void *priv __attribute__((unused)))
2159 {
2160 int nr_fields = 0;
2161
2162 tracer_print_static_fields(desc, sav_desc, &nr_fields);
2163 printf("\n");
2164 }
2165
2166 void tracer_call_variadic(const struct side_event_description *desc,
2167 const struct side_arg_vec_description *sav_desc,
2168 const struct side_arg_dynamic_event_struct *var_struct,
2169 void *priv __attribute__((unused)))
2170 {
2171 uint32_t var_struct_len = var_struct->len;
2172 int nr_fields = 0, i;
2173
2174 tracer_print_static_fields(desc, sav_desc, &nr_fields);
2175
2176 if (side_unlikely(!(desc->flags & SIDE_EVENT_FLAG_VARIADIC))) {
2177 fprintf(stderr, "ERROR: unexpected non-variadic event description\n");
2178 abort();
2179 }
2180 print_attributes(", attr ", "::", var_struct->attr, var_struct->nr_attr);
2181 printf("%s", var_struct_len ? ", fields:: [ " : "");
2182 for (i = 0; i < var_struct_len; i++, nr_fields++) {
2183 printf("%s", i ? ", " : "");
2184 printf("%s:: ", var_struct->fields[i].field_name);
2185 tracer_print_dynamic(&var_struct->fields[i].elem);
2186 }
2187 if (i)
2188 printf(" ]");
2189 printf("\n");
2190 }
2191
2192 void tracer_event_notification(enum side_tracer_notification notif,
2193 struct side_event_description **events, uint32_t nr_events, void *priv)
2194 {
2195 uint32_t i;
2196 int ret;
2197
2198 printf("----------------------------------------------------------\n");
2199 printf("Tracer notified of events %s\n",
2200 notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS ? "inserted" : "removed");
2201 for (i = 0; i < nr_events; i++) {
2202 struct side_event_description *event = events[i];
2203
2204 /* Skip NULL pointers */
2205 if (!event)
2206 continue;
2207 printf("provider: %s, event: %s\n",
2208 event->provider_name, event->event_name);
2209 if (notif == SIDE_TRACER_NOTIFICATION_INSERT_EVENTS) {
2210 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
2211 ret = side_tracer_callback_variadic_register(event, tracer_call_variadic, NULL);
2212 if (ret)
2213 abort();
2214 } else {
2215 ret = side_tracer_callback_register(event, tracer_call, NULL);
2216 if (ret)
2217 abort();
2218 }
2219 } else {
2220 if (event->flags & SIDE_EVENT_FLAG_VARIADIC) {
2221 ret = side_tracer_callback_variadic_unregister(event, tracer_call_variadic, NULL);
2222 if (ret)
2223 abort();
2224 } else {
2225 ret = side_tracer_callback_unregister(event, tracer_call, NULL);
2226 if (ret)
2227 abort();
2228 }
2229 }
2230 }
2231 printf("----------------------------------------------------------\n");
2232 }
2233
2234 static __attribute__((constructor))
2235 void tracer_init(void);
2236 static
2237 void tracer_init(void)
2238 {
2239 tracer_handle = side_tracer_event_notification_register(tracer_event_notification, NULL);
2240 if (!tracer_handle)
2241 abort();
2242 }
2243
2244 static __attribute__((destructor))
2245 void tracer_exit(void);
2246 static
2247 void tracer_exit(void)
2248 {
2249 side_tracer_event_notification_unregister(tracer_handle);
2250 }
This page took 0.096125 seconds and 4 git commands to generate.