2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
24 #include <lttng/ust-endian.h>
26 #include <urcu/arch.h>
27 #include <urcu/compiler.h>
28 #include <urcu/hlist.h>
29 #include <urcu/list.h>
30 #include <urcu/uatomic.h>
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
35 #include <usterr-signal-safe.h>
36 #include <ust-helper.h>
37 #include <lttng/ust-ctl.h>
40 #include <ust-dynamic-type.h>
41 #include <ust-context-provider.h>
44 #include "lttng-ust-uuid.h"
46 #include "tracepoint-internal.h"
47 #include "string-utils.h"
48 #include "lttng-bytecode.h"
49 #include "lttng-tracer.h"
50 #include "lttng-tracer-core.h"
51 #include "lttng-ust-statedump.h"
52 #include "context-internal.h"
53 #include "ust-events-internal.h"
55 #include "../libringbuffer/shm.h"
56 #include "../libringbuffer/frontend_types.h"
57 #include "../libringbuffer/frontend.h"
58 #include "../libcounter/counter.h"
60 #include <lttng/ust-abi.h>
61 #include "context-provider-internal.h"
64 * All operations within this file are called by the communication
65 * thread, under ust_lock protection.
68 static CDS_LIST_HEAD(sessions
);
69 static CDS_LIST_HEAD(event_notifier_groups
);
71 struct cds_list_head
*lttng_get_sessions(void)
76 static void _lttng_event_destroy(struct lttng_ust_event_common
*event
);
77 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
80 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
);
82 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
);
84 void lttng_event_notifier_group_sync_enablers(
85 struct lttng_event_notifier_group
*event_notifier_group
);
87 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
89 bool lttng_ust_validate_event_name(const struct lttng_ust_event_desc
*desc
)
91 if (strlen(desc
->probe_desc
->provider_name
) + 1 +
92 strlen(desc
->event_name
) >= LTTNG_UST_ABI_SYM_NAME_LEN
)
97 void lttng_ust_format_event_name(const struct lttng_ust_event_desc
*desc
,
100 strcpy(name
, desc
->probe_desc
->provider_name
);
102 strcat(name
, desc
->event_name
);
106 * Called with ust lock held.
108 int lttng_session_active(void)
110 struct lttng_ust_session_private
*iter
;
112 cds_list_for_each_entry(iter
, &sessions
, node
) {
113 if (iter
->pub
->active
)
120 int lttng_loglevel_match(int loglevel
,
121 unsigned int has_loglevel
,
122 enum lttng_ust_abi_loglevel_type req_type
,
126 loglevel
= TRACE_DEFAULT
;
128 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
129 if (loglevel
<= req_loglevel
130 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
134 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
135 if (loglevel
== req_loglevel
136 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
140 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
142 if (loglevel
<= TRACE_DEBUG
)
149 struct lttng_ust_session
*lttng_session_create(void)
151 struct lttng_ust_session
*session
;
152 struct lttng_ust_session_private
*session_priv
;
155 session
= zmalloc(sizeof(struct lttng_ust_session
));
158 session
->struct_size
= sizeof(struct lttng_ust_session
);
159 session_priv
= zmalloc(sizeof(struct lttng_ust_session_private
));
164 session
->priv
= session_priv
;
165 session_priv
->pub
= session
;
166 if (lttng_context_init_all(&session
->priv
->ctx
)) {
171 CDS_INIT_LIST_HEAD(&session
->priv
->chan_head
);
172 CDS_INIT_LIST_HEAD(&session
->priv
->events_head
);
173 CDS_INIT_LIST_HEAD(&session
->priv
->enums_head
);
174 CDS_INIT_LIST_HEAD(&session
->priv
->enablers_head
);
175 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
176 CDS_INIT_HLIST_HEAD(&session
->priv
->events_ht
.table
[i
]);
177 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
178 CDS_INIT_HLIST_HEAD(&session
->priv
->enums_ht
.table
[i
]);
179 cds_list_add(&session
->priv
->node
, &sessions
);
183 struct lttng_counter
*lttng_ust_counter_create(
184 const char *counter_transport_name
,
185 size_t number_dimensions
, const struct lttng_counter_dimension
*dimensions
)
187 struct lttng_counter_transport
*counter_transport
= NULL
;
188 struct lttng_counter
*counter
= NULL
;
190 counter_transport
= lttng_counter_transport_find(counter_transport_name
);
191 if (!counter_transport
)
193 counter
= zmalloc(sizeof(struct lttng_counter
));
197 counter
->ops
= &counter_transport
->ops
;
198 counter
->transport
= counter_transport
;
200 counter
->counter
= counter
->ops
->counter_create(
201 number_dimensions
, dimensions
, 0,
203 if (!counter
->counter
) {
217 void lttng_ust_counter_destroy(struct lttng_counter
*counter
)
219 counter
->ops
->counter_destroy(counter
->counter
);
223 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
225 struct lttng_event_notifier_group
*event_notifier_group
;
228 event_notifier_group
= zmalloc(sizeof(struct lttng_event_notifier_group
));
229 if (!event_notifier_group
)
232 /* Add all contexts. */
233 if (lttng_context_init_all(&event_notifier_group
->ctx
)) {
234 free(event_notifier_group
);
238 CDS_INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
239 CDS_INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
240 for (i
= 0; i
< LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
; i
++)
241 CDS_INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
243 cds_list_add(&event_notifier_group
->node
, &event_notifier_groups
);
245 return event_notifier_group
;
249 * Only used internally at session destruction.
252 void _lttng_channel_unmap(struct lttng_ust_channel_buffer
*lttng_chan
)
254 struct lttng_ust_lib_ring_buffer_channel
*chan
;
255 struct lttng_ust_shm_handle
*handle
;
257 cds_list_del(<tng_chan
->priv
->node
);
258 lttng_destroy_context(lttng_chan
->priv
->ctx
);
259 chan
= lttng_chan
->priv
->rb_chan
;
260 handle
= chan
->handle
;
261 channel_destroy(chan
, handle
, 0);
262 free(lttng_chan
->parent
);
263 free(lttng_chan
->priv
);
268 void register_event(struct lttng_ust_event_common
*event
)
271 const struct lttng_ust_event_desc
*desc
;
272 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
274 assert(event
->priv
->registered
== 0);
275 desc
= event
->priv
->desc
;
276 lttng_ust_format_event_name(desc
, name
);
277 ret
= lttng_ust_tp_probe_register_queue_release(name
,
278 desc
->probe_callback
,
279 event
, desc
->signature
);
282 event
->priv
->registered
= 1;
286 void unregister_event(struct lttng_ust_event_common
*event
)
289 const struct lttng_ust_event_desc
*desc
;
290 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
292 assert(event
->priv
->registered
== 1);
293 desc
= event
->priv
->desc
;
294 lttng_ust_format_event_name(desc
, name
);
295 ret
= lttng_ust_tp_probe_unregister_queue_release(name
,
296 desc
->probe_callback
,
300 event
->priv
->registered
= 0;
304 void _lttng_event_unregister(struct lttng_ust_event_common
*event
)
306 if (event
->priv
->registered
)
307 unregister_event(event
);
310 void lttng_session_destroy(struct lttng_ust_session
*session
)
312 struct lttng_ust_channel_buffer_private
*chan
, *tmpchan
;
313 struct lttng_ust_event_recorder_private
*event_recorder_priv
, *tmpevent_recorder_priv
;
314 struct lttng_enum
*_enum
, *tmp_enum
;
315 struct lttng_event_enabler
*event_enabler
, *event_tmpenabler
;
317 CMM_ACCESS_ONCE(session
->active
) = 0;
318 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
319 _lttng_event_unregister(event_recorder_priv
->parent
.pub
);
321 lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */
322 lttng_ust_tp_probe_prune_release_queue();
323 cds_list_for_each_entry_safe(event_enabler
, event_tmpenabler
,
324 &session
->priv
->enablers_head
, node
)
325 lttng_event_enabler_destroy(event_enabler
);
326 cds_list_for_each_entry_safe(event_recorder_priv
, tmpevent_recorder_priv
,
327 &session
->priv
->events_head
, node
)
328 _lttng_event_destroy(event_recorder_priv
->parent
.pub
);
329 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
330 &session
->priv
->enums_head
, node
)
331 _lttng_enum_destroy(_enum
);
332 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->priv
->chan_head
, node
)
333 _lttng_channel_unmap(chan
->pub
);
334 cds_list_del(&session
->priv
->node
);
335 lttng_destroy_context(session
->priv
->ctx
);
340 void lttng_event_notifier_group_destroy(
341 struct lttng_event_notifier_group
*event_notifier_group
)
344 struct lttng_event_notifier_enabler
*notifier_enabler
, *tmpnotifier_enabler
;
345 struct lttng_ust_event_notifier_private
*event_notifier_priv
, *tmpevent_notifier_priv
;
347 if (!event_notifier_group
) {
351 cds_list_for_each_entry(event_notifier_priv
,
352 &event_notifier_group
->event_notifiers_head
, node
)
353 _lttng_event_unregister(event_notifier_priv
->parent
.pub
);
355 lttng_ust_urcu_synchronize_rcu();
357 cds_list_for_each_entry_safe(notifier_enabler
, tmpnotifier_enabler
,
358 &event_notifier_group
->enablers_head
, node
)
359 lttng_event_notifier_enabler_destroy(notifier_enabler
);
361 cds_list_for_each_entry_safe(event_notifier_priv
, tmpevent_notifier_priv
,
362 &event_notifier_group
->event_notifiers_head
, node
)
363 _lttng_event_destroy(event_notifier_priv
->parent
.pub
);
365 if (event_notifier_group
->error_counter
)
366 lttng_ust_counter_destroy(event_notifier_group
->error_counter
);
368 /* Close the notification fd to the listener of event_notifiers. */
370 lttng_ust_lock_fd_tracker();
371 close_ret
= close(event_notifier_group
->notification_fd
);
373 lttng_ust_delete_fd_from_tracker(
374 event_notifier_group
->notification_fd
);
379 lttng_ust_unlock_fd_tracker();
381 cds_list_del(&event_notifier_group
->node
);
383 free(event_notifier_group
);
387 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
389 struct lttng_ust_bytecode_node
*filter_node
, *tmp_filter_node
;
390 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
396 /* Destroy filter bytecode */
397 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
398 &enabler
->filter_bytecode_head
, node
) {
402 /* Destroy excluders */
403 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
404 &enabler
->excluder_head
, node
) {
409 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
411 if (!event_notifier_enabler
) {
415 cds_list_del(&event_notifier_enabler
->node
);
417 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
419 free(event_notifier_enabler
);
423 int lttng_enum_create(const struct lttng_ust_enum_desc
*desc
,
424 struct lttng_ust_session
*session
)
426 const char *enum_name
= desc
->name
;
427 struct lttng_enum
*_enum
;
428 struct cds_hlist_head
*head
;
430 size_t name_len
= strlen(enum_name
);
434 /* Check if this enum is already registered for this session. */
435 hash
= jhash(enum_name
, name_len
, 0);
436 head
= &session
->priv
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
438 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
444 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
445 if (notify_socket
< 0) {
450 _enum
= zmalloc(sizeof(*_enum
));
455 _enum
->session
= session
;
458 ret
= ustcomm_register_enum(notify_socket
,
465 DBG("Error (%d) registering enumeration to sessiond", ret
);
466 goto sessiond_register_error
;
468 cds_list_add(&_enum
->node
, &session
->priv
->enums_head
);
469 cds_hlist_add_head(&_enum
->hlist
, head
);
472 sessiond_register_error
:
481 int lttng_create_enum_check(const struct lttng_ust_type_common
*type
,
482 struct lttng_ust_session
*session
)
484 switch (type
->type
) {
485 case lttng_ust_type_enum
:
487 const struct lttng_ust_enum_desc
*enum_desc
;
490 enum_desc
= lttng_ust_get_type_enum(type
)->desc
;
491 ret
= lttng_enum_create(enum_desc
, session
);
492 if (ret
&& ret
!= -EEXIST
) {
493 DBG("Unable to create enum error: (%d)", ret
);
498 case lttng_ust_type_dynamic
:
500 const struct lttng_ust_event_field
*tag_field_generic
;
501 const struct lttng_ust_enum_desc
*enum_desc
;
504 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
505 enum_desc
= lttng_ust_get_type_enum(tag_field_generic
->type
)->desc
;
506 ret
= lttng_enum_create(enum_desc
, session
);
507 if (ret
&& ret
!= -EEXIST
) {
508 DBG("Unable to create enum error: (%d)", ret
);
514 /* TODO: nested types when they become supported. */
521 int lttng_create_all_event_enums(size_t nr_fields
,
522 const struct lttng_ust_event_field
**event_fields
,
523 struct lttng_ust_session
*session
)
528 /* For each field, ensure enum is part of the session. */
529 for (i
= 0; i
< nr_fields
; i
++) {
530 const struct lttng_ust_type_common
*type
= event_fields
[i
]->type
;
532 ret
= lttng_create_enum_check(type
, session
);
540 int lttng_create_all_ctx_enums(size_t nr_fields
,
541 struct lttng_ust_ctx_field
*ctx_fields
,
542 struct lttng_ust_session
*session
)
547 /* For each field, ensure enum is part of the session. */
548 for (i
= 0; i
< nr_fields
; i
++) {
549 const struct lttng_ust_type_common
*type
= ctx_fields
[i
].event_field
->type
;
551 ret
= lttng_create_enum_check(type
, session
);
559 * Ensure that a state-dump will be performed for this session at the end
560 * of the current handle_message().
562 int lttng_session_statedump(struct lttng_ust_session
*session
)
564 session
->priv
->statedump_pending
= 1;
565 lttng_ust_sockinfo_session_enabled(session
->priv
->owner
);
569 int lttng_session_enable(struct lttng_ust_session
*session
)
572 struct lttng_ust_channel_buffer_private
*chan
;
575 if (session
->active
) {
580 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
581 if (notify_socket
< 0)
582 return notify_socket
;
584 /* Set transient enabler state to "enabled" */
585 session
->priv
->tstate
= 1;
587 /* We need to sync enablers with session before activation. */
588 lttng_session_sync_event_enablers(session
);
591 * Snapshot the number of events per channel to know the type of header
594 cds_list_for_each_entry(chan
, &session
->priv
->chan_head
, node
) {
595 struct lttng_ust_ctx
*ctx
;
596 struct lttng_ust_ctx_field
*fields
= NULL
;
597 size_t nr_fields
= 0;
600 /* don't change it if session stop/restart */
601 if (chan
->header_type
)
605 nr_fields
= ctx
->nr_fields
;
606 fields
= ctx
->fields
;
607 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
610 DBG("Error (%d) adding enum to session", ret
);
614 ret
= ustcomm_register_channel(notify_socket
,
623 DBG("Error (%d) registering channel to sessiond", ret
);
626 if (chan_id
!= chan
->id
) {
627 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
633 /* Set atomically the state to "active" */
634 CMM_ACCESS_ONCE(session
->active
) = 1;
635 CMM_ACCESS_ONCE(session
->priv
->been_active
) = 1;
637 ret
= lttng_session_statedump(session
);
644 int lttng_session_disable(struct lttng_ust_session
*session
)
648 if (!session
->active
) {
652 /* Set atomically the state to "inactive" */
653 CMM_ACCESS_ONCE(session
->active
) = 0;
655 /* Set transient enabler state to "disabled" */
656 session
->priv
->tstate
= 0;
657 lttng_session_sync_event_enablers(session
);
662 int lttng_channel_enable(struct lttng_ust_channel_common
*lttng_channel
)
666 if (lttng_channel
->enabled
) {
670 /* Set transient enabler state to "enabled" */
671 lttng_channel
->priv
->tstate
= 1;
672 lttng_session_sync_event_enablers(lttng_channel
->session
);
673 /* Set atomically the state to "enabled" */
674 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 1;
679 int lttng_channel_disable(struct lttng_ust_channel_common
*lttng_channel
)
683 if (!lttng_channel
->enabled
) {
687 /* Set atomically the state to "disabled" */
688 CMM_ACCESS_ONCE(lttng_channel
->enabled
) = 0;
689 /* Set transient enabler state to "enabled" */
690 lttng_channel
->priv
->tstate
= 0;
691 lttng_session_sync_event_enablers(lttng_channel
->session
);
697 struct cds_hlist_head
*borrow_hash_table_bucket(
698 struct cds_hlist_head
*hash_table
,
699 unsigned int hash_table_size
,
700 const struct lttng_ust_event_desc
*desc
)
702 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
706 lttng_ust_format_event_name(desc
, name
);
707 name_len
= strlen(name
);
709 hash
= jhash(name
, name_len
, 0);
710 return &hash_table
[hash
& (hash_table_size
- 1)];
714 * Supports event creation while tracing session is active.
717 int lttng_event_recorder_create(const struct lttng_ust_event_desc
*desc
,
718 struct lttng_ust_channel_buffer
*chan
)
720 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
721 struct lttng_ust_event_recorder
*event_recorder
;
722 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
723 struct lttng_ust_session
*session
= chan
->parent
->session
;
724 struct cds_hlist_head
*head
;
726 int notify_socket
, loglevel
;
729 head
= borrow_hash_table_bucket(chan
->parent
->session
->priv
->events_ht
.table
,
730 LTTNG_UST_EVENT_HT_SIZE
, desc
);
732 notify_socket
= lttng_get_notify_socket(session
->priv
->owner
);
733 if (notify_socket
< 0) {
738 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
741 DBG("Error (%d) adding enum to session", ret
);
742 goto create_enum_error
;
746 * Check if loglevel match. Refuse to connect event if not.
748 event_recorder
= zmalloc(sizeof(struct lttng_ust_event_recorder
));
749 if (!event_recorder
) {
753 event_recorder
->struct_size
= sizeof(struct lttng_ust_event_recorder
);
755 event_recorder
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
756 if (!event_recorder
->parent
) {
760 event_recorder
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
761 event_recorder
->parent
->type
= LTTNG_UST_EVENT_TYPE_RECORDER
;
762 event_recorder
->parent
->child
= event_recorder
;
764 event_recorder_priv
= zmalloc(sizeof(struct lttng_ust_event_recorder_private
));
765 if (!event_recorder_priv
) {
769 event_recorder
->priv
= event_recorder_priv
;
770 event_recorder_priv
->pub
= event_recorder
;
771 event_recorder
->parent
->priv
= &event_recorder_priv
->parent
;
772 event_recorder_priv
->parent
.pub
= event_recorder
->parent
;
774 event_recorder
->chan
= chan
;
776 /* Event will be enabled by enabler sync. */
777 event_recorder
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
778 event_recorder
->parent
->enabled
= 0;
779 event_recorder
->parent
->priv
->registered
= 0;
780 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->filter_bytecode_runtime_head
);
781 CDS_INIT_LIST_HEAD(&event_recorder
->parent
->priv
->enablers_ref_head
);
782 event_recorder
->parent
->priv
->desc
= desc
;
785 loglevel
= *(*event_recorder
->parent
->priv
->desc
->loglevel
);
787 loglevel
= TRACE_DEFAULT
;
788 if (desc
->model_emf_uri
)
789 uri
= *(desc
->model_emf_uri
);
793 lttng_ust_format_event_name(desc
, name
);
795 /* Fetch event ID from sessiond */
796 ret
= ustcomm_register_event(notify_socket
,
799 chan
->priv
->parent
.objd
,
806 &event_recorder
->priv
->id
);
808 DBG("Error (%d) registering event to sessiond", ret
);
809 goto sessiond_register_error
;
812 cds_list_add(&event_recorder_priv
->node
, &chan
->parent
->session
->priv
->events_head
);
813 cds_hlist_add_head(&event_recorder_priv
->hlist
, head
);
816 sessiond_register_error
:
817 free(event_recorder_priv
);
819 free(event_recorder
->parent
);
821 free(event_recorder
);
829 int lttng_event_notifier_create(const struct lttng_ust_event_desc
*desc
,
830 uint64_t token
, uint64_t error_counter_index
,
831 struct lttng_event_notifier_group
*event_notifier_group
)
833 struct lttng_ust_event_notifier
*event_notifier
;
834 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
835 struct cds_hlist_head
*head
;
839 * Get the hashtable bucket the created lttng_event_notifier object
840 * should be inserted.
842 head
= borrow_hash_table_bucket(
843 event_notifier_group
->event_notifiers_ht
.table
,
844 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
846 event_notifier
= zmalloc(sizeof(struct lttng_ust_event_notifier
));
847 if (!event_notifier
) {
851 event_notifier
->struct_size
= sizeof(struct lttng_ust_event_notifier
);
853 event_notifier
->parent
= zmalloc(sizeof(struct lttng_ust_event_common
));
854 if (!event_notifier
->parent
) {
858 event_notifier
->parent
->struct_size
= sizeof(struct lttng_ust_event_common
);
859 event_notifier
->parent
->type
= LTTNG_UST_EVENT_TYPE_NOTIFIER
;
860 event_notifier
->parent
->child
= event_notifier
;
862 event_notifier_priv
= zmalloc(sizeof(struct lttng_ust_event_notifier_private
));
863 if (!event_notifier_priv
) {
867 event_notifier
->priv
= event_notifier_priv
;
868 event_notifier_priv
->pub
= event_notifier
;
869 event_notifier
->parent
->priv
= &event_notifier_priv
->parent
;
870 event_notifier_priv
->parent
.pub
= event_notifier
->parent
;
872 event_notifier_priv
->group
= event_notifier_group
;
873 event_notifier_priv
->parent
.user_token
= token
;
874 event_notifier_priv
->error_counter_index
= error_counter_index
;
876 /* Event notifier will be enabled by enabler sync. */
877 event_notifier
->parent
->run_filter
= lttng_ust_interpret_event_filter
;
878 event_notifier
->parent
->enabled
= 0;
879 event_notifier_priv
->parent
.registered
= 0;
881 CDS_INIT_LIST_HEAD(&event_notifier
->parent
->priv
->filter_bytecode_runtime_head
);
882 CDS_INIT_LIST_HEAD(&event_notifier
->priv
->capture_bytecode_runtime_head
);
883 CDS_INIT_LIST_HEAD(&event_notifier_priv
->parent
.enablers_ref_head
);
884 event_notifier_priv
->parent
.desc
= desc
;
885 event_notifier
->notification_send
= lttng_event_notifier_notification_send
;
887 cds_list_add(&event_notifier_priv
->node
,
888 &event_notifier_group
->event_notifiers_head
);
889 cds_hlist_add_head(&event_notifier_priv
->hlist
, head
);
894 free(event_notifier
->parent
);
896 free(event_notifier
);
902 int lttng_desc_match_star_glob_enabler(const struct lttng_ust_event_desc
*desc
,
903 struct lttng_enabler
*enabler
)
905 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
907 unsigned int has_loglevel
= 0;
909 lttng_ust_format_event_name(desc
, name
);
910 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_STAR_GLOB
);
911 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
914 if (desc
->loglevel
) {
915 loglevel
= *(*desc
->loglevel
);
918 if (!lttng_loglevel_match(loglevel
,
920 enabler
->event_param
.loglevel_type
,
921 enabler
->event_param
.loglevel
))
927 int lttng_desc_match_event_enabler(const struct lttng_ust_event_desc
*desc
,
928 struct lttng_enabler
*enabler
)
930 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
932 unsigned int has_loglevel
= 0;
934 lttng_ust_format_event_name(desc
, name
);
935 assert(enabler
->format_type
== LTTNG_ENABLER_FORMAT_EVENT
);
936 if (strcmp(name
, enabler
->event_param
.name
))
938 if (desc
->loglevel
) {
939 loglevel
= *(*desc
->loglevel
);
942 if (!lttng_loglevel_match(loglevel
,
944 enabler
->event_param
.loglevel_type
,
945 enabler
->event_param
.loglevel
))
951 int lttng_desc_match_enabler(const struct lttng_ust_event_desc
*desc
,
952 struct lttng_enabler
*enabler
)
954 switch (enabler
->format_type
) {
955 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
957 struct lttng_ust_excluder_node
*excluder
;
959 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
964 * If the matching event matches with an excluder,
965 * return 'does not match'
967 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
970 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
974 excluder_name
= (char *) (excluder
->excluder
.names
)
975 + count
* LTTNG_UST_ABI_SYM_NAME_LEN
;
976 len
= strnlen(excluder_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
978 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
980 lttng_ust_format_event_name(desc
, name
);
981 if (strutils_star_glob_match(excluder_name
, len
, name
, SIZE_MAX
)) {
989 case LTTNG_ENABLER_FORMAT_EVENT
:
990 return lttng_desc_match_event_enabler(desc
, enabler
);
997 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
998 struct lttng_ust_event_recorder
*event_recorder
)
1000 if (lttng_desc_match_enabler(event_recorder
->parent
->priv
->desc
,
1001 lttng_event_enabler_as_enabler(event_enabler
))
1002 && event_recorder
->chan
== event_enabler
->chan
)
1009 int lttng_event_notifier_enabler_match_event_notifier(
1010 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1011 struct lttng_ust_event_notifier
*event_notifier
)
1013 int desc_matches
= lttng_desc_match_enabler(event_notifier
->priv
->parent
.desc
,
1014 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1016 if (desc_matches
&& event_notifier
->priv
->group
== event_notifier_enabler
->group
&&
1017 event_notifier
->priv
->parent
.user_token
== event_notifier_enabler
->user_token
)
1024 struct lttng_enabler_ref
*lttng_enabler_ref(
1025 struct cds_list_head
*enabler_ref_list
,
1026 struct lttng_enabler
*enabler
)
1028 struct lttng_enabler_ref
*enabler_ref
;
1030 cds_list_for_each_entry(enabler_ref
, enabler_ref_list
, node
) {
1031 if (enabler_ref
->ref
== enabler
)
1038 * Create struct lttng_event if it is missing and present in the list of
1039 * tracepoint probes.
1042 void lttng_create_event_recorder_if_missing(struct lttng_event_enabler
*event_enabler
)
1044 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1045 struct lttng_ust_registered_probe
*reg_probe
;
1046 const struct lttng_ust_event_desc
*desc
;
1047 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1049 struct cds_list_head
*probe_list
;
1051 probe_list
= lttng_get_probe_list_head();
1053 * For each probe event, if we find that a probe event matches
1054 * our enabler, create an associated lttng_event if not
1057 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1058 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1060 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1063 struct cds_hlist_head
*head
;
1064 struct cds_hlist_node
*node
;
1066 desc
= probe_desc
->event_desc
[i
];
1067 if (!lttng_desc_match_enabler(desc
,
1068 lttng_event_enabler_as_enabler(event_enabler
)))
1071 head
= borrow_hash_table_bucket(
1072 session
->priv
->events_ht
.table
,
1073 LTTNG_UST_EVENT_HT_SIZE
, desc
);
1075 cds_hlist_for_each_entry(event_recorder_priv
, node
, head
, hlist
) {
1076 if (event_recorder_priv
->parent
.desc
== desc
1077 && event_recorder_priv
->pub
->chan
== event_enabler
->chan
) {
1086 * We need to create an event for this
1089 ret
= lttng_event_recorder_create(probe_desc
->event_desc
[i
],
1090 event_enabler
->chan
);
1092 DBG("Unable to create event \"%s:%s\", error %d\n",
1093 probe_desc
->provider_name
,
1094 probe_desc
->event_desc
[i
]->event_name
, ret
);
1101 void probe_provider_event_for_each(const struct lttng_ust_probe_desc
*provider_desc
,
1102 void (*event_func
)(struct lttng_ust_event_common
*event
))
1104 struct cds_hlist_node
*node
, *tmp_node
;
1105 struct cds_list_head
*sessionsp
;
1108 /* Get handle on list of sessions. */
1109 sessionsp
= lttng_get_sessions();
1112 * Iterate over all events in the probe provider descriptions and
1113 * sessions to queue the unregistration of the events.
1115 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
1116 const struct lttng_ust_event_desc
*event_desc
;
1117 struct lttng_event_notifier_group
*event_notifier_group
;
1118 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1119 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1120 struct lttng_ust_session_private
*session_priv
;
1121 struct cds_hlist_head
*head
;
1123 event_desc
= provider_desc
->event_desc
[i
];
1126 * Iterate over all session to find the current event
1129 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
1131 * Get the list of events in the hashtable bucket and
1132 * iterate to find the event matching this descriptor.
1134 head
= borrow_hash_table_bucket(
1135 session_priv
->events_ht
.table
,
1136 LTTNG_UST_EVENT_HT_SIZE
, event_desc
);
1138 cds_hlist_for_each_entry_safe(event_recorder_priv
, node
, tmp_node
, head
, hlist
) {
1139 if (event_desc
== event_recorder_priv
->parent
.desc
) {
1140 event_func(event_recorder_priv
->parent
.pub
);
1147 * Iterate over all event_notifier groups to find the current event
1150 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1152 * Get the list of event_notifiers in the hashtable bucket and
1153 * iterate to find the event_notifier matching this
1156 head
= borrow_hash_table_bucket(
1157 event_notifier_group
->event_notifiers_ht
.table
,
1158 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, event_desc
);
1160 cds_hlist_for_each_entry_safe(event_notifier_priv
, node
, tmp_node
, head
, hlist
) {
1161 if (event_desc
== event_notifier_priv
->parent
.desc
) {
1162 event_func(event_notifier_priv
->parent
.pub
);
1171 void _event_enum_destroy(struct lttng_ust_event_common
*event
)
1174 switch (event
->type
) {
1175 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1177 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1178 struct lttng_ust_session
*session
= event_recorder
->chan
->parent
->session
;
1181 /* Destroy enums of the current event. */
1182 for (i
= 0; i
< event_recorder
->parent
->priv
->desc
->nr_fields
; i
++) {
1183 const struct lttng_ust_enum_desc
*enum_desc
;
1184 const struct lttng_ust_event_field
*field
;
1185 struct lttng_enum
*curr_enum
;
1187 field
= event_recorder
->parent
->priv
->desc
->fields
[i
];
1188 switch (field
->type
->type
) {
1189 case lttng_ust_type_enum
:
1190 enum_desc
= lttng_ust_get_type_enum(field
->type
)->desc
;
1196 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
1198 _lttng_enum_destroy(curr_enum
);
1203 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1208 /* Destroy event. */
1209 _lttng_event_destroy(event
);
1213 * Iterate over all the UST sessions to unregister and destroy all probes from
1214 * the probe provider descriptor received as argument. Must me called with the
1217 void lttng_probe_provider_unregister_events(
1218 const struct lttng_ust_probe_desc
*provider_desc
)
1221 * Iterate over all events in the probe provider descriptions and sessions
1222 * to queue the unregistration of the events.
1224 probe_provider_event_for_each(provider_desc
, _lttng_event_unregister
);
1226 /* Wait for grace period. */
1227 lttng_ust_urcu_synchronize_rcu();
1228 /* Prune the unregistration queue. */
1229 lttng_ust_tp_probe_prune_release_queue();
1232 * It is now safe to destroy the events and remove them from the event list
1235 probe_provider_event_for_each(provider_desc
, _event_enum_destroy
);
1239 * Create events associated with an event enabler (if not already present),
1240 * and add backward reference from the event to the enabler.
1243 int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler
*event_enabler
)
1245 struct lttng_ust_session
*session
= event_enabler
->chan
->parent
->session
;
1246 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1248 if (!lttng_event_enabler_as_enabler(event_enabler
)->enabled
)
1251 /* First ensure that probe events are created for this enabler. */
1252 lttng_create_event_recorder_if_missing(event_enabler
);
1254 /* For each event matching enabler in session event list. */
1255 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1256 struct lttng_enabler_ref
*enabler_ref
;
1258 if (!lttng_event_enabler_match_event(event_enabler
, event_recorder_priv
->pub
))
1261 enabler_ref
= lttng_enabler_ref(&event_recorder_priv
->parent
.enablers_ref_head
,
1262 lttng_event_enabler_as_enabler(event_enabler
));
1265 * If no backward ref, create it.
1266 * Add backward ref from event to enabler.
1268 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1271 enabler_ref
->ref
= lttng_event_enabler_as_enabler(
1273 cds_list_add(&enabler_ref
->node
,
1274 &event_recorder_priv
->parent
.enablers_ref_head
);
1278 * Link filter bytecodes if not linked yet.
1280 lttng_enabler_link_bytecode(event_recorder_priv
->parent
.desc
,
1281 &session
->priv
->ctx
,
1282 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
,
1283 <tng_event_enabler_as_enabler(event_enabler
)->filter_bytecode_head
);
1285 /* TODO: merge event context. */
1292 * Called at library load: connect the probe on all enablers matching
1294 * Called with session mutex held.
1296 int lttng_fix_pending_events(void)
1298 struct lttng_ust_session_private
*session_priv
;
1300 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1301 lttng_session_lazy_sync_event_enablers(session_priv
->pub
);
1306 int lttng_fix_pending_event_notifiers(void)
1308 struct lttng_event_notifier_group
*event_notifier_group
;
1310 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
1311 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1317 * For each session of the owner thread, execute pending statedump.
1318 * Only dump state for the sessions owned by the caller thread, because
1319 * we don't keep ust_lock across the entire iteration.
1321 void lttng_handle_pending_statedump(void *owner
)
1323 struct lttng_ust_session_private
*session_priv
;
1325 /* Execute state dump */
1326 do_lttng_ust_statedump(owner
);
1328 /* Clear pending state dump */
1332 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
1333 if (session_priv
->owner
!= owner
)
1335 if (!session_priv
->statedump_pending
)
1337 session_priv
->statedump_pending
= 0;
1345 void _lttng_event_destroy(struct lttng_ust_event_common
*event
)
1347 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1349 lttng_free_event_filter_runtime(event
);
1350 /* Free event enabler refs */
1351 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1352 &event
->priv
->enablers_ref_head
, node
)
1355 switch (event
->type
) {
1356 case LTTNG_UST_EVENT_TYPE_RECORDER
:
1358 struct lttng_ust_event_recorder
*event_recorder
= event
->child
;
1360 /* Remove from event list. */
1361 cds_list_del(&event_recorder
->priv
->node
);
1362 /* Remove from event hash table. */
1363 cds_hlist_del(&event_recorder
->priv
->hlist
);
1365 lttng_destroy_context(event_recorder
->priv
->ctx
);
1366 free(event_recorder
->parent
);
1367 free(event_recorder
->priv
);
1368 free(event_recorder
);
1371 case LTTNG_UST_EVENT_TYPE_NOTIFIER
:
1373 struct lttng_ust_event_notifier
*event_notifier
= event
->child
;
1375 /* Remove from event list. */
1376 cds_list_del(&event_notifier
->priv
->node
);
1377 /* Remove from event hash table. */
1378 cds_hlist_del(&event_notifier
->priv
->hlist
);
1380 free(event_notifier
->priv
);
1381 free(event_notifier
->parent
);
1382 free(event_notifier
);
1391 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1393 cds_list_del(&_enum
->node
);
1394 cds_hlist_del(&_enum
->hlist
);
1398 void lttng_ust_abi_events_exit(void)
1400 struct lttng_ust_session_private
*session_priv
, *tmpsession_priv
;
1402 cds_list_for_each_entry_safe(session_priv
, tmpsession_priv
, &sessions
, node
)
1403 lttng_session_destroy(session_priv
->pub
);
1407 * Enabler management.
1409 struct lttng_event_enabler
*lttng_event_enabler_create(
1410 enum lttng_enabler_format_type format_type
,
1411 struct lttng_ust_abi_event
*event_param
,
1412 struct lttng_ust_channel_buffer
*chan
)
1414 struct lttng_event_enabler
*event_enabler
;
1416 event_enabler
= zmalloc(sizeof(*event_enabler
));
1419 event_enabler
->base
.format_type
= format_type
;
1420 CDS_INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1421 CDS_INIT_LIST_HEAD(&event_enabler
->base
.excluder_head
);
1422 memcpy(&event_enabler
->base
.event_param
, event_param
,
1423 sizeof(event_enabler
->base
.event_param
));
1424 event_enabler
->chan
= chan
;
1426 event_enabler
->base
.enabled
= 0;
1427 cds_list_add(&event_enabler
->node
, &event_enabler
->chan
->parent
->session
->priv
->enablers_head
);
1428 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1430 return event_enabler
;
1433 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
1434 struct lttng_event_notifier_group
*event_notifier_group
,
1435 enum lttng_enabler_format_type format_type
,
1436 struct lttng_ust_abi_event_notifier
*event_notifier_param
)
1438 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1440 event_notifier_enabler
= zmalloc(sizeof(*event_notifier_enabler
));
1441 if (!event_notifier_enabler
)
1443 event_notifier_enabler
->base
.format_type
= format_type
;
1444 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
1445 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->capture_bytecode_head
);
1446 CDS_INIT_LIST_HEAD(&event_notifier_enabler
->base
.excluder_head
);
1448 event_notifier_enabler
->user_token
= event_notifier_param
->event
.token
;
1449 event_notifier_enabler
->error_counter_index
= event_notifier_param
->error_counter_index
;
1450 event_notifier_enabler
->num_captures
= 0;
1452 memcpy(&event_notifier_enabler
->base
.event_param
.name
,
1453 event_notifier_param
->event
.name
,
1454 sizeof(event_notifier_enabler
->base
.event_param
.name
));
1455 event_notifier_enabler
->base
.event_param
.instrumentation
=
1456 event_notifier_param
->event
.instrumentation
;
1457 event_notifier_enabler
->base
.event_param
.loglevel
=
1458 event_notifier_param
->event
.loglevel
;
1459 event_notifier_enabler
->base
.event_param
.loglevel_type
=
1460 event_notifier_param
->event
.loglevel_type
;
1462 event_notifier_enabler
->base
.enabled
= 0;
1463 event_notifier_enabler
->group
= event_notifier_group
;
1465 cds_list_add(&event_notifier_enabler
->node
,
1466 &event_notifier_group
->enablers_head
);
1468 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
1470 return event_notifier_enabler
;
1473 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1475 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1476 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1481 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1483 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1484 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1490 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
1491 struct lttng_ust_bytecode_node
**bytecode
)
1493 (*bytecode
)->enabler
= enabler
;
1494 cds_list_add_tail(&(*bytecode
)->node
, &enabler
->filter_bytecode_head
);
1495 /* Take ownership of bytecode */
1499 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
1500 struct lttng_ust_bytecode_node
**bytecode
)
1502 _lttng_enabler_attach_filter_bytecode(
1503 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1505 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1510 void _lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1511 struct lttng_ust_excluder_node
**excluder
)
1513 (*excluder
)->enabler
= enabler
;
1514 cds_list_add_tail(&(*excluder
)->node
, &enabler
->excluder_head
);
1515 /* Take ownership of excluder */
1519 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler
*event_enabler
,
1520 struct lttng_ust_excluder_node
**excluder
)
1522 _lttng_enabler_attach_exclusion(
1523 lttng_event_enabler_as_enabler(event_enabler
), excluder
);
1525 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->parent
->session
);
1529 int lttng_event_notifier_enabler_enable(
1530 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1532 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
1533 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1538 int lttng_event_notifier_enabler_disable(
1539 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1541 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
1542 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1547 int lttng_event_notifier_enabler_attach_filter_bytecode(
1548 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1549 struct lttng_ust_bytecode_node
**bytecode
)
1551 _lttng_enabler_attach_filter_bytecode(
1552 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1555 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1559 int lttng_event_notifier_enabler_attach_capture_bytecode(
1560 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1561 struct lttng_ust_bytecode_node
**bytecode
)
1563 (*bytecode
)->enabler
= lttng_event_notifier_enabler_as_enabler(
1564 event_notifier_enabler
);
1565 cds_list_add_tail(&(*bytecode
)->node
,
1566 &event_notifier_enabler
->capture_bytecode_head
);
1567 /* Take ownership of bytecode */
1569 event_notifier_enabler
->num_captures
++;
1571 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1575 int lttng_event_notifier_enabler_attach_exclusion(
1576 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1577 struct lttng_ust_excluder_node
**excluder
)
1579 _lttng_enabler_attach_exclusion(
1580 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
1583 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
1587 int lttng_attach_context(struct lttng_ust_abi_context
*context_param
,
1588 union lttng_ust_abi_args
*uargs
,
1589 struct lttng_ust_ctx
**ctx
, struct lttng_ust_session
*session
)
1592 * We cannot attach a context after trace has been started for a
1593 * session because the metadata does not allow expressing this
1594 * information outside of the original channel scope.
1596 if (session
->priv
->been_active
)
1599 switch (context_param
->ctx
) {
1600 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
1601 return lttng_add_pthread_id_to_ctx(ctx
);
1602 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1604 struct lttng_ust_abi_perf_counter_ctx
*perf_ctx_param
;
1606 perf_ctx_param
= &context_param
->u
.perf_counter
;
1607 return lttng_add_perf_counter_to_ctx(
1608 perf_ctx_param
->type
,
1609 perf_ctx_param
->config
,
1610 perf_ctx_param
->name
,
1613 case LTTNG_UST_ABI_CONTEXT_VTID
:
1614 return lttng_add_vtid_to_ctx(ctx
);
1615 case LTTNG_UST_ABI_CONTEXT_VPID
:
1616 return lttng_add_vpid_to_ctx(ctx
);
1617 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
1618 return lttng_add_procname_to_ctx(ctx
);
1619 case LTTNG_UST_ABI_CONTEXT_IP
:
1620 return lttng_add_ip_to_ctx(ctx
);
1621 case LTTNG_UST_ABI_CONTEXT_CPU_ID
:
1622 return lttng_add_cpu_id_to_ctx(ctx
);
1623 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1624 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1626 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
1627 return lttng_add_cgroup_ns_to_ctx(ctx
);
1628 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
1629 return lttng_add_ipc_ns_to_ctx(ctx
);
1630 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
1631 return lttng_add_mnt_ns_to_ctx(ctx
);
1632 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
1633 return lttng_add_net_ns_to_ctx(ctx
);
1634 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
1635 return lttng_add_pid_ns_to_ctx(ctx
);
1636 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
1637 return lttng_add_time_ns_to_ctx(ctx
);
1638 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
1639 return lttng_add_user_ns_to_ctx(ctx
);
1640 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
1641 return lttng_add_uts_ns_to_ctx(ctx
);
1642 case LTTNG_UST_ABI_CONTEXT_VUID
:
1643 return lttng_add_vuid_to_ctx(ctx
);
1644 case LTTNG_UST_ABI_CONTEXT_VEUID
:
1645 return lttng_add_veuid_to_ctx(ctx
);
1646 case LTTNG_UST_ABI_CONTEXT_VSUID
:
1647 return lttng_add_vsuid_to_ctx(ctx
);
1648 case LTTNG_UST_ABI_CONTEXT_VGID
:
1649 return lttng_add_vgid_to_ctx(ctx
);
1650 case LTTNG_UST_ABI_CONTEXT_VEGID
:
1651 return lttng_add_vegid_to_ctx(ctx
);
1652 case LTTNG_UST_ABI_CONTEXT_VSGID
:
1653 return lttng_add_vsgid_to_ctx(ctx
);
1659 int lttng_event_enabler_attach_context(
1660 struct lttng_event_enabler
*enabler
__attribute__((unused
)),
1661 struct lttng_ust_abi_context
*context_param
__attribute__((unused
)))
1666 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
1668 if (!event_enabler
) {
1671 cds_list_del(&event_enabler
->node
);
1673 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
1675 lttng_destroy_context(event_enabler
->ctx
);
1676 free(event_enabler
);
1680 * lttng_session_sync_event_enablers should be called just before starting a
1684 void lttng_session_sync_event_enablers(struct lttng_ust_session
*session
)
1686 struct lttng_event_enabler
*event_enabler
;
1687 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
1689 cds_list_for_each_entry(event_enabler
, &session
->priv
->enablers_head
, node
)
1690 lttng_event_enabler_ref_event_recorders(event_enabler
);
1692 * For each event, if at least one of its enablers is enabled,
1693 * and its channel and session transient states are enabled, we
1694 * enable the event, else we disable it.
1696 cds_list_for_each_entry(event_recorder_priv
, &session
->priv
->events_head
, node
) {
1697 struct lttng_enabler_ref
*enabler_ref
;
1698 struct lttng_ust_bytecode_runtime
*runtime
;
1699 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1703 cds_list_for_each_entry(enabler_ref
,
1704 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1705 if (enabler_ref
->ref
->enabled
) {
1711 * Enabled state is based on union of enablers, with
1712 * intesection of session and channel transient enable
1715 enabled
= enabled
&& session
->priv
->tstate
&& event_recorder_priv
->pub
->chan
->priv
->parent
.tstate
;
1717 CMM_STORE_SHARED(event_recorder_priv
->pub
->parent
->enabled
, enabled
);
1719 * Sync tracepoint registration with event enabled
1723 if (!event_recorder_priv
->parent
.registered
)
1724 register_event(event_recorder_priv
->parent
.pub
);
1726 if (event_recorder_priv
->parent
.registered
)
1727 unregister_event(event_recorder_priv
->parent
.pub
);
1730 /* Check if has enablers without bytecode enabled */
1731 cds_list_for_each_entry(enabler_ref
,
1732 &event_recorder_priv
->parent
.enablers_ref_head
, node
) {
1733 if (enabler_ref
->ref
->enabled
1734 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1735 has_enablers_without_filter_bytecode
= 1;
1739 event_recorder_priv
->parent
.has_enablers_without_filter_bytecode
=
1740 has_enablers_without_filter_bytecode
;
1742 /* Enable filters */
1743 cds_list_for_each_entry(runtime
,
1744 &event_recorder_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1745 lttng_bytecode_sync_state(runtime
);
1748 CMM_STORE_SHARED(event_recorder_priv
->parent
.pub
->eval_filter
,
1749 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1751 lttng_ust_tp_probe_prune_release_queue();
1754 /* Support for event notifier is introduced by probe provider major version 2. */
1756 bool lttng_ust_probe_supports_event_notifier(const struct lttng_ust_probe_desc
*probe_desc
)
1758 return probe_desc
->major
>= 2;
1762 void lttng_create_event_notifier_if_missing(
1763 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1765 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1766 struct lttng_ust_registered_probe
*reg_probe
;
1767 struct cds_list_head
*probe_list
;
1770 probe_list
= lttng_get_probe_list_head();
1772 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
1773 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
1775 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1778 const struct lttng_ust_event_desc
*desc
;
1779 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1780 struct cds_hlist_head
*head
;
1781 struct cds_hlist_node
*node
;
1783 desc
= probe_desc
->event_desc
[i
];
1785 if (!lttng_desc_match_enabler(desc
,
1786 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1790 * Given the current event_notifier group, get the bucket that
1791 * the target event_notifier would be if it was already
1794 head
= borrow_hash_table_bucket(
1795 event_notifier_group
->event_notifiers_ht
.table
,
1796 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE
, desc
);
1798 cds_hlist_for_each_entry(event_notifier_priv
, node
, head
, hlist
) {
1800 * Check if event_notifier already exists by checking
1801 * if the event_notifier and enabler share the same
1802 * description and id.
1804 if (event_notifier_priv
->parent
.desc
== desc
&&
1805 event_notifier_priv
->parent
.user_token
== event_notifier_enabler
->user_token
) {
1814 /* Check that the probe supports event notifiers, else report the error. */
1815 if (!lttng_ust_probe_supports_event_notifier(probe_desc
)) {
1816 ERR("Probe \"%s\" contains event \"%s:%s\" which matches an enabled event notifier, "
1817 "but its version (%u.%u) is too old and does not implement event notifiers. "
1818 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1819 "this event will not generate any notification.",
1820 probe_desc
->provider_name
,
1821 probe_desc
->provider_name
, desc
->event_name
,
1827 * We need to create a event_notifier for this event probe.
1829 ret
= lttng_event_notifier_create(desc
,
1830 event_notifier_enabler
->user_token
,
1831 event_notifier_enabler
->error_counter_index
,
1832 event_notifier_group
);
1834 DBG("Unable to create event_notifier \"%s:%s\", error %d\n",
1835 probe_desc
->provider_name
,
1836 probe_desc
->event_desc
[i
]->event_name
, ret
);
1843 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1846 int lttng_event_notifier_enabler_ref_event_notifiers(
1847 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1849 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1850 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1853 * Only try to create event_notifiers for enablers that are enabled, the user
1854 * might still be attaching filter or exclusion to the
1855 * event_notifier_enabler.
1857 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
)
1860 /* First, ensure that probe event_notifiers are created for this enabler. */
1861 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
1863 /* Link the created event_notifier with its associated enabler. */
1864 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1865 struct lttng_enabler_ref
*enabler_ref
;
1867 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier_priv
->pub
))
1870 enabler_ref
= lttng_enabler_ref(&event_notifier_priv
->parent
.enablers_ref_head
,
1871 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
1874 * If no backward ref, create it.
1875 * Add backward ref from event_notifier to enabler.
1877 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
1881 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
1882 event_notifier_enabler
);
1883 cds_list_add(&enabler_ref
->node
,
1884 &event_notifier_priv
->parent
.enablers_ref_head
);
1888 * Link filter bytecodes if not linked yet.
1890 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1891 &event_notifier_group
->ctx
,
1892 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
,
1893 <tng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->filter_bytecode_head
);
1896 * Link capture bytecodes if not linked yet.
1898 lttng_enabler_link_bytecode(event_notifier_priv
->parent
.desc
,
1899 &event_notifier_group
->ctx
, &event_notifier_priv
->capture_bytecode_runtime_head
,
1900 &event_notifier_enabler
->capture_bytecode_head
);
1902 event_notifier_priv
->num_captures
= event_notifier_enabler
->num_captures
;
1909 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
1911 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
1912 struct lttng_ust_event_notifier_private
*event_notifier_priv
;
1914 cds_list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
1915 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
1918 * For each event_notifier, if at least one of its enablers is enabled,
1919 * we enable the event_notifier, else we disable it.
1921 cds_list_for_each_entry(event_notifier_priv
, &event_notifier_group
->event_notifiers_head
, node
) {
1922 struct lttng_enabler_ref
*enabler_ref
;
1923 struct lttng_ust_bytecode_runtime
*runtime
;
1924 int enabled
= 0, has_enablers_without_filter_bytecode
= 0;
1925 int nr_filters
= 0, nr_captures
= 0;
1927 /* Enable event_notifiers */
1928 cds_list_for_each_entry(enabler_ref
,
1929 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1930 if (enabler_ref
->ref
->enabled
) {
1936 CMM_STORE_SHARED(event_notifier_priv
->pub
->parent
->enabled
, enabled
);
1938 * Sync tracepoint registration with event_notifier enabled
1942 if (!event_notifier_priv
->parent
.registered
)
1943 register_event(event_notifier_priv
->parent
.pub
);
1945 if (event_notifier_priv
->parent
.registered
)
1946 unregister_event(event_notifier_priv
->parent
.pub
);
1949 /* Check if has enablers without bytecode enabled */
1950 cds_list_for_each_entry(enabler_ref
,
1951 &event_notifier_priv
->parent
.enablers_ref_head
, node
) {
1952 if (enabler_ref
->ref
->enabled
1953 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1954 has_enablers_without_filter_bytecode
= 1;
1958 event_notifier_priv
->parent
.has_enablers_without_filter_bytecode
=
1959 has_enablers_without_filter_bytecode
;
1961 /* Enable filters */
1962 cds_list_for_each_entry(runtime
,
1963 &event_notifier_priv
->parent
.filter_bytecode_runtime_head
, node
) {
1964 lttng_bytecode_sync_state(runtime
);
1967 CMM_STORE_SHARED(event_notifier_priv
->parent
.pub
->eval_filter
,
1968 !(has_enablers_without_filter_bytecode
|| !nr_filters
));
1970 /* Enable captures. */
1971 cds_list_for_each_entry(runtime
,
1972 &event_notifier_priv
->capture_bytecode_runtime_head
, node
) {
1973 lttng_bytecode_sync_state(runtime
);
1976 CMM_STORE_SHARED(event_notifier_priv
->pub
->eval_capture
,
1979 lttng_ust_tp_probe_prune_release_queue();
1983 * Apply enablers to session events, adding events to session if need
1984 * be. It is required after each modification applied to an active
1985 * session, and right before session "start".
1986 * "lazy" sync means we only sync if required.
1989 void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session
*session
)
1991 /* We can skip if session is not active */
1992 if (!session
->active
)
1994 lttng_session_sync_event_enablers(session
);
1998 * Update all sessions with the given app context.
1999 * Called with ust lock held.
2000 * This is invoked when an application context gets loaded/unloaded. It
2001 * ensures the context callbacks are in sync with the application
2002 * context (either app context callbacks, or dummy callbacks).
2004 void lttng_ust_context_set_session_provider(const char *name
,
2005 size_t (*get_size
)(void *priv
, size_t offset
),
2006 void (*record
)(void *priv
, struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2007 struct lttng_ust_channel_buffer
*chan
),
2008 void (*get_value
)(void *priv
, struct lttng_ust_ctx_value
*value
),
2011 struct lttng_ust_session_private
*session_priv
;
2013 cds_list_for_each_entry(session_priv
, &sessions
, node
) {
2014 struct lttng_ust_channel_buffer_private
*chan
;
2015 struct lttng_ust_event_recorder_private
*event_recorder_priv
;
2018 ret
= lttng_ust_context_set_provider_rcu(&session_priv
->ctx
,
2019 name
, get_size
, record
, get_value
, priv
);
2022 cds_list_for_each_entry(chan
, &session_priv
->chan_head
, node
) {
2023 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
2024 name
, get_size
, record
, get_value
, priv
);
2028 cds_list_for_each_entry(event_recorder_priv
, &session_priv
->events_head
, node
) {
2029 ret
= lttng_ust_context_set_provider_rcu(&event_recorder_priv
->ctx
,
2030 name
, get_size
, record
, get_value
, priv
);
2038 * Update all event_notifier groups with the given app context.
2039 * Called with ust lock held.
2040 * This is invoked when an application context gets loaded/unloaded. It
2041 * ensures the context callbacks are in sync with the application
2042 * context (either app context callbacks, or dummy callbacks).
2044 void lttng_ust_context_set_event_notifier_group_provider(const char *name
,
2045 size_t (*get_size
)(void *priv
, size_t offset
),
2046 void (*record
)(void *priv
, struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2047 struct lttng_ust_channel_buffer
*chan
),
2048 void (*get_value
)(void *priv
, struct lttng_ust_ctx_value
*value
),
2051 struct lttng_event_notifier_group
*event_notifier_group
;
2053 cds_list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2056 ret
= lttng_ust_context_set_provider_rcu(
2057 &event_notifier_group
->ctx
,
2058 name
, get_size
, record
, get_value
, priv
);
2064 int lttng_ust_session_uuid_validate(struct lttng_ust_session
*session
,
2065 unsigned char *uuid
)
2069 /* Compare UUID with session. */
2070 if (session
->priv
->uuid_set
) {
2071 if (memcmp(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
)) {
2075 memcpy(session
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
2076 session
->priv
->uuid_set
= true;