Cleanup
[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();
f611d0c3
MD
233 }
234 printf(" ]");
f611d0c3
MD
235}
236
ba845af5
MD
237void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
238{
239 const struct side_type_description *elem_type = type_desc->u.side_array.elem_type;
240 uint32_t side_sav_len = type_desc->u.side_array.length;
241 void *p = item->u.side_array_fixint;
242 enum side_type side_type;
243 int i;
244
245 if (elem_type->type != SIDE_TYPE_DYNAMIC) {
246 switch (item->type) {
247 case SIDE_TYPE_ARRAY_U8:
248 if (elem_type->type != SIDE_TYPE_U8)
249 goto type_error;
250 break;
251 case SIDE_TYPE_ARRAY_U16:
252 if (elem_type->type != SIDE_TYPE_U16)
253 goto type_error;
254 break;
255 case SIDE_TYPE_ARRAY_U32:
256 if (elem_type->type != SIDE_TYPE_U32)
257 goto type_error;
258 break;
259 case SIDE_TYPE_ARRAY_U64:
260 if (elem_type->type != SIDE_TYPE_U64)
261 goto type_error;
262 break;
263 case SIDE_TYPE_ARRAY_S8:
264 if (elem_type->type != SIDE_TYPE_S8)
265 goto type_error;
266 break;
267 case SIDE_TYPE_ARRAY_S16:
268 if (elem_type->type != SIDE_TYPE_S16)
269 goto type_error;
270 break;
271 case SIDE_TYPE_ARRAY_S32:
272 if (elem_type->type != SIDE_TYPE_S32)
273 goto type_error;
274 break;
275 case SIDE_TYPE_ARRAY_S64:
276 if (elem_type->type != SIDE_TYPE_S64)
277 goto type_error;
278 break;
279 }
280 side_type = elem_type->type;
281 } else {
282 switch (item->type) {
283 case SIDE_TYPE_ARRAY_U8:
284 side_type = SIDE_TYPE_U8;
285 break;
286 case SIDE_TYPE_ARRAY_U16:
287 side_type = SIDE_TYPE_U16;
288 break;
289 case SIDE_TYPE_ARRAY_U32:
290 side_type = SIDE_TYPE_U32;
291 break;
292 case SIDE_TYPE_ARRAY_U64:
293 side_type = SIDE_TYPE_U64;
294 break;
295 case SIDE_TYPE_ARRAY_S8:
296 side_type = SIDE_TYPE_S8;
297 break;
298 case SIDE_TYPE_ARRAY_S16:
299 side_type = SIDE_TYPE_S16;
300 break;
301 case SIDE_TYPE_ARRAY_S32:
302 side_type = SIDE_TYPE_S32;
303 break;
304 case SIDE_TYPE_ARRAY_S64:
305 side_type = SIDE_TYPE_S64;
306 break;
307 }
308 }
309
1533629f
MD
310 printf("[ ");
311 for (i = 0; i < side_sav_len; i++) {
312 struct side_arg_vec sav_elem = {
313 .type = side_type,
314 };
315
316 switch (side_type) {
317 case SIDE_TYPE_U8:
318 sav_elem.u.side_u8 = ((const uint8_t *) p)[i];
319 break;
320 case SIDE_TYPE_S8:
321 sav_elem.u.side_s8 = ((const int8_t *) p)[i];
322 break;
323 case SIDE_TYPE_U16:
324 sav_elem.u.side_u16 = ((const uint16_t *) p)[i];
325 break;
326 case SIDE_TYPE_S16:
327 sav_elem.u.side_s16 = ((const int16_t *) p)[i];
328 break;
329 case SIDE_TYPE_U32:
330 sav_elem.u.side_u32 = ((const uint32_t *) p)[i];
331 break;
332 case SIDE_TYPE_S32:
333 sav_elem.u.side_s32 = ((const int32_t *) p)[i];
334 break;
335 case SIDE_TYPE_U64:
336 sav_elem.u.side_u64 = ((const uint64_t *) p)[i];
337 break;
338 case SIDE_TYPE_S64:
339 sav_elem.u.side_s64 = ((const int64_t *) p)[i];
340 break;
341
342 default:
343 printf("ERROR: Unexpected type\n");
344 abort();
345 }
346
347 printf("%s", i ? ", " : "");
348 tracer_print_type(elem_type, &sav_elem);
349 }
350 printf(" ]");
351 return;
352
353type_error:
354 printf("ERROR: type mismatch\n");
355 abort();
356}
357
358void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
359{
360 const struct side_type_description *elem_type = type_desc->u.side_vla.elem_type;
361 uint32_t side_sav_len = item->u.side_vla_fixint.length;
362 void *p = item->u.side_vla_fixint.p;
363 enum side_type side_type;
364 int i;
365
a2e2357e
MD
366 switch (item->type) {
367 case SIDE_TYPE_VLA_U8:
368 if (elem_type->type != SIDE_TYPE_U8)
1533629f 369 goto type_error;
a2e2357e
MD
370 break;
371 case SIDE_TYPE_VLA_U16:
372 if (elem_type->type != SIDE_TYPE_U16)
1533629f 373 goto type_error;
a2e2357e
MD
374 break;
375 case SIDE_TYPE_VLA_U32:
376 if (elem_type->type != SIDE_TYPE_U32)
377 goto type_error;
378 break;
379 case SIDE_TYPE_VLA_U64:
380 if (elem_type->type != SIDE_TYPE_U64)
381 goto type_error;
382 break;
383 case SIDE_TYPE_VLA_S8:
384 if (elem_type->type != SIDE_TYPE_S8)
385 goto type_error;
386 break;
387 case SIDE_TYPE_VLA_S16:
388 if (elem_type->type != SIDE_TYPE_S16)
389 goto type_error;
390 break;
391 case SIDE_TYPE_VLA_S32:
392 if (elem_type->type != SIDE_TYPE_S32)
393 goto type_error;
394 break;
395 case SIDE_TYPE_VLA_S64:
396 if (elem_type->type != SIDE_TYPE_S64)
397 goto type_error;
398 break;
399 default:
400 goto type_error;
1533629f 401 }
a2e2357e 402 side_type = elem_type->type;
1533629f 403
ba845af5
MD
404 printf("[ ");
405 for (i = 0; i < side_sav_len; i++) {
406 struct side_arg_vec sav_elem = {
407 .type = side_type,
408 };
409
410 switch (side_type) {
411 case SIDE_TYPE_U8:
412 sav_elem.u.side_u8 = ((const uint8_t *) p)[i];
413 break;
414 case SIDE_TYPE_S8:
415 sav_elem.u.side_s8 = ((const int8_t *) p)[i];
416 break;
417 case SIDE_TYPE_U16:
418 sav_elem.u.side_u16 = ((const uint16_t *) p)[i];
419 break;
420 case SIDE_TYPE_S16:
421 sav_elem.u.side_s16 = ((const int16_t *) p)[i];
422 break;
423 case SIDE_TYPE_U32:
424 sav_elem.u.side_u32 = ((const uint32_t *) p)[i];
425 break;
426 case SIDE_TYPE_S32:
427 sav_elem.u.side_s32 = ((const int32_t *) p)[i];
428 break;
429 case SIDE_TYPE_U64:
430 sav_elem.u.side_u64 = ((const uint64_t *) p)[i];
431 break;
432 case SIDE_TYPE_S64:
433 sav_elem.u.side_s64 = ((const int64_t *) p)[i];
434 break;
435
436 default:
437 printf("ERROR: Unexpected type\n");
438 abort();
439 }
440
441 printf("%s", i ? ", " : "");
442 tracer_print_type(elem_type, &sav_elem);
443 }
444 printf(" ]");
445 return;
446
447type_error:
448 printf("ERROR: type mismatch\n");
449 abort();
450}
451
a2e2357e 452static
c208889e 453void tracer_print_dynamic_struct(const struct side_arg_dynamic_event_struct *dynamic_struct)
a2e2357e 454{
c208889e
MD
455 const struct side_arg_dynamic_event_field *fields = dynamic_struct->fields;
456 uint32_t len = dynamic_struct->len;
465e5e7e
MD
457 int i;
458
459 printf("[ ");
460 for (i = 0; i < len; i++) {
461 printf("%s", i ? ", " : "");
462 printf("%s:: ", fields[i].field_name);
463 tracer_print_dynamic(&fields[i].elem);
464 }
465 printf(" ]");
a2e2357e
MD
466}
467
468static
c208889e 469void tracer_print_dynamic_struct_visitor(const struct side_arg_dynamic_vec *item)
a2e2357e
MD
470{
471 //TODO
472}
473
474static
475void tracer_print_dynamic_vla(const struct side_arg_dynamic_vec_vla *vla)
476{
477 const struct side_arg_dynamic_vec *sav = vla->sav;
478 uint32_t side_sav_len = vla->len;
479 int i;
480
481 printf("[ ");
482 for (i = 0; i < side_sav_len; i++) {
483 printf("%s", i ? ", " : "");
484 tracer_print_dynamic(&sav[i]);
485 }
486 printf(" ]");
487}
488
489static
490void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item)
491{
492 //TODO
493}
494
495static
496void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
497{
498 switch (item->type) {
499 case SIDE_DYNAMIC_TYPE_NULL:
500 printf("<NULL TYPE>");
501 break;
502 case SIDE_DYNAMIC_TYPE_U8:
503 printf("%" PRIu8, item->u.side_u8);
504 break;
505 case SIDE_DYNAMIC_TYPE_U16:
506 printf("%" PRIu16, item->u.side_u16);
507 break;
508 case SIDE_DYNAMIC_TYPE_U32:
509 printf("%" PRIu32, item->u.side_u32);
510 break;
511 case SIDE_DYNAMIC_TYPE_U64:
512 printf("%" PRIu64, item->u.side_u64);
513 break;
514 case SIDE_DYNAMIC_TYPE_S8:
515 printf("%" PRId8, item->u.side_s8);
516 break;
517 case SIDE_DYNAMIC_TYPE_S16:
518 printf("%" PRId16, item->u.side_s16);
519 break;
520 case SIDE_DYNAMIC_TYPE_S32:
521 printf("%" PRId32, item->u.side_s32);
522 break;
523 case SIDE_DYNAMIC_TYPE_S64:
524 printf("%" PRId64, item->u.side_s64);
525 break;
526 case SIDE_DYNAMIC_TYPE_STRING:
527 printf("\"%s\"", item->u.string);
528 break;
c208889e
MD
529 case SIDE_DYNAMIC_TYPE_STRUCT:
530 tracer_print_dynamic_struct(item->u.side_dynamic_struct);
a2e2357e 531 break;
c208889e
MD
532 case SIDE_DYNAMIC_TYPE_STRUCT_VISITOR:
533 tracer_print_dynamic_struct_visitor(item);
a2e2357e
MD
534 break;
535 case SIDE_DYNAMIC_TYPE_VLA:
536 tracer_print_dynamic_vla(item->u.side_dynamic_vla);
537 break;
538 case SIDE_DYNAMIC_TYPE_VLA_VISITOR:
539 tracer_print_dynamic_vla_visitor(item);
540 break;
541 default:
542 printf("<UNKNOWN TYPE>");
543 abort();
544 }
545}
546
f611d0c3
MD
547void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
548{
549 const struct side_arg_vec *sav = sav_desc->sav;
550 uint32_t side_sav_len = sav_desc->len;
551 int i;
552
553 printf("provider: %s, event: %s, ", desc->provider_name, desc->event_name);
554 if (desc->nr_fields != side_sav_len) {
555 printf("ERROR: number of fields mismatch between description and arguments\n");
556 abort();
557 }
558 for (i = 0; i < side_sav_len; i++) {
559 printf("%s", i ? ", " : "");
560 tracer_print_field(&desc->fields[i], &sav[i]);
561 }
562 printf("\n");
563}
This page took 0.061622 seconds and 4 git commands to generate.