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>
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
);
81 static int _lttng_event_unregister(struct lttng_event
*event
);
83 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
84 struct lttng_channel
*chan
,
85 struct lttng_event
*event
);
87 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
90 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
92 void lttng_session_sync_enablers(struct lttng_session
*session
);
94 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
97 * Called with ust lock held.
99 int lttng_session_active(void)
101 struct lttng_session
*iter
;
103 cds_list_for_each_entry(iter
, &sessions
, node
) {
111 int lttng_loglevel_match(int loglevel
,
112 unsigned int has_loglevel
,
113 enum lttng_ust_loglevel_type req_type
,
116 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
119 loglevel
= TRACE_DEFAULT
;
121 case LTTNG_UST_LOGLEVEL_RANGE
:
122 if (loglevel
<= req_loglevel
|| req_loglevel
== -1)
126 case LTTNG_UST_LOGLEVEL_SINGLE
:
127 if (loglevel
== req_loglevel
|| req_loglevel
== -1)
131 case LTTNG_UST_LOGLEVEL_ALL
:
137 void synchronize_trace(void)
142 struct lttng_session
*lttng_session_create(void)
144 struct lttng_session
*session
;
147 session
= zmalloc(sizeof(struct lttng_session
));
150 CDS_INIT_LIST_HEAD(&session
->chan_head
);
151 CDS_INIT_LIST_HEAD(&session
->events_head
);
152 CDS_INIT_LIST_HEAD(&session
->enablers_head
);
153 for (i
= 0; i
< LTTNG_UST_EVENT_HT_SIZE
; i
++)
154 CDS_INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
155 cds_list_add(&session
->node
, &sessions
);
160 * Only used internally at session destruction.
163 void _lttng_channel_unmap(struct lttng_channel
*lttng_chan
)
165 struct channel
*chan
;
166 struct lttng_ust_shm_handle
*handle
;
168 cds_list_del(<tng_chan
->node
);
169 lttng_destroy_context(lttng_chan
->ctx
);
170 chan
= lttng_chan
->chan
;
171 handle
= lttng_chan
->handle
;
173 * note: lttng_chan is private data contained within handle. It
174 * will be freed along with the handle.
176 channel_destroy(chan
, handle
, 0);
179 void lttng_session_destroy(struct lttng_session
*session
)
181 struct lttng_channel
*chan
, *tmpchan
;
182 struct lttng_event
*event
, *tmpevent
;
183 struct lttng_enabler
*enabler
, *tmpenabler
;
186 CMM_ACCESS_ONCE(session
->active
) = 0;
187 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
188 ret
= _lttng_event_unregister(event
);
191 synchronize_trace(); /* Wait for in-flight events to complete */
192 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
193 &session
->enablers_head
, node
)
194 lttng_enabler_destroy(enabler
);
195 cds_list_for_each_entry_safe(event
, tmpevent
,
196 &session
->events_head
, node
)
197 _lttng_event_destroy(event
);
198 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
199 _lttng_channel_unmap(chan
);
200 cds_list_del(&session
->node
);
204 int lttng_session_enable(struct lttng_session
*session
)
207 struct lttng_channel
*chan
;
210 if (session
->active
) {
215 notify_socket
= lttng_get_notify_socket(session
->owner
);
216 if (notify_socket
< 0)
217 return notify_socket
;
219 /* We need to sync enablers with session before activation. */
220 lttng_session_sync_enablers(session
);
223 * Snapshot the number of events per channel to know the type of header
226 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
227 const struct lttng_ctx
*ctx
;
228 const struct lttng_event_field
*fields
= NULL
;
229 size_t nr_fields
= 0;
231 /* don't change it if session stop/restart */
232 if (chan
->header_type
)
236 nr_fields
= ctx
->nr_fields
;
237 fields
= &ctx
->fields
->event_field
;
239 ret
= ustcomm_register_channel(notify_socket
,
250 CMM_ACCESS_ONCE(session
->active
) = 1;
251 CMM_ACCESS_ONCE(session
->been_active
) = 1;
252 ret
= _lttng_session_metadata_statedump(session
);
254 CMM_ACCESS_ONCE(session
->active
) = 0;
259 int lttng_session_disable(struct lttng_session
*session
)
263 if (!session
->active
) {
267 CMM_ACCESS_ONCE(session
->active
) = 0;
272 int lttng_channel_enable(struct lttng_channel
*channel
)
276 if (channel
== channel
->session
->metadata
)
278 old
= uatomic_xchg(&channel
->enabled
, 1);
284 int lttng_channel_disable(struct lttng_channel
*channel
)
288 if (channel
== channel
->session
->metadata
)
290 old
= uatomic_xchg(&channel
->enabled
, 0);
296 int lttng_event_enable(struct lttng_event
*event
)
300 if (event
->chan
== event
->chan
->session
->metadata
)
302 old
= uatomic_xchg(&event
->enabled
, 1);
308 int lttng_event_disable(struct lttng_event
*event
)
312 if (event
->chan
== event
->chan
->session
->metadata
)
314 old
= uatomic_xchg(&event
->enabled
, 0);
321 * Supports event creation while tracing session is active.
324 int lttng_event_create(const struct lttng_event_desc
*desc
,
325 struct lttng_channel
*chan
)
327 const char *event_name
= desc
->name
;
328 struct lttng_event
*event
;
329 struct lttng_session
*session
= chan
->session
;
330 struct cds_hlist_head
*head
;
331 struct cds_hlist_node
*node
;
333 size_t name_len
= strlen(event_name
);
335 int notify_socket
, loglevel
;
338 hash
= jhash(event_name
, name_len
, 0);
339 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
340 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
342 if (!strncmp(event
->desc
->name
,
344 LTTNG_UST_SYM_NAME_LEN
- 1)) {
350 notify_socket
= lttng_get_notify_socket(session
->owner
);
351 if (notify_socket
< 0) {
357 * Check if loglevel match. Refuse to connect event if not.
359 event
= zmalloc(sizeof(struct lttng_event
));
367 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
368 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
372 loglevel
= *(*event
->desc
->loglevel
);
374 loglevel
= TRACE_DEFAULT
;
375 if (desc
->u
.ext
.model_emf_uri
)
376 uri
= *(desc
->u
.ext
.model_emf_uri
);
380 /* Fetch event ID from sessiond */
381 ret
= ustcomm_register_event(notify_socket
,
392 goto sessiond_register_error
;
394 /* Populate lttng_event structure before tracepoint registration. */
396 ret
= __tracepoint_probe_register(event_name
,
397 desc
->probe_callback
,
398 event
, desc
->signature
);
400 goto tracepoint_register_error
;
402 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
404 goto statedump_error
;
405 cds_list_add(&event
->node
, &chan
->session
->events_head
);
406 cds_hlist_add_head(&event
->hlist
, head
);
410 WARN_ON_ONCE(__tracepoint_probe_unregister(event_name
,
411 desc
->probe_callback
,
413 tracepoint_register_error
:
414 sessiond_register_error
:
423 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc
*desc
,
424 struct lttng_enabler
*enabler
)
427 unsigned int has_loglevel
;
429 assert(enabler
->type
== LTTNG_ENABLER_WILDCARD
);
430 /* Compare excluding final '*' */
431 if (strncmp(desc
->name
, enabler
->event_param
.name
,
432 strlen(enabler
->event_param
.name
) - 1))
434 if (desc
->loglevel
) {
435 loglevel
= *(*desc
->loglevel
);
438 if (!lttng_loglevel_match(loglevel
,
440 enabler
->event_param
.loglevel_type
,
441 enabler
->event_param
.loglevel
))
447 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
448 struct lttng_enabler
*enabler
)
451 unsigned int has_loglevel
= 0;
453 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
454 if (strcmp(desc
->name
, enabler
->event_param
.name
))
456 if (desc
->loglevel
) {
457 loglevel
= *(*desc
->loglevel
);
460 if (!lttng_loglevel_match(loglevel
,
462 enabler
->event_param
.loglevel_type
,
463 enabler
->event_param
.loglevel
))
469 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
470 struct lttng_enabler
*enabler
)
472 switch (enabler
->type
) {
473 case LTTNG_ENABLER_WILDCARD
:
474 return lttng_desc_match_wildcard_enabler(desc
, enabler
);
475 case LTTNG_ENABLER_EVENT
:
476 return lttng_desc_match_event_enabler(desc
, enabler
);
483 int lttng_event_match_enabler(struct lttng_event
*event
,
484 struct lttng_enabler
*enabler
)
486 return lttng_desc_match_enabler(event
->desc
, enabler
);
490 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
491 struct lttng_enabler
*enabler
)
493 struct lttng_enabler_ref
*enabler_ref
;
495 cds_list_for_each_entry(enabler_ref
,
496 &event
->enablers_ref_head
, node
) {
497 if (enabler_ref
->ref
== enabler
)
504 * Create struct lttng_event if it is missing and present in the list of
508 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
510 struct lttng_session
*session
= enabler
->chan
->session
;
511 struct lttng_probe_desc
*probe_desc
;
512 const struct lttng_event_desc
*desc
;
513 struct lttng_event
*event
;
515 struct cds_list_head
*probe_list
;
517 probe_list
= lttng_get_probe_list_head();
519 * For each probe event, if we find that a probe event matches
520 * our enabler, create an associated lttng_event if not
523 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
524 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
526 struct cds_hlist_head
*head
;
527 struct cds_hlist_node
*node
;
528 const char *event_name
;
532 desc
= probe_desc
->event_desc
[i
];
533 if (!lttng_desc_match_enabler(desc
, enabler
))
535 event_name
= desc
->name
;
536 name_len
= strlen(event_name
);
539 * Check if already created.
541 hash
= jhash(event_name
, name_len
, 0);
542 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
543 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
544 if (event
->desc
== desc
)
551 * We need to create an event for this
554 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
557 DBG("Unable to create event %s, error %d\n",
558 probe_desc
->event_desc
[i
]->name
, ret
);
565 * Create events associated with an enabler (if not already present),
566 * and add backward reference from the event to the enabler.
569 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
571 struct lttng_session
*session
= enabler
->chan
->session
;
572 struct lttng_event
*event
;
574 /* First ensure that probe events are created for this enabler. */
575 lttng_create_event_if_missing(enabler
);
577 /* For each event matching enabler in session event list. */
578 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
579 struct lttng_enabler_ref
*enabler_ref
;
581 if (!lttng_event_match_enabler(event
, enabler
))
584 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
587 * If no backward ref, create it.
588 * Add backward ref from event to enabler.
590 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
593 enabler_ref
->ref
= enabler
;
594 cds_list_add(&enabler_ref
->node
,
595 &event
->enablers_ref_head
);
599 * Link filter bytecodes if not linked yet.
601 lttng_enabler_event_link_bytecode(event
, enabler
);
603 /* TODO: merge event context. */
609 * Called at library load: connect the probe on all enablers matching
611 * called with session mutex held.
612 * TODO: currently, for each desc added, we iterate on all event desc
613 * (inefficient). We should create specific code that only target the
616 int lttng_fix_pending_event_desc(const struct lttng_event_desc
*desc
)
618 struct lttng_session
*session
;
620 cds_list_for_each_entry(session
, &sessions
, node
) {
621 lttng_session_lazy_sync_enablers(session
);
627 * Only used internally at session destruction.
629 int _lttng_event_unregister(struct lttng_event
*event
)
631 return __tracepoint_probe_unregister(event
->desc
->name
,
632 event
->desc
->probe_callback
,
637 * Only used internally at session destruction.
640 void _lttng_event_destroy(struct lttng_event
*event
)
642 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
644 cds_list_del(&event
->node
);
645 lttng_destroy_context(event
->ctx
);
646 lttng_free_event_filter_runtime(event
);
647 /* Free event enabler refs */
648 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
649 &event
->enablers_ref_head
, node
)
655 * We have exclusive access to our metadata buffer (protected by the
656 * ust_lock), so we can do racy operations such as looking for
657 * remaining space left in packet and write, since mutual exclusion
658 * protects us from concurrent writes.
660 int lttng_metadata_printf(struct lttng_session
*session
,
661 const char *fmt
, ...)
663 struct lttng_ust_lib_ring_buffer_ctx ctx
;
664 struct lttng_channel
*chan
= session
->metadata
;
666 int ret
= 0, waitret
;
667 size_t len
, reserve_len
, pos
;
670 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
673 ret
= vasprintf(&str
, fmt
, ap
);
681 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
682 reserve_len
= min_t(size_t,
683 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
685 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
686 sizeof(char), -1, chan
->handle
);
688 * We don't care about metadata buffer's records lost
689 * count, because we always retry here. Report error if
690 * we need to bail out after timeout or being
693 waitret
= wait_cond_interruptible_timeout(
695 ret
= chan
->ops
->event_reserve(&ctx
, 0);
696 ret
!= -ENOBUFS
|| !ret
;
698 LTTNG_METADATA_TIMEOUT_MSEC
);
699 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
700 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
701 waitret
== -EINTR
? "interrupted" :
702 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
703 if (waitret
== -EINTR
)
707 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
708 chan
->ops
->event_commit(&ctx
);
716 int _lttng_field_statedump(struct lttng_session
*session
,
717 const struct lttng_event_field
*field
)
724 switch (field
->type
.atype
) {
726 ret
= lttng_metadata_printf(session
,
727 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
728 field
->type
.u
.basic
.integer
.size
,
729 field
->type
.u
.basic
.integer
.alignment
,
730 field
->type
.u
.basic
.integer
.signedness
,
731 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
733 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
736 field
->type
.u
.basic
.integer
.base
,
737 #if (BYTE_ORDER == BIG_ENDIAN)
738 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
740 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
745 ret
= lttng_metadata_printf(session
,
746 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
747 field
->type
.u
.basic
._float
.exp_dig
,
748 field
->type
.u
.basic
._float
.mant_dig
,
749 field
->type
.u
.basic
._float
.alignment
,
750 #if (BYTE_ORDER == BIG_ENDIAN)
751 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
753 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
758 ret
= lttng_metadata_printf(session
,
760 field
->type
.u
.basic
.enumeration
.name
,
765 const struct lttng_basic_type
*elem_type
;
767 elem_type
= &field
->type
.u
.array
.elem_type
;
768 ret
= lttng_metadata_printf(session
,
769 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
770 elem_type
->u
.basic
.integer
.size
,
771 elem_type
->u
.basic
.integer
.alignment
,
772 elem_type
->u
.basic
.integer
.signedness
,
773 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
775 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
778 elem_type
->u
.basic
.integer
.base
,
779 #if (BYTE_ORDER == BIG_ENDIAN)
780 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
782 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
784 field
->name
, field
->type
.u
.array
.length
);
789 const struct lttng_basic_type
*elem_type
;
790 const struct lttng_basic_type
*length_type
;
792 elem_type
= &field
->type
.u
.sequence
.elem_type
;
793 length_type
= &field
->type
.u
.sequence
.length_type
;
794 ret
= lttng_metadata_printf(session
,
795 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
796 length_type
->u
.basic
.integer
.size
,
797 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
798 length_type
->u
.basic
.integer
.signedness
,
799 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
801 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
804 length_type
->u
.basic
.integer
.base
,
805 #if (BYTE_ORDER == BIG_ENDIAN)
806 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
808 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
814 ret
= lttng_metadata_printf(session
,
815 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
816 elem_type
->u
.basic
.integer
.size
,
817 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
818 elem_type
->u
.basic
.integer
.signedness
,
819 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
821 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
824 elem_type
->u
.basic
.integer
.base
,
825 #if (BYTE_ORDER == BIG_ENDIAN)
826 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
828 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
836 /* Default encoding is UTF8 */
837 ret
= lttng_metadata_printf(session
,
839 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
840 " { encoding = ASCII; }" : "",
851 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
852 struct lttng_ctx
*ctx
)
859 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
860 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
862 ret
= _lttng_field_statedump(session
, &field
->event_field
);
870 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
871 struct lttng_event
*event
)
873 const struct lttng_event_desc
*desc
= event
->desc
;
877 for (i
= 0; i
< desc
->nr_fields
; i
++) {
878 const struct lttng_event_field
*field
= &desc
->fields
[i
];
880 ret
= _lttng_field_statedump(session
, field
);
888 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
889 struct lttng_channel
*chan
,
890 struct lttng_event
*event
)
893 int loglevel
= TRACE_DEFAULT
;
895 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
897 if (chan
== session
->metadata
)
900 * Don't print events for which probe load is pending.
905 ret
= lttng_metadata_printf(session
,
909 " stream_id = %u;\n",
916 if (event
->desc
->loglevel
)
917 loglevel
= *(*event
->desc
->loglevel
);
919 ret
= lttng_metadata_printf(session
,
925 if (event
->desc
->u
.ext
.model_emf_uri
) {
926 ret
= lttng_metadata_printf(session
,
927 " model.emf.uri = \"%s\";\n",
928 *(event
->desc
->u
.ext
.model_emf_uri
));
934 ret
= lttng_metadata_printf(session
,
935 " context := struct {\n");
939 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
943 ret
= lttng_metadata_printf(session
,
949 ret
= lttng_metadata_printf(session
,
950 " fields := struct {\n"
955 ret
= _lttng_fields_metadata_statedump(session
, event
);
960 * LTTng space reservation can only reserve multiples of the
963 ret
= lttng_metadata_printf(session
,
969 event
->metadata_dumped
= 1;
976 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
977 struct lttng_channel
*chan
)
981 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
983 if (chan
== session
->metadata
)
986 WARN_ON_ONCE(!chan
->header_type
);
987 ret
= lttng_metadata_printf(session
,
990 " event.header := %s;\n"
991 " packet.context := struct packet_context;\n",
993 chan
->header_type
== 1 ? "struct event_header_compact" :
994 "struct event_header_large");
999 ret
= lttng_metadata_printf(session
,
1000 " event.context := struct {\n");
1004 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1008 ret
= lttng_metadata_printf(session
,
1014 ret
= lttng_metadata_printf(session
,
1017 chan
->metadata_dumped
= 1;
1023 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1025 return lttng_metadata_printf(session
,
1026 "struct packet_context {\n"
1027 " uint64_clock_monotonic_t timestamp_begin;\n"
1028 " uint64_clock_monotonic_t timestamp_end;\n"
1029 " uint64_t content_size;\n"
1030 " uint64_t packet_size;\n"
1031 " unsigned long events_discarded;\n"
1032 " uint32_t cpu_id;\n"
1039 * id: range: 0 - 30.
1040 * id 31 is reserved to indicate an extended header.
1043 * id: range: 0 - 65534.
1044 * id 65535 is reserved to indicate an extended header.
1047 int _lttng_event_header_declare(struct lttng_session
*session
)
1049 return lttng_metadata_printf(session
,
1050 "struct event_header_compact {\n"
1051 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1054 " uint27_clock_monotonic_t timestamp;\n"
1058 " uint64_clock_monotonic_t timestamp;\n"
1063 "struct event_header_large {\n"
1064 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1067 " uint32_clock_monotonic_t timestamp;\n"
1071 " uint64_clock_monotonic_t timestamp;\n"
1075 lttng_alignof(uint32_t) * CHAR_BIT
,
1076 lttng_alignof(uint16_t) * CHAR_BIT
1081 * Approximation of NTP time of day to clock monotonic correlation,
1082 * taken at start of trace.
1083 * Yes, this is only an approximation. Yes, we can (and will) do better
1084 * in future versions.
1087 uint64_t measure_clock_offset(void)
1089 uint64_t offset
, monotonic
[2], realtime
;
1090 struct timespec rts
= { 0, 0 };
1093 monotonic
[0] = trace_clock_read64();
1094 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1097 monotonic
[1] = trace_clock_read64();
1098 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1099 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
1100 realtime
+= rts
.tv_nsec
;
1101 offset
= realtime
- offset
;
1106 * Output metadata into this session's metadata buffers.
1109 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1111 unsigned char *uuid_c
;
1112 char uuid_s
[LTTNG_UST_UUID_STR_LEN
],
1113 clock_uuid_s
[LTTNG_UST_UUID_STR_LEN
];
1114 struct lttng_channel
*chan
;
1115 struct lttng_event
*event
;
1117 char procname
[LTTNG_UST_PROCNAME_LEN
] = "";
1118 char hostname
[HOST_NAME_MAX
];
1120 if (!CMM_ACCESS_ONCE(session
->active
))
1122 if (session
->metadata_dumped
)
1124 if (!session
->metadata
) {
1125 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1128 uuid_c
= session
->metadata
->uuid
;
1130 snprintf(uuid_s
, sizeof(uuid_s
),
1131 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1132 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1133 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1134 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1135 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1137 ret
= lttng_metadata_printf(session
,
1138 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1139 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1140 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1141 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1142 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1143 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1144 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1150 " byte_order = %s;\n"
1151 " packet.header := struct {\n"
1152 " uint32_t magic;\n"
1153 " uint8_t uuid[16];\n"
1154 " uint32_t stream_id;\n"
1157 lttng_alignof(uint8_t) * CHAR_BIT
,
1158 lttng_alignof(uint16_t) * CHAR_BIT
,
1159 lttng_alignof(uint32_t) * CHAR_BIT
,
1160 lttng_alignof(uint64_t) * CHAR_BIT
,
1161 sizeof(unsigned long) * CHAR_BIT
,
1162 lttng_alignof(unsigned long) * CHAR_BIT
,
1166 #if (BYTE_ORDER == BIG_ENDIAN)
1175 /* ignore error, just use empty string if error. */
1177 ret
= gethostname(hostname
, sizeof(hostname
));
1178 if (ret
&& errno
== ENAMETOOLONG
)
1179 hostname
[HOST_NAME_MAX
- 1] = '\0';
1180 lttng_ust_getprocname(procname
);
1181 procname
[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
1182 ret
= lttng_metadata_printf(session
,
1184 " hostname = \"%s\";\n"
1186 " procname = \"%s\";\n"
1187 " domain = \"ust\";\n"
1188 " tracer_name = \"lttng-ust\";\n"
1189 " tracer_major = %u;\n"
1190 " tracer_minor = %u;\n"
1191 " tracer_patchlevel = %u;\n"
1196 LTTNG_UST_MAJOR_VERSION
,
1197 LTTNG_UST_MINOR_VERSION
,
1198 LTTNG_UST_PATCHLEVEL_VERSION
1203 ret
= lttng_metadata_printf(session
,
1211 if (!trace_clock_uuid(clock_uuid_s
)) {
1212 ret
= lttng_metadata_printf(session
,
1213 " uuid = \"%s\";\n",
1220 ret
= lttng_metadata_printf(session
,
1221 " description = \"Monotonic Clock\";\n"
1222 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1223 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1224 " offset = %" PRIu64
";\n"
1227 measure_clock_offset()
1232 ret
= lttng_metadata_printf(session
,
1233 "typealias integer {\n"
1234 " size = 27; align = 1; signed = false;\n"
1235 " map = clock.monotonic.value;\n"
1236 "} := uint27_clock_monotonic_t;\n"
1238 "typealias integer {\n"
1239 " size = 32; align = %u; signed = false;\n"
1240 " map = clock.monotonic.value;\n"
1241 "} := uint32_clock_monotonic_t;\n"
1243 "typealias integer {\n"
1244 " size = 64; align = %u; signed = false;\n"
1245 " map = clock.monotonic.value;\n"
1246 "} := uint64_clock_monotonic_t;\n\n",
1247 lttng_alignof(uint32_t) * CHAR_BIT
,
1248 lttng_alignof(uint64_t) * CHAR_BIT
1253 ret
= _lttng_stream_packet_context_declare(session
);
1257 ret
= _lttng_event_header_declare(session
);
1262 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
1263 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1268 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1269 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1273 session
->metadata_dumped
= 1;
1278 void lttng_ust_events_exit(void)
1280 struct lttng_session
*session
, *tmpsession
;
1282 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
1283 lttng_session_destroy(session
);
1287 * Enabler management.
1289 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1290 struct lttng_ust_event
*event_param
,
1291 struct lttng_channel
*chan
)
1293 struct lttng_enabler
*enabler
;
1295 enabler
= zmalloc(sizeof(*enabler
));
1298 enabler
->type
= type
;
1299 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1300 memcpy(&enabler
->event_param
, event_param
,
1301 sizeof(enabler
->event_param
));
1302 enabler
->chan
= chan
;
1304 enabler
->enabled
= 1;
1305 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1306 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1310 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1312 enabler
->enabled
= 1;
1313 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1317 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1319 if (enabler
->chan
== enabler
->chan
->session
->metadata
)
1321 enabler
->enabled
= 0;
1322 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1326 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1327 struct lttng_ust_filter_bytecode_node
*bytecode
)
1329 bytecode
->enabler
= enabler
;
1330 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
1331 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1335 int lttng_attach_context(struct lttng_ust_context
*context_param
,
1336 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
1339 * We cannot attach a context after trace has been started for a
1340 * session because the metadata does not allow expressing this
1341 * information outside of the original channel scope.
1343 if (session
->been_active
)
1346 switch (context_param
->ctx
) {
1347 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
1348 return lttng_add_pthread_id_to_ctx(ctx
);
1349 case LTTNG_UST_CONTEXT_VTID
:
1350 return lttng_add_vtid_to_ctx(ctx
);
1351 case LTTNG_UST_CONTEXT_VPID
:
1352 return lttng_add_vpid_to_ctx(ctx
);
1353 case LTTNG_UST_CONTEXT_PROCNAME
:
1354 return lttng_add_procname_to_ctx(ctx
);
1360 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1361 struct lttng_ust_context
*context_param
)
1363 #if 0 // disabled for now.
1364 struct lttng_session
*session
= enabler
->chan
->session
;
1367 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
1371 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1377 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1379 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1381 /* Destroy filter bytecode */
1382 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1383 &enabler
->filter_bytecode_head
, node
) {
1387 /* Destroy contexts */
1388 lttng_destroy_context(enabler
->ctx
);
1390 cds_list_del(&enabler
->node
);
1395 * lttng_session_sync_enablers should be called just before starting a
1399 void lttng_session_sync_enablers(struct lttng_session
*session
)
1401 struct lttng_enabler
*enabler
;
1402 struct lttng_event
*event
;
1404 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1405 lttng_enabler_ref_events(enabler
);
1407 * For each event, if at least one of its enablers is enabled,
1408 * we enable the event, else we disable it.
1410 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1411 struct lttng_enabler_ref
*enabler_ref
;
1412 struct lttng_bytecode_runtime
*runtime
;
1413 int enabled
= 0, has_enablers_without_bytecode
= 0;
1416 cds_list_for_each_entry(enabler_ref
,
1417 &event
->enablers_ref_head
, node
) {
1418 if (enabler_ref
->ref
->enabled
) {
1423 event
->enabled
= enabled
;
1425 /* Check if has enablers without bytecode enabled */
1426 cds_list_for_each_entry(enabler_ref
,
1427 &event
->enablers_ref_head
, node
) {
1428 if (enabler_ref
->ref
->enabled
1429 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1430 has_enablers_without_bytecode
= 1;
1434 event
->has_enablers_without_bytecode
=
1435 has_enablers_without_bytecode
;
1437 /* Enable filters */
1438 cds_list_for_each_entry(runtime
,
1439 &event
->bytecode_runtime_head
, node
) {
1440 lttng_filter_sync_state(runtime
);
1446 * Apply enablers to session events, adding events to session if need
1447 * be. It is required after each modification applied to an active
1448 * session, and right before session "start".
1449 * "lazy" sync means we only sync if required.
1452 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1454 /* We can skip if session is not active */
1455 if (!session
->active
)
1457 lttng_session_sync_enablers(session
);