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
,
1022 callsite
->tp_cs
->tp
->name
,
1023 callsite
->tp_cs
->func
,
1024 callsite
->tp_cs
->file
,
1025 callsite
->tp_cs
->lineno
);
1033 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
1034 struct ltt_channel
*chan
,
1035 struct ltt_event
*event
)
1038 int loglevel
= TRACE_DEFAULT
;
1040 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
1042 if (chan
== session
->metadata
)
1045 * Don't print events for which probe load is pending.
1050 ret
= lttng_metadata_printf(session
,
1054 " stream_id = %u;\n",
1061 if (event
->desc
->loglevel
)
1062 loglevel
= *(*event
->desc
->loglevel
);
1064 ret
= lttng_metadata_printf(session
,
1065 " loglevel = %d;\n",
1070 if (event
->desc
->u
.ext
.model_emf_uri
) {
1071 ret
= lttng_metadata_printf(session
,
1072 " model.emf.uri = \"%s\";\n",
1073 *(event
->desc
->u
.ext
.model_emf_uri
));
1079 ret
= lttng_metadata_printf(session
,
1080 " context := struct {\n");
1084 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
1088 ret
= lttng_metadata_printf(session
,
1094 ret
= lttng_metadata_printf(session
,
1095 " fields := struct {\n"
1100 ret
= _ltt_fields_metadata_statedump(session
, event
);
1105 * LTTng space reservation can only reserve multiples of the
1108 ret
= lttng_metadata_printf(session
,
1114 event
->metadata_dumped
= 1;
1121 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
1122 struct ltt_channel
*chan
)
1126 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
1128 if (chan
== session
->metadata
)
1131 WARN_ON_ONCE(!chan
->header_type
);
1132 ret
= lttng_metadata_printf(session
,
1135 " event.header := %s;\n"
1136 " packet.context := struct packet_context;\n",
1138 chan
->header_type
== 1 ? "struct event_header_compact" :
1139 "struct event_header_large");
1144 ret
= lttng_metadata_printf(session
,
1145 " event.context := struct {\n");
1149 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
1153 ret
= lttng_metadata_printf(session
,
1159 ret
= lttng_metadata_printf(session
,
1162 chan
->metadata_dumped
= 1;
1168 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
1170 return lttng_metadata_printf(session
,
1171 "struct packet_context {\n"
1172 " uint64_clock_monotonic_t timestamp_begin;\n"
1173 " uint64_clock_monotonic_t timestamp_end;\n"
1174 " uint64_t content_size;\n"
1175 " uint64_t packet_size;\n"
1176 " unsigned long events_discarded;\n"
1177 " uint32_t cpu_id;\n"
1184 * id: range: 0 - 30.
1185 * id 31 is reserved to indicate an extended header.
1188 * id: range: 0 - 65534.
1189 * id 65535 is reserved to indicate an extended header.
1192 int _ltt_event_header_declare(struct ltt_session
*session
)
1194 return lttng_metadata_printf(session
,
1195 "struct event_header_compact {\n"
1196 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1199 " uint27_clock_monotonic_t timestamp;\n"
1203 " uint64_clock_monotonic_t timestamp;\n"
1208 "struct event_header_large {\n"
1209 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1212 " uint32_clock_monotonic_t timestamp;\n"
1216 " uint64_clock_monotonic_t timestamp;\n"
1220 lttng_alignof(uint32_t) * CHAR_BIT
,
1221 lttng_alignof(uint16_t) * CHAR_BIT
1226 * Approximation of NTP time of day to clock monotonic correlation,
1227 * taken at start of trace.
1228 * Yes, this is only an approximation. Yes, we can (and will) do better
1229 * in future versions.
1232 uint64_t measure_clock_offset(void)
1234 uint64_t offset
, monotonic
[2], realtime
;
1235 struct timespec rts
= { 0, 0 };
1238 monotonic
[0] = trace_clock_read64();
1239 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1242 monotonic
[1] = trace_clock_read64();
1243 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1244 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
1245 realtime
+= rts
.tv_nsec
;
1246 offset
= realtime
- offset
;
1251 * Output metadata into this session's metadata buffers.
1254 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1256 unsigned char *uuid_c
= session
->uuid
;
1257 char uuid_s
[LTTNG_UST_UUID_STR_LEN
],
1258 clock_uuid_s
[LTTNG_UST_UUID_STR_LEN
];
1259 struct ltt_channel
*chan
;
1260 struct ltt_event
*event
;
1262 char procname
[LTTNG_UST_PROCNAME_LEN
] = "";
1263 char hostname
[HOST_NAME_MAX
];
1265 if (!CMM_ACCESS_ONCE(session
->active
))
1267 if (session
->metadata_dumped
)
1269 if (!session
->metadata
) {
1270 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1274 snprintf(uuid_s
, sizeof(uuid_s
),
1275 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1276 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1277 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1278 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1279 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1281 ret
= lttng_metadata_printf(session
,
1282 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1283 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1284 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1285 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1286 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1287 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1288 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1294 " byte_order = %s;\n"
1295 " packet.header := struct {\n"
1296 " uint32_t magic;\n"
1297 " uint8_t uuid[16];\n"
1298 " uint32_t stream_id;\n"
1301 lttng_alignof(uint8_t) * CHAR_BIT
,
1302 lttng_alignof(uint16_t) * CHAR_BIT
,
1303 lttng_alignof(uint32_t) * CHAR_BIT
,
1304 lttng_alignof(uint64_t) * CHAR_BIT
,
1305 sizeof(unsigned long) * CHAR_BIT
,
1306 lttng_alignof(unsigned long) * CHAR_BIT
,
1310 #if (BYTE_ORDER == BIG_ENDIAN)
1319 /* ignore error, just use empty string if error. */
1321 ret
= gethostname(hostname
, sizeof(hostname
));
1322 if (ret
&& errno
== ENAMETOOLONG
)
1323 hostname
[HOST_NAME_MAX
- 1] = '\0';
1324 lttng_ust_getprocname(procname
);
1325 procname
[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
1326 ret
= lttng_metadata_printf(session
,
1328 " hostname = \"%s\";\n"
1330 " procname = \"%s\";\n"
1331 " domain = \"ust\";\n"
1332 " tracer_name = \"lttng-ust\";\n"
1333 " tracer_major = %u;\n"
1334 " tracer_minor = %u;\n"
1335 " tracer_patchlevel = %u;\n"
1340 LTTNG_UST_MAJOR_VERSION
,
1341 LTTNG_UST_MINOR_VERSION
,
1342 LTTNG_UST_PATCHLEVEL_VERSION
1347 ret
= lttng_metadata_printf(session
,
1355 if (!trace_clock_uuid(clock_uuid_s
)) {
1356 ret
= lttng_metadata_printf(session
,
1357 " uuid = \"%s\";\n",
1364 ret
= lttng_metadata_printf(session
,
1365 " description = \"Monotonic Clock\";\n"
1366 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1367 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1368 " offset = %" PRIu64
";\n"
1371 measure_clock_offset()
1376 ret
= lttng_metadata_printf(session
,
1377 "typealias integer {\n"
1378 " size = 27; align = 1; signed = false;\n"
1379 " map = clock.monotonic.value;\n"
1380 "} := uint27_clock_monotonic_t;\n"
1382 "typealias integer {\n"
1383 " size = 32; align = %u; signed = false;\n"
1384 " map = clock.monotonic.value;\n"
1385 "} := uint32_clock_monotonic_t;\n"
1387 "typealias integer {\n"
1388 " size = 64; align = %u; signed = false;\n"
1389 " map = clock.monotonic.value;\n"
1390 "} := uint64_clock_monotonic_t;\n\n",
1391 lttng_alignof(uint32_t) * CHAR_BIT
,
1392 lttng_alignof(uint64_t) * CHAR_BIT
1397 ret
= _ltt_stream_packet_context_declare(session
);
1401 ret
= _ltt_event_header_declare(session
);
1404 ret
= _callsite_session_metadata_statedump(session
);
1409 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1410 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1415 cds_list_for_each_entry(event
, &session
->events
, list
) {
1416 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1420 session
->metadata_dumped
= 1;
1425 void lttng_ust_events_exit(void)
1427 struct ltt_session
*session
, *tmpsession
;
1429 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1430 ltt_session_destroy(session
);
1436 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1437 enum lttng_ust_loglevel_type loglevel_type
,
1440 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1448 int wildcard_is_within(struct wildcard_entry
*e
,
1449 enum lttng_ust_loglevel_type loglevel_type
,
1452 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1453 || e
->loglevel
== -1)
1455 switch (e
->loglevel_type
) {
1456 case LTTNG_UST_LOGLEVEL_RANGE
:
1457 switch (loglevel_type
) {
1458 case LTTNG_UST_LOGLEVEL_RANGE
:
1459 if (e
->loglevel
>= loglevel
)
1463 case LTTNG_UST_LOGLEVEL_SINGLE
:
1464 if (e
->loglevel
<= 0 && loglevel
== 0)
1469 case LTTNG_UST_LOGLEVEL_SINGLE
:
1470 switch (loglevel_type
) {
1471 case LTTNG_UST_LOGLEVEL_RANGE
:
1476 case LTTNG_UST_LOGLEVEL_SINGLE
:
1477 if (e
->loglevel
== loglevel
)
1487 * Add the wildcard to the wildcard list. Must be called with
1491 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1492 struct lttng_ust_event
*event_param
)
1494 struct wildcard_entry
*e
;
1495 struct session_wildcard
*sw
;
1496 size_t name_len
= strlen(event_param
->name
) + 1;
1500 * Try to find global wildcard entry. Given that this is shared
1501 * across all sessions, we need to check for exact loglevel
1502 * match, not just whether contained within the existing ones.
1504 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1505 if (!strncmp(event_param
->name
, e
->name
,
1506 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1507 if (wildcard_same_loglevel(e
,
1508 event_param
->loglevel_type
,
1509 event_param
->loglevel
)) {
1518 * Create global wildcard entry if not found. Using
1519 * zmalloc here to allocate a variable length element.
1520 * Could cause some memory fragmentation if overused.
1522 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1524 return ERR_PTR(-ENOMEM
);
1525 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1526 e
->loglevel_type
= event_param
->loglevel_type
;
1527 e
->loglevel
= event_param
->loglevel
;
1528 cds_list_add(&e
->list
, &wildcard_list
);
1529 CDS_INIT_LIST_HEAD(&e
->session_list
);
1532 /* session wildcard */
1533 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1534 if (chan
== sw
->chan
) {
1535 DBG("wildcard %s busy for this channel",
1537 return ERR_PTR(-EEXIST
); /* Already there */
1540 sw
= zmalloc(sizeof(struct session_wildcard
));
1542 return ERR_PTR(-ENOMEM
);
1545 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1546 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1547 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1548 sw
->event_param
.loglevel
= event_param
->loglevel
;
1549 CDS_INIT_LIST_HEAD(&sw
->events
);
1550 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1551 cds_list_add(&sw
->session_list
, &e
->session_list
);
1553 ltt_probes_create_wildcard_events(e
, sw
);
1558 * Remove the wildcard from the wildcard list. Must be called with
1559 * ust_lock held. Only called at session teardown.
1562 void _remove_wildcard(struct session_wildcard
*wildcard
)
1564 struct ltt_event
*ev
, *tmp
;
1567 * Just remove the events owned (for enable/disable) by this
1568 * wildcard from the list. The session teardown will take care
1569 * of freeing the event memory.
1571 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1573 cds_list_del(&ev
->wildcard_list
);
1575 cds_list_del(&wildcard
->session_list
);
1576 cds_list_del(&wildcard
->list
);
1577 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1578 cds_list_del(&wildcard
->entry
->list
);
1579 free(wildcard
->entry
);
1581 free(wildcard
->filter_bytecode
);
1585 int ltt_wildcard_create(struct ltt_channel
*chan
,
1586 struct lttng_ust_event
*event_param
,
1587 struct session_wildcard
**_sw
)
1589 struct session_wildcard
*sw
;
1591 sw
= add_wildcard(chan
, event_param
);
1592 if (!sw
|| IS_ERR(sw
)) {
1600 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1602 _remove_wildcard(sw
);
1605 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1607 struct ltt_event
*ev
;
1610 if (wildcard
->enabled
)
1612 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1613 ret
= ltt_event_enable(ev
);
1615 DBG("Error: enable error.\n");
1619 wildcard
->enabled
= 1;
1623 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1625 struct ltt_event
*ev
;
1628 if (!wildcard
->enabled
)
1630 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1631 ret
= ltt_event_disable(ev
);
1633 DBG("Error: disable error.\n");
1637 wildcard
->enabled
= 0;
1642 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1643 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1644 * taken in constructors, which are called with dynamic linker mutex
1647 void lttng_fixup_event_tls(void)
1649 unsigned char uuid
[LTTNG_UST_UUID_STR_LEN
];
1651 (void) lttng_ust_uuid_generate(uuid
);