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
,
110 loglevel
= TRACE_DEFAULT
;
112 case LTTNG_UST_LOGLEVEL_RANGE
:
113 if (loglevel
<= req_loglevel
|| req_loglevel
== -1)
117 case LTTNG_UST_LOGLEVEL_SINGLE
:
118 if (loglevel
== req_loglevel
|| req_loglevel
== -1)
122 case LTTNG_UST_LOGLEVEL_ALL
:
128 void synchronize_trace(void)
133 struct lttng_session
*lttng_session_create(void)
135 struct lttng_session
*session
;
138 session
= zmalloc(sizeof(struct lttng_session
));
141 CDS_INIT_LIST_HEAD(&session
->chan_head
);
142 CDS_INIT_LIST_HEAD(&session
->events_head
);
143 CDS_INIT_LIST_HEAD(&session
->enablers_head
);
144 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
145 CDS_INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
146 cds_list_add(&session
->node
, &sessions
);
151 * Only used internally at session destruction.
154 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
156 struct channel
*chan
;
157 struct lttng_ust_shm_handle
*handle
;
159 cds_list_del(<tng_chan
->node
);
160 lttng_destroy_context(lttng_chan
->ctx
);
161 chan
= lttng_chan
->chan
;
162 handle
= lttng_chan
->handle
;
164 * note: lttng_chan is private data contained within handle. It
165 * will be freed along with the handle.
167 channel_destroy(chan
, handle
, 0);
171 void register_event(struct lttng_event
*event
)
174 const struct lttng_event_desc
*desc
;
176 assert(event
->registered
== 0);
178 ret
= __tracepoint_probe_register(desc
->name
,
179 desc
->probe_callback
,
180 event
, desc
->signature
);
183 event
->registered
= 1;
187 void unregister_event(struct lttng_event
*event
)
190 const struct lttng_event_desc
*desc
;
192 assert(event
->registered
== 1);
194 ret
= __tracepoint_probe_unregister(desc
->name
,
195 desc
->probe_callback
,
199 event
->registered
= 0;
203 * Only used internally at session destruction.
206 void _lttng_event_unregister(struct lttng_event
*event
)
208 if (event
->registered
)
209 unregister_event(event
);
212 void lttng_session_destroy(struct lttng_session
*session
)
214 struct lttng_channel
*chan
, *tmpchan
;
215 struct lttng_event
*event
, *tmpevent
;
216 struct lttng_enabler
*enabler
, *tmpenabler
;
218 CMM_ACCESS_ONCE(session
->active
) = 0;
219 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
220 _lttng_event_unregister(event
);
222 synchronize_trace(); /* Wait for in-flight events to complete */
223 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
224 &session
->enablers_head
, node
)
225 lttng_enabler_destroy(enabler
);
226 cds_list_for_each_entry_safe(event
, tmpevent
,
227 &session
->events_head
, node
)
228 _lttng_event_destroy(event
);
229 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
230 _lttng_channel_unmap(chan
);
231 cds_list_del(&session
->node
);
235 int lttng_session_enable(struct lttng_session
*session
)
238 struct lttng_channel
*chan
;
241 if (session
->active
) {
246 notify_socket
= lttng_get_notify_socket(session
->owner
);
247 if (notify_socket
< 0)
248 return notify_socket
;
250 /* Set transient enabler state to "enabled" */
252 /* We need to sync enablers with session before activation. */
253 lttng_session_sync_enablers(session
);
256 * Snapshot the number of events per channel to know the type of header
259 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
260 const struct lttng_ctx
*ctx
;
261 const struct lttng_ctx_field
*fields
= NULL
;
262 size_t nr_fields
= 0;
265 /* don't change it if session stop/restart */
266 if (chan
->header_type
)
270 nr_fields
= ctx
->nr_fields
;
271 fields
= ctx
->fields
;
273 ret
= ustcomm_register_channel(notify_socket
,
281 DBG("Error (%d) registering channel to sessiond", ret
);
284 if (chan_id
!= chan
->id
) {
285 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
291 /* Set atomically the state to "active" */
292 CMM_ACCESS_ONCE(session
->active
) = 1;
293 CMM_ACCESS_ONCE(session
->been_active
) = 1;
298 int lttng_session_disable(struct lttng_session
*session
)
302 if (!session
->active
) {
306 /* Set atomically the state to "inactive" */
307 CMM_ACCESS_ONCE(session
->active
) = 0;
309 /* Set transient enabler state to "disabled" */
311 lttng_session_sync_enablers(session
);
316 int lttng_channel_enable(struct lttng_channel
*channel
)
320 if (channel
->enabled
) {
324 /* Set transient enabler state to "enabled" */
326 lttng_session_sync_enablers(channel
->session
);
327 /* Set atomically the state to "enabled" */
328 CMM_ACCESS_ONCE(channel
->enabled
) = 1;
333 int lttng_channel_disable(struct lttng_channel
*channel
)
337 if (!channel
->enabled
) {
341 /* Set atomically the state to "disabled" */
342 CMM_ACCESS_ONCE(channel
->enabled
) = 0;
343 /* Set transient enabler state to "enabled" */
345 lttng_session_sync_enablers(channel
->session
);
351 * Supports event creation while tracing session is active.
354 int lttng_event_create(const struct lttng_event_desc
*desc
,
355 struct lttng_channel
*chan
)
357 const char *event_name
= desc
->name
;
358 struct lttng_event
*event
;
359 struct lttng_session
*session
= chan
->session
;
360 struct cds_hlist_head
*head
;
361 struct cds_hlist_node
*node
;
363 size_t name_len
= strlen(event_name
);
365 int notify_socket
, loglevel
;
368 hash
= jhash(event_name
, name_len
, 0);
369 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
370 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
372 if (!strncmp(event
->desc
->name
, desc
->name
,
373 LTTNG_UST_SYM_NAME_LEN
- 1)
374 && chan
== event
->chan
) {
380 notify_socket
= lttng_get_notify_socket(session
->owner
);
381 if (notify_socket
< 0) {
387 * Check if loglevel match. Refuse to connect event if not.
389 event
= zmalloc(sizeof(struct lttng_event
));
396 /* Event will be enabled by enabler sync. */
398 event
->registered
= 0;
399 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
400 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
404 loglevel
= *(*event
->desc
->loglevel
);
406 loglevel
= TRACE_DEFAULT
;
407 if (desc
->u
.ext
.model_emf_uri
)
408 uri
= *(desc
->u
.ext
.model_emf_uri
);
412 /* Fetch event ID from sessiond */
413 ret
= ustcomm_register_event(notify_socket
,
424 DBG("Error (%d) registering event to sessiond", ret
);
425 goto sessiond_register_error
;
428 /* Populate lttng_event structure before tracepoint registration. */
430 cds_list_add(&event
->node
, &chan
->session
->events_head
);
431 cds_hlist_add_head(&event
->hlist
, head
);
434 sessiond_register_error
:
443 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc
*desc
,
444 struct lttng_enabler
*enabler
)
447 unsigned int has_loglevel
= 0;
449 assert(enabler
->type
== LTTNG_ENABLER_WILDCARD
);
450 /* Compare excluding final '*' */
451 if (strncmp(desc
->name
, enabler
->event_param
.name
,
452 strlen(enabler
->event_param
.name
) - 1))
454 if (desc
->loglevel
) {
455 loglevel
= *(*desc
->loglevel
);
458 if (!lttng_loglevel_match(loglevel
,
460 enabler
->event_param
.loglevel_type
,
461 enabler
->event_param
.loglevel
))
467 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
468 struct lttng_enabler
*enabler
)
471 unsigned int has_loglevel
= 0;
473 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
474 if (strcmp(desc
->name
, enabler
->event_param
.name
))
476 if (desc
->loglevel
) {
477 loglevel
= *(*desc
->loglevel
);
480 if (!lttng_loglevel_match(loglevel
,
482 enabler
->event_param
.loglevel_type
,
483 enabler
->event_param
.loglevel
))
489 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
490 struct lttng_enabler
*enabler
)
492 switch (enabler
->type
) {
493 case LTTNG_ENABLER_WILDCARD
:
494 return lttng_desc_match_wildcard_enabler(desc
, enabler
);
495 case LTTNG_ENABLER_EVENT
:
496 return lttng_desc_match_event_enabler(desc
, enabler
);
503 int lttng_event_match_enabler(struct lttng_event
*event
,
504 struct lttng_enabler
*enabler
)
506 if (lttng_desc_match_enabler(event
->desc
, enabler
)
507 && event
->chan
== enabler
->chan
)
514 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
515 struct lttng_enabler
*enabler
)
517 struct lttng_enabler_ref
*enabler_ref
;
519 cds_list_for_each_entry(enabler_ref
,
520 &event
->enablers_ref_head
, node
) {
521 if (enabler_ref
->ref
== enabler
)
528 * Create struct lttng_event if it is missing and present in the list of
532 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
534 struct lttng_session
*session
= enabler
->chan
->session
;
535 struct lttng_probe_desc
*probe_desc
;
536 const struct lttng_event_desc
*desc
;
537 struct lttng_event
*event
;
539 struct cds_list_head
*probe_list
;
541 probe_list
= lttng_get_probe_list_head();
543 * For each probe event, if we find that a probe event matches
544 * our enabler, create an associated lttng_event if not
547 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
548 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
550 struct cds_hlist_head
*head
;
551 struct cds_hlist_node
*node
;
552 const char *event_name
;
556 desc
= probe_desc
->event_desc
[i
];
557 if (!lttng_desc_match_enabler(desc
, enabler
))
559 event_name
= desc
->name
;
560 name_len
= strlen(event_name
);
563 * Check if already created.
565 hash
= jhash(event_name
, name_len
, 0);
566 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
567 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
568 if (event
->desc
== desc
569 && event
->chan
== enabler
->chan
)
576 * We need to create an event for this
579 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
582 DBG("Unable to create event %s, error %d\n",
583 probe_desc
->event_desc
[i
]->name
, ret
);
590 * Create events associated with an enabler (if not already present),
591 * and add backward reference from the event to the enabler.
594 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
596 struct lttng_session
*session
= enabler
->chan
->session
;
597 struct lttng_event
*event
;
599 /* First ensure that probe events are created for this enabler. */
600 lttng_create_event_if_missing(enabler
);
602 /* For each event matching enabler in session event list. */
603 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
604 struct lttng_enabler_ref
*enabler_ref
;
606 if (!lttng_event_match_enabler(event
, enabler
))
609 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
612 * If no backward ref, create it.
613 * Add backward ref from event to enabler.
615 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
618 enabler_ref
->ref
= enabler
;
619 cds_list_add(&enabler_ref
->node
,
620 &event
->enablers_ref_head
);
624 * Link filter bytecodes if not linked yet.
626 lttng_enabler_event_link_bytecode(event
, enabler
);
628 /* TODO: merge event context. */
634 * Called at library load: connect the probe on all enablers matching
636 * Called with session mutex held.
638 int lttng_fix_pending_events(void)
640 struct lttng_session
*session
;
642 cds_list_for_each_entry(session
, &sessions
, node
) {
643 lttng_session_lazy_sync_enablers(session
);
649 * Only used internally at session destruction.
652 void _lttng_event_destroy(struct lttng_event
*event
)
654 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
656 cds_list_del(&event
->node
);
657 lttng_destroy_context(event
->ctx
);
658 lttng_free_event_filter_runtime(event
);
659 /* Free event enabler refs */
660 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
661 &event
->enablers_ref_head
, node
)
666 void lttng_ust_events_exit(void)
668 struct lttng_session
*session
, *tmpsession
;
670 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
671 lttng_session_destroy(session
);
675 * Enabler management.
677 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
678 struct lttng_ust_event
*event_param
,
679 struct lttng_channel
*chan
)
681 struct lttng_enabler
*enabler
;
683 enabler
= zmalloc(sizeof(*enabler
));
686 enabler
->type
= type
;
687 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
688 memcpy(&enabler
->event_param
, event_param
,
689 sizeof(enabler
->event_param
));
690 enabler
->chan
= chan
;
692 enabler
->enabled
= 1;
693 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
694 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
698 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
700 enabler
->enabled
= 1;
701 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
705 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
707 enabler
->enabled
= 0;
708 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
712 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
713 struct lttng_ust_filter_bytecode_node
*bytecode
)
715 bytecode
->enabler
= enabler
;
716 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
717 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
721 int lttng_attach_context(struct lttng_ust_context
*context_param
,
722 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
725 * We cannot attach a context after trace has been started for a
726 * session because the metadata does not allow expressing this
727 * information outside of the original channel scope.
729 if (session
->been_active
)
732 switch (context_param
->ctx
) {
733 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
734 return lttng_add_pthread_id_to_ctx(ctx
);
735 case LTTNG_UST_CONTEXT_VTID
:
736 return lttng_add_vtid_to_ctx(ctx
);
737 case LTTNG_UST_CONTEXT_VPID
:
738 return lttng_add_vpid_to_ctx(ctx
);
739 case LTTNG_UST_CONTEXT_PROCNAME
:
740 return lttng_add_procname_to_ctx(ctx
);
741 case LTTNG_UST_CONTEXT_IP
:
742 return lttng_add_ip_to_ctx(ctx
);
748 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
749 struct lttng_ust_context
*context_param
)
751 #if 0 // disabled for now.
752 struct lttng_session
*session
= enabler
->chan
->session
;
755 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
759 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
765 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
767 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
769 /* Destroy filter bytecode */
770 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
771 &enabler
->filter_bytecode_head
, node
) {
775 /* Destroy contexts */
776 lttng_destroy_context(enabler
->ctx
);
778 cds_list_del(&enabler
->node
);
783 * lttng_session_sync_enablers should be called just before starting a
787 void lttng_session_sync_enablers(struct lttng_session
*session
)
789 struct lttng_enabler
*enabler
;
790 struct lttng_event
*event
;
792 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
793 lttng_enabler_ref_events(enabler
);
795 * For each event, if at least one of its enablers is enabled,
796 * and its channel and session transient states are enabled, we
797 * enable the event, else we disable it.
799 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
800 struct lttng_enabler_ref
*enabler_ref
;
801 struct lttng_bytecode_runtime
*runtime
;
802 int enabled
= 0, has_enablers_without_bytecode
= 0;
805 cds_list_for_each_entry(enabler_ref
,
806 &event
->enablers_ref_head
, node
) {
807 if (enabler_ref
->ref
->enabled
) {
813 * Enabled state is based on union of enablers, with
814 * intesection of session and channel transient enable
817 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
819 CMM_STORE_SHARED(event
->enabled
, enabled
);
821 * Sync tracepoint registration with event enabled
825 if (!event
->registered
)
826 register_event(event
);
828 if (event
->registered
)
829 unregister_event(event
);
832 /* Check if has enablers without bytecode enabled */
833 cds_list_for_each_entry(enabler_ref
,
834 &event
->enablers_ref_head
, node
) {
835 if (enabler_ref
->ref
->enabled
836 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
837 has_enablers_without_bytecode
= 1;
841 event
->has_enablers_without_bytecode
=
842 has_enablers_without_bytecode
;
845 cds_list_for_each_entry(runtime
,
846 &event
->bytecode_runtime_head
, node
) {
847 lttng_filter_sync_state(runtime
);
853 * Apply enablers to session events, adding events to session if need
854 * be. It is required after each modification applied to an active
855 * session, and right before session "start".
856 * "lazy" sync means we only sync if required.
859 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
861 /* We can skip if session is not active */
862 if (!session
->active
)
864 lttng_session_sync_enablers(session
);