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/mutex.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/utsname.h>
29 #include <linux/err.h>
30 #include <linux/seq_file.h>
31 #include <linux/file.h>
32 #include <linux/anon_inodes.h>
33 #include "wrapper/file.h"
34 #include <linux/jhash.h>
36 #include "wrapper/uuid.h"
37 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
38 #include "wrapper/random.h"
39 #include "wrapper/tracepoint.h"
40 #include "wrapper/list.h"
41 #include "lttng-kernel-version.h"
42 #include "lttng-events.h"
43 #include "lttng-tracer.h"
44 #include "lttng-abi-old.h"
46 #define METADATA_CACHE_DEFAULT_SIZE 4096
48 static LIST_HEAD(sessions
);
49 static LIST_HEAD(lttng_transport_list
);
51 * Protect the sessions and metadata caches.
53 static DEFINE_MUTEX(sessions_mutex
);
54 static struct kmem_cache
*event_cache
;
56 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
57 static void lttng_session_sync_enablers(struct lttng_session
*session
);
58 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
60 static void _lttng_event_destroy(struct lttng_event
*event
);
61 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
62 static int _lttng_event_unregister(struct lttng_event
*event
);
64 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
65 struct lttng_channel
*chan
,
66 struct lttng_event
*event
);
68 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
70 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
72 void synchronize_trace(void)
75 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
76 #ifdef CONFIG_PREEMPT_RT_FULL
79 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
80 #ifdef CONFIG_PREEMPT_RT
83 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
86 void lttng_lock_sessions(void)
88 mutex_lock(&sessions_mutex
);
91 void lttng_unlock_sessions(void)
93 mutex_unlock(&sessions_mutex
);
97 * Called with sessions lock held.
99 int lttng_session_active(void)
101 struct lttng_session
*iter
;
103 list_for_each_entry(iter
, &sessions
, list
) {
110 struct lttng_session
*lttng_session_create(void)
112 struct lttng_session
*session
;
113 struct lttng_metadata_cache
*metadata_cache
;
116 mutex_lock(&sessions_mutex
);
117 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
120 INIT_LIST_HEAD(&session
->chan
);
121 INIT_LIST_HEAD(&session
->events
);
122 uuid_le_gen(&session
->uuid
);
124 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
127 goto err_free_session
;
128 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
130 if (!metadata_cache
->data
)
132 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
133 kref_init(&metadata_cache
->refcount
);
134 session
->metadata_cache
= metadata_cache
;
135 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
136 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
137 sizeof(metadata_cache
->uuid
));
138 INIT_LIST_HEAD(&session
->enablers_head
);
139 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
140 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
141 list_add(&session
->list
, &sessions
);
142 mutex_unlock(&sessions_mutex
);
146 kfree(metadata_cache
);
150 mutex_unlock(&sessions_mutex
);
154 void metadata_cache_destroy(struct kref
*kref
)
156 struct lttng_metadata_cache
*cache
=
157 container_of(kref
, struct lttng_metadata_cache
, refcount
);
162 void lttng_session_destroy(struct lttng_session
*session
)
164 struct lttng_channel
*chan
, *tmpchan
;
165 struct lttng_event
*event
, *tmpevent
;
166 struct lttng_metadata_stream
*metadata_stream
;
167 struct lttng_enabler
*enabler
, *tmpenabler
;
170 mutex_lock(&sessions_mutex
);
171 ACCESS_ONCE(session
->active
) = 0;
172 list_for_each_entry(chan
, &session
->chan
, list
) {
173 ret
= lttng_syscalls_unregister(chan
);
176 list_for_each_entry(event
, &session
->events
, list
) {
177 ret
= _lttng_event_unregister(event
);
180 synchronize_trace(); /* Wait for in-flight events to complete */
181 list_for_each_entry_safe(enabler
, tmpenabler
,
182 &session
->enablers_head
, node
)
183 lttng_enabler_destroy(enabler
);
184 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
185 _lttng_event_destroy(event
);
186 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
187 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
188 _lttng_channel_destroy(chan
);
190 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
191 _lttng_metadata_channel_hangup(metadata_stream
);
192 if (session
->pid_tracker
)
193 lttng_pid_tracker_destroy(session
->pid_tracker
);
194 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
195 list_del(&session
->list
);
196 mutex_unlock(&sessions_mutex
);
200 int lttng_session_enable(struct lttng_session
*session
)
203 struct lttng_channel
*chan
;
205 mutex_lock(&sessions_mutex
);
206 if (session
->active
) {
211 /* Set transient enabler state to "enabled" */
215 * Snapshot the number of events per channel to know the type of header
218 list_for_each_entry(chan
, &session
->chan
, list
) {
219 if (chan
->header_type
)
220 continue; /* don't change it if session stop/restart */
221 if (chan
->free_event_id
< 31)
222 chan
->header_type
= 1; /* compact */
224 chan
->header_type
= 2; /* large */
227 /* We need to sync enablers with session before activation. */
228 lttng_session_sync_enablers(session
);
230 ACCESS_ONCE(session
->active
) = 1;
231 ACCESS_ONCE(session
->been_active
) = 1;
232 ret
= _lttng_session_metadata_statedump(session
);
234 ACCESS_ONCE(session
->active
) = 0;
237 ret
= lttng_statedump_start(session
);
239 ACCESS_ONCE(session
->active
) = 0;
241 mutex_unlock(&sessions_mutex
);
245 int lttng_session_disable(struct lttng_session
*session
)
249 mutex_lock(&sessions_mutex
);
250 if (!session
->active
) {
254 ACCESS_ONCE(session
->active
) = 0;
256 /* Set transient enabler state to "disabled" */
258 lttng_session_sync_enablers(session
);
260 mutex_unlock(&sessions_mutex
);
264 int lttng_channel_enable(struct lttng_channel
*channel
)
268 mutex_lock(&sessions_mutex
);
269 if (channel
->channel_type
== METADATA_CHANNEL
) {
273 if (channel
->enabled
) {
277 /* Set transient enabler state to "enabled" */
279 lttng_session_sync_enablers(channel
->session
);
280 /* Set atomically the state to "enabled" */
281 ACCESS_ONCE(channel
->enabled
) = 1;
283 mutex_unlock(&sessions_mutex
);
287 int lttng_channel_disable(struct lttng_channel
*channel
)
291 mutex_lock(&sessions_mutex
);
292 if (channel
->channel_type
== METADATA_CHANNEL
) {
296 if (!channel
->enabled
) {
300 /* Set atomically the state to "disabled" */
301 ACCESS_ONCE(channel
->enabled
) = 0;
302 /* Set transient enabler state to "enabled" */
304 lttng_session_sync_enablers(channel
->session
);
306 mutex_unlock(&sessions_mutex
);
310 int lttng_event_enable(struct lttng_event
*event
)
314 mutex_lock(&sessions_mutex
);
315 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
319 if (event
->enabled
) {
323 ACCESS_ONCE(event
->enabled
) = 1;
324 lttng_session_sync_enablers(event
->chan
->session
);
326 mutex_unlock(&sessions_mutex
);
330 int lttng_event_disable(struct lttng_event
*event
)
334 mutex_lock(&sessions_mutex
);
335 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
339 if (!event
->enabled
) {
343 ACCESS_ONCE(event
->enabled
) = 0;
344 lttng_session_sync_enablers(event
->chan
->session
);
346 mutex_unlock(&sessions_mutex
);
350 static struct lttng_transport
*lttng_transport_find(const char *name
)
352 struct lttng_transport
*transport
;
354 list_for_each_entry(transport
, <tng_transport_list
, node
) {
355 if (!strcmp(transport
->name
, name
))
361 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
362 const char *transport_name
,
364 size_t subbuf_size
, size_t num_subbuf
,
365 unsigned int switch_timer_interval
,
366 unsigned int read_timer_interval
,
367 enum channel_type channel_type
)
369 struct lttng_channel
*chan
;
370 struct lttng_transport
*transport
= NULL
;
372 mutex_lock(&sessions_mutex
);
373 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
374 goto active
; /* Refuse to add channel to active session */
375 transport
= lttng_transport_find(transport_name
);
377 printk(KERN_WARNING
"LTTng transport %s not found\n",
381 if (!try_module_get(transport
->owner
)) {
382 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
385 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
388 chan
->session
= session
;
389 chan
->id
= session
->free_chan_id
++;
390 chan
->ops
= &transport
->ops
;
392 * Note: the channel creation op already writes into the packet
393 * headers. Therefore the "chan" information used as input
394 * should be already accessible.
396 chan
->chan
= transport
->ops
.channel_create(transport_name
,
397 chan
, buf_addr
, subbuf_size
, num_subbuf
,
398 switch_timer_interval
, read_timer_interval
);
403 chan
->transport
= transport
;
404 chan
->channel_type
= channel_type
;
405 list_add(&chan
->list
, &session
->chan
);
406 mutex_unlock(&sessions_mutex
);
413 module_put(transport
->owner
);
416 mutex_unlock(&sessions_mutex
);
421 * Only used internally at session destruction for per-cpu channels, and
422 * when metadata channel is released.
423 * Needs to be called with sessions mutex held.
426 void _lttng_channel_destroy(struct lttng_channel
*chan
)
428 chan
->ops
->channel_destroy(chan
->chan
);
429 module_put(chan
->transport
->owner
);
430 list_del(&chan
->list
);
431 lttng_destroy_context(chan
->ctx
);
435 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
437 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
439 /* Protect the metadata cache with the sessions_mutex. */
440 mutex_lock(&sessions_mutex
);
441 _lttng_channel_destroy(chan
);
442 mutex_unlock(&sessions_mutex
);
444 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
447 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
449 stream
->finalized
= 1;
450 wake_up_interruptible(&stream
->read_wait
);
454 * Supports event creation while tracing session is active.
455 * Needs to be called with sessions mutex held.
457 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
458 struct lttng_kernel_event
*event_param
,
460 const struct lttng_event_desc
*event_desc
,
461 enum lttng_kernel_instrumentation itype
)
463 struct lttng_session
*session
= chan
->session
;
464 struct lttng_event
*event
;
465 const char *event_name
;
466 struct hlist_head
*head
;
471 if (chan
->free_event_id
== -1U) {
477 case LTTNG_KERNEL_TRACEPOINT
:
478 event_name
= event_desc
->name
;
480 case LTTNG_KERNEL_KPROBE
:
481 case LTTNG_KERNEL_KRETPROBE
:
482 case LTTNG_KERNEL_FUNCTION
:
483 case LTTNG_KERNEL_NOOP
:
484 case LTTNG_KERNEL_SYSCALL
:
485 event_name
= event_param
->name
;
492 name_len
= strlen(event_name
);
493 hash
= jhash(event_name
, name_len
, 0);
494 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
495 hlist_for_each_entry(event
, head
, hlist
) {
496 WARN_ON_ONCE(!event
->desc
);
497 if (!strncmp(event
->desc
->name
, event_name
,
498 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
499 && chan
== event
->chan
) {
505 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
511 event
->filter
= filter
;
512 event
->id
= chan
->free_event_id
++;
513 event
->instrumentation
= itype
;
514 event
->evtype
= LTTNG_TYPE_EVENT
;
515 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
516 INIT_LIST_HEAD(&event
->enablers_ref_head
);
519 case LTTNG_KERNEL_TRACEPOINT
:
520 /* Event will be enabled by enabler sync. */
522 event
->registered
= 0;
523 event
->desc
= lttng_event_get(event_name
);
528 /* Populate lttng_event structure before event registration. */
531 case LTTNG_KERNEL_KPROBE
:
533 event
->registered
= 1;
535 * Populate lttng_event structure before event
539 ret
= lttng_kprobes_register(event_name
,
540 event_param
->u
.kprobe
.symbol_name
,
541 event_param
->u
.kprobe
.offset
,
542 event_param
->u
.kprobe
.addr
,
548 ret
= try_module_get(event
->desc
->owner
);
551 case LTTNG_KERNEL_KRETPROBE
:
553 struct lttng_event
*event_return
;
555 /* kretprobe defines 2 events */
557 event
->registered
= 1;
559 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
564 event_return
->chan
= chan
;
565 event_return
->filter
= filter
;
566 event_return
->id
= chan
->free_event_id
++;
567 event_return
->enabled
= 1;
568 event_return
->registered
= 1;
569 event_return
->instrumentation
= itype
;
571 * Populate lttng_event structure before kretprobe registration.
574 ret
= lttng_kretprobes_register(event_name
,
575 event_param
->u
.kretprobe
.symbol_name
,
576 event_param
->u
.kretprobe
.offset
,
577 event_param
->u
.kretprobe
.addr
,
578 event
, event_return
);
580 kmem_cache_free(event_cache
, event_return
);
584 /* Take 2 refs on the module: one per event. */
585 ret
= try_module_get(event
->desc
->owner
);
587 ret
= try_module_get(event
->desc
->owner
);
589 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
591 WARN_ON_ONCE(ret
> 0);
593 kmem_cache_free(event_cache
, event_return
);
594 module_put(event
->desc
->owner
);
595 module_put(event
->desc
->owner
);
596 goto statedump_error
;
598 list_add(&event_return
->list
, &chan
->session
->events
);
601 case LTTNG_KERNEL_FUNCTION
:
603 event
->registered
= 1;
605 * Populate lttng_event structure before event
609 ret
= lttng_ftrace_register(event_name
,
610 event_param
->u
.ftrace
.symbol_name
,
615 ret
= try_module_get(event
->desc
->owner
);
618 case LTTNG_KERNEL_NOOP
:
619 case LTTNG_KERNEL_SYSCALL
:
621 event
->registered
= 0;
622 event
->desc
= event_desc
;
633 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
634 WARN_ON_ONCE(ret
> 0);
636 goto statedump_error
;
638 hlist_add_head(&event
->hlist
, head
);
639 list_add(&event
->list
, &chan
->session
->events
);
640 mutex_unlock(&sessions_mutex
);
644 /* If a statedump error occurs, events will not be readable. */
646 kmem_cache_free(event_cache
, event
);
654 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
655 struct lttng_kernel_event
*event_param
,
657 const struct lttng_event_desc
*event_desc
,
658 enum lttng_kernel_instrumentation itype
)
660 struct lttng_event
*event
;
662 mutex_lock(&sessions_mutex
);
663 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
665 mutex_unlock(&sessions_mutex
);
669 /* Only used for tracepoints for now. */
671 void register_event(struct lttng_event
*event
)
673 const struct lttng_event_desc
*desc
;
676 if (event
->registered
)
680 switch (event
->instrumentation
) {
681 case LTTNG_KERNEL_TRACEPOINT
:
682 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
683 desc
->probe_callback
,
686 case LTTNG_KERNEL_SYSCALL
:
687 ret
= lttng_syscall_filter_enable(event
->chan
,
690 case LTTNG_KERNEL_KPROBE
:
691 case LTTNG_KERNEL_KRETPROBE
:
692 case LTTNG_KERNEL_FUNCTION
:
693 case LTTNG_KERNEL_NOOP
:
700 event
->registered
= 1;
704 * Only used internally at session destruction.
706 int _lttng_event_unregister(struct lttng_event
*event
)
708 const struct lttng_event_desc
*desc
;
711 if (!event
->registered
)
715 switch (event
->instrumentation
) {
716 case LTTNG_KERNEL_TRACEPOINT
:
717 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
718 event
->desc
->probe_callback
,
721 case LTTNG_KERNEL_KPROBE
:
722 lttng_kprobes_unregister(event
);
725 case LTTNG_KERNEL_KRETPROBE
:
726 lttng_kretprobes_unregister(event
);
729 case LTTNG_KERNEL_FUNCTION
:
730 lttng_ftrace_unregister(event
);
733 case LTTNG_KERNEL_SYSCALL
:
734 ret
= lttng_syscall_filter_disable(event
->chan
,
737 case LTTNG_KERNEL_NOOP
:
744 event
->registered
= 0;
749 * Only used internally at session destruction.
752 void _lttng_event_destroy(struct lttng_event
*event
)
754 switch (event
->instrumentation
) {
755 case LTTNG_KERNEL_TRACEPOINT
:
756 lttng_event_put(event
->desc
);
758 case LTTNG_KERNEL_KPROBE
:
759 module_put(event
->desc
->owner
);
760 lttng_kprobes_destroy_private(event
);
762 case LTTNG_KERNEL_KRETPROBE
:
763 module_put(event
->desc
->owner
);
764 lttng_kretprobes_destroy_private(event
);
766 case LTTNG_KERNEL_FUNCTION
:
767 module_put(event
->desc
->owner
);
768 lttng_ftrace_destroy_private(event
);
770 case LTTNG_KERNEL_NOOP
:
771 case LTTNG_KERNEL_SYSCALL
:
776 list_del(&event
->list
);
777 lttng_destroy_context(event
->ctx
);
778 kmem_cache_free(event_cache
, event
);
781 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
787 mutex_lock(&sessions_mutex
);
789 /* track all pids: destroy tracker. */
790 if (session
->pid_tracker
) {
791 struct lttng_pid_tracker
*lpf
;
793 lpf
= session
->pid_tracker
;
794 rcu_assign_pointer(session
->pid_tracker
, NULL
);
796 lttng_pid_tracker_destroy(lpf
);
800 if (!session
->pid_tracker
) {
801 struct lttng_pid_tracker
*lpf
;
803 lpf
= lttng_pid_tracker_create();
808 ret
= lttng_pid_tracker_add(lpf
, pid
);
809 rcu_assign_pointer(session
->pid_tracker
, lpf
);
811 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
815 mutex_unlock(&sessions_mutex
);
819 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
825 mutex_lock(&sessions_mutex
);
827 /* untrack all pids: replace by empty tracker. */
828 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
829 struct lttng_pid_tracker
*lpf
;
831 lpf
= lttng_pid_tracker_create();
836 rcu_assign_pointer(session
->pid_tracker
, lpf
);
839 lttng_pid_tracker_destroy(old_lpf
);
842 if (!session
->pid_tracker
) {
846 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
849 mutex_unlock(&sessions_mutex
);
854 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
856 struct lttng_session
*session
= m
->private;
857 struct lttng_pid_tracker
*lpf
;
858 struct lttng_pid_hash_node
*e
;
861 mutex_lock(&sessions_mutex
);
862 lpf
= session
->pid_tracker
;
864 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
865 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
867 lttng_hlist_for_each_entry(e
, head
, hlist
) {
873 /* PID tracker disabled. */
874 if (iter
>= *pos
&& iter
== 0) {
875 return session
; /* empty tracker */
883 /* Called with sessions_mutex held. */
885 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
887 struct lttng_session
*session
= m
->private;
888 struct lttng_pid_tracker
*lpf
;
889 struct lttng_pid_hash_node
*e
;
893 lpf
= session
->pid_tracker
;
895 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
896 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
898 lttng_hlist_for_each_entry(e
, head
, hlist
) {
904 /* PID tracker disabled. */
905 if (iter
>= *ppos
&& iter
== 0)
906 return session
; /* empty tracker */
915 void pid_list_stop(struct seq_file
*m
, void *p
)
917 mutex_unlock(&sessions_mutex
);
921 int pid_list_show(struct seq_file
*m
, void *p
)
925 if (p
== m
->private) {
926 /* Tracker disabled. */
929 const struct lttng_pid_hash_node
*e
= p
;
931 pid
= lttng_pid_tracker_get_node_pid(e
);
933 seq_printf(m
, "process { pid = %d; };\n", pid
);
938 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
939 .start
= pid_list_start
,
940 .next
= pid_list_next
,
941 .stop
= pid_list_stop
,
942 .show
= pid_list_show
,
946 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
948 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
952 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
954 struct seq_file
*m
= file
->private_data
;
955 struct lttng_session
*session
= m
->private;
958 WARN_ON_ONCE(!session
);
959 ret
= seq_release(inode
, file
);
965 const struct file_operations lttng_tracker_pids_list_fops
= {
966 .owner
= THIS_MODULE
,
967 .open
= lttng_tracker_pids_list_open
,
970 .release
= lttng_tracker_pids_list_release
,
973 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
975 struct file
*tracker_pids_list_file
;
979 file_fd
= lttng_get_unused_fd();
985 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
986 <tng_tracker_pids_list_fops
,
988 if (IS_ERR(tracker_pids_list_file
)) {
989 ret
= PTR_ERR(tracker_pids_list_file
);
992 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
995 m
= tracker_pids_list_file
->private_data
;
996 m
->private = session
;
997 fd_install(file_fd
, tracker_pids_list_file
);
998 atomic_long_inc(&session
->file
->f_count
);
1003 fput(tracker_pids_list_file
);
1005 put_unused_fd(file_fd
);
1011 * Enabler management.
1014 int lttng_match_enabler_wildcard(const char *desc_name
,
1017 /* Compare excluding final '*' */
1018 if (strncmp(desc_name
, name
, strlen(name
) - 1))
1024 int lttng_match_enabler_name(const char *desc_name
,
1027 if (strcmp(desc_name
, name
))
1033 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1034 struct lttng_enabler
*enabler
)
1036 const char *desc_name
, *enabler_name
;
1038 enabler_name
= enabler
->event_param
.name
;
1039 switch (enabler
->event_param
.instrumentation
) {
1040 case LTTNG_KERNEL_TRACEPOINT
:
1041 desc_name
= desc
->name
;
1043 case LTTNG_KERNEL_SYSCALL
:
1044 desc_name
= desc
->name
;
1045 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1046 desc_name
+= strlen("compat_");
1047 if (!strncmp(desc_name
, "syscall_exit_",
1048 strlen("syscall_exit_"))) {
1049 desc_name
+= strlen("syscall_exit_");
1050 } else if (!strncmp(desc_name
, "syscall_entry_",
1051 strlen("syscall_entry_"))) {
1052 desc_name
+= strlen("syscall_entry_");
1062 switch (enabler
->type
) {
1063 case LTTNG_ENABLER_WILDCARD
:
1064 return lttng_match_enabler_wildcard(desc_name
, enabler_name
);
1065 case LTTNG_ENABLER_NAME
:
1066 return lttng_match_enabler_name(desc_name
, enabler_name
);
1073 int lttng_event_match_enabler(struct lttng_event
*event
,
1074 struct lttng_enabler
*enabler
)
1076 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1078 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1079 && event
->chan
== enabler
->chan
)
1086 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1087 struct lttng_enabler
*enabler
)
1089 struct lttng_enabler_ref
*enabler_ref
;
1091 list_for_each_entry(enabler_ref
,
1092 &event
->enablers_ref_head
, node
) {
1093 if (enabler_ref
->ref
== enabler
)
1100 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1102 struct lttng_session
*session
= enabler
->chan
->session
;
1103 struct lttng_probe_desc
*probe_desc
;
1104 const struct lttng_event_desc
*desc
;
1106 struct list_head
*probe_list
;
1108 probe_list
= lttng_get_probe_list_head();
1110 * For each probe event, if we find that a probe event matches
1111 * our enabler, create an associated lttng_event if not
1114 list_for_each_entry(probe_desc
, probe_list
, head
) {
1115 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1117 struct hlist_head
*head
;
1118 const char *event_name
;
1121 struct lttng_event
*event
;
1123 desc
= probe_desc
->event_desc
[i
];
1124 if (!lttng_desc_match_enabler(desc
, enabler
))
1126 event_name
= desc
->name
;
1127 name_len
= strlen(event_name
);
1130 * Check if already created.
1132 hash
= jhash(event_name
, name_len
, 0);
1133 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1134 hlist_for_each_entry(event
, head
, hlist
) {
1135 if (event
->desc
== desc
1136 && event
->chan
== enabler
->chan
)
1143 * We need to create an event for this
1146 event
= _lttng_event_create(enabler
->chan
,
1148 LTTNG_KERNEL_TRACEPOINT
);
1150 printk(KERN_INFO
"Unable to create event %s\n",
1151 probe_desc
->event_desc
[i
]->name
);
1158 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1162 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1167 * Create struct lttng_event if it is missing and present in the list of
1168 * tracepoint probes.
1169 * Should be called with sessions mutex held.
1172 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1174 switch (enabler
->event_param
.instrumentation
) {
1175 case LTTNG_KERNEL_TRACEPOINT
:
1176 lttng_create_tracepoint_if_missing(enabler
);
1178 case LTTNG_KERNEL_SYSCALL
:
1179 lttng_create_syscall_if_missing(enabler
);
1188 * Create events associated with an enabler (if not already present),
1189 * and add backward reference from the event to the enabler.
1190 * Should be called with sessions mutex held.
1193 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1195 struct lttng_session
*session
= enabler
->chan
->session
;
1196 struct lttng_event
*event
;
1198 /* First ensure that probe events are created for this enabler. */
1199 lttng_create_event_if_missing(enabler
);
1201 /* For each event matching enabler in session event list. */
1202 list_for_each_entry(event
, &session
->events
, list
) {
1203 struct lttng_enabler_ref
*enabler_ref
;
1205 if (!lttng_event_match_enabler(event
, enabler
))
1207 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1210 * If no backward ref, create it.
1211 * Add backward ref from event to enabler.
1213 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1216 enabler_ref
->ref
= enabler
;
1217 list_add(&enabler_ref
->node
,
1218 &event
->enablers_ref_head
);
1222 * Link filter bytecodes if not linked yet.
1224 lttng_enabler_event_link_bytecode(event
, enabler
);
1226 /* TODO: merge event context. */
1232 * Called at module load: connect the probe on all enablers matching
1234 * Called with sessions lock held.
1236 int lttng_fix_pending_events(void)
1238 struct lttng_session
*session
;
1240 list_for_each_entry(session
, &sessions
, list
)
1241 lttng_session_lazy_sync_enablers(session
);
1245 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1246 struct lttng_kernel_event
*event_param
,
1247 struct lttng_channel
*chan
)
1249 struct lttng_enabler
*enabler
;
1251 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1254 enabler
->type
= type
;
1255 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1256 memcpy(&enabler
->event_param
, event_param
,
1257 sizeof(enabler
->event_param
));
1258 enabler
->chan
= chan
;
1260 enabler
->enabled
= 0;
1261 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1262 mutex_lock(&sessions_mutex
);
1263 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1264 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1265 mutex_unlock(&sessions_mutex
);
1269 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1271 mutex_lock(&sessions_mutex
);
1272 enabler
->enabled
= 1;
1273 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1274 mutex_unlock(&sessions_mutex
);
1278 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1280 mutex_lock(&sessions_mutex
);
1281 enabler
->enabled
= 0;
1282 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1283 mutex_unlock(&sessions_mutex
);
1287 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1288 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1290 struct lttng_filter_bytecode_node
*bytecode_node
;
1291 uint32_t bytecode_len
;
1294 ret
= get_user(bytecode_len
, &bytecode
->len
);
1297 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1301 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1302 sizeof(*bytecode
) + bytecode_len
);
1305 bytecode_node
->enabler
= enabler
;
1306 /* Enforce length based on allocated size */
1307 bytecode_node
->bc
.len
= bytecode_len
;
1308 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1309 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1313 kfree(bytecode_node
);
1317 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1318 struct lttng_kernel_context
*context_param
)
1324 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1326 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1328 /* Destroy filter bytecode */
1329 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1330 &enabler
->filter_bytecode_head
, node
) {
1334 /* Destroy contexts */
1335 lttng_destroy_context(enabler
->ctx
);
1337 list_del(&enabler
->node
);
1342 * lttng_session_sync_enablers should be called just before starting a
1344 * Should be called with sessions mutex held.
1347 void lttng_session_sync_enablers(struct lttng_session
*session
)
1349 struct lttng_enabler
*enabler
;
1350 struct lttng_event
*event
;
1352 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1353 lttng_enabler_ref_events(enabler
);
1355 * For each event, if at least one of its enablers is enabled,
1356 * and its channel and session transient states are enabled, we
1357 * enable the event, else we disable it.
1359 list_for_each_entry(event
, &session
->events
, list
) {
1360 struct lttng_enabler_ref
*enabler_ref
;
1361 struct lttng_bytecode_runtime
*runtime
;
1362 int enabled
= 0, has_enablers_without_bytecode
= 0;
1364 switch (event
->instrumentation
) {
1365 case LTTNG_KERNEL_TRACEPOINT
:
1366 case LTTNG_KERNEL_SYSCALL
:
1368 list_for_each_entry(enabler_ref
,
1369 &event
->enablers_ref_head
, node
) {
1370 if (enabler_ref
->ref
->enabled
) {
1377 /* Not handled with lazy sync. */
1381 * Enabled state is based on union of enablers, with
1382 * intesection of session and channel transient enable
1385 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1387 ACCESS_ONCE(event
->enabled
) = enabled
;
1389 * Sync tracepoint registration with event enabled
1393 register_event(event
);
1395 _lttng_event_unregister(event
);
1398 /* Check if has enablers without bytecode enabled */
1399 list_for_each_entry(enabler_ref
,
1400 &event
->enablers_ref_head
, node
) {
1401 if (enabler_ref
->ref
->enabled
1402 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1403 has_enablers_without_bytecode
= 1;
1407 event
->has_enablers_without_bytecode
=
1408 has_enablers_without_bytecode
;
1410 /* Enable filters */
1411 list_for_each_entry(runtime
,
1412 &event
->bytecode_runtime_head
, node
)
1413 lttng_filter_sync_state(runtime
);
1418 * Apply enablers to session events, adding events to session if need
1419 * be. It is required after each modification applied to an active
1420 * session, and right before session "start".
1421 * "lazy" sync means we only sync if required.
1422 * Should be called with sessions mutex held.
1425 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1427 /* We can skip if session is not active */
1428 if (!session
->active
)
1430 lttng_session_sync_enablers(session
);
1434 * Serialize at most one packet worth of metadata into a metadata
1436 * We have exclusive access to our metadata buffer (protected by the
1437 * sessions_mutex), so we can do racy operations such as looking for
1438 * remaining space left in packet and write, since mutual exclusion
1439 * protects us from concurrent writes.
1440 * Returns the number of bytes written in the channel, 0 if no data
1441 * was written and a negative value on error.
1443 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1444 struct channel
*chan
)
1446 struct lib_ring_buffer_ctx ctx
;
1448 size_t len
, reserve_len
;
1451 * Ensure we support mutiple get_next / put sequences followed
1452 * by put_next. The metadata stream lock internally protects
1453 * reading the metadata cache. It can indeed be read
1454 * concurrently by "get_next_subbuf" and "flush" operations on
1455 * the buffer invoked by different processes.
1457 mutex_lock(&stream
->lock
);
1458 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1459 if (stream
->metadata_in
!= stream
->metadata_out
)
1462 len
= stream
->metadata_cache
->metadata_written
-
1463 stream
->metadata_in
;
1466 reserve_len
= min_t(size_t,
1467 stream
->transport
->ops
.packet_avail_size(chan
),
1469 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1472 * If reservation failed, return an error to the caller.
1474 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1476 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1479 stream
->transport
->ops
.event_write(&ctx
,
1480 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1482 stream
->transport
->ops
.event_commit(&ctx
);
1483 stream
->metadata_in
+= reserve_len
;
1487 mutex_unlock(&stream
->lock
);
1492 * Write the metadata to the metadata cache.
1493 * Must be called with sessions_mutex held.
1495 int lttng_metadata_printf(struct lttng_session
*session
,
1496 const char *fmt
, ...)
1501 struct lttng_metadata_stream
*stream
;
1503 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
1506 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1512 if (session
->metadata_cache
->metadata_written
+ len
>
1513 session
->metadata_cache
->cache_alloc
) {
1514 char *tmp_cache_realloc
;
1515 unsigned int tmp_cache_alloc_size
;
1517 tmp_cache_alloc_size
= max_t(unsigned int,
1518 session
->metadata_cache
->cache_alloc
+ len
,
1519 session
->metadata_cache
->cache_alloc
<< 1);
1520 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
1521 tmp_cache_alloc_size
, GFP_KERNEL
);
1522 if (!tmp_cache_realloc
)
1524 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1525 session
->metadata_cache
->data
= tmp_cache_realloc
;
1527 memcpy(session
->metadata_cache
->data
+
1528 session
->metadata_cache
->metadata_written
,
1530 session
->metadata_cache
->metadata_written
+= len
;
1533 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1534 wake_up_interruptible(&stream
->read_wait
);
1544 * Must be called with sessions_mutex held.
1547 int _lttng_field_statedump(struct lttng_session
*session
,
1548 const struct lttng_event_field
*field
)
1552 switch (field
->type
.atype
) {
1554 ret
= lttng_metadata_printf(session
,
1555 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1556 field
->type
.u
.basic
.integer
.size
,
1557 field
->type
.u
.basic
.integer
.alignment
,
1558 field
->type
.u
.basic
.integer
.signedness
,
1559 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
1561 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1564 field
->type
.u
.basic
.integer
.base
,
1566 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1568 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1573 ret
= lttng_metadata_printf(session
,
1575 field
->type
.u
.basic
.enumeration
.name
,
1580 const struct lttng_basic_type
*elem_type
;
1582 elem_type
= &field
->type
.u
.array
.elem_type
;
1583 ret
= lttng_metadata_printf(session
,
1584 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1585 elem_type
->u
.basic
.integer
.size
,
1586 elem_type
->u
.basic
.integer
.alignment
,
1587 elem_type
->u
.basic
.integer
.signedness
,
1588 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1590 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1593 elem_type
->u
.basic
.integer
.base
,
1595 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1597 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1599 field
->name
, field
->type
.u
.array
.length
);
1602 case atype_sequence
:
1604 const struct lttng_basic_type
*elem_type
;
1605 const struct lttng_basic_type
*length_type
;
1607 elem_type
= &field
->type
.u
.sequence
.elem_type
;
1608 length_type
= &field
->type
.u
.sequence
.length_type
;
1609 ret
= lttng_metadata_printf(session
,
1610 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1611 length_type
->u
.basic
.integer
.size
,
1612 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
1613 length_type
->u
.basic
.integer
.signedness
,
1614 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1616 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1619 length_type
->u
.basic
.integer
.base
,
1621 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1623 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1629 ret
= lttng_metadata_printf(session
,
1630 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1631 elem_type
->u
.basic
.integer
.size
,
1632 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
1633 elem_type
->u
.basic
.integer
.signedness
,
1634 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1636 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1639 elem_type
->u
.basic
.integer
.base
,
1641 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1643 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1651 /* Default encoding is UTF8 */
1652 ret
= lttng_metadata_printf(session
,
1654 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
1655 " { encoding = ASCII; }" : "",
1666 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
1667 struct lttng_ctx
*ctx
)
1674 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
1675 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
1677 ret
= _lttng_field_statedump(session
, &field
->event_field
);
1685 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
1686 struct lttng_event
*event
)
1688 const struct lttng_event_desc
*desc
= event
->desc
;
1692 for (i
= 0; i
< desc
->nr_fields
; i
++) {
1693 const struct lttng_event_field
*field
= &desc
->fields
[i
];
1695 ret
= _lttng_field_statedump(session
, field
);
1703 * Must be called with sessions_mutex held.
1706 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
1707 struct lttng_channel
*chan
,
1708 struct lttng_event
*event
)
1712 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1714 if (chan
->channel_type
== METADATA_CHANNEL
)
1717 ret
= lttng_metadata_printf(session
,
1721 " stream_id = %u;\n",
1729 ret
= lttng_metadata_printf(session
,
1730 " context := struct {\n");
1734 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
1738 ret
= lttng_metadata_printf(session
,
1744 ret
= lttng_metadata_printf(session
,
1745 " fields := struct {\n"
1750 ret
= _lttng_fields_metadata_statedump(session
, event
);
1755 * LTTng space reservation can only reserve multiples of the
1758 ret
= lttng_metadata_printf(session
,
1764 event
->metadata_dumped
= 1;
1771 * Must be called with sessions_mutex held.
1774 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1775 struct lttng_channel
*chan
)
1779 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1782 if (chan
->channel_type
== METADATA_CHANNEL
)
1785 WARN_ON_ONCE(!chan
->header_type
);
1786 ret
= lttng_metadata_printf(session
,
1789 " event.header := %s;\n"
1790 " packet.context := struct packet_context;\n",
1792 chan
->header_type
== 1 ? "struct event_header_compact" :
1793 "struct event_header_large");
1798 ret
= lttng_metadata_printf(session
,
1799 " event.context := struct {\n");
1803 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1807 ret
= lttng_metadata_printf(session
,
1813 ret
= lttng_metadata_printf(session
,
1816 chan
->metadata_dumped
= 1;
1822 * Must be called with sessions_mutex held.
1825 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1827 return lttng_metadata_printf(session
,
1828 "struct packet_context {\n"
1829 " uint64_clock_monotonic_t timestamp_begin;\n"
1830 " uint64_clock_monotonic_t timestamp_end;\n"
1831 " uint64_t content_size;\n"
1832 " uint64_t packet_size;\n"
1833 " unsigned long events_discarded;\n"
1834 " uint32_t cpu_id;\n"
1841 * id: range: 0 - 30.
1842 * id 31 is reserved to indicate an extended header.
1845 * id: range: 0 - 65534.
1846 * id 65535 is reserved to indicate an extended header.
1848 * Must be called with sessions_mutex held.
1851 int _lttng_event_header_declare(struct lttng_session
*session
)
1853 return lttng_metadata_printf(session
,
1854 "struct event_header_compact {\n"
1855 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1858 " uint27_clock_monotonic_t timestamp;\n"
1862 " uint64_clock_monotonic_t timestamp;\n"
1867 "struct event_header_large {\n"
1868 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1871 " uint32_clock_monotonic_t timestamp;\n"
1875 " uint64_clock_monotonic_t timestamp;\n"
1879 lttng_alignof(uint32_t) * CHAR_BIT
,
1880 lttng_alignof(uint16_t) * CHAR_BIT
1885 * Approximation of NTP time of day to clock monotonic correlation,
1886 * taken at start of trace.
1887 * Yes, this is only an approximation. Yes, we can (and will) do better
1888 * in future versions.
1891 uint64_t measure_clock_offset(void)
1893 uint64_t offset
, monotonic
[2], realtime
;
1894 struct timespec rts
= { 0, 0 };
1895 unsigned long flags
;
1897 /* Disable interrupts to increase correlation precision. */
1898 local_irq_save(flags
);
1899 monotonic
[0] = trace_clock_read64();
1900 getnstimeofday(&rts
);
1901 monotonic
[1] = trace_clock_read64();
1902 local_irq_restore(flags
);
1904 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1905 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1906 realtime
+= rts
.tv_nsec
;
1907 offset
= realtime
- offset
;
1912 * Output metadata into this session's metadata buffers.
1913 * Must be called with sessions_mutex held.
1916 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1918 unsigned char *uuid_c
= session
->uuid
.b
;
1919 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1920 struct lttng_channel
*chan
;
1921 struct lttng_event
*event
;
1924 if (!ACCESS_ONCE(session
->active
))
1926 if (session
->metadata_dumped
)
1929 snprintf(uuid_s
, sizeof(uuid_s
),
1930 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1931 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1932 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1933 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1934 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1936 ret
= lttng_metadata_printf(session
,
1937 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1938 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1939 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1940 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1941 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1942 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1943 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1949 " byte_order = %s;\n"
1950 " packet.header := struct {\n"
1951 " uint32_t magic;\n"
1952 " uint8_t uuid[16];\n"
1953 " uint32_t stream_id;\n"
1956 lttng_alignof(uint8_t) * CHAR_BIT
,
1957 lttng_alignof(uint16_t) * CHAR_BIT
,
1958 lttng_alignof(uint32_t) * CHAR_BIT
,
1959 lttng_alignof(uint64_t) * CHAR_BIT
,
1960 sizeof(unsigned long) * CHAR_BIT
,
1961 lttng_alignof(unsigned long) * CHAR_BIT
,
1974 ret
= lttng_metadata_printf(session
,
1976 " hostname = \"%s\";\n"
1977 " domain = \"kernel\";\n"
1978 " sysname = \"%s\";\n"
1979 " kernel_release = \"%s\";\n"
1980 " kernel_version = \"%s\";\n"
1981 " tracer_name = \"lttng-modules\";\n"
1982 " tracer_major = %d;\n"
1983 " tracer_minor = %d;\n"
1984 " tracer_patchlevel = %d;\n"
1986 current
->nsproxy
->uts_ns
->name
.nodename
,
1990 LTTNG_MODULES_MAJOR_VERSION
,
1991 LTTNG_MODULES_MINOR_VERSION
,
1992 LTTNG_MODULES_PATCHLEVEL_VERSION
1997 ret
= lttng_metadata_printf(session
,
2005 if (!trace_clock_uuid(clock_uuid_s
)) {
2006 ret
= lttng_metadata_printf(session
,
2007 " uuid = \"%s\";\n",
2014 ret
= lttng_metadata_printf(session
,
2015 " description = \"Monotonic Clock\";\n"
2016 " freq = %llu; /* Frequency, in Hz */\n"
2017 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2020 (unsigned long long) trace_clock_freq(),
2021 (unsigned long long) measure_clock_offset()
2026 ret
= lttng_metadata_printf(session
,
2027 "typealias integer {\n"
2028 " size = 27; align = 1; signed = false;\n"
2029 " map = clock.monotonic.value;\n"
2030 "} := uint27_clock_monotonic_t;\n"
2032 "typealias integer {\n"
2033 " size = 32; align = %u; signed = false;\n"
2034 " map = clock.monotonic.value;\n"
2035 "} := uint32_clock_monotonic_t;\n"
2037 "typealias integer {\n"
2038 " size = 64; align = %u; signed = false;\n"
2039 " map = clock.monotonic.value;\n"
2040 "} := uint64_clock_monotonic_t;\n\n",
2041 lttng_alignof(uint32_t) * CHAR_BIT
,
2042 lttng_alignof(uint64_t) * CHAR_BIT
2047 ret
= _lttng_stream_packet_context_declare(session
);
2051 ret
= _lttng_event_header_declare(session
);
2056 list_for_each_entry(chan
, &session
->chan
, list
) {
2057 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2062 list_for_each_entry(event
, &session
->events
, list
) {
2063 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2067 session
->metadata_dumped
= 1;
2073 * lttng_transport_register - LTT transport registration
2074 * @transport: transport structure
2076 * Registers a transport which can be used as output to extract the data out of
2077 * LTTng. The module calling this registration function must ensure that no
2078 * trap-inducing code will be executed by the transport functions. E.g.
2079 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2080 * is made visible to the transport function. This registration acts as a
2081 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2082 * after its registration must it synchronize the TLBs.
2084 void lttng_transport_register(struct lttng_transport
*transport
)
2087 * Make sure no page fault can be triggered by the module about to be
2088 * registered. We deal with this here so we don't have to call
2089 * vmalloc_sync_all() in each module's init.
2091 wrapper_vmalloc_sync_all();
2093 mutex_lock(&sessions_mutex
);
2094 list_add_tail(&transport
->node
, <tng_transport_list
);
2095 mutex_unlock(&sessions_mutex
);
2097 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2100 * lttng_transport_unregister - LTT transport unregistration
2101 * @transport: transport structure
2103 void lttng_transport_unregister(struct lttng_transport
*transport
)
2105 mutex_lock(&sessions_mutex
);
2106 list_del(&transport
->node
);
2107 mutex_unlock(&sessions_mutex
);
2109 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2111 static int __init
lttng_events_init(void)
2115 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2119 ret
= lttng_context_init();
2122 ret
= lttng_tracepoint_init();
2125 event_cache
= KMEM_CACHE(lttng_event
, 0);
2130 ret
= lttng_abi_init();
2133 ret
= lttng_logger_init();
2141 kmem_cache_destroy(event_cache
);
2143 lttng_tracepoint_exit();
2145 lttng_context_exit();
2149 module_init(lttng_events_init
);
2151 static void __exit
lttng_events_exit(void)
2153 struct lttng_session
*session
, *tmpsession
;
2155 lttng_logger_exit();
2157 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
2158 lttng_session_destroy(session
);
2159 kmem_cache_destroy(event_cache
);
2160 lttng_tracepoint_exit();
2161 lttng_context_exit();
2164 module_exit(lttng_events_exit
);
2166 MODULE_LICENSE("GPL and additional rights");
2167 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2168 MODULE_DESCRIPTION("LTTng Events");
2169 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
2170 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
2171 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
2172 LTTNG_MODULES_EXTRAVERSION
);