Commit | Line | Data |
---|---|---|
f611d0c3 MD |
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_type_description; | |
bdc39c09 | 19 | struct side_dynamic_type_description; |
f611d0c3 | 20 | struct side_event_field; |
352a4b77 | 21 | struct side_tracer_visitor_ctx; |
bdc39c09 MD |
22 | struct side_tracer_dynamic_map_visitor_ctx; |
23 | struct side_tracer_dynamic_vla_visitor_ctx; | |
f611d0c3 MD |
24 | |
25 | enum side_type { | |
26 | SIDE_TYPE_U8, | |
27 | SIDE_TYPE_U16, | |
28 | SIDE_TYPE_U32, | |
29 | SIDE_TYPE_U64, | |
30 | SIDE_TYPE_S8, | |
31 | SIDE_TYPE_S16, | |
32 | SIDE_TYPE_S32, | |
33 | SIDE_TYPE_S64, | |
ba845af5 | 34 | |
f611d0c3 | 35 | SIDE_TYPE_STRING, |
bdc39c09 | 36 | |
f611d0c3 MD |
37 | SIDE_TYPE_STRUCT, |
38 | SIDE_TYPE_ARRAY, | |
39 | SIDE_TYPE_VLA, | |
40 | SIDE_TYPE_VLA_VISITOR, | |
ba845af5 MD |
41 | |
42 | SIDE_TYPE_ARRAY_U8, | |
43 | SIDE_TYPE_ARRAY_U16, | |
44 | SIDE_TYPE_ARRAY_U32, | |
45 | SIDE_TYPE_ARRAY_U64, | |
46 | SIDE_TYPE_ARRAY_S8, | |
47 | SIDE_TYPE_ARRAY_S16, | |
48 | SIDE_TYPE_ARRAY_S32, | |
49 | SIDE_TYPE_ARRAY_S64, | |
50 | ||
1533629f MD |
51 | SIDE_TYPE_VLA_U8, |
52 | SIDE_TYPE_VLA_U16, | |
53 | SIDE_TYPE_VLA_U32, | |
54 | SIDE_TYPE_VLA_U64, | |
55 | SIDE_TYPE_VLA_S8, | |
56 | SIDE_TYPE_VLA_S16, | |
57 | SIDE_TYPE_VLA_S32, | |
58 | SIDE_TYPE_VLA_S64, | |
59 | ||
bdc39c09 MD |
60 | SIDE_TYPE_DYNAMIC, |
61 | }; | |
62 | ||
63 | enum side_dynamic_type { | |
64 | SIDE_DYNAMIC_TYPE_NULL, | |
65 | ||
66 | SIDE_DYNAMIC_TYPE_U8, | |
67 | SIDE_DYNAMIC_TYPE_U16, | |
68 | SIDE_DYNAMIC_TYPE_U32, | |
69 | SIDE_DYNAMIC_TYPE_U64, | |
70 | SIDE_DYNAMIC_TYPE_S8, | |
71 | SIDE_DYNAMIC_TYPE_S16, | |
72 | SIDE_DYNAMIC_TYPE_S32, | |
73 | SIDE_DYNAMIC_TYPE_S64, | |
74 | ||
75 | SIDE_DYNAMIC_TYPE_STRING, | |
76 | ||
77 | SIDE_DYNAMIC_TYPE_MAP_VISITOR, | |
78 | SIDE_DYNAMIC_TYPE_VLA_VISITOR, | |
f611d0c3 MD |
79 | }; |
80 | ||
81 | enum side_loglevel { | |
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, | |
90 | }; | |
91 | ||
92 | enum side_visitor_status { | |
93 | SIDE_VISITOR_STATUS_ERROR = -1, | |
94 | SIDE_VISITOR_STATUS_OK = 0, | |
95 | SIDE_VISITOR_STATUS_END = 1, | |
96 | }; | |
97 | ||
352a4b77 MD |
98 | typedef enum side_visitor_status (*side_visitor)(const struct side_tracer_visitor_ctx *tracer_ctx, |
99 | void *app_ctx); | |
bdc39c09 MD |
100 | typedef enum side_visitor_status (*side_dynamic_map_visitor)(const struct side_tracer_dynamic_map_visitor_ctx *tracer_ctx, |
101 | void *app_ctx); | |
102 | typedef enum side_visitor_status (*side_dynamic_vla_visitor)(const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx, | |
103 | void *app_ctx); | |
f611d0c3 MD |
104 | |
105 | struct side_type_description { | |
106 | enum side_type type; | |
8d02fd98 | 107 | //TODO: we should add something like a list of user attributes (namespaced strings) |
f611d0c3 MD |
108 | union { |
109 | struct { | |
110 | uint32_t nr_fields; | |
111 | const struct side_event_field *fields; | |
112 | } side_struct; | |
113 | struct { | |
114 | uint32_t length; | |
115 | const struct side_type_description *elem_type; | |
116 | } side_array; | |
117 | struct { | |
118 | const struct side_type_description *elem_type; | |
119 | } side_vla; | |
120 | struct { | |
121 | const struct side_type_description *elem_type; | |
352a4b77 | 122 | side_visitor visitor; |
f611d0c3 MD |
123 | } side_vla_visitor; |
124 | } u; | |
125 | }; | |
126 | ||
127 | struct side_event_field { | |
128 | const char *field_name; | |
129 | struct side_type_description side_type; | |
130 | }; | |
131 | ||
132 | struct side_event_description { | |
133 | uint32_t version; | |
134 | uint32_t enabled; | |
135 | uint32_t loglevel; /* enum side_loglevel */ | |
136 | uint32_t nr_fields; | |
137 | const char *provider_name; | |
138 | const char *event_name; | |
139 | const struct side_event_field *fields; | |
140 | }; | |
141 | ||
bdc39c09 MD |
142 | struct side_arg_dynamic_vec { |
143 | uint32_t type; /* enum side_dynamic_type */ | |
144 | union { | |
145 | uint8_t side_u8; | |
146 | uint16_t side_u16; | |
147 | uint32_t side_u32; | |
148 | uint64_t side_u64; | |
149 | int8_t side_s8; | |
150 | int16_t side_s16; | |
151 | int32_t side_s32; | |
152 | int64_t side_s64; | |
153 | ||
154 | const char *string; | |
155 | ||
156 | struct { | |
157 | void *app_dynamic_visitor_ctx; | |
158 | side_dynamic_map_visitor visitor; | |
159 | } side_dynamic_map_visitor; | |
160 | struct { | |
161 | void *app_dynamic_visitor_ctx; | |
162 | side_dynamic_vla_visitor visitor; | |
163 | } side_dynamic_vla_visitor; | |
164 | } u; | |
165 | }; | |
166 | ||
f611d0c3 MD |
167 | struct side_arg_vec { |
168 | uint32_t type; /* enum side_type */ | |
169 | union { | |
170 | uint8_t side_u8; | |
171 | uint16_t side_u16; | |
172 | uint32_t side_u32; | |
173 | uint64_t side_u64; | |
174 | int8_t side_s8; | |
175 | int16_t side_s16; | |
176 | int32_t side_s32; | |
177 | int64_t side_s64; | |
ba845af5 | 178 | |
f611d0c3 MD |
179 | const char *string; |
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; | |
352a4b77 | 183 | void *side_vla_app_visitor_ctx; |
ba845af5 MD |
184 | |
185 | void *side_array_fixint; | |
1533629f MD |
186 | struct { |
187 | void *p; | |
188 | uint32_t length; | |
189 | } side_vla_fixint; | |
bdc39c09 MD |
190 | |
191 | struct side_arg_dynamic_vec dynamic; | |
f611d0c3 MD |
192 | } u; |
193 | }; | |
194 | ||
195 | struct side_arg_vec_description { | |
196 | const struct side_arg_vec *sav; | |
197 | uint32_t len; | |
198 | }; | |
199 | ||
bdc39c09 MD |
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) | |
204 | }; | |
205 | ||
352a4b77 MD |
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. */ | |
211 | }; | |
212 | ||
bdc39c09 MD |
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. */ | |
217 | }; | |
218 | ||
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. */ | |
223 | }; | |
224 | ||
f611d0c3 MD |
225 | #define side_type_decl(_type) { .type = _type } |
226 | #define side_field(_type, _name) { .field_name = _name, .side_type = side_type_decl(_type) } | |
227 | ||
228 | #define side_type_struct_decl(_fields) \ | |
229 | { \ | |
230 | .type = SIDE_TYPE_STRUCT, \ | |
231 | .u = { \ | |
232 | .side_struct = { \ | |
233 | .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \ | |
234 | .fields = _fields, \ | |
235 | }, \ | |
236 | }, \ | |
237 | } | |
238 | #define side_field_struct(_name, _fields) \ | |
239 | { \ | |
240 | .field_name = _name, \ | |
241 | .side_type = side_type_struct_decl(SIDE_PARAM(_fields)), \ | |
242 | } | |
243 | ||
244 | #define side_type_array_decl(_elem_type, _length) \ | |
245 | { \ | |
246 | .type = SIDE_TYPE_ARRAY, \ | |
247 | .u = { \ | |
248 | .side_array = { \ | |
249 | .length = _length, \ | |
250 | .elem_type = _elem_type, \ | |
251 | }, \ | |
252 | }, \ | |
253 | } | |
254 | #define side_field_array(_name, _elem_type, _length) \ | |
255 | { \ | |
256 | .field_name = _name, \ | |
257 | .side_type = side_type_array_decl(_elem_type, _length), \ | |
258 | } | |
259 | ||
260 | #define side_type_vla_decl(_elem_type) \ | |
261 | { \ | |
262 | .type = SIDE_TYPE_VLA, \ | |
263 | .u = { \ | |
264 | .side_vla = { \ | |
265 | .elem_type = _elem_type, \ | |
266 | }, \ | |
267 | }, \ | |
268 | } | |
269 | #define side_field_vla(_name, _elem_type) \ | |
270 | { \ | |
271 | .field_name = _name, \ | |
272 | .side_type = side_type_vla_decl(_elem_type), \ | |
273 | } | |
274 | ||
352a4b77 | 275 | #define side_type_vla_visitor_decl(_elem_type, _visitor) \ |
f611d0c3 MD |
276 | { \ |
277 | .type = SIDE_TYPE_VLA_VISITOR, \ | |
278 | .u = { \ | |
279 | .side_vla_visitor = { \ | |
cdd6e858 | 280 | .elem_type = SIDE_PARAM(_elem_type), \ |
352a4b77 | 281 | .visitor = _visitor, \ |
f611d0c3 MD |
282 | }, \ |
283 | }, \ | |
284 | } | |
352a4b77 | 285 | #define side_field_vla_visitor(_name, _elem_type, _visitor) \ |
f611d0c3 MD |
286 | { \ |
287 | .field_name = _name, \ | |
cdd6e858 | 288 | .side_type = side_type_vla_visitor_decl(SIDE_PARAM(_elem_type), _visitor), \ |
f611d0c3 MD |
289 | } |
290 | ||
71002b5e | 291 | #define side_elem(...) \ |
cdd6e858 MD |
292 | SIDE_COMPOUND_LITERAL(const struct side_type_description, __VA_ARGS__) |
293 | ||
294 | #define side_elem_type(...) \ | |
295 | side_elem(side_type_decl(__VA_ARGS__)) | |
f611d0c3 MD |
296 | |
297 | #define side_field_list(...) \ | |
298 | SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__) | |
299 | ||
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) } } | |
352a4b77 | 312 | #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_app_visitor_ctx = (_ctx) } } |
f611d0c3 | 313 | |
1533629f MD |
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) } } | |
322 | ||
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) } } } | |
ba845af5 | 331 | |
bdc39c09 MD |
332 | #define side_arg_dynamic(dynamic_arg_type) \ |
333 | { \ | |
334 | .type = SIDE_TYPE_DYNAMIC, \ | |
335 | .u = { \ | |
336 | .dynamic = dynamic_arg_type, \ | |
337 | }, \ | |
338 | } | |
339 | ||
340 | #define side_arg_dynamic_null(val) { .type = SIDE_DYNAMIC_TYPE_NULL } | |
341 | ||
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) } } | |
351 | ||
352 | #define side_arg_dynamic_vla_visitor(_dynamic_vla_visitor, _ctx) \ | |
353 | { \ | |
354 | .type = SIDE_DYNAMIC_TYPE_VLA_VISITOR, \ | |
355 | .app_dynamic_visitor_ctx = _ctx, \ | |
356 | .u = { \ | |
357 | .vla_visitor = _dynamic_vla_visitor, \ | |
358 | }, \ | |
359 | } | |
360 | ||
361 | #define side_arg_dynamic_map_visitor(_dynamic_map_visitor, _ctx) \ | |
362 | { \ | |
363 | .type = SIDE_DYNAMIC_TYPE_MAP_VISITOR, \ | |
364 | .app_dynamic_visitor_ctx = _ctx, \ | |
365 | .u = { \ | |
366 | .map_visitor = _dynamic_map_visitor, \ | |
367 | }, \ | |
368 | } | |
369 | ||
f611d0c3 MD |
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), \ | |
375 | } | |
376 | ||
377 | #define side_arg_list(...) __VA_ARGS__ | |
378 | ||
379 | #define side_event_cond(desc) if (side_unlikely((desc)->enabled)) | |
380 | #define side_event_call(desc, _sav) \ | |
381 | { \ | |
382 | const struct side_arg_vec side_sav[] = { _sav }; \ | |
383 | const struct side_arg_vec_description sav_desc = { \ | |
384 | .sav = side_sav, \ | |
385 | .len = SIDE_ARRAY_SIZE(side_sav), \ | |
386 | }; \ | |
387 | tracer_call(desc, &sav_desc); \ | |
388 | } | |
389 | ||
390 | #define side_event(desc, sav) \ | |
391 | side_event_cond(desc) \ | |
392 | side_event_call(desc, SIDE_PARAM(sav)); \ | |
393 | ||
394 | #define side_define_event(_identifier, _provider, _event, _loglevel, _fields) \ | |
395 | struct side_event_description _identifier = { \ | |
396 | .version = 0, \ | |
397 | .enabled = 0, \ | |
398 | .loglevel = _loglevel, \ | |
399 | .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \ | |
400 | .provider_name = _provider, \ | |
401 | .event_name = _event, \ | |
402 | .fields = _fields, \ | |
403 | } | |
404 | ||
405 | #define side_declare_event(_identifier) \ | |
406 | struct side_event_description _identifier | |
407 | ||
408 | #endif /* _SIDE_TRACE_H */ |