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 "lttng-ust-uuid.h"
45 #include "tracepoint-internal.h"
46 #include "string-utils.h"
47 #include "lttng-bytecode.h"
48 #include "lttng-tracer.h"
49 #include "lttng-tracer-core.h"
50 #include "lttng-ust-statedump.h"
51 #include "context-internal.h"
52 #include "ust-events-internal.h"
54 #include "common/ringbuffer/shm.h"
55 #include "common/ringbuffer/frontend_types.h"
56 #include "common/ringbuffer/frontend.h"
57 #include "common/counter/counter.h"
59 #include <lttng/ust-abi.h>
60 #include "context-provider-internal.h"
63 * All operations within this file are called by the communication
64 * thread, under ust_lock protection.
67 static CDS_LIST_HEAD(sessions
);
68 static CDS_LIST_HEAD(event_notifier_groups
);
70 struct cds_list_head
*lttng_get_sessions(void)
75 static void _lttng_event_destroy(struct lttng_ust_event_common
*event
);
76 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
79 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
);
81 void lttng_session_sync_event_enablers(struct lttng_ust_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
);
88 bool lttng_ust_validate_event_name(const struct lttng_ust_event_desc
*desc
)
90 if (strlen(desc
->probe_desc
->provider_name
) + 1 +
91 strlen(desc
->event_name
) >= LTTNG_UST_ABI_SYM_NAME_LEN
)
96 void lttng_ust_format_event_name(const struct lttng_ust_event_desc
*desc
,
99 strcpy(name
, desc
->probe_desc
->provider_name
);
101 strcat(name
, desc
->event_name
);
105 * Called with ust lock held.
107 int lttng_session_active(void)
109 struct lttng_ust_session_private
*iter
;
111 cds_list_for_each_entry(iter
, &sessions
, node
) {
112 if (iter
->pub
->active
)
119 int lttng_loglevel_match(int loglevel
,
120 unsigned int has_loglevel
,
121 enum lttng_ust_abi_loglevel_type req_type
,
125 loglevel
= TRACE_DEFAULT
;
127 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
128 if (loglevel
<= req_loglevel
129 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
133 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
134 if (loglevel
== req_loglevel
135 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
139 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
141 if (loglevel
<= TRACE_DEBUG
)
148 struct lttng_ust_session
*lttng_session_create(void)
150 struct lttng_ust_session
*session
;
151 struct lttng_ust_session_private
*session_priv
;
154 session
= zmalloc(sizeof(struct lttng_ust_session
));
157 session
->struct_size
= sizeof(struct lttng_ust_session
);
158 session_priv
= zmalloc(sizeof(struct lttng_ust_session_private
));
163 session
->priv
= session_priv
;
164 session_priv
->pub
= session
;
165 if (lttng_context_init_all(&session
->priv
->ctx
)) {
170 CDS_INIT_LIST_HEAD(&session
->priv
->chan_head
);
171 CDS_INIT_LIST_HEAD(&session
->priv
->events_head
);
172 CDS_INIT_LIST_HEAD(&session
->priv
->enums_head
);
173 CDS_INIT_LIST_HEAD(&session
->priv
->enablers_head
);
174 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
175 CDS_INIT_HLIST_HEAD(&session
->priv
->events_ht
.table
[i
]);
176 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
177 CDS_INIT_HLIST_HEAD(&session
->priv
->enums_ht
.table
[i
]);
178 cds_list_add(&session
->priv
->node
, &sessions
);
182 struct lttng_counter
*lttng_ust_counter_create(
183 const char *counter_transport_name
,
184 size_t number_dimensions
, const struct lttng_counter_dimension
*dimensions
)
186 struct lttng_counter_transport
*counter_transport
= NULL
;
187 struct lttng_counter
*counter
= NULL
;
189 counter_transport
= lttng_counter_transport_find(counter_transport_name
);
190 if (!counter_transport
)
192 counter
= zmalloc(sizeof(struct lttng_counter
));
196 counter
->ops
= &counter_transport
->ops
;
197 counter
->transport
= counter_transport
;
199 counter
->counter
= counter
->ops
->counter_create(
200 number_dimensions
, dimensions
, 0,
202 if (!counter
->counter
) {
216 void lttng_ust_counter_destroy(struct lttng_counter
*counter
)
218 counter
->ops
->counter_destroy(counter
->counter
);
222 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
224 struct lttng_event_notifier_group
*event_notifier_group
;
227 event_notifier_group
= zmalloc(sizeof(struct lttng_event_notifier_group
));
228 if (!event_notifier_group
)
231 /* Add all contexts. */
232 if (lttng_context_init_all(&event_notifier_group
->ctx
)) {
233 free(event_notifier_group
);
237 CDS_INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
238 CDS_INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
239 for (i
= 0; i
< LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
; i
++)
240 CDS_INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
242 cds_list_add(&event_notifier_group
->node
, &event_notifier_groups
);
244 return event_notifier_group
;
248 * Only used internally at session destruction.
251 void _lttng_channel_unmap(struct lttng_ust_channel_buffer
*lttng_chan
)
253 struct lttng_ust_lib_ring_buffer_channel
*chan
;
254 struct lttng_ust_shm_handle
*handle
;
256 cds_list_del(<tng_chan
->priv
->node
);
257 lttng_destroy_context(lttng_chan
->priv
->ctx
);
258 chan
= lttng_chan
->priv
->rb_chan
;
259 handle
= chan
->handle
;
260 channel_destroy(chan
, handle
, 0);
261 free(lttng_chan
->parent
);
262 free(lttng_chan
->priv
);
267 void register_event(struct lttng_ust_event_common
*event
)
270 const struct lttng_ust_event_desc
*desc
;
271 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
273 assert(event
->priv
->registered
== 0);
274 desc
= event
->priv
->desc
;
275 lttng_ust_format_event_name(desc
, name
);
276 ret
= lttng_ust_tp_probe_register_queue_release(name
,
277 desc
->probe_callback
,
278 event
, desc
->signature
);
281 event
->priv
->registered
= 1;
285 void unregister_event(struct lttng_ust_event_common
*event
)
288 const struct lttng_ust_event_desc
*desc
;
289 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
291 assert(event
->priv
->registered
== 1);
292 desc
= event
->priv
->desc
;
293 lttng_ust_format_event_name(desc
, name
);
294 ret
= lttng_ust_tp_probe_unregister_queue_release(name
,
295 desc
->probe_callback
,
299 event
->priv
->registered
= 0;
303 void _lttng_event_unregister(struct lttng_ust_event_common
*event
)
305 if (event
->priv
->registered
)
306 unregister_event(event
);
309 void lttng_session_destroy(struct lttng_ust_session
*session
)
311 struct lttng_ust_channel_buffer_private
*chan
, *tmpchan
;
312 struct lttng_ust_event_recorder_private
*event_recorder_priv
, *tmpevent_recorder_priv
;
313 struct lttng_enum
*_enum
, *tmp_enum
;
314 struct lttng_event_enabler
*event_enabler
, *event_tmpenabler
;
316 CMM_ACCESS_ONCE(session
->active
) = 0;
317 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
318 _lttng_event_unregister(event_recorder_priv
->parent
.pub
);
320 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
321 lttng_ust_tp_probe_prune_release_queue();
322 cds_list_for_each_entry_safe(event_enabler
, event_tmpenabler
,
323 &session
->priv
->enablers_head
, node
)
324 lttng_event_enabler_destroy(event_enabler
);
325 cds_list_for_each_entry_safe(event_recorder_priv
, tmpevent_recorder_priv
,
326 &session
->priv
->events_head
, node
)
327 _lttng_event_destroy(event_recorder_priv
->parent
.pub
);
328 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
329 &session
->priv
->enums_head
, node
)
330 _lttng_enum_destroy(_enum
);
331 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->priv
->chan_head
, node
)
332 _lttng_channel_unmap(chan
->pub
);
333 cds_list_del(&session
->priv
->node
);
334 lttng_destroy_context(session
->priv
->ctx
);
339 void lttng_event_notifier_group_destroy(
340 struct lttng_event_notifier_group
*event_notifier_group
)
343 struct lttng_event_notifier_enabler
*notifier_enabler
, *tmpnotifier_enabler
;
344 struct lttng_ust_event_notifier_private
*event_notifier_priv
, *tmpevent_notifier_priv
;
346 if (!event_notifier_group
) {
350 cds_list_for_each_entry(event_notifier_priv
,
351 &event_notifier_group
->event_notifiers_head
, node
)
352 _lttng_event_unregister(event_notifier_priv
->parent
.pub
);
354 lttng_ust_urcu_synchronize_rcu();
356 cds_list_for_each_entry_safe(notifier_enabler
, tmpnotifier_enabler
,
357 &event_notifier_group
->enablers_head
, node
)
358 lttng_event_notifier_enabler_destroy(notifier_enabler
);
360 cds_list_for_each_entry_safe(event_notifier_priv
, tmpevent_notifier_priv
,
361 &event_notifier_group
->event_notifiers_head
, node
)
362 _lttng_event_destroy(event_notifier_priv
->parent
.pub
);
364 if (event_notifier_group
->error_counter
)
365 lttng_ust_counter_destroy(event_notifier_group
->error_counter
);
367 /* Close the notification fd to the listener of event_notifiers. */
369 lttng_ust_lock_fd_tracker();
370 close_ret
= close(event_notifier_group
->notification_fd
);
372 lttng_ust_delete_fd_from_tracker(
373 event_notifier_group
->notification_fd
);
378 lttng_ust_unlock_fd_tracker();
380 cds_list_del(&event_notifier_group
->node
);
381 lttng_destroy_context(event_notifier_group
->ctx
);
382 free(event_notifier_group
);
386 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
388 struct lttng_ust_bytecode_node
*filter_node
, *tmp_filter_node
;
389 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
395 /* Destroy filter bytecode */
396 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
397 &enabler
->filter_bytecode_head
, node
) {
401 /* Destroy excluders */
402 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
403 &enabler
->excluder_head
, node
) {
408 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
410 if (!event_notifier_enabler
) {
414 cds_list_del(&event_notifier_enabler
->node
);
416 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
418 free(event_notifier_enabler
);
422 int lttng_enum_create(const struct lttng_ust_enum_desc
*desc
,
423 struct lttng_ust_session
*session
)
425 const char *enum_name
= desc
->name
;
426 struct lttng_enum
*_enum
;
427 struct cds_hlist_head
*head
;
429 size_t name_len
= strlen(enum_name
);
433 /* Check if this enum is already registered for this session. */
434 hash
= jhash(enum_name
, name_len
, 0);
435 head
= &session
->priv
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
437 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
443 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
444 if (notify_socket
< 0) {
449 _enum
= zmalloc(sizeof(*_enum
));
454 _enum
->session
= session
;
457 ret
= ustcomm_register_enum(notify_socket
,
464 DBG("Error (%d) registering enumeration to sessiond", ret
);
465 goto sessiond_register_error
;
467 cds_list_add(&_enum
->node
, &session
->priv
->enums_head
);
468 cds_hlist_add_head(&_enum
->hlist
, head
);
471 sessiond_register_error
:
480 int lttng_create_enum_check(const struct lttng_ust_type_common
*type
,
481 struct lttng_ust_session
*session
)
483 switch (type
->type
) {
484 case lttng_ust_type_enum
:
486 const struct lttng_ust_enum_desc
*enum_desc
;
489 enum_desc
= lttng_ust_get_type_enum(type
)->desc
;
490 ret
= lttng_enum_create(enum_desc
, session
);
491 if (ret
&& ret
!= -EEXIST
) {
492 DBG("Unable to create enum error: (%d)", ret
);
497 case lttng_ust_type_dynamic
:
499 const struct lttng_ust_event_field
*tag_field_generic
;
500 const struct lttng_ust_enum_desc
*enum_desc
;
503 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
504 enum_desc
= lttng_ust_get_type_enum(tag_field_generic
->type
)->desc
;
505 ret
= lttng_enum_create(enum_desc
, session
);
506 if (ret
&& ret
!= -EEXIST
) {
507 DBG("Unable to create enum error: (%d)", ret
);
513 /* TODO: nested types when they become supported. */
520 int lttng_create_all_event_enums(size_t nr_fields
,
521 const struct lttng_ust_event_field
**event_fields
,
522 struct lttng_ust_session
*session
)
527 /* For each field, ensure enum is part of the session. */
528 for (i
= 0; i
< nr_fields
; i
++) {
529 const struct lttng_ust_type_common
*type
= event_fields
[i
]->type
;
531 ret
= lttng_create_enum_check(type
, session
);
539 int lttng_create_all_ctx_enums(size_t nr_fields
,
540 struct lttng_ust_ctx_field
*ctx_fields
,
541 struct lttng_ust_session
*session
)
546 /* For each field, ensure enum is part of the session. */
547 for (i
= 0; i
< nr_fields
; i
++) {
548 const struct lttng_ust_type_common
*type
= ctx_fields
[i
].event_field
->type
;
550 ret
= lttng_create_enum_check(type
, session
);
558 * Ensure that a state-dump will be performed for this session at the end
559 * of the current handle_message().
561 int lttng_session_statedump(struct lttng_ust_session
*session
)
563 session
->priv
->statedump_pending
= 1;
564 lttng_ust_sockinfo_session_enabled(session
->priv
->owner
);
568 int lttng_session_enable(struct lttng_ust_session
*session
)
571 struct lttng_ust_channel_buffer_private
*chan
;
574 if (session
->active
) {
579 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
580 if (notify_socket
< 0)
581 return notify_socket
;
583 /* Set transient enabler state to "enabled" */
584 session
->priv
->tstate
= 1;
586 /* We need to sync enablers with session before activation. */
587 lttng_session_sync_event_enablers(session
);
590 * Snapshot the number of events per channel to know the type of header
593 cds_list_for_each_entry(chan
, &session
->priv
->chan_head
, node
) {
594 struct lttng_ust_ctx
*ctx
;
595 struct lttng_ust_ctx_field
*fields
= NULL
;
596 size_t nr_fields
= 0;
599 /* don't change it if session stop/restart */
600 if (chan
->header_type
)
604 nr_fields
= ctx
->nr_fields
;
605 fields
= ctx
->fields
;
606 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
609 DBG("Error (%d) adding enum to session", ret
);
613 ret
= ustcomm_register_channel(notify_socket
,
622 DBG("Error (%d) registering channel to sessiond", ret
);
625 if (chan_id
!= chan
->id
) {
626 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
632 /* Set atomically the state to "active" */
633 CMM_ACCESS_ONCE(session
->active
) = 1;
634 CMM_ACCESS_ONCE(session
->priv
->been_active
) = 1;
636 ret
= lttng_session_statedump(session
);
643 int lttng_session_disable(struct lttng_ust_session
*session
)
647 if (!session
->active
) {
651 /* Set atomically the state to "inactive" */
652 CMM_ACCESS_ONCE(session
->active
) = 0;
654 /* Set transient enabler state to "disabled" */
655 session
->priv
->tstate
= 0;
656 lttng_session_sync_event_enablers(session
);
661 int lttng_channel_enable(struct lttng_ust_channel_common
*lttng_channel
)
665 if (lttng_channel
->enabled
) {
669 /* Set transient enabler state to "enabled" */
670 lttng_channel
->priv
->tstate
= 1;
671 lttng_session_sync_event_enablers(lttng_channel
->session
);
672 /* Set atomically the state to "enabled" */
673 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 1;
678 int lttng_channel_disable(struct lttng_ust_channel_common
*lttng_channel
)
682 if (!lttng_channel
->enabled
) {
686 /* Set atomically the state to "disabled" */
687 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 0;
688 /* Set transient enabler state to "enabled" */
689 lttng_channel
->priv
->tstate
= 0;
690 lttng_session_sync_event_enablers(lttng_channel
->session
);
696 struct cds_hlist_head
*borrow_hash_table_bucket(
697 struct cds_hlist_head
*hash_table
,
698 unsigned int hash_table_size
,
699 const struct lttng_ust_event_desc
*desc
)
701 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
705 lttng_ust_format_event_name(desc
, name
);
706 name_len
= strlen(name
);
708 hash
= jhash(name
, name_len
, 0);
709 return &hash_table
[hash
& (hash_table_size
- 1)];
713 * Supports event creation while tracing session is active.
716 int lttng_event_recorder_create(const struct lttng_ust_event_desc
*desc
,
717 struct lttng_ust_channel_buffer
*chan
)
719 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
720 struct lttng_ust_event_recorder
*event_recorder
;
721 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
722 struct lttng_ust_session
*session
= chan
->parent
->session
;
723 struct cds_hlist_head
*head
;
725 int notify_socket
, loglevel
;
728 head
= borrow_hash_table_bucket(chan
->parent
->session
->priv
->events_ht
.table
,
729 LTTNG_UST_EVENT_HT_SIZE
, desc
);
731 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
732 if (notify_socket
< 0) {
737 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
740 DBG("Error (%d) adding enum to session", ret
);
741 goto create_enum_error
;
745 * Check if loglevel match. Refuse to connect event if not.
747 event_recorder
= zmalloc(sizeof(struct lttng_ust_event_recorder
));
748 if (!event_recorder
) {
752 event_recorder
->struct_size
= sizeof(struct lttng_ust_event_recorder
);
754 event_recorder
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
755 if (!event_recorder
->parent
) {
759 event_recorder
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
760 event_recorder
->parent
->type
= LTTNG_UST_EVENT_TYPE_RECORDER
;
761 event_recorder
->parent
->child
= event_recorder
;
763 event_recorder_priv
= zmalloc(sizeof(struct lttng_ust_event_recorder_private
));
764 if (!event_recorder_priv
) {
768 event_recorder
->priv
= event_recorder_priv
;
769 event_recorder_priv
->pub
= event_recorder
;
770 event_recorder
->parent
->priv
= &event_recorder_priv
->parent
;
771 event_recorder_priv
->parent
.pub
= event_recorder
->parent
;
773 event_recorder
->chan
= chan
;
775 /* Event will be enabled by enabler sync. */
776 event_recorder
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
777 event_recorder
->parent
->enabled
= 0;
778 event_recorder
->parent
->priv
->registered
= 0;
779 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->filter_bytecode_runtime_head
);
780 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->enablers_ref_head
);
781 event_recorder
->parent
->priv
->desc
= desc
;
784 loglevel
= *(*event_recorder
->parent
->priv
->desc
->loglevel
);
786 loglevel
= TRACE_DEFAULT
;
787 if (desc
->model_emf_uri
)
788 uri
= *(desc
->model_emf_uri
);
792 lttng_ust_format_event_name(desc
, name
);
794 /* Fetch event ID from sessiond */
795 ret
= ustcomm_register_event(notify_socket
,
798 chan
->priv
->parent
.objd
,
805 &event_recorder
->priv
->id
);
807 DBG("Error (%d) registering event to sessiond", ret
);
808 goto sessiond_register_error
;
811 cds_list_add(&event_recorder_priv
->node
, &chan
->parent
->session
->priv
->events_head
);
812 cds_hlist_add_head(&event_recorder_priv
->hlist
, head
);
815 sessiond_register_error
:
816 free(event_recorder_priv
);
818 free(event_recorder
->parent
);
820 free(event_recorder
);
828 int lttng_event_notifier_create(const struct lttng_ust_event_desc
*desc
,
829 uint64_t token
, uint64_t error_counter_index
,
830 struct lttng_event_notifier_group
*event_notifier_group
)
832 struct lttng_ust_event_notifier
*event_notifier
;
833 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
834 struct cds_hlist_head
*head
;
838 * Get the hashtable bucket the created lttng_event_notifier object
839 * should be inserted.
841 head
= borrow_hash_table_bucket(
842 event_notifier_group
->event_notifiers_ht
.table
,
843 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
845 event_notifier
= zmalloc(sizeof(struct lttng_ust_event_notifier
));
846 if (!event_notifier
) {
850 event_notifier
->struct_size
= sizeof(struct lttng_ust_event_notifier
);
852 event_notifier
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
853 if (!event_notifier
->parent
) {
857 event_notifier
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
858 event_notifier
->parent
->type
= LTTNG_UST_EVENT_TYPE_NOTIFIER
;
859 event_notifier
->parent
->child
= event_notifier
;
861 event_notifier_priv
= zmalloc(sizeof(struct lttng_ust_event_notifier_private
));
862 if (!event_notifier_priv
) {
866 event_notifier
->priv
= event_notifier_priv
;
867 event_notifier_priv
->pub
= event_notifier
;
868 event_notifier
->parent
->priv
= &event_notifier_priv
->parent
;
869 event_notifier_priv
->parent
.pub
= event_notifier
->parent
;
871 event_notifier_priv
->group
= event_notifier_group
;
872 event_notifier_priv
->parent
.user_token
= token
;
873 event_notifier_priv
->error_counter_index
= error_counter_index
;
875 /* Event notifier will be enabled by enabler sync. */
876 event_notifier
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
877 event_notifier
->parent
->enabled
= 0;
878 event_notifier_priv
->parent
.registered
= 0;
880 CDS_INIT_LIST_HEAD(&event_notifier
->parent
->priv
->filter_bytecode_runtime_head
);
881 CDS_INIT_LIST_HEAD(&event_notifier
->priv
->capture_bytecode_runtime_head
);
882 CDS_INIT_LIST_HEAD(&event_notifier_priv
->parent
.enablers_ref_head
);
883 event_notifier_priv
->parent
.desc
= desc
;
884 event_notifier
->notification_send
= lttng_event_notifier_notification_send
;
886 cds_list_add(&event_notifier_priv
->node
,
887 &event_notifier_group
->event_notifiers_head
);
888 cds_hlist_add_head(&event_notifier_priv
->hlist
, head
);
893 free(event_notifier
->parent
);
895 free(event_notifier
);
901 int lttng_desc_match_star_glob_enabler(const struct lttng_ust_event_desc
*desc
,
902 struct lttng_enabler
*enabler
)
904 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
906 unsigned int has_loglevel
= 0;
908 lttng_ust_format_event_name(desc
, name
);
909 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_STAR_GLOB
);
910 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
913 if (desc
->loglevel
) {
914 loglevel
= *(*desc
->loglevel
);
917 if (!lttng_loglevel_match(loglevel
,
919 enabler
->event_param
.loglevel_type
,
920 enabler
->event_param
.loglevel
))
926 int lttng_desc_match_event_enabler(const struct lttng_ust_event_desc
*desc
,
927 struct lttng_enabler
*enabler
)
929 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
931 unsigned int has_loglevel
= 0;
933 lttng_ust_format_event_name(desc
, name
);
934 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_EVENT
);
935 if (strcmp(name
, enabler
->event_param
.name
))
937 if (desc
->loglevel
) {
938 loglevel
= *(*desc
->loglevel
);
941 if (!lttng_loglevel_match(loglevel
,
943 enabler
->event_param
.loglevel_type
,
944 enabler
->event_param
.loglevel
))
950 int lttng_desc_match_enabler(const struct lttng_ust_event_desc
*desc
,
951 struct lttng_enabler
*enabler
)
953 switch (enabler
->format_type
) {
954 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
956 struct lttng_ust_excluder_node
*excluder
;
958 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
963 * If the matching event matches with an excluder,
964 * return 'does not match'
966 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
969 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
973 excluder_name
= (char *) (excluder
->excluder
.names
)
974 + count
* LTTNG_UST_ABI_SYM_NAME_LEN
;
975 len
= strnlen(excluder_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
977 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
979 lttng_ust_format_event_name(desc
, name
);
980 if (strutils_star_glob_match(excluder_name
, len
, name
, SIZE_MAX
)) {
988 case LTTNG_ENABLER_FORMAT_EVENT
:
989 return lttng_desc_match_event_enabler(desc
, enabler
);
996 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
997 struct lttng_ust_event_recorder
*event_recorder
)
999 if (lttng_desc_match_enabler(event_recorder
->parent
->priv
->desc
,
1000 lttng_event_enabler_as_enabler(event_enabler
))
1001 && event_recorder
->chan
== event_enabler
->chan
)
1008 int lttng_event_notifier_enabler_match_event_notifier(
1009 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1010 struct lttng_ust_event_notifier
*event_notifier
)
1012 int desc_matches
= lttng_desc_match_enabler(event_notifier
->priv
->parent
.desc
,
1013 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1015 if (desc_matches
&& event_notifier
->priv
->group
== event_notifier_enabler
->group
&&
1016 event_notifier
->priv
->parent
.user_token
== event_notifier_enabler
->user_token
)
1023 struct lttng_enabler_ref
*lttng_enabler_ref(
1024 struct cds_list_head
*enabler_ref_list
,
1025 struct lttng_enabler
*enabler
)
1027 struct lttng_enabler_ref
*enabler_ref
;
1029 cds_list_for_each_entry(enabler_ref
, enabler_ref_list
, node
) {
1030 if (enabler_ref
->ref
== enabler
)
1037 * Create struct lttng_event if it is missing and present in the list of
1038 * tracepoint probes.
1041 void lttng_create_event_recorder_if_missing(struct lttng_event_enabler
*event_enabler
)
1043 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1044 struct lttng_ust_registered_probe
*reg_probe
;
1045 const struct lttng_ust_event_desc
*desc
;
1046 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1048 struct cds_list_head
*probe_list
;
1050 probe_list
= lttng_get_probe_list_head();
1052 * For each probe event, if we find that a probe event matches
1053 * our enabler, create an associated lttng_event if not
1056 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1057 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1059 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1062 struct cds_hlist_head
*head
;
1063 struct cds_hlist_node
*node
;
1065 desc
= probe_desc
->event_desc
[i
];
1066 if (!lttng_desc_match_enabler(desc
,
1067 lttng_event_enabler_as_enabler(event_enabler
)))
1070 head
= borrow_hash_table_bucket(
1071 session
->priv
->events_ht
.table
,
1072 LTTNG_UST_EVENT_HT_SIZE
, desc
);
1074 cds_hlist_for_each_entry(event_recorder_priv
, node
, head
, hlist
) {
1075 if (event_recorder_priv
->parent
.desc
== desc
1076 && event_recorder_priv
->pub
->chan
== event_enabler
->chan
) {
1085 * We need to create an event for this
1088 ret
= lttng_event_recorder_create(probe_desc
->event_desc
[i
],
1089 event_enabler
->chan
);
1091 DBG("Unable to create event \"%s:%s\", error %d\n",
1092 probe_desc
->provider_name
,
1093 probe_desc
->event_desc
[i
]->event_name
, ret
);
1100 void probe_provider_event_for_each(const struct lttng_ust_probe_desc
*provider_desc
,
1101 void (*event_func
)(struct lttng_ust_event_common
*event
))
1103 struct cds_hlist_node
*node
, *tmp_node
;
1104 struct cds_list_head
*sessionsp
;
1107 /* Get handle on list of sessions. */
1108 sessionsp
= lttng_get_sessions();
1111 * Iterate over all events in the probe provider descriptions and
1112 * sessions to queue the unregistration of the events.
1114 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
1115 const struct lttng_ust_event_desc
*event_desc
;
1116 struct lttng_event_notifier_group
*event_notifier_group
;
1117 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1118 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1119 struct lttng_ust_session_private
*session_priv
;
1120 struct cds_hlist_head
*head
;
1122 event_desc
= provider_desc
->event_desc
[i
];
1125 * Iterate over all session to find the current event
1128 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
1130 * Get the list of events in the hashtable bucket and
1131 * iterate to find the event matching this descriptor.
1133 head
= borrow_hash_table_bucket(
1134 session_priv
->events_ht
.table
,
1135 LTTNG_UST_EVENT_HT_SIZE
, event_desc
);
1137 cds_hlist_for_each_entry_safe(event_recorder_priv
, node
, tmp_node
, head
, hlist
) {
1138 if (event_desc
== event_recorder_priv
->parent
.desc
) {
1139 event_func(event_recorder_priv
->parent
.pub
);
1146 * Iterate over all event_notifier groups to find the current event
1149 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1151 * Get the list of event_notifiers in the hashtable bucket and
1152 * iterate to find the event_notifier matching this
1155 head
= borrow_hash_table_bucket(
1156 event_notifier_group
->event_notifiers_ht
.table
,
1157 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, event_desc
);
1159 cds_hlist_for_each_entry_safe(event_notifier_priv
, node
, tmp_node
, head
, hlist
) {
1160 if (event_desc
== event_notifier_priv
->parent
.desc
) {
1161 event_func(event_notifier_priv
->parent
.pub
);
1170 void _event_enum_destroy(struct lttng_ust_event_common
*event
)
1173 switch (event
->type
) {
1174 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1176 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1177 struct lttng_ust_session
*session
= event_recorder
->chan
->parent
->session
;
1180 /* Destroy enums of the current event. */
1181 for (i
= 0; i
< event_recorder
->parent
->priv
->desc
->nr_fields
; i
++) {
1182 const struct lttng_ust_enum_desc
*enum_desc
;
1183 const struct lttng_ust_event_field
*field
;
1184 struct lttng_enum
*curr_enum
;
1186 field
= event_recorder
->parent
->priv
->desc
->fields
[i
];
1187 switch (field
->type
->type
) {
1188 case lttng_ust_type_enum
:
1189 enum_desc
= lttng_ust_get_type_enum(field
->type
)->desc
;
1195 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
1197 _lttng_enum_destroy(curr_enum
);
1202 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1207 /* Destroy event. */
1208 _lttng_event_destroy(event
);
1212 * Iterate over all the UST sessions to unregister and destroy all probes from
1213 * the probe provider descriptor received as argument. Must me called with the
1216 void lttng_probe_provider_unregister_events(
1217 const struct lttng_ust_probe_desc
*provider_desc
)
1220 * Iterate over all events in the probe provider descriptions and sessions
1221 * to queue the unregistration of the events.
1223 probe_provider_event_for_each(provider_desc
, _lttng_event_unregister
);
1225 /* Wait for grace period. */
1226 lttng_ust_urcu_synchronize_rcu();
1227 /* Prune the unregistration queue. */
1228 lttng_ust_tp_probe_prune_release_queue();
1231 * It is now safe to destroy the events and remove them from the event list
1234 probe_provider_event_for_each(provider_desc
, _event_enum_destroy
);
1238 * Create events associated with an event enabler (if not already present),
1239 * and add backward reference from the event to the enabler.
1242 int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler
*event_enabler
)
1244 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1245 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1247 if (!lttng_event_enabler_as_enabler(event_enabler
)->enabled
)
1250 /* First ensure that probe events are created for this enabler. */
1251 lttng_create_event_recorder_if_missing(event_enabler
);
1253 /* For each event matching enabler in session event list. */
1254 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1255 struct lttng_enabler_ref
*enabler_ref
;
1257 if (!lttng_event_enabler_match_event(event_enabler
, event_recorder_priv
->pub
))
1260 enabler_ref
= lttng_enabler_ref(&event_recorder_priv
->parent
.enablers_ref_head
,
1261 lttng_event_enabler_as_enabler(event_enabler
));
1264 * If no backward ref, create it.
1265 * Add backward ref from event to enabler.
1267 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1270 enabler_ref
->ref
= lttng_event_enabler_as_enabler(
1272 cds_list_add(&enabler_ref
->node
,
1273 &event_recorder_priv
->parent
.enablers_ref_head
);
1277 * Link filter bytecodes if not linked yet.
1279 lttng_enabler_link_bytecode(event_recorder_priv
->parent
.desc
,
1280 &session
->priv
->ctx
,
1281 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
,
1282 <tng_event_enabler_as_enabler(event_enabler
)->filter_bytecode_head
);
1284 /* TODO: merge event context. */
1291 * Called at library load: connect the probe on all enablers matching
1293 * Called with session mutex held.
1295 int lttng_fix_pending_events(void)
1297 struct lttng_ust_session_private
*session_priv
;
1299 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1300 lttng_session_lazy_sync_event_enablers(session_priv
->pub
);
1305 int lttng_fix_pending_event_notifiers(void)
1307 struct lttng_event_notifier_group
*event_notifier_group
;
1309 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1310 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1316 * For each session of the owner thread, execute pending statedump.
1317 * Only dump state for the sessions owned by the caller thread, because
1318 * we don't keep ust_lock across the entire iteration.
1320 void lttng_handle_pending_statedump(void *owner
)
1322 struct lttng_ust_session_private
*session_priv
;
1324 /* Execute state dump */
1325 do_lttng_ust_statedump(owner
);
1327 /* Clear pending state dump */
1331 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1332 if (session_priv
->owner
!= owner
)
1334 if (!session_priv
->statedump_pending
)
1336 session_priv
->statedump_pending
= 0;
1344 void _lttng_event_destroy(struct lttng_ust_event_common
*event
)
1346 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1348 lttng_free_event_filter_runtime(event
);
1349 /* Free event enabler refs */
1350 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1351 &event
->priv
->enablers_ref_head
, node
)
1354 switch (event
->type
) {
1355 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1357 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1359 /* Remove from event list. */
1360 cds_list_del(&event_recorder
->priv
->node
);
1361 /* Remove from event hash table. */
1362 cds_hlist_del(&event_recorder
->priv
->hlist
);
1364 lttng_destroy_context(event_recorder
->priv
->ctx
);
1365 free(event_recorder
->parent
);
1366 free(event_recorder
->priv
);
1367 free(event_recorder
);
1370 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1372 struct lttng_ust_event_notifier
*event_notifier
= event
->child
;
1374 /* Remove from event list. */
1375 cds_list_del(&event_notifier
->priv
->node
);
1376 /* Remove from event hash table. */
1377 cds_hlist_del(&event_notifier
->priv
->hlist
);
1379 free(event_notifier
->priv
);
1380 free(event_notifier
->parent
);
1381 free(event_notifier
);
1390 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1392 cds_list_del(&_enum
->node
);
1393 cds_hlist_del(&_enum
->hlist
);
1397 void lttng_ust_abi_events_exit(void)
1399 struct lttng_ust_session_private
*session_priv
, *tmpsession_priv
;
1401 cds_list_for_each_entry_safe(session_priv
, tmpsession_priv
, &sessions
, node
)
1402 lttng_session_destroy(session_priv
->pub
);
1406 * Enabler management.
1408 struct lttng_event_enabler
*lttng_event_enabler_create(
1409 enum lttng_enabler_format_type format_type
,
1410 struct lttng_ust_abi_event
*event_param
,
1411 struct lttng_ust_channel_buffer
*chan
)
1413 struct lttng_event_enabler
*event_enabler
;
1415 event_enabler
= zmalloc(sizeof(*event_enabler
));
1418 event_enabler
->base
.format_type
= format_type
;
1419 CDS_INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1420 CDS_INIT_LIST_HEAD(&event_enabler
->base
.excluder_head
);
1421 memcpy(&event_enabler
->base
.event_param
, event_param
,
1422 sizeof(event_enabler
->base
.event_param
));
1423 event_enabler
->chan
= chan
;
1425 event_enabler
->base
.enabled
= 0;
1426 cds_list_add(&event_enabler
->node
, &event_enabler
->chan
->parent
->session
->priv
->enablers_head
);
1427 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1429 return event_enabler
;
1432 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
1433 struct lttng_event_notifier_group
*event_notifier_group
,
1434 enum lttng_enabler_format_type format_type
,
1435 struct lttng_ust_abi_event_notifier
*event_notifier_param
)
1437 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1439 event_notifier_enabler
= zmalloc(sizeof(*event_notifier_enabler
));
1440 if (!event_notifier_enabler
)
1442 event_notifier_enabler
->base
.format_type
= format_type
;
1443 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
1444 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->capture_bytecode_head
);
1445 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.excluder_head
);
1447 event_notifier_enabler
->user_token
= event_notifier_param
->event
.token
;
1448 event_notifier_enabler
->error_counter_index
= event_notifier_param
->error_counter_index
;
1449 event_notifier_enabler
->num_captures
= 0;
1451 memcpy(&event_notifier_enabler
->base
.event_param
.name
,
1452 event_notifier_param
->event
.name
,
1453 sizeof(event_notifier_enabler
->base
.event_param
.name
));
1454 event_notifier_enabler
->base
.event_param
.instrumentation
=
1455 event_notifier_param
->event
.instrumentation
;
1456 event_notifier_enabler
->base
.event_param
.loglevel
=
1457 event_notifier_param
->event
.loglevel
;
1458 event_notifier_enabler
->base
.event_param
.loglevel_type
=
1459 event_notifier_param
->event
.loglevel_type
;
1461 event_notifier_enabler
->base
.enabled
= 0;
1462 event_notifier_enabler
->group
= event_notifier_group
;
1464 cds_list_add(&event_notifier_enabler
->node
,
1465 &event_notifier_group
->enablers_head
);
1467 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1469 return event_notifier_enabler
;
1472 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1474 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1475 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1480 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1482 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1483 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1489 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
1490 struct lttng_ust_bytecode_node
**bytecode
)
1492 (*bytecode
)->enabler
= enabler
;
1493 cds_list_add_tail(&(*bytecode
)->node
, &enabler
->filter_bytecode_head
);
1494 /* Take ownership of bytecode */
1498 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
1499 struct lttng_ust_bytecode_node
**bytecode
)
1501 _lttng_enabler_attach_filter_bytecode(
1502 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1504 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1509 void _lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1510 struct lttng_ust_excluder_node
**excluder
)
1512 (*excluder
)->enabler
= enabler
;
1513 cds_list_add_tail(&(*excluder
)->node
, &enabler
->excluder_head
);
1514 /* Take ownership of excluder */
1518 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler
*event_enabler
,
1519 struct lttng_ust_excluder_node
**excluder
)
1521 _lttng_enabler_attach_exclusion(
1522 lttng_event_enabler_as_enabler(event_enabler
), excluder
);
1524 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1528 int lttng_event_notifier_enabler_enable(
1529 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1531 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
1532 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1537 int lttng_event_notifier_enabler_disable(
1538 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1540 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
1541 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1546 int lttng_event_notifier_enabler_attach_filter_bytecode(
1547 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1548 struct lttng_ust_bytecode_node
**bytecode
)
1550 _lttng_enabler_attach_filter_bytecode(
1551 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1554 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1558 int lttng_event_notifier_enabler_attach_capture_bytecode(
1559 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1560 struct lttng_ust_bytecode_node
**bytecode
)
1562 (*bytecode
)->enabler
= lttng_event_notifier_enabler_as_enabler(
1563 event_notifier_enabler
);
1564 cds_list_add_tail(&(*bytecode
)->node
,
1565 &event_notifier_enabler
->capture_bytecode_head
);
1566 /* Take ownership of bytecode */
1568 event_notifier_enabler
->num_captures
++;
1570 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1574 int lttng_event_notifier_enabler_attach_exclusion(
1575 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1576 struct lttng_ust_excluder_node
**excluder
)
1578 _lttng_enabler_attach_exclusion(
1579 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1582 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1586 int lttng_attach_context(struct lttng_ust_abi_context
*context_param
,
1587 union lttng_ust_abi_args
*uargs
,
1588 struct lttng_ust_ctx
**ctx
, struct lttng_ust_session
*session
)
1591 * We cannot attach a context after trace has been started for a
1592 * session because the metadata does not allow expressing this
1593 * information outside of the original channel scope.
1595 if (session
->priv
->been_active
)
1598 switch (context_param
->ctx
) {
1599 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
1600 return lttng_add_pthread_id_to_ctx(ctx
);
1601 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1603 struct lttng_ust_abi_perf_counter_ctx
*perf_ctx_param
;
1605 perf_ctx_param
= &context_param
->u
.perf_counter
;
1606 return lttng_add_perf_counter_to_ctx(
1607 perf_ctx_param
->type
,
1608 perf_ctx_param
->config
,
1609 perf_ctx_param
->name
,
1612 case LTTNG_UST_ABI_CONTEXT_VTID
:
1613 return lttng_add_vtid_to_ctx(ctx
);
1614 case LTTNG_UST_ABI_CONTEXT_VPID
:
1615 return lttng_add_vpid_to_ctx(ctx
);
1616 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
1617 return lttng_add_procname_to_ctx(ctx
);
1618 case LTTNG_UST_ABI_CONTEXT_IP
:
1619 return lttng_add_ip_to_ctx(ctx
);
1620 case LTTNG_UST_ABI_CONTEXT_CPU_ID
:
1621 return lttng_add_cpu_id_to_ctx(ctx
);
1622 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1623 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1625 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
1626 return lttng_add_cgroup_ns_to_ctx(ctx
);
1627 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
1628 return lttng_add_ipc_ns_to_ctx(ctx
);
1629 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
1630 return lttng_add_mnt_ns_to_ctx(ctx
);
1631 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
1632 return lttng_add_net_ns_to_ctx(ctx
);
1633 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
1634 return lttng_add_pid_ns_to_ctx(ctx
);
1635 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
1636 return lttng_add_time_ns_to_ctx(ctx
);
1637 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
1638 return lttng_add_user_ns_to_ctx(ctx
);
1639 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
1640 return lttng_add_uts_ns_to_ctx(ctx
);
1641 case LTTNG_UST_ABI_CONTEXT_VUID
:
1642 return lttng_add_vuid_to_ctx(ctx
);
1643 case LTTNG_UST_ABI_CONTEXT_VEUID
:
1644 return lttng_add_veuid_to_ctx(ctx
);
1645 case LTTNG_UST_ABI_CONTEXT_VSUID
:
1646 return lttng_add_vsuid_to_ctx(ctx
);
1647 case LTTNG_UST_ABI_CONTEXT_VGID
:
1648 return lttng_add_vgid_to_ctx(ctx
);
1649 case LTTNG_UST_ABI_CONTEXT_VEGID
:
1650 return lttng_add_vegid_to_ctx(ctx
);
1651 case LTTNG_UST_ABI_CONTEXT_VSGID
:
1652 return lttng_add_vsgid_to_ctx(ctx
);
1658 int lttng_event_enabler_attach_context(
1659 struct lttng_event_enabler
*enabler
__attribute__((unused
)),
1660 struct lttng_ust_abi_context
*context_param
__attribute__((unused
)))
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_ust_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_event_recorders(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_ust_bytecode_runtime
*runtime
;
1698 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1702 cds_list_for_each_entry(enabler_ref
,
1703 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1704 if (enabler_ref
->ref
->enabled
) {
1710 * Enabled state is based on union of enablers, with
1711 * intesection of session and channel transient enable
1714 enabled
= enabled
&& session
->priv
->tstate
&& event_recorder_priv
->pub
->chan
->priv
->parent
.tstate
;
1716 CMM_STORE_SHARED(event_recorder_priv
->pub
->parent
->enabled
, enabled
);
1718 * Sync tracepoint registration with event enabled
1722 if (!event_recorder_priv
->parent
.registered
)
1723 register_event(event_recorder_priv
->parent
.pub
);
1725 if (event_recorder_priv
->parent
.registered
)
1726 unregister_event(event_recorder_priv
->parent
.pub
);
1729 /* Check if has enablers without bytecode enabled */
1730 cds_list_for_each_entry(enabler_ref
,
1731 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1732 if (enabler_ref
->ref
->enabled
1733 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1734 has_enablers_without_filter_bytecode
= 1;
1738 event_recorder_priv
->parent
.has_enablers_without_filter_bytecode
=
1739 has_enablers_without_filter_bytecode
;
1741 /* Enable filters */
1742 cds_list_for_each_entry(runtime
,
1743 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1744 lttng_bytecode_sync_state(runtime
);
1747 CMM_STORE_SHARED(event_recorder_priv
->parent
.pub
->eval_filter
,
1748 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1750 lttng_ust_tp_probe_prune_release_queue();
1753 /* Support for event notifier is introduced by probe provider major version 2. */
1755 bool lttng_ust_probe_supports_event_notifier(const struct lttng_ust_probe_desc
*probe_desc
)
1757 return probe_desc
->major
>= 2;
1761 void lttng_create_event_notifier_if_missing(
1762 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1764 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1765 struct lttng_ust_registered_probe
*reg_probe
;
1766 struct cds_list_head
*probe_list
;
1769 probe_list
= lttng_get_probe_list_head();
1771 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1772 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1774 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1777 const struct lttng_ust_event_desc
*desc
;
1778 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1779 struct cds_hlist_head
*head
;
1780 struct cds_hlist_node
*node
;
1782 desc
= probe_desc
->event_desc
[i
];
1784 if (!lttng_desc_match_enabler(desc
,
1785 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1789 * Given the current event_notifier group, get the bucket that
1790 * the target event_notifier would be if it was already
1793 head
= borrow_hash_table_bucket(
1794 event_notifier_group
->event_notifiers_ht
.table
,
1795 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
1797 cds_hlist_for_each_entry(event_notifier_priv
, node
, head
, hlist
) {
1799 * Check if event_notifier already exists by checking
1800 * if the event_notifier and enabler share the same
1801 * description and id.
1803 if (event_notifier_priv
->parent
.desc
== desc
&&
1804 event_notifier_priv
->parent
.user_token
== event_notifier_enabler
->user_token
) {
1813 /* Check that the probe supports event notifiers, else report the error. */
1814 if (!lttng_ust_probe_supports_event_notifier(probe_desc
)) {
1815 ERR("Probe \"%s\" contains event \"%s:%s\" which matches an enabled event notifier, "
1816 "but its version (%u.%u) is too old and does not implement event notifiers. "
1817 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1818 "this event will not generate any notification.",
1819 probe_desc
->provider_name
,
1820 probe_desc
->provider_name
, desc
->event_name
,
1826 * We need to create a event_notifier for this event probe.
1828 ret
= lttng_event_notifier_create(desc
,
1829 event_notifier_enabler
->user_token
,
1830 event_notifier_enabler
->error_counter_index
,
1831 event_notifier_group
);
1833 DBG("Unable to create event_notifier \"%s:%s\", error %d\n",
1834 probe_desc
->provider_name
,
1835 probe_desc
->event_desc
[i
]->event_name
, ret
);
1842 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1845 int lttng_event_notifier_enabler_ref_event_notifiers(
1846 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1848 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1849 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1852 * Only try to create event_notifiers for enablers that are enabled, the user
1853 * might still be attaching filter or exclusion to the
1854 * event_notifier_enabler.
1856 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
)
1859 /* First, ensure that probe event_notifiers are created for this enabler. */
1860 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
1862 /* Link the created event_notifier with its associated enabler. */
1863 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1864 struct lttng_enabler_ref
*enabler_ref
;
1866 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier_priv
->pub
))
1869 enabler_ref
= lttng_enabler_ref(&event_notifier_priv
->parent
.enablers_ref_head
,
1870 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1873 * If no backward ref, create it.
1874 * Add backward ref from event_notifier to enabler.
1876 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1880 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
1881 event_notifier_enabler
);
1882 cds_list_add(&enabler_ref
->node
,
1883 &event_notifier_priv
->parent
.enablers_ref_head
);
1887 * Link filter bytecodes if not linked yet.
1889 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1890 &event_notifier_group
->ctx
,
1891 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
,
1892 <tng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->filter_bytecode_head
);
1895 * Link capture bytecodes if not linked yet.
1897 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1898 &event_notifier_group
->ctx
, &event_notifier_priv
->capture_bytecode_runtime_head
,
1899 &event_notifier_enabler
->capture_bytecode_head
);
1901 event_notifier_priv
->num_captures
= event_notifier_enabler
->num_captures
;
1908 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
1910 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1911 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1913 cds_list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
1914 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
1917 * For each event_notifier, if at least one of its enablers is enabled,
1918 * we enable the event_notifier, else we disable it.
1920 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1921 struct lttng_enabler_ref
*enabler_ref
;
1922 struct lttng_ust_bytecode_runtime
*runtime
;
1923 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1924 int nr_filters
= 0, nr_captures
= 0;
1926 /* Enable event_notifiers */
1927 cds_list_for_each_entry(enabler_ref
,
1928 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1929 if (enabler_ref
->ref
->enabled
) {
1935 CMM_STORE_SHARED(event_notifier_priv
->pub
->parent
->enabled
, enabled
);
1937 * Sync tracepoint registration with event_notifier enabled
1941 if (!event_notifier_priv
->parent
.registered
)
1942 register_event(event_notifier_priv
->parent
.pub
);
1944 if (event_notifier_priv
->parent
.registered
)
1945 unregister_event(event_notifier_priv
->parent
.pub
);
1948 /* Check if has enablers without bytecode enabled */
1949 cds_list_for_each_entry(enabler_ref
,
1950 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1951 if (enabler_ref
->ref
->enabled
1952 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1953 has_enablers_without_filter_bytecode
= 1;
1957 event_notifier_priv
->parent
.has_enablers_without_filter_bytecode
=
1958 has_enablers_without_filter_bytecode
;
1960 /* Enable filters */
1961 cds_list_for_each_entry(runtime
,
1962 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1963 lttng_bytecode_sync_state(runtime
);
1966 CMM_STORE_SHARED(event_notifier_priv
->parent
.pub
->eval_filter
,
1967 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1969 /* Enable captures. */
1970 cds_list_for_each_entry(runtime
,
1971 &event_notifier_priv
->capture_bytecode_runtime_head
, node
) {
1972 lttng_bytecode_sync_state(runtime
);
1975 CMM_STORE_SHARED(event_notifier_priv
->pub
->eval_capture
,
1978 lttng_ust_tp_probe_prune_release_queue();
1982 * Apply enablers to session events, adding events to session if need
1983 * be. It is required after each modification applied to an active
1984 * session, and right before session "start".
1985 * "lazy" sync means we only sync if required.
1988 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
)
1990 /* We can skip if session is not active */
1991 if (!session
->active
)
1993 lttng_session_sync_event_enablers(session
);
1997 * Update all sessions with the given app context.
1998 * Called with ust lock held.
1999 * This is invoked when an application context gets loaded/unloaded. It
2000 * ensures the context callbacks are in sync with the application
2001 * context (either app context callbacks, or dummy callbacks).
2003 void lttng_ust_context_set_session_provider(const char *name
,
2004 size_t (*get_size
)(void *priv
, size_t offset
),
2005 void (*record
)(void *priv
, struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2006 struct lttng_ust_channel_buffer
*chan
),
2007 void (*get_value
)(void *priv
, struct lttng_ust_ctx_value
*value
),
2010 struct lttng_ust_session_private
*session_priv
;
2012 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
2013 struct lttng_ust_channel_buffer_private
*chan
;
2014 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
2017 ret
= lttng_ust_context_set_provider_rcu(&session_priv
->ctx
,
2018 name
, get_size
, record
, get_value
, priv
);
2021 cds_list_for_each_entry(chan
, &session_priv
->chan_head
, node
) {
2022 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
2023 name
, get_size
, record
, get_value
, priv
);
2027 cds_list_for_each_entry(event_recorder_priv
, &session_priv
->events_head
, node
) {
2028 ret
= lttng_ust_context_set_provider_rcu(&event_recorder_priv
->ctx
,
2029 name
, get_size
, record
, get_value
, priv
);
2037 * Update all event_notifier groups with the given app context.
2038 * Called with ust lock held.
2039 * This is invoked when an application context gets loaded/unloaded. It
2040 * ensures the context callbacks are in sync with the application
2041 * context (either app context callbacks, or dummy callbacks).
2043 void lttng_ust_context_set_event_notifier_group_provider(const char *name
,
2044 size_t (*get_size
)(void *priv
, size_t offset
),
2045 void (*record
)(void *priv
, struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2046 struct lttng_ust_channel_buffer
*chan
),
2047 void (*get_value
)(void *priv
, struct lttng_ust_ctx_value
*value
),
2050 struct lttng_event_notifier_group
*event_notifier_group
;
2052 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2055 ret
= lttng_ust_context_set_provider_rcu(
2056 &event_notifier_group
->ctx
,
2057 name
, get_size
, record
, get_value
, priv
);
2063 int lttng_ust_session_uuid_validate(struct lttng_ust_session
*session
,
2064 unsigned char *uuid
)
2068 /* Compare UUID with session. */
2069 if (session
->priv
->uuid_set
) {
2070 if (memcmp(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
)) {
2074 memcpy(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
2075 session
->priv
->uuid_set
= true;