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 * All operations within this file are called by the communication
64 * thread, under ust_lock protection.
67 static CDS_LIST_HEAD(sessions
);
69 struct cds_list_head
*_lttng_get_sessions(void)
74 static void _lttng_event_destroy(struct lttng_event
*event
);
77 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
79 void lttng_session_sync_enablers(struct lttng_session
*session
);
81 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
84 * Called with ust lock held.
86 int lttng_session_active(void)
88 struct lttng_session
*iter
;
90 cds_list_for_each_entry(iter
, &sessions
, node
) {
98 int lttng_loglevel_match(int loglevel
,
99 unsigned int has_loglevel
,
100 enum lttng_ust_loglevel_type req_type
,
104 loglevel
= TRACE_DEFAULT
;
106 case LTTNG_UST_LOGLEVEL_RANGE
:
107 if (loglevel
<= req_loglevel
|| req_loglevel
== -1)
111 case LTTNG_UST_LOGLEVEL_SINGLE
:
112 if (loglevel
== req_loglevel
|| req_loglevel
== -1)
116 case LTTNG_UST_LOGLEVEL_ALL
:
122 void synchronize_trace(void)
127 struct lttng_session
*lttng_session_create(void)
129 struct lttng_session
*session
;
132 session
= zmalloc(sizeof(struct lttng_session
));
135 CDS_INIT_LIST_HEAD(&session
->chan_head
);
136 CDS_INIT_LIST_HEAD(&session
->events_head
);
137 CDS_INIT_LIST_HEAD(&session
->enablers_head
);
138 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
139 CDS_INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
140 cds_list_add(&session
->node
, &sessions
);
145 * Only used internally at session destruction.
148 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
150 struct channel
*chan
;
151 struct lttng_ust_shm_handle
*handle
;
153 cds_list_del(<tng_chan
->node
);
154 lttng_destroy_context(lttng_chan
->ctx
);
155 chan
= lttng_chan
->chan
;
156 handle
= lttng_chan
->handle
;
158 * note: lttng_chan is private data contained within handle. It
159 * will be freed along with the handle.
161 channel_destroy(chan
, handle
, 0);
165 void register_event(struct lttng_event
*event
)
168 const struct lttng_event_desc
*desc
;
170 assert(event
->registered
== 0);
172 ret
= __tracepoint_probe_register(desc
->name
,
173 desc
->probe_callback
,
174 event
, desc
->signature
);
177 event
->registered
= 1;
181 void unregister_event(struct lttng_event
*event
)
184 const struct lttng_event_desc
*desc
;
186 assert(event
->registered
== 1);
188 ret
= __tracepoint_probe_unregister(desc
->name
,
189 desc
->probe_callback
,
193 event
->registered
= 0;
197 * Only used internally at session destruction.
200 void _lttng_event_unregister(struct lttng_event
*event
)
202 if (event
->registered
)
203 unregister_event(event
);
206 void lttng_session_destroy(struct lttng_session
*session
)
208 struct lttng_channel
*chan
, *tmpchan
;
209 struct lttng_event
*event
, *tmpevent
;
210 struct lttng_enabler
*enabler
, *tmpenabler
;
212 CMM_ACCESS_ONCE(session
->active
) = 0;
213 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
214 _lttng_event_unregister(event
);
216 synchronize_trace(); /* Wait for in-flight events to complete */
217 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
218 &session
->enablers_head
, node
)
219 lttng_enabler_destroy(enabler
);
220 cds_list_for_each_entry_safe(event
, tmpevent
,
221 &session
->events_head
, node
)
222 _lttng_event_destroy(event
);
223 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
224 _lttng_channel_unmap(chan
);
225 cds_list_del(&session
->node
);
229 int lttng_session_enable(struct lttng_session
*session
)
232 struct lttng_channel
*chan
;
235 if (session
->active
) {
240 notify_socket
= lttng_get_notify_socket(session
->owner
);
241 if (notify_socket
< 0)
242 return notify_socket
;
244 /* Set transient enabler state to "enabled" */
246 /* We need to sync enablers with session before activation. */
247 lttng_session_sync_enablers(session
);
250 * Snapshot the number of events per channel to know the type of header
253 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
254 const struct lttng_ctx
*ctx
;
255 const struct lttng_ctx_field
*fields
= NULL
;
256 size_t nr_fields
= 0;
259 /* don't change it if session stop/restart */
260 if (chan
->header_type
)
264 nr_fields
= ctx
->nr_fields
;
265 fields
= ctx
->fields
;
267 ret
= ustcomm_register_channel(notify_socket
,
275 DBG("Error (%d) registering channel to sessiond", ret
);
278 if (chan_id
!= chan
->id
) {
279 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
285 /* Set atomically the state to "active" */
286 CMM_ACCESS_ONCE(session
->active
) = 1;
287 CMM_ACCESS_ONCE(session
->been_active
) = 1;
289 session
->statedump_pending
= 1;
290 lttng_ust_sockinfo_session_enabled(session
->owner
);
295 int lttng_session_disable(struct lttng_session
*session
)
299 if (!session
->active
) {
303 /* Set atomically the state to "inactive" */
304 CMM_ACCESS_ONCE(session
->active
) = 0;
306 /* Set transient enabler state to "disabled" */
308 lttng_session_sync_enablers(session
);
313 int lttng_channel_enable(struct lttng_channel
*channel
)
317 if (channel
->enabled
) {
321 /* Set transient enabler state to "enabled" */
323 lttng_session_sync_enablers(channel
->session
);
324 /* Set atomically the state to "enabled" */
325 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
330 int lttng_channel_disable(struct lttng_channel
*channel
)
334 if (!channel
->enabled
) {
338 /* Set atomically the state to "disabled" */
339 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
340 /* Set transient enabler state to "enabled" */
342 lttng_session_sync_enablers(channel
->session
);
348 * Supports event creation while tracing session is active.
351 int lttng_event_create(const struct lttng_event_desc
*desc
,
352 struct lttng_channel
*chan
)
354 const char *event_name
= desc
->name
;
355 struct lttng_event
*event
;
356 struct lttng_session
*session
= chan
->session
;
357 struct cds_hlist_head
*head
;
358 struct cds_hlist_node
*node
;
360 size_t name_len
= strlen(event_name
);
362 int notify_socket
, loglevel
;
365 hash
= jhash(event_name
, name_len
, 0);
366 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
367 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
369 if (!strncmp(event
->desc
->name
, desc
->name
,
370 LTTNG_UST_SYM_NAME_LEN
- 1)
371 && chan
== event
->chan
) {
377 notify_socket
= lttng_get_notify_socket(session
->owner
);
378 if (notify_socket
< 0) {
384 * Check if loglevel match. Refuse to connect event if not.
386 event
= zmalloc(sizeof(struct lttng_event
));
393 /* Event will be enabled by enabler sync. */
395 event
->registered
= 0;
396 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
397 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
401 loglevel
= *(*event
->desc
->loglevel
);
403 loglevel
= TRACE_DEFAULT
;
404 if (desc
->u
.ext
.model_emf_uri
)
405 uri
= *(desc
->u
.ext
.model_emf_uri
);
409 /* Fetch event ID from sessiond */
410 ret
= ustcomm_register_event(notify_socket
,
421 DBG("Error (%d) registering event to sessiond", ret
);
422 goto sessiond_register_error
;
425 /* Populate lttng_event structure before tracepoint registration. */
427 cds_list_add(&event
->node
, &chan
->session
->events_head
);
428 cds_hlist_add_head(&event
->hlist
, head
);
431 sessiond_register_error
:
440 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc
*desc
,
441 struct lttng_enabler
*enabler
)
444 unsigned int has_loglevel
= 0;
446 assert(enabler
->type
== LTTNG_ENABLER_WILDCARD
);
447 /* Compare excluding final '*' */
448 if (strncmp(desc
->name
, enabler
->event_param
.name
,
449 strlen(enabler
->event_param
.name
) - 1))
451 if (desc
->loglevel
) {
452 loglevel
= *(*desc
->loglevel
);
455 if (!lttng_loglevel_match(loglevel
,
457 enabler
->event_param
.loglevel_type
,
458 enabler
->event_param
.loglevel
))
464 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
465 struct lttng_enabler
*enabler
)
468 unsigned int has_loglevel
= 0;
470 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
471 if (strcmp(desc
->name
, enabler
->event_param
.name
))
473 if (desc
->loglevel
) {
474 loglevel
= *(*desc
->loglevel
);
477 if (!lttng_loglevel_match(loglevel
,
479 enabler
->event_param
.loglevel_type
,
480 enabler
->event_param
.loglevel
))
486 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
487 struct lttng_enabler
*enabler
)
489 struct lttng_ust_excluder_node
*excluder
;
491 /* If event matches with an excluder, return 'does not match' */
492 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
495 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
499 excluder_name
= (char *) (excluder
->excluder
.names
)
500 + count
* LTTNG_UST_SYM_NAME_LEN
;
501 len
= strnlen(excluder_name
, LTTNG_UST_SYM_NAME_LEN
);
502 if (len
> 0 && excluder_name
[len
- 1] == '*') {
503 found
= !strncmp(desc
->name
, excluder_name
,
506 found
= !strncmp(desc
->name
, excluder_name
,
507 LTTNG_UST_SYM_NAME_LEN
- 1);
514 switch (enabler
->type
) {
515 case LTTNG_ENABLER_WILDCARD
:
516 return lttng_desc_match_wildcard_enabler(desc
, enabler
);
517 case LTTNG_ENABLER_EVENT
:
518 return lttng_desc_match_event_enabler(desc
, enabler
);
525 int lttng_event_match_enabler(struct lttng_event
*event
,
526 struct lttng_enabler
*enabler
)
528 if (lttng_desc_match_enabler(event
->desc
, enabler
)
529 && event
->chan
== enabler
->chan
)
536 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
537 struct lttng_enabler
*enabler
)
539 struct lttng_enabler_ref
*enabler_ref
;
541 cds_list_for_each_entry(enabler_ref
,
542 &event
->enablers_ref_head
, node
) {
543 if (enabler_ref
->ref
== enabler
)
550 * Create struct lttng_event if it is missing and present in the list of
554 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
556 struct lttng_session
*session
= enabler
->chan
->session
;
557 struct lttng_probe_desc
*probe_desc
;
558 const struct lttng_event_desc
*desc
;
559 struct lttng_event
*event
;
561 struct cds_list_head
*probe_list
;
563 probe_list
= lttng_get_probe_list_head();
565 * For each probe event, if we find that a probe event matches
566 * our enabler, create an associated lttng_event if not
569 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
570 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
572 struct cds_hlist_head
*head
;
573 struct cds_hlist_node
*node
;
574 const char *event_name
;
578 desc
= probe_desc
->event_desc
[i
];
579 if (!lttng_desc_match_enabler(desc
, enabler
))
581 event_name
= desc
->name
;
582 name_len
= strlen(event_name
);
585 * Check if already created.
587 hash
= jhash(event_name
, name_len
, 0);
588 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
589 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
590 if (event
->desc
== desc
591 && event
->chan
== enabler
->chan
)
598 * We need to create an event for this
601 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
604 DBG("Unable to create event %s, error %d\n",
605 probe_desc
->event_desc
[i
]->name
, ret
);
612 * Create events associated with an enabler (if not already present),
613 * and add backward reference from the event to the enabler.
616 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
618 struct lttng_session
*session
= enabler
->chan
->session
;
619 struct lttng_event
*event
;
621 /* First ensure that probe events are created for this enabler. */
622 lttng_create_event_if_missing(enabler
);
624 /* For each event matching enabler in session event list. */
625 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
626 struct lttng_enabler_ref
*enabler_ref
;
628 if (!lttng_event_match_enabler(event
, enabler
))
631 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
634 * If no backward ref, create it.
635 * Add backward ref from event to enabler.
637 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
640 enabler_ref
->ref
= enabler
;
641 cds_list_add(&enabler_ref
->node
,
642 &event
->enablers_ref_head
);
646 * Link filter bytecodes if not linked yet.
648 lttng_enabler_event_link_bytecode(event
, enabler
);
650 /* TODO: merge event context. */
656 * Called at library load: connect the probe on all enablers matching
658 * Called with session mutex held.
660 int lttng_fix_pending_events(void)
662 struct lttng_session
*session
;
664 cds_list_for_each_entry(session
, &sessions
, node
) {
665 lttng_session_lazy_sync_enablers(session
);
671 * For each session of the owner thread, execute pending statedump.
672 * Only dump state for the sessions owned by the caller thread, because
673 * we don't keep ust_lock across the entire iteration.
675 void lttng_handle_pending_statedump(void *owner
)
677 struct lttng_session
*session
;
679 /* Execute state dump */
680 lttng_ust_baddr_statedump(owner
);
682 /* Clear pending state dump */
686 cds_list_for_each_entry(session
, &sessions
, node
) {
687 if (session
->owner
!= owner
)
689 if (!session
->statedump_pending
)
691 session
->statedump_pending
= 0;
699 * Only used internally at session destruction.
702 void _lttng_event_destroy(struct lttng_event
*event
)
704 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
706 cds_list_del(&event
->node
);
707 lttng_destroy_context(event
->ctx
);
708 lttng_free_event_filter_runtime(event
);
709 /* Free event enabler refs */
710 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
711 &event
->enablers_ref_head
, node
)
716 void lttng_ust_events_exit(void)
718 struct lttng_session
*session
, *tmpsession
;
720 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
721 lttng_session_destroy(session
);
725 * Enabler management.
727 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
728 struct lttng_ust_event
*event_param
,
729 struct lttng_channel
*chan
)
731 struct lttng_enabler
*enabler
;
733 enabler
= zmalloc(sizeof(*enabler
));
736 enabler
->type
= type
;
737 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
738 CDS_INIT_LIST_HEAD(&enabler
->excluder_head
);
739 memcpy(&enabler
->event_param
, event_param
,
740 sizeof(enabler
->event_param
));
741 enabler
->chan
= chan
;
743 enabler
->enabled
= 1;
744 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
745 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
749 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
751 enabler
->enabled
= 1;
752 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
756 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
758 enabler
->enabled
= 0;
759 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
763 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
764 struct lttng_ust_filter_bytecode_node
*bytecode
)
766 bytecode
->enabler
= enabler
;
767 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
768 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
772 int lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
773 struct lttng_ust_excluder_node
*excluder
)
775 excluder
->enabler
= enabler
;
776 cds_list_add_tail(&excluder
->node
, &enabler
->excluder_head
);
777 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
781 int lttng_attach_context(struct lttng_ust_context
*context_param
,
782 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
785 * We cannot attach a context after trace has been started for a
786 * session because the metadata does not allow expressing this
787 * information outside of the original channel scope.
789 if (session
->been_active
)
792 switch (context_param
->ctx
) {
793 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
794 return lttng_add_pthread_id_to_ctx(ctx
);
795 case LTTNG_UST_CONTEXT_VTID
:
796 return lttng_add_vtid_to_ctx(ctx
);
797 case LTTNG_UST_CONTEXT_VPID
:
798 return lttng_add_vpid_to_ctx(ctx
);
799 case LTTNG_UST_CONTEXT_PROCNAME
:
800 return lttng_add_procname_to_ctx(ctx
);
801 case LTTNG_UST_CONTEXT_IP
:
802 return lttng_add_ip_to_ctx(ctx
);
808 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
809 struct lttng_ust_context
*context_param
)
811 #if 0 // disabled for now.
812 struct lttng_session
*session
= enabler
->chan
->session
;
815 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
819 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
825 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
827 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
828 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
830 /* Destroy filter bytecode */
831 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
832 &enabler
->filter_bytecode_head
, node
) {
836 /* Destroy excluders */
837 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
838 &enabler
->excluder_head
, node
) {
842 /* Destroy contexts */
843 lttng_destroy_context(enabler
->ctx
);
845 cds_list_del(&enabler
->node
);
850 * lttng_session_sync_enablers should be called just before starting a
854 void lttng_session_sync_enablers(struct lttng_session
*session
)
856 struct lttng_enabler
*enabler
;
857 struct lttng_event
*event
;
859 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
860 lttng_enabler_ref_events(enabler
);
862 * For each event, if at least one of its enablers is enabled,
863 * and its channel and session transient states are enabled, we
864 * enable the event, else we disable it.
866 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
867 struct lttng_enabler_ref
*enabler_ref
;
868 struct lttng_bytecode_runtime
*runtime
;
869 int enabled
= 0, has_enablers_without_bytecode
= 0;
872 cds_list_for_each_entry(enabler_ref
,
873 &event
->enablers_ref_head
, node
) {
874 if (enabler_ref
->ref
->enabled
) {
880 * Enabled state is based on union of enablers, with
881 * intesection of session and channel transient enable
884 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
886 CMM_STORE_SHARED(event
->enabled
, enabled
);
888 * Sync tracepoint registration with event enabled
892 if (!event
->registered
)
893 register_event(event
);
895 if (event
->registered
)
896 unregister_event(event
);
899 /* Check if has enablers without bytecode enabled */
900 cds_list_for_each_entry(enabler_ref
,
901 &event
->enablers_ref_head
, node
) {
902 if (enabler_ref
->ref
->enabled
903 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
904 has_enablers_without_bytecode
= 1;
908 event
->has_enablers_without_bytecode
=
909 has_enablers_without_bytecode
;
912 cds_list_for_each_entry(runtime
,
913 &event
->bytecode_runtime_head
, node
) {
914 lttng_filter_sync_state(runtime
);
920 * Apply enablers to session events, adding events to session if need
921 * be. It is required after each modification applied to an active
922 * session, and right before session "start".
923 * "lazy" sync means we only sync if required.
926 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
928 /* We can skip if session is not active */
929 if (!session
->active
)
931 lttng_session_sync_enablers(session
);