Add static type attributes
[libside.git] / include / side / trace.h
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
6 #ifndef _SIDE_TRACE_H
7 #define _SIDE_TRACE_H
8
9 #include <stdint.h>
10 #include <inttypes.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <side/macros.h>
14
15 /* SIDE stands for "Static Instrumentation Dynamically Enabled" */
16
17 struct side_arg_vec;
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;
26
27 enum side_type {
28 SIDE_TYPE_BOOL,
29
30 SIDE_TYPE_U8,
31 SIDE_TYPE_U16,
32 SIDE_TYPE_U32,
33 SIDE_TYPE_U64,
34 SIDE_TYPE_S8,
35 SIDE_TYPE_S16,
36 SIDE_TYPE_S32,
37 SIDE_TYPE_S64,
38
39 SIDE_TYPE_STRING,
40
41 SIDE_TYPE_STRUCT,
42 SIDE_TYPE_ARRAY,
43 SIDE_TYPE_VLA,
44 SIDE_TYPE_VLA_VISITOR,
45
46 SIDE_TYPE_ARRAY_U8,
47 SIDE_TYPE_ARRAY_U16,
48 SIDE_TYPE_ARRAY_U32,
49 SIDE_TYPE_ARRAY_U64,
50 SIDE_TYPE_ARRAY_S8,
51 SIDE_TYPE_ARRAY_S16,
52 SIDE_TYPE_ARRAY_S32,
53 SIDE_TYPE_ARRAY_S64,
54
55 SIDE_TYPE_VLA_U8,
56 SIDE_TYPE_VLA_U16,
57 SIDE_TYPE_VLA_U32,
58 SIDE_TYPE_VLA_U64,
59 SIDE_TYPE_VLA_S8,
60 SIDE_TYPE_VLA_S16,
61 SIDE_TYPE_VLA_S32,
62 SIDE_TYPE_VLA_S64,
63
64 SIDE_TYPE_DYNAMIC,
65 };
66
67 enum side_dynamic_type {
68 SIDE_DYNAMIC_TYPE_NULL,
69
70 SIDE_DYNAMIC_TYPE_BOOL,
71
72 SIDE_DYNAMIC_TYPE_U8,
73 SIDE_DYNAMIC_TYPE_U16,
74 SIDE_DYNAMIC_TYPE_U32,
75 SIDE_DYNAMIC_TYPE_U64,
76 SIDE_DYNAMIC_TYPE_S8,
77 SIDE_DYNAMIC_TYPE_S16,
78 SIDE_DYNAMIC_TYPE_S32,
79 SIDE_DYNAMIC_TYPE_S64,
80
81 SIDE_DYNAMIC_TYPE_STRING,
82
83 SIDE_DYNAMIC_TYPE_STRUCT,
84 SIDE_DYNAMIC_TYPE_STRUCT_VISITOR,
85
86 SIDE_DYNAMIC_TYPE_VLA,
87 SIDE_DYNAMIC_TYPE_VLA_VISITOR,
88 };
89
90 enum side_loglevel {
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,
99 };
100
101 enum side_visitor_status {
102 SIDE_VISITOR_STATUS_OK = 0,
103 SIDE_VISITOR_STATUS_ERROR = -1,
104 };
105
106 typedef enum side_visitor_status (*side_visitor)(
107 const struct side_tracer_visitor_ctx *tracer_ctx,
108 void *app_ctx);
109 typedef enum side_visitor_status (*side_dynamic_struct_visitor)(
110 const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
111 void *app_ctx);
112 typedef enum side_visitor_status (*side_dynamic_vla_visitor)(
113 const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx,
114 void *app_ctx);
115
116 /* User attributes. */
117 struct side_attr {
118 const char *key;
119 const char *value;
120 };
121
122 struct side_type_description {
123 uint32_t type; /* enum side_type */
124 uint32_t nr_attr;
125 const struct side_attr *attr;
126 union {
127 struct {
128 uint32_t nr_fields;
129 const struct side_event_field *fields;
130 } side_struct;
131 struct {
132 uint32_t length;
133 const struct side_type_description *elem_type;
134 } side_array;
135 struct {
136 const struct side_type_description *elem_type;
137 } side_vla;
138 struct {
139 const struct side_type_description *elem_type;
140 side_visitor visitor;
141 } side_vla_visitor;
142 } u;
143 };
144
145 struct side_event_field {
146 const char *field_name;
147 struct side_type_description side_type;
148 };
149
150 enum side_event_flags {
151 SIDE_EVENT_FLAG_VARIADIC = (1 << 0),
152 };
153
154 struct side_event_description {
155 uint32_t version;
156 uint32_t enabled;
157 uint32_t loglevel; /* enum side_loglevel */
158 uint32_t nr_fields;
159 uint32_t nr_attr;
160 uint32_t _unused;
161 uint64_t flags;
162 const char *provider_name;
163 const char *event_name;
164 const struct side_event_field *fields;
165 const struct side_attr *attr;
166 };
167
168 struct side_arg_dynamic_vec_vla {
169 const struct side_arg_dynamic_vec *sav;
170 uint32_t len;
171 };
172
173 struct side_arg_dynamic_vec {
174 uint32_t dynamic_type; /* enum side_dynamic_type */
175 union {
176 uint8_t side_bool;
177
178 uint8_t side_u8;
179 uint16_t side_u16;
180 uint32_t side_u32;
181 uint64_t side_u64;
182 int8_t side_s8;
183 int16_t side_s16;
184 int32_t side_s32;
185 int64_t side_s64;
186
187 const char *string;
188
189 const struct side_arg_dynamic_event_struct *side_dynamic_struct;
190 struct {
191 void *app_ctx;
192 side_dynamic_struct_visitor visitor;
193 } side_dynamic_struct_visitor;
194
195 const struct side_arg_dynamic_vec_vla *side_dynamic_vla;
196 struct {
197 void *app_ctx;
198 side_dynamic_vla_visitor visitor;
199 } side_dynamic_vla_visitor;
200 } u;
201 };
202
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)
207 };
208
209 struct side_arg_dynamic_event_struct {
210 const struct side_arg_dynamic_event_field *fields;
211 uint32_t len;
212 };
213
214 struct side_arg_vec {
215 enum side_type type;
216 union {
217 uint8_t side_bool;
218
219 uint8_t side_u8;
220 uint16_t side_u16;
221 uint32_t side_u32;
222 uint64_t side_u64;
223 int8_t side_s8;
224 int16_t side_s16;
225 int32_t side_s32;
226 int64_t side_s64;
227
228 const char *string;
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;
233
234 void *side_array_fixint;
235 struct {
236 void *p;
237 uint32_t length;
238 } side_vla_fixint;
239
240 struct side_arg_dynamic_vec dynamic;
241 } u;
242 };
243
244 struct side_arg_vec_description {
245 const struct side_arg_vec *sav;
246 uint32_t len;
247 };
248
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. */
255 };
256
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. */
262 };
263
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. */
269 };
270
271 #define side_attr(_key, _value) \
272 { \
273 .key = _key, \
274 .value = _value, \
275 }
276
277 #define side_attr_list(...) \
278 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
279
280 #define side_type_decl(_type, _attr) \
281 { \
282 .type = _type, \
283 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
284 .attr = _attr, \
285 }
286
287 #define side_field(_name, _type, _attr) \
288 { \
289 .field_name = _name, \
290 .side_type = side_type_decl(_type, SIDE_PARAM(_attr)), \
291 }
292
293 #define side_type_struct_decl(_fields, _attr) \
294 { \
295 .type = SIDE_TYPE_STRUCT, \
296 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
297 .attr = _attr, \
298 .u = { \
299 .side_struct = { \
300 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
301 .fields = _fields, \
302 }, \
303 }, \
304 }
305 #define side_field_struct(_name, _fields, _attr) \
306 { \
307 .field_name = _name, \
308 .side_type = side_type_struct_decl(SIDE_PARAM(_fields), SIDE_PARAM(_attr)), \
309 }
310
311 #define side_type_array_decl(_elem_type, _length, _attr) \
312 { \
313 .type = SIDE_TYPE_ARRAY, \
314 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
315 .attr = _attr, \
316 .u = { \
317 .side_array = { \
318 .length = _length, \
319 .elem_type = _elem_type, \
320 }, \
321 }, \
322 }
323 #define side_field_array(_name, _elem_type, _length, _attr) \
324 { \
325 .field_name = _name, \
326 .side_type = side_type_array_decl(SIDE_PARAM(_elem_type), _length, SIDE_PARAM(_attr)), \
327 }
328
329 #define side_type_vla_decl(_elem_type, _attr) \
330 { \
331 .type = SIDE_TYPE_VLA, \
332 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
333 .attr = _attr, \
334 .u = { \
335 .side_vla = { \
336 .elem_type = _elem_type, \
337 }, \
338 }, \
339 }
340 #define side_field_vla(_name, _elem_type, _attr) \
341 { \
342 .field_name = _name, \
343 .side_type = side_type_vla_decl(SIDE_PARAM(_elem_type), SIDE_PARAM(_attr)), \
344 }
345
346 #define side_type_vla_visitor_decl(_elem_type, _visitor, _attr) \
347 { \
348 .type = SIDE_TYPE_VLA_VISITOR, \
349 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
350 .attr = _attr, \
351 .u = { \
352 .side_vla_visitor = { \
353 .elem_type = SIDE_PARAM(_elem_type), \
354 .visitor = _visitor, \
355 }, \
356 }, \
357 }
358 #define side_field_vla_visitor(_name, _elem_type, _visitor, _attr) \
359 { \
360 .field_name = _name, \
361 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor, SIDE_PARAM(_attr)), \
362 }
363
364 #define side_elem(...) \
365 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
366
367 #define side_elem_type(_type, _attr) \
368 side_elem(side_type_decl(_type, SIDE_PARAM(_attr)))
369
370 #define side_field_list(...) \
371 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
372
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) } }
387
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) } }
396
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) } } }
405
406 #define side_arg_dynamic(dynamic_arg_type) \
407 { \
408 .type = SIDE_TYPE_DYNAMIC, \
409 .u = { \
410 .dynamic = dynamic_arg_type, \
411 }, \
412 }
413
414 #define side_arg_dynamic_null(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_NULL }
415
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) } }
426
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) \
429 { \
430 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
431 .u = { \
432 .side_dynamic_vla_visitor = { \
433 .app_ctx = _ctx, \
434 .visitor = _dynamic_vla_visitor, \
435 }, \
436 }, \
437 }
438
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) \
441 { \
442 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
443 .u = { \
444 .side_dynamic_struct_visitor = { \
445 .app_ctx = _ctx, \
446 .visitor = _dynamic_struct_visitor, \
447 }, \
448 }, \
449 }
450
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), \
456 }
457
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), \
463 }
464
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), \
470 }
471
472 #define side_arg_dynamic_field(_name, _elem) \
473 { \
474 .field_name = _name, \
475 .elem = _elem, \
476 }
477
478 #define side_arg_list(...) __VA_ARGS__
479
480 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
481
482 #define side_event_call(desc, _sav) \
483 { \
484 const struct side_arg_vec side_sav[] = { _sav }; \
485 const struct side_arg_vec_description sav_desc = { \
486 .sav = side_sav, \
487 .len = SIDE_ARRAY_SIZE(side_sav), \
488 }; \
489 tracer_call(desc, &sav_desc); \
490 }
491
492 #define side_event(desc, sav) \
493 side_event_cond(desc) \
494 side_event_call(desc, SIDE_PARAM(sav))
495
496 #define side_event_call_variadic(desc, _sav, _var_fields) \
497 { \
498 const struct side_arg_vec side_sav[] = { _sav }; \
499 const struct side_arg_vec_description sav_desc = { \
500 .sav = side_sav, \
501 .len = SIDE_ARRAY_SIZE(side_sav), \
502 }; \
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), \
507 }; \
508 tracer_call_variadic(desc, &sav_desc, &var_struct); \
509 }
510
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))
514
515 #define _side_define_event(_identifier, _provider, _event, _loglevel, _attr, _fields, _flags) \
516 struct side_event_description _identifier = { \
517 .version = 0, \
518 .enabled = 0, \
519 .loglevel = _loglevel, \
520 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
521 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
522 .flags = (_flags), \
523 .provider_name = _provider, \
524 .event_name = _event, \
525 .fields = _fields, \
526 .attr = _attr, \
527 }
528
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)
532
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)
536
537 #define side_declare_event(_identifier) \
538 struct side_event_description _identifier
539
540 #endif /* _SIDE_TRACE_H */
This page took 0.066695 seconds and 5 git commands to generate.