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 */
124 //TODO: we should add something like a list of user attributes (namespaced strings)
128 const struct side_event_field
*fields
;
132 const struct side_type_description
*elem_type
;
135 const struct side_type_description
*elem_type
;
138 const struct side_type_description
*elem_type
;
139 side_visitor visitor
;
144 struct side_event_field
{
145 const char *field_name
;
146 struct side_type_description side_type
;
149 enum side_event_flags
{
150 SIDE_EVENT_FLAG_VARIADIC
= (1 << 0),
153 struct side_event_description
{
156 uint32_t loglevel
; /* enum side_loglevel */
161 const char *provider_name
;
162 const char *event_name
;
163 const struct side_event_field
*fields
;
164 const struct side_attr
*attr
;
167 struct side_arg_dynamic_vec_vla
{
168 const struct side_arg_dynamic_vec
*sav
;
172 struct side_arg_dynamic_vec
{
173 uint32_t dynamic_type
; /* enum side_dynamic_type */
188 const struct side_arg_dynamic_event_struct
*side_dynamic_struct
;
191 side_dynamic_struct_visitor visitor
;
192 } side_dynamic_struct_visitor
;
194 const struct side_arg_dynamic_vec_vla
*side_dynamic_vla
;
197 side_dynamic_vla_visitor visitor
;
198 } side_dynamic_vla_visitor
;
202 struct side_arg_dynamic_event_field
{
203 const char *field_name
;
204 const struct side_arg_dynamic_vec elem
;
205 //TODO: we should add something like a list of user attributes (namespaced strings)
208 struct side_arg_dynamic_event_struct
{
209 const struct side_arg_dynamic_event_field
*fields
;
213 struct side_arg_vec
{
228 const struct side_arg_vec_description
*side_struct
;
229 const struct side_arg_vec_description
*side_array
;
230 const struct side_arg_vec_description
*side_vla
;
231 void *side_vla_app_visitor_ctx
;
233 void *side_array_fixint
;
239 struct side_arg_dynamic_vec dynamic
;
243 struct side_arg_vec_description
{
244 const struct side_arg_vec
*sav
;
248 /* The visitor pattern is a double-dispatch visitor. */
249 struct side_tracer_visitor_ctx
{
250 enum side_visitor_status (*write_elem
)(
251 const struct side_tracer_visitor_ctx
*tracer_ctx
,
252 const struct side_arg_vec
*elem
);
253 void *priv
; /* Private tracer context. */
256 struct side_tracer_dynamic_struct_visitor_ctx
{
257 enum side_visitor_status (*write_field
)(
258 const struct side_tracer_dynamic_struct_visitor_ctx
*tracer_ctx
,
259 const struct side_arg_dynamic_event_field
*dynamic_field
);
260 void *priv
; /* Private tracer context. */
263 struct side_tracer_dynamic_vla_visitor_ctx
{
264 enum side_visitor_status (*write_elem
)(
265 const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
266 const struct side_arg_dynamic_vec
*elem
);
267 void *priv
; /* Private tracer context. */
270 #define side_attr(_key, _value) \
276 #define side_attr_list(...) \
277 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
279 #define side_type_decl(_type) { .type = _type }
281 #define side_field(_name, _type) \
283 .field_name = _name, \
284 .side_type = side_type_decl(_type), \
287 #define side_type_struct_decl(_fields) \
289 .type = SIDE_TYPE_STRUCT, \
292 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
297 #define side_field_struct(_name, _fields) \
299 .field_name = _name, \
300 .side_type = side_type_struct_decl(SIDE_PARAM(_fields)), \
303 #define side_type_array_decl(_elem_type, _length) \
305 .type = SIDE_TYPE_ARRAY, \
309 .elem_type = _elem_type, \
313 #define side_field_array(_name, _elem_type, _length) \
315 .field_name = _name, \
316 .side_type = side_type_array_decl(_elem_type, _length), \
319 #define side_type_vla_decl(_elem_type) \
321 .type = SIDE_TYPE_VLA, \
324 .elem_type = _elem_type, \
328 #define side_field_vla(_name, _elem_type) \
330 .field_name = _name, \
331 .side_type = side_type_vla_decl(_elem_type), \
334 #define side_type_vla_visitor_decl(_elem_type, _visitor) \
336 .type = SIDE_TYPE_VLA_VISITOR, \
338 .side_vla_visitor = { \
339 .elem_type = SIDE_PARAM(_elem_type), \
340 .visitor = _visitor, \
344 #define side_field_vla_visitor(_name, _elem_type, _visitor) \
346 .field_name = _name, \
347 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor), \
350 #define side_elem(...) \
351 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
353 #define side_elem_type(...) \
354 side_elem(side_type_decl(__VA_ARGS__))
356 #define side_field_list(...) \
357 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
359 #define side_arg_bool(val) { .type = SIDE_TYPE_BOOL, .u = { .side_bool = !!(val) } }
360 #define side_arg_u8(val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (val) } }
361 #define side_arg_u16(val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (val) } }
362 #define side_arg_u32(val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (val) } }
363 #define side_arg_u64(val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (val) } }
364 #define side_arg_s8(val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (val) } }
365 #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } }
366 #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } }
367 #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } }
368 #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } }
369 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
370 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
371 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
372 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
374 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
375 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
376 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
377 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
378 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
379 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
380 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
381 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
383 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
384 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
385 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
386 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
387 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
388 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
389 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
390 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
392 #define side_arg_dynamic(dynamic_arg_type) \
394 .type = SIDE_TYPE_DYNAMIC, \
396 .dynamic = dynamic_arg_type, \
400 #define side_arg_dynamic_null(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_NULL }
402 #define side_arg_dynamic_bool(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_BOOL, .u = { .side_bool = !!(val) } }
403 #define side_arg_dynamic_u8(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U8, .u = { .side_u8 = (val) } }
404 #define side_arg_dynamic_u16(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U16, .u = { .side_u16 = (val) } }
405 #define side_arg_dynamic_u32(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U32, .u = { .side_u32 = (val) } }
406 #define side_arg_dynamic_u64(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_U64, .u = { .side_u64 = (val) } }
407 #define side_arg_dynamic_s8(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S8, .u = { .side_s8 = (val) } }
408 #define side_arg_dynamic_s16(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S16, .u = { .side_s16 = (val) } }
409 #define side_arg_dynamic_s32(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S32, .u = { .side_s32 = (val) } }
410 #define side_arg_dynamic_s64(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_S64, .u = { .side_s64 = (val) } }
411 #define side_arg_dynamic_string(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_STRING, .u = { .string = (val) } }
413 #define side_arg_dynamic_vla(_vla) { .dynamic_type = SIDE_DYNAMIC_TYPE_VLA, .u = { .side_dynamic_vla = (_vla) } }
414 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx) \
416 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
418 .side_dynamic_vla_visitor = { \
420 .visitor = _dynamic_vla_visitor, \
425 #define side_arg_dynamic_struct(_struct) { .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT, .u = { .side_dynamic_struct = (_struct) } }
426 #define side_arg_dynamic_struct_visitor(_dynamic_struct_visitor, _ctx) \
428 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
430 .side_dynamic_struct_visitor = { \
432 .visitor = _dynamic_struct_visitor, \
437 #define side_arg_dynamic_define_vec(_identifier, _sav) \
438 const struct side_arg_dynamic_vec _identifier##_vec[] = { _sav }; \
439 const struct side_arg_dynamic_vec_vla _identifier = { \
440 .sav = _identifier##_vec, \
441 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
444 #define side_arg_dynamic_define_struct(_identifier, _struct_fields) \
445 const struct side_arg_dynamic_event_field _identifier##_fields[] = { _struct_fields }; \
446 const struct side_arg_dynamic_event_struct _identifier = { \
447 .fields = _identifier##_fields, \
448 .len = SIDE_ARRAY_SIZE(_identifier##_fields), \
451 #define side_arg_define_vec(_identifier, _sav) \
452 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
453 const struct side_arg_vec_description _identifier = { \
454 .sav = _identifier##_vec, \
455 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
458 #define side_arg_dynamic_field(_name, _elem) \
460 .field_name = _name, \
464 #define side_arg_list(...) __VA_ARGS__
466 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
468 #define side_event_call(desc, _sav) \
470 const struct side_arg_vec side_sav[] = { _sav }; \
471 const struct side_arg_vec_description sav_desc = { \
473 .len = SIDE_ARRAY_SIZE(side_sav), \
475 tracer_call(desc, &sav_desc); \
478 #define side_event(desc, sav) \
479 side_event_cond(desc) \
480 side_event_call(desc, SIDE_PARAM(sav))
482 #define side_event_call_variadic(desc, _sav, _var_fields) \
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 const struct side_arg_dynamic_event_field side_fields[] = { _var_fields }; \
490 const struct side_arg_dynamic_event_struct var_struct = { \
491 .fields = side_fields, \
492 .len = SIDE_ARRAY_SIZE(side_fields), \
494 tracer_call_variadic(desc, &sav_desc, &var_struct); \
497 #define side_event_variadic(desc, sav, var) \
498 side_event_cond(desc) \
499 side_event_call_variadic(desc, SIDE_PARAM(sav), SIDE_PARAM(var))
501 #define _side_define_event(_identifier, _provider, _event, _loglevel, _attr, _fields, _flags) \
502 struct side_event_description _identifier = { \
505 .loglevel = _loglevel, \
506 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
507 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
509 .provider_name = _provider, \
510 .event_name = _event, \
515 #define side_define_event(_identifier, _provider, _event, _loglevel, _attr, _fields) \
516 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_attr), \
517 SIDE_PARAM(_fields), 0)
519 #define side_define_event_variadic(_identifier, _provider, _event, _loglevel, _attr, _fields) \
520 _side_define_event(_identifier, _provider, _event, _loglevel, SIDE_PARAM(_attr), \
521 SIDE_PARAM(_fields), SIDE_EVENT_FLAG_VARIADIC)
523 #define side_declare_event(_identifier) \
524 struct side_event_description _identifier
526 #endif /* _SIDE_TRACE_H */