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_type_description
;
19 struct side_dynamic_type_description
;
20 struct side_event_field
;
21 struct side_tracer_visitor_ctx
;
22 struct side_tracer_dynamic_map_visitor_ctx
;
23 struct side_tracer_dynamic_vla_visitor_ctx
;
40 SIDE_TYPE_VLA_VISITOR
,
63 enum side_dynamic_type
{
64 SIDE_DYNAMIC_TYPE_NULL
,
67 SIDE_DYNAMIC_TYPE_U16
,
68 SIDE_DYNAMIC_TYPE_U32
,
69 SIDE_DYNAMIC_TYPE_U64
,
71 SIDE_DYNAMIC_TYPE_S16
,
72 SIDE_DYNAMIC_TYPE_S32
,
73 SIDE_DYNAMIC_TYPE_S64
,
75 SIDE_DYNAMIC_TYPE_STRING
,
77 SIDE_DYNAMIC_TYPE_MAP_VISITOR
,
78 SIDE_DYNAMIC_TYPE_VLA_VISITOR
,
82 SIDE_LOGLEVEL_EMERG
= 0,
83 SIDE_LOGLEVEL_ALERT
= 1,
84 SIDE_LOGLEVEL_CRIT
= 2,
85 SIDE_LOGLEVEL_ERR
= 3,
86 SIDE_LOGLEVEL_WARNING
= 4,
87 SIDE_LOGLEVEL_NOTICE
= 5,
88 SIDE_LOGLEVEL_INFO
= 6,
89 SIDE_LOGLEVEL_DEBUG
= 7,
92 enum side_visitor_status
{
93 SIDE_VISITOR_STATUS_ERROR
= -1,
94 SIDE_VISITOR_STATUS_OK
= 0,
95 SIDE_VISITOR_STATUS_END
= 1,
98 typedef enum side_visitor_status (*side_visitor
)(const struct side_tracer_visitor_ctx
*tracer_ctx
,
100 typedef enum side_visitor_status (*side_dynamic_map_visitor
)(const struct side_tracer_dynamic_map_visitor_ctx
*tracer_ctx
,
102 typedef enum side_visitor_status (*side_dynamic_vla_visitor
)(const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
105 struct side_type_description
{
107 //TODO: we should add something like a list of user attributes (namespaced strings)
111 const struct side_event_field
*fields
;
115 const struct side_type_description
*elem_type
;
118 const struct side_type_description
*elem_type
;
121 const struct side_type_description
*elem_type
;
122 side_visitor visitor
;
127 struct side_event_field
{
128 const char *field_name
;
129 struct side_type_description side_type
;
132 struct side_event_description
{
135 uint32_t loglevel
; /* enum side_loglevel */
137 const char *provider_name
;
138 const char *event_name
;
139 const struct side_event_field
*fields
;
142 struct side_arg_dynamic_vec
{
143 uint32_t type
; /* enum side_dynamic_type */
157 void *app_dynamic_visitor_ctx
;
158 side_dynamic_map_visitor visitor
;
159 } side_dynamic_map_visitor
;
161 void *app_dynamic_visitor_ctx
;
162 side_dynamic_vla_visitor visitor
;
163 } side_dynamic_vla_visitor
;
167 struct side_arg_vec
{
168 uint32_t type
; /* enum side_type */
180 const struct side_arg_vec_description
*side_struct
;
181 const struct side_arg_vec_description
*side_array
;
182 const struct side_arg_vec_description
*side_vla
;
183 void *side_vla_app_visitor_ctx
;
185 void *side_array_fixint
;
191 struct side_arg_dynamic_vec dynamic
;
195 struct side_arg_vec_description
{
196 const struct side_arg_vec
*sav
;
200 struct side_dynamic_event_field
{
201 const char *field_name
;
202 const struct side_arg_dynamic_vec
*elem
;
203 //TODO: we should add something like a list of user attributes (namespaced strings)
206 /* The visitor pattern is a double-dispatch visitor. */
207 struct side_tracer_visitor_ctx
{
208 enum side_visitor_status (*write_elem
)(const struct side_tracer_visitor_ctx
*tracer_ctx
,
209 const struct side_arg_vec
*elem
);
210 void *priv
; /* Private tracer context. */
213 struct side_tracer_dynamic_map_visitor_ctx
{
214 enum side_visitor_status (*write_field
)(const struct side_tracer_dynamic_map_visitor_ctx
*tracer_ctx
,
215 const struct side_dynamic_event_field
*field
);
216 void *priv
; /* Private tracer context. */
219 struct side_tracer_dynamic_vla_visitor_ctx
{
220 enum side_visitor_status (*write_elem
)(const struct side_tracer_dynamic_vla_visitor_ctx
*tracer_ctx
,
221 const struct side_arg_dynamic_vec
*elem
);
222 void *priv
; /* Private tracer context. */
225 #define side_type_decl(_type) { .type = _type }
226 #define side_field(_type, _name) { .field_name = _name, .side_type = side_type_decl(_type) }
228 #define side_type_struct_decl(_fields) \
230 .type = SIDE_TYPE_STRUCT, \
233 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
238 #define side_field_struct(_name, _fields) \
240 .field_name = _name, \
241 .side_type = side_type_struct_decl(SIDE_PARAM(_fields)), \
244 #define side_type_array_decl(_elem_type, _length) \
246 .type = SIDE_TYPE_ARRAY, \
250 .elem_type = _elem_type, \
254 #define side_field_array(_name, _elem_type, _length) \
256 .field_name = _name, \
257 .side_type = side_type_array_decl(_elem_type, _length), \
260 #define side_type_vla_decl(_elem_type) \
262 .type = SIDE_TYPE_VLA, \
265 .elem_type = _elem_type, \
269 #define side_field_vla(_name, _elem_type) \
271 .field_name = _name, \
272 .side_type = side_type_vla_decl(_elem_type), \
275 #define side_type_vla_visitor_decl(_elem_type, _visitor) \
277 .type = SIDE_TYPE_VLA_VISITOR, \
279 .side_vla_visitor = { \
280 .elem_type = SIDE_PARAM(_elem_type), \
281 .visitor = _visitor, \
285 #define side_field_vla_visitor(_name, _elem_type, _visitor) \
287 .field_name = _name, \
288 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor), \
291 #define side_elem(...) \
292 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
294 #define side_elem_type(...) \
295 side_elem(side_type_decl(__VA_ARGS__))
297 #define side_field_list(...) \
298 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
300 #define side_arg_u8(val) { .type = SIDE_TYPE_U8, .u = { .side_u8 = (val) } }
301 #define side_arg_u16(val) { .type = SIDE_TYPE_U16, .u = { .side_u16 = (val) } }
302 #define side_arg_u32(val) { .type = SIDE_TYPE_U32, .u = { .side_u32 = (val) } }
303 #define side_arg_u64(val) { .type = SIDE_TYPE_U64, .u = { .side_u64 = (val) } }
304 #define side_arg_s8(val) { .type = SIDE_TYPE_S8, .u = { .side_s8 = (val) } }
305 #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } }
306 #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } }
307 #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } }
308 #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } }
309 #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } }
310 #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } }
311 #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } }
312 #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } }
314 #define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } }
315 #define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } }
316 #define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } }
317 #define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } }
318 #define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } }
319 #define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } }
320 #define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } }
321 #define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } }
323 #define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } }
324 #define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
325 #define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
326 #define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
327 #define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
328 #define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
329 #define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
330 #define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } }
332 #define side_arg_dynamic(dynamic_arg_type) \
334 .type = SIDE_TYPE_DYNAMIC, \
336 .dynamic = dynamic_arg_type, \
340 #define side_arg_dynamic_null(val) { .type = SIDE_DYNAMIC_TYPE_NULL }
342 #define side_arg_dynamic_u8(val) { .type = SIDE_DYNAMIC_TYPE_U8, .u = { .side_u8 = (val) } }
343 #define side_arg_dynamic_u16(val) { .type = SIDE_DYNAMIC_TYPE_U16, .u = { .side_u16 = (val) } }
344 #define side_arg_dynamic_u32(val) { .type = SIDE_DYNAMIC_TYPE_U32, .u = { .side_u32 = (val) } }
345 #define side_arg_dynamic_u64(val) { .type = SIDE_DYNAMIC_TYPE_U64, .u = { .side_u64 = (val) } }
346 #define side_arg_dynamic_s8(val) { .type = SIDE_DYNAMIC_TYPE_S8, .u = { .side_s8 = (val) } }
347 #define side_arg_dynamic_s16(val) { .type = SIDE_DYNAMIC_TYPE_S16, .u = { .side_s16 = (val) } }
348 #define side_arg_dynamic_s32(val) { .type = SIDE_DYNAMIC_TYPE_S32, .u = { .side_s32 = (val) } }
349 #define side_arg_dynamic_s64(val) { .type = SIDE_DYNAMIC_TYPE_S64, .u = { .side_s64 = (val) } }
350 #define side_arg_dynamic_string(val) { .type = SIDE_DYNAMIC_TYPE_STRING, .u = { .string = (val) } }
352 #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx) \
354 .type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
355 .app_dynamic_visitor_ctx = _ctx, \
357 .vla_visitor = _dynamic_vla_visitor, \
361 #define side_arg_dynamic_map_visitor(_dynamic_map_visitor, _ctx) \
363 .type = SIDE_DYNAMIC_TYPE_MAP_VISITOR, \
364 .app_dynamic_visitor_ctx = _ctx, \
366 .map_visitor = _dynamic_map_visitor, \
370 #define side_arg_define_vec(_identifier, _sav) \
371 const struct side_arg_vec _identifier##_vec[] = { _sav }; \
372 const struct side_arg_vec_description _identifier = { \
373 .sav = _identifier##_vec, \
374 .len = SIDE_ARRAY_SIZE(_identifier##_vec), \
377 #define side_arg_list(...) __VA_ARGS__
379 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
380 #define side_event_call(desc, _sav) \
382 const struct side_arg_vec side_sav[] = { _sav }; \
383 const struct side_arg_vec_description sav_desc = { \
385 .len = SIDE_ARRAY_SIZE(side_sav), \
387 tracer_call(desc, &sav_desc); \
390 #define side_event(desc, sav) \
391 side_event_cond(desc) \
392 side_event_call(desc, SIDE_PARAM(sav)); \
394 #define side_define_event(_identifier, _provider, _event, _loglevel, _fields) \
395 struct side_event_description _identifier = { \
398 .loglevel = _loglevel, \
399 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
400 .provider_name = _provider, \
401 .event_name = _event, \
405 #define side_declare_event(_identifier) \
406 struct side_event_description _identifier
408 #endif /* _SIDE_TRACE_H */