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 */
189 const struct side_arg_dynamic_event_struct
*side_dynamic_struct
;
192 side_dynamic_struct_visitor visitor
;
193 } side_dynamic_struct_visitor
;
195 const struct side_arg_dynamic_vec_vla
*side_dynamic_vla
;
198 side_dynamic_vla_visitor visitor
;
199 } side_dynamic_vla_visitor
;
203 struct side_arg_dynamic_event_field
{
204 const char *field_name
;
205 const struct side_arg_dynamic_vec elem
;
206 //TODO: we should add something like a list of user attributes (namespaced strings)
209 struct side_arg_dynamic_event_struct
{
210 const struct side_arg_dynamic_event_field
*fields
;
214 struct side_arg_vec
{
229 const struct side_arg_vec_description
*side_struct
;
230 const struct side_arg_vec_description
*side_array
;
231 const struct side_arg_vec_description
*side_vla
;
232 void *side_vla_app_visitor_ctx
;
234 void *side_array_fixint
;
240 struct side_arg_dynamic_vec dynamic
;
244 struct side_arg_vec_description
{
245 const struct side_arg_vec
*sav
;
249 /* The visitor pattern is a double-dispatch visitor. */
250 struct side_tracer_visitor_ctx
{
251 enum side_visitor_status (*write_elem
)(
252 const struct side_tracer_visitor_ctx
*tracer_ctx
,
253 const struct side_arg_vec
*elem
);
254 void *priv
; /* Private tracer context. */
257 struct side_tracer_dynamic_struct_visitor_ctx
{
258 enum side_visitor_status (*write_field
)(
259 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
260 const struct side_arg_dynamic_event_field
*dynamic_field
);
261 void *priv
; /* Private tracer context. */
264 struct side_tracer_dynamic_vla_visitor_ctx
{
265 enum side_visitor_status (*write_elem
)(
266 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
267 const struct side_arg_dynamic_vec
*elem
);
268 void *priv
; /* Private tracer context. */
271 #define side_attr(_key, _value) \
277 #define side_attr_list(...) \
278 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
280 #define side_type_decl(_type, _attr) \
283 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
287 #define side_field(_name, _type, _attr) \
289 .field_name = _name, \
290 .side_type = side_type_decl(_type, SIDE_PARAM(_attr)), \
293 #define side_type_struct_decl(_fields, _attr) \
295 .type = SIDE_TYPE_STRUCT, \
296 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
300 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
305 #define side_field_struct(_name, _fields, _attr) \
307 .field_name = _name, \
308 .side_type = side_type_struct_decl(SIDE_PARAM(_fields), SIDE_PARAM(_attr)), \
311 #define side_type_array_decl(_elem_type, _length, _attr) \
313 .type = SIDE_TYPE_ARRAY, \
314 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
319 .elem_type = _elem_type, \
323 #define side_field_array(_name, _elem_type, _length, _attr) \
325 .field_name = _name, \
326 .side_type = side_type_array_decl(SIDE_PARAM(_elem_type), _length, SIDE_PARAM(_attr)), \
329 #define side_type_vla_decl(_elem_type, _attr) \
331 .type = SIDE_TYPE_VLA, \
332 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
336 .elem_type = _elem_type, \
340 #define side_field_vla(_name, _elem_type, _attr) \
342 .field_name = _name, \
343 .side_type = side_type_vla_decl(SIDE_PARAM(_elem_type), SIDE_PARAM(_attr)), \
346 #define side_type_vla_visitor_decl(_elem_type, _visitor, _attr) \
348 .type = SIDE_TYPE_VLA_VISITOR, \
349 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
352 .side_vla_visitor = { \
353 .elem_type = SIDE_PARAM(_elem_type), \
354 .visitor = _visitor, \
358 #define side_field_vla_visitor(_name, _elem_type, _visitor, _attr) \
360 .field_name = _name, \
361 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor, SIDE_PARAM(_attr)), \
364 #define side_elem(...) \
365 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
367 #define side_elem_type(_type, _attr) \
368 side_elem(side_type_decl(_type, SIDE_PARAM(_attr)))
370 #define side_field_list(...) \
371 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
373 #define side_arg_bool(val) { .type = SIDE_TYPE_BOOL, .u = { .side_bool = !!(val) } }
374 #define side_arg_u8(val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (val) } }
375 #define side_arg_u16(val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (val) } }
376 #define side_arg_u32(val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (val) } }
377 #define side_arg_u64(val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (val) } }
378 #define side_arg_s8(val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (val) } }
379 #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } }
380 #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } }
381 #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } }
382 #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } }
383 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
384 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
385 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
386 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
388 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
389 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
390 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
391 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
392 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
393 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
394 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
395 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
397 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
398 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
399 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
400 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
401 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
402 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
403 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
404 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
406 #define side_arg_dynamic(dynamic_arg_type) \
408 .type = SIDE_TYPE_DYNAMIC, \
410 .dynamic = dynamic_arg_type, \
414 #define side_arg_dynamic_null(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_NULL }
416 #define side_arg_dynamic_bool(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_BOOL, .u = { .side_bool = !!(val) } }
417 #define side_arg_dynamic_u8(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U8, .u = { .side_u8 = (val) } }
418 #define side_arg_dynamic_u16(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U16, .u = { .side_u16 = (val) } }
419 #define side_arg_dynamic_u32(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U32, .u = { .side_u32 = (val) } }
420 #define side_arg_dynamic_u64(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U64, .u = { .side_u64 = (val) } }
421 #define side_arg_dynamic_s8(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S8, .u = { .side_s8 = (val) } }
422 #define side_arg_dynamic_s16(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S16, .u = { .side_s16 = (val) } }
423 #define side_arg_dynamic_s32(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S32, .u = { .side_s32 = (val) } }
424 #define side_arg_dynamic_s64(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S64, .u = { .side_s64 = (val) } }
425 #define side_arg_dynamic_string(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_STRING, .u = { .string = (val) } }
427 #define side_arg_dynamic_vla(_vla) { .dynamic_type = SIDE_DYNAMIC_TYPE_VLA, .u = { .side_dynamic_vla = (_vla) } }
428 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx) \
430 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
432 .side_dynamic_vla_visitor = { \
434 .visitor = _dynamic_vla_visitor, \
439 #define side_arg_dynamic_struct(_struct) { .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT, .u = { .side_dynamic_struct = (_struct) } }
440 #define side_arg_dynamic_struct_visitor(_dynamic_struct_visitor, _ctx) \
442 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
444 .side_dynamic_struct_visitor = { \
446 .visitor = _dynamic_struct_visitor, \
451 #define side_arg_dynamic_define_vec(_identifier, _sav) \
452 const struct side_arg_dynamic_vec _identifier##_vec[] = { _sav }; \
453 const struct side_arg_dynamic_vec_vla _identifier = { \
454 .sav = _identifier##_vec, \
455 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
458 #define side_arg_dynamic_define_struct(_identifier, _struct_fields) \
459 const struct side_arg_dynamic_event_field _identifier##_fields[] = { _struct_fields }; \
460 const struct side_arg_dynamic_event_struct _identifier = { \
461 .fields = _identifier##_fields, \
462 .len = SIDE_ARRAY_SIZE(_identifier##_fields), \
465 #define side_arg_define_vec(_identifier, _sav) \
466 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
467 const struct side_arg_vec_description _identifier = { \
468 .sav = _identifier##_vec, \
469 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
472 #define side_arg_dynamic_field(_name, _elem) \
474 .field_name = _name, \
478 #define side_arg_list(...) __VA_ARGS__
480 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
482 #define side_event_call(desc, _sav) \
484 const struct side_arg_vec side_sav[] = { _sav }; \
485 const struct side_arg_vec_description sav_desc = { \
487 .len = SIDE_ARRAY_SIZE(side_sav), \
489 tracer_call(desc, &sav_desc); \
492 #define side_event(desc, sav) \
493 side_event_cond(desc) \
494 side_event_call(desc, SIDE_PARAM(sav))
496 #define side_event_call_variadic(desc, _sav, _var_fields) \
498 const struct side_arg_vec side_sav[] = { _sav }; \
499 const struct side_arg_vec_description sav_desc = { \
501 .len = SIDE_ARRAY_SIZE(side_sav), \
503 const struct side_arg_dynamic_event_field side_fields[] = { _var_fields }; \
504 const struct side_arg_dynamic_event_struct var_struct = { \
505 .fields = side_fields, \
506 .len = SIDE_ARRAY_SIZE(side_fields), \
508 tracer_call_variadic(desc, &sav_desc, &var_struct); \
511 #define side_event_variadic(desc, sav, var) \
512 side_event_cond(desc) \
513 side_event_call_variadic(desc, SIDE_PARAM(sav), SIDE_PARAM(var))
515 #define _side_define_event(_identifier, _provider, _event, _loglevel, _attr, _fields, _flags) \
516 struct side_event_description _identifier = { \
519 .loglevel = _loglevel, \
520 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
521 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
523 .provider_name = _provider, \
524 .event_name = _event, \
529 #define side_define_event(_identifier, _provider, _event, _loglevel, _attr, _fields) \
530 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_attr), \
531 SIDE_PARAM(_fields), 0)
533 #define side_define_event_variadic(_identifier, _provider, _event, _loglevel, _attr, _fields) \
534 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_attr), \
535 SIDE_PARAM(_fields), SIDE_EVENT_FLAG_VARIADIC)
537 #define side_declare_event(_identifier) \
538 struct side_event_description _identifier
540 #endif /* _SIDE_TRACE_H */