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