Introduce dynamic types
[libside.git] / src / tracer.c
CommitLineData
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
13static
14void tracer_print_struct(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc);
15static
16void tracer_print_array(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc);
17static
18void tracer_print_vla(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc);
19static
352a4b77 20void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *app_ctx);
ba845af5
MD
21static
22void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item);
1533629f
MD
23static
24void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item);
a2e2357e
MD
25static
26void tracer_print_dynamic(const struct side_arg_dynamic_vec *dynamic_item);
f611d0c3
MD
27
28static
29void tracer_print_type(const struct side_type_description *type_desc, const struct side_arg_vec *item)
30{
ba845af5
MD
31 switch (item->type) {
32 case SIDE_TYPE_ARRAY_U8:
33 case SIDE_TYPE_ARRAY_U16:
34 case SIDE_TYPE_ARRAY_U32:
35 case SIDE_TYPE_ARRAY_U64:
36 case SIDE_TYPE_ARRAY_S8:
37 case SIDE_TYPE_ARRAY_S16:
38 case SIDE_TYPE_ARRAY_S32:
39 case SIDE_TYPE_ARRAY_S64:
40 if (type_desc->type != SIDE_TYPE_ARRAY) {
41 printf("ERROR: type mismatch between description and arguments\n");
42 abort();
43 }
44 break;
1533629f
MD
45 case SIDE_TYPE_VLA_U8:
46 case SIDE_TYPE_VLA_U16:
47 case SIDE_TYPE_VLA_U32:
48 case SIDE_TYPE_VLA_U64:
49 case SIDE_TYPE_VLA_S8:
50 case SIDE_TYPE_VLA_S16:
51 case SIDE_TYPE_VLA_S32:
52 case SIDE_TYPE_VLA_S64:
53 if (type_desc->type != SIDE_TYPE_VLA) {
54 printf("ERROR: type mismatch between description and arguments\n");
55 abort();
56 }
57 break;
58
ba845af5 59 default:
a2e2357e 60 if (type_desc->type != item->type) {
ba845af5
MD
61 printf("ERROR: type mismatch between description and arguments\n");
62 abort();
63 }
64 break;
f611d0c3 65 }
f611d0c3
MD
66 switch (item->type) {
67 case SIDE_TYPE_U8:
68 printf("%" PRIu8, item->u.side_u8);
69 break;
70 case SIDE_TYPE_U16:
71 printf("%" PRIu16, item->u.side_u16);
72 break;
73 case SIDE_TYPE_U32:
74 printf("%" PRIu32, item->u.side_u32);
75 break;
76 case SIDE_TYPE_U64:
77 printf("%" PRIu64, item->u.side_u64);
78 break;
79 case SIDE_TYPE_S8:
80 printf("%" PRId8, item->u.side_s8);
81 break;
82 case SIDE_TYPE_S16:
83 printf("%" PRId16, item->u.side_s16);
84 break;
85 case SIDE_TYPE_S32:
86 printf("%" PRId32, item->u.side_s32);
87 break;
88 case SIDE_TYPE_S64:
89 printf("%" PRId64, item->u.side_s64);
90 break;
91 case SIDE_TYPE_STRING:
a2e2357e 92 printf("\"%s\"", item->u.string);
f611d0c3
MD
93 break;
94 case SIDE_TYPE_STRUCT:
95 tracer_print_struct(type_desc, item->u.side_struct);
96 break;
97 case SIDE_TYPE_ARRAY:
98 tracer_print_array(type_desc, item->u.side_array);
99 break;
100 case SIDE_TYPE_VLA:
101 tracer_print_vla(type_desc, item->u.side_vla);
102 break;
103 case SIDE_TYPE_VLA_VISITOR:
352a4b77 104 tracer_print_vla_visitor(type_desc, item->u.side_vla_app_visitor_ctx);
f611d0c3 105 break;
ba845af5
MD
106 case SIDE_TYPE_ARRAY_U8:
107 case SIDE_TYPE_ARRAY_U16:
108 case SIDE_TYPE_ARRAY_U32:
109 case SIDE_TYPE_ARRAY_U64:
110 case SIDE_TYPE_ARRAY_S8:
111 case SIDE_TYPE_ARRAY_S16:
112 case SIDE_TYPE_ARRAY_S32:
113 case SIDE_TYPE_ARRAY_S64:
114 tracer_print_array_fixint(type_desc, item);
115 break;
1533629f
MD
116 case SIDE_TYPE_VLA_U8:
117 case SIDE_TYPE_VLA_U16:
118 case SIDE_TYPE_VLA_U32:
119 case SIDE_TYPE_VLA_U64:
120 case SIDE_TYPE_VLA_S8:
121 case SIDE_TYPE_VLA_S16:
122 case SIDE_TYPE_VLA_S32:
123 case SIDE_TYPE_VLA_S64:
124 tracer_print_vla_fixint(type_desc, item);
125 break;
a2e2357e
MD
126 case SIDE_TYPE_DYNAMIC:
127 tracer_print_dynamic(&item->u.dynamic);
128 break;
f611d0c3
MD
129 default:
130 printf("<UNKNOWN TYPE>");
131 abort();
132 }
133}
134
135static
136void tracer_print_field(const struct side_event_field *item_desc, const struct side_arg_vec *item)
137{
a2e2357e 138 printf("(%s: ", item_desc->field_name);
f611d0c3
MD
139 tracer_print_type(&item_desc->side_type, item);
140 printf(")");
141}
142
143static
144void tracer_print_struct(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc)
145{
146 const struct side_arg_vec *sav = sav_desc->sav;
147 uint32_t side_sav_len = sav_desc->len;
148 int i;
149
150 if (type_desc->u.side_struct.nr_fields != side_sav_len) {
151 printf("ERROR: number of fields mismatch between description and arguments of structure\n");
152 abort();
153 }
154 printf("{ ");
155 for (i = 0; i < side_sav_len; i++) {
156 printf("%s", i ? ", " : "");
157 tracer_print_field(&type_desc->u.side_struct.fields[i], &sav[i]);
158 }
159 printf(" }");
160}
161
162static
163void tracer_print_array(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc)
164{
165 const struct side_arg_vec *sav = sav_desc->sav;
166 uint32_t side_sav_len = sav_desc->len;
167 int i;
168
169 if (type_desc->u.side_array.length != side_sav_len) {
170 printf("ERROR: length mismatch between description and arguments of array\n");
171 abort();
172 }
173 printf("[ ");
174 for (i = 0; i < side_sav_len; i++) {
175 printf("%s", i ? ", " : "");
176 tracer_print_type(type_desc->u.side_array.elem_type, &sav[i]);
177 }
178 printf(" ]");
179}
180
181static
182void tracer_print_vla(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc)
183{
184 const struct side_arg_vec *sav = sav_desc->sav;
185 uint32_t side_sav_len = sav_desc->len;
186 int i;
187
188 printf("[ ");
189 for (i = 0; i < side_sav_len; i++) {
190 printf("%s", i ? ", " : "");
191 tracer_print_type(type_desc->u.side_vla.elem_type, &sav[i]);
192 }
193 printf(" ]");
194}
195
352a4b77
MD
196struct tracer_visitor_priv {
197 const struct side_type_description *elem_type;
198 int i;
199};
200
201static
202enum side_visitor_status tracer_write_elem_cb(const struct side_tracer_visitor_ctx *tracer_ctx,
203 const struct side_arg_vec *elem)
204{
205 struct tracer_visitor_priv *tracer_priv = tracer_ctx->priv;
206
207 printf("%s", tracer_priv->i++ ? ", " : "");
208 tracer_print_type(tracer_priv->elem_type, elem);
209 return SIDE_VISITOR_STATUS_OK;
210}
211
f611d0c3 212static
352a4b77 213void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *app_ctx)
f611d0c3
MD
214{
215 enum side_visitor_status status;
352a4b77
MD
216 struct tracer_visitor_priv tracer_priv = {
217 .elem_type = type_desc->u.side_vla_visitor.elem_type,
218 .i = 0,
219 };
220 const struct side_tracer_visitor_ctx tracer_ctx = {
221 .write_elem = tracer_write_elem_cb,
222 .priv = &tracer_priv,
223 };
f611d0c3 224
352a4b77
MD
225 printf("[ ");
226 status = type_desc->u.side_vla_visitor.visitor(&tracer_ctx, app_ctx);
227 switch (status) {
228 case SIDE_VISITOR_STATUS_OK:
229 break;
230 case SIDE_VISITOR_STATUS_ERROR:
f611d0c3
MD
231 printf("ERROR: Visitor error\n");
232 abort();
352a4b77
MD
233 case SIDE_VISITOR_STATUS_END:
234 break;
f611d0c3
MD
235 }
236 printf(" ]");
f611d0c3
MD
237}
238
ba845af5
MD
239void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
240{
241 const struct side_type_description *elem_type = type_desc->u.side_array.elem_type;
242 uint32_t side_sav_len = type_desc->u.side_array.length;
243 void *p = item->u.side_array_fixint;
244 enum side_type side_type;
245 int i;
246
247 if (elem_type->type != SIDE_TYPE_DYNAMIC) {
248 switch (item->type) {
249 case SIDE_TYPE_ARRAY_U8:
250 if (elem_type->type != SIDE_TYPE_U8)
251 goto type_error;
252 break;
253 case SIDE_TYPE_ARRAY_U16:
254 if (elem_type->type != SIDE_TYPE_U16)
255 goto type_error;
256 break;
257 case SIDE_TYPE_ARRAY_U32:
258 if (elem_type->type != SIDE_TYPE_U32)
259 goto type_error;
260 break;
261 case SIDE_TYPE_ARRAY_U64:
262 if (elem_type->type != SIDE_TYPE_U64)
263 goto type_error;
264 break;
265 case SIDE_TYPE_ARRAY_S8:
266 if (elem_type->type != SIDE_TYPE_S8)
267 goto type_error;
268 break;
269 case SIDE_TYPE_ARRAY_S16:
270 if (elem_type->type != SIDE_TYPE_S16)
271 goto type_error;
272 break;
273 case SIDE_TYPE_ARRAY_S32:
274 if (elem_type->type != SIDE_TYPE_S32)
275 goto type_error;
276 break;
277 case SIDE_TYPE_ARRAY_S64:
278 if (elem_type->type != SIDE_TYPE_S64)
279 goto type_error;
280 break;
281 }
282 side_type = elem_type->type;
283 } else {
284 switch (item->type) {
285 case SIDE_TYPE_ARRAY_U8:
286 side_type = SIDE_TYPE_U8;
287 break;
288 case SIDE_TYPE_ARRAY_U16:
289 side_type = SIDE_TYPE_U16;
290 break;
291 case SIDE_TYPE_ARRAY_U32:
292 side_type = SIDE_TYPE_U32;
293 break;
294 case SIDE_TYPE_ARRAY_U64:
295 side_type = SIDE_TYPE_U64;
296 break;
297 case SIDE_TYPE_ARRAY_S8:
298 side_type = SIDE_TYPE_S8;
299 break;
300 case SIDE_TYPE_ARRAY_S16:
301 side_type = SIDE_TYPE_S16;
302 break;
303 case SIDE_TYPE_ARRAY_S32:
304 side_type = SIDE_TYPE_S32;
305 break;
306 case SIDE_TYPE_ARRAY_S64:
307 side_type = SIDE_TYPE_S64;
308 break;
309 }
310 }
311
1533629f
MD
312 printf("[ ");
313 for (i = 0; i < side_sav_len; i++) {
314 struct side_arg_vec sav_elem = {
315 .type = side_type,
316 };
317
318 switch (side_type) {
319 case SIDE_TYPE_U8:
320 sav_elem.u.side_u8 = ((const uint8_t *) p)[i];
321 break;
322 case SIDE_TYPE_S8:
323 sav_elem.u.side_s8 = ((const int8_t *) p)[i];
324 break;
325 case SIDE_TYPE_U16:
326 sav_elem.u.side_u16 = ((const uint16_t *) p)[i];
327 break;
328 case SIDE_TYPE_S16:
329 sav_elem.u.side_s16 = ((const int16_t *) p)[i];
330 break;
331 case SIDE_TYPE_U32:
332 sav_elem.u.side_u32 = ((const uint32_t *) p)[i];
333 break;
334 case SIDE_TYPE_S32:
335 sav_elem.u.side_s32 = ((const int32_t *) p)[i];
336 break;
337 case SIDE_TYPE_U64:
338 sav_elem.u.side_u64 = ((const uint64_t *) p)[i];
339 break;
340 case SIDE_TYPE_S64:
341 sav_elem.u.side_s64 = ((const int64_t *) p)[i];
342 break;
343
344 default:
345 printf("ERROR: Unexpected type\n");
346 abort();
347 }
348
349 printf("%s", i ? ", " : "");
350 tracer_print_type(elem_type, &sav_elem);
351 }
352 printf(" ]");
353 return;
354
355type_error:
356 printf("ERROR: type mismatch\n");
357 abort();
358}
359
360void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
361{
362 const struct side_type_description *elem_type = type_desc->u.side_vla.elem_type;
363 uint32_t side_sav_len = item->u.side_vla_fixint.length;
364 void *p = item->u.side_vla_fixint.p;
365 enum side_type side_type;
366 int i;
367
a2e2357e
MD
368 switch (item->type) {
369 case SIDE_TYPE_VLA_U8:
370 if (elem_type->type != SIDE_TYPE_U8)
1533629f 371 goto type_error;
a2e2357e
MD
372 break;
373 case SIDE_TYPE_VLA_U16:
374 if (elem_type->type != SIDE_TYPE_U16)
1533629f 375 goto type_error;
a2e2357e
MD
376 break;
377 case SIDE_TYPE_VLA_U32:
378 if (elem_type->type != SIDE_TYPE_U32)
379 goto type_error;
380 break;
381 case SIDE_TYPE_VLA_U64:
382 if (elem_type->type != SIDE_TYPE_U64)
383 goto type_error;
384 break;
385 case SIDE_TYPE_VLA_S8:
386 if (elem_type->type != SIDE_TYPE_S8)
387 goto type_error;
388 break;
389 case SIDE_TYPE_VLA_S16:
390 if (elem_type->type != SIDE_TYPE_S16)
391 goto type_error;
392 break;
393 case SIDE_TYPE_VLA_S32:
394 if (elem_type->type != SIDE_TYPE_S32)
395 goto type_error;
396 break;
397 case SIDE_TYPE_VLA_S64:
398 if (elem_type->type != SIDE_TYPE_S64)
399 goto type_error;
400 break;
401 default:
402 goto type_error;
1533629f 403 }
a2e2357e 404 side_type = elem_type->type;
1533629f 405
ba845af5
MD
406 printf("[ ");
407 for (i = 0; i < side_sav_len; i++) {
408 struct side_arg_vec sav_elem = {
409 .type = side_type,
410 };
411
412 switch (side_type) {
413 case SIDE_TYPE_U8:
414 sav_elem.u.side_u8 = ((const uint8_t *) p)[i];
415 break;
416 case SIDE_TYPE_S8:
417 sav_elem.u.side_s8 = ((const int8_t *) p)[i];
418 break;
419 case SIDE_TYPE_U16:
420 sav_elem.u.side_u16 = ((const uint16_t *) p)[i];
421 break;
422 case SIDE_TYPE_S16:
423 sav_elem.u.side_s16 = ((const int16_t *) p)[i];
424 break;
425 case SIDE_TYPE_U32:
426 sav_elem.u.side_u32 = ((const uint32_t *) p)[i];
427 break;
428 case SIDE_TYPE_S32:
429 sav_elem.u.side_s32 = ((const int32_t *) p)[i];
430 break;
431 case SIDE_TYPE_U64:
432 sav_elem.u.side_u64 = ((const uint64_t *) p)[i];
433 break;
434 case SIDE_TYPE_S64:
435 sav_elem.u.side_s64 = ((const int64_t *) p)[i];
436 break;
437
438 default:
439 printf("ERROR: Unexpected type\n");
440 abort();
441 }
442
443 printf("%s", i ? ", " : "");
444 tracer_print_type(elem_type, &sav_elem);
445 }
446 printf(" ]");
447 return;
448
449type_error:
450 printf("ERROR: type mismatch\n");
451 abort();
452}
453
a2e2357e
MD
454static
455void tracer_print_dynamic_map(const struct side_dynamic_event_map *map)
456{
457 //TODO
458}
459
460static
461void tracer_print_dynamic_map_visitor(const struct side_arg_dynamic_vec *item)
462{
463 //TODO
464}
465
466static
467void tracer_print_dynamic_vla(const struct side_arg_dynamic_vec_vla *vla)
468{
469 const struct side_arg_dynamic_vec *sav = vla->sav;
470 uint32_t side_sav_len = vla->len;
471 int i;
472
473 printf("[ ");
474 for (i = 0; i < side_sav_len; i++) {
475 printf("%s", i ? ", " : "");
476 tracer_print_dynamic(&sav[i]);
477 }
478 printf(" ]");
479}
480
481static
482void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item)
483{
484 //TODO
485}
486
487static
488void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
489{
490 switch (item->type) {
491 case SIDE_DYNAMIC_TYPE_NULL:
492 printf("<NULL TYPE>");
493 break;
494 case SIDE_DYNAMIC_TYPE_U8:
495 printf("%" PRIu8, item->u.side_u8);
496 break;
497 case SIDE_DYNAMIC_TYPE_U16:
498 printf("%" PRIu16, item->u.side_u16);
499 break;
500 case SIDE_DYNAMIC_TYPE_U32:
501 printf("%" PRIu32, item->u.side_u32);
502 break;
503 case SIDE_DYNAMIC_TYPE_U64:
504 printf("%" PRIu64, item->u.side_u64);
505 break;
506 case SIDE_DYNAMIC_TYPE_S8:
507 printf("%" PRId8, item->u.side_s8);
508 break;
509 case SIDE_DYNAMIC_TYPE_S16:
510 printf("%" PRId16, item->u.side_s16);
511 break;
512 case SIDE_DYNAMIC_TYPE_S32:
513 printf("%" PRId32, item->u.side_s32);
514 break;
515 case SIDE_DYNAMIC_TYPE_S64:
516 printf("%" PRId64, item->u.side_s64);
517 break;
518 case SIDE_DYNAMIC_TYPE_STRING:
519 printf("\"%s\"", item->u.string);
520 break;
521 case SIDE_DYNAMIC_TYPE_MAP:
522 tracer_print_dynamic_map(item->u.side_dynamic_map);
523 break;
524 case SIDE_DYNAMIC_TYPE_MAP_VISITOR:
525 tracer_print_dynamic_map_visitor(item);
526 break;
527 case SIDE_DYNAMIC_TYPE_VLA:
528 tracer_print_dynamic_vla(item->u.side_dynamic_vla);
529 break;
530 case SIDE_DYNAMIC_TYPE_VLA_VISITOR:
531 tracer_print_dynamic_vla_visitor(item);
532 break;
533 default:
534 printf("<UNKNOWN TYPE>");
535 abort();
536 }
537}
538
f611d0c3
MD
539void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
540{
541 const struct side_arg_vec *sav = sav_desc->sav;
542 uint32_t side_sav_len = sav_desc->len;
543 int i;
544
545 printf("provider: %s, event: %s, ", desc->provider_name, desc->event_name);
546 if (desc->nr_fields != side_sav_len) {
547 printf("ERROR: number of fields mismatch between description and arguments\n");
548 abort();
549 }
550 for (i = 0; i < side_sav_len; i++) {
551 printf("%s", i ? ", " : "");
552 tracer_print_field(&desc->fields[i], &sav[i]);
553 }
554 printf("\n");
555}
This page took 0.073327 seconds and 4 git commands to generate.