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_map_visitor_ctx
;
25 struct side_tracer_dynamic_vla_visitor_ctx
;
42 SIDE_TYPE_VLA_VISITOR
,
65 enum side_dynamic_type
{
66 SIDE_DYNAMIC_TYPE_NULL
,
69 SIDE_DYNAMIC_TYPE_U16
,
70 SIDE_DYNAMIC_TYPE_U32
,
71 SIDE_DYNAMIC_TYPE_U64
,
73 SIDE_DYNAMIC_TYPE_S16
,
74 SIDE_DYNAMIC_TYPE_S32
,
75 SIDE_DYNAMIC_TYPE_S64
,
77 SIDE_DYNAMIC_TYPE_STRING
,
79 SIDE_DYNAMIC_TYPE_MAP
,
80 SIDE_DYNAMIC_TYPE_MAP_VISITOR
,
82 SIDE_DYNAMIC_TYPE_VLA
,
83 SIDE_DYNAMIC_TYPE_VLA_VISITOR
,
87 SIDE_LOGLEVEL_EMERG
= 0,
88 SIDE_LOGLEVEL_ALERT
= 1,
89 SIDE_LOGLEVEL_CRIT
= 2,
90 SIDE_LOGLEVEL_ERR
= 3,
91 SIDE_LOGLEVEL_WARNING
= 4,
92 SIDE_LOGLEVEL_NOTICE
= 5,
93 SIDE_LOGLEVEL_INFO
= 6,
94 SIDE_LOGLEVEL_DEBUG
= 7,
97 enum side_visitor_status
{
98 SIDE_VISITOR_STATUS_OK
= 0,
99 SIDE_VISITOR_STATUS_ERROR
= -1,
102 typedef enum side_visitor_status (*side_visitor
)(const struct side_tracer_visitor_ctx
*tracer_ctx
,
104 typedef enum side_visitor_status (*side_dynamic_map_visitor
)(const struct side_tracer_dynamic_map_visitor_ctx
*tracer_ctx
,
106 typedef enum side_visitor_status (*side_dynamic_vla_visitor
)(const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
109 struct side_type_description
{
111 //TODO: we should add something like a list of user attributes (namespaced strings)
115 const struct side_event_field
*fields
;
119 const struct side_type_description
*elem_type
;
122 const struct side_type_description
*elem_type
;
125 const struct side_type_description
*elem_type
;
126 side_visitor visitor
;
131 struct side_event_field
{
132 const char *field_name
;
133 struct side_type_description side_type
;
136 struct side_event_description
{
139 uint32_t loglevel
; /* enum side_loglevel */
141 const char *provider_name
;
142 const char *event_name
;
143 const struct side_event_field
*fields
;
146 struct side_arg_dynamic_vec_vla
{
147 const struct side_arg_dynamic_vec
*sav
;
151 struct side_arg_dynamic_vec
{
152 uint32_t type
; /* enum side_dynamic_type */
165 const struct side_arg_dynamic_event_map
*side_dynamic_map
;
167 void *app_dynamic_visitor_ctx
;
168 side_dynamic_map_visitor visitor
;
169 } side_dynamic_map_visitor
;
171 const struct side_arg_dynamic_vec_vla
*side_dynamic_vla
;
173 void *app_dynamic_visitor_ctx
;
174 side_dynamic_vla_visitor visitor
;
175 } side_dynamic_vla_visitor
;
179 struct side_arg_dynamic_event_field
{
180 const char *field_name
;
181 const struct side_arg_dynamic_vec elem
;
182 //TODO: we should add something like a list of user attributes (namespaced strings)
185 struct side_arg_dynamic_event_map
{
186 const struct side_arg_dynamic_event_field
*fields
;
190 struct side_arg_vec
{
191 uint32_t type
; /* enum side_type */
203 const struct side_arg_vec_description
*side_struct
;
204 const struct side_arg_vec_description
*side_array
;
205 const struct side_arg_vec_description
*side_vla
;
206 void *side_vla_app_visitor_ctx
;
208 void *side_array_fixint
;
214 struct side_arg_dynamic_vec dynamic
;
218 struct side_arg_vec_description
{
219 const struct side_arg_vec
*sav
;
223 /* The visitor pattern is a double-dispatch visitor. */
224 struct side_tracer_visitor_ctx
{
225 enum side_visitor_status (*write_elem
)(const struct side_tracer_visitor_ctx
*tracer_ctx
,
226 const struct side_arg_vec
*elem
);
227 void *priv
; /* Private tracer context. */
230 struct side_tracer_dynamic_map_visitor_ctx
{
231 enum side_visitor_status (*write_field
)(const struct side_tracer_dynamic_map_visitor_ctx
*tracer_ctx
,
232 const struct side_arg_dynamic_event_field
*field
);
233 void *priv
; /* Private tracer context. */
236 struct side_tracer_dynamic_vla_visitor_ctx
{
237 enum side_visitor_status (*write_elem
)(const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
238 const struct side_arg_dynamic_vec
*elem
);
239 void *priv
; /* Private tracer context. */
242 #define side_type_decl(_type) { .type = _type }
243 #define side_field(_type, _name) { .field_name = _name, .side_type = side_type_decl(_type) }
245 #define side_type_struct_decl(_fields) \
247 .type = SIDE_TYPE_STRUCT, \
250 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
255 #define side_field_struct(_name, _fields) \
257 .field_name = _name, \
258 .side_type = side_type_struct_decl(SIDE_PARAM(_fields)), \
261 #define side_type_array_decl(_elem_type, _length) \
263 .type = SIDE_TYPE_ARRAY, \
267 .elem_type = _elem_type, \
271 #define side_field_array(_name, _elem_type, _length) \
273 .field_name = _name, \
274 .side_type = side_type_array_decl(_elem_type, _length), \
277 #define side_type_vla_decl(_elem_type) \
279 .type = SIDE_TYPE_VLA, \
282 .elem_type = _elem_type, \
286 #define side_field_vla(_name, _elem_type) \
288 .field_name = _name, \
289 .side_type = side_type_vla_decl(_elem_type), \
292 #define side_type_vla_visitor_decl(_elem_type, _visitor) \
294 .type = SIDE_TYPE_VLA_VISITOR, \
296 .side_vla_visitor = { \
297 .elem_type = SIDE_PARAM(_elem_type), \
298 .visitor = _visitor, \
302 #define side_field_vla_visitor(_name, _elem_type, _visitor) \
304 .field_name = _name, \
305 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor), \
308 #define side_elem(...) \
309 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
311 #define side_elem_type(...) \
312 side_elem(side_type_decl(__VA_ARGS__))
314 #define side_field_list(...) \
315 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
317 #define side_arg_u8(val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (val) } }
318 #define side_arg_u16(val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (val) } }
319 #define side_arg_u32(val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (val) } }
320 #define side_arg_u64(val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (val) } }
321 #define side_arg_s8(val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (val) } }
322 #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } }
323 #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } }
324 #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } }
325 #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } }
326 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
327 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
328 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
329 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
331 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
332 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
333 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
334 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
335 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
336 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
337 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
338 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
340 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
341 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
342 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
343 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
344 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
345 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
346 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
347 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
349 #define side_arg_dynamic(dynamic_arg_type) \
351 .type = SIDE_TYPE_DYNAMIC, \
353 .dynamic = dynamic_arg_type, \
357 #define side_arg_dynamic_null(val) { .type = SIDE_DYNAMIC_TYPE_NULL }
359 #define side_arg_dynamic_u8(val) { .type = SIDE_DYNAMIC_TYPE_U8, .u = { .side_u8 = (val) } }
360 #define side_arg_dynamic_u16(val) { .type = SIDE_DYNAMIC_TYPE_U16, .u = { .side_u16 = (val) } }
361 #define side_arg_dynamic_u32(val) { .type = SIDE_DYNAMIC_TYPE_U32, .u = { .side_u32 = (val) } }
362 #define side_arg_dynamic_u64(val) { .type = SIDE_DYNAMIC_TYPE_U64, .u = { .side_u64 = (val) } }
363 #define side_arg_dynamic_s8(val) { .type = SIDE_DYNAMIC_TYPE_S8, .u = { .side_s8 = (val) } }
364 #define side_arg_dynamic_s16(val) { .type = SIDE_DYNAMIC_TYPE_S16, .u = { .side_s16 = (val) } }
365 #define side_arg_dynamic_s32(val) { .type = SIDE_DYNAMIC_TYPE_S32, .u = { .side_s32 = (val) } }
366 #define side_arg_dynamic_s64(val) { .type = SIDE_DYNAMIC_TYPE_S64, .u = { .side_s64 = (val) } }
367 #define side_arg_dynamic_string(val) { .type = SIDE_DYNAMIC_TYPE_STRING, .u = { .string = (val) } }
369 #define side_arg_dynamic_vla(_vla) { .type = SIDE_DYNAMIC_TYPE_VLA, .u = { .side_dynamic_vla = (_vla) } }
370 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx) \
372 .type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
373 .app_dynamic_visitor_ctx = _ctx, \
375 .vla_visitor = _dynamic_vla_visitor, \
379 #define side_arg_dynamic_map(_map) { .type = SIDE_DYNAMIC_TYPE_MAP, .u = { .side_dynamic_map = (_map) } }
380 #define side_arg_dynamic_map_visitor(_dynamic_map_visitor, _ctx) \
382 .type = SIDE_DYNAMIC_TYPE_MAP_VISITOR, \
383 .app_dynamic_visitor_ctx = _ctx, \
385 .map_visitor = _dynamic_map_visitor, \
389 #define side_arg_dynamic_define_vec(_identifier, _sav) \
390 const struct side_arg_dynamic_vec _identifier##_vec[] = { _sav }; \
391 const struct side_arg_dynamic_vec_vla _identifier = { \
392 .sav = _identifier##_vec, \
393 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
396 #define side_arg_dynamic_define_map(_identifier, _map_fields) \
397 const struct side_arg_dynamic_event_field _identifier##_fields[] = { _map_fields }; \
398 const struct side_arg_dynamic_event_map _identifier = { \
399 .fields = _identifier##_fields, \
400 .len = SIDE_ARRAY_SIZE(_identifier##_fields), \
403 #define side_arg_define_vec(_identifier, _sav) \
404 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
405 const struct side_arg_vec_description _identifier = { \
406 .sav = _identifier##_vec, \
407 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
410 #define side_arg_dynamic_field(_name, _elem) \
412 .field_name = _name, \
416 #define side_arg_list(...) __VA_ARGS__
418 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
419 #define side_event_call(desc, _sav) \
421 const struct side_arg_vec side_sav[] = { _sav }; \
422 const struct side_arg_vec_description sav_desc = { \
424 .len = SIDE_ARRAY_SIZE(side_sav), \
426 tracer_call(desc, &sav_desc); \
429 #define side_event(desc, sav) \
430 side_event_cond(desc) \
431 side_event_call(desc, SIDE_PARAM(sav)); \
433 #define side_define_event(_identifier, _provider, _event, _loglevel, _fields) \
434 struct side_event_description _identifier = { \
437 .loglevel = _loglevel, \
438 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
439 .provider_name = _provider, \
440 .event_name = _event, \
444 #define side_declare_event(_identifier) \
445 struct side_event_description _identifier
447 #endif /* _SIDE_TRACE_H */