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