4 * Holds LTTng per-session event registry.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <urcu/list.h>
26 #include <urcu/hlist.h>
36 #include <lttng/ust-endian.h>
40 #include <urcu/compiler.h>
41 #include <urcu/uatomic.h>
42 #include <urcu/arch.h>
44 #include <lttng/tracepoint.h>
45 #include <lttng/ust-events.h>
47 #include <usterr-signal-safe.h>
49 #include <lttng/ust-ctl.h>
51 #include <lttng/ust-dynamic-type.h>
52 #include <lttng/ust-context-provider.h>
55 #include "lttng-ust-uuid.h"
57 #include "tracepoint-internal.h"
58 #include "string-utils.h"
59 #include "lttng-tracer.h"
60 #include "lttng-tracer-core.h"
61 #include "lttng-ust-statedump.h"
63 #include "../libringbuffer/shm.h"
67 * All operations within this file are called by the communication
68 * thread, under ust_lock protection.
71 static CDS_LIST_HEAD(sessions
);
73 struct cds_list_head
*_lttng_get_sessions(void)
78 static void _lttng_event_destroy(struct lttng_event
*event
);
79 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
82 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
84 void lttng_session_sync_enablers(struct lttng_session
*session
);
86 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
89 * Called with ust lock held.
91 int lttng_session_active(void)
93 struct lttng_session
*iter
;
95 cds_list_for_each_entry(iter
, &sessions
, node
) {
103 int lttng_loglevel_match(int loglevel
,
104 unsigned int has_loglevel
,
105 enum lttng_ust_loglevel_type req_type
,
109 loglevel
= TRACE_DEFAULT
;
111 case LTTNG_UST_LOGLEVEL_RANGE
:
112 if (loglevel
<= req_loglevel
113 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
117 case LTTNG_UST_LOGLEVEL_SINGLE
:
118 if (loglevel
== req_loglevel
119 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
123 case LTTNG_UST_LOGLEVEL_ALL
:
125 if (loglevel
<= TRACE_DEBUG
)
132 void synchronize_trace(void)
137 struct lttng_session
*lttng_session_create(void)
139 struct lttng_session
*session
;
142 session
= zmalloc(sizeof(struct lttng_session
));
145 if (lttng_session_context_init(&session
->ctx
)) {
149 CDS_INIT_LIST_HEAD(&session
->chan_head
);
150 CDS_INIT_LIST_HEAD(&session
->events_head
);
151 CDS_INIT_LIST_HEAD(&session
->enums_head
);
152 CDS_INIT_LIST_HEAD(&session
->enablers_head
);
153 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
154 CDS_INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
155 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
156 CDS_INIT_HLIST_HEAD(&session
->enums_ht
.table
[i
]);
157 cds_list_add(&session
->node
, &sessions
);
162 * Only used internally at session destruction.
165 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
167 struct channel
*chan
;
168 struct lttng_ust_shm_handle
*handle
;
170 cds_list_del(<tng_chan
->node
);
171 lttng_destroy_context(lttng_chan
->ctx
);
172 chan
= lttng_chan
->chan
;
173 handle
= lttng_chan
->handle
;
175 * note: lttng_chan is private data contained within handle. It
176 * will be freed along with the handle.
178 channel_destroy(chan
, handle
, 0);
182 void register_event(struct lttng_event
*event
)
185 const struct lttng_event_desc
*desc
;
187 assert(event
->registered
== 0);
189 ret
= __tracepoint_probe_register_queue_release(desc
->name
,
190 desc
->probe_callback
,
191 event
, desc
->signature
);
194 event
->registered
= 1;
198 void unregister_event(struct lttng_event
*event
)
201 const struct lttng_event_desc
*desc
;
203 assert(event
->registered
== 1);
205 ret
= __tracepoint_probe_unregister_queue_release(desc
->name
,
206 desc
->probe_callback
,
210 event
->registered
= 0;
214 * Only used internally at session destruction.
217 void _lttng_event_unregister(struct lttng_event
*event
)
219 if (event
->registered
)
220 unregister_event(event
);
223 void lttng_session_destroy(struct lttng_session
*session
)
225 struct lttng_channel
*chan
, *tmpchan
;
226 struct lttng_event
*event
, *tmpevent
;
227 struct lttng_enum
*_enum
, *tmp_enum
;
228 struct lttng_enabler
*enabler
, *tmpenabler
;
230 CMM_ACCESS_ONCE(session
->active
) = 0;
231 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
232 _lttng_event_unregister(event
);
234 synchronize_trace(); /* Wait for in-flight events to complete */
235 __tracepoint_probe_prune_release_queue();
236 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
237 &session
->enablers_head
, node
)
238 lttng_enabler_destroy(enabler
);
239 cds_list_for_each_entry_safe(event
, tmpevent
,
240 &session
->events_head
, node
)
241 _lttng_event_destroy(event
);
242 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
243 &session
->enums_head
, node
)
244 _lttng_enum_destroy(_enum
);
245 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
246 _lttng_channel_unmap(chan
);
247 cds_list_del(&session
->node
);
248 lttng_destroy_context(session
->ctx
);
253 int lttng_enum_create(const struct lttng_enum_desc
*desc
,
254 struct lttng_session
*session
)
256 const char *enum_name
= desc
->name
;
257 struct lttng_enum
*_enum
;
258 struct cds_hlist_head
*head
;
260 size_t name_len
= strlen(enum_name
);
264 /* Check if this enum is already registered for this session. */
265 hash
= jhash(enum_name
, name_len
, 0);
266 head
= &session
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
268 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
274 notify_socket
= lttng_get_notify_socket(session
->owner
);
275 if (notify_socket
< 0) {
280 _enum
= zmalloc(sizeof(*_enum
));
285 _enum
->session
= session
;
288 ret
= ustcomm_register_enum(notify_socket
,
295 DBG("Error (%d) registering enumeration to sessiond", ret
);
296 goto sessiond_register_error
;
298 cds_list_add(&_enum
->node
, &session
->enums_head
);
299 cds_hlist_add_head(&_enum
->hlist
, head
);
302 sessiond_register_error
:
311 int lttng_create_enum_check(const struct lttng_type
*type
,
312 struct lttng_session
*session
)
314 switch (type
->atype
) {
317 const struct lttng_enum_desc
*enum_desc
;
320 enum_desc
= type
->u
.legacy
.basic
.enumeration
.desc
;
321 ret
= lttng_enum_create(enum_desc
, session
);
322 if (ret
&& ret
!= -EEXIST
) {
323 DBG("Unable to create enum error: (%d)", ret
);
328 case atype_enum_nestable
:
330 const struct lttng_enum_desc
*enum_desc
;
333 enum_desc
= type
->u
.enum_nestable
.desc
;
334 ret
= lttng_enum_create(enum_desc
, session
);
335 if (ret
&& ret
!= -EEXIST
) {
336 DBG("Unable to create enum error: (%d)", ret
);
343 const struct lttng_event_field
*tag_field_generic
;
344 const struct lttng_enum_desc
*enum_desc
;
347 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
348 enum_desc
= tag_field_generic
->type
.u
.enum_nestable
.desc
;
349 ret
= lttng_enum_create(enum_desc
, session
);
350 if (ret
&& ret
!= -EEXIST
) {
351 DBG("Unable to create enum error: (%d)", ret
);
357 /* TODO: nested types when they become supported. */
364 int lttng_create_all_event_enums(size_t nr_fields
,
365 const struct lttng_event_field
*event_fields
,
366 struct lttng_session
*session
)
371 /* For each field, ensure enum is part of the session. */
372 for (i
= 0; i
< nr_fields
; i
++) {
373 const struct lttng_type
*type
= &event_fields
[i
].type
;
375 ret
= lttng_create_enum_check(type
, session
);
383 int lttng_create_all_ctx_enums(size_t nr_fields
,
384 const struct lttng_ctx_field
*ctx_fields
,
385 struct lttng_session
*session
)
390 /* For each field, ensure enum is part of the session. */
391 for (i
= 0; i
< nr_fields
; i
++) {
392 const struct lttng_type
*type
= &ctx_fields
[i
].event_field
.type
;
394 ret
= lttng_create_enum_check(type
, session
);
402 * Ensure that a state-dump will be performed for this session at the end
403 * of the current handle_message().
405 int lttng_session_statedump(struct lttng_session
*session
)
407 session
->statedump_pending
= 1;
408 lttng_ust_sockinfo_session_enabled(session
->owner
);
412 int lttng_session_enable(struct lttng_session
*session
)
415 struct lttng_channel
*chan
;
418 if (session
->active
) {
423 notify_socket
= lttng_get_notify_socket(session
->owner
);
424 if (notify_socket
< 0)
425 return notify_socket
;
427 /* Set transient enabler state to "enabled" */
430 /* We need to sync enablers with session before activation. */
431 lttng_session_sync_enablers(session
);
434 * Snapshot the number of events per channel to know the type of header
437 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
438 const struct lttng_ctx
*ctx
;
439 const struct lttng_ctx_field
*fields
= NULL
;
440 size_t nr_fields
= 0;
443 /* don't change it if session stop/restart */
444 if (chan
->header_type
)
448 nr_fields
= ctx
->nr_fields
;
449 fields
= ctx
->fields
;
450 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
453 DBG("Error (%d) adding enum to session", ret
);
457 ret
= ustcomm_register_channel(notify_socket
,
466 DBG("Error (%d) registering channel to sessiond", ret
);
469 if (chan_id
!= chan
->id
) {
470 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
476 /* Set atomically the state to "active" */
477 CMM_ACCESS_ONCE(session
->active
) = 1;
478 CMM_ACCESS_ONCE(session
->been_active
) = 1;
480 ret
= lttng_session_statedump(session
);
487 int lttng_session_disable(struct lttng_session
*session
)
491 if (!session
->active
) {
495 /* Set atomically the state to "inactive" */
496 CMM_ACCESS_ONCE(session
->active
) = 0;
498 /* Set transient enabler state to "disabled" */
500 lttng_session_sync_enablers(session
);
505 int lttng_channel_enable(struct lttng_channel
*channel
)
509 if (channel
->enabled
) {
513 /* Set transient enabler state to "enabled" */
515 lttng_session_sync_enablers(channel
->session
);
516 /* Set atomically the state to "enabled" */
517 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
522 int lttng_channel_disable(struct lttng_channel
*channel
)
526 if (!channel
->enabled
) {
530 /* Set atomically the state to "disabled" */
531 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
532 /* Set transient enabler state to "enabled" */
534 lttng_session_sync_enablers(channel
->session
);
540 * Supports event creation while tracing session is active.
543 int lttng_event_create(const struct lttng_event_desc
*desc
,
544 struct lttng_channel
*chan
)
546 const char *event_name
= desc
->name
;
547 struct lttng_event
*event
;
548 struct lttng_session
*session
= chan
->session
;
549 struct cds_hlist_head
*head
;
551 size_t name_len
= strlen(event_name
);
553 int notify_socket
, loglevel
;
556 hash
= jhash(event_name
, name_len
, 0);
557 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
559 notify_socket
= lttng_get_notify_socket(session
->owner
);
560 if (notify_socket
< 0) {
565 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
568 DBG("Error (%d) adding enum to session", ret
);
569 goto create_enum_error
;
573 * Check if loglevel match. Refuse to connect event if not.
575 event
= zmalloc(sizeof(struct lttng_event
));
582 /* Event will be enabled by enabler sync. */
584 event
->registered
= 0;
585 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
586 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
590 loglevel
= *(*event
->desc
->loglevel
);
592 loglevel
= TRACE_DEFAULT
;
593 if (desc
->u
.ext
.model_emf_uri
)
594 uri
= *(desc
->u
.ext
.model_emf_uri
);
598 /* Fetch event ID from sessiond */
599 ret
= ustcomm_register_event(notify_socket
,
611 DBG("Error (%d) registering event to sessiond", ret
);
612 goto sessiond_register_error
;
615 cds_list_add(&event
->node
, &chan
->session
->events_head
);
616 cds_hlist_add_head(&event
->hlist
, head
);
619 sessiond_register_error
:
628 int lttng_desc_match_star_glob_enabler(const struct lttng_event_desc
*desc
,
629 struct lttng_enabler
*enabler
)
632 unsigned int has_loglevel
= 0;
634 assert(enabler
->type
== LTTNG_ENABLER_STAR_GLOB
);
635 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
636 desc
->name
, SIZE_MAX
))
638 if (desc
->loglevel
) {
639 loglevel
= *(*desc
->loglevel
);
642 if (!lttng_loglevel_match(loglevel
,
644 enabler
->event_param
.loglevel_type
,
645 enabler
->event_param
.loglevel
))
651 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
652 struct lttng_enabler
*enabler
)
655 unsigned int has_loglevel
= 0;
657 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
658 if (strcmp(desc
->name
, enabler
->event_param
.name
))
660 if (desc
->loglevel
) {
661 loglevel
= *(*desc
->loglevel
);
664 if (!lttng_loglevel_match(loglevel
,
666 enabler
->event_param
.loglevel_type
,
667 enabler
->event_param
.loglevel
))
673 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
674 struct lttng_enabler
*enabler
)
676 switch (enabler
->type
) {
677 case LTTNG_ENABLER_STAR_GLOB
:
679 struct lttng_ust_excluder_node
*excluder
;
681 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
686 * If the matching event matches with an excluder,
687 * return 'does not match'
689 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
692 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
696 excluder_name
= (char *) (excluder
->excluder
.names
)
697 + count
* LTTNG_UST_SYM_NAME_LEN
;
698 len
= strnlen(excluder_name
, LTTNG_UST_SYM_NAME_LEN
);
699 if (len
> 0 && strutils_star_glob_match(excluder_name
, len
, desc
->name
, SIZE_MAX
))
705 case LTTNG_ENABLER_EVENT
:
706 return lttng_desc_match_event_enabler(desc
, enabler
);
713 int lttng_event_match_enabler(struct lttng_event
*event
,
714 struct lttng_enabler
*enabler
)
716 if (lttng_desc_match_enabler(event
->desc
, enabler
)
717 && event
->chan
== enabler
->chan
)
724 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
725 struct lttng_enabler
*enabler
)
727 struct lttng_enabler_ref
*enabler_ref
;
729 cds_list_for_each_entry(enabler_ref
,
730 &event
->enablers_ref_head
, node
) {
731 if (enabler_ref
->ref
== enabler
)
738 * Create struct lttng_event if it is missing and present in the list of
742 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
744 struct lttng_session
*session
= enabler
->chan
->session
;
745 struct lttng_probe_desc
*probe_desc
;
746 const struct lttng_event_desc
*desc
;
747 struct lttng_event
*event
;
749 struct cds_list_head
*probe_list
;
751 probe_list
= lttng_get_probe_list_head();
753 * For each probe event, if we find that a probe event matches
754 * our enabler, create an associated lttng_event if not
757 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
758 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
761 struct cds_hlist_head
*head
;
762 struct cds_hlist_node
*node
;
763 const char *event_name
;
767 desc
= probe_desc
->event_desc
[i
];
768 if (!lttng_desc_match_enabler(desc
, enabler
))
770 event_name
= desc
->name
;
771 name_len
= strlen(event_name
);
774 * Check if already created.
776 hash
= jhash(event_name
, name_len
, 0);
777 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
778 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
779 if (event
->desc
== desc
780 && event
->chan
== enabler
->chan
) {
789 * We need to create an event for this
792 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
795 DBG("Unable to create event %s, error %d\n",
796 probe_desc
->event_desc
[i
]->name
, ret
);
803 * Iterate over all the UST sessions to unregister and destroy all probes from
804 * the probe provider descriptor received as argument. Must me called with the
807 void lttng_probe_provider_unregister_events(struct lttng_probe_desc
*provider_desc
)
809 struct cds_hlist_node
*node
, *tmp_node
;
810 struct cds_list_head
*sessionsp
;
811 struct lttng_session
*session
;
812 struct cds_hlist_head
*head
;
813 struct lttng_event
*event
;
816 /* Get handle on list of sessions. */
817 sessionsp
= _lttng_get_sessions();
820 * Iterate over all events in the probe provider descriptions and sessions
821 * to queue the unregistration of the events.
823 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
824 const struct lttng_event_desc
*event_desc
;
825 const char *event_name
;
829 event_desc
= provider_desc
->event_desc
[i
];
830 event_name
= event_desc
->name
;
831 name_len
= strlen(event_name
);
832 hash
= jhash(event_name
, name_len
, 0);
834 /* Iterate over all session to find the current event description. */
835 cds_list_for_each_entry(session
, sessionsp
, node
) {
837 * Get the list of events in the hashtable bucket and iterate to
838 * find the event matching this descriptor.
840 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
841 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
842 if (event_desc
== event
->desc
) {
843 /* Queue the unregistration of this event. */
844 _lttng_event_unregister(event
);
851 /* Wait for grace period. */
853 /* Prune the unregistration queue. */
854 __tracepoint_probe_prune_release_queue();
857 * It is now safe to destroy the events and remove them from the event list
860 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
861 const struct lttng_event_desc
*event_desc
;
862 const char *event_name
;
866 event_desc
= provider_desc
->event_desc
[i
];
867 event_name
= event_desc
->name
;
868 name_len
= strlen(event_name
);
869 hash
= jhash(event_name
, name_len
, 0);
871 /* Iterate over all sessions to find the current event description. */
872 cds_list_for_each_entry(session
, sessionsp
, node
) {
874 * Get the list of events in the hashtable bucket and iterate to
875 * find the event matching this descriptor.
877 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
878 cds_hlist_for_each_entry_safe(event
, node
, tmp_node
, head
, hlist
) {
879 if (event_desc
== event
->desc
) {
880 /* Destroy enums of the current event. */
881 for (j
= 0; j
< event
->desc
->nr_fields
; j
++) {
882 const struct lttng_enum_desc
*enum_desc
;
883 const struct lttng_event_field
*field
;
884 struct lttng_enum
*curr_enum
;
886 field
= &(event
->desc
->fields
[j
]);
887 switch (field
->type
.atype
) {
889 enum_desc
= field
->type
.u
.legacy
.basic
.enumeration
.desc
;
891 case atype_enum_nestable
:
892 enum_desc
= field
->type
.u
.enum_nestable
.desc
;
897 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
899 _lttng_enum_destroy(curr_enum
);
904 _lttng_event_destroy(event
);
913 * Create events associated with an enabler (if not already present),
914 * and add backward reference from the event to the enabler.
917 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
919 struct lttng_session
*session
= enabler
->chan
->session
;
920 struct lttng_event
*event
;
922 if (!enabler
->enabled
)
925 /* First ensure that probe events are created for this enabler. */
926 lttng_create_event_if_missing(enabler
);
928 /* For each event matching enabler in session event list. */
929 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
930 struct lttng_enabler_ref
*enabler_ref
;
932 if (!lttng_event_match_enabler(event
, enabler
))
935 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
938 * If no backward ref, create it.
939 * Add backward ref from event to enabler.
941 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
944 enabler_ref
->ref
= enabler
;
945 cds_list_add(&enabler_ref
->node
,
946 &event
->enablers_ref_head
);
950 * Link filter bytecodes if not linked yet.
952 lttng_enabler_event_link_bytecode(event
, enabler
);
954 /* TODO: merge event context. */
961 * Called at library load: connect the probe on all enablers matching
963 * Called with session mutex held.
965 int lttng_fix_pending_events(void)
967 struct lttng_session
*session
;
969 cds_list_for_each_entry(session
, &sessions
, node
) {
970 lttng_session_lazy_sync_enablers(session
);
976 * For each session of the owner thread, execute pending statedump.
977 * Only dump state for the sessions owned by the caller thread, because
978 * we don't keep ust_lock across the entire iteration.
980 void lttng_handle_pending_statedump(void *owner
)
982 struct lttng_session
*session
;
984 /* Execute state dump */
985 do_lttng_ust_statedump(owner
);
987 /* Clear pending state dump */
991 cds_list_for_each_entry(session
, &sessions
, node
) {
992 if (session
->owner
!= owner
)
994 if (!session
->statedump_pending
)
996 session
->statedump_pending
= 0;
1004 * Only used internally at session destruction.
1007 void _lttng_event_destroy(struct lttng_event
*event
)
1009 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
1011 /* Remove from event list. */
1012 cds_list_del(&event
->node
);
1013 /* Remove from event hash table. */
1014 cds_hlist_del(&event
->hlist
);
1016 lttng_destroy_context(event
->ctx
);
1017 lttng_free_event_filter_runtime(event
);
1018 /* Free event enabler refs */
1019 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
1020 &event
->enablers_ref_head
, node
)
1026 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1028 cds_list_del(&_enum
->node
);
1029 cds_hlist_del(&_enum
->hlist
);
1033 void lttng_ust_events_exit(void)
1035 struct lttng_session
*session
, *tmpsession
;
1037 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
1038 lttng_session_destroy(session
);
1042 * Enabler management.
1044 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1045 struct lttng_ust_event
*event_param
,
1046 struct lttng_channel
*chan
)
1048 struct lttng_enabler
*enabler
;
1050 enabler
= zmalloc(sizeof(*enabler
));
1053 enabler
->type
= type
;
1054 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1055 CDS_INIT_LIST_HEAD(&enabler
->excluder_head
);
1056 memcpy(&enabler
->event_param
, event_param
,
1057 sizeof(enabler
->event_param
));
1058 enabler
->chan
= chan
;
1060 enabler
->enabled
= 0;
1061 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1062 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1066 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1068 enabler
->enabled
= 1;
1069 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1073 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1075 enabler
->enabled
= 0;
1076 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1080 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1081 struct lttng_ust_filter_bytecode_node
*bytecode
)
1083 bytecode
->enabler
= enabler
;
1084 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
1085 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1089 int lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1090 struct lttng_ust_excluder_node
*excluder
)
1092 excluder
->enabler
= enabler
;
1093 cds_list_add_tail(&excluder
->node
, &enabler
->excluder_head
);
1094 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1098 int lttng_attach_context(struct lttng_ust_context
*context_param
,
1099 union ust_args
*uargs
,
1100 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
1103 * We cannot attach a context after trace has been started for a
1104 * session because the metadata does not allow expressing this
1105 * information outside of the original channel scope.
1107 if (session
->been_active
)
1110 switch (context_param
->ctx
) {
1111 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
1112 return lttng_add_pthread_id_to_ctx(ctx
);
1113 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
1115 struct lttng_ust_perf_counter_ctx
*perf_ctx_param
;
1117 perf_ctx_param
= &context_param
->u
.perf_counter
;
1118 return lttng_add_perf_counter_to_ctx(
1119 perf_ctx_param
->type
,
1120 perf_ctx_param
->config
,
1121 perf_ctx_param
->name
,
1124 case LTTNG_UST_CONTEXT_VTID
:
1125 return lttng_add_vtid_to_ctx(ctx
);
1126 case LTTNG_UST_CONTEXT_VPID
:
1127 return lttng_add_vpid_to_ctx(ctx
);
1128 case LTTNG_UST_CONTEXT_PROCNAME
:
1129 return lttng_add_procname_to_ctx(ctx
);
1130 case LTTNG_UST_CONTEXT_IP
:
1131 return lttng_add_ip_to_ctx(ctx
);
1132 case LTTNG_UST_CONTEXT_CPU_ID
:
1133 return lttng_add_cpu_id_to_ctx(ctx
);
1134 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1135 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1137 case LTTNG_UST_CONTEXT_CGROUP_NS
:
1138 return lttng_add_cgroup_ns_to_ctx(ctx
);
1139 case LTTNG_UST_CONTEXT_IPC_NS
:
1140 return lttng_add_ipc_ns_to_ctx(ctx
);
1141 case LTTNG_UST_CONTEXT_MNT_NS
:
1142 return lttng_add_mnt_ns_to_ctx(ctx
);
1143 case LTTNG_UST_CONTEXT_NET_NS
:
1144 return lttng_add_net_ns_to_ctx(ctx
);
1145 case LTTNG_UST_CONTEXT_PID_NS
:
1146 return lttng_add_pid_ns_to_ctx(ctx
);
1147 case LTTNG_UST_CONTEXT_TIME_NS
:
1148 return lttng_add_time_ns_to_ctx(ctx
);
1149 case LTTNG_UST_CONTEXT_USER_NS
:
1150 return lttng_add_user_ns_to_ctx(ctx
);
1151 case LTTNG_UST_CONTEXT_UTS_NS
:
1152 return lttng_add_uts_ns_to_ctx(ctx
);
1153 case LTTNG_UST_CONTEXT_VUID
:
1154 return lttng_add_vuid_to_ctx(ctx
);
1155 case LTTNG_UST_CONTEXT_VEUID
:
1156 return lttng_add_veuid_to_ctx(ctx
);
1157 case LTTNG_UST_CONTEXT_VSUID
:
1158 return lttng_add_vsuid_to_ctx(ctx
);
1159 case LTTNG_UST_CONTEXT_VGID
:
1160 return lttng_add_vgid_to_ctx(ctx
);
1161 case LTTNG_UST_CONTEXT_VEGID
:
1162 return lttng_add_vegid_to_ctx(ctx
);
1163 case LTTNG_UST_CONTEXT_VSGID
:
1164 return lttng_add_vsgid_to_ctx(ctx
);
1170 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1171 struct lttng_ust_context
*context_param
)
1173 #if 0 // disabled for now.
1174 struct lttng_session
*session
= enabler
->chan
->session
;
1177 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
1181 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1187 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1189 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1190 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
1192 /* Destroy filter bytecode */
1193 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1194 &enabler
->filter_bytecode_head
, node
) {
1198 /* Destroy excluders */
1199 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
1200 &enabler
->excluder_head
, node
) {
1201 free(excluder_node
);
1204 /* Destroy contexts */
1205 lttng_destroy_context(enabler
->ctx
);
1207 cds_list_del(&enabler
->node
);
1212 * lttng_session_sync_enablers should be called just before starting a
1216 void lttng_session_sync_enablers(struct lttng_session
*session
)
1218 struct lttng_enabler
*enabler
;
1219 struct lttng_event
*event
;
1221 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1222 lttng_enabler_ref_events(enabler
);
1224 * For each event, if at least one of its enablers is enabled,
1225 * and its channel and session transient states are enabled, we
1226 * enable the event, else we disable it.
1228 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1229 struct lttng_enabler_ref
*enabler_ref
;
1230 struct lttng_bytecode_runtime
*runtime
;
1231 int enabled
= 0, has_enablers_without_bytecode
= 0;
1234 cds_list_for_each_entry(enabler_ref
,
1235 &event
->enablers_ref_head
, node
) {
1236 if (enabler_ref
->ref
->enabled
) {
1242 * Enabled state is based on union of enablers, with
1243 * intesection of session and channel transient enable
1246 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1248 CMM_STORE_SHARED(event
->enabled
, enabled
);
1250 * Sync tracepoint registration with event enabled
1254 if (!event
->registered
)
1255 register_event(event
);
1257 if (event
->registered
)
1258 unregister_event(event
);
1261 /* Check if has enablers without bytecode enabled */
1262 cds_list_for_each_entry(enabler_ref
,
1263 &event
->enablers_ref_head
, node
) {
1264 if (enabler_ref
->ref
->enabled
1265 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1266 has_enablers_without_bytecode
= 1;
1270 event
->has_enablers_without_bytecode
=
1271 has_enablers_without_bytecode
;
1273 /* Enable filters */
1274 cds_list_for_each_entry(runtime
,
1275 &event
->bytecode_runtime_head
, node
) {
1276 lttng_filter_sync_state(runtime
);
1279 __tracepoint_probe_prune_release_queue();
1283 * Apply enablers to session events, adding events to session if need
1284 * be. It is required after each modification applied to an active
1285 * session, and right before session "start".
1286 * "lazy" sync means we only sync if required.
1289 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1291 /* We can skip if session is not active */
1292 if (!session
->active
)
1294 lttng_session_sync_enablers(session
);
1298 * Update all sessions with the given app context.
1299 * Called with ust lock held.
1300 * This is invoked when an application context gets loaded/unloaded. It
1301 * ensures the context callbacks are in sync with the application
1302 * context (either app context callbacks, or dummy callbacks).
1304 void lttng_ust_context_set_session_provider(const char *name
,
1305 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
1306 void (*record
)(struct lttng_ctx_field
*field
,
1307 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
1308 struct lttng_channel
*chan
),
1309 void (*get_value
)(struct lttng_ctx_field
*field
,
1310 struct lttng_ctx_value
*value
))
1312 struct lttng_session
*session
;
1314 cds_list_for_each_entry(session
, &sessions
, node
) {
1315 struct lttng_channel
*chan
;
1316 struct lttng_event
*event
;
1319 ret
= lttng_ust_context_set_provider_rcu(&session
->ctx
,
1320 name
, get_size
, record
, get_value
);
1323 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
1324 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
1325 name
, get_size
, record
, get_value
);
1329 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1330 ret
= lttng_ust_context_set_provider_rcu(&event
->ctx
,
1331 name
, get_size
, record
, get_value
);