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>
35 #include <lttng/ust-endian.h>
39 #include <urcu/compiler.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/arch.h>
43 #include <lttng/tracepoint.h>
44 #include <lttng/ust-events.h>
46 #include <usterr-signal-safe.h>
48 #include <lttng/ust-ctl.h>
52 #include "lttng-ust-uuid.h"
54 #include "tracepoint-internal.h"
55 #include "lttng-tracer.h"
56 #include "lttng-tracer-core.h"
57 #include "lttng-ust-baddr.h"
59 #include "../libringbuffer/shm.h"
63 * The sessions mutex is the centralized mutex across UST tracing
64 * control and probe registration. All operations within this file are
65 * called by the communication thread, under ust_lock protection.
67 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
71 pthread_mutex_lock(&sessions_mutex
);
76 pthread_mutex_unlock(&sessions_mutex
);
79 static CDS_LIST_HEAD(sessions
);
81 struct cds_list_head
*_lttng_get_sessions(void)
86 static void _lttng_event_destroy(struct lttng_event
*event
);
89 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
91 void lttng_session_sync_enablers(struct lttng_session
*session
);
93 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
96 * Called with ust lock held.
98 int lttng_session_active(void)
100 struct lttng_session
*iter
;
102 cds_list_for_each_entry(iter
, &sessions
, node
) {
110 int lttng_loglevel_match(int loglevel
,
111 unsigned int has_loglevel
,
112 enum lttng_ust_loglevel_type req_type
,
115 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
118 loglevel
= TRACE_DEFAULT
;
120 case LTTNG_UST_LOGLEVEL_RANGE
:
121 if (loglevel
<= req_loglevel
|| req_loglevel
== -1)
125 case LTTNG_UST_LOGLEVEL_SINGLE
:
126 if (loglevel
== req_loglevel
|| req_loglevel
== -1)
130 case LTTNG_UST_LOGLEVEL_ALL
:
136 void synchronize_trace(void)
141 struct lttng_session
*lttng_session_create(void)
143 struct lttng_session
*session
;
146 session
= zmalloc(sizeof(struct lttng_session
));
149 CDS_INIT_LIST_HEAD(&session
->chan_head
);
150 CDS_INIT_LIST_HEAD(&session
->events_head
);
151 CDS_INIT_LIST_HEAD(&session
->enablers_head
);
152 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
153 CDS_INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
154 cds_list_add(&session
->node
, &sessions
);
159 * Only used internally at session destruction.
162 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
164 struct channel
*chan
;
165 struct lttng_ust_shm_handle
*handle
;
167 cds_list_del(<tng_chan
->node
);
168 lttng_destroy_context(lttng_chan
->ctx
);
169 chan
= lttng_chan
->chan
;
170 handle
= lttng_chan
->handle
;
172 * note: lttng_chan is private data contained within handle. It
173 * will be freed along with the handle.
175 channel_destroy(chan
, handle
, 0);
179 void register_event(struct lttng_event
*event
)
182 const struct lttng_event_desc
*desc
;
184 assert(event
->registered
== 0);
186 ret
= __tracepoint_probe_register(desc
->name
,
187 desc
->probe_callback
,
188 event
, desc
->signature
);
191 event
->registered
= 1;
195 void unregister_event(struct lttng_event
*event
)
198 const struct lttng_event_desc
*desc
;
200 assert(event
->registered
== 1);
202 ret
= __tracepoint_probe_unregister(desc
->name
,
203 desc
->probe_callback
,
207 event
->registered
= 0;
211 * Only used internally at session destruction.
214 void _lttng_event_unregister(struct lttng_event
*event
)
216 if (event
->registered
)
217 unregister_event(event
);
220 void lttng_session_destroy(struct lttng_session
*session
)
222 struct lttng_channel
*chan
, *tmpchan
;
223 struct lttng_event
*event
, *tmpevent
;
224 struct lttng_enabler
*enabler
, *tmpenabler
;
226 CMM_ACCESS_ONCE(session
->active
) = 0;
227 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
228 _lttng_event_unregister(event
);
230 synchronize_trace(); /* Wait for in-flight events to complete */
231 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
232 &session
->enablers_head
, node
)
233 lttng_enabler_destroy(enabler
);
234 cds_list_for_each_entry_safe(event
, tmpevent
,
235 &session
->events_head
, node
)
236 _lttng_event_destroy(event
);
237 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
238 _lttng_channel_unmap(chan
);
239 cds_list_del(&session
->node
);
243 int lttng_session_enable(struct lttng_session
*session
)
246 struct lttng_channel
*chan
;
249 if (session
->active
) {
254 notify_socket
= lttng_get_notify_socket(session
->owner
);
255 if (notify_socket
< 0)
256 return notify_socket
;
258 /* Set transient enabler state to "enabled" */
260 /* We need to sync enablers with session before activation. */
261 lttng_session_sync_enablers(session
);
264 * Snapshot the number of events per channel to know the type of header
267 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
268 const struct lttng_ctx
*ctx
;
269 const struct lttng_ctx_field
*fields
= NULL
;
270 size_t nr_fields
= 0;
273 /* don't change it if session stop/restart */
274 if (chan
->header_type
)
278 nr_fields
= ctx
->nr_fields
;
279 fields
= ctx
->fields
;
281 ret
= ustcomm_register_channel(notify_socket
,
289 DBG("Error (%d) registering channel to sessiond", ret
);
292 if (chan_id
!= chan
->id
) {
293 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
299 /* Set atomically the state to "active" */
300 CMM_ACCESS_ONCE(session
->active
) = 1;
301 CMM_ACCESS_ONCE(session
->been_active
) = 1;
303 session
->statedump_pending
= 1;
304 lttng_ust_sockinfo_session_enabled(session
->owner
);
309 int lttng_session_disable(struct lttng_session
*session
)
313 if (!session
->active
) {
317 /* Set atomically the state to "inactive" */
318 CMM_ACCESS_ONCE(session
->active
) = 0;
320 /* Set transient enabler state to "disabled" */
322 lttng_session_sync_enablers(session
);
327 int lttng_channel_enable(struct lttng_channel
*channel
)
331 if (channel
->enabled
) {
335 /* Set transient enabler state to "enabled" */
337 lttng_session_sync_enablers(channel
->session
);
338 /* Set atomically the state to "enabled" */
339 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
344 int lttng_channel_disable(struct lttng_channel
*channel
)
348 if (!channel
->enabled
) {
352 /* Set atomically the state to "disabled" */
353 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
354 /* Set transient enabler state to "enabled" */
356 lttng_session_sync_enablers(channel
->session
);
362 * Supports event creation while tracing session is active.
365 int lttng_event_create(const struct lttng_event_desc
*desc
,
366 struct lttng_channel
*chan
)
368 const char *event_name
= desc
->name
;
369 struct lttng_event
*event
;
370 struct lttng_session
*session
= chan
->session
;
371 struct cds_hlist_head
*head
;
372 struct cds_hlist_node
*node
;
374 size_t name_len
= strlen(event_name
);
376 int notify_socket
, loglevel
;
379 hash
= jhash(event_name
, name_len
, 0);
380 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
381 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
383 if (!strncmp(event
->desc
->name
, desc
->name
,
384 LTTNG_UST_SYM_NAME_LEN
- 1)
385 && chan
== event
->chan
) {
391 notify_socket
= lttng_get_notify_socket(session
->owner
);
392 if (notify_socket
< 0) {
398 * Check if loglevel match. Refuse to connect event if not.
400 event
= zmalloc(sizeof(struct lttng_event
));
407 /* Event will be enabled by enabler sync. */
409 event
->registered
= 0;
410 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
411 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
415 loglevel
= *(*event
->desc
->loglevel
);
417 loglevel
= TRACE_DEFAULT
;
418 if (desc
->u
.ext
.model_emf_uri
)
419 uri
= *(desc
->u
.ext
.model_emf_uri
);
423 /* Fetch event ID from sessiond */
424 ret
= ustcomm_register_event(notify_socket
,
435 DBG("Error (%d) registering event to sessiond", ret
);
436 goto sessiond_register_error
;
439 /* Populate lttng_event structure before tracepoint registration. */
441 cds_list_add(&event
->node
, &chan
->session
->events_head
);
442 cds_hlist_add_head(&event
->hlist
, head
);
445 sessiond_register_error
:
454 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc
*desc
,
455 struct lttng_enabler
*enabler
)
458 unsigned int has_loglevel
= 0;
460 assert(enabler
->type
== LTTNG_ENABLER_WILDCARD
);
461 /* Compare excluding final '*' */
462 if (strncmp(desc
->name
, enabler
->event_param
.name
,
463 strlen(enabler
->event_param
.name
) - 1))
465 if (desc
->loglevel
) {
466 loglevel
= *(*desc
->loglevel
);
469 if (!lttng_loglevel_match(loglevel
,
471 enabler
->event_param
.loglevel_type
,
472 enabler
->event_param
.loglevel
))
478 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
479 struct lttng_enabler
*enabler
)
482 unsigned int has_loglevel
= 0;
484 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
485 if (strcmp(desc
->name
, enabler
->event_param
.name
))
487 if (desc
->loglevel
) {
488 loglevel
= *(*desc
->loglevel
);
491 if (!lttng_loglevel_match(loglevel
,
493 enabler
->event_param
.loglevel_type
,
494 enabler
->event_param
.loglevel
))
500 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
501 struct lttng_enabler
*enabler
)
503 struct lttng_ust_excluder_node
*excluder
;
505 /* If event matches with an excluder, return 'does not match' */
506 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
509 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
513 excluder_name
= (char *) (excluder
->excluder
.names
)
514 + count
* LTTNG_UST_SYM_NAME_LEN
;
515 len
= strnlen(excluder_name
, LTTNG_UST_SYM_NAME_LEN
);
516 if (len
> 0 && excluder_name
[len
- 1] == '*') {
517 found
= !strncmp(desc
->name
, excluder_name
,
520 found
= !strncmp(desc
->name
, excluder_name
,
521 LTTNG_UST_SYM_NAME_LEN
- 1);
528 switch (enabler
->type
) {
529 case LTTNG_ENABLER_WILDCARD
:
530 return lttng_desc_match_wildcard_enabler(desc
, enabler
);
531 case LTTNG_ENABLER_EVENT
:
532 return lttng_desc_match_event_enabler(desc
, enabler
);
539 int lttng_event_match_enabler(struct lttng_event
*event
,
540 struct lttng_enabler
*enabler
)
542 if (lttng_desc_match_enabler(event
->desc
, enabler
)
543 && event
->chan
== enabler
->chan
)
550 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
551 struct lttng_enabler
*enabler
)
553 struct lttng_enabler_ref
*enabler_ref
;
555 cds_list_for_each_entry(enabler_ref
,
556 &event
->enablers_ref_head
, node
) {
557 if (enabler_ref
->ref
== enabler
)
564 * Create struct lttng_event if it is missing and present in the list of
568 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
570 struct lttng_session
*session
= enabler
->chan
->session
;
571 struct lttng_probe_desc
*probe_desc
;
572 const struct lttng_event_desc
*desc
;
573 struct lttng_event
*event
;
575 struct cds_list_head
*probe_list
;
577 probe_list
= lttng_get_probe_list_head();
579 * For each probe event, if we find that a probe event matches
580 * our enabler, create an associated lttng_event if not
583 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
584 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
586 struct cds_hlist_head
*head
;
587 struct cds_hlist_node
*node
;
588 const char *event_name
;
592 desc
= probe_desc
->event_desc
[i
];
593 if (!lttng_desc_match_enabler(desc
, enabler
))
595 event_name
= desc
->name
;
596 name_len
= strlen(event_name
);
599 * Check if already created.
601 hash
= jhash(event_name
, name_len
, 0);
602 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
603 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
604 if (event
->desc
== desc
605 && event
->chan
== enabler
->chan
)
612 * We need to create an event for this
615 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
618 DBG("Unable to create event %s, error %d\n",
619 probe_desc
->event_desc
[i
]->name
, ret
);
626 * Create events associated with an enabler (if not already present),
627 * and add backward reference from the event to the enabler.
630 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
632 struct lttng_session
*session
= enabler
->chan
->session
;
633 struct lttng_event
*event
;
635 /* First ensure that probe events are created for this enabler. */
636 lttng_create_event_if_missing(enabler
);
638 /* For each event matching enabler in session event list. */
639 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
640 struct lttng_enabler_ref
*enabler_ref
;
642 if (!lttng_event_match_enabler(event
, enabler
))
645 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
648 * If no backward ref, create it.
649 * Add backward ref from event to enabler.
651 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
654 enabler_ref
->ref
= enabler
;
655 cds_list_add(&enabler_ref
->node
,
656 &event
->enablers_ref_head
);
660 * Link filter bytecodes if not linked yet.
662 lttng_enabler_event_link_bytecode(event
, enabler
);
664 /* TODO: merge event context. */
670 * Called at library load: connect the probe on all enablers matching
672 * Called with session mutex held.
674 int lttng_fix_pending_events(void)
676 struct lttng_session
*session
;
678 cds_list_for_each_entry(session
, &sessions
, node
) {
679 lttng_session_lazy_sync_enablers(session
);
685 * For each session of the owner thread, execute pending statedump.
686 * Only dump state for the sessions owned by the caller thread, because
687 * we don't keep ust_lock across the entire iteration.
689 int lttng_handle_pending_statedump(void *owner
)
691 struct lttng_session
*session
;
693 /* Execute state dump */
694 lttng_ust_baddr_statedump(owner
);
696 /* Clear pending state dump */
698 cds_list_for_each_entry(session
, &sessions
, node
) {
699 if (session
->owner
!= owner
)
701 if (!session
->statedump_pending
)
703 session
->statedump_pending
= 0;
710 * Only used internally at session destruction.
713 void _lttng_event_destroy(struct lttng_event
*event
)
715 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
717 cds_list_del(&event
->node
);
718 lttng_destroy_context(event
->ctx
);
719 lttng_free_event_filter_runtime(event
);
720 /* Free event enabler refs */
721 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
722 &event
->enablers_ref_head
, node
)
727 void lttng_ust_events_exit(void)
729 struct lttng_session
*session
, *tmpsession
;
731 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
732 lttng_session_destroy(session
);
736 * Enabler management.
738 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
739 struct lttng_ust_event
*event_param
,
740 struct lttng_channel
*chan
)
742 struct lttng_enabler
*enabler
;
744 enabler
= zmalloc(sizeof(*enabler
));
747 enabler
->type
= type
;
748 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
749 CDS_INIT_LIST_HEAD(&enabler
->excluder_head
);
750 memcpy(&enabler
->event_param
, event_param
,
751 sizeof(enabler
->event_param
));
752 enabler
->chan
= chan
;
754 enabler
->enabled
= 1;
755 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
756 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
760 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
762 enabler
->enabled
= 1;
763 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
767 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
769 enabler
->enabled
= 0;
770 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
774 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
775 struct lttng_ust_filter_bytecode_node
*bytecode
)
777 bytecode
->enabler
= enabler
;
778 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
779 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
783 int lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
784 struct lttng_ust_excluder_node
*excluder
)
786 excluder
->enabler
= enabler
;
787 cds_list_add_tail(&excluder
->node
, &enabler
->excluder_head
);
788 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
792 int lttng_attach_context(struct lttng_ust_context
*context_param
,
793 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
796 * We cannot attach a context after trace has been started for a
797 * session because the metadata does not allow expressing this
798 * information outside of the original channel scope.
800 if (session
->been_active
)
803 switch (context_param
->ctx
) {
804 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
805 return lttng_add_pthread_id_to_ctx(ctx
);
806 case LTTNG_UST_CONTEXT_VTID
:
807 return lttng_add_vtid_to_ctx(ctx
);
808 case LTTNG_UST_CONTEXT_VPID
:
809 return lttng_add_vpid_to_ctx(ctx
);
810 case LTTNG_UST_CONTEXT_PROCNAME
:
811 return lttng_add_procname_to_ctx(ctx
);
812 case LTTNG_UST_CONTEXT_IP
:
813 return lttng_add_ip_to_ctx(ctx
);
819 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
820 struct lttng_ust_context
*context_param
)
822 #if 0 // disabled for now.
823 struct lttng_session
*session
= enabler
->chan
->session
;
826 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
830 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
836 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
838 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
839 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
841 /* Destroy filter bytecode */
842 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
843 &enabler
->filter_bytecode_head
, node
) {
847 /* Destroy excluders */
848 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
849 &enabler
->excluder_head
, node
) {
853 /* Destroy contexts */
854 lttng_destroy_context(enabler
->ctx
);
856 cds_list_del(&enabler
->node
);
861 * lttng_session_sync_enablers should be called just before starting a
865 void lttng_session_sync_enablers(struct lttng_session
*session
)
867 struct lttng_enabler
*enabler
;
868 struct lttng_event
*event
;
870 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
871 lttng_enabler_ref_events(enabler
);
873 * For each event, if at least one of its enablers is enabled,
874 * and its channel and session transient states are enabled, we
875 * enable the event, else we disable it.
877 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
878 struct lttng_enabler_ref
*enabler_ref
;
879 struct lttng_bytecode_runtime
*runtime
;
880 int enabled
= 0, has_enablers_without_bytecode
= 0;
883 cds_list_for_each_entry(enabler_ref
,
884 &event
->enablers_ref_head
, node
) {
885 if (enabler_ref
->ref
->enabled
) {
891 * Enabled state is based on union of enablers, with
892 * intesection of session and channel transient enable
895 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
897 CMM_STORE_SHARED(event
->enabled
, enabled
);
899 * Sync tracepoint registration with event enabled
903 if (!event
->registered
)
904 register_event(event
);
906 if (event
->registered
)
907 unregister_event(event
);
910 /* Check if has enablers without bytecode enabled */
911 cds_list_for_each_entry(enabler_ref
,
912 &event
->enablers_ref_head
, node
) {
913 if (enabler_ref
->ref
->enabled
914 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
915 has_enablers_without_bytecode
= 1;
919 event
->has_enablers_without_bytecode
=
920 has_enablers_without_bytecode
;
923 cds_list_for_each_entry(runtime
,
924 &event
->bytecode_runtime_head
, node
) {
925 lttng_filter_sync_state(runtime
);
931 * Apply enablers to session events, adding events to session if need
932 * be. It is required after each modification applied to an active
933 * session, and right before session "start".
934 * "lazy" sync means we only sync if required.
937 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
939 /* We can skip if session is not active */
940 if (!session
->active
)
942 lttng_session_sync_enablers(session
);