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"
58 #include "../libringbuffer/shm.h"
62 * The sessions mutex is the centralized mutex across UST tracing
63 * control and probe registration. All operations within this file are
64 * called by the communication thread, under ust_lock protection.
66 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
70 pthread_mutex_lock(&sessions_mutex
);
75 pthread_mutex_unlock(&sessions_mutex
);
78 static CDS_LIST_HEAD(sessions
);
80 static void _lttng_event_destroy(struct lttng_event
*event
);
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
,
109 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
112 loglevel
= TRACE_DEFAULT
;
114 case LTTNG_UST_LOGLEVEL_RANGE
:
115 if (loglevel
<= req_loglevel
|| req_loglevel
== -1)
119 case LTTNG_UST_LOGLEVEL_SINGLE
:
120 if (loglevel
== req_loglevel
|| req_loglevel
== -1)
124 case LTTNG_UST_LOGLEVEL_ALL
:
130 void synchronize_trace(void)
135 struct lttng_session
*lttng_session_create(void)
137 struct lttng_session
*session
;
140 session
= zmalloc(sizeof(struct lttng_session
));
143 CDS_INIT_LIST_HEAD(&session
->chan_head
);
144 CDS_INIT_LIST_HEAD(&session
->events_head
);
145 CDS_INIT_LIST_HEAD(&session
->enablers_head
);
146 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
147 CDS_INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
148 cds_list_add(&session
->node
, &sessions
);
153 * Only used internally at session destruction.
156 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
158 struct channel
*chan
;
159 struct lttng_ust_shm_handle
*handle
;
161 cds_list_del(<tng_chan
->node
);
162 lttng_destroy_context(lttng_chan
->ctx
);
163 chan
= lttng_chan
->chan
;
164 handle
= lttng_chan
->handle
;
166 * note: lttng_chan is private data contained within handle. It
167 * will be freed along with the handle.
169 channel_destroy(chan
, handle
, 0);
173 void register_event(struct lttng_event
*event
)
176 const struct lttng_event_desc
*desc
;
178 assert(event
->registered
== 0);
180 ret
= __tracepoint_probe_register(desc
->name
,
181 desc
->probe_callback
,
182 event
, desc
->signature
);
185 event
->registered
= 1;
189 void unregister_event(struct lttng_event
*event
)
192 const struct lttng_event_desc
*desc
;
194 assert(event
->registered
== 1);
196 ret
= __tracepoint_probe_unregister(desc
->name
,
197 desc
->probe_callback
,
201 event
->registered
= 0;
205 * Only used internally at session destruction.
208 void _lttng_event_unregister(struct lttng_event
*event
)
210 if (event
->registered
)
211 unregister_event(event
);
214 void lttng_session_destroy(struct lttng_session
*session
)
216 struct lttng_channel
*chan
, *tmpchan
;
217 struct lttng_event
*event
, *tmpevent
;
218 struct lttng_enabler
*enabler
, *tmpenabler
;
220 CMM_ACCESS_ONCE(session
->active
) = 0;
221 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
222 _lttng_event_unregister(event
);
224 synchronize_trace(); /* Wait for in-flight events to complete */
225 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
226 &session
->enablers_head
, node
)
227 lttng_enabler_destroy(enabler
);
228 cds_list_for_each_entry_safe(event
, tmpevent
,
229 &session
->events_head
, node
)
230 _lttng_event_destroy(event
);
231 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
232 _lttng_channel_unmap(chan
);
233 cds_list_del(&session
->node
);
237 int lttng_session_enable(struct lttng_session
*session
)
240 struct lttng_channel
*chan
;
243 if (session
->active
) {
248 notify_socket
= lttng_get_notify_socket(session
->owner
);
249 if (notify_socket
< 0)
250 return notify_socket
;
252 /* Set transient enabler state to "enabled" */
254 /* We need to sync enablers with session before activation. */
255 lttng_session_sync_enablers(session
);
258 * Snapshot the number of events per channel to know the type of header
261 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
262 const struct lttng_ctx
*ctx
;
263 const struct lttng_ctx_field
*fields
= NULL
;
264 size_t nr_fields
= 0;
267 /* don't change it if session stop/restart */
268 if (chan
->header_type
)
272 nr_fields
= ctx
->nr_fields
;
273 fields
= ctx
->fields
;
275 ret
= ustcomm_register_channel(notify_socket
,
283 DBG("Error (%d) registering channel to sessiond", ret
);
286 if (chan_id
!= chan
->id
) {
287 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
293 /* Set atomically the state to "active" */
294 CMM_ACCESS_ONCE(session
->active
) = 1;
295 CMM_ACCESS_ONCE(session
->been_active
) = 1;
297 lttng_ust_sockinfo_session_enabled(session
->owner
, session
);
302 int lttng_session_disable(struct lttng_session
*session
)
306 if (!session
->active
) {
310 /* Set atomically the state to "inactive" */
311 CMM_ACCESS_ONCE(session
->active
) = 0;
313 /* Set transient enabler state to "disabled" */
315 lttng_session_sync_enablers(session
);
320 int lttng_channel_enable(struct lttng_channel
*channel
)
324 if (channel
->enabled
) {
328 /* Set transient enabler state to "enabled" */
330 lttng_session_sync_enablers(channel
->session
);
331 /* Set atomically the state to "enabled" */
332 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
337 int lttng_channel_disable(struct lttng_channel
*channel
)
341 if (!channel
->enabled
) {
345 /* Set atomically the state to "disabled" */
346 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
347 /* Set transient enabler state to "enabled" */
349 lttng_session_sync_enablers(channel
->session
);
355 * Supports event creation while tracing session is active.
358 int lttng_event_create(const struct lttng_event_desc
*desc
,
359 struct lttng_channel
*chan
)
361 const char *event_name
= desc
->name
;
362 struct lttng_event
*event
;
363 struct lttng_session
*session
= chan
->session
;
364 struct cds_hlist_head
*head
;
365 struct cds_hlist_node
*node
;
367 size_t name_len
= strlen(event_name
);
369 int notify_socket
, loglevel
;
372 hash
= jhash(event_name
, name_len
, 0);
373 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
374 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
376 if (!strncmp(event
->desc
->name
, desc
->name
,
377 LTTNG_UST_SYM_NAME_LEN
- 1)
378 && chan
== event
->chan
) {
384 notify_socket
= lttng_get_notify_socket(session
->owner
);
385 if (notify_socket
< 0) {
391 * Check if loglevel match. Refuse to connect event if not.
393 event
= zmalloc(sizeof(struct lttng_event
));
400 /* Event will be enabled by enabler sync. */
402 event
->registered
= 0;
403 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
404 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
408 loglevel
= *(*event
->desc
->loglevel
);
410 loglevel
= TRACE_DEFAULT
;
411 if (desc
->u
.ext
.model_emf_uri
)
412 uri
= *(desc
->u
.ext
.model_emf_uri
);
416 /* Fetch event ID from sessiond */
417 ret
= ustcomm_register_event(notify_socket
,
428 DBG("Error (%d) registering event to sessiond", ret
);
429 goto sessiond_register_error
;
432 /* Populate lttng_event structure before tracepoint registration. */
434 cds_list_add(&event
->node
, &chan
->session
->events_head
);
435 cds_hlist_add_head(&event
->hlist
, head
);
438 sessiond_register_error
:
447 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc
*desc
,
448 struct lttng_enabler
*enabler
)
451 unsigned int has_loglevel
= 0;
453 assert(enabler
->type
== LTTNG_ENABLER_WILDCARD
);
454 /* Compare excluding final '*' */
455 if (strncmp(desc
->name
, enabler
->event_param
.name
,
456 strlen(enabler
->event_param
.name
) - 1))
458 if (desc
->loglevel
) {
459 loglevel
= *(*desc
->loglevel
);
462 if (!lttng_loglevel_match(loglevel
,
464 enabler
->event_param
.loglevel_type
,
465 enabler
->event_param
.loglevel
))
471 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
472 struct lttng_enabler
*enabler
)
475 unsigned int has_loglevel
= 0;
477 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
478 if (strcmp(desc
->name
, enabler
->event_param
.name
))
480 if (desc
->loglevel
) {
481 loglevel
= *(*desc
->loglevel
);
484 if (!lttng_loglevel_match(loglevel
,
486 enabler
->event_param
.loglevel_type
,
487 enabler
->event_param
.loglevel
))
493 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
494 struct lttng_enabler
*enabler
)
496 struct lttng_ust_excluder_node
*excluder
;
498 /* If event matches with an excluder, return 'does not match' */
499 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
502 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
506 excluder_name
= (char *) (excluder
->excluder
.names
)
507 + count
* LTTNG_UST_SYM_NAME_LEN
;
508 len
= strnlen(excluder_name
, LTTNG_UST_SYM_NAME_LEN
);
509 if (len
> 0 && excluder_name
[len
- 1] == '*') {
510 found
= !strncmp(desc
->name
, excluder_name
,
513 found
= !strncmp(desc
->name
, excluder_name
,
514 LTTNG_UST_SYM_NAME_LEN
- 1);
521 switch (enabler
->type
) {
522 case LTTNG_ENABLER_WILDCARD
:
523 return lttng_desc_match_wildcard_enabler(desc
, enabler
);
524 case LTTNG_ENABLER_EVENT
:
525 return lttng_desc_match_event_enabler(desc
, enabler
);
532 int lttng_event_match_enabler(struct lttng_event
*event
,
533 struct lttng_enabler
*enabler
)
535 if (lttng_desc_match_enabler(event
->desc
, enabler
)
536 && event
->chan
== enabler
->chan
)
543 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
544 struct lttng_enabler
*enabler
)
546 struct lttng_enabler_ref
*enabler_ref
;
548 cds_list_for_each_entry(enabler_ref
,
549 &event
->enablers_ref_head
, node
) {
550 if (enabler_ref
->ref
== enabler
)
557 * Create struct lttng_event if it is missing and present in the list of
561 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
563 struct lttng_session
*session
= enabler
->chan
->session
;
564 struct lttng_probe_desc
*probe_desc
;
565 const struct lttng_event_desc
*desc
;
566 struct lttng_event
*event
;
568 struct cds_list_head
*probe_list
;
570 probe_list
= lttng_get_probe_list_head();
572 * For each probe event, if we find that a probe event matches
573 * our enabler, create an associated lttng_event if not
576 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
577 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
579 struct cds_hlist_head
*head
;
580 struct cds_hlist_node
*node
;
581 const char *event_name
;
585 desc
= probe_desc
->event_desc
[i
];
586 if (!lttng_desc_match_enabler(desc
, enabler
))
588 event_name
= desc
->name
;
589 name_len
= strlen(event_name
);
592 * Check if already created.
594 hash
= jhash(event_name
, name_len
, 0);
595 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
596 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
597 if (event
->desc
== desc
598 && event
->chan
== enabler
->chan
)
605 * We need to create an event for this
608 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
611 DBG("Unable to create event %s, error %d\n",
612 probe_desc
->event_desc
[i
]->name
, ret
);
619 * Create events associated with an enabler (if not already present),
620 * and add backward reference from the event to the enabler.
623 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
625 struct lttng_session
*session
= enabler
->chan
->session
;
626 struct lttng_event
*event
;
628 /* First ensure that probe events are created for this enabler. */
629 lttng_create_event_if_missing(enabler
);
631 /* For each event matching enabler in session event list. */
632 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
633 struct lttng_enabler_ref
*enabler_ref
;
635 if (!lttng_event_match_enabler(event
, enabler
))
638 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
641 * If no backward ref, create it.
642 * Add backward ref from event to enabler.
644 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
647 enabler_ref
->ref
= enabler
;
648 cds_list_add(&enabler_ref
->node
,
649 &event
->enablers_ref_head
);
653 * Link filter bytecodes if not linked yet.
655 lttng_enabler_event_link_bytecode(event
, enabler
);
657 /* TODO: merge event context. */
663 * Called at library load: connect the probe on all enablers matching
665 * Called with session mutex held.
667 int lttng_fix_pending_events(void)
669 struct lttng_session
*session
;
671 cds_list_for_each_entry(session
, &sessions
, node
) {
672 lttng_session_lazy_sync_enablers(session
);
678 * Only used internally at session destruction.
681 void _lttng_event_destroy(struct lttng_event
*event
)
683 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
685 cds_list_del(&event
->node
);
686 lttng_destroy_context(event
->ctx
);
687 lttng_free_event_filter_runtime(event
);
688 /* Free event enabler refs */
689 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
690 &event
->enablers_ref_head
, node
)
695 void lttng_ust_events_exit(void)
697 struct lttng_session
*session
, *tmpsession
;
699 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
700 lttng_session_destroy(session
);
704 * Enabler management.
706 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
707 struct lttng_ust_event
*event_param
,
708 struct lttng_channel
*chan
)
710 struct lttng_enabler
*enabler
;
712 enabler
= zmalloc(sizeof(*enabler
));
715 enabler
->type
= type
;
716 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
717 CDS_INIT_LIST_HEAD(&enabler
->excluder_head
);
718 memcpy(&enabler
->event_param
, event_param
,
719 sizeof(enabler
->event_param
));
720 enabler
->chan
= chan
;
722 enabler
->enabled
= 1;
723 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
724 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
728 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
730 enabler
->enabled
= 1;
731 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
735 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
737 enabler
->enabled
= 0;
738 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
742 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
743 struct lttng_ust_filter_bytecode_node
*bytecode
)
745 bytecode
->enabler
= enabler
;
746 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
747 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
751 int lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
752 struct lttng_ust_excluder_node
*excluder
)
754 excluder
->enabler
= enabler
;
755 cds_list_add_tail(&excluder
->node
, &enabler
->excluder_head
);
756 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
760 int lttng_attach_context(struct lttng_ust_context
*context_param
,
761 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
764 * We cannot attach a context after trace has been started for a
765 * session because the metadata does not allow expressing this
766 * information outside of the original channel scope.
768 if (session
->been_active
)
771 switch (context_param
->ctx
) {
772 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
773 return lttng_add_pthread_id_to_ctx(ctx
);
774 case LTTNG_UST_CONTEXT_VTID
:
775 return lttng_add_vtid_to_ctx(ctx
);
776 case LTTNG_UST_CONTEXT_VPID
:
777 return lttng_add_vpid_to_ctx(ctx
);
778 case LTTNG_UST_CONTEXT_PROCNAME
:
779 return lttng_add_procname_to_ctx(ctx
);
780 case LTTNG_UST_CONTEXT_IP
:
781 return lttng_add_ip_to_ctx(ctx
);
787 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
788 struct lttng_ust_context
*context_param
)
790 #if 0 // disabled for now.
791 struct lttng_session
*session
= enabler
->chan
->session
;
794 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
798 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
804 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
806 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
807 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
809 /* Destroy filter bytecode */
810 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
811 &enabler
->filter_bytecode_head
, node
) {
815 /* Destroy excluders */
816 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
817 &enabler
->excluder_head
, node
) {
821 /* Destroy contexts */
822 lttng_destroy_context(enabler
->ctx
);
824 cds_list_del(&enabler
->node
);
829 * lttng_session_sync_enablers should be called just before starting a
833 void lttng_session_sync_enablers(struct lttng_session
*session
)
835 struct lttng_enabler
*enabler
;
836 struct lttng_event
*event
;
838 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
839 lttng_enabler_ref_events(enabler
);
841 * For each event, if at least one of its enablers is enabled,
842 * and its channel and session transient states are enabled, we
843 * enable the event, else we disable it.
845 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
846 struct lttng_enabler_ref
*enabler_ref
;
847 struct lttng_bytecode_runtime
*runtime
;
848 int enabled
= 0, has_enablers_without_bytecode
= 0;
851 cds_list_for_each_entry(enabler_ref
,
852 &event
->enablers_ref_head
, node
) {
853 if (enabler_ref
->ref
->enabled
) {
859 * Enabled state is based on union of enablers, with
860 * intesection of session and channel transient enable
863 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
865 CMM_STORE_SHARED(event
->enabled
, enabled
);
867 * Sync tracepoint registration with event enabled
871 if (!event
->registered
)
872 register_event(event
);
874 if (event
->registered
)
875 unregister_event(event
);
878 /* Check if has enablers without bytecode enabled */
879 cds_list_for_each_entry(enabler_ref
,
880 &event
->enablers_ref_head
, node
) {
881 if (enabler_ref
->ref
->enabled
882 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
883 has_enablers_without_bytecode
= 1;
887 event
->has_enablers_without_bytecode
=
888 has_enablers_without_bytecode
;
891 cds_list_for_each_entry(runtime
,
892 &event
->bytecode_runtime_head
, node
) {
893 lttng_filter_sync_state(runtime
);
899 * Apply enablers to session events, adding events to session if need
900 * be. It is required after each modification applied to an active
901 * session, and right before session "start".
902 * "lazy" sync means we only sync if required.
905 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
907 /* We can skip if session is not active */
908 if (!session
->active
)
910 lttng_session_sync_enablers(session
);