baac5f4f24a88513b1ac0ea4844d96964cf02090
[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 //TODO: we should add something like a list of user attributes (namespaced strings)
125 union {
126 struct {
127 uint32_t nr_fields;
128 const struct side_event_field *fields;
129 } side_struct;
130 struct {
131 uint32_t length;
132 const struct side_type_description *elem_type;
133 } side_array;
134 struct {
135 const struct side_type_description *elem_type;
136 } side_vla;
137 struct {
138 const struct side_type_description *elem_type;
139 side_visitor visitor;
140 } side_vla_visitor;
141 } u;
142 };
143
144 struct side_event_field {
145 const char *field_name;
146 struct side_type_description side_type;
147 };
148
149 enum side_event_flags {
150 SIDE_EVENT_FLAG_VARIADIC = (1 << 0),
151 };
152
153 struct side_event_description {
154 uint32_t version;
155 uint32_t enabled;
156 uint32_t loglevel; /* enum side_loglevel */
157 uint32_t nr_fields;
158 uint32_t nr_attr;
159 uint32_t _unused;
160 uint64_t flags;
161 const char *provider_name;
162 const char *event_name;
163 const struct side_event_field *fields;
164 const struct side_attr *attr;
165 };
166
167 struct side_arg_dynamic_vec_vla {
168 const struct side_arg_dynamic_vec *sav;
169 uint32_t len;
170 };
171
172 struct side_arg_dynamic_vec {
173 uint32_t dynamic_type; /* enum side_dynamic_type */
174 union {
175 uint8_t side_bool;
176
177 uint8_t side_u8;
178 uint16_t side_u16;
179 uint32_t side_u32;
180 uint64_t side_u64;
181 int8_t side_s8;
182 int16_t side_s16;
183 int32_t side_s32;
184 int64_t side_s64;
185
186 const char *string;
187
188 const struct side_arg_dynamic_event_struct *side_dynamic_struct;
189 struct {
190 void *app_ctx;
191 side_dynamic_struct_visitor visitor;
192 } side_dynamic_struct_visitor;
193
194 const struct side_arg_dynamic_vec_vla *side_dynamic_vla;
195 struct {
196 void *app_ctx;
197 side_dynamic_vla_visitor visitor;
198 } side_dynamic_vla_visitor;
199 } u;
200 };
201
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)
206 };
207
208 struct side_arg_dynamic_event_struct {
209 const struct side_arg_dynamic_event_field *fields;
210 uint32_t len;
211 };
212
213 struct side_arg_vec {
214 enum side_type type;
215 union {
216 uint8_t side_bool;
217
218 uint8_t side_u8;
219 uint16_t side_u16;
220 uint32_t side_u32;
221 uint64_t side_u64;
222 int8_t side_s8;
223 int16_t side_s16;
224 int32_t side_s32;
225 int64_t side_s64;
226
227 const char *string;
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;
232
233 void *side_array_fixint;
234 struct {
235 void *p;
236 uint32_t length;
237 } side_vla_fixint;
238
239 struct side_arg_dynamic_vec dynamic;
240 } u;
241 };
242
243 struct side_arg_vec_description {
244 const struct side_arg_vec *sav;
245 uint32_t len;
246 };
247
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. */
254 };
255
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. */
261 };
262
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. */
268 };
269
270 #define side_attr(_key, _value) \
271 { \
272 .key = _key, \
273 .value = _value, \
274 }
275
276 #define side_attr_list(...) \
277 SIDE_COMPOUND_LITERAL(const struct side_attr, __VA_ARGS__)
278
279 #define side_type_decl(_type) { .type = _type }
280
281 #define side_field(_name, _type) \
282 { \
283 .field_name = _name, \
284 .side_type = side_type_decl(_type), \
285 }
286
287 #define side_type_struct_decl(_fields) \
288 { \
289 .type = SIDE_TYPE_STRUCT, \
290 .u = { \
291 .side_struct = { \
292 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
293 .fields = _fields, \
294 }, \
295 }, \
296 }
297 #define side_field_struct(_name, _fields) \
298 { \
299 .field_name = _name, \
300 .side_type = side_type_struct_decl(SIDE_PARAM(_fields)), \
301 }
302
303 #define side_type_array_decl(_elem_type, _length) \
304 { \
305 .type = SIDE_TYPE_ARRAY, \
306 .u = { \
307 .side_array = { \
308 .length = _length, \
309 .elem_type = _elem_type, \
310 }, \
311 }, \
312 }
313 #define side_field_array(_name, _elem_type, _length) \
314 { \
315 .field_name = _name, \
316 .side_type = side_type_array_decl(_elem_type, _length), \
317 }
318
319 #define side_type_vla_decl(_elem_type) \
320 { \
321 .type = SIDE_TYPE_VLA, \
322 .u = { \
323 .side_vla = { \
324 .elem_type = _elem_type, \
325 }, \
326 }, \
327 }
328 #define side_field_vla(_name, _elem_type) \
329 { \
330 .field_name = _name, \
331 .side_type = side_type_vla_decl(_elem_type), \
332 }
333
334 #define side_type_vla_visitor_decl(_elem_type, _visitor) \
335 { \
336 .type = SIDE_TYPE_VLA_VISITOR, \
337 .u = { \
338 .side_vla_visitor = { \
339 .elem_type = SIDE_PARAM(_elem_type), \
340 .visitor = _visitor, \
341 }, \
342 }, \
343 }
344 #define side_field_vla_visitor(_name, _elem_type, _visitor) \
345 { \
346 .field_name = _name, \
347 .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor), \
348 }
349
350 #define side_elem(...) \
351 SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__)
352
353 #define side_elem_type(...) \
354 side_elem(side_type_decl(__VA_ARGS__))
355
356 #define side_field_list(...) \
357 SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
358
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) } }
373
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) } }
382
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) } } }
391
392 #define side_arg_dynamic(dynamic_arg_type) \
393 { \
394 .type = SIDE_TYPE_DYNAMIC, \
395 .u = { \
396 .dynamic = dynamic_arg_type, \
397 }, \
398 }
399
400 #define side_arg_dynamic_null(val) { .dynamic_type = SIDE_DYNAMIC_TYPE_NULL }
401
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) } }
412
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) \
415 { \
416 .dynamic_type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \
417 .u = { \
418 .side_dynamic_vla_visitor = { \
419 .app_ctx = _ctx, \
420 .visitor = _dynamic_vla_visitor, \
421 }, \
422 }, \
423 }
424
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) \
427 { \
428 .dynamic_type = SIDE_DYNAMIC_TYPE_STRUCT_VISITOR, \
429 .u = { \
430 .side_dynamic_struct_visitor = { \
431 .app_ctx = _ctx, \
432 .visitor = _dynamic_struct_visitor, \
433 }, \
434 }, \
435 }
436
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), \
442 }
443
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), \
449 }
450
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), \
456 }
457
458 #define side_arg_dynamic_field(_name, _elem) \
459 { \
460 .field_name = _name, \
461 .elem = _elem, \
462 }
463
464 #define side_arg_list(...) __VA_ARGS__
465
466 #define side_event_cond(desc) if (side_unlikely((desc)->enabled))
467
468 #define side_event_call(desc, _sav) \
469 { \
470 const struct side_arg_vec side_sav[] = { _sav }; \
471 const struct side_arg_vec_description sav_desc = { \
472 .sav = side_sav, \
473 .len = SIDE_ARRAY_SIZE(side_sav), \
474 }; \
475 tracer_call(desc, &sav_desc); \
476 }
477
478 #define side_event(desc, sav) \
479 side_event_cond(desc) \
480 side_event_call(desc, SIDE_PARAM(sav))
481
482 #define side_event_call_variadic(desc, _sav, _var_fields) \
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 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), \
493 }; \
494 tracer_call_variadic(desc, &sav_desc, &var_struct); \
495 }
496
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))
500
501 #define _side_define_event(_identifier, _provider, _event, _loglevel, _attr, _fields, _flags) \
502 struct side_event_description _identifier = { \
503 .version = 0, \
504 .enabled = 0, \
505 .loglevel = _loglevel, \
506 .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \
507 .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
508 .flags = (_flags), \
509 .provider_name = _provider, \
510 .event_name = _event, \
511 .fields = _fields, \
512 .attr = _attr, \
513 }
514
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)
518
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)
522
523 #define side_declare_event(_identifier) \
524 struct side_event_description _identifier
525
526 #endif /* _SIDE_TRACE_H */
This page took 0.051587 seconds and 4 git commands to generate.