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 <linux/seq_file.h>
32 #include <linux/file.h>
33 #include <linux/anon_inodes.h>
34 #include "wrapper/file.h"
35 #include "wrapper/uuid.h"
36 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
37 #include "wrapper/random.h"
38 #include "wrapper/tracepoint.h"
39 #include "lttng-kernel-version.h"
40 #include "lttng-events.h"
41 #include "lttng-tracer.h"
42 #include "lttng-abi-old.h"
44 #define METADATA_CACHE_DEFAULT_SIZE 4096
46 static LIST_HEAD(sessions
);
47 static LIST_HEAD(lttng_transport_list
);
49 * Protect the sessions and metadata caches.
51 static DEFINE_MUTEX(sessions_mutex
);
52 static struct kmem_cache
*event_cache
;
54 static void _lttng_event_destroy(struct lttng_event
*event
);
55 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
56 static int _lttng_event_unregister(struct lttng_event
*event
);
58 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
59 struct lttng_channel
*chan
,
60 struct lttng_event
*event
);
62 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
64 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
66 void synchronize_trace(void)
69 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
70 #ifdef CONFIG_PREEMPT_RT_FULL
73 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
74 #ifdef CONFIG_PREEMPT_RT
77 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
80 struct lttng_session
*lttng_session_create(void)
82 struct lttng_session
*session
;
83 struct lttng_metadata_cache
*metadata_cache
;
85 mutex_lock(&sessions_mutex
);
86 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
89 INIT_LIST_HEAD(&session
->chan
);
90 INIT_LIST_HEAD(&session
->events
);
91 uuid_le_gen(&session
->uuid
);
93 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
96 goto err_free_session
;
97 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
99 if (!metadata_cache
->data
)
101 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
102 kref_init(&metadata_cache
->refcount
);
103 session
->metadata_cache
= metadata_cache
;
104 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
105 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
106 sizeof(metadata_cache
->uuid
));
107 list_add(&session
->list
, &sessions
);
108 mutex_unlock(&sessions_mutex
);
112 kfree(metadata_cache
);
116 mutex_unlock(&sessions_mutex
);
120 void metadata_cache_destroy(struct kref
*kref
)
122 struct lttng_metadata_cache
*cache
=
123 container_of(kref
, struct lttng_metadata_cache
, refcount
);
128 void lttng_session_destroy(struct lttng_session
*session
)
130 struct lttng_channel
*chan
, *tmpchan
;
131 struct lttng_event
*event
, *tmpevent
;
132 struct lttng_metadata_stream
*metadata_stream
;
135 mutex_lock(&sessions_mutex
);
136 ACCESS_ONCE(session
->active
) = 0;
137 list_for_each_entry(chan
, &session
->chan
, list
) {
138 ret
= lttng_syscalls_unregister(chan
);
141 list_for_each_entry(event
, &session
->events
, list
) {
142 ret
= _lttng_event_unregister(event
);
145 synchronize_trace(); /* Wait for in-flight events to complete */
146 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
147 _lttng_event_destroy(event
);
148 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
149 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
150 _lttng_channel_destroy(chan
);
152 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
153 _lttng_metadata_channel_hangup(metadata_stream
);
154 if (session
->pid_tracker
)
155 lttng_pid_tracker_destroy(session
->pid_tracker
);
156 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
157 list_del(&session
->list
);
158 mutex_unlock(&sessions_mutex
);
162 int lttng_session_enable(struct lttng_session
*session
)
165 struct lttng_channel
*chan
;
167 mutex_lock(&sessions_mutex
);
168 if (session
->active
) {
174 * Snapshot the number of events per channel to know the type of header
177 list_for_each_entry(chan
, &session
->chan
, list
) {
178 if (chan
->header_type
)
179 continue; /* don't change it if session stop/restart */
180 if (chan
->free_event_id
< 31)
181 chan
->header_type
= 1; /* compact */
183 chan
->header_type
= 2; /* large */
186 ACCESS_ONCE(session
->active
) = 1;
187 ACCESS_ONCE(session
->been_active
) = 1;
188 ret
= _lttng_session_metadata_statedump(session
);
190 ACCESS_ONCE(session
->active
) = 0;
193 ret
= lttng_statedump_start(session
);
195 ACCESS_ONCE(session
->active
) = 0;
197 mutex_unlock(&sessions_mutex
);
201 int lttng_session_disable(struct lttng_session
*session
)
205 mutex_lock(&sessions_mutex
);
206 if (!session
->active
) {
210 ACCESS_ONCE(session
->active
) = 0;
212 mutex_unlock(&sessions_mutex
);
216 int lttng_channel_enable(struct lttng_channel
*channel
)
220 if (channel
->channel_type
== METADATA_CHANNEL
)
222 old
= xchg(&channel
->enabled
, 1);
228 int lttng_channel_disable(struct lttng_channel
*channel
)
232 if (channel
->channel_type
== METADATA_CHANNEL
)
234 old
= xchg(&channel
->enabled
, 0);
240 int lttng_event_enable(struct lttng_event
*event
)
244 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
246 old
= xchg(&event
->enabled
, 1);
252 int lttng_event_disable(struct lttng_event
*event
)
256 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
258 old
= xchg(&event
->enabled
, 0);
264 static struct lttng_transport
*lttng_transport_find(const char *name
)
266 struct lttng_transport
*transport
;
268 list_for_each_entry(transport
, <tng_transport_list
, node
) {
269 if (!strcmp(transport
->name
, name
))
275 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
276 const char *transport_name
,
278 size_t subbuf_size
, size_t num_subbuf
,
279 unsigned int switch_timer_interval
,
280 unsigned int read_timer_interval
,
281 enum channel_type channel_type
)
283 struct lttng_channel
*chan
;
284 struct lttng_transport
*transport
= NULL
;
286 mutex_lock(&sessions_mutex
);
287 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
288 goto active
; /* Refuse to add channel to active session */
289 transport
= lttng_transport_find(transport_name
);
291 printk(KERN_WARNING
"LTTng transport %s not found\n",
295 if (!try_module_get(transport
->owner
)) {
296 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
299 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
302 chan
->session
= session
;
303 chan
->id
= session
->free_chan_id
++;
304 chan
->ops
= &transport
->ops
;
306 * Note: the channel creation op already writes into the packet
307 * headers. Therefore the "chan" information used as input
308 * should be already accessible.
310 chan
->chan
= transport
->ops
.channel_create(transport_name
,
311 chan
, buf_addr
, subbuf_size
, num_subbuf
,
312 switch_timer_interval
, read_timer_interval
);
316 chan
->transport
= transport
;
317 chan
->channel_type
= channel_type
;
318 list_add(&chan
->list
, &session
->chan
);
319 mutex_unlock(&sessions_mutex
);
326 module_put(transport
->owner
);
329 mutex_unlock(&sessions_mutex
);
334 * Only used internally at session destruction for per-cpu channels, and
335 * when metadata channel is released.
336 * Needs to be called with sessions mutex held.
339 void _lttng_channel_destroy(struct lttng_channel
*chan
)
341 chan
->ops
->channel_destroy(chan
->chan
);
342 module_put(chan
->transport
->owner
);
343 list_del(&chan
->list
);
344 lttng_destroy_context(chan
->ctx
);
348 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
350 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
352 /* Protect the metadata cache with the sessions_mutex. */
353 mutex_lock(&sessions_mutex
);
354 _lttng_channel_destroy(chan
);
355 mutex_unlock(&sessions_mutex
);
357 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
360 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
362 stream
->finalized
= 1;
363 wake_up_interruptible(&stream
->read_wait
);
367 * Supports event creation while tracing session is active.
369 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
370 struct lttng_kernel_event
*event_param
,
372 const struct lttng_event_desc
*internal_desc
)
374 struct lttng_event
*event
;
377 mutex_lock(&sessions_mutex
);
378 if (chan
->free_event_id
== -1U) {
383 * This is O(n^2) (for each event, the loop is called at event
384 * creation). Might require a hash if we have lots of events.
386 list_for_each_entry(event
, &chan
->session
->events
, list
) {
387 if (!strcmp(event
->desc
->name
, event_param
->name
)) {
389 * Allow events with the same name to appear in
390 * different channels.
392 if (event
->chan
== chan
) {
398 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
404 event
->filter
= filter
;
405 event
->id
= chan
->free_event_id
++;
407 event
->instrumentation
= event_param
->instrumentation
;
408 /* Populate lttng_event structure before tracepoint registration. */
410 switch (event_param
->instrumentation
) {
411 case LTTNG_KERNEL_TRACEPOINT
:
412 event
->desc
= lttng_event_get(event_param
->name
);
417 ret
= lttng_wrapper_tracepoint_probe_register(event
->desc
->kname
,
418 event
->desc
->probe_callback
,
425 case LTTNG_KERNEL_KPROBE
:
426 ret
= lttng_kprobes_register(event_param
->name
,
427 event_param
->u
.kprobe
.symbol_name
,
428 event_param
->u
.kprobe
.offset
,
429 event_param
->u
.kprobe
.addr
,
435 ret
= try_module_get(event
->desc
->owner
);
438 case LTTNG_KERNEL_KRETPROBE
:
440 struct lttng_event
*event_return
;
442 /* kretprobe defines 2 events */
444 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
449 event_return
->chan
= chan
;
450 event_return
->filter
= filter
;
451 event_return
->id
= chan
->free_event_id
++;
452 event_return
->enabled
= 1;
453 event_return
->instrumentation
= event_param
->instrumentation
;
455 * Populate lttng_event structure before kretprobe registration.
458 ret
= lttng_kretprobes_register(event_param
->name
,
459 event_param
->u
.kretprobe
.symbol_name
,
460 event_param
->u
.kretprobe
.offset
,
461 event_param
->u
.kretprobe
.addr
,
462 event
, event_return
);
464 kmem_cache_free(event_cache
, event_return
);
468 /* Take 2 refs on the module: one per event. */
469 ret
= try_module_get(event
->desc
->owner
);
471 ret
= try_module_get(event
->desc
->owner
);
473 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
475 WARN_ON_ONCE(ret
> 0);
477 kmem_cache_free(event_cache
, event_return
);
478 module_put(event
->desc
->owner
);
479 module_put(event
->desc
->owner
);
480 goto statedump_error
;
482 list_add(&event_return
->list
, &chan
->session
->events
);
485 case LTTNG_KERNEL_FUNCTION
:
486 ret
= lttng_ftrace_register(event_param
->name
,
487 event_param
->u
.ftrace
.symbol_name
,
492 ret
= try_module_get(event
->desc
->owner
);
495 case LTTNG_KERNEL_NOOP
:
496 event
->desc
= internal_desc
;
507 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
508 WARN_ON_ONCE(ret
> 0);
510 goto statedump_error
;
512 list_add(&event
->list
, &chan
->session
->events
);
513 mutex_unlock(&sessions_mutex
);
517 /* If a statedump error occurs, events will not be readable. */
519 kmem_cache_free(event_cache
, event
);
523 mutex_unlock(&sessions_mutex
);
528 * Only used internally at session destruction.
530 int _lttng_event_unregister(struct lttng_event
*event
)
534 switch (event
->instrumentation
) {
535 case LTTNG_KERNEL_TRACEPOINT
:
536 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
537 event
->desc
->probe_callback
,
542 case LTTNG_KERNEL_KPROBE
:
543 lttng_kprobes_unregister(event
);
546 case LTTNG_KERNEL_KRETPROBE
:
547 lttng_kretprobes_unregister(event
);
550 case LTTNG_KERNEL_FUNCTION
:
551 lttng_ftrace_unregister(event
);
554 case LTTNG_KERNEL_NOOP
:
564 * Only used internally at session destruction.
567 void _lttng_event_destroy(struct lttng_event
*event
)
569 switch (event
->instrumentation
) {
570 case LTTNG_KERNEL_TRACEPOINT
:
571 lttng_event_put(event
->desc
);
573 case LTTNG_KERNEL_KPROBE
:
574 module_put(event
->desc
->owner
);
575 lttng_kprobes_destroy_private(event
);
577 case LTTNG_KERNEL_KRETPROBE
:
578 module_put(event
->desc
->owner
);
579 lttng_kretprobes_destroy_private(event
);
581 case LTTNG_KERNEL_FUNCTION
:
582 module_put(event
->desc
->owner
);
583 lttng_ftrace_destroy_private(event
);
585 case LTTNG_KERNEL_NOOP
:
590 list_del(&event
->list
);
591 lttng_destroy_context(event
->ctx
);
592 kmem_cache_free(event_cache
, event
);
595 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
601 mutex_lock(&sessions_mutex
);
603 /* track all pids: destroy tracker. */
604 if (session
->pid_tracker
) {
605 struct lttng_pid_tracker
*lpf
;
607 lpf
= session
->pid_tracker
;
608 rcu_assign_pointer(session
->pid_tracker
, NULL
);
610 lttng_pid_tracker_destroy(lpf
);
614 if (!session
->pid_tracker
) {
615 struct lttng_pid_tracker
*lpf
;
617 lpf
= lttng_pid_tracker_create();
622 ret
= lttng_pid_tracker_add(lpf
, pid
);
623 rcu_assign_pointer(session
->pid_tracker
, lpf
);
625 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
629 mutex_unlock(&sessions_mutex
);
633 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
639 mutex_lock(&sessions_mutex
);
641 /* untrack all pids: replace by empty tracker. */
642 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
643 struct lttng_pid_tracker
*lpf
;
645 lpf
= lttng_pid_tracker_create();
650 rcu_assign_pointer(session
->pid_tracker
, lpf
);
653 lttng_pid_tracker_destroy(old_lpf
);
656 if (!session
->pid_tracker
) {
660 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
663 mutex_unlock(&sessions_mutex
);
668 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
670 struct lttng_session
*session
= m
->private;
671 struct lttng_pid_tracker
*lpf
;
672 struct lttng_pid_hash_node
*e
;
675 mutex_lock(&sessions_mutex
);
676 lpf
= session
->pid_tracker
;
678 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
679 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
681 hlist_for_each_entry(e
, head
, hlist
) {
687 /* PID tracker disabled. */
688 if (iter
>= *pos
&& iter
== 0) {
689 return session
; /* empty tracker */
697 /* Called with sessions_mutex held. */
699 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
701 struct lttng_session
*session
= m
->private;
702 struct lttng_pid_tracker
*lpf
;
703 struct lttng_pid_hash_node
*e
;
707 lpf
= session
->pid_tracker
;
709 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
710 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
712 hlist_for_each_entry(e
, head
, hlist
) {
718 /* PID tracker disabled. */
719 if (iter
>= *ppos
&& iter
== 0)
720 return session
; /* empty tracker */
729 void pid_list_stop(struct seq_file
*m
, void *p
)
731 mutex_unlock(&sessions_mutex
);
735 int pid_list_show(struct seq_file
*m
, void *p
)
739 if (p
== m
->private) {
740 /* Tracker disabled. */
743 const struct lttng_pid_hash_node
*e
= p
;
745 pid
= lttng_pid_tracker_get_node_pid(e
);
747 seq_printf(m
, "process { pid = %d; };\n", pid
);
752 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
753 .start
= pid_list_start
,
754 .next
= pid_list_next
,
755 .stop
= pid_list_stop
,
756 .show
= pid_list_show
,
760 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
762 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
766 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
768 struct seq_file
*m
= file
->private_data
;
769 struct lttng_session
*session
= m
->private;
772 WARN_ON_ONCE(!session
);
773 ret
= seq_release(inode
, file
);
779 const struct file_operations lttng_tracker_pids_list_fops
= {
780 .owner
= THIS_MODULE
,
781 .open
= lttng_tracker_pids_list_open
,
784 .release
= lttng_tracker_pids_list_release
,
787 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
789 struct file
*tracker_pids_list_file
;
793 file_fd
= lttng_get_unused_fd();
799 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
800 <tng_tracker_pids_list_fops
,
802 if (IS_ERR(tracker_pids_list_file
)) {
803 ret
= PTR_ERR(tracker_pids_list_file
);
806 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
809 m
= tracker_pids_list_file
->private_data
;
810 m
->private = session
;
811 fd_install(file_fd
, tracker_pids_list_file
);
812 atomic_long_inc(&session
->file
->f_count
);
817 fput(tracker_pids_list_file
);
819 put_unused_fd(file_fd
);
825 * Serialize at most one packet worth of metadata into a metadata
827 * We have exclusive access to our metadata buffer (protected by the
828 * sessions_mutex), so we can do racy operations such as looking for
829 * remaining space left in packet and write, since mutual exclusion
830 * protects us from concurrent writes.
831 * Returns the number of bytes written in the channel, 0 if no data
832 * was written and a negative value on error.
834 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
835 struct channel
*chan
)
837 struct lib_ring_buffer_ctx ctx
;
839 size_t len
, reserve_len
;
842 * Ensure we support mutiple get_next / put sequences followed
843 * by put_next. The metadata stream lock internally protects
844 * reading the metadata cache. It can indeed be read
845 * concurrently by "get_next_subbuf" and "flush" operations on
846 * the buffer invoked by different processes.
848 mutex_lock(&stream
->lock
);
849 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
850 if (stream
->metadata_in
!= stream
->metadata_out
)
853 len
= stream
->metadata_cache
->metadata_written
-
857 reserve_len
= min_t(size_t,
858 stream
->transport
->ops
.packet_avail_size(chan
),
860 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
863 * If reservation failed, return an error to the caller.
865 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
867 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
870 stream
->transport
->ops
.event_write(&ctx
,
871 stream
->metadata_cache
->data
+ stream
->metadata_in
,
873 stream
->transport
->ops
.event_commit(&ctx
);
874 stream
->metadata_in
+= reserve_len
;
878 mutex_unlock(&stream
->lock
);
883 * Write the metadata to the metadata cache.
884 * Must be called with sessions_mutex held.
886 int lttng_metadata_printf(struct lttng_session
*session
,
887 const char *fmt
, ...)
892 struct lttng_metadata_stream
*stream
;
894 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
897 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
903 if (session
->metadata_cache
->metadata_written
+ len
>
904 session
->metadata_cache
->cache_alloc
) {
905 char *tmp_cache_realloc
;
906 unsigned int tmp_cache_alloc_size
;
908 tmp_cache_alloc_size
= max_t(unsigned int,
909 session
->metadata_cache
->cache_alloc
+ len
,
910 session
->metadata_cache
->cache_alloc
<< 1);
911 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
912 tmp_cache_alloc_size
, GFP_KERNEL
);
913 if (!tmp_cache_realloc
)
915 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
916 session
->metadata_cache
->data
= tmp_cache_realloc
;
918 memcpy(session
->metadata_cache
->data
+
919 session
->metadata_cache
->metadata_written
,
921 session
->metadata_cache
->metadata_written
+= len
;
924 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
925 wake_up_interruptible(&stream
->read_wait
);
935 * Must be called with sessions_mutex held.
938 int _lttng_field_statedump(struct lttng_session
*session
,
939 const struct lttng_event_field
*field
)
943 switch (field
->type
.atype
) {
945 ret
= lttng_metadata_printf(session
,
946 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
947 field
->type
.u
.basic
.integer
.size
,
948 field
->type
.u
.basic
.integer
.alignment
,
949 field
->type
.u
.basic
.integer
.signedness
,
950 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
952 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
955 field
->type
.u
.basic
.integer
.base
,
957 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
959 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
964 ret
= lttng_metadata_printf(session
,
966 field
->type
.u
.basic
.enumeration
.name
,
971 const struct lttng_basic_type
*elem_type
;
973 elem_type
= &field
->type
.u
.array
.elem_type
;
974 ret
= lttng_metadata_printf(session
,
975 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
976 elem_type
->u
.basic
.integer
.size
,
977 elem_type
->u
.basic
.integer
.alignment
,
978 elem_type
->u
.basic
.integer
.signedness
,
979 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
981 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
984 elem_type
->u
.basic
.integer
.base
,
986 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
988 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
990 field
->name
, field
->type
.u
.array
.length
);
995 const struct lttng_basic_type
*elem_type
;
996 const struct lttng_basic_type
*length_type
;
998 elem_type
= &field
->type
.u
.sequence
.elem_type
;
999 length_type
= &field
->type
.u
.sequence
.length_type
;
1000 ret
= lttng_metadata_printf(session
,
1001 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1002 length_type
->u
.basic
.integer
.size
,
1003 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
1004 length_type
->u
.basic
.integer
.signedness
,
1005 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1007 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1010 length_type
->u
.basic
.integer
.base
,
1012 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1014 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1020 ret
= lttng_metadata_printf(session
,
1021 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1022 elem_type
->u
.basic
.integer
.size
,
1023 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
1024 elem_type
->u
.basic
.integer
.signedness
,
1025 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1027 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1030 elem_type
->u
.basic
.integer
.base
,
1032 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1034 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1042 /* Default encoding is UTF8 */
1043 ret
= lttng_metadata_printf(session
,
1045 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
1046 " { encoding = ASCII; }" : "",
1057 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
1058 struct lttng_ctx
*ctx
)
1065 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
1066 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
1068 ret
= _lttng_field_statedump(session
, &field
->event_field
);
1076 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
1077 struct lttng_event
*event
)
1079 const struct lttng_event_desc
*desc
= event
->desc
;
1083 for (i
= 0; i
< desc
->nr_fields
; i
++) {
1084 const struct lttng_event_field
*field
= &desc
->fields
[i
];
1086 ret
= _lttng_field_statedump(session
, field
);
1094 * Must be called with sessions_mutex held.
1097 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
1098 struct lttng_channel
*chan
,
1099 struct lttng_event
*event
)
1103 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1105 if (chan
->channel_type
== METADATA_CHANNEL
)
1108 ret
= lttng_metadata_printf(session
,
1112 " stream_id = %u;\n",
1120 ret
= lttng_metadata_printf(session
,
1121 " context := struct {\n");
1125 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
1129 ret
= lttng_metadata_printf(session
,
1135 ret
= lttng_metadata_printf(session
,
1136 " fields := struct {\n"
1141 ret
= _lttng_fields_metadata_statedump(session
, event
);
1146 * LTTng space reservation can only reserve multiples of the
1149 ret
= lttng_metadata_printf(session
,
1155 event
->metadata_dumped
= 1;
1162 * Must be called with sessions_mutex held.
1165 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1166 struct lttng_channel
*chan
)
1170 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1173 if (chan
->channel_type
== METADATA_CHANNEL
)
1176 WARN_ON_ONCE(!chan
->header_type
);
1177 ret
= lttng_metadata_printf(session
,
1180 " event.header := %s;\n"
1181 " packet.context := struct packet_context;\n",
1183 chan
->header_type
== 1 ? "struct event_header_compact" :
1184 "struct event_header_large");
1189 ret
= lttng_metadata_printf(session
,
1190 " event.context := struct {\n");
1194 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1198 ret
= lttng_metadata_printf(session
,
1204 ret
= lttng_metadata_printf(session
,
1207 chan
->metadata_dumped
= 1;
1213 * Must be called with sessions_mutex held.
1216 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1218 return lttng_metadata_printf(session
,
1219 "struct packet_context {\n"
1220 " uint64_clock_monotonic_t timestamp_begin;\n"
1221 " uint64_clock_monotonic_t timestamp_end;\n"
1222 " uint64_t content_size;\n"
1223 " uint64_t packet_size;\n"
1224 " unsigned long events_discarded;\n"
1225 " uint32_t cpu_id;\n"
1232 * id: range: 0 - 30.
1233 * id 31 is reserved to indicate an extended header.
1236 * id: range: 0 - 65534.
1237 * id 65535 is reserved to indicate an extended header.
1239 * Must be called with sessions_mutex held.
1242 int _lttng_event_header_declare(struct lttng_session
*session
)
1244 return lttng_metadata_printf(session
,
1245 "struct event_header_compact {\n"
1246 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1249 " uint27_clock_monotonic_t timestamp;\n"
1253 " uint64_clock_monotonic_t timestamp;\n"
1258 "struct event_header_large {\n"
1259 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1262 " uint32_clock_monotonic_t timestamp;\n"
1266 " uint64_clock_monotonic_t timestamp;\n"
1270 lttng_alignof(uint32_t) * CHAR_BIT
,
1271 lttng_alignof(uint16_t) * CHAR_BIT
1276 * Approximation of NTP time of day to clock monotonic correlation,
1277 * taken at start of trace.
1278 * Yes, this is only an approximation. Yes, we can (and will) do better
1279 * in future versions.
1282 uint64_t measure_clock_offset(void)
1284 uint64_t offset
, monotonic
[2], realtime
;
1285 struct timespec rts
= { 0, 0 };
1286 unsigned long flags
;
1288 /* Disable interrupts to increase correlation precision. */
1289 local_irq_save(flags
);
1290 monotonic
[0] = trace_clock_read64();
1291 getnstimeofday(&rts
);
1292 monotonic
[1] = trace_clock_read64();
1293 local_irq_restore(flags
);
1295 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1296 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1297 realtime
+= rts
.tv_nsec
;
1298 offset
= realtime
- offset
;
1303 * Output metadata into this session's metadata buffers.
1304 * Must be called with sessions_mutex held.
1307 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1309 unsigned char *uuid_c
= session
->uuid
.b
;
1310 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1311 struct lttng_channel
*chan
;
1312 struct lttng_event
*event
;
1315 if (!ACCESS_ONCE(session
->active
))
1317 if (session
->metadata_dumped
)
1320 snprintf(uuid_s
, sizeof(uuid_s
),
1321 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1322 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1323 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1324 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1325 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1327 ret
= lttng_metadata_printf(session
,
1328 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1329 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1330 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1331 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1332 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1333 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1334 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1340 " byte_order = %s;\n"
1341 " packet.header := struct {\n"
1342 " uint32_t magic;\n"
1343 " uint8_t uuid[16];\n"
1344 " uint32_t stream_id;\n"
1347 lttng_alignof(uint8_t) * CHAR_BIT
,
1348 lttng_alignof(uint16_t) * CHAR_BIT
,
1349 lttng_alignof(uint32_t) * CHAR_BIT
,
1350 lttng_alignof(uint64_t) * CHAR_BIT
,
1351 sizeof(unsigned long) * CHAR_BIT
,
1352 lttng_alignof(unsigned long) * CHAR_BIT
,
1365 ret
= lttng_metadata_printf(session
,
1367 " hostname = \"%s\";\n"
1368 " domain = \"kernel\";\n"
1369 " sysname = \"%s\";\n"
1370 " kernel_release = \"%s\";\n"
1371 " kernel_version = \"%s\";\n"
1372 " tracer_name = \"lttng-modules\";\n"
1373 " tracer_major = %d;\n"
1374 " tracer_minor = %d;\n"
1375 " tracer_patchlevel = %d;\n"
1377 current
->nsproxy
->uts_ns
->name
.nodename
,
1381 LTTNG_MODULES_MAJOR_VERSION
,
1382 LTTNG_MODULES_MINOR_VERSION
,
1383 LTTNG_MODULES_PATCHLEVEL_VERSION
1388 ret
= lttng_metadata_printf(session
,
1396 if (!trace_clock_uuid(clock_uuid_s
)) {
1397 ret
= lttng_metadata_printf(session
,
1398 " uuid = \"%s\";\n",
1405 ret
= lttng_metadata_printf(session
,
1406 " description = \"Monotonic Clock\";\n"
1407 " freq = %llu; /* Frequency, in Hz */\n"
1408 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1411 (unsigned long long) trace_clock_freq(),
1412 (unsigned long long) measure_clock_offset()
1417 ret
= lttng_metadata_printf(session
,
1418 "typealias integer {\n"
1419 " size = 27; align = 1; signed = false;\n"
1420 " map = clock.monotonic.value;\n"
1421 "} := uint27_clock_monotonic_t;\n"
1423 "typealias integer {\n"
1424 " size = 32; align = %u; signed = false;\n"
1425 " map = clock.monotonic.value;\n"
1426 "} := uint32_clock_monotonic_t;\n"
1428 "typealias integer {\n"
1429 " size = 64; align = %u; signed = false;\n"
1430 " map = clock.monotonic.value;\n"
1431 "} := uint64_clock_monotonic_t;\n\n",
1432 lttng_alignof(uint32_t) * CHAR_BIT
,
1433 lttng_alignof(uint64_t) * CHAR_BIT
1438 ret
= _lttng_stream_packet_context_declare(session
);
1442 ret
= _lttng_event_header_declare(session
);
1447 list_for_each_entry(chan
, &session
->chan
, list
) {
1448 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1453 list_for_each_entry(event
, &session
->events
, list
) {
1454 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1458 session
->metadata_dumped
= 1;
1464 * lttng_transport_register - LTT transport registration
1465 * @transport: transport structure
1467 * Registers a transport which can be used as output to extract the data out of
1468 * LTTng. The module calling this registration function must ensure that no
1469 * trap-inducing code will be executed by the transport functions. E.g.
1470 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1471 * is made visible to the transport function. This registration acts as a
1472 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1473 * after its registration must it synchronize the TLBs.
1475 void lttng_transport_register(struct lttng_transport
*transport
)
1478 * Make sure no page fault can be triggered by the module about to be
1479 * registered. We deal with this here so we don't have to call
1480 * vmalloc_sync_all() in each module's init.
1482 wrapper_vmalloc_sync_all();
1484 mutex_lock(&sessions_mutex
);
1485 list_add_tail(&transport
->node
, <tng_transport_list
);
1486 mutex_unlock(&sessions_mutex
);
1488 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1491 * lttng_transport_unregister - LTT transport unregistration
1492 * @transport: transport structure
1494 void lttng_transport_unregister(struct lttng_transport
*transport
)
1496 mutex_lock(&sessions_mutex
);
1497 list_del(&transport
->node
);
1498 mutex_unlock(&sessions_mutex
);
1500 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1502 static int __init
lttng_events_init(void)
1506 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
1510 ret
= lttng_tracepoint_init();
1513 event_cache
= KMEM_CACHE(lttng_event
, 0);
1518 ret
= lttng_abi_init();
1521 ret
= lttng_logger_init();
1529 kmem_cache_destroy(event_cache
);
1531 lttng_tracepoint_exit();
1535 module_init(lttng_events_init
);
1537 static void __exit
lttng_events_exit(void)
1539 struct lttng_session
*session
, *tmpsession
;
1541 lttng_logger_exit();
1543 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1544 lttng_session_destroy(session
);
1545 kmem_cache_destroy(event_cache
);
1546 lttng_tracepoint_exit();
1549 module_exit(lttng_events_exit
);
1551 MODULE_LICENSE("GPL and additional rights");
1552 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1553 MODULE_DESCRIPTION("LTTng Events");
1554 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
1555 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
1556 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
1557 LTTNG_MODULES_EXTRAVERSION
);