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 strncpy(event_param
.name
,
263 sizeof(event_param
.name
));
264 event_param
.name
[sizeof(event_param
.name
) - 1] = '\0';
266 ret
= ltt_event_create(sw
->chan
,
269 DBG("Error creating event");
272 cds_list_add(&ev
->wildcard_list
,
274 lttng_filter_event_link_bytecode(ev
,
275 sw
->filter_bytecode
);
280 if (name_len
> LTTNG_UST_SYM_NAME_LEN
) {
281 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
);
282 name_len
= LTTNG_UST_SYM_NAME_LEN
;
284 hash
= jhash(name
, name_len
- 1, 0);
285 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
286 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
287 struct ltt_event
*event
;
288 struct ltt_channel
*chan
;
290 if (!ltt_loglevel_match(desc
,
295 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
300 assert(!event
->desc
);
302 event
->pending_probe
= NULL
;
303 remove_pending_probe(e
);
304 ret
|= __tracepoint_probe_register(name
,
305 event
->desc
->probe_callback
,
306 event
, event
->desc
->signature
);
309 event
->id
= chan
->free_event_id
++;
310 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
312 lttng_filter_event_link_bytecode(event
,
313 event
->filter_bytecode
);
319 * Called at library load: add callsite information.
320 * Takes the session mutex.
321 * Should _not_ be called with tracepoint mutex held (would cause
322 * deadlock with session mutex).
324 int lttng_callsite_add(const struct tracepoint_callsite
*tp_cs
)
326 struct cds_hlist_head
*head
;
327 struct lttng_callsite
*cs_node
;
328 struct ltt_session
*session
;
333 /* hash by pointer value */
334 hash
= jhash(&tp_cs
, sizeof(tp_cs
), 0);
335 head
= &callsite_table
[hash
& (CALLSITE_HASH_SIZE
- 1)];
336 cs_node
= zmalloc(sizeof(struct lttng_callsite
));
339 cds_hlist_add_head(&cs_node
->node
, head
);
340 cs_node
->tp_cs
= tp_cs
;
342 /* print metadata for each session */
343 cds_list_for_each_entry(session
, &sessions
, list
) {
344 ret
= _ltt_callsite_metadata_statedump(session
, cs_node
);
356 * Called at library unload: remove callsite information.
357 * Takes the session mutex.
358 * Should _not_ be called with tracepoint mutex held (would cause
359 * deadlock with session mutex).
361 int lttng_callsite_remove(const struct tracepoint_callsite
*tp_cs
)
363 struct cds_hlist_head
*head
;
364 struct cds_hlist_node
*node
;
365 struct lttng_callsite
*cs_node
;
370 /* hash by pointer value */
371 hash
= jhash(&tp_cs
, sizeof(tp_cs
), 0);
372 head
= &callsite_table
[hash
& (CALLSITE_HASH_SIZE
- 1)];
373 cds_hlist_for_each_entry(cs_node
, node
, head
, node
) {
374 if (cs_node
->tp_cs
== tp_cs
) {
381 cds_hlist_del(&cs_node
->node
);
392 * Called with ust mutex held.
395 int _callsite_session_metadata_statedump(struct ltt_session
*session
)
400 for (i
= 0; i
< CALLSITE_HASH_SIZE
; i
++) {
401 struct cds_hlist_head
*head
;
402 struct cds_hlist_node
*node
;
403 struct lttng_callsite
*cs_node
;
405 head
= &callsite_table
[i
];
406 cds_hlist_for_each_entry(cs_node
, node
, head
, node
) {
407 ret
= _ltt_callsite_metadata_statedump(session
,
416 void synchronize_trace(void)
421 struct ltt_session
*ltt_session_create(void)
423 struct ltt_session
*session
;
426 session
= zmalloc(sizeof(struct ltt_session
));
429 CDS_INIT_LIST_HEAD(&session
->chan
);
430 CDS_INIT_LIST_HEAD(&session
->events
);
431 CDS_INIT_LIST_HEAD(&session
->wildcards
);
432 ret
= lttng_ust_uuid_generate(session
->uuid
);
434 session
->uuid
[0] = '\0';
436 cds_list_add(&session
->list
, &sessions
);
440 void ltt_session_destroy(struct ltt_session
*session
)
442 struct ltt_channel
*chan
, *tmpchan
;
443 struct ltt_event
*event
, *tmpevent
;
444 struct session_wildcard
*wildcard
, *tmpwildcard
;
447 CMM_ACCESS_ONCE(session
->active
) = 0;
448 cds_list_for_each_entry(event
, &session
->events
, list
) {
449 ret
= _ltt_event_unregister(event
);
452 synchronize_trace(); /* Wait for in-flight events to complete */
453 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
454 _ltt_wildcard_destroy(wildcard
);
455 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
456 _ltt_event_destroy(event
);
457 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
458 _ltt_channel_destroy(chan
);
459 cds_list_del(&session
->list
);
463 int ltt_session_enable(struct ltt_session
*session
)
466 struct ltt_channel
*chan
;
468 if (session
->active
) {
474 * Snapshot the number of events per channel to know the type of header
477 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
478 if (chan
->header_type
)
479 continue; /* don't change it if session stop/restart */
480 if (chan
->free_event_id
< 31)
481 chan
->header_type
= 1; /* compact */
483 chan
->header_type
= 2; /* large */
486 CMM_ACCESS_ONCE(session
->active
) = 1;
487 CMM_ACCESS_ONCE(session
->been_active
) = 1;
488 ret
= _ltt_session_metadata_statedump(session
);
490 CMM_ACCESS_ONCE(session
->active
) = 0;
495 int ltt_session_disable(struct ltt_session
*session
)
499 if (!session
->active
) {
503 CMM_ACCESS_ONCE(session
->active
) = 0;
508 int ltt_channel_enable(struct ltt_channel
*channel
)
512 if (channel
== channel
->session
->metadata
)
514 old
= uatomic_xchg(&channel
->enabled
, 1);
520 int ltt_channel_disable(struct ltt_channel
*channel
)
524 if (channel
== channel
->session
->metadata
)
526 old
= uatomic_xchg(&channel
->enabled
, 0);
532 int ltt_event_enable(struct ltt_event
*event
)
536 if (event
->chan
== event
->chan
->session
->metadata
)
538 old
= uatomic_xchg(&event
->enabled
, 1);
544 int ltt_event_disable(struct ltt_event
*event
)
548 if (event
->chan
== event
->chan
->session
->metadata
)
550 old
= uatomic_xchg(&event
->enabled
, 0);
556 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
557 const char *transport_name
,
559 size_t subbuf_size
, size_t num_subbuf
,
560 unsigned int switch_timer_interval
,
561 unsigned int read_timer_interval
,
562 int **shm_fd
, int **wait_fd
,
563 uint64_t **memory_map_size
,
564 struct ltt_channel
*chan_priv_init
)
566 struct ltt_channel
*chan
= NULL
;
567 struct ltt_transport
*transport
;
569 if (session
->been_active
)
570 goto active
; /* Refuse to add channel to active session */
571 transport
= ltt_transport_find(transport_name
);
573 DBG("LTTng transport %s not found\n",
577 chan_priv_init
->id
= session
->free_chan_id
++;
578 chan_priv_init
->session
= session
;
580 * Note: the channel creation op already writes into the packet
581 * headers. Therefore the "chan" information used as input
582 * should be already accessible.
584 chan
= transport
->ops
.channel_create(transport_name
, buf_addr
,
585 subbuf_size
, num_subbuf
, switch_timer_interval
,
586 read_timer_interval
, shm_fd
, wait_fd
,
587 memory_map_size
, chan_priv_init
);
591 chan
->ops
= &transport
->ops
;
592 cds_list_add(&chan
->list
, &session
->chan
);
602 * Only used internally at session destruction.
605 void _ltt_channel_destroy(struct ltt_channel
*chan
)
607 cds_list_del(&chan
->list
);
608 lttng_destroy_context(chan
->ctx
);
609 chan
->ops
->channel_destroy(chan
);
613 * Supports event creation while tracing session is active.
615 int ltt_event_create(struct ltt_channel
*chan
,
616 struct lttng_ust_event
*event_param
,
617 struct ltt_event
**_event
)
619 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
620 struct ltt_event
*event
;
623 if (chan
->used_event_id
== -1U) {
628 * This is O(n^2) (for each event, the loop is called at event
629 * creation). Might require a hash if we have lots of events.
631 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
632 if (event
->desc
&& !strncmp(event
->desc
->name
,
634 LTTNG_UST_SYM_NAME_LEN
- 1)) {
641 * Check if loglevel match. Refuse to connect event if not.
643 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
644 desc
= ltt_event_get(event_param
->name
);
646 if (!ltt_loglevel_match(desc
,
647 event_param
->loglevel_type
,
648 event_param
->loglevel
)) {
650 goto no_loglevel_match
;
654 * If descriptor is not there, it will be added to
658 event
= zmalloc(sizeof(struct ltt_event
));
665 * used_event_id counts the maximum number of event IDs that can
666 * register if all probes register.
668 chan
->used_event_id
++;
670 event
->instrumentation
= event_param
->instrumentation
;
671 /* Populate ltt_event structure before tracepoint registration. */
673 switch (event_param
->instrumentation
) {
674 case LTTNG_UST_TRACEPOINT
:
677 ret
= __tracepoint_probe_register(event_param
->name
,
678 event
->desc
->probe_callback
,
679 event
, event
->desc
->signature
);
682 event
->id
= chan
->free_event_id
++;
685 * If the probe is not present, event->desc stays NULL,
686 * waiting for the probe to register, and the event->id
689 ret
= add_pending_probe(event
, event_param
->name
,
690 event_param
->loglevel_type
,
691 event_param
->loglevel
);
693 goto add_pending_error
;
700 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
702 goto statedump_error
;
704 cds_list_add(&event
->list
, &chan
->session
->events
);
710 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
711 event
->desc
->probe_callback
,
713 ltt_event_put(event
->desc
);
726 * Only used internally at session destruction.
728 int _ltt_event_unregister(struct ltt_event
*event
)
732 switch (event
->instrumentation
) {
733 case LTTNG_UST_TRACEPOINT
:
735 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
736 event
->desc
->probe_callback
,
741 remove_pending_probe(event
->pending_probe
);
752 * Only used internally at session destruction.
755 void _ltt_event_destroy(struct ltt_event
*event
)
757 switch (event
->instrumentation
) {
758 case LTTNG_UST_TRACEPOINT
:
760 ltt_event_put(event
->desc
);
766 cds_list_del(&event
->list
);
767 lttng_destroy_context(event
->ctx
);
768 free(event
->filter_bytecode
);
769 free(event
->filter_data
);
774 * We have exclusive access to our metadata buffer (protected by the
775 * ust_lock), so we can do racy operations such as looking for
776 * remaining space left in packet and write, since mutual exclusion
777 * protects us from concurrent writes.
779 int lttng_metadata_printf(struct ltt_session
*session
,
780 const char *fmt
, ...)
782 struct lttng_ust_lib_ring_buffer_ctx ctx
;
783 struct ltt_channel
*chan
= session
->metadata
;
785 int ret
= 0, waitret
;
786 size_t len
, reserve_len
, pos
;
789 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
792 ret
= vasprintf(&str
, fmt
, ap
);
800 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
801 reserve_len
= min_t(size_t,
802 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
804 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
805 sizeof(char), -1, chan
->handle
);
807 * We don't care about metadata buffer's records lost
808 * count, because we always retry here. Report error if
809 * we need to bail out after timeout or being
812 waitret
= wait_cond_interruptible_timeout(
814 ret
= chan
->ops
->event_reserve(&ctx
, 0);
815 ret
!= -ENOBUFS
|| !ret
;
817 LTTNG_METADATA_TIMEOUT_MSEC
);
818 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
819 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
820 waitret
== -EINTR
? "interrupted" :
821 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
822 if (waitret
== -EINTR
)
826 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
827 chan
->ops
->event_commit(&ctx
);
835 int _ltt_field_statedump(struct ltt_session
*session
,
836 const struct lttng_event_field
*field
)
843 switch (field
->type
.atype
) {
845 ret
= lttng_metadata_printf(session
,
846 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
847 field
->type
.u
.basic
.integer
.size
,
848 field
->type
.u
.basic
.integer
.alignment
,
849 field
->type
.u
.basic
.integer
.signedness
,
850 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
852 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
855 field
->type
.u
.basic
.integer
.base
,
856 #if (BYTE_ORDER == BIG_ENDIAN)
857 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
859 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
864 ret
= lttng_metadata_printf(session
,
865 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
866 field
->type
.u
.basic
._float
.exp_dig
,
867 field
->type
.u
.basic
._float
.mant_dig
,
868 field
->type
.u
.basic
._float
.alignment
,
869 #if (BYTE_ORDER == BIG_ENDIAN)
870 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
872 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
877 ret
= lttng_metadata_printf(session
,
879 field
->type
.u
.basic
.enumeration
.name
,
884 const struct lttng_basic_type
*elem_type
;
886 elem_type
= &field
->type
.u
.array
.elem_type
;
887 ret
= lttng_metadata_printf(session
,
888 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
889 elem_type
->u
.basic
.integer
.size
,
890 elem_type
->u
.basic
.integer
.alignment
,
891 elem_type
->u
.basic
.integer
.signedness
,
892 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
894 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
897 elem_type
->u
.basic
.integer
.base
,
898 #if (BYTE_ORDER == BIG_ENDIAN)
899 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
901 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
903 field
->name
, field
->type
.u
.array
.length
);
908 const struct lttng_basic_type
*elem_type
;
909 const struct lttng_basic_type
*length_type
;
911 elem_type
= &field
->type
.u
.sequence
.elem_type
;
912 length_type
= &field
->type
.u
.sequence
.length_type
;
913 ret
= lttng_metadata_printf(session
,
914 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
915 length_type
->u
.basic
.integer
.size
,
916 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
917 length_type
->u
.basic
.integer
.signedness
,
918 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
920 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
923 length_type
->u
.basic
.integer
.base
,
924 #if (BYTE_ORDER == BIG_ENDIAN)
925 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
927 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
933 ret
= lttng_metadata_printf(session
,
934 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
935 elem_type
->u
.basic
.integer
.size
,
936 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
937 elem_type
->u
.basic
.integer
.signedness
,
938 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
940 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
943 elem_type
->u
.basic
.integer
.base
,
944 #if (BYTE_ORDER == BIG_ENDIAN)
945 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
947 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
955 /* Default encoding is UTF8 */
956 ret
= lttng_metadata_printf(session
,
958 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
959 " { encoding = ASCII; }" : "",
970 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
971 struct lttng_ctx
*ctx
)
978 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
979 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
981 ret
= _ltt_field_statedump(session
, &field
->event_field
);
989 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
990 struct ltt_event
*event
)
992 const struct lttng_event_desc
*desc
= event
->desc
;
996 for (i
= 0; i
< desc
->nr_fields
; i
++) {
997 const struct lttng_event_field
*field
= &desc
->fields
[i
];
999 ret
= _ltt_field_statedump(session
, field
);
1007 int _ltt_callsite_metadata_statedump(struct ltt_session
*session
,
1008 struct lttng_callsite
*callsite
)
1012 if (!CMM_ACCESS_ONCE(session
->active
))
1015 ret
= lttng_metadata_printf(session
,
1023 callsite
->tp_cs
->name
,
1024 callsite
->tp_cs
->func
,
1025 callsite
->tp_cs
->ip
,
1026 callsite
->tp_cs
->file
,
1027 callsite
->tp_cs
->lineno
);
1035 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
1036 struct ltt_channel
*chan
,
1037 struct ltt_event
*event
)
1040 int loglevel
= TRACE_DEFAULT
;
1042 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
1044 if (chan
== session
->metadata
)
1047 * Don't print events for which probe load is pending.
1052 ret
= lttng_metadata_printf(session
,
1056 " stream_id = %u;\n",
1063 if (event
->desc
->loglevel
)
1064 loglevel
= *(*event
->desc
->loglevel
);
1066 ret
= lttng_metadata_printf(session
,
1067 " loglevel = %d;\n",
1072 if (event
->desc
->u
.ext
.model_emf_uri
) {
1073 ret
= lttng_metadata_printf(session
,
1074 " model.emf.uri = \"%s\";\n",
1075 *(event
->desc
->u
.ext
.model_emf_uri
));
1081 ret
= lttng_metadata_printf(session
,
1082 " context := struct {\n");
1086 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
1090 ret
= lttng_metadata_printf(session
,
1096 ret
= lttng_metadata_printf(session
,
1097 " fields := struct {\n"
1102 ret
= _ltt_fields_metadata_statedump(session
, event
);
1107 * LTTng space reservation can only reserve multiples of the
1110 ret
= lttng_metadata_printf(session
,
1116 event
->metadata_dumped
= 1;
1123 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
1124 struct ltt_channel
*chan
)
1128 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
1130 if (chan
== session
->metadata
)
1133 WARN_ON_ONCE(!chan
->header_type
);
1134 ret
= lttng_metadata_printf(session
,
1137 " event.header := %s;\n"
1138 " packet.context := struct packet_context;\n",
1140 chan
->header_type
== 1 ? "struct event_header_compact" :
1141 "struct event_header_large");
1146 ret
= lttng_metadata_printf(session
,
1147 " event.context := struct {\n");
1151 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
1155 ret
= lttng_metadata_printf(session
,
1161 ret
= lttng_metadata_printf(session
,
1164 chan
->metadata_dumped
= 1;
1170 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
1172 return lttng_metadata_printf(session
,
1173 "struct packet_context {\n"
1174 " uint64_clock_monotonic_t timestamp_begin;\n"
1175 " uint64_clock_monotonic_t timestamp_end;\n"
1176 " uint64_t content_size;\n"
1177 " uint64_t packet_size;\n"
1178 " unsigned long events_discarded;\n"
1179 " uint32_t cpu_id;\n"
1186 * id: range: 0 - 30.
1187 * id 31 is reserved to indicate an extended header.
1190 * id: range: 0 - 65534.
1191 * id 65535 is reserved to indicate an extended header.
1194 int _ltt_event_header_declare(struct ltt_session
*session
)
1196 return lttng_metadata_printf(session
,
1197 "struct event_header_compact {\n"
1198 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1201 " uint27_clock_monotonic_t timestamp;\n"
1205 " uint64_clock_monotonic_t timestamp;\n"
1210 "struct event_header_large {\n"
1211 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1214 " uint32_clock_monotonic_t timestamp;\n"
1218 " uint64_clock_monotonic_t timestamp;\n"
1222 lttng_alignof(uint32_t) * CHAR_BIT
,
1223 lttng_alignof(uint16_t) * CHAR_BIT
1228 * Approximation of NTP time of day to clock monotonic correlation,
1229 * taken at start of trace.
1230 * Yes, this is only an approximation. Yes, we can (and will) do better
1231 * in future versions.
1234 uint64_t measure_clock_offset(void)
1236 uint64_t offset
, monotonic
[2], realtime
;
1237 struct timespec rts
= { 0, 0 };
1240 monotonic
[0] = trace_clock_read64();
1241 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1244 monotonic
[1] = trace_clock_read64();
1245 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1246 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
1247 realtime
+= rts
.tv_nsec
;
1248 offset
= realtime
- offset
;
1253 * Output metadata into this session's metadata buffers.
1256 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1258 unsigned char *uuid_c
= session
->uuid
;
1259 char uuid_s
[LTTNG_UST_UUID_STR_LEN
],
1260 clock_uuid_s
[LTTNG_UST_UUID_STR_LEN
];
1261 struct ltt_channel
*chan
;
1262 struct ltt_event
*event
;
1264 char procname
[LTTNG_UST_PROCNAME_LEN
] = "";
1265 char hostname
[HOST_NAME_MAX
];
1267 if (!CMM_ACCESS_ONCE(session
->active
))
1269 if (session
->metadata_dumped
)
1271 if (!session
->metadata
) {
1272 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1276 snprintf(uuid_s
, sizeof(uuid_s
),
1277 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1278 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1279 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1280 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1281 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1283 ret
= lttng_metadata_printf(session
,
1284 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1285 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1286 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1287 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1288 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1289 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1290 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1296 " byte_order = %s;\n"
1297 " packet.header := struct {\n"
1298 " uint32_t magic;\n"
1299 " uint8_t uuid[16];\n"
1300 " uint32_t stream_id;\n"
1303 lttng_alignof(uint8_t) * CHAR_BIT
,
1304 lttng_alignof(uint16_t) * CHAR_BIT
,
1305 lttng_alignof(uint32_t) * CHAR_BIT
,
1306 lttng_alignof(uint64_t) * CHAR_BIT
,
1307 sizeof(unsigned long) * CHAR_BIT
,
1308 lttng_alignof(unsigned long) * CHAR_BIT
,
1312 #if (BYTE_ORDER == BIG_ENDIAN)
1321 /* ignore error, just use empty string if error. */
1323 ret
= gethostname(hostname
, sizeof(hostname
));
1324 if (ret
&& errno
== ENAMETOOLONG
)
1325 hostname
[HOST_NAME_MAX
- 1] = '\0';
1326 lttng_ust_getprocname(procname
);
1327 procname
[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
1328 ret
= lttng_metadata_printf(session
,
1330 " hostname = \"%s\";\n"
1332 " procname = \"%s\";\n"
1333 " domain = \"ust\";\n"
1334 " tracer_name = \"lttng-ust\";\n"
1335 " tracer_major = %u;\n"
1336 " tracer_minor = %u;\n"
1337 " tracer_patchlevel = %u;\n"
1342 LTTNG_UST_MAJOR_VERSION
,
1343 LTTNG_UST_MINOR_VERSION
,
1344 LTTNG_UST_PATCHLEVEL_VERSION
1349 ret
= lttng_metadata_printf(session
,
1357 if (!trace_clock_uuid(clock_uuid_s
)) {
1358 ret
= lttng_metadata_printf(session
,
1359 " uuid = \"%s\";\n",
1366 ret
= lttng_metadata_printf(session
,
1367 " description = \"Monotonic Clock\";\n"
1368 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1369 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1370 " offset = %" PRIu64
";\n"
1373 measure_clock_offset()
1378 ret
= lttng_metadata_printf(session
,
1379 "typealias integer {\n"
1380 " size = 27; align = 1; signed = false;\n"
1381 " map = clock.monotonic.value;\n"
1382 "} := uint27_clock_monotonic_t;\n"
1384 "typealias integer {\n"
1385 " size = 32; align = %u; signed = false;\n"
1386 " map = clock.monotonic.value;\n"
1387 "} := uint32_clock_monotonic_t;\n"
1389 "typealias integer {\n"
1390 " size = 64; align = %u; signed = false;\n"
1391 " map = clock.monotonic.value;\n"
1392 "} := uint64_clock_monotonic_t;\n\n",
1393 lttng_alignof(uint32_t) * CHAR_BIT
,
1394 lttng_alignof(uint64_t) * CHAR_BIT
1399 ret
= _ltt_stream_packet_context_declare(session
);
1403 ret
= _ltt_event_header_declare(session
);
1406 ret
= _callsite_session_metadata_statedump(session
);
1411 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1412 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1417 cds_list_for_each_entry(event
, &session
->events
, list
) {
1418 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1422 session
->metadata_dumped
= 1;
1427 void lttng_ust_events_exit(void)
1429 struct ltt_session
*session
, *tmpsession
;
1431 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1432 ltt_session_destroy(session
);
1438 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1439 enum lttng_ust_loglevel_type loglevel_type
,
1442 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1450 int wildcard_is_within(struct wildcard_entry
*e
,
1451 enum lttng_ust_loglevel_type loglevel_type
,
1454 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1455 || e
->loglevel
== -1)
1457 switch (e
->loglevel_type
) {
1458 case LTTNG_UST_LOGLEVEL_RANGE
:
1459 switch (loglevel_type
) {
1460 case LTTNG_UST_LOGLEVEL_RANGE
:
1461 if (e
->loglevel
>= loglevel
)
1465 case LTTNG_UST_LOGLEVEL_SINGLE
:
1466 if (e
->loglevel
<= 0 && loglevel
== 0)
1471 case LTTNG_UST_LOGLEVEL_SINGLE
:
1472 switch (loglevel_type
) {
1473 case LTTNG_UST_LOGLEVEL_RANGE
:
1478 case LTTNG_UST_LOGLEVEL_SINGLE
:
1479 if (e
->loglevel
== loglevel
)
1489 * Add the wildcard to the wildcard list. Must be called with
1493 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1494 struct lttng_ust_event
*event_param
)
1496 struct wildcard_entry
*e
;
1497 struct session_wildcard
*sw
;
1498 size_t name_len
= strlen(event_param
->name
) + 1;
1502 * Try to find global wildcard entry. Given that this is shared
1503 * across all sessions, we need to check for exact loglevel
1504 * match, not just whether contained within the existing ones.
1506 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1507 if (!strncmp(event_param
->name
, e
->name
,
1508 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1509 if (wildcard_same_loglevel(e
,
1510 event_param
->loglevel_type
,
1511 event_param
->loglevel
)) {
1520 * Create global wildcard entry if not found. Using
1521 * zmalloc here to allocate a variable length element.
1522 * Could cause some memory fragmentation if overused.
1524 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1526 return ERR_PTR(-ENOMEM
);
1527 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1528 e
->loglevel_type
= event_param
->loglevel_type
;
1529 e
->loglevel
= event_param
->loglevel
;
1530 cds_list_add(&e
->list
, &wildcard_list
);
1531 CDS_INIT_LIST_HEAD(&e
->session_list
);
1534 /* session wildcard */
1535 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1536 if (chan
== sw
->chan
) {
1537 DBG("wildcard %s busy for this channel",
1539 return ERR_PTR(-EEXIST
); /* Already there */
1542 sw
= zmalloc(sizeof(struct session_wildcard
));
1544 return ERR_PTR(-ENOMEM
);
1547 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1548 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1549 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1550 sw
->event_param
.loglevel
= event_param
->loglevel
;
1551 CDS_INIT_LIST_HEAD(&sw
->events
);
1552 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1553 cds_list_add(&sw
->session_list
, &e
->session_list
);
1555 ltt_probes_create_wildcard_events(e
, sw
);
1560 * Remove the wildcard from the wildcard list. Must be called with
1561 * ust_lock held. Only called at session teardown.
1564 void _remove_wildcard(struct session_wildcard
*wildcard
)
1566 struct ltt_event
*ev
, *tmp
;
1569 * Just remove the events owned (for enable/disable) by this
1570 * wildcard from the list. The session teardown will take care
1571 * of freeing the event memory.
1573 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1575 cds_list_del(&ev
->wildcard_list
);
1577 cds_list_del(&wildcard
->session_list
);
1578 cds_list_del(&wildcard
->list
);
1579 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1580 cds_list_del(&wildcard
->entry
->list
);
1581 free(wildcard
->entry
);
1583 free(wildcard
->filter_bytecode
);
1587 int ltt_wildcard_create(struct ltt_channel
*chan
,
1588 struct lttng_ust_event
*event_param
,
1589 struct session_wildcard
**_sw
)
1591 struct session_wildcard
*sw
;
1593 sw
= add_wildcard(chan
, event_param
);
1594 if (!sw
|| IS_ERR(sw
)) {
1602 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1604 _remove_wildcard(sw
);
1607 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1609 struct ltt_event
*ev
;
1612 if (wildcard
->enabled
)
1614 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1615 ret
= ltt_event_enable(ev
);
1617 DBG("Error: enable error.\n");
1621 wildcard
->enabled
= 1;
1625 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1627 struct ltt_event
*ev
;
1630 if (!wildcard
->enabled
)
1632 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1633 ret
= ltt_event_disable(ev
);
1635 DBG("Error: disable error.\n");
1639 wildcard
->enabled
= 0;
1644 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1645 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1646 * taken in constructors, which are called with dynamic linker mutex
1649 void lttng_fixup_event_tls(void)
1651 unsigned char uuid
[LTTNG_UST_UUID_STR_LEN
];
1653 (void) lttng_ust_uuid_generate(uuid
);