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 "../libringbuffer/frontend_types.h"
57 #include "../libcounter/counter.h"
59 #include <lttng/ust-abi.h>
62 * All operations within this file are called by the communication
63 * thread, under ust_lock protection.
66 static CDS_LIST_HEAD(sessions
);
67 static CDS_LIST_HEAD(event_notifier_groups
);
69 struct cds_list_head
*lttng_get_sessions(void)
74 static void _lttng_event_destroy(struct lttng_ust_event_common
*event
);
75 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
78 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
);
80 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
);
82 void lttng_event_notifier_group_sync_enablers(
83 struct lttng_event_notifier_group
*event_notifier_group
);
85 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
88 * Called with ust lock held.
90 int lttng_session_active(void)
92 struct lttng_ust_session_private
*iter
;
94 cds_list_for_each_entry(iter
, &sessions
, node
) {
95 if (iter
->pub
->active
)
102 int lttng_loglevel_match(int loglevel
,
103 unsigned int has_loglevel
,
104 enum lttng_ust_abi_loglevel_type req_type
,
108 loglevel
= TRACE_DEFAULT
;
110 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
111 if (loglevel
<= req_loglevel
112 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
116 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
117 if (loglevel
== req_loglevel
118 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
122 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
124 if (loglevel
<= TRACE_DEBUG
)
131 struct lttng_ust_session
*lttng_session_create(void)
133 struct lttng_ust_session
*session
;
134 struct lttng_ust_session_private
*session_priv
;
137 session
= zmalloc(sizeof(struct lttng_ust_session
));
140 session
->struct_size
= sizeof(struct lttng_ust_session
);
141 session_priv
= zmalloc(sizeof(struct lttng_ust_session_private
));
146 session
->priv
= session_priv
;
147 session_priv
->pub
= session
;
148 if (lttng_context_init_all(&session
->priv
->ctx
)) {
153 CDS_INIT_LIST_HEAD(&session
->priv
->chan_head
);
154 CDS_INIT_LIST_HEAD(&session
->priv
->events_head
);
155 CDS_INIT_LIST_HEAD(&session
->priv
->enums_head
);
156 CDS_INIT_LIST_HEAD(&session
->priv
->enablers_head
);
157 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
158 CDS_INIT_HLIST_HEAD(&session
->priv
->events_ht
.table
[i
]);
159 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
160 CDS_INIT_HLIST_HEAD(&session
->priv
->enums_ht
.table
[i
]);
161 cds_list_add(&session
->priv
->node
, &sessions
);
165 struct lttng_counter
*lttng_ust_counter_create(
166 const char *counter_transport_name
,
167 size_t number_dimensions
, const struct lttng_counter_dimension
*dimensions
)
169 struct lttng_counter_transport
*counter_transport
= NULL
;
170 struct lttng_counter
*counter
= NULL
;
172 counter_transport
= lttng_counter_transport_find(counter_transport_name
);
173 if (!counter_transport
)
175 counter
= zmalloc(sizeof(struct lttng_counter
));
179 counter
->ops
= &counter_transport
->ops
;
180 counter
->transport
= counter_transport
;
182 counter
->counter
= counter
->ops
->counter_create(
183 number_dimensions
, dimensions
, 0,
185 if (!counter
->counter
) {
199 void lttng_ust_counter_destroy(struct lttng_counter
*counter
)
201 counter
->ops
->counter_destroy(counter
->counter
);
205 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
207 struct lttng_event_notifier_group
*event_notifier_group
;
210 event_notifier_group
= zmalloc(sizeof(struct lttng_event_notifier_group
));
211 if (!event_notifier_group
)
214 /* Add all contexts. */
215 if (lttng_context_init_all(&event_notifier_group
->ctx
)) {
216 free(event_notifier_group
);
220 CDS_INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
221 CDS_INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
222 for (i
= 0; i
< LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
; i
++)
223 CDS_INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
225 cds_list_add(&event_notifier_group
->node
, &event_notifier_groups
);
227 return event_notifier_group
;
231 * Only used internally at session destruction.
234 void _lttng_channel_unmap(struct lttng_ust_channel_buffer
*lttng_chan
)
236 struct lttng_ust_lib_ring_buffer_channel
*chan
;
237 struct lttng_ust_shm_handle
*handle
;
239 cds_list_del(<tng_chan
->priv
->node
);
240 lttng_destroy_context(lttng_chan
->priv
->ctx
);
241 chan
= lttng_chan
->priv
->rb_chan
;
242 handle
= chan
->handle
;
243 channel_destroy(chan
, handle
, 0);
244 free(lttng_chan
->parent
);
245 free(lttng_chan
->priv
);
250 void register_event(struct lttng_ust_event_common
*event
)
253 struct lttng_ust_event_desc
*desc
;
255 assert(event
->priv
->registered
== 0);
256 desc
= event
->priv
->desc
;
257 ret
= lttng_ust_tp_probe_register_queue_release(desc
->name
,
258 desc
->probe_callback
,
259 event
, desc
->signature
);
262 event
->priv
->registered
= 1;
266 void unregister_event(struct lttng_ust_event_common
*event
)
269 struct lttng_ust_event_desc
*desc
;
271 assert(event
->priv
->registered
== 1);
272 desc
= event
->priv
->desc
;
273 ret
= lttng_ust_tp_probe_unregister_queue_release(desc
->name
,
274 desc
->probe_callback
,
278 event
->priv
->registered
= 0;
282 void _lttng_event_unregister(struct lttng_ust_event_common
*event
)
284 if (event
->priv
->registered
)
285 unregister_event(event
);
288 void lttng_session_destroy(struct lttng_ust_session
*session
)
290 struct lttng_ust_channel_buffer_private
*chan
, *tmpchan
;
291 struct lttng_ust_event_recorder_private
*event_recorder_priv
, *tmpevent_recorder_priv
;
292 struct lttng_enum
*_enum
, *tmp_enum
;
293 struct lttng_event_enabler
*event_enabler
, *event_tmpenabler
;
295 CMM_ACCESS_ONCE(session
->active
) = 0;
296 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
297 _lttng_event_unregister(event_recorder_priv
->parent
.pub
);
299 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
300 lttng_ust_tp_probe_prune_release_queue();
301 cds_list_for_each_entry_safe(event_enabler
, event_tmpenabler
,
302 &session
->priv
->enablers_head
, node
)
303 lttng_event_enabler_destroy(event_enabler
);
304 cds_list_for_each_entry_safe(event_recorder_priv
, tmpevent_recorder_priv
,
305 &session
->priv
->events_head
, node
)
306 _lttng_event_destroy(event_recorder_priv
->parent
.pub
);
307 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
308 &session
->priv
->enums_head
, node
)
309 _lttng_enum_destroy(_enum
);
310 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->priv
->chan_head
, node
)
311 _lttng_channel_unmap(chan
->pub
);
312 cds_list_del(&session
->priv
->node
);
313 lttng_destroy_context(session
->priv
->ctx
);
318 void lttng_event_notifier_group_destroy(
319 struct lttng_event_notifier_group
*event_notifier_group
)
322 struct lttng_event_notifier_enabler
*notifier_enabler
, *tmpnotifier_enabler
;
323 struct lttng_ust_event_notifier_private
*event_notifier_priv
, *tmpevent_notifier_priv
;
325 if (!event_notifier_group
) {
329 cds_list_for_each_entry(event_notifier_priv
,
330 &event_notifier_group
->event_notifiers_head
, node
)
331 _lttng_event_unregister(event_notifier_priv
->parent
.pub
);
333 lttng_ust_urcu_synchronize_rcu();
335 cds_list_for_each_entry_safe(notifier_enabler
, tmpnotifier_enabler
,
336 &event_notifier_group
->enablers_head
, node
)
337 lttng_event_notifier_enabler_destroy(notifier_enabler
);
339 cds_list_for_each_entry_safe(event_notifier_priv
, tmpevent_notifier_priv
,
340 &event_notifier_group
->event_notifiers_head
, node
)
341 _lttng_event_destroy(event_notifier_priv
->parent
.pub
);
343 if (event_notifier_group
->error_counter
)
344 lttng_ust_counter_destroy(event_notifier_group
->error_counter
);
346 /* Close the notification fd to the listener of event_notifiers. */
348 lttng_ust_lock_fd_tracker();
349 close_ret
= close(event_notifier_group
->notification_fd
);
351 lttng_ust_delete_fd_from_tracker(
352 event_notifier_group
->notification_fd
);
357 lttng_ust_unlock_fd_tracker();
359 cds_list_del(&event_notifier_group
->node
);
361 free(event_notifier_group
);
365 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
367 struct lttng_ust_bytecode_node
*filter_node
, *tmp_filter_node
;
368 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
374 /* Destroy filter bytecode */
375 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
376 &enabler
->filter_bytecode_head
, node
) {
380 /* Destroy excluders */
381 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
382 &enabler
->excluder_head
, node
) {
387 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
389 if (!event_notifier_enabler
) {
393 cds_list_del(&event_notifier_enabler
->node
);
395 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
397 free(event_notifier_enabler
);
401 int lttng_enum_create(struct lttng_ust_enum_desc
*desc
,
402 struct lttng_ust_session
*session
)
404 const char *enum_name
= desc
->name
;
405 struct lttng_enum
*_enum
;
406 struct cds_hlist_head
*head
;
408 size_t name_len
= strlen(enum_name
);
412 /* Check if this enum is already registered for this session. */
413 hash
= jhash(enum_name
, name_len
, 0);
414 head
= &session
->priv
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
416 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
422 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
423 if (notify_socket
< 0) {
428 _enum
= zmalloc(sizeof(*_enum
));
433 _enum
->session
= session
;
436 ret
= ustcomm_register_enum(notify_socket
,
443 DBG("Error (%d) registering enumeration to sessiond", ret
);
444 goto sessiond_register_error
;
446 cds_list_add(&_enum
->node
, &session
->priv
->enums_head
);
447 cds_hlist_add_head(&_enum
->hlist
, head
);
450 sessiond_register_error
:
459 int lttng_create_enum_check(struct lttng_ust_type_common
*type
,
460 struct lttng_ust_session
*session
)
462 switch (type
->type
) {
463 case lttng_ust_type_enum
:
465 struct lttng_ust_enum_desc
*enum_desc
;
468 enum_desc
= lttng_ust_get_type_enum(type
)->desc
;
469 ret
= lttng_enum_create(enum_desc
, session
);
470 if (ret
&& ret
!= -EEXIST
) {
471 DBG("Unable to create enum error: (%d)", ret
);
476 case lttng_ust_type_dynamic
:
478 struct lttng_ust_event_field
*tag_field_generic
;
479 struct lttng_ust_enum_desc
*enum_desc
;
482 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
483 enum_desc
= lttng_ust_get_type_enum(tag_field_generic
->type
)->desc
;
484 ret
= lttng_enum_create(enum_desc
, session
);
485 if (ret
&& ret
!= -EEXIST
) {
486 DBG("Unable to create enum error: (%d)", ret
);
492 /* TODO: nested types when they become supported. */
499 int lttng_create_all_event_enums(size_t nr_fields
,
500 struct lttng_ust_event_field
**event_fields
,
501 struct lttng_ust_session
*session
)
506 /* For each field, ensure enum is part of the session. */
507 for (i
= 0; i
< nr_fields
; i
++) {
508 struct lttng_ust_type_common
*type
= event_fields
[i
]->type
;
510 ret
= lttng_create_enum_check(type
, session
);
518 int lttng_create_all_ctx_enums(size_t nr_fields
,
519 struct lttng_ust_ctx_field
**ctx_fields
,
520 struct lttng_ust_session
*session
)
525 /* For each field, ensure enum is part of the session. */
526 for (i
= 0; i
< nr_fields
; i
++) {
527 struct lttng_ust_type_common
*type
= ctx_fields
[i
]->event_field
->type
;
529 ret
= lttng_create_enum_check(type
, session
);
537 * Ensure that a state-dump will be performed for this session at the end
538 * of the current handle_message().
540 int lttng_session_statedump(struct lttng_ust_session
*session
)
542 session
->priv
->statedump_pending
= 1;
543 lttng_ust_sockinfo_session_enabled(session
->priv
->owner
);
547 int lttng_session_enable(struct lttng_ust_session
*session
)
550 struct lttng_ust_channel_buffer_private
*chan
;
553 if (session
->active
) {
558 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
559 if (notify_socket
< 0)
560 return notify_socket
;
562 /* Set transient enabler state to "enabled" */
563 session
->priv
->tstate
= 1;
565 /* We need to sync enablers with session before activation. */
566 lttng_session_sync_event_enablers(session
);
569 * Snapshot the number of events per channel to know the type of header
572 cds_list_for_each_entry(chan
, &session
->priv
->chan_head
, node
) {
573 struct lttng_ust_ctx
*ctx
;
574 struct lttng_ust_ctx_field
**fields
= NULL
;
575 size_t nr_fields
= 0;
578 /* don't change it if session stop/restart */
579 if (chan
->header_type
)
583 nr_fields
= ctx
->nr_fields
;
584 fields
= ctx
->fields
;
585 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
588 DBG("Error (%d) adding enum to session", ret
);
592 ret
= ustcomm_register_channel(notify_socket
,
601 DBG("Error (%d) registering channel to sessiond", ret
);
604 if (chan_id
!= chan
->id
) {
605 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
611 /* Set atomically the state to "active" */
612 CMM_ACCESS_ONCE(session
->active
) = 1;
613 CMM_ACCESS_ONCE(session
->priv
->been_active
) = 1;
615 ret
= lttng_session_statedump(session
);
622 int lttng_session_disable(struct lttng_ust_session
*session
)
626 if (!session
->active
) {
630 /* Set atomically the state to "inactive" */
631 CMM_ACCESS_ONCE(session
->active
) = 0;
633 /* Set transient enabler state to "disabled" */
634 session
->priv
->tstate
= 0;
635 lttng_session_sync_event_enablers(session
);
640 int lttng_channel_enable(struct lttng_ust_channel_common
*lttng_channel
)
644 if (lttng_channel
->enabled
) {
648 /* Set transient enabler state to "enabled" */
649 lttng_channel
->priv
->tstate
= 1;
650 lttng_session_sync_event_enablers(lttng_channel
->session
);
651 /* Set atomically the state to "enabled" */
652 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 1;
657 int lttng_channel_disable(struct lttng_ust_channel_common
*lttng_channel
)
661 if (!lttng_channel
->enabled
) {
665 /* Set atomically the state to "disabled" */
666 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 0;
667 /* Set transient enabler state to "enabled" */
668 lttng_channel
->priv
->tstate
= 0;
669 lttng_session_sync_event_enablers(lttng_channel
->session
);
675 struct cds_hlist_head
*borrow_hash_table_bucket(
676 struct cds_hlist_head
*hash_table
,
677 unsigned int hash_table_size
,
678 struct lttng_ust_event_desc
*desc
)
680 const char *event_name
;
684 event_name
= desc
->name
;
685 name_len
= strlen(event_name
);
687 hash
= jhash(event_name
, name_len
, 0);
688 return &hash_table
[hash
& (hash_table_size
- 1)];
692 * Supports event creation while tracing session is active.
695 int lttng_event_recorder_create(struct lttng_ust_event_desc
*desc
,
696 struct lttng_ust_channel_buffer
*chan
)
698 struct lttng_ust_event_recorder
*event_recorder
;
699 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
700 struct lttng_ust_session
*session
= chan
->parent
->session
;
701 struct cds_hlist_head
*head
;
703 int notify_socket
, loglevel
;
706 head
= borrow_hash_table_bucket(chan
->parent
->session
->priv
->events_ht
.table
,
707 LTTNG_UST_EVENT_HT_SIZE
, desc
);
709 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
710 if (notify_socket
< 0) {
715 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
718 DBG("Error (%d) adding enum to session", ret
);
719 goto create_enum_error
;
723 * Check if loglevel match. Refuse to connect event if not.
725 event_recorder
= zmalloc(sizeof(struct lttng_ust_event_recorder
));
726 if (!event_recorder
) {
730 event_recorder
->struct_size
= sizeof(struct lttng_ust_event_recorder
);
732 event_recorder
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
733 if (!event_recorder
->parent
) {
737 event_recorder
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
738 event_recorder
->parent
->type
= LTTNG_UST_EVENT_TYPE_RECORDER
;
739 event_recorder
->parent
->child
= event_recorder
;
741 event_recorder_priv
= zmalloc(sizeof(struct lttng_ust_event_recorder_private
));
742 if (!event_recorder_priv
) {
746 event_recorder
->priv
= event_recorder_priv
;
747 event_recorder_priv
->pub
= event_recorder
;
748 event_recorder
->parent
->priv
= &event_recorder_priv
->parent
;
749 event_recorder_priv
->parent
.pub
= event_recorder
->parent
;
751 event_recorder
->chan
= chan
;
753 /* Event will be enabled by enabler sync. */
754 event_recorder
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
755 event_recorder
->parent
->enabled
= 0;
756 event_recorder
->parent
->priv
->registered
= 0;
757 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->filter_bytecode_runtime_head
);
758 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->enablers_ref_head
);
759 event_recorder
->parent
->priv
->desc
= desc
;
762 loglevel
= *(*event_recorder
->parent
->priv
->desc
->loglevel
);
764 loglevel
= TRACE_DEFAULT
;
765 if (desc
->model_emf_uri
)
766 uri
= *(desc
->model_emf_uri
);
770 /* Fetch event ID from sessiond */
771 ret
= ustcomm_register_event(notify_socket
,
774 chan
->priv
->parent
.objd
,
781 &event_recorder
->id
);
783 DBG("Error (%d) registering event to sessiond", ret
);
784 goto sessiond_register_error
;
787 cds_list_add(&event_recorder_priv
->node
, &chan
->parent
->session
->priv
->events_head
);
788 cds_hlist_add_head(&event_recorder_priv
->hlist
, head
);
791 sessiond_register_error
:
792 free(event_recorder_priv
);
794 free(event_recorder
->parent
);
796 free(event_recorder
);
804 int lttng_event_notifier_create(struct lttng_ust_event_desc
*desc
,
805 uint64_t token
, uint64_t error_counter_index
,
806 struct lttng_event_notifier_group
*event_notifier_group
)
808 struct lttng_ust_event_notifier
*event_notifier
;
809 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
810 struct cds_hlist_head
*head
;
814 * Get the hashtable bucket the created lttng_event_notifier object
815 * should be inserted.
817 head
= borrow_hash_table_bucket(
818 event_notifier_group
->event_notifiers_ht
.table
,
819 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
821 event_notifier
= zmalloc(sizeof(struct lttng_ust_event_notifier
));
822 if (!event_notifier
) {
826 event_notifier
->struct_size
= sizeof(struct lttng_ust_event_notifier
);
828 event_notifier
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
829 if (!event_notifier
->parent
) {
833 event_notifier
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
834 event_notifier
->parent
->type
= LTTNG_UST_EVENT_TYPE_NOTIFIER
;
835 event_notifier
->parent
->child
= event_notifier
;
837 event_notifier_priv
= zmalloc(sizeof(struct lttng_ust_event_notifier_private
));
838 if (!event_notifier_priv
) {
842 event_notifier
->priv
= event_notifier_priv
;
843 event_notifier_priv
->pub
= event_notifier
;
844 event_notifier
->parent
->priv
= &event_notifier_priv
->parent
;
845 event_notifier_priv
->parent
.pub
= event_notifier
->parent
;
847 event_notifier_priv
->group
= event_notifier_group
;
848 event_notifier_priv
->parent
.user_token
= token
;
849 event_notifier_priv
->error_counter_index
= error_counter_index
;
851 /* Event notifier will be enabled by enabler sync. */
852 event_notifier
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
853 event_notifier
->parent
->enabled
= 0;
854 event_notifier_priv
->parent
.registered
= 0;
856 CDS_INIT_LIST_HEAD(&event_notifier
->parent
->priv
->filter_bytecode_runtime_head
);
857 CDS_INIT_LIST_HEAD(&event_notifier
->priv
->capture_bytecode_runtime_head
);
858 CDS_INIT_LIST_HEAD(&event_notifier_priv
->parent
.enablers_ref_head
);
859 event_notifier_priv
->parent
.desc
= desc
;
860 event_notifier
->notification_send
= lttng_event_notifier_notification_send
;
862 cds_list_add(&event_notifier_priv
->node
,
863 &event_notifier_group
->event_notifiers_head
);
864 cds_hlist_add_head(&event_notifier_priv
->hlist
, head
);
869 free(event_notifier
->parent
);
871 free(event_notifier
);
877 int lttng_desc_match_star_glob_enabler(struct lttng_ust_event_desc
*desc
,
878 struct lttng_enabler
*enabler
)
881 unsigned int has_loglevel
= 0;
883 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_STAR_GLOB
);
884 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
885 desc
->name
, SIZE_MAX
))
887 if (desc
->loglevel
) {
888 loglevel
= *(*desc
->loglevel
);
891 if (!lttng_loglevel_match(loglevel
,
893 enabler
->event_param
.loglevel_type
,
894 enabler
->event_param
.loglevel
))
900 int lttng_desc_match_event_enabler(struct lttng_ust_event_desc
*desc
,
901 struct lttng_enabler
*enabler
)
904 unsigned int has_loglevel
= 0;
906 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_EVENT
);
907 if (strcmp(desc
->name
, enabler
->event_param
.name
))
909 if (desc
->loglevel
) {
910 loglevel
= *(*desc
->loglevel
);
913 if (!lttng_loglevel_match(loglevel
,
915 enabler
->event_param
.loglevel_type
,
916 enabler
->event_param
.loglevel
))
922 int lttng_desc_match_enabler(struct lttng_ust_event_desc
*desc
,
923 struct lttng_enabler
*enabler
)
925 switch (enabler
->format_type
) {
926 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
928 struct lttng_ust_excluder_node
*excluder
;
930 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
935 * If the matching event matches with an excluder,
936 * return 'does not match'
938 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
941 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
945 excluder_name
= (char *) (excluder
->excluder
.names
)
946 + count
* LTTNG_UST_ABI_SYM_NAME_LEN
;
947 len
= strnlen(excluder_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
948 if (len
> 0 && strutils_star_glob_match(excluder_name
, len
, desc
->name
, SIZE_MAX
))
954 case LTTNG_ENABLER_FORMAT_EVENT
:
955 return lttng_desc_match_event_enabler(desc
, enabler
);
962 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
963 struct lttng_ust_event_recorder
*event_recorder
)
965 if (lttng_desc_match_enabler(event_recorder
->parent
->priv
->desc
,
966 lttng_event_enabler_as_enabler(event_enabler
))
967 && event_recorder
->chan
== event_enabler
->chan
)
974 int lttng_event_notifier_enabler_match_event_notifier(
975 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
976 struct lttng_ust_event_notifier
*event_notifier
)
978 int desc_matches
= lttng_desc_match_enabler(event_notifier
->priv
->parent
.desc
,
979 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
981 if (desc_matches
&& event_notifier
->priv
->group
== event_notifier_enabler
->group
&&
982 event_notifier
->priv
->parent
.user_token
== event_notifier_enabler
->user_token
)
989 struct lttng_enabler_ref
*lttng_enabler_ref(
990 struct cds_list_head
*enabler_ref_list
,
991 struct lttng_enabler
*enabler
)
993 struct lttng_enabler_ref
*enabler_ref
;
995 cds_list_for_each_entry(enabler_ref
, enabler_ref_list
, node
) {
996 if (enabler_ref
->ref
== enabler
)
1003 * Create struct lttng_event if it is missing and present in the list of
1004 * tracepoint probes.
1007 void lttng_create_event_recorder_if_missing(struct lttng_event_enabler
*event_enabler
)
1009 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1010 struct lttng_ust_probe_desc
*probe_desc
;
1011 struct lttng_ust_event_desc
*desc
;
1012 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1014 struct cds_list_head
*probe_list
;
1016 probe_list
= lttng_get_probe_list_head();
1018 * For each probe event, if we find that a probe event matches
1019 * our enabler, create an associated lttng_event if not
1022 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
1023 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1026 struct cds_hlist_head
*head
;
1027 struct cds_hlist_node
*node
;
1029 desc
= probe_desc
->event_desc
[i
];
1030 if (!lttng_desc_match_enabler(desc
,
1031 lttng_event_enabler_as_enabler(event_enabler
)))
1034 head
= borrow_hash_table_bucket(
1035 session
->priv
->events_ht
.table
,
1036 LTTNG_UST_EVENT_HT_SIZE
, desc
);
1038 cds_hlist_for_each_entry(event_recorder_priv
, node
, head
, hlist
) {
1039 if (event_recorder_priv
->parent
.desc
== desc
1040 && event_recorder_priv
->pub
->chan
== event_enabler
->chan
) {
1049 * We need to create an event for this
1052 ret
= lttng_event_recorder_create(probe_desc
->event_desc
[i
],
1053 event_enabler
->chan
);
1055 DBG("Unable to create event %s, error %d\n",
1056 probe_desc
->event_desc
[i
]->name
, ret
);
1063 void probe_provider_event_for_each(struct lttng_ust_probe_desc
*provider_desc
,
1064 void (*event_func
)(struct lttng_ust_event_common
*event
))
1066 struct cds_hlist_node
*node
, *tmp_node
;
1067 struct cds_list_head
*sessionsp
;
1070 /* Get handle on list of sessions. */
1071 sessionsp
= lttng_get_sessions();
1074 * Iterate over all events in the probe provider descriptions and
1075 * sessions to queue the unregistration of the events.
1077 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
1078 struct lttng_ust_event_desc
*event_desc
;
1079 struct lttng_event_notifier_group
*event_notifier_group
;
1080 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1081 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1082 struct lttng_ust_session_private
*session_priv
;
1083 struct cds_hlist_head
*head
;
1085 event_desc
= provider_desc
->event_desc
[i
];
1088 * Iterate over all session to find the current event
1091 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
1093 * Get the list of events in the hashtable bucket and
1094 * iterate to find the event matching this descriptor.
1096 head
= borrow_hash_table_bucket(
1097 session_priv
->events_ht
.table
,
1098 LTTNG_UST_EVENT_HT_SIZE
, event_desc
);
1100 cds_hlist_for_each_entry_safe(event_recorder_priv
, node
, tmp_node
, head
, hlist
) {
1101 if (event_desc
== event_recorder_priv
->parent
.desc
) {
1102 event_func(event_recorder_priv
->parent
.pub
);
1109 * Iterate over all event_notifier groups to find the current event
1112 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1114 * Get the list of event_notifiers in the hashtable bucket and
1115 * iterate to find the event_notifier matching this
1118 head
= borrow_hash_table_bucket(
1119 event_notifier_group
->event_notifiers_ht
.table
,
1120 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, event_desc
);
1122 cds_hlist_for_each_entry_safe(event_notifier_priv
, node
, tmp_node
, head
, hlist
) {
1123 if (event_desc
== event_notifier_priv
->parent
.desc
) {
1124 event_func(event_notifier_priv
->parent
.pub
);
1133 void _event_enum_destroy(struct lttng_ust_event_common
*event
)
1136 switch (event
->type
) {
1137 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1139 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1140 struct lttng_ust_session
*session
= event_recorder
->chan
->parent
->session
;
1143 /* Destroy enums of the current event. */
1144 for (i
= 0; i
< event_recorder
->parent
->priv
->desc
->nr_fields
; i
++) {
1145 struct lttng_ust_enum_desc
*enum_desc
;
1146 struct lttng_ust_event_field
*field
;
1147 struct lttng_enum
*curr_enum
;
1149 field
= event_recorder
->parent
->priv
->desc
->fields
[i
];
1150 switch (field
->type
->type
) {
1151 case lttng_ust_type_enum
:
1152 enum_desc
= lttng_ust_get_type_enum(field
->type
)->desc
;
1158 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
1160 _lttng_enum_destroy(curr_enum
);
1165 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1170 /* Destroy event. */
1171 _lttng_event_destroy(event
);
1175 * Iterate over all the UST sessions to unregister and destroy all probes from
1176 * the probe provider descriptor received as argument. Must me called with the
1179 void lttng_probe_provider_unregister_events(
1180 struct lttng_ust_probe_desc
*provider_desc
)
1183 * Iterate over all events in the probe provider descriptions and sessions
1184 * to queue the unregistration of the events.
1186 probe_provider_event_for_each(provider_desc
, _lttng_event_unregister
);
1188 /* Wait for grace period. */
1189 lttng_ust_urcu_synchronize_rcu();
1190 /* Prune the unregistration queue. */
1191 lttng_ust_tp_probe_prune_release_queue();
1194 * It is now safe to destroy the events and remove them from the event list
1197 probe_provider_event_for_each(provider_desc
, _event_enum_destroy
);
1201 * Create events associated with an event enabler (if not already present),
1202 * and add backward reference from the event to the enabler.
1205 int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler
*event_enabler
)
1207 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1208 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1210 if (!lttng_event_enabler_as_enabler(event_enabler
)->enabled
)
1213 /* First ensure that probe events are created for this enabler. */
1214 lttng_create_event_recorder_if_missing(event_enabler
);
1216 /* For each event matching enabler in session event list. */
1217 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1218 struct lttng_enabler_ref
*enabler_ref
;
1220 if (!lttng_event_enabler_match_event(event_enabler
, event_recorder_priv
->pub
))
1223 enabler_ref
= lttng_enabler_ref(&event_recorder_priv
->parent
.enablers_ref_head
,
1224 lttng_event_enabler_as_enabler(event_enabler
));
1227 * If no backward ref, create it.
1228 * Add backward ref from event to enabler.
1230 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1233 enabler_ref
->ref
= lttng_event_enabler_as_enabler(
1235 cds_list_add(&enabler_ref
->node
,
1236 &event_recorder_priv
->parent
.enablers_ref_head
);
1240 * Link filter bytecodes if not linked yet.
1242 lttng_enabler_link_bytecode(event_recorder_priv
->parent
.desc
,
1243 &session
->priv
->ctx
,
1244 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
,
1245 <tng_event_enabler_as_enabler(event_enabler
)->filter_bytecode_head
);
1247 /* TODO: merge event context. */
1254 * Called at library load: connect the probe on all enablers matching
1256 * Called with session mutex held.
1258 int lttng_fix_pending_events(void)
1260 struct lttng_ust_session_private
*session_priv
;
1262 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1263 lttng_session_lazy_sync_event_enablers(session_priv
->pub
);
1268 int lttng_fix_pending_event_notifiers(void)
1270 struct lttng_event_notifier_group
*event_notifier_group
;
1272 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1273 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1279 * For each session of the owner thread, execute pending statedump.
1280 * Only dump state for the sessions owned by the caller thread, because
1281 * we don't keep ust_lock across the entire iteration.
1283 void lttng_handle_pending_statedump(void *owner
)
1285 struct lttng_ust_session_private
*session_priv
;
1287 /* Execute state dump */
1288 do_lttng_ust_statedump(owner
);
1290 /* Clear pending state dump */
1294 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1295 if (session_priv
->owner
!= owner
)
1297 if (!session_priv
->statedump_pending
)
1299 session_priv
->statedump_pending
= 0;
1307 void _lttng_event_destroy(struct lttng_ust_event_common
*event
)
1309 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1311 lttng_free_event_filter_runtime(event
);
1312 /* Free event enabler refs */
1313 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1314 &event
->priv
->enablers_ref_head
, node
)
1317 switch (event
->type
) {
1318 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1320 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1322 /* Remove from event list. */
1323 cds_list_del(&event_recorder
->priv
->node
);
1324 /* Remove from event hash table. */
1325 cds_hlist_del(&event_recorder
->priv
->hlist
);
1327 lttng_destroy_context(event_recorder
->priv
->ctx
);
1328 free(event_recorder
->parent
);
1329 free(event_recorder
->priv
);
1330 free(event_recorder
);
1333 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1335 struct lttng_ust_event_notifier
*event_notifier
= event
->child
;
1337 /* Remove from event list. */
1338 cds_list_del(&event_notifier
->priv
->node
);
1339 /* Remove from event hash table. */
1340 cds_hlist_del(&event_notifier
->priv
->hlist
);
1342 free(event_notifier
->priv
);
1343 free(event_notifier
->parent
);
1344 free(event_notifier
);
1353 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1355 cds_list_del(&_enum
->node
);
1356 cds_hlist_del(&_enum
->hlist
);
1360 void lttng_ust_abi_events_exit(void)
1362 struct lttng_ust_session_private
*session_priv
, *tmpsession_priv
;
1364 cds_list_for_each_entry_safe(session_priv
, tmpsession_priv
, &sessions
, node
)
1365 lttng_session_destroy(session_priv
->pub
);
1369 * Enabler management.
1371 struct lttng_event_enabler
*lttng_event_enabler_create(
1372 enum lttng_enabler_format_type format_type
,
1373 struct lttng_ust_abi_event
*event_param
,
1374 struct lttng_ust_channel_buffer
*chan
)
1376 struct lttng_event_enabler
*event_enabler
;
1378 event_enabler
= zmalloc(sizeof(*event_enabler
));
1381 event_enabler
->base
.format_type
= format_type
;
1382 CDS_INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1383 CDS_INIT_LIST_HEAD(&event_enabler
->base
.excluder_head
);
1384 memcpy(&event_enabler
->base
.event_param
, event_param
,
1385 sizeof(event_enabler
->base
.event_param
));
1386 event_enabler
->chan
= chan
;
1388 event_enabler
->base
.enabled
= 0;
1389 cds_list_add(&event_enabler
->node
, &event_enabler
->chan
->parent
->session
->priv
->enablers_head
);
1390 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1392 return event_enabler
;
1395 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
1396 struct lttng_event_notifier_group
*event_notifier_group
,
1397 enum lttng_enabler_format_type format_type
,
1398 struct lttng_ust_abi_event_notifier
*event_notifier_param
)
1400 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1402 event_notifier_enabler
= zmalloc(sizeof(*event_notifier_enabler
));
1403 if (!event_notifier_enabler
)
1405 event_notifier_enabler
->base
.format_type
= format_type
;
1406 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
1407 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->capture_bytecode_head
);
1408 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.excluder_head
);
1410 event_notifier_enabler
->user_token
= event_notifier_param
->event
.token
;
1411 event_notifier_enabler
->error_counter_index
= event_notifier_param
->error_counter_index
;
1412 event_notifier_enabler
->num_captures
= 0;
1414 memcpy(&event_notifier_enabler
->base
.event_param
.name
,
1415 event_notifier_param
->event
.name
,
1416 sizeof(event_notifier_enabler
->base
.event_param
.name
));
1417 event_notifier_enabler
->base
.event_param
.instrumentation
=
1418 event_notifier_param
->event
.instrumentation
;
1419 event_notifier_enabler
->base
.event_param
.loglevel
=
1420 event_notifier_param
->event
.loglevel
;
1421 event_notifier_enabler
->base
.event_param
.loglevel_type
=
1422 event_notifier_param
->event
.loglevel_type
;
1424 event_notifier_enabler
->base
.enabled
= 0;
1425 event_notifier_enabler
->group
= event_notifier_group
;
1427 cds_list_add(&event_notifier_enabler
->node
,
1428 &event_notifier_group
->enablers_head
);
1430 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1432 return event_notifier_enabler
;
1435 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1437 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1438 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1443 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1445 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1446 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1452 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
1453 struct lttng_ust_bytecode_node
**bytecode
)
1455 (*bytecode
)->enabler
= enabler
;
1456 cds_list_add_tail(&(*bytecode
)->node
, &enabler
->filter_bytecode_head
);
1457 /* Take ownership of bytecode */
1461 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
1462 struct lttng_ust_bytecode_node
**bytecode
)
1464 _lttng_enabler_attach_filter_bytecode(
1465 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1467 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1472 void _lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1473 struct lttng_ust_excluder_node
**excluder
)
1475 (*excluder
)->enabler
= enabler
;
1476 cds_list_add_tail(&(*excluder
)->node
, &enabler
->excluder_head
);
1477 /* Take ownership of excluder */
1481 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler
*event_enabler
,
1482 struct lttng_ust_excluder_node
**excluder
)
1484 _lttng_enabler_attach_exclusion(
1485 lttng_event_enabler_as_enabler(event_enabler
), excluder
);
1487 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1491 int lttng_event_notifier_enabler_enable(
1492 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1494 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
1495 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1500 int lttng_event_notifier_enabler_disable(
1501 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1503 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
1504 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1509 int lttng_event_notifier_enabler_attach_filter_bytecode(
1510 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1511 struct lttng_ust_bytecode_node
**bytecode
)
1513 _lttng_enabler_attach_filter_bytecode(
1514 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1517 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1521 int lttng_event_notifier_enabler_attach_capture_bytecode(
1522 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1523 struct lttng_ust_bytecode_node
**bytecode
)
1525 (*bytecode
)->enabler
= lttng_event_notifier_enabler_as_enabler(
1526 event_notifier_enabler
);
1527 cds_list_add_tail(&(*bytecode
)->node
,
1528 &event_notifier_enabler
->capture_bytecode_head
);
1529 /* Take ownership of bytecode */
1531 event_notifier_enabler
->num_captures
++;
1533 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1537 int lttng_event_notifier_enabler_attach_exclusion(
1538 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1539 struct lttng_ust_excluder_node
**excluder
)
1541 _lttng_enabler_attach_exclusion(
1542 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1545 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1549 int lttng_attach_context(struct lttng_ust_abi_context
*context_param
,
1550 union lttng_ust_abi_args
*uargs
,
1551 struct lttng_ust_ctx
**ctx
, struct lttng_ust_session
*session
)
1554 * We cannot attach a context after trace has been started for a
1555 * session because the metadata does not allow expressing this
1556 * information outside of the original channel scope.
1558 if (session
->priv
->been_active
)
1561 switch (context_param
->ctx
) {
1562 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
1563 return lttng_add_pthread_id_to_ctx(ctx
);
1564 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1566 struct lttng_ust_abi_perf_counter_ctx
*perf_ctx_param
;
1568 perf_ctx_param
= &context_param
->u
.perf_counter
;
1569 return lttng_add_perf_counter_to_ctx(
1570 perf_ctx_param
->type
,
1571 perf_ctx_param
->config
,
1572 perf_ctx_param
->name
,
1575 case LTTNG_UST_ABI_CONTEXT_VTID
:
1576 return lttng_add_vtid_to_ctx(ctx
);
1577 case LTTNG_UST_ABI_CONTEXT_VPID
:
1578 return lttng_add_vpid_to_ctx(ctx
);
1579 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
1580 return lttng_add_procname_to_ctx(ctx
);
1581 case LTTNG_UST_ABI_CONTEXT_IP
:
1582 return lttng_add_ip_to_ctx(ctx
);
1583 case LTTNG_UST_ABI_CONTEXT_CPU_ID
:
1584 return lttng_add_cpu_id_to_ctx(ctx
);
1585 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1586 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1588 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
1589 return lttng_add_cgroup_ns_to_ctx(ctx
);
1590 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
1591 return lttng_add_ipc_ns_to_ctx(ctx
);
1592 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
1593 return lttng_add_mnt_ns_to_ctx(ctx
);
1594 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
1595 return lttng_add_net_ns_to_ctx(ctx
);
1596 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
1597 return lttng_add_pid_ns_to_ctx(ctx
);
1598 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
1599 return lttng_add_time_ns_to_ctx(ctx
);
1600 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
1601 return lttng_add_user_ns_to_ctx(ctx
);
1602 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
1603 return lttng_add_uts_ns_to_ctx(ctx
);
1604 case LTTNG_UST_ABI_CONTEXT_VUID
:
1605 return lttng_add_vuid_to_ctx(ctx
);
1606 case LTTNG_UST_ABI_CONTEXT_VEUID
:
1607 return lttng_add_veuid_to_ctx(ctx
);
1608 case LTTNG_UST_ABI_CONTEXT_VSUID
:
1609 return lttng_add_vsuid_to_ctx(ctx
);
1610 case LTTNG_UST_ABI_CONTEXT_VGID
:
1611 return lttng_add_vgid_to_ctx(ctx
);
1612 case LTTNG_UST_ABI_CONTEXT_VEGID
:
1613 return lttng_add_vegid_to_ctx(ctx
);
1614 case LTTNG_UST_ABI_CONTEXT_VSGID
:
1615 return lttng_add_vsgid_to_ctx(ctx
);
1621 int lttng_event_enabler_attach_context(struct lttng_event_enabler
*enabler
,
1622 struct lttng_ust_abi_context
*context_param
)
1627 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
1629 if (!event_enabler
) {
1632 cds_list_del(&event_enabler
->node
);
1634 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
1636 lttng_destroy_context(event_enabler
->ctx
);
1637 free(event_enabler
);
1641 * lttng_session_sync_event_enablers should be called just before starting a
1645 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
)
1647 struct lttng_event_enabler
*event_enabler
;
1648 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1650 cds_list_for_each_entry(event_enabler
, &session
->priv
->enablers_head
, node
)
1651 lttng_event_enabler_ref_event_recorders(event_enabler
);
1653 * For each event, if at least one of its enablers is enabled,
1654 * and its channel and session transient states are enabled, we
1655 * enable the event, else we disable it.
1657 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1658 struct lttng_enabler_ref
*enabler_ref
;
1659 struct lttng_ust_bytecode_runtime
*runtime
;
1660 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1664 cds_list_for_each_entry(enabler_ref
,
1665 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1666 if (enabler_ref
->ref
->enabled
) {
1672 * Enabled state is based on union of enablers, with
1673 * intesection of session and channel transient enable
1676 enabled
= enabled
&& session
->priv
->tstate
&& event_recorder_priv
->pub
->chan
->priv
->parent
.tstate
;
1678 CMM_STORE_SHARED(event_recorder_priv
->pub
->parent
->enabled
, enabled
);
1680 * Sync tracepoint registration with event enabled
1684 if (!event_recorder_priv
->parent
.registered
)
1685 register_event(event_recorder_priv
->parent
.pub
);
1687 if (event_recorder_priv
->parent
.registered
)
1688 unregister_event(event_recorder_priv
->parent
.pub
);
1691 /* Check if has enablers without bytecode enabled */
1692 cds_list_for_each_entry(enabler_ref
,
1693 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1694 if (enabler_ref
->ref
->enabled
1695 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1696 has_enablers_without_filter_bytecode
= 1;
1700 event_recorder_priv
->parent
.has_enablers_without_filter_bytecode
=
1701 has_enablers_without_filter_bytecode
;
1703 /* Enable filters */
1704 cds_list_for_each_entry(runtime
,
1705 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1706 lttng_bytecode_sync_state(runtime
);
1709 CMM_STORE_SHARED(event_recorder_priv
->parent
.pub
->eval_filter
,
1710 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1712 lttng_ust_tp_probe_prune_release_queue();
1715 /* Support for event notifier is introduced by probe provider major version 2. */
1717 bool lttng_ust_probe_supports_event_notifier(struct lttng_ust_probe_desc
*probe_desc
)
1719 return probe_desc
->major
>= 2;
1723 void lttng_create_event_notifier_if_missing(
1724 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1726 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1727 struct lttng_ust_probe_desc
*probe_desc
;
1728 struct cds_list_head
*probe_list
;
1731 probe_list
= lttng_get_probe_list_head();
1733 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
1734 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1737 struct lttng_ust_event_desc
*desc
;
1738 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1739 struct cds_hlist_head
*head
;
1740 struct cds_hlist_node
*node
;
1742 desc
= probe_desc
->event_desc
[i
];
1744 if (!lttng_desc_match_enabler(desc
,
1745 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1749 * Given the current event_notifier group, get the bucket that
1750 * the target event_notifier would be if it was already
1753 head
= borrow_hash_table_bucket(
1754 event_notifier_group
->event_notifiers_ht
.table
,
1755 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
1757 cds_hlist_for_each_entry(event_notifier_priv
, node
, head
, hlist
) {
1759 * Check if event_notifier already exists by checking
1760 * if the event_notifier and enabler share the same
1761 * description and id.
1763 if (event_notifier_priv
->parent
.desc
== desc
&&
1764 event_notifier_priv
->parent
.user_token
== event_notifier_enabler
->user_token
) {
1773 /* Check that the probe supports event notifiers, else report the error. */
1774 if (!lttng_ust_probe_supports_event_notifier(probe_desc
)) {
1775 ERR("Probe \"%s\" contains event \"%s\" which matches an enabled event notifier, "
1776 "but its version (%u.%u) is too old and does not implement event notifiers. "
1777 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1778 "this event will not generate any notification.",
1779 probe_desc
->provider
,
1786 * We need to create a event_notifier for this event probe.
1788 ret
= lttng_event_notifier_create(desc
,
1789 event_notifier_enabler
->user_token
,
1790 event_notifier_enabler
->error_counter_index
,
1791 event_notifier_group
);
1793 DBG("Unable to create event_notifier %s, error %d\n",
1794 probe_desc
->event_desc
[i
]->name
, ret
);
1801 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1804 int lttng_event_notifier_enabler_ref_event_notifiers(
1805 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1807 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1808 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1811 * Only try to create event_notifiers for enablers that are enabled, the user
1812 * might still be attaching filter or exclusion to the
1813 * event_notifier_enabler.
1815 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
)
1818 /* First, ensure that probe event_notifiers are created for this enabler. */
1819 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
1821 /* Link the created event_notifier with its associated enabler. */
1822 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1823 struct lttng_enabler_ref
*enabler_ref
;
1825 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier_priv
->pub
))
1828 enabler_ref
= lttng_enabler_ref(&event_notifier_priv
->parent
.enablers_ref_head
,
1829 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1832 * If no backward ref, create it.
1833 * Add backward ref from event_notifier to enabler.
1835 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1839 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
1840 event_notifier_enabler
);
1841 cds_list_add(&enabler_ref
->node
,
1842 &event_notifier_priv
->parent
.enablers_ref_head
);
1846 * Link filter bytecodes if not linked yet.
1848 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1849 &event_notifier_group
->ctx
,
1850 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
,
1851 <tng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->filter_bytecode_head
);
1854 * Link capture bytecodes if not linked yet.
1856 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1857 &event_notifier_group
->ctx
, &event_notifier_priv
->capture_bytecode_runtime_head
,
1858 &event_notifier_enabler
->capture_bytecode_head
);
1860 event_notifier_priv
->num_captures
= event_notifier_enabler
->num_captures
;
1867 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
1869 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1870 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1872 cds_list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
1873 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
1876 * For each event_notifier, if at least one of its enablers is enabled,
1877 * we enable the event_notifier, else we disable it.
1879 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1880 struct lttng_enabler_ref
*enabler_ref
;
1881 struct lttng_ust_bytecode_runtime
*runtime
;
1882 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1883 int nr_filters
= 0, nr_captures
= 0;
1885 /* Enable event_notifiers */
1886 cds_list_for_each_entry(enabler_ref
,
1887 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1888 if (enabler_ref
->ref
->enabled
) {
1894 CMM_STORE_SHARED(event_notifier_priv
->pub
->parent
->enabled
, enabled
);
1896 * Sync tracepoint registration with event_notifier enabled
1900 if (!event_notifier_priv
->parent
.registered
)
1901 register_event(event_notifier_priv
->parent
.pub
);
1903 if (event_notifier_priv
->parent
.registered
)
1904 unregister_event(event_notifier_priv
->parent
.pub
);
1907 /* Check if has enablers without bytecode enabled */
1908 cds_list_for_each_entry(enabler_ref
,
1909 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1910 if (enabler_ref
->ref
->enabled
1911 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1912 has_enablers_without_filter_bytecode
= 1;
1916 event_notifier_priv
->parent
.has_enablers_without_filter_bytecode
=
1917 has_enablers_without_filter_bytecode
;
1919 /* Enable filters */
1920 cds_list_for_each_entry(runtime
,
1921 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1922 lttng_bytecode_sync_state(runtime
);
1925 CMM_STORE_SHARED(event_notifier_priv
->parent
.pub
->eval_filter
,
1926 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1928 /* Enable captures. */
1929 cds_list_for_each_entry(runtime
,
1930 &event_notifier_priv
->capture_bytecode_runtime_head
, node
) {
1931 lttng_bytecode_sync_state(runtime
);
1934 CMM_STORE_SHARED(event_notifier_priv
->pub
->eval_capture
,
1937 lttng_ust_tp_probe_prune_release_queue();
1941 * Apply enablers to session events, adding events to session if need
1942 * be. It is required after each modification applied to an active
1943 * session, and right before session "start".
1944 * "lazy" sync means we only sync if required.
1947 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
)
1949 /* We can skip if session is not active */
1950 if (!session
->active
)
1952 lttng_session_sync_event_enablers(session
);
1956 * Update all sessions with the given app context.
1957 * Called with ust lock held.
1958 * This is invoked when an application context gets loaded/unloaded. It
1959 * ensures the context callbacks are in sync with the application
1960 * context (either app context callbacks, or dummy callbacks).
1962 void lttng_ust_context_set_session_provider(const char *name
,
1963 size_t (*get_size
)(struct lttng_ust_ctx_field
*field
, size_t offset
),
1964 void (*record
)(struct lttng_ust_ctx_field
*field
,
1965 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
1966 struct lttng_ust_channel_buffer
*chan
),
1967 void (*get_value
)(struct lttng_ust_ctx_field
*field
,
1968 struct lttng_ust_ctx_value
*value
))
1970 struct lttng_ust_session_private
*session_priv
;
1972 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1973 struct lttng_ust_channel_buffer_private
*chan
;
1974 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1977 ret
= lttng_ust_context_set_provider_rcu(&session_priv
->ctx
,
1978 name
, get_size
, record
, get_value
);
1981 cds_list_for_each_entry(chan
, &session_priv
->chan_head
, node
) {
1982 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
1983 name
, get_size
, record
, get_value
);
1987 cds_list_for_each_entry(event_recorder_priv
, &session_priv
->events_head
, node
) {
1988 ret
= lttng_ust_context_set_provider_rcu(&event_recorder_priv
->ctx
,
1989 name
, get_size
, record
, get_value
);
1997 * Update all event_notifier groups with the given app context.
1998 * Called with ust lock held.
1999 * This is invoked when an application context gets loaded/unloaded. It
2000 * ensures the context callbacks are in sync with the application
2001 * context (either app context callbacks, or dummy callbacks).
2003 void lttng_ust_context_set_event_notifier_group_provider(const char *name
,
2004 size_t (*get_size
)(struct lttng_ust_ctx_field
*field
, size_t offset
),
2005 void (*record
)(struct lttng_ust_ctx_field
*field
,
2006 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2007 struct lttng_ust_channel_buffer
*chan
),
2008 void (*get_value
)(struct lttng_ust_ctx_field
*field
,
2009 struct lttng_ust_ctx_value
*value
))
2011 struct lttng_event_notifier_group
*event_notifier_group
;
2013 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2016 ret
= lttng_ust_context_set_provider_rcu(
2017 &event_notifier_group
->ctx
,
2018 name
, get_size
, record
, get_value
);
2024 int lttng_ust_session_uuid_validate(struct lttng_ust_session
*session
,
2025 unsigned char *uuid
)
2029 /* Compare UUID with session. */
2030 if (session
->priv
->uuid_set
) {
2031 if (memcmp(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
)) {
2035 memcpy(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
2036 session
->priv
->uuid_set
= true;