1 // SPDX-License-Identifier: MIT
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 #include <side/macros.h>
16 /* SIDE stands for "Static Instrumentation Dynamically Enabled" */
19 struct side_arg_vec_description
;
20 struct side_arg_dynamic_vec
;
21 struct side_arg_dynamic_vec_vla
;
22 struct side_type_description
;
23 struct side_event_field
;
24 struct side_tracer_visitor_ctx
;
25 struct side_tracer_dynamic_struct_visitor_ctx
;
26 struct side_tracer_dynamic_vla_visitor_ctx
;
40 SIDE_TYPE_FLOAT_BINARY16
,
41 SIDE_TYPE_FLOAT_BINARY32
,
42 SIDE_TYPE_FLOAT_BINARY64
,
43 SIDE_TYPE_FLOAT_BINARY128
,
50 SIDE_TYPE_VLA_VISITOR
,
73 enum side_dynamic_type
{
74 SIDE_DYNAMIC_TYPE_NULL
,
76 SIDE_DYNAMIC_TYPE_BOOL
,
79 SIDE_DYNAMIC_TYPE_U16
,
80 SIDE_DYNAMIC_TYPE_U32
,
81 SIDE_DYNAMIC_TYPE_U64
,
83 SIDE_DYNAMIC_TYPE_S16
,
84 SIDE_DYNAMIC_TYPE_S32
,
85 SIDE_DYNAMIC_TYPE_S64
,
87 SIDE_DYNAMIC_TYPE_FLOAT_BINARY16
,
88 SIDE_DYNAMIC_TYPE_FLOAT_BINARY32
,
89 SIDE_DYNAMIC_TYPE_FLOAT_BINARY64
,
90 SIDE_DYNAMIC_TYPE_FLOAT_BINARY128
,
92 SIDE_DYNAMIC_TYPE_STRING
,
94 SIDE_DYNAMIC_TYPE_STRUCT
,
95 SIDE_DYNAMIC_TYPE_STRUCT_VISITOR
,
97 SIDE_DYNAMIC_TYPE_VLA
,
98 SIDE_DYNAMIC_TYPE_VLA_VISITOR
,
102 SIDE_LOGLEVEL_EMERG
= 0,
103 SIDE_LOGLEVEL_ALERT
= 1,
104 SIDE_LOGLEVEL_CRIT
= 2,
105 SIDE_LOGLEVEL_ERR
= 3,
106 SIDE_LOGLEVEL_WARNING
= 4,
107 SIDE_LOGLEVEL_NOTICE
= 5,
108 SIDE_LOGLEVEL_INFO
= 6,
109 SIDE_LOGLEVEL_DEBUG
= 7,
112 enum side_visitor_status
{
113 SIDE_VISITOR_STATUS_OK
= 0,
114 SIDE_VISITOR_STATUS_ERROR
= -1,
117 typedef enum side_visitor_status (*side_visitor
)(
118 const struct side_tracer_visitor_ctx
*tracer_ctx
,
120 typedef enum side_visitor_status (*side_dynamic_struct_visitor
)(
121 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
123 typedef enum side_visitor_status (*side_dynamic_vla_visitor
)(
124 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
127 /* User attributes. */
133 struct side_type_description
{
134 uint32_t type
; /* enum side_type */
136 const struct side_attr
*attr
;
140 const struct side_event_field
*fields
;
144 const struct side_type_description
*elem_type
;
147 const struct side_type_description
*elem_type
;
150 const struct side_type_description
*elem_type
;
151 side_visitor visitor
;
156 struct side_event_field
{
157 const char *field_name
;
158 struct side_type_description side_type
;
161 enum side_event_flags
{
162 SIDE_EVENT_FLAG_VARIADIC
= (1 << 0),
165 struct side_event_description
{
168 uint32_t loglevel
; /* enum side_loglevel */
173 const char *provider_name
;
174 const char *event_name
;
175 const struct side_event_field
*fields
;
176 const struct side_attr
*attr
;
179 struct side_arg_dynamic_vec_vla
{
180 const struct side_arg_dynamic_vec
*sav
;
184 struct side_arg_dynamic_vec
{
185 uint32_t dynamic_type
; /* enum side_dynamic_type */
187 const struct side_attr
*attr
;
201 _Float16 side_float_binary16
;
204 _Float32 side_float_binary32
;
207 _Float64 side_float_binary64
;
210 _Float128 side_float_binary128
;
215 const struct side_arg_dynamic_event_struct
*side_dynamic_struct
;
218 side_dynamic_struct_visitor visitor
;
219 } side_dynamic_struct_visitor
;
221 const struct side_arg_dynamic_vec_vla
*side_dynamic_vla
;
224 side_dynamic_vla_visitor visitor
;
225 } side_dynamic_vla_visitor
;
229 struct side_arg_dynamic_event_field
{
230 const char *field_name
;
231 const struct side_arg_dynamic_vec elem
;
234 struct side_arg_dynamic_event_struct
{
235 const struct side_arg_dynamic_event_field
*fields
;
239 struct side_arg_vec
{
254 _Float16 side_float_binary16
;
257 _Float32 side_float_binary32
;
260 _Float64 side_float_binary64
;
263 _Float128 side_float_binary128
;
267 const struct side_arg_vec_description
*side_struct
;
268 const struct side_arg_vec_description
*side_array
;
269 const struct side_arg_vec_description
*side_vla
;
270 void *side_vla_app_visitor_ctx
;
272 void *side_array_fixint
;
278 struct side_arg_dynamic_vec dynamic
;
282 struct side_arg_vec_description
{
283 const struct side_arg_vec
*sav
;
287 /* The visitor pattern is a double-dispatch visitor. */
288 struct side_tracer_visitor_ctx
{
289 enum side_visitor_status (*write_elem
)(
290 const struct side_tracer_visitor_ctx
*tracer_ctx
,
291 const struct side_arg_vec
*elem
);
292 void *priv
; /* Private tracer context. */
295 struct side_tracer_dynamic_struct_visitor_ctx
{
296 enum side_visitor_status (*write_field
)(
297 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
298 const struct side_arg_dynamic_event_field
*dynamic_field
);
299 void *priv
; /* Private tracer context. */
302 struct side_tracer_dynamic_vla_visitor_ctx
{
303 enum side_visitor_status (*write_elem
)(
304 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
305 const struct side_arg_dynamic_vec
*elem
);
306 void *priv
; /* Private tracer context. */
309 #define side_attr(_key, _value) \
315 #define side_attr_list(...) \
316 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
318 #define side_type_decl(_type, _attr) \
321 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
325 #define side_field(_name, _type, _attr) \
327 .field_name = _name, \
328 .side_type = side_type_decl(_type, SIDE_PARAM(_attr)), \
331 #define side_type_struct_decl(_fields, _attr) \
333 .type = SIDE_TYPE_STRUCT, \
334 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
338 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
343 #define side_field_struct(_name, _fields, _attr) \
345 .field_name = _name, \
346 .side_type = side_type_struct_decl(SIDE_PARAM(_fields), SIDE_PARAM(_attr)), \
349 #define side_type_array_decl(_elem_type, _length, _attr) \
351 .type = SIDE_TYPE_ARRAY, \
352 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
357 .elem_type = _elem_type, \
361 #define side_field_array(_name, _elem_type, _length, _attr) \
363 .field_name = _name, \
364 .side_type = side_type_array_decl(SIDE_PARAM(_elem_type), _length, SIDE_PARAM(_attr)), \
367 #define side_type_vla_decl(_elem_type, _attr) \
369 .type = SIDE_TYPE_VLA, \
370 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
374 .elem_type = _elem_type, \
378 #define side_field_vla(_name, _elem_type, _attr) \
380 .field_name = _name, \
381 .side_type = side_type_vla_decl(SIDE_PARAM(_elem_type), SIDE_PARAM(_attr)), \
384 #define side_type_vla_visitor_decl(_elem_type, _visitor, _attr) \
386 .type = SIDE_TYPE_VLA_VISITOR, \
387 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
390 .side_vla_visitor = { \
391 .elem_type = SIDE_PARAM(_elem_type), \
392 .visitor = _visitor, \
396 #define side_field_vla_visitor(_name, _elem_type, _visitor, _attr) \
398 .field_name = _name, \
399 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor, SIDE_PARAM(_attr)), \
402 #define side_elem(...) \
403 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
405 #define side_elem_type(_type, _attr) \
406 side_elem(side_type_decl(_type, SIDE_PARAM(_attr)))
408 #define side_field_list(...) \
409 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
411 #define side_arg_bool(val) { .type = SIDE_TYPE_BOOL, .u = { .side_bool = !!(val) } }
412 #define side_arg_u8(val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (val) } }
413 #define side_arg_u16(val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (val) } }
414 #define side_arg_u32(val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (val) } }
415 #define side_arg_u64(val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (val) } }
416 #define side_arg_s8(val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (val) } }
417 #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } }
418 #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } }
419 #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } }
420 #define side_arg_float_binary16(val) { .type = SIDE_TYPE_FLOAT_BINARY16, .u = { .side_float_binary16 = (val) } }
421 #define side_arg_float_binary32(val) { .type = SIDE_TYPE_FLOAT_BINARY32, .u = { .side_float_binary32 = (val) } }
422 #define side_arg_float_binary64(val) { .type = SIDE_TYPE_FLOAT_BINARY64, .u = { .side_float_binary64 = (val) } }
423 #define side_arg_float_binary128(val) { .type = SIDE_TYPE_FLOAT_BINARY128, .u = { .side_float_binary128 = (val) } }
425 #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } }
426 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
427 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
428 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
429 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
431 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
432 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
433 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
434 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
435 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
436 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
437 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
438 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
440 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
441 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
442 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
443 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
444 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
445 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
446 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
447 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
449 #define side_arg_dynamic(dynamic_arg_type) \
451 .type = SIDE_TYPE_DYNAMIC, \
453 .dynamic = dynamic_arg_type, \
457 #define side_arg_dynamic_null(_attr) \
459 .dynamic_type = SIDE_DYNAMIC_TYPE_NULL, \
460 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
464 #define side_arg_dynamic_bool(_val, _attr) \
466 .dynamic_type = SIDE_DYNAMIC_TYPE_BOOL, \
467 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
470 .side_bool = !!(_val), \
474 #define side_arg_dynamic_u8(_val, _attr) \
476 .dynamic_type = SIDE_DYNAMIC_TYPE_U8, \
477 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
483 #define side_arg_dynamic_u16(_val, _attr) \
485 .dynamic_type = SIDE_DYNAMIC_TYPE_U16, \
486 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
489 .side_u16 = (_val), \
492 #define side_arg_dynamic_u32(_val, _attr) \
494 .dynamic_type = SIDE_DYNAMIC_TYPE_U32, \
495 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
498 .side_u32 = (_val), \
501 #define side_arg_dynamic_u64(_val, _attr) \
503 .dynamic_type = SIDE_DYNAMIC_TYPE_U64, \
504 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
507 .side_u64 = (_val), \
511 #define side_arg_dynamic_s8(_val, _attr) \
513 .dynamic_type = SIDE_DYNAMIC_TYPE_S8, \
514 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
520 #define side_arg_dynamic_s16(_val, _attr) \
522 .dynamic_type = SIDE_DYNAMIC_TYPE_S16, \
523 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
526 .side_s16 = (_val), \
529 #define side_arg_dynamic_s32(_val, _attr) \
531 .dynamic_type = SIDE_DYNAMIC_TYPE_S32, \
532 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
535 .side_s32 = (_val), \
538 #define side_arg_dynamic_s64(_val, _attr) \
540 .dynamic_type = SIDE_DYNAMIC_TYPE_S64, \
541 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
544 .side_s64 = (_val), \
548 #define side_arg_dynamic_float_binary16(_val, _attr) \
550 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY16, \
551 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
554 .side_float_binary16 = (_val), \
557 #define side_arg_dynamic_float_binary32(_val, _attr) \
559 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY32, \
560 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
563 .side_float_binary32 = (_val), \
566 #define side_arg_dynamic_float_binary64(_val, _attr) \
568 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY64, \
569 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
572 .side_float_binary64 = (_val), \
575 #define side_arg_dynamic_float_binary128(_val, _attr) \
577 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY128, \
578 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
581 .side_float_binary128 = (_val), \
585 #define side_arg_dynamic_string(_val, _attr) \
587 .dynamic_type = SIDE_DYNAMIC_TYPE_STRING, \
588 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
595 #define side_arg_dynamic_vla(_vla, _attr) \
597 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA, \
598 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
601 .side_dynamic_vla = (_vla), \
605 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx, _attr) \
607 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
608 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
611 .side_dynamic_vla_visitor = { \
613 .visitor = _dynamic_vla_visitor, \
618 #define side_arg_dynamic_struct(_struct, _attr) \
620 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT, \
621 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
624 .side_dynamic_struct = (_struct), \
628 #define side_arg_dynamic_struct_visitor(_dynamic_struct_visitor, _ctx, _attr) \
630 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
631 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
634 .side_dynamic_struct_visitor = { \
636 .visitor = _dynamic_struct_visitor, \
641 #define side_arg_dynamic_define_vec(_identifier, _sav) \
642 const struct side_arg_dynamic_vec _identifier##_vec[] = { _sav }; \
643 const struct side_arg_dynamic_vec_vla _identifier = { \
644 .sav = _identifier##_vec, \
645 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
648 #define side_arg_dynamic_define_struct(_identifier, _struct_fields) \
649 const struct side_arg_dynamic_event_field _identifier##_fields[] = { _struct_fields }; \
650 const struct side_arg_dynamic_event_struct _identifier = { \
651 .fields = _identifier##_fields, \
652 .len = SIDE_ARRAY_SIZE(_identifier##_fields), \
655 #define side_arg_define_vec(_identifier, _sav) \
656 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
657 const struct side_arg_vec_description _identifier = { \
658 .sav = _identifier##_vec, \
659 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
662 #define side_arg_dynamic_field(_name, _elem) \
664 .field_name = _name, \
668 #define side_arg_list(...) __VA_ARGS__
670 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
672 #define side_event_call(desc, _sav) \
674 const struct side_arg_vec side_sav[] = { _sav }; \
675 const struct side_arg_vec_description sav_desc = { \
677 .len = SIDE_ARRAY_SIZE(side_sav), \
679 tracer_call(desc, &sav_desc); \
682 #define side_event(desc, sav) \
683 side_event_cond(desc) \
684 side_event_call(desc, SIDE_PARAM(sav))
686 #define side_event_call_variadic(desc, _sav, _var_fields) \
688 const struct side_arg_vec side_sav[] = { _sav }; \
689 const struct side_arg_vec_description sav_desc = { \
691 .len = SIDE_ARRAY_SIZE(side_sav), \
693 const struct side_arg_dynamic_event_field side_fields[] = { _var_fields }; \
694 const struct side_arg_dynamic_event_struct var_struct = { \
695 .fields = side_fields, \
696 .len = SIDE_ARRAY_SIZE(side_fields), \
698 tracer_call_variadic(desc, &sav_desc, &var_struct); \
701 #define side_event_variadic(desc, sav, var) \
702 side_event_cond(desc) \
703 side_event_call_variadic(desc, SIDE_PARAM(sav), SIDE_PARAM(var))
705 #define _side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr, _flags) \
706 struct side_event_description _identifier = { \
709 .loglevel = _loglevel, \
710 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
711 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
713 .provider_name = _provider, \
714 .event_name = _event, \
719 #define side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr) \
720 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
721 SIDE_PARAM(_attr), 0)
723 #define side_define_event_variadic(_identifier, _provider, _event, _loglevel, _fields, _attr) \
724 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
725 SIDE_PARAM(_attr), SIDE_EVENT_FLAG_VARIADIC)
727 #define side_declare_event(_identifier) \
728 struct side_event_description _identifier
730 #endif /* _SIDE_TRACE_H */