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 session
->statedump_pending
= 1;
298 lttng_ust_sockinfo_session_enabled(session
->owner
);
303 int lttng_session_disable(struct lttng_session
*session
)
307 if (!session
->active
) {
311 /* Set atomically the state to "inactive" */
312 CMM_ACCESS_ONCE(session
->active
) = 0;
314 /* Set transient enabler state to "disabled" */
316 lttng_session_sync_enablers(session
);
321 int lttng_channel_enable(struct lttng_channel
*channel
)
325 if (channel
->enabled
) {
329 /* Set transient enabler state to "enabled" */
331 lttng_session_sync_enablers(channel
->session
);
332 /* Set atomically the state to "enabled" */
333 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
338 int lttng_channel_disable(struct lttng_channel
*channel
)
342 if (!channel
->enabled
) {
346 /* Set atomically the state to "disabled" */
347 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
348 /* Set transient enabler state to "enabled" */
350 lttng_session_sync_enablers(channel
->session
);
356 * Supports event creation while tracing session is active.
359 int lttng_event_create(const struct lttng_event_desc
*desc
,
360 struct lttng_channel
*chan
)
362 const char *event_name
= desc
->name
;
363 struct lttng_event
*event
;
364 struct lttng_session
*session
= chan
->session
;
365 struct cds_hlist_head
*head
;
366 struct cds_hlist_node
*node
;
368 size_t name_len
= strlen(event_name
);
370 int notify_socket
, loglevel
;
373 hash
= jhash(event_name
, name_len
, 0);
374 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
375 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
377 if (!strncmp(event
->desc
->name
, desc
->name
,
378 LTTNG_UST_SYM_NAME_LEN
- 1)
379 && chan
== event
->chan
) {
385 notify_socket
= lttng_get_notify_socket(session
->owner
);
386 if (notify_socket
< 0) {
392 * Check if loglevel match. Refuse to connect event if not.
394 event
= zmalloc(sizeof(struct lttng_event
));
401 /* Event will be enabled by enabler sync. */
403 event
->registered
= 0;
404 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
405 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
409 loglevel
= *(*event
->desc
->loglevel
);
411 loglevel
= TRACE_DEFAULT
;
412 if (desc
->u
.ext
.model_emf_uri
)
413 uri
= *(desc
->u
.ext
.model_emf_uri
);
417 /* Fetch event ID from sessiond */
418 ret
= ustcomm_register_event(notify_socket
,
429 DBG("Error (%d) registering event to sessiond", ret
);
430 goto sessiond_register_error
;
433 /* Populate lttng_event structure before tracepoint registration. */
435 cds_list_add(&event
->node
, &chan
->session
->events_head
);
436 cds_hlist_add_head(&event
->hlist
, head
);
439 sessiond_register_error
:
448 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc
*desc
,
449 struct lttng_enabler
*enabler
)
452 unsigned int has_loglevel
= 0;
454 assert(enabler
->type
== LTTNG_ENABLER_WILDCARD
);
455 /* Compare excluding final '*' */
456 if (strncmp(desc
->name
, enabler
->event_param
.name
,
457 strlen(enabler
->event_param
.name
) - 1))
459 if (desc
->loglevel
) {
460 loglevel
= *(*desc
->loglevel
);
463 if (!lttng_loglevel_match(loglevel
,
465 enabler
->event_param
.loglevel_type
,
466 enabler
->event_param
.loglevel
))
472 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
473 struct lttng_enabler
*enabler
)
476 unsigned int has_loglevel
= 0;
478 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
479 if (strcmp(desc
->name
, enabler
->event_param
.name
))
481 if (desc
->loglevel
) {
482 loglevel
= *(*desc
->loglevel
);
485 if (!lttng_loglevel_match(loglevel
,
487 enabler
->event_param
.loglevel_type
,
488 enabler
->event_param
.loglevel
))
494 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
495 struct lttng_enabler
*enabler
)
497 struct lttng_ust_excluder_node
*excluder
;
499 /* If event matches with an excluder, return 'does not match' */
500 cds_list_for_each_entry(excluder
, &enabler
->excluder_head
, node
) {
503 for (count
= 0; count
< excluder
->excluder
.count
; count
++) {
507 excluder_name
= (char *) (excluder
->excluder
.names
)
508 + count
* LTTNG_UST_SYM_NAME_LEN
;
509 len
= strnlen(excluder_name
, LTTNG_UST_SYM_NAME_LEN
);
510 if (len
> 0 && excluder_name
[len
- 1] == '*') {
511 found
= !strncmp(desc
->name
, excluder_name
,
514 found
= !strncmp(desc
->name
, excluder_name
,
515 LTTNG_UST_SYM_NAME_LEN
- 1);
522 switch (enabler
->type
) {
523 case LTTNG_ENABLER_WILDCARD
:
524 return lttng_desc_match_wildcard_enabler(desc
, enabler
);
525 case LTTNG_ENABLER_EVENT
:
526 return lttng_desc_match_event_enabler(desc
, enabler
);
533 int lttng_event_match_enabler(struct lttng_event
*event
,
534 struct lttng_enabler
*enabler
)
536 if (lttng_desc_match_enabler(event
->desc
, enabler
)
537 && event
->chan
== enabler
->chan
)
544 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
545 struct lttng_enabler
*enabler
)
547 struct lttng_enabler_ref
*enabler_ref
;
549 cds_list_for_each_entry(enabler_ref
,
550 &event
->enablers_ref_head
, node
) {
551 if (enabler_ref
->ref
== enabler
)
558 * Create struct lttng_event if it is missing and present in the list of
562 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
564 struct lttng_session
*session
= enabler
->chan
->session
;
565 struct lttng_probe_desc
*probe_desc
;
566 const struct lttng_event_desc
*desc
;
567 struct lttng_event
*event
;
569 struct cds_list_head
*probe_list
;
571 probe_list
= lttng_get_probe_list_head();
573 * For each probe event, if we find that a probe event matches
574 * our enabler, create an associated lttng_event if not
577 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
578 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
580 struct cds_hlist_head
*head
;
581 struct cds_hlist_node
*node
;
582 const char *event_name
;
586 desc
= probe_desc
->event_desc
[i
];
587 if (!lttng_desc_match_enabler(desc
, enabler
))
589 event_name
= desc
->name
;
590 name_len
= strlen(event_name
);
593 * Check if already created.
595 hash
= jhash(event_name
, name_len
, 0);
596 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
597 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
598 if (event
->desc
== desc
599 && event
->chan
== enabler
->chan
)
606 * We need to create an event for this
609 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
612 DBG("Unable to create event %s, error %d\n",
613 probe_desc
->event_desc
[i
]->name
, ret
);
620 * Create events associated with an enabler (if not already present),
621 * and add backward reference from the event to the enabler.
624 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
626 struct lttng_session
*session
= enabler
->chan
->session
;
627 struct lttng_event
*event
;
629 /* First ensure that probe events are created for this enabler. */
630 lttng_create_event_if_missing(enabler
);
632 /* For each event matching enabler in session event list. */
633 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
634 struct lttng_enabler_ref
*enabler_ref
;
636 if (!lttng_event_match_enabler(event
, enabler
))
639 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
642 * If no backward ref, create it.
643 * Add backward ref from event to enabler.
645 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
648 enabler_ref
->ref
= enabler
;
649 cds_list_add(&enabler_ref
->node
,
650 &event
->enablers_ref_head
);
654 * Link filter bytecodes if not linked yet.
656 lttng_enabler_event_link_bytecode(event
, enabler
);
658 /* TODO: merge event context. */
664 * Called at library load: connect the probe on all enablers matching
666 * Called with session mutex held.
668 int lttng_fix_pending_events(void)
670 struct lttng_session
*session
;
672 cds_list_for_each_entry(session
, &sessions
, node
) {
673 lttng_session_lazy_sync_enablers(session
);
679 * Called after session enable: For each session, execute pending statedumps.
681 int lttng_handle_pending_statedumps(t_statedump_func_ptr statedump_func_ptr
)
683 struct lttng_session
*session
;
685 cds_list_for_each_entry(session
, &sessions
, node
) {
686 if (session
->statedump_pending
) {
687 session
->statedump_pending
= 0;
688 statedump_func_ptr(session
);
695 * Only used internally at session destruction.
698 void _lttng_event_destroy(struct lttng_event
*event
)
700 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
702 cds_list_del(&event
->node
);
703 lttng_destroy_context(event
->ctx
);
704 lttng_free_event_filter_runtime(event
);
705 /* Free event enabler refs */
706 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
707 &event
->enablers_ref_head
, node
)
712 void lttng_ust_events_exit(void)
714 struct lttng_session
*session
, *tmpsession
;
716 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
717 lttng_session_destroy(session
);
721 * Enabler management.
723 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
724 struct lttng_ust_event
*event_param
,
725 struct lttng_channel
*chan
)
727 struct lttng_enabler
*enabler
;
729 enabler
= zmalloc(sizeof(*enabler
));
732 enabler
->type
= type
;
733 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
734 CDS_INIT_LIST_HEAD(&enabler
->excluder_head
);
735 memcpy(&enabler
->event_param
, event_param
,
736 sizeof(enabler
->event_param
));
737 enabler
->chan
= chan
;
739 enabler
->enabled
= 1;
740 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
741 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
745 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
747 enabler
->enabled
= 1;
748 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
752 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
754 enabler
->enabled
= 0;
755 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
759 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
760 struct lttng_ust_filter_bytecode_node
*bytecode
)
762 bytecode
->enabler
= enabler
;
763 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
764 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
768 int lttng_enabler_attach_exclusion(struct lttng_enabler
*enabler
,
769 struct lttng_ust_excluder_node
*excluder
)
771 excluder
->enabler
= enabler
;
772 cds_list_add_tail(&excluder
->node
, &enabler
->excluder_head
);
773 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
777 int lttng_attach_context(struct lttng_ust_context
*context_param
,
778 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
781 * We cannot attach a context after trace has been started for a
782 * session because the metadata does not allow expressing this
783 * information outside of the original channel scope.
785 if (session
->been_active
)
788 switch (context_param
->ctx
) {
789 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
790 return lttng_add_pthread_id_to_ctx(ctx
);
791 case LTTNG_UST_CONTEXT_VTID
:
792 return lttng_add_vtid_to_ctx(ctx
);
793 case LTTNG_UST_CONTEXT_VPID
:
794 return lttng_add_vpid_to_ctx(ctx
);
795 case LTTNG_UST_CONTEXT_PROCNAME
:
796 return lttng_add_procname_to_ctx(ctx
);
797 case LTTNG_UST_CONTEXT_IP
:
798 return lttng_add_ip_to_ctx(ctx
);
804 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
805 struct lttng_ust_context
*context_param
)
807 #if 0 // disabled for now.
808 struct lttng_session
*session
= enabler
->chan
->session
;
811 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
815 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
821 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
823 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
824 struct lttng_ust_excluder_node
*excluder_node
, *tmp_excluder_node
;
826 /* Destroy filter bytecode */
827 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
828 &enabler
->filter_bytecode_head
, node
) {
832 /* Destroy excluders */
833 cds_list_for_each_entry_safe(excluder_node
, tmp_excluder_node
,
834 &enabler
->excluder_head
, node
) {
838 /* Destroy contexts */
839 lttng_destroy_context(enabler
->ctx
);
841 cds_list_del(&enabler
->node
);
846 * lttng_session_sync_enablers should be called just before starting a
850 void lttng_session_sync_enablers(struct lttng_session
*session
)
852 struct lttng_enabler
*enabler
;
853 struct lttng_event
*event
;
855 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
856 lttng_enabler_ref_events(enabler
);
858 * For each event, if at least one of its enablers is enabled,
859 * and its channel and session transient states are enabled, we
860 * enable the event, else we disable it.
862 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
863 struct lttng_enabler_ref
*enabler_ref
;
864 struct lttng_bytecode_runtime
*runtime
;
865 int enabled
= 0, has_enablers_without_bytecode
= 0;
868 cds_list_for_each_entry(enabler_ref
,
869 &event
->enablers_ref_head
, node
) {
870 if (enabler_ref
->ref
->enabled
) {
876 * Enabled state is based on union of enablers, with
877 * intesection of session and channel transient enable
880 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
882 CMM_STORE_SHARED(event
->enabled
, enabled
);
884 * Sync tracepoint registration with event enabled
888 if (!event
->registered
)
889 register_event(event
);
891 if (event
->registered
)
892 unregister_event(event
);
895 /* Check if has enablers without bytecode enabled */
896 cds_list_for_each_entry(enabler_ref
,
897 &event
->enablers_ref_head
, node
) {
898 if (enabler_ref
->ref
->enabled
899 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
900 has_enablers_without_bytecode
= 1;
904 event
->has_enablers_without_bytecode
=
905 has_enablers_without_bytecode
;
908 cds_list_for_each_entry(runtime
,
909 &event
->bytecode_runtime_head
, node
) {
910 lttng_filter_sync_state(runtime
);
916 * Apply enablers to session events, adding events to session if need
917 * be. It is required after each modification applied to an active
918 * session, and right before session "start".
919 * "lazy" sync means we only sync if required.
922 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
924 /* We can skip if session is not active */
925 if (!session
->active
)
927 lttng_session_sync_enablers(session
);