API refactoring: introduce probe context
[lttng-ust.git] / src / lib / lttng-ust / lttng-events.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng per-session event registry.
7 */
8
9 #define _LGPL_SOURCE
10 #include <stdio.h>
11 #include <assert.h>
12 #include <errno.h>
13 #include <limits.h>
14 #include <pthread.h>
15 #include <sys/shm.h>
16 #include <sys/ipc.h>
17 #include <stdint.h>
18 #include <stddef.h>
19 #include <inttypes.h>
20 #include <time.h>
21 #include <stdbool.h>
22 #include <unistd.h>
23 #include <dlfcn.h>
24 #include <lttng/ust-endian.h>
25
26 #include <urcu/arch.h>
27 #include <urcu/compiler.h>
28 #include <urcu/hlist.h>
29 #include <urcu/list.h>
30 #include <urcu/uatomic.h>
31
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
34
35 #include "common/logging.h"
36 #include "common/macros.h"
37 #include <lttng/ust-ctl.h>
38 #include "common/ustcomm.h"
39 #include "common/ust-fd.h"
40 #include "common/dynamic-type.h"
41 #include "common/ust-context-provider.h"
42 #include "lttng-ust-uuid.h"
43
44 #include "common/tracepoint.h"
45 #include "common/strutils.h"
46 #include "lttng-bytecode.h"
47 #include "lttng-tracer.h"
48 #include "lttng-tracer-core.h"
49 #include "lttng-ust-statedump.h"
50 #include "context-internal.h"
51 #include "lib/lttng-ust/events.h"
52 #include "common/ringbuffer/shm.h"
53 #include "common/ringbuffer/frontend_types.h"
54 #include "common/ringbuffer/frontend.h"
55 #include "common/counter/counter.h"
56 #include "common/jhash.h"
57 #include <lttng/ust-abi.h>
58 #include "context-provider-internal.h"
59
60 /*
61 * All operations within this file are called by the communication
62 * thread, under ust_lock protection.
63 */
64
65 static CDS_LIST_HEAD(sessions);
66 static CDS_LIST_HEAD(event_notifier_groups);
67
68 struct cds_list_head *lttng_get_sessions(void)
69 {
70 return &sessions;
71 }
72
73 static void _lttng_event_destroy(struct lttng_ust_event_common *event);
74 static void _lttng_enum_destroy(struct lttng_enum *_enum);
75
76 static
77 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session *session);
78 static
79 void lttng_session_sync_event_enablers(struct lttng_ust_session *session);
80 static
81 void lttng_event_notifier_group_sync_enablers(
82 struct lttng_event_notifier_group *event_notifier_group);
83 static
84 void lttng_enabler_destroy(struct lttng_enabler *enabler);
85
86 bool lttng_ust_validate_event_name(const struct lttng_ust_event_desc *desc)
87 {
88 if (strlen(desc->probe_desc->provider_name) + 1 +
89 strlen(desc->event_name) >= LTTNG_UST_ABI_SYM_NAME_LEN)
90 return false;
91 return true;
92 }
93
94 void lttng_ust_format_event_name(const struct lttng_ust_event_desc *desc,
95 char *name)
96 {
97 strcpy(name, desc->probe_desc->provider_name);
98 strcat(name, ":");
99 strcat(name, desc->event_name);
100 }
101
102 /*
103 * Called with ust lock held.
104 */
105 int lttng_session_active(void)
106 {
107 struct lttng_ust_session_private *iter;
108
109 cds_list_for_each_entry(iter, &sessions, node) {
110 if (iter->pub->active)
111 return 1;
112 }
113 return 0;
114 }
115
116 static
117 int lttng_loglevel_match(int loglevel,
118 unsigned int has_loglevel,
119 enum lttng_ust_abi_loglevel_type req_type,
120 int req_loglevel)
121 {
122 if (!has_loglevel)
123 loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
124 switch (req_type) {
125 case LTTNG_UST_ABI_LOGLEVEL_RANGE:
126 if (loglevel <= req_loglevel
127 || (req_loglevel == -1 && loglevel <= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG))
128 return 1;
129 else
130 return 0;
131 case LTTNG_UST_ABI_LOGLEVEL_SINGLE:
132 if (loglevel == req_loglevel
133 || (req_loglevel == -1 && loglevel <= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG))
134 return 1;
135 else
136 return 0;
137 case LTTNG_UST_ABI_LOGLEVEL_ALL:
138 default:
139 if (loglevel <= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG)
140 return 1;
141 else
142 return 0;
143 }
144 }
145
146 struct lttng_ust_session *lttng_session_create(void)
147 {
148 struct lttng_ust_session *session;
149 struct lttng_ust_session_private *session_priv;
150 int i;
151
152 session = zmalloc(sizeof(struct lttng_ust_session));
153 if (!session)
154 return NULL;
155 session->struct_size = sizeof(struct lttng_ust_session);
156 session_priv = zmalloc(sizeof(struct lttng_ust_session_private));
157 if (!session_priv) {
158 free(session);
159 return NULL;
160 }
161 session->priv = session_priv;
162 session_priv->pub = session;
163 if (lttng_context_init_all(&session->priv->ctx)) {
164 free(session_priv);
165 free(session);
166 return NULL;
167 }
168 CDS_INIT_LIST_HEAD(&session->priv->chan_head);
169 CDS_INIT_LIST_HEAD(&session->priv->events_head);
170 CDS_INIT_LIST_HEAD(&session->priv->enums_head);
171 CDS_INIT_LIST_HEAD(&session->priv->enablers_head);
172 for (i = 0; i < LTTNG_UST_EVENT_HT_SIZE; i++)
173 CDS_INIT_HLIST_HEAD(&session->priv->events_ht.table[i]);
174 for (i = 0; i < LTTNG_UST_ENUM_HT_SIZE; i++)
175 CDS_INIT_HLIST_HEAD(&session->priv->enums_ht.table[i]);
176 cds_list_add(&session->priv->node, &sessions);
177 return session;
178 }
179
180 struct lttng_counter *lttng_ust_counter_create(
181 const char *counter_transport_name,
182 size_t number_dimensions, const struct lttng_counter_dimension *dimensions)
183 {
184 struct lttng_counter_transport *counter_transport = NULL;
185 struct lttng_counter *counter = NULL;
186
187 counter_transport = lttng_counter_transport_find(counter_transport_name);
188 if (!counter_transport)
189 goto notransport;
190 counter = zmalloc(sizeof(struct lttng_counter));
191 if (!counter)
192 goto nomem;
193
194 counter->ops = &counter_transport->ops;
195 counter->transport = counter_transport;
196
197 counter->counter = counter->ops->counter_create(
198 number_dimensions, dimensions, 0,
199 -1, 0, NULL, false);
200 if (!counter->counter) {
201 goto create_error;
202 }
203
204 return counter;
205
206 create_error:
207 free(counter);
208 nomem:
209 notransport:
210 return NULL;
211 }
212
213 static
214 void lttng_ust_counter_destroy(struct lttng_counter *counter)
215 {
216 counter->ops->counter_destroy(counter->counter);
217 free(counter);
218 }
219
220 struct lttng_event_notifier_group *lttng_event_notifier_group_create(void)
221 {
222 struct lttng_event_notifier_group *event_notifier_group;
223 int i;
224
225 event_notifier_group = zmalloc(sizeof(struct lttng_event_notifier_group));
226 if (!event_notifier_group)
227 return NULL;
228
229 /* Add all contexts. */
230 if (lttng_context_init_all(&event_notifier_group->ctx)) {
231 free(event_notifier_group);
232 return NULL;
233 }
234
235 CDS_INIT_LIST_HEAD(&event_notifier_group->enablers_head);
236 CDS_INIT_LIST_HEAD(&event_notifier_group->event_notifiers_head);
237 for (i = 0; i < LTTNG_UST_EVENT_NOTIFIER_HT_SIZE; i++)
238 CDS_INIT_HLIST_HEAD(&event_notifier_group->event_notifiers_ht.table[i]);
239
240 cds_list_add(&event_notifier_group->node, &event_notifier_groups);
241
242 return event_notifier_group;
243 }
244
245 /*
246 * Only used internally at session destruction.
247 */
248 static
249 void _lttng_channel_unmap(struct lttng_ust_channel_buffer *lttng_chan)
250 {
251 struct lttng_ust_ring_buffer_channel *chan;
252 struct lttng_ust_shm_handle *handle;
253
254 cds_list_del(&lttng_chan->priv->node);
255 lttng_destroy_context(lttng_chan->priv->ctx);
256 chan = lttng_chan->priv->rb_chan;
257 handle = chan->handle;
258 channel_destroy(chan, handle, 0);
259 free(lttng_chan->parent);
260 free(lttng_chan->priv);
261 free(lttng_chan);
262 }
263
264 static
265 void register_event(struct lttng_ust_event_common *event)
266 {
267 int ret;
268 const struct lttng_ust_event_desc *desc;
269
270 assert(event->priv->registered == 0);
271 desc = event->priv->desc;
272 ret = lttng_ust_tp_probe_register_queue_release(desc->probe_desc->provider_name,
273 desc->event_name,
274 desc->probe_callback,
275 event, desc->signature);
276 WARN_ON_ONCE(ret);
277 if (!ret)
278 event->priv->registered = 1;
279 }
280
281 static
282 void unregister_event(struct lttng_ust_event_common *event)
283 {
284 int ret;
285 const struct lttng_ust_event_desc *desc;
286
287 assert(event->priv->registered == 1);
288 desc = event->priv->desc;
289 ret = lttng_ust_tp_probe_unregister_queue_release(desc->probe_desc->provider_name,
290 desc->event_name,
291 desc->probe_callback,
292 event);
293 WARN_ON_ONCE(ret);
294 if (!ret)
295 event->priv->registered = 0;
296 }
297
298 static
299 void _lttng_event_unregister(struct lttng_ust_event_common *event)
300 {
301 if (event->priv->registered)
302 unregister_event(event);
303 }
304
305 void lttng_session_destroy(struct lttng_ust_session *session)
306 {
307 struct lttng_ust_channel_buffer_private *chan, *tmpchan;
308 struct lttng_ust_event_recorder_private *event_recorder_priv, *tmpevent_recorder_priv;
309 struct lttng_enum *_enum, *tmp_enum;
310 struct lttng_event_enabler *event_enabler, *event_tmpenabler;
311
312 CMM_ACCESS_ONCE(session->active) = 0;
313 cds_list_for_each_entry(event_recorder_priv, &session->priv->events_head, node) {
314 _lttng_event_unregister(event_recorder_priv->parent.pub);
315 }
316 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
317 lttng_ust_tp_probe_prune_release_queue();
318 cds_list_for_each_entry_safe(event_enabler, event_tmpenabler,
319 &session->priv->enablers_head, node)
320 lttng_event_enabler_destroy(event_enabler);
321 cds_list_for_each_entry_safe(event_recorder_priv, tmpevent_recorder_priv,
322 &session->priv->events_head, node)
323 _lttng_event_destroy(event_recorder_priv->parent.pub);
324 cds_list_for_each_entry_safe(_enum, tmp_enum,
325 &session->priv->enums_head, node)
326 _lttng_enum_destroy(_enum);
327 cds_list_for_each_entry_safe(chan, tmpchan, &session->priv->chan_head, node)
328 _lttng_channel_unmap(chan->pub);
329 cds_list_del(&session->priv->node);
330 lttng_destroy_context(session->priv->ctx);
331 free(session->priv);
332 free(session);
333 }
334
335 void lttng_event_notifier_group_destroy(
336 struct lttng_event_notifier_group *event_notifier_group)
337 {
338 int close_ret;
339 struct lttng_event_notifier_enabler *notifier_enabler, *tmpnotifier_enabler;
340 struct lttng_ust_event_notifier_private *event_notifier_priv, *tmpevent_notifier_priv;
341
342 if (!event_notifier_group) {
343 return;
344 }
345
346 cds_list_for_each_entry(event_notifier_priv,
347 &event_notifier_group->event_notifiers_head, node)
348 _lttng_event_unregister(event_notifier_priv->parent.pub);
349
350 lttng_ust_urcu_synchronize_rcu();
351
352 cds_list_for_each_entry_safe(notifier_enabler, tmpnotifier_enabler,
353 &event_notifier_group->enablers_head, node)
354 lttng_event_notifier_enabler_destroy(notifier_enabler);
355
356 cds_list_for_each_entry_safe(event_notifier_priv, tmpevent_notifier_priv,
357 &event_notifier_group->event_notifiers_head, node)
358 _lttng_event_destroy(event_notifier_priv->parent.pub);
359
360 if (event_notifier_group->error_counter)
361 lttng_ust_counter_destroy(event_notifier_group->error_counter);
362
363 /* Close the notification fd to the listener of event_notifiers. */
364
365 lttng_ust_lock_fd_tracker();
366 close_ret = close(event_notifier_group->notification_fd);
367 if (!close_ret) {
368 lttng_ust_delete_fd_from_tracker(
369 event_notifier_group->notification_fd);
370 } else {
371 PERROR("close");
372 abort();
373 }
374 lttng_ust_unlock_fd_tracker();
375
376 cds_list_del(&event_notifier_group->node);
377 lttng_destroy_context(event_notifier_group->ctx);
378 free(event_notifier_group);
379 }
380
381 static
382 void lttng_enabler_destroy(struct lttng_enabler *enabler)
383 {
384 struct lttng_ust_bytecode_node *filter_node, *tmp_filter_node;
385 struct lttng_ust_excluder_node *excluder_node, *tmp_excluder_node;
386
387 if (!enabler) {
388 return;
389 }
390
391 /* Destroy filter bytecode */
392 cds_list_for_each_entry_safe(filter_node, tmp_filter_node,
393 &enabler->filter_bytecode_head, node) {
394 free(filter_node);
395 }
396
397 /* Destroy excluders */
398 cds_list_for_each_entry_safe(excluder_node, tmp_excluder_node,
399 &enabler->excluder_head, node) {
400 free(excluder_node);
401 }
402 }
403
404 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler *event_notifier_enabler)
405 {
406 if (!event_notifier_enabler) {
407 return;
408 }
409
410 cds_list_del(&event_notifier_enabler->node);
411
412 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
413
414 free(event_notifier_enabler);
415 }
416
417 static
418 int lttng_enum_create(const struct lttng_ust_enum_desc *desc,
419 struct lttng_ust_session *session)
420 {
421 const char *enum_name = desc->name;
422 struct lttng_enum *_enum;
423 struct cds_hlist_head *head;
424 int ret = 0;
425 size_t name_len = strlen(enum_name);
426 uint32_t hash;
427 int notify_socket;
428
429 /* Check if this enum is already registered for this session. */
430 hash = jhash(enum_name, name_len, 0);
431 head = &session->priv->enums_ht.table[hash & (LTTNG_UST_ENUM_HT_SIZE - 1)];
432
433 _enum = lttng_ust_enum_get_from_desc(session, desc);
434 if (_enum) {
435 ret = -EEXIST;
436 goto exist;
437 }
438
439 notify_socket = lttng_get_notify_socket(session->priv->owner);
440 if (notify_socket < 0) {
441 ret = notify_socket;
442 goto socket_error;
443 }
444
445 _enum = zmalloc(sizeof(*_enum));
446 if (!_enum) {
447 ret = -ENOMEM;
448 goto cache_error;
449 }
450 _enum->session = session;
451 _enum->desc = desc;
452
453 ret = ustcomm_register_enum(notify_socket,
454 session->priv->objd,
455 enum_name,
456 desc->nr_entries,
457 desc->entries,
458 &_enum->id);
459 if (ret < 0) {
460 DBG("Error (%d) registering enumeration to sessiond", ret);
461 goto sessiond_register_error;
462 }
463 cds_list_add(&_enum->node, &session->priv->enums_head);
464 cds_hlist_add_head(&_enum->hlist, head);
465 return 0;
466
467 sessiond_register_error:
468 free(_enum);
469 cache_error:
470 socket_error:
471 exist:
472 return ret;
473 }
474
475 static
476 int lttng_create_enum_check(const struct lttng_ust_type_common *type,
477 struct lttng_ust_session *session)
478 {
479 switch (type->type) {
480 case lttng_ust_type_enum:
481 {
482 const struct lttng_ust_enum_desc *enum_desc;
483 int ret;
484
485 enum_desc = lttng_ust_get_type_enum(type)->desc;
486 ret = lttng_enum_create(enum_desc, session);
487 if (ret && ret != -EEXIST) {
488 DBG("Unable to create enum error: (%d)", ret);
489 return ret;
490 }
491 break;
492 }
493 case lttng_ust_type_dynamic:
494 {
495 const struct lttng_ust_event_field *tag_field_generic;
496 const struct lttng_ust_enum_desc *enum_desc;
497 int ret;
498
499 tag_field_generic = lttng_ust_dynamic_type_tag_field();
500 enum_desc = lttng_ust_get_type_enum(tag_field_generic->type)->desc;
501 ret = lttng_enum_create(enum_desc, session);
502 if (ret && ret != -EEXIST) {
503 DBG("Unable to create enum error: (%d)", ret);
504 return ret;
505 }
506 break;
507 }
508 default:
509 /* TODO: nested types when they become supported. */
510 break;
511 }
512 return 0;
513 }
514
515 static
516 int lttng_create_all_event_enums(size_t nr_fields,
517 const struct lttng_ust_event_field * const *event_fields,
518 struct lttng_ust_session *session)
519 {
520 size_t i;
521 int ret;
522
523 /* For each field, ensure enum is part of the session. */
524 for (i = 0; i < nr_fields; i++) {
525 const struct lttng_ust_type_common *type = event_fields[i]->type;
526
527 ret = lttng_create_enum_check(type, session);
528 if (ret)
529 return ret;
530 }
531 return 0;
532 }
533
534 static
535 int lttng_create_all_ctx_enums(size_t nr_fields,
536 struct lttng_ust_ctx_field *ctx_fields,
537 struct lttng_ust_session *session)
538 {
539 size_t i;
540 int ret;
541
542 /* For each field, ensure enum is part of the session. */
543 for (i = 0; i < nr_fields; i++) {
544 const struct lttng_ust_type_common *type = ctx_fields[i].event_field->type;
545
546 ret = lttng_create_enum_check(type, session);
547 if (ret)
548 return ret;
549 }
550 return 0;
551 }
552
553 /*
554 * Ensure that a state-dump will be performed for this session at the end
555 * of the current handle_message().
556 */
557 int lttng_session_statedump(struct lttng_ust_session *session)
558 {
559 session->priv->statedump_pending = 1;
560 lttng_ust_sockinfo_session_enabled(session->priv->owner);
561 return 0;
562 }
563
564 int lttng_session_enable(struct lttng_ust_session *session)
565 {
566 int ret = 0;
567 struct lttng_ust_channel_buffer_private *chan;
568 int notify_socket;
569
570 if (session->active) {
571 ret = -EBUSY;
572 goto end;
573 }
574
575 notify_socket = lttng_get_notify_socket(session->priv->owner);
576 if (notify_socket < 0)
577 return notify_socket;
578
579 /* Set transient enabler state to "enabled" */
580 session->priv->tstate = 1;
581
582 /* We need to sync enablers with session before activation. */
583 lttng_session_sync_event_enablers(session);
584
585 /*
586 * Snapshot the number of events per channel to know the type of header
587 * we need to use.
588 */
589 cds_list_for_each_entry(chan, &session->priv->chan_head, node) {
590 struct lttng_ust_ctx *ctx;
591 struct lttng_ust_ctx_field *fields = NULL;
592 size_t nr_fields = 0;
593 uint32_t chan_id;
594
595 /* don't change it if session stop/restart */
596 if (chan->header_type)
597 continue;
598 ctx = chan->ctx;
599 if (ctx) {
600 nr_fields = ctx->nr_fields;
601 fields = ctx->fields;
602 ret = lttng_create_all_ctx_enums(nr_fields, fields,
603 session);
604 if (ret < 0) {
605 DBG("Error (%d) adding enum to session", ret);
606 return ret;
607 }
608 }
609 ret = ustcomm_register_channel(notify_socket,
610 session,
611 session->priv->objd,
612 chan->parent.objd,
613 nr_fields,
614 fields,
615 &chan_id,
616 &chan->header_type);
617 if (ret) {
618 DBG("Error (%d) registering channel to sessiond", ret);
619 return ret;
620 }
621 if (chan_id != chan->id) {
622 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
623 chan_id, chan->id);
624 return -EINVAL;
625 }
626 }
627
628 /* Set atomically the state to "active" */
629 CMM_ACCESS_ONCE(session->active) = 1;
630 CMM_ACCESS_ONCE(session->priv->been_active) = 1;
631
632 ret = lttng_session_statedump(session);
633 if (ret)
634 return ret;
635 end:
636 return ret;
637 }
638
639 int lttng_session_disable(struct lttng_ust_session *session)
640 {
641 int ret = 0;
642
643 if (!session->active) {
644 ret = -EBUSY;
645 goto end;
646 }
647 /* Set atomically the state to "inactive" */
648 CMM_ACCESS_ONCE(session->active) = 0;
649
650 /* Set transient enabler state to "disabled" */
651 session->priv->tstate = 0;
652 lttng_session_sync_event_enablers(session);
653 end:
654 return ret;
655 }
656
657 int lttng_channel_enable(struct lttng_ust_channel_common *lttng_channel)
658 {
659 int ret = 0;
660
661 if (lttng_channel->enabled) {
662 ret = -EBUSY;
663 goto end;
664 }
665 /* Set transient enabler state to "enabled" */
666 lttng_channel->priv->tstate = 1;
667 lttng_session_sync_event_enablers(lttng_channel->session);
668 /* Set atomically the state to "enabled" */
669 CMM_ACCESS_ONCE(lttng_channel->enabled) = 1;
670 end:
671 return ret;
672 }
673
674 int lttng_channel_disable(struct lttng_ust_channel_common *lttng_channel)
675 {
676 int ret = 0;
677
678 if (!lttng_channel->enabled) {
679 ret = -EBUSY;
680 goto end;
681 }
682 /* Set atomically the state to "disabled" */
683 CMM_ACCESS_ONCE(lttng_channel->enabled) = 0;
684 /* Set transient enabler state to "enabled" */
685 lttng_channel->priv->tstate = 0;
686 lttng_session_sync_event_enablers(lttng_channel->session);
687 end:
688 return ret;
689 }
690
691 static inline
692 struct cds_hlist_head *borrow_hash_table_bucket(
693 struct cds_hlist_head *hash_table,
694 unsigned int hash_table_size,
695 const struct lttng_ust_event_desc *desc)
696 {
697 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
698 size_t name_len;
699 uint32_t hash;
700
701 lttng_ust_format_event_name(desc, name);
702 name_len = strlen(name);
703
704 hash = jhash(name, name_len, 0);
705 return &hash_table[hash & (hash_table_size - 1)];
706 }
707
708 /*
709 * Supports event creation while tracing session is active.
710 */
711 static
712 int lttng_event_recorder_create(const struct lttng_ust_event_desc *desc,
713 struct lttng_ust_channel_buffer *chan)
714 {
715 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
716 struct lttng_ust_event_recorder *event_recorder;
717 struct lttng_ust_event_recorder_private *event_recorder_priv;
718 struct lttng_ust_session *session = chan->parent->session;
719 struct cds_hlist_head *head;
720 int ret = 0;
721 int notify_socket, loglevel;
722 const char *uri;
723
724 head = borrow_hash_table_bucket(chan->parent->session->priv->events_ht.table,
725 LTTNG_UST_EVENT_HT_SIZE, desc);
726
727 notify_socket = lttng_get_notify_socket(session->priv->owner);
728 if (notify_socket < 0) {
729 ret = notify_socket;
730 goto socket_error;
731 }
732
733 ret = lttng_create_all_event_enums(desc->nr_fields, desc->fields,
734 session);
735 if (ret < 0) {
736 DBG("Error (%d) adding enum to session", ret);
737 goto create_enum_error;
738 }
739
740 /*
741 * Check if loglevel match. Refuse to connect event if not.
742 */
743 event_recorder = zmalloc(sizeof(struct lttng_ust_event_recorder));
744 if (!event_recorder) {
745 ret = -ENOMEM;
746 goto cache_error;
747 }
748 event_recorder->struct_size = sizeof(struct lttng_ust_event_recorder);
749
750 event_recorder->parent = zmalloc(sizeof(struct lttng_ust_event_common));
751 if (!event_recorder->parent) {
752 ret = -ENOMEM;
753 goto parent_error;
754 }
755 event_recorder->parent->struct_size = sizeof(struct lttng_ust_event_common);
756 event_recorder->parent->type = LTTNG_UST_EVENT_TYPE_RECORDER;
757 event_recorder->parent->child = event_recorder;
758
759 event_recorder_priv = zmalloc(sizeof(struct lttng_ust_event_recorder_private));
760 if (!event_recorder_priv) {
761 ret = -ENOMEM;
762 goto priv_error;
763 }
764 event_recorder->priv = event_recorder_priv;
765 event_recorder_priv->pub = event_recorder;
766 event_recorder->parent->priv = &event_recorder_priv->parent;
767 event_recorder_priv->parent.pub = event_recorder->parent;
768
769 event_recorder->chan = chan;
770
771 /* Event will be enabled by enabler sync. */
772 event_recorder->parent->run_filter = lttng_ust_interpret_event_filter;
773 event_recorder->parent->enabled = 0;
774 event_recorder->parent->priv->registered = 0;
775 CDS_INIT_LIST_HEAD(&event_recorder->parent->priv->filter_bytecode_runtime_head);
776 CDS_INIT_LIST_HEAD(&event_recorder->parent->priv->enablers_ref_head);
777 event_recorder->parent->priv->desc = desc;
778
779 if (desc->loglevel)
780 loglevel = *(*desc->loglevel);
781 else
782 loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
783 if (desc->model_emf_uri)
784 uri = *(desc->model_emf_uri);
785 else
786 uri = NULL;
787
788 lttng_ust_format_event_name(desc, name);
789
790 /* Fetch event ID from sessiond */
791 ret = ustcomm_register_event(notify_socket,
792 session,
793 session->priv->objd,
794 chan->priv->parent.objd,
795 name,
796 loglevel,
797 desc->signature,
798 desc->nr_fields,
799 desc->fields,
800 uri,
801 &event_recorder->priv->id);
802 if (ret < 0) {
803 DBG("Error (%d) registering event to sessiond", ret);
804 goto sessiond_register_error;
805 }
806
807 cds_list_add(&event_recorder_priv->node, &chan->parent->session->priv->events_head);
808 cds_hlist_add_head(&event_recorder_priv->hlist, head);
809 return 0;
810
811 sessiond_register_error:
812 free(event_recorder_priv);
813 priv_error:
814 free(event_recorder->parent);
815 parent_error:
816 free(event_recorder);
817 cache_error:
818 create_enum_error:
819 socket_error:
820 return ret;
821 }
822
823 static
824 int lttng_event_notifier_create(const struct lttng_ust_event_desc *desc,
825 uint64_t token, uint64_t error_counter_index,
826 struct lttng_event_notifier_group *event_notifier_group)
827 {
828 struct lttng_ust_event_notifier *event_notifier;
829 struct lttng_ust_event_notifier_private *event_notifier_priv;
830 struct cds_hlist_head *head;
831 int ret = 0;
832
833 /*
834 * Get the hashtable bucket the created lttng_event_notifier object
835 * should be inserted.
836 */
837 head = borrow_hash_table_bucket(
838 event_notifier_group->event_notifiers_ht.table,
839 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, desc);
840
841 event_notifier = zmalloc(sizeof(struct lttng_ust_event_notifier));
842 if (!event_notifier) {
843 ret = -ENOMEM;
844 goto error;
845 }
846 event_notifier->struct_size = sizeof(struct lttng_ust_event_notifier);
847
848 event_notifier->parent = zmalloc(sizeof(struct lttng_ust_event_common));
849 if (!event_notifier->parent) {
850 ret = -ENOMEM;
851 goto parent_error;
852 }
853 event_notifier->parent->struct_size = sizeof(struct lttng_ust_event_common);
854 event_notifier->parent->type = LTTNG_UST_EVENT_TYPE_NOTIFIER;
855 event_notifier->parent->child = event_notifier;
856
857 event_notifier_priv = zmalloc(sizeof(struct lttng_ust_event_notifier_private));
858 if (!event_notifier_priv) {
859 ret = -ENOMEM;
860 goto priv_error;
861 }
862 event_notifier->priv = event_notifier_priv;
863 event_notifier_priv->pub = event_notifier;
864 event_notifier->parent->priv = &event_notifier_priv->parent;
865 event_notifier_priv->parent.pub = event_notifier->parent;
866
867 event_notifier_priv->group = event_notifier_group;
868 event_notifier_priv->parent.user_token = token;
869 event_notifier_priv->error_counter_index = error_counter_index;
870
871 /* Event notifier will be enabled by enabler sync. */
872 event_notifier->parent->run_filter = lttng_ust_interpret_event_filter;
873 event_notifier->parent->enabled = 0;
874 event_notifier_priv->parent.registered = 0;
875
876 CDS_INIT_LIST_HEAD(&event_notifier->parent->priv->filter_bytecode_runtime_head);
877 CDS_INIT_LIST_HEAD(&event_notifier->priv->capture_bytecode_runtime_head);
878 CDS_INIT_LIST_HEAD(&event_notifier_priv->parent.enablers_ref_head);
879 event_notifier_priv->parent.desc = desc;
880 event_notifier->notification_send = lttng_event_notifier_notification_send;
881
882 cds_list_add(&event_notifier_priv->node,
883 &event_notifier_group->event_notifiers_head);
884 cds_hlist_add_head(&event_notifier_priv->hlist, head);
885
886 return 0;
887
888 priv_error:
889 free(event_notifier->parent);
890 parent_error:
891 free(event_notifier);
892 error:
893 return ret;
894 }
895
896 static
897 int lttng_desc_match_star_glob_enabler(const struct lttng_ust_event_desc *desc,
898 struct lttng_enabler *enabler)
899 {
900 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
901 int loglevel = 0;
902 unsigned int has_loglevel = 0;
903
904 lttng_ust_format_event_name(desc, name);
905 assert(enabler->format_type == LTTNG_ENABLER_FORMAT_STAR_GLOB);
906 if (!strutils_star_glob_match(enabler->event_param.name, SIZE_MAX,
907 name, SIZE_MAX))
908 return 0;
909 if (desc->loglevel) {
910 loglevel = *(*desc->loglevel);
911 has_loglevel = 1;
912 }
913 if (!lttng_loglevel_match(loglevel,
914 has_loglevel,
915 enabler->event_param.loglevel_type,
916 enabler->event_param.loglevel))
917 return 0;
918 return 1;
919 }
920
921 static
922 int lttng_desc_match_event_enabler(const struct lttng_ust_event_desc *desc,
923 struct lttng_enabler *enabler)
924 {
925 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
926 int loglevel = 0;
927 unsigned int has_loglevel = 0;
928
929 lttng_ust_format_event_name(desc, name);
930 assert(enabler->format_type == LTTNG_ENABLER_FORMAT_EVENT);
931 if (strcmp(name, enabler->event_param.name))
932 return 0;
933 if (desc->loglevel) {
934 loglevel = *(*desc->loglevel);
935 has_loglevel = 1;
936 }
937 if (!lttng_loglevel_match(loglevel,
938 has_loglevel,
939 enabler->event_param.loglevel_type,
940 enabler->event_param.loglevel))
941 return 0;
942 return 1;
943 }
944
945 static
946 int lttng_desc_match_enabler(const struct lttng_ust_event_desc *desc,
947 struct lttng_enabler *enabler)
948 {
949 switch (enabler->format_type) {
950 case LTTNG_ENABLER_FORMAT_STAR_GLOB:
951 {
952 struct lttng_ust_excluder_node *excluder;
953
954 if (!lttng_desc_match_star_glob_enabler(desc, enabler)) {
955 return 0;
956 }
957
958 /*
959 * If the matching event matches with an excluder,
960 * return 'does not match'
961 */
962 cds_list_for_each_entry(excluder, &enabler->excluder_head, node) {
963 int count;
964
965 for (count = 0; count < excluder->excluder.count; count++) {
966 int len;
967 char *excluder_name;
968
969 excluder_name = (char *) (excluder->excluder.names)
970 + count * LTTNG_UST_ABI_SYM_NAME_LEN;
971 len = strnlen(excluder_name, LTTNG_UST_ABI_SYM_NAME_LEN);
972 if (len > 0) {
973 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
974
975 lttng_ust_format_event_name(desc, name);
976 if (strutils_star_glob_match(excluder_name, len, name, SIZE_MAX)) {
977 return 0;
978 }
979 }
980 }
981 }
982 return 1;
983 }
984 case LTTNG_ENABLER_FORMAT_EVENT:
985 return lttng_desc_match_event_enabler(desc, enabler);
986 default:
987 return -EINVAL;
988 }
989 }
990
991 static
992 int lttng_event_enabler_match_event(struct lttng_event_enabler *event_enabler,
993 struct lttng_ust_event_recorder *event_recorder)
994 {
995 if (lttng_desc_match_enabler(event_recorder->parent->priv->desc,
996 lttng_event_enabler_as_enabler(event_enabler))
997 && event_recorder->chan == event_enabler->chan)
998 return 1;
999 else
1000 return 0;
1001 }
1002
1003 static
1004 int lttng_event_notifier_enabler_match_event_notifier(
1005 struct lttng_event_notifier_enabler *event_notifier_enabler,
1006 struct lttng_ust_event_notifier *event_notifier)
1007 {
1008 int desc_matches = lttng_desc_match_enabler(event_notifier->priv->parent.desc,
1009 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
1010
1011 if (desc_matches && event_notifier->priv->group == event_notifier_enabler->group &&
1012 event_notifier->priv->parent.user_token == event_notifier_enabler->user_token)
1013 return 1;
1014 else
1015 return 0;
1016 }
1017
1018 static
1019 struct lttng_enabler_ref *lttng_enabler_ref(
1020 struct cds_list_head *enabler_ref_list,
1021 struct lttng_enabler *enabler)
1022 {
1023 struct lttng_enabler_ref *enabler_ref;
1024
1025 cds_list_for_each_entry(enabler_ref, enabler_ref_list, node) {
1026 if (enabler_ref->ref == enabler)
1027 return enabler_ref;
1028 }
1029 return NULL;
1030 }
1031
1032 /*
1033 * Create struct lttng_event if it is missing and present in the list of
1034 * tracepoint probes.
1035 */
1036 static
1037 void lttng_create_event_recorder_if_missing(struct lttng_event_enabler *event_enabler)
1038 {
1039 struct lttng_ust_session *session = event_enabler->chan->parent->session;
1040 struct lttng_ust_registered_probe *reg_probe;
1041 const struct lttng_ust_event_desc *desc;
1042 struct lttng_ust_event_recorder_private *event_recorder_priv;
1043 int i;
1044 struct cds_list_head *probe_list;
1045
1046 probe_list = lttng_get_probe_list_head();
1047 /*
1048 * For each probe event, if we find that a probe event matches
1049 * our enabler, create an associated lttng_event if not
1050 * already present.
1051 */
1052 cds_list_for_each_entry(reg_probe, probe_list, head) {
1053 const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc;
1054
1055 for (i = 0; i < probe_desc->nr_events; i++) {
1056 int ret;
1057 bool found = false;
1058 struct cds_hlist_head *head;
1059 struct cds_hlist_node *node;
1060
1061 desc = probe_desc->event_desc[i];
1062 if (!lttng_desc_match_enabler(desc,
1063 lttng_event_enabler_as_enabler(event_enabler)))
1064 continue;
1065
1066 head = borrow_hash_table_bucket(
1067 session->priv->events_ht.table,
1068 LTTNG_UST_EVENT_HT_SIZE, desc);
1069
1070 cds_hlist_for_each_entry(event_recorder_priv, node, head, hlist) {
1071 if (event_recorder_priv->parent.desc == desc
1072 && event_recorder_priv->pub->chan == event_enabler->chan) {
1073 found = true;
1074 break;
1075 }
1076 }
1077 if (found)
1078 continue;
1079
1080 /*
1081 * We need to create an event for this
1082 * event probe.
1083 */
1084 ret = lttng_event_recorder_create(probe_desc->event_desc[i],
1085 event_enabler->chan);
1086 if (ret) {
1087 DBG("Unable to create event \"%s:%s\", error %d\n",
1088 probe_desc->provider_name,
1089 probe_desc->event_desc[i]->event_name, ret);
1090 }
1091 }
1092 }
1093 }
1094
1095 static
1096 void probe_provider_event_for_each(const struct lttng_ust_probe_desc *provider_desc,
1097 void (*event_func)(struct lttng_ust_event_common *event))
1098 {
1099 struct cds_hlist_node *node, *tmp_node;
1100 struct cds_list_head *sessionsp;
1101 unsigned int i;
1102
1103 /* Get handle on list of sessions. */
1104 sessionsp = lttng_get_sessions();
1105
1106 /*
1107 * Iterate over all events in the probe provider descriptions and
1108 * sessions to queue the unregistration of the events.
1109 */
1110 for (i = 0; i < provider_desc->nr_events; i++) {
1111 const struct lttng_ust_event_desc *event_desc;
1112 struct lttng_event_notifier_group *event_notifier_group;
1113 struct lttng_ust_event_recorder_private *event_recorder_priv;
1114 struct lttng_ust_event_notifier_private *event_notifier_priv;
1115 struct lttng_ust_session_private *session_priv;
1116 struct cds_hlist_head *head;
1117
1118 event_desc = provider_desc->event_desc[i];
1119
1120 /*
1121 * Iterate over all session to find the current event
1122 * description.
1123 */
1124 cds_list_for_each_entry(session_priv, sessionsp, node) {
1125 /*
1126 * Get the list of events in the hashtable bucket and
1127 * iterate to find the event matching this descriptor.
1128 */
1129 head = borrow_hash_table_bucket(
1130 session_priv->events_ht.table,
1131 LTTNG_UST_EVENT_HT_SIZE, event_desc);
1132
1133 cds_hlist_for_each_entry_safe(event_recorder_priv, node, tmp_node, head, hlist) {
1134 if (event_desc == event_recorder_priv->parent.desc) {
1135 event_func(event_recorder_priv->parent.pub);
1136 break;
1137 }
1138 }
1139 }
1140
1141 /*
1142 * Iterate over all event_notifier groups to find the current event
1143 * description.
1144 */
1145 cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) {
1146 /*
1147 * Get the list of event_notifiers in the hashtable bucket and
1148 * iterate to find the event_notifier matching this
1149 * descriptor.
1150 */
1151 head = borrow_hash_table_bucket(
1152 event_notifier_group->event_notifiers_ht.table,
1153 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, event_desc);
1154
1155 cds_hlist_for_each_entry_safe(event_notifier_priv, node, tmp_node, head, hlist) {
1156 if (event_desc == event_notifier_priv->parent.desc) {
1157 event_func(event_notifier_priv->parent.pub);
1158 break;
1159 }
1160 }
1161 }
1162 }
1163 }
1164
1165 static
1166 void _event_enum_destroy(struct lttng_ust_event_common *event)
1167 {
1168
1169 switch (event->type) {
1170 case LTTNG_UST_EVENT_TYPE_RECORDER:
1171 {
1172 struct lttng_ust_event_recorder *event_recorder = event->child;
1173 struct lttng_ust_session *session = event_recorder->chan->parent->session;
1174 unsigned int i;
1175
1176 /* Destroy enums of the current event. */
1177 for (i = 0; i < event_recorder->parent->priv->desc->nr_fields; i++) {
1178 const struct lttng_ust_enum_desc *enum_desc;
1179 const struct lttng_ust_event_field *field;
1180 struct lttng_enum *curr_enum;
1181
1182 field = event_recorder->parent->priv->desc->fields[i];
1183 switch (field->type->type) {
1184 case lttng_ust_type_enum:
1185 enum_desc = lttng_ust_get_type_enum(field->type)->desc;
1186 break;
1187 default:
1188 continue;
1189 }
1190
1191 curr_enum = lttng_ust_enum_get_from_desc(session, enum_desc);
1192 if (curr_enum) {
1193 _lttng_enum_destroy(curr_enum);
1194 }
1195 }
1196 break;
1197 }
1198 case LTTNG_UST_EVENT_TYPE_NOTIFIER:
1199 break;
1200 default:
1201 abort();
1202 }
1203 /* Destroy event. */
1204 _lttng_event_destroy(event);
1205 }
1206
1207 /*
1208 * Iterate over all the UST sessions to unregister and destroy all probes from
1209 * the probe provider descriptor received as argument. Must me called with the
1210 * ust_lock held.
1211 */
1212 void lttng_probe_provider_unregister_events(
1213 const struct lttng_ust_probe_desc *provider_desc)
1214 {
1215 /*
1216 * Iterate over all events in the probe provider descriptions and sessions
1217 * to queue the unregistration of the events.
1218 */
1219 probe_provider_event_for_each(provider_desc, _lttng_event_unregister);
1220
1221 /* Wait for grace period. */
1222 lttng_ust_urcu_synchronize_rcu();
1223 /* Prune the unregistration queue. */
1224 lttng_ust_tp_probe_prune_release_queue();
1225
1226 /*
1227 * It is now safe to destroy the events and remove them from the event list
1228 * and hashtables.
1229 */
1230 probe_provider_event_for_each(provider_desc, _event_enum_destroy);
1231 }
1232
1233 /*
1234 * Create events associated with an event enabler (if not already present),
1235 * and add backward reference from the event to the enabler.
1236 */
1237 static
1238 int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler *event_enabler)
1239 {
1240 struct lttng_ust_session *session = event_enabler->chan->parent->session;
1241 struct lttng_ust_event_recorder_private *event_recorder_priv;
1242
1243 if (!lttng_event_enabler_as_enabler(event_enabler)->enabled)
1244 goto end;
1245
1246 /* First ensure that probe events are created for this enabler. */
1247 lttng_create_event_recorder_if_missing(event_enabler);
1248
1249 /* For each event matching enabler in session event list. */
1250 cds_list_for_each_entry(event_recorder_priv, &session->priv->events_head, node) {
1251 struct lttng_enabler_ref *enabler_ref;
1252
1253 if (!lttng_event_enabler_match_event(event_enabler, event_recorder_priv->pub))
1254 continue;
1255
1256 enabler_ref = lttng_enabler_ref(&event_recorder_priv->parent.enablers_ref_head,
1257 lttng_event_enabler_as_enabler(event_enabler));
1258 if (!enabler_ref) {
1259 /*
1260 * If no backward ref, create it.
1261 * Add backward ref from event to enabler.
1262 */
1263 enabler_ref = zmalloc(sizeof(*enabler_ref));
1264 if (!enabler_ref)
1265 return -ENOMEM;
1266 enabler_ref->ref = lttng_event_enabler_as_enabler(
1267 event_enabler);
1268 cds_list_add(&enabler_ref->node,
1269 &event_recorder_priv->parent.enablers_ref_head);
1270 }
1271
1272 /*
1273 * Link filter bytecodes if not linked yet.
1274 */
1275 lttng_enabler_link_bytecode(event_recorder_priv->parent.desc,
1276 &session->priv->ctx,
1277 &event_recorder_priv->parent.filter_bytecode_runtime_head,
1278 &lttng_event_enabler_as_enabler(event_enabler)->filter_bytecode_head);
1279
1280 /* TODO: merge event context. */
1281 }
1282 end:
1283 return 0;
1284 }
1285
1286 /*
1287 * Called at library load: connect the probe on all enablers matching
1288 * this event.
1289 * Called with session mutex held.
1290 */
1291 int lttng_fix_pending_events(void)
1292 {
1293 struct lttng_ust_session_private *session_priv;
1294
1295 cds_list_for_each_entry(session_priv, &sessions, node) {
1296 lttng_session_lazy_sync_event_enablers(session_priv->pub);
1297 }
1298 return 0;
1299 }
1300
1301 int lttng_fix_pending_event_notifiers(void)
1302 {
1303 struct lttng_event_notifier_group *event_notifier_group;
1304
1305 cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) {
1306 lttng_event_notifier_group_sync_enablers(event_notifier_group);
1307 }
1308 return 0;
1309 }
1310
1311 /*
1312 * For each session of the owner thread, execute pending statedump.
1313 * Only dump state for the sessions owned by the caller thread, because
1314 * we don't keep ust_lock across the entire iteration.
1315 */
1316 void lttng_handle_pending_statedump(void *owner)
1317 {
1318 struct lttng_ust_session_private *session_priv;
1319
1320 /* Execute state dump */
1321 do_lttng_ust_statedump(owner);
1322
1323 /* Clear pending state dump */
1324 if (ust_lock()) {
1325 goto end;
1326 }
1327 cds_list_for_each_entry(session_priv, &sessions, node) {
1328 if (session_priv->owner != owner)
1329 continue;
1330 if (!session_priv->statedump_pending)
1331 continue;
1332 session_priv->statedump_pending = 0;
1333 }
1334 end:
1335 ust_unlock();
1336 return;
1337 }
1338
1339 static
1340 void _lttng_event_destroy(struct lttng_ust_event_common *event)
1341 {
1342 struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref;
1343
1344 lttng_free_event_filter_runtime(event);
1345 /* Free event enabler refs */
1346 cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref,
1347 &event->priv->enablers_ref_head, node)
1348 free(enabler_ref);
1349
1350 switch (event->type) {
1351 case LTTNG_UST_EVENT_TYPE_RECORDER:
1352 {
1353 struct lttng_ust_event_recorder *event_recorder = event->child;
1354
1355 /* Remove from event list. */
1356 cds_list_del(&event_recorder->priv->node);
1357 /* Remove from event hash table. */
1358 cds_hlist_del(&event_recorder->priv->hlist);
1359
1360 lttng_destroy_context(event_recorder->priv->ctx);
1361 free(event_recorder->parent);
1362 free(event_recorder->priv);
1363 free(event_recorder);
1364 break;
1365 }
1366 case LTTNG_UST_EVENT_TYPE_NOTIFIER:
1367 {
1368 struct lttng_ust_event_notifier *event_notifier = event->child;
1369
1370 /* Remove from event list. */
1371 cds_list_del(&event_notifier->priv->node);
1372 /* Remove from event hash table. */
1373 cds_hlist_del(&event_notifier->priv->hlist);
1374
1375 free(event_notifier->priv);
1376 free(event_notifier->parent);
1377 free(event_notifier);
1378 break;
1379 }
1380 default:
1381 abort();
1382 }
1383 }
1384
1385 static
1386 void _lttng_enum_destroy(struct lttng_enum *_enum)
1387 {
1388 cds_list_del(&_enum->node);
1389 cds_hlist_del(&_enum->hlist);
1390 free(_enum);
1391 }
1392
1393 void lttng_ust_abi_events_exit(void)
1394 {
1395 struct lttng_ust_session_private *session_priv, *tmpsession_priv;
1396
1397 cds_list_for_each_entry_safe(session_priv, tmpsession_priv, &sessions, node)
1398 lttng_session_destroy(session_priv->pub);
1399 }
1400
1401 /*
1402 * Enabler management.
1403 */
1404 struct lttng_event_enabler *lttng_event_enabler_create(
1405 enum lttng_enabler_format_type format_type,
1406 struct lttng_ust_abi_event *event_param,
1407 struct lttng_ust_channel_buffer *chan)
1408 {
1409 struct lttng_event_enabler *event_enabler;
1410
1411 event_enabler = zmalloc(sizeof(*event_enabler));
1412 if (!event_enabler)
1413 return NULL;
1414 event_enabler->base.format_type = format_type;
1415 CDS_INIT_LIST_HEAD(&event_enabler->base.filter_bytecode_head);
1416 CDS_INIT_LIST_HEAD(&event_enabler->base.excluder_head);
1417 memcpy(&event_enabler->base.event_param, event_param,
1418 sizeof(event_enabler->base.event_param));
1419 event_enabler->chan = chan;
1420 /* ctx left NULL */
1421 event_enabler->base.enabled = 0;
1422 cds_list_add(&event_enabler->node, &event_enabler->chan->parent->session->priv->enablers_head);
1423 lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session);
1424
1425 return event_enabler;
1426 }
1427
1428 struct lttng_event_notifier_enabler *lttng_event_notifier_enabler_create(
1429 struct lttng_event_notifier_group *event_notifier_group,
1430 enum lttng_enabler_format_type format_type,
1431 struct lttng_ust_abi_event_notifier *event_notifier_param)
1432 {
1433 struct lttng_event_notifier_enabler *event_notifier_enabler;
1434
1435 event_notifier_enabler = zmalloc(sizeof(*event_notifier_enabler));
1436 if (!event_notifier_enabler)
1437 return NULL;
1438 event_notifier_enabler->base.format_type = format_type;
1439 CDS_INIT_LIST_HEAD(&event_notifier_enabler->base.filter_bytecode_head);
1440 CDS_INIT_LIST_HEAD(&event_notifier_enabler->capture_bytecode_head);
1441 CDS_INIT_LIST_HEAD(&event_notifier_enabler->base.excluder_head);
1442
1443 event_notifier_enabler->user_token = event_notifier_param->event.token;
1444 event_notifier_enabler->error_counter_index = event_notifier_param->error_counter_index;
1445 event_notifier_enabler->num_captures = 0;
1446
1447 memcpy(&event_notifier_enabler->base.event_param.name,
1448 event_notifier_param->event.name,
1449 sizeof(event_notifier_enabler->base.event_param.name));
1450 event_notifier_enabler->base.event_param.instrumentation =
1451 event_notifier_param->event.instrumentation;
1452 event_notifier_enabler->base.event_param.loglevel =
1453 event_notifier_param->event.loglevel;
1454 event_notifier_enabler->base.event_param.loglevel_type =
1455 event_notifier_param->event.loglevel_type;
1456
1457 event_notifier_enabler->base.enabled = 0;
1458 event_notifier_enabler->group = event_notifier_group;
1459
1460 cds_list_add(&event_notifier_enabler->node,
1461 &event_notifier_group->enablers_head);
1462
1463 lttng_event_notifier_group_sync_enablers(event_notifier_group);
1464
1465 return event_notifier_enabler;
1466 }
1467
1468 int lttng_event_enabler_enable(struct lttng_event_enabler *event_enabler)
1469 {
1470 lttng_event_enabler_as_enabler(event_enabler)->enabled = 1;
1471 lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session);
1472
1473 return 0;
1474 }
1475
1476 int lttng_event_enabler_disable(struct lttng_event_enabler *event_enabler)
1477 {
1478 lttng_event_enabler_as_enabler(event_enabler)->enabled = 0;
1479 lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session);
1480
1481 return 0;
1482 }
1483
1484 static
1485 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler *enabler,
1486 struct lttng_ust_bytecode_node **bytecode)
1487 {
1488 (*bytecode)->enabler = enabler;
1489 cds_list_add_tail(&(*bytecode)->node, &enabler->filter_bytecode_head);
1490 /* Take ownership of bytecode */
1491 *bytecode = NULL;
1492 }
1493
1494 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler *event_enabler,
1495 struct lttng_ust_bytecode_node **bytecode)
1496 {
1497 _lttng_enabler_attach_filter_bytecode(
1498 lttng_event_enabler_as_enabler(event_enabler), bytecode);
1499
1500 lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session);
1501 return 0;
1502 }
1503
1504 static
1505 void _lttng_enabler_attach_exclusion(struct lttng_enabler *enabler,
1506 struct lttng_ust_excluder_node **excluder)
1507 {
1508 (*excluder)->enabler = enabler;
1509 cds_list_add_tail(&(*excluder)->node, &enabler->excluder_head);
1510 /* Take ownership of excluder */
1511 *excluder = NULL;
1512 }
1513
1514 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler *event_enabler,
1515 struct lttng_ust_excluder_node **excluder)
1516 {
1517 _lttng_enabler_attach_exclusion(
1518 lttng_event_enabler_as_enabler(event_enabler), excluder);
1519
1520 lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session);
1521 return 0;
1522 }
1523
1524 int lttng_event_notifier_enabler_enable(
1525 struct lttng_event_notifier_enabler *event_notifier_enabler)
1526 {
1527 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 1;
1528 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1529
1530 return 0;
1531 }
1532
1533 int lttng_event_notifier_enabler_disable(
1534 struct lttng_event_notifier_enabler *event_notifier_enabler)
1535 {
1536 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 0;
1537 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1538
1539 return 0;
1540 }
1541
1542 int lttng_event_notifier_enabler_attach_filter_bytecode(
1543 struct lttng_event_notifier_enabler *event_notifier_enabler,
1544 struct lttng_ust_bytecode_node **bytecode)
1545 {
1546 _lttng_enabler_attach_filter_bytecode(
1547 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler),
1548 bytecode);
1549
1550 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1551 return 0;
1552 }
1553
1554 int lttng_event_notifier_enabler_attach_capture_bytecode(
1555 struct lttng_event_notifier_enabler *event_notifier_enabler,
1556 struct lttng_ust_bytecode_node **bytecode)
1557 {
1558 (*bytecode)->enabler = lttng_event_notifier_enabler_as_enabler(
1559 event_notifier_enabler);
1560 cds_list_add_tail(&(*bytecode)->node,
1561 &event_notifier_enabler->capture_bytecode_head);
1562 /* Take ownership of bytecode */
1563 *bytecode = NULL;
1564 event_notifier_enabler->num_captures++;
1565
1566 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1567 return 0;
1568 }
1569
1570 int lttng_event_notifier_enabler_attach_exclusion(
1571 struct lttng_event_notifier_enabler *event_notifier_enabler,
1572 struct lttng_ust_excluder_node **excluder)
1573 {
1574 _lttng_enabler_attach_exclusion(
1575 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler),
1576 excluder);
1577
1578 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1579 return 0;
1580 }
1581
1582 int lttng_attach_context(struct lttng_ust_abi_context *context_param,
1583 union lttng_ust_abi_args *uargs,
1584 struct lttng_ust_ctx **ctx, struct lttng_ust_session *session)
1585 {
1586 /*
1587 * We cannot attach a context after trace has been started for a
1588 * session because the metadata does not allow expressing this
1589 * information outside of the original channel scope.
1590 */
1591 if (session->priv->been_active)
1592 return -EPERM;
1593
1594 switch (context_param->ctx) {
1595 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID:
1596 return lttng_add_pthread_id_to_ctx(ctx);
1597 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
1598 {
1599 struct lttng_ust_abi_perf_counter_ctx *perf_ctx_param;
1600
1601 perf_ctx_param = &context_param->u.perf_counter;
1602 return lttng_add_perf_counter_to_ctx(
1603 perf_ctx_param->type,
1604 perf_ctx_param->config,
1605 perf_ctx_param->name,
1606 ctx);
1607 }
1608 case LTTNG_UST_ABI_CONTEXT_VTID:
1609 return lttng_add_vtid_to_ctx(ctx);
1610 case LTTNG_UST_ABI_CONTEXT_VPID:
1611 return lttng_add_vpid_to_ctx(ctx);
1612 case LTTNG_UST_ABI_CONTEXT_PROCNAME:
1613 return lttng_add_procname_to_ctx(ctx);
1614 case LTTNG_UST_ABI_CONTEXT_IP:
1615 return lttng_add_ip_to_ctx(ctx);
1616 case LTTNG_UST_ABI_CONTEXT_CPU_ID:
1617 return lttng_add_cpu_id_to_ctx(ctx);
1618 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
1619 return lttng_ust_add_app_context_to_ctx_rcu(uargs->app_context.ctxname,
1620 ctx);
1621 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS:
1622 return lttng_add_cgroup_ns_to_ctx(ctx);
1623 case LTTNG_UST_ABI_CONTEXT_IPC_NS:
1624 return lttng_add_ipc_ns_to_ctx(ctx);
1625 case LTTNG_UST_ABI_CONTEXT_MNT_NS:
1626 return lttng_add_mnt_ns_to_ctx(ctx);
1627 case LTTNG_UST_ABI_CONTEXT_NET_NS:
1628 return lttng_add_net_ns_to_ctx(ctx);
1629 case LTTNG_UST_ABI_CONTEXT_PID_NS:
1630 return lttng_add_pid_ns_to_ctx(ctx);
1631 case LTTNG_UST_ABI_CONTEXT_TIME_NS:
1632 return lttng_add_time_ns_to_ctx(ctx);
1633 case LTTNG_UST_ABI_CONTEXT_USER_NS:
1634 return lttng_add_user_ns_to_ctx(ctx);
1635 case LTTNG_UST_ABI_CONTEXT_UTS_NS:
1636 return lttng_add_uts_ns_to_ctx(ctx);
1637 case LTTNG_UST_ABI_CONTEXT_VUID:
1638 return lttng_add_vuid_to_ctx(ctx);
1639 case LTTNG_UST_ABI_CONTEXT_VEUID:
1640 return lttng_add_veuid_to_ctx(ctx);
1641 case LTTNG_UST_ABI_CONTEXT_VSUID:
1642 return lttng_add_vsuid_to_ctx(ctx);
1643 case LTTNG_UST_ABI_CONTEXT_VGID:
1644 return lttng_add_vgid_to_ctx(ctx);
1645 case LTTNG_UST_ABI_CONTEXT_VEGID:
1646 return lttng_add_vegid_to_ctx(ctx);
1647 case LTTNG_UST_ABI_CONTEXT_VSGID:
1648 return lttng_add_vsgid_to_ctx(ctx);
1649 default:
1650 return -EINVAL;
1651 }
1652 }
1653
1654 int lttng_event_enabler_attach_context(
1655 struct lttng_event_enabler *enabler __attribute__((unused)),
1656 struct lttng_ust_abi_context *context_param __attribute__((unused)))
1657 {
1658 return -ENOSYS;
1659 }
1660
1661 void lttng_event_enabler_destroy(struct lttng_event_enabler *event_enabler)
1662 {
1663 if (!event_enabler) {
1664 return;
1665 }
1666 cds_list_del(&event_enabler->node);
1667
1668 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler));
1669
1670 lttng_destroy_context(event_enabler->ctx);
1671 free(event_enabler);
1672 }
1673
1674 /*
1675 * lttng_session_sync_event_enablers should be called just before starting a
1676 * session.
1677 */
1678 static
1679 void lttng_session_sync_event_enablers(struct lttng_ust_session *session)
1680 {
1681 struct lttng_event_enabler *event_enabler;
1682 struct lttng_ust_event_recorder_private *event_recorder_priv;
1683
1684 cds_list_for_each_entry(event_enabler, &session->priv->enablers_head, node)
1685 lttng_event_enabler_ref_event_recorders(event_enabler);
1686 /*
1687 * For each event, if at least one of its enablers is enabled,
1688 * and its channel and session transient states are enabled, we
1689 * enable the event, else we disable it.
1690 */
1691 cds_list_for_each_entry(event_recorder_priv, &session->priv->events_head, node) {
1692 struct lttng_enabler_ref *enabler_ref;
1693 struct lttng_ust_bytecode_runtime *runtime;
1694 int enabled = 0, has_enablers_without_filter_bytecode = 0;
1695 int nr_filters = 0;
1696
1697 /* Enable events */
1698 cds_list_for_each_entry(enabler_ref,
1699 &event_recorder_priv->parent.enablers_ref_head, node) {
1700 if (enabler_ref->ref->enabled) {
1701 enabled = 1;
1702 break;
1703 }
1704 }
1705 /*
1706 * Enabled state is based on union of enablers, with
1707 * intesection of session and channel transient enable
1708 * states.
1709 */
1710 enabled = enabled && session->priv->tstate && event_recorder_priv->pub->chan->priv->parent.tstate;
1711
1712 CMM_STORE_SHARED(event_recorder_priv->pub->parent->enabled, enabled);
1713 /*
1714 * Sync tracepoint registration with event enabled
1715 * state.
1716 */
1717 if (enabled) {
1718 if (!event_recorder_priv->parent.registered)
1719 register_event(event_recorder_priv->parent.pub);
1720 } else {
1721 if (event_recorder_priv->parent.registered)
1722 unregister_event(event_recorder_priv->parent.pub);
1723 }
1724
1725 /* Check if has enablers without bytecode enabled */
1726 cds_list_for_each_entry(enabler_ref,
1727 &event_recorder_priv->parent.enablers_ref_head, node) {
1728 if (enabler_ref->ref->enabled
1729 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1730 has_enablers_without_filter_bytecode = 1;
1731 break;
1732 }
1733 }
1734 event_recorder_priv->parent.has_enablers_without_filter_bytecode =
1735 has_enablers_without_filter_bytecode;
1736
1737 /* Enable filters */
1738 cds_list_for_each_entry(runtime,
1739 &event_recorder_priv->parent.filter_bytecode_runtime_head, node) {
1740 lttng_bytecode_sync_state(runtime);
1741 nr_filters++;
1742 }
1743 CMM_STORE_SHARED(event_recorder_priv->parent.pub->eval_filter,
1744 !(has_enablers_without_filter_bytecode || !nr_filters));
1745 }
1746 lttng_ust_tp_probe_prune_release_queue();
1747 }
1748
1749 /* Support for event notifier is introduced by probe provider major version 2. */
1750 static
1751 bool lttng_ust_probe_supports_event_notifier(const struct lttng_ust_probe_desc *probe_desc)
1752 {
1753 return probe_desc->major >= 2;
1754 }
1755
1756 static
1757 void lttng_create_event_notifier_if_missing(
1758 struct lttng_event_notifier_enabler *event_notifier_enabler)
1759 {
1760 struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group;
1761 struct lttng_ust_registered_probe *reg_probe;
1762 struct cds_list_head *probe_list;
1763 int i;
1764
1765 probe_list = lttng_get_probe_list_head();
1766
1767 cds_list_for_each_entry(reg_probe, probe_list, head) {
1768 const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc;
1769
1770 for (i = 0; i < probe_desc->nr_events; i++) {
1771 int ret;
1772 bool found = false;
1773 const struct lttng_ust_event_desc *desc;
1774 struct lttng_ust_event_notifier_private *event_notifier_priv;
1775 struct cds_hlist_head *head;
1776 struct cds_hlist_node *node;
1777
1778 desc = probe_desc->event_desc[i];
1779
1780 if (!lttng_desc_match_enabler(desc,
1781 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)))
1782 continue;
1783
1784 /*
1785 * Given the current event_notifier group, get the bucket that
1786 * the target event_notifier would be if it was already
1787 * created.
1788 */
1789 head = borrow_hash_table_bucket(
1790 event_notifier_group->event_notifiers_ht.table,
1791 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, desc);
1792
1793 cds_hlist_for_each_entry(event_notifier_priv, node, head, hlist) {
1794 /*
1795 * Check if event_notifier already exists by checking
1796 * if the event_notifier and enabler share the same
1797 * description and id.
1798 */
1799 if (event_notifier_priv->parent.desc == desc &&
1800 event_notifier_priv->parent.user_token == event_notifier_enabler->user_token) {
1801 found = true;
1802 break;
1803 }
1804 }
1805
1806 if (found)
1807 continue;
1808
1809 /* Check that the probe supports event notifiers, else report the error. */
1810 if (!lttng_ust_probe_supports_event_notifier(probe_desc)) {
1811 ERR("Probe \"%s\" contains event \"%s:%s\" which matches an enabled event notifier, "
1812 "but its version (%u.%u) is too old and does not implement event notifiers. "
1813 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1814 "this event will not generate any notification.",
1815 probe_desc->provider_name,
1816 probe_desc->provider_name, desc->event_name,
1817 probe_desc->major,
1818 probe_desc->minor);
1819 continue;
1820 }
1821 /*
1822 * We need to create a event_notifier for this event probe.
1823 */
1824 ret = lttng_event_notifier_create(desc,
1825 event_notifier_enabler->user_token,
1826 event_notifier_enabler->error_counter_index,
1827 event_notifier_group);
1828 if (ret) {
1829 DBG("Unable to create event_notifier \"%s:%s\", error %d\n",
1830 probe_desc->provider_name,
1831 probe_desc->event_desc[i]->event_name, ret);
1832 }
1833 }
1834 }
1835 }
1836
1837 /*
1838 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1839 */
1840 static
1841 int lttng_event_notifier_enabler_ref_event_notifiers(
1842 struct lttng_event_notifier_enabler *event_notifier_enabler)
1843 {
1844 struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group;
1845 struct lttng_ust_event_notifier_private *event_notifier_priv;
1846
1847 /*
1848 * Only try to create event_notifiers for enablers that are enabled, the user
1849 * might still be attaching filter or exclusion to the
1850 * event_notifier_enabler.
1851 */
1852 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled)
1853 goto end;
1854
1855 /* First, ensure that probe event_notifiers are created for this enabler. */
1856 lttng_create_event_notifier_if_missing(event_notifier_enabler);
1857
1858 /* Link the created event_notifier with its associated enabler. */
1859 cds_list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) {
1860 struct lttng_enabler_ref *enabler_ref;
1861
1862 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler, event_notifier_priv->pub))
1863 continue;
1864
1865 enabler_ref = lttng_enabler_ref(&event_notifier_priv->parent.enablers_ref_head,
1866 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
1867 if (!enabler_ref) {
1868 /*
1869 * If no backward ref, create it.
1870 * Add backward ref from event_notifier to enabler.
1871 */
1872 enabler_ref = zmalloc(sizeof(*enabler_ref));
1873 if (!enabler_ref)
1874 return -ENOMEM;
1875
1876 enabler_ref->ref = lttng_event_notifier_enabler_as_enabler(
1877 event_notifier_enabler);
1878 cds_list_add(&enabler_ref->node,
1879 &event_notifier_priv->parent.enablers_ref_head);
1880 }
1881
1882 /*
1883 * Link filter bytecodes if not linked yet.
1884 */
1885 lttng_enabler_link_bytecode(event_notifier_priv->parent.desc,
1886 &event_notifier_group->ctx,
1887 &event_notifier_priv->parent.filter_bytecode_runtime_head,
1888 &lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->filter_bytecode_head);
1889
1890 /*
1891 * Link capture bytecodes if not linked yet.
1892 */
1893 lttng_enabler_link_bytecode(event_notifier_priv->parent.desc,
1894 &event_notifier_group->ctx, &event_notifier_priv->capture_bytecode_runtime_head,
1895 &event_notifier_enabler->capture_bytecode_head);
1896
1897 event_notifier_priv->num_captures = event_notifier_enabler->num_captures;
1898 }
1899 end:
1900 return 0;
1901 }
1902
1903 static
1904 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group *event_notifier_group)
1905 {
1906 struct lttng_event_notifier_enabler *event_notifier_enabler;
1907 struct lttng_ust_event_notifier_private *event_notifier_priv;
1908
1909 cds_list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head, node)
1910 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler);
1911
1912 /*
1913 * For each event_notifier, if at least one of its enablers is enabled,
1914 * we enable the event_notifier, else we disable it.
1915 */
1916 cds_list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) {
1917 struct lttng_enabler_ref *enabler_ref;
1918 struct lttng_ust_bytecode_runtime *runtime;
1919 int enabled = 0, has_enablers_without_filter_bytecode = 0;
1920 int nr_filters = 0, nr_captures = 0;
1921
1922 /* Enable event_notifiers */
1923 cds_list_for_each_entry(enabler_ref,
1924 &event_notifier_priv->parent.enablers_ref_head, node) {
1925 if (enabler_ref->ref->enabled) {
1926 enabled = 1;
1927 break;
1928 }
1929 }
1930
1931 CMM_STORE_SHARED(event_notifier_priv->pub->parent->enabled, enabled);
1932 /*
1933 * Sync tracepoint registration with event_notifier enabled
1934 * state.
1935 */
1936 if (enabled) {
1937 if (!event_notifier_priv->parent.registered)
1938 register_event(event_notifier_priv->parent.pub);
1939 } else {
1940 if (event_notifier_priv->parent.registered)
1941 unregister_event(event_notifier_priv->parent.pub);
1942 }
1943
1944 /* Check if has enablers without bytecode enabled */
1945 cds_list_for_each_entry(enabler_ref,
1946 &event_notifier_priv->parent.enablers_ref_head, node) {
1947 if (enabler_ref->ref->enabled
1948 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1949 has_enablers_without_filter_bytecode = 1;
1950 break;
1951 }
1952 }
1953 event_notifier_priv->parent.has_enablers_without_filter_bytecode =
1954 has_enablers_without_filter_bytecode;
1955
1956 /* Enable filters */
1957 cds_list_for_each_entry(runtime,
1958 &event_notifier_priv->parent.filter_bytecode_runtime_head, node) {
1959 lttng_bytecode_sync_state(runtime);
1960 nr_filters++;
1961 }
1962 CMM_STORE_SHARED(event_notifier_priv->parent.pub->eval_filter,
1963 !(has_enablers_without_filter_bytecode || !nr_filters));
1964
1965 /* Enable captures. */
1966 cds_list_for_each_entry(runtime,
1967 &event_notifier_priv->capture_bytecode_runtime_head, node) {
1968 lttng_bytecode_sync_state(runtime);
1969 nr_captures++;
1970 }
1971 CMM_STORE_SHARED(event_notifier_priv->pub->eval_capture,
1972 !!nr_captures);
1973 }
1974 lttng_ust_tp_probe_prune_release_queue();
1975 }
1976
1977 /*
1978 * Apply enablers to session events, adding events to session if need
1979 * be. It is required after each modification applied to an active
1980 * session, and right before session "start".
1981 * "lazy" sync means we only sync if required.
1982 */
1983 static
1984 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session *session)
1985 {
1986 /* We can skip if session is not active */
1987 if (!session->active)
1988 return;
1989 lttng_session_sync_event_enablers(session);
1990 }
1991
1992 /*
1993 * Update all sessions with the given app context.
1994 * Called with ust lock held.
1995 * This is invoked when an application context gets loaded/unloaded. It
1996 * ensures the context callbacks are in sync with the application
1997 * context (either app context callbacks, or dummy callbacks).
1998 */
1999 void lttng_ust_context_set_session_provider(const char *name,
2000 size_t (*get_size)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
2001 size_t offset),
2002 void (*record)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
2003 struct lttng_ust_ring_buffer_ctx *ctx,
2004 struct lttng_ust_channel_buffer *chan),
2005 void (*get_value)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
2006 struct lttng_ust_ctx_value *value),
2007 void *priv)
2008 {
2009 struct lttng_ust_session_private *session_priv;
2010
2011 cds_list_for_each_entry(session_priv, &sessions, node) {
2012 struct lttng_ust_channel_buffer_private *chan;
2013 struct lttng_ust_event_recorder_private *event_recorder_priv;
2014 int ret;
2015
2016 ret = lttng_ust_context_set_provider_rcu(&session_priv->ctx,
2017 name, get_size, record, get_value, priv);
2018 if (ret)
2019 abort();
2020 cds_list_for_each_entry(chan, &session_priv->chan_head, node) {
2021 ret = lttng_ust_context_set_provider_rcu(&chan->ctx,
2022 name, get_size, record, get_value, priv);
2023 if (ret)
2024 abort();
2025 }
2026 cds_list_for_each_entry(event_recorder_priv, &session_priv->events_head, node) {
2027 ret = lttng_ust_context_set_provider_rcu(&event_recorder_priv->ctx,
2028 name, get_size, record, get_value, priv);
2029 if (ret)
2030 abort();
2031 }
2032 }
2033 }
2034
2035 /*
2036 * Update all event_notifier groups with the given app context.
2037 * Called with ust lock held.
2038 * This is invoked when an application context gets loaded/unloaded. It
2039 * ensures the context callbacks are in sync with the application
2040 * context (either app context callbacks, or dummy callbacks).
2041 */
2042 void lttng_ust_context_set_event_notifier_group_provider(const char *name,
2043 size_t (*get_size)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
2044 size_t offset),
2045 void (*record)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
2046 struct lttng_ust_ring_buffer_ctx *ctx,
2047 struct lttng_ust_channel_buffer *chan),
2048 void (*get_value)(void *priv, struct lttng_ust_probe_ctx *probe_ctx,
2049 struct lttng_ust_ctx_value *value),
2050 void *priv)
2051 {
2052 struct lttng_event_notifier_group *event_notifier_group;
2053
2054 cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) {
2055 int ret;
2056
2057 ret = lttng_ust_context_set_provider_rcu(
2058 &event_notifier_group->ctx,
2059 name, get_size, record, get_value, priv);
2060 if (ret)
2061 abort();
2062 }
2063 }
2064
2065 int lttng_ust_session_uuid_validate(struct lttng_ust_session *session,
2066 unsigned char *uuid)
2067 {
2068 if (!session)
2069 return 0;
2070 /* Compare UUID with session. */
2071 if (session->priv->uuid_set) {
2072 if (memcmp(session->priv->uuid, uuid, LTTNG_UST_UUID_LEN)) {
2073 return -1;
2074 }
2075 } else {
2076 memcpy(session->priv->uuid, uuid, LTTNG_UST_UUID_LEN);
2077 session->priv->uuid_set = true;
2078 }
2079 return 0;
2080
2081 }
This page took 0.118601 seconds and 4 git commands to generate.