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> |
f611d0c3 MD |
11 | |
12 | #include <side/trace.h> | |
13 | ||
14 | static | |
15 | void tracer_print_struct(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc); | |
16 | static | |
17 | void tracer_print_array(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc); | |
18 | static | |
19 | void tracer_print_vla(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc); | |
20 | static | |
352a4b77 | 21 | void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *app_ctx); |
ba845af5 MD |
22 | static |
23 | void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item); | |
1533629f MD |
24 | static |
25 | void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item); | |
a2e2357e MD |
26 | static |
27 | void tracer_print_dynamic(const struct side_arg_dynamic_vec *dynamic_item); | |
f611d0c3 | 28 | |
bc3c89b3 MD |
29 | static |
30 | void tracer_print_attr_type(const struct side_attr *attr) | |
31 | { | |
32 | printf("{ key: \"%s\", value: ", attr->key); | |
33 | switch (attr->value.type) { | |
34 | case SIDE_ATTR_TYPE_BOOL: | |
35 | printf("%s", attr->value.u.side_bool ? "true" : "false"); | |
36 | break; | |
37 | case SIDE_ATTR_TYPE_U8: | |
38 | printf("%" PRIu8, attr->value.u.side_u8); | |
39 | break; | |
40 | case SIDE_ATTR_TYPE_U16: | |
41 | printf("%" PRIu16, attr->value.u.side_u16); | |
42 | break; | |
43 | case SIDE_ATTR_TYPE_U32: | |
44 | printf("%" PRIu32, attr->value.u.side_u32); | |
45 | break; | |
46 | case SIDE_ATTR_TYPE_U64: | |
47 | printf("%" PRIu64, attr->value.u.side_u64); | |
48 | break; | |
49 | case SIDE_ATTR_TYPE_S8: | |
50 | printf("%" PRId8, attr->value.u.side_s8); | |
51 | break; | |
52 | case SIDE_ATTR_TYPE_S16: | |
53 | printf("%" PRId16, attr->value.u.side_s16); | |
54 | break; | |
55 | case SIDE_ATTR_TYPE_S32: | |
56 | printf("%" PRId32, attr->value.u.side_s32); | |
57 | break; | |
58 | case SIDE_ATTR_TYPE_S64: | |
59 | printf("%" PRId64, attr->value.u.side_s64); | |
60 | break; | |
61 | case SIDE_ATTR_TYPE_FLOAT_BINARY16: | |
62 | #if __HAVE_FLOAT16 | |
63 | printf("%g", (double) attr->value.u.side_float_binary16); | |
64 | break; | |
65 | #else | |
66 | printf("ERROR: Unsupported binary16 float type\n"); | |
67 | abort(); | |
68 | #endif | |
69 | case SIDE_ATTR_TYPE_FLOAT_BINARY32: | |
70 | #if __HAVE_FLOAT32 | |
71 | printf("%g", (double) attr->value.u.side_float_binary32); | |
72 | break; | |
73 | #else | |
74 | printf("ERROR: Unsupported binary32 float type\n"); | |
75 | abort(); | |
76 | #endif | |
77 | case SIDE_ATTR_TYPE_FLOAT_BINARY64: | |
78 | #if __HAVE_FLOAT64 | |
79 | printf("%g", (double) attr->value.u.side_float_binary64); | |
80 | break; | |
81 | #else | |
82 | printf("ERROR: Unsupported binary64 float type\n"); | |
83 | abort(); | |
84 | #endif | |
85 | case SIDE_ATTR_TYPE_FLOAT_BINARY128: | |
86 | #if __HAVE_FLOAT128 | |
87 | printf("%Lg", (long double) attr->value.u.side_float_binary128); | |
88 | break; | |
89 | #else | |
90 | printf("ERROR: Unsupported binary128 float type\n"); | |
91 | abort(); | |
92 | #endif | |
93 | case SIDE_ATTR_TYPE_STRING: | |
94 | printf("\"%s\"", attr->value.u.string); | |
95 | break; | |
96 | default: | |
97 | printf("<UNKNOWN TYPE>"); | |
98 | abort(); | |
99 | } | |
100 | printf(" }"); | |
101 | } | |
102 | ||
7d21cf51 | 103 | static |
a848763d | 104 | void print_attributes(const char *prefix_str, const struct side_attr *attr, uint32_t nr_attr) |
7d21cf51 MD |
105 | { |
106 | int i; | |
107 | ||
108 | if (!nr_attr) | |
109 | return; | |
a848763d | 110 | printf("%s[ ", prefix_str); |
7d21cf51 MD |
111 | for (i = 0; i < nr_attr; i++) { |
112 | printf("%s", i ? ", " : ""); | |
bc3c89b3 | 113 | tracer_print_attr_type(&attr[i]); |
7d21cf51 MD |
114 | } |
115 | printf(" ]"); | |
116 | } | |
117 | ||
79f677ba MD |
118 | static |
119 | void print_enum(const struct side_enum_mappings *side_enum_mappings, int64_t value) | |
120 | { | |
121 | int i, print_count = 0; | |
122 | ||
123 | printf("%" PRId64 ", labels: [ ", value); | |
124 | for (i = 0; i < side_enum_mappings->nr_mappings; i++) { | |
125 | const struct side_enum_mapping *mapping = &side_enum_mappings->mappings[i]; | |
126 | ||
ea32e5fc MD |
127 | if (mapping->range_end < mapping->range_begin) { |
128 | printf("ERROR: Unexpected enum range: %" PRIu64 "-%" PRIu64 "\n", | |
129 | mapping->range_begin, mapping->range_end); | |
130 | abort(); | |
131 | } | |
79f677ba MD |
132 | if (value >= mapping->range_begin && value <= mapping->range_end) { |
133 | printf("%s", print_count++ ? ", " : ""); | |
134 | printf("\"%s\"", mapping->label); | |
135 | } | |
136 | } | |
137 | if (!print_count) | |
138 | printf("<NO LABEL>"); | |
139 | printf(" ]"); | |
140 | } | |
141 | ||
ea32e5fc | 142 | static |
66cff328 | 143 | void print_enum_bitmap(const struct side_enum_bitmap_mappings *side_enum_mappings, uint64_t value) |
ea32e5fc MD |
144 | { |
145 | int i, print_count = 0; | |
146 | ||
147 | printf("0x%" PRIx64 ", labels: [ ", value); | |
148 | for (i = 0; i < side_enum_mappings->nr_mappings; i++) { | |
66cff328 | 149 | const struct side_enum_bitmap_mapping *mapping = &side_enum_mappings->mappings[i]; |
ea32e5fc MD |
150 | bool match = false; |
151 | int64_t bit; | |
152 | ||
153 | if (mapping->range_begin < 0 || mapping->range_end > 63 | |
154 | || mapping->range_end < mapping->range_begin) { | |
155 | printf("ERROR: Unexpected enum bitmap range: %" PRIu64 "-%" PRIu64 "\n", | |
156 | mapping->range_begin, mapping->range_end); | |
157 | abort(); | |
158 | } | |
159 | for (bit = mapping->range_begin; bit <= mapping->range_end; bit++) { | |
160 | if (value & (1ULL << bit)) { | |
161 | match = true; | |
162 | break; | |
163 | } | |
164 | } | |
165 | if (match) { | |
166 | printf("%s", print_count++ ? ", " : ""); | |
167 | printf("\"%s\"", mapping->label); | |
168 | } | |
169 | } | |
170 | if (!print_count) | |
171 | printf("<NO LABEL>"); | |
172 | printf(" ]"); | |
173 | } | |
174 | ||
f611d0c3 MD |
175 | static |
176 | void tracer_print_type(const struct side_type_description *type_desc, const struct side_arg_vec *item) | |
177 | { | |
ba845af5 MD |
178 | switch (item->type) { |
179 | case SIDE_TYPE_ARRAY_U8: | |
180 | case SIDE_TYPE_ARRAY_U16: | |
181 | case SIDE_TYPE_ARRAY_U32: | |
182 | case SIDE_TYPE_ARRAY_U64: | |
183 | case SIDE_TYPE_ARRAY_S8: | |
184 | case SIDE_TYPE_ARRAY_S16: | |
185 | case SIDE_TYPE_ARRAY_S32: | |
186 | case SIDE_TYPE_ARRAY_S64: | |
187 | if (type_desc->type != SIDE_TYPE_ARRAY) { | |
188 | printf("ERROR: type mismatch between description and arguments\n"); | |
189 | abort(); | |
190 | } | |
191 | break; | |
1533629f MD |
192 | case SIDE_TYPE_VLA_U8: |
193 | case SIDE_TYPE_VLA_U16: | |
194 | case SIDE_TYPE_VLA_U32: | |
195 | case SIDE_TYPE_VLA_U64: | |
196 | case SIDE_TYPE_VLA_S8: | |
197 | case SIDE_TYPE_VLA_S16: | |
198 | case SIDE_TYPE_VLA_S32: | |
199 | case SIDE_TYPE_VLA_S64: | |
200 | if (type_desc->type != SIDE_TYPE_VLA) { | |
201 | printf("ERROR: type mismatch between description and arguments\n"); | |
202 | abort(); | |
203 | } | |
204 | break; | |
205 | ||
ba845af5 | 206 | default: |
a2e2357e | 207 | if (type_desc->type != item->type) { |
ba845af5 MD |
208 | printf("ERROR: type mismatch between description and arguments\n"); |
209 | abort(); | |
210 | } | |
211 | break; | |
f611d0c3 | 212 | } |
a848763d MD |
213 | printf("{ "); |
214 | print_attributes("attr: ", type_desc->attr, type_desc->nr_attr); | |
215 | printf("%s", type_desc->nr_attr ? ", " : ""); | |
216 | printf("value: "); | |
f611d0c3 | 217 | switch (item->type) { |
4f40d951 MD |
218 | case SIDE_TYPE_BOOL: |
219 | printf("%s", item->u.side_bool ? "true" : "false"); | |
220 | break; | |
f611d0c3 MD |
221 | case SIDE_TYPE_U8: |
222 | printf("%" PRIu8, item->u.side_u8); | |
223 | break; | |
224 | case SIDE_TYPE_U16: | |
225 | printf("%" PRIu16, item->u.side_u16); | |
226 | break; | |
227 | case SIDE_TYPE_U32: | |
228 | printf("%" PRIu32, item->u.side_u32); | |
229 | break; | |
230 | case SIDE_TYPE_U64: | |
231 | printf("%" PRIu64, item->u.side_u64); | |
232 | break; | |
233 | case SIDE_TYPE_S8: | |
234 | printf("%" PRId8, item->u.side_s8); | |
235 | break; | |
236 | case SIDE_TYPE_S16: | |
237 | printf("%" PRId16, item->u.side_s16); | |
238 | break; | |
239 | case SIDE_TYPE_S32: | |
240 | printf("%" PRId32, item->u.side_s32); | |
241 | break; | |
242 | case SIDE_TYPE_S64: | |
243 | printf("%" PRId64, item->u.side_s64); | |
244 | break; | |
79f677ba MD |
245 | |
246 | case SIDE_TYPE_ENUM_U8: | |
247 | print_enum(type_desc->u.side_enum_mappings, | |
248 | (int64_t) item->u.side_u8); | |
249 | break; | |
250 | case SIDE_TYPE_ENUM_U16: | |
251 | print_enum(type_desc->u.side_enum_mappings, | |
252 | (int64_t) item->u.side_u16); | |
253 | break; | |
254 | case SIDE_TYPE_ENUM_U32: | |
255 | print_enum(type_desc->u.side_enum_mappings, | |
256 | (int64_t) item->u.side_u32); | |
257 | break; | |
258 | case SIDE_TYPE_ENUM_U64: | |
259 | print_enum(type_desc->u.side_enum_mappings, | |
260 | (int64_t) item->u.side_u64); | |
261 | break; | |
262 | case SIDE_TYPE_ENUM_S8: | |
263 | print_enum(type_desc->u.side_enum_mappings, | |
264 | (int64_t) item->u.side_s8); | |
265 | break; | |
266 | case SIDE_TYPE_ENUM_S16: | |
267 | print_enum(type_desc->u.side_enum_mappings, | |
268 | (int64_t) item->u.side_s16); | |
269 | break; | |
270 | case SIDE_TYPE_ENUM_S32: | |
271 | print_enum(type_desc->u.side_enum_mappings, | |
272 | (int64_t) item->u.side_s32); | |
273 | break; | |
274 | case SIDE_TYPE_ENUM_S64: | |
275 | print_enum(type_desc->u.side_enum_mappings, | |
276 | item->u.side_s64); | |
277 | break; | |
278 | ||
ea32e5fc | 279 | case SIDE_TYPE_ENUM_BITMAP8: |
66cff328 | 280 | print_enum_bitmap(type_desc->u.side_enum_bitmap_mappings, |
ea32e5fc MD |
281 | (uint64_t) item->u.side_u8); |
282 | break; | |
283 | case SIDE_TYPE_ENUM_BITMAP16: | |
66cff328 | 284 | print_enum_bitmap(type_desc->u.side_enum_bitmap_mappings, |
ea32e5fc MD |
285 | (uint64_t) item->u.side_u16); |
286 | break; | |
287 | case SIDE_TYPE_ENUM_BITMAP32: | |
66cff328 | 288 | print_enum_bitmap(type_desc->u.side_enum_bitmap_mappings, |
ea32e5fc MD |
289 | (uint64_t) item->u.side_u32); |
290 | break; | |
291 | case SIDE_TYPE_ENUM_BITMAP64: | |
66cff328 | 292 | print_enum_bitmap(type_desc->u.side_enum_bitmap_mappings, |
ea32e5fc MD |
293 | item->u.side_u64); |
294 | break; | |
295 | ||
fb25b355 MD |
296 | case SIDE_TYPE_FLOAT_BINARY16: |
297 | #if __HAVE_FLOAT16 | |
298 | printf("%g", (double) item->u.side_float_binary16); | |
299 | break; | |
300 | #else | |
301 | printf("ERROR: Unsupported binary16 float type\n"); | |
302 | abort(); | |
303 | #endif | |
304 | case SIDE_TYPE_FLOAT_BINARY32: | |
305 | #if __HAVE_FLOAT32 | |
306 | printf("%g", (double) item->u.side_float_binary32); | |
307 | break; | |
308 | #else | |
309 | printf("ERROR: Unsupported binary32 float type\n"); | |
310 | abort(); | |
311 | #endif | |
312 | case SIDE_TYPE_FLOAT_BINARY64: | |
313 | #if __HAVE_FLOAT64 | |
314 | printf("%g", (double) item->u.side_float_binary64); | |
315 | break; | |
316 | #else | |
317 | printf("ERROR: Unsupported binary64 float type\n"); | |
318 | abort(); | |
319 | #endif | |
320 | case SIDE_TYPE_FLOAT_BINARY128: | |
321 | #if __HAVE_FLOAT128 | |
322 | printf("%Lg", (long double) item->u.side_float_binary128); | |
323 | break; | |
324 | #else | |
325 | printf("ERROR: Unsupported binary128 float type\n"); | |
326 | abort(); | |
327 | #endif | |
f611d0c3 | 328 | case SIDE_TYPE_STRING: |
a2e2357e | 329 | printf("\"%s\"", item->u.string); |
f611d0c3 MD |
330 | break; |
331 | case SIDE_TYPE_STRUCT: | |
332 | tracer_print_struct(type_desc, item->u.side_struct); | |
333 | break; | |
334 | case SIDE_TYPE_ARRAY: | |
335 | tracer_print_array(type_desc, item->u.side_array); | |
336 | break; | |
337 | case SIDE_TYPE_VLA: | |
338 | tracer_print_vla(type_desc, item->u.side_vla); | |
339 | break; | |
340 | case SIDE_TYPE_VLA_VISITOR: | |
352a4b77 | 341 | tracer_print_vla_visitor(type_desc, item->u.side_vla_app_visitor_ctx); |
f611d0c3 | 342 | break; |
ba845af5 MD |
343 | case SIDE_TYPE_ARRAY_U8: |
344 | case SIDE_TYPE_ARRAY_U16: | |
345 | case SIDE_TYPE_ARRAY_U32: | |
346 | case SIDE_TYPE_ARRAY_U64: | |
347 | case SIDE_TYPE_ARRAY_S8: | |
348 | case SIDE_TYPE_ARRAY_S16: | |
349 | case SIDE_TYPE_ARRAY_S32: | |
350 | case SIDE_TYPE_ARRAY_S64: | |
351 | tracer_print_array_fixint(type_desc, item); | |
352 | break; | |
1533629f MD |
353 | case SIDE_TYPE_VLA_U8: |
354 | case SIDE_TYPE_VLA_U16: | |
355 | case SIDE_TYPE_VLA_U32: | |
356 | case SIDE_TYPE_VLA_U64: | |
357 | case SIDE_TYPE_VLA_S8: | |
358 | case SIDE_TYPE_VLA_S16: | |
359 | case SIDE_TYPE_VLA_S32: | |
360 | case SIDE_TYPE_VLA_S64: | |
361 | tracer_print_vla_fixint(type_desc, item); | |
362 | break; | |
a2e2357e MD |
363 | case SIDE_TYPE_DYNAMIC: |
364 | tracer_print_dynamic(&item->u.dynamic); | |
365 | break; | |
f611d0c3 MD |
366 | default: |
367 | printf("<UNKNOWN TYPE>"); | |
368 | abort(); | |
369 | } | |
a848763d | 370 | printf(" }"); |
f611d0c3 MD |
371 | } |
372 | ||
373 | static | |
374 | void tracer_print_field(const struct side_event_field *item_desc, const struct side_arg_vec *item) | |
375 | { | |
19fa6aa2 | 376 | printf("%s: ", item_desc->field_name); |
f611d0c3 | 377 | tracer_print_type(&item_desc->side_type, item); |
f611d0c3 MD |
378 | } |
379 | ||
380 | static | |
381 | void tracer_print_struct(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc) | |
382 | { | |
383 | const struct side_arg_vec *sav = sav_desc->sav; | |
384 | uint32_t side_sav_len = sav_desc->len; | |
385 | int i; | |
386 | ||
387 | if (type_desc->u.side_struct.nr_fields != side_sav_len) { | |
388 | printf("ERROR: number of fields mismatch between description and arguments of structure\n"); | |
389 | abort(); | |
390 | } | |
391 | printf("{ "); | |
392 | for (i = 0; i < side_sav_len; i++) { | |
393 | printf("%s", i ? ", " : ""); | |
394 | tracer_print_field(&type_desc->u.side_struct.fields[i], &sav[i]); | |
395 | } | |
396 | printf(" }"); | |
397 | } | |
398 | ||
399 | static | |
400 | void tracer_print_array(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc) | |
401 | { | |
402 | const struct side_arg_vec *sav = sav_desc->sav; | |
403 | uint32_t side_sav_len = sav_desc->len; | |
404 | int i; | |
405 | ||
406 | if (type_desc->u.side_array.length != side_sav_len) { | |
407 | printf("ERROR: length mismatch between description and arguments of array\n"); | |
408 | abort(); | |
409 | } | |
410 | printf("[ "); | |
411 | for (i = 0; i < side_sav_len; i++) { | |
412 | printf("%s", i ? ", " : ""); | |
413 | tracer_print_type(type_desc->u.side_array.elem_type, &sav[i]); | |
414 | } | |
415 | printf(" ]"); | |
416 | } | |
417 | ||
418 | static | |
419 | void tracer_print_vla(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc) | |
420 | { | |
421 | const struct side_arg_vec *sav = sav_desc->sav; | |
422 | uint32_t side_sav_len = sav_desc->len; | |
423 | int i; | |
424 | ||
425 | printf("[ "); | |
426 | for (i = 0; i < side_sav_len; i++) { | |
427 | printf("%s", i ? ", " : ""); | |
428 | tracer_print_type(type_desc->u.side_vla.elem_type, &sav[i]); | |
429 | } | |
430 | printf(" ]"); | |
431 | } | |
432 | ||
352a4b77 MD |
433 | struct tracer_visitor_priv { |
434 | const struct side_type_description *elem_type; | |
435 | int i; | |
436 | }; | |
437 | ||
438 | static | |
439 | enum side_visitor_status tracer_write_elem_cb(const struct side_tracer_visitor_ctx *tracer_ctx, | |
440 | const struct side_arg_vec *elem) | |
441 | { | |
442 | struct tracer_visitor_priv *tracer_priv = tracer_ctx->priv; | |
443 | ||
444 | printf("%s", tracer_priv->i++ ? ", " : ""); | |
445 | tracer_print_type(tracer_priv->elem_type, elem); | |
446 | return SIDE_VISITOR_STATUS_OK; | |
447 | } | |
448 | ||
f611d0c3 | 449 | static |
352a4b77 | 450 | void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *app_ctx) |
f611d0c3 MD |
451 | { |
452 | enum side_visitor_status status; | |
352a4b77 MD |
453 | struct tracer_visitor_priv tracer_priv = { |
454 | .elem_type = type_desc->u.side_vla_visitor.elem_type, | |
455 | .i = 0, | |
456 | }; | |
457 | const struct side_tracer_visitor_ctx tracer_ctx = { | |
458 | .write_elem = tracer_write_elem_cb, | |
459 | .priv = &tracer_priv, | |
460 | }; | |
f611d0c3 | 461 | |
352a4b77 MD |
462 | printf("[ "); |
463 | status = type_desc->u.side_vla_visitor.visitor(&tracer_ctx, app_ctx); | |
464 | switch (status) { | |
465 | case SIDE_VISITOR_STATUS_OK: | |
466 | break; | |
467 | case SIDE_VISITOR_STATUS_ERROR: | |
f611d0c3 MD |
468 | printf("ERROR: Visitor error\n"); |
469 | abort(); | |
f611d0c3 MD |
470 | } |
471 | printf(" ]"); | |
f611d0c3 MD |
472 | } |
473 | ||
ba845af5 MD |
474 | void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item) |
475 | { | |
476 | const struct side_type_description *elem_type = type_desc->u.side_array.elem_type; | |
477 | uint32_t side_sav_len = type_desc->u.side_array.length; | |
478 | void *p = item->u.side_array_fixint; | |
479 | enum side_type side_type; | |
480 | int i; | |
481 | ||
1e8256c9 MD |
482 | switch (item->type) { |
483 | case SIDE_TYPE_ARRAY_U8: | |
484 | if (elem_type->type != SIDE_TYPE_U8) | |
485 | goto type_error; | |
486 | break; | |
487 | case SIDE_TYPE_ARRAY_U16: | |
488 | if (elem_type->type != SIDE_TYPE_U16) | |
489 | goto type_error; | |
490 | break; | |
491 | case SIDE_TYPE_ARRAY_U32: | |
492 | if (elem_type->type != SIDE_TYPE_U32) | |
493 | goto type_error; | |
494 | break; | |
495 | case SIDE_TYPE_ARRAY_U64: | |
496 | if (elem_type->type != SIDE_TYPE_U64) | |
497 | goto type_error; | |
498 | break; | |
499 | case SIDE_TYPE_ARRAY_S8: | |
500 | if (elem_type->type != SIDE_TYPE_S8) | |
501 | goto type_error; | |
502 | break; | |
503 | case SIDE_TYPE_ARRAY_S16: | |
504 | if (elem_type->type != SIDE_TYPE_S16) | |
505 | goto type_error; | |
506 | break; | |
507 | case SIDE_TYPE_ARRAY_S32: | |
508 | if (elem_type->type != SIDE_TYPE_S32) | |
509 | goto type_error; | |
510 | break; | |
511 | case SIDE_TYPE_ARRAY_S64: | |
512 | if (elem_type->type != SIDE_TYPE_S64) | |
513 | goto type_error; | |
514 | break; | |
515 | default: | |
516 | goto type_error; | |
ba845af5 | 517 | } |
1e8256c9 | 518 | side_type = elem_type->type; |
ba845af5 | 519 | |
1533629f MD |
520 | printf("[ "); |
521 | for (i = 0; i < side_sav_len; i++) { | |
522 | struct side_arg_vec sav_elem = { | |
523 | .type = side_type, | |
524 | }; | |
525 | ||
526 | switch (side_type) { | |
527 | case SIDE_TYPE_U8: | |
528 | sav_elem.u.side_u8 = ((const uint8_t *) p)[i]; | |
529 | break; | |
530 | case SIDE_TYPE_S8: | |
531 | sav_elem.u.side_s8 = ((const int8_t *) p)[i]; | |
532 | break; | |
533 | case SIDE_TYPE_U16: | |
534 | sav_elem.u.side_u16 = ((const uint16_t *) p)[i]; | |
535 | break; | |
536 | case SIDE_TYPE_S16: | |
537 | sav_elem.u.side_s16 = ((const int16_t *) p)[i]; | |
538 | break; | |
539 | case SIDE_TYPE_U32: | |
540 | sav_elem.u.side_u32 = ((const uint32_t *) p)[i]; | |
541 | break; | |
542 | case SIDE_TYPE_S32: | |
543 | sav_elem.u.side_s32 = ((const int32_t *) p)[i]; | |
544 | break; | |
545 | case SIDE_TYPE_U64: | |
546 | sav_elem.u.side_u64 = ((const uint64_t *) p)[i]; | |
547 | break; | |
548 | case SIDE_TYPE_S64: | |
549 | sav_elem.u.side_s64 = ((const int64_t *) p)[i]; | |
550 | break; | |
551 | ||
552 | default: | |
553 | printf("ERROR: Unexpected type\n"); | |
554 | abort(); | |
555 | } | |
556 | ||
557 | printf("%s", i ? ", " : ""); | |
558 | tracer_print_type(elem_type, &sav_elem); | |
559 | } | |
560 | printf(" ]"); | |
561 | return; | |
562 | ||
563 | type_error: | |
564 | printf("ERROR: type mismatch\n"); | |
565 | abort(); | |
566 | } | |
567 | ||
568 | void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item) | |
569 | { | |
570 | const struct side_type_description *elem_type = type_desc->u.side_vla.elem_type; | |
571 | uint32_t side_sav_len = item->u.side_vla_fixint.length; | |
572 | void *p = item->u.side_vla_fixint.p; | |
573 | enum side_type side_type; | |
574 | int i; | |
575 | ||
a2e2357e MD |
576 | switch (item->type) { |
577 | case SIDE_TYPE_VLA_U8: | |
578 | if (elem_type->type != SIDE_TYPE_U8) | |
1533629f | 579 | goto type_error; |
a2e2357e MD |
580 | break; |
581 | case SIDE_TYPE_VLA_U16: | |
582 | if (elem_type->type != SIDE_TYPE_U16) | |
1533629f | 583 | goto type_error; |
a2e2357e MD |
584 | break; |
585 | case SIDE_TYPE_VLA_U32: | |
586 | if (elem_type->type != SIDE_TYPE_U32) | |
587 | goto type_error; | |
588 | break; | |
589 | case SIDE_TYPE_VLA_U64: | |
590 | if (elem_type->type != SIDE_TYPE_U64) | |
591 | goto type_error; | |
592 | break; | |
593 | case SIDE_TYPE_VLA_S8: | |
594 | if (elem_type->type != SIDE_TYPE_S8) | |
595 | goto type_error; | |
596 | break; | |
597 | case SIDE_TYPE_VLA_S16: | |
598 | if (elem_type->type != SIDE_TYPE_S16) | |
599 | goto type_error; | |
600 | break; | |
601 | case SIDE_TYPE_VLA_S32: | |
602 | if (elem_type->type != SIDE_TYPE_S32) | |
603 | goto type_error; | |
604 | break; | |
605 | case SIDE_TYPE_VLA_S64: | |
606 | if (elem_type->type != SIDE_TYPE_S64) | |
607 | goto type_error; | |
608 | break; | |
609 | default: | |
610 | goto type_error; | |
1533629f | 611 | } |
a2e2357e | 612 | side_type = elem_type->type; |
1533629f | 613 | |
ba845af5 MD |
614 | printf("[ "); |
615 | for (i = 0; i < side_sav_len; i++) { | |
616 | struct side_arg_vec sav_elem = { | |
617 | .type = side_type, | |
618 | }; | |
619 | ||
620 | switch (side_type) { | |
621 | case SIDE_TYPE_U8: | |
622 | sav_elem.u.side_u8 = ((const uint8_t *) p)[i]; | |
623 | break; | |
624 | case SIDE_TYPE_S8: | |
625 | sav_elem.u.side_s8 = ((const int8_t *) p)[i]; | |
626 | break; | |
627 | case SIDE_TYPE_U16: | |
628 | sav_elem.u.side_u16 = ((const uint16_t *) p)[i]; | |
629 | break; | |
630 | case SIDE_TYPE_S16: | |
631 | sav_elem.u.side_s16 = ((const int16_t *) p)[i]; | |
632 | break; | |
633 | case SIDE_TYPE_U32: | |
634 | sav_elem.u.side_u32 = ((const uint32_t *) p)[i]; | |
635 | break; | |
636 | case SIDE_TYPE_S32: | |
637 | sav_elem.u.side_s32 = ((const int32_t *) p)[i]; | |
638 | break; | |
639 | case SIDE_TYPE_U64: | |
640 | sav_elem.u.side_u64 = ((const uint64_t *) p)[i]; | |
641 | break; | |
642 | case SIDE_TYPE_S64: | |
643 | sav_elem.u.side_s64 = ((const int64_t *) p)[i]; | |
644 | break; | |
645 | ||
646 | default: | |
647 | printf("ERROR: Unexpected type\n"); | |
648 | abort(); | |
649 | } | |
650 | ||
651 | printf("%s", i ? ", " : ""); | |
652 | tracer_print_type(elem_type, &sav_elem); | |
653 | } | |
654 | printf(" ]"); | |
655 | return; | |
656 | ||
657 | type_error: | |
658 | printf("ERROR: type mismatch\n"); | |
659 | abort(); | |
660 | } | |
661 | ||
a2e2357e | 662 | static |
c208889e | 663 | void tracer_print_dynamic_struct(const struct side_arg_dynamic_event_struct *dynamic_struct) |
a2e2357e | 664 | { |
c208889e MD |
665 | const struct side_arg_dynamic_event_field *fields = dynamic_struct->fields; |
666 | uint32_t len = dynamic_struct->len; | |
465e5e7e MD |
667 | int i; |
668 | ||
669 | printf("[ "); | |
670 | for (i = 0; i < len; i++) { | |
671 | printf("%s", i ? ", " : ""); | |
672 | printf("%s:: ", fields[i].field_name); | |
673 | tracer_print_dynamic(&fields[i].elem); | |
674 | } | |
675 | printf(" ]"); | |
a2e2357e MD |
676 | } |
677 | ||
2b359235 MD |
678 | struct tracer_dynamic_struct_visitor_priv { |
679 | int i; | |
680 | }; | |
681 | ||
682 | static | |
683 | enum side_visitor_status tracer_dynamic_struct_write_elem_cb( | |
684 | const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx, | |
685 | const struct side_arg_dynamic_event_field *dynamic_field) | |
686 | { | |
687 | struct tracer_dynamic_struct_visitor_priv *tracer_priv = tracer_ctx->priv; | |
688 | ||
689 | printf("%s", tracer_priv->i++ ? ", " : ""); | |
690 | printf("%s:: ", dynamic_field->field_name); | |
691 | tracer_print_dynamic(&dynamic_field->elem); | |
692 | return SIDE_VISITOR_STATUS_OK; | |
693 | } | |
694 | ||
a2e2357e | 695 | static |
c208889e | 696 | void tracer_print_dynamic_struct_visitor(const struct side_arg_dynamic_vec *item) |
a2e2357e | 697 | { |
2b359235 MD |
698 | enum side_visitor_status status; |
699 | struct tracer_dynamic_struct_visitor_priv tracer_priv = { | |
700 | .i = 0, | |
701 | }; | |
702 | const struct side_tracer_dynamic_struct_visitor_ctx tracer_ctx = { | |
703 | .write_field = tracer_dynamic_struct_write_elem_cb, | |
704 | .priv = &tracer_priv, | |
705 | }; | |
706 | void *app_ctx = item->u.side_dynamic_struct_visitor.app_ctx; | |
707 | ||
708 | printf("[ "); | |
709 | status = item->u.side_dynamic_struct_visitor.visitor(&tracer_ctx, app_ctx); | |
710 | switch (status) { | |
711 | case SIDE_VISITOR_STATUS_OK: | |
712 | break; | |
713 | case SIDE_VISITOR_STATUS_ERROR: | |
714 | printf("ERROR: Visitor error\n"); | |
715 | abort(); | |
716 | } | |
717 | printf(" ]"); | |
a2e2357e MD |
718 | } |
719 | ||
720 | static | |
721 | void tracer_print_dynamic_vla(const struct side_arg_dynamic_vec_vla *vla) | |
722 | { | |
723 | const struct side_arg_dynamic_vec *sav = vla->sav; | |
724 | uint32_t side_sav_len = vla->len; | |
725 | int i; | |
726 | ||
727 | printf("[ "); | |
728 | for (i = 0; i < side_sav_len; i++) { | |
729 | printf("%s", i ? ", " : ""); | |
730 | tracer_print_dynamic(&sav[i]); | |
731 | } | |
732 | printf(" ]"); | |
733 | } | |
734 | ||
8ceca0cd MD |
735 | struct tracer_dynamic_vla_visitor_priv { |
736 | int i; | |
737 | }; | |
738 | ||
739 | static | |
740 | enum side_visitor_status tracer_dynamic_vla_write_elem_cb( | |
741 | const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx, | |
742 | const struct side_arg_dynamic_vec *elem) | |
743 | { | |
744 | struct tracer_dynamic_vla_visitor_priv *tracer_priv = tracer_ctx->priv; | |
745 | ||
746 | printf("%s", tracer_priv->i++ ? ", " : ""); | |
747 | tracer_print_dynamic(elem); | |
748 | return SIDE_VISITOR_STATUS_OK; | |
749 | } | |
750 | ||
a2e2357e MD |
751 | static |
752 | void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item) | |
753 | { | |
8ceca0cd MD |
754 | enum side_visitor_status status; |
755 | struct tracer_dynamic_vla_visitor_priv tracer_priv = { | |
756 | .i = 0, | |
757 | }; | |
758 | const struct side_tracer_dynamic_vla_visitor_ctx tracer_ctx = { | |
759 | .write_elem = tracer_dynamic_vla_write_elem_cb, | |
760 | .priv = &tracer_priv, | |
761 | }; | |
762 | void *app_ctx = item->u.side_dynamic_vla_visitor.app_ctx; | |
763 | ||
764 | printf("[ "); | |
765 | status = item->u.side_dynamic_vla_visitor.visitor(&tracer_ctx, app_ctx); | |
766 | switch (status) { | |
767 | case SIDE_VISITOR_STATUS_OK: | |
768 | break; | |
769 | case SIDE_VISITOR_STATUS_ERROR: | |
770 | printf("ERROR: Visitor error\n"); | |
771 | abort(); | |
772 | } | |
773 | printf(" ]"); | |
a2e2357e MD |
774 | } |
775 | ||
776 | static | |
777 | void tracer_print_dynamic(const struct side_arg_dynamic_vec *item) | |
778 | { | |
808bd9bf | 779 | printf("{ "); |
bc3c89b3 | 780 | print_attributes("attr:: ", item->attr, item->nr_attr); |
808bd9bf MD |
781 | printf("%s", item->nr_attr ? ", " : ""); |
782 | printf("value: "); | |
1e8256c9 | 783 | switch (item->dynamic_type) { |
a2e2357e MD |
784 | case SIDE_DYNAMIC_TYPE_NULL: |
785 | printf("<NULL TYPE>"); | |
786 | break; | |
4f40d951 MD |
787 | case SIDE_DYNAMIC_TYPE_BOOL: |
788 | printf("%s", item->u.side_bool ? "true" : "false"); | |
789 | break; | |
a2e2357e MD |
790 | case SIDE_DYNAMIC_TYPE_U8: |
791 | printf("%" PRIu8, item->u.side_u8); | |
792 | break; | |
793 | case SIDE_DYNAMIC_TYPE_U16: | |
794 | printf("%" PRIu16, item->u.side_u16); | |
795 | break; | |
796 | case SIDE_DYNAMIC_TYPE_U32: | |
797 | printf("%" PRIu32, item->u.side_u32); | |
798 | break; | |
799 | case SIDE_DYNAMIC_TYPE_U64: | |
800 | printf("%" PRIu64, item->u.side_u64); | |
801 | break; | |
802 | case SIDE_DYNAMIC_TYPE_S8: | |
803 | printf("%" PRId8, item->u.side_s8); | |
804 | break; | |
805 | case SIDE_DYNAMIC_TYPE_S16: | |
806 | printf("%" PRId16, item->u.side_s16); | |
807 | break; | |
808 | case SIDE_DYNAMIC_TYPE_S32: | |
809 | printf("%" PRId32, item->u.side_s32); | |
810 | break; | |
811 | case SIDE_DYNAMIC_TYPE_S64: | |
812 | printf("%" PRId64, item->u.side_s64); | |
813 | break; | |
fb25b355 MD |
814 | case SIDE_DYNAMIC_TYPE_FLOAT_BINARY16: |
815 | #if __HAVE_FLOAT16 | |
816 | printf("%g", (double) item->u.side_float_binary16); | |
817 | break; | |
818 | #else | |
819 | printf("ERROR: Unsupported binary16 float type\n"); | |
820 | abort(); | |
821 | #endif | |
822 | case SIDE_DYNAMIC_TYPE_FLOAT_BINARY32: | |
823 | #if __HAVE_FLOAT32 | |
824 | printf("%g", (double) item->u.side_float_binary32); | |
825 | break; | |
826 | #else | |
827 | printf("ERROR: Unsupported binary32 float type\n"); | |
828 | abort(); | |
829 | #endif | |
830 | case SIDE_DYNAMIC_TYPE_FLOAT_BINARY64: | |
831 | #if __HAVE_FLOAT64 | |
832 | printf("%g", (double) item->u.side_float_binary64); | |
833 | break; | |
834 | #else | |
835 | printf("ERROR: Unsupported binary64 float type\n"); | |
836 | abort(); | |
837 | #endif | |
838 | case SIDE_DYNAMIC_TYPE_FLOAT_BINARY128: | |
839 | #if __HAVE_FLOAT128 | |
840 | printf("%Lg", (long double) item->u.side_float_binary128); | |
841 | break; | |
842 | #else | |
843 | printf("ERROR: Unsupported binary128 float type\n"); | |
844 | abort(); | |
845 | #endif | |
a2e2357e MD |
846 | case SIDE_DYNAMIC_TYPE_STRING: |
847 | printf("\"%s\"", item->u.string); | |
848 | break; | |
c208889e MD |
849 | case SIDE_DYNAMIC_TYPE_STRUCT: |
850 | tracer_print_dynamic_struct(item->u.side_dynamic_struct); | |
a2e2357e | 851 | break; |
c208889e MD |
852 | case SIDE_DYNAMIC_TYPE_STRUCT_VISITOR: |
853 | tracer_print_dynamic_struct_visitor(item); | |
a2e2357e MD |
854 | break; |
855 | case SIDE_DYNAMIC_TYPE_VLA: | |
856 | tracer_print_dynamic_vla(item->u.side_dynamic_vla); | |
857 | break; | |
858 | case SIDE_DYNAMIC_TYPE_VLA_VISITOR: | |
859 | tracer_print_dynamic_vla_visitor(item); | |
860 | break; | |
861 | default: | |
862 | printf("<UNKNOWN TYPE>"); | |
863 | abort(); | |
864 | } | |
808bd9bf | 865 | printf(" }"); |
a2e2357e MD |
866 | } |
867 | ||
68f8cfbe MD |
868 | static |
869 | void tracer_print_static_fields(const struct side_event_description *desc, | |
870 | const struct side_arg_vec_description *sav_desc, | |
871 | int *nr_items) | |
f611d0c3 MD |
872 | { |
873 | const struct side_arg_vec *sav = sav_desc->sav; | |
874 | uint32_t side_sav_len = sav_desc->len; | |
875 | int i; | |
876 | ||
65010f43 | 877 | printf("provider: %s, event: %s", desc->provider_name, desc->event_name); |
f611d0c3 MD |
878 | if (desc->nr_fields != side_sav_len) { |
879 | printf("ERROR: number of fields mismatch between description and arguments\n"); | |
880 | abort(); | |
881 | } | |
a848763d MD |
882 | print_attributes(", attributes: ", desc->attr, desc->nr_attr); |
883 | printf("%s", side_sav_len ? ", fields: [ " : ""); | |
f611d0c3 MD |
884 | for (i = 0; i < side_sav_len; i++) { |
885 | printf("%s", i ? ", " : ""); | |
886 | tracer_print_field(&desc->fields[i], &sav[i]); | |
887 | } | |
68f8cfbe MD |
888 | if (nr_items) |
889 | *nr_items = i; | |
890 | } | |
891 | ||
892 | void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc) | |
893 | { | |
a848763d MD |
894 | int nr_fields = 0; |
895 | ||
a848763d MD |
896 | tracer_print_static_fields(desc, sav_desc, &nr_fields); |
897 | if (nr_fields) | |
898 | printf(" ]"); | |
f611d0c3 MD |
899 | printf("\n"); |
900 | } | |
19fa6aa2 MD |
901 | |
902 | void tracer_call_variadic(const struct side_event_description *desc, | |
903 | const struct side_arg_vec_description *sav_desc, | |
904 | const struct side_arg_dynamic_event_struct *var_struct) | |
905 | { | |
68f8cfbe MD |
906 | uint32_t var_struct_len = var_struct->len; |
907 | int nr_fields = 0, i; | |
19fa6aa2 | 908 | |
68f8cfbe MD |
909 | tracer_print_static_fields(desc, sav_desc, &nr_fields); |
910 | ||
8a25ce77 MD |
911 | if (side_unlikely(!(desc->flags & SIDE_EVENT_FLAG_VARIADIC))) { |
912 | printf("ERROR: unexpected non-variadic event description\n"); | |
913 | abort(); | |
914 | } | |
a848763d | 915 | printf("%s", var_struct_len && !nr_fields ? ", fields: [ " : ""); |
68f8cfbe MD |
916 | for (i = 0; i < var_struct_len; i++, nr_fields++) { |
917 | printf("%s", nr_fields ? ", " : ""); | |
918 | printf("%s:: ", var_struct->fields[i].field_name); | |
919 | tracer_print_dynamic(&var_struct->fields[i].elem); | |
19fa6aa2 | 920 | } |
a848763d MD |
921 | if (i) |
922 | printf(" ]"); | |
19fa6aa2 MD |
923 | printf("\n"); |
924 | } |