1 // SPDX-License-Identifier: MIT
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 #include <side/macros.h>
15 /* SIDE stands for "Static Instrumentation Dynamically Enabled" */
18 struct side_arg_vec_description
;
19 struct side_arg_dynamic_vec
;
20 struct side_arg_dynamic_vec_vla
;
21 struct side_type_description
;
22 struct side_event_field
;
23 struct side_tracer_visitor_ctx
;
24 struct side_tracer_dynamic_struct_visitor_ctx
;
25 struct side_tracer_dynamic_vla_visitor_ctx
;
44 SIDE_TYPE_VLA_VISITOR
,
67 enum side_dynamic_type
{
68 SIDE_DYNAMIC_TYPE_NULL
,
70 SIDE_DYNAMIC_TYPE_BOOL
,
73 SIDE_DYNAMIC_TYPE_U16
,
74 SIDE_DYNAMIC_TYPE_U32
,
75 SIDE_DYNAMIC_TYPE_U64
,
77 SIDE_DYNAMIC_TYPE_S16
,
78 SIDE_DYNAMIC_TYPE_S32
,
79 SIDE_DYNAMIC_TYPE_S64
,
81 SIDE_DYNAMIC_TYPE_STRING
,
83 SIDE_DYNAMIC_TYPE_STRUCT
,
84 SIDE_DYNAMIC_TYPE_STRUCT_VISITOR
,
86 SIDE_DYNAMIC_TYPE_VLA
,
87 SIDE_DYNAMIC_TYPE_VLA_VISITOR
,
91 SIDE_LOGLEVEL_EMERG
= 0,
92 SIDE_LOGLEVEL_ALERT
= 1,
93 SIDE_LOGLEVEL_CRIT
= 2,
94 SIDE_LOGLEVEL_ERR
= 3,
95 SIDE_LOGLEVEL_WARNING
= 4,
96 SIDE_LOGLEVEL_NOTICE
= 5,
97 SIDE_LOGLEVEL_INFO
= 6,
98 SIDE_LOGLEVEL_DEBUG
= 7,
101 enum side_visitor_status
{
102 SIDE_VISITOR_STATUS_OK
= 0,
103 SIDE_VISITOR_STATUS_ERROR
= -1,
106 typedef enum side_visitor_status (*side_visitor
)(
107 const struct side_tracer_visitor_ctx
*tracer_ctx
,
109 typedef enum side_visitor_status (*side_dynamic_struct_visitor
)(
110 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
112 typedef enum side_visitor_status (*side_dynamic_vla_visitor
)(
113 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
116 /* User attributes. */
122 struct side_type_description
{
123 uint32_t type
; /* enum side_type */
125 const struct side_attr
*attr
;
129 const struct side_event_field
*fields
;
133 const struct side_type_description
*elem_type
;
136 const struct side_type_description
*elem_type
;
139 const struct side_type_description
*elem_type
;
140 side_visitor visitor
;
145 struct side_event_field
{
146 const char *field_name
;
147 struct side_type_description side_type
;
150 enum side_event_flags
{
151 SIDE_EVENT_FLAG_VARIADIC
= (1 << 0),
154 struct side_event_description
{
157 uint32_t loglevel
; /* enum side_loglevel */
162 const char *provider_name
;
163 const char *event_name
;
164 const struct side_event_field
*fields
;
165 const struct side_attr
*attr
;
168 struct side_arg_dynamic_vec_vla
{
169 const struct side_arg_dynamic_vec
*sav
;
173 struct side_arg_dynamic_vec
{
174 uint32_t dynamic_type
; /* enum side_dynamic_type */
176 const struct side_attr
*attr
;
191 const struct side_arg_dynamic_event_struct
*side_dynamic_struct
;
194 side_dynamic_struct_visitor visitor
;
195 } side_dynamic_struct_visitor
;
197 const struct side_arg_dynamic_vec_vla
*side_dynamic_vla
;
200 side_dynamic_vla_visitor visitor
;
201 } side_dynamic_vla_visitor
;
205 struct side_arg_dynamic_event_field
{
206 const char *field_name
;
207 const struct side_arg_dynamic_vec elem
;
210 struct side_arg_dynamic_event_struct
{
211 const struct side_arg_dynamic_event_field
*fields
;
215 struct side_arg_vec
{
230 const struct side_arg_vec_description
*side_struct
;
231 const struct side_arg_vec_description
*side_array
;
232 const struct side_arg_vec_description
*side_vla
;
233 void *side_vla_app_visitor_ctx
;
235 void *side_array_fixint
;
241 struct side_arg_dynamic_vec dynamic
;
245 struct side_arg_vec_description
{
246 const struct side_arg_vec
*sav
;
250 /* The visitor pattern is a double-dispatch visitor. */
251 struct side_tracer_visitor_ctx
{
252 enum side_visitor_status (*write_elem
)(
253 const struct side_tracer_visitor_ctx
*tracer_ctx
,
254 const struct side_arg_vec
*elem
);
255 void *priv
; /* Private tracer context. */
258 struct side_tracer_dynamic_struct_visitor_ctx
{
259 enum side_visitor_status (*write_field
)(
260 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
261 const struct side_arg_dynamic_event_field
*dynamic_field
);
262 void *priv
; /* Private tracer context. */
265 struct side_tracer_dynamic_vla_visitor_ctx
{
266 enum side_visitor_status (*write_elem
)(
267 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
268 const struct side_arg_dynamic_vec
*elem
);
269 void *priv
; /* Private tracer context. */
272 #define side_attr(_key, _value) \
278 #define side_attr_list(...) \
279 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
281 #define side_type_decl(_type, _attr) \
284 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
288 #define side_field(_name, _type, _attr) \
290 .field_name = _name, \
291 .side_type = side_type_decl(_type, SIDE_PARAM(_attr)), \
294 #define side_type_struct_decl(_fields, _attr) \
296 .type = SIDE_TYPE_STRUCT, \
297 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
301 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
306 #define side_field_struct(_name, _fields, _attr) \
308 .field_name = _name, \
309 .side_type = side_type_struct_decl(SIDE_PARAM(_fields), SIDE_PARAM(_attr)), \
312 #define side_type_array_decl(_elem_type, _length, _attr) \
314 .type = SIDE_TYPE_ARRAY, \
315 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
320 .elem_type = _elem_type, \
324 #define side_field_array(_name, _elem_type, _length, _attr) \
326 .field_name = _name, \
327 .side_type = side_type_array_decl(SIDE_PARAM(_elem_type), _length, SIDE_PARAM(_attr)), \
330 #define side_type_vla_decl(_elem_type, _attr) \
332 .type = SIDE_TYPE_VLA, \
333 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
337 .elem_type = _elem_type, \
341 #define side_field_vla(_name, _elem_type, _attr) \
343 .field_name = _name, \
344 .side_type = side_type_vla_decl(SIDE_PARAM(_elem_type), SIDE_PARAM(_attr)), \
347 #define side_type_vla_visitor_decl(_elem_type, _visitor, _attr) \
349 .type = SIDE_TYPE_VLA_VISITOR, \
350 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
353 .side_vla_visitor = { \
354 .elem_type = SIDE_PARAM(_elem_type), \
355 .visitor = _visitor, \
359 #define side_field_vla_visitor(_name, _elem_type, _visitor, _attr) \
361 .field_name = _name, \
362 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor, SIDE_PARAM(_attr)), \
365 #define side_elem(...) \
366 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
368 #define side_elem_type(_type, _attr) \
369 side_elem(side_type_decl(_type, SIDE_PARAM(_attr)))
371 #define side_field_list(...) \
372 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
374 #define side_arg_bool(val) { .type = SIDE_TYPE_BOOL, .u = { .side_bool = !!(val) } }
375 #define side_arg_u8(val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (val) } }
376 #define side_arg_u16(val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (val) } }
377 #define side_arg_u32(val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (val) } }
378 #define side_arg_u64(val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (val) } }
379 #define side_arg_s8(val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (val) } }
380 #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } }
381 #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } }
382 #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } }
383 #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } }
384 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
385 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
386 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
387 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
389 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
390 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
391 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
392 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
393 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
394 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
395 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
396 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
398 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
399 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
400 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
401 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
402 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
403 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
404 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
405 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
407 #define side_arg_dynamic(dynamic_arg_type) \
409 .type = SIDE_TYPE_DYNAMIC, \
411 .dynamic = dynamic_arg_type, \
415 #define side_arg_dynamic_null(_attr) \
417 .dynamic_type = SIDE_DYNAMIC_TYPE_NULL, \
418 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
422 #define side_arg_dynamic_bool(_val, _attr) \
424 .dynamic_type = SIDE_DYNAMIC_TYPE_BOOL, \
425 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
428 .side_bool = !!(_val), \
432 #define side_arg_dynamic_u8(_val, _attr) \
434 .dynamic_type = SIDE_DYNAMIC_TYPE_U8, \
435 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
441 #define side_arg_dynamic_u16(_val, _attr) \
443 .dynamic_type = SIDE_DYNAMIC_TYPE_U16, \
444 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
447 .side_u16 = (_val), \
450 #define side_arg_dynamic_u32(_val, _attr) \
452 .dynamic_type = SIDE_DYNAMIC_TYPE_U32, \
453 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
456 .side_u32 = (_val), \
459 #define side_arg_dynamic_u64(_val, _attr) \
461 .dynamic_type = SIDE_DYNAMIC_TYPE_U64, \
462 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
465 .side_u64 = (_val), \
469 #define side_arg_dynamic_s8(_val, _attr) \
471 .dynamic_type = SIDE_DYNAMIC_TYPE_S8, \
472 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
478 #define side_arg_dynamic_s16(_val, _attr) \
480 .dynamic_type = SIDE_DYNAMIC_TYPE_S16, \
481 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
484 .side_s16 = (_val), \
487 #define side_arg_dynamic_s32(_val, _attr) \
489 .dynamic_type = SIDE_DYNAMIC_TYPE_S32, \
490 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
493 .side_s32 = (_val), \
496 #define side_arg_dynamic_s64(_val, _attr) \
498 .dynamic_type = SIDE_DYNAMIC_TYPE_S64, \
499 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
502 .side_s64 = (_val), \
506 #define side_arg_dynamic_string(_val, _attr) \
508 .dynamic_type = SIDE_DYNAMIC_TYPE_STRING, \
509 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
516 #define side_arg_dynamic_vla(_vla, _attr) \
518 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA, \
519 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
522 .side_dynamic_vla = (_vla), \
526 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx, _attr) \
528 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
529 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
532 .side_dynamic_vla_visitor = { \
534 .visitor = _dynamic_vla_visitor, \
539 #define side_arg_dynamic_struct(_struct, _attr) \
541 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT, \
542 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
545 .side_dynamic_struct = (_struct), \
549 #define side_arg_dynamic_struct_visitor(_dynamic_struct_visitor, _ctx, _attr) \
551 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
552 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
555 .side_dynamic_struct_visitor = { \
557 .visitor = _dynamic_struct_visitor, \
562 #define side_arg_dynamic_define_vec(_identifier, _sav) \
563 const struct side_arg_dynamic_vec _identifier##_vec[] = { _sav }; \
564 const struct side_arg_dynamic_vec_vla _identifier = { \
565 .sav = _identifier##_vec, \
566 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
569 #define side_arg_dynamic_define_struct(_identifier, _struct_fields) \
570 const struct side_arg_dynamic_event_field _identifier##_fields[] = { _struct_fields }; \
571 const struct side_arg_dynamic_event_struct _identifier = { \
572 .fields = _identifier##_fields, \
573 .len = SIDE_ARRAY_SIZE(_identifier##_fields), \
576 #define side_arg_define_vec(_identifier, _sav) \
577 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
578 const struct side_arg_vec_description _identifier = { \
579 .sav = _identifier##_vec, \
580 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
583 #define side_arg_dynamic_field(_name, _elem) \
585 .field_name = _name, \
589 #define side_arg_list(...) __VA_ARGS__
591 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
593 #define side_event_call(desc, _sav) \
595 const struct side_arg_vec side_sav[] = { _sav }; \
596 const struct side_arg_vec_description sav_desc = { \
598 .len = SIDE_ARRAY_SIZE(side_sav), \
600 tracer_call(desc, &sav_desc); \
603 #define side_event(desc, sav) \
604 side_event_cond(desc) \
605 side_event_call(desc, SIDE_PARAM(sav))
607 #define side_event_call_variadic(desc, _sav, _var_fields) \
609 const struct side_arg_vec side_sav[] = { _sav }; \
610 const struct side_arg_vec_description sav_desc = { \
612 .len = SIDE_ARRAY_SIZE(side_sav), \
614 const struct side_arg_dynamic_event_field side_fields[] = { _var_fields }; \
615 const struct side_arg_dynamic_event_struct var_struct = { \
616 .fields = side_fields, \
617 .len = SIDE_ARRAY_SIZE(side_fields), \
619 tracer_call_variadic(desc, &sav_desc, &var_struct); \
622 #define side_event_variadic(desc, sav, var) \
623 side_event_cond(desc) \
624 side_event_call_variadic(desc, SIDE_PARAM(sav), SIDE_PARAM(var))
626 #define _side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr, _flags) \
627 struct side_event_description _identifier = { \
630 .loglevel = _loglevel, \
631 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
632 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
634 .provider_name = _provider, \
635 .event_name = _event, \
640 #define side_define_event(_identifier, _provider, _event, _loglevel, _fields, _attr) \
641 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
642 SIDE_PARAM(_attr), 0)
644 #define side_define_event_variadic(_identifier, _provider, _event, _loglevel, _fields, _attr) \
645 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_fields), \
646 SIDE_PARAM(_attr), SIDE_EVENT_FLAG_VARIADIC)
648 #define side_declare_event(_identifier) \
649 struct side_event_description _identifier
651 #endif /* _SIDE_TRACE_H */