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
);
79 * Wildcard list, containing the active wildcards.
80 * Protected by ust lock.
82 static CDS_LIST_HEAD(wildcard_list
);
85 * Pending probes hash table, containing the registered ltt events for
86 * which tracepoint probes are still missing. Protected by the sessions
89 #define PENDING_PROBE_HASH_BITS 6
90 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
91 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
93 struct ust_pending_probe
{
94 struct lttng_event
*event
;
95 struct cds_hlist_node node
;
96 enum lttng_ust_loglevel_type loglevel_type
;
101 static void _lttng_event_destroy(struct lttng_event
*event
);
102 static void _lttng_wildcard_destroy(struct session_wildcard
*sw
);
103 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
104 static int _lttng_event_unregister(struct lttng_event
*event
);
106 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
107 struct lttng_channel
*chan
,
108 struct lttng_event
*event
);
110 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
112 int lttng_loglevel_match(const struct lttng_event_desc
*desc
,
113 enum lttng_ust_loglevel_type req_type
,
118 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
121 ev_loglevel
= TRACE_DEFAULT
;
123 ev_loglevel
= *(*desc
->loglevel
);
125 case LTTNG_UST_LOGLEVEL_RANGE
:
126 if (ev_loglevel
<= req_loglevel
|| req_loglevel
== -1)
130 case LTTNG_UST_LOGLEVEL_SINGLE
:
131 if (ev_loglevel
== req_loglevel
|| req_loglevel
== -1)
135 case LTTNG_UST_LOGLEVEL_ALL
:
142 * Return wildcard for a given event name if the event name match the
143 * one of the wildcards.
144 * Must be called with ust lock held.
145 * Returns NULL if not present.
148 struct wildcard_entry
*match_wildcard(const struct lttng_event_desc
*desc
)
150 struct wildcard_entry
*e
;
152 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
153 /* If only contain '*' */
154 if (strlen(e
->name
) == 1)
156 /* Compare excluding final '*' */
157 if (!strncmp(desc
->name
, e
->name
, strlen(e
->name
) - 1))
159 continue; /* goto next, no match */
161 if (lttng_loglevel_match(desc
,
166 /* no match, loop to next */
172 * called at event creation if probe is missing.
173 * called with session mutex held.
176 int add_pending_probe(struct lttng_event
*event
, const char *name
,
177 enum lttng_ust_loglevel_type loglevel_type
,
180 struct cds_hlist_head
*head
;
181 struct ust_pending_probe
*e
;
182 size_t name_len
= strlen(name
) + 1;
185 if (name_len
> LTTNG_UST_SYM_NAME_LEN
) {
186 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
);
187 name_len
= LTTNG_UST_SYM_NAME_LEN
;
189 hash
= jhash(name
, name_len
- 1, 0);
190 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
191 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
194 memcpy(&e
->name
[0], name
, name_len
);
195 e
->name
[name_len
- 1] = '\0';
196 e
->loglevel_type
= loglevel_type
;
197 e
->loglevel
= loglevel
;
198 cds_hlist_add_head(&e
->node
, head
);
200 event
->pending_probe
= e
;
205 * remove a pending probe. called when at event teardown and when an
206 * event is fixed (probe is loaded).
207 * called with session mutex held.
210 void remove_pending_probe(struct ust_pending_probe
*e
)
214 cds_hlist_del(&e
->node
);
219 * Called at library load: connect the probe on the events pending on
221 * called with session mutex held.
223 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
225 struct cds_hlist_head
*head
;
226 struct cds_hlist_node
*node
, *p
;
227 struct ust_pending_probe
*e
;
228 const char *name
= desc
->name
;
230 struct lttng_ust_event event_param
;
231 size_t name_len
= strlen(name
) + 1;
236 struct wildcard_entry
*wildcard
;
238 //FIXME: should iterate on all match for filter.
239 //FIXME: should re-use pending event if present rather
240 //than create duplicate.
241 wildcard
= match_wildcard(desc
);
242 if (strcmp(desc
->name
, "lttng_ust:metadata") && wildcard
) {
243 struct session_wildcard
*sw
;
245 cds_list_for_each_entry(sw
, &wildcard
->session_list
,
247 struct lttng_event
*ev
;
250 memcpy(&event_param
, &sw
->event_param
,
251 sizeof(event_param
));
252 strncpy(event_param
.name
,
254 sizeof(event_param
.name
));
255 event_param
.name
[sizeof(event_param
.name
) - 1] = '\0';
257 ret
= lttng_event_create(sw
->chan
,
260 DBG("Error creating event");
263 cds_list_add(&ev
->wildcard_list
,
265 lttng_filter_event_link_wildcard_bytecode(ev
,
271 if (name_len
> LTTNG_UST_SYM_NAME_LEN
) {
272 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
);
273 name_len
= LTTNG_UST_SYM_NAME_LEN
;
275 hash
= jhash(name
, name_len
- 1, 0);
276 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
277 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
278 struct lttng_event
*event
;
279 struct lttng_channel
*chan
;
281 if (!lttng_loglevel_match(desc
,
286 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
289 /* TODO: wildcard same as pending event: duplicate */
290 /* TODO: Should apply filter though */
293 assert(!event
->desc
);
295 event
->pending_probe
= NULL
;
296 remove_pending_probe(e
);
297 ret
|= __tracepoint_probe_register(name
,
298 event
->desc
->probe_callback
,
299 event
, event
->desc
->signature
);
302 event
->id
= chan
->free_event_id
++;
303 ret
|= _lttng_event_metadata_statedump(chan
->session
, chan
,
305 lttng_filter_event_link_bytecode(event
);
310 void synchronize_trace(void)
315 struct lttng_session
*lttng_session_create(void)
317 struct lttng_session
*session
;
320 session
= zmalloc(sizeof(struct lttng_session
));
323 CDS_INIT_LIST_HEAD(&session
->chan
);
324 CDS_INIT_LIST_HEAD(&session
->events
);
325 CDS_INIT_LIST_HEAD(&session
->wildcards
);
326 ret
= lttng_ust_uuid_generate(session
->uuid
);
328 session
->uuid
[0] = '\0';
330 cds_list_add(&session
->list
, &sessions
);
334 void lttng_session_destroy(struct lttng_session
*session
)
336 struct lttng_channel
*chan
, *tmpchan
;
337 struct lttng_event
*event
, *tmpevent
;
338 struct session_wildcard
*wildcard
, *tmpwildcard
;
341 CMM_ACCESS_ONCE(session
->active
) = 0;
342 cds_list_for_each_entry(event
, &session
->events
, list
) {
343 ret
= _lttng_event_unregister(event
);
346 synchronize_trace(); /* Wait for in-flight events to complete */
347 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
348 _lttng_wildcard_destroy(wildcard
);
349 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
350 _lttng_event_destroy(event
);
351 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
352 _lttng_channel_destroy(chan
);
353 cds_list_del(&session
->list
);
357 int lttng_session_enable(struct lttng_session
*session
)
360 struct lttng_channel
*chan
;
362 if (session
->active
) {
368 * Snapshot the number of events per channel to know the type of header
371 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
372 if (chan
->header_type
)
373 continue; /* don't change it if session stop/restart */
374 if (chan
->free_event_id
< 31)
375 chan
->header_type
= 1; /* compact */
377 chan
->header_type
= 2; /* large */
380 CMM_ACCESS_ONCE(session
->active
) = 1;
381 CMM_ACCESS_ONCE(session
->been_active
) = 1;
382 ret
= _lttng_session_metadata_statedump(session
);
384 CMM_ACCESS_ONCE(session
->active
) = 0;
389 int lttng_session_disable(struct lttng_session
*session
)
393 if (!session
->active
) {
397 CMM_ACCESS_ONCE(session
->active
) = 0;
402 int lttng_channel_enable(struct lttng_channel
*channel
)
406 if (channel
== channel
->session
->metadata
)
408 old
= uatomic_xchg(&channel
->enabled
, 1);
414 int lttng_channel_disable(struct lttng_channel
*channel
)
418 if (channel
== channel
->session
->metadata
)
420 old
= uatomic_xchg(&channel
->enabled
, 0);
426 int lttng_event_enable(struct lttng_event
*event
)
430 if (event
->chan
== event
->chan
->session
->metadata
)
432 old
= uatomic_xchg(&event
->enabled
, 1);
438 int lttng_event_disable(struct lttng_event
*event
)
442 if (event
->chan
== event
->chan
->session
->metadata
)
444 old
= uatomic_xchg(&event
->enabled
, 0);
450 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
451 const char *transport_name
,
453 size_t subbuf_size
, size_t num_subbuf
,
454 unsigned int switch_timer_interval
,
455 unsigned int read_timer_interval
,
456 int **shm_fd
, int **wait_fd
,
457 uint64_t **memory_map_size
,
458 struct lttng_channel
*chan_priv_init
)
460 struct lttng_channel
*chan
= NULL
;
461 struct lttng_transport
*transport
;
463 if (session
->been_active
)
464 goto active
; /* Refuse to add channel to active session */
465 transport
= lttng_transport_find(transport_name
);
467 DBG("LTTng transport %s not found\n",
471 chan_priv_init
->id
= session
->free_chan_id
++;
472 chan_priv_init
->session
= session
;
474 * Note: the channel creation op already writes into the packet
475 * headers. Therefore the "chan" information used as input
476 * should be already accessible.
478 chan
= transport
->ops
.channel_create(transport_name
, buf_addr
,
479 subbuf_size
, num_subbuf
, switch_timer_interval
,
480 read_timer_interval
, shm_fd
, wait_fd
,
481 memory_map_size
, chan_priv_init
);
485 chan
->ops
= &transport
->ops
;
486 cds_list_add(&chan
->list
, &session
->chan
);
496 * Only used internally at session destruction.
499 void _lttng_channel_destroy(struct lttng_channel
*chan
)
501 cds_list_del(&chan
->list
);
502 lttng_destroy_context(chan
->ctx
);
503 chan
->ops
->channel_destroy(chan
);
507 * Supports event creation while tracing session is active.
509 int lttng_event_create(struct lttng_channel
*chan
,
510 struct lttng_ust_event
*event_param
,
511 struct lttng_event
**_event
)
513 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
514 struct lttng_event
*event
;
517 if (chan
->used_event_id
== -1U) {
521 //FIXME: re-use event if already registered by wildcard or
522 //if we have a pending probe.... (CHECK)
524 * This is O(n^2) (for each event, the loop is called at event
525 * creation). Might require a hash if we have lots of events.
527 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
528 if (event
->desc
&& !strncmp(event
->desc
->name
,
530 LTTNG_UST_SYM_NAME_LEN
- 1)) {
537 * Check if loglevel match. Refuse to connect event if not.
539 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
540 desc
= lttng_event_get(event_param
->name
);
542 if (!lttng_loglevel_match(desc
,
543 event_param
->loglevel_type
,
544 event_param
->loglevel
)) {
546 goto no_loglevel_match
;
550 * If descriptor is not there, it will be added to
554 event
= zmalloc(sizeof(struct lttng_event
));
561 * used_event_id counts the maximum number of event IDs that can
562 * register if all probes register.
564 chan
->used_event_id
++;
566 CDS_INIT_LIST_HEAD(&event
->filter_bytecode
);
567 CDS_INIT_LIST_HEAD(&event
->bytecode_runtime
);
568 event
->instrumentation
= event_param
->instrumentation
;
569 /* Populate lttng_event structure before tracepoint registration. */
571 switch (event_param
->instrumentation
) {
572 case LTTNG_UST_TRACEPOINT
:
575 ret
= __tracepoint_probe_register(event_param
->name
,
576 event
->desc
->probe_callback
,
577 event
, event
->desc
->signature
);
580 event
->id
= chan
->free_event_id
++;
583 * If the probe is not present, event->desc stays NULL,
584 * waiting for the probe to register, and the event->id
587 ret
= add_pending_probe(event
, event_param
->name
,
588 event_param
->loglevel_type
,
589 event_param
->loglevel
);
591 goto add_pending_error
;
598 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
600 goto statedump_error
;
602 cds_list_add(&event
->list
, &chan
->session
->events
);
608 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
609 event
->desc
->probe_callback
,
611 lttng_event_put(event
->desc
);
624 * Only used internally at session destruction.
626 int _lttng_event_unregister(struct lttng_event
*event
)
630 switch (event
->instrumentation
) {
631 case LTTNG_UST_TRACEPOINT
:
633 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
634 event
->desc
->probe_callback
,
639 remove_pending_probe(event
->pending_probe
);
650 * Only used internally at session destruction.
653 void _lttng_event_destroy(struct lttng_event
*event
)
655 switch (event
->instrumentation
) {
656 case LTTNG_UST_TRACEPOINT
:
658 lttng_event_put(event
->desc
);
664 cds_list_del(&event
->list
);
665 lttng_destroy_context(event
->ctx
);
666 lttng_free_event_filter_runtime(event
);
667 lttng_free_event_filter_bytecode(event
);
672 * We have exclusive access to our metadata buffer (protected by the
673 * ust_lock), so we can do racy operations such as looking for
674 * remaining space left in packet and write, since mutual exclusion
675 * protects us from concurrent writes.
677 int lttng_metadata_printf(struct lttng_session
*session
,
678 const char *fmt
, ...)
680 struct lttng_ust_lib_ring_buffer_ctx ctx
;
681 struct lttng_channel
*chan
= session
->metadata
;
683 int ret
= 0, waitret
;
684 size_t len
, reserve_len
, pos
;
687 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
690 ret
= vasprintf(&str
, fmt
, ap
);
698 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
699 reserve_len
= min_t(size_t,
700 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
702 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
703 sizeof(char), -1, chan
->handle
);
705 * We don't care about metadata buffer's records lost
706 * count, because we always retry here. Report error if
707 * we need to bail out after timeout or being
710 waitret
= wait_cond_interruptible_timeout(
712 ret
= chan
->ops
->event_reserve(&ctx
, 0);
713 ret
!= -ENOBUFS
|| !ret
;
715 LTTNG_METADATA_TIMEOUT_MSEC
);
716 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
717 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
718 waitret
== -EINTR
? "interrupted" :
719 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
720 if (waitret
== -EINTR
)
724 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
725 chan
->ops
->event_commit(&ctx
);
733 int _lttng_field_statedump(struct lttng_session
*session
,
734 const struct lttng_event_field
*field
)
741 switch (field
->type
.atype
) {
743 ret
= lttng_metadata_printf(session
,
744 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
745 field
->type
.u
.basic
.integer
.size
,
746 field
->type
.u
.basic
.integer
.alignment
,
747 field
->type
.u
.basic
.integer
.signedness
,
748 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
750 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
753 field
->type
.u
.basic
.integer
.base
,
754 #if (BYTE_ORDER == BIG_ENDIAN)
755 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
757 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
762 ret
= lttng_metadata_printf(session
,
763 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
764 field
->type
.u
.basic
._float
.exp_dig
,
765 field
->type
.u
.basic
._float
.mant_dig
,
766 field
->type
.u
.basic
._float
.alignment
,
767 #if (BYTE_ORDER == BIG_ENDIAN)
768 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
770 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
775 ret
= lttng_metadata_printf(session
,
777 field
->type
.u
.basic
.enumeration
.name
,
782 const struct lttng_basic_type
*elem_type
;
784 elem_type
= &field
->type
.u
.array
.elem_type
;
785 ret
= lttng_metadata_printf(session
,
786 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
787 elem_type
->u
.basic
.integer
.size
,
788 elem_type
->u
.basic
.integer
.alignment
,
789 elem_type
->u
.basic
.integer
.signedness
,
790 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
792 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
795 elem_type
->u
.basic
.integer
.base
,
796 #if (BYTE_ORDER == BIG_ENDIAN)
797 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
799 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
801 field
->name
, field
->type
.u
.array
.length
);
806 const struct lttng_basic_type
*elem_type
;
807 const struct lttng_basic_type
*length_type
;
809 elem_type
= &field
->type
.u
.sequence
.elem_type
;
810 length_type
= &field
->type
.u
.sequence
.length_type
;
811 ret
= lttng_metadata_printf(session
,
812 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
813 length_type
->u
.basic
.integer
.size
,
814 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
815 length_type
->u
.basic
.integer
.signedness
,
816 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
818 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
821 length_type
->u
.basic
.integer
.base
,
822 #if (BYTE_ORDER == BIG_ENDIAN)
823 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
825 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
831 ret
= lttng_metadata_printf(session
,
832 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
833 elem_type
->u
.basic
.integer
.size
,
834 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
835 elem_type
->u
.basic
.integer
.signedness
,
836 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
838 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
841 elem_type
->u
.basic
.integer
.base
,
842 #if (BYTE_ORDER == BIG_ENDIAN)
843 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
845 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
853 /* Default encoding is UTF8 */
854 ret
= lttng_metadata_printf(session
,
856 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
857 " { encoding = ASCII; }" : "",
868 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
869 struct lttng_ctx
*ctx
)
876 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
877 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
879 ret
= _lttng_field_statedump(session
, &field
->event_field
);
887 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
888 struct lttng_event
*event
)
890 const struct lttng_event_desc
*desc
= event
->desc
;
894 for (i
= 0; i
< desc
->nr_fields
; i
++) {
895 const struct lttng_event_field
*field
= &desc
->fields
[i
];
897 ret
= _lttng_field_statedump(session
, field
);
905 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
906 struct lttng_channel
*chan
,
907 struct lttng_event
*event
)
910 int loglevel
= TRACE_DEFAULT
;
912 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
914 if (chan
== session
->metadata
)
917 * Don't print events for which probe load is pending.
922 ret
= lttng_metadata_printf(session
,
926 " stream_id = %u;\n",
933 if (event
->desc
->loglevel
)
934 loglevel
= *(*event
->desc
->loglevel
);
936 ret
= lttng_metadata_printf(session
,
942 if (event
->desc
->u
.ext
.model_emf_uri
) {
943 ret
= lttng_metadata_printf(session
,
944 " model.emf.uri = \"%s\";\n",
945 *(event
->desc
->u
.ext
.model_emf_uri
));
951 ret
= lttng_metadata_printf(session
,
952 " context := struct {\n");
956 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
960 ret
= lttng_metadata_printf(session
,
966 ret
= lttng_metadata_printf(session
,
967 " fields := struct {\n"
972 ret
= _lttng_fields_metadata_statedump(session
, event
);
977 * LTTng space reservation can only reserve multiples of the
980 ret
= lttng_metadata_printf(session
,
986 event
->metadata_dumped
= 1;
993 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
994 struct lttng_channel
*chan
)
998 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
1000 if (chan
== session
->metadata
)
1003 WARN_ON_ONCE(!chan
->header_type
);
1004 ret
= lttng_metadata_printf(session
,
1007 " event.header := %s;\n"
1008 " packet.context := struct packet_context;\n",
1010 chan
->header_type
== 1 ? "struct event_header_compact" :
1011 "struct event_header_large");
1016 ret
= lttng_metadata_printf(session
,
1017 " event.context := struct {\n");
1021 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1025 ret
= lttng_metadata_printf(session
,
1031 ret
= lttng_metadata_printf(session
,
1034 chan
->metadata_dumped
= 1;
1040 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1042 return lttng_metadata_printf(session
,
1043 "struct packet_context {\n"
1044 " uint64_clock_monotonic_t timestamp_begin;\n"
1045 " uint64_clock_monotonic_t timestamp_end;\n"
1046 " uint64_t content_size;\n"
1047 " uint64_t packet_size;\n"
1048 " unsigned long events_discarded;\n"
1049 " uint32_t cpu_id;\n"
1056 * id: range: 0 - 30.
1057 * id 31 is reserved to indicate an extended header.
1060 * id: range: 0 - 65534.
1061 * id 65535 is reserved to indicate an extended header.
1064 int _lttng_event_header_declare(struct lttng_session
*session
)
1066 return lttng_metadata_printf(session
,
1067 "struct event_header_compact {\n"
1068 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1071 " uint27_clock_monotonic_t timestamp;\n"
1075 " uint64_clock_monotonic_t timestamp;\n"
1080 "struct event_header_large {\n"
1081 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1084 " uint32_clock_monotonic_t timestamp;\n"
1088 " uint64_clock_monotonic_t timestamp;\n"
1092 lttng_alignof(uint32_t) * CHAR_BIT
,
1093 lttng_alignof(uint16_t) * CHAR_BIT
1098 * Approximation of NTP time of day to clock monotonic correlation,
1099 * taken at start of trace.
1100 * Yes, this is only an approximation. Yes, we can (and will) do better
1101 * in future versions.
1104 uint64_t measure_clock_offset(void)
1106 uint64_t offset
, monotonic
[2], realtime
;
1107 struct timespec rts
= { 0, 0 };
1110 monotonic
[0] = trace_clock_read64();
1111 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1114 monotonic
[1] = trace_clock_read64();
1115 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1116 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
1117 realtime
+= rts
.tv_nsec
;
1118 offset
= realtime
- offset
;
1123 * Output metadata into this session's metadata buffers.
1126 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1128 unsigned char *uuid_c
= session
->uuid
;
1129 char uuid_s
[LTTNG_UST_UUID_STR_LEN
],
1130 clock_uuid_s
[LTTNG_UST_UUID_STR_LEN
];
1131 struct lttng_channel
*chan
;
1132 struct lttng_event
*event
;
1134 char procname
[LTTNG_UST_PROCNAME_LEN
] = "";
1135 char hostname
[HOST_NAME_MAX
];
1137 if (!CMM_ACCESS_ONCE(session
->active
))
1139 if (session
->metadata_dumped
)
1141 if (!session
->metadata
) {
1142 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1146 snprintf(uuid_s
, sizeof(uuid_s
),
1147 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1148 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1149 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1150 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1151 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1153 ret
= lttng_metadata_printf(session
,
1154 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1155 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1156 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1157 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1158 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1159 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1160 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1166 " byte_order = %s;\n"
1167 " packet.header := struct {\n"
1168 " uint32_t magic;\n"
1169 " uint8_t uuid[16];\n"
1170 " uint32_t stream_id;\n"
1173 lttng_alignof(uint8_t) * CHAR_BIT
,
1174 lttng_alignof(uint16_t) * CHAR_BIT
,
1175 lttng_alignof(uint32_t) * CHAR_BIT
,
1176 lttng_alignof(uint64_t) * CHAR_BIT
,
1177 sizeof(unsigned long) * CHAR_BIT
,
1178 lttng_alignof(unsigned long) * CHAR_BIT
,
1182 #if (BYTE_ORDER == BIG_ENDIAN)
1191 /* ignore error, just use empty string if error. */
1193 ret
= gethostname(hostname
, sizeof(hostname
));
1194 if (ret
&& errno
== ENAMETOOLONG
)
1195 hostname
[HOST_NAME_MAX
- 1] = '\0';
1196 lttng_ust_getprocname(procname
);
1197 procname
[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
1198 ret
= lttng_metadata_printf(session
,
1200 " hostname = \"%s\";\n"
1202 " procname = \"%s\";\n"
1203 " domain = \"ust\";\n"
1204 " tracer_name = \"lttng-ust\";\n"
1205 " tracer_major = %u;\n"
1206 " tracer_minor = %u;\n"
1207 " tracer_patchlevel = %u;\n"
1212 LTTNG_UST_MAJOR_VERSION
,
1213 LTTNG_UST_MINOR_VERSION
,
1214 LTTNG_UST_PATCHLEVEL_VERSION
1219 ret
= lttng_metadata_printf(session
,
1227 if (!trace_clock_uuid(clock_uuid_s
)) {
1228 ret
= lttng_metadata_printf(session
,
1229 " uuid = \"%s\";\n",
1236 ret
= lttng_metadata_printf(session
,
1237 " description = \"Monotonic Clock\";\n"
1238 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1239 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1240 " offset = %" PRIu64
";\n"
1243 measure_clock_offset()
1248 ret
= lttng_metadata_printf(session
,
1249 "typealias integer {\n"
1250 " size = 27; align = 1; signed = false;\n"
1251 " map = clock.monotonic.value;\n"
1252 "} := uint27_clock_monotonic_t;\n"
1254 "typealias integer {\n"
1255 " size = 32; align = %u; signed = false;\n"
1256 " map = clock.monotonic.value;\n"
1257 "} := uint32_clock_monotonic_t;\n"
1259 "typealias integer {\n"
1260 " size = 64; align = %u; signed = false;\n"
1261 " map = clock.monotonic.value;\n"
1262 "} := uint64_clock_monotonic_t;\n\n",
1263 lttng_alignof(uint32_t) * CHAR_BIT
,
1264 lttng_alignof(uint64_t) * CHAR_BIT
1269 ret
= _lttng_stream_packet_context_declare(session
);
1273 ret
= _lttng_event_header_declare(session
);
1278 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1279 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1284 cds_list_for_each_entry(event
, &session
->events
, list
) {
1285 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1289 session
->metadata_dumped
= 1;
1294 void lttng_ust_events_exit(void)
1296 struct lttng_session
*session
, *tmpsession
;
1298 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1299 lttng_session_destroy(session
);
1305 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1306 enum lttng_ust_loglevel_type loglevel_type
,
1309 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1317 int wildcard_is_within(struct wildcard_entry
*e
,
1318 enum lttng_ust_loglevel_type loglevel_type
,
1321 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1322 || e
->loglevel
== -1)
1324 switch (e
->loglevel_type
) {
1325 case LTTNG_UST_LOGLEVEL_RANGE
:
1326 switch (loglevel_type
) {
1327 case LTTNG_UST_LOGLEVEL_RANGE
:
1328 if (e
->loglevel
>= loglevel
)
1332 case LTTNG_UST_LOGLEVEL_SINGLE
:
1333 if (e
->loglevel
<= 0 && loglevel
== 0)
1338 case LTTNG_UST_LOGLEVEL_SINGLE
:
1339 switch (loglevel_type
) {
1340 case LTTNG_UST_LOGLEVEL_RANGE
:
1345 case LTTNG_UST_LOGLEVEL_SINGLE
:
1346 if (e
->loglevel
== loglevel
)
1356 * Add the wildcard to the wildcard list. Must be called with
1360 struct session_wildcard
*add_wildcard(struct lttng_channel
*chan
,
1361 struct lttng_ust_event
*event_param
)
1363 struct wildcard_entry
*e
;
1364 struct session_wildcard
*sw
;
1365 size_t name_len
= strlen(event_param
->name
) + 1;
1368 //FIXME: ensure that wildcard re-use pending events, or
1369 //re-use actual events, applying its filter on top.
1372 * Try to find global wildcard entry. Given that this is shared
1373 * across all sessions, we need to check for exact loglevel
1374 * match, not just whether contained within the existing ones.
1376 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1377 if (!strncmp(event_param
->name
, e
->name
,
1378 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1379 if (wildcard_same_loglevel(e
,
1380 event_param
->loglevel_type
,
1381 event_param
->loglevel
)) {
1390 * Create global wildcard entry if not found. Using
1391 * zmalloc here to allocate a variable length element.
1392 * Could cause some memory fragmentation if overused.
1394 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1396 return ERR_PTR(-ENOMEM
);
1397 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1398 e
->loglevel_type
= event_param
->loglevel_type
;
1399 e
->loglevel
= event_param
->loglevel
;
1400 CDS_INIT_LIST_HEAD(&e
->filter_bytecode
);
1401 cds_list_add(&e
->list
, &wildcard_list
);
1402 CDS_INIT_LIST_HEAD(&e
->session_list
);
1405 /* session wildcard */
1406 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1407 if (chan
== sw
->chan
) {
1408 DBG("wildcard %s busy for this channel",
1410 return ERR_PTR(-EEXIST
); /* Already there */
1413 sw
= zmalloc(sizeof(struct session_wildcard
));
1415 return ERR_PTR(-ENOMEM
);
1418 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1419 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1420 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1421 sw
->event_param
.loglevel
= event_param
->loglevel
;
1422 CDS_INIT_LIST_HEAD(&sw
->filter_bytecode
);
1423 CDS_INIT_LIST_HEAD(&sw
->events
);
1424 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1425 cds_list_add(&sw
->session_list
, &e
->session_list
);
1427 lttng_probes_create_wildcard_events(e
, sw
);
1432 * Remove the wildcard from the wildcard list. Must be called with
1433 * ust_lock held. Only called at session teardown.
1436 void _remove_wildcard(struct session_wildcard
*wildcard
)
1438 struct lttng_event
*ev
, *tmp
;
1441 * Just remove the events owned (for enable/disable) by this
1442 * wildcard from the list. The session teardown will take care
1443 * of freeing the event memory.
1445 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1447 cds_list_del(&ev
->wildcard_list
);
1449 cds_list_del(&wildcard
->session_list
);
1450 cds_list_del(&wildcard
->list
);
1451 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1452 cds_list_del(&wildcard
->entry
->list
);
1453 free(wildcard
->entry
);
1455 lttng_free_wildcard_filter_bytecode(wildcard
);
1459 int lttng_wildcard_create(struct lttng_channel
*chan
,
1460 struct lttng_ust_event
*event_param
,
1461 struct session_wildcard
**_sw
)
1463 struct session_wildcard
*sw
;
1465 sw
= add_wildcard(chan
, event_param
);
1466 if (!sw
|| IS_ERR(sw
)) {
1474 void _lttng_wildcard_destroy(struct session_wildcard
*sw
)
1476 _remove_wildcard(sw
);
1479 int lttng_wildcard_enable(struct session_wildcard
*wildcard
)
1481 struct lttng_event
*ev
;
1484 if (wildcard
->enabled
)
1486 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1487 ret
= lttng_event_enable(ev
);
1489 DBG("Error: enable error.\n");
1493 wildcard
->enabled
= 1;
1497 int lttng_wildcard_disable(struct session_wildcard
*wildcard
)
1499 struct lttng_event
*ev
;
1502 if (!wildcard
->enabled
)
1504 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1505 ret
= lttng_event_disable(ev
);
1507 DBG("Error: disable error.\n");
1511 wildcard
->enabled
= 0;
1516 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1517 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1518 * taken in constructors, which are called with dynamic linker mutex
1521 void lttng_fixup_event_tls(void)
1523 unsigned char uuid
[LTTNG_UST_UUID_STR_LEN
];
1525 (void) lttng_ust_uuid_generate(uuid
);