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
26 #include <urcu/list.h>
27 #include <urcu/hlist.h>
29 #include <uuid/uuid.h>
37 #include <sys/prctl.h>
41 #include <urcu/compiler.h>
42 #include <urcu/uatomic.h>
43 #include <urcu/arch.h>
45 #include <lttng/tracepoint.h>
46 #include <lttng/ust-events.h>
48 #include <usterr-signal-safe.h>
52 #include "tracepoint-internal.h"
53 #include "ltt-tracer.h"
54 #include "ltt-tracer-core.h"
56 #include "../libringbuffer/shm.h"
59 #define PROCNAME_LEN 17
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
);
81 * Wildcard list, containing the active wildcards.
82 * Protected by ust lock.
84 static CDS_LIST_HEAD(wildcard_list
);
87 * Pending probes hash table, containing the registered ltt events for
88 * which tracepoint probes are still missing. Protected by the sessions
91 #define PENDING_PROBE_HASH_BITS 6
92 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
93 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
95 struct ust_pending_probe
{
96 struct ltt_event
*event
;
97 struct cds_hlist_node node
;
98 enum lttng_ust_loglevel_type loglevel_type
;
103 static void _ltt_event_destroy(struct ltt_event
*event
);
104 static void _ltt_wildcard_destroy(struct session_wildcard
*sw
);
105 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
106 static int _ltt_event_unregister(struct ltt_event
*event
);
108 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
109 struct ltt_channel
*chan
,
110 struct ltt_event
*event
);
112 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
114 int ltt_loglevel_match(const struct lttng_event_desc
*desc
,
115 enum lttng_ust_loglevel_type req_type
,
120 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
123 ev_loglevel
= TRACE_DEFAULT
;
125 ev_loglevel
= *(*desc
->loglevel
);
127 case LTTNG_UST_LOGLEVEL_RANGE
:
128 if (ev_loglevel
<= req_loglevel
|| req_loglevel
== -1)
132 case LTTNG_UST_LOGLEVEL_SINGLE
:
133 if (ev_loglevel
== req_loglevel
|| req_loglevel
== -1)
137 case LTTNG_UST_LOGLEVEL_ALL
:
144 * Return wildcard for a given event name if the event name match the
145 * one of the wildcards.
146 * Must be called with ust lock held.
147 * Returns NULL if not present.
150 struct wildcard_entry
*match_wildcard(const struct lttng_event_desc
*desc
)
152 struct wildcard_entry
*e
;
154 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
155 /* If only contain '*' */
156 if (strlen(e
->name
) == 1)
158 /* Compare excluding final '*' */
159 if (!strncmp(desc
->name
, e
->name
, strlen(e
->name
) - 1))
161 continue; /* goto next, no match */
163 if (ltt_loglevel_match(desc
,
168 /* no match, loop to next */
174 * called at event creation if probe is missing.
175 * called with session mutex held.
178 int add_pending_probe(struct ltt_event
*event
, const char *name
,
179 enum lttng_ust_loglevel_type loglevel_type
,
182 struct cds_hlist_head
*head
;
183 struct ust_pending_probe
*e
;
184 size_t name_len
= strlen(name
) + 1;
187 if (name_len
> LTTNG_UST_SYM_NAME_LEN
) {
188 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
);
189 name_len
= LTTNG_UST_SYM_NAME_LEN
;
191 hash
= jhash(name
, name_len
- 1, 0);
192 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
193 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
196 memcpy(&e
->name
[0], name
, name_len
);
197 e
->name
[name_len
- 1] = '\0';
198 e
->loglevel_type
= loglevel_type
;
199 e
->loglevel
= loglevel
;
200 cds_hlist_add_head(&e
->node
, head
);
202 event
->pending_probe
= e
;
207 * remove a pending probe. called when at event teardown and when an
208 * event is fixed (probe is loaded).
209 * called with session mutex held.
212 void remove_pending_probe(struct ust_pending_probe
*e
)
216 cds_hlist_del(&e
->node
);
221 * Called at library load: connect the probe on the events pending on
223 * called with session mutex held.
225 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
227 struct cds_hlist_head
*head
;
228 struct cds_hlist_node
*node
, *p
;
229 struct ust_pending_probe
*e
;
230 const char *name
= desc
->name
;
232 struct lttng_ust_event event_param
;
233 size_t name_len
= strlen(name
) + 1;
238 struct wildcard_entry
*wildcard
;
240 wildcard
= match_wildcard(desc
);
241 if (strcmp(desc
->name
, "lttng_ust:metadata") && wildcard
) {
242 struct session_wildcard
*sw
;
244 cds_list_for_each_entry(sw
, &wildcard
->session_list
,
246 struct ltt_event
*ev
;
249 memcpy(&event_param
, &sw
->event_param
,
250 sizeof(event_param
));
251 memcpy(event_param
.name
,
253 sizeof(event_param
.name
));
255 ret
= ltt_event_create(sw
->chan
,
259 DBG("Error creating event");
262 cds_list_add(&ev
->wildcard_list
,
268 if (name_len
> LTTNG_UST_SYM_NAME_LEN
) {
269 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
);
270 name_len
= LTTNG_UST_SYM_NAME_LEN
;
272 hash
= jhash(name
, name_len
- 1, 0);
273 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
274 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
275 struct ltt_event
*event
;
276 struct ltt_channel
*chan
;
278 if (!ltt_loglevel_match(desc
,
283 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
288 assert(!event
->desc
);
290 event
->pending_probe
= NULL
;
291 remove_pending_probe(e
);
292 ret
|= __tracepoint_probe_register(name
,
293 event
->desc
->probe_callback
,
294 event
, event
->desc
->signature
);
297 event
->id
= chan
->free_event_id
++;
298 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
304 void synchronize_trace(void)
309 struct ltt_session
*ltt_session_create(void)
311 struct ltt_session
*session
;
313 session
= zmalloc(sizeof(struct ltt_session
));
316 CDS_INIT_LIST_HEAD(&session
->chan
);
317 CDS_INIT_LIST_HEAD(&session
->events
);
318 CDS_INIT_LIST_HEAD(&session
->wildcards
);
319 uuid_generate(session
->uuid
);
320 cds_list_add(&session
->list
, &sessions
);
324 void ltt_session_destroy(struct ltt_session
*session
)
326 struct ltt_channel
*chan
, *tmpchan
;
327 struct ltt_event
*event
, *tmpevent
;
328 struct session_wildcard
*wildcard
, *tmpwildcard
;
331 CMM_ACCESS_ONCE(session
->active
) = 0;
332 cds_list_for_each_entry(event
, &session
->events
, list
) {
333 ret
= _ltt_event_unregister(event
);
336 synchronize_trace(); /* Wait for in-flight events to complete */
337 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
338 _ltt_wildcard_destroy(wildcard
);
339 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
340 _ltt_event_destroy(event
);
341 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
342 _ltt_channel_destroy(chan
);
343 cds_list_del(&session
->list
);
347 int ltt_session_enable(struct ltt_session
*session
)
350 struct ltt_channel
*chan
;
352 if (session
->active
) {
358 * Snapshot the number of events per channel to know the type of header
361 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
362 if (chan
->header_type
)
363 continue; /* don't change it if session stop/restart */
364 if (chan
->free_event_id
< 31)
365 chan
->header_type
= 1; /* compact */
367 chan
->header_type
= 2; /* large */
370 CMM_ACCESS_ONCE(session
->active
) = 1;
371 CMM_ACCESS_ONCE(session
->been_active
) = 1;
372 ret
= _ltt_session_metadata_statedump(session
);
374 CMM_ACCESS_ONCE(session
->active
) = 0;
379 int ltt_session_disable(struct ltt_session
*session
)
383 if (!session
->active
) {
387 CMM_ACCESS_ONCE(session
->active
) = 0;
392 int ltt_channel_enable(struct ltt_channel
*channel
)
396 if (channel
== channel
->session
->metadata
)
398 old
= uatomic_xchg(&channel
->enabled
, 1);
404 int ltt_channel_disable(struct ltt_channel
*channel
)
408 if (channel
== channel
->session
->metadata
)
410 old
= uatomic_xchg(&channel
->enabled
, 0);
416 int ltt_event_enable(struct ltt_event
*event
)
420 if (event
->chan
== event
->chan
->session
->metadata
)
422 old
= uatomic_xchg(&event
->enabled
, 1);
428 int ltt_event_disable(struct ltt_event
*event
)
432 if (event
->chan
== event
->chan
->session
->metadata
)
434 old
= uatomic_xchg(&event
->enabled
, 0);
440 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
441 const char *transport_name
,
443 size_t subbuf_size
, size_t num_subbuf
,
444 unsigned int switch_timer_interval
,
445 unsigned int read_timer_interval
,
446 int **shm_fd
, int **wait_fd
,
447 uint64_t **memory_map_size
,
448 struct ltt_channel
*chan_priv_init
)
450 struct ltt_channel
*chan
= NULL
;
451 struct ltt_transport
*transport
;
453 if (session
->been_active
)
454 goto active
; /* Refuse to add channel to active session */
455 transport
= ltt_transport_find(transport_name
);
457 DBG("LTTng transport %s not found\n",
461 chan_priv_init
->id
= session
->free_chan_id
++;
462 chan_priv_init
->session
= session
;
464 * Note: the channel creation op already writes into the packet
465 * headers. Therefore the "chan" information used as input
466 * should be already accessible.
468 chan
= transport
->ops
.channel_create(transport_name
, buf_addr
,
469 subbuf_size
, num_subbuf
, switch_timer_interval
,
470 read_timer_interval
, shm_fd
, wait_fd
,
471 memory_map_size
, chan_priv_init
);
475 chan
->ops
= &transport
->ops
;
476 cds_list_add(&chan
->list
, &session
->chan
);
486 * Only used internally at session destruction.
489 void _ltt_channel_destroy(struct ltt_channel
*chan
)
491 cds_list_del(&chan
->list
);
492 lttng_destroy_context(chan
->ctx
);
493 chan
->ops
->channel_destroy(chan
);
497 * Supports event creation while tracing session is active.
499 int ltt_event_create(struct ltt_channel
*chan
,
500 struct lttng_ust_event
*event_param
,
502 struct ltt_event
**_event
)
504 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
505 struct ltt_event
*event
;
508 if (chan
->used_event_id
== -1U) {
513 * This is O(n^2) (for each event, the loop is called at event
514 * creation). Might require a hash if we have lots of events.
516 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
517 if (event
->desc
&& !strncmp(event
->desc
->name
,
519 LTTNG_UST_SYM_NAME_LEN
- 1)) {
526 * Check if loglevel match. Refuse to connect event if not.
528 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
529 desc
= ltt_event_get(event_param
->name
);
531 if (!ltt_loglevel_match(desc
,
532 event_param
->loglevel_type
,
533 event_param
->loglevel
)) {
535 goto no_loglevel_match
;
539 * If descriptor is not there, it will be added to
543 event
= zmalloc(sizeof(struct ltt_event
));
549 event
->filter
= filter
;
551 * used_event_id counts the maximum number of event IDs that can
552 * register if all probes register.
554 chan
->used_event_id
++;
556 event
->instrumentation
= event_param
->instrumentation
;
557 /* Populate ltt_event structure before tracepoint registration. */
559 switch (event_param
->instrumentation
) {
560 case LTTNG_UST_TRACEPOINT
:
563 ret
= __tracepoint_probe_register(event_param
->name
,
564 event
->desc
->probe_callback
,
565 event
, event
->desc
->signature
);
568 event
->id
= chan
->free_event_id
++;
571 * If the probe is not present, event->desc stays NULL,
572 * waiting for the probe to register, and the event->id
575 ret
= add_pending_probe(event
, event_param
->name
,
576 event_param
->loglevel_type
,
577 event_param
->loglevel
);
579 goto add_pending_error
;
586 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
588 goto statedump_error
;
590 cds_list_add(&event
->list
, &chan
->session
->events
);
596 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
597 event
->desc
->probe_callback
,
599 ltt_event_put(event
->desc
);
612 * Only used internally at session destruction.
614 int _ltt_event_unregister(struct ltt_event
*event
)
618 switch (event
->instrumentation
) {
619 case LTTNG_UST_TRACEPOINT
:
621 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
622 event
->desc
->probe_callback
,
627 remove_pending_probe(event
->pending_probe
);
638 * Only used internally at session destruction.
641 void _ltt_event_destroy(struct ltt_event
*event
)
643 switch (event
->instrumentation
) {
644 case LTTNG_UST_TRACEPOINT
:
646 ltt_event_put(event
->desc
);
652 cds_list_del(&event
->list
);
653 lttng_destroy_context(event
->ctx
);
658 * We have exclusive access to our metadata buffer (protected by the
659 * ust_lock), so we can do racy operations such as looking for
660 * remaining space left in packet and write, since mutual exclusion
661 * protects us from concurrent writes.
663 int lttng_metadata_printf(struct ltt_session
*session
,
664 const char *fmt
, ...)
666 struct lttng_ust_lib_ring_buffer_ctx ctx
;
667 struct ltt_channel
*chan
= session
->metadata
;
669 int ret
= 0, waitret
;
670 size_t len
, reserve_len
, pos
;
673 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
676 ret
= vasprintf(&str
, fmt
, ap
);
684 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
685 reserve_len
= min_t(size_t,
686 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
688 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
689 sizeof(char), -1, chan
->handle
);
691 * We don't care about metadata buffer's records lost
692 * count, because we always retry here. Report error if
693 * we need to bail out after timeout or being
696 waitret
= wait_cond_interruptible_timeout(
698 ret
= chan
->ops
->event_reserve(&ctx
, 0);
699 ret
!= -ENOBUFS
|| !ret
;
701 LTTNG_METADATA_TIMEOUT_MSEC
);
702 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
703 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
704 waitret
== -EINTR
? "interrupted" :
705 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
706 if (waitret
== -EINTR
)
710 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
711 chan
->ops
->event_commit(&ctx
);
719 int _ltt_field_statedump(struct ltt_session
*session
,
720 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 _ltt_context_metadata_statedump(struct ltt_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
= _ltt_field_statedump(session
, &field
->event_field
);
870 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
871 struct ltt_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
= _ltt_field_statedump(session
, field
);
888 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
889 struct ltt_channel
*chan
,
890 struct ltt_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
,
926 ret
= lttng_metadata_printf(session
,
927 " context := struct {\n");
931 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
935 ret
= lttng_metadata_printf(session
,
941 ret
= lttng_metadata_printf(session
,
942 " fields := struct {\n"
947 ret
= _ltt_fields_metadata_statedump(session
, event
);
952 * LTTng space reservation can only reserve multiples of the
955 ret
= lttng_metadata_printf(session
,
961 event
->metadata_dumped
= 1;
968 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
969 struct ltt_channel
*chan
)
973 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
975 if (chan
== session
->metadata
)
978 WARN_ON_ONCE(!chan
->header_type
);
979 ret
= lttng_metadata_printf(session
,
982 " event.header := %s;\n"
983 " packet.context := struct packet_context;\n",
985 chan
->header_type
== 1 ? "struct event_header_compact" :
986 "struct event_header_large");
991 ret
= lttng_metadata_printf(session
,
992 " event.context := struct {\n");
996 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
1000 ret
= lttng_metadata_printf(session
,
1006 ret
= lttng_metadata_printf(session
,
1009 chan
->metadata_dumped
= 1;
1015 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
1017 return lttng_metadata_printf(session
,
1018 "struct packet_context {\n"
1019 " uint64_clock_monotonic_t timestamp_begin;\n"
1020 " uint64_clock_monotonic_t timestamp_end;\n"
1021 " uint32_t events_discarded;\n"
1022 " uint32_t content_size;\n"
1023 " uint32_t packet_size;\n"
1024 " uint32_t cpu_id;\n"
1031 * id: range: 0 - 30.
1032 * id 31 is reserved to indicate an extended header.
1035 * id: range: 0 - 65534.
1036 * id 65535 is reserved to indicate an extended header.
1039 int _ltt_event_header_declare(struct ltt_session
*session
)
1041 return lttng_metadata_printf(session
,
1042 "struct event_header_compact {\n"
1043 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1046 " uint27_clock_monotonic_t timestamp;\n"
1050 " uint64_clock_monotonic_t timestamp;\n"
1055 "struct event_header_large {\n"
1056 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1059 " uint32_clock_monotonic_t timestamp;\n"
1063 " uint64_clock_monotonic_t timestamp;\n"
1067 lttng_alignof(uint32_t) * CHAR_BIT
,
1068 lttng_alignof(uint16_t) * CHAR_BIT
1073 * Approximation of NTP time of day to clock monotonic correlation,
1074 * taken at start of trace.
1075 * Yes, this is only an approximation. Yes, we can (and will) do better
1076 * in future versions.
1079 uint64_t measure_clock_offset(void)
1081 uint64_t offset
, monotonic
[2], realtime
;
1082 struct timespec rts
= { 0, 0 };
1085 monotonic
[0] = trace_clock_read64();
1086 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1089 monotonic
[1] = trace_clock_read64();
1090 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1091 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
1092 realtime
+= rts
.tv_nsec
;
1093 offset
= realtime
- offset
;
1098 * Output metadata into this session's metadata buffers.
1101 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1103 unsigned char *uuid_c
= session
->uuid
;
1104 char uuid_s
[37], clock_uuid_s
[CLOCK_UUID_LEN
];
1105 struct ltt_channel
*chan
;
1106 struct ltt_event
*event
;
1108 char procname
[PROCNAME_LEN
] = "";
1110 if (!CMM_ACCESS_ONCE(session
->active
))
1112 if (session
->metadata_dumped
)
1114 if (!session
->metadata
) {
1115 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1119 snprintf(uuid_s
, sizeof(uuid_s
),
1120 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1121 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1122 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1123 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1124 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1126 ret
= lttng_metadata_printf(session
,
1127 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1128 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1129 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1130 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1131 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1132 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1138 " byte_order = %s;\n"
1139 " packet.header := struct {\n"
1140 " uint32_t magic;\n"
1141 " uint8_t uuid[16];\n"
1142 " uint32_t stream_id;\n"
1145 lttng_alignof(uint8_t) * CHAR_BIT
,
1146 lttng_alignof(uint16_t) * CHAR_BIT
,
1147 lttng_alignof(uint32_t) * CHAR_BIT
,
1148 lttng_alignof(uint64_t) * CHAR_BIT
,
1152 #if (BYTE_ORDER == BIG_ENDIAN)
1161 /* ignore error, just use empty string if error. */
1162 (void) prctl(PR_GET_NAME
, (unsigned long) procname
, 0, 0, 0);
1163 procname
[PROCNAME_LEN
- 1] = '\0';
1164 ret
= lttng_metadata_printf(session
,
1167 " procname = \"%s\";\n"
1168 " domain = \"ust\";\n"
1169 " tracer_name = \"lttng-ust\";\n"
1170 " tracer_major = %u;\n"
1171 " tracer_minor = %u;\n"
1172 " tracer_patchlevel = %u;\n"
1176 LTTNG_UST_MAJOR_VERSION
,
1177 LTTNG_UST_MINOR_VERSION
,
1178 LTTNG_UST_PATCHLEVEL_VERSION
1183 ret
= lttng_metadata_printf(session
,
1191 if (!trace_clock_uuid(clock_uuid_s
)) {
1192 ret
= lttng_metadata_printf(session
,
1193 " uuid = \"%s\";\n",
1200 ret
= lttng_metadata_printf(session
,
1201 " description = \"Monotonic Clock\";\n"
1202 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1203 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1204 " offset = %" PRIu64
";\n"
1207 measure_clock_offset()
1212 ret
= lttng_metadata_printf(session
,
1213 "typealias integer {\n"
1214 " size = 27; align = 1; signed = false;\n"
1215 " map = clock.monotonic.value;\n"
1216 "} := uint27_clock_monotonic_t;\n"
1218 "typealias integer {\n"
1219 " size = 32; align = %u; signed = false;\n"
1220 " map = clock.monotonic.value;\n"
1221 "} := uint32_clock_monotonic_t;\n"
1223 "typealias integer {\n"
1224 " size = 64; align = %u; signed = false;\n"
1225 " map = clock.monotonic.value;\n"
1226 "} := uint64_clock_monotonic_t;\n\n",
1227 lttng_alignof(uint32_t) * CHAR_BIT
,
1228 lttng_alignof(uint64_t) * CHAR_BIT
1233 ret
= _ltt_stream_packet_context_declare(session
);
1237 ret
= _ltt_event_header_declare(session
);
1242 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1243 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1248 cds_list_for_each_entry(event
, &session
->events
, list
) {
1249 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1253 session
->metadata_dumped
= 1;
1258 void lttng_ust_events_exit(void)
1260 struct ltt_session
*session
, *tmpsession
;
1262 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1263 ltt_session_destroy(session
);
1269 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1270 enum lttng_ust_loglevel_type loglevel_type
,
1273 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1281 int wildcard_is_within(struct wildcard_entry
*e
,
1282 enum lttng_ust_loglevel_type loglevel_type
,
1285 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1286 || e
->loglevel
== -1)
1288 switch (e
->loglevel_type
) {
1289 case LTTNG_UST_LOGLEVEL_RANGE
:
1290 switch (loglevel_type
) {
1291 case LTTNG_UST_LOGLEVEL_RANGE
:
1292 if (e
->loglevel
>= loglevel
)
1296 case LTTNG_UST_LOGLEVEL_SINGLE
:
1297 if (e
->loglevel
<= 0 && loglevel
== 0)
1302 case LTTNG_UST_LOGLEVEL_SINGLE
:
1303 switch (loglevel_type
) {
1304 case LTTNG_UST_LOGLEVEL_RANGE
:
1309 case LTTNG_UST_LOGLEVEL_SINGLE
:
1310 if (e
->loglevel
== loglevel
)
1320 * Add the wildcard to the wildcard list. Must be called with
1324 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1325 struct lttng_ust_event
*event_param
)
1327 struct wildcard_entry
*e
;
1328 struct session_wildcard
*sw
;
1329 size_t name_len
= strlen(event_param
->name
) + 1;
1333 * Try to find global wildcard entry. Given that this is shared
1334 * across all sessions, we need to check for exact loglevel
1335 * match, not just whether contained within the existing ones.
1337 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1338 if (!strncmp(event_param
->name
, e
->name
,
1339 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1340 if (wildcard_same_loglevel(e
,
1341 event_param
->loglevel_type
,
1342 event_param
->loglevel
)) {
1351 * Create global wildcard entry if not found. Using
1352 * zmalloc here to allocate a variable length element.
1353 * Could cause some memory fragmentation if overused.
1355 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1357 return ERR_PTR(-ENOMEM
);
1358 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1359 cds_list_add(&e
->list
, &wildcard_list
);
1360 CDS_INIT_LIST_HEAD(&e
->session_list
);
1363 /* session wildcard */
1364 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1365 if (chan
== sw
->chan
) {
1366 DBG("wildcard %s busy for this channel",
1368 return ERR_PTR(-EEXIST
); /* Already there */
1371 sw
= zmalloc(sizeof(struct session_wildcard
));
1373 return ERR_PTR(-ENOMEM
);
1376 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1377 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1378 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1379 sw
->event_param
.loglevel
= event_param
->loglevel
;
1380 CDS_INIT_LIST_HEAD(&sw
->events
);
1381 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1382 cds_list_add(&sw
->session_list
, &e
->session_list
);
1384 ltt_probes_create_wildcard_events(e
, sw
);
1389 * Remove the wildcard from the wildcard list. Must be called with
1390 * ust_lock held. Only called at session teardown.
1393 void _remove_wildcard(struct session_wildcard
*wildcard
)
1395 struct ltt_event
*ev
, *tmp
;
1398 * Just remove the events owned (for enable/disable) by this
1399 * wildcard from the list. The session teardown will take care
1400 * of freeing the event memory.
1402 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1404 cds_list_del(&ev
->wildcard_list
);
1406 cds_list_del(&wildcard
->session_list
);
1407 cds_list_del(&wildcard
->list
);
1408 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1409 cds_list_del(&wildcard
->entry
->list
);
1410 free(wildcard
->entry
);
1415 int ltt_wildcard_create(struct ltt_channel
*chan
,
1416 struct lttng_ust_event
*event_param
,
1417 struct session_wildcard
**_sw
)
1419 struct session_wildcard
*sw
;
1421 sw
= add_wildcard(chan
, event_param
);
1422 if (!sw
|| IS_ERR(sw
)) {
1430 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1432 _remove_wildcard(sw
);
1435 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1437 struct ltt_event
*ev
;
1440 if (wildcard
->enabled
)
1442 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1443 ret
= ltt_event_enable(ev
);
1445 DBG("Error: enable error.\n");
1449 wildcard
->enabled
= 1;
1453 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1455 struct ltt_event
*ev
;
1458 if (!wildcard
->enabled
)
1460 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1461 ret
= ltt_event_disable(ev
);
1463 DBG("Error: disable error.\n");
1467 wildcard
->enabled
= 0;
1472 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1473 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1474 * taken in constructors, which are called with dynamic linker mutex
1477 void lttng_fixup_event_tls(void)
1479 unsigned char uuid
[37];
1481 (void) uuid_generate(uuid
);