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