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