2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
24 #include <lttng/ust-endian.h>
26 #include <urcu/arch.h>
27 #include <urcu/compiler.h>
28 #include <urcu/hlist.h>
29 #include <urcu/list.h>
30 #include <urcu/uatomic.h>
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
35 #include <usterr-signal-safe.h>
36 #include <ust-helper.h>
37 #include <lttng/ust-ctl.h>
40 #include <ust-dynamic-type.h>
41 #include <ust-context-provider.h>
44 #include "lttng-ust-uuid.h"
46 #include "tracepoint-internal.h"
47 #include "string-utils.h"
48 #include "lttng-bytecode.h"
49 #include "lttng-tracer.h"
50 #include "lttng-tracer-core.h"
51 #include "lttng-ust-statedump.h"
52 #include "context-internal.h"
53 #include "ust-events-internal.h"
55 #include "../libringbuffer/shm.h"
56 #include "../libcounter/counter.h"
58 #include <lttng/ust-abi.h>
61 * All operations within this file are called by the communication
62 * thread, under ust_lock protection.
65 static CDS_LIST_HEAD(sessions
);
66 static CDS_LIST_HEAD(event_notifier_groups
);
68 struct cds_list_head
*lttng_get_sessions(void)
73 static void _lttng_event_recorder_destroy(struct lttng_ust_event_recorder
*event_recorder
);
74 static void _lttng_event_notifier_destroy(
75 struct lttng_ust_event_notifier
*event_notifier
);
76 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
79 void lttng_session_lazy_sync_event_enablers(struct lttng_session
*session
);
81 void lttng_session_sync_event_enablers(struct lttng_session
*session
);
83 void lttng_event_notifier_group_sync_enablers(
84 struct lttng_event_notifier_group
*event_notifier_group
);
86 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
89 * Called with ust lock held.
91 int lttng_session_active(void)
93 struct lttng_ust_session_private
*iter
;
95 cds_list_for_each_entry(iter
, &sessions
, node
) {
96 if (iter
->pub
->active
)
103 int lttng_loglevel_match(int loglevel
,
104 unsigned int has_loglevel
,
105 enum lttng_ust_abi_loglevel_type req_type
,
109 loglevel
= TRACE_DEFAULT
;
111 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
112 if (loglevel
<= req_loglevel
113 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
117 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
118 if (loglevel
== req_loglevel
119 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
123 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
125 if (loglevel
<= TRACE_DEBUG
)
132 struct lttng_session
*lttng_session_create(void)
134 struct lttng_session
*session
;
135 struct lttng_ust_session_private
*session_priv
;
138 session
= zmalloc(sizeof(struct lttng_session
));
141 session
->struct_size
= sizeof(struct lttng_session
);
142 session_priv
= zmalloc(sizeof(struct lttng_ust_session_private
));
147 session
->priv
= session_priv
;
148 session_priv
->pub
= session
;
149 if (lttng_context_init_all(&session
->priv
->ctx
)) {
154 CDS_INIT_LIST_HEAD(&session
->priv
->chan_head
);
155 CDS_INIT_LIST_HEAD(&session
->priv
->events_head
);
156 CDS_INIT_LIST_HEAD(&session
->priv
->enums_head
);
157 CDS_INIT_LIST_HEAD(&session
->priv
->enablers_head
);
158 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
159 CDS_INIT_HLIST_HEAD(&session
->priv
->events_ht
.table
[i
]);
160 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
161 CDS_INIT_HLIST_HEAD(&session
->priv
->enums_ht
.table
[i
]);
162 cds_list_add(&session
->priv
->node
, &sessions
);
166 struct lttng_counter
*lttng_ust_counter_create(
167 const char *counter_transport_name
,
168 size_t number_dimensions
, const struct lttng_counter_dimension
*dimensions
)
170 struct lttng_counter_transport
*counter_transport
= NULL
;
171 struct lttng_counter
*counter
= NULL
;
173 counter_transport
= lttng_counter_transport_find(counter_transport_name
);
174 if (!counter_transport
)
176 counter
= zmalloc(sizeof(struct lttng_counter
));
180 counter
->ops
= &counter_transport
->ops
;
181 counter
->transport
= counter_transport
;
183 counter
->counter
= counter
->ops
->counter_create(
184 number_dimensions
, dimensions
, 0,
186 if (!counter
->counter
) {
200 void lttng_ust_counter_destroy(struct lttng_counter
*counter
)
202 counter
->ops
->counter_destroy(counter
->counter
);
206 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
208 struct lttng_event_notifier_group
*event_notifier_group
;
211 event_notifier_group
= zmalloc(sizeof(struct lttng_event_notifier_group
));
212 if (!event_notifier_group
)
215 /* Add all contexts. */
216 if (lttng_context_init_all(&event_notifier_group
->ctx
)) {
217 free(event_notifier_group
);
221 CDS_INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
222 CDS_INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
223 for (i
= 0; i
< LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
; i
++)
224 CDS_INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
226 cds_list_add(&event_notifier_group
->node
, &event_notifier_groups
);
228 return event_notifier_group
;
232 * Only used internally at session destruction.
235 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
237 struct channel
*chan
;
238 struct lttng_ust_shm_handle
*handle
;
240 cds_list_del(<tng_chan
->node
);
241 lttng_destroy_context(lttng_chan
->ctx
);
242 chan
= lttng_chan
->chan
;
243 handle
= lttng_chan
->handle
;
245 * note: lttng_chan is private data contained within handle. It
246 * will be freed along with the handle.
248 channel_destroy(chan
, handle
, 0);
252 void register_event_recorder(struct lttng_ust_event_recorder
*event_recorder
)
255 const struct lttng_event_desc
*desc
;
257 assert(event_recorder
->parent
->priv
->registered
== 0);
258 desc
= event_recorder
->parent
->priv
->desc
;
259 ret
= __tracepoint_probe_register_queue_release(desc
->name
,
260 desc
->probe_callback
,
261 event_recorder
, desc
->signature
);
264 event_recorder
->parent
->priv
->registered
= 1;
268 void register_event_notifier(struct lttng_ust_event_notifier
*event_notifier
)
271 const struct lttng_event_desc
*desc
;
273 assert(event_notifier
->parent
->priv
->registered
== 0);
274 desc
= event_notifier
->parent
->priv
->desc
;
275 ret
= __tracepoint_probe_register_queue_release(desc
->name
,
276 desc
->u
.ext
.event_notifier_callback
, event_notifier
, desc
->signature
);
279 event_notifier
->parent
->priv
->registered
= 1;
283 void unregister_event_recorder(struct lttng_ust_event_recorder
*event_recorder
)
286 const struct lttng_event_desc
*desc
;
288 assert(event_recorder
->parent
->priv
->registered
== 1);
289 desc
= event_recorder
->parent
->priv
->desc
;
290 ret
= __tracepoint_probe_unregister_queue_release(desc
->name
,
291 desc
->probe_callback
,
295 event_recorder
->parent
->priv
->registered
= 0;
299 void unregister_event_notifier(struct lttng_ust_event_notifier
*event_notifier
)
302 const struct lttng_event_desc
*desc
;
304 assert(event_notifier
->parent
->priv
->registered
== 1);
305 desc
= event_notifier
->parent
->priv
->desc
;
306 ret
= __tracepoint_probe_unregister_queue_release(desc
->name
,
307 desc
->u
.ext
.event_notifier_callback
, event_notifier
);
310 event_notifier
->parent
->priv
->registered
= 0;
314 * Only used internally at session destruction.
317 void _lttng_event_recorder_unregister(struct lttng_ust_event_recorder
*event_recorder
)
319 if (event_recorder
->parent
->priv
->registered
)
320 unregister_event_recorder(event_recorder
);
324 * Only used internally at session destruction.
327 void _lttng_event_notifier_unregister(struct lttng_ust_event_notifier
*event_notifier
)
329 if (event_notifier
->parent
->priv
->registered
)
330 unregister_event_notifier(event_notifier
);
333 void lttng_session_destroy(struct lttng_session
*session
)
335 struct lttng_channel
*chan
, *tmpchan
;
336 struct lttng_ust_event_recorder_private
*event_recorder_priv
, *tmpevent_recorder_priv
;
337 struct lttng_enum
*_enum
, *tmp_enum
;
338 struct lttng_event_enabler
*event_enabler
, *event_tmpenabler
;
340 CMM_ACCESS_ONCE(session
->active
) = 0;
341 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
342 _lttng_event_recorder_unregister(event_recorder_priv
->pub
);
344 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
345 __tracepoint_probe_prune_release_queue();
346 cds_list_for_each_entry_safe(event_enabler
, event_tmpenabler
,
347 &session
->priv
->enablers_head
, node
)
348 lttng_event_enabler_destroy(event_enabler
);
349 cds_list_for_each_entry_safe(event_recorder_priv
, tmpevent_recorder_priv
,
350 &session
->priv
->events_head
, node
)
351 _lttng_event_recorder_destroy(event_recorder_priv
->pub
);
352 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
353 &session
->priv
->enums_head
, node
)
354 _lttng_enum_destroy(_enum
);
355 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->priv
->chan_head
, node
)
356 _lttng_channel_unmap(chan
);
357 cds_list_del(&session
->priv
->node
);
358 lttng_destroy_context(session
->priv
->ctx
);
363 void lttng_event_notifier_group_destroy(
364 struct lttng_event_notifier_group
*event_notifier_group
)
367 struct lttng_event_notifier_enabler
*notifier_enabler
, *tmpnotifier_enabler
;
368 struct lttng_ust_event_notifier_private
*notifier_priv
, *tmpnotifier_priv
;
370 if (!event_notifier_group
) {
374 cds_list_for_each_entry(notifier_priv
,
375 &event_notifier_group
->event_notifiers_head
, node
)
376 _lttng_event_notifier_unregister(notifier_priv
->pub
);
378 lttng_ust_urcu_synchronize_rcu();
380 cds_list_for_each_entry_safe(notifier_enabler
, tmpnotifier_enabler
,
381 &event_notifier_group
->enablers_head
, node
)
382 lttng_event_notifier_enabler_destroy(notifier_enabler
);
384 cds_list_for_each_entry_safe(notifier_priv
, tmpnotifier_priv
,
385 &event_notifier_group
->event_notifiers_head
, node
)
386 _lttng_event_notifier_destroy(notifier_priv
->pub
);
388 if (event_notifier_group
->error_counter
)
389 lttng_ust_counter_destroy(event_notifier_group
->error_counter
);
391 /* Close the notification fd to the listener of event_notifiers. */
393 lttng_ust_lock_fd_tracker();
394 close_ret
= close(event_notifier_group
->notification_fd
);
396 lttng_ust_delete_fd_from_tracker(
397 event_notifier_group
->notification_fd
);
402 lttng_ust_unlock_fd_tracker();
404 cds_list_del(&event_notifier_group
->node
);
406 free(event_notifier_group
);
410 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
412 struct lttng_ust_bytecode_node
*filter_node
, *tmp_filter_node
;
413 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
419 /* Destroy filter bytecode */
420 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
421 &enabler
->filter_bytecode_head
, node
) {
425 /* Destroy excluders */
426 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
427 &enabler
->excluder_head
, node
) {
432 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
434 if (!event_notifier_enabler
) {
438 cds_list_del(&event_notifier_enabler
->node
);
440 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
442 free(event_notifier_enabler
);
446 int lttng_enum_create(const struct lttng_enum_desc
*desc
,
447 struct lttng_session
*session
)
449 const char *enum_name
= desc
->name
;
450 struct lttng_enum
*_enum
;
451 struct cds_hlist_head
*head
;
453 size_t name_len
= strlen(enum_name
);
457 /* Check if this enum is already registered for this session. */
458 hash
= jhash(enum_name
, name_len
, 0);
459 head
= &session
->priv
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
461 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
467 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
468 if (notify_socket
< 0) {
473 _enum
= zmalloc(sizeof(*_enum
));
478 _enum
->session
= session
;
481 ret
= ustcomm_register_enum(notify_socket
,
488 DBG("Error (%d) registering enumeration to sessiond", ret
);
489 goto sessiond_register_error
;
491 cds_list_add(&_enum
->node
, &session
->priv
->enums_head
);
492 cds_hlist_add_head(&_enum
->hlist
, head
);
495 sessiond_register_error
:
504 int lttng_create_enum_check(const struct lttng_type
*type
,
505 struct lttng_session
*session
)
507 switch (type
->atype
) {
508 case atype_enum_nestable
:
510 const struct lttng_enum_desc
*enum_desc
;
513 enum_desc
= type
->u
.enum_nestable
.desc
;
514 ret
= lttng_enum_create(enum_desc
, session
);
515 if (ret
&& ret
!= -EEXIST
) {
516 DBG("Unable to create enum error: (%d)", ret
);
523 const struct lttng_event_field
*tag_field_generic
;
524 const struct lttng_enum_desc
*enum_desc
;
527 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
528 enum_desc
= tag_field_generic
->type
.u
.enum_nestable
.desc
;
529 ret
= lttng_enum_create(enum_desc
, session
);
530 if (ret
&& ret
!= -EEXIST
) {
531 DBG("Unable to create enum error: (%d)", ret
);
537 /* TODO: nested types when they become supported. */
544 int lttng_create_all_event_enums(size_t nr_fields
,
545 const struct lttng_event_field
*event_fields
,
546 struct lttng_session
*session
)
551 /* For each field, ensure enum is part of the session. */
552 for (i
= 0; i
< nr_fields
; i
++) {
553 const struct lttng_type
*type
= &event_fields
[i
].type
;
555 ret
= lttng_create_enum_check(type
, session
);
563 int lttng_create_all_ctx_enums(size_t nr_fields
,
564 const struct lttng_ctx_field
*ctx_fields
,
565 struct lttng_session
*session
)
570 /* For each field, ensure enum is part of the session. */
571 for (i
= 0; i
< nr_fields
; i
++) {
572 const struct lttng_type
*type
= &ctx_fields
[i
].event_field
.type
;
574 ret
= lttng_create_enum_check(type
, session
);
582 * Ensure that a state-dump will be performed for this session at the end
583 * of the current handle_message().
585 int lttng_session_statedump(struct lttng_session
*session
)
587 session
->priv
->statedump_pending
= 1;
588 lttng_ust_sockinfo_session_enabled(session
->priv
->owner
);
592 int lttng_session_enable(struct lttng_session
*session
)
595 struct lttng_channel
*chan
;
598 if (session
->active
) {
603 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
604 if (notify_socket
< 0)
605 return notify_socket
;
607 /* Set transient enabler state to "enabled" */
608 session
->priv
->tstate
= 1;
610 /* We need to sync enablers with session before activation. */
611 lttng_session_sync_event_enablers(session
);
614 * Snapshot the number of events per channel to know the type of header
617 cds_list_for_each_entry(chan
, &session
->priv
->chan_head
, node
) {
618 const struct lttng_ctx
*ctx
;
619 const struct lttng_ctx_field
*fields
= NULL
;
620 size_t nr_fields
= 0;
623 /* don't change it if session stop/restart */
624 if (chan
->header_type
)
628 nr_fields
= ctx
->nr_fields
;
629 fields
= ctx
->fields
;
630 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
633 DBG("Error (%d) adding enum to session", ret
);
637 ret
= ustcomm_register_channel(notify_socket
,
646 DBG("Error (%d) registering channel to sessiond", ret
);
649 if (chan_id
!= chan
->id
) {
650 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
656 /* Set atomically the state to "active" */
657 CMM_ACCESS_ONCE(session
->active
) = 1;
658 CMM_ACCESS_ONCE(session
->priv
->been_active
) = 1;
660 ret
= lttng_session_statedump(session
);
667 int lttng_session_disable(struct lttng_session
*session
)
671 if (!session
->active
) {
675 /* Set atomically the state to "inactive" */
676 CMM_ACCESS_ONCE(session
->active
) = 0;
678 /* Set transient enabler state to "disabled" */
679 session
->priv
->tstate
= 0;
680 lttng_session_sync_event_enablers(session
);
685 int lttng_channel_enable(struct lttng_channel
*channel
)
689 if (channel
->enabled
) {
693 /* Set transient enabler state to "enabled" */
695 lttng_session_sync_event_enablers(channel
->session
);
696 /* Set atomically the state to "enabled" */
697 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
702 int lttng_channel_disable(struct lttng_channel
*channel
)
706 if (!channel
->enabled
) {
710 /* Set atomically the state to "disabled" */
711 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
712 /* Set transient enabler state to "enabled" */
714 lttng_session_sync_event_enablers(channel
->session
);
720 struct cds_hlist_head
*borrow_hash_table_bucket(
721 struct cds_hlist_head
*hash_table
,
722 unsigned int hash_table_size
,
723 const struct lttng_event_desc
*desc
)
725 const char *event_name
;
729 event_name
= desc
->name
;
730 name_len
= strlen(event_name
);
732 hash
= jhash(event_name
, name_len
, 0);
733 return &hash_table
[hash
& (hash_table_size
- 1)];
737 * Supports event creation while tracing session is active.
740 int lttng_event_recorder_create(const struct lttng_event_desc
*desc
,
741 struct lttng_channel
*chan
)
743 struct lttng_ust_event_recorder
*event_recorder
;
744 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
745 struct lttng_session
*session
= chan
->session
;
746 struct cds_hlist_head
*head
;
748 int notify_socket
, loglevel
;
751 head
= borrow_hash_table_bucket(chan
->session
->priv
->events_ht
.table
,
752 LTTNG_UST_EVENT_HT_SIZE
, desc
);
754 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
755 if (notify_socket
< 0) {
760 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
763 DBG("Error (%d) adding enum to session", ret
);
764 goto create_enum_error
;
768 * Check if loglevel match. Refuse to connect event if not.
770 event_recorder
= zmalloc(sizeof(struct lttng_ust_event_recorder
));
771 if (!event_recorder
) {
775 event_recorder
->struct_size
= sizeof(struct lttng_ust_event_recorder
);
777 event_recorder
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
778 if (!event_recorder
->parent
) {
782 event_recorder
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
784 event_recorder_priv
= zmalloc(sizeof(struct lttng_ust_event_recorder_private
));
785 if (!event_recorder_priv
) {
789 event_recorder
->priv
= event_recorder_priv
;
790 event_recorder_priv
->pub
= event_recorder
;
791 event_recorder
->parent
->priv
= &event_recorder_priv
->parent
;
792 event_recorder_priv
->parent
.pub
= event_recorder
->parent
;
794 event_recorder
->chan
= chan
;
796 /* Event will be enabled by enabler sync. */
797 event_recorder
->parent
->enabled
= 0;
798 event_recorder
->parent
->priv
->registered
= 0;
799 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->filter_bytecode_runtime_head
);
800 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->enablers_ref_head
);
801 event_recorder
->parent
->priv
->desc
= desc
;
804 loglevel
= *(*event_recorder
->parent
->priv
->desc
->loglevel
);
806 loglevel
= TRACE_DEFAULT
;
807 if (desc
->u
.ext
.model_emf_uri
)
808 uri
= *(desc
->u
.ext
.model_emf_uri
);
812 /* Fetch event ID from sessiond */
813 ret
= ustcomm_register_event(notify_socket
,
823 &event_recorder
->id
);
825 DBG("Error (%d) registering event to sessiond", ret
);
826 goto sessiond_register_error
;
829 cds_list_add(&event_recorder
->priv
->node
, &chan
->session
->priv
->events_head
);
830 cds_hlist_add_head(&event_recorder
->priv
->hlist
, head
);
833 sessiond_register_error
:
834 free(event_recorder_priv
);
836 free(event_recorder
->parent
);
838 free(event_recorder
);
846 int lttng_event_notifier_create(const struct lttng_event_desc
*desc
,
847 uint64_t token
, uint64_t error_counter_index
,
848 struct lttng_event_notifier_group
*event_notifier_group
)
850 struct lttng_ust_event_notifier
*event_notifier
;
851 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
852 struct cds_hlist_head
*head
;
856 * Get the hashtable bucket the created lttng_event_notifier object
857 * should be inserted.
859 head
= borrow_hash_table_bucket(
860 event_notifier_group
->event_notifiers_ht
.table
,
861 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
863 event_notifier
= zmalloc(sizeof(struct lttng_ust_event_notifier
));
864 if (!event_notifier
) {
868 event_notifier
->struct_size
= sizeof(struct lttng_ust_event_notifier
);
870 event_notifier
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
871 if (!event_notifier
->parent
) {
875 event_notifier
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
877 event_notifier_priv
= zmalloc(sizeof(struct lttng_ust_event_notifier_private
));
878 if (!event_notifier_priv
) {
882 event_notifier
->priv
= event_notifier_priv
;
883 event_notifier_priv
->pub
= event_notifier
;
884 event_notifier
->parent
->priv
= &event_notifier_priv
->parent
;
885 event_notifier_priv
->parent
.pub
= event_notifier
->parent
;
887 event_notifier_priv
->group
= event_notifier_group
;
888 event_notifier_priv
->parent
.user_token
= token
;
889 event_notifier_priv
->error_counter_index
= error_counter_index
;
891 /* Event notifier will be enabled by enabler sync. */
892 event_notifier
->parent
->enabled
= 0;
893 event_notifier_priv
->parent
.registered
= 0;
895 CDS_INIT_LIST_HEAD(&event_notifier
->parent
->filter_bytecode_runtime_head
);
896 CDS_INIT_LIST_HEAD(&event_notifier
->capture_bytecode_runtime_head
);
897 CDS_INIT_LIST_HEAD(&event_notifier_priv
->parent
.enablers_ref_head
);
898 event_notifier_priv
->parent
.desc
= desc
;
899 event_notifier
->notification_send
= lttng_event_notifier_notification_send
;
901 cds_list_add(&event_notifier_priv
->node
,
902 &event_notifier_group
->event_notifiers_head
);
903 cds_hlist_add_head(&event_notifier_priv
->hlist
, head
);
908 free(event_notifier
->parent
);
910 free(event_notifier
);
916 void _lttng_event_notifier_destroy(struct lttng_ust_event_notifier
*event_notifier
)
918 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
920 /* Remove from event_notifier list. */
921 cds_list_del(&event_notifier
->priv
->node
);
922 /* Remove from event_notifier hash table. */
923 cds_hlist_del(&event_notifier
->priv
->hlist
);
925 lttng_free_event_filter_runtime(event_notifier
->parent
);
927 /* Free event_notifier enabler refs */
928 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
929 &event_notifier
->priv
->parent
.enablers_ref_head
, node
)
931 free(event_notifier
->priv
);
932 free(event_notifier
->parent
);
933 free(event_notifier
);
937 int lttng_desc_match_star_glob_enabler(const struct lttng_event_desc
*desc
,
938 struct lttng_enabler
*enabler
)
941 unsigned int has_loglevel
= 0;
943 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_STAR_GLOB
);
944 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
945 desc
->name
, SIZE_MAX
))
947 if (desc
->loglevel
) {
948 loglevel
= *(*desc
->loglevel
);
951 if (!lttng_loglevel_match(loglevel
,
953 enabler
->event_param
.loglevel_type
,
954 enabler
->event_param
.loglevel
))
960 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
961 struct lttng_enabler
*enabler
)
964 unsigned int has_loglevel
= 0;
966 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_EVENT
);
967 if (strcmp(desc
->name
, enabler
->event_param
.name
))
969 if (desc
->loglevel
) {
970 loglevel
= *(*desc
->loglevel
);
973 if (!lttng_loglevel_match(loglevel
,
975 enabler
->event_param
.loglevel_type
,
976 enabler
->event_param
.loglevel
))
982 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
983 struct lttng_enabler
*enabler
)
985 switch (enabler
->format_type
) {
986 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
988 struct lttng_ust_excluder_node
*excluder
;
990 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
995 * If the matching event matches with an excluder,
996 * return 'does not match'
998 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
1001 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
1003 char *excluder_name
;
1005 excluder_name
= (char *) (excluder
->excluder
.names
)
1006 + count
* LTTNG_UST_ABI_SYM_NAME_LEN
;
1007 len
= strnlen(excluder_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1008 if (len
> 0 && strutils_star_glob_match(excluder_name
, len
, desc
->name
, SIZE_MAX
))
1014 case LTTNG_ENABLER_FORMAT_EVENT
:
1015 return lttng_desc_match_event_enabler(desc
, enabler
);
1022 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
1023 struct lttng_ust_event_recorder
*event_recorder
)
1025 if (lttng_desc_match_enabler(event_recorder
->parent
->priv
->desc
,
1026 lttng_event_enabler_as_enabler(event_enabler
))
1027 && event_recorder
->chan
== event_enabler
->chan
)
1034 int lttng_event_notifier_enabler_match_event_notifier(
1035 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1036 struct lttng_ust_event_notifier
*event_notifier
)
1038 int desc_matches
= lttng_desc_match_enabler(event_notifier
->priv
->parent
.desc
,
1039 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1041 if (desc_matches
&& event_notifier
->priv
->group
== event_notifier_enabler
->group
&&
1042 event_notifier
->priv
->parent
.user_token
== event_notifier_enabler
->user_token
)
1049 struct lttng_enabler_ref
*lttng_enabler_ref(
1050 struct cds_list_head
*enabler_ref_list
,
1051 struct lttng_enabler
*enabler
)
1053 struct lttng_enabler_ref
*enabler_ref
;
1055 cds_list_for_each_entry(enabler_ref
, enabler_ref_list
, node
) {
1056 if (enabler_ref
->ref
== enabler
)
1063 * Create struct lttng_event if it is missing and present in the list of
1064 * tracepoint probes.
1067 void lttng_create_event_if_missing(struct lttng_event_enabler
*event_enabler
)
1069 struct lttng_session
*session
= event_enabler
->chan
->session
;
1070 struct lttng_probe_desc
*probe_desc
;
1071 const struct lttng_event_desc
*desc
;
1072 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1074 struct cds_list_head
*probe_list
;
1076 probe_list
= lttng_get_probe_list_head();
1078 * For each probe event, if we find that a probe event matches
1079 * our enabler, create an associated lttng_event if not
1082 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
1083 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1086 struct cds_hlist_head
*head
;
1087 struct cds_hlist_node
*node
;
1089 desc
= probe_desc
->event_desc
[i
];
1090 if (!lttng_desc_match_enabler(desc
,
1091 lttng_event_enabler_as_enabler(event_enabler
)))
1094 head
= borrow_hash_table_bucket(
1095 session
->priv
->events_ht
.table
,
1096 LTTNG_UST_EVENT_HT_SIZE
, desc
);
1098 cds_hlist_for_each_entry(event_recorder_priv
, node
, head
, hlist
) {
1099 if (event_recorder_priv
->parent
.desc
== desc
1100 && event_recorder_priv
->pub
->chan
== event_enabler
->chan
) {
1109 * We need to create an event for this
1112 ret
= lttng_event_recorder_create(probe_desc
->event_desc
[i
],
1113 event_enabler
->chan
);
1115 DBG("Unable to create event %s, error %d\n",
1116 probe_desc
->event_desc
[i
]->name
, ret
);
1123 void probe_provider_event_for_each(struct lttng_probe_desc
*provider_desc
,
1124 void (*event_func
)(struct lttng_session
*session
,
1125 struct lttng_ust_event_recorder
*event_recorder
),
1126 void (*event_notifier_func
)(struct lttng_ust_event_notifier
*event_notifier
))
1128 struct cds_hlist_node
*node
, *tmp_node
;
1129 struct cds_list_head
*sessionsp
;
1132 /* Get handle on list of sessions. */
1133 sessionsp
= lttng_get_sessions();
1136 * Iterate over all events in the probe provider descriptions and
1137 * sessions to queue the unregistration of the events.
1139 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
1140 const struct lttng_event_desc
*event_desc
;
1141 struct lttng_event_notifier_group
*event_notifier_group
;
1142 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1143 struct lttng_ust_session_private
*session_priv
;
1144 struct cds_hlist_head
*head
;
1145 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1147 event_desc
= provider_desc
->event_desc
[i
];
1150 * Iterate over all session to find the current event
1153 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
1155 * Get the list of events in the hashtable bucket and
1156 * iterate to find the event matching this descriptor.
1158 head
= borrow_hash_table_bucket(
1159 session_priv
->events_ht
.table
,
1160 LTTNG_UST_EVENT_HT_SIZE
, event_desc
);
1162 cds_hlist_for_each_entry_safe(event_recorder_priv
, node
, tmp_node
, head
, hlist
) {
1163 if (event_desc
== event_recorder_priv
->parent
.desc
) {
1164 event_func(session_priv
->pub
, event_recorder_priv
->pub
);
1171 * Iterate over all event_notifier groups to find the current event
1174 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1176 * Get the list of event_notifiers in the hashtable bucket and
1177 * iterate to find the event_notifier matching this
1180 head
= borrow_hash_table_bucket(
1181 event_notifier_group
->event_notifiers_ht
.table
,
1182 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, event_desc
);
1184 cds_hlist_for_each_entry_safe(event_notifier_priv
, node
, tmp_node
, head
, hlist
) {
1185 if (event_desc
== event_notifier_priv
->parent
.desc
) {
1186 event_notifier_func(event_notifier_priv
->pub
);
1195 void _unregister_event_recorder(struct lttng_session
*session
,
1196 struct lttng_ust_event_recorder
*event_recorder
)
1198 _lttng_event_recorder_unregister(event_recorder
);
1202 void _event_enum_destroy(struct lttng_session
*session
,
1203 struct lttng_ust_event_recorder
*event_recorder
)
1207 /* Destroy enums of the current event. */
1208 for (i
= 0; i
< event_recorder
->parent
->priv
->desc
->nr_fields
; i
++) {
1209 const struct lttng_enum_desc
*enum_desc
;
1210 const struct lttng_event_field
*field
;
1211 struct lttng_enum
*curr_enum
;
1213 field
= &(event_recorder
->parent
->priv
->desc
->fields
[i
]);
1214 switch (field
->type
.atype
) {
1215 case atype_enum_nestable
:
1216 enum_desc
= field
->type
.u
.enum_nestable
.desc
;
1222 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
1224 _lttng_enum_destroy(curr_enum
);
1228 /* Destroy event. */
1229 _lttng_event_recorder_destroy(event_recorder
);
1233 * Iterate over all the UST sessions to unregister and destroy all probes from
1234 * the probe provider descriptor received as argument. Must me called with the
1237 void lttng_probe_provider_unregister_events(
1238 struct lttng_probe_desc
*provider_desc
)
1241 * Iterate over all events in the probe provider descriptions and sessions
1242 * to queue the unregistration of the events.
1244 probe_provider_event_for_each(provider_desc
, _unregister_event_recorder
,
1245 _lttng_event_notifier_unregister
);
1247 /* Wait for grace period. */
1248 lttng_ust_urcu_synchronize_rcu();
1249 /* Prune the unregistration queue. */
1250 __tracepoint_probe_prune_release_queue();
1253 * It is now safe to destroy the events and remove them from the event list
1256 probe_provider_event_for_each(provider_desc
, _event_enum_destroy
,
1257 _lttng_event_notifier_destroy
);
1261 * Create events associated with an event enabler (if not already present),
1262 * and add backward reference from the event to the enabler.
1265 int lttng_event_enabler_ref_events(struct lttng_event_enabler
*event_enabler
)
1267 struct lttng_session
*session
= event_enabler
->chan
->session
;
1268 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1270 if (!lttng_event_enabler_as_enabler(event_enabler
)->enabled
)
1273 /* First ensure that probe events are created for this enabler. */
1274 lttng_create_event_if_missing(event_enabler
);
1276 /* For each event matching enabler in session event list. */
1277 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1278 struct lttng_enabler_ref
*enabler_ref
;
1280 if (!lttng_event_enabler_match_event(event_enabler
, event_recorder_priv
->pub
))
1283 enabler_ref
= lttng_enabler_ref(&event_recorder_priv
->parent
.enablers_ref_head
,
1284 lttng_event_enabler_as_enabler(event_enabler
));
1287 * If no backward ref, create it.
1288 * Add backward ref from event to enabler.
1290 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1293 enabler_ref
->ref
= lttng_event_enabler_as_enabler(
1295 cds_list_add(&enabler_ref
->node
,
1296 &event_recorder_priv
->parent
.enablers_ref_head
);
1300 * Link filter bytecodes if not linked yet.
1302 lttng_enabler_link_bytecode(event_recorder_priv
->parent
.desc
,
1303 &session
->priv
->ctx
,
1304 &event_recorder_priv
->pub
->parent
->filter_bytecode_runtime_head
,
1305 <tng_event_enabler_as_enabler(event_enabler
)->filter_bytecode_head
);
1307 /* TODO: merge event context. */
1314 * Called at library load: connect the probe on all enablers matching
1316 * Called with session mutex held.
1318 int lttng_fix_pending_events(void)
1320 struct lttng_ust_session_private
*session_priv
;
1322 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1323 lttng_session_lazy_sync_event_enablers(session_priv
->pub
);
1328 int lttng_fix_pending_event_notifiers(void)
1330 struct lttng_event_notifier_group
*event_notifier_group
;
1332 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1333 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1339 * For each session of the owner thread, execute pending statedump.
1340 * Only dump state for the sessions owned by the caller thread, because
1341 * we don't keep ust_lock across the entire iteration.
1343 void lttng_handle_pending_statedump(void *owner
)
1345 struct lttng_ust_session_private
*session_priv
;
1347 /* Execute state dump */
1348 do_lttng_ust_statedump(owner
);
1350 /* Clear pending state dump */
1354 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1355 if (session_priv
->owner
!= owner
)
1357 if (!session_priv
->statedump_pending
)
1359 session_priv
->statedump_pending
= 0;
1367 * Only used internally at session destruction.
1370 void _lttng_event_recorder_destroy(struct lttng_ust_event_recorder
*event_recorder
)
1372 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1374 /* Remove from event list. */
1375 cds_list_del(&event_recorder
->priv
->node
);
1376 /* Remove from event hash table. */
1377 cds_hlist_del(&event_recorder
->priv
->hlist
);
1379 lttng_destroy_context(event_recorder
->ctx
);
1380 lttng_free_event_filter_runtime(event_recorder
->parent
);
1381 /* Free event enabler refs */
1382 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1383 &event_recorder
->parent
->priv
->enablers_ref_head
, node
)
1385 free(event_recorder
->priv
);
1386 free(event_recorder
->parent
);
1387 free(event_recorder
);
1391 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1393 cds_list_del(&_enum
->node
);
1394 cds_hlist_del(&_enum
->hlist
);
1398 void lttng_ust_abi_events_exit(void)
1400 struct lttng_ust_session_private
*session_priv
, *tmpsession_priv
;
1402 cds_list_for_each_entry_safe(session_priv
, tmpsession_priv
, &sessions
, node
)
1403 lttng_session_destroy(session_priv
->pub
);
1407 * Enabler management.
1409 struct lttng_event_enabler
*lttng_event_enabler_create(
1410 enum lttng_enabler_format_type format_type
,
1411 struct lttng_ust_abi_event
*event_param
,
1412 struct lttng_channel
*chan
)
1414 struct lttng_event_enabler
*event_enabler
;
1416 event_enabler
= zmalloc(sizeof(*event_enabler
));
1419 event_enabler
->base
.format_type
= format_type
;
1420 CDS_INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1421 CDS_INIT_LIST_HEAD(&event_enabler
->base
.excluder_head
);
1422 memcpy(&event_enabler
->base
.event_param
, event_param
,
1423 sizeof(event_enabler
->base
.event_param
));
1424 event_enabler
->chan
= chan
;
1426 event_enabler
->base
.enabled
= 0;
1427 cds_list_add(&event_enabler
->node
, &event_enabler
->chan
->session
->priv
->enablers_head
);
1428 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1430 return event_enabler
;
1433 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
1434 struct lttng_event_notifier_group
*event_notifier_group
,
1435 enum lttng_enabler_format_type format_type
,
1436 struct lttng_ust_abi_event_notifier
*event_notifier_param
)
1438 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1440 event_notifier_enabler
= zmalloc(sizeof(*event_notifier_enabler
));
1441 if (!event_notifier_enabler
)
1443 event_notifier_enabler
->base
.format_type
= format_type
;
1444 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
1445 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->capture_bytecode_head
);
1446 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.excluder_head
);
1448 event_notifier_enabler
->user_token
= event_notifier_param
->event
.token
;
1449 event_notifier_enabler
->error_counter_index
= event_notifier_param
->error_counter_index
;
1450 event_notifier_enabler
->num_captures
= 0;
1452 memcpy(&event_notifier_enabler
->base
.event_param
.name
,
1453 event_notifier_param
->event
.name
,
1454 sizeof(event_notifier_enabler
->base
.event_param
.name
));
1455 event_notifier_enabler
->base
.event_param
.instrumentation
=
1456 event_notifier_param
->event
.instrumentation
;
1457 event_notifier_enabler
->base
.event_param
.loglevel
=
1458 event_notifier_param
->event
.loglevel
;
1459 event_notifier_enabler
->base
.event_param
.loglevel_type
=
1460 event_notifier_param
->event
.loglevel_type
;
1462 event_notifier_enabler
->base
.enabled
= 0;
1463 event_notifier_enabler
->group
= event_notifier_group
;
1465 cds_list_add(&event_notifier_enabler
->node
,
1466 &event_notifier_group
->enablers_head
);
1468 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1470 return event_notifier_enabler
;
1473 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1475 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1476 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1481 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1483 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1484 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1490 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
1491 struct lttng_ust_bytecode_node
**bytecode
)
1493 (*bytecode
)->enabler
= enabler
;
1494 cds_list_add_tail(&(*bytecode
)->node
, &enabler
->filter_bytecode_head
);
1495 /* Take ownership of bytecode */
1499 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
1500 struct lttng_ust_bytecode_node
**bytecode
)
1502 _lttng_enabler_attach_filter_bytecode(
1503 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1505 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1510 void _lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1511 struct lttng_ust_excluder_node
**excluder
)
1513 (*excluder
)->enabler
= enabler
;
1514 cds_list_add_tail(&(*excluder
)->node
, &enabler
->excluder_head
);
1515 /* Take ownership of excluder */
1519 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler
*event_enabler
,
1520 struct lttng_ust_excluder_node
**excluder
)
1522 _lttng_enabler_attach_exclusion(
1523 lttng_event_enabler_as_enabler(event_enabler
), excluder
);
1525 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1529 int lttng_event_notifier_enabler_enable(
1530 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1532 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
1533 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1538 int lttng_event_notifier_enabler_disable(
1539 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1541 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
1542 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1547 int lttng_event_notifier_enabler_attach_filter_bytecode(
1548 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1549 struct lttng_ust_bytecode_node
**bytecode
)
1551 _lttng_enabler_attach_filter_bytecode(
1552 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1555 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1559 int lttng_event_notifier_enabler_attach_capture_bytecode(
1560 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1561 struct lttng_ust_bytecode_node
**bytecode
)
1563 (*bytecode
)->enabler
= lttng_event_notifier_enabler_as_enabler(
1564 event_notifier_enabler
);
1565 cds_list_add_tail(&(*bytecode
)->node
,
1566 &event_notifier_enabler
->capture_bytecode_head
);
1567 /* Take ownership of bytecode */
1569 event_notifier_enabler
->num_captures
++;
1571 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1575 int lttng_event_notifier_enabler_attach_exclusion(
1576 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1577 struct lttng_ust_excluder_node
**excluder
)
1579 _lttng_enabler_attach_exclusion(
1580 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1583 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1587 int lttng_attach_context(struct lttng_ust_abi_context
*context_param
,
1588 union lttng_ust_abi_args
*uargs
,
1589 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
1592 * We cannot attach a context after trace has been started for a
1593 * session because the metadata does not allow expressing this
1594 * information outside of the original channel scope.
1596 if (session
->priv
->been_active
)
1599 switch (context_param
->ctx
) {
1600 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
1601 return lttng_add_pthread_id_to_ctx(ctx
);
1602 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1604 struct lttng_ust_abi_perf_counter_ctx
*perf_ctx_param
;
1606 perf_ctx_param
= &context_param
->u
.perf_counter
;
1607 return lttng_add_perf_counter_to_ctx(
1608 perf_ctx_param
->type
,
1609 perf_ctx_param
->config
,
1610 perf_ctx_param
->name
,
1613 case LTTNG_UST_ABI_CONTEXT_VTID
:
1614 return lttng_add_vtid_to_ctx(ctx
);
1615 case LTTNG_UST_ABI_CONTEXT_VPID
:
1616 return lttng_add_vpid_to_ctx(ctx
);
1617 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
1618 return lttng_add_procname_to_ctx(ctx
);
1619 case LTTNG_UST_ABI_CONTEXT_IP
:
1620 return lttng_add_ip_to_ctx(ctx
);
1621 case LTTNG_UST_ABI_CONTEXT_CPU_ID
:
1622 return lttng_add_cpu_id_to_ctx(ctx
);
1623 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1624 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1626 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
1627 return lttng_add_cgroup_ns_to_ctx(ctx
);
1628 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
1629 return lttng_add_ipc_ns_to_ctx(ctx
);
1630 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
1631 return lttng_add_mnt_ns_to_ctx(ctx
);
1632 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
1633 return lttng_add_net_ns_to_ctx(ctx
);
1634 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
1635 return lttng_add_pid_ns_to_ctx(ctx
);
1636 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
1637 return lttng_add_time_ns_to_ctx(ctx
);
1638 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
1639 return lttng_add_user_ns_to_ctx(ctx
);
1640 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
1641 return lttng_add_uts_ns_to_ctx(ctx
);
1642 case LTTNG_UST_ABI_CONTEXT_VUID
:
1643 return lttng_add_vuid_to_ctx(ctx
);
1644 case LTTNG_UST_ABI_CONTEXT_VEUID
:
1645 return lttng_add_veuid_to_ctx(ctx
);
1646 case LTTNG_UST_ABI_CONTEXT_VSUID
:
1647 return lttng_add_vsuid_to_ctx(ctx
);
1648 case LTTNG_UST_ABI_CONTEXT_VGID
:
1649 return lttng_add_vgid_to_ctx(ctx
);
1650 case LTTNG_UST_ABI_CONTEXT_VEGID
:
1651 return lttng_add_vegid_to_ctx(ctx
);
1652 case LTTNG_UST_ABI_CONTEXT_VSGID
:
1653 return lttng_add_vsgid_to_ctx(ctx
);
1659 int lttng_event_enabler_attach_context(struct lttng_event_enabler
*enabler
,
1660 struct lttng_ust_abi_context
*context_param
)
1665 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
1667 if (!event_enabler
) {
1670 cds_list_del(&event_enabler
->node
);
1672 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
1674 lttng_destroy_context(event_enabler
->ctx
);
1675 free(event_enabler
);
1679 * lttng_session_sync_event_enablers should be called just before starting a
1683 void lttng_session_sync_event_enablers(struct lttng_session
*session
)
1685 struct lttng_event_enabler
*event_enabler
;
1686 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1688 cds_list_for_each_entry(event_enabler
, &session
->priv
->enablers_head
, node
)
1689 lttng_event_enabler_ref_events(event_enabler
);
1691 * For each event, if at least one of its enablers is enabled,
1692 * and its channel and session transient states are enabled, we
1693 * enable the event, else we disable it.
1695 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1696 struct lttng_enabler_ref
*enabler_ref
;
1697 struct lttng_bytecode_runtime
*runtime
;
1698 int enabled
= 0, has_enablers_without_bytecode
= 0;
1701 cds_list_for_each_entry(enabler_ref
,
1702 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1703 if (enabler_ref
->ref
->enabled
) {
1709 * Enabled state is based on union of enablers, with
1710 * intesection of session and channel transient enable
1713 enabled
= enabled
&& session
->priv
->tstate
&& event_recorder_priv
->pub
->chan
->tstate
;
1715 CMM_STORE_SHARED(event_recorder_priv
->pub
->parent
->enabled
, enabled
);
1717 * Sync tracepoint registration with event enabled
1721 if (!event_recorder_priv
->parent
.registered
)
1722 register_event_recorder(event_recorder_priv
->pub
);
1724 if (event_recorder_priv
->parent
.registered
)
1725 unregister_event_recorder(event_recorder_priv
->pub
);
1728 /* Check if has enablers without bytecode enabled */
1729 cds_list_for_each_entry(enabler_ref
,
1730 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1731 if (enabler_ref
->ref
->enabled
1732 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1733 has_enablers_without_bytecode
= 1;
1737 event_recorder_priv
->pub
->parent
->has_enablers_without_bytecode
=
1738 has_enablers_without_bytecode
;
1740 /* Enable filters */
1741 cds_list_for_each_entry(runtime
,
1742 &event_recorder_priv
->pub
->parent
->filter_bytecode_runtime_head
, node
) {
1743 lttng_bytecode_filter_sync_state(runtime
);
1746 __tracepoint_probe_prune_release_queue();
1749 /* Support for event notifier is introduced by probe provider major version 2. */
1751 bool lttng_ust_probe_supports_event_notifier(struct lttng_probe_desc
*probe_desc
)
1753 return probe_desc
->major
>= 2;
1757 void lttng_create_event_notifier_if_missing(
1758 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1760 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1761 struct lttng_probe_desc
*probe_desc
;
1762 struct cds_list_head
*probe_list
;
1765 probe_list
= lttng_get_probe_list_head();
1767 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
1768 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1771 const struct lttng_event_desc
*desc
;
1772 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1773 struct cds_hlist_head
*head
;
1774 struct cds_hlist_node
*node
;
1776 desc
= probe_desc
->event_desc
[i
];
1778 if (!lttng_desc_match_enabler(desc
,
1779 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1783 * Given the current event_notifier group, get the bucket that
1784 * the target event_notifier would be if it was already
1787 head
= borrow_hash_table_bucket(
1788 event_notifier_group
->event_notifiers_ht
.table
,
1789 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
1791 cds_hlist_for_each_entry(event_notifier_priv
, node
, head
, hlist
) {
1793 * Check if event_notifier already exists by checking
1794 * if the event_notifier and enabler share the same
1795 * description and id.
1797 if (event_notifier_priv
->parent
.desc
== desc
&&
1798 event_notifier_priv
->parent
.user_token
== event_notifier_enabler
->user_token
) {
1807 /* Check that the probe supports event notifiers, else report the error. */
1808 if (!lttng_ust_probe_supports_event_notifier(probe_desc
)) {
1809 ERR("Probe \"%s\" contains event \"%s\" which matches an enabled event notifier, "
1810 "but its version (%u.%u) is too old and does not implement event notifiers. "
1811 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1812 "this event will not generate any notification.",
1813 probe_desc
->provider
,
1820 * We need to create a event_notifier for this event probe.
1822 ret
= lttng_event_notifier_create(desc
,
1823 event_notifier_enabler
->user_token
,
1824 event_notifier_enabler
->error_counter_index
,
1825 event_notifier_group
);
1827 DBG("Unable to create event_notifier %s, error %d\n",
1828 probe_desc
->event_desc
[i
]->name
, ret
);
1835 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1838 int lttng_event_notifier_enabler_ref_event_notifiers(
1839 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1841 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1842 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1845 * Only try to create event_notifiers for enablers that are enabled, the user
1846 * might still be attaching filter or exclusion to the
1847 * event_notifier_enabler.
1849 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
)
1852 /* First, ensure that probe event_notifiers are created for this enabler. */
1853 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
1855 /* Link the created event_notifier with its associated enabler. */
1856 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1857 struct lttng_enabler_ref
*enabler_ref
;
1859 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier_priv
->pub
))
1862 enabler_ref
= lttng_enabler_ref(&event_notifier_priv
->parent
.enablers_ref_head
,
1863 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1866 * If no backward ref, create it.
1867 * Add backward ref from event_notifier to enabler.
1869 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1873 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
1874 event_notifier_enabler
);
1875 cds_list_add(&enabler_ref
->node
,
1876 &event_notifier_priv
->parent
.enablers_ref_head
);
1880 * Link filter bytecodes if not linked yet.
1882 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1883 &event_notifier_group
->ctx
,
1884 &event_notifier_priv
->pub
->parent
->filter_bytecode_runtime_head
,
1885 <tng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->filter_bytecode_head
);
1888 * Link capture bytecodes if not linked yet.
1890 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1891 &event_notifier_group
->ctx
, &event_notifier_priv
->pub
->capture_bytecode_runtime_head
,
1892 &event_notifier_enabler
->capture_bytecode_head
);
1894 event_notifier_priv
->num_captures
= event_notifier_enabler
->num_captures
;
1901 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
1903 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1904 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1906 cds_list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
1907 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
1910 * For each event_notifier, if at least one of its enablers is enabled,
1911 * we enable the event_notifier, else we disable it.
1913 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1914 struct lttng_enabler_ref
*enabler_ref
;
1915 struct lttng_bytecode_runtime
*runtime
;
1916 int enabled
= 0, has_enablers_without_bytecode
= 0;
1918 /* Enable event_notifiers */
1919 cds_list_for_each_entry(enabler_ref
,
1920 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1921 if (enabler_ref
->ref
->enabled
) {
1927 CMM_STORE_SHARED(event_notifier_priv
->pub
->parent
->enabled
, enabled
);
1929 * Sync tracepoint registration with event_notifier enabled
1933 if (!event_notifier_priv
->parent
.registered
)
1934 register_event_notifier(event_notifier_priv
->pub
);
1936 if (event_notifier_priv
->parent
.registered
)
1937 unregister_event_notifier(event_notifier_priv
->pub
);
1940 /* Check if has enablers without bytecode enabled */
1941 cds_list_for_each_entry(enabler_ref
,
1942 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1943 if (enabler_ref
->ref
->enabled
1944 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1945 has_enablers_without_bytecode
= 1;
1949 event_notifier_priv
->pub
->parent
->has_enablers_without_bytecode
=
1950 has_enablers_without_bytecode
;
1952 /* Enable filters */
1953 cds_list_for_each_entry(runtime
,
1954 &event_notifier_priv
->pub
->parent
->filter_bytecode_runtime_head
, node
) {
1955 lttng_bytecode_filter_sync_state(runtime
);
1958 /* Enable captures. */
1959 cds_list_for_each_entry(runtime
,
1960 &event_notifier_priv
->pub
->capture_bytecode_runtime_head
, node
) {
1961 lttng_bytecode_capture_sync_state(runtime
);
1964 __tracepoint_probe_prune_release_queue();
1968 * Apply enablers to session events, adding events to session if need
1969 * be. It is required after each modification applied to an active
1970 * session, and right before session "start".
1971 * "lazy" sync means we only sync if required.
1974 void lttng_session_lazy_sync_event_enablers(struct lttng_session
*session
)
1976 /* We can skip if session is not active */
1977 if (!session
->active
)
1979 lttng_session_sync_event_enablers(session
);
1983 * Update all sessions with the given app context.
1984 * Called with ust lock held.
1985 * This is invoked when an application context gets loaded/unloaded. It
1986 * ensures the context callbacks are in sync with the application
1987 * context (either app context callbacks, or dummy callbacks).
1989 void lttng_ust_context_set_session_provider(const char *name
,
1990 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
1991 void (*record
)(struct lttng_ctx_field
*field
,
1992 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
1993 struct lttng_channel
*chan
),
1994 void (*get_value
)(struct lttng_ctx_field
*field
,
1995 struct lttng_ctx_value
*value
))
1997 struct lttng_ust_session_private
*session_priv
;
1999 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
2000 struct lttng_channel
*chan
;
2001 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
2004 ret
= lttng_ust_context_set_provider_rcu(&session_priv
->ctx
,
2005 name
, get_size
, record
, get_value
);
2008 cds_list_for_each_entry(chan
, &session_priv
->chan_head
, node
) {
2009 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
2010 name
, get_size
, record
, get_value
);
2014 cds_list_for_each_entry(event_recorder_priv
, &session_priv
->events_head
, node
) {
2015 ret
= lttng_ust_context_set_provider_rcu(&event_recorder_priv
->pub
->ctx
,
2016 name
, get_size
, record
, get_value
);
2024 * Update all event_notifier groups with the given app context.
2025 * Called with ust lock held.
2026 * This is invoked when an application context gets loaded/unloaded. It
2027 * ensures the context callbacks are in sync with the application
2028 * context (either app context callbacks, or dummy callbacks).
2030 void lttng_ust_context_set_event_notifier_group_provider(const char *name
,
2031 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
2032 void (*record
)(struct lttng_ctx_field
*field
,
2033 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2034 struct lttng_channel
*chan
),
2035 void (*get_value
)(struct lttng_ctx_field
*field
,
2036 struct lttng_ctx_value
*value
))
2038 struct lttng_event_notifier_group
*event_notifier_group
;
2040 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2043 ret
= lttng_ust_context_set_provider_rcu(
2044 &event_notifier_group
->ctx
,
2045 name
, get_size
, record
, get_value
);