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>
50 #include "lttng-ust-uuid.h"
52 #include "tracepoint-internal.h"
53 #include "lttng-tracer.h"
54 #include "lttng-tracer-core.h"
56 #include "../libringbuffer/shm.h"
60 * The sessions mutex is the centralized mutex across UST tracing
61 * control and probe registration. All operations within this file are
62 * called by the communication thread, under ust_lock protection.
64 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
68 pthread_mutex_lock(&sessions_mutex
);
73 pthread_mutex_unlock(&sessions_mutex
);
76 static CDS_LIST_HEAD(sessions
);
78 static void _lttng_event_destroy(struct lttng_event
*event
);
79 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
80 static int _lttng_event_unregister(struct lttng_event
*event
);
82 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
83 struct lttng_channel
*chan
,
84 struct lttng_event
*event
);
86 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
89 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
91 void lttng_session_sync_enablers(struct lttng_session
*session
);
93 void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
96 int lttng_loglevel_match(int loglevel
,
97 unsigned int has_loglevel
,
98 enum lttng_ust_loglevel_type req_type
,
101 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
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 ret
= lttng_ust_uuid_generate(session
->uuid
);
142 session
->uuid
[0] = '\0';
144 cds_list_add(&session
->node
, &sessions
);
148 void lttng_session_destroy(struct lttng_session
*session
)
150 struct lttng_channel
*chan
, *tmpchan
;
151 struct lttng_event
*event
, *tmpevent
;
152 struct lttng_enabler
*enabler
, *tmpenabler
;
155 CMM_ACCESS_ONCE(session
->active
) = 0;
156 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
157 ret
= _lttng_event_unregister(event
);
160 synchronize_trace(); /* Wait for in-flight events to complete */
161 cds_list_for_each_entry_safe(enabler
, tmpenabler
,
162 &session
->enablers_head
, node
)
163 lttng_enabler_destroy(enabler
);
164 cds_list_for_each_entry_safe(event
, tmpevent
,
165 &session
->events_head
, node
)
166 _lttng_event_destroy(event
);
167 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan_head
, node
)
168 _lttng_channel_destroy(chan
);
169 cds_list_del(&session
->node
);
173 int lttng_session_enable(struct lttng_session
*session
)
176 struct lttng_channel
*chan
;
178 if (session
->active
) {
183 /* We need to sync enablers with session before activation. */
184 lttng_session_sync_enablers(session
);
187 * Snapshot the number of events per channel to know the type of header
190 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
191 if (chan
->header_type
)
192 continue; /* don't change it if session stop/restart */
193 if (chan
->free_event_id
< 31)
194 chan
->header_type
= 1; /* compact */
196 chan
->header_type
= 2; /* large */
199 CMM_ACCESS_ONCE(session
->active
) = 1;
200 CMM_ACCESS_ONCE(session
->been_active
) = 1;
201 ret
= _lttng_session_metadata_statedump(session
);
203 CMM_ACCESS_ONCE(session
->active
) = 0;
208 int lttng_session_disable(struct lttng_session
*session
)
212 if (!session
->active
) {
216 CMM_ACCESS_ONCE(session
->active
) = 0;
221 int lttng_channel_enable(struct lttng_channel
*channel
)
225 if (channel
== channel
->session
->metadata
)
227 old
= uatomic_xchg(&channel
->enabled
, 1);
233 int lttng_channel_disable(struct lttng_channel
*channel
)
237 if (channel
== channel
->session
->metadata
)
239 old
= uatomic_xchg(&channel
->enabled
, 0);
245 int lttng_event_enable(struct lttng_event
*event
)
249 if (event
->chan
== event
->chan
->session
->metadata
)
251 old
= uatomic_xchg(&event
->enabled
, 1);
257 int lttng_event_disable(struct lttng_event
*event
)
261 if (event
->chan
== event
->chan
->session
->metadata
)
263 old
= uatomic_xchg(&event
->enabled
, 0);
269 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
270 const char *transport_name
,
272 size_t subbuf_size
, size_t num_subbuf
,
273 unsigned int switch_timer_interval
,
274 unsigned int read_timer_interval
,
275 int **shm_fd
, int **wait_fd
,
276 uint64_t **memory_map_size
,
277 struct lttng_channel
*chan_priv_init
)
279 struct lttng_channel
*chan
= NULL
;
280 struct lttng_transport
*transport
;
282 if (session
->been_active
)
283 goto active
; /* Refuse to add channel to active session */
284 transport
= lttng_transport_find(transport_name
);
286 DBG("LTTng transport %s not found\n",
290 chan_priv_init
->id
= session
->free_chan_id
++;
291 chan_priv_init
->session
= session
;
293 * Note: the channel creation op already writes into the packet
294 * headers. Therefore the "chan" information used as input
295 * should be already accessible.
297 chan
= transport
->ops
.channel_create(transport_name
, buf_addr
,
298 subbuf_size
, num_subbuf
, switch_timer_interval
,
299 read_timer_interval
, shm_fd
, wait_fd
,
300 memory_map_size
, chan_priv_init
);
304 chan
->ops
= &transport
->ops
;
305 cds_list_add(&chan
->node
, &session
->chan_head
);
315 * Only used internally at session destruction.
318 void _lttng_channel_destroy(struct lttng_channel
*chan
)
320 cds_list_del(&chan
->node
);
321 lttng_destroy_context(chan
->ctx
);
322 chan
->ops
->channel_destroy(chan
);
326 * Supports event creation while tracing session is active.
329 int lttng_event_create(const struct lttng_event_desc
*desc
,
330 struct lttng_channel
*chan
)
332 const char *event_name
= desc
->name
;
333 struct lttng_event
*event
;
334 struct cds_hlist_head
*head
;
335 struct cds_hlist_node
*node
;
337 size_t name_len
= strlen(event_name
);
340 if (chan
->used_event_id
== -1U) {
344 hash
= jhash(event_name
, name_len
, 0);
345 head
= &chan
->session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
346 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
348 if (!strncmp(event
->desc
->name
,
350 LTTNG_UST_SYM_NAME_LEN
- 1)) {
357 * Check if loglevel match. Refuse to connect event if not.
359 event
= zmalloc(sizeof(struct lttng_event
));
367 * used_event_id counts the maximum number of event IDs that can
368 * register if all probes register.
370 chan
->used_event_id
++;
372 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
373 CDS_INIT_LIST_HEAD(&event
->enablers_ref_head
);
375 /* Populate lttng_event structure before tracepoint registration. */
377 ret
= __tracepoint_probe_register(event_name
,
378 desc
->probe_callback
,
379 event
, desc
->signature
);
382 event
->id
= chan
->free_event_id
++;
383 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
385 goto statedump_error
;
386 cds_list_add(&event
->node
, &chan
->session
->events_head
);
387 cds_hlist_add_head(&event
->hlist
, head
);
391 WARN_ON_ONCE(__tracepoint_probe_unregister(event_name
,
392 desc
->probe_callback
,
403 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc
*desc
,
404 struct lttng_enabler
*enabler
)
407 unsigned int has_loglevel
= 0;
409 assert(enabler
->type
== LTTNG_ENABLER_WILDCARD
);
410 /* Compare excluding final '*' */
411 if (strncmp(desc
->name
, enabler
->event_param
.name
,
412 strlen(enabler
->event_param
.name
) - 1))
414 if (desc
->loglevel
) {
415 loglevel
= *(*desc
->loglevel
);
418 if (!lttng_loglevel_match(loglevel
,
420 enabler
->event_param
.loglevel_type
,
421 enabler
->event_param
.loglevel
))
427 int lttng_desc_match_event_enabler(const struct lttng_event_desc
*desc
,
428 struct lttng_enabler
*enabler
)
431 unsigned int has_loglevel
= 0;
433 assert(enabler
->type
== LTTNG_ENABLER_EVENT
);
434 if (strcmp(desc
->name
, enabler
->event_param
.name
))
436 if (desc
->loglevel
) {
437 loglevel
= *(*desc
->loglevel
);
440 if (!lttng_loglevel_match(loglevel
,
442 enabler
->event_param
.loglevel_type
,
443 enabler
->event_param
.loglevel
))
449 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
450 struct lttng_enabler
*enabler
)
452 switch (enabler
->type
) {
453 case LTTNG_ENABLER_WILDCARD
:
454 return lttng_desc_match_wildcard_enabler(desc
, enabler
);
455 case LTTNG_ENABLER_EVENT
:
456 return lttng_desc_match_event_enabler(desc
, enabler
);
463 int lttng_event_match_enabler(struct lttng_event
*event
,
464 struct lttng_enabler
*enabler
)
466 return lttng_desc_match_enabler(event
->desc
, enabler
);
470 struct lttng_enabler_ref
* lttng_event_enabler_ref(struct lttng_event
*event
,
471 struct lttng_enabler
*enabler
)
473 struct lttng_enabler_ref
*enabler_ref
;
475 cds_list_for_each_entry(enabler_ref
,
476 &event
->enablers_ref_head
, node
) {
477 if (enabler_ref
->ref
== enabler
)
484 * Create struct lttng_event if it is missing and present in the list of
488 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
490 struct lttng_session
*session
= enabler
->chan
->session
;
491 struct lttng_probe_desc
*probe_desc
;
492 const struct lttng_event_desc
*desc
;
493 struct lttng_event
*event
;
495 struct cds_list_head
*probe_list
;
497 probe_list
= lttng_get_probe_list_head();
499 * For each probe event, if we find that a probe event matches
500 * our enabler, create an associated lttng_event if not
503 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
504 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
506 struct cds_hlist_head
*head
;
507 struct cds_hlist_node
*node
;
508 const char *event_name
;
512 desc
= probe_desc
->event_desc
[i
];
513 if (!lttng_desc_match_enabler(desc
, enabler
))
515 event_name
= desc
->name
;
516 name_len
= strlen(event_name
);
519 * Check if already created.
521 hash
= jhash(event_name
, name_len
, 0);
522 head
= &session
->events_ht
.table
[hash
& (LTTNG_UST_EVENT_HT_SIZE
- 1)];
523 cds_hlist_for_each_entry(event
, node
, head
, hlist
) {
524 if (event
->desc
== desc
)
531 * We need to create an event for this
534 ret
= lttng_event_create(probe_desc
->event_desc
[i
],
537 DBG("Unable to create event %s\n",
538 probe_desc
->event_desc
[i
]->name
);
545 * Create events associated with an enabler (if not already present),
546 * and add backward reference from the event to the enabler.
549 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
551 struct lttng_session
*session
= enabler
->chan
->session
;
552 struct lttng_event
*event
;
554 /* First ensure that probe events are created for this enabler. */
555 lttng_create_event_if_missing(enabler
);
557 /* For each event matching enabler in session event list. */
558 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
559 struct lttng_enabler_ref
*enabler_ref
;
561 if (!lttng_event_match_enabler(event
, enabler
))
564 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
567 * If no backward ref, create it.
568 * Add backward ref from event to enabler.
570 enabler_ref
= zmalloc(sizeof(*enabler_ref
));
573 enabler_ref
->ref
= enabler
;
574 cds_list_add(&enabler_ref
->node
,
575 &event
->enablers_ref_head
);
579 * Link filter bytecodes if not linked yet.
581 lttng_enabler_event_link_bytecode(event
, enabler
);
583 /* TODO: merge event context. */
589 * Called at library load: connect the probe on all enablers matching
591 * called with session mutex held.
592 * TODO: currently, for each desc added, we iterate on all event desc
593 * (inefficient). We should create specific code that only target the
596 int lttng_fix_pending_event_desc(const struct lttng_event_desc
*desc
)
598 struct lttng_session
*session
;
600 cds_list_for_each_entry(session
, &sessions
, node
) {
601 lttng_session_lazy_sync_enablers(session
);
607 * Only used internally at session destruction.
609 int _lttng_event_unregister(struct lttng_event
*event
)
611 return __tracepoint_probe_unregister(event
->desc
->name
,
612 event
->desc
->probe_callback
,
617 * Only used internally at session destruction.
620 void _lttng_event_destroy(struct lttng_event
*event
)
622 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
624 cds_list_del(&event
->node
);
625 lttng_destroy_context(event
->ctx
);
626 lttng_free_event_filter_runtime(event
);
627 /* Free event enabler refs */
628 cds_list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
629 &event
->enablers_ref_head
, node
)
635 * We have exclusive access to our metadata buffer (protected by the
636 * ust_lock), so we can do racy operations such as looking for
637 * remaining space left in packet and write, since mutual exclusion
638 * protects us from concurrent writes.
640 int lttng_metadata_printf(struct lttng_session
*session
,
641 const char *fmt
, ...)
643 struct lttng_ust_lib_ring_buffer_ctx ctx
;
644 struct lttng_channel
*chan
= session
->metadata
;
646 int ret
= 0, waitret
;
647 size_t len
, reserve_len
, pos
;
650 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
653 ret
= vasprintf(&str
, fmt
, ap
);
661 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
662 reserve_len
= min_t(size_t,
663 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
665 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
666 sizeof(char), -1, chan
->handle
);
668 * We don't care about metadata buffer's records lost
669 * count, because we always retry here. Report error if
670 * we need to bail out after timeout or being
673 waitret
= wait_cond_interruptible_timeout(
675 ret
= chan
->ops
->event_reserve(&ctx
, 0);
676 ret
!= -ENOBUFS
|| !ret
;
678 LTTNG_METADATA_TIMEOUT_MSEC
);
679 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
680 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
681 waitret
== -EINTR
? "interrupted" :
682 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
683 if (waitret
== -EINTR
)
687 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
688 chan
->ops
->event_commit(&ctx
);
696 int _lttng_field_statedump(struct lttng_session
*session
,
697 const struct lttng_event_field
*field
)
704 switch (field
->type
.atype
) {
706 ret
= lttng_metadata_printf(session
,
707 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
708 field
->type
.u
.basic
.integer
.size
,
709 field
->type
.u
.basic
.integer
.alignment
,
710 field
->type
.u
.basic
.integer
.signedness
,
711 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
713 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
716 field
->type
.u
.basic
.integer
.base
,
717 #if (BYTE_ORDER == BIG_ENDIAN)
718 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
720 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
725 ret
= lttng_metadata_printf(session
,
726 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
727 field
->type
.u
.basic
._float
.exp_dig
,
728 field
->type
.u
.basic
._float
.mant_dig
,
729 field
->type
.u
.basic
._float
.alignment
,
730 #if (BYTE_ORDER == BIG_ENDIAN)
731 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
733 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
738 ret
= lttng_metadata_printf(session
,
740 field
->type
.u
.basic
.enumeration
.name
,
745 const struct lttng_basic_type
*elem_type
;
747 elem_type
= &field
->type
.u
.array
.elem_type
;
748 ret
= lttng_metadata_printf(session
,
749 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
750 elem_type
->u
.basic
.integer
.size
,
751 elem_type
->u
.basic
.integer
.alignment
,
752 elem_type
->u
.basic
.integer
.signedness
,
753 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
755 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
758 elem_type
->u
.basic
.integer
.base
,
759 #if (BYTE_ORDER == BIG_ENDIAN)
760 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
762 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
764 field
->name
, field
->type
.u
.array
.length
);
769 const struct lttng_basic_type
*elem_type
;
770 const struct lttng_basic_type
*length_type
;
772 elem_type
= &field
->type
.u
.sequence
.elem_type
;
773 length_type
= &field
->type
.u
.sequence
.length_type
;
774 ret
= lttng_metadata_printf(session
,
775 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
776 length_type
->u
.basic
.integer
.size
,
777 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
778 length_type
->u
.basic
.integer
.signedness
,
779 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
781 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
784 length_type
->u
.basic
.integer
.base
,
785 #if (BYTE_ORDER == BIG_ENDIAN)
786 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
788 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
794 ret
= lttng_metadata_printf(session
,
795 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
796 elem_type
->u
.basic
.integer
.size
,
797 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
798 elem_type
->u
.basic
.integer
.signedness
,
799 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
801 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
804 elem_type
->u
.basic
.integer
.base
,
805 #if (BYTE_ORDER == BIG_ENDIAN)
806 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
808 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
816 /* Default encoding is UTF8 */
817 ret
= lttng_metadata_printf(session
,
819 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
820 " { encoding = ASCII; }" : "",
831 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
832 struct lttng_ctx
*ctx
)
839 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
840 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
842 ret
= _lttng_field_statedump(session
, &field
->event_field
);
850 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
851 struct lttng_event
*event
)
853 const struct lttng_event_desc
*desc
= event
->desc
;
857 for (i
= 0; i
< desc
->nr_fields
; i
++) {
858 const struct lttng_event_field
*field
= &desc
->fields
[i
];
860 ret
= _lttng_field_statedump(session
, field
);
868 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
869 struct lttng_channel
*chan
,
870 struct lttng_event
*event
)
873 int loglevel
= TRACE_DEFAULT
;
875 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
877 if (chan
== session
->metadata
)
880 * Don't print events for which probe load is pending.
885 ret
= lttng_metadata_printf(session
,
889 " stream_id = %u;\n",
896 if (event
->desc
->loglevel
)
897 loglevel
= *(*event
->desc
->loglevel
);
899 ret
= lttng_metadata_printf(session
,
905 if (event
->desc
->u
.ext
.model_emf_uri
) {
906 ret
= lttng_metadata_printf(session
,
907 " model.emf.uri = \"%s\";\n",
908 *(event
->desc
->u
.ext
.model_emf_uri
));
914 ret
= lttng_metadata_printf(session
,
915 " context := struct {\n");
919 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
923 ret
= lttng_metadata_printf(session
,
929 ret
= lttng_metadata_printf(session
,
930 " fields := struct {\n"
935 ret
= _lttng_fields_metadata_statedump(session
, event
);
940 * LTTng space reservation can only reserve multiples of the
943 ret
= lttng_metadata_printf(session
,
949 event
->metadata_dumped
= 1;
956 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
957 struct lttng_channel
*chan
)
961 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
963 if (chan
== session
->metadata
)
966 WARN_ON_ONCE(!chan
->header_type
);
967 ret
= lttng_metadata_printf(session
,
970 " event.header := %s;\n"
971 " packet.context := struct packet_context;\n",
973 chan
->header_type
== 1 ? "struct event_header_compact" :
974 "struct event_header_large");
979 ret
= lttng_metadata_printf(session
,
980 " event.context := struct {\n");
984 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
988 ret
= lttng_metadata_printf(session
,
994 ret
= lttng_metadata_printf(session
,
997 chan
->metadata_dumped
= 1;
1003 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1005 return lttng_metadata_printf(session
,
1006 "struct packet_context {\n"
1007 " uint64_clock_monotonic_t timestamp_begin;\n"
1008 " uint64_clock_monotonic_t timestamp_end;\n"
1009 " uint64_t content_size;\n"
1010 " uint64_t packet_size;\n"
1011 " unsigned long events_discarded;\n"
1012 " uint32_t cpu_id;\n"
1019 * id: range: 0 - 30.
1020 * id 31 is reserved to indicate an extended header.
1023 * id: range: 0 - 65534.
1024 * id 65535 is reserved to indicate an extended header.
1027 int _lttng_event_header_declare(struct lttng_session
*session
)
1029 return lttng_metadata_printf(session
,
1030 "struct event_header_compact {\n"
1031 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1034 " uint27_clock_monotonic_t timestamp;\n"
1038 " uint64_clock_monotonic_t timestamp;\n"
1043 "struct event_header_large {\n"
1044 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1047 " uint32_clock_monotonic_t timestamp;\n"
1051 " uint64_clock_monotonic_t timestamp;\n"
1055 lttng_alignof(uint32_t) * CHAR_BIT
,
1056 lttng_alignof(uint16_t) * CHAR_BIT
1061 * Approximation of NTP time of day to clock monotonic correlation,
1062 * taken at start of trace.
1063 * Yes, this is only an approximation. Yes, we can (and will) do better
1064 * in future versions.
1067 uint64_t measure_clock_offset(void)
1069 uint64_t offset
, monotonic
[2], realtime
;
1070 struct timespec rts
= { 0, 0 };
1073 monotonic
[0] = trace_clock_read64();
1074 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1077 monotonic
[1] = trace_clock_read64();
1078 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1079 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
1080 realtime
+= rts
.tv_nsec
;
1081 offset
= realtime
- offset
;
1086 * Output metadata into this session's metadata buffers.
1089 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1091 unsigned char *uuid_c
= session
->uuid
;
1092 char uuid_s
[LTTNG_UST_UUID_STR_LEN
],
1093 clock_uuid_s
[LTTNG_UST_UUID_STR_LEN
];
1094 struct lttng_channel
*chan
;
1095 struct lttng_event
*event
;
1097 char procname
[LTTNG_UST_PROCNAME_LEN
] = "";
1098 char hostname
[HOST_NAME_MAX
];
1100 if (!CMM_ACCESS_ONCE(session
->active
))
1102 if (session
->metadata_dumped
)
1104 if (!session
->metadata
) {
1105 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1109 snprintf(uuid_s
, sizeof(uuid_s
),
1110 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1111 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1112 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1113 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1114 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1116 ret
= lttng_metadata_printf(session
,
1117 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1118 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1119 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1120 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1121 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1122 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1123 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1129 " byte_order = %s;\n"
1130 " packet.header := struct {\n"
1131 " uint32_t magic;\n"
1132 " uint8_t uuid[16];\n"
1133 " uint32_t stream_id;\n"
1136 lttng_alignof(uint8_t) * CHAR_BIT
,
1137 lttng_alignof(uint16_t) * CHAR_BIT
,
1138 lttng_alignof(uint32_t) * CHAR_BIT
,
1139 lttng_alignof(uint64_t) * CHAR_BIT
,
1140 sizeof(unsigned long) * CHAR_BIT
,
1141 lttng_alignof(unsigned long) * CHAR_BIT
,
1145 #if (BYTE_ORDER == BIG_ENDIAN)
1154 /* ignore error, just use empty string if error. */
1156 ret
= gethostname(hostname
, sizeof(hostname
));
1157 if (ret
&& errno
== ENAMETOOLONG
)
1158 hostname
[HOST_NAME_MAX
- 1] = '\0';
1159 lttng_ust_getprocname(procname
);
1160 procname
[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
1161 ret
= lttng_metadata_printf(session
,
1163 " hostname = \"%s\";\n"
1165 " procname = \"%s\";\n"
1166 " domain = \"ust\";\n"
1167 " tracer_name = \"lttng-ust\";\n"
1168 " tracer_major = %u;\n"
1169 " tracer_minor = %u;\n"
1170 " tracer_patchlevel = %u;\n"
1175 LTTNG_UST_MAJOR_VERSION
,
1176 LTTNG_UST_MINOR_VERSION
,
1177 LTTNG_UST_PATCHLEVEL_VERSION
1182 ret
= lttng_metadata_printf(session
,
1190 if (!trace_clock_uuid(clock_uuid_s
)) {
1191 ret
= lttng_metadata_printf(session
,
1192 " uuid = \"%s\";\n",
1199 ret
= lttng_metadata_printf(session
,
1200 " description = \"Monotonic Clock\";\n"
1201 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1202 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1203 " offset = %" PRIu64
";\n"
1206 measure_clock_offset()
1211 ret
= lttng_metadata_printf(session
,
1212 "typealias integer {\n"
1213 " size = 27; align = 1; signed = false;\n"
1214 " map = clock.monotonic.value;\n"
1215 "} := uint27_clock_monotonic_t;\n"
1217 "typealias integer {\n"
1218 " size = 32; align = %u; signed = false;\n"
1219 " map = clock.monotonic.value;\n"
1220 "} := uint32_clock_monotonic_t;\n"
1222 "typealias integer {\n"
1223 " size = 64; align = %u; signed = false;\n"
1224 " map = clock.monotonic.value;\n"
1225 "} := uint64_clock_monotonic_t;\n\n",
1226 lttng_alignof(uint32_t) * CHAR_BIT
,
1227 lttng_alignof(uint64_t) * CHAR_BIT
1232 ret
= _lttng_stream_packet_context_declare(session
);
1236 ret
= _lttng_event_header_declare(session
);
1241 cds_list_for_each_entry(chan
, &session
->chan_head
, node
) {
1242 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1247 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1248 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1252 session
->metadata_dumped
= 1;
1257 void lttng_ust_events_exit(void)
1259 struct lttng_session
*session
, *tmpsession
;
1261 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, node
)
1262 lttng_session_destroy(session
);
1266 * Enabler management.
1268 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1269 struct lttng_ust_event
*event_param
,
1270 struct lttng_channel
*chan
)
1272 struct lttng_enabler
*enabler
;
1274 enabler
= zmalloc(sizeof(*enabler
));
1277 enabler
->type
= type
;
1278 CDS_INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1279 memcpy(&enabler
->event_param
, event_param
,
1280 sizeof(enabler
->event_param
));
1281 enabler
->chan
= chan
;
1283 enabler
->enabled
= 1;
1284 cds_list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1285 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1289 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1291 enabler
->enabled
= 1;
1292 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1296 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1298 if (enabler
->chan
== enabler
->chan
->session
->metadata
)
1300 enabler
->enabled
= 0;
1301 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1305 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1306 struct lttng_ust_filter_bytecode_node
*bytecode
)
1308 bytecode
->enabler
= enabler
;
1309 cds_list_add_tail(&bytecode
->node
, &enabler
->filter_bytecode_head
);
1310 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1314 int lttng_attach_context(struct lttng_ust_context
*context_param
,
1315 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
1318 * We cannot attach a context after trace has been started for a
1319 * session because the metadata does not allow expressing this
1320 * information outside of the original channel scope.
1322 if (session
->been_active
)
1325 switch (context_param
->ctx
) {
1326 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
1327 return lttng_add_pthread_id_to_ctx(ctx
);
1328 case LTTNG_UST_CONTEXT_VTID
:
1329 return lttng_add_vtid_to_ctx(ctx
);
1330 case LTTNG_UST_CONTEXT_VPID
:
1331 return lttng_add_vpid_to_ctx(ctx
);
1332 case LTTNG_UST_CONTEXT_PROCNAME
:
1333 return lttng_add_procname_to_ctx(ctx
);
1339 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1340 struct lttng_ust_context
*context_param
)
1342 #if 0 // disabled for now.
1343 struct lttng_session
*session
= enabler
->chan
->session
;
1346 ret
= lttng_attach_context(context_param
, &enabler
->ctx
,
1350 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1356 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1358 struct lttng_ust_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1360 /* Destroy filter bytecode */
1361 cds_list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1362 &enabler
->filter_bytecode_head
, node
) {
1366 /* Destroy contexts */
1367 lttng_destroy_context(enabler
->ctx
);
1369 cds_list_del(&enabler
->node
);
1374 * lttng_session_sync_enablers should be called just before starting a
1378 void lttng_session_sync_enablers(struct lttng_session
*session
)
1380 struct lttng_enabler
*enabler
;
1381 struct lttng_event
*event
;
1383 cds_list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1384 lttng_enabler_ref_events(enabler
);
1386 * For each event, if at least one of its enablers is enabled,
1387 * we enable the event, else we disable it.
1389 cds_list_for_each_entry(event
, &session
->events_head
, node
) {
1390 struct lttng_enabler_ref
*enabler_ref
;
1391 struct lttng_bytecode_runtime
*runtime
;
1392 int enabled
= 0, has_enablers_without_bytecode
= 0;
1395 cds_list_for_each_entry(enabler_ref
,
1396 &event
->enablers_ref_head
, node
) {
1397 if (enabler_ref
->ref
->enabled
) {
1402 event
->enabled
= enabled
;
1404 /* Check if has enablers without bytecode enabled */
1405 cds_list_for_each_entry(enabler_ref
,
1406 &event
->enablers_ref_head
, node
) {
1407 if (enabler_ref
->ref
->enabled
1408 && cds_list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1409 has_enablers_without_bytecode
= 1;
1413 event
->has_enablers_without_bytecode
=
1414 has_enablers_without_bytecode
;
1416 /* Enable filters */
1417 cds_list_for_each_entry(runtime
,
1418 &event
->bytecode_runtime_head
, node
) {
1419 lttng_filter_sync_state(runtime
);
1425 * Apply enablers to session events, adding events to session if need
1426 * be. It is required after each modification applied to an active
1427 * session, and right before session "start".
1428 * "lazy" sync means we only sync if required.
1431 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1433 /* We can skip if session is not active */
1434 if (!session
->active
)
1436 lttng_session_sync_enablers(session
);
1440 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1441 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1442 * taken in constructors, which are called with dynamic linker mutex
1445 void lttng_fixup_event_tls(void)
1447 unsigned char uuid
[LTTNG_UST_UUID_STR_LEN
];
1449 (void) lttng_ust_uuid_generate(uuid
);