Rename "blob" type to "byte"
[libside.git] / src / test.c
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 #include <stdbool.h>
11
12 #include <side/trace.h>
13 #include "tracer.h"
14
15 /* User code example */
16
17 static side_define_event(my_provider_event, "myprovider", "myevent", SIDE_LOGLEVEL_DEBUG,
18 side_field_list(
19 side_field_u32("abc", side_attr_list()),
20 side_field_s64("def", side_attr_list()),
21 side_field_dynamic("dynamic", side_attr_list()),
22 ),
23 side_attr_list()
24 );
25
26 static
27 void test_fields(void)
28 {
29 uint32_t uw = 42;
30 int64_t sdw = -500;
31
32 my_provider_event_enabled = 1;
33 side_event(my_provider_event, side_arg_list(side_arg_u32(uw), side_arg_s64(sdw),
34 side_arg_dynamic(side_arg_dynamic_string("zzz", side_attr_list()))));
35 }
36
37 static side_define_event(my_provider_event_struct_literal, "myprovider", "myeventstructliteral", SIDE_LOGLEVEL_DEBUG,
38 side_field_list(
39 side_field_struct("structliteral",
40 side_struct_literal(
41 side_field_list(
42 side_field_u32("x", side_attr_list()),
43 side_field_s64("y", side_attr_list()),
44 ),
45 side_attr_list()
46 )
47 ),
48 side_field_u8("z", side_attr_list()),
49 ),
50 side_attr_list()
51 );
52
53 static
54 void test_struct_literal(void)
55 {
56 my_provider_event_struct_literal_enabled = 1;
57 side_event_cond(my_provider_event_struct_literal) {
58 side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22)));
59 side_event_call(my_provider_event_struct_literal, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55)));
60 }
61 }
62
63 static side_define_struct(mystructdef,
64 side_field_list(
65 side_field_u32("x", side_attr_list()),
66 side_field_s64("y", side_attr_list()),
67 ),
68 side_attr_list()
69 );
70
71 static side_define_event(my_provider_event_struct, "myprovider", "myeventstruct", SIDE_LOGLEVEL_DEBUG,
72 side_field_list(
73 side_field_struct("struct", &mystructdef),
74 side_field_u8("z", side_attr_list()),
75 ),
76 side_attr_list()
77 );
78
79 static
80 void test_struct(void)
81 {
82 my_provider_event_struct_enabled = 1;
83 side_event_cond(my_provider_event_struct) {
84 side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22)));
85 side_event_call(my_provider_event_struct, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55)));
86 }
87 }
88
89 static side_define_event(my_provider_event_array, "myprovider", "myarray", SIDE_LOGLEVEL_DEBUG,
90 side_field_list(
91 side_field_array("arr", side_elem(side_type_u32(side_attr_list())), 3, side_attr_list()),
92 side_field_s64("v", side_attr_list()),
93 ),
94 side_attr_list()
95 );
96
97 static
98 void test_array(void)
99 {
100 my_provider_event_array_enabled = 1;
101 side_event_cond(my_provider_event_array) {
102 side_arg_define_vec(myarray, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
103 side_event_call(my_provider_event_array, side_arg_list(side_arg_array(&myarray), side_arg_s64(42)));
104 }
105 }
106
107 static side_define_event(my_provider_event_vla, "myprovider", "myvla", SIDE_LOGLEVEL_DEBUG,
108 side_field_list(
109 side_field_vla("vla", side_elem(side_type_u32(side_attr_list())), side_attr_list()),
110 side_field_s64("v", side_attr_list()),
111 ),
112 side_attr_list()
113 );
114
115 static
116 void test_vla(void)
117 {
118 my_provider_event_vla_enabled = 1;
119 side_event_cond(my_provider_event_vla) {
120 side_arg_define_vec(myvla, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
121 side_event_call(my_provider_event_vla, side_arg_list(side_arg_vla(&myvla), side_arg_s64(42)));
122 }
123 }
124
125 /* 1D array visitor */
126
127 struct app_visitor_ctx {
128 const uint32_t *ptr;
129 uint32_t length;
130 };
131
132 static
133 enum side_visitor_status test_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
134 {
135 struct app_visitor_ctx *ctx = (struct app_visitor_ctx *) _ctx;
136 uint32_t length = ctx->length, i;
137
138 for (i = 0; i < length; i++) {
139 const struct side_arg_vec elem = side_arg_u32(ctx->ptr[i]);
140
141 if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK)
142 return SIDE_VISITOR_STATUS_ERROR;
143 }
144 return SIDE_VISITOR_STATUS_OK;
145 }
146
147 static uint32_t testarray[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
148
149 static side_define_event(my_provider_event_vla_visitor, "myprovider", "myvlavisit", SIDE_LOGLEVEL_DEBUG,
150 side_field_list(
151 side_field_vla_visitor("vlavisit", side_elem(side_type_u32(side_attr_list())), test_visitor, side_attr_list()),
152 side_field_s64("v", side_attr_list()),
153 ),
154 side_attr_list()
155 );
156
157 static
158 void test_vla_visitor(void)
159 {
160 my_provider_event_vla_visitor_enabled = 1;
161 side_event_cond(my_provider_event_vla_visitor) {
162 struct app_visitor_ctx ctx = {
163 .ptr = testarray,
164 .length = SIDE_ARRAY_SIZE(testarray),
165 };
166 side_event_call(my_provider_event_vla_visitor, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
167 }
168 }
169
170 /* 2D array visitor */
171
172 struct app_visitor_2d_inner_ctx {
173 const uint32_t *ptr;
174 uint32_t length;
175 };
176
177 static
178 enum side_visitor_status test_inner_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
179 {
180 struct app_visitor_2d_inner_ctx *ctx = (struct app_visitor_2d_inner_ctx *) _ctx;
181 uint32_t length = ctx->length, i;
182
183 for (i = 0; i < length; i++) {
184 const struct side_arg_vec elem = side_arg_u32(ctx->ptr[i]);
185
186 if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK)
187 return SIDE_VISITOR_STATUS_ERROR;
188 }
189 return SIDE_VISITOR_STATUS_OK;
190 }
191
192 struct app_visitor_2d_outer_ctx {
193 const uint32_t (*ptr)[2];
194 uint32_t length;
195 };
196
197 static
198 enum side_visitor_status test_outer_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
199 {
200 struct app_visitor_2d_outer_ctx *ctx = (struct app_visitor_2d_outer_ctx *) _ctx;
201 uint32_t length = ctx->length, i;
202
203 for (i = 0; i < length; i++) {
204 struct app_visitor_2d_inner_ctx inner_ctx = {
205 .ptr = ctx->ptr[i],
206 .length = 2,
207 };
208 const struct side_arg_vec elem = side_arg_vla_visitor(&inner_ctx);
209 if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK)
210 return SIDE_VISITOR_STATUS_ERROR;
211 }
212 return SIDE_VISITOR_STATUS_OK;
213 }
214
215 static uint32_t testarray2d[][2] = {
216 { 1, 2 },
217 { 33, 44 },
218 { 55, 66 },
219 };
220
221 static side_define_event(my_provider_event_vla_visitor2d, "myprovider", "myvlavisit2d", SIDE_LOGLEVEL_DEBUG,
222 side_field_list(
223 side_field_vla_visitor("vlavisit2d",
224 side_elem(
225 side_type_vla_visitor(
226 side_elem(side_type_u32(side_attr_list())),
227 test_inner_visitor,
228 side_attr_list())
229 ), test_outer_visitor, side_attr_list()),
230 side_field_s64("v", side_attr_list()),
231 ),
232 side_attr_list()
233 );
234
235 static
236 void test_vla_visitor_2d(void)
237 {
238 my_provider_event_vla_visitor2d_enabled = 1;
239 side_event_cond(my_provider_event_vla_visitor2d) {
240 struct app_visitor_2d_outer_ctx ctx = {
241 .ptr = testarray2d,
242 .length = SIDE_ARRAY_SIZE(testarray2d),
243 };
244 side_event_call(my_provider_event_vla_visitor2d, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
245 }
246 }
247
248 static int64_t array_fixint[] = { -444, 555, 123, 2897432587 };
249
250 static side_define_event(my_provider_event_array_fixint, "myprovider", "myarrayfixint", SIDE_LOGLEVEL_DEBUG,
251 side_field_list(
252 side_field_array("arrfixint", side_elem(side_type_s64(side_attr_list())), SIDE_ARRAY_SIZE(array_fixint), side_attr_list()),
253 side_field_s64("v", side_attr_list()),
254 ),
255 side_attr_list()
256 );
257
258 static
259 void test_array_fixint(void)
260 {
261 my_provider_event_array_fixint_enabled = 1;
262 side_event(my_provider_event_array_fixint,
263 side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42)));
264 }
265
266 static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 };
267
268 static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG,
269 side_field_list(
270 side_field_vla("vlafixint", side_elem(side_type_s64(side_attr_list())), side_attr_list()),
271 side_field_s64("v", side_attr_list()),
272 ),
273 side_attr_list()
274 );
275
276 static
277 void test_vla_fixint(void)
278 {
279 my_provider_event_vla_fixint_enabled = 1;
280 side_event(my_provider_event_vla_fixint,
281 side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42)));
282 }
283
284 static side_define_event(my_provider_event_dynamic_basic,
285 "myprovider", "mydynamicbasic", SIDE_LOGLEVEL_DEBUG,
286 side_field_list(
287 side_field_dynamic("dynamic", side_attr_list()),
288 ),
289 side_attr_list()
290 );
291
292 static
293 void test_dynamic_basic_type(void)
294 {
295 my_provider_event_dynamic_basic_enabled = 1;
296 side_event(my_provider_event_dynamic_basic,
297 side_arg_list(side_arg_dynamic(side_arg_dynamic_s16(-33, side_attr_list()))));
298 }
299
300 static side_define_event(my_provider_event_dynamic_vla,
301 "myprovider", "mydynamicvla", SIDE_LOGLEVEL_DEBUG,
302 side_field_list(
303 side_field_dynamic("dynamic", side_attr_list()),
304 ),
305 side_attr_list()
306 );
307
308 static
309 void test_dynamic_vla(void)
310 {
311 side_arg_dynamic_define_vec(myvla,
312 side_arg_list(
313 side_arg_dynamic_u32(1, side_attr_list()),
314 side_arg_dynamic_u32(2, side_attr_list()),
315 side_arg_dynamic_u32(3, side_attr_list()),
316 ),
317 side_attr_list()
318 );
319 my_provider_event_dynamic_vla_enabled = 1;
320 side_event(my_provider_event_dynamic_vla,
321 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
322 }
323
324 static side_define_event(my_provider_event_dynamic_null,
325 "myprovider", "mydynamicnull", SIDE_LOGLEVEL_DEBUG,
326 side_field_list(
327 side_field_dynamic("dynamic", side_attr_list()),
328 ),
329 side_attr_list()
330 );
331
332 static
333 void test_dynamic_null(void)
334 {
335 my_provider_event_dynamic_null_enabled = 1;
336 side_event(my_provider_event_dynamic_null,
337 side_arg_list(side_arg_dynamic(side_arg_dynamic_null(side_attr_list()))));
338 }
339
340 static side_define_event(my_provider_event_dynamic_struct,
341 "myprovider", "mydynamicstruct", SIDE_LOGLEVEL_DEBUG,
342 side_field_list(
343 side_field_dynamic("dynamic", side_attr_list()),
344 ),
345 side_attr_list()
346 );
347
348 static
349 void test_dynamic_struct(void)
350 {
351 side_arg_dynamic_define_struct(mystruct,
352 side_arg_list(
353 side_arg_dynamic_field("a", side_arg_dynamic_u32(43, side_attr_list())),
354 side_arg_dynamic_field("b", side_arg_dynamic_string("zzz", side_attr_list())),
355 side_arg_dynamic_field("c", side_arg_dynamic_null(side_attr_list())),
356 ),
357 side_attr_list()
358 );
359
360 my_provider_event_dynamic_struct_enabled = 1;
361 side_event(my_provider_event_dynamic_struct,
362 side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct))));
363 }
364
365 static side_define_event(my_provider_event_dynamic_nested_struct,
366 "myprovider", "mydynamicnestedstruct", SIDE_LOGLEVEL_DEBUG,
367 side_field_list(
368 side_field_dynamic("dynamic", side_attr_list()),
369 ),
370 side_attr_list()
371 );
372
373 static
374 void test_dynamic_nested_struct(void)
375 {
376 side_arg_dynamic_define_struct(nested,
377 side_arg_list(
378 side_arg_dynamic_field("a", side_arg_dynamic_u32(43, side_attr_list())),
379 side_arg_dynamic_field("b", side_arg_dynamic_u8(55, side_attr_list())),
380 ),
381 side_attr_list()
382 );
383 side_arg_dynamic_define_struct(nested2,
384 side_arg_list(
385 side_arg_dynamic_field("aa", side_arg_dynamic_u64(128, side_attr_list())),
386 side_arg_dynamic_field("bb", side_arg_dynamic_u16(1, side_attr_list())),
387 ),
388 side_attr_list()
389 );
390 side_arg_dynamic_define_struct(mystruct,
391 side_arg_list(
392 side_arg_dynamic_field("nested", side_arg_dynamic_struct(&nested)),
393 side_arg_dynamic_field("nested2", side_arg_dynamic_struct(&nested2)),
394 ),
395 side_attr_list()
396 );
397 my_provider_event_dynamic_nested_struct_enabled = 1;
398 side_event(my_provider_event_dynamic_nested_struct,
399 side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct))));
400 }
401
402 static side_define_event(my_provider_event_dynamic_vla_struct,
403 "myprovider", "mydynamicvlastruct", SIDE_LOGLEVEL_DEBUG,
404 side_field_list(
405 side_field_dynamic("dynamic", side_attr_list()),
406 ),
407 side_attr_list()
408 );
409
410 static
411 void test_dynamic_vla_struct(void)
412 {
413 side_arg_dynamic_define_struct(nested,
414 side_arg_list(
415 side_arg_dynamic_field("a", side_arg_dynamic_u32(43, side_attr_list())),
416 side_arg_dynamic_field("b", side_arg_dynamic_u8(55, side_attr_list())),
417 ),
418 side_attr_list()
419 );
420 side_arg_dynamic_define_vec(myvla,
421 side_arg_list(
422 side_arg_dynamic_struct(&nested),
423 side_arg_dynamic_struct(&nested),
424 side_arg_dynamic_struct(&nested),
425 side_arg_dynamic_struct(&nested),
426 ),
427 side_attr_list()
428 );
429 my_provider_event_dynamic_vla_struct_enabled = 1;
430 side_event(my_provider_event_dynamic_vla_struct,
431 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
432 }
433
434 static side_define_event(my_provider_event_dynamic_struct_vla,
435 "myprovider", "mydynamicstructvla", SIDE_LOGLEVEL_DEBUG,
436 side_field_list(
437 side_field_dynamic("dynamic", side_attr_list()),
438 ),
439 side_attr_list()
440 );
441
442 static
443 void test_dynamic_struct_vla(void)
444 {
445 side_arg_dynamic_define_vec(myvla,
446 side_arg_list(
447 side_arg_dynamic_u32(1, side_attr_list()),
448 side_arg_dynamic_u32(2, side_attr_list()),
449 side_arg_dynamic_u32(3, side_attr_list()),
450 ),
451 side_attr_list()
452 );
453 side_arg_dynamic_define_vec(myvla2,
454 side_arg_list(
455 side_arg_dynamic_u32(4, side_attr_list()),
456 side_arg_dynamic_u64(5, side_attr_list()),
457 side_arg_dynamic_u32(6, side_attr_list()),
458 ),
459 side_attr_list()
460 );
461 side_arg_dynamic_define_struct(mystruct,
462 side_arg_list(
463 side_arg_dynamic_field("a", side_arg_dynamic_vla(&myvla)),
464 side_arg_dynamic_field("b", side_arg_dynamic_vla(&myvla2)),
465 ),
466 side_attr_list()
467 );
468 my_provider_event_dynamic_struct_vla_enabled = 1;
469 side_event(my_provider_event_dynamic_struct_vla,
470 side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct))));
471 }
472
473 static side_define_event(my_provider_event_dynamic_nested_vla,
474 "myprovider", "mydynamicnestedvla", SIDE_LOGLEVEL_DEBUG,
475 side_field_list(
476 side_field_dynamic("dynamic", side_attr_list()),
477 ),
478 side_attr_list()
479 );
480
481 static
482 void test_dynamic_nested_vla(void)
483 {
484 side_arg_dynamic_define_vec(nestedvla,
485 side_arg_list(
486 side_arg_dynamic_u32(1, side_attr_list()),
487 side_arg_dynamic_u16(2, side_attr_list()),
488 side_arg_dynamic_u32(3, side_attr_list()),
489 ),
490 side_attr_list()
491 );
492 side_arg_dynamic_define_vec(nestedvla2,
493 side_arg_list(
494 side_arg_dynamic_u8(4, side_attr_list()),
495 side_arg_dynamic_u32(5, side_attr_list()),
496 side_arg_dynamic_u32(6, side_attr_list()),
497 ),
498 side_attr_list()
499 );
500 side_arg_dynamic_define_vec(myvla,
501 side_arg_list(
502 side_arg_dynamic_vla(&nestedvla),
503 side_arg_dynamic_vla(&nestedvla2),
504 ),
505 side_attr_list()
506 );
507 my_provider_event_dynamic_nested_vla_enabled = 1;
508 side_event(my_provider_event_dynamic_nested_vla,
509 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
510 }
511
512 static side_define_event_variadic(my_provider_event_variadic,
513 "myprovider", "myvariadicevent", SIDE_LOGLEVEL_DEBUG,
514 side_field_list(),
515 side_attr_list()
516 );
517
518 static
519 void test_variadic(void)
520 {
521 my_provider_event_variadic_enabled = 1;
522 side_event_variadic(my_provider_event_variadic,
523 side_arg_list(),
524 side_arg_list(
525 side_arg_dynamic_field("a", side_arg_dynamic_u32(55, side_attr_list())),
526 side_arg_dynamic_field("b", side_arg_dynamic_s8(-4, side_attr_list())),
527 ),
528 side_attr_list()
529 );
530 }
531
532 static side_define_event_variadic(my_provider_event_static_variadic,
533 "myprovider", "mystaticvariadicevent", SIDE_LOGLEVEL_DEBUG,
534 side_field_list(
535 side_field_u32("abc", side_attr_list()),
536 side_field_u16("def", side_attr_list()),
537 ),
538 side_attr_list()
539 );
540
541 static
542 void test_static_variadic(void)
543 {
544 my_provider_event_static_variadic_enabled = 1;
545 side_event_variadic(my_provider_event_static_variadic,
546 side_arg_list(
547 side_arg_u32(1),
548 side_arg_u16(2),
549 ),
550 side_arg_list(
551 side_arg_dynamic_field("a", side_arg_dynamic_u32(55, side_attr_list())),
552 side_arg_dynamic_field("b", side_arg_dynamic_s8(-4, side_attr_list())),
553 ),
554 side_attr_list()
555 );
556 }
557
558 static side_define_event(my_provider_event_bool, "myprovider", "myeventbool", SIDE_LOGLEVEL_DEBUG,
559 side_field_list(
560 side_field_bool("a_false", side_attr_list()),
561 side_field_bool("b_true", side_attr_list()),
562 side_field_bool("c_true", side_attr_list()),
563 side_field_bool("d_true", side_attr_list()),
564 side_field_bool("e_true", side_attr_list()),
565 side_field_bool("f_false", side_attr_list()),
566 side_field_bool("g_true", side_attr_list()),
567 ),
568 side_attr_list()
569 );
570
571 static
572 void test_bool(void)
573 {
574 uint32_t a = 0;
575 uint32_t b = 1;
576 uint64_t c = 0x12345678;
577 int16_t d = -32768;
578 bool e = true;
579 bool f = false;
580 uint32_t g = 256;
581
582 my_provider_event_bool_enabled = 1;
583 side_event(my_provider_event_bool,
584 side_arg_list(
585 side_arg_bool(a),
586 side_arg_bool(b),
587 side_arg_bool(c),
588 side_arg_bool(d),
589 side_arg_bool(e),
590 side_arg_bool(f),
591 side_arg_bool(g),
592 )
593 );
594 }
595
596 static side_define_event_variadic(my_provider_event_dynamic_bool,
597 "myprovider", "mydynamicbool", SIDE_LOGLEVEL_DEBUG,
598 side_field_list(),
599 side_attr_list()
600 );
601
602 static
603 void test_dynamic_bool(void)
604 {
605 my_provider_event_dynamic_bool_enabled = 1;
606 side_event_variadic(my_provider_event_dynamic_bool,
607 side_arg_list(),
608 side_arg_list(
609 side_arg_dynamic_field("a_true", side_arg_dynamic_bool(55, side_attr_list())),
610 side_arg_dynamic_field("b_true", side_arg_dynamic_bool(-4, side_attr_list())),
611 side_arg_dynamic_field("c_false", side_arg_dynamic_bool(0, side_attr_list())),
612 side_arg_dynamic_field("d_true", side_arg_dynamic_bool(256, side_attr_list())),
613 ),
614 side_attr_list()
615 );
616 }
617
618 static side_define_event(my_provider_event_dynamic_vla_visitor,
619 "myprovider", "mydynamicvlavisitor", SIDE_LOGLEVEL_DEBUG,
620 side_field_list(
621 side_field_dynamic("dynamic", side_attr_list()),
622 ),
623 side_attr_list()
624 );
625
626 struct app_dynamic_vla_visitor_ctx {
627 const uint32_t *ptr;
628 uint32_t length;
629 };
630
631 static
632 enum side_visitor_status test_dynamic_vla_visitor(const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx, void *_ctx)
633 {
634 struct app_dynamic_vla_visitor_ctx *ctx = (struct app_dynamic_vla_visitor_ctx *) _ctx;
635 uint32_t length = ctx->length, i;
636
637 for (i = 0; i < length; i++) {
638 const struct side_arg_dynamic_vec elem = {
639 .dynamic_type = SIDE_DYNAMIC_TYPE_U32,
640 .u = {
641 .side_basic = {
642 .attr = NULL,
643 .nr_attr = 0,
644 .u = {
645 .side_u32 = ctx->ptr[i],
646 },
647 },
648 },
649 };
650 if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK)
651 return SIDE_VISITOR_STATUS_ERROR;
652 }
653 return SIDE_VISITOR_STATUS_OK;
654 }
655
656 static uint32_t testarray_dynamic_vla[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
657
658 static
659 void test_dynamic_vla_with_visitor(void)
660 {
661 my_provider_event_dynamic_vla_visitor_enabled = 1;
662 side_event_cond(my_provider_event_dynamic_vla_visitor) {
663 struct app_dynamic_vla_visitor_ctx ctx = {
664 .ptr = testarray_dynamic_vla,
665 .length = SIDE_ARRAY_SIZE(testarray_dynamic_vla),
666 };
667 side_event_call(my_provider_event_dynamic_vla_visitor,
668 side_arg_list(
669 side_arg_dynamic(
670 side_arg_dynamic_vla_visitor(test_dynamic_vla_visitor, &ctx, side_attr_list())
671 )
672 )
673 );
674 }
675 }
676
677 static side_define_event(my_provider_event_dynamic_struct_visitor,
678 "myprovider", "mydynamicstructvisitor", SIDE_LOGLEVEL_DEBUG,
679 side_field_list(
680 side_field_dynamic("dynamic", side_attr_list()),
681 ),
682 side_attr_list()
683 );
684
685 struct struct_visitor_pair {
686 const char *name;
687 uint32_t value;
688 };
689
690 struct app_dynamic_struct_visitor_ctx {
691 const struct struct_visitor_pair *ptr;
692 uint32_t length;
693 };
694
695 static
696 enum side_visitor_status test_dynamic_struct_visitor(const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx, void *_ctx)
697 {
698 struct app_dynamic_struct_visitor_ctx *ctx = (struct app_dynamic_struct_visitor_ctx *) _ctx;
699 uint32_t length = ctx->length, i;
700
701 for (i = 0; i < length; i++) {
702 struct side_arg_dynamic_event_field dynamic_field = {
703 .field_name = ctx->ptr[i].name,
704 .elem = {
705 .dynamic_type = SIDE_DYNAMIC_TYPE_U32,
706 .u = {
707 .side_basic = {
708 .nr_attr = 0,
709 .attr = NULL,
710 .u = {
711 .side_u32 = ctx->ptr[i].value,
712 },
713 },
714 },
715 },
716 };
717 if (tracer_ctx->write_field(tracer_ctx, &dynamic_field) != SIDE_VISITOR_STATUS_OK)
718 return SIDE_VISITOR_STATUS_ERROR;
719 }
720 return SIDE_VISITOR_STATUS_OK;
721 }
722
723 static struct struct_visitor_pair testarray_dynamic_struct[] = {
724 { "a", 1, },
725 { "b", 2, },
726 { "c", 3, },
727 { "d", 4, },
728 };
729
730 static
731 void test_dynamic_struct_with_visitor(void)
732 {
733 my_provider_event_dynamic_struct_visitor_enabled = 1;
734 side_event_cond(my_provider_event_dynamic_struct_visitor) {
735 struct app_dynamic_struct_visitor_ctx ctx = {
736 .ptr = testarray_dynamic_struct,
737 .length = SIDE_ARRAY_SIZE(testarray_dynamic_struct),
738 };
739 side_event_call(my_provider_event_dynamic_struct_visitor,
740 side_arg_list(
741 side_arg_dynamic(
742 side_arg_dynamic_struct_visitor(test_dynamic_struct_visitor, &ctx, side_attr_list())
743 )
744 )
745 );
746 }
747 }
748
749 static side_define_event(my_provider_event_user_attribute, "myprovider", "myevent_user_attribute", SIDE_LOGLEVEL_DEBUG,
750 side_field_list(
751 side_field_u32("abc", side_attr_list()),
752 side_field_s64("def", side_attr_list()),
753 ),
754 side_attr_list(
755 side_attr("user_attribute_a", side_attr_string("val1")),
756 side_attr("user_attribute_b", side_attr_string("val2")),
757 )
758 );
759
760 static
761 void test_event_user_attribute(void)
762 {
763 my_provider_event_user_attribute_enabled = 1;
764 side_event(my_provider_event_user_attribute, side_arg_list(side_arg_u32(1), side_arg_s64(2)));
765 }
766
767 static side_define_event(my_provider_field_user_attribute, "myprovider", "myevent_field_attribute", SIDE_LOGLEVEL_DEBUG,
768 side_field_list(
769 side_field_u32("abc",
770 side_attr_list(
771 side_attr("user_attribute_a", side_attr_string("val1")),
772 side_attr("user_attribute_b", side_attr_u32(2)),
773 )
774 ),
775 side_field_s64("def",
776 side_attr_list(
777 side_attr("user_attribute_c", side_attr_string("val3")),
778 side_attr("user_attribute_d", side_attr_s64(-5)),
779 )
780 ),
781 ),
782 side_attr_list()
783 );
784
785 static
786 void test_field_user_attribute(void)
787 {
788 my_provider_field_user_attribute_enabled = 1;
789 side_event(my_provider_field_user_attribute, side_arg_list(side_arg_u32(1), side_arg_s64(2)));
790 }
791
792 static side_define_event_variadic(my_provider_event_variadic_attr,
793 "myprovider", "myvariadiceventattr", SIDE_LOGLEVEL_DEBUG,
794 side_field_list(),
795 side_attr_list()
796 );
797
798 static
799 void test_variadic_attr(void)
800 {
801 my_provider_event_variadic_attr_enabled = 1;
802 side_event_variadic(my_provider_event_variadic_attr,
803 side_arg_list(),
804 side_arg_list(
805 side_arg_dynamic_field("a",
806 side_arg_dynamic_u32(55,
807 side_attr_list(
808 side_attr("user_attribute_c", side_attr_string("valX")),
809 side_attr("user_attribute_d", side_attr_u8(55)),
810 )
811 )
812 ),
813 side_arg_dynamic_field("b",
814 side_arg_dynamic_s8(-4,
815 side_attr_list(
816 side_attr("X", side_attr_u8(1)),
817 side_attr("Y", side_attr_s8(2)),
818 )
819 )
820 ),
821 ),
822 side_attr_list()
823 );
824 }
825
826 static side_define_event_variadic(my_provider_event_variadic_vla_attr,
827 "myprovider", "myvariadiceventvlaattr", SIDE_LOGLEVEL_DEBUG,
828 side_field_list(),
829 side_attr_list()
830 );
831
832 static
833 void test_variadic_vla_attr(void)
834 {
835 side_arg_dynamic_define_vec(myvla,
836 side_arg_list(
837 side_arg_dynamic_u32(1,
838 side_attr_list(
839 side_attr("Z", side_attr_u8(0)),
840 side_attr("A", side_attr_u8(123)),
841 )
842 ),
843 side_arg_dynamic_u32(2, side_attr_list()),
844 side_arg_dynamic_u32(3, side_attr_list()),
845 ),
846 side_attr_list(
847 side_attr("X", side_attr_u8(1)),
848 side_attr("Y", side_attr_u8(2)),
849 )
850 );
851 my_provider_event_variadic_vla_attr_enabled = 1;
852 side_event_variadic(my_provider_event_variadic_vla_attr,
853 side_arg_list(),
854 side_arg_list(
855 side_arg_dynamic_field("a", side_arg_dynamic_vla(&myvla)),
856 ),
857 side_attr_list()
858 );
859 }
860
861 static side_define_event_variadic(my_provider_event_variadic_struct_attr,
862 "myprovider", "myvariadiceventstructattr", SIDE_LOGLEVEL_DEBUG,
863 side_field_list(),
864 side_attr_list()
865 );
866
867 static
868 void test_variadic_struct_attr(void)
869 {
870 my_provider_event_variadic_struct_attr_enabled = 1;
871 side_event_cond(my_provider_event_variadic_struct_attr) {
872 side_arg_dynamic_define_struct(mystruct,
873 side_arg_list(
874 side_arg_dynamic_field("a",
875 side_arg_dynamic_u32(43,
876 side_attr_list(
877 side_attr("A", side_attr_bool(true)),
878 )
879 )
880 ),
881 side_arg_dynamic_field("b", side_arg_dynamic_u8(55, side_attr_list())),
882 ),
883 side_attr_list(
884 side_attr("X", side_attr_u8(1)),
885 side_attr("Y", side_attr_u8(2)),
886 )
887 );
888 side_event_call_variadic(my_provider_event_variadic_struct_attr,
889 side_arg_list(),
890 side_arg_list(
891 side_arg_dynamic_field("a", side_arg_dynamic_struct(&mystruct)),
892 ),
893 side_attr_list()
894 );
895 }
896 }
897
898 static side_define_event(my_provider_event_float, "myprovider", "myeventfloat", SIDE_LOGLEVEL_DEBUG,
899 side_field_list(
900 #if __HAVE_FLOAT16
901 side_field_float_binary16("binary16", side_attr_list()),
902 #endif
903 #if __HAVE_FLOAT32
904 side_field_float_binary32("binary32", side_attr_list()),
905 #endif
906 #if __HAVE_FLOAT64
907 side_field_float_binary64("binary64", side_attr_list()),
908 #endif
909 #if __HAVE_FLOAT128
910 side_field_float_binary128("binary128", side_attr_list()),
911 #endif
912 ),
913 side_attr_list()
914 );
915
916 static
917 void test_float(void)
918 {
919 my_provider_event_float_enabled = 1;
920 side_event(my_provider_event_float,
921 side_arg_list(
922 #if __HAVE_FLOAT16
923 side_arg_float_binary16(1.1),
924 #endif
925 #if __HAVE_FLOAT32
926 side_arg_float_binary32(2.2),
927 #endif
928 #if __HAVE_FLOAT64
929 side_arg_float_binary64(3.3),
930 #endif
931 #if __HAVE_FLOAT128
932 side_arg_float_binary128(4.4),
933 #endif
934 )
935 );
936 }
937
938 static side_define_event_variadic(my_provider_event_variadic_float,
939 "myprovider", "myvariadicfloat", SIDE_LOGLEVEL_DEBUG,
940 side_field_list(),
941 side_attr_list()
942 );
943
944 static
945 void test_variadic_float(void)
946 {
947 my_provider_event_variadic_float_enabled = 1;
948 side_event_variadic(my_provider_event_variadic_float,
949 side_arg_list(),
950 side_arg_list(
951 #if __HAVE_FLOAT16
952 side_arg_dynamic_field("binary16",
953 side_arg_dynamic_float_binary16(1.1, side_attr_list())
954 ),
955 #endif
956 #if __HAVE_FLOAT32
957 side_arg_dynamic_field("binary32",
958 side_arg_dynamic_float_binary32(2.2, side_attr_list())
959 ),
960 #endif
961 #if __HAVE_FLOAT64
962 side_arg_dynamic_field("binary64",
963 side_arg_dynamic_float_binary64(3.3, side_attr_list())
964 ),
965 #endif
966 #if __HAVE_FLOAT128
967 side_arg_dynamic_field("binary128",
968 side_arg_dynamic_float_binary128(4.4, side_attr_list())
969 ),
970 #endif
971 ),
972 side_attr_list()
973 );
974 }
975
976 static side_define_enum(myenum,
977 side_enum_mapping_list(
978 side_enum_mapping_range("one-ten", 1, 10),
979 side_enum_mapping_range("100-200", 100, 200),
980 side_enum_mapping_value("200", 200),
981 side_enum_mapping_value("300", 300),
982 ),
983 side_attr_list()
984 );
985
986 static side_define_event(my_provider_event_enum, "myprovider", "myeventenum", SIDE_LOGLEVEL_DEBUG,
987 side_field_list(
988 side_field_enum("5", &myenum, side_elem(side_type_u32(side_attr_list()))),
989 side_field_enum("400", &myenum, side_elem(side_type_u64(side_attr_list()))),
990 side_field_enum("200", &myenum, side_elem(side_type_u8(side_attr_list()))),
991 side_field_enum("-100", &myenum, side_elem(side_type_s8(side_attr_list()))),
992 ),
993 side_attr_list()
994 );
995
996 static
997 void test_enum(void)
998 {
999 my_provider_event_enum_enabled = 1;
1000 side_event(my_provider_event_enum,
1001 side_arg_list(
1002 side_arg_u32(5),
1003 side_arg_u64(400),
1004 side_arg_u8(200),
1005 side_arg_s8(-100),
1006 )
1007 );
1008 }
1009
1010 /* A bitmap enum maps bits to labels. */
1011 static side_define_enum_bitmap(myenum_bitmap,
1012 side_enum_bitmap_mapping_list(
1013 side_enum_bitmap_mapping_value("0", 0),
1014 side_enum_bitmap_mapping_range("1-2", 1, 2),
1015 side_enum_bitmap_mapping_range("2-4", 2, 4),
1016 side_enum_bitmap_mapping_value("3", 3),
1017 side_enum_bitmap_mapping_value("30", 30),
1018 side_enum_bitmap_mapping_value("63", 63),
1019 side_enum_bitmap_mapping_range("158-160", 158, 160),
1020 side_enum_bitmap_mapping_value("159", 159),
1021 side_enum_bitmap_mapping_range("500-700", 500, 700),
1022 ),
1023 side_attr_list()
1024 );
1025
1026 static side_define_event(my_provider_event_enum_bitmap, "myprovider", "myeventenumbitmap", SIDE_LOGLEVEL_DEBUG,
1027 side_field_list(
1028 side_field_enum_bitmap("bit_0", &myenum_bitmap, side_elem(side_type_u32(side_attr_list()))),
1029 side_field_enum_bitmap("bit_1", &myenum_bitmap, side_elem(side_type_u32(side_attr_list()))),
1030 side_field_enum_bitmap("bit_2", &myenum_bitmap, side_elem(side_type_u8(side_attr_list()))),
1031 side_field_enum_bitmap("bit_3", &myenum_bitmap, side_elem(side_type_u8(side_attr_list()))),
1032 side_field_enum_bitmap("bit_30", &myenum_bitmap, side_elem(side_type_u32(side_attr_list()))),
1033 side_field_enum_bitmap("bit_31", &myenum_bitmap, side_elem(side_type_u32(side_attr_list()))),
1034 side_field_enum_bitmap("bit_63", &myenum_bitmap, side_elem(side_type_u64(side_attr_list()))),
1035 side_field_enum_bitmap("bits_1+63", &myenum_bitmap, side_elem(side_type_u64(side_attr_list()))),
1036 side_field_enum_bitmap("bit_159", &myenum_bitmap,
1037 side_elem(side_type_array(side_elem(side_type_u32(side_attr_list())), 5, side_attr_list()))),
1038 side_field_enum_bitmap("bit_159", &myenum_bitmap,
1039 side_elem(side_type_vla(side_elem(side_type_u32(side_attr_list())), side_attr_list()))),
1040 ),
1041 side_attr_list()
1042 );
1043
1044 static
1045 void test_enum_bitmap(void)
1046 {
1047 my_provider_event_enum_bitmap_enabled = 1;
1048 side_event_cond(my_provider_event_enum_bitmap) {
1049 side_arg_define_vec(myarray,
1050 side_arg_list(
1051 side_arg_u32(0),
1052 side_arg_u32(0),
1053 side_arg_u32(0),
1054 side_arg_u32(0),
1055 side_arg_u32(0x80000000), /* bit 159 */
1056 )
1057 );
1058 side_event_call(my_provider_event_enum_bitmap,
1059 side_arg_list(
1060 side_arg_u32(1 << 0),
1061 side_arg_u32(1 << 1),
1062 side_arg_u8(1 << 2),
1063 side_arg_u8(1 << 3),
1064 side_arg_u32(1 << 30),
1065 side_arg_u32(1 << 31),
1066 side_arg_u64(1ULL << 63),
1067 side_arg_u64((1ULL << 1) | (1ULL << 63)),
1068 side_arg_array(&myarray),
1069 side_arg_vla(&myarray),
1070 )
1071 );
1072 }
1073 }
1074
1075 static uint8_t blob_fixint[] = { 0x55, 0x44, 0x33, 0x22, 0x11 };
1076
1077 static side_define_event_variadic(my_provider_event_blob, "myprovider", "myeventblob", SIDE_LOGLEVEL_DEBUG,
1078 side_field_list(
1079 side_field_byte("blobfield", side_attr_list()),
1080 side_field_array("arrayblob", side_elem(side_type_byte(side_attr_list())), 3, side_attr_list()),
1081 side_field_array("arrayblobfix", side_elem(side_type_byte(side_attr_list())), SIDE_ARRAY_SIZE(blob_fixint), side_attr_list()),
1082 side_field_vla("vlablobfix", side_elem(side_type_byte(side_attr_list())), side_attr_list()),
1083 ),
1084 side_attr_list()
1085 );
1086
1087 static
1088 void test_blob(void)
1089 {
1090 my_provider_event_blob_enabled = 1;
1091 side_event_cond(my_provider_event_blob) {
1092 side_arg_define_vec(myarray, side_arg_list(side_arg_byte(1), side_arg_byte(2), side_arg_byte(3)));
1093 side_arg_dynamic_define_vec(myvla,
1094 side_arg_list(
1095 side_arg_dynamic_byte(0x22, side_attr_list()),
1096 side_arg_dynamic_byte(0x33, side_attr_list()),
1097 ),
1098 side_attr_list()
1099 );
1100 side_event_call_variadic(my_provider_event_blob,
1101 side_arg_list(
1102 side_arg_byte(0x55),
1103 side_arg_array(&myarray),
1104 side_arg_array_byte(blob_fixint),
1105 side_arg_vla_byte(blob_fixint, SIDE_ARRAY_SIZE(blob_fixint)),
1106 ),
1107 side_arg_list(
1108 side_arg_dynamic_field("varblobfield",
1109 side_arg_dynamic_byte(0x55, side_attr_list())
1110 ),
1111 side_arg_dynamic_field("varblobvla", side_arg_dynamic_vla(&myvla)),
1112 ),
1113 side_attr_list()
1114 );
1115 }
1116 }
1117
1118 static side_define_event_variadic(my_provider_event_format_string,
1119 "myprovider", "myeventformatstring", SIDE_LOGLEVEL_DEBUG,
1120 side_field_list(
1121 side_field_string("fmt", side_attr_list()),
1122 ),
1123 side_attr_list(
1124 side_attr("lang.c.format_string", side_attr_bool(true)),
1125 )
1126 );
1127
1128 static
1129 void test_fmt_string(void)
1130 {
1131 my_provider_event_format_string_enabled = 1;
1132 side_event_cond(my_provider_event_format_string) {
1133 side_arg_dynamic_define_vec(args,
1134 side_arg_list(
1135 side_arg_dynamic_string("blah", side_attr_list()),
1136 side_arg_dynamic_s32(123, side_attr_list()),
1137 ),
1138 side_attr_list()
1139 );
1140 side_event_call_variadic(my_provider_event_format_string,
1141 side_arg_list(
1142 side_arg_string("This is a formatted string with str: %s int: %d"),
1143 ),
1144 side_arg_list(
1145 side_arg_dynamic_field("arguments", side_arg_dynamic_vla(&args)),
1146 ),
1147 side_attr_list()
1148 );
1149 }
1150 }
1151
1152 int main()
1153 {
1154 test_fields();
1155 test_struct_literal();
1156 test_struct();
1157 test_array();
1158 test_vla();
1159 test_vla_visitor();
1160 test_vla_visitor_2d();
1161 test_array_fixint();
1162 test_vla_fixint();
1163 test_dynamic_basic_type();
1164 test_dynamic_vla();
1165 test_dynamic_null();
1166 test_dynamic_struct();
1167 test_dynamic_nested_struct();
1168 test_dynamic_vla_struct();
1169 test_dynamic_struct_vla();
1170 test_dynamic_nested_vla();
1171 test_variadic();
1172 test_static_variadic();
1173 test_bool();
1174 test_dynamic_bool();
1175 test_dynamic_vla_with_visitor();
1176 test_dynamic_struct_with_visitor();
1177 test_event_user_attribute();
1178 test_field_user_attribute();
1179 test_variadic_attr();
1180 test_variadic_vla_attr();
1181 test_variadic_struct_attr();
1182 test_float();
1183 test_variadic_float();
1184 test_enum();
1185 test_enum_bitmap();
1186 test_blob();
1187 test_fmt_string();
1188 return 0;
1189 }
This page took 0.067236 seconds and 4 git commands to generate.