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 "ltt-tracer.h"
54 #include "ltt-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 ltt_event
*event
;
95 struct cds_hlist_node node
;
96 enum lttng_ust_loglevel_type loglevel_type
;
102 * Callsite hash table, containing the registered callsites.
103 * Protected by the sessions mutex.
105 #define CALLSITE_HASH_BITS 6
106 #define CALLSITE_HASH_SIZE (1 << CALLSITE_HASH_BITS)
107 static struct cds_hlist_head callsite_table
[CALLSITE_HASH_SIZE
];
109 static void _ltt_event_destroy(struct ltt_event
*event
);
110 static void _ltt_wildcard_destroy(struct session_wildcard
*sw
);
111 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
112 static int _ltt_event_unregister(struct ltt_event
*event
);
114 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
115 struct ltt_channel
*chan
,
116 struct ltt_event
*event
);
118 int _ltt_callsite_metadata_statedump(struct ltt_session
*session
,
119 struct lttng_callsite
*callsite
);
122 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
124 int ltt_loglevel_match(const struct lttng_event_desc
*desc
,
125 enum lttng_ust_loglevel_type req_type
,
130 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
133 ev_loglevel
= TRACE_DEFAULT
;
135 ev_loglevel
= *(*desc
->loglevel
);
137 case LTTNG_UST_LOGLEVEL_RANGE
:
138 if (ev_loglevel
<= req_loglevel
|| req_loglevel
== -1)
142 case LTTNG_UST_LOGLEVEL_SINGLE
:
143 if (ev_loglevel
== req_loglevel
|| req_loglevel
== -1)
147 case LTTNG_UST_LOGLEVEL_ALL
:
154 * Return wildcard for a given event name if the event name match the
155 * one of the wildcards.
156 * Must be called with ust lock held.
157 * Returns NULL if not present.
160 struct wildcard_entry
*match_wildcard(const struct lttng_event_desc
*desc
)
162 struct wildcard_entry
*e
;
164 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
165 /* If only contain '*' */
166 if (strlen(e
->name
) == 1)
168 /* Compare excluding final '*' */
169 if (!strncmp(desc
->name
, e
->name
, strlen(e
->name
) - 1))
171 continue; /* goto next, no match */
173 if (ltt_loglevel_match(desc
,
178 /* no match, loop to next */
184 * called at event creation if probe is missing.
185 * called with session mutex held.
188 int add_pending_probe(struct ltt_event
*event
, const char *name
,
189 enum lttng_ust_loglevel_type loglevel_type
,
192 struct cds_hlist_head
*head
;
193 struct ust_pending_probe
*e
;
194 size_t name_len
= strlen(name
) + 1;
197 if (name_len
> LTTNG_UST_SYM_NAME_LEN
) {
198 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
);
199 name_len
= LTTNG_UST_SYM_NAME_LEN
;
201 hash
= jhash(name
, name_len
- 1, 0);
202 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
203 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
206 memcpy(&e
->name
[0], name
, name_len
);
207 e
->name
[name_len
- 1] = '\0';
208 e
->loglevel_type
= loglevel_type
;
209 e
->loglevel
= loglevel
;
210 cds_hlist_add_head(&e
->node
, head
);
212 event
->pending_probe
= e
;
217 * remove a pending probe. called when at event teardown and when an
218 * event is fixed (probe is loaded).
219 * called with session mutex held.
222 void remove_pending_probe(struct ust_pending_probe
*e
)
226 cds_hlist_del(&e
->node
);
231 * Called at library load: connect the probe on the events pending on
233 * called with session mutex held.
235 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
237 struct cds_hlist_head
*head
;
238 struct cds_hlist_node
*node
, *p
;
239 struct ust_pending_probe
*e
;
240 const char *name
= desc
->name
;
242 struct lttng_ust_event event_param
;
243 size_t name_len
= strlen(name
) + 1;
248 struct wildcard_entry
*wildcard
;
250 wildcard
= match_wildcard(desc
);
251 if (strcmp(desc
->name
, "lttng_ust:metadata") && wildcard
) {
252 struct session_wildcard
*sw
;
254 cds_list_for_each_entry(sw
, &wildcard
->session_list
,
256 struct ltt_event
*ev
;
259 memcpy(&event_param
, &sw
->event_param
,
260 sizeof(event_param
));
261 memcpy(event_param
.name
,
263 sizeof(event_param
.name
));
265 ret
= ltt_event_create(sw
->chan
,
268 DBG("Error creating event");
271 cds_list_add(&ev
->wildcard_list
,
273 lttng_filter_event_link_bytecode(ev
,
274 sw
->filter_bytecode
);
279 if (name_len
> LTTNG_UST_SYM_NAME_LEN
) {
280 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
);
281 name_len
= LTTNG_UST_SYM_NAME_LEN
;
283 hash
= jhash(name
, name_len
- 1, 0);
284 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
285 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
286 struct ltt_event
*event
;
287 struct ltt_channel
*chan
;
289 if (!ltt_loglevel_match(desc
,
294 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
299 assert(!event
->desc
);
301 event
->pending_probe
= NULL
;
302 remove_pending_probe(e
);
303 ret
|= __tracepoint_probe_register(name
,
304 event
->desc
->probe_callback
,
305 event
, event
->desc
->signature
);
308 event
->id
= chan
->free_event_id
++;
309 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
311 lttng_filter_event_link_bytecode(event
,
312 event
->filter_bytecode
);
318 * Called at library load: add callsite information.
319 * Takes the session mutex.
320 * Should _not_ be called with tracepoint mutex held (would cause
321 * deadlock with session mutex).
323 int lttng_callsite_add(const struct tracepoint_callsite
*tp_cs
)
325 struct cds_hlist_head
*head
;
326 struct lttng_callsite
*cs_node
;
327 struct ltt_session
*session
;
332 /* hash by pointer value */
333 hash
= jhash(&tp_cs
, sizeof(tp_cs
), 0);
334 head
= &callsite_table
[hash
& (CALLSITE_HASH_SIZE
- 1)];
335 cs_node
= zmalloc(sizeof(struct lttng_callsite
));
338 cds_hlist_add_head(&cs_node
->node
, head
);
339 cs_node
->tp_cs
= tp_cs
;
341 /* print metadata for each session */
342 cds_list_for_each_entry(session
, &sessions
, list
) {
343 ret
= _ltt_callsite_metadata_statedump(session
, cs_node
);
355 * Called at library unload: remove callsite information.
356 * Takes the session mutex.
357 * Should _not_ be called with tracepoint mutex held (would cause
358 * deadlock with session mutex).
360 int lttng_callsite_remove(const struct tracepoint_callsite
*tp_cs
)
362 struct cds_hlist_head
*head
;
363 struct cds_hlist_node
*node
;
364 struct lttng_callsite
*cs_node
;
369 /* hash by pointer value */
370 hash
= jhash(&tp_cs
, sizeof(tp_cs
), 0);
371 head
= &callsite_table
[hash
& (CALLSITE_HASH_SIZE
- 1)];
372 cds_hlist_for_each_entry(cs_node
, node
, head
, node
) {
373 if (cs_node
->tp_cs
== tp_cs
) {
380 cds_hlist_del(&cs_node
->node
);
391 * Called with ust mutex held.
394 int _callsite_session_metadata_statedump(struct ltt_session
*session
)
399 for (i
= 0; i
< CALLSITE_HASH_SIZE
; i
++) {
400 struct cds_hlist_head
*head
;
401 struct cds_hlist_node
*node
;
402 struct lttng_callsite
*cs_node
;
404 head
= &callsite_table
[i
];
405 cds_hlist_for_each_entry(cs_node
, node
, head
, node
) {
406 ret
= _ltt_callsite_metadata_statedump(session
,
415 void synchronize_trace(void)
420 struct ltt_session
*ltt_session_create(void)
422 struct ltt_session
*session
;
425 session
= zmalloc(sizeof(struct ltt_session
));
428 CDS_INIT_LIST_HEAD(&session
->chan
);
429 CDS_INIT_LIST_HEAD(&session
->events
);
430 CDS_INIT_LIST_HEAD(&session
->wildcards
);
431 ret
= lttng_ust_uuid_generate(session
->uuid
);
433 session
->uuid
[0] = '\0';
435 cds_list_add(&session
->list
, &sessions
);
439 void ltt_session_destroy(struct ltt_session
*session
)
441 struct ltt_channel
*chan
, *tmpchan
;
442 struct ltt_event
*event
, *tmpevent
;
443 struct session_wildcard
*wildcard
, *tmpwildcard
;
446 CMM_ACCESS_ONCE(session
->active
) = 0;
447 cds_list_for_each_entry(event
, &session
->events
, list
) {
448 ret
= _ltt_event_unregister(event
);
451 synchronize_trace(); /* Wait for in-flight events to complete */
452 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
453 _ltt_wildcard_destroy(wildcard
);
454 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
455 _ltt_event_destroy(event
);
456 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
457 _ltt_channel_destroy(chan
);
458 cds_list_del(&session
->list
);
462 int ltt_session_enable(struct ltt_session
*session
)
465 struct ltt_channel
*chan
;
467 if (session
->active
) {
473 * Snapshot the number of events per channel to know the type of header
476 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
477 if (chan
->header_type
)
478 continue; /* don't change it if session stop/restart */
479 if (chan
->free_event_id
< 31)
480 chan
->header_type
= 1; /* compact */
482 chan
->header_type
= 2; /* large */
485 CMM_ACCESS_ONCE(session
->active
) = 1;
486 CMM_ACCESS_ONCE(session
->been_active
) = 1;
487 ret
= _ltt_session_metadata_statedump(session
);
489 CMM_ACCESS_ONCE(session
->active
) = 0;
494 int ltt_session_disable(struct ltt_session
*session
)
498 if (!session
->active
) {
502 CMM_ACCESS_ONCE(session
->active
) = 0;
507 int ltt_channel_enable(struct ltt_channel
*channel
)
511 if (channel
== channel
->session
->metadata
)
513 old
= uatomic_xchg(&channel
->enabled
, 1);
519 int ltt_channel_disable(struct ltt_channel
*channel
)
523 if (channel
== channel
->session
->metadata
)
525 old
= uatomic_xchg(&channel
->enabled
, 0);
531 int ltt_event_enable(struct ltt_event
*event
)
535 if (event
->chan
== event
->chan
->session
->metadata
)
537 old
= uatomic_xchg(&event
->enabled
, 1);
543 int ltt_event_disable(struct ltt_event
*event
)
547 if (event
->chan
== event
->chan
->session
->metadata
)
549 old
= uatomic_xchg(&event
->enabled
, 0);
555 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
556 const char *transport_name
,
558 size_t subbuf_size
, size_t num_subbuf
,
559 unsigned int switch_timer_interval
,
560 unsigned int read_timer_interval
,
561 int **shm_fd
, int **wait_fd
,
562 uint64_t **memory_map_size
,
563 struct ltt_channel
*chan_priv_init
)
565 struct ltt_channel
*chan
= NULL
;
566 struct ltt_transport
*transport
;
568 if (session
->been_active
)
569 goto active
; /* Refuse to add channel to active session */
570 transport
= ltt_transport_find(transport_name
);
572 DBG("LTTng transport %s not found\n",
576 chan_priv_init
->id
= session
->free_chan_id
++;
577 chan_priv_init
->session
= session
;
579 * Note: the channel creation op already writes into the packet
580 * headers. Therefore the "chan" information used as input
581 * should be already accessible.
583 chan
= transport
->ops
.channel_create(transport_name
, buf_addr
,
584 subbuf_size
, num_subbuf
, switch_timer_interval
,
585 read_timer_interval
, shm_fd
, wait_fd
,
586 memory_map_size
, chan_priv_init
);
590 chan
->ops
= &transport
->ops
;
591 cds_list_add(&chan
->list
, &session
->chan
);
601 * Only used internally at session destruction.
604 void _ltt_channel_destroy(struct ltt_channel
*chan
)
606 cds_list_del(&chan
->list
);
607 lttng_destroy_context(chan
->ctx
);
608 chan
->ops
->channel_destroy(chan
);
612 * Supports event creation while tracing session is active.
614 int ltt_event_create(struct ltt_channel
*chan
,
615 struct lttng_ust_event
*event_param
,
616 struct ltt_event
**_event
)
618 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
619 struct ltt_event
*event
;
622 if (chan
->used_event_id
== -1U) {
627 * This is O(n^2) (for each event, the loop is called at event
628 * creation). Might require a hash if we have lots of events.
630 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
631 if (event
->desc
&& !strncmp(event
->desc
->name
,
633 LTTNG_UST_SYM_NAME_LEN
- 1)) {
640 * Check if loglevel match. Refuse to connect event if not.
642 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
643 desc
= ltt_event_get(event_param
->name
);
645 if (!ltt_loglevel_match(desc
,
646 event_param
->loglevel_type
,
647 event_param
->loglevel
)) {
649 goto no_loglevel_match
;
653 * If descriptor is not there, it will be added to
657 event
= zmalloc(sizeof(struct ltt_event
));
664 * used_event_id counts the maximum number of event IDs that can
665 * register if all probes register.
667 chan
->used_event_id
++;
669 event
->instrumentation
= event_param
->instrumentation
;
670 /* Populate ltt_event structure before tracepoint registration. */
672 switch (event_param
->instrumentation
) {
673 case LTTNG_UST_TRACEPOINT
:
676 ret
= __tracepoint_probe_register(event_param
->name
,
677 event
->desc
->probe_callback
,
678 event
, event
->desc
->signature
);
681 event
->id
= chan
->free_event_id
++;
684 * If the probe is not present, event->desc stays NULL,
685 * waiting for the probe to register, and the event->id
688 ret
= add_pending_probe(event
, event_param
->name
,
689 event_param
->loglevel_type
,
690 event_param
->loglevel
);
692 goto add_pending_error
;
699 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
701 goto statedump_error
;
703 cds_list_add(&event
->list
, &chan
->session
->events
);
709 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
710 event
->desc
->probe_callback
,
712 ltt_event_put(event
->desc
);
725 * Only used internally at session destruction.
727 int _ltt_event_unregister(struct ltt_event
*event
)
731 switch (event
->instrumentation
) {
732 case LTTNG_UST_TRACEPOINT
:
734 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
735 event
->desc
->probe_callback
,
740 remove_pending_probe(event
->pending_probe
);
751 * Only used internally at session destruction.
754 void _ltt_event_destroy(struct ltt_event
*event
)
756 switch (event
->instrumentation
) {
757 case LTTNG_UST_TRACEPOINT
:
759 ltt_event_put(event
->desc
);
765 cds_list_del(&event
->list
);
766 lttng_destroy_context(event
->ctx
);
767 free(event
->filter_bytecode
);
768 free(event
->filter_data
);
773 * We have exclusive access to our metadata buffer (protected by the
774 * ust_lock), so we can do racy operations such as looking for
775 * remaining space left in packet and write, since mutual exclusion
776 * protects us from concurrent writes.
778 int lttng_metadata_printf(struct ltt_session
*session
,
779 const char *fmt
, ...)
781 struct lttng_ust_lib_ring_buffer_ctx ctx
;
782 struct ltt_channel
*chan
= session
->metadata
;
784 int ret
= 0, waitret
;
785 size_t len
, reserve_len
, pos
;
788 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
791 ret
= vasprintf(&str
, fmt
, ap
);
799 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
800 reserve_len
= min_t(size_t,
801 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
803 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
804 sizeof(char), -1, chan
->handle
);
806 * We don't care about metadata buffer's records lost
807 * count, because we always retry here. Report error if
808 * we need to bail out after timeout or being
811 waitret
= wait_cond_interruptible_timeout(
813 ret
= chan
->ops
->event_reserve(&ctx
, 0);
814 ret
!= -ENOBUFS
|| !ret
;
816 LTTNG_METADATA_TIMEOUT_MSEC
);
817 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
818 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
819 waitret
== -EINTR
? "interrupted" :
820 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
821 if (waitret
== -EINTR
)
825 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
826 chan
->ops
->event_commit(&ctx
);
834 int _ltt_field_statedump(struct ltt_session
*session
,
835 const struct lttng_event_field
*field
)
842 switch (field
->type
.atype
) {
844 ret
= lttng_metadata_printf(session
,
845 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
846 field
->type
.u
.basic
.integer
.size
,
847 field
->type
.u
.basic
.integer
.alignment
,
848 field
->type
.u
.basic
.integer
.signedness
,
849 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
851 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
854 field
->type
.u
.basic
.integer
.base
,
855 #if (BYTE_ORDER == BIG_ENDIAN)
856 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
858 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
863 ret
= lttng_metadata_printf(session
,
864 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
865 field
->type
.u
.basic
._float
.exp_dig
,
866 field
->type
.u
.basic
._float
.mant_dig
,
867 field
->type
.u
.basic
._float
.alignment
,
868 #if (BYTE_ORDER == BIG_ENDIAN)
869 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
871 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
876 ret
= lttng_metadata_printf(session
,
878 field
->type
.u
.basic
.enumeration
.name
,
883 const struct lttng_basic_type
*elem_type
;
885 elem_type
= &field
->type
.u
.array
.elem_type
;
886 ret
= lttng_metadata_printf(session
,
887 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
888 elem_type
->u
.basic
.integer
.size
,
889 elem_type
->u
.basic
.integer
.alignment
,
890 elem_type
->u
.basic
.integer
.signedness
,
891 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
893 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
896 elem_type
->u
.basic
.integer
.base
,
897 #if (BYTE_ORDER == BIG_ENDIAN)
898 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
900 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
902 field
->name
, field
->type
.u
.array
.length
);
907 const struct lttng_basic_type
*elem_type
;
908 const struct lttng_basic_type
*length_type
;
910 elem_type
= &field
->type
.u
.sequence
.elem_type
;
911 length_type
= &field
->type
.u
.sequence
.length_type
;
912 ret
= lttng_metadata_printf(session
,
913 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
914 length_type
->u
.basic
.integer
.size
,
915 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
916 length_type
->u
.basic
.integer
.signedness
,
917 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
919 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
922 length_type
->u
.basic
.integer
.base
,
923 #if (BYTE_ORDER == BIG_ENDIAN)
924 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
926 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
932 ret
= lttng_metadata_printf(session
,
933 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
934 elem_type
->u
.basic
.integer
.size
,
935 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
936 elem_type
->u
.basic
.integer
.signedness
,
937 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
939 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
942 elem_type
->u
.basic
.integer
.base
,
943 #if (BYTE_ORDER == BIG_ENDIAN)
944 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
946 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
954 /* Default encoding is UTF8 */
955 ret
= lttng_metadata_printf(session
,
957 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
958 " { encoding = ASCII; }" : "",
969 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
970 struct lttng_ctx
*ctx
)
977 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
978 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
980 ret
= _ltt_field_statedump(session
, &field
->event_field
);
988 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
989 struct ltt_event
*event
)
991 const struct lttng_event_desc
*desc
= event
->desc
;
995 for (i
= 0; i
< desc
->nr_fields
; i
++) {
996 const struct lttng_event_field
*field
= &desc
->fields
[i
];
998 ret
= _ltt_field_statedump(session
, field
);
1006 int _ltt_callsite_metadata_statedump(struct ltt_session
*session
,
1007 struct lttng_callsite
*callsite
)
1011 if (!CMM_ACCESS_ONCE(session
->active
))
1014 ret
= lttng_metadata_printf(session
,
1021 callsite
->tp_cs
->tp
->name
,
1022 callsite
->tp_cs
->func
,
1023 callsite
->tp_cs
->file
,
1024 callsite
->tp_cs
->lineno
);
1032 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
1033 struct ltt_channel
*chan
,
1034 struct ltt_event
*event
)
1037 int loglevel
= TRACE_DEFAULT
;
1039 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
1041 if (chan
== session
->metadata
)
1044 * Don't print events for which probe load is pending.
1049 ret
= lttng_metadata_printf(session
,
1053 " stream_id = %u;\n",
1060 if (event
->desc
->loglevel
)
1061 loglevel
= *(*event
->desc
->loglevel
);
1063 ret
= lttng_metadata_printf(session
,
1064 " loglevel = %d;\n",
1069 if (event
->desc
->u
.ext
.model_emf_uri
) {
1070 ret
= lttng_metadata_printf(session
,
1071 " model.emf.uri = \"%s\";\n",
1072 *(event
->desc
->u
.ext
.model_emf_uri
));
1078 ret
= lttng_metadata_printf(session
,
1079 " context := struct {\n");
1083 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
1087 ret
= lttng_metadata_printf(session
,
1093 ret
= lttng_metadata_printf(session
,
1094 " fields := struct {\n"
1099 ret
= _ltt_fields_metadata_statedump(session
, event
);
1104 * LTTng space reservation can only reserve multiples of the
1107 ret
= lttng_metadata_printf(session
,
1113 event
->metadata_dumped
= 1;
1120 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
1121 struct ltt_channel
*chan
)
1125 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
1127 if (chan
== session
->metadata
)
1130 WARN_ON_ONCE(!chan
->header_type
);
1131 ret
= lttng_metadata_printf(session
,
1134 " event.header := %s;\n"
1135 " packet.context := struct packet_context;\n",
1137 chan
->header_type
== 1 ? "struct event_header_compact" :
1138 "struct event_header_large");
1143 ret
= lttng_metadata_printf(session
,
1144 " event.context := struct {\n");
1148 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
1152 ret
= lttng_metadata_printf(session
,
1158 ret
= lttng_metadata_printf(session
,
1161 chan
->metadata_dumped
= 1;
1167 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
1169 return lttng_metadata_printf(session
,
1170 "struct packet_context {\n"
1171 " uint64_clock_monotonic_t timestamp_begin;\n"
1172 " uint64_clock_monotonic_t timestamp_end;\n"
1173 " uint64_t content_size;\n"
1174 " uint64_t packet_size;\n"
1175 " unsigned long events_discarded;\n"
1176 " uint32_t cpu_id;\n"
1183 * id: range: 0 - 30.
1184 * id 31 is reserved to indicate an extended header.
1187 * id: range: 0 - 65534.
1188 * id 65535 is reserved to indicate an extended header.
1191 int _ltt_event_header_declare(struct ltt_session
*session
)
1193 return lttng_metadata_printf(session
,
1194 "struct event_header_compact {\n"
1195 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1198 " uint27_clock_monotonic_t timestamp;\n"
1202 " uint64_clock_monotonic_t timestamp;\n"
1207 "struct event_header_large {\n"
1208 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1211 " uint32_clock_monotonic_t timestamp;\n"
1215 " uint64_clock_monotonic_t timestamp;\n"
1219 lttng_alignof(uint32_t) * CHAR_BIT
,
1220 lttng_alignof(uint16_t) * CHAR_BIT
1225 * Approximation of NTP time of day to clock monotonic correlation,
1226 * taken at start of trace.
1227 * Yes, this is only an approximation. Yes, we can (and will) do better
1228 * in future versions.
1231 uint64_t measure_clock_offset(void)
1233 uint64_t offset
, monotonic
[2], realtime
;
1234 struct timespec rts
= { 0, 0 };
1237 monotonic
[0] = trace_clock_read64();
1238 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1241 monotonic
[1] = trace_clock_read64();
1242 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1243 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
1244 realtime
+= rts
.tv_nsec
;
1245 offset
= realtime
- offset
;
1250 * Output metadata into this session's metadata buffers.
1253 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1255 unsigned char *uuid_c
= session
->uuid
;
1256 char uuid_s
[LTTNG_UST_UUID_STR_LEN
],
1257 clock_uuid_s
[LTTNG_UST_UUID_STR_LEN
];
1258 struct ltt_channel
*chan
;
1259 struct ltt_event
*event
;
1261 char procname
[LTTNG_UST_PROCNAME_LEN
] = "";
1262 char hostname
[HOST_NAME_MAX
];
1264 if (!CMM_ACCESS_ONCE(session
->active
))
1266 if (session
->metadata_dumped
)
1268 if (!session
->metadata
) {
1269 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1273 snprintf(uuid_s
, sizeof(uuid_s
),
1274 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1275 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1276 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1277 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1278 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1280 ret
= lttng_metadata_printf(session
,
1281 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1282 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1283 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1284 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1285 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1286 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1287 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1293 " byte_order = %s;\n"
1294 " packet.header := struct {\n"
1295 " uint32_t magic;\n"
1296 " uint8_t uuid[16];\n"
1297 " uint32_t stream_id;\n"
1300 lttng_alignof(uint8_t) * CHAR_BIT
,
1301 lttng_alignof(uint16_t) * CHAR_BIT
,
1302 lttng_alignof(uint32_t) * CHAR_BIT
,
1303 lttng_alignof(uint64_t) * CHAR_BIT
,
1304 sizeof(unsigned long) * CHAR_BIT
,
1305 lttng_alignof(unsigned long) * CHAR_BIT
,
1309 #if (BYTE_ORDER == BIG_ENDIAN)
1318 /* ignore error, just use empty string if error. */
1320 ret
= gethostname(hostname
, sizeof(hostname
));
1321 if (ret
&& errno
== ENAMETOOLONG
)
1322 hostname
[HOST_NAME_MAX
- 1] = '\0';
1323 lttng_ust_getprocname(procname
);
1324 procname
[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
1325 ret
= lttng_metadata_printf(session
,
1327 " hostname = \"%s\";\n"
1329 " procname = \"%s\";\n"
1330 " domain = \"ust\";\n"
1331 " tracer_name = \"lttng-ust\";\n"
1332 " tracer_major = %u;\n"
1333 " tracer_minor = %u;\n"
1334 " tracer_patchlevel = %u;\n"
1339 LTTNG_UST_MAJOR_VERSION
,
1340 LTTNG_UST_MINOR_VERSION
,
1341 LTTNG_UST_PATCHLEVEL_VERSION
1346 ret
= lttng_metadata_printf(session
,
1354 if (!trace_clock_uuid(clock_uuid_s
)) {
1355 ret
= lttng_metadata_printf(session
,
1356 " uuid = \"%s\";\n",
1363 ret
= lttng_metadata_printf(session
,
1364 " description = \"Monotonic Clock\";\n"
1365 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1366 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1367 " offset = %" PRIu64
";\n"
1370 measure_clock_offset()
1375 ret
= lttng_metadata_printf(session
,
1376 "typealias integer {\n"
1377 " size = 27; align = 1; signed = false;\n"
1378 " map = clock.monotonic.value;\n"
1379 "} := uint27_clock_monotonic_t;\n"
1381 "typealias integer {\n"
1382 " size = 32; align = %u; signed = false;\n"
1383 " map = clock.monotonic.value;\n"
1384 "} := uint32_clock_monotonic_t;\n"
1386 "typealias integer {\n"
1387 " size = 64; align = %u; signed = false;\n"
1388 " map = clock.monotonic.value;\n"
1389 "} := uint64_clock_monotonic_t;\n\n",
1390 lttng_alignof(uint32_t) * CHAR_BIT
,
1391 lttng_alignof(uint64_t) * CHAR_BIT
1396 ret
= _ltt_stream_packet_context_declare(session
);
1400 ret
= _ltt_event_header_declare(session
);
1403 ret
= _callsite_session_metadata_statedump(session
);
1408 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1409 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1414 cds_list_for_each_entry(event
, &session
->events
, list
) {
1415 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1419 session
->metadata_dumped
= 1;
1424 void lttng_ust_events_exit(void)
1426 struct ltt_session
*session
, *tmpsession
;
1428 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1429 ltt_session_destroy(session
);
1435 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1436 enum lttng_ust_loglevel_type loglevel_type
,
1439 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1447 int wildcard_is_within(struct wildcard_entry
*e
,
1448 enum lttng_ust_loglevel_type loglevel_type
,
1451 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1452 || e
->loglevel
== -1)
1454 switch (e
->loglevel_type
) {
1455 case LTTNG_UST_LOGLEVEL_RANGE
:
1456 switch (loglevel_type
) {
1457 case LTTNG_UST_LOGLEVEL_RANGE
:
1458 if (e
->loglevel
>= loglevel
)
1462 case LTTNG_UST_LOGLEVEL_SINGLE
:
1463 if (e
->loglevel
<= 0 && loglevel
== 0)
1468 case LTTNG_UST_LOGLEVEL_SINGLE
:
1469 switch (loglevel_type
) {
1470 case LTTNG_UST_LOGLEVEL_RANGE
:
1475 case LTTNG_UST_LOGLEVEL_SINGLE
:
1476 if (e
->loglevel
== loglevel
)
1486 * Add the wildcard to the wildcard list. Must be called with
1490 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1491 struct lttng_ust_event
*event_param
)
1493 struct wildcard_entry
*e
;
1494 struct session_wildcard
*sw
;
1495 size_t name_len
= strlen(event_param
->name
) + 1;
1499 * Try to find global wildcard entry. Given that this is shared
1500 * across all sessions, we need to check for exact loglevel
1501 * match, not just whether contained within the existing ones.
1503 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1504 if (!strncmp(event_param
->name
, e
->name
,
1505 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1506 if (wildcard_same_loglevel(e
,
1507 event_param
->loglevel_type
,
1508 event_param
->loglevel
)) {
1517 * Create global wildcard entry if not found. Using
1518 * zmalloc here to allocate a variable length element.
1519 * Could cause some memory fragmentation if overused.
1521 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1523 return ERR_PTR(-ENOMEM
);
1524 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1525 e
->loglevel_type
= event_param
->loglevel_type
;
1526 e
->loglevel
= event_param
->loglevel
;
1527 cds_list_add(&e
->list
, &wildcard_list
);
1528 CDS_INIT_LIST_HEAD(&e
->session_list
);
1531 /* session wildcard */
1532 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1533 if (chan
== sw
->chan
) {
1534 DBG("wildcard %s busy for this channel",
1536 return ERR_PTR(-EEXIST
); /* Already there */
1539 sw
= zmalloc(sizeof(struct session_wildcard
));
1541 return ERR_PTR(-ENOMEM
);
1544 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1545 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1546 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1547 sw
->event_param
.loglevel
= event_param
->loglevel
;
1548 CDS_INIT_LIST_HEAD(&sw
->events
);
1549 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1550 cds_list_add(&sw
->session_list
, &e
->session_list
);
1552 ltt_probes_create_wildcard_events(e
, sw
);
1557 * Remove the wildcard from the wildcard list. Must be called with
1558 * ust_lock held. Only called at session teardown.
1561 void _remove_wildcard(struct session_wildcard
*wildcard
)
1563 struct ltt_event
*ev
, *tmp
;
1566 * Just remove the events owned (for enable/disable) by this
1567 * wildcard from the list. The session teardown will take care
1568 * of freeing the event memory.
1570 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1572 cds_list_del(&ev
->wildcard_list
);
1574 cds_list_del(&wildcard
->session_list
);
1575 cds_list_del(&wildcard
->list
);
1576 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1577 cds_list_del(&wildcard
->entry
->list
);
1578 free(wildcard
->entry
);
1580 free(wildcard
->filter_bytecode
);
1584 int ltt_wildcard_create(struct ltt_channel
*chan
,
1585 struct lttng_ust_event
*event_param
,
1586 struct session_wildcard
**_sw
)
1588 struct session_wildcard
*sw
;
1590 sw
= add_wildcard(chan
, event_param
);
1591 if (!sw
|| IS_ERR(sw
)) {
1599 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1601 _remove_wildcard(sw
);
1604 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1606 struct ltt_event
*ev
;
1609 if (wildcard
->enabled
)
1611 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1612 ret
= ltt_event_enable(ev
);
1614 DBG("Error: enable error.\n");
1618 wildcard
->enabled
= 1;
1622 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1624 struct ltt_event
*ev
;
1627 if (!wildcard
->enabled
)
1629 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1630 ret
= ltt_event_disable(ev
);
1632 DBG("Error: disable error.\n");
1636 wildcard
->enabled
= 0;
1641 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1642 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1643 * taken in constructors, which are called with dynamic linker mutex
1646 void lttng_fixup_event_tls(void)
1648 unsigned char uuid
[LTTNG_UST_UUID_STR_LEN
];
1650 (void) lttng_ust_uuid_generate(uuid
);