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
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/utsname.h>
30 #include <linux/err.h>
31 #include "wrapper/uuid.h"
32 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
33 #include "wrapper/random.h"
34 #include "wrapper/tracepoint.h"
35 #include "lttng-kernel-version.h"
36 #include "lttng-events.h"
37 #include "lttng-tracer.h"
38 #include "lttng-abi-old.h"
40 #define METADATA_CACHE_DEFAULT_SIZE 4096
42 static LIST_HEAD(sessions
);
43 static LIST_HEAD(lttng_transport_list
);
45 * Protect the sessions and metadata caches.
47 static DEFINE_MUTEX(sessions_mutex
);
48 static struct kmem_cache
*event_cache
;
50 static void _lttng_event_destroy(struct lttng_event
*event
);
51 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
52 static int _lttng_event_unregister(struct lttng_event
*event
);
54 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
55 struct lttng_channel
*chan
,
56 struct lttng_event
*event
);
58 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
60 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
62 void synchronize_trace(void)
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
66 #ifdef CONFIG_PREEMPT_RT_FULL
69 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
70 #ifdef CONFIG_PREEMPT_RT
73 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
76 struct lttng_session
*lttng_session_create(void)
78 struct lttng_session
*session
;
79 struct lttng_metadata_cache
*metadata_cache
;
81 mutex_lock(&sessions_mutex
);
82 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
85 INIT_LIST_HEAD(&session
->chan
);
86 INIT_LIST_HEAD(&session
->events
);
87 uuid_le_gen(&session
->uuid
);
89 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
92 goto err_free_session
;
93 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
95 if (!metadata_cache
->data
)
97 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
98 kref_init(&metadata_cache
->refcount
);
99 session
->metadata_cache
= metadata_cache
;
100 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
101 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
102 sizeof(metadata_cache
->uuid
));
103 list_add(&session
->list
, &sessions
);
104 mutex_unlock(&sessions_mutex
);
108 kfree(metadata_cache
);
112 mutex_unlock(&sessions_mutex
);
116 void metadata_cache_destroy(struct kref
*kref
)
118 struct lttng_metadata_cache
*cache
=
119 container_of(kref
, struct lttng_metadata_cache
, refcount
);
124 void lttng_session_destroy(struct lttng_session
*session
)
126 struct lttng_channel
*chan
, *tmpchan
;
127 struct lttng_event
*event
, *tmpevent
;
128 struct lttng_metadata_stream
*metadata_stream
;
131 mutex_lock(&sessions_mutex
);
132 ACCESS_ONCE(session
->active
) = 0;
133 list_for_each_entry(chan
, &session
->chan
, list
) {
134 ret
= lttng_syscalls_unregister(chan
);
137 list_for_each_entry(event
, &session
->events
, list
) {
138 ret
= _lttng_event_unregister(event
);
141 synchronize_trace(); /* Wait for in-flight events to complete */
142 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
143 _lttng_event_destroy(event
);
144 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
145 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
146 _lttng_channel_destroy(chan
);
148 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
149 _lttng_metadata_channel_hangup(metadata_stream
);
150 if (session
->pid_tracker
)
151 lttng_pid_tracker_destroy(session
->pid_tracker
);
152 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
153 list_del(&session
->list
);
154 mutex_unlock(&sessions_mutex
);
158 int lttng_session_enable(struct lttng_session
*session
)
161 struct lttng_channel
*chan
;
163 mutex_lock(&sessions_mutex
);
164 if (session
->active
) {
170 * Snapshot the number of events per channel to know the type of header
173 list_for_each_entry(chan
, &session
->chan
, list
) {
174 if (chan
->header_type
)
175 continue; /* don't change it if session stop/restart */
176 if (chan
->free_event_id
< 31)
177 chan
->header_type
= 1; /* compact */
179 chan
->header_type
= 2; /* large */
182 ACCESS_ONCE(session
->active
) = 1;
183 ACCESS_ONCE(session
->been_active
) = 1;
184 ret
= _lttng_session_metadata_statedump(session
);
186 ACCESS_ONCE(session
->active
) = 0;
189 ret
= lttng_statedump_start(session
);
191 ACCESS_ONCE(session
->active
) = 0;
193 mutex_unlock(&sessions_mutex
);
197 int lttng_session_disable(struct lttng_session
*session
)
201 mutex_lock(&sessions_mutex
);
202 if (!session
->active
) {
206 ACCESS_ONCE(session
->active
) = 0;
208 mutex_unlock(&sessions_mutex
);
212 int lttng_channel_enable(struct lttng_channel
*channel
)
216 if (channel
->channel_type
== METADATA_CHANNEL
)
218 old
= xchg(&channel
->enabled
, 1);
224 int lttng_channel_disable(struct lttng_channel
*channel
)
228 if (channel
->channel_type
== METADATA_CHANNEL
)
230 old
= xchg(&channel
->enabled
, 0);
236 int lttng_event_enable(struct lttng_event
*event
)
240 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
242 old
= xchg(&event
->enabled
, 1);
248 int lttng_event_disable(struct lttng_event
*event
)
252 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
254 old
= xchg(&event
->enabled
, 0);
260 static struct lttng_transport
*lttng_transport_find(const char *name
)
262 struct lttng_transport
*transport
;
264 list_for_each_entry(transport
, <tng_transport_list
, node
) {
265 if (!strcmp(transport
->name
, name
))
271 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
272 const char *transport_name
,
274 size_t subbuf_size
, size_t num_subbuf
,
275 unsigned int switch_timer_interval
,
276 unsigned int read_timer_interval
,
277 enum channel_type channel_type
)
279 struct lttng_channel
*chan
;
280 struct lttng_transport
*transport
= NULL
;
282 mutex_lock(&sessions_mutex
);
283 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
284 goto active
; /* Refuse to add channel to active session */
285 transport
= lttng_transport_find(transport_name
);
287 printk(KERN_WARNING
"LTTng transport %s not found\n",
291 if (!try_module_get(transport
->owner
)) {
292 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
295 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
298 chan
->session
= session
;
299 chan
->id
= session
->free_chan_id
++;
300 chan
->ops
= &transport
->ops
;
302 * Note: the channel creation op already writes into the packet
303 * headers. Therefore the "chan" information used as input
304 * should be already accessible.
306 chan
->chan
= transport
->ops
.channel_create(transport_name
,
307 chan
, buf_addr
, subbuf_size
, num_subbuf
,
308 switch_timer_interval
, read_timer_interval
);
312 chan
->transport
= transport
;
313 chan
->channel_type
= channel_type
;
314 list_add(&chan
->list
, &session
->chan
);
315 mutex_unlock(&sessions_mutex
);
322 module_put(transport
->owner
);
325 mutex_unlock(&sessions_mutex
);
330 * Only used internally at session destruction for per-cpu channels, and
331 * when metadata channel is released.
332 * Needs to be called with sessions mutex held.
335 void _lttng_channel_destroy(struct lttng_channel
*chan
)
337 chan
->ops
->channel_destroy(chan
->chan
);
338 module_put(chan
->transport
->owner
);
339 list_del(&chan
->list
);
340 lttng_destroy_context(chan
->ctx
);
344 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
346 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
348 /* Protect the metadata cache with the sessions_mutex. */
349 mutex_lock(&sessions_mutex
);
350 _lttng_channel_destroy(chan
);
351 mutex_unlock(&sessions_mutex
);
353 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
356 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
358 stream
->finalized
= 1;
359 wake_up_interruptible(&stream
->read_wait
);
363 * Supports event creation while tracing session is active.
365 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
366 struct lttng_kernel_event
*event_param
,
368 const struct lttng_event_desc
*internal_desc
)
370 struct lttng_event
*event
;
373 mutex_lock(&sessions_mutex
);
374 if (chan
->free_event_id
== -1U) {
379 * This is O(n^2) (for each event, the loop is called at event
380 * creation). Might require a hash if we have lots of events.
382 list_for_each_entry(event
, &chan
->session
->events
, list
) {
383 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
385 * Allow events with the same name to appear in
386 * different channels.
388 if (event
->chan
== chan
) {
394 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
400 event
->filter
= filter
;
401 event
->id
= chan
->free_event_id
++;
403 event
->instrumentation
= event_param
->instrumentation
;
404 /* Populate lttng_event structure before tracepoint registration. */
406 switch (event_param
->instrumentation
) {
407 case LTTNG_KERNEL_TRACEPOINT
:
408 event
->desc
= lttng_event_get(event_param
->name
);
413 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
414 event
->desc
->probe_callback
,
421 case LTTNG_KERNEL_KPROBE
:
422 ret
= lttng_kprobes_register(event_param
->name
,
423 event_param
->u
.kprobe
.symbol_name
,
424 event_param
->u
.kprobe
.offset
,
425 event_param
->u
.kprobe
.addr
,
431 ret
= try_module_get(event
->desc
->owner
);
434 case LTTNG_KERNEL_KRETPROBE
:
436 struct lttng_event
*event_return
;
438 /* kretprobe defines 2 events */
440 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
445 event_return
->chan
= chan
;
446 event_return
->filter
= filter
;
447 event_return
->id
= chan
->free_event_id
++;
448 event_return
->enabled
= 1;
449 event_return
->instrumentation
= event_param
->instrumentation
;
451 * Populate lttng_event structure before kretprobe registration.
454 ret
= lttng_kretprobes_register(event_param
->name
,
455 event_param
->u
.kretprobe
.symbol_name
,
456 event_param
->u
.kretprobe
.offset
,
457 event_param
->u
.kretprobe
.addr
,
458 event
, event_return
);
460 kmem_cache_free(event_cache
, event_return
);
464 /* Take 2 refs on the module: one per event. */
465 ret
= try_module_get(event
->desc
->owner
);
467 ret
= try_module_get(event
->desc
->owner
);
469 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
471 WARN_ON_ONCE(ret
> 0);
473 kmem_cache_free(event_cache
, event_return
);
474 module_put(event
->desc
->owner
);
475 module_put(event
->desc
->owner
);
476 goto statedump_error
;
478 list_add(&event_return
->list
, &chan
->session
->events
);
481 case LTTNG_KERNEL_FUNCTION
:
482 ret
= lttng_ftrace_register(event_param
->name
,
483 event_param
->u
.ftrace
.symbol_name
,
488 ret
= try_module_get(event
->desc
->owner
);
491 case LTTNG_KERNEL_NOOP
:
492 event
->desc
= internal_desc
;
503 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
504 WARN_ON_ONCE(ret
> 0);
506 goto statedump_error
;
508 list_add(&event
->list
, &chan
->session
->events
);
509 mutex_unlock(&sessions_mutex
);
513 /* If a statedump error occurs, events will not be readable. */
515 kmem_cache_free(event_cache
, event
);
519 mutex_unlock(&sessions_mutex
);
524 * Only used internally at session destruction.
526 int _lttng_event_unregister(struct lttng_event
*event
)
530 switch (event
->instrumentation
) {
531 case LTTNG_KERNEL_TRACEPOINT
:
532 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
533 event
->desc
->probe_callback
,
538 case LTTNG_KERNEL_KPROBE
:
539 lttng_kprobes_unregister(event
);
542 case LTTNG_KERNEL_KRETPROBE
:
543 lttng_kretprobes_unregister(event
);
546 case LTTNG_KERNEL_FUNCTION
:
547 lttng_ftrace_unregister(event
);
550 case LTTNG_KERNEL_NOOP
:
560 * Only used internally at session destruction.
563 void _lttng_event_destroy(struct lttng_event
*event
)
565 switch (event
->instrumentation
) {
566 case LTTNG_KERNEL_TRACEPOINT
:
567 lttng_event_put(event
->desc
);
569 case LTTNG_KERNEL_KPROBE
:
570 module_put(event
->desc
->owner
);
571 lttng_kprobes_destroy_private(event
);
573 case LTTNG_KERNEL_KRETPROBE
:
574 module_put(event
->desc
->owner
);
575 lttng_kretprobes_destroy_private(event
);
577 case LTTNG_KERNEL_FUNCTION
:
578 module_put(event
->desc
->owner
);
579 lttng_ftrace_destroy_private(event
);
581 case LTTNG_KERNEL_NOOP
:
586 list_del(&event
->list
);
587 lttng_destroy_context(event
->ctx
);
588 kmem_cache_free(event_cache
, event
);
591 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
597 mutex_lock(&sessions_mutex
);
599 /* track all pids: destroy tracker. */
600 if (session
->pid_tracker
) {
601 struct lttng_pid_tracker
*lpf
;
603 lpf
= session
->pid_tracker
;
604 rcu_assign_pointer(session
->pid_tracker
, NULL
);
606 lttng_pid_tracker_destroy(lpf
);
610 if (!session
->pid_tracker
) {
611 struct lttng_pid_tracker
*lpf
;
613 lpf
= lttng_pid_tracker_create();
618 ret
= lttng_pid_tracker_add(lpf
, pid
);
619 rcu_assign_pointer(session
->pid_tracker
, lpf
);
621 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
625 mutex_unlock(&sessions_mutex
);
629 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
635 mutex_lock(&sessions_mutex
);
637 /* untrack all pids: replace by empty tracker. */
638 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
639 struct lttng_pid_tracker
*lpf
;
641 lpf
= lttng_pid_tracker_create();
646 rcu_assign_pointer(session
->pid_tracker
, lpf
);
649 lttng_pid_tracker_destroy(old_lpf
);
652 if (!session
->pid_tracker
) {
656 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
659 mutex_unlock(&sessions_mutex
);
664 * Serialize at most one packet worth of metadata into a metadata
666 * We have exclusive access to our metadata buffer (protected by the
667 * sessions_mutex), so we can do racy operations such as looking for
668 * remaining space left in packet and write, since mutual exclusion
669 * protects us from concurrent writes.
670 * Returns the number of bytes written in the channel, 0 if no data
671 * was written and a negative value on error.
673 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
674 struct channel
*chan
)
676 struct lib_ring_buffer_ctx ctx
;
678 size_t len
, reserve_len
;
681 * Ensure we support mutiple get_next / put sequences followed
682 * by put_next. The metadata stream lock internally protects
683 * reading the metadata cache. It can indeed be read
684 * concurrently by "get_next_subbuf" and "flush" operations on
685 * the buffer invoked by different processes.
687 mutex_lock(&stream
->lock
);
688 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
689 if (stream
->metadata_in
!= stream
->metadata_out
)
692 len
= stream
->metadata_cache
->metadata_written
-
696 reserve_len
= min_t(size_t,
697 stream
->transport
->ops
.packet_avail_size(chan
),
699 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
702 * If reservation failed, return an error to the caller.
704 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
706 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
709 stream
->transport
->ops
.event_write(&ctx
,
710 stream
->metadata_cache
->data
+ stream
->metadata_in
,
712 stream
->transport
->ops
.event_commit(&ctx
);
713 stream
->metadata_in
+= reserve_len
;
717 mutex_unlock(&stream
->lock
);
722 * Write the metadata to the metadata cache.
723 * Must be called with sessions_mutex held.
725 int lttng_metadata_printf(struct lttng_session
*session
,
726 const char *fmt
, ...)
731 struct lttng_metadata_stream
*stream
;
733 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
736 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
742 if (session
->metadata_cache
->metadata_written
+ len
>
743 session
->metadata_cache
->cache_alloc
) {
744 char *tmp_cache_realloc
;
745 unsigned int tmp_cache_alloc_size
;
747 tmp_cache_alloc_size
= max_t(unsigned int,
748 session
->metadata_cache
->cache_alloc
+ len
,
749 session
->metadata_cache
->cache_alloc
<< 1);
750 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
751 tmp_cache_alloc_size
, GFP_KERNEL
);
752 if (!tmp_cache_realloc
)
754 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
755 session
->metadata_cache
->data
= tmp_cache_realloc
;
757 memcpy(session
->metadata_cache
->data
+
758 session
->metadata_cache
->metadata_written
,
760 session
->metadata_cache
->metadata_written
+= len
;
763 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
764 wake_up_interruptible(&stream
->read_wait
);
774 * Must be called with sessions_mutex held.
777 int _lttng_field_statedump(struct lttng_session
*session
,
778 const struct lttng_event_field
*field
)
782 switch (field
->type
.atype
) {
784 ret
= lttng_metadata_printf(session
,
785 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
786 field
->type
.u
.basic
.integer
.size
,
787 field
->type
.u
.basic
.integer
.alignment
,
788 field
->type
.u
.basic
.integer
.signedness
,
789 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
791 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
794 field
->type
.u
.basic
.integer
.base
,
796 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
798 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
803 ret
= lttng_metadata_printf(session
,
805 field
->type
.u
.basic
.enumeration
.name
,
810 const struct lttng_basic_type
*elem_type
;
812 elem_type
= &field
->type
.u
.array
.elem_type
;
813 ret
= lttng_metadata_printf(session
,
814 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
815 elem_type
->u
.basic
.integer
.size
,
816 elem_type
->u
.basic
.integer
.alignment
,
817 elem_type
->u
.basic
.integer
.signedness
,
818 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
820 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
823 elem_type
->u
.basic
.integer
.base
,
825 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
827 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
829 field
->name
, field
->type
.u
.array
.length
);
834 const struct lttng_basic_type
*elem_type
;
835 const struct lttng_basic_type
*length_type
;
837 elem_type
= &field
->type
.u
.sequence
.elem_type
;
838 length_type
= &field
->type
.u
.sequence
.length_type
;
839 ret
= lttng_metadata_printf(session
,
840 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
841 length_type
->u
.basic
.integer
.size
,
842 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
843 length_type
->u
.basic
.integer
.signedness
,
844 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
846 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
849 length_type
->u
.basic
.integer
.base
,
851 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
853 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
859 ret
= lttng_metadata_printf(session
,
860 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
861 elem_type
->u
.basic
.integer
.size
,
862 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
863 elem_type
->u
.basic
.integer
.signedness
,
864 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
866 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
869 elem_type
->u
.basic
.integer
.base
,
871 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
873 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
881 /* Default encoding is UTF8 */
882 ret
= lttng_metadata_printf(session
,
884 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
885 " { encoding = ASCII; }" : "",
896 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
897 struct lttng_ctx
*ctx
)
904 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
905 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
907 ret
= _lttng_field_statedump(session
, &field
->event_field
);
915 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
916 struct lttng_event
*event
)
918 const struct lttng_event_desc
*desc
= event
->desc
;
922 for (i
= 0; i
< desc
->nr_fields
; i
++) {
923 const struct lttng_event_field
*field
= &desc
->fields
[i
];
925 ret
= _lttng_field_statedump(session
, field
);
933 * Must be called with sessions_mutex held.
936 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
937 struct lttng_channel
*chan
,
938 struct lttng_event
*event
)
942 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
944 if (chan
->channel_type
== METADATA_CHANNEL
)
947 ret
= lttng_metadata_printf(session
,
951 " stream_id = %u;\n",
959 ret
= lttng_metadata_printf(session
,
960 " context := struct {\n");
964 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
968 ret
= lttng_metadata_printf(session
,
974 ret
= lttng_metadata_printf(session
,
975 " fields := struct {\n"
980 ret
= _lttng_fields_metadata_statedump(session
, event
);
985 * LTTng space reservation can only reserve multiples of the
988 ret
= lttng_metadata_printf(session
,
994 event
->metadata_dumped
= 1;
1001 * Must be called with sessions_mutex held.
1004 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1005 struct lttng_channel
*chan
)
1009 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1012 if (chan
->channel_type
== METADATA_CHANNEL
)
1015 WARN_ON_ONCE(!chan
->header_type
);
1016 ret
= lttng_metadata_printf(session
,
1019 " event.header := %s;\n"
1020 " packet.context := struct packet_context;\n",
1022 chan
->header_type
== 1 ? "struct event_header_compact" :
1023 "struct event_header_large");
1028 ret
= lttng_metadata_printf(session
,
1029 " event.context := struct {\n");
1033 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1037 ret
= lttng_metadata_printf(session
,
1043 ret
= lttng_metadata_printf(session
,
1046 chan
->metadata_dumped
= 1;
1052 * Must be called with sessions_mutex held.
1055 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1057 return lttng_metadata_printf(session
,
1058 "struct packet_context {\n"
1059 " uint64_clock_monotonic_t timestamp_begin;\n"
1060 " uint64_clock_monotonic_t timestamp_end;\n"
1061 " uint64_t content_size;\n"
1062 " uint64_t packet_size;\n"
1063 " unsigned long events_discarded;\n"
1064 " uint32_t cpu_id;\n"
1071 * id: range: 0 - 30.
1072 * id 31 is reserved to indicate an extended header.
1075 * id: range: 0 - 65534.
1076 * id 65535 is reserved to indicate an extended header.
1078 * Must be called with sessions_mutex held.
1081 int _lttng_event_header_declare(struct lttng_session
*session
)
1083 return lttng_metadata_printf(session
,
1084 "struct event_header_compact {\n"
1085 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1088 " uint27_clock_monotonic_t timestamp;\n"
1092 " uint64_clock_monotonic_t timestamp;\n"
1097 "struct event_header_large {\n"
1098 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1101 " uint32_clock_monotonic_t timestamp;\n"
1105 " uint64_clock_monotonic_t timestamp;\n"
1109 lttng_alignof(uint32_t) * CHAR_BIT
,
1110 lttng_alignof(uint16_t) * CHAR_BIT
1115 * Approximation of NTP time of day to clock monotonic correlation,
1116 * taken at start of trace.
1117 * Yes, this is only an approximation. Yes, we can (and will) do better
1118 * in future versions.
1121 uint64_t measure_clock_offset(void)
1123 uint64_t offset
, monotonic
[2], realtime
;
1124 struct timespec rts
= { 0, 0 };
1125 unsigned long flags
;
1127 /* Disable interrupts to increase correlation precision. */
1128 local_irq_save(flags
);
1129 monotonic
[0] = trace_clock_read64();
1130 getnstimeofday(&rts
);
1131 monotonic
[1] = trace_clock_read64();
1132 local_irq_restore(flags
);
1134 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1135 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1136 realtime
+= rts
.tv_nsec
;
1137 offset
= realtime
- offset
;
1142 * Output metadata into this session's metadata buffers.
1143 * Must be called with sessions_mutex held.
1146 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1148 unsigned char *uuid_c
= session
->uuid
.b
;
1149 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1150 struct lttng_channel
*chan
;
1151 struct lttng_event
*event
;
1154 if (!ACCESS_ONCE(session
->active
))
1156 if (session
->metadata_dumped
)
1159 snprintf(uuid_s
, sizeof(uuid_s
),
1160 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1161 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1162 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1163 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1164 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1166 ret
= lttng_metadata_printf(session
,
1167 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1168 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1169 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1170 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1171 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1172 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1173 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1179 " byte_order = %s;\n"
1180 " packet.header := struct {\n"
1181 " uint32_t magic;\n"
1182 " uint8_t uuid[16];\n"
1183 " uint32_t stream_id;\n"
1186 lttng_alignof(uint8_t) * CHAR_BIT
,
1187 lttng_alignof(uint16_t) * CHAR_BIT
,
1188 lttng_alignof(uint32_t) * CHAR_BIT
,
1189 lttng_alignof(uint64_t) * CHAR_BIT
,
1190 sizeof(unsigned long) * CHAR_BIT
,
1191 lttng_alignof(unsigned long) * CHAR_BIT
,
1204 ret
= lttng_metadata_printf(session
,
1206 " hostname = \"%s\";\n"
1207 " domain = \"kernel\";\n"
1208 " sysname = \"%s\";\n"
1209 " kernel_release = \"%s\";\n"
1210 " kernel_version = \"%s\";\n"
1211 " tracer_name = \"lttng-modules\";\n"
1212 " tracer_major = %d;\n"
1213 " tracer_minor = %d;\n"
1214 " tracer_patchlevel = %d;\n"
1216 current
->nsproxy
->uts_ns
->name
.nodename
,
1220 LTTNG_MODULES_MAJOR_VERSION
,
1221 LTTNG_MODULES_MINOR_VERSION
,
1222 LTTNG_MODULES_PATCHLEVEL_VERSION
1227 ret
= lttng_metadata_printf(session
,
1235 if (!trace_clock_uuid(clock_uuid_s
)) {
1236 ret
= lttng_metadata_printf(session
,
1237 " uuid = \"%s\";\n",
1244 ret
= lttng_metadata_printf(session
,
1245 " description = \"Monotonic Clock\";\n"
1246 " freq = %llu; /* Frequency, in Hz */\n"
1247 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1250 (unsigned long long) trace_clock_freq(),
1251 (unsigned long long) measure_clock_offset()
1256 ret
= lttng_metadata_printf(session
,
1257 "typealias integer {\n"
1258 " size = 27; align = 1; signed = false;\n"
1259 " map = clock.monotonic.value;\n"
1260 "} := uint27_clock_monotonic_t;\n"
1262 "typealias integer {\n"
1263 " size = 32; align = %u; signed = false;\n"
1264 " map = clock.monotonic.value;\n"
1265 "} := uint32_clock_monotonic_t;\n"
1267 "typealias integer {\n"
1268 " size = 64; align = %u; signed = false;\n"
1269 " map = clock.monotonic.value;\n"
1270 "} := uint64_clock_monotonic_t;\n\n",
1271 lttng_alignof(uint32_t) * CHAR_BIT
,
1272 lttng_alignof(uint64_t) * CHAR_BIT
1277 ret
= _lttng_stream_packet_context_declare(session
);
1281 ret
= _lttng_event_header_declare(session
);
1286 list_for_each_entry(chan
, &session
->chan
, list
) {
1287 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1292 list_for_each_entry(event
, &session
->events
, list
) {
1293 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1297 session
->metadata_dumped
= 1;
1303 * lttng_transport_register - LTT transport registration
1304 * @transport: transport structure
1306 * Registers a transport which can be used as output to extract the data out of
1307 * LTTng. The module calling this registration function must ensure that no
1308 * trap-inducing code will be executed by the transport functions. E.g.
1309 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1310 * is made visible to the transport function. This registration acts as a
1311 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1312 * after its registration must it synchronize the TLBs.
1314 void lttng_transport_register(struct lttng_transport
*transport
)
1317 * Make sure no page fault can be triggered by the module about to be
1318 * registered. We deal with this here so we don't have to call
1319 * vmalloc_sync_all() in each module's init.
1321 wrapper_vmalloc_sync_all();
1323 mutex_lock(&sessions_mutex
);
1324 list_add_tail(&transport
->node
, <tng_transport_list
);
1325 mutex_unlock(&sessions_mutex
);
1327 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1330 * lttng_transport_unregister - LTT transport unregistration
1331 * @transport: transport structure
1333 void lttng_transport_unregister(struct lttng_transport
*transport
)
1335 mutex_lock(&sessions_mutex
);
1336 list_del(&transport
->node
);
1337 mutex_unlock(&sessions_mutex
);
1339 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1341 static int __init
lttng_events_init(void)
1345 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1349 ret
= lttng_tracepoint_init();
1352 event_cache
= KMEM_CACHE(lttng_event
, 0);
1357 ret
= lttng_abi_init();
1360 ret
= lttng_logger_init();
1368 kmem_cache_destroy(event_cache
);
1370 lttng_tracepoint_exit();
1374 module_init(lttng_events_init
);
1376 static void __exit
lttng_events_exit(void)
1378 struct lttng_session
*session
, *tmpsession
;
1380 lttng_logger_exit();
1382 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1383 lttng_session_destroy(session
);
1384 kmem_cache_destroy(event_cache
);
1385 lttng_tracepoint_exit();
1388 module_exit(lttng_events_exit
);
1390 MODULE_LICENSE("GPL and additional rights");
1391 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1392 MODULE_DESCRIPTION("LTTng Events");
1393 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1394 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1395 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1396 LTTNG_MODULES_EXTRAVERSION
);