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" */
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 extected if need be.
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
;
44 SIDE_TYPE_FLOAT_BINARY16
,
45 SIDE_TYPE_FLOAT_BINARY32
,
46 SIDE_TYPE_FLOAT_BINARY64
,
47 SIDE_TYPE_FLOAT_BINARY128
,
54 SIDE_TYPE_VLA_VISITOR
,
77 enum side_dynamic_type
{
78 SIDE_DYNAMIC_TYPE_NULL
,
80 SIDE_DYNAMIC_TYPE_BOOL
,
83 SIDE_DYNAMIC_TYPE_U16
,
84 SIDE_DYNAMIC_TYPE_U32
,
85 SIDE_DYNAMIC_TYPE_U64
,
87 SIDE_DYNAMIC_TYPE_S16
,
88 SIDE_DYNAMIC_TYPE_S32
,
89 SIDE_DYNAMIC_TYPE_S64
,
91 SIDE_DYNAMIC_TYPE_FLOAT_BINARY16
,
92 SIDE_DYNAMIC_TYPE_FLOAT_BINARY32
,
93 SIDE_DYNAMIC_TYPE_FLOAT_BINARY64
,
94 SIDE_DYNAMIC_TYPE_FLOAT_BINARY128
,
96 SIDE_DYNAMIC_TYPE_STRING
,
98 SIDE_DYNAMIC_TYPE_STRUCT
,
99 SIDE_DYNAMIC_TYPE_STRUCT_VISITOR
,
101 SIDE_DYNAMIC_TYPE_VLA
,
102 SIDE_DYNAMIC_TYPE_VLA_VISITOR
,
106 SIDE_LOGLEVEL_EMERG
= 0,
107 SIDE_LOGLEVEL_ALERT
= 1,
108 SIDE_LOGLEVEL_CRIT
= 2,
109 SIDE_LOGLEVEL_ERR
= 3,
110 SIDE_LOGLEVEL_WARNING
= 4,
111 SIDE_LOGLEVEL_NOTICE
= 5,
112 SIDE_LOGLEVEL_INFO
= 6,
113 SIDE_LOGLEVEL_DEBUG
= 7,
116 enum side_visitor_status
{
117 SIDE_VISITOR_STATUS_OK
= 0,
118 SIDE_VISITOR_STATUS_ERROR
= -1,
121 typedef enum side_visitor_status (*side_visitor
)(
122 const struct side_tracer_visitor_ctx
*tracer_ctx
,
124 typedef enum side_visitor_status (*side_dynamic_struct_visitor
)(
125 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
127 typedef enum side_visitor_status (*side_dynamic_vla_visitor
)(
128 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
131 /* User attributes. */
137 struct side_type_description
{
138 uint32_t type
; /* enum side_type */
140 const struct side_attr
*attr
;
144 const struct side_event_field
*fields
;
148 const struct side_type_description
*elem_type
;
151 const struct side_type_description
*elem_type
;
154 const struct side_type_description
*elem_type
;
155 side_visitor visitor
;
160 struct side_event_field
{
161 const char *field_name
;
162 struct side_type_description side_type
;
165 enum side_event_flags
{
166 SIDE_EVENT_FLAG_VARIADIC
= (1 << 0),
169 struct side_event_description
{
172 uint32_t loglevel
; /* enum side_loglevel */
177 const char *provider_name
;
178 const char *event_name
;
179 const struct side_event_field
*fields
;
180 const struct side_attr
*attr
;
183 struct side_arg_dynamic_vec_vla
{
184 const struct side_arg_dynamic_vec
*sav
;
188 struct side_arg_dynamic_vec
{
189 uint32_t dynamic_type
; /* enum side_dynamic_type */
191 const struct side_attr
*attr
;
205 _Float16 side_float_binary16
;
208 _Float32 side_float_binary32
;
211 _Float64 side_float_binary64
;
214 _Float128 side_float_binary128
;
219 const struct side_arg_dynamic_event_struct
*side_dynamic_struct
;
222 side_dynamic_struct_visitor visitor
;
223 } side_dynamic_struct_visitor
;
225 const struct side_arg_dynamic_vec_vla
*side_dynamic_vla
;
228 side_dynamic_vla_visitor visitor
;
229 } side_dynamic_vla_visitor
;
233 struct side_arg_dynamic_event_field
{
234 const char *field_name
;
235 const struct side_arg_dynamic_vec elem
;
238 struct side_arg_dynamic_event_struct
{
239 const struct side_arg_dynamic_event_field
*fields
;
243 struct side_arg_vec
{
258 _Float16 side_float_binary16
;
261 _Float32 side_float_binary32
;
264 _Float64 side_float_binary64
;
267 _Float128 side_float_binary128
;
271 const struct side_arg_vec_description
*side_struct
;
272 const struct side_arg_vec_description
*side_array
;
273 const struct side_arg_vec_description
*side_vla
;
274 void *side_vla_app_visitor_ctx
;
276 void *side_array_fixint
;
282 struct side_arg_dynamic_vec dynamic
;
286 struct side_arg_vec_description
{
287 const struct side_arg_vec
*sav
;
291 /* The visitor pattern is a double-dispatch visitor. */
292 struct side_tracer_visitor_ctx
{
293 enum side_visitor_status (*write_elem
)(
294 const struct side_tracer_visitor_ctx
*tracer_ctx
,
295 const struct side_arg_vec
*elem
);
296 void *priv
; /* Private tracer context. */
299 struct side_tracer_dynamic_struct_visitor_ctx
{
300 enum side_visitor_status (*write_field
)(
301 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
302 const struct side_arg_dynamic_event_field
*dynamic_field
);
303 void *priv
; /* Private tracer context. */
306 struct side_tracer_dynamic_vla_visitor_ctx
{
307 enum side_visitor_status (*write_elem
)(
308 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
309 const struct side_arg_dynamic_vec
*elem
);
310 void *priv
; /* Private tracer context. */
313 #define side_attr(_key, _value) \
319 #define side_attr_list(...) \
320 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
322 #define side_type_decl(_type, _attr) \
325 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
329 #define side_field(_name, _type, _attr) \
331 .field_name = _name, \
332 .side_type = side_type_decl(_type, SIDE_PARAM(_attr)), \
335 #define side_type_struct_decl(_fields, _attr) \
337 .type = SIDE_TYPE_STRUCT, \
338 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
342 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
347 #define side_field_struct(_name, _fields, _attr) \
349 .field_name = _name, \
350 .side_type = side_type_struct_decl(SIDE_PARAM(_fields), SIDE_PARAM(_attr)), \
353 #define side_type_array_decl(_elem_type, _length, _attr) \
355 .type = SIDE_TYPE_ARRAY, \
356 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
361 .elem_type = _elem_type, \
365 #define side_field_array(_name, _elem_type, _length, _attr) \
367 .field_name = _name, \
368 .side_type = side_type_array_decl(SIDE_PARAM(_elem_type), _length, SIDE_PARAM(_attr)), \
371 #define side_type_vla_decl(_elem_type, _attr) \
373 .type = SIDE_TYPE_VLA, \
374 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
378 .elem_type = _elem_type, \
382 #define side_field_vla(_name, _elem_type, _attr) \
384 .field_name = _name, \
385 .side_type = side_type_vla_decl(SIDE_PARAM(_elem_type), SIDE_PARAM(_attr)), \
388 #define side_type_vla_visitor_decl(_elem_type, _visitor, _attr) \
390 .type = SIDE_TYPE_VLA_VISITOR, \
391 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
394 .side_vla_visitor = { \
395 .elem_type = SIDE_PARAM(_elem_type), \
396 .visitor = _visitor, \
400 #define side_field_vla_visitor(_name, _elem_type, _visitor, _attr) \
402 .field_name = _name, \
403 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor, SIDE_PARAM(_attr)), \
406 #define side_elem(...) \
407 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
409 #define side_elem_type(_type, _attr) \
410 side_elem(side_type_decl(_type, SIDE_PARAM(_attr)))
412 #define side_field_list(...) \
413 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
415 #define side_arg_bool(val) { .type = SIDE_TYPE_BOOL, .u = { .side_bool = !!(val) } }
416 #define side_arg_u8(val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (val) } }
417 #define side_arg_u16(val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (val) } }
418 #define side_arg_u32(val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (val) } }
419 #define side_arg_u64(val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (val) } }
420 #define side_arg_s8(val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (val) } }
421 #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } }
422 #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } }
423 #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } }
424 #define side_arg_float_binary16(val) { .type = SIDE_TYPE_FLOAT_BINARY16, .u = { .side_float_binary16 = (val) } }
425 #define side_arg_float_binary32(val) { .type = SIDE_TYPE_FLOAT_BINARY32, .u = { .side_float_binary32 = (val) } }
426 #define side_arg_float_binary64(val) { .type = SIDE_TYPE_FLOAT_BINARY64, .u = { .side_float_binary64 = (val) } }
427 #define side_arg_float_binary128(val) { .type = SIDE_TYPE_FLOAT_BINARY128, .u = { .side_float_binary128 = (val) } }
429 #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } }
430 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
431 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
432 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
433 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
435 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
436 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
437 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
438 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
439 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
440 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
441 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
442 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
444 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
445 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
446 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
447 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
448 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
449 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
450 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
451 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
453 #define side_arg_dynamic(dynamic_arg_type) \
455 .type = SIDE_TYPE_DYNAMIC, \
457 .dynamic = dynamic_arg_type, \
461 #define side_arg_dynamic_null(_attr) \
463 .dynamic_type = SIDE_DYNAMIC_TYPE_NULL, \
464 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
468 #define side_arg_dynamic_bool(_val, _attr) \
470 .dynamic_type = SIDE_DYNAMIC_TYPE_BOOL, \
471 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
474 .side_bool = !!(_val), \
478 #define side_arg_dynamic_u8(_val, _attr) \
480 .dynamic_type = SIDE_DYNAMIC_TYPE_U8, \
481 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
487 #define side_arg_dynamic_u16(_val, _attr) \
489 .dynamic_type = SIDE_DYNAMIC_TYPE_U16, \
490 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
493 .side_u16 = (_val), \
496 #define side_arg_dynamic_u32(_val, _attr) \
498 .dynamic_type = SIDE_DYNAMIC_TYPE_U32, \
499 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
502 .side_u32 = (_val), \
505 #define side_arg_dynamic_u64(_val, _attr) \
507 .dynamic_type = SIDE_DYNAMIC_TYPE_U64, \
508 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
511 .side_u64 = (_val), \
515 #define side_arg_dynamic_s8(_val, _attr) \
517 .dynamic_type = SIDE_DYNAMIC_TYPE_S8, \
518 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
524 #define side_arg_dynamic_s16(_val, _attr) \
526 .dynamic_type = SIDE_DYNAMIC_TYPE_S16, \
527 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
530 .side_s16 = (_val), \
533 #define side_arg_dynamic_s32(_val, _attr) \
535 .dynamic_type = SIDE_DYNAMIC_TYPE_S32, \
536 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
539 .side_s32 = (_val), \
542 #define side_arg_dynamic_s64(_val, _attr) \
544 .dynamic_type = SIDE_DYNAMIC_TYPE_S64, \
545 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
548 .side_s64 = (_val), \
552 #define side_arg_dynamic_float_binary16(_val, _attr) \
554 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY16, \
555 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
558 .side_float_binary16 = (_val), \
561 #define side_arg_dynamic_float_binary32(_val, _attr) \
563 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY32, \
564 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
567 .side_float_binary32 = (_val), \
570 #define side_arg_dynamic_float_binary64(_val, _attr) \
572 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY64, \
573 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
576 .side_float_binary64 = (_val), \
579 #define side_arg_dynamic_float_binary128(_val, _attr) \
581 .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY128, \
582 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
585 .side_float_binary128 = (_val), \
589 #define side_arg_dynamic_string(_val, _attr) \
591 .dynamic_type = SIDE_DYNAMIC_TYPE_STRING, \
592 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
599 #define side_arg_dynamic_vla(_vla, _attr) \
601 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA, \
602 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
605 .side_dynamic_vla = (_vla), \
609 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx, _attr) \
611 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
612 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
615 .side_dynamic_vla_visitor = { \
617 .visitor = _dynamic_vla_visitor, \
622 #define side_arg_dynamic_struct(_struct, _attr) \
624 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT, \
625 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
628 .side_dynamic_struct = (_struct), \
632 #define side_arg_dynamic_struct_visitor(_dynamic_struct_visitor, _ctx, _attr) \
634 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
635 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
638 .side_dynamic_struct_visitor = { \
640 .visitor = _dynamic_struct_visitor, \
645 #define side_arg_dynamic_define_vec(_identifier, _sav) \
646 const struct side_arg_dynamic_vec _identifier##_vec[] = { _sav }; \
647 const struct side_arg_dynamic_vec_vla _identifier = { \
648 .sav = _identifier##_vec, \
649 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
652 #define side_arg_dynamic_define_struct(_identifier, _struct_fields) \
653 const struct side_arg_dynamic_event_field _identifier##_fields[] = { _struct_fields }; \
654 const struct side_arg_dynamic_event_struct _identifier = { \
655 .fields = _identifier##_fields, \
656 .len = SIDE_ARRAY_SIZE(_identifier##_fields), \
659 #define side_arg_define_vec(_identifier, _sav) \
660 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
661 const struct side_arg_vec_description _identifier = { \
662 .sav = _identifier##_vec, \
663 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
666 #define side_arg_dynamic_field(_name, _elem) \
668 .field_name = _name, \
672 #define side_arg_list(...) __VA_ARGS__
674 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
676 #define side_event_call(desc, _sav) \
678 const struct side_arg_vec side_sav[] = { _sav }; \
679 const struct side_arg_vec_description sav_desc = { \
681 .len = SIDE_ARRAY_SIZE(side_sav), \
683 tracer_call(desc, &sav_desc); \
686 #define side_event(desc, sav) \
687 side_event_cond(desc) \
688 side_event_call(desc, SIDE_PARAM(sav))
690 #define side_event_call_variadic(desc, _sav, _var_fields) \
692 const struct side_arg_vec side_sav[] = { _sav }; \
693 const struct side_arg_vec_description sav_desc = { \
695 .len = SIDE_ARRAY_SIZE(side_sav), \
697 const struct side_arg_dynamic_event_field side_fields[] = { _var_fields }; \
698 const struct side_arg_dynamic_event_struct var_struct = { \
699 .fields = side_fields, \
700 .len = SIDE_ARRAY_SIZE(side_fields), \
702 tracer_call_variadic(desc, &sav_desc, &var_struct); \
705 #define side_event_variadic(desc, sav, var) \
706 side_event_cond(desc) \
707 side_event_call_variadic(desc, SIDE_PARAM(sav), SIDE_PARAM(var))
709 #define _side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr, _flags) \
710 struct side_event_description _identifier = { \
713 .loglevel = _loglevel, \
714 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
715 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
717 .provider_name = _provider, \
718 .event_name = _event, \
723 #define side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr) \
724 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
725 SIDE_PARAM(_attr), 0)
727 #define side_define_event_variadic(_identifier, _provider, _event, _loglevel, _fields, _attr) \
728 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
729 SIDE_PARAM(_attr), SIDE_EVENT_FLAG_VARIADIC)
731 #define side_declare_event(_identifier) \
732 struct side_event_description _identifier
734 #endif /* _SIDE_TRACE_H */