Move the ringbuffer and counter clients to 'src/common/'
[lttng-ust.git] / src / common / events.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2019 (c) Francis Deslauriers <francis.deslauriers@efficios.com>
5 */
6
7 #ifndef _UST_COMMON_UST_EVENTS_H
8 #define _UST_COMMON_UST_EVENTS_H
9
10 #include <limits.h>
11 #include <stdint.h>
12
13 #include <urcu/list.h>
14 #include <urcu/hlist.h>
15
16 #include <lttng/ust-events.h>
17
18 #include "common/macros.h"
19 #include "common/ust-context-provider.h"
20
21 struct lttng_ust_abi_obj;
22 struct lttng_event_notifier_group;
23
24 union lttng_ust_abi_args {
25 struct {
26 void *chan_data;
27 int wakeup_fd;
28 } channel;
29 struct {
30 int shm_fd;
31 int wakeup_fd;
32 } stream;
33 struct {
34 struct lttng_ust_abi_field_iter entry;
35 } field_list;
36 struct {
37 char *ctxname;
38 } app_context;
39 struct {
40 int event_notifier_notif_fd;
41 } event_notifier_handle;
42 struct {
43 void *counter_data;
44 } counter;
45 struct {
46 int shm_fd;
47 } counter_shm;
48 };
49
50 struct lttng_ust_abi_objd_ops {
51 long (*cmd)(int objd, unsigned int cmd, unsigned long arg,
52 union lttng_ust_abi_args *args, void *owner);
53 int (*release)(int objd);
54 };
55
56 enum lttng_enabler_format_type {
57 LTTNG_ENABLER_FORMAT_STAR_GLOB,
58 LTTNG_ENABLER_FORMAT_EVENT,
59 };
60
61 /*
62 * Enabler field, within whatever object is enabling an event. Target of
63 * backward reference.
64 */
65 struct lttng_enabler {
66 enum lttng_enabler_format_type format_type;
67
68 /* head list of struct lttng_ust_filter_bytecode_node */
69 struct cds_list_head filter_bytecode_head;
70 /* head list of struct lttng_ust_excluder_node */
71 struct cds_list_head excluder_head;
72
73 struct lttng_ust_abi_event event_param;
74 unsigned int enabled:1;
75 };
76
77 struct lttng_event_enabler {
78 struct lttng_enabler base;
79 struct cds_list_head node; /* per-session list of enablers */
80 struct lttng_ust_channel_buffer *chan;
81 /*
82 * Unused, but kept around to make it explicit that the tracer can do
83 * it.
84 */
85 struct lttng_ust_ctx *ctx;
86 };
87
88 struct lttng_event_notifier_enabler {
89 struct lttng_enabler base;
90 uint64_t error_counter_index;
91 struct cds_list_head node; /* per-app list of event_notifier enablers */
92 struct cds_list_head capture_bytecode_head;
93 struct lttng_event_notifier_group *group; /* weak ref */
94 uint64_t user_token; /* User-provided token */
95 uint64_t num_captures;
96 };
97
98 enum lttng_ust_bytecode_type {
99 LTTNG_UST_BYTECODE_TYPE_FILTER,
100 LTTNG_UST_BYTECODE_TYPE_CAPTURE,
101 };
102
103 struct lttng_ust_bytecode_node {
104 enum lttng_ust_bytecode_type type;
105 struct cds_list_head node;
106 struct lttng_enabler *enabler;
107 struct {
108 uint32_t len;
109 uint32_t reloc_offset;
110 uint64_t seqnum;
111 char data[];
112 } bc;
113 };
114
115 /*
116 * Bytecode interpreter return value.
117 */
118 enum lttng_ust_bytecode_interpreter_ret {
119 LTTNG_UST_BYTECODE_INTERPRETER_ERROR = -1,
120 LTTNG_UST_BYTECODE_INTERPRETER_OK = 0,
121 };
122
123 struct lttng_interpreter_output;
124 struct lttng_ust_bytecode_runtime_private;
125
126 enum lttng_ust_bytecode_filter_result {
127 LTTNG_UST_BYTECODE_FILTER_ACCEPT = 0,
128 LTTNG_UST_BYTECODE_FILTER_REJECT = 1,
129 };
130
131 struct lttng_ust_bytecode_filter_ctx {
132 enum lttng_ust_bytecode_filter_result result;
133 };
134
135 struct lttng_ust_excluder_node {
136 struct cds_list_head node;
137 struct lttng_enabler *enabler;
138 /*
139 * struct lttng_ust_event_exclusion had variable sized array,
140 * must be last field.
141 */
142 struct lttng_ust_abi_event_exclusion excluder;
143 };
144
145 /* Data structures used by the tracer. */
146
147 struct tp_list_entry {
148 struct lttng_ust_abi_tracepoint_iter tp;
149 struct cds_list_head head;
150 };
151
152 struct lttng_ust_tracepoint_list {
153 struct tp_list_entry *iter;
154 struct cds_list_head head;
155 };
156
157 struct tp_field_list_entry {
158 struct lttng_ust_abi_field_iter field;
159 struct cds_list_head head;
160 };
161
162 struct lttng_ust_field_list {
163 struct tp_field_list_entry *iter;
164 struct cds_list_head head;
165 };
166
167 /*
168 * Objects in a linked-list of enablers, owned by an event or event_notifier.
169 * This is used because an event (or a event_notifier) can be enabled by more
170 * than one enabler and we want a quick way to iterate over all enablers of an
171 * object.
172 *
173 * For example, event rules "my_app:a*" and "my_app:ab*" will both match the
174 * event with the name "my_app:abc".
175 */
176 struct lttng_enabler_ref {
177 struct cds_list_head node; /* enabler ref list */
178 struct lttng_enabler *ref; /* backward ref */
179 };
180
181 #define LTTNG_COUNTER_DIMENSION_MAX 8
182 struct lttng_counter_dimension {
183 uint64_t size;
184 uint64_t underflow_index;
185 uint64_t overflow_index;
186 uint8_t has_underflow;
187 uint8_t has_overflow;
188 };
189
190 struct lttng_counter_ops {
191 struct lib_counter *(*counter_create)(size_t nr_dimensions,
192 const struct lttng_counter_dimension *dimensions,
193 int64_t global_sum_step,
194 int global_counter_fd,
195 int nr_counter_cpu_fds,
196 const int *counter_cpu_fds,
197 bool is_daemon);
198 void (*counter_destroy)(struct lib_counter *counter);
199 int (*counter_add)(struct lib_counter *counter,
200 const size_t *dimension_indexes, int64_t v);
201 int (*counter_read)(struct lib_counter *counter,
202 const size_t *dimension_indexes, int cpu,
203 int64_t *value, bool *overflow, bool *underflow);
204 int (*counter_aggregate)(struct lib_counter *counter,
205 const size_t *dimension_indexes, int64_t *value,
206 bool *overflow, bool *underflow);
207 int (*counter_clear)(struct lib_counter *counter, const size_t *dimension_indexes);
208 };
209
210 struct lttng_counter {
211 int objd;
212 struct lttng_event_notifier_group *event_notifier_group; /* owner */
213 struct lttng_counter_transport *transport;
214 struct lib_counter *counter;
215 struct lttng_counter_ops *ops;
216 };
217
218 #define LTTNG_UST_EVENT_HT_BITS 12
219 #define LTTNG_UST_EVENT_HT_SIZE (1U << LTTNG_UST_EVENT_HT_BITS)
220
221 struct lttng_ust_event_ht {
222 struct cds_hlist_head table[LTTNG_UST_EVENT_HT_SIZE];
223 };
224
225 #define LTTNG_UST_EVENT_NOTIFIER_HT_BITS 12
226 #define LTTNG_UST_EVENT_NOTIFIER_HT_SIZE (1U << LTTNG_UST_EVENT_NOTIFIER_HT_BITS)
227 struct lttng_ust_event_notifier_ht {
228 struct cds_hlist_head table[LTTNG_UST_EVENT_NOTIFIER_HT_SIZE];
229 };
230
231 #define LTTNG_UST_ENUM_HT_BITS 12
232 #define LTTNG_UST_ENUM_HT_SIZE (1U << LTTNG_UST_ENUM_HT_BITS)
233
234 struct lttng_ust_enum_ht {
235 struct cds_hlist_head table[LTTNG_UST_ENUM_HT_SIZE];
236 };
237
238 struct lttng_event_notifier_group {
239 int objd;
240 void *owner;
241 int notification_fd;
242 struct cds_list_head node; /* Event notifier group handle list */
243 struct cds_list_head enablers_head;
244 struct cds_list_head event_notifiers_head; /* list of event_notifiers */
245 struct lttng_ust_event_notifier_ht event_notifiers_ht; /* hashtable of event_notifiers */
246 struct lttng_ust_ctx *ctx; /* contexts for filters. */
247
248 struct lttng_counter *error_counter;
249 size_t error_counter_len;
250 };
251
252 struct lttng_transport {
253 const char *name;
254 struct cds_list_head node;
255 struct lttng_ust_channel_buffer_ops ops;
256 const struct lttng_ust_ring_buffer_config *client_config;
257 };
258
259 struct lttng_counter_transport {
260 const char *name;
261 struct cds_list_head node;
262 struct lttng_counter_ops ops;
263 const struct lib_counter_config *client_config;
264 };
265
266 struct lttng_ust_event_common_private {
267 struct lttng_ust_event_common *pub; /* Public event interface */
268
269 const struct lttng_ust_event_desc *desc;
270 /* Backward references: list of lttng_enabler_ref (ref to enablers) */
271 struct cds_list_head enablers_ref_head;
272 int registered; /* has reg'd tracepoint probe */
273 uint64_t user_token;
274
275 int has_enablers_without_filter_bytecode;
276 /* list of struct lttng_ust_bytecode_runtime, sorted by seqnum */
277 struct cds_list_head filter_bytecode_runtime_head;
278 };
279
280 struct lttng_ust_event_recorder_private {
281 struct lttng_ust_event_common_private parent;
282
283 struct lttng_ust_event_recorder *pub; /* Public event interface */
284 struct cds_list_head node; /* Event recorder list */
285 struct cds_hlist_node hlist; /* Hash table of event recorders */
286 struct lttng_ust_ctx *ctx;
287 unsigned int id;
288 };
289
290 struct lttng_ust_event_notifier_private {
291 struct lttng_ust_event_common_private parent;
292
293 struct lttng_ust_event_notifier *pub; /* Public event notifier interface */
294 struct lttng_event_notifier_group *group; /* weak ref */
295 size_t num_captures; /* Needed to allocate the msgpack array. */
296 uint64_t error_counter_index;
297 struct cds_list_head node; /* Event notifier list */
298 struct cds_hlist_node hlist; /* Hash table of event notifiers */
299 struct cds_list_head capture_bytecode_runtime_head;
300 };
301
302 struct lttng_ust_bytecode_runtime {
303 enum lttng_ust_bytecode_type type;
304 struct lttng_ust_bytecode_node *bc;
305 int link_failed;
306 int (*interpreter_func)(struct lttng_ust_bytecode_runtime *bytecode_runtime,
307 const char *interpreter_stack_data,
308 struct lttng_ust_probe_ctx *probe_ctx,
309 void *ctx);
310 struct cds_list_head node; /* list of bytecode runtime in event */
311 /*
312 * Pointer to a URCU-protected pointer owned by an `struct
313 * lttng_session`or `struct lttng_event_notifier_group`.
314 */
315 struct lttng_ust_ctx **pctx;
316 };
317
318 struct lttng_ust_session_private {
319 struct lttng_ust_session *pub; /* Public session interface */
320
321 int been_active; /* Been active ? */
322 int objd; /* Object associated */
323 struct cds_list_head chan_head; /* Channel list head */
324 struct cds_list_head events_head; /* list of events */
325 struct cds_list_head node; /* Session list */
326
327 /* List of enablers */
328 struct cds_list_head enablers_head;
329 struct lttng_ust_event_ht events_ht; /* ht of events */
330 void *owner; /* object owner */
331 int tstate:1; /* Transient enable state */
332
333 int statedump_pending:1;
334
335 struct lttng_ust_enum_ht enums_ht; /* ht of enumerations */
336 struct cds_list_head enums_head;
337 struct lttng_ust_ctx *ctx; /* contexts for filters. */
338
339 unsigned char uuid[LTTNG_UST_UUID_LEN]; /* Trace session unique ID */
340 bool uuid_set; /* Is uuid set ? */
341 };
342
343 struct lttng_enum {
344 const struct lttng_ust_enum_desc *desc;
345 struct lttng_ust_session *session;
346 struct cds_list_head node; /* Enum list in session */
347 struct cds_hlist_node hlist; /* Session ht of enums */
348 uint64_t id; /* Enumeration ID in sessiond */
349 };
350
351 struct lttng_ust_shm_handle;
352
353 struct lttng_ust_channel_buffer_ops_private {
354 struct lttng_ust_channel_buffer_ops *pub; /* Public channel buffer ops interface */
355
356 struct lttng_ust_channel_buffer *(*channel_create)(const char *name,
357 void *buf_addr,
358 size_t subbuf_size, size_t num_subbuf,
359 unsigned int switch_timer_interval,
360 unsigned int read_timer_interval,
361 unsigned char *uuid,
362 uint32_t chan_id,
363 const int *stream_fds, int nr_stream_fds,
364 int64_t blocking_timeout);
365 void (*channel_destroy)(struct lttng_ust_channel_buffer *chan);
366 /*
367 * packet_avail_size returns the available size in the current
368 * packet. Note that the size returned is only a hint, since it
369 * may change due to concurrent writes.
370 */
371 size_t (*packet_avail_size)(struct lttng_ust_channel_buffer *chan);
372 int (*is_finalized)(struct lttng_ust_channel_buffer *chan);
373 int (*is_disabled)(struct lttng_ust_channel_buffer *chan);
374 int (*flush_buffer)(struct lttng_ust_channel_buffer *chan);
375 };
376
377 struct lttng_ust_channel_common_private {
378 struct lttng_ust_channel_common *pub; /* Public channel interface */
379
380 int objd; /* Object associated with channel. */
381 int tstate:1; /* Transient enable state */
382 };
383
384 struct lttng_ust_channel_buffer_private {
385 struct lttng_ust_channel_common_private parent;
386
387 struct lttng_ust_channel_buffer *pub; /* Public channel buffer interface */
388 struct cds_list_head node; /* Channel list in session */
389 int header_type; /* 0: unset, 1: compact, 2: large */
390 unsigned int id; /* Channel ID */
391 enum lttng_ust_abi_chan_type type;
392 struct lttng_ust_ctx *ctx;
393 struct lttng_ust_ring_buffer_channel *rb_chan; /* Ring buffer channel */
394 unsigned char uuid[LTTNG_UST_UUID_LEN]; /* Trace session unique ID */
395 };
396
397 /*
398 * IMPORTANT: this structure is part of the ABI between the consumer
399 * daemon and the UST library within traced applications. Changing it
400 * breaks the UST communication protocol.
401 *
402 * TODO: remove unused fields on next UST communication protocol
403 * breaking update.
404 */
405 struct lttng_ust_abi_channel_config {
406 void *unused1;
407 int unused2;
408 void *unused3;
409 void *unused4;
410 int unused5;
411 struct cds_list_head unused6;
412 void *unused7;
413 int unused8;
414 void *unused9;
415
416 /* Channel ID */
417 unsigned int id;
418 enum lttng_ust_abi_chan_type unused10;
419 unsigned char uuid[LTTNG_UST_UUID_LEN]; /* Trace session unique ID */
420 int unused11:1;
421 };
422
423 /* Global (filter), event and channel contexts. */
424 struct lttng_ust_ctx {
425 struct lttng_ust_ctx_field *fields;
426 unsigned int nr_fields;
427 unsigned int allocated_fields;
428 unsigned int largest_align;
429 };
430
431 struct lttng_ust_registered_probe {
432 const struct lttng_ust_probe_desc *desc;
433
434 struct cds_list_head head; /* chain registered probes */
435 struct cds_list_head lazy_init_head;
436 int lazy; /* lazy registration */
437 };
438
439 /*
440 * Context field
441 */
442
443 struct lttng_ust_ctx_field {
444 const struct lttng_ust_event_field *event_field;
445 size_t (*get_size)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
446 size_t offset);
447 void (*record)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
448 struct lttng_ust_ring_buffer_ctx *ctx,
449 struct lttng_ust_channel_buffer *chan);
450 void (*get_value)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
451 struct lttng_ust_ctx_value *value);
452 void (*destroy)(void *priv);
453 void *priv;
454 };
455
456 static inline
457 const struct lttng_ust_type_integer *lttng_ust_get_type_integer(const struct lttng_ust_type_common *type)
458 {
459 if (type->type != lttng_ust_type_integer)
460 return NULL;
461 return caa_container_of(type, const struct lttng_ust_type_integer, parent);
462 }
463
464 static inline
465 const struct lttng_ust_type_float *lttng_ust_get_type_float(const struct lttng_ust_type_common *type)
466 {
467 if (type->type != lttng_ust_type_float)
468 return NULL;
469 return caa_container_of(type, const struct lttng_ust_type_float, parent);
470 }
471
472 static inline
473 const struct lttng_ust_type_string *lttng_ust_get_type_string(const struct lttng_ust_type_common *type)
474 {
475 if (type->type != lttng_ust_type_string)
476 return NULL;
477 return caa_container_of(type, const struct lttng_ust_type_string, parent);
478 }
479
480 static inline
481 const struct lttng_ust_type_enum *lttng_ust_get_type_enum(const struct lttng_ust_type_common *type)
482 {
483 if (type->type != lttng_ust_type_enum)
484 return NULL;
485 return caa_container_of(type, const struct lttng_ust_type_enum, parent);
486 }
487
488 static inline
489 const struct lttng_ust_type_array *lttng_ust_get_type_array(const struct lttng_ust_type_common *type)
490 {
491 if (type->type != lttng_ust_type_array)
492 return NULL;
493 return caa_container_of(type, const struct lttng_ust_type_array, parent);
494 }
495
496 static inline
497 const struct lttng_ust_type_sequence *lttng_ust_get_type_sequence(const struct lttng_ust_type_common *type)
498 {
499 if (type->type != lttng_ust_type_sequence)
500 return NULL;
501 return caa_container_of(type, const struct lttng_ust_type_sequence, parent);
502 }
503
504 static inline
505 const struct lttng_ust_type_struct *lttng_ust_get_type_struct(const struct lttng_ust_type_common *type)
506 {
507 if (type->type != lttng_ust_type_struct)
508 return NULL;
509 return caa_container_of(type, const struct lttng_ust_type_struct, parent);
510 }
511
512 #define lttng_ust_static_type_integer(_size, _alignment, _signedness, _byte_order, _base) \
513 ((const struct lttng_ust_type_common *) LTTNG_UST_COMPOUND_LITERAL(const struct lttng_ust_type_integer, { \
514 .parent = { \
515 .type = lttng_ust_type_integer, \
516 }, \
517 .struct_size = sizeof(struct lttng_ust_type_integer), \
518 .size = (_size), \
519 .alignment = (_alignment), \
520 .signedness = (_signedness), \
521 .reverse_byte_order = (_byte_order) != LTTNG_UST_BYTE_ORDER, \
522 .base = (_base), \
523 }))
524
525 #define lttng_ust_static_type_array_text(_length) \
526 ((const struct lttng_ust_type_common *) LTTNG_UST_COMPOUND_LITERAL(const struct lttng_ust_type_array, { \
527 .parent = { \
528 .type = lttng_ust_type_array, \
529 }, \
530 .struct_size = sizeof(struct lttng_ust_type_array), \
531 .length = (_length), \
532 .alignment = 0, \
533 .encoding = lttng_ust_string_encoding_UTF8, \
534 .elem_type = lttng_ust_static_type_integer(sizeof(char) * CHAR_BIT, \
535 lttng_ust_rb_alignof(char) * CHAR_BIT, lttng_ust_is_signed_type(char), \
536 LTTNG_UST_BYTE_ORDER, 10), \
537 }))
538
539 #define lttng_ust_static_event_field(_name, _type, _nowrite, _nofilter) \
540 LTTNG_UST_COMPOUND_LITERAL(const struct lttng_ust_event_field, { \
541 .struct_size = sizeof(struct lttng_ust_event_field), \
542 .name = (_name), \
543 .type = (_type), \
544 .nowrite = (_nowrite), \
545 .nofilter = (_nofilter), \
546 })
547
548 #define lttng_ust_static_ctx_field(_event_field, _get_size, _record, _get_value, _destroy, _priv) \
549 LTTNG_UST_COMPOUND_LITERAL(const struct lttng_ust_ctx_field, { \
550 .event_field = (_event_field), \
551 .get_size = (_get_size), \
552 .record = (_record), \
553 .get_value = (_get_value), \
554 .destroy = (_destroy), \
555 .priv = (_priv), \
556 })
557
558 static inline
559 struct lttng_enabler *lttng_event_enabler_as_enabler(
560 struct lttng_event_enabler *event_enabler)
561 {
562 return &event_enabler->base;
563 }
564
565 static inline
566 struct lttng_enabler *lttng_event_notifier_enabler_as_enabler(
567 struct lttng_event_notifier_enabler *event_notifier_enabler)
568 {
569 return &event_notifier_enabler->base;
570 }
571
572
573
574 /* This is ABI between liblttng-ust and liblttng-ust-dl */
575 void lttng_ust_dl_update(void *ip);
576
577 struct lttng_enum *lttng_ust_enum_get_from_desc(struct lttng_ust_session *session,
578 const struct lttng_ust_enum_desc *enum_desc)
579 __attribute__((visibility("hidden")));
580
581
582 #endif /* _UST_COMMON_UST_EVENTS_H */
This page took 0.04275 seconds and 4 git commands to generate.