Refactor enum bitmap
[libside.git] / include / side / trace.h
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
6 #ifndef _SIDE_TRACE_H
7 #define _SIDE_TRACE_H
8
9 #include <stdint.h>
10 #include <inttypes.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <math.h>
14 #include <side/macros.h>
15
16 /* SIDE stands for "Static Instrumentation Dynamically Enabled" */
17
18 //TODO: as those structures will be ABI, we need to either consider them
19 //fixed forever, or think of a scheme that would allow their binary
20 //representation to be extended if need be.
21
22 struct side_arg_vec;
23 struct side_arg_vec_description;
24 struct side_arg_dynamic_vec;
25 struct side_arg_dynamic_vec_vla;
26 struct side_type_description;
27 struct side_event_field;
28 struct side_tracer_visitor_ctx;
29 struct side_tracer_dynamic_struct_visitor_ctx;
30 struct side_tracer_dynamic_vla_visitor_ctx;
31 struct side_event_description;
32 struct side_arg_dynamic_event_struct;
33
34 enum side_type {
35 /* Basic types */
36 SIDE_TYPE_BOOL,
37 SIDE_TYPE_U8,
38 SIDE_TYPE_U16,
39 SIDE_TYPE_U32,
40 SIDE_TYPE_U64,
41 SIDE_TYPE_S8,
42 SIDE_TYPE_S16,
43 SIDE_TYPE_S32,
44 SIDE_TYPE_S64,
45 SIDE_TYPE_BLOB,
46 SIDE_TYPE_FLOAT_BINARY16,
47 SIDE_TYPE_FLOAT_BINARY32,
48 SIDE_TYPE_FLOAT_BINARY64,
49 SIDE_TYPE_FLOAT_BINARY128,
50 SIDE_TYPE_STRING,
51
52 /* Compound types */
53 SIDE_TYPE_STRUCT,
54 SIDE_TYPE_ARRAY,
55 SIDE_TYPE_VLA,
56 SIDE_TYPE_VLA_VISITOR,
57
58 SIDE_TYPE_ARRAY_U8,
59 SIDE_TYPE_ARRAY_U16,
60 SIDE_TYPE_ARRAY_U32,
61 SIDE_TYPE_ARRAY_U64,
62 SIDE_TYPE_ARRAY_S8,
63 SIDE_TYPE_ARRAY_S16,
64 SIDE_TYPE_ARRAY_S32,
65 SIDE_TYPE_ARRAY_S64,
66 SIDE_TYPE_ARRAY_BLOB,
67
68 SIDE_TYPE_VLA_U8,
69 SIDE_TYPE_VLA_U16,
70 SIDE_TYPE_VLA_U32,
71 SIDE_TYPE_VLA_U64,
72 SIDE_TYPE_VLA_S8,
73 SIDE_TYPE_VLA_S16,
74 SIDE_TYPE_VLA_S32,
75 SIDE_TYPE_VLA_S64,
76 SIDE_TYPE_VLA_BLOB,
77
78 /* Enumeration types */
79 SIDE_TYPE_ENUM,
80 SIDE_TYPE_ENUM_BITMAP,
81
82 /* Dynamic type */
83 SIDE_TYPE_DYNAMIC,
84 };
85
86 enum side_dynamic_type {
87 /* Basic types */
88 SIDE_DYNAMIC_TYPE_NULL,
89 SIDE_DYNAMIC_TYPE_BOOL,
90 SIDE_DYNAMIC_TYPE_U8,
91 SIDE_DYNAMIC_TYPE_U16,
92 SIDE_DYNAMIC_TYPE_U32,
93 SIDE_DYNAMIC_TYPE_U64,
94 SIDE_DYNAMIC_TYPE_S8,
95 SIDE_DYNAMIC_TYPE_S16,
96 SIDE_DYNAMIC_TYPE_S32,
97 SIDE_DYNAMIC_TYPE_S64,
98 SIDE_DYNAMIC_TYPE_BLOB,
99 SIDE_DYNAMIC_TYPE_FLOAT_BINARY16,
100 SIDE_DYNAMIC_TYPE_FLOAT_BINARY32,
101 SIDE_DYNAMIC_TYPE_FLOAT_BINARY64,
102 SIDE_DYNAMIC_TYPE_FLOAT_BINARY128,
103 SIDE_DYNAMIC_TYPE_STRING,
104
105 /* Compound types */
106 SIDE_DYNAMIC_TYPE_STRUCT,
107 SIDE_DYNAMIC_TYPE_STRUCT_VISITOR,
108 SIDE_DYNAMIC_TYPE_VLA,
109 SIDE_DYNAMIC_TYPE_VLA_VISITOR,
110 };
111
112 enum side_attr_type {
113 SIDE_ATTR_TYPE_BOOL,
114 SIDE_ATTR_TYPE_U8,
115 SIDE_ATTR_TYPE_U16,
116 SIDE_ATTR_TYPE_U32,
117 SIDE_ATTR_TYPE_U64,
118 SIDE_ATTR_TYPE_S8,
119 SIDE_ATTR_TYPE_S16,
120 SIDE_ATTR_TYPE_S32,
121 SIDE_ATTR_TYPE_S64,
122 SIDE_ATTR_TYPE_FLOAT_BINARY16,
123 SIDE_ATTR_TYPE_FLOAT_BINARY32,
124 SIDE_ATTR_TYPE_FLOAT_BINARY64,
125 SIDE_ATTR_TYPE_FLOAT_BINARY128,
126 SIDE_ATTR_TYPE_STRING,
127 };
128
129 enum side_loglevel {
130 SIDE_LOGLEVEL_EMERG = 0,
131 SIDE_LOGLEVEL_ALERT = 1,
132 SIDE_LOGLEVEL_CRIT = 2,
133 SIDE_LOGLEVEL_ERR = 3,
134 SIDE_LOGLEVEL_WARNING = 4,
135 SIDE_LOGLEVEL_NOTICE = 5,
136 SIDE_LOGLEVEL_INFO = 6,
137 SIDE_LOGLEVEL_DEBUG = 7,
138 };
139
140 enum side_visitor_status {
141 SIDE_VISITOR_STATUS_OK = 0,
142 SIDE_VISITOR_STATUS_ERROR = -1,
143 };
144
145 typedef enum side_visitor_status (*side_visitor)(
146 const struct side_tracer_visitor_ctx *tracer_ctx,
147 void *app_ctx);
148 typedef enum side_visitor_status (*side_dynamic_struct_visitor)(
149 const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
150 void *app_ctx);
151 typedef enum side_visitor_status (*side_dynamic_vla_visitor)(
152 const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx,
153 void *app_ctx);
154
155 struct side_attr_value {
156 uint32_t type; /* enum side_attr_type */
157 union {
158 uint8_t side_bool;
159 uint8_t side_u8;
160 uint16_t side_u16;
161 uint32_t side_u32;
162 uint64_t side_u64;
163 int8_t side_s8;
164 int16_t side_s16;
165 int32_t side_s32;
166 int64_t side_s64;
167 #if __HAVE_FLOAT16
168 _Float16 side_float_binary16;
169 #endif
170 #if __HAVE_FLOAT32
171 _Float32 side_float_binary32;
172 #endif
173 #if __HAVE_FLOAT64
174 _Float64 side_float_binary64;
175 #endif
176 #if __HAVE_FLOAT128
177 _Float128 side_float_binary128;
178 #endif
179 const char *string;
180 } u;
181 };
182
183 /* User attributes. */
184 struct side_attr {
185 const char *key;
186 const struct side_attr_value value;
187 };
188
189 struct side_enum_mapping {
190 int64_t range_begin;
191 int64_t range_end;
192 const char *label;
193 };
194
195 struct side_enum_mappings {
196 const struct side_enum_mapping *mappings;
197 const struct side_attr *attr;
198 uint32_t nr_mappings;
199 uint32_t nr_attr;
200 };
201
202 struct side_enum_bitmap_mapping {
203 int64_t range_begin;
204 int64_t range_end;
205 const char *label;
206 };
207
208 struct side_enum_bitmap_mappings {
209 const struct side_enum_bitmap_mapping *mappings;
210 const struct side_attr *attr;
211 uint32_t nr_mappings;
212 uint32_t nr_attr;
213 };
214
215 struct side_type_struct {
216 uint32_t nr_fields;
217 uint32_t nr_attr;
218 const struct side_event_field *fields;
219 const struct side_attr *attr;
220 };
221
222 struct side_type_description {
223 uint32_t type; /* enum side_type */
224 union {
225 /* Basic types */
226 struct {
227 const struct side_attr *attr;
228 uint32_t nr_attr;
229 } side_basic;
230
231 /* Compound types */
232 struct {
233 const struct side_type_description *elem_type;
234 const struct side_attr *attr;
235 uint32_t length;
236 uint32_t nr_attr;
237 } side_array;
238 struct {
239 const struct side_type_description *elem_type;
240 const struct side_attr *attr;
241 uint32_t nr_attr;
242 } side_vla;
243 struct {
244 const struct side_type_description *elem_type;
245 side_visitor visitor;
246 const struct side_attr *attr;
247 uint32_t nr_attr;
248 } side_vla_visitor;
249 const struct side_type_struct *side_struct;
250
251 /* Enumeration types */
252 struct {
253 const struct side_type_description *elem_type;
254 const struct side_enum_mappings *mappings;
255 } side_enum;
256 struct {
257 const struct side_type_description *elem_type;
258 const struct side_enum_bitmap_mappings *mappings;
259 } side_enum_bitmap;
260 } u;
261 };
262
263 struct side_event_field {
264 const char *field_name;
265 struct side_type_description side_type;
266 };
267
268 enum side_event_flags {
269 SIDE_EVENT_FLAG_VARIADIC = (1 << 0),
270 };
271
272 struct side_callback {
273 union {
274 void (*call)(const struct side_event_description *desc,
275 const struct side_arg_vec_description *sav_desc,
276 void *priv);
277 void (*call_variadic)(const struct side_event_description *desc,
278 const struct side_arg_vec_description *sav_desc,
279 const struct side_arg_dynamic_event_struct *var_struct,
280 void *priv);
281 } u;
282 void *priv;
283 };
284
285 struct side_callbacks {
286 struct side_callback *cb;
287 uint32_t nr_cb;
288 };
289
290 struct side_event_description {
291 uint32_t version;
292 uint32_t *enabled;
293 uint32_t loglevel; /* enum side_loglevel */
294 uint32_t nr_fields;
295 uint32_t nr_attr;
296 uint32_t _unused;
297 uint64_t flags;
298 const char *provider_name;
299 const char *event_name;
300 const struct side_event_field *fields;
301 const struct side_attr *attr;
302 struct side_callbacks *callbacks;
303 };
304
305 struct side_arg_dynamic_vec {
306 uint32_t dynamic_type; /* enum side_dynamic_type */
307 union {
308 /* Basic types */
309 struct {
310 const struct side_attr *attr;
311 uint32_t nr_attr;
312 union {
313 uint8_t side_bool;
314 uint8_t side_u8;
315 uint16_t side_u16;
316 uint32_t side_u32;
317 uint64_t side_u64;
318 int8_t side_s8;
319 int16_t side_s16;
320 int32_t side_s32;
321 int64_t side_s64;
322 uint8_t side_blob;
323 #if __HAVE_FLOAT16
324 _Float16 side_float_binary16;
325 #endif
326 #if __HAVE_FLOAT32
327 _Float32 side_float_binary32;
328 #endif
329 #if __HAVE_FLOAT64
330 _Float64 side_float_binary64;
331 #endif
332 #if __HAVE_FLOAT128
333 _Float128 side_float_binary128;
334 #endif
335 const char *string;
336 } u;
337 } side_basic;
338
339 /* Compound types */
340 const struct side_arg_dynamic_event_struct *side_dynamic_struct;
341 struct {
342 void *app_ctx;
343 side_dynamic_struct_visitor visitor;
344 const struct side_attr *attr;
345 uint32_t nr_attr;
346 } side_dynamic_struct_visitor;
347 const struct side_arg_dynamic_vec_vla *side_dynamic_vla;
348 struct {
349 void *app_ctx;
350 side_dynamic_vla_visitor visitor;
351 const struct side_attr *attr;
352 uint32_t nr_attr;
353 } side_dynamic_vla_visitor;
354 } u;
355 };
356
357 struct side_arg_dynamic_vec_vla {
358 const struct side_arg_dynamic_vec *sav;
359 const struct side_attr *attr;
360 uint32_t len;
361 uint32_t nr_attr;
362 };
363
364 struct side_arg_dynamic_event_field {
365 const char *field_name;
366 const struct side_arg_dynamic_vec elem;
367 };
368
369 struct side_arg_dynamic_event_struct {
370 const struct side_arg_dynamic_event_field *fields;
371 const struct side_attr *attr;
372 uint32_t len;
373 uint32_t nr_attr;
374 };
375
376 struct side_arg_vec {
377 enum side_type type;
378 union {
379 /* Basic types */
380 uint8_t side_bool;
381 uint8_t side_u8;
382 uint16_t side_u16;
383 uint32_t side_u32;
384 uint64_t side_u64;
385 int8_t side_s8;
386 int16_t side_s16;
387 int32_t side_s32;
388 int64_t side_s64;
389 uint8_t side_blob;
390 #if __HAVE_FLOAT16
391 _Float16 side_float_binary16;
392 #endif
393 #if __HAVE_FLOAT32
394 _Float32 side_float_binary32;
395 #endif
396 #if __HAVE_FLOAT64
397 _Float64 side_float_binary64;
398 #endif
399 #if __HAVE_FLOAT128
400 _Float128 side_float_binary128;
401 #endif
402 const char *string;
403
404 /* Compound types */
405 const struct side_arg_vec_description *side_struct;
406 const struct side_arg_vec_description *side_array;
407 const struct side_arg_vec_description *side_vla;
408 void *side_vla_app_visitor_ctx;
409 void *side_array_fixint;
410 struct {
411 void *p;
412 uint32_t length;
413 } side_vla_fixint;
414
415 /* Dynamic type */
416 struct side_arg_dynamic_vec dynamic;
417 } u;
418 };
419
420 struct side_arg_vec_description {
421 const struct side_arg_vec *sav;
422 uint32_t len;
423 };
424
425 /* The visitor pattern is a double-dispatch visitor. */
426 struct side_tracer_visitor_ctx {
427 enum side_visitor_status (*write_elem)(
428 const struct side_tracer_visitor_ctx *tracer_ctx,
429 const struct side_arg_vec *elem);
430 void *priv; /* Private tracer context. */
431 };
432
433 struct side_tracer_dynamic_struct_visitor_ctx {
434 enum side_visitor_status (*write_field)(
435 const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
436 const struct side_arg_dynamic_event_field *dynamic_field);
437 void *priv; /* Private tracer context. */
438 };
439
440 struct side_tracer_dynamic_vla_visitor_ctx {
441 enum side_visitor_status (*write_elem)(
442 const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx,
443 const struct side_arg_dynamic_vec *elem);
444 void *priv; /* Private tracer context. */
445 };
446
447 /* Event and type attributes */
448
449 #define side_attr(_key, _value) \
450 { \
451 .key = _key, \
452 .value = SIDE_PARAM(_value), \
453 }
454
455 #define side_attr_list(...) \
456 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
457
458 #define side_attr_bool(_val) { .type = SIDE_ATTR_TYPE_BOOL, .u = { .side_bool = !!(_val) } }
459 #define side_attr_u8(_val) { .type = SIDE_ATTR_TYPE_U8, .u = { .side_u8 = (_val) } }
460 #define side_attr_u16(_val) { .type = SIDE_ATTR_TYPE_U16, .u = { .side_u16 = (_val) } }
461 #define side_attr_u32(_val) { .type = SIDE_ATTR_TYPE_U32, .u = { .side_u32 = (_val) } }
462 #define side_attr_u64(_val) { .type = SIDE_ATTR_TYPE_U64, .u = { .side_u64 = (_val) } }
463 #define side_attr_s8(_val) { .type = SIDE_ATTR_TYPE_S8, .u = { .side_s8 = (_val) } }
464 #define side_attr_s16(_val) { .type = SIDE_ATTR_TYPE_S16, .u = { .side_s16 = (_val) } }
465 #define side_attr_s32(_val) { .type = SIDE_ATTR_TYPE_S32, .u = { .side_s32 = (_val) } }
466 #define side_attr_s64(_val) { .type = SIDE_ATTR_TYPE_S64, .u = { .side_s64 = (_val) } }
467 #define side_attr_float_binary16(_val) { .type = SIDE_ATTR_TYPE_FLOAT_BINARY16, .u = { .side_float_binary16 = (_val) } }
468 #define side_attr_float_binary32(_val) { .type = SIDE_ATTR_TYPE_FLOAT_BINARY32, .u = { .side_float_binary32 = (_val) } }
469 #define side_attr_float_binary64(_val) { .type = SIDE_ATTR_TYPE_FLOAT_BINARY64, .u = { .side_float_binary64 = (_val) } }
470 #define side_attr_float_binary128(_val) { .type = SIDE_ATTR_TYPE_FLOAT_BINARY128, .u = { .side_float_binary128 = (_val) } }
471 #define side_attr_string(_val) { .type = SIDE_ATTR_TYPE_STRING, .u = { .string = (_val) } }
472
473 /* Static field definition */
474
475 #define _side_type_basic(_type, _attr) \
476 { \
477 .type = _type, \
478 .u = { \
479 .side_basic = { \
480 .attr = _attr, \
481 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
482 }, \
483 }, \
484 }
485
486 #define side_type_bool(_attr) _side_type_basic(SIDE_TYPE_BOOL, SIDE_PARAM(_attr))
487 #define side_type_u8(_attr) _side_type_basic(SIDE_TYPE_U8, SIDE_PARAM(_attr))
488 #define side_type_u16(_attr) _side_type_basic(SIDE_TYPE_U16, SIDE_PARAM(_attr))
489 #define side_type_u32(_attr) _side_type_basic(SIDE_TYPE_U32, SIDE_PARAM(_attr))
490 #define side_type_u64(_attr) _side_type_basic(SIDE_TYPE_U64, SIDE_PARAM(_attr))
491 #define side_type_s8(_attr) _side_type_basic(SIDE_TYPE_S8, SIDE_PARAM(_attr))
492 #define side_type_s16(_attr) _side_type_basic(SIDE_TYPE_S16, SIDE_PARAM(_attr))
493 #define side_type_s32(_attr) _side_type_basic(SIDE_TYPE_S32, SIDE_PARAM(_attr))
494 #define side_type_s64(_attr) _side_type_basic(SIDE_TYPE_S64, SIDE_PARAM(_attr))
495 #define side_type_blob(_attr) _side_type_basic(SIDE_TYPE_BLOB, SIDE_PARAM(_attr))
496 #define side_type_float_binary16(_attr) _side_type_basic(SIDE_TYPE_FLOAT_BINARY16, SIDE_PARAM(_attr))
497 #define side_type_float_binary32(_attr) _side_type_basic(SIDE_TYPE_FLOAT_BINARY32, SIDE_PARAM(_attr))
498 #define side_type_float_binary64(_attr) _side_type_basic(SIDE_TYPE_FLOAT_BINARY64, SIDE_PARAM(_attr))
499 #define side_type_float_binary128(_attr) _side_type_basic(SIDE_TYPE_FLOAT_BINARY128, SIDE_PARAM(_attr))
500 #define side_type_string(_attr) _side_type_basic(SIDE_TYPE_STRING, SIDE_PARAM(_attr))
501 #define side_type_dynamic(_attr) _side_type_basic(SIDE_TYPE_DYNAMIC, SIDE_PARAM(_attr))
502
503 #define _side_field(_name, _type) \
504 { \
505 .field_name = _name, \
506 .side_type = _type, \
507 }
508
509 #define side_field_bool(_name, _attr) _side_field(_name, side_type_bool(SIDE_PARAM(_attr)))
510 #define side_field_u8(_name, _attr) _side_field(_name, side_type_u8(SIDE_PARAM(_attr)))
511 #define side_field_u16(_name, _attr) _side_field(_name, side_type_u16(SIDE_PARAM(_attr)))
512 #define side_field_u32(_name, _attr) _side_field(_name, side_type_u32(SIDE_PARAM(_attr)))
513 #define side_field_u64(_name, _attr) _side_field(_name, side_type_u64(SIDE_PARAM(_attr)))
514 #define side_field_s8(_name, _attr) _side_field(_name, side_type_s8(SIDE_PARAM(_attr)))
515 #define side_field_s16(_name, _attr) _side_field(_name, side_type_s16(SIDE_PARAM(_attr)))
516 #define side_field_s32(_name, _attr) _side_field(_name, side_type_s32(SIDE_PARAM(_attr)))
517 #define side_field_s64(_name, _attr) _side_field(_name, side_type_s64(SIDE_PARAM(_attr)))
518 #define side_field_blob(_name, _attr) _side_field(_name, side_type_blob(SIDE_PARAM(_attr)))
519 #define side_field_float_binary16(_name, _attr) _side_field(_name, side_type_float_binary16(SIDE_PARAM(_attr)))
520 #define side_field_float_binary32(_name, _attr) _side_field(_name, side_type_float_binary32(SIDE_PARAM(_attr)))
521 #define side_field_float_binary64(_name, _attr) _side_field(_name, side_type_float_binary64(SIDE_PARAM(_attr)))
522 #define side_field_float_binary128(_name, _attr) _side_field(_name, side_type_float_binary128(SIDE_PARAM(_attr)))
523 #define side_field_string(_name, _attr) _side_field(_name, side_type_string(SIDE_PARAM(_attr)))
524 #define side_field_dynamic(_name, _attr) _side_field(_name, side_type_dynamic(SIDE_PARAM(_attr)))
525
526 #define side_type_enum(_elem_type, _mappings) \
527 { \
528 .type = SIDE_TYPE_ENUM, \
529 .u = { \
530 .side_enum = { \
531 .elem_type = _elem_type, \
532 .mappings = _mappings, \
533 }, \
534 }, \
535 }
536 #define side_field_enum(_name, _elem_type, _mappings) \
537 _side_field(_name, side_type_enum(SIDE_PARAM(_elem_type), SIDE_PARAM(_mappings)))
538
539 #define side_type_enum_bitmap(_elem_type, _mappings) \
540 { \
541 .type = SIDE_TYPE_ENUM_BITMAP, \
542 .u = { \
543 .side_enum_bitmap = { \
544 .elem_type = _elem_type, \
545 .mappings = _mappings, \
546 }, \
547 }, \
548 }
549 #define side_field_enum_bitmap(_name, _elem_type, _mappings) \
550 _side_field(_name, side_type_enum_bitmap(SIDE_PARAM(_elem_type), SIDE_PARAM(_mappings)))
551
552 #define side_type_struct(_struct) \
553 { \
554 .type = SIDE_TYPE_STRUCT, \
555 .u = { \
556 .side_struct = _struct, \
557 }, \
558 }
559 #define side_field_struct(_name, _struct) \
560 _side_field(_name, side_type_struct(SIDE_PARAM(_struct)))
561
562 #define _side_type_struct_define(_fields, _attr) \
563 { \
564 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
565 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
566 .fields = _fields, \
567 .attr = _attr, \
568 }
569
570 #define side_define_struct(_identifier, _fields, _attr) \
571 const struct side_type_struct _identifier = _side_type_struct_define(SIDE_PARAM(_fields), SIDE_PARAM(_attr))
572
573 #define side_struct_literal(_fields, _attr) \
574 SIDE_COMPOUND_LITERAL(const struct side_type_struct, \
575 _side_type_struct_define(SIDE_PARAM(_fields), SIDE_PARAM(_attr)))
576
577 #define side_type_array(_elem_type, _length, _attr) \
578 { \
579 .type = SIDE_TYPE_ARRAY, \
580 .u = { \
581 .side_array = { \
582 .elem_type = _elem_type, \
583 .attr = _attr, \
584 .length = _length, \
585 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
586 }, \
587 }, \
588 }
589 #define side_field_array(_name, _elem_type, _length, _attr) \
590 _side_field(_name, side_type_array(SIDE_PARAM(_elem_type), _length, SIDE_PARAM(_attr)))
591
592 #define side_type_vla(_elem_type, _attr) \
593 { \
594 .type = SIDE_TYPE_VLA, \
595 .u = { \
596 .side_vla = { \
597 .elem_type = _elem_type, \
598 .attr = _attr, \
599 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
600 }, \
601 }, \
602 }
603 #define side_field_vla(_name, _elem_type, _attr) \
604 _side_field(_name, side_type_vla(SIDE_PARAM(_elem_type), SIDE_PARAM(_attr)))
605
606 #define side_type_vla_visitor(_elem_type, _visitor, _attr) \
607 { \
608 .type = SIDE_TYPE_VLA_VISITOR, \
609 .u = { \
610 .side_vla_visitor = { \
611 .elem_type = SIDE_PARAM(_elem_type), \
612 .visitor = _visitor, \
613 .attr = _attr, \
614 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
615 }, \
616 }, \
617 }
618 #define side_field_vla_visitor(_name, _elem_type, _visitor, _attr) \
619 _side_field(_name, side_type_vla_visitor(SIDE_PARAM(_elem_type), _visitor, SIDE_PARAM(_attr)))
620
621 #define side_elem(...) \
622 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
623
624 #define side_field_list(...) \
625 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
626
627 /* Static field arguments */
628
629 #define side_arg_bool(_val) { .type = SIDE_TYPE_BOOL, .u = { .side_bool = !!(_val) } }
630 #define side_arg_u8(_val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (_val) } }
631 #define side_arg_u16(_val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (_val) } }
632 #define side_arg_u32(_val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (_val) } }
633 #define side_arg_u64(_val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (_val) } }
634 #define side_arg_s8(_val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (_val) } }
635 #define side_arg_s16(_val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (_val) } }
636 #define side_arg_s32(_val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (_val) } }
637 #define side_arg_s64(_val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (_val) } }
638 #define side_arg_blob(_val) { .type = SIDE_TYPE_BLOB, .u = { .side_blob = (_val) } }
639 #define side_arg_enum_bitmap8(_val) { .type = SIDE_TYPE_ENUM_BITMAP8, .u = { .side_u8 = (_val) } }
640 #define side_arg_enum_bitmap16(_val) { .type = SIDE_TYPE_ENUM_BITMAP16, .u = { .side_u16 = (_val) } }
641 #define side_arg_enum_bitmap32(_val) { .type = SIDE_TYPE_ENUM_BITMAP32, .u = { .side_u32 = (_val) } }
642 #define side_arg_enum_bitmap64(_val) { .type = SIDE_TYPE_ENUM_BITMAP64, .u = { .side_u64 = (_val) } }
643 #define side_arg_enum_bitmap_array(_side_type) { .type = SIDE_TYPE_ENUM_BITMAP_ARRAY, .u = { .side_array = (_side_type) } }
644 #define side_arg_enum_bitmap_vla(_side_type) { .type = SIDE_TYPE_ENUM_BITMAP_VLA, .u = { .side_vla = (_side_type) } }
645 #define side_arg_float_binary16(_val) { .type = SIDE_TYPE_FLOAT_BINARY16, .u = { .side_float_binary16 = (_val) } }
646 #define side_arg_float_binary32(_val) { .type = SIDE_TYPE_FLOAT_BINARY32, .u = { .side_float_binary32 = (_val) } }
647 #define side_arg_float_binary64(_val) { .type = SIDE_TYPE_FLOAT_BINARY64, .u = { .side_float_binary64 = (_val) } }
648 #define side_arg_float_binary128(_val) { .type = SIDE_TYPE_FLOAT_BINARY128, .u = { .side_float_binary128 = (_val) } }
649
650 #define side_arg_string(_val) { .type = SIDE_TYPE_STRING, .u = { .string = (_val) } }
651 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
652 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
653 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
654 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
655
656 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
657 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
658 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
659 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
660 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
661 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
662 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
663 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
664 #define side_arg_array_blob(_ptr) { .type = SIDE_TYPE_ARRAY_BLOB, .u = { .side_array_fixint = (_ptr) } }
665
666 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
667 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
668 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
669 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
670 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
671 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
672 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
673 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
674 #define side_arg_vla_blob(_ptr, _length) { .type = SIDE_TYPE_VLA_BLOB, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
675
676 #define side_arg_dynamic(_dynamic_arg_type) \
677 { \
678 .type = SIDE_TYPE_DYNAMIC, \
679 .u = { \
680 .dynamic = _dynamic_arg_type, \
681 }, \
682 }
683
684 /* Dynamic field arguments */
685
686 #define side_arg_dynamic_null(_attr) \
687 { \
688 .dynamic_type = SIDE_DYNAMIC_TYPE_NULL, \
689 .u = { \
690 .side_basic = { \
691 .attr = _attr, \
692 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
693 }, \
694 }, \
695 }
696
697 #define side_arg_dynamic_bool(_val, _attr) \
698 { \
699 .dynamic_type = SIDE_DYNAMIC_TYPE_BOOL, \
700 .u = { \
701 .side_basic = { \
702 .attr = _attr, \
703 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
704 .u = { \
705 .side_bool = !!(_val), \
706 }, \
707 }, \
708 }, \
709 }
710
711 #define side_arg_dynamic_u8(_val, _attr) \
712 { \
713 .dynamic_type = SIDE_DYNAMIC_TYPE_U8, \
714 .u = { \
715 .side_basic = { \
716 .attr = _attr, \
717 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
718 .u = { \
719 .side_u8 = (_val), \
720 }, \
721 }, \
722 }, \
723 }
724 #define side_arg_dynamic_u16(_val, _attr) \
725 { \
726 .dynamic_type = SIDE_DYNAMIC_TYPE_U16, \
727 .u = { \
728 .side_basic = { \
729 .attr = _attr, \
730 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
731 .u = { \
732 .side_u16 = (_val), \
733 }, \
734 }, \
735 }, \
736 }
737 #define side_arg_dynamic_u32(_val, _attr) \
738 { \
739 .dynamic_type = SIDE_DYNAMIC_TYPE_U32, \
740 .u = { \
741 .side_basic = { \
742 .attr = _attr, \
743 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
744 .u = { \
745 .side_u32 = (_val), \
746 }, \
747 }, \
748 }, \
749 }
750 #define side_arg_dynamic_u64(_val, _attr) \
751 { \
752 .dynamic_type = SIDE_DYNAMIC_TYPE_U64, \
753 .u = { \
754 .side_basic = { \
755 .attr = _attr, \
756 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
757 .u = { \
758 .side_u64 = (_val), \
759 }, \
760 }, \
761 }, \
762 }
763
764 #define side_arg_dynamic_s8(_val, _attr) \
765 { \
766 .dynamic_type = SIDE_DYNAMIC_TYPE_S8, \
767 .u = { \
768 .side_basic = { \
769 .attr = _attr, \
770 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
771 .u = { \
772 .side_s8 = (_val), \
773 }, \
774 }, \
775 }, \
776 }
777 #define side_arg_dynamic_s16(_val, _attr) \
778 { \
779 .dynamic_type = SIDE_DYNAMIC_TYPE_S16, \
780 .u = { \
781 .side_basic = { \
782 .attr = _attr, \
783 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
784 .u = { \
785 .side_s16 = (_val), \
786 }, \
787 }, \
788 }, \
789 }
790 #define side_arg_dynamic_s32(_val, _attr) \
791 { \
792 .dynamic_type = SIDE_DYNAMIC_TYPE_S32, \
793 .u = { \
794 .side_basic = { \
795 .attr = _attr, \
796 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
797 .u = { \
798 .side_s32 = (_val), \
799 }, \
800 }, \
801 }, \
802 }
803 #define side_arg_dynamic_s64(_val, _attr) \
804 { \
805 .dynamic_type = SIDE_DYNAMIC_TYPE_S64, \
806 .u = { \
807 .side_basic = { \
808 .attr = _attr, \
809 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
810 .u = { \
811 .side_s64 = (_val), \
812 }, \
813 }, \
814 }, \
815 }
816 #define side_arg_dynamic_blob(_val, _attr) \
817 { \
818 .dynamic_type = SIDE_DYNAMIC_TYPE_BLOB, \
819 .u = { \
820 .side_basic = { \
821 .attr = _attr, \
822 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
823 .u = { \
824 .side_blob = (_val), \
825 }, \
826 }, \
827 }, \
828 }
829
830 #define side_arg_dynamic_float_binary16(_val, _attr) \
831 { \
832 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY16, \
833 .u = { \
834 .side_basic = { \
835 .attr = _attr, \
836 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
837 .u = { \
838 .side_float_binary16 = (_val), \
839 }, \
840 }, \
841 }, \
842 }
843 #define side_arg_dynamic_float_binary32(_val, _attr) \
844 { \
845 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY32, \
846 .u = { \
847 .side_basic = { \
848 .attr = _attr, \
849 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
850 .u = { \
851 .side_float_binary32 = (_val), \
852 }, \
853 }, \
854 }, \
855 }
856 #define side_arg_dynamic_float_binary64(_val, _attr) \
857 { \
858 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY64, \
859 .u = { \
860 .side_basic = { \
861 .attr = _attr, \
862 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
863 .u = { \
864 .side_float_binary64 = (_val), \
865 }, \
866 }, \
867 }, \
868 }
869 #define side_arg_dynamic_float_binary128(_val, _attr) \
870 { \
871 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY128, \
872 .u = { \
873 .side_basic = { \
874 .attr = _attr, \
875 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
876 .u = { \
877 .side_float_binary128 = (_val), \
878 }, \
879 }, \
880 }, \
881 }
882
883 #define side_arg_dynamic_string(_val, _attr) \
884 { \
885 .dynamic_type = SIDE_DYNAMIC_TYPE_STRING, \
886 .u = { \
887 .side_basic = { \
888 .attr = _attr, \
889 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
890 .u = { \
891 .string = (_val), \
892 }, \
893 }, \
894 }, \
895 }
896
897 #define side_arg_dynamic_vla(_vla) \
898 { \
899 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA, \
900 .u = { \
901 .side_dynamic_vla = (_vla), \
902 }, \
903 }
904
905 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx, _attr) \
906 { \
907 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
908 .u = { \
909 .side_dynamic_vla_visitor = { \
910 .app_ctx = _ctx, \
911 .visitor = _dynamic_vla_visitor, \
912 .attr = _attr, \
913 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
914 }, \
915 }, \
916 }
917
918 #define side_arg_dynamic_struct(_struct) \
919 { \
920 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT, \
921 .u = { \
922 .side_dynamic_struct = (_struct), \
923 }, \
924 }
925
926 #define side_arg_dynamic_struct_visitor(_dynamic_struct_visitor, _ctx, _attr) \
927 { \
928 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
929 .u = { \
930 .side_dynamic_struct_visitor = { \
931 .app_ctx = _ctx, \
932 .visitor = _dynamic_struct_visitor, \
933 .attr = _attr, \
934 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
935 }, \
936 }, \
937 }
938
939 #define side_arg_dynamic_define_vec(_identifier, _sav, _attr) \
940 const struct side_arg_dynamic_vec _identifier##_vec[] = { _sav }; \
941 const struct side_arg_dynamic_vec_vla _identifier = { \
942 .sav = _identifier##_vec, \
943 .attr = _attr, \
944 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
945 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
946 }
947
948 #define side_arg_dynamic_define_struct(_identifier, _struct_fields, _attr) \
949 const struct side_arg_dynamic_event_field _identifier##_fields[] = { _struct_fields }; \
950 const struct side_arg_dynamic_event_struct _identifier = { \
951 .fields = _identifier##_fields, \
952 .attr = _attr, \
953 .len = SIDE_ARRAY_SIZE(_identifier##_fields), \
954 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
955 }
956
957 #define side_arg_define_vec(_identifier, _sav) \
958 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
959 const struct side_arg_vec_description _identifier = { \
960 .sav = _identifier##_vec, \
961 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
962 }
963
964 #define side_arg_dynamic_field(_name, _elem) \
965 { \
966 .field_name = _name, \
967 .elem = _elem, \
968 }
969
970 #define side_arg_list(...) __VA_ARGS__
971
972 #define side_define_enum(_identifier, _mappings, _attr) \
973 const struct side_enum_mappings _identifier = { \
974 .mappings = _mappings, \
975 .attr = _attr, \
976 .nr_mappings = SIDE_ARRAY_SIZE(SIDE_PARAM(_mappings)), \
977 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
978 }
979
980 #define side_enum_mapping_list(...) \
981 SIDE_COMPOUND_LITERAL(const struct side_enum_mapping, __VA_ARGS__)
982
983 #define side_enum_mapping_range(_label, _begin, _end) \
984 { \
985 .range_begin = _begin, \
986 .range_end = _end, \
987 .label = _label, \
988 }
989
990 #define side_enum_mapping_value(_label, _value) \
991 { \
992 .range_begin = _value, \
993 .range_end = _value, \
994 .label = _label, \
995 }
996
997 #define side_define_enum_bitmap(_identifier, _mappings, _attr) \
998 const struct side_enum_bitmap_mappings _identifier = { \
999 .mappings = _mappings, \
1000 .attr = _attr, \
1001 .nr_mappings = SIDE_ARRAY_SIZE(SIDE_PARAM(_mappings)), \
1002 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
1003 }
1004
1005 #define side_enum_bitmap_mapping_list(...) \
1006 SIDE_COMPOUND_LITERAL(const struct side_enum_bitmap_mapping, __VA_ARGS__)
1007
1008 #define side_enum_bitmap_mapping_range(_label, _begin, _end) \
1009 { \
1010 .range_begin = _begin, \
1011 .range_end = _end, \
1012 .label = _label, \
1013 }
1014
1015 #define side_enum_bitmap_mapping_value(_label, _value) \
1016 { \
1017 .range_begin = _value, \
1018 .range_end = _value, \
1019 .label = _label, \
1020 }
1021
1022 #define side_event_cond(_desc) if (side_unlikely(_desc##_enabled))
1023
1024 #define side_event_call(_desc, _sav) \
1025 { \
1026 const struct side_arg_vec side_sav[] = { _sav }; \
1027 const struct side_arg_vec_description sav_desc = { \
1028 .sav = side_sav, \
1029 .len = SIDE_ARRAY_SIZE(side_sav), \
1030 }; \
1031 side_call(&(_desc), &sav_desc); \
1032 }
1033
1034 #define side_event(_desc, _sav) \
1035 side_event_cond(_desc) \
1036 side_event_call(_desc, SIDE_PARAM(_sav))
1037
1038 #define side_event_call_variadic(_desc, _sav, _var_fields, _attr) \
1039 { \
1040 const struct side_arg_vec side_sav[] = { _sav }; \
1041 const struct side_arg_vec_description sav_desc = { \
1042 .sav = side_sav, \
1043 .len = SIDE_ARRAY_SIZE(side_sav), \
1044 }; \
1045 const struct side_arg_dynamic_event_field side_fields[] = { _var_fields }; \
1046 const struct side_arg_dynamic_event_struct var_struct = { \
1047 .fields = side_fields, \
1048 .attr = _attr, \
1049 .len = SIDE_ARRAY_SIZE(side_fields), \
1050 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
1051 }; \
1052 side_call_variadic(&(_desc), &sav_desc, &var_struct); \
1053 }
1054
1055 #define side_event_variadic(_desc, _sav, _var, _attr) \
1056 side_event_cond(_desc) \
1057 side_event_call_variadic(_desc, SIDE_PARAM(_sav), SIDE_PARAM(_var), SIDE_PARAM(_attr))
1058
1059 #define _side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr, _flags) \
1060 uint32_t _identifier##_enabled __attribute__((section("side_event_enable"))); \
1061 struct side_callbacks _identifier##_callbacks __attribute__((section("side_event_callbacks"))); \
1062 const struct side_event_description _identifier = { \
1063 .version = 0, \
1064 .enabled = &(_identifier##_enabled), \
1065 .loglevel = _loglevel, \
1066 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
1067 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
1068 .flags = (_flags), \
1069 .provider_name = _provider, \
1070 .event_name = _event, \
1071 .fields = _fields, \
1072 .attr = _attr, \
1073 .callbacks = &(_identifier##_callbacks), \
1074 }; \
1075 const struct side_event_description *_identifier##_ptr \
1076 __attribute__((section("side_event_description"), used)) = &(_identifier);
1077
1078 #define side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr) \
1079 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
1080 SIDE_PARAM(_attr), 0)
1081
1082 #define side_define_event_variadic(_identifier, _provider, _event, _loglevel, _fields, _attr) \
1083 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
1084 SIDE_PARAM(_attr), SIDE_EVENT_FLAG_VARIADIC)
1085
1086 #define side_declare_event(_identifier) \
1087 struct side_event_description _identifier
1088
1089 void side_call(const struct side_event_description *desc,
1090 const struct side_arg_vec_description *sav_desc);
1091 void side_call_variadic(const struct side_event_description *desc,
1092 const struct side_arg_vec_description *sav_desc,
1093 const struct side_arg_dynamic_event_struct *var_struct);
1094
1095 #endif /* _SIDE_TRACE_H */
This page took 0.049213 seconds and 5 git commands to generate.