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
26 #include <urcu/list.h>
27 #include <urcu/hlist.h>
37 #include <lttng/ust-endian.h>
41 #include <urcu/compiler.h>
42 #include <urcu/uatomic.h>
43 #include <urcu/arch.h>
45 #include <lttng/tracepoint.h>
46 #include <lttng/ust-events.h>
48 #include <usterr-signal-safe.h>
50 #include <lttng/ust-ctl.h>
52 #include <lttng/ust-dynamic-type.h>
53 #include <lttng/ust-context-provider.h>
56 #include "lttng-ust-uuid.h"
58 #include "tracepoint-internal.h"
59 #include "string-utils.h"
60 #include "lttng-tracer.h"
61 #include "lttng-tracer-core.h"
62 #include "lttng-ust-statedump.h"
64 #include "../libringbuffer/shm.h"
68 * All operations within this file are called by the communication
69 * thread, under ust_lock protection.
72 static CDS_LIST_HEAD(sessions
);
74 struct cds_list_head
*_lttng_get_sessions(void)
79 static void _lttng_event_destroy(struct lttng_event
*event
);
80 static void _lttng_enum_destroy(struct lttng_enum
*_enum
);
83 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
85 void lttng_session_sync_enablers(struct lttng_session
*session
);
87 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
90 * Called with ust lock held.
92 int lttng_session_active(void)
94 struct lttng_session
*iter
;
96 cds_list_for_each_entry(iter
, &sessions
, node
) {
104 int lttng_loglevel_match(int loglevel
,
105 unsigned int has_loglevel
,
106 enum lttng_ust_loglevel_type req_type
,
110 loglevel
= TRACE_DEFAULT
;
112 case LTTNG_UST_LOGLEVEL_RANGE
:
113 if (loglevel
<= req_loglevel
114 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
118 case LTTNG_UST_LOGLEVEL_SINGLE
:
119 if (loglevel
== req_loglevel
120 || (req_loglevel
== -1 && loglevel
<= TRACE_DEBUG
))
124 case LTTNG_UST_LOGLEVEL_ALL
:
126 if (loglevel
<= TRACE_DEBUG
)
133 void synchronize_trace(void)
138 struct lttng_session
*lttng_session_create(void)
140 struct lttng_session
*session
;
143 session
= zmalloc(sizeof(struct lttng_session
));
146 if (lttng_session_context_init(&session
->ctx
)) {
150 CDS_INIT_LIST_HEAD(&session
->chan_head
);
151 CDS_INIT_LIST_HEAD(&session
->events_head
);
152 CDS_INIT_LIST_HEAD(&session
->enums_head
);
153 CDS_INIT_LIST_HEAD(&session
->enablers_head
);
154 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
155 CDS_INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
156 for (i
= 0; i
< LTTNG_UST_ENUM_HT_SIZE
; i
++)
157 CDS_INIT_HLIST_HEAD(&session
->enums_ht
.table
[i
]);
158 cds_list_add(&session
->node
, &sessions
);
163 * Only used internally at session destruction.
166 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
168 struct channel
*chan
;
169 struct lttng_ust_shm_handle
*handle
;
171 cds_list_del(<tng_chan
->node
);
172 lttng_destroy_context(lttng_chan
->ctx
);
173 chan
= lttng_chan
->chan
;
174 handle
= lttng_chan
->handle
;
176 * note: lttng_chan is private data contained within handle. It
177 * will be freed along with the handle.
179 channel_destroy(chan
, handle
, 0);
183 void register_event(struct lttng_event
*event
)
186 const struct lttng_event_desc
*desc
;
188 assert(event
->registered
== 0);
190 ret
= __tracepoint_probe_register_queue_release(desc
->name
,
191 desc
->probe_callback
,
192 event
, desc
->signature
);
195 event
->registered
= 1;
199 void unregister_event(struct lttng_event
*event
)
202 const struct lttng_event_desc
*desc
;
204 assert(event
->registered
== 1);
206 ret
= __tracepoint_probe_unregister_queue_release(desc
->name
,
207 desc
->probe_callback
,
211 event
->registered
= 0;
215 * Only used internally at session destruction.
218 void _lttng_event_unregister(struct lttng_event
*event
)
220 if (event
->registered
)
221 unregister_event(event
);
224 void lttng_session_destroy(struct lttng_session
*session
)
226 struct lttng_channel
*chan
, *tmpchan
;
227 struct lttng_event
*event
, *tmpevent
;
228 struct lttng_enum
*_enum
, *tmp_enum
;
229 struct lttng_enabler
*enabler
, *tmpenabler
;
231 CMM_ACCESS_ONCE(session
->active
) = 0;
232 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
233 _lttng_event_unregister(event
);
235 synchronize_trace(); /* Wait for in-flight events to complete */
236 __tracepoint_probe_prune_release_queue();
237 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
238 &session
->enablers_head
, node
)
239 lttng_enabler_destroy(enabler
);
240 cds_list_for_each_entry_safe(event
, tmpevent
,
241 &session
->events_head
, node
)
242 _lttng_event_destroy(event
);
243 cds_list_for_each_entry_safe(_enum
, tmp_enum
,
244 &session
->enums_head
, node
)
245 _lttng_enum_destroy(_enum
);
246 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
247 _lttng_channel_unmap(chan
);
248 cds_list_del(&session
->node
);
249 lttng_destroy_context(session
->ctx
);
254 int lttng_enum_create(const struct lttng_enum_desc
*desc
,
255 struct lttng_session
*session
)
257 const char *enum_name
= desc
->name
;
258 struct lttng_enum
*_enum
;
259 struct cds_hlist_head
*head
;
261 size_t name_len
= strlen(enum_name
);
265 /* Check if this enum is already registered for this session. */
266 hash
= jhash(enum_name
, name_len
, 0);
267 head
= &session
->enums_ht
.table
[hash
& (LTTNG_UST_ENUM_HT_SIZE
- 1)];
269 _enum
= lttng_ust_enum_get_from_desc(session
, desc
);
275 notify_socket
= lttng_get_notify_socket(session
->owner
);
276 if (notify_socket
< 0) {
281 _enum
= zmalloc(sizeof(*_enum
));
286 _enum
->session
= session
;
289 ret
= ustcomm_register_enum(notify_socket
,
296 DBG("Error (%d) registering enumeration to sessiond", ret
);
297 goto sessiond_register_error
;
299 cds_list_add(&_enum
->node
, &session
->enums_head
);
300 cds_hlist_add_head(&_enum
->hlist
, head
);
303 sessiond_register_error
:
312 int lttng_create_enum_check(const struct lttng_type
*type
,
313 struct lttng_session
*session
)
315 switch (type
->atype
) {
318 const struct lttng_enum_desc
*enum_desc
;
321 enum_desc
= type
->u
.basic
.enumeration
.desc
;
322 ret
= lttng_enum_create(enum_desc
, session
);
323 if (ret
&& ret
!= -EEXIST
) {
324 DBG("Unable to create enum error: (%d)", ret
);
331 const struct lttng_event_field
*tag_field_generic
;
332 const struct lttng_enum_desc
*enum_desc
;
335 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
336 enum_desc
= tag_field_generic
->type
.u
.basic
.enumeration
.desc
;
337 ret
= lttng_enum_create(enum_desc
, session
);
338 if (ret
&& ret
!= -EEXIST
) {
339 DBG("Unable to create enum error: (%d)", ret
);
345 /* TODO: nested types when they become supported. */
352 int lttng_create_all_event_enums(size_t nr_fields
,
353 const struct lttng_event_field
*event_fields
,
354 struct lttng_session
*session
)
359 /* For each field, ensure enum is part of the session. */
360 for (i
= 0; i
< nr_fields
; i
++) {
361 const struct lttng_type
*type
= &event_fields
[i
].type
;
363 ret
= lttng_create_enum_check(type
, session
);
371 int lttng_create_all_ctx_enums(size_t nr_fields
,
372 const struct lttng_ctx_field
*ctx_fields
,
373 struct lttng_session
*session
)
378 /* For each field, ensure enum is part of the session. */
379 for (i
= 0; i
< nr_fields
; i
++) {
380 const struct lttng_type
*type
= &ctx_fields
[i
].event_field
.type
;
382 ret
= lttng_create_enum_check(type
, session
);
390 * Ensure that a state-dump will be performed for this session at the end
391 * of the current handle_message().
393 int lttng_session_statedump(struct lttng_session
*session
)
395 session
->statedump_pending
= 1;
396 lttng_ust_sockinfo_session_enabled(session
->owner
);
400 int lttng_session_enable(struct lttng_session
*session
)
403 struct lttng_channel
*chan
;
406 if (session
->active
) {
411 notify_socket
= lttng_get_notify_socket(session
->owner
);
412 if (notify_socket
< 0)
413 return notify_socket
;
415 /* Set transient enabler state to "enabled" */
419 * Snapshot the number of events per channel to know the type of header
422 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
423 const struct lttng_ctx
*ctx
;
424 const struct lttng_ctx_field
*fields
= NULL
;
425 size_t nr_fields
= 0;
428 /* don't change it if session stop/restart */
429 if (chan
->header_type
)
433 nr_fields
= ctx
->nr_fields
;
434 fields
= ctx
->fields
;
435 ret
= lttng_create_all_ctx_enums(nr_fields
, fields
,
438 DBG("Error (%d) adding enum to session", ret
);
442 ret
= ustcomm_register_channel(notify_socket
,
451 DBG("Error (%d) registering channel to sessiond", ret
);
454 if (chan_id
!= chan
->id
) {
455 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
461 /* We need to sync enablers with session before activation. */
462 lttng_session_sync_enablers(session
);
464 /* Set atomically the state to "active" */
465 CMM_ACCESS_ONCE(session
->active
) = 1;
466 CMM_ACCESS_ONCE(session
->been_active
) = 1;
468 ret
= lttng_session_statedump(session
);
475 int lttng_session_disable(struct lttng_session
*session
)
479 if (!session
->active
) {
483 /* Set atomically the state to "inactive" */
484 CMM_ACCESS_ONCE(session
->active
) = 0;
486 /* Set transient enabler state to "disabled" */
488 lttng_session_sync_enablers(session
);
493 int lttng_channel_enable(struct lttng_channel
*channel
)
497 if (channel
->enabled
) {
501 /* Set transient enabler state to "enabled" */
503 lttng_session_sync_enablers(channel
->session
);
504 /* Set atomically the state to "enabled" */
505 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
510 int lttng_channel_disable(struct lttng_channel
*channel
)
514 if (!channel
->enabled
) {
518 /* Set atomically the state to "disabled" */
519 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
520 /* Set transient enabler state to "enabled" */
522 lttng_session_sync_enablers(channel
->session
);
528 * Supports event creation while tracing session is active.
531 int lttng_event_create(const struct lttng_event_desc
*desc
,
532 struct lttng_channel
*chan
)
534 const char *event_name
= desc
->name
;
535 struct lttng_event
*event
;
536 struct lttng_session
*session
= chan
->session
;
537 struct cds_hlist_head
*head
;
539 size_t name_len
= strlen(event_name
);
541 int notify_socket
, loglevel
;
544 hash
= jhash(event_name
, name_len
, 0);
545 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
547 notify_socket
= lttng_get_notify_socket(session
->owner
);
548 if (notify_socket
< 0) {
553 ret
= lttng_create_all_event_enums(desc
->nr_fields
, desc
->fields
,
556 DBG("Error (%d) adding enum to session", ret
);
557 goto create_enum_error
;
561 * Check if loglevel match. Refuse to connect event if not.
563 event
= zmalloc(sizeof(struct lttng_event
));
570 /* Event will be enabled by enabler sync. */
572 event
->registered
= 0;
573 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
574 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
578 loglevel
= *(*event
->desc
->loglevel
);
580 loglevel
= TRACE_DEFAULT
;
581 if (desc
->u
.ext
.model_emf_uri
)
582 uri
= *(desc
->u
.ext
.model_emf_uri
);
586 /* Fetch event ID from sessiond */
587 ret
= ustcomm_register_event(notify_socket
,
599 DBG("Error (%d) registering event to sessiond", ret
);
600 goto sessiond_register_error
;
603 /* Populate lttng_event structure before tracepoint registration. */
605 cds_list_add(&event
->node
, &chan
->session
->events_head
);
606 cds_hlist_add_head(&event
->hlist
, head
);
609 sessiond_register_error
:
618 int lttng_desc_match_star_glob_enabler(const struct lttng_event_desc
*desc
,
619 struct lttng_enabler
*enabler
)
622 unsigned int has_loglevel
= 0;
624 assert(enabler
->type
== LTTNG_ENABLER_STAR_GLOB
);
625 if (!strutils_star_glob_match(enabler
->event_param
.name
, SIZE_MAX
,
626 desc
->name
, SIZE_MAX
))
628 if (desc
->loglevel
) {
629 loglevel
= *(*desc
->loglevel
);
632 if (!lttng_loglevel_match(loglevel
,
634 enabler
->event_param
.loglevel_type
,
635 enabler
->event_param
.loglevel
))
641 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
642 struct lttng_enabler
*enabler
)
645 unsigned int has_loglevel
= 0;
647 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
648 if (strcmp(desc
->name
, enabler
->event_param
.name
))
650 if (desc
->loglevel
) {
651 loglevel
= *(*desc
->loglevel
);
654 if (!lttng_loglevel_match(loglevel
,
656 enabler
->event_param
.loglevel_type
,
657 enabler
->event_param
.loglevel
))
663 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
664 struct lttng_enabler
*enabler
)
666 switch (enabler
->type
) {
667 case LTTNG_ENABLER_STAR_GLOB
:
669 struct lttng_ust_excluder_node
*excluder
;
671 if (!lttng_desc_match_star_glob_enabler(desc
, enabler
)) {
676 * If the matching event matches with an excluder,
677 * return 'does not match'
679 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
682 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
686 excluder_name
= (char *) (excluder
->excluder
.names
)
687 + count
* LTTNG_UST_SYM_NAME_LEN
;
688 len
= strnlen(excluder_name
, LTTNG_UST_SYM_NAME_LEN
);
689 if (len
> 0 && strutils_star_glob_match(excluder_name
, len
, desc
->name
, SIZE_MAX
))
695 case LTTNG_ENABLER_EVENT
:
696 return lttng_desc_match_event_enabler(desc
, enabler
);
703 int lttng_event_match_enabler(struct lttng_event
*event
,
704 struct lttng_enabler
*enabler
)
706 if (lttng_desc_match_enabler(event
->desc
, enabler
)
707 && event
->chan
== enabler
->chan
)
714 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
715 struct lttng_enabler
*enabler
)
717 struct lttng_enabler_ref
*enabler_ref
;
719 cds_list_for_each_entry(enabler_ref
,
720 &event
->enablers_ref_head
, node
) {
721 if (enabler_ref
->ref
== enabler
)
728 * Create struct lttng_event if it is missing and present in the list of
732 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
734 struct lttng_session
*session
= enabler
->chan
->session
;
735 struct lttng_probe_desc
*probe_desc
;
736 const struct lttng_event_desc
*desc
;
737 struct lttng_event
*event
;
739 struct cds_list_head
*probe_list
;
741 probe_list
= lttng_get_probe_list_head();
743 * For each probe event, if we find that a probe event matches
744 * our enabler, create an associated lttng_event if not
747 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
748 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
750 struct cds_hlist_head
*head
;
751 struct cds_hlist_node
*node
;
752 const char *event_name
;
756 desc
= probe_desc
->event_desc
[i
];
757 if (!lttng_desc_match_enabler(desc
, enabler
))
759 event_name
= desc
->name
;
760 name_len
= strlen(event_name
);
763 * Check if already created.
765 hash
= jhash(event_name
, name_len
, 0);
766 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
767 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
768 if (event
->desc
== desc
769 && event
->chan
== enabler
->chan
)
776 * We need to create an event for this
779 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
782 DBG("Unable to create event %s, error %d\n",
783 probe_desc
->event_desc
[i
]->name
, ret
);
790 * Iterate over all the UST sessions to unregister and destroy all probes from
791 * the probe provider descriptor received as argument. Must me called with the
794 void lttng_probe_provider_unregister_events(struct lttng_probe_desc
*provider_desc
)
796 struct cds_hlist_node
*node
, *tmp_node
;
797 struct cds_list_head
*sessionsp
;
798 struct lttng_session
*session
;
799 struct cds_hlist_head
*head
;
800 struct lttng_event
*event
;
803 /* Get handle on list of sessions. */
804 sessionsp
= _lttng_get_sessions();
807 * Iterate over all events in the probe provider descriptions and sessions
808 * to queue the unregistration of the events.
810 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
811 const struct lttng_event_desc
*event_desc
;
812 const char *event_name
;
816 event_desc
= provider_desc
->event_desc
[i
];
817 event_name
= event_desc
->name
;
818 name_len
= strlen(event_name
);
819 hash
= jhash(event_name
, name_len
, 0);
821 /* Iterate over all session to find the current event description. */
822 cds_list_for_each_entry(session
, sessionsp
, node
) {
824 * Get the list of events in the hashtable bucket and iterate to
825 * find the event matching this descriptor.
827 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
828 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
829 if (event_desc
== event
->desc
) {
830 /* Queue the unregistration of this event. */
831 _lttng_event_unregister(event
);
838 /* Wait for grace period. */
840 /* Prune the unregistration queue. */
841 __tracepoint_probe_prune_release_queue();
844 * It is now safe to destroy the events and remove them from the event list
847 for (i
= 0; i
< provider_desc
->nr_events
; i
++) {
848 const struct lttng_event_desc
*event_desc
;
849 const char *event_name
;
853 event_desc
= provider_desc
->event_desc
[i
];
854 event_name
= event_desc
->name
;
855 name_len
= strlen(event_name
);
856 hash
= jhash(event_name
, name_len
, 0);
858 /* Iterate over all sessions to find the current event description. */
859 cds_list_for_each_entry(session
, sessionsp
, node
) {
861 * Get the list of events in the hashtable bucket and iterate to
862 * find the event matching this descriptor.
864 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
865 cds_hlist_for_each_entry_safe(event
, node
, tmp_node
, head
, hlist
) {
866 if (event_desc
== event
->desc
) {
867 /* Destroy enums of the current event. */
868 for (j
= 0; j
< event
->desc
->nr_fields
; j
++) {
869 const struct lttng_enum_desc
*enum_desc
;
870 const struct lttng_event_field
*field
;
871 struct lttng_enum
*curr_enum
;
873 field
= &(event
->desc
->fields
[j
]);
874 if (field
->type
.atype
!= atype_enum
) {
878 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
879 curr_enum
= lttng_ust_enum_get_from_desc(session
, enum_desc
);
881 _lttng_enum_destroy(curr_enum
);
886 _lttng_event_destroy(event
);
895 * Create events associated with an enabler (if not already present),
896 * and add backward reference from the event to the enabler.
899 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
901 struct lttng_session
*session
= enabler
->chan
->session
;
902 struct lttng_event
*event
;
904 /* First ensure that probe events are created for this enabler. */
905 lttng_create_event_if_missing(enabler
);
907 /* For each event matching enabler in session event list. */
908 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
909 struct lttng_enabler_ref
*enabler_ref
;
911 if (!lttng_event_match_enabler(event
, enabler
))
914 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
917 * If no backward ref, create it.
918 * Add backward ref from event to enabler.
920 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
923 enabler_ref
->ref
= enabler
;
924 cds_list_add(&enabler_ref
->node
,
925 &event
->enablers_ref_head
);
929 * Link filter bytecodes if not linked yet.
931 lttng_enabler_event_link_bytecode(event
, enabler
);
933 /* TODO: merge event context. */
939 * Called at library load: connect the probe on all enablers matching
941 * Called with session mutex held.
943 int lttng_fix_pending_events(void)
945 struct lttng_session
*session
;
947 cds_list_for_each_entry(session
, &sessions
, node
) {
948 lttng_session_lazy_sync_enablers(session
);
954 * For each session of the owner thread, execute pending statedump.
955 * Only dump state for the sessions owned by the caller thread, because
956 * we don't keep ust_lock across the entire iteration.
958 void lttng_handle_pending_statedump(void *owner
)
960 struct lttng_session
*session
;
962 /* Execute state dump */
963 do_lttng_ust_statedump(owner
);
965 /* Clear pending state dump */
969 cds_list_for_each_entry(session
, &sessions
, node
) {
970 if (session
->owner
!= owner
)
972 if (!session
->statedump_pending
)
974 session
->statedump_pending
= 0;
982 * Only used internally at session destruction.
985 void _lttng_event_destroy(struct lttng_event
*event
)
987 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
989 /* Remove from event list. */
990 cds_list_del(&event
->node
);
991 /* Remove from event hash table. */
992 cds_hlist_del(&event
->hlist
);
994 lttng_destroy_context(event
->ctx
);
995 lttng_free_event_filter_runtime(event
);
996 /* Free event enabler refs */
997 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
998 &event
->enablers_ref_head
, node
)
1004 void _lttng_enum_destroy(struct lttng_enum
*_enum
)
1006 cds_list_del(&_enum
->node
);
1007 cds_hlist_del(&_enum
->hlist
);
1011 void lttng_ust_events_exit(void)
1013 struct lttng_session
*session
, *tmpsession
;
1015 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
1016 lttng_session_destroy(session
);
1020 * Enabler management.
1022 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1023 struct lttng_ust_event
*event_param
,
1024 struct lttng_channel
*chan
)
1026 struct lttng_enabler
*enabler
;
1028 enabler
= zmalloc(sizeof(*enabler
));
1031 enabler
->type
= type
;
1032 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1033 CDS_INIT_LIST_HEAD(&enabler
->excluder_head
);
1034 memcpy(&enabler
->event_param
, event_param
,
1035 sizeof(enabler
->event_param
));
1036 enabler
->chan
= chan
;
1038 enabler
->enabled
= 0;
1039 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1040 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1044 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1046 enabler
->enabled
= 1;
1047 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1051 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1053 enabler
->enabled
= 0;
1054 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1058 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1059 struct lttng_ust_filter_bytecode_node
*bytecode
)
1061 bytecode
->enabler
= enabler
;
1062 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
1063 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1067 int lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
1068 struct lttng_ust_excluder_node
*excluder
)
1070 excluder
->enabler
= enabler
;
1071 cds_list_add_tail(&excluder
->node
, &enabler
->excluder_head
);
1072 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1076 int lttng_attach_context(struct lttng_ust_context
*context_param
,
1077 union ust_args
*uargs
,
1078 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
1081 * We cannot attach a context after trace has been started for a
1082 * session because the metadata does not allow expressing this
1083 * information outside of the original channel scope.
1085 if (session
->been_active
)
1088 switch (context_param
->ctx
) {
1089 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
1090 return lttng_add_pthread_id_to_ctx(ctx
);
1091 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
1093 struct lttng_ust_perf_counter_ctx
*perf_ctx_param
;
1095 perf_ctx_param
= &context_param
->u
.perf_counter
;
1096 return lttng_add_perf_counter_to_ctx(
1097 perf_ctx_param
->type
,
1098 perf_ctx_param
->config
,
1099 perf_ctx_param
->name
,
1102 case LTTNG_UST_CONTEXT_VTID
:
1103 return lttng_add_vtid_to_ctx(ctx
);
1104 case LTTNG_UST_CONTEXT_VPID
:
1105 return lttng_add_vpid_to_ctx(ctx
);
1106 case LTTNG_UST_CONTEXT_PROCNAME
:
1107 return lttng_add_procname_to_ctx(ctx
);
1108 case LTTNG_UST_CONTEXT_IP
:
1109 return lttng_add_ip_to_ctx(ctx
);
1110 case LTTNG_UST_CONTEXT_CPU_ID
:
1111 return lttng_add_cpu_id_to_ctx(ctx
);
1112 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1113 return lttng_ust_add_app_context_to_ctx_rcu(uargs
->app_context
.ctxname
,
1120 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1121 struct lttng_ust_context
*context_param
)
1123 #if 0 // disabled for now.
1124 struct lttng_session
*session
= enabler
->chan
->session
;
1127 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
1131 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1137 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1139 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1140 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
1142 /* Destroy filter bytecode */
1143 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1144 &enabler
->filter_bytecode_head
, node
) {
1148 /* Destroy excluders */
1149 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
1150 &enabler
->excluder_head
, node
) {
1151 free(excluder_node
);
1154 /* Destroy contexts */
1155 lttng_destroy_context(enabler
->ctx
);
1157 cds_list_del(&enabler
->node
);
1162 * lttng_session_sync_enablers should be called just before starting a
1166 void lttng_session_sync_enablers(struct lttng_session
*session
)
1168 struct lttng_enabler
*enabler
;
1169 struct lttng_event
*event
;
1171 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1172 lttng_enabler_ref_events(enabler
);
1174 * For each event, if at least one of its enablers is enabled,
1175 * and its channel and session transient states are enabled, we
1176 * enable the event, else we disable it.
1178 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1179 struct lttng_enabler_ref
*enabler_ref
;
1180 struct lttng_bytecode_runtime
*runtime
;
1181 int enabled
= 0, has_enablers_without_bytecode
= 0;
1184 cds_list_for_each_entry(enabler_ref
,
1185 &event
->enablers_ref_head
, node
) {
1186 if (enabler_ref
->ref
->enabled
) {
1192 * Enabled state is based on union of enablers, with
1193 * intesection of session and channel transient enable
1196 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1198 CMM_STORE_SHARED(event
->enabled
, enabled
);
1200 * Sync tracepoint registration with event enabled
1204 if (!event
->registered
)
1205 register_event(event
);
1207 if (event
->registered
)
1208 unregister_event(event
);
1211 /* Check if has enablers without bytecode enabled */
1212 cds_list_for_each_entry(enabler_ref
,
1213 &event
->enablers_ref_head
, node
) {
1214 if (enabler_ref
->ref
->enabled
1215 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1216 has_enablers_without_bytecode
= 1;
1220 event
->has_enablers_without_bytecode
=
1221 has_enablers_without_bytecode
;
1223 /* Enable filters */
1224 cds_list_for_each_entry(runtime
,
1225 &event
->bytecode_runtime_head
, node
) {
1226 lttng_filter_sync_state(runtime
);
1229 __tracepoint_probe_prune_release_queue();
1233 * Apply enablers to session events, adding events to session if need
1234 * be. It is required after each modification applied to an active
1235 * session, and right before session "start".
1236 * "lazy" sync means we only sync if required.
1239 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1241 /* We can skip if session is not active */
1242 if (!session
->active
)
1244 lttng_session_sync_enablers(session
);
1248 * Update all sessions with the given app context.
1249 * Called with ust lock held.
1250 * This is invoked when an application context gets loaded/unloaded. It
1251 * ensures the context callbacks are in sync with the application
1252 * context (either app context callbacks, or dummy callbacks).
1254 void lttng_ust_context_set_session_provider(const char *name
,
1255 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
1256 void (*record
)(struct lttng_ctx_field
*field
,
1257 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
1258 struct lttng_channel
*chan
),
1259 void (*get_value
)(struct lttng_ctx_field
*field
,
1260 struct lttng_ctx_value
*value
))
1262 struct lttng_session
*session
;
1264 cds_list_for_each_entry(session
, &sessions
, node
) {
1265 struct lttng_channel
*chan
;
1266 struct lttng_event
*event
;
1269 ret
= lttng_ust_context_set_provider_rcu(&session
->ctx
,
1270 name
, get_size
, record
, get_value
);
1273 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
1274 ret
= lttng_ust_context_set_provider_rcu(&chan
->ctx
,
1275 name
, get_size
, record
, get_value
);
1279 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1280 ret
= lttng_ust_context_set_provider_rcu(&event
->ctx
,
1281 name
, get_size
, record
, get_value
);