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