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"
42 #include "lttng-ust-uuid.h"
44 #include "common/tracepoint.h"
45 #include "common/strutils.h"
46 #include "lttng-bytecode.h"
47 #include "lttng-tracer.h"
48 #include "lttng-tracer-core.h"
49 #include "lttng-ust-statedump.h"
50 #include "context-internal.h"
51 #include "lib/lttng-ust/events.h"
52 #include "common/ringbuffer/shm.h"
53 #include "common/ringbuffer/frontend_types.h"
54 #include "common/ringbuffer/frontend.h"
55 #include "common/counter/counter.h"
56 #include "common/jhash.h"
57 #include <lttng/ust-abi.h>
58 #include "context-provider-internal.h"
61 * All operations within this file are called by the communication
62 * thread, under ust_lock protection.
65 static CDS_LIST_HEAD(sessions
);
66 static CDS_LIST_HEAD(event_notifier_groups
);
68 struct cds_list_head
*lttng_get_sessions(void)
73 static void _lttng_event_destroy(struct lttng_ust_event_common
*event
);
74 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
77 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
);
79 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
);
81 void lttng_event_notifier_group_sync_enablers(
82 struct lttng_event_notifier_group
*event_notifier_group
);
84 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
86 bool lttng_ust_validate_event_name(const struct lttng_ust_event_desc
*desc
)
88 if (strlen(desc
->probe_desc
->provider_name
) + 1 +
89 strlen(desc
->event_name
) >= LTTNG_UST_ABI_SYM_NAME_LEN
)
94 void lttng_ust_format_event_name(const struct lttng_ust_event_desc
*desc
,
97 strcpy(name
, desc
->probe_desc
->provider_name
);
99 strcat(name
, desc
->event_name
);
103 * Called with ust lock held.
105 int lttng_session_active(void)
107 struct lttng_ust_session_private
*iter
;
109 cds_list_for_each_entry(iter
, &sessions
, node
) {
110 if (iter
->pub
->active
)
117 int lttng_loglevel_match(int loglevel
,
118 unsigned int has_loglevel
,
119 enum lttng_ust_abi_loglevel_type req_type
,
123 loglevel
= TRACE_DEFAULT
;
125 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
126 if (loglevel
<= req_loglevel
127 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
131 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
132 if (loglevel
== req_loglevel
133 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
137 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
139 if (loglevel
<= TRACE_DEBUG
)
146 struct lttng_ust_session
*lttng_session_create(void)
148 struct lttng_ust_session
*session
;
149 struct lttng_ust_session_private
*session_priv
;
152 session
= zmalloc(sizeof(struct lttng_ust_session
));
155 session
->struct_size
= sizeof(struct lttng_ust_session
);
156 session_priv
= zmalloc(sizeof(struct lttng_ust_session_private
));
161 session
->priv
= session_priv
;
162 session_priv
->pub
= session
;
163 if (lttng_context_init_all(&session
->priv
->ctx
)) {
168 CDS_INIT_LIST_HEAD(&session
->priv
->chan_head
);
169 CDS_INIT_LIST_HEAD(&session
->priv
->events_head
);
170 CDS_INIT_LIST_HEAD(&session
->priv
->enums_head
);
171 CDS_INIT_LIST_HEAD(&session
->priv
->enablers_head
);
172 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
173 CDS_INIT_HLIST_HEAD(&session
->priv
->events_ht
.table
[i
]);
174 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
175 CDS_INIT_HLIST_HEAD(&session
->priv
->enums_ht
.table
[i
]);
176 cds_list_add(&session
->priv
->node
, &sessions
);
180 struct lttng_counter
*lttng_ust_counter_create(
181 const char *counter_transport_name
,
182 size_t number_dimensions
, const struct lttng_counter_dimension
*dimensions
)
184 struct lttng_counter_transport
*counter_transport
= NULL
;
185 struct lttng_counter
*counter
= NULL
;
187 counter_transport
= lttng_counter_transport_find(counter_transport_name
);
188 if (!counter_transport
)
190 counter
= zmalloc(sizeof(struct lttng_counter
));
194 counter
->ops
= &counter_transport
->ops
;
195 counter
->transport
= counter_transport
;
197 counter
->counter
= counter
->ops
->counter_create(
198 number_dimensions
, dimensions
, 0,
200 if (!counter
->counter
) {
214 void lttng_ust_counter_destroy(struct lttng_counter
*counter
)
216 counter
->ops
->counter_destroy(counter
->counter
);
220 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
222 struct lttng_event_notifier_group
*event_notifier_group
;
225 event_notifier_group
= zmalloc(sizeof(struct lttng_event_notifier_group
));
226 if (!event_notifier_group
)
229 /* Add all contexts. */
230 if (lttng_context_init_all(&event_notifier_group
->ctx
)) {
231 free(event_notifier_group
);
235 CDS_INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
236 CDS_INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
237 for (i
= 0; i
< LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
; i
++)
238 CDS_INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
240 cds_list_add(&event_notifier_group
->node
, &event_notifier_groups
);
242 return event_notifier_group
;
246 * Only used internally at session destruction.
249 void _lttng_channel_unmap(struct lttng_ust_channel_buffer
*lttng_chan
)
251 struct lttng_ust_ring_buffer_channel
*chan
;
252 struct lttng_ust_shm_handle
*handle
;
254 cds_list_del(<tng_chan
->priv
->node
);
255 lttng_destroy_context(lttng_chan
->priv
->ctx
);
256 chan
= lttng_chan
->priv
->rb_chan
;
257 handle
= chan
->handle
;
258 channel_destroy(chan
, handle
, 0);
259 free(lttng_chan
->parent
);
260 free(lttng_chan
->priv
);
265 void register_event(struct lttng_ust_event_common
*event
)
268 const struct lttng_ust_event_desc
*desc
;
270 assert(event
->priv
->registered
== 0);
271 desc
= event
->priv
->desc
;
272 ret
= lttng_ust_tp_probe_register_queue_release(desc
->probe_desc
->provider_name
,
274 desc
->probe_callback
,
275 event
, desc
->signature
);
278 event
->priv
->registered
= 1;
282 void unregister_event(struct lttng_ust_event_common
*event
)
285 const struct lttng_ust_event_desc
*desc
;
287 assert(event
->priv
->registered
== 1);
288 desc
= event
->priv
->desc
;
289 ret
= lttng_ust_tp_probe_unregister_queue_release(desc
->probe_desc
->provider_name
,
291 desc
->probe_callback
,
295 event
->priv
->registered
= 0;
299 void _lttng_event_unregister(struct lttng_ust_event_common
*event
)
301 if (event
->priv
->registered
)
302 unregister_event(event
);
305 void lttng_session_destroy(struct lttng_ust_session
*session
)
307 struct lttng_ust_channel_buffer_private
*chan
, *tmpchan
;
308 struct lttng_ust_event_recorder_private
*event_recorder_priv
, *tmpevent_recorder_priv
;
309 struct lttng_enum
*_enum
, *tmp_enum
;
310 struct lttng_event_enabler
*event_enabler
, *event_tmpenabler
;
312 CMM_ACCESS_ONCE(session
->active
) = 0;
313 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
314 _lttng_event_unregister(event_recorder_priv
->parent
.pub
);
316 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
317 lttng_ust_tp_probe_prune_release_queue();
318 cds_list_for_each_entry_safe(event_enabler
, event_tmpenabler
,
319 &session
->priv
->enablers_head
, node
)
320 lttng_event_enabler_destroy(event_enabler
);
321 cds_list_for_each_entry_safe(event_recorder_priv
, tmpevent_recorder_priv
,
322 &session
->priv
->events_head
, node
)
323 _lttng_event_destroy(event_recorder_priv
->parent
.pub
);
324 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
325 &session
->priv
->enums_head
, node
)
326 _lttng_enum_destroy(_enum
);
327 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->priv
->chan_head
, node
)
328 _lttng_channel_unmap(chan
->pub
);
329 cds_list_del(&session
->priv
->node
);
330 lttng_destroy_context(session
->priv
->ctx
);
335 void lttng_event_notifier_group_destroy(
336 struct lttng_event_notifier_group
*event_notifier_group
)
339 struct lttng_event_notifier_enabler
*notifier_enabler
, *tmpnotifier_enabler
;
340 struct lttng_ust_event_notifier_private
*event_notifier_priv
, *tmpevent_notifier_priv
;
342 if (!event_notifier_group
) {
346 cds_list_for_each_entry(event_notifier_priv
,
347 &event_notifier_group
->event_notifiers_head
, node
)
348 _lttng_event_unregister(event_notifier_priv
->parent
.pub
);
350 lttng_ust_urcu_synchronize_rcu();
352 cds_list_for_each_entry_safe(notifier_enabler
, tmpnotifier_enabler
,
353 &event_notifier_group
->enablers_head
, node
)
354 lttng_event_notifier_enabler_destroy(notifier_enabler
);
356 cds_list_for_each_entry_safe(event_notifier_priv
, tmpevent_notifier_priv
,
357 &event_notifier_group
->event_notifiers_head
, node
)
358 _lttng_event_destroy(event_notifier_priv
->parent
.pub
);
360 if (event_notifier_group
->error_counter
)
361 lttng_ust_counter_destroy(event_notifier_group
->error_counter
);
363 /* Close the notification fd to the listener of event_notifiers. */
365 lttng_ust_lock_fd_tracker();
366 close_ret
= close(event_notifier_group
->notification_fd
);
368 lttng_ust_delete_fd_from_tracker(
369 event_notifier_group
->notification_fd
);
374 lttng_ust_unlock_fd_tracker();
376 cds_list_del(&event_notifier_group
->node
);
377 lttng_destroy_context(event_notifier_group
->ctx
);
378 free(event_notifier_group
);
382 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
384 struct lttng_ust_bytecode_node
*filter_node
, *tmp_filter_node
;
385 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
391 /* Destroy filter bytecode */
392 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
393 &enabler
->filter_bytecode_head
, node
) {
397 /* Destroy excluders */
398 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
399 &enabler
->excluder_head
, node
) {
404 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
406 if (!event_notifier_enabler
) {
410 cds_list_del(&event_notifier_enabler
->node
);
412 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
414 free(event_notifier_enabler
);
418 int lttng_enum_create(const struct lttng_ust_enum_desc
*desc
,
419 struct lttng_ust_session
*session
)
421 const char *enum_name
= desc
->name
;
422 struct lttng_enum
*_enum
;
423 struct cds_hlist_head
*head
;
425 size_t name_len
= strlen(enum_name
);
429 /* Check if this enum is already registered for this session. */
430 hash
= jhash(enum_name
, name_len
, 0);
431 head
= &session
->priv
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
433 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
439 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
440 if (notify_socket
< 0) {
445 _enum
= zmalloc(sizeof(*_enum
));
450 _enum
->session
= session
;
453 ret
= ustcomm_register_enum(notify_socket
,
460 DBG("Error (%d) registering enumeration to sessiond", ret
);
461 goto sessiond_register_error
;
463 cds_list_add(&_enum
->node
, &session
->priv
->enums_head
);
464 cds_hlist_add_head(&_enum
->hlist
, head
);
467 sessiond_register_error
:
476 int lttng_create_enum_check(const struct lttng_ust_type_common
*type
,
477 struct lttng_ust_session
*session
)
479 switch (type
->type
) {
480 case lttng_ust_type_enum
:
482 const struct lttng_ust_enum_desc
*enum_desc
;
485 enum_desc
= lttng_ust_get_type_enum(type
)->desc
;
486 ret
= lttng_enum_create(enum_desc
, session
);
487 if (ret
&& ret
!= -EEXIST
) {
488 DBG("Unable to create enum error: (%d)", ret
);
493 case lttng_ust_type_dynamic
:
495 const struct lttng_ust_event_field
*tag_field_generic
;
496 const struct lttng_ust_enum_desc
*enum_desc
;
499 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
500 enum_desc
= lttng_ust_get_type_enum(tag_field_generic
->type
)->desc
;
501 ret
= lttng_enum_create(enum_desc
, session
);
502 if (ret
&& ret
!= -EEXIST
) {
503 DBG("Unable to create enum error: (%d)", ret
);
509 /* TODO: nested types when they become supported. */
516 int lttng_create_all_event_enums(size_t nr_fields
,
517 const struct lttng_ust_event_field
* const *event_fields
,
518 struct lttng_ust_session
*session
)
523 /* For each field, ensure enum is part of the session. */
524 for (i
= 0; i
< nr_fields
; i
++) {
525 const struct lttng_ust_type_common
*type
= event_fields
[i
]->type
;
527 ret
= lttng_create_enum_check(type
, session
);
535 int lttng_create_all_ctx_enums(size_t nr_fields
,
536 struct lttng_ust_ctx_field
*ctx_fields
,
537 struct lttng_ust_session
*session
)
542 /* For each field, ensure enum is part of the session. */
543 for (i
= 0; i
< nr_fields
; i
++) {
544 const struct lttng_ust_type_common
*type
= ctx_fields
[i
].event_field
->type
;
546 ret
= lttng_create_enum_check(type
, session
);
554 * Ensure that a state-dump will be performed for this session at the end
555 * of the current handle_message().
557 int lttng_session_statedump(struct lttng_ust_session
*session
)
559 session
->priv
->statedump_pending
= 1;
560 lttng_ust_sockinfo_session_enabled(session
->priv
->owner
);
564 int lttng_session_enable(struct lttng_ust_session
*session
)
567 struct lttng_ust_channel_buffer_private
*chan
;
570 if (session
->active
) {
575 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
576 if (notify_socket
< 0)
577 return notify_socket
;
579 /* Set transient enabler state to "enabled" */
580 session
->priv
->tstate
= 1;
582 /* We need to sync enablers with session before activation. */
583 lttng_session_sync_event_enablers(session
);
586 * Snapshot the number of events per channel to know the type of header
589 cds_list_for_each_entry(chan
, &session
->priv
->chan_head
, node
) {
590 struct lttng_ust_ctx
*ctx
;
591 struct lttng_ust_ctx_field
*fields
= NULL
;
592 size_t nr_fields
= 0;
595 /* don't change it if session stop/restart */
596 if (chan
->header_type
)
600 nr_fields
= ctx
->nr_fields
;
601 fields
= ctx
->fields
;
602 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
605 DBG("Error (%d) adding enum to session", ret
);
609 ret
= ustcomm_register_channel(notify_socket
,
618 DBG("Error (%d) registering channel to sessiond", ret
);
621 if (chan_id
!= chan
->id
) {
622 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
628 /* Set atomically the state to "active" */
629 CMM_ACCESS_ONCE(session
->active
) = 1;
630 CMM_ACCESS_ONCE(session
->priv
->been_active
) = 1;
632 ret
= lttng_session_statedump(session
);
639 int lttng_session_disable(struct lttng_ust_session
*session
)
643 if (!session
->active
) {
647 /* Set atomically the state to "inactive" */
648 CMM_ACCESS_ONCE(session
->active
) = 0;
650 /* Set transient enabler state to "disabled" */
651 session
->priv
->tstate
= 0;
652 lttng_session_sync_event_enablers(session
);
657 int lttng_channel_enable(struct lttng_ust_channel_common
*lttng_channel
)
661 if (lttng_channel
->enabled
) {
665 /* Set transient enabler state to "enabled" */
666 lttng_channel
->priv
->tstate
= 1;
667 lttng_session_sync_event_enablers(lttng_channel
->session
);
668 /* Set atomically the state to "enabled" */
669 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 1;
674 int lttng_channel_disable(struct lttng_ust_channel_common
*lttng_channel
)
678 if (!lttng_channel
->enabled
) {
682 /* Set atomically the state to "disabled" */
683 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 0;
684 /* Set transient enabler state to "enabled" */
685 lttng_channel
->priv
->tstate
= 0;
686 lttng_session_sync_event_enablers(lttng_channel
->session
);
692 struct cds_hlist_head
*borrow_hash_table_bucket(
693 struct cds_hlist_head
*hash_table
,
694 unsigned int hash_table_size
,
695 const struct lttng_ust_event_desc
*desc
)
697 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
701 lttng_ust_format_event_name(desc
, name
);
702 name_len
= strlen(name
);
704 hash
= jhash(name
, name_len
, 0);
705 return &hash_table
[hash
& (hash_table_size
- 1)];
709 * Supports event creation while tracing session is active.
712 int lttng_event_recorder_create(const struct lttng_ust_event_desc
*desc
,
713 struct lttng_ust_channel_buffer
*chan
)
715 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
716 struct lttng_ust_event_recorder
*event_recorder
;
717 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
718 struct lttng_ust_session
*session
= chan
->parent
->session
;
719 struct cds_hlist_head
*head
;
721 int notify_socket
, loglevel
;
724 head
= borrow_hash_table_bucket(chan
->parent
->session
->priv
->events_ht
.table
,
725 LTTNG_UST_EVENT_HT_SIZE
, desc
);
727 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
728 if (notify_socket
< 0) {
733 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
736 DBG("Error (%d) adding enum to session", ret
);
737 goto create_enum_error
;
741 * Check if loglevel match. Refuse to connect event if not.
743 event_recorder
= zmalloc(sizeof(struct lttng_ust_event_recorder
));
744 if (!event_recorder
) {
748 event_recorder
->struct_size
= sizeof(struct lttng_ust_event_recorder
);
750 event_recorder
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
751 if (!event_recorder
->parent
) {
755 event_recorder
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
756 event_recorder
->parent
->type
= LTTNG_UST_EVENT_TYPE_RECORDER
;
757 event_recorder
->parent
->child
= event_recorder
;
759 event_recorder_priv
= zmalloc(sizeof(struct lttng_ust_event_recorder_private
));
760 if (!event_recorder_priv
) {
764 event_recorder
->priv
= event_recorder_priv
;
765 event_recorder_priv
->pub
= event_recorder
;
766 event_recorder
->parent
->priv
= &event_recorder_priv
->parent
;
767 event_recorder_priv
->parent
.pub
= event_recorder
->parent
;
769 event_recorder
->chan
= chan
;
771 /* Event will be enabled by enabler sync. */
772 event_recorder
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
773 event_recorder
->parent
->enabled
= 0;
774 event_recorder
->parent
->priv
->registered
= 0;
775 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->filter_bytecode_runtime_head
);
776 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->enablers_ref_head
);
777 event_recorder
->parent
->priv
->desc
= desc
;
780 loglevel
= *(*desc
->loglevel
);
782 loglevel
= TRACE_DEFAULT
;
783 if (desc
->model_emf_uri
)
784 uri
= *(desc
->model_emf_uri
);
788 lttng_ust_format_event_name(desc
, name
);
790 /* Fetch event ID from sessiond */
791 ret
= ustcomm_register_event(notify_socket
,
794 chan
->priv
->parent
.objd
,
801 &event_recorder
->priv
->id
);
803 DBG("Error (%d) registering event to sessiond", ret
);
804 goto sessiond_register_error
;
807 cds_list_add(&event_recorder_priv
->node
, &chan
->parent
->session
->priv
->events_head
);
808 cds_hlist_add_head(&event_recorder_priv
->hlist
, head
);
811 sessiond_register_error
:
812 free(event_recorder_priv
);
814 free(event_recorder
->parent
);
816 free(event_recorder
);
824 int lttng_event_notifier_create(const struct lttng_ust_event_desc
*desc
,
825 uint64_t token
, uint64_t error_counter_index
,
826 struct lttng_event_notifier_group
*event_notifier_group
)
828 struct lttng_ust_event_notifier
*event_notifier
;
829 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
830 struct cds_hlist_head
*head
;
834 * Get the hashtable bucket the created lttng_event_notifier object
835 * should be inserted.
837 head
= borrow_hash_table_bucket(
838 event_notifier_group
->event_notifiers_ht
.table
,
839 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
841 event_notifier
= zmalloc(sizeof(struct lttng_ust_event_notifier
));
842 if (!event_notifier
) {
846 event_notifier
->struct_size
= sizeof(struct lttng_ust_event_notifier
);
848 event_notifier
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
849 if (!event_notifier
->parent
) {
853 event_notifier
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
854 event_notifier
->parent
->type
= LTTNG_UST_EVENT_TYPE_NOTIFIER
;
855 event_notifier
->parent
->child
= event_notifier
;
857 event_notifier_priv
= zmalloc(sizeof(struct lttng_ust_event_notifier_private
));
858 if (!event_notifier_priv
) {
862 event_notifier
->priv
= event_notifier_priv
;
863 event_notifier_priv
->pub
= event_notifier
;
864 event_notifier
->parent
->priv
= &event_notifier_priv
->parent
;
865 event_notifier_priv
->parent
.pub
= event_notifier
->parent
;
867 event_notifier_priv
->group
= event_notifier_group
;
868 event_notifier_priv
->parent
.user_token
= token
;
869 event_notifier_priv
->error_counter_index
= error_counter_index
;
871 /* Event notifier will be enabled by enabler sync. */
872 event_notifier
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
873 event_notifier
->parent
->enabled
= 0;
874 event_notifier_priv
->parent
.registered
= 0;
876 CDS_INIT_LIST_HEAD(&event_notifier
->parent
->priv
->filter_bytecode_runtime_head
);
877 CDS_INIT_LIST_HEAD(&event_notifier
->priv
->capture_bytecode_runtime_head
);
878 CDS_INIT_LIST_HEAD(&event_notifier_priv
->parent
.enablers_ref_head
);
879 event_notifier_priv
->parent
.desc
= desc
;
880 event_notifier
->notification_send
= lttng_event_notifier_notification_send
;
882 cds_list_add(&event_notifier_priv
->node
,
883 &event_notifier_group
->event_notifiers_head
);
884 cds_hlist_add_head(&event_notifier_priv
->hlist
, head
);
889 free(event_notifier
->parent
);
891 free(event_notifier
);
897 int lttng_desc_match_star_glob_enabler(const struct lttng_ust_event_desc
*desc
,
898 struct lttng_enabler
*enabler
)
900 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
902 unsigned int has_loglevel
= 0;
904 lttng_ust_format_event_name(desc
, name
);
905 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_STAR_GLOB
);
906 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
909 if (desc
->loglevel
) {
910 loglevel
= *(*desc
->loglevel
);
913 if (!lttng_loglevel_match(loglevel
,
915 enabler
->event_param
.loglevel_type
,
916 enabler
->event_param
.loglevel
))
922 int lttng_desc_match_event_enabler(const struct lttng_ust_event_desc
*desc
,
923 struct lttng_enabler
*enabler
)
925 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
927 unsigned int has_loglevel
= 0;
929 lttng_ust_format_event_name(desc
, name
);
930 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_EVENT
);
931 if (strcmp(name
, enabler
->event_param
.name
))
933 if (desc
->loglevel
) {
934 loglevel
= *(*desc
->loglevel
);
937 if (!lttng_loglevel_match(loglevel
,
939 enabler
->event_param
.loglevel_type
,
940 enabler
->event_param
.loglevel
))
946 int lttng_desc_match_enabler(const struct lttng_ust_event_desc
*desc
,
947 struct lttng_enabler
*enabler
)
949 switch (enabler
->format_type
) {
950 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
952 struct lttng_ust_excluder_node
*excluder
;
954 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
959 * If the matching event matches with an excluder,
960 * return 'does not match'
962 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
965 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
969 excluder_name
= (char *) (excluder
->excluder
.names
)
970 + count
* LTTNG_UST_ABI_SYM_NAME_LEN
;
971 len
= strnlen(excluder_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
973 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
975 lttng_ust_format_event_name(desc
, name
);
976 if (strutils_star_glob_match(excluder_name
, len
, name
, SIZE_MAX
)) {
984 case LTTNG_ENABLER_FORMAT_EVENT
:
985 return lttng_desc_match_event_enabler(desc
, enabler
);
992 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
993 struct lttng_ust_event_recorder
*event_recorder
)
995 if (lttng_desc_match_enabler(event_recorder
->parent
->priv
->desc
,
996 lttng_event_enabler_as_enabler(event_enabler
))
997 && event_recorder
->chan
== event_enabler
->chan
)
1004 int lttng_event_notifier_enabler_match_event_notifier(
1005 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1006 struct lttng_ust_event_notifier
*event_notifier
)
1008 int desc_matches
= lttng_desc_match_enabler(event_notifier
->priv
->parent
.desc
,
1009 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1011 if (desc_matches
&& event_notifier
->priv
->group
== event_notifier_enabler
->group
&&
1012 event_notifier
->priv
->parent
.user_token
== event_notifier_enabler
->user_token
)
1019 struct lttng_enabler_ref
*lttng_enabler_ref(
1020 struct cds_list_head
*enabler_ref_list
,
1021 struct lttng_enabler
*enabler
)
1023 struct lttng_enabler_ref
*enabler_ref
;
1025 cds_list_for_each_entry(enabler_ref
, enabler_ref_list
, node
) {
1026 if (enabler_ref
->ref
== enabler
)
1033 * Create struct lttng_event if it is missing and present in the list of
1034 * tracepoint probes.
1037 void lttng_create_event_recorder_if_missing(struct lttng_event_enabler
*event_enabler
)
1039 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1040 struct lttng_ust_registered_probe
*reg_probe
;
1041 const struct lttng_ust_event_desc
*desc
;
1042 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1044 struct cds_list_head
*probe_list
;
1046 probe_list
= lttng_get_probe_list_head();
1048 * For each probe event, if we find that a probe event matches
1049 * our enabler, create an associated lttng_event if not
1052 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1053 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1055 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1058 struct cds_hlist_head
*head
;
1059 struct cds_hlist_node
*node
;
1061 desc
= probe_desc
->event_desc
[i
];
1062 if (!lttng_desc_match_enabler(desc
,
1063 lttng_event_enabler_as_enabler(event_enabler
)))
1066 head
= borrow_hash_table_bucket(
1067 session
->priv
->events_ht
.table
,
1068 LTTNG_UST_EVENT_HT_SIZE
, desc
);
1070 cds_hlist_for_each_entry(event_recorder_priv
, node
, head
, hlist
) {
1071 if (event_recorder_priv
->parent
.desc
== desc
1072 && event_recorder_priv
->pub
->chan
== event_enabler
->chan
) {
1081 * We need to create an event for this
1084 ret
= lttng_event_recorder_create(probe_desc
->event_desc
[i
],
1085 event_enabler
->chan
);
1087 DBG("Unable to create event \"%s:%s\", error %d\n",
1088 probe_desc
->provider_name
,
1089 probe_desc
->event_desc
[i
]->event_name
, ret
);
1096 void probe_provider_event_for_each(const struct lttng_ust_probe_desc
*provider_desc
,
1097 void (*event_func
)(struct lttng_ust_event_common
*event
))
1099 struct cds_hlist_node
*node
, *tmp_node
;
1100 struct cds_list_head
*sessionsp
;
1103 /* Get handle on list of sessions. */
1104 sessionsp
= lttng_get_sessions();
1107 * Iterate over all events in the probe provider descriptions and
1108 * sessions to queue the unregistration of the events.
1110 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
1111 const struct lttng_ust_event_desc
*event_desc
;
1112 struct lttng_event_notifier_group
*event_notifier_group
;
1113 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1114 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1115 struct lttng_ust_session_private
*session_priv
;
1116 struct cds_hlist_head
*head
;
1118 event_desc
= provider_desc
->event_desc
[i
];
1121 * Iterate over all session to find the current event
1124 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
1126 * Get the list of events in the hashtable bucket and
1127 * iterate to find the event matching this descriptor.
1129 head
= borrow_hash_table_bucket(
1130 session_priv
->events_ht
.table
,
1131 LTTNG_UST_EVENT_HT_SIZE
, event_desc
);
1133 cds_hlist_for_each_entry_safe(event_recorder_priv
, node
, tmp_node
, head
, hlist
) {
1134 if (event_desc
== event_recorder_priv
->parent
.desc
) {
1135 event_func(event_recorder_priv
->parent
.pub
);
1142 * Iterate over all event_notifier groups to find the current event
1145 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1147 * Get the list of event_notifiers in the hashtable bucket and
1148 * iterate to find the event_notifier matching this
1151 head
= borrow_hash_table_bucket(
1152 event_notifier_group
->event_notifiers_ht
.table
,
1153 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, event_desc
);
1155 cds_hlist_for_each_entry_safe(event_notifier_priv
, node
, tmp_node
, head
, hlist
) {
1156 if (event_desc
== event_notifier_priv
->parent
.desc
) {
1157 event_func(event_notifier_priv
->parent
.pub
);
1166 void _event_enum_destroy(struct lttng_ust_event_common
*event
)
1169 switch (event
->type
) {
1170 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1172 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1173 struct lttng_ust_session
*session
= event_recorder
->chan
->parent
->session
;
1176 /* Destroy enums of the current event. */
1177 for (i
= 0; i
< event_recorder
->parent
->priv
->desc
->nr_fields
; i
++) {
1178 const struct lttng_ust_enum_desc
*enum_desc
;
1179 const struct lttng_ust_event_field
*field
;
1180 struct lttng_enum
*curr_enum
;
1182 field
= event_recorder
->parent
->priv
->desc
->fields
[i
];
1183 switch (field
->type
->type
) {
1184 case lttng_ust_type_enum
:
1185 enum_desc
= lttng_ust_get_type_enum(field
->type
)->desc
;
1191 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
1193 _lttng_enum_destroy(curr_enum
);
1198 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1203 /* Destroy event. */
1204 _lttng_event_destroy(event
);
1208 * Iterate over all the UST sessions to unregister and destroy all probes from
1209 * the probe provider descriptor received as argument. Must me called with the
1212 void lttng_probe_provider_unregister_events(
1213 const struct lttng_ust_probe_desc
*provider_desc
)
1216 * Iterate over all events in the probe provider descriptions and sessions
1217 * to queue the unregistration of the events.
1219 probe_provider_event_for_each(provider_desc
, _lttng_event_unregister
);
1221 /* Wait for grace period. */
1222 lttng_ust_urcu_synchronize_rcu();
1223 /* Prune the unregistration queue. */
1224 lttng_ust_tp_probe_prune_release_queue();
1227 * It is now safe to destroy the events and remove them from the event list
1230 probe_provider_event_for_each(provider_desc
, _event_enum_destroy
);
1234 * Create events associated with an event enabler (if not already present),
1235 * and add backward reference from the event to the enabler.
1238 int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler
*event_enabler
)
1240 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1241 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1243 if (!lttng_event_enabler_as_enabler(event_enabler
)->enabled
)
1246 /* First ensure that probe events are created for this enabler. */
1247 lttng_create_event_recorder_if_missing(event_enabler
);
1249 /* For each event matching enabler in session event list. */
1250 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1251 struct lttng_enabler_ref
*enabler_ref
;
1253 if (!lttng_event_enabler_match_event(event_enabler
, event_recorder_priv
->pub
))
1256 enabler_ref
= lttng_enabler_ref(&event_recorder_priv
->parent
.enablers_ref_head
,
1257 lttng_event_enabler_as_enabler(event_enabler
));
1260 * If no backward ref, create it.
1261 * Add backward ref from event to enabler.
1263 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1266 enabler_ref
->ref
= lttng_event_enabler_as_enabler(
1268 cds_list_add(&enabler_ref
->node
,
1269 &event_recorder_priv
->parent
.enablers_ref_head
);
1273 * Link filter bytecodes if not linked yet.
1275 lttng_enabler_link_bytecode(event_recorder_priv
->parent
.desc
,
1276 &session
->priv
->ctx
,
1277 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
,
1278 <tng_event_enabler_as_enabler(event_enabler
)->filter_bytecode_head
);
1280 /* TODO: merge event context. */
1287 * Called at library load: connect the probe on all enablers matching
1289 * Called with session mutex held.
1291 int lttng_fix_pending_events(void)
1293 struct lttng_ust_session_private
*session_priv
;
1295 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1296 lttng_session_lazy_sync_event_enablers(session_priv
->pub
);
1301 int lttng_fix_pending_event_notifiers(void)
1303 struct lttng_event_notifier_group
*event_notifier_group
;
1305 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1306 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1312 * For each session of the owner thread, execute pending statedump.
1313 * Only dump state for the sessions owned by the caller thread, because
1314 * we don't keep ust_lock across the entire iteration.
1316 void lttng_handle_pending_statedump(void *owner
)
1318 struct lttng_ust_session_private
*session_priv
;
1320 /* Execute state dump */
1321 do_lttng_ust_statedump(owner
);
1323 /* Clear pending state dump */
1327 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1328 if (session_priv
->owner
!= owner
)
1330 if (!session_priv
->statedump_pending
)
1332 session_priv
->statedump_pending
= 0;
1340 void _lttng_event_destroy(struct lttng_ust_event_common
*event
)
1342 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1344 lttng_free_event_filter_runtime(event
);
1345 /* Free event enabler refs */
1346 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1347 &event
->priv
->enablers_ref_head
, node
)
1350 switch (event
->type
) {
1351 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1353 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1355 /* Remove from event list. */
1356 cds_list_del(&event_recorder
->priv
->node
);
1357 /* Remove from event hash table. */
1358 cds_hlist_del(&event_recorder
->priv
->hlist
);
1360 lttng_destroy_context(event_recorder
->priv
->ctx
);
1361 free(event_recorder
->parent
);
1362 free(event_recorder
->priv
);
1363 free(event_recorder
);
1366 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1368 struct lttng_ust_event_notifier
*event_notifier
= event
->child
;
1370 /* Remove from event list. */
1371 cds_list_del(&event_notifier
->priv
->node
);
1372 /* Remove from event hash table. */
1373 cds_hlist_del(&event_notifier
->priv
->hlist
);
1375 free(event_notifier
->priv
);
1376 free(event_notifier
->parent
);
1377 free(event_notifier
);
1386 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1388 cds_list_del(&_enum
->node
);
1389 cds_hlist_del(&_enum
->hlist
);
1393 void lttng_ust_abi_events_exit(void)
1395 struct lttng_ust_session_private
*session_priv
, *tmpsession_priv
;
1397 cds_list_for_each_entry_safe(session_priv
, tmpsession_priv
, &sessions
, node
)
1398 lttng_session_destroy(session_priv
->pub
);
1402 * Enabler management.
1404 struct lttng_event_enabler
*lttng_event_enabler_create(
1405 enum lttng_enabler_format_type format_type
,
1406 struct lttng_ust_abi_event
*event_param
,
1407 struct lttng_ust_channel_buffer
*chan
)
1409 struct lttng_event_enabler
*event_enabler
;
1411 event_enabler
= zmalloc(sizeof(*event_enabler
));
1414 event_enabler
->base
.format_type
= format_type
;
1415 CDS_INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1416 CDS_INIT_LIST_HEAD(&event_enabler
->base
.excluder_head
);
1417 memcpy(&event_enabler
->base
.event_param
, event_param
,
1418 sizeof(event_enabler
->base
.event_param
));
1419 event_enabler
->chan
= chan
;
1421 event_enabler
->base
.enabled
= 0;
1422 cds_list_add(&event_enabler
->node
, &event_enabler
->chan
->parent
->session
->priv
->enablers_head
);
1423 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1425 return event_enabler
;
1428 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
1429 struct lttng_event_notifier_group
*event_notifier_group
,
1430 enum lttng_enabler_format_type format_type
,
1431 struct lttng_ust_abi_event_notifier
*event_notifier_param
)
1433 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1435 event_notifier_enabler
= zmalloc(sizeof(*event_notifier_enabler
));
1436 if (!event_notifier_enabler
)
1438 event_notifier_enabler
->base
.format_type
= format_type
;
1439 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
1440 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->capture_bytecode_head
);
1441 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.excluder_head
);
1443 event_notifier_enabler
->user_token
= event_notifier_param
->event
.token
;
1444 event_notifier_enabler
->error_counter_index
= event_notifier_param
->error_counter_index
;
1445 event_notifier_enabler
->num_captures
= 0;
1447 memcpy(&event_notifier_enabler
->base
.event_param
.name
,
1448 event_notifier_param
->event
.name
,
1449 sizeof(event_notifier_enabler
->base
.event_param
.name
));
1450 event_notifier_enabler
->base
.event_param
.instrumentation
=
1451 event_notifier_param
->event
.instrumentation
;
1452 event_notifier_enabler
->base
.event_param
.loglevel
=
1453 event_notifier_param
->event
.loglevel
;
1454 event_notifier_enabler
->base
.event_param
.loglevel_type
=
1455 event_notifier_param
->event
.loglevel_type
;
1457 event_notifier_enabler
->base
.enabled
= 0;
1458 event_notifier_enabler
->group
= event_notifier_group
;
1460 cds_list_add(&event_notifier_enabler
->node
,
1461 &event_notifier_group
->enablers_head
);
1463 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1465 return event_notifier_enabler
;
1468 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1470 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1471 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1476 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1478 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1479 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1485 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
1486 struct lttng_ust_bytecode_node
**bytecode
)
1488 (*bytecode
)->enabler
= enabler
;
1489 cds_list_add_tail(&(*bytecode
)->node
, &enabler
->filter_bytecode_head
);
1490 /* Take ownership of bytecode */
1494 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
1495 struct lttng_ust_bytecode_node
**bytecode
)
1497 _lttng_enabler_attach_filter_bytecode(
1498 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1500 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1505 void _lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1506 struct lttng_ust_excluder_node
**excluder
)
1508 (*excluder
)->enabler
= enabler
;
1509 cds_list_add_tail(&(*excluder
)->node
, &enabler
->excluder_head
);
1510 /* Take ownership of excluder */
1514 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler
*event_enabler
,
1515 struct lttng_ust_excluder_node
**excluder
)
1517 _lttng_enabler_attach_exclusion(
1518 lttng_event_enabler_as_enabler(event_enabler
), excluder
);
1520 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1524 int lttng_event_notifier_enabler_enable(
1525 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1527 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
1528 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1533 int lttng_event_notifier_enabler_disable(
1534 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1536 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
1537 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1542 int lttng_event_notifier_enabler_attach_filter_bytecode(
1543 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1544 struct lttng_ust_bytecode_node
**bytecode
)
1546 _lttng_enabler_attach_filter_bytecode(
1547 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1550 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1554 int lttng_event_notifier_enabler_attach_capture_bytecode(
1555 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1556 struct lttng_ust_bytecode_node
**bytecode
)
1558 (*bytecode
)->enabler
= lttng_event_notifier_enabler_as_enabler(
1559 event_notifier_enabler
);
1560 cds_list_add_tail(&(*bytecode
)->node
,
1561 &event_notifier_enabler
->capture_bytecode_head
);
1562 /* Take ownership of bytecode */
1564 event_notifier_enabler
->num_captures
++;
1566 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1570 int lttng_event_notifier_enabler_attach_exclusion(
1571 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1572 struct lttng_ust_excluder_node
**excluder
)
1574 _lttng_enabler_attach_exclusion(
1575 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1578 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1582 int lttng_attach_context(struct lttng_ust_abi_context
*context_param
,
1583 union lttng_ust_abi_args
*uargs
,
1584 struct lttng_ust_ctx
**ctx
, struct lttng_ust_session
*session
)
1587 * We cannot attach a context after trace has been started for a
1588 * session because the metadata does not allow expressing this
1589 * information outside of the original channel scope.
1591 if (session
->priv
->been_active
)
1594 switch (context_param
->ctx
) {
1595 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
1596 return lttng_add_pthread_id_to_ctx(ctx
);
1597 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1599 struct lttng_ust_abi_perf_counter_ctx
*perf_ctx_param
;
1601 perf_ctx_param
= &context_param
->u
.perf_counter
;
1602 return lttng_add_perf_counter_to_ctx(
1603 perf_ctx_param
->type
,
1604 perf_ctx_param
->config
,
1605 perf_ctx_param
->name
,
1608 case LTTNG_UST_ABI_CONTEXT_VTID
:
1609 return lttng_add_vtid_to_ctx(ctx
);
1610 case LTTNG_UST_ABI_CONTEXT_VPID
:
1611 return lttng_add_vpid_to_ctx(ctx
);
1612 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
1613 return lttng_add_procname_to_ctx(ctx
);
1614 case LTTNG_UST_ABI_CONTEXT_IP
:
1615 return lttng_add_ip_to_ctx(ctx
);
1616 case LTTNG_UST_ABI_CONTEXT_CPU_ID
:
1617 return lttng_add_cpu_id_to_ctx(ctx
);
1618 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1619 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1621 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
1622 return lttng_add_cgroup_ns_to_ctx(ctx
);
1623 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
1624 return lttng_add_ipc_ns_to_ctx(ctx
);
1625 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
1626 return lttng_add_mnt_ns_to_ctx(ctx
);
1627 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
1628 return lttng_add_net_ns_to_ctx(ctx
);
1629 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
1630 return lttng_add_pid_ns_to_ctx(ctx
);
1631 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
1632 return lttng_add_time_ns_to_ctx(ctx
);
1633 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
1634 return lttng_add_user_ns_to_ctx(ctx
);
1635 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
1636 return lttng_add_uts_ns_to_ctx(ctx
);
1637 case LTTNG_UST_ABI_CONTEXT_VUID
:
1638 return lttng_add_vuid_to_ctx(ctx
);
1639 case LTTNG_UST_ABI_CONTEXT_VEUID
:
1640 return lttng_add_veuid_to_ctx(ctx
);
1641 case LTTNG_UST_ABI_CONTEXT_VSUID
:
1642 return lttng_add_vsuid_to_ctx(ctx
);
1643 case LTTNG_UST_ABI_CONTEXT_VGID
:
1644 return lttng_add_vgid_to_ctx(ctx
);
1645 case LTTNG_UST_ABI_CONTEXT_VEGID
:
1646 return lttng_add_vegid_to_ctx(ctx
);
1647 case LTTNG_UST_ABI_CONTEXT_VSGID
:
1648 return lttng_add_vsgid_to_ctx(ctx
);
1654 int lttng_event_enabler_attach_context(
1655 struct lttng_event_enabler
*enabler
__attribute__((unused
)),
1656 struct lttng_ust_abi_context
*context_param
__attribute__((unused
)))
1661 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
1663 if (!event_enabler
) {
1666 cds_list_del(&event_enabler
->node
);
1668 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
1670 lttng_destroy_context(event_enabler
->ctx
);
1671 free(event_enabler
);
1675 * lttng_session_sync_event_enablers should be called just before starting a
1679 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
)
1681 struct lttng_event_enabler
*event_enabler
;
1682 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1684 cds_list_for_each_entry(event_enabler
, &session
->priv
->enablers_head
, node
)
1685 lttng_event_enabler_ref_event_recorders(event_enabler
);
1687 * For each event, if at least one of its enablers is enabled,
1688 * and its channel and session transient states are enabled, we
1689 * enable the event, else we disable it.
1691 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1692 struct lttng_enabler_ref
*enabler_ref
;
1693 struct lttng_ust_bytecode_runtime
*runtime
;
1694 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1698 cds_list_for_each_entry(enabler_ref
,
1699 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1700 if (enabler_ref
->ref
->enabled
) {
1706 * Enabled state is based on union of enablers, with
1707 * intesection of session and channel transient enable
1710 enabled
= enabled
&& session
->priv
->tstate
&& event_recorder_priv
->pub
->chan
->priv
->parent
.tstate
;
1712 CMM_STORE_SHARED(event_recorder_priv
->pub
->parent
->enabled
, enabled
);
1714 * Sync tracepoint registration with event enabled
1718 if (!event_recorder_priv
->parent
.registered
)
1719 register_event(event_recorder_priv
->parent
.pub
);
1721 if (event_recorder_priv
->parent
.registered
)
1722 unregister_event(event_recorder_priv
->parent
.pub
);
1725 /* Check if has enablers without bytecode enabled */
1726 cds_list_for_each_entry(enabler_ref
,
1727 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1728 if (enabler_ref
->ref
->enabled
1729 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1730 has_enablers_without_filter_bytecode
= 1;
1734 event_recorder_priv
->parent
.has_enablers_without_filter_bytecode
=
1735 has_enablers_without_filter_bytecode
;
1737 /* Enable filters */
1738 cds_list_for_each_entry(runtime
,
1739 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1740 lttng_bytecode_sync_state(runtime
);
1743 CMM_STORE_SHARED(event_recorder_priv
->parent
.pub
->eval_filter
,
1744 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1746 lttng_ust_tp_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(const struct lttng_ust_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_ust_registered_probe
*reg_probe
;
1762 struct cds_list_head
*probe_list
;
1765 probe_list
= lttng_get_probe_list_head();
1767 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1768 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1770 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1773 const struct lttng_ust_event_desc
*desc
;
1774 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1775 struct cds_hlist_head
*head
;
1776 struct cds_hlist_node
*node
;
1778 desc
= probe_desc
->event_desc
[i
];
1780 if (!lttng_desc_match_enabler(desc
,
1781 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1785 * Given the current event_notifier group, get the bucket that
1786 * the target event_notifier would be if it was already
1789 head
= borrow_hash_table_bucket(
1790 event_notifier_group
->event_notifiers_ht
.table
,
1791 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
1793 cds_hlist_for_each_entry(event_notifier_priv
, node
, head
, hlist
) {
1795 * Check if event_notifier already exists by checking
1796 * if the event_notifier and enabler share the same
1797 * description and id.
1799 if (event_notifier_priv
->parent
.desc
== desc
&&
1800 event_notifier_priv
->parent
.user_token
== event_notifier_enabler
->user_token
) {
1809 /* Check that the probe supports event notifiers, else report the error. */
1810 if (!lttng_ust_probe_supports_event_notifier(probe_desc
)) {
1811 ERR("Probe \"%s\" contains event \"%s:%s\" which matches an enabled event notifier, "
1812 "but its version (%u.%u) is too old and does not implement event notifiers. "
1813 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1814 "this event will not generate any notification.",
1815 probe_desc
->provider_name
,
1816 probe_desc
->provider_name
, desc
->event_name
,
1822 * We need to create a event_notifier for this event probe.
1824 ret
= lttng_event_notifier_create(desc
,
1825 event_notifier_enabler
->user_token
,
1826 event_notifier_enabler
->error_counter_index
,
1827 event_notifier_group
);
1829 DBG("Unable to create event_notifier \"%s:%s\", error %d\n",
1830 probe_desc
->provider_name
,
1831 probe_desc
->event_desc
[i
]->event_name
, ret
);
1838 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1841 int lttng_event_notifier_enabler_ref_event_notifiers(
1842 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1844 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1845 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1848 * Only try to create event_notifiers for enablers that are enabled, the user
1849 * might still be attaching filter or exclusion to the
1850 * event_notifier_enabler.
1852 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
)
1855 /* First, ensure that probe event_notifiers are created for this enabler. */
1856 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
1858 /* Link the created event_notifier with its associated enabler. */
1859 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1860 struct lttng_enabler_ref
*enabler_ref
;
1862 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier_priv
->pub
))
1865 enabler_ref
= lttng_enabler_ref(&event_notifier_priv
->parent
.enablers_ref_head
,
1866 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1869 * If no backward ref, create it.
1870 * Add backward ref from event_notifier to enabler.
1872 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1876 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
1877 event_notifier_enabler
);
1878 cds_list_add(&enabler_ref
->node
,
1879 &event_notifier_priv
->parent
.enablers_ref_head
);
1883 * Link filter bytecodes if not linked yet.
1885 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1886 &event_notifier_group
->ctx
,
1887 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
,
1888 <tng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->filter_bytecode_head
);
1891 * Link capture bytecodes if not linked yet.
1893 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1894 &event_notifier_group
->ctx
, &event_notifier_priv
->capture_bytecode_runtime_head
,
1895 &event_notifier_enabler
->capture_bytecode_head
);
1897 event_notifier_priv
->num_captures
= event_notifier_enabler
->num_captures
;
1904 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
1906 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1907 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1909 cds_list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
1910 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
1913 * For each event_notifier, if at least one of its enablers is enabled,
1914 * we enable the event_notifier, else we disable it.
1916 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1917 struct lttng_enabler_ref
*enabler_ref
;
1918 struct lttng_ust_bytecode_runtime
*runtime
;
1919 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1920 int nr_filters
= 0, nr_captures
= 0;
1922 /* Enable event_notifiers */
1923 cds_list_for_each_entry(enabler_ref
,
1924 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1925 if (enabler_ref
->ref
->enabled
) {
1931 CMM_STORE_SHARED(event_notifier_priv
->pub
->parent
->enabled
, enabled
);
1933 * Sync tracepoint registration with event_notifier enabled
1937 if (!event_notifier_priv
->parent
.registered
)
1938 register_event(event_notifier_priv
->parent
.pub
);
1940 if (event_notifier_priv
->parent
.registered
)
1941 unregister_event(event_notifier_priv
->parent
.pub
);
1944 /* Check if has enablers without bytecode enabled */
1945 cds_list_for_each_entry(enabler_ref
,
1946 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1947 if (enabler_ref
->ref
->enabled
1948 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1949 has_enablers_without_filter_bytecode
= 1;
1953 event_notifier_priv
->parent
.has_enablers_without_filter_bytecode
=
1954 has_enablers_without_filter_bytecode
;
1956 /* Enable filters */
1957 cds_list_for_each_entry(runtime
,
1958 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1959 lttng_bytecode_sync_state(runtime
);
1962 CMM_STORE_SHARED(event_notifier_priv
->parent
.pub
->eval_filter
,
1963 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1965 /* Enable captures. */
1966 cds_list_for_each_entry(runtime
,
1967 &event_notifier_priv
->capture_bytecode_runtime_head
, node
) {
1968 lttng_bytecode_sync_state(runtime
);
1971 CMM_STORE_SHARED(event_notifier_priv
->pub
->eval_capture
,
1974 lttng_ust_tp_probe_prune_release_queue();
1978 * Apply enablers to session events, adding events to session if need
1979 * be. It is required after each modification applied to an active
1980 * session, and right before session "start".
1981 * "lazy" sync means we only sync if required.
1984 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
)
1986 /* We can skip if session is not active */
1987 if (!session
->active
)
1989 lttng_session_sync_event_enablers(session
);
1993 * Update all sessions with the given app context.
1994 * Called with ust lock held.
1995 * This is invoked when an application context gets loaded/unloaded. It
1996 * ensures the context callbacks are in sync with the application
1997 * context (either app context callbacks, or dummy callbacks).
1999 void lttng_ust_context_set_session_provider(const char *name
,
2000 size_t (*get_size
)(void *priv
, size_t offset
),
2001 void (*record
)(void *priv
, struct lttng_ust_ring_buffer_ctx
*ctx
,
2002 struct lttng_ust_channel_buffer
*chan
),
2003 void (*get_value
)(void *priv
, struct lttng_ust_ctx_value
*value
),
2006 struct lttng_ust_session_private
*session_priv
;
2008 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
2009 struct lttng_ust_channel_buffer_private
*chan
;
2010 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
2013 ret
= lttng_ust_context_set_provider_rcu(&session_priv
->ctx
,
2014 name
, get_size
, record
, get_value
, priv
);
2017 cds_list_for_each_entry(chan
, &session_priv
->chan_head
, node
) {
2018 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
2019 name
, get_size
, record
, get_value
, priv
);
2023 cds_list_for_each_entry(event_recorder_priv
, &session_priv
->events_head
, node
) {
2024 ret
= lttng_ust_context_set_provider_rcu(&event_recorder_priv
->ctx
,
2025 name
, get_size
, record
, get_value
, priv
);
2033 * Update all event_notifier groups with the given app context.
2034 * Called with ust lock held.
2035 * This is invoked when an application context gets loaded/unloaded. It
2036 * ensures the context callbacks are in sync with the application
2037 * context (either app context callbacks, or dummy callbacks).
2039 void lttng_ust_context_set_event_notifier_group_provider(const char *name
,
2040 size_t (*get_size
)(void *priv
, size_t offset
),
2041 void (*record
)(void *priv
, struct lttng_ust_ring_buffer_ctx
*ctx
,
2042 struct lttng_ust_channel_buffer
*chan
),
2043 void (*get_value
)(void *priv
, struct lttng_ust_ctx_value
*value
),
2046 struct lttng_event_notifier_group
*event_notifier_group
;
2048 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2051 ret
= lttng_ust_context_set_provider_rcu(
2052 &event_notifier_group
->ctx
,
2053 name
, get_size
, record
, get_value
, priv
);
2059 int lttng_ust_session_uuid_validate(struct lttng_ust_session
*session
,
2060 unsigned char *uuid
)
2064 /* Compare UUID with session. */
2065 if (session
->priv
->uuid_set
) {
2066 if (memcmp(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
)) {
2070 memcpy(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
2071 session
->priv
->uuid_set
= true;