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 "common/logging.h"
36 #include "common/macros.h"
37 #include <lttng/ust-ctl.h>
38 #include "common/ustcomm.h"
39 #include "common/ust-fd.h"
40 #include "common/dynamic-type.h"
41 #include "common/ust-context-provider.h"
43 #include "common/tracepoint.h"
44 #include "common/strutils.h"
45 #include "lttng-bytecode.h"
46 #include "common/tracer.h"
47 #include "lttng-tracer-core.h"
48 #include "lttng-ust-statedump.h"
49 #include "context-internal.h"
50 #include "lib/lttng-ust/events.h"
51 #include "common/ringbuffer/shm.h"
52 #include "common/ringbuffer/frontend_types.h"
53 #include "common/ringbuffer/frontend.h"
54 #include "common/counter/counter.h"
55 #include "common/jhash.h"
56 #include <lttng/ust-abi.h>
57 #include "context-provider-internal.h"
60 * All operations within this file are called by the communication
61 * thread, under ust_lock protection.
64 static CDS_LIST_HEAD(sessions
);
65 static CDS_LIST_HEAD(event_notifier_groups
);
67 struct cds_list_head
*lttng_get_sessions(void)
72 static void _lttng_event_destroy(struct lttng_ust_event_common
*event
);
73 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
76 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
);
78 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
);
80 void lttng_event_notifier_group_sync_enablers(
81 struct lttng_event_notifier_group
*event_notifier_group
);
83 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
85 bool lttng_ust_validate_event_name(const struct lttng_ust_event_desc
*desc
)
87 if (strlen(desc
->probe_desc
->provider_name
) + 1 +
88 strlen(desc
->event_name
) >= LTTNG_UST_ABI_SYM_NAME_LEN
)
93 void lttng_ust_format_event_name(const struct lttng_ust_event_desc
*desc
,
96 strcpy(name
, desc
->probe_desc
->provider_name
);
98 strcat(name
, desc
->event_name
);
102 * Called with ust lock held.
104 int lttng_session_active(void)
106 struct lttng_ust_session_private
*iter
;
108 cds_list_for_each_entry(iter
, &sessions
, node
) {
109 if (iter
->pub
->active
)
116 int lttng_loglevel_match(int loglevel
,
117 unsigned int has_loglevel
,
118 enum lttng_ust_abi_loglevel_type req_type
,
122 loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
124 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
125 if (loglevel
<= req_loglevel
126 || (req_loglevel
== -1 && loglevel
<= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG
))
130 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
131 if (loglevel
== req_loglevel
132 || (req_loglevel
== -1 && loglevel
<= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG
))
136 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
138 if (loglevel
<= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG
)
145 struct lttng_ust_session
*lttng_session_create(void)
147 struct lttng_ust_session
*session
;
148 struct lttng_ust_session_private
*session_priv
;
151 session
= zmalloc(sizeof(struct lttng_ust_session
));
154 session
->struct_size
= sizeof(struct lttng_ust_session
);
155 session_priv
= zmalloc(sizeof(struct lttng_ust_session_private
));
160 session
->priv
= session_priv
;
161 session_priv
->pub
= session
;
162 if (lttng_context_init_all(&session
->priv
->ctx
)) {
167 CDS_INIT_LIST_HEAD(&session
->priv
->chan_head
);
168 CDS_INIT_LIST_HEAD(&session
->priv
->events_head
);
169 CDS_INIT_LIST_HEAD(&session
->priv
->enums_head
);
170 CDS_INIT_LIST_HEAD(&session
->priv
->enablers_head
);
171 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
172 CDS_INIT_HLIST_HEAD(&session
->priv
->events_ht
.table
[i
]);
173 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
174 CDS_INIT_HLIST_HEAD(&session
->priv
->enums_ht
.table
[i
]);
175 cds_list_add(&session
->priv
->node
, &sessions
);
179 struct lttng_counter
*lttng_ust_counter_create(
180 const char *counter_transport_name
,
181 size_t number_dimensions
, const struct lttng_counter_dimension
*dimensions
)
183 struct lttng_counter_transport
*counter_transport
= NULL
;
184 struct lttng_counter
*counter
= NULL
;
186 counter_transport
= lttng_counter_transport_find(counter_transport_name
);
187 if (!counter_transport
)
189 counter
= zmalloc(sizeof(struct lttng_counter
));
193 counter
->ops
= &counter_transport
->ops
;
194 counter
->transport
= counter_transport
;
196 counter
->counter
= counter
->ops
->counter_create(
197 number_dimensions
, dimensions
, 0,
199 if (!counter
->counter
) {
213 void lttng_ust_counter_destroy(struct lttng_counter
*counter
)
215 counter
->ops
->counter_destroy(counter
->counter
);
219 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
221 struct lttng_event_notifier_group
*event_notifier_group
;
224 event_notifier_group
= zmalloc(sizeof(struct lttng_event_notifier_group
));
225 if (!event_notifier_group
)
228 /* Add all contexts. */
229 if (lttng_context_init_all(&event_notifier_group
->ctx
)) {
230 free(event_notifier_group
);
234 CDS_INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
235 CDS_INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
236 for (i
= 0; i
< LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
; i
++)
237 CDS_INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
239 cds_list_add(&event_notifier_group
->node
, &event_notifier_groups
);
241 return event_notifier_group
;
245 * Only used internally at session destruction.
248 void _lttng_channel_unmap(struct lttng_ust_channel_buffer
*lttng_chan
)
250 struct lttng_ust_ring_buffer_channel
*chan
;
251 struct lttng_ust_shm_handle
*handle
;
253 cds_list_del(<tng_chan
->priv
->node
);
254 lttng_destroy_context(lttng_chan
->priv
->ctx
);
255 chan
= lttng_chan
->priv
->rb_chan
;
256 handle
= chan
->handle
;
257 channel_destroy(chan
, handle
, 0);
258 free(lttng_chan
->parent
);
259 free(lttng_chan
->priv
);
264 void register_event(struct lttng_ust_event_common
*event
)
267 const struct lttng_ust_event_desc
*desc
;
269 assert(event
->priv
->registered
== 0);
270 desc
= event
->priv
->desc
;
271 ret
= lttng_ust_tp_probe_register_queue_release(desc
->probe_desc
->provider_name
,
273 desc
->probe_callback
,
274 event
, desc
->signature
);
277 event
->priv
->registered
= 1;
281 void unregister_event(struct lttng_ust_event_common
*event
)
284 const struct lttng_ust_event_desc
*desc
;
286 assert(event
->priv
->registered
== 1);
287 desc
= event
->priv
->desc
;
288 ret
= lttng_ust_tp_probe_unregister_queue_release(desc
->probe_desc
->provider_name
,
290 desc
->probe_callback
,
294 event
->priv
->registered
= 0;
298 void _lttng_event_unregister(struct lttng_ust_event_common
*event
)
300 if (event
->priv
->registered
)
301 unregister_event(event
);
304 void lttng_session_destroy(struct lttng_ust_session
*session
)
306 struct lttng_ust_channel_buffer_private
*chan
, *tmpchan
;
307 struct lttng_ust_event_recorder_private
*event_recorder_priv
, *tmpevent_recorder_priv
;
308 struct lttng_enum
*_enum
, *tmp_enum
;
309 struct lttng_event_enabler
*event_enabler
, *event_tmpenabler
;
311 CMM_ACCESS_ONCE(session
->active
) = 0;
312 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
313 _lttng_event_unregister(event_recorder_priv
->parent
.pub
);
315 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
316 lttng_ust_tp_probe_prune_release_queue();
317 cds_list_for_each_entry_safe(event_enabler
, event_tmpenabler
,
318 &session
->priv
->enablers_head
, node
)
319 lttng_event_enabler_destroy(event_enabler
);
320 cds_list_for_each_entry_safe(event_recorder_priv
, tmpevent_recorder_priv
,
321 &session
->priv
->events_head
, node
)
322 _lttng_event_destroy(event_recorder_priv
->parent
.pub
);
323 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
324 &session
->priv
->enums_head
, node
)
325 _lttng_enum_destroy(_enum
);
326 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->priv
->chan_head
, node
)
327 _lttng_channel_unmap(chan
->pub
);
328 cds_list_del(&session
->priv
->node
);
329 lttng_destroy_context(session
->priv
->ctx
);
334 void lttng_event_notifier_group_destroy(
335 struct lttng_event_notifier_group
*event_notifier_group
)
338 struct lttng_event_notifier_enabler
*notifier_enabler
, *tmpnotifier_enabler
;
339 struct lttng_ust_event_notifier_private
*event_notifier_priv
, *tmpevent_notifier_priv
;
341 if (!event_notifier_group
) {
345 cds_list_for_each_entry(event_notifier_priv
,
346 &event_notifier_group
->event_notifiers_head
, node
)
347 _lttng_event_unregister(event_notifier_priv
->parent
.pub
);
349 lttng_ust_urcu_synchronize_rcu();
351 cds_list_for_each_entry_safe(notifier_enabler
, tmpnotifier_enabler
,
352 &event_notifier_group
->enablers_head
, node
)
353 lttng_event_notifier_enabler_destroy(notifier_enabler
);
355 cds_list_for_each_entry_safe(event_notifier_priv
, tmpevent_notifier_priv
,
356 &event_notifier_group
->event_notifiers_head
, node
)
357 _lttng_event_destroy(event_notifier_priv
->parent
.pub
);
359 if (event_notifier_group
->error_counter
)
360 lttng_ust_counter_destroy(event_notifier_group
->error_counter
);
362 /* Close the notification fd to the listener of event_notifiers. */
364 lttng_ust_lock_fd_tracker();
365 close_ret
= close(event_notifier_group
->notification_fd
);
367 lttng_ust_delete_fd_from_tracker(
368 event_notifier_group
->notification_fd
);
373 lttng_ust_unlock_fd_tracker();
375 cds_list_del(&event_notifier_group
->node
);
376 lttng_destroy_context(event_notifier_group
->ctx
);
377 free(event_notifier_group
);
381 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
383 struct lttng_ust_bytecode_node
*filter_node
, *tmp_filter_node
;
384 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
390 /* Destroy filter bytecode */
391 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
392 &enabler
->filter_bytecode_head
, node
) {
396 /* Destroy excluders */
397 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
398 &enabler
->excluder_head
, node
) {
403 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
405 if (!event_notifier_enabler
) {
409 cds_list_del(&event_notifier_enabler
->node
);
411 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
413 free(event_notifier_enabler
);
417 int lttng_enum_create(const struct lttng_ust_enum_desc
*desc
,
418 struct lttng_ust_session
*session
)
420 const char *enum_name
= desc
->name
;
421 struct lttng_enum
*_enum
;
422 struct cds_hlist_head
*head
;
424 size_t name_len
= strlen(enum_name
);
428 /* Check if this enum is already registered for this session. */
429 hash
= jhash(enum_name
, name_len
, 0);
430 head
= &session
->priv
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
432 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
438 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
439 if (notify_socket
< 0) {
444 _enum
= zmalloc(sizeof(*_enum
));
449 _enum
->session
= session
;
452 ret
= ustcomm_register_enum(notify_socket
,
459 DBG("Error (%d) registering enumeration to sessiond", ret
);
460 goto sessiond_register_error
;
462 cds_list_add(&_enum
->node
, &session
->priv
->enums_head
);
463 cds_hlist_add_head(&_enum
->hlist
, head
);
466 sessiond_register_error
:
475 int lttng_create_enum_check(const struct lttng_ust_type_common
*type
,
476 struct lttng_ust_session
*session
)
478 switch (type
->type
) {
479 case lttng_ust_type_enum
:
481 const struct lttng_ust_enum_desc
*enum_desc
;
484 enum_desc
= lttng_ust_get_type_enum(type
)->desc
;
485 ret
= lttng_enum_create(enum_desc
, session
);
486 if (ret
&& ret
!= -EEXIST
) {
487 DBG("Unable to create enum error: (%d)", ret
);
492 case lttng_ust_type_dynamic
:
494 const struct lttng_ust_event_field
*tag_field_generic
;
495 const struct lttng_ust_enum_desc
*enum_desc
;
498 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
499 enum_desc
= lttng_ust_get_type_enum(tag_field_generic
->type
)->desc
;
500 ret
= lttng_enum_create(enum_desc
, session
);
501 if (ret
&& ret
!= -EEXIST
) {
502 DBG("Unable to create enum error: (%d)", ret
);
508 /* TODO: nested types when they become supported. */
515 int lttng_create_all_event_enums(size_t nr_fields
,
516 const struct lttng_ust_event_field
* const *event_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 const struct lttng_ust_type_common
*type
= event_fields
[i
]->type
;
526 ret
= lttng_create_enum_check(type
, session
);
534 int lttng_create_all_ctx_enums(size_t nr_fields
,
535 struct lttng_ust_ctx_field
*ctx_fields
,
536 struct lttng_ust_session
*session
)
541 /* For each field, ensure enum is part of the session. */
542 for (i
= 0; i
< nr_fields
; i
++) {
543 const struct lttng_ust_type_common
*type
= ctx_fields
[i
].event_field
->type
;
545 ret
= lttng_create_enum_check(type
, session
);
553 * Ensure that a state-dump will be performed for this session at the end
554 * of the current handle_message().
556 int lttng_session_statedump(struct lttng_ust_session
*session
)
558 session
->priv
->statedump_pending
= 1;
559 lttng_ust_sockinfo_session_enabled(session
->priv
->owner
);
563 int lttng_session_enable(struct lttng_ust_session
*session
)
566 struct lttng_ust_channel_buffer_private
*chan
;
569 if (session
->active
) {
574 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
575 if (notify_socket
< 0)
576 return notify_socket
;
578 /* Set transient enabler state to "enabled" */
579 session
->priv
->tstate
= 1;
581 /* We need to sync enablers with session before activation. */
582 lttng_session_sync_event_enablers(session
);
585 * Snapshot the number of events per channel to know the type of header
588 cds_list_for_each_entry(chan
, &session
->priv
->chan_head
, node
) {
589 struct lttng_ust_ctx
*ctx
;
590 struct lttng_ust_ctx_field
*fields
= NULL
;
591 size_t nr_fields
= 0;
594 /* don't change it if session stop/restart */
595 if (chan
->header_type
)
599 nr_fields
= ctx
->nr_fields
;
600 fields
= ctx
->fields
;
601 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
604 DBG("Error (%d) adding enum to session", ret
);
608 ret
= ustcomm_register_channel(notify_socket
,
617 DBG("Error (%d) registering channel to sessiond", ret
);
620 if (chan_id
!= chan
->id
) {
621 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
627 /* Set atomically the state to "active" */
628 CMM_ACCESS_ONCE(session
->active
) = 1;
629 CMM_ACCESS_ONCE(session
->priv
->been_active
) = 1;
631 ret
= lttng_session_statedump(session
);
638 int lttng_session_disable(struct lttng_ust_session
*session
)
642 if (!session
->active
) {
646 /* Set atomically the state to "inactive" */
647 CMM_ACCESS_ONCE(session
->active
) = 0;
649 /* Set transient enabler state to "disabled" */
650 session
->priv
->tstate
= 0;
651 lttng_session_sync_event_enablers(session
);
656 int lttng_channel_enable(struct lttng_ust_channel_common
*lttng_channel
)
660 if (lttng_channel
->enabled
) {
664 /* Set transient enabler state to "enabled" */
665 lttng_channel
->priv
->tstate
= 1;
666 lttng_session_sync_event_enablers(lttng_channel
->session
);
667 /* Set atomically the state to "enabled" */
668 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 1;
673 int lttng_channel_disable(struct lttng_ust_channel_common
*lttng_channel
)
677 if (!lttng_channel
->enabled
) {
681 /* Set atomically the state to "disabled" */
682 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 0;
683 /* Set transient enabler state to "enabled" */
684 lttng_channel
->priv
->tstate
= 0;
685 lttng_session_sync_event_enablers(lttng_channel
->session
);
691 struct cds_hlist_head
*borrow_hash_table_bucket(
692 struct cds_hlist_head
*hash_table
,
693 unsigned int hash_table_size
,
694 const struct lttng_ust_event_desc
*desc
)
696 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
700 lttng_ust_format_event_name(desc
, name
);
701 name_len
= strlen(name
);
703 hash
= jhash(name
, name_len
, 0);
704 return &hash_table
[hash
& (hash_table_size
- 1)];
708 * Supports event creation while tracing session is active.
711 int lttng_event_recorder_create(const struct lttng_ust_event_desc
*desc
,
712 struct lttng_ust_channel_buffer
*chan
)
714 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
715 struct lttng_ust_event_recorder
*event_recorder
;
716 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
717 struct lttng_ust_session
*session
= chan
->parent
->session
;
718 struct cds_hlist_head
*head
;
720 int notify_socket
, loglevel
;
723 head
= borrow_hash_table_bucket(chan
->parent
->session
->priv
->events_ht
.table
,
724 LTTNG_UST_EVENT_HT_SIZE
, desc
);
726 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
727 if (notify_socket
< 0) {
732 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
735 DBG("Error (%d) adding enum to session", ret
);
736 goto create_enum_error
;
740 * Check if loglevel match. Refuse to connect event if not.
742 event_recorder
= zmalloc(sizeof(struct lttng_ust_event_recorder
));
743 if (!event_recorder
) {
747 event_recorder
->struct_size
= sizeof(struct lttng_ust_event_recorder
);
749 event_recorder
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
750 if (!event_recorder
->parent
) {
754 event_recorder
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
755 event_recorder
->parent
->type
= LTTNG_UST_EVENT_TYPE_RECORDER
;
756 event_recorder
->parent
->child
= event_recorder
;
758 event_recorder_priv
= zmalloc(sizeof(struct lttng_ust_event_recorder_private
));
759 if (!event_recorder_priv
) {
763 event_recorder
->priv
= event_recorder_priv
;
764 event_recorder_priv
->pub
= event_recorder
;
765 event_recorder
->parent
->priv
= &event_recorder_priv
->parent
;
766 event_recorder_priv
->parent
.pub
= event_recorder
->parent
;
768 event_recorder
->chan
= chan
;
770 /* Event will be enabled by enabler sync. */
771 event_recorder
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
772 event_recorder
->parent
->enabled
= 0;
773 event_recorder
->parent
->priv
->registered
= 0;
774 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->filter_bytecode_runtime_head
);
775 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->enablers_ref_head
);
776 event_recorder
->parent
->priv
->desc
= desc
;
779 loglevel
= *(*desc
->loglevel
);
781 loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
782 if (desc
->model_emf_uri
)
783 uri
= *(desc
->model_emf_uri
);
787 lttng_ust_format_event_name(desc
, name
);
789 /* Fetch event ID from sessiond */
790 ret
= ustcomm_register_event(notify_socket
,
793 chan
->priv
->parent
.objd
,
800 &event_recorder
->priv
->id
);
802 DBG("Error (%d) registering event to sessiond", ret
);
803 goto sessiond_register_error
;
806 cds_list_add(&event_recorder_priv
->node
, &chan
->parent
->session
->priv
->events_head
);
807 cds_hlist_add_head(&event_recorder_priv
->hlist
, head
);
810 sessiond_register_error
:
811 free(event_recorder_priv
);
813 free(event_recorder
->parent
);
815 free(event_recorder
);
823 int lttng_event_notifier_create(const struct lttng_ust_event_desc
*desc
,
824 uint64_t token
, uint64_t error_counter_index
,
825 struct lttng_event_notifier_group
*event_notifier_group
)
827 struct lttng_ust_event_notifier
*event_notifier
;
828 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
829 struct cds_hlist_head
*head
;
833 * Get the hashtable bucket the created lttng_event_notifier object
834 * should be inserted.
836 head
= borrow_hash_table_bucket(
837 event_notifier_group
->event_notifiers_ht
.table
,
838 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
840 event_notifier
= zmalloc(sizeof(struct lttng_ust_event_notifier
));
841 if (!event_notifier
) {
845 event_notifier
->struct_size
= sizeof(struct lttng_ust_event_notifier
);
847 event_notifier
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
848 if (!event_notifier
->parent
) {
852 event_notifier
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
853 event_notifier
->parent
->type
= LTTNG_UST_EVENT_TYPE_NOTIFIER
;
854 event_notifier
->parent
->child
= event_notifier
;
856 event_notifier_priv
= zmalloc(sizeof(struct lttng_ust_event_notifier_private
));
857 if (!event_notifier_priv
) {
861 event_notifier
->priv
= event_notifier_priv
;
862 event_notifier_priv
->pub
= event_notifier
;
863 event_notifier
->parent
->priv
= &event_notifier_priv
->parent
;
864 event_notifier_priv
->parent
.pub
= event_notifier
->parent
;
866 event_notifier_priv
->group
= event_notifier_group
;
867 event_notifier_priv
->parent
.user_token
= token
;
868 event_notifier_priv
->error_counter_index
= error_counter_index
;
870 /* Event notifier will be enabled by enabler sync. */
871 event_notifier
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
872 event_notifier
->parent
->enabled
= 0;
873 event_notifier_priv
->parent
.registered
= 0;
875 CDS_INIT_LIST_HEAD(&event_notifier
->parent
->priv
->filter_bytecode_runtime_head
);
876 CDS_INIT_LIST_HEAD(&event_notifier
->priv
->capture_bytecode_runtime_head
);
877 CDS_INIT_LIST_HEAD(&event_notifier_priv
->parent
.enablers_ref_head
);
878 event_notifier_priv
->parent
.desc
= desc
;
879 event_notifier
->notification_send
= lttng_event_notifier_notification_send
;
881 cds_list_add(&event_notifier_priv
->node
,
882 &event_notifier_group
->event_notifiers_head
);
883 cds_hlist_add_head(&event_notifier_priv
->hlist
, head
);
888 free(event_notifier
->parent
);
890 free(event_notifier
);
896 int lttng_desc_match_star_glob_enabler(const struct lttng_ust_event_desc
*desc
,
897 struct lttng_enabler
*enabler
)
899 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
901 unsigned int has_loglevel
= 0;
903 lttng_ust_format_event_name(desc
, name
);
904 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_STAR_GLOB
);
905 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
908 if (desc
->loglevel
) {
909 loglevel
= *(*desc
->loglevel
);
912 if (!lttng_loglevel_match(loglevel
,
914 enabler
->event_param
.loglevel_type
,
915 enabler
->event_param
.loglevel
))
921 int lttng_desc_match_event_enabler(const struct lttng_ust_event_desc
*desc
,
922 struct lttng_enabler
*enabler
)
924 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
926 unsigned int has_loglevel
= 0;
928 lttng_ust_format_event_name(desc
, name
);
929 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_EVENT
);
930 if (strcmp(name
, enabler
->event_param
.name
))
932 if (desc
->loglevel
) {
933 loglevel
= *(*desc
->loglevel
);
936 if (!lttng_loglevel_match(loglevel
,
938 enabler
->event_param
.loglevel_type
,
939 enabler
->event_param
.loglevel
))
945 int lttng_desc_match_enabler(const struct lttng_ust_event_desc
*desc
,
946 struct lttng_enabler
*enabler
)
948 switch (enabler
->format_type
) {
949 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
951 struct lttng_ust_excluder_node
*excluder
;
953 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
958 * If the matching event matches with an excluder,
959 * return 'does not match'
961 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
964 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
968 excluder_name
= (char *) (excluder
->excluder
.names
)
969 + count
* LTTNG_UST_ABI_SYM_NAME_LEN
;
970 len
= strnlen(excluder_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
972 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
974 lttng_ust_format_event_name(desc
, name
);
975 if (strutils_star_glob_match(excluder_name
, len
, name
, SIZE_MAX
)) {
983 case LTTNG_ENABLER_FORMAT_EVENT
:
984 return lttng_desc_match_event_enabler(desc
, enabler
);
991 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
992 struct lttng_ust_event_recorder
*event_recorder
)
994 if (lttng_desc_match_enabler(event_recorder
->parent
->priv
->desc
,
995 lttng_event_enabler_as_enabler(event_enabler
))
996 && event_recorder
->chan
== event_enabler
->chan
)
1003 int lttng_event_notifier_enabler_match_event_notifier(
1004 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1005 struct lttng_ust_event_notifier
*event_notifier
)
1007 int desc_matches
= lttng_desc_match_enabler(event_notifier
->priv
->parent
.desc
,
1008 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1010 if (desc_matches
&& event_notifier
->priv
->group
== event_notifier_enabler
->group
&&
1011 event_notifier
->priv
->parent
.user_token
== event_notifier_enabler
->user_token
)
1018 struct lttng_enabler_ref
*lttng_enabler_ref(
1019 struct cds_list_head
*enabler_ref_list
,
1020 struct lttng_enabler
*enabler
)
1022 struct lttng_enabler_ref
*enabler_ref
;
1024 cds_list_for_each_entry(enabler_ref
, enabler_ref_list
, node
) {
1025 if (enabler_ref
->ref
== enabler
)
1032 * Create struct lttng_event if it is missing and present in the list of
1033 * tracepoint probes.
1036 void lttng_create_event_recorder_if_missing(struct lttng_event_enabler
*event_enabler
)
1038 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1039 struct lttng_ust_registered_probe
*reg_probe
;
1040 const struct lttng_ust_event_desc
*desc
;
1041 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1043 struct cds_list_head
*probe_list
;
1045 probe_list
= lttng_get_probe_list_head();
1047 * For each probe event, if we find that a probe event matches
1048 * our enabler, create an associated lttng_event if not
1051 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1052 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1054 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1057 struct cds_hlist_head
*head
;
1058 struct cds_hlist_node
*node
;
1060 desc
= probe_desc
->event_desc
[i
];
1061 if (!lttng_desc_match_enabler(desc
,
1062 lttng_event_enabler_as_enabler(event_enabler
)))
1065 head
= borrow_hash_table_bucket(
1066 session
->priv
->events_ht
.table
,
1067 LTTNG_UST_EVENT_HT_SIZE
, desc
);
1069 cds_hlist_for_each_entry(event_recorder_priv
, node
, head
, hlist
) {
1070 if (event_recorder_priv
->parent
.desc
== desc
1071 && event_recorder_priv
->pub
->chan
== event_enabler
->chan
) {
1080 * We need to create an event for this
1083 ret
= lttng_event_recorder_create(probe_desc
->event_desc
[i
],
1084 event_enabler
->chan
);
1086 DBG("Unable to create event \"%s:%s\", error %d\n",
1087 probe_desc
->provider_name
,
1088 probe_desc
->event_desc
[i
]->event_name
, ret
);
1095 void probe_provider_event_for_each(const struct lttng_ust_probe_desc
*provider_desc
,
1096 void (*event_func
)(struct lttng_ust_event_common
*event
))
1098 struct cds_hlist_node
*node
, *tmp_node
;
1099 struct cds_list_head
*sessionsp
;
1102 /* Get handle on list of sessions. */
1103 sessionsp
= lttng_get_sessions();
1106 * Iterate over all events in the probe provider descriptions and
1107 * sessions to queue the unregistration of the events.
1109 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
1110 const struct lttng_ust_event_desc
*event_desc
;
1111 struct lttng_event_notifier_group
*event_notifier_group
;
1112 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1113 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1114 struct lttng_ust_session_private
*session_priv
;
1115 struct cds_hlist_head
*head
;
1117 event_desc
= provider_desc
->event_desc
[i
];
1120 * Iterate over all session to find the current event
1123 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
1125 * Get the list of events in the hashtable bucket and
1126 * iterate to find the event matching this descriptor.
1128 head
= borrow_hash_table_bucket(
1129 session_priv
->events_ht
.table
,
1130 LTTNG_UST_EVENT_HT_SIZE
, event_desc
);
1132 cds_hlist_for_each_entry_safe(event_recorder_priv
, node
, tmp_node
, head
, hlist
) {
1133 if (event_desc
== event_recorder_priv
->parent
.desc
) {
1134 event_func(event_recorder_priv
->parent
.pub
);
1141 * Iterate over all event_notifier groups to find the current event
1144 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1146 * Get the list of event_notifiers in the hashtable bucket and
1147 * iterate to find the event_notifier matching this
1150 head
= borrow_hash_table_bucket(
1151 event_notifier_group
->event_notifiers_ht
.table
,
1152 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, event_desc
);
1154 cds_hlist_for_each_entry_safe(event_notifier_priv
, node
, tmp_node
, head
, hlist
) {
1155 if (event_desc
== event_notifier_priv
->parent
.desc
) {
1156 event_func(event_notifier_priv
->parent
.pub
);
1165 void _event_enum_destroy(struct lttng_ust_event_common
*event
)
1168 switch (event
->type
) {
1169 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1171 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1172 struct lttng_ust_session
*session
= event_recorder
->chan
->parent
->session
;
1175 /* Destroy enums of the current event. */
1176 for (i
= 0; i
< event_recorder
->parent
->priv
->desc
->nr_fields
; i
++) {
1177 const struct lttng_ust_enum_desc
*enum_desc
;
1178 const struct lttng_ust_event_field
*field
;
1179 struct lttng_enum
*curr_enum
;
1181 field
= event_recorder
->parent
->priv
->desc
->fields
[i
];
1182 switch (field
->type
->type
) {
1183 case lttng_ust_type_enum
:
1184 enum_desc
= lttng_ust_get_type_enum(field
->type
)->desc
;
1190 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
1192 _lttng_enum_destroy(curr_enum
);
1197 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1202 /* Destroy event. */
1203 _lttng_event_destroy(event
);
1207 * Iterate over all the UST sessions to unregister and destroy all probes from
1208 * the probe provider descriptor received as argument. Must me called with the
1211 void lttng_probe_provider_unregister_events(
1212 const struct lttng_ust_probe_desc
*provider_desc
)
1215 * Iterate over all events in the probe provider descriptions and sessions
1216 * to queue the unregistration of the events.
1218 probe_provider_event_for_each(provider_desc
, _lttng_event_unregister
);
1220 /* Wait for grace period. */
1221 lttng_ust_urcu_synchronize_rcu();
1222 /* Prune the unregistration queue. */
1223 lttng_ust_tp_probe_prune_release_queue();
1226 * It is now safe to destroy the events and remove them from the event list
1229 probe_provider_event_for_each(provider_desc
, _event_enum_destroy
);
1233 * Create events associated with an event enabler (if not already present),
1234 * and add backward reference from the event to the enabler.
1237 int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler
*event_enabler
)
1239 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1240 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1242 if (!lttng_event_enabler_as_enabler(event_enabler
)->enabled
)
1245 /* First ensure that probe events are created for this enabler. */
1246 lttng_create_event_recorder_if_missing(event_enabler
);
1248 /* For each event matching enabler in session event list. */
1249 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1250 struct lttng_enabler_ref
*enabler_ref
;
1252 if (!lttng_event_enabler_match_event(event_enabler
, event_recorder_priv
->pub
))
1255 enabler_ref
= lttng_enabler_ref(&event_recorder_priv
->parent
.enablers_ref_head
,
1256 lttng_event_enabler_as_enabler(event_enabler
));
1259 * If no backward ref, create it.
1260 * Add backward ref from event to enabler.
1262 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1265 enabler_ref
->ref
= lttng_event_enabler_as_enabler(
1267 cds_list_add(&enabler_ref
->node
,
1268 &event_recorder_priv
->parent
.enablers_ref_head
);
1272 * Link filter bytecodes if not linked yet.
1274 lttng_enabler_link_bytecode(event_recorder_priv
->parent
.desc
,
1275 &session
->priv
->ctx
,
1276 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
,
1277 <tng_event_enabler_as_enabler(event_enabler
)->filter_bytecode_head
);
1279 /* TODO: merge event context. */
1286 * Called at library load: connect the probe on all enablers matching
1288 * Called with session mutex held.
1290 int lttng_fix_pending_events(void)
1292 struct lttng_ust_session_private
*session_priv
;
1294 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1295 lttng_session_lazy_sync_event_enablers(session_priv
->pub
);
1300 int lttng_fix_pending_event_notifiers(void)
1302 struct lttng_event_notifier_group
*event_notifier_group
;
1304 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1305 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1311 * For each session of the owner thread, execute pending statedump.
1312 * Only dump state for the sessions owned by the caller thread, because
1313 * we don't keep ust_lock across the entire iteration.
1315 void lttng_handle_pending_statedump(void *owner
)
1317 struct lttng_ust_session_private
*session_priv
;
1319 /* Execute state dump */
1320 do_lttng_ust_statedump(owner
);
1322 /* Clear pending state dump */
1326 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1327 if (session_priv
->owner
!= owner
)
1329 if (!session_priv
->statedump_pending
)
1331 session_priv
->statedump_pending
= 0;
1339 void _lttng_event_destroy(struct lttng_ust_event_common
*event
)
1341 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1343 lttng_free_event_filter_runtime(event
);
1344 /* Free event enabler refs */
1345 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1346 &event
->priv
->enablers_ref_head
, node
)
1349 switch (event
->type
) {
1350 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1352 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1354 /* Remove from event list. */
1355 cds_list_del(&event_recorder
->priv
->node
);
1356 /* Remove from event hash table. */
1357 cds_hlist_del(&event_recorder
->priv
->hlist
);
1359 lttng_destroy_context(event_recorder
->priv
->ctx
);
1360 free(event_recorder
->parent
);
1361 free(event_recorder
->priv
);
1362 free(event_recorder
);
1365 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1367 struct lttng_ust_event_notifier
*event_notifier
= event
->child
;
1369 /* Remove from event list. */
1370 cds_list_del(&event_notifier
->priv
->node
);
1371 /* Remove from event hash table. */
1372 cds_hlist_del(&event_notifier
->priv
->hlist
);
1374 free(event_notifier
->priv
);
1375 free(event_notifier
->parent
);
1376 free(event_notifier
);
1385 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1387 cds_list_del(&_enum
->node
);
1388 cds_hlist_del(&_enum
->hlist
);
1392 void lttng_ust_abi_events_exit(void)
1394 struct lttng_ust_session_private
*session_priv
, *tmpsession_priv
;
1396 cds_list_for_each_entry_safe(session_priv
, tmpsession_priv
, &sessions
, node
)
1397 lttng_session_destroy(session_priv
->pub
);
1401 * Enabler management.
1403 struct lttng_event_enabler
*lttng_event_enabler_create(
1404 enum lttng_enabler_format_type format_type
,
1405 struct lttng_ust_abi_event
*event_param
,
1406 struct lttng_ust_channel_buffer
*chan
)
1408 struct lttng_event_enabler
*event_enabler
;
1410 event_enabler
= zmalloc(sizeof(*event_enabler
));
1413 event_enabler
->base
.format_type
= format_type
;
1414 CDS_INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1415 CDS_INIT_LIST_HEAD(&event_enabler
->base
.excluder_head
);
1416 memcpy(&event_enabler
->base
.event_param
, event_param
,
1417 sizeof(event_enabler
->base
.event_param
));
1418 event_enabler
->chan
= chan
;
1420 event_enabler
->base
.enabled
= 0;
1421 cds_list_add(&event_enabler
->node
, &event_enabler
->chan
->parent
->session
->priv
->enablers_head
);
1422 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1424 return event_enabler
;
1427 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
1428 struct lttng_event_notifier_group
*event_notifier_group
,
1429 enum lttng_enabler_format_type format_type
,
1430 struct lttng_ust_abi_event_notifier
*event_notifier_param
)
1432 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1434 event_notifier_enabler
= zmalloc(sizeof(*event_notifier_enabler
));
1435 if (!event_notifier_enabler
)
1437 event_notifier_enabler
->base
.format_type
= format_type
;
1438 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
1439 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->capture_bytecode_head
);
1440 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.excluder_head
);
1442 event_notifier_enabler
->user_token
= event_notifier_param
->event
.token
;
1443 event_notifier_enabler
->error_counter_index
= event_notifier_param
->error_counter_index
;
1444 event_notifier_enabler
->num_captures
= 0;
1446 memcpy(&event_notifier_enabler
->base
.event_param
.name
,
1447 event_notifier_param
->event
.name
,
1448 sizeof(event_notifier_enabler
->base
.event_param
.name
));
1449 event_notifier_enabler
->base
.event_param
.instrumentation
=
1450 event_notifier_param
->event
.instrumentation
;
1451 event_notifier_enabler
->base
.event_param
.loglevel
=
1452 event_notifier_param
->event
.loglevel
;
1453 event_notifier_enabler
->base
.event_param
.loglevel_type
=
1454 event_notifier_param
->event
.loglevel_type
;
1456 event_notifier_enabler
->base
.enabled
= 0;
1457 event_notifier_enabler
->group
= event_notifier_group
;
1459 cds_list_add(&event_notifier_enabler
->node
,
1460 &event_notifier_group
->enablers_head
);
1462 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1464 return event_notifier_enabler
;
1467 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1469 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1470 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1475 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1477 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1478 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1484 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
1485 struct lttng_ust_bytecode_node
**bytecode
)
1487 (*bytecode
)->enabler
= enabler
;
1488 cds_list_add_tail(&(*bytecode
)->node
, &enabler
->filter_bytecode_head
);
1489 /* Take ownership of bytecode */
1493 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
1494 struct lttng_ust_bytecode_node
**bytecode
)
1496 _lttng_enabler_attach_filter_bytecode(
1497 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1499 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1504 void _lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1505 struct lttng_ust_excluder_node
**excluder
)
1507 (*excluder
)->enabler
= enabler
;
1508 cds_list_add_tail(&(*excluder
)->node
, &enabler
->excluder_head
);
1509 /* Take ownership of excluder */
1513 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler
*event_enabler
,
1514 struct lttng_ust_excluder_node
**excluder
)
1516 _lttng_enabler_attach_exclusion(
1517 lttng_event_enabler_as_enabler(event_enabler
), excluder
);
1519 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1523 int lttng_event_notifier_enabler_enable(
1524 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1526 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
1527 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1532 int lttng_event_notifier_enabler_disable(
1533 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1535 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
1536 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1541 int lttng_event_notifier_enabler_attach_filter_bytecode(
1542 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1543 struct lttng_ust_bytecode_node
**bytecode
)
1545 _lttng_enabler_attach_filter_bytecode(
1546 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1549 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1553 int lttng_event_notifier_enabler_attach_capture_bytecode(
1554 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1555 struct lttng_ust_bytecode_node
**bytecode
)
1557 (*bytecode
)->enabler
= lttng_event_notifier_enabler_as_enabler(
1558 event_notifier_enabler
);
1559 cds_list_add_tail(&(*bytecode
)->node
,
1560 &event_notifier_enabler
->capture_bytecode_head
);
1561 /* Take ownership of bytecode */
1563 event_notifier_enabler
->num_captures
++;
1565 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1569 int lttng_event_notifier_enabler_attach_exclusion(
1570 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1571 struct lttng_ust_excluder_node
**excluder
)
1573 _lttng_enabler_attach_exclusion(
1574 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1577 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1581 int lttng_attach_context(struct lttng_ust_abi_context
*context_param
,
1582 union lttng_ust_abi_args
*uargs
,
1583 struct lttng_ust_ctx
**ctx
, struct lttng_ust_session
*session
)
1586 * We cannot attach a context after trace has been started for a
1587 * session because the metadata does not allow expressing this
1588 * information outside of the original channel scope.
1590 if (session
->priv
->been_active
)
1593 switch (context_param
->ctx
) {
1594 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
1595 return lttng_add_pthread_id_to_ctx(ctx
);
1596 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1598 struct lttng_ust_abi_perf_counter_ctx
*perf_ctx_param
;
1600 perf_ctx_param
= &context_param
->u
.perf_counter
;
1601 return lttng_add_perf_counter_to_ctx(
1602 perf_ctx_param
->type
,
1603 perf_ctx_param
->config
,
1604 perf_ctx_param
->name
,
1607 case LTTNG_UST_ABI_CONTEXT_VTID
:
1608 return lttng_add_vtid_to_ctx(ctx
);
1609 case LTTNG_UST_ABI_CONTEXT_VPID
:
1610 return lttng_add_vpid_to_ctx(ctx
);
1611 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
1612 return lttng_add_procname_to_ctx(ctx
);
1613 case LTTNG_UST_ABI_CONTEXT_IP
:
1614 return lttng_add_ip_to_ctx(ctx
);
1615 case LTTNG_UST_ABI_CONTEXT_CPU_ID
:
1616 return lttng_add_cpu_id_to_ctx(ctx
);
1617 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1618 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1620 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
1621 return lttng_add_cgroup_ns_to_ctx(ctx
);
1622 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
1623 return lttng_add_ipc_ns_to_ctx(ctx
);
1624 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
1625 return lttng_add_mnt_ns_to_ctx(ctx
);
1626 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
1627 return lttng_add_net_ns_to_ctx(ctx
);
1628 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
1629 return lttng_add_pid_ns_to_ctx(ctx
);
1630 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
1631 return lttng_add_time_ns_to_ctx(ctx
);
1632 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
1633 return lttng_add_user_ns_to_ctx(ctx
);
1634 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
1635 return lttng_add_uts_ns_to_ctx(ctx
);
1636 case LTTNG_UST_ABI_CONTEXT_VUID
:
1637 return lttng_add_vuid_to_ctx(ctx
);
1638 case LTTNG_UST_ABI_CONTEXT_VEUID
:
1639 return lttng_add_veuid_to_ctx(ctx
);
1640 case LTTNG_UST_ABI_CONTEXT_VSUID
:
1641 return lttng_add_vsuid_to_ctx(ctx
);
1642 case LTTNG_UST_ABI_CONTEXT_VGID
:
1643 return lttng_add_vgid_to_ctx(ctx
);
1644 case LTTNG_UST_ABI_CONTEXT_VEGID
:
1645 return lttng_add_vegid_to_ctx(ctx
);
1646 case LTTNG_UST_ABI_CONTEXT_VSGID
:
1647 return lttng_add_vsgid_to_ctx(ctx
);
1653 int lttng_event_enabler_attach_context(
1654 struct lttng_event_enabler
*enabler
__attribute__((unused
)),
1655 struct lttng_ust_abi_context
*context_param
__attribute__((unused
)))
1660 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
1662 if (!event_enabler
) {
1665 cds_list_del(&event_enabler
->node
);
1667 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
1669 lttng_destroy_context(event_enabler
->ctx
);
1670 free(event_enabler
);
1674 * lttng_session_sync_event_enablers should be called just before starting a
1678 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
)
1680 struct lttng_event_enabler
*event_enabler
;
1681 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1683 cds_list_for_each_entry(event_enabler
, &session
->priv
->enablers_head
, node
)
1684 lttng_event_enabler_ref_event_recorders(event_enabler
);
1686 * For each event, if at least one of its enablers is enabled,
1687 * and its channel and session transient states are enabled, we
1688 * enable the event, else we disable it.
1690 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1691 struct lttng_enabler_ref
*enabler_ref
;
1692 struct lttng_ust_bytecode_runtime
*runtime
;
1693 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1697 cds_list_for_each_entry(enabler_ref
,
1698 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1699 if (enabler_ref
->ref
->enabled
) {
1705 * Enabled state is based on union of enablers, with
1706 * intesection of session and channel transient enable
1709 enabled
= enabled
&& session
->priv
->tstate
&& event_recorder_priv
->pub
->chan
->priv
->parent
.tstate
;
1711 CMM_STORE_SHARED(event_recorder_priv
->pub
->parent
->enabled
, enabled
);
1713 * Sync tracepoint registration with event enabled
1717 if (!event_recorder_priv
->parent
.registered
)
1718 register_event(event_recorder_priv
->parent
.pub
);
1720 if (event_recorder_priv
->parent
.registered
)
1721 unregister_event(event_recorder_priv
->parent
.pub
);
1724 /* Check if has enablers without bytecode enabled */
1725 cds_list_for_each_entry(enabler_ref
,
1726 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1727 if (enabler_ref
->ref
->enabled
1728 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1729 has_enablers_without_filter_bytecode
= 1;
1733 event_recorder_priv
->parent
.has_enablers_without_filter_bytecode
=
1734 has_enablers_without_filter_bytecode
;
1736 /* Enable filters */
1737 cds_list_for_each_entry(runtime
,
1738 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1739 lttng_bytecode_sync_state(runtime
);
1742 CMM_STORE_SHARED(event_recorder_priv
->parent
.pub
->eval_filter
,
1743 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1745 lttng_ust_tp_probe_prune_release_queue();
1748 /* Support for event notifier is introduced by probe provider major version 2. */
1750 bool lttng_ust_probe_supports_event_notifier(const struct lttng_ust_probe_desc
*probe_desc
)
1752 return probe_desc
->major
>= 2;
1756 void lttng_create_event_notifier_if_missing(
1757 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1759 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1760 struct lttng_ust_registered_probe
*reg_probe
;
1761 struct cds_list_head
*probe_list
;
1764 probe_list
= lttng_get_probe_list_head();
1766 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1767 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1769 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1772 const struct lttng_ust_event_desc
*desc
;
1773 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1774 struct cds_hlist_head
*head
;
1775 struct cds_hlist_node
*node
;
1777 desc
= probe_desc
->event_desc
[i
];
1779 if (!lttng_desc_match_enabler(desc
,
1780 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1784 * Given the current event_notifier group, get the bucket that
1785 * the target event_notifier would be if it was already
1788 head
= borrow_hash_table_bucket(
1789 event_notifier_group
->event_notifiers_ht
.table
,
1790 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
1792 cds_hlist_for_each_entry(event_notifier_priv
, node
, head
, hlist
) {
1794 * Check if event_notifier already exists by checking
1795 * if the event_notifier and enabler share the same
1796 * description and id.
1798 if (event_notifier_priv
->parent
.desc
== desc
&&
1799 event_notifier_priv
->parent
.user_token
== event_notifier_enabler
->user_token
) {
1808 /* Check that the probe supports event notifiers, else report the error. */
1809 if (!lttng_ust_probe_supports_event_notifier(probe_desc
)) {
1810 ERR("Probe \"%s\" contains event \"%s:%s\" which matches an enabled event notifier, "
1811 "but its version (%u.%u) is too old and does not implement event notifiers. "
1812 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1813 "this event will not generate any notification.",
1814 probe_desc
->provider_name
,
1815 probe_desc
->provider_name
, desc
->event_name
,
1821 * We need to create a event_notifier for this event probe.
1823 ret
= lttng_event_notifier_create(desc
,
1824 event_notifier_enabler
->user_token
,
1825 event_notifier_enabler
->error_counter_index
,
1826 event_notifier_group
);
1828 DBG("Unable to create event_notifier \"%s:%s\", error %d\n",
1829 probe_desc
->provider_name
,
1830 probe_desc
->event_desc
[i
]->event_name
, ret
);
1837 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1840 int lttng_event_notifier_enabler_ref_event_notifiers(
1841 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1843 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1844 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1847 * Only try to create event_notifiers for enablers that are enabled, the user
1848 * might still be attaching filter or exclusion to the
1849 * event_notifier_enabler.
1851 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
)
1854 /* First, ensure that probe event_notifiers are created for this enabler. */
1855 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
1857 /* Link the created event_notifier with its associated enabler. */
1858 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1859 struct lttng_enabler_ref
*enabler_ref
;
1861 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier_priv
->pub
))
1864 enabler_ref
= lttng_enabler_ref(&event_notifier_priv
->parent
.enablers_ref_head
,
1865 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1868 * If no backward ref, create it.
1869 * Add backward ref from event_notifier to enabler.
1871 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1875 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
1876 event_notifier_enabler
);
1877 cds_list_add(&enabler_ref
->node
,
1878 &event_notifier_priv
->parent
.enablers_ref_head
);
1882 * Link filter bytecodes if not linked yet.
1884 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1885 &event_notifier_group
->ctx
,
1886 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
,
1887 <tng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->filter_bytecode_head
);
1890 * Link capture bytecodes if not linked yet.
1892 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1893 &event_notifier_group
->ctx
, &event_notifier_priv
->capture_bytecode_runtime_head
,
1894 &event_notifier_enabler
->capture_bytecode_head
);
1896 event_notifier_priv
->num_captures
= event_notifier_enabler
->num_captures
;
1903 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
1905 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1906 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1908 cds_list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
1909 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
1912 * For each event_notifier, if at least one of its enablers is enabled,
1913 * we enable the event_notifier, else we disable it.
1915 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1916 struct lttng_enabler_ref
*enabler_ref
;
1917 struct lttng_ust_bytecode_runtime
*runtime
;
1918 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1919 int nr_filters
= 0, nr_captures
= 0;
1921 /* Enable event_notifiers */
1922 cds_list_for_each_entry(enabler_ref
,
1923 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1924 if (enabler_ref
->ref
->enabled
) {
1930 CMM_STORE_SHARED(event_notifier_priv
->pub
->parent
->enabled
, enabled
);
1932 * Sync tracepoint registration with event_notifier enabled
1936 if (!event_notifier_priv
->parent
.registered
)
1937 register_event(event_notifier_priv
->parent
.pub
);
1939 if (event_notifier_priv
->parent
.registered
)
1940 unregister_event(event_notifier_priv
->parent
.pub
);
1943 /* Check if has enablers without bytecode enabled */
1944 cds_list_for_each_entry(enabler_ref
,
1945 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1946 if (enabler_ref
->ref
->enabled
1947 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1948 has_enablers_without_filter_bytecode
= 1;
1952 event_notifier_priv
->parent
.has_enablers_without_filter_bytecode
=
1953 has_enablers_without_filter_bytecode
;
1955 /* Enable filters */
1956 cds_list_for_each_entry(runtime
,
1957 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1958 lttng_bytecode_sync_state(runtime
);
1961 CMM_STORE_SHARED(event_notifier_priv
->parent
.pub
->eval_filter
,
1962 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1964 /* Enable captures. */
1965 cds_list_for_each_entry(runtime
,
1966 &event_notifier_priv
->capture_bytecode_runtime_head
, node
) {
1967 lttng_bytecode_sync_state(runtime
);
1970 CMM_STORE_SHARED(event_notifier_priv
->pub
->eval_capture
,
1973 lttng_ust_tp_probe_prune_release_queue();
1977 * Apply enablers to session events, adding events to session if need
1978 * be. It is required after each modification applied to an active
1979 * session, and right before session "start".
1980 * "lazy" sync means we only sync if required.
1983 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
)
1985 /* We can skip if session is not active */
1986 if (!session
->active
)
1988 lttng_session_sync_event_enablers(session
);
1992 * Update all sessions with the given app context.
1993 * Called with ust lock held.
1994 * This is invoked when an application context gets loaded/unloaded. It
1995 * ensures the context callbacks are in sync with the application
1996 * context (either app context callbacks, or dummy callbacks).
1998 void lttng_ust_context_set_session_provider(const char *name
,
1999 size_t (*get_size
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
2001 void (*record
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
2002 struct lttng_ust_ring_buffer_ctx
*ctx
,
2003 struct lttng_ust_channel_buffer
*chan
),
2004 void (*get_value
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
2005 struct lttng_ust_ctx_value
*value
),
2008 struct lttng_ust_session_private
*session_priv
;
2010 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
2011 struct lttng_ust_channel_buffer_private
*chan
;
2012 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
2015 ret
= lttng_ust_context_set_provider_rcu(&session_priv
->ctx
,
2016 name
, get_size
, record
, get_value
, priv
);
2019 cds_list_for_each_entry(chan
, &session_priv
->chan_head
, node
) {
2020 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
2021 name
, get_size
, record
, get_value
, priv
);
2025 cds_list_for_each_entry(event_recorder_priv
, &session_priv
->events_head
, node
) {
2026 ret
= lttng_ust_context_set_provider_rcu(&event_recorder_priv
->ctx
,
2027 name
, get_size
, record
, get_value
, priv
);
2035 * Update all event_notifier groups with the given app context.
2036 * Called with ust lock held.
2037 * This is invoked when an application context gets loaded/unloaded. It
2038 * ensures the context callbacks are in sync with the application
2039 * context (either app context callbacks, or dummy callbacks).
2041 void lttng_ust_context_set_event_notifier_group_provider(const char *name
,
2042 size_t (*get_size
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
2044 void (*record
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
2045 struct lttng_ust_ring_buffer_ctx
*ctx
,
2046 struct lttng_ust_channel_buffer
*chan
),
2047 void (*get_value
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
2048 struct lttng_ust_ctx_value
*value
),
2051 struct lttng_event_notifier_group
*event_notifier_group
;
2053 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2056 ret
= lttng_ust_context_set_provider_rcu(
2057 &event_notifier_group
->ctx
,
2058 name
, get_size
, record
, get_value
, priv
);
2064 int lttng_ust_session_uuid_validate(struct lttng_ust_session
*session
,
2065 unsigned char *uuid
)
2069 /* Compare UUID with session. */
2070 if (session
->priv
->uuid_set
) {
2071 if (memcmp(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
)) {
2075 memcpy(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
2076 session
->priv
->uuid_set
= true;