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 | #include "tracer.h" | |
13 | ||
14 | /* User code example */ | |
15 | ||
16 | static side_define_event(my_provider_event, "myprovider", "myevent", SIDE_LOGLEVEL_DEBUG, | |
17 | side_field_list( | |
91d140a6 MD |
18 | side_field("abc", SIDE_TYPE_U32), |
19 | side_field("def", SIDE_TYPE_S64), | |
20 | side_field("dynamic", SIDE_TYPE_DYNAMIC), | |
f611d0c3 MD |
21 | ) |
22 | ); | |
23 | ||
24 | static | |
25 | void test_fields(void) | |
26 | { | |
27 | uint32_t uw = 42; | |
28 | int64_t sdw = -500; | |
29 | ||
30 | my_provider_event.enabled = 1; | |
a2e2357e MD |
31 | side_event(&my_provider_event, side_arg_list(side_arg_u32(uw), side_arg_s64(sdw), |
32 | side_arg_dynamic(side_arg_dynamic_string("zzz")))); | |
f611d0c3 MD |
33 | } |
34 | ||
35 | static side_define_event(my_provider_event2, "myprovider", "myevent2", SIDE_LOGLEVEL_DEBUG, | |
36 | side_field_list( | |
37 | side_field_struct("structfield", | |
38 | side_field_list( | |
91d140a6 MD |
39 | side_field("x", SIDE_TYPE_U32), |
40 | side_field("y", SIDE_TYPE_S64), | |
f611d0c3 MD |
41 | ) |
42 | ), | |
91d140a6 | 43 | side_field("z", SIDE_TYPE_U8), |
f611d0c3 MD |
44 | ) |
45 | ); | |
46 | ||
47 | static | |
48 | void test_struct(void) | |
49 | { | |
50 | my_provider_event2.enabled = 1; | |
51 | side_event_cond(&my_provider_event2) { | |
52 | side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22))); | |
53 | side_event_call(&my_provider_event2, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55))); | |
54 | } | |
55 | } | |
56 | ||
57 | static side_define_event(my_provider_event_array, "myprovider", "myarray", SIDE_LOGLEVEL_DEBUG, | |
58 | side_field_list( | |
cdd6e858 | 59 | side_field_array("arr", side_elem_type(SIDE_TYPE_U32), 3), |
91d140a6 | 60 | side_field("v", SIDE_TYPE_S64), |
f611d0c3 MD |
61 | ) |
62 | ); | |
63 | ||
64 | static | |
65 | void test_array(void) | |
66 | { | |
67 | my_provider_event_array.enabled = 1; | |
68 | side_event_cond(&my_provider_event_array) { | |
69 | side_arg_define_vec(myarray, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3))); | |
70 | side_event_call(&my_provider_event_array, side_arg_list(side_arg_array(&myarray), side_arg_s64(42))); | |
71 | } | |
72 | } | |
73 | ||
74 | static side_define_event(my_provider_event_vla, "myprovider", "myvla", SIDE_LOGLEVEL_DEBUG, | |
75 | side_field_list( | |
cdd6e858 | 76 | side_field_vla("vla", side_elem_type(SIDE_TYPE_U32)), |
91d140a6 | 77 | side_field("v", SIDE_TYPE_S64), |
f611d0c3 MD |
78 | ) |
79 | ); | |
80 | ||
81 | static | |
82 | void test_vla(void) | |
83 | { | |
84 | my_provider_event_vla.enabled = 1; | |
85 | side_event_cond(&my_provider_event_vla) { | |
86 | side_arg_define_vec(myvla, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3))); | |
87 | side_event_call(&my_provider_event_vla, side_arg_list(side_arg_vla(&myvla), side_arg_s64(42))); | |
88 | } | |
89 | } | |
90 | ||
cdd6e858 MD |
91 | /* 1D array visitor */ |
92 | ||
f611d0c3 MD |
93 | struct app_visitor_ctx { |
94 | const uint32_t *ptr; | |
352a4b77 | 95 | uint32_t length; |
f611d0c3 MD |
96 | }; |
97 | ||
352a4b77 MD |
98 | static |
99 | enum side_visitor_status test_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx) | |
f611d0c3 MD |
100 | { |
101 | struct app_visitor_ctx *ctx = (struct app_visitor_ctx *) _ctx; | |
352a4b77 MD |
102 | uint32_t length = ctx->length, i; |
103 | ||
104 | for (i = 0; i < length; i++) { | |
105 | const struct side_arg_vec elem = { | |
106 | .type = SIDE_TYPE_U32, | |
107 | .u = { | |
108 | .side_u32 = ctx->ptr[i], | |
109 | }, | |
110 | }; | |
db6ecef9 MD |
111 | if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK) |
112 | return SIDE_VISITOR_STATUS_ERROR; | |
352a4b77 | 113 | } |
f611d0c3 MD |
114 | return SIDE_VISITOR_STATUS_OK; |
115 | } | |
116 | ||
117 | static uint32_t testarray[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
118 | ||
119 | static side_define_event(my_provider_event_vla_visitor, "myprovider", "myvlavisit", SIDE_LOGLEVEL_DEBUG, | |
120 | side_field_list( | |
cdd6e858 | 121 | side_field_vla_visitor("vlavisit", side_elem_type(SIDE_TYPE_U32), test_visitor), |
91d140a6 | 122 | side_field("v", SIDE_TYPE_S64), |
f611d0c3 MD |
123 | ) |
124 | ); | |
125 | ||
126 | static | |
127 | void test_vla_visitor(void) | |
128 | { | |
129 | my_provider_event_vla_visitor.enabled = 1; | |
130 | side_event_cond(&my_provider_event_vla_visitor) { | |
131 | struct app_visitor_ctx ctx = { | |
132 | .ptr = testarray, | |
352a4b77 | 133 | .length = SIDE_ARRAY_SIZE(testarray), |
f611d0c3 MD |
134 | }; |
135 | side_event_call(&my_provider_event_vla_visitor, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42))); | |
136 | } | |
137 | } | |
138 | ||
cdd6e858 MD |
139 | /* 2D array visitor */ |
140 | ||
141 | struct app_visitor_2d_inner_ctx { | |
142 | const uint32_t *ptr; | |
143 | uint32_t length; | |
144 | }; | |
145 | ||
146 | static | |
147 | enum side_visitor_status test_inner_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx) | |
148 | { | |
149 | struct app_visitor_2d_inner_ctx *ctx = (struct app_visitor_2d_inner_ctx *) _ctx; | |
150 | uint32_t length = ctx->length, i; | |
151 | ||
152 | for (i = 0; i < length; i++) { | |
153 | const struct side_arg_vec elem = { | |
154 | .type = SIDE_TYPE_U32, | |
155 | .u = { | |
156 | .side_u32 = ctx->ptr[i], | |
157 | }, | |
158 | }; | |
db6ecef9 MD |
159 | if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK) |
160 | return SIDE_VISITOR_STATUS_ERROR; | |
cdd6e858 MD |
161 | } |
162 | return SIDE_VISITOR_STATUS_OK; | |
163 | } | |
164 | ||
165 | struct app_visitor_2d_outer_ctx { | |
166 | const uint32_t (*ptr)[2]; | |
167 | uint32_t length; | |
168 | }; | |
169 | ||
170 | static | |
171 | enum side_visitor_status test_outer_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx) | |
172 | { | |
173 | struct app_visitor_2d_outer_ctx *ctx = (struct app_visitor_2d_outer_ctx *) _ctx; | |
174 | uint32_t length = ctx->length, i; | |
175 | ||
176 | for (i = 0; i < length; i++) { | |
177 | struct app_visitor_2d_inner_ctx inner_ctx = { | |
178 | .ptr = ctx->ptr[i], | |
179 | .length = 2, | |
180 | }; | |
181 | const struct side_arg_vec elem = side_arg_vla_visitor(&inner_ctx); | |
db6ecef9 MD |
182 | if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK) |
183 | return SIDE_VISITOR_STATUS_ERROR; | |
cdd6e858 MD |
184 | } |
185 | return SIDE_VISITOR_STATUS_OK; | |
186 | } | |
187 | ||
188 | static uint32_t testarray2d[][2] = { | |
189 | { 1, 2 }, | |
190 | { 33, 44 }, | |
191 | { 55, 66 }, | |
192 | }; | |
193 | ||
194 | static side_define_event(my_provider_event_vla_visitor2d, "myprovider", "myvlavisit2d", SIDE_LOGLEVEL_DEBUG, | |
195 | side_field_list( | |
196 | side_field_vla_visitor("vlavisit2d", | |
197 | side_elem(side_type_vla_visitor_decl(side_elem_type(SIDE_TYPE_U32), test_inner_visitor)), test_outer_visitor), | |
91d140a6 | 198 | side_field("v", SIDE_TYPE_S64), |
cdd6e858 MD |
199 | ) |
200 | ); | |
201 | ||
202 | static | |
203 | void test_vla_visitor_2d(void) | |
204 | { | |
205 | my_provider_event_vla_visitor2d.enabled = 1; | |
206 | side_event_cond(&my_provider_event_vla_visitor2d) { | |
207 | struct app_visitor_2d_outer_ctx ctx = { | |
208 | .ptr = testarray2d, | |
209 | .length = SIDE_ARRAY_SIZE(testarray2d), | |
210 | }; | |
211 | side_event_call(&my_provider_event_vla_visitor2d, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42))); | |
212 | } | |
213 | } | |
214 | ||
ba845af5 MD |
215 | static int64_t array_fixint[] = { -444, 555, 123, 2897432587 }; |
216 | ||
217 | static side_define_event(my_provider_event_array_fixint, "myprovider", "myarrayfixint", SIDE_LOGLEVEL_DEBUG, | |
218 | side_field_list( | |
cdd6e858 | 219 | side_field_array("arrfixint", side_elem_type(SIDE_TYPE_S64), SIDE_ARRAY_SIZE(array_fixint)), |
91d140a6 | 220 | side_field("v", SIDE_TYPE_S64), |
ba845af5 MD |
221 | ) |
222 | ); | |
223 | ||
224 | static | |
225 | void test_array_fixint(void) | |
226 | { | |
227 | my_provider_event_array_fixint.enabled = 1; | |
1533629f MD |
228 | side_event(&my_provider_event_array_fixint, |
229 | side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42))); | |
230 | } | |
231 | ||
232 | static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 }; | |
233 | ||
234 | static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG, | |
235 | side_field_list( | |
cdd6e858 | 236 | side_field_vla("vlafixint", side_elem_type(SIDE_TYPE_S64)), |
91d140a6 | 237 | side_field("v", SIDE_TYPE_S64), |
1533629f MD |
238 | ) |
239 | ); | |
240 | ||
241 | static | |
242 | void test_vla_fixint(void) | |
243 | { | |
244 | my_provider_event_vla_fixint.enabled = 1; | |
245 | side_event(&my_provider_event_vla_fixint, | |
246 | side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42))); | |
ba845af5 MD |
247 | } |
248 | ||
a2e2357e MD |
249 | static side_define_event(my_provider_event_dynamic_basic, |
250 | "myprovider", "mydynamicbasic", SIDE_LOGLEVEL_DEBUG, | |
251 | side_field_list( | |
91d140a6 | 252 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
a2e2357e MD |
253 | ) |
254 | ); | |
255 | ||
256 | static | |
257 | void test_dynamic_basic_type(void) | |
258 | { | |
259 | my_provider_event_dynamic_basic.enabled = 1; | |
260 | side_event(&my_provider_event_dynamic_basic, | |
261 | side_arg_list(side_arg_dynamic(side_arg_dynamic_s16(-33)))); | |
262 | } | |
263 | ||
264 | static side_define_event(my_provider_event_dynamic_vla, | |
265 | "myprovider", "mydynamicvla", SIDE_LOGLEVEL_DEBUG, | |
266 | side_field_list( | |
91d140a6 | 267 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
a2e2357e MD |
268 | ) |
269 | ); | |
270 | ||
271 | static | |
272 | void test_dynamic_vla(void) | |
273 | { | |
948e3e72 MD |
274 | side_arg_dynamic_define_vec(myvla, |
275 | side_arg_list( | |
276 | side_arg_dynamic_u32(1), side_arg_dynamic_u32(2), side_arg_dynamic_u32(3)) | |
277 | ); | |
a2e2357e MD |
278 | my_provider_event_dynamic_vla.enabled = 1; |
279 | side_event(&my_provider_event_dynamic_vla, | |
280 | side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla)))); | |
281 | } | |
282 | ||
465e5e7e MD |
283 | static side_define_event(my_provider_event_dynamic_null, |
284 | "myprovider", "mydynamicnull", SIDE_LOGLEVEL_DEBUG, | |
285 | side_field_list( | |
91d140a6 | 286 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
465e5e7e MD |
287 | ) |
288 | ); | |
289 | ||
290 | static | |
291 | void test_dynamic_null(void) | |
292 | { | |
293 | my_provider_event_dynamic_null.enabled = 1; | |
294 | side_event(&my_provider_event_dynamic_null, | |
295 | side_arg_list(side_arg_dynamic(side_arg_dynamic_null()))); | |
296 | } | |
297 | ||
c208889e MD |
298 | static side_define_event(my_provider_event_dynamic_struct, |
299 | "myprovider", "mydynamicstruct", SIDE_LOGLEVEL_DEBUG, | |
465e5e7e | 300 | side_field_list( |
91d140a6 | 301 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
465e5e7e MD |
302 | ) |
303 | ); | |
304 | ||
305 | static | |
c208889e | 306 | void test_dynamic_struct(void) |
465e5e7e | 307 | { |
c208889e | 308 | side_arg_dynamic_define_struct(mystruct, |
465e5e7e MD |
309 | side_arg_list( |
310 | side_arg_dynamic_field("a", side_arg_dynamic_u32(43)), | |
311 | side_arg_dynamic_field("b", side_arg_dynamic_string("zzz")), | |
312 | side_arg_dynamic_field("c", side_arg_dynamic_null()) | |
313 | ) | |
314 | ); | |
315 | ||
c208889e MD |
316 | my_provider_event_dynamic_struct.enabled = 1; |
317 | side_event(&my_provider_event_dynamic_struct, | |
318 | side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct)))); | |
465e5e7e MD |
319 | } |
320 | ||
c208889e MD |
321 | static side_define_event(my_provider_event_dynamic_nested_struct, |
322 | "myprovider", "mydynamicnestedstruct", SIDE_LOGLEVEL_DEBUG, | |
3222d397 | 323 | side_field_list( |
91d140a6 | 324 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
3222d397 MD |
325 | ) |
326 | ); | |
327 | ||
328 | static | |
c208889e | 329 | void test_dynamic_nested_struct(void) |
3222d397 | 330 | { |
c208889e | 331 | side_arg_dynamic_define_struct(nested, |
3222d397 MD |
332 | side_arg_list( |
333 | side_arg_dynamic_field("a", side_arg_dynamic_u32(43)), | |
334 | side_arg_dynamic_field("b", side_arg_dynamic_u8(55)), | |
335 | ) | |
336 | ); | |
c208889e | 337 | side_arg_dynamic_define_struct(nested2, |
3222d397 MD |
338 | side_arg_list( |
339 | side_arg_dynamic_field("aa", side_arg_dynamic_u64(128)), | |
340 | side_arg_dynamic_field("bb", side_arg_dynamic_u16(1)), | |
341 | ) | |
342 | ); | |
c208889e | 343 | side_arg_dynamic_define_struct(mystruct, |
3222d397 | 344 | side_arg_list( |
c208889e MD |
345 | side_arg_dynamic_field("nested", side_arg_dynamic_struct(&nested)), |
346 | side_arg_dynamic_field("nested2", side_arg_dynamic_struct(&nested2)), | |
3222d397 MD |
347 | ) |
348 | ); | |
c208889e MD |
349 | my_provider_event_dynamic_nested_struct.enabled = 1; |
350 | side_event(&my_provider_event_dynamic_nested_struct, | |
351 | side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct)))); | |
3222d397 MD |
352 | } |
353 | ||
c208889e MD |
354 | static side_define_event(my_provider_event_dynamic_vla_struct, |
355 | "myprovider", "mydynamicvlastruct", SIDE_LOGLEVEL_DEBUG, | |
7ce1b78f | 356 | side_field_list( |
91d140a6 | 357 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
7ce1b78f MD |
358 | ) |
359 | ); | |
360 | ||
361 | static | |
c208889e | 362 | void test_dynamic_vla_struct(void) |
7ce1b78f | 363 | { |
c208889e | 364 | side_arg_dynamic_define_struct(nested, |
7ce1b78f MD |
365 | side_arg_list( |
366 | side_arg_dynamic_field("a", side_arg_dynamic_u32(43)), | |
367 | side_arg_dynamic_field("b", side_arg_dynamic_u8(55)), | |
368 | ) | |
369 | ); | |
370 | side_arg_dynamic_define_vec(myvla, | |
371 | side_arg_list( | |
c208889e MD |
372 | side_arg_dynamic_struct(&nested), |
373 | side_arg_dynamic_struct(&nested), | |
374 | side_arg_dynamic_struct(&nested), | |
375 | side_arg_dynamic_struct(&nested), | |
7ce1b78f MD |
376 | ) |
377 | ); | |
c208889e MD |
378 | my_provider_event_dynamic_vla_struct.enabled = 1; |
379 | side_event(&my_provider_event_dynamic_vla_struct, | |
7ce1b78f MD |
380 | side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla)))); |
381 | } | |
382 | ||
c208889e MD |
383 | static side_define_event(my_provider_event_dynamic_struct_vla, |
384 | "myprovider", "mydynamicstructvla", SIDE_LOGLEVEL_DEBUG, | |
980bfdc4 | 385 | side_field_list( |
91d140a6 | 386 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
980bfdc4 MD |
387 | ) |
388 | ); | |
389 | ||
390 | static | |
c208889e | 391 | void test_dynamic_struct_vla(void) |
980bfdc4 MD |
392 | { |
393 | side_arg_dynamic_define_vec(myvla, | |
948e3e72 MD |
394 | side_arg_list( |
395 | side_arg_dynamic_u32(1), side_arg_dynamic_u32(2), side_arg_dynamic_u32(3)) | |
396 | ); | |
980bfdc4 | 397 | side_arg_dynamic_define_vec(myvla2, |
948e3e72 MD |
398 | side_arg_list( |
399 | side_arg_dynamic_u32(4), side_arg_dynamic_u64(5), side_arg_dynamic_u32(6)) | |
400 | ); | |
c208889e | 401 | side_arg_dynamic_define_struct(mystruct, |
980bfdc4 MD |
402 | side_arg_list( |
403 | side_arg_dynamic_field("a", side_arg_dynamic_vla(&myvla)), | |
404 | side_arg_dynamic_field("b", side_arg_dynamic_vla(&myvla2)), | |
405 | ) | |
406 | ); | |
c208889e MD |
407 | my_provider_event_dynamic_struct_vla.enabled = 1; |
408 | side_event(&my_provider_event_dynamic_struct_vla, | |
409 | side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct)))); | |
980bfdc4 MD |
410 | } |
411 | ||
948e3e72 MD |
412 | static side_define_event(my_provider_event_dynamic_nested_vla, |
413 | "myprovider", "mydynamicnestedvla", SIDE_LOGLEVEL_DEBUG, | |
414 | side_field_list( | |
91d140a6 | 415 | side_field("dynamic", SIDE_TYPE_DYNAMIC), |
948e3e72 MD |
416 | ) |
417 | ); | |
418 | ||
419 | static | |
420 | void test_dynamic_nested_vla(void) | |
421 | { | |
422 | side_arg_dynamic_define_vec(nestedvla, | |
423 | side_arg_list( | |
424 | side_arg_dynamic_u32(1), side_arg_dynamic_u16(2), side_arg_dynamic_u32(3), | |
425 | ) | |
426 | ); | |
427 | side_arg_dynamic_define_vec(nestedvla2, | |
428 | side_arg_list( | |
429 | side_arg_dynamic_u8(4), side_arg_dynamic_u32(5), side_arg_dynamic_u32(6), | |
430 | ) | |
431 | ); | |
432 | side_arg_dynamic_define_vec(myvla, | |
433 | side_arg_list( | |
434 | side_arg_dynamic_vla(&nestedvla), | |
435 | side_arg_dynamic_vla(&nestedvla2), | |
436 | ) | |
437 | ); | |
438 | my_provider_event_dynamic_nested_vla.enabled = 1; | |
439 | side_event(&my_provider_event_dynamic_nested_vla, | |
440 | side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla)))); | |
441 | } | |
442 | ||
f611d0c3 MD |
443 | int main() |
444 | { | |
445 | test_fields(); | |
446 | test_struct(); | |
447 | test_array(); | |
448 | test_vla(); | |
449 | test_vla_visitor(); | |
cdd6e858 | 450 | test_vla_visitor_2d(); |
ba845af5 | 451 | test_array_fixint(); |
1533629f | 452 | test_vla_fixint(); |
a2e2357e MD |
453 | test_dynamic_basic_type(); |
454 | test_dynamic_vla(); | |
465e5e7e | 455 | test_dynamic_null(); |
c208889e MD |
456 | test_dynamic_struct(); |
457 | test_dynamic_nested_struct(); | |
458 | test_dynamic_vla_struct(); | |
459 | test_dynamic_struct_vla(); | |
948e3e72 | 460 | test_dynamic_nested_vla(); |
f611d0c3 MD |
461 | return 0; |
462 | } |