4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
8 * Dual LGPL v2.1/GPL v2 license.
14 #include <urcu/list.h>
15 #include <urcu/hlist.h>
17 #include <uuid/uuid.h>
28 #include <urcu/compiler.h>
29 #include <urcu/uatomic.h>
30 #include <urcu/arch.h>
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
35 #include <usterr-signal-safe.h>
39 #include "tracepoint-internal.h"
40 #include "ltt-tracer.h"
41 #include "ltt-tracer-core.h"
43 #include "../libringbuffer/shm.h"
47 * The sessions mutex is the centralized mutex across UST tracing
48 * control and probe registration. All operations within this file are
49 * called by the communication thread, under ust_lock protection.
51 static pthread_mutex_t sessions_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 pthread_mutex_lock(&sessions_mutex
);
60 pthread_mutex_unlock(&sessions_mutex
);
63 static CDS_LIST_HEAD(sessions
);
66 * Wildcard list, containing the active wildcards.
67 * Protected by ust lock.
69 static CDS_LIST_HEAD(wildcard_list
);
72 * Pending probes hash table, containing the registered ltt events for
73 * which tracepoint probes are still missing. Protected by the sessions
76 #define PENDING_PROBE_HASH_BITS 6
77 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
78 static struct cds_hlist_head pending_probe_table
[PENDING_PROBE_HASH_SIZE
];
80 struct ust_pending_probe
{
81 struct ltt_event
*event
;
82 struct cds_hlist_node node
;
83 enum lttng_ust_loglevel_type loglevel_type
;
88 static void _ltt_event_destroy(struct ltt_event
*event
);
89 static void _ltt_wildcard_destroy(struct session_wildcard
*sw
);
90 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
91 static int _ltt_event_unregister(struct ltt_event
*event
);
93 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
94 struct ltt_channel
*chan
,
95 struct ltt_event
*event
);
97 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
99 int ltt_loglevel_match(const struct lttng_event_desc
*desc
,
100 enum lttng_ust_loglevel_type req_type
,
105 if (req_type
== LTTNG_UST_LOGLEVEL_ALL
)
108 ev_loglevel
= TRACE_DEFAULT
;
110 ev_loglevel
= *(*desc
->loglevel
);
112 case LTTNG_UST_LOGLEVEL_RANGE
:
113 if (ev_loglevel
<= req_loglevel
|| req_loglevel
== -1)
117 case LTTNG_UST_LOGLEVEL_SINGLE
:
118 if (ev_loglevel
== req_loglevel
|| req_loglevel
== -1)
122 case LTTNG_UST_LOGLEVEL_ALL
:
129 * Return wildcard for a given event name if the event name match the
130 * one of the wildcards.
131 * Must be called with ust lock held.
132 * Returns NULL if not present.
135 struct wildcard_entry
*match_wildcard(const struct lttng_event_desc
*desc
)
137 struct wildcard_entry
*e
;
139 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
140 /* If only contain '*' */
141 if (strlen(e
->name
) == 1)
143 /* Compare excluding final '*' */
144 if (!strncmp(desc
->name
, e
->name
, strlen(e
->name
) - 1))
146 continue; /* goto next, no match */
148 if (ltt_loglevel_match(desc
,
153 /* no match, loop to next */
159 * called at event creation if probe is missing.
160 * called with session mutex held.
163 int add_pending_probe(struct ltt_event
*event
, const char *name
,
164 enum lttng_ust_loglevel_type loglevel_type
,
167 struct cds_hlist_head
*head
;
168 struct ust_pending_probe
*e
;
169 size_t name_len
= strlen(name
);
172 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
173 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
174 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
176 hash
= jhash(name
, name_len
, 0);
177 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
178 e
= zmalloc(sizeof(struct ust_pending_probe
) + name_len
);
181 memcpy(&e
->name
[0], name
, name_len
+ 1);
182 e
->name
[name_len
] = '\0';
183 e
->loglevel_type
= loglevel_type
;
184 e
->loglevel
= loglevel
;
185 cds_hlist_add_head(&e
->node
, head
);
187 event
->pending_probe
= e
;
192 * remove a pending probe. called when at event teardown and when an
193 * event is fixed (probe is loaded).
194 * called with session mutex held.
197 void remove_pending_probe(struct ust_pending_probe
*e
)
201 cds_hlist_del(&e
->node
);
206 * Called at library load: connect the probe on the events pending on
208 * called with session mutex held.
210 int pending_probe_fix_events(const struct lttng_event_desc
*desc
)
212 struct cds_hlist_head
*head
;
213 struct cds_hlist_node
*node
, *p
;
214 struct ust_pending_probe
*e
;
215 const char *name
= desc
->name
;
217 struct lttng_ust_event event_param
;
218 size_t name_len
= strlen(name
);
223 struct wildcard_entry
*wildcard
;
225 wildcard
= match_wildcard(desc
);
226 if (strcmp(desc
->name
, "lttng_ust:metadata") && wildcard
) {
227 struct session_wildcard
*sw
;
229 cds_list_for_each_entry(sw
, &wildcard
->session_list
,
231 struct ltt_event
*ev
;
234 memcpy(&event_param
, &sw
->event_param
,
235 sizeof(event_param
));
236 memcpy(event_param
.name
,
238 sizeof(event_param
.name
));
240 ret
= ltt_event_create(sw
->chan
,
244 DBG("Error creating event");
247 cds_list_add(&ev
->wildcard_list
,
253 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
254 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
255 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
257 hash
= jhash(name
, name_len
, 0);
258 head
= &pending_probe_table
[hash
& (PENDING_PROBE_HASH_SIZE
- 1)];
259 cds_hlist_for_each_entry_safe(e
, node
, p
, head
, node
) {
260 struct ltt_event
*event
;
261 struct ltt_channel
*chan
;
264 if (!ltt_loglevel_match(event
->desc
,
269 if (strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
273 assert(!event
->desc
);
275 event
->pending_probe
= NULL
;
276 remove_pending_probe(e
);
277 ret
|= __tracepoint_probe_register(name
,
278 event
->desc
->probe_callback
,
282 event
->id
= chan
->free_event_id
++;
283 ret
|= _ltt_event_metadata_statedump(chan
->session
, chan
,
289 void synchronize_trace(void)
294 struct ltt_session
*ltt_session_create(void)
296 struct ltt_session
*session
;
298 session
= zmalloc(sizeof(struct ltt_session
));
301 CDS_INIT_LIST_HEAD(&session
->chan
);
302 CDS_INIT_LIST_HEAD(&session
->events
);
303 CDS_INIT_LIST_HEAD(&session
->wildcards
);
304 uuid_generate(session
->uuid
);
305 cds_list_add(&session
->list
, &sessions
);
309 void ltt_session_destroy(struct ltt_session
*session
)
311 struct ltt_channel
*chan
, *tmpchan
;
312 struct ltt_event
*event
, *tmpevent
;
313 struct session_wildcard
*wildcard
, *tmpwildcard
;
316 CMM_ACCESS_ONCE(session
->active
) = 0;
317 cds_list_for_each_entry(event
, &session
->events
, list
) {
318 ret
= _ltt_event_unregister(event
);
321 synchronize_trace(); /* Wait for in-flight events to complete */
322 cds_list_for_each_entry_safe(wildcard
, tmpwildcard
, &session
->wildcards
, list
)
323 _ltt_wildcard_destroy(wildcard
);
324 cds_list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
325 _ltt_event_destroy(event
);
326 cds_list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
327 _ltt_channel_destroy(chan
);
328 cds_list_del(&session
->list
);
332 int ltt_session_enable(struct ltt_session
*session
)
335 struct ltt_channel
*chan
;
337 if (session
->active
) {
343 * Snapshot the number of events per channel to know the type of header
346 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
347 if (chan
->header_type
)
348 continue; /* don't change it if session stop/restart */
349 if (chan
->free_event_id
< 31)
350 chan
->header_type
= 1; /* compact */
352 chan
->header_type
= 2; /* large */
355 CMM_ACCESS_ONCE(session
->active
) = 1;
356 CMM_ACCESS_ONCE(session
->been_active
) = 1;
357 ret
= _ltt_session_metadata_statedump(session
);
359 CMM_ACCESS_ONCE(session
->active
) = 0;
364 int ltt_session_disable(struct ltt_session
*session
)
368 if (!session
->active
) {
372 CMM_ACCESS_ONCE(session
->active
) = 0;
377 int ltt_channel_enable(struct ltt_channel
*channel
)
381 if (channel
== channel
->session
->metadata
)
383 old
= uatomic_xchg(&channel
->enabled
, 1);
389 int ltt_channel_disable(struct ltt_channel
*channel
)
393 if (channel
== channel
->session
->metadata
)
395 old
= uatomic_xchg(&channel
->enabled
, 0);
401 int ltt_event_enable(struct ltt_event
*event
)
405 if (event
->chan
== event
->chan
->session
->metadata
)
407 old
= uatomic_xchg(&event
->enabled
, 1);
413 int ltt_event_disable(struct ltt_event
*event
)
417 if (event
->chan
== event
->chan
->session
->metadata
)
419 old
= uatomic_xchg(&event
->enabled
, 0);
425 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
426 const char *transport_name
,
428 size_t subbuf_size
, size_t num_subbuf
,
429 unsigned int switch_timer_interval
,
430 unsigned int read_timer_interval
,
431 int **shm_fd
, int **wait_fd
,
432 uint64_t **memory_map_size
,
433 struct ltt_channel
*chan_priv_init
)
435 struct ltt_channel
*chan
= NULL
;
436 struct ltt_transport
*transport
;
438 if (session
->been_active
)
439 goto active
; /* Refuse to add channel to active session */
440 transport
= ltt_transport_find(transport_name
);
442 DBG("LTTng transport %s not found\n",
446 chan_priv_init
->id
= session
->free_chan_id
++;
447 chan_priv_init
->session
= session
;
449 * Note: the channel creation op already writes into the packet
450 * headers. Therefore the "chan" information used as input
451 * should be already accessible.
453 chan
= transport
->ops
.channel_create("[lttng]", buf_addr
,
454 subbuf_size
, num_subbuf
, switch_timer_interval
,
455 read_timer_interval
, shm_fd
, wait_fd
,
456 memory_map_size
, chan_priv_init
);
460 chan
->ops
= &transport
->ops
;
461 cds_list_add(&chan
->list
, &session
->chan
);
471 * Only used internally at session destruction.
474 void _ltt_channel_destroy(struct ltt_channel
*chan
)
476 cds_list_del(&chan
->list
);
477 lttng_destroy_context(chan
->ctx
);
478 chan
->ops
->channel_destroy(chan
);
482 * Supports event creation while tracing session is active.
484 int ltt_event_create(struct ltt_channel
*chan
,
485 struct lttng_ust_event
*event_param
,
487 struct ltt_event
**_event
)
489 const struct lttng_event_desc
*desc
= NULL
; /* silence gcc */
490 struct ltt_event
*event
;
493 if (chan
->used_event_id
== -1UL) {
498 * This is O(n^2) (for each event, the loop is called at event
499 * creation). Might require a hash if we have lots of events.
501 cds_list_for_each_entry(event
, &chan
->session
->events
, list
) {
502 if (event
->desc
&& !strncmp(event
->desc
->name
,
504 LTTNG_UST_SYM_NAME_LEN
- 1)) {
511 * Check if loglevel match. Refuse to connect event if not.
513 if (event_param
->instrumentation
== LTTNG_UST_TRACEPOINT
) {
514 desc
= ltt_event_get(event_param
->name
);
515 if (!ltt_loglevel_match(desc
,
516 event_param
->loglevel_type
,
517 event_param
->loglevel
)) {
519 goto no_loglevel_match
;
522 event
= zmalloc(sizeof(struct ltt_event
));
528 event
->filter
= filter
;
530 * used_event_id counts the maximum number of event IDs that can
531 * register if all probes register.
533 chan
->used_event_id
++;
535 event
->instrumentation
= event_param
->instrumentation
;
536 /* Populate ltt_event structure before tracepoint registration. */
538 switch (event_param
->instrumentation
) {
539 case LTTNG_UST_TRACEPOINT
:
542 ret
= __tracepoint_probe_register(event_param
->name
,
543 event
->desc
->probe_callback
,
547 event
->id
= chan
->free_event_id
++;
550 * If the probe is not present, event->desc stays NULL,
551 * waiting for the probe to register, and the event->id
554 ret
= add_pending_probe(event
, event_param
->name
,
555 event_param
->loglevel_type
,
556 event_param
->loglevel
);
558 goto add_pending_error
;
565 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
567 goto statedump_error
;
569 cds_list_add(&event
->list
, &chan
->session
->events
);
575 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param
->name
,
576 event
->desc
->probe_callback
,
578 ltt_event_put(event
->desc
);
591 * Only used internally at session destruction.
593 int _ltt_event_unregister(struct ltt_event
*event
)
597 switch (event
->instrumentation
) {
598 case LTTNG_UST_TRACEPOINT
:
600 ret
= __tracepoint_probe_unregister(event
->desc
->name
,
601 event
->desc
->probe_callback
,
606 remove_pending_probe(event
->pending_probe
);
617 * Only used internally at session destruction.
620 void _ltt_event_destroy(struct ltt_event
*event
)
622 switch (event
->instrumentation
) {
623 case LTTNG_UST_TRACEPOINT
:
625 ltt_event_put(event
->desc
);
631 cds_list_del(&event
->list
);
632 lttng_destroy_context(event
->ctx
);
637 * We have exclusive access to our metadata buffer (protected by the
638 * ust_lock), so we can do racy operations such as looking for
639 * remaining space left in packet and write, since mutual exclusion
640 * protects us from concurrent writes.
642 int lttng_metadata_printf(struct ltt_session
*session
,
643 const char *fmt
, ...)
645 struct lttng_ust_lib_ring_buffer_ctx ctx
;
646 struct ltt_channel
*chan
= session
->metadata
;
648 int ret
= 0, waitret
;
649 size_t len
, reserve_len
, pos
;
652 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session
->active
));
655 ret
= vasprintf(&str
, fmt
, ap
);
663 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
664 reserve_len
= min_t(size_t,
665 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
667 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
668 sizeof(char), -1, chan
->handle
);
670 * We don't care about metadata buffer's records lost
671 * count, because we always retry here. Report error if
672 * we need to bail out after timeout or being
675 waitret
= wait_cond_interruptible_timeout(
677 ret
= chan
->ops
->event_reserve(&ctx
, 0);
678 ret
!= -ENOBUFS
|| !ret
;
680 LTTNG_METADATA_TIMEOUT_MSEC
);
681 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
682 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
683 waitret
== -EINTR
? "interrupted" :
684 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
685 if (waitret
== -EINTR
)
689 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
690 chan
->ops
->event_commit(&ctx
);
698 int _ltt_field_statedump(struct ltt_session
*session
,
699 const struct lttng_event_field
*field
)
703 switch (field
->type
.atype
) {
705 ret
= lttng_metadata_printf(session
,
706 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
707 field
->type
.u
.basic
.integer
.size
,
708 field
->type
.u
.basic
.integer
.alignment
,
709 field
->type
.u
.basic
.integer
.signedness
,
710 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
712 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
715 field
->type
.u
.basic
.integer
.base
,
716 #if (BYTE_ORDER == BIG_ENDIAN)
717 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
719 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
724 ret
= lttng_metadata_printf(session
,
725 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
726 field
->type
.u
.basic
._float
.exp_dig
,
727 field
->type
.u
.basic
._float
.mant_dig
,
728 field
->type
.u
.basic
._float
.alignment
,
729 #if (BYTE_ORDER == BIG_ENDIAN)
730 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
732 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
737 ret
= lttng_metadata_printf(session
,
739 field
->type
.u
.basic
.enumeration
.name
,
744 const struct lttng_basic_type
*elem_type
;
746 elem_type
= &field
->type
.u
.array
.elem_type
;
747 ret
= lttng_metadata_printf(session
,
748 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
749 elem_type
->u
.basic
.integer
.size
,
750 elem_type
->u
.basic
.integer
.alignment
,
751 elem_type
->u
.basic
.integer
.signedness
,
752 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
754 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
757 elem_type
->u
.basic
.integer
.base
,
758 #if (BYTE_ORDER == BIG_ENDIAN)
759 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
761 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
763 field
->name
, field
->type
.u
.array
.length
);
768 const struct lttng_basic_type
*elem_type
;
769 const struct lttng_basic_type
*length_type
;
771 elem_type
= &field
->type
.u
.sequence
.elem_type
;
772 length_type
= &field
->type
.u
.sequence
.length_type
;
773 ret
= lttng_metadata_printf(session
,
774 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
775 length_type
->u
.basic
.integer
.size
,
776 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
777 length_type
->u
.basic
.integer
.signedness
,
778 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
780 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
783 length_type
->u
.basic
.integer
.base
,
784 #if (BYTE_ORDER == BIG_ENDIAN)
785 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
787 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
793 ret
= lttng_metadata_printf(session
,
794 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
795 elem_type
->u
.basic
.integer
.size
,
796 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
797 elem_type
->u
.basic
.integer
.signedness
,
798 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
800 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
803 elem_type
->u
.basic
.integer
.base
,
804 #if (BYTE_ORDER == BIG_ENDIAN)
805 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
807 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
815 /* Default encoding is UTF8 */
816 ret
= lttng_metadata_printf(session
,
818 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
819 " { encoding = ASCII; }" : "",
830 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
831 struct lttng_ctx
*ctx
)
838 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
839 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
841 ret
= _ltt_field_statedump(session
, &field
->event_field
);
849 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
850 struct ltt_event
*event
)
852 const struct lttng_event_desc
*desc
= event
->desc
;
856 for (i
= 0; i
< desc
->nr_fields
; i
++) {
857 const struct lttng_event_field
*field
= &desc
->fields
[i
];
859 ret
= _ltt_field_statedump(session
, field
);
867 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
868 struct ltt_channel
*chan
,
869 struct ltt_event
*event
)
873 if (event
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
875 if (chan
== session
->metadata
)
878 * Don't print events for which probe load is pending.
883 ret
= lttng_metadata_printf(session
,
887 " stream_id = %u;\n",
894 if (event
->desc
->loglevel
) {
895 ret
= lttng_metadata_printf(session
,
897 *(*event
->desc
->loglevel
));
903 ret
= lttng_metadata_printf(session
,
904 " context := struct {\n");
908 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
912 ret
= lttng_metadata_printf(session
,
918 ret
= lttng_metadata_printf(session
,
919 " fields := struct {\n"
924 ret
= _ltt_fields_metadata_statedump(session
, event
);
929 * LTTng space reservation can only reserve multiples of the
932 ret
= lttng_metadata_printf(session
,
938 event
->metadata_dumped
= 1;
945 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
946 struct ltt_channel
*chan
)
950 if (chan
->metadata_dumped
|| !CMM_ACCESS_ONCE(session
->active
))
952 if (chan
== session
->metadata
)
955 WARN_ON_ONCE(!chan
->header_type
);
956 ret
= lttng_metadata_printf(session
,
959 " event.header := %s;\n"
960 " packet.context := struct packet_context;\n",
962 chan
->header_type
== 1 ? "struct event_header_compact" :
963 "struct event_header_large");
968 ret
= lttng_metadata_printf(session
,
969 " event.context := struct {\n");
973 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
977 ret
= lttng_metadata_printf(session
,
983 ret
= lttng_metadata_printf(session
,
986 chan
->metadata_dumped
= 1;
992 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
994 return lttng_metadata_printf(session
,
995 "struct packet_context {\n"
996 " uint64_clock_monotonic_t timestamp_begin;\n"
997 " uint64_clock_monotonic_t timestamp_end;\n"
998 " uint32_t events_discarded;\n"
999 " uint32_t content_size;\n"
1000 " uint32_t packet_size;\n"
1001 " uint32_t cpu_id;\n"
1008 * id: range: 0 - 30.
1009 * id 31 is reserved to indicate an extended header.
1012 * id: range: 0 - 65534.
1013 * id 65535 is reserved to indicate an extended header.
1016 int _ltt_event_header_declare(struct ltt_session
*session
)
1018 return lttng_metadata_printf(session
,
1019 "struct event_header_compact {\n"
1020 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1023 " uint27_clock_monotonic_t timestamp;\n"
1027 " uint64_clock_monotonic_t timestamp;\n"
1032 "struct event_header_large {\n"
1033 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1036 " uint32_clock_monotonic_t timestamp;\n"
1040 " uint64_clock_monotonic_t timestamp;\n"
1044 lttng_alignof(uint32_t) * CHAR_BIT
,
1045 lttng_alignof(uint16_t) * CHAR_BIT
1050 * Approximation of NTP time of day to clock monotonic correlation,
1051 * taken at start of trace.
1052 * Yes, this is only an approximation. Yes, we can (and will) do better
1053 * in future versions.
1056 uint64_t measure_clock_offset(void)
1058 uint64_t offset
, monotonic
[2], realtime
;
1059 struct timespec rts
= { 0, 0 };
1062 monotonic
[0] = trace_clock_read64();
1063 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
1066 monotonic
[1] = trace_clock_read64();
1067 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1068 realtime
= rts
.tv_sec
* 1000000000ULL;
1069 realtime
+= rts
.tv_nsec
;
1070 offset
= realtime
- offset
;
1075 * Output metadata into this session's metadata buffers.
1078 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
1080 unsigned char *uuid_c
= session
->uuid
;
1081 char uuid_s
[37], clock_uuid_s
[CLOCK_UUID_LEN
];
1082 struct ltt_channel
*chan
;
1083 struct ltt_event
*event
;
1086 if (!CMM_ACCESS_ONCE(session
->active
))
1088 if (session
->metadata_dumped
)
1090 if (!session
->metadata
) {
1091 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1095 snprintf(uuid_s
, sizeof(uuid_s
),
1096 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1097 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1098 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1099 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1100 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1102 ret
= lttng_metadata_printf(session
,
1103 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1104 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1105 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1106 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1107 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1108 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1114 " byte_order = %s;\n"
1115 " packet.header := struct {\n"
1116 " uint32_t magic;\n"
1117 " uint8_t uuid[16];\n"
1118 " uint32_t stream_id;\n"
1121 lttng_alignof(uint8_t) * CHAR_BIT
,
1122 lttng_alignof(uint16_t) * CHAR_BIT
,
1123 lttng_alignof(uint32_t) * CHAR_BIT
,
1124 lttng_alignof(uint64_t) * CHAR_BIT
,
1128 #if (BYTE_ORDER == BIG_ENDIAN)
1137 ret
= lttng_metadata_printf(session
,
1145 if (!trace_clock_uuid(clock_uuid_s
)) {
1146 ret
= lttng_metadata_printf(session
,
1147 " uuid = \"%s\";\n",
1154 ret
= lttng_metadata_printf(session
,
1155 " description = \"Monotonic Clock\";\n"
1156 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1157 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1158 " offset = %" PRIu64
";\n"
1161 measure_clock_offset()
1166 ret
= lttng_metadata_printf(session
,
1167 "typealias integer {\n"
1168 " size = 27; align = 1; signed = false;\n"
1169 " map = clock.monotonic.value;\n"
1170 "} := uint27_clock_monotonic_t;\n"
1172 "typealias integer {\n"
1173 " size = 32; align = %u; signed = false;\n"
1174 " map = clock.monotonic.value;\n"
1175 "} := uint32_clock_monotonic_t;\n"
1177 "typealias integer {\n"
1178 " size = 64; align = %u; signed = false;\n"
1179 " map = clock.monotonic.value;\n"
1180 "} := uint64_clock_monotonic_t;\n\n",
1181 lttng_alignof(uint32_t) * CHAR_BIT
,
1182 lttng_alignof(uint64_t) * CHAR_BIT
1187 ret
= _ltt_stream_packet_context_declare(session
);
1191 ret
= _ltt_event_header_declare(session
);
1196 cds_list_for_each_entry(chan
, &session
->chan
, list
) {
1197 ret
= _ltt_channel_metadata_statedump(session
, chan
);
1202 cds_list_for_each_entry(event
, &session
->events
, list
) {
1203 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
1207 session
->metadata_dumped
= 1;
1212 void lttng_ust_events_exit(void)
1214 struct ltt_session
*session
, *tmpsession
;
1216 cds_list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1217 ltt_session_destroy(session
);
1223 int wildcard_same_loglevel(struct wildcard_entry
*e
,
1224 enum lttng_ust_loglevel_type loglevel_type
,
1227 if (e
->loglevel_type
== loglevel_type
&& e
->loglevel
== loglevel
)
1235 int wildcard_is_within(struct wildcard_entry
*e
,
1236 enum lttng_ust_loglevel_type loglevel_type
,
1239 if (e
->loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
1240 || e
->loglevel
== -1)
1242 switch (e
->loglevel_type
) {
1243 case LTTNG_UST_LOGLEVEL_RANGE
:
1244 switch (loglevel_type
) {
1245 case LTTNG_UST_LOGLEVEL_RANGE
:
1246 if (e
->loglevel
>= loglevel
)
1250 case LTTNG_UST_LOGLEVEL_SINGLE
:
1251 if (e
->loglevel
<= 0 && loglevel
== 0)
1256 case LTTNG_UST_LOGLEVEL_SINGLE
:
1257 switch (loglevel_type
) {
1258 case LTTNG_UST_LOGLEVEL_RANGE
:
1263 case LTTNG_UST_LOGLEVEL_SINGLE
:
1264 if (e
->loglevel
== loglevel
)
1274 * Add the wildcard to the wildcard list. Must be called with
1278 struct session_wildcard
*add_wildcard(struct ltt_channel
*chan
,
1279 struct lttng_ust_event
*event_param
)
1281 struct wildcard_entry
*e
;
1282 struct session_wildcard
*sw
;
1283 size_t name_len
= strlen(event_param
->name
) + 1;
1287 * Try to find global wildcard entry. Given that this is shared
1288 * across all sessions, we need to check for exact loglevel
1289 * match, not just whether contained within the existing ones.
1291 cds_list_for_each_entry(e
, &wildcard_list
, list
) {
1292 if (!strncmp(event_param
->name
, e
->name
,
1293 LTTNG_UST_SYM_NAME_LEN
- 1)) {
1294 if (wildcard_same_loglevel(e
,
1295 event_param
->loglevel_type
,
1296 event_param
->loglevel
)) {
1305 * Create global wildcard entry if not found. Using
1306 * zmalloc here to allocate a variable length element.
1307 * Could cause some memory fragmentation if overused.
1309 e
= zmalloc(sizeof(struct wildcard_entry
) + name_len
);
1311 return ERR_PTR(-ENOMEM
);
1312 memcpy(&e
->name
[0], event_param
->name
, name_len
);
1313 cds_list_add(&e
->list
, &wildcard_list
);
1314 CDS_INIT_LIST_HEAD(&e
->session_list
);
1317 /* session wildcard */
1318 cds_list_for_each_entry(sw
, &e
->session_list
, session_list
) {
1319 if (chan
== sw
->chan
) {
1320 DBG("wildcard %s busy for this channel",
1322 return ERR_PTR(-EEXIST
); /* Already there */
1325 sw
= zmalloc(sizeof(struct session_wildcard
));
1327 return ERR_PTR(-ENOMEM
);
1330 memcpy(&sw
->event_param
, event_param
, sizeof(sw
->event_param
));
1331 sw
->event_param
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1332 sw
->event_param
.loglevel_type
= event_param
->loglevel_type
;
1333 sw
->event_param
.loglevel
= event_param
->loglevel
;
1334 CDS_INIT_LIST_HEAD(&sw
->events
);
1335 cds_list_add(&sw
->list
, &chan
->session
->wildcards
);
1336 cds_list_add(&sw
->session_list
, &e
->session_list
);
1338 ltt_probes_create_wildcard_events(e
, sw
);
1343 * Remove the wildcard from the wildcard list. Must be called with
1344 * ust_lock held. Only called at session teardown.
1347 void _remove_wildcard(struct session_wildcard
*wildcard
)
1349 struct ltt_event
*ev
, *tmp
;
1352 * Just remove the events owned (for enable/disable) by this
1353 * wildcard from the list. The session teardown will take care
1354 * of freeing the event memory.
1356 cds_list_for_each_entry_safe(ev
, tmp
, &wildcard
->events
,
1358 cds_list_del(&ev
->wildcard_list
);
1360 cds_list_del(&wildcard
->session_list
);
1361 cds_list_del(&wildcard
->list
);
1362 if (cds_list_empty(&wildcard
->entry
->session_list
)) {
1363 cds_list_del(&wildcard
->entry
->list
);
1364 free(wildcard
->entry
);
1369 int ltt_wildcard_create(struct ltt_channel
*chan
,
1370 struct lttng_ust_event
*event_param
,
1371 struct session_wildcard
**_sw
)
1373 struct session_wildcard
*sw
;
1375 sw
= add_wildcard(chan
, event_param
);
1376 if (!sw
|| IS_ERR(sw
)) {
1384 void _ltt_wildcard_destroy(struct session_wildcard
*sw
)
1386 _remove_wildcard(sw
);
1389 int ltt_wildcard_enable(struct session_wildcard
*wildcard
)
1391 struct ltt_event
*ev
;
1394 if (wildcard
->enabled
)
1396 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1397 ret
= ltt_event_enable(ev
);
1399 DBG("Error: enable error.\n");
1403 wildcard
->enabled
= 1;
1407 int ltt_wildcard_disable(struct session_wildcard
*wildcard
)
1409 struct ltt_event
*ev
;
1412 if (!wildcard
->enabled
)
1414 cds_list_for_each_entry(ev
, &wildcard
->events
, wildcard_list
) {
1415 ret
= ltt_event_disable(ev
);
1417 DBG("Error: disable error.\n");
1421 wildcard
->enabled
= 0;