2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
24 #include <lttng/ust-endian.h>
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>
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
35 #include <usterr-signal-safe.h>
36 #include <ust-helper.h>
37 #include <lttng/ust-ctl.h>
40 #include <ust-dynamic-type.h>
41 #include <ust-context-provider.h>
44 #include "lttng-ust-uuid.h"
46 #include "tracepoint-internal.h"
47 #include "string-utils.h"
48 #include "lttng-bytecode.h"
49 #include "lttng-tracer.h"
50 #include "lttng-tracer-core.h"
51 #include "lttng-ust-statedump.h"
52 #include "context-internal.h"
53 #include "ust-events-internal.h"
55 #include "../libringbuffer/shm.h"
56 #include "../libcounter/counter.h"
58 #include <lttng/ust-abi.h>
61 * All operations within this file are called by the communication
62 * thread, under ust_lock protection.
65 static CDS_LIST_HEAD(sessions
);
66 static CDS_LIST_HEAD(event_notifier_groups
);
68 struct cds_list_head
*lttng_get_sessions(void)
73 static void _lttng_event_destroy(struct lttng_ust_event_common
*event
);
74 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
77 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
);
79 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
);
81 void lttng_event_notifier_group_sync_enablers(
82 struct lttng_event_notifier_group
*event_notifier_group
);
84 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
87 * Called with ust lock held.
89 int lttng_session_active(void)
91 struct lttng_ust_session_private
*iter
;
93 cds_list_for_each_entry(iter
, &sessions
, node
) {
94 if (iter
->pub
->active
)
101 int lttng_loglevel_match(int loglevel
,
102 unsigned int has_loglevel
,
103 enum lttng_ust_abi_loglevel_type req_type
,
107 loglevel
= TRACE_DEFAULT
;
109 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
110 if (loglevel
<= req_loglevel
111 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
115 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
116 if (loglevel
== req_loglevel
117 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
121 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
123 if (loglevel
<= TRACE_DEBUG
)
130 struct lttng_ust_session
*lttng_session_create(void)
132 struct lttng_ust_session
*session
;
133 struct lttng_ust_session_private
*session_priv
;
136 session
= zmalloc(sizeof(struct lttng_ust_session
));
139 session
->struct_size
= sizeof(struct lttng_ust_session
);
140 session_priv
= zmalloc(sizeof(struct lttng_ust_session_private
));
145 session
->priv
= session_priv
;
146 session_priv
->pub
= session
;
147 if (lttng_context_init_all(&session
->priv
->ctx
)) {
152 CDS_INIT_LIST_HEAD(&session
->priv
->chan_head
);
153 CDS_INIT_LIST_HEAD(&session
->priv
->events_head
);
154 CDS_INIT_LIST_HEAD(&session
->priv
->enums_head
);
155 CDS_INIT_LIST_HEAD(&session
->priv
->enablers_head
);
156 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
157 CDS_INIT_HLIST_HEAD(&session
->priv
->events_ht
.table
[i
]);
158 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
159 CDS_INIT_HLIST_HEAD(&session
->priv
->enums_ht
.table
[i
]);
160 cds_list_add(&session
->priv
->node
, &sessions
);
164 struct lttng_counter
*lttng_ust_counter_create(
165 const char *counter_transport_name
,
166 size_t number_dimensions
, const struct lttng_counter_dimension
*dimensions
)
168 struct lttng_counter_transport
*counter_transport
= NULL
;
169 struct lttng_counter
*counter
= NULL
;
171 counter_transport
= lttng_counter_transport_find(counter_transport_name
);
172 if (!counter_transport
)
174 counter
= zmalloc(sizeof(struct lttng_counter
));
178 counter
->ops
= &counter_transport
->ops
;
179 counter
->transport
= counter_transport
;
181 counter
->counter
= counter
->ops
->counter_create(
182 number_dimensions
, dimensions
, 0,
184 if (!counter
->counter
) {
198 void lttng_ust_counter_destroy(struct lttng_counter
*counter
)
200 counter
->ops
->counter_destroy(counter
->counter
);
204 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
206 struct lttng_event_notifier_group
*event_notifier_group
;
209 event_notifier_group
= zmalloc(sizeof(struct lttng_event_notifier_group
));
210 if (!event_notifier_group
)
213 /* Add all contexts. */
214 if (lttng_context_init_all(&event_notifier_group
->ctx
)) {
215 free(event_notifier_group
);
219 CDS_INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
220 CDS_INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
221 for (i
= 0; i
< LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
; i
++)
222 CDS_INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
224 cds_list_add(&event_notifier_group
->node
, &event_notifier_groups
);
226 return event_notifier_group
;
230 * Only used internally at session destruction.
233 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
235 struct lttng_ust_lib_ring_buffer_channel
*chan
;
236 struct lttng_ust_shm_handle
*handle
;
238 cds_list_del(<tng_chan
->node
);
239 lttng_destroy_context(lttng_chan
->ctx
);
240 chan
= lttng_chan
->chan
;
241 handle
= lttng_chan
->handle
;
242 channel_destroy(chan
, handle
, 0);
247 void register_event(struct lttng_ust_event_common
*event
)
250 struct lttng_ust_event_desc
*desc
;
252 assert(event
->priv
->registered
== 0);
253 desc
= event
->priv
->desc
;
254 ret
= lttng_ust_tp_probe_register_queue_release(desc
->name
,
255 desc
->probe_callback
,
256 event
, desc
->signature
);
259 event
->priv
->registered
= 1;
263 void unregister_event(struct lttng_ust_event_common
*event
)
266 struct lttng_ust_event_desc
*desc
;
268 assert(event
->priv
->registered
== 1);
269 desc
= event
->priv
->desc
;
270 ret
= lttng_ust_tp_probe_unregister_queue_release(desc
->name
,
271 desc
->probe_callback
,
275 event
->priv
->registered
= 0;
279 void _lttng_event_unregister(struct lttng_ust_event_common
*event
)
281 if (event
->priv
->registered
)
282 unregister_event(event
);
285 void lttng_session_destroy(struct lttng_ust_session
*session
)
287 struct lttng_channel
*chan
, *tmpchan
;
288 struct lttng_ust_event_recorder_private
*event_recorder_priv
, *tmpevent_recorder_priv
;
289 struct lttng_enum
*_enum
, *tmp_enum
;
290 struct lttng_event_enabler
*event_enabler
, *event_tmpenabler
;
292 CMM_ACCESS_ONCE(session
->active
) = 0;
293 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
294 _lttng_event_unregister(event_recorder_priv
->parent
.pub
);
296 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
297 lttng_ust_tp_probe_prune_release_queue();
298 cds_list_for_each_entry_safe(event_enabler
, event_tmpenabler
,
299 &session
->priv
->enablers_head
, node
)
300 lttng_event_enabler_destroy(event_enabler
);
301 cds_list_for_each_entry_safe(event_recorder_priv
, tmpevent_recorder_priv
,
302 &session
->priv
->events_head
, node
)
303 _lttng_event_destroy(event_recorder_priv
->parent
.pub
);
304 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
305 &session
->priv
->enums_head
, node
)
306 _lttng_enum_destroy(_enum
);
307 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->priv
->chan_head
, node
)
308 _lttng_channel_unmap(chan
);
309 cds_list_del(&session
->priv
->node
);
310 lttng_destroy_context(session
->priv
->ctx
);
315 void lttng_event_notifier_group_destroy(
316 struct lttng_event_notifier_group
*event_notifier_group
)
319 struct lttng_event_notifier_enabler
*notifier_enabler
, *tmpnotifier_enabler
;
320 struct lttng_ust_event_notifier_private
*event_notifier_priv
, *tmpevent_notifier_priv
;
322 if (!event_notifier_group
) {
326 cds_list_for_each_entry(event_notifier_priv
,
327 &event_notifier_group
->event_notifiers_head
, node
)
328 _lttng_event_unregister(event_notifier_priv
->parent
.pub
);
330 lttng_ust_urcu_synchronize_rcu();
332 cds_list_for_each_entry_safe(notifier_enabler
, tmpnotifier_enabler
,
333 &event_notifier_group
->enablers_head
, node
)
334 lttng_event_notifier_enabler_destroy(notifier_enabler
);
336 cds_list_for_each_entry_safe(event_notifier_priv
, tmpevent_notifier_priv
,
337 &event_notifier_group
->event_notifiers_head
, node
)
338 _lttng_event_destroy(event_notifier_priv
->parent
.pub
);
340 if (event_notifier_group
->error_counter
)
341 lttng_ust_counter_destroy(event_notifier_group
->error_counter
);
343 /* Close the notification fd to the listener of event_notifiers. */
345 lttng_ust_lock_fd_tracker();
346 close_ret
= close(event_notifier_group
->notification_fd
);
348 lttng_ust_delete_fd_from_tracker(
349 event_notifier_group
->notification_fd
);
354 lttng_ust_unlock_fd_tracker();
356 cds_list_del(&event_notifier_group
->node
);
358 free(event_notifier_group
);
362 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
364 struct lttng_ust_bytecode_node
*filter_node
, *tmp_filter_node
;
365 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
371 /* Destroy filter bytecode */
372 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
373 &enabler
->filter_bytecode_head
, node
) {
377 /* Destroy excluders */
378 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
379 &enabler
->excluder_head
, node
) {
384 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
386 if (!event_notifier_enabler
) {
390 cds_list_del(&event_notifier_enabler
->node
);
392 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
394 free(event_notifier_enabler
);
398 int lttng_enum_create(struct lttng_ust_enum_desc
*desc
,
399 struct lttng_ust_session
*session
)
401 const char *enum_name
= desc
->name
;
402 struct lttng_enum
*_enum
;
403 struct cds_hlist_head
*head
;
405 size_t name_len
= strlen(enum_name
);
409 /* Check if this enum is already registered for this session. */
410 hash
= jhash(enum_name
, name_len
, 0);
411 head
= &session
->priv
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
413 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
419 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
420 if (notify_socket
< 0) {
425 _enum
= zmalloc(sizeof(*_enum
));
430 _enum
->session
= session
;
433 ret
= ustcomm_register_enum(notify_socket
,
440 DBG("Error (%d) registering enumeration to sessiond", ret
);
441 goto sessiond_register_error
;
443 cds_list_add(&_enum
->node
, &session
->priv
->enums_head
);
444 cds_hlist_add_head(&_enum
->hlist
, head
);
447 sessiond_register_error
:
456 int lttng_create_enum_check(struct lttng_ust_type_common
*type
,
457 struct lttng_ust_session
*session
)
459 switch (type
->type
) {
460 case lttng_ust_type_enum
:
462 struct lttng_ust_enum_desc
*enum_desc
;
465 enum_desc
= lttng_ust_get_type_enum(type
)->desc
;
466 ret
= lttng_enum_create(enum_desc
, session
);
467 if (ret
&& ret
!= -EEXIST
) {
468 DBG("Unable to create enum error: (%d)", ret
);
473 case lttng_ust_type_dynamic
:
475 struct lttng_ust_event_field
*tag_field_generic
;
476 struct lttng_ust_enum_desc
*enum_desc
;
479 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
480 enum_desc
= lttng_ust_get_type_enum(tag_field_generic
->type
)->desc
;
481 ret
= lttng_enum_create(enum_desc
, session
);
482 if (ret
&& ret
!= -EEXIST
) {
483 DBG("Unable to create enum error: (%d)", ret
);
489 /* TODO: nested types when they become supported. */
496 int lttng_create_all_event_enums(size_t nr_fields
,
497 struct lttng_ust_event_field
**event_fields
,
498 struct lttng_ust_session
*session
)
503 /* For each field, ensure enum is part of the session. */
504 for (i
= 0; i
< nr_fields
; i
++) {
505 struct lttng_ust_type_common
*type
= event_fields
[i
]->type
;
507 ret
= lttng_create_enum_check(type
, session
);
515 int lttng_create_all_ctx_enums(size_t nr_fields
,
516 struct lttng_ust_ctx_field
**ctx_fields
,
517 struct lttng_ust_session
*session
)
522 /* For each field, ensure enum is part of the session. */
523 for (i
= 0; i
< nr_fields
; i
++) {
524 struct lttng_ust_type_common
*type
= ctx_fields
[i
]->event_field
->type
;
526 ret
= lttng_create_enum_check(type
, session
);
534 * Ensure that a state-dump will be performed for this session at the end
535 * of the current handle_message().
537 int lttng_session_statedump(struct lttng_ust_session
*session
)
539 session
->priv
->statedump_pending
= 1;
540 lttng_ust_sockinfo_session_enabled(session
->priv
->owner
);
544 int lttng_session_enable(struct lttng_ust_session
*session
)
547 struct lttng_channel
*chan
;
550 if (session
->active
) {
555 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
556 if (notify_socket
< 0)
557 return notify_socket
;
559 /* Set transient enabler state to "enabled" */
560 session
->priv
->tstate
= 1;
562 /* We need to sync enablers with session before activation. */
563 lttng_session_sync_event_enablers(session
);
566 * Snapshot the number of events per channel to know the type of header
569 cds_list_for_each_entry(chan
, &session
->priv
->chan_head
, node
) {
570 struct lttng_ust_ctx
*ctx
;
571 struct lttng_ust_ctx_field
**fields
= NULL
;
572 size_t nr_fields
= 0;
575 /* don't change it if session stop/restart */
576 if (chan
->header_type
)
580 nr_fields
= ctx
->nr_fields
;
581 fields
= ctx
->fields
;
582 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
585 DBG("Error (%d) adding enum to session", ret
);
589 ret
= ustcomm_register_channel(notify_socket
,
598 DBG("Error (%d) registering channel to sessiond", ret
);
601 if (chan_id
!= chan
->id
) {
602 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
608 /* Set atomically the state to "active" */
609 CMM_ACCESS_ONCE(session
->active
) = 1;
610 CMM_ACCESS_ONCE(session
->priv
->been_active
) = 1;
612 ret
= lttng_session_statedump(session
);
619 int lttng_session_disable(struct lttng_ust_session
*session
)
623 if (!session
->active
) {
627 /* Set atomically the state to "inactive" */
628 CMM_ACCESS_ONCE(session
->active
) = 0;
630 /* Set transient enabler state to "disabled" */
631 session
->priv
->tstate
= 0;
632 lttng_session_sync_event_enablers(session
);
637 int lttng_channel_enable(struct lttng_channel
*channel
)
641 if (channel
->enabled
) {
645 /* Set transient enabler state to "enabled" */
647 lttng_session_sync_event_enablers(channel
->session
);
648 /* Set atomically the state to "enabled" */
649 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
654 int lttng_channel_disable(struct lttng_channel
*channel
)
658 if (!channel
->enabled
) {
662 /* Set atomically the state to "disabled" */
663 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
664 /* Set transient enabler state to "enabled" */
666 lttng_session_sync_event_enablers(channel
->session
);
672 struct cds_hlist_head
*borrow_hash_table_bucket(
673 struct cds_hlist_head
*hash_table
,
674 unsigned int hash_table_size
,
675 struct lttng_ust_event_desc
*desc
)
677 const char *event_name
;
681 event_name
= desc
->name
;
682 name_len
= strlen(event_name
);
684 hash
= jhash(event_name
, name_len
, 0);
685 return &hash_table
[hash
& (hash_table_size
- 1)];
689 * Supports event creation while tracing session is active.
692 int lttng_event_recorder_create(struct lttng_ust_event_desc
*desc
,
693 struct lttng_channel
*chan
)
695 struct lttng_ust_event_recorder
*event_recorder
;
696 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
697 struct lttng_ust_session
*session
= chan
->session
;
698 struct cds_hlist_head
*head
;
700 int notify_socket
, loglevel
;
703 head
= borrow_hash_table_bucket(chan
->session
->priv
->events_ht
.table
,
704 LTTNG_UST_EVENT_HT_SIZE
, desc
);
706 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
707 if (notify_socket
< 0) {
712 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
715 DBG("Error (%d) adding enum to session", ret
);
716 goto create_enum_error
;
720 * Check if loglevel match. Refuse to connect event if not.
722 event_recorder
= zmalloc(sizeof(struct lttng_ust_event_recorder
));
723 if (!event_recorder
) {
727 event_recorder
->struct_size
= sizeof(struct lttng_ust_event_recorder
);
729 event_recorder
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
730 if (!event_recorder
->parent
) {
734 event_recorder
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
735 event_recorder
->parent
->type
= LTTNG_UST_EVENT_TYPE_RECORDER
;
736 event_recorder
->parent
->child
= event_recorder
;
738 event_recorder_priv
= zmalloc(sizeof(struct lttng_ust_event_recorder_private
));
739 if (!event_recorder_priv
) {
743 event_recorder
->priv
= event_recorder_priv
;
744 event_recorder_priv
->pub
= event_recorder
;
745 event_recorder
->parent
->priv
= &event_recorder_priv
->parent
;
746 event_recorder_priv
->parent
.pub
= event_recorder
->parent
;
748 event_recorder
->chan
= chan
;
750 /* Event will be enabled by enabler sync. */
751 event_recorder
->parent
->enabled
= 0;
752 event_recorder
->parent
->priv
->registered
= 0;
753 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->filter_bytecode_runtime_head
);
754 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->enablers_ref_head
);
755 event_recorder
->parent
->priv
->desc
= desc
;
758 loglevel
= *(*event_recorder
->parent
->priv
->desc
->loglevel
);
760 loglevel
= TRACE_DEFAULT
;
761 if (desc
->model_emf_uri
)
762 uri
= *(desc
->model_emf_uri
);
766 /* Fetch event ID from sessiond */
767 ret
= ustcomm_register_event(notify_socket
,
777 &event_recorder
->id
);
779 DBG("Error (%d) registering event to sessiond", ret
);
780 goto sessiond_register_error
;
783 cds_list_add(&event_recorder_priv
->node
, &chan
->session
->priv
->events_head
);
784 cds_hlist_add_head(&event_recorder_priv
->hlist
, head
);
787 sessiond_register_error
:
788 free(event_recorder_priv
);
790 free(event_recorder
->parent
);
792 free(event_recorder
);
800 int lttng_event_notifier_create(struct lttng_ust_event_desc
*desc
,
801 uint64_t token
, uint64_t error_counter_index
,
802 struct lttng_event_notifier_group
*event_notifier_group
)
804 struct lttng_ust_event_notifier
*event_notifier
;
805 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
806 struct cds_hlist_head
*head
;
810 * Get the hashtable bucket the created lttng_event_notifier object
811 * should be inserted.
813 head
= borrow_hash_table_bucket(
814 event_notifier_group
->event_notifiers_ht
.table
,
815 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
817 event_notifier
= zmalloc(sizeof(struct lttng_ust_event_notifier
));
818 if (!event_notifier
) {
822 event_notifier
->struct_size
= sizeof(struct lttng_ust_event_notifier
);
824 event_notifier
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
825 if (!event_notifier
->parent
) {
829 event_notifier
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
830 event_notifier
->parent
->type
= LTTNG_UST_EVENT_TYPE_NOTIFIER
;
831 event_notifier
->parent
->child
= event_notifier
;
833 event_notifier_priv
= zmalloc(sizeof(struct lttng_ust_event_notifier_private
));
834 if (!event_notifier_priv
) {
838 event_notifier
->priv
= event_notifier_priv
;
839 event_notifier_priv
->pub
= event_notifier
;
840 event_notifier
->parent
->priv
= &event_notifier_priv
->parent
;
841 event_notifier_priv
->parent
.pub
= event_notifier
->parent
;
843 event_notifier_priv
->group
= event_notifier_group
;
844 event_notifier_priv
->parent
.user_token
= token
;
845 event_notifier_priv
->error_counter_index
= error_counter_index
;
847 /* Event notifier will be enabled by enabler sync. */
848 event_notifier
->parent
->enabled
= 0;
849 event_notifier_priv
->parent
.registered
= 0;
851 CDS_INIT_LIST_HEAD(&event_notifier
->parent
->filter_bytecode_runtime_head
);
852 CDS_INIT_LIST_HEAD(&event_notifier
->capture_bytecode_runtime_head
);
853 CDS_INIT_LIST_HEAD(&event_notifier_priv
->parent
.enablers_ref_head
);
854 event_notifier_priv
->parent
.desc
= desc
;
855 event_notifier
->notification_send
= lttng_event_notifier_notification_send
;
857 cds_list_add(&event_notifier_priv
->node
,
858 &event_notifier_group
->event_notifiers_head
);
859 cds_hlist_add_head(&event_notifier_priv
->hlist
, head
);
864 free(event_notifier
->parent
);
866 free(event_notifier
);
872 int lttng_desc_match_star_glob_enabler(struct lttng_ust_event_desc
*desc
,
873 struct lttng_enabler
*enabler
)
876 unsigned int has_loglevel
= 0;
878 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_STAR_GLOB
);
879 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
880 desc
->name
, SIZE_MAX
))
882 if (desc
->loglevel
) {
883 loglevel
= *(*desc
->loglevel
);
886 if (!lttng_loglevel_match(loglevel
,
888 enabler
->event_param
.loglevel_type
,
889 enabler
->event_param
.loglevel
))
895 int lttng_desc_match_event_enabler(struct lttng_ust_event_desc
*desc
,
896 struct lttng_enabler
*enabler
)
899 unsigned int has_loglevel
= 0;
901 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_EVENT
);
902 if (strcmp(desc
->name
, enabler
->event_param
.name
))
904 if (desc
->loglevel
) {
905 loglevel
= *(*desc
->loglevel
);
908 if (!lttng_loglevel_match(loglevel
,
910 enabler
->event_param
.loglevel_type
,
911 enabler
->event_param
.loglevel
))
917 int lttng_desc_match_enabler(struct lttng_ust_event_desc
*desc
,
918 struct lttng_enabler
*enabler
)
920 switch (enabler
->format_type
) {
921 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
923 struct lttng_ust_excluder_node
*excluder
;
925 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
930 * If the matching event matches with an excluder,
931 * return 'does not match'
933 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
936 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
940 excluder_name
= (char *) (excluder
->excluder
.names
)
941 + count
* LTTNG_UST_ABI_SYM_NAME_LEN
;
942 len
= strnlen(excluder_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
943 if (len
> 0 && strutils_star_glob_match(excluder_name
, len
, desc
->name
, SIZE_MAX
))
949 case LTTNG_ENABLER_FORMAT_EVENT
:
950 return lttng_desc_match_event_enabler(desc
, enabler
);
957 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
958 struct lttng_ust_event_recorder
*event_recorder
)
960 if (lttng_desc_match_enabler(event_recorder
->parent
->priv
->desc
,
961 lttng_event_enabler_as_enabler(event_enabler
))
962 && event_recorder
->chan
== event_enabler
->chan
)
969 int lttng_event_notifier_enabler_match_event_notifier(
970 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
971 struct lttng_ust_event_notifier
*event_notifier
)
973 int desc_matches
= lttng_desc_match_enabler(event_notifier
->priv
->parent
.desc
,
974 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
976 if (desc_matches
&& event_notifier
->priv
->group
== event_notifier_enabler
->group
&&
977 event_notifier
->priv
->parent
.user_token
== event_notifier_enabler
->user_token
)
984 struct lttng_enabler_ref
*lttng_enabler_ref(
985 struct cds_list_head
*enabler_ref_list
,
986 struct lttng_enabler
*enabler
)
988 struct lttng_enabler_ref
*enabler_ref
;
990 cds_list_for_each_entry(enabler_ref
, enabler_ref_list
, node
) {
991 if (enabler_ref
->ref
== enabler
)
998 * Create struct lttng_event if it is missing and present in the list of
1002 void lttng_create_event_recorder_if_missing(struct lttng_event_enabler
*event_enabler
)
1004 struct lttng_ust_session
*session
= event_enabler
->chan
->session
;
1005 struct lttng_ust_probe_desc
*probe_desc
;
1006 struct lttng_ust_event_desc
*desc
;
1007 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1009 struct cds_list_head
*probe_list
;
1011 probe_list
= lttng_get_probe_list_head();
1013 * For each probe event, if we find that a probe event matches
1014 * our enabler, create an associated lttng_event if not
1017 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
1018 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1021 struct cds_hlist_head
*head
;
1022 struct cds_hlist_node
*node
;
1024 desc
= probe_desc
->event_desc
[i
];
1025 if (!lttng_desc_match_enabler(desc
,
1026 lttng_event_enabler_as_enabler(event_enabler
)))
1029 head
= borrow_hash_table_bucket(
1030 session
->priv
->events_ht
.table
,
1031 LTTNG_UST_EVENT_HT_SIZE
, desc
);
1033 cds_hlist_for_each_entry(event_recorder_priv
, node
, head
, hlist
) {
1034 if (event_recorder_priv
->parent
.desc
== desc
1035 && event_recorder_priv
->pub
->chan
== event_enabler
->chan
) {
1044 * We need to create an event for this
1047 ret
= lttng_event_recorder_create(probe_desc
->event_desc
[i
],
1048 event_enabler
->chan
);
1050 DBG("Unable to create event %s, error %d\n",
1051 probe_desc
->event_desc
[i
]->name
, ret
);
1058 void probe_provider_event_for_each(struct lttng_ust_probe_desc
*provider_desc
,
1059 void (*event_func
)(struct lttng_ust_event_common
*event
))
1061 struct cds_hlist_node
*node
, *tmp_node
;
1062 struct cds_list_head
*sessionsp
;
1065 /* Get handle on list of sessions. */
1066 sessionsp
= lttng_get_sessions();
1069 * Iterate over all events in the probe provider descriptions and
1070 * sessions to queue the unregistration of the events.
1072 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
1073 struct lttng_ust_event_desc
*event_desc
;
1074 struct lttng_event_notifier_group
*event_notifier_group
;
1075 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1076 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1077 struct lttng_ust_session_private
*session_priv
;
1078 struct cds_hlist_head
*head
;
1080 event_desc
= provider_desc
->event_desc
[i
];
1083 * Iterate over all session to find the current event
1086 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
1088 * Get the list of events in the hashtable bucket and
1089 * iterate to find the event matching this descriptor.
1091 head
= borrow_hash_table_bucket(
1092 session_priv
->events_ht
.table
,
1093 LTTNG_UST_EVENT_HT_SIZE
, event_desc
);
1095 cds_hlist_for_each_entry_safe(event_recorder_priv
, node
, tmp_node
, head
, hlist
) {
1096 if (event_desc
== event_recorder_priv
->parent
.desc
) {
1097 event_func(event_recorder_priv
->parent
.pub
);
1104 * Iterate over all event_notifier groups to find the current event
1107 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1109 * Get the list of event_notifiers in the hashtable bucket and
1110 * iterate to find the event_notifier matching this
1113 head
= borrow_hash_table_bucket(
1114 event_notifier_group
->event_notifiers_ht
.table
,
1115 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, event_desc
);
1117 cds_hlist_for_each_entry_safe(event_notifier_priv
, node
, tmp_node
, head
, hlist
) {
1118 if (event_desc
== event_notifier_priv
->parent
.desc
) {
1119 event_func(event_notifier_priv
->parent
.pub
);
1128 void _event_enum_destroy(struct lttng_ust_event_common
*event
)
1131 switch (event
->type
) {
1132 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1134 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1135 struct lttng_ust_session
*session
= event_recorder
->chan
->session
;
1138 /* Destroy enums of the current event. */
1139 for (i
= 0; i
< event_recorder
->parent
->priv
->desc
->nr_fields
; i
++) {
1140 struct lttng_ust_enum_desc
*enum_desc
;
1141 struct lttng_ust_event_field
*field
;
1142 struct lttng_enum
*curr_enum
;
1144 field
= event_recorder
->parent
->priv
->desc
->fields
[i
];
1145 switch (field
->type
->type
) {
1146 case lttng_ust_type_enum
:
1147 enum_desc
= lttng_ust_get_type_enum(field
->type
)->desc
;
1153 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
1155 _lttng_enum_destroy(curr_enum
);
1160 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1165 /* Destroy event. */
1166 _lttng_event_destroy(event
);
1170 * Iterate over all the UST sessions to unregister and destroy all probes from
1171 * the probe provider descriptor received as argument. Must me called with the
1174 void lttng_probe_provider_unregister_events(
1175 struct lttng_ust_probe_desc
*provider_desc
)
1178 * Iterate over all events in the probe provider descriptions and sessions
1179 * to queue the unregistration of the events.
1181 probe_provider_event_for_each(provider_desc
, _lttng_event_unregister
);
1183 /* Wait for grace period. */
1184 lttng_ust_urcu_synchronize_rcu();
1185 /* Prune the unregistration queue. */
1186 lttng_ust_tp_probe_prune_release_queue();
1189 * It is now safe to destroy the events and remove them from the event list
1192 probe_provider_event_for_each(provider_desc
, _event_enum_destroy
);
1196 * Create events associated with an event enabler (if not already present),
1197 * and add backward reference from the event to the enabler.
1200 int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler
*event_enabler
)
1202 struct lttng_ust_session
*session
= event_enabler
->chan
->session
;
1203 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1205 if (!lttng_event_enabler_as_enabler(event_enabler
)->enabled
)
1208 /* First ensure that probe events are created for this enabler. */
1209 lttng_create_event_recorder_if_missing(event_enabler
);
1211 /* For each event matching enabler in session event list. */
1212 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1213 struct lttng_enabler_ref
*enabler_ref
;
1215 if (!lttng_event_enabler_match_event(event_enabler
, event_recorder_priv
->pub
))
1218 enabler_ref
= lttng_enabler_ref(&event_recorder_priv
->parent
.enablers_ref_head
,
1219 lttng_event_enabler_as_enabler(event_enabler
));
1222 * If no backward ref, create it.
1223 * Add backward ref from event to enabler.
1225 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1228 enabler_ref
->ref
= lttng_event_enabler_as_enabler(
1230 cds_list_add(&enabler_ref
->node
,
1231 &event_recorder_priv
->parent
.enablers_ref_head
);
1235 * Link filter bytecodes if not linked yet.
1237 lttng_enabler_link_bytecode(event_recorder_priv
->parent
.desc
,
1238 &session
->priv
->ctx
,
1239 &event_recorder_priv
->pub
->parent
->filter_bytecode_runtime_head
,
1240 <tng_event_enabler_as_enabler(event_enabler
)->filter_bytecode_head
);
1242 /* TODO: merge event context. */
1249 * Called at library load: connect the probe on all enablers matching
1251 * Called with session mutex held.
1253 int lttng_fix_pending_events(void)
1255 struct lttng_ust_session_private
*session_priv
;
1257 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1258 lttng_session_lazy_sync_event_enablers(session_priv
->pub
);
1263 int lttng_fix_pending_event_notifiers(void)
1265 struct lttng_event_notifier_group
*event_notifier_group
;
1267 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1268 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1274 * For each session of the owner thread, execute pending statedump.
1275 * Only dump state for the sessions owned by the caller thread, because
1276 * we don't keep ust_lock across the entire iteration.
1278 void lttng_handle_pending_statedump(void *owner
)
1280 struct lttng_ust_session_private
*session_priv
;
1282 /* Execute state dump */
1283 do_lttng_ust_statedump(owner
);
1285 /* Clear pending state dump */
1289 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1290 if (session_priv
->owner
!= owner
)
1292 if (!session_priv
->statedump_pending
)
1294 session_priv
->statedump_pending
= 0;
1302 void _lttng_event_destroy(struct lttng_ust_event_common
*event
)
1304 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1306 lttng_free_event_filter_runtime(event
);
1307 /* Free event enabler refs */
1308 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1309 &event
->priv
->enablers_ref_head
, node
)
1312 switch (event
->type
) {
1313 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1315 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1317 /* Remove from event list. */
1318 cds_list_del(&event_recorder
->priv
->node
);
1319 /* Remove from event hash table. */
1320 cds_hlist_del(&event_recorder
->priv
->hlist
);
1322 lttng_destroy_context(event_recorder
->ctx
);
1323 free(event_recorder
->parent
);
1324 free(event_recorder
->priv
);
1325 free(event_recorder
);
1328 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1330 struct lttng_ust_event_notifier
*event_notifier
= event
->child
;
1332 /* Remove from event list. */
1333 cds_list_del(&event_notifier
->priv
->node
);
1334 /* Remove from event hash table. */
1335 cds_hlist_del(&event_notifier
->priv
->hlist
);
1337 free(event_notifier
->priv
);
1338 free(event_notifier
->parent
);
1339 free(event_notifier
);
1348 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1350 cds_list_del(&_enum
->node
);
1351 cds_hlist_del(&_enum
->hlist
);
1355 void lttng_ust_abi_events_exit(void)
1357 struct lttng_ust_session_private
*session_priv
, *tmpsession_priv
;
1359 cds_list_for_each_entry_safe(session_priv
, tmpsession_priv
, &sessions
, node
)
1360 lttng_session_destroy(session_priv
->pub
);
1364 * Enabler management.
1366 struct lttng_event_enabler
*lttng_event_enabler_create(
1367 enum lttng_enabler_format_type format_type
,
1368 struct lttng_ust_abi_event
*event_param
,
1369 struct lttng_channel
*chan
)
1371 struct lttng_event_enabler
*event_enabler
;
1373 event_enabler
= zmalloc(sizeof(*event_enabler
));
1376 event_enabler
->base
.format_type
= format_type
;
1377 CDS_INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1378 CDS_INIT_LIST_HEAD(&event_enabler
->base
.excluder_head
);
1379 memcpy(&event_enabler
->base
.event_param
, event_param
,
1380 sizeof(event_enabler
->base
.event_param
));
1381 event_enabler
->chan
= chan
;
1383 event_enabler
->base
.enabled
= 0;
1384 cds_list_add(&event_enabler
->node
, &event_enabler
->chan
->session
->priv
->enablers_head
);
1385 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1387 return event_enabler
;
1390 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
1391 struct lttng_event_notifier_group
*event_notifier_group
,
1392 enum lttng_enabler_format_type format_type
,
1393 struct lttng_ust_abi_event_notifier
*event_notifier_param
)
1395 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1397 event_notifier_enabler
= zmalloc(sizeof(*event_notifier_enabler
));
1398 if (!event_notifier_enabler
)
1400 event_notifier_enabler
->base
.format_type
= format_type
;
1401 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
1402 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->capture_bytecode_head
);
1403 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.excluder_head
);
1405 event_notifier_enabler
->user_token
= event_notifier_param
->event
.token
;
1406 event_notifier_enabler
->error_counter_index
= event_notifier_param
->error_counter_index
;
1407 event_notifier_enabler
->num_captures
= 0;
1409 memcpy(&event_notifier_enabler
->base
.event_param
.name
,
1410 event_notifier_param
->event
.name
,
1411 sizeof(event_notifier_enabler
->base
.event_param
.name
));
1412 event_notifier_enabler
->base
.event_param
.instrumentation
=
1413 event_notifier_param
->event
.instrumentation
;
1414 event_notifier_enabler
->base
.event_param
.loglevel
=
1415 event_notifier_param
->event
.loglevel
;
1416 event_notifier_enabler
->base
.event_param
.loglevel_type
=
1417 event_notifier_param
->event
.loglevel_type
;
1419 event_notifier_enabler
->base
.enabled
= 0;
1420 event_notifier_enabler
->group
= event_notifier_group
;
1422 cds_list_add(&event_notifier_enabler
->node
,
1423 &event_notifier_group
->enablers_head
);
1425 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1427 return event_notifier_enabler
;
1430 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1432 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1433 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1438 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1440 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1441 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1447 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
1448 struct lttng_ust_bytecode_node
**bytecode
)
1450 (*bytecode
)->enabler
= enabler
;
1451 cds_list_add_tail(&(*bytecode
)->node
, &enabler
->filter_bytecode_head
);
1452 /* Take ownership of bytecode */
1456 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
1457 struct lttng_ust_bytecode_node
**bytecode
)
1459 _lttng_enabler_attach_filter_bytecode(
1460 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1462 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1467 void _lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1468 struct lttng_ust_excluder_node
**excluder
)
1470 (*excluder
)->enabler
= enabler
;
1471 cds_list_add_tail(&(*excluder
)->node
, &enabler
->excluder_head
);
1472 /* Take ownership of excluder */
1476 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler
*event_enabler
,
1477 struct lttng_ust_excluder_node
**excluder
)
1479 _lttng_enabler_attach_exclusion(
1480 lttng_event_enabler_as_enabler(event_enabler
), excluder
);
1482 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1486 int lttng_event_notifier_enabler_enable(
1487 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1489 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
1490 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1495 int lttng_event_notifier_enabler_disable(
1496 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1498 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
1499 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1504 int lttng_event_notifier_enabler_attach_filter_bytecode(
1505 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1506 struct lttng_ust_bytecode_node
**bytecode
)
1508 _lttng_enabler_attach_filter_bytecode(
1509 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1512 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1516 int lttng_event_notifier_enabler_attach_capture_bytecode(
1517 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1518 struct lttng_ust_bytecode_node
**bytecode
)
1520 (*bytecode
)->enabler
= lttng_event_notifier_enabler_as_enabler(
1521 event_notifier_enabler
);
1522 cds_list_add_tail(&(*bytecode
)->node
,
1523 &event_notifier_enabler
->capture_bytecode_head
);
1524 /* Take ownership of bytecode */
1526 event_notifier_enabler
->num_captures
++;
1528 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1532 int lttng_event_notifier_enabler_attach_exclusion(
1533 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1534 struct lttng_ust_excluder_node
**excluder
)
1536 _lttng_enabler_attach_exclusion(
1537 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1540 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1544 int lttng_attach_context(struct lttng_ust_abi_context
*context_param
,
1545 union lttng_ust_abi_args
*uargs
,
1546 struct lttng_ust_ctx
**ctx
, struct lttng_ust_session
*session
)
1549 * We cannot attach a context after trace has been started for a
1550 * session because the metadata does not allow expressing this
1551 * information outside of the original channel scope.
1553 if (session
->priv
->been_active
)
1556 switch (context_param
->ctx
) {
1557 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
1558 return lttng_add_pthread_id_to_ctx(ctx
);
1559 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1561 struct lttng_ust_abi_perf_counter_ctx
*perf_ctx_param
;
1563 perf_ctx_param
= &context_param
->u
.perf_counter
;
1564 return lttng_add_perf_counter_to_ctx(
1565 perf_ctx_param
->type
,
1566 perf_ctx_param
->config
,
1567 perf_ctx_param
->name
,
1570 case LTTNG_UST_ABI_CONTEXT_VTID
:
1571 return lttng_add_vtid_to_ctx(ctx
);
1572 case LTTNG_UST_ABI_CONTEXT_VPID
:
1573 return lttng_add_vpid_to_ctx(ctx
);
1574 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
1575 return lttng_add_procname_to_ctx(ctx
);
1576 case LTTNG_UST_ABI_CONTEXT_IP
:
1577 return lttng_add_ip_to_ctx(ctx
);
1578 case LTTNG_UST_ABI_CONTEXT_CPU_ID
:
1579 return lttng_add_cpu_id_to_ctx(ctx
);
1580 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1581 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1583 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
1584 return lttng_add_cgroup_ns_to_ctx(ctx
);
1585 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
1586 return lttng_add_ipc_ns_to_ctx(ctx
);
1587 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
1588 return lttng_add_mnt_ns_to_ctx(ctx
);
1589 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
1590 return lttng_add_net_ns_to_ctx(ctx
);
1591 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
1592 return lttng_add_pid_ns_to_ctx(ctx
);
1593 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
1594 return lttng_add_time_ns_to_ctx(ctx
);
1595 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
1596 return lttng_add_user_ns_to_ctx(ctx
);
1597 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
1598 return lttng_add_uts_ns_to_ctx(ctx
);
1599 case LTTNG_UST_ABI_CONTEXT_VUID
:
1600 return lttng_add_vuid_to_ctx(ctx
);
1601 case LTTNG_UST_ABI_CONTEXT_VEUID
:
1602 return lttng_add_veuid_to_ctx(ctx
);
1603 case LTTNG_UST_ABI_CONTEXT_VSUID
:
1604 return lttng_add_vsuid_to_ctx(ctx
);
1605 case LTTNG_UST_ABI_CONTEXT_VGID
:
1606 return lttng_add_vgid_to_ctx(ctx
);
1607 case LTTNG_UST_ABI_CONTEXT_VEGID
:
1608 return lttng_add_vegid_to_ctx(ctx
);
1609 case LTTNG_UST_ABI_CONTEXT_VSGID
:
1610 return lttng_add_vsgid_to_ctx(ctx
);
1616 int lttng_event_enabler_attach_context(struct lttng_event_enabler
*enabler
,
1617 struct lttng_ust_abi_context
*context_param
)
1622 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
1624 if (!event_enabler
) {
1627 cds_list_del(&event_enabler
->node
);
1629 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
1631 lttng_destroy_context(event_enabler
->ctx
);
1632 free(event_enabler
);
1636 * lttng_session_sync_event_enablers should be called just before starting a
1640 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
)
1642 struct lttng_event_enabler
*event_enabler
;
1643 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1645 cds_list_for_each_entry(event_enabler
, &session
->priv
->enablers_head
, node
)
1646 lttng_event_enabler_ref_event_recorders(event_enabler
);
1648 * For each event, if at least one of its enablers is enabled,
1649 * and its channel and session transient states are enabled, we
1650 * enable the event, else we disable it.
1652 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1653 struct lttng_enabler_ref
*enabler_ref
;
1654 struct lttng_ust_bytecode_runtime
*runtime
;
1655 int enabled
= 0, has_enablers_without_bytecode
= 0;
1658 cds_list_for_each_entry(enabler_ref
,
1659 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1660 if (enabler_ref
->ref
->enabled
) {
1666 * Enabled state is based on union of enablers, with
1667 * intesection of session and channel transient enable
1670 enabled
= enabled
&& session
->priv
->tstate
&& event_recorder_priv
->pub
->chan
->tstate
;
1672 CMM_STORE_SHARED(event_recorder_priv
->pub
->parent
->enabled
, enabled
);
1674 * Sync tracepoint registration with event enabled
1678 if (!event_recorder_priv
->parent
.registered
)
1679 register_event(event_recorder_priv
->parent
.pub
);
1681 if (event_recorder_priv
->parent
.registered
)
1682 unregister_event(event_recorder_priv
->parent
.pub
);
1685 /* Check if has enablers without bytecode enabled */
1686 cds_list_for_each_entry(enabler_ref
,
1687 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1688 if (enabler_ref
->ref
->enabled
1689 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1690 has_enablers_without_bytecode
= 1;
1694 event_recorder_priv
->pub
->parent
->has_enablers_without_bytecode
=
1695 has_enablers_without_bytecode
;
1697 /* Enable filters */
1698 cds_list_for_each_entry(runtime
,
1699 &event_recorder_priv
->pub
->parent
->filter_bytecode_runtime_head
, node
) {
1700 lttng_bytecode_filter_sync_state(runtime
);
1703 lttng_ust_tp_probe_prune_release_queue();
1706 /* Support for event notifier is introduced by probe provider major version 2. */
1708 bool lttng_ust_probe_supports_event_notifier(struct lttng_ust_probe_desc
*probe_desc
)
1710 return probe_desc
->major
>= 2;
1714 void lttng_create_event_notifier_if_missing(
1715 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1717 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1718 struct lttng_ust_probe_desc
*probe_desc
;
1719 struct cds_list_head
*probe_list
;
1722 probe_list
= lttng_get_probe_list_head();
1724 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
1725 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1728 struct lttng_ust_event_desc
*desc
;
1729 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1730 struct cds_hlist_head
*head
;
1731 struct cds_hlist_node
*node
;
1733 desc
= probe_desc
->event_desc
[i
];
1735 if (!lttng_desc_match_enabler(desc
,
1736 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1740 * Given the current event_notifier group, get the bucket that
1741 * the target event_notifier would be if it was already
1744 head
= borrow_hash_table_bucket(
1745 event_notifier_group
->event_notifiers_ht
.table
,
1746 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
1748 cds_hlist_for_each_entry(event_notifier_priv
, node
, head
, hlist
) {
1750 * Check if event_notifier already exists by checking
1751 * if the event_notifier and enabler share the same
1752 * description and id.
1754 if (event_notifier_priv
->parent
.desc
== desc
&&
1755 event_notifier_priv
->parent
.user_token
== event_notifier_enabler
->user_token
) {
1764 /* Check that the probe supports event notifiers, else report the error. */
1765 if (!lttng_ust_probe_supports_event_notifier(probe_desc
)) {
1766 ERR("Probe \"%s\" contains event \"%s\" which matches an enabled event notifier, "
1767 "but its version (%u.%u) is too old and does not implement event notifiers. "
1768 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1769 "this event will not generate any notification.",
1770 probe_desc
->provider
,
1777 * We need to create a event_notifier for this event probe.
1779 ret
= lttng_event_notifier_create(desc
,
1780 event_notifier_enabler
->user_token
,
1781 event_notifier_enabler
->error_counter_index
,
1782 event_notifier_group
);
1784 DBG("Unable to create event_notifier %s, error %d\n",
1785 probe_desc
->event_desc
[i
]->name
, ret
);
1792 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1795 int lttng_event_notifier_enabler_ref_event_notifiers(
1796 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1798 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1799 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1802 * Only try to create event_notifiers for enablers that are enabled, the user
1803 * might still be attaching filter or exclusion to the
1804 * event_notifier_enabler.
1806 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
)
1809 /* First, ensure that probe event_notifiers are created for this enabler. */
1810 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
1812 /* Link the created event_notifier with its associated enabler. */
1813 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1814 struct lttng_enabler_ref
*enabler_ref
;
1816 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier_priv
->pub
))
1819 enabler_ref
= lttng_enabler_ref(&event_notifier_priv
->parent
.enablers_ref_head
,
1820 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1823 * If no backward ref, create it.
1824 * Add backward ref from event_notifier to enabler.
1826 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1830 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
1831 event_notifier_enabler
);
1832 cds_list_add(&enabler_ref
->node
,
1833 &event_notifier_priv
->parent
.enablers_ref_head
);
1837 * Link filter bytecodes if not linked yet.
1839 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1840 &event_notifier_group
->ctx
,
1841 &event_notifier_priv
->pub
->parent
->filter_bytecode_runtime_head
,
1842 <tng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->filter_bytecode_head
);
1845 * Link capture bytecodes if not linked yet.
1847 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1848 &event_notifier_group
->ctx
, &event_notifier_priv
->pub
->capture_bytecode_runtime_head
,
1849 &event_notifier_enabler
->capture_bytecode_head
);
1851 event_notifier_priv
->num_captures
= event_notifier_enabler
->num_captures
;
1858 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
1860 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1861 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1863 cds_list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
1864 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
1867 * For each event_notifier, if at least one of its enablers is enabled,
1868 * we enable the event_notifier, else we disable it.
1870 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1871 struct lttng_enabler_ref
*enabler_ref
;
1872 struct lttng_ust_bytecode_runtime
*runtime
;
1873 int enabled
= 0, has_enablers_without_bytecode
= 0;
1875 /* Enable event_notifiers */
1876 cds_list_for_each_entry(enabler_ref
,
1877 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1878 if (enabler_ref
->ref
->enabled
) {
1884 CMM_STORE_SHARED(event_notifier_priv
->pub
->parent
->enabled
, enabled
);
1886 * Sync tracepoint registration with event_notifier enabled
1890 if (!event_notifier_priv
->parent
.registered
)
1891 register_event(event_notifier_priv
->parent
.pub
);
1893 if (event_notifier_priv
->parent
.registered
)
1894 unregister_event(event_notifier_priv
->parent
.pub
);
1897 /* Check if has enablers without bytecode enabled */
1898 cds_list_for_each_entry(enabler_ref
,
1899 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1900 if (enabler_ref
->ref
->enabled
1901 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1902 has_enablers_without_bytecode
= 1;
1906 event_notifier_priv
->pub
->parent
->has_enablers_without_bytecode
=
1907 has_enablers_without_bytecode
;
1909 /* Enable filters */
1910 cds_list_for_each_entry(runtime
,
1911 &event_notifier_priv
->pub
->parent
->filter_bytecode_runtime_head
, node
) {
1912 lttng_bytecode_filter_sync_state(runtime
);
1915 /* Enable captures. */
1916 cds_list_for_each_entry(runtime
,
1917 &event_notifier_priv
->pub
->capture_bytecode_runtime_head
, node
) {
1918 lttng_bytecode_capture_sync_state(runtime
);
1921 lttng_ust_tp_probe_prune_release_queue();
1925 * Apply enablers to session events, adding events to session if need
1926 * be. It is required after each modification applied to an active
1927 * session, and right before session "start".
1928 * "lazy" sync means we only sync if required.
1931 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
)
1933 /* We can skip if session is not active */
1934 if (!session
->active
)
1936 lttng_session_sync_event_enablers(session
);
1940 * Update all sessions with the given app context.
1941 * Called with ust lock held.
1942 * This is invoked when an application context gets loaded/unloaded. It
1943 * ensures the context callbacks are in sync with the application
1944 * context (either app context callbacks, or dummy callbacks).
1946 void lttng_ust_context_set_session_provider(const char *name
,
1947 size_t (*get_size
)(struct lttng_ust_ctx_field
*field
, size_t offset
),
1948 void (*record
)(struct lttng_ust_ctx_field
*field
,
1949 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
1950 struct lttng_channel
*chan
),
1951 void (*get_value
)(struct lttng_ust_ctx_field
*field
,
1952 struct lttng_ust_ctx_value
*value
))
1954 struct lttng_ust_session_private
*session_priv
;
1956 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1957 struct lttng_channel
*chan
;
1958 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1961 ret
= lttng_ust_context_set_provider_rcu(&session_priv
->ctx
,
1962 name
, get_size
, record
, get_value
);
1965 cds_list_for_each_entry(chan
, &session_priv
->chan_head
, node
) {
1966 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
1967 name
, get_size
, record
, get_value
);
1971 cds_list_for_each_entry(event_recorder_priv
, &session_priv
->events_head
, node
) {
1972 ret
= lttng_ust_context_set_provider_rcu(&event_recorder_priv
->pub
->ctx
,
1973 name
, get_size
, record
, get_value
);
1981 * Update all event_notifier groups with the given app context.
1982 * Called with ust lock held.
1983 * This is invoked when an application context gets loaded/unloaded. It
1984 * ensures the context callbacks are in sync with the application
1985 * context (either app context callbacks, or dummy callbacks).
1987 void lttng_ust_context_set_event_notifier_group_provider(const char *name
,
1988 size_t (*get_size
)(struct lttng_ust_ctx_field
*field
, size_t offset
),
1989 void (*record
)(struct lttng_ust_ctx_field
*field
,
1990 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
1991 struct lttng_channel
*chan
),
1992 void (*get_value
)(struct lttng_ust_ctx_field
*field
,
1993 struct lttng_ust_ctx_value
*value
))
1995 struct lttng_event_notifier_group
*event_notifier_group
;
1997 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2000 ret
= lttng_ust_context_set_provider_rcu(
2001 &event_notifier_group
->ctx
,
2002 name
, get_size
, record
, get_value
);