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
24 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
25 * overrides a function with a define.
27 #include "wrapper/page_alloc.h"
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/jiffies.h>
34 #include <linux/utsname.h>
35 #include <linux/err.h>
36 #include <linux/seq_file.h>
37 #include <linux/file.h>
38 #include <linux/anon_inodes.h>
39 #include <wrapper/file.h>
40 #include <linux/jhash.h>
41 #include <linux/uaccess.h>
42 #include <linux/vmalloc.h>
44 #include <wrapper/uuid.h>
45 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
46 #include <wrapper/random.h>
47 #include <wrapper/tracepoint.h>
48 #include <wrapper/list.h>
49 #include <lttng-kernel-version.h>
50 #include <lttng-events.h>
51 #include <lttng-tracer.h>
52 #include <lttng-abi-old.h>
53 #include <lttng-endian.h>
54 #include <wrapper/vzalloc.h>
55 #include <wrapper/ringbuffer/backend.h>
56 #include <wrapper/ringbuffer/frontend.h>
58 #define METADATA_CACHE_DEFAULT_SIZE 4096
60 static LIST_HEAD(sessions
);
61 static LIST_HEAD(lttng_transport_list
);
63 * Protect the sessions and metadata caches.
65 static DEFINE_MUTEX(sessions_mutex
);
66 static struct kmem_cache
*event_cache
;
68 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
69 static void lttng_session_sync_enablers(struct lttng_session
*session
);
70 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
72 static void _lttng_event_destroy(struct lttng_event
*event
);
73 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
74 static int _lttng_event_unregister(struct lttng_event
*event
);
76 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
77 struct lttng_channel
*chan
,
78 struct lttng_event
*event
);
80 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
82 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
84 int _lttng_field_statedump(struct lttng_session
*session
,
85 const struct lttng_event_field
*field
,
88 void synchronize_trace(void)
91 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
92 #ifdef CONFIG_PREEMPT_RT_FULL
95 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
96 #ifdef CONFIG_PREEMPT_RT
99 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
102 void lttng_lock_sessions(void)
104 mutex_lock(&sessions_mutex
);
107 void lttng_unlock_sessions(void)
109 mutex_unlock(&sessions_mutex
);
113 * Called with sessions lock held.
115 int lttng_session_active(void)
117 struct lttng_session
*iter
;
119 list_for_each_entry(iter
, &sessions
, list
) {
126 struct lttng_session
*lttng_session_create(void)
128 struct lttng_session
*session
;
129 struct lttng_metadata_cache
*metadata_cache
;
132 mutex_lock(&sessions_mutex
);
133 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
136 INIT_LIST_HEAD(&session
->chan
);
137 INIT_LIST_HEAD(&session
->events
);
138 uuid_le_gen(&session
->uuid
);
140 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
143 goto err_free_session
;
144 metadata_cache
->data
= lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
145 if (!metadata_cache
->data
)
147 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
148 kref_init(&metadata_cache
->refcount
);
149 mutex_init(&metadata_cache
->lock
);
150 session
->metadata_cache
= metadata_cache
;
151 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
152 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
153 sizeof(metadata_cache
->uuid
));
154 INIT_LIST_HEAD(&session
->enablers_head
);
155 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
156 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
157 list_add(&session
->list
, &sessions
);
158 mutex_unlock(&sessions_mutex
);
162 kfree(metadata_cache
);
166 mutex_unlock(&sessions_mutex
);
170 void metadata_cache_destroy(struct kref
*kref
)
172 struct lttng_metadata_cache
*cache
=
173 container_of(kref
, struct lttng_metadata_cache
, refcount
);
178 void lttng_session_destroy(struct lttng_session
*session
)
180 struct lttng_channel
*chan
, *tmpchan
;
181 struct lttng_event
*event
, *tmpevent
;
182 struct lttng_metadata_stream
*metadata_stream
;
183 struct lttng_enabler
*enabler
, *tmpenabler
;
186 mutex_lock(&sessions_mutex
);
187 ACCESS_ONCE(session
->active
) = 0;
188 list_for_each_entry(chan
, &session
->chan
, list
) {
189 ret
= lttng_syscalls_unregister(chan
);
192 list_for_each_entry(event
, &session
->events
, list
) {
193 ret
= _lttng_event_unregister(event
);
196 synchronize_trace(); /* Wait for in-flight events to complete */
197 list_for_each_entry_safe(enabler
, tmpenabler
,
198 &session
->enablers_head
, node
)
199 lttng_enabler_destroy(enabler
);
200 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
201 _lttng_event_destroy(event
);
202 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
203 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
204 _lttng_channel_destroy(chan
);
206 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
207 _lttng_metadata_channel_hangup(metadata_stream
);
208 if (session
->pid_tracker
)
209 lttng_pid_tracker_destroy(session
->pid_tracker
);
210 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
211 list_del(&session
->list
);
212 mutex_unlock(&sessions_mutex
);
216 int lttng_session_statedump(struct lttng_session
*session
)
220 mutex_lock(&sessions_mutex
);
221 ret
= lttng_statedump_start(session
);
222 mutex_unlock(&sessions_mutex
);
226 int lttng_session_enable(struct lttng_session
*session
)
229 struct lttng_channel
*chan
;
231 mutex_lock(&sessions_mutex
);
232 if (session
->active
) {
237 /* Set transient enabler state to "enabled" */
241 * Snapshot the number of events per channel to know the type of header
244 list_for_each_entry(chan
, &session
->chan
, list
) {
245 if (chan
->header_type
)
246 continue; /* don't change it if session stop/restart */
247 if (chan
->free_event_id
< 31)
248 chan
->header_type
= 1; /* compact */
250 chan
->header_type
= 2; /* large */
253 /* We need to sync enablers with session before activation. */
254 lttng_session_sync_enablers(session
);
256 /* Clear each stream's quiescent state. */
257 list_for_each_entry(chan
, &session
->chan
, list
) {
258 if (chan
->channel_type
!= METADATA_CHANNEL
)
259 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
262 ACCESS_ONCE(session
->active
) = 1;
263 ACCESS_ONCE(session
->been_active
) = 1;
264 ret
= _lttng_session_metadata_statedump(session
);
266 ACCESS_ONCE(session
->active
) = 0;
269 ret
= lttng_statedump_start(session
);
271 ACCESS_ONCE(session
->active
) = 0;
273 mutex_unlock(&sessions_mutex
);
277 int lttng_session_disable(struct lttng_session
*session
)
280 struct lttng_channel
*chan
;
282 mutex_lock(&sessions_mutex
);
283 if (!session
->active
) {
287 ACCESS_ONCE(session
->active
) = 0;
289 /* Set transient enabler state to "disabled" */
291 lttng_session_sync_enablers(session
);
293 /* Set each stream's quiescent state. */
294 list_for_each_entry(chan
, &session
->chan
, list
) {
295 if (chan
->channel_type
!= METADATA_CHANNEL
)
296 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
299 mutex_unlock(&sessions_mutex
);
303 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
306 struct lttng_channel
*chan
;
307 struct lttng_event
*event
;
308 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
309 struct lttng_metadata_stream
*stream
;
311 mutex_lock(&sessions_mutex
);
312 if (!session
->active
) {
317 mutex_lock(&cache
->lock
);
318 memset(cache
->data
, 0, cache
->cache_alloc
);
319 cache
->metadata_written
= 0;
321 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
322 stream
->metadata_out
= 0;
323 stream
->metadata_in
= 0;
325 mutex_unlock(&cache
->lock
);
327 session
->metadata_dumped
= 0;
328 list_for_each_entry(chan
, &session
->chan
, list
) {
329 chan
->metadata_dumped
= 0;
332 list_for_each_entry(event
, &session
->events
, list
) {
333 event
->metadata_dumped
= 0;
336 ret
= _lttng_session_metadata_statedump(session
);
339 mutex_unlock(&sessions_mutex
);
345 int lttng_channel_enable(struct lttng_channel
*channel
)
349 mutex_lock(&sessions_mutex
);
350 if (channel
->channel_type
== METADATA_CHANNEL
) {
354 if (channel
->enabled
) {
358 /* Set transient enabler state to "enabled" */
360 lttng_session_sync_enablers(channel
->session
);
361 /* Set atomically the state to "enabled" */
362 ACCESS_ONCE(channel
->enabled
) = 1;
364 mutex_unlock(&sessions_mutex
);
368 int lttng_channel_disable(struct lttng_channel
*channel
)
372 mutex_lock(&sessions_mutex
);
373 if (channel
->channel_type
== METADATA_CHANNEL
) {
377 if (!channel
->enabled
) {
381 /* Set atomically the state to "disabled" */
382 ACCESS_ONCE(channel
->enabled
) = 0;
383 /* Set transient enabler state to "enabled" */
385 lttng_session_sync_enablers(channel
->session
);
387 mutex_unlock(&sessions_mutex
);
391 int lttng_event_enable(struct lttng_event
*event
)
395 mutex_lock(&sessions_mutex
);
396 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
400 if (event
->enabled
) {
404 switch (event
->instrumentation
) {
405 case LTTNG_KERNEL_TRACEPOINT
:
406 case LTTNG_KERNEL_SYSCALL
:
409 case LTTNG_KERNEL_KPROBE
:
410 case LTTNG_KERNEL_FUNCTION
:
411 case LTTNG_KERNEL_NOOP
:
412 ACCESS_ONCE(event
->enabled
) = 1;
414 case LTTNG_KERNEL_KRETPROBE
:
415 ret
= lttng_kretprobes_event_enable_state(event
, 1);
422 mutex_unlock(&sessions_mutex
);
426 int lttng_event_disable(struct lttng_event
*event
)
430 mutex_lock(&sessions_mutex
);
431 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
435 if (!event
->enabled
) {
439 switch (event
->instrumentation
) {
440 case LTTNG_KERNEL_TRACEPOINT
:
441 case LTTNG_KERNEL_SYSCALL
:
444 case LTTNG_KERNEL_KPROBE
:
445 case LTTNG_KERNEL_FUNCTION
:
446 case LTTNG_KERNEL_NOOP
:
447 ACCESS_ONCE(event
->enabled
) = 0;
449 case LTTNG_KERNEL_KRETPROBE
:
450 ret
= lttng_kretprobes_event_enable_state(event
, 0);
457 mutex_unlock(&sessions_mutex
);
461 static struct lttng_transport
*lttng_transport_find(const char *name
)
463 struct lttng_transport
*transport
;
465 list_for_each_entry(transport
, <tng_transport_list
, node
) {
466 if (!strcmp(transport
->name
, name
))
472 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
473 const char *transport_name
,
475 size_t subbuf_size
, size_t num_subbuf
,
476 unsigned int switch_timer_interval
,
477 unsigned int read_timer_interval
,
478 enum channel_type channel_type
)
480 struct lttng_channel
*chan
;
481 struct lttng_transport
*transport
= NULL
;
483 mutex_lock(&sessions_mutex
);
484 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
485 goto active
; /* Refuse to add channel to active session */
486 transport
= lttng_transport_find(transport_name
);
488 printk(KERN_WARNING
"LTTng transport %s not found\n",
492 if (!try_module_get(transport
->owner
)) {
493 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
496 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
499 chan
->session
= session
;
500 chan
->id
= session
->free_chan_id
++;
501 chan
->ops
= &transport
->ops
;
503 * Note: the channel creation op already writes into the packet
504 * headers. Therefore the "chan" information used as input
505 * should be already accessible.
507 chan
->chan
= transport
->ops
.channel_create(transport_name
,
508 chan
, buf_addr
, subbuf_size
, num_subbuf
,
509 switch_timer_interval
, read_timer_interval
);
514 chan
->transport
= transport
;
515 chan
->channel_type
= channel_type
;
516 list_add(&chan
->list
, &session
->chan
);
517 mutex_unlock(&sessions_mutex
);
524 module_put(transport
->owner
);
527 mutex_unlock(&sessions_mutex
);
532 * Only used internally at session destruction for per-cpu channels, and
533 * when metadata channel is released.
534 * Needs to be called with sessions mutex held.
537 void _lttng_channel_destroy(struct lttng_channel
*chan
)
539 chan
->ops
->channel_destroy(chan
->chan
);
540 module_put(chan
->transport
->owner
);
541 list_del(&chan
->list
);
542 lttng_destroy_context(chan
->ctx
);
546 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
548 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
550 /* Protect the metadata cache with the sessions_mutex. */
551 mutex_lock(&sessions_mutex
);
552 _lttng_channel_destroy(chan
);
553 mutex_unlock(&sessions_mutex
);
555 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
558 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
560 stream
->finalized
= 1;
561 wake_up_interruptible(&stream
->read_wait
);
565 * Supports event creation while tracing session is active.
566 * Needs to be called with sessions mutex held.
568 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
569 struct lttng_kernel_event
*event_param
,
571 const struct lttng_event_desc
*event_desc
,
572 enum lttng_kernel_instrumentation itype
)
574 struct lttng_session
*session
= chan
->session
;
575 struct lttng_event
*event
;
576 const char *event_name
;
577 struct hlist_head
*head
;
582 if (chan
->free_event_id
== -1U) {
588 case LTTNG_KERNEL_TRACEPOINT
:
589 event_name
= event_desc
->name
;
591 case LTTNG_KERNEL_KPROBE
:
592 case LTTNG_KERNEL_KRETPROBE
:
593 case LTTNG_KERNEL_FUNCTION
:
594 case LTTNG_KERNEL_NOOP
:
595 case LTTNG_KERNEL_SYSCALL
:
596 event_name
= event_param
->name
;
603 name_len
= strlen(event_name
);
604 hash
= jhash(event_name
, name_len
, 0);
605 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
606 lttng_hlist_for_each_entry(event
, head
, hlist
) {
607 WARN_ON_ONCE(!event
->desc
);
608 if (!strncmp(event
->desc
->name
, event_name
,
609 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
610 && chan
== event
->chan
) {
616 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
622 event
->filter
= filter
;
623 event
->id
= chan
->free_event_id
++;
624 event
->instrumentation
= itype
;
625 event
->evtype
= LTTNG_TYPE_EVENT
;
626 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
627 INIT_LIST_HEAD(&event
->enablers_ref_head
);
630 case LTTNG_KERNEL_TRACEPOINT
:
631 /* Event will be enabled by enabler sync. */
633 event
->registered
= 0;
634 event
->desc
= lttng_event_get(event_name
);
639 /* Populate lttng_event structure before event registration. */
642 case LTTNG_KERNEL_KPROBE
:
644 * Needs to be explicitly enabled after creation, since
645 * we may want to apply filters.
648 event
->registered
= 1;
650 * Populate lttng_event structure before event
654 ret
= lttng_kprobes_register(event_name
,
655 event_param
->u
.kprobe
.symbol_name
,
656 event_param
->u
.kprobe
.offset
,
657 event_param
->u
.kprobe
.addr
,
663 ret
= try_module_get(event
->desc
->owner
);
666 case LTTNG_KERNEL_KRETPROBE
:
668 struct lttng_event
*event_return
;
670 /* kretprobe defines 2 events */
672 * Needs to be explicitly enabled after creation, since
673 * we may want to apply filters.
676 event
->registered
= 1;
678 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
683 event_return
->chan
= chan
;
684 event_return
->filter
= filter
;
685 event_return
->id
= chan
->free_event_id
++;
686 event_return
->enabled
= 0;
687 event_return
->registered
= 1;
688 event_return
->instrumentation
= itype
;
690 * Populate lttng_event structure before kretprobe registration.
693 ret
= lttng_kretprobes_register(event_name
,
694 event_param
->u
.kretprobe
.symbol_name
,
695 event_param
->u
.kretprobe
.offset
,
696 event_param
->u
.kretprobe
.addr
,
697 event
, event_return
);
699 kmem_cache_free(event_cache
, event_return
);
703 /* Take 2 refs on the module: one per event. */
704 ret
= try_module_get(event
->desc
->owner
);
706 ret
= try_module_get(event
->desc
->owner
);
708 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
710 WARN_ON_ONCE(ret
> 0);
712 kmem_cache_free(event_cache
, event_return
);
713 module_put(event
->desc
->owner
);
714 module_put(event
->desc
->owner
);
715 goto statedump_error
;
717 list_add(&event_return
->list
, &chan
->session
->events
);
720 case LTTNG_KERNEL_FUNCTION
:
722 * Needs to be explicitly enabled after creation, since
723 * we may want to apply filters.
726 event
->registered
= 1;
728 * Populate lttng_event structure before event
732 ret
= lttng_ftrace_register(event_name
,
733 event_param
->u
.ftrace
.symbol_name
,
738 ret
= try_module_get(event
->desc
->owner
);
741 case LTTNG_KERNEL_NOOP
:
742 case LTTNG_KERNEL_SYSCALL
:
744 * Needs to be explicitly enabled after creation, since
745 * we may want to apply filters.
748 event
->registered
= 0;
749 event
->desc
= event_desc
;
760 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
761 WARN_ON_ONCE(ret
> 0);
763 goto statedump_error
;
765 hlist_add_head(&event
->hlist
, head
);
766 list_add(&event
->list
, &chan
->session
->events
);
770 /* If a statedump error occurs, events will not be readable. */
772 kmem_cache_free(event_cache
, event
);
780 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
781 struct lttng_kernel_event
*event_param
,
783 const struct lttng_event_desc
*event_desc
,
784 enum lttng_kernel_instrumentation itype
)
786 struct lttng_event
*event
;
788 mutex_lock(&sessions_mutex
);
789 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
791 mutex_unlock(&sessions_mutex
);
795 /* Only used for tracepoints for now. */
797 void register_event(struct lttng_event
*event
)
799 const struct lttng_event_desc
*desc
;
802 if (event
->registered
)
806 switch (event
->instrumentation
) {
807 case LTTNG_KERNEL_TRACEPOINT
:
808 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
809 desc
->probe_callback
,
812 case LTTNG_KERNEL_SYSCALL
:
813 ret
= lttng_syscall_filter_enable(event
->chan
,
816 case LTTNG_KERNEL_KPROBE
:
817 case LTTNG_KERNEL_KRETPROBE
:
818 case LTTNG_KERNEL_FUNCTION
:
819 case LTTNG_KERNEL_NOOP
:
826 event
->registered
= 1;
830 * Only used internally at session destruction.
832 int _lttng_event_unregister(struct lttng_event
*event
)
834 const struct lttng_event_desc
*desc
;
837 if (!event
->registered
)
841 switch (event
->instrumentation
) {
842 case LTTNG_KERNEL_TRACEPOINT
:
843 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
844 event
->desc
->probe_callback
,
847 case LTTNG_KERNEL_KPROBE
:
848 lttng_kprobes_unregister(event
);
851 case LTTNG_KERNEL_KRETPROBE
:
852 lttng_kretprobes_unregister(event
);
855 case LTTNG_KERNEL_FUNCTION
:
856 lttng_ftrace_unregister(event
);
859 case LTTNG_KERNEL_SYSCALL
:
860 ret
= lttng_syscall_filter_disable(event
->chan
,
863 case LTTNG_KERNEL_NOOP
:
870 event
->registered
= 0;
875 * Only used internally at session destruction.
878 void _lttng_event_destroy(struct lttng_event
*event
)
880 switch (event
->instrumentation
) {
881 case LTTNG_KERNEL_TRACEPOINT
:
882 lttng_event_put(event
->desc
);
884 case LTTNG_KERNEL_KPROBE
:
885 module_put(event
->desc
->owner
);
886 lttng_kprobes_destroy_private(event
);
888 case LTTNG_KERNEL_KRETPROBE
:
889 module_put(event
->desc
->owner
);
890 lttng_kretprobes_destroy_private(event
);
892 case LTTNG_KERNEL_FUNCTION
:
893 module_put(event
->desc
->owner
);
894 lttng_ftrace_destroy_private(event
);
896 case LTTNG_KERNEL_NOOP
:
897 case LTTNG_KERNEL_SYSCALL
:
902 list_del(&event
->list
);
903 lttng_destroy_context(event
->ctx
);
904 kmem_cache_free(event_cache
, event
);
907 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
913 mutex_lock(&sessions_mutex
);
915 /* track all pids: destroy tracker. */
916 if (session
->pid_tracker
) {
917 struct lttng_pid_tracker
*lpf
;
919 lpf
= session
->pid_tracker
;
920 rcu_assign_pointer(session
->pid_tracker
, NULL
);
922 lttng_pid_tracker_destroy(lpf
);
926 if (!session
->pid_tracker
) {
927 struct lttng_pid_tracker
*lpf
;
929 lpf
= lttng_pid_tracker_create();
934 ret
= lttng_pid_tracker_add(lpf
, pid
);
935 rcu_assign_pointer(session
->pid_tracker
, lpf
);
937 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
941 mutex_unlock(&sessions_mutex
);
945 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
951 mutex_lock(&sessions_mutex
);
953 /* untrack all pids: replace by empty tracker. */
954 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
955 struct lttng_pid_tracker
*lpf
;
957 lpf
= lttng_pid_tracker_create();
962 rcu_assign_pointer(session
->pid_tracker
, lpf
);
965 lttng_pid_tracker_destroy(old_lpf
);
968 if (!session
->pid_tracker
) {
972 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
975 mutex_unlock(&sessions_mutex
);
980 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
982 struct lttng_session
*session
= m
->private;
983 struct lttng_pid_tracker
*lpf
;
984 struct lttng_pid_hash_node
*e
;
987 mutex_lock(&sessions_mutex
);
988 lpf
= session
->pid_tracker
;
990 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
991 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
993 lttng_hlist_for_each_entry(e
, head
, hlist
) {
999 /* PID tracker disabled. */
1000 if (iter
>= *pos
&& iter
== 0) {
1001 return session
; /* empty tracker */
1009 /* Called with sessions_mutex held. */
1011 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1013 struct lttng_session
*session
= m
->private;
1014 struct lttng_pid_tracker
*lpf
;
1015 struct lttng_pid_hash_node
*e
;
1019 lpf
= session
->pid_tracker
;
1021 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
1022 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
1024 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1025 if (iter
++ >= *ppos
)
1030 /* PID tracker disabled. */
1031 if (iter
>= *ppos
&& iter
== 0)
1032 return session
; /* empty tracker */
1041 void pid_list_stop(struct seq_file
*m
, void *p
)
1043 mutex_unlock(&sessions_mutex
);
1047 int pid_list_show(struct seq_file
*m
, void *p
)
1051 if (p
== m
->private) {
1052 /* Tracker disabled. */
1055 const struct lttng_pid_hash_node
*e
= p
;
1057 pid
= lttng_pid_tracker_get_node_pid(e
);
1059 seq_printf(m
, "process { pid = %d; };\n", pid
);
1064 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
1065 .start
= pid_list_start
,
1066 .next
= pid_list_next
,
1067 .stop
= pid_list_stop
,
1068 .show
= pid_list_show
,
1072 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
1074 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
1078 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
1080 struct seq_file
*m
= file
->private_data
;
1081 struct lttng_session
*session
= m
->private;
1084 WARN_ON_ONCE(!session
);
1085 ret
= seq_release(inode
, file
);
1086 if (!ret
&& session
)
1087 fput(session
->file
);
1091 const struct file_operations lttng_tracker_pids_list_fops
= {
1092 .owner
= THIS_MODULE
,
1093 .open
= lttng_tracker_pids_list_open
,
1095 .llseek
= seq_lseek
,
1096 .release
= lttng_tracker_pids_list_release
,
1099 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
1101 struct file
*tracker_pids_list_file
;
1105 file_fd
= lttng_get_unused_fd();
1111 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
1112 <tng_tracker_pids_list_fops
,
1114 if (IS_ERR(tracker_pids_list_file
)) {
1115 ret
= PTR_ERR(tracker_pids_list_file
);
1118 if (atomic_long_add_unless(&session
->file
->f_count
,
1119 1, INT_MAX
) == INT_MAX
) {
1120 goto refcount_error
;
1122 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1125 m
= tracker_pids_list_file
->private_data
;
1126 m
->private = session
;
1127 fd_install(file_fd
, tracker_pids_list_file
);
1132 atomic_long_dec(&session
->file
->f_count
);
1134 fput(tracker_pids_list_file
);
1136 put_unused_fd(file_fd
);
1142 * Enabler management.
1145 int lttng_match_enabler_wildcard(const char *desc_name
,
1148 /* Compare excluding final '*' */
1149 if (strncmp(desc_name
, name
, strlen(name
) - 1))
1155 int lttng_match_enabler_name(const char *desc_name
,
1158 if (strcmp(desc_name
, name
))
1164 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1165 struct lttng_enabler
*enabler
)
1167 const char *desc_name
, *enabler_name
;
1169 enabler_name
= enabler
->event_param
.name
;
1170 switch (enabler
->event_param
.instrumentation
) {
1171 case LTTNG_KERNEL_TRACEPOINT
:
1172 desc_name
= desc
->name
;
1174 case LTTNG_KERNEL_SYSCALL
:
1175 desc_name
= desc
->name
;
1176 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1177 desc_name
+= strlen("compat_");
1178 if (!strncmp(desc_name
, "syscall_exit_",
1179 strlen("syscall_exit_"))) {
1180 desc_name
+= strlen("syscall_exit_");
1181 } else if (!strncmp(desc_name
, "syscall_entry_",
1182 strlen("syscall_entry_"))) {
1183 desc_name
+= strlen("syscall_entry_");
1193 switch (enabler
->type
) {
1194 case LTTNG_ENABLER_WILDCARD
:
1195 return lttng_match_enabler_wildcard(desc_name
, enabler_name
);
1196 case LTTNG_ENABLER_NAME
:
1197 return lttng_match_enabler_name(desc_name
, enabler_name
);
1204 int lttng_event_match_enabler(struct lttng_event
*event
,
1205 struct lttng_enabler
*enabler
)
1207 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1209 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1210 && event
->chan
== enabler
->chan
)
1217 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1218 struct lttng_enabler
*enabler
)
1220 struct lttng_enabler_ref
*enabler_ref
;
1222 list_for_each_entry(enabler_ref
,
1223 &event
->enablers_ref_head
, node
) {
1224 if (enabler_ref
->ref
== enabler
)
1231 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1233 struct lttng_session
*session
= enabler
->chan
->session
;
1234 struct lttng_probe_desc
*probe_desc
;
1235 const struct lttng_event_desc
*desc
;
1237 struct list_head
*probe_list
;
1239 probe_list
= lttng_get_probe_list_head();
1241 * For each probe event, if we find that a probe event matches
1242 * our enabler, create an associated lttng_event if not
1245 list_for_each_entry(probe_desc
, probe_list
, head
) {
1246 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1248 struct hlist_head
*head
;
1249 const char *event_name
;
1252 struct lttng_event
*event
;
1254 desc
= probe_desc
->event_desc
[i
];
1255 if (!lttng_desc_match_enabler(desc
, enabler
))
1257 event_name
= desc
->name
;
1258 name_len
= strlen(event_name
);
1261 * Check if already created.
1263 hash
= jhash(event_name
, name_len
, 0);
1264 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1265 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1266 if (event
->desc
== desc
1267 && event
->chan
== enabler
->chan
)
1274 * We need to create an event for this
1277 event
= _lttng_event_create(enabler
->chan
,
1279 LTTNG_KERNEL_TRACEPOINT
);
1281 printk(KERN_INFO
"Unable to create event %s\n",
1282 probe_desc
->event_desc
[i
]->name
);
1289 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1293 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1298 * Create struct lttng_event if it is missing and present in the list of
1299 * tracepoint probes.
1300 * Should be called with sessions mutex held.
1303 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1305 switch (enabler
->event_param
.instrumentation
) {
1306 case LTTNG_KERNEL_TRACEPOINT
:
1307 lttng_create_tracepoint_if_missing(enabler
);
1309 case LTTNG_KERNEL_SYSCALL
:
1310 lttng_create_syscall_if_missing(enabler
);
1319 * Create events associated with an enabler (if not already present),
1320 * and add backward reference from the event to the enabler.
1321 * Should be called with sessions mutex held.
1324 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1326 struct lttng_session
*session
= enabler
->chan
->session
;
1327 struct lttng_event
*event
;
1329 /* First ensure that probe events are created for this enabler. */
1330 lttng_create_event_if_missing(enabler
);
1332 /* For each event matching enabler in session event list. */
1333 list_for_each_entry(event
, &session
->events
, list
) {
1334 struct lttng_enabler_ref
*enabler_ref
;
1336 if (!lttng_event_match_enabler(event
, enabler
))
1338 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1341 * If no backward ref, create it.
1342 * Add backward ref from event to enabler.
1344 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1347 enabler_ref
->ref
= enabler
;
1348 list_add(&enabler_ref
->node
,
1349 &event
->enablers_ref_head
);
1353 * Link filter bytecodes if not linked yet.
1355 lttng_enabler_event_link_bytecode(event
, enabler
);
1357 /* TODO: merge event context. */
1363 * Called at module load: connect the probe on all enablers matching
1365 * Called with sessions lock held.
1367 int lttng_fix_pending_events(void)
1369 struct lttng_session
*session
;
1371 list_for_each_entry(session
, &sessions
, list
)
1372 lttng_session_lazy_sync_enablers(session
);
1376 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1377 struct lttng_kernel_event
*event_param
,
1378 struct lttng_channel
*chan
)
1380 struct lttng_enabler
*enabler
;
1382 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1385 enabler
->type
= type
;
1386 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1387 memcpy(&enabler
->event_param
, event_param
,
1388 sizeof(enabler
->event_param
));
1389 enabler
->chan
= chan
;
1391 enabler
->enabled
= 0;
1392 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1393 mutex_lock(&sessions_mutex
);
1394 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1395 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1396 mutex_unlock(&sessions_mutex
);
1400 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1402 mutex_lock(&sessions_mutex
);
1403 enabler
->enabled
= 1;
1404 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1405 mutex_unlock(&sessions_mutex
);
1409 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1411 mutex_lock(&sessions_mutex
);
1412 enabler
->enabled
= 0;
1413 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1414 mutex_unlock(&sessions_mutex
);
1418 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1419 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1421 struct lttng_filter_bytecode_node
*bytecode_node
;
1422 uint32_t bytecode_len
;
1425 ret
= get_user(bytecode_len
, &bytecode
->len
);
1428 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1432 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1433 sizeof(*bytecode
) + bytecode_len
);
1436 bytecode_node
->enabler
= enabler
;
1437 /* Enforce length based on allocated size */
1438 bytecode_node
->bc
.len
= bytecode_len
;
1439 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1440 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1444 kfree(bytecode_node
);
1448 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1449 struct lttng_kernel_context
*context_param
)
1455 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1457 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1459 /* Destroy filter bytecode */
1460 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1461 &enabler
->filter_bytecode_head
, node
) {
1465 /* Destroy contexts */
1466 lttng_destroy_context(enabler
->ctx
);
1468 list_del(&enabler
->node
);
1473 * lttng_session_sync_enablers should be called just before starting a
1475 * Should be called with sessions mutex held.
1478 void lttng_session_sync_enablers(struct lttng_session
*session
)
1480 struct lttng_enabler
*enabler
;
1481 struct lttng_event
*event
;
1483 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1484 lttng_enabler_ref_events(enabler
);
1486 * For each event, if at least one of its enablers is enabled,
1487 * and its channel and session transient states are enabled, we
1488 * enable the event, else we disable it.
1490 list_for_each_entry(event
, &session
->events
, list
) {
1491 struct lttng_enabler_ref
*enabler_ref
;
1492 struct lttng_bytecode_runtime
*runtime
;
1493 int enabled
= 0, has_enablers_without_bytecode
= 0;
1495 switch (event
->instrumentation
) {
1496 case LTTNG_KERNEL_TRACEPOINT
:
1497 case LTTNG_KERNEL_SYSCALL
:
1499 list_for_each_entry(enabler_ref
,
1500 &event
->enablers_ref_head
, node
) {
1501 if (enabler_ref
->ref
->enabled
) {
1508 /* Not handled with lazy sync. */
1512 * Enabled state is based on union of enablers, with
1513 * intesection of session and channel transient enable
1516 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1518 ACCESS_ONCE(event
->enabled
) = enabled
;
1520 * Sync tracepoint registration with event enabled
1524 register_event(event
);
1526 _lttng_event_unregister(event
);
1529 /* Check if has enablers without bytecode enabled */
1530 list_for_each_entry(enabler_ref
,
1531 &event
->enablers_ref_head
, node
) {
1532 if (enabler_ref
->ref
->enabled
1533 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1534 has_enablers_without_bytecode
= 1;
1538 event
->has_enablers_without_bytecode
=
1539 has_enablers_without_bytecode
;
1541 /* Enable filters */
1542 list_for_each_entry(runtime
,
1543 &event
->bytecode_runtime_head
, node
)
1544 lttng_filter_sync_state(runtime
);
1549 * Apply enablers to session events, adding events to session if need
1550 * be. It is required after each modification applied to an active
1551 * session, and right before session "start".
1552 * "lazy" sync means we only sync if required.
1553 * Should be called with sessions mutex held.
1556 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1558 /* We can skip if session is not active */
1559 if (!session
->active
)
1561 lttng_session_sync_enablers(session
);
1565 * Serialize at most one packet worth of metadata into a metadata
1567 * We grab the metadata cache mutex to get exclusive access to our metadata
1568 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1569 * allows us to do racy operations such as looking for remaining space left in
1570 * packet and write, since mutual exclusion protects us from concurrent writes.
1571 * Mutual exclusion on the metadata cache allow us to read the cache content
1572 * without racing against reallocation of the cache by updates.
1573 * Returns the number of bytes written in the channel, 0 if no data
1574 * was written and a negative value on error.
1576 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1577 struct channel
*chan
)
1579 struct lib_ring_buffer_ctx ctx
;
1581 size_t len
, reserve_len
;
1584 * Ensure we support mutiple get_next / put sequences followed by
1585 * put_next. The metadata cache lock protects reading the metadata
1586 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1587 * "flush" operations on the buffer invoked by different processes.
1588 * Moreover, since the metadata cache memory can be reallocated, we
1589 * need to have exclusive access against updates even though we only
1592 mutex_lock(&stream
->metadata_cache
->lock
);
1593 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1594 if (stream
->metadata_in
!= stream
->metadata_out
)
1597 /* Metadata regenerated, change the version. */
1598 if (stream
->metadata_cache
->version
!= stream
->version
)
1599 stream
->version
= stream
->metadata_cache
->version
;
1601 len
= stream
->metadata_cache
->metadata_written
-
1602 stream
->metadata_in
;
1605 reserve_len
= min_t(size_t,
1606 stream
->transport
->ops
.packet_avail_size(chan
),
1608 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1611 * If reservation failed, return an error to the caller.
1613 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1615 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1618 stream
->transport
->ops
.event_write(&ctx
,
1619 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1621 stream
->transport
->ops
.event_commit(&ctx
);
1622 stream
->metadata_in
+= reserve_len
;
1626 mutex_unlock(&stream
->metadata_cache
->lock
);
1631 * Write the metadata to the metadata cache.
1632 * Must be called with sessions_mutex held.
1633 * The metadata cache lock protects us from concurrent read access from
1634 * thread outputting metadata content to ring buffer.
1636 int lttng_metadata_printf(struct lttng_session
*session
,
1637 const char *fmt
, ...)
1642 struct lttng_metadata_stream
*stream
;
1644 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
1647 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1653 mutex_lock(&session
->metadata_cache
->lock
);
1654 if (session
->metadata_cache
->metadata_written
+ len
>
1655 session
->metadata_cache
->cache_alloc
) {
1656 char *tmp_cache_realloc
;
1657 unsigned int tmp_cache_alloc_size
;
1659 tmp_cache_alloc_size
= max_t(unsigned int,
1660 session
->metadata_cache
->cache_alloc
+ len
,
1661 session
->metadata_cache
->cache_alloc
<< 1);
1662 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
1663 if (!tmp_cache_realloc
)
1665 if (session
->metadata_cache
->data
) {
1666 memcpy(tmp_cache_realloc
,
1667 session
->metadata_cache
->data
,
1668 session
->metadata_cache
->cache_alloc
);
1669 vfree(session
->metadata_cache
->data
);
1672 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1673 session
->metadata_cache
->data
= tmp_cache_realloc
;
1675 memcpy(session
->metadata_cache
->data
+
1676 session
->metadata_cache
->metadata_written
,
1678 session
->metadata_cache
->metadata_written
+= len
;
1679 mutex_unlock(&session
->metadata_cache
->lock
);
1682 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1683 wake_up_interruptible(&stream
->read_wait
);
1688 mutex_unlock(&session
->metadata_cache
->lock
);
1694 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1698 for (i
= 0; i
< nesting
; i
++) {
1701 ret
= lttng_metadata_printf(session
, " ");
1710 * Must be called with sessions_mutex held.
1713 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1714 const struct lttng_type
*type
,
1718 uint32_t i
, nr_fields
;
1720 ret
= print_tabs(session
, nesting
);
1723 ret
= lttng_metadata_printf(session
,
1727 nr_fields
= type
->u
._struct
.nr_fields
;
1728 for (i
= 0; i
< nr_fields
; i
++) {
1729 const struct lttng_event_field
*iter_field
;
1731 iter_field
= &type
->u
._struct
.fields
[i
];
1732 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1736 ret
= print_tabs(session
, nesting
);
1739 ret
= lttng_metadata_printf(session
,
1745 * Must be called with sessions_mutex held.
1748 int _lttng_struct_statedump(struct lttng_session
*session
,
1749 const struct lttng_event_field
*field
,
1754 ret
= _lttng_struct_type_statedump(session
,
1755 &field
->type
, nesting
);
1758 ret
= lttng_metadata_printf(session
,
1765 * Must be called with sessions_mutex held.
1768 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1769 const struct lttng_type
*type
,
1773 uint32_t i
, nr_choices
;
1775 ret
= print_tabs(session
, nesting
);
1778 ret
= lttng_metadata_printf(session
,
1779 "variant <_%s> {\n",
1780 type
->u
.variant
.tag_name
);
1783 nr_choices
= type
->u
.variant
.nr_choices
;
1784 for (i
= 0; i
< nr_choices
; i
++) {
1785 const struct lttng_event_field
*iter_field
;
1787 iter_field
= &type
->u
.variant
.choices
[i
];
1788 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1792 ret
= print_tabs(session
, nesting
);
1795 ret
= lttng_metadata_printf(session
,
1801 * Must be called with sessions_mutex held.
1804 int _lttng_variant_statedump(struct lttng_session
*session
,
1805 const struct lttng_event_field
*field
,
1810 ret
= _lttng_variant_type_statedump(session
,
1811 &field
->type
, nesting
);
1814 ret
= lttng_metadata_printf(session
,
1821 * Must be called with sessions_mutex held.
1824 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1825 const struct lttng_event_field
*field
,
1829 const struct lttng_type
*elem_type
;
1831 /* Only array of structures and variants are currently supported. */
1832 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1833 switch (elem_type
->atype
) {
1835 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1840 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1847 ret
= lttng_metadata_printf(session
,
1850 field
->type
.u
.array_compound
.length
);
1855 * Must be called with sessions_mutex held.
1858 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
1859 const struct lttng_event_field
*field
,
1863 const char *length_name
;
1864 const struct lttng_type
*elem_type
;
1866 length_name
= field
->type
.u
.sequence_compound
.length_name
;
1868 /* Only array of structures and variants are currently supported. */
1869 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
1870 switch (elem_type
->atype
) {
1872 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1877 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1884 ret
= lttng_metadata_printf(session
,
1892 * Must be called with sessions_mutex held.
1895 int _lttng_enum_statedump(struct lttng_session
*session
,
1896 const struct lttng_event_field
*field
,
1899 const struct lttng_enum_desc
*enum_desc
;
1900 const struct lttng_integer_type
*container_type
;
1902 unsigned int i
, nr_entries
;
1904 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
1905 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
1906 nr_entries
= enum_desc
->nr_entries
;
1908 ret
= print_tabs(session
, nesting
);
1911 ret
= lttng_metadata_printf(session
,
1912 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
1913 container_type
->size
,
1914 container_type
->alignment
,
1915 container_type
->signedness
,
1916 (container_type
->encoding
== lttng_encode_none
)
1918 : (container_type
->encoding
== lttng_encode_UTF8
)
1921 container_type
->base
,
1922 #if __BYTE_ORDER == __BIG_ENDIAN
1923 container_type
->reverse_byte_order
? " byte_order = le;" : ""
1925 container_type
->reverse_byte_order
? " byte_order = be;" : ""
1930 /* Dump all entries */
1931 for (i
= 0; i
< nr_entries
; i
++) {
1932 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
1935 ret
= print_tabs(session
, nesting
+ 1);
1938 ret
= lttng_metadata_printf(session
,
1942 len
= strlen(entry
->string
);
1943 /* Escape the character '"' */
1944 for (j
= 0; j
< len
; j
++) {
1945 char c
= entry
->string
[j
];
1949 ret
= lttng_metadata_printf(session
,
1953 ret
= lttng_metadata_printf(session
,
1957 ret
= lttng_metadata_printf(session
,
1964 ret
= lttng_metadata_printf(session
, "\"");
1968 if (entry
->options
.is_auto
) {
1969 ret
= lttng_metadata_printf(session
, ",\n");
1973 ret
= lttng_metadata_printf(session
,
1977 if (entry
->start
.signedness
)
1978 ret
= lttng_metadata_printf(session
,
1979 "%lld", (long long) entry
->start
.value
);
1981 ret
= lttng_metadata_printf(session
,
1982 "%llu", entry
->start
.value
);
1985 if (entry
->start
.signedness
== entry
->end
.signedness
&&
1987 == entry
->end
.value
) {
1988 ret
= lttng_metadata_printf(session
,
1991 if (entry
->end
.signedness
) {
1992 ret
= lttng_metadata_printf(session
,
1994 (long long) entry
->end
.value
);
1996 ret
= lttng_metadata_printf(session
,
2005 ret
= print_tabs(session
, nesting
);
2008 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2015 * Must be called with sessions_mutex held.
2018 int _lttng_field_statedump(struct lttng_session
*session
,
2019 const struct lttng_event_field
*field
,
2024 switch (field
->type
.atype
) {
2026 ret
= print_tabs(session
, nesting
);
2029 ret
= lttng_metadata_printf(session
,
2030 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2031 field
->type
.u
.basic
.integer
.size
,
2032 field
->type
.u
.basic
.integer
.alignment
,
2033 field
->type
.u
.basic
.integer
.signedness
,
2034 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2036 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2039 field
->type
.u
.basic
.integer
.base
,
2040 #if __BYTE_ORDER == __BIG_ENDIAN
2041 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2043 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2048 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2052 const struct lttng_basic_type
*elem_type
;
2054 elem_type
= &field
->type
.u
.array
.elem_type
;
2055 if (field
->type
.u
.array
.elem_alignment
) {
2056 ret
= print_tabs(session
, nesting
);
2059 ret
= lttng_metadata_printf(session
,
2060 "struct { } align(%u) _%s_padding;\n",
2061 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2066 ret
= print_tabs(session
, nesting
);
2069 ret
= lttng_metadata_printf(session
,
2070 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2071 elem_type
->u
.basic
.integer
.size
,
2072 elem_type
->u
.basic
.integer
.alignment
,
2073 elem_type
->u
.basic
.integer
.signedness
,
2074 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2076 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2079 elem_type
->u
.basic
.integer
.base
,
2080 #if __BYTE_ORDER == __BIG_ENDIAN
2081 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2083 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2085 field
->name
, field
->type
.u
.array
.length
);
2088 case atype_sequence
:
2090 const struct lttng_basic_type
*elem_type
;
2091 const struct lttng_basic_type
*length_type
;
2093 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2094 length_type
= &field
->type
.u
.sequence
.length_type
;
2095 ret
= print_tabs(session
, nesting
);
2098 ret
= lttng_metadata_printf(session
,
2099 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2100 length_type
->u
.basic
.integer
.size
,
2101 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2102 length_type
->u
.basic
.integer
.signedness
,
2103 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2105 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2108 length_type
->u
.basic
.integer
.base
,
2109 #if __BYTE_ORDER == __BIG_ENDIAN
2110 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2112 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2118 if (field
->type
.u
.sequence
.elem_alignment
) {
2119 ret
= print_tabs(session
, nesting
);
2122 ret
= lttng_metadata_printf(session
,
2123 "struct { } align(%u) _%s_padding;\n",
2124 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2129 ret
= print_tabs(session
, nesting
);
2132 ret
= lttng_metadata_printf(session
,
2133 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2134 elem_type
->u
.basic
.integer
.size
,
2135 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2136 elem_type
->u
.basic
.integer
.signedness
,
2137 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2139 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2142 elem_type
->u
.basic
.integer
.base
,
2143 #if __BYTE_ORDER == __BIG_ENDIAN
2144 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2146 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2154 /* Default encoding is UTF8 */
2155 ret
= print_tabs(session
, nesting
);
2158 ret
= lttng_metadata_printf(session
,
2160 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2161 " { encoding = ASCII; }" : "",
2165 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2167 case atype_array_compound
:
2168 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2170 case atype_sequence_compound
:
2171 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2174 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2185 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2186 struct lttng_ctx
*ctx
)
2193 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2194 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2196 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2204 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2205 struct lttng_event
*event
)
2207 const struct lttng_event_desc
*desc
= event
->desc
;
2211 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2212 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2214 ret
= _lttng_field_statedump(session
, field
, 2);
2222 * Must be called with sessions_mutex held.
2225 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2226 struct lttng_channel
*chan
,
2227 struct lttng_event
*event
)
2231 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
2233 if (chan
->channel_type
== METADATA_CHANNEL
)
2236 ret
= lttng_metadata_printf(session
,
2240 " stream_id = %u;\n",
2248 ret
= lttng_metadata_printf(session
,
2249 " context := struct {\n");
2253 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2257 ret
= lttng_metadata_printf(session
,
2263 ret
= lttng_metadata_printf(session
,
2264 " fields := struct {\n"
2269 ret
= _lttng_fields_metadata_statedump(session
, event
);
2274 * LTTng space reservation can only reserve multiples of the
2277 ret
= lttng_metadata_printf(session
,
2283 event
->metadata_dumped
= 1;
2290 * Must be called with sessions_mutex held.
2293 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2294 struct lttng_channel
*chan
)
2298 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
2301 if (chan
->channel_type
== METADATA_CHANNEL
)
2304 WARN_ON_ONCE(!chan
->header_type
);
2305 ret
= lttng_metadata_printf(session
,
2308 " event.header := %s;\n"
2309 " packet.context := struct packet_context;\n",
2311 chan
->header_type
== 1 ? "struct event_header_compact" :
2312 "struct event_header_large");
2317 ret
= lttng_metadata_printf(session
,
2318 " event.context := struct {\n");
2322 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2326 ret
= lttng_metadata_printf(session
,
2332 ret
= lttng_metadata_printf(session
,
2335 chan
->metadata_dumped
= 1;
2341 * Must be called with sessions_mutex held.
2344 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2346 return lttng_metadata_printf(session
,
2347 "struct packet_context {\n"
2348 " uint64_clock_monotonic_t timestamp_begin;\n"
2349 " uint64_clock_monotonic_t timestamp_end;\n"
2350 " uint64_t content_size;\n"
2351 " uint64_t packet_size;\n"
2352 " uint64_t packet_seq_num;\n"
2353 " unsigned long events_discarded;\n"
2354 " uint32_t cpu_id;\n"
2361 * id: range: 0 - 30.
2362 * id 31 is reserved to indicate an extended header.
2365 * id: range: 0 - 65534.
2366 * id 65535 is reserved to indicate an extended header.
2368 * Must be called with sessions_mutex held.
2371 int _lttng_event_header_declare(struct lttng_session
*session
)
2373 return lttng_metadata_printf(session
,
2374 "struct event_header_compact {\n"
2375 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2378 " uint27_clock_monotonic_t timestamp;\n"
2382 " uint64_clock_monotonic_t timestamp;\n"
2387 "struct event_header_large {\n"
2388 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2391 " uint32_clock_monotonic_t timestamp;\n"
2395 " uint64_clock_monotonic_t timestamp;\n"
2399 lttng_alignof(uint32_t) * CHAR_BIT
,
2400 lttng_alignof(uint16_t) * CHAR_BIT
2405 * Approximation of NTP time of day to clock monotonic correlation,
2406 * taken at start of trace.
2407 * Yes, this is only an approximation. Yes, we can (and will) do better
2408 * in future versions.
2409 * This function may return a negative offset. It may happen if the
2410 * system sets the REALTIME clock to 0 after boot.
2413 int64_t measure_clock_offset(void)
2415 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2416 uint64_t tcf
= trace_clock_freq();
2418 struct timespec rts
= { 0, 0 };
2419 unsigned long flags
;
2421 /* Disable interrupts to increase correlation precision. */
2422 local_irq_save(flags
);
2423 monotonic
[0] = trace_clock_read64();
2424 getnstimeofday(&rts
);
2425 monotonic
[1] = trace_clock_read64();
2426 local_irq_restore(flags
);
2428 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2429 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2430 if (tcf
== NSEC_PER_SEC
) {
2431 realtime
+= rts
.tv_nsec
;
2433 uint64_t n
= rts
.tv_nsec
* tcf
;
2435 do_div(n
, NSEC_PER_SEC
);
2438 offset
= (int64_t) realtime
- monotonic_avg
;
2443 * Output metadata into this session's metadata buffers.
2444 * Must be called with sessions_mutex held.
2447 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2449 unsigned char *uuid_c
= session
->uuid
.b
;
2450 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2451 struct lttng_channel
*chan
;
2452 struct lttng_event
*event
;
2455 if (!ACCESS_ONCE(session
->active
))
2457 if (session
->metadata_dumped
)
2460 snprintf(uuid_s
, sizeof(uuid_s
),
2461 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2462 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2463 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2464 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2465 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2467 ret
= lttng_metadata_printf(session
,
2468 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2469 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2470 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2471 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2472 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2473 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2474 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2480 " byte_order = %s;\n"
2481 " packet.header := struct {\n"
2482 " uint32_t magic;\n"
2483 " uint8_t uuid[16];\n"
2484 " uint32_t stream_id;\n"
2485 " uint64_t stream_instance_id;\n"
2488 lttng_alignof(uint8_t) * CHAR_BIT
,
2489 lttng_alignof(uint16_t) * CHAR_BIT
,
2490 lttng_alignof(uint32_t) * CHAR_BIT
,
2491 lttng_alignof(uint64_t) * CHAR_BIT
,
2492 sizeof(unsigned long) * CHAR_BIT
,
2493 lttng_alignof(unsigned long) * CHAR_BIT
,
2497 #if __BYTE_ORDER == __BIG_ENDIAN
2506 ret
= lttng_metadata_printf(session
,
2508 " hostname = \"%s\";\n"
2509 " domain = \"kernel\";\n"
2510 " sysname = \"%s\";\n"
2511 " kernel_release = \"%s\";\n"
2512 " kernel_version = \"%s\";\n"
2513 " tracer_name = \"lttng-modules\";\n"
2514 " tracer_major = %d;\n"
2515 " tracer_minor = %d;\n"
2516 " tracer_patchlevel = %d;\n"
2518 current
->nsproxy
->uts_ns
->name
.nodename
,
2522 LTTNG_MODULES_MAJOR_VERSION
,
2523 LTTNG_MODULES_MINOR_VERSION
,
2524 LTTNG_MODULES_PATCHLEVEL_VERSION
2529 ret
= lttng_metadata_printf(session
,
2531 " name = \"%s\";\n",
2537 if (!trace_clock_uuid(clock_uuid_s
)) {
2538 ret
= lttng_metadata_printf(session
,
2539 " uuid = \"%s\";\n",
2546 ret
= lttng_metadata_printf(session
,
2547 " description = \"%s\";\n"
2548 " freq = %llu; /* Frequency, in Hz */\n"
2549 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2552 trace_clock_description(),
2553 (unsigned long long) trace_clock_freq(),
2554 (long long) measure_clock_offset()
2559 ret
= lttng_metadata_printf(session
,
2560 "typealias integer {\n"
2561 " size = 27; align = 1; signed = false;\n"
2562 " map = clock.%s.value;\n"
2563 "} := uint27_clock_monotonic_t;\n"
2565 "typealias integer {\n"
2566 " size = 32; align = %u; signed = false;\n"
2567 " map = clock.%s.value;\n"
2568 "} := uint32_clock_monotonic_t;\n"
2570 "typealias integer {\n"
2571 " size = 64; align = %u; signed = false;\n"
2572 " map = clock.%s.value;\n"
2573 "} := uint64_clock_monotonic_t;\n\n",
2575 lttng_alignof(uint32_t) * CHAR_BIT
,
2577 lttng_alignof(uint64_t) * CHAR_BIT
,
2583 ret
= _lttng_stream_packet_context_declare(session
);
2587 ret
= _lttng_event_header_declare(session
);
2592 list_for_each_entry(chan
, &session
->chan
, list
) {
2593 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2598 list_for_each_entry(event
, &session
->events
, list
) {
2599 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2603 session
->metadata_dumped
= 1;
2609 * lttng_transport_register - LTT transport registration
2610 * @transport: transport structure
2612 * Registers a transport which can be used as output to extract the data out of
2613 * LTTng. The module calling this registration function must ensure that no
2614 * trap-inducing code will be executed by the transport functions. E.g.
2615 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2616 * is made visible to the transport function. This registration acts as a
2617 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2618 * after its registration must it synchronize the TLBs.
2620 void lttng_transport_register(struct lttng_transport
*transport
)
2623 * Make sure no page fault can be triggered by the module about to be
2624 * registered. We deal with this here so we don't have to call
2625 * vmalloc_sync_all() in each module's init.
2627 wrapper_vmalloc_sync_all();
2629 mutex_lock(&sessions_mutex
);
2630 list_add_tail(&transport
->node
, <tng_transport_list
);
2631 mutex_unlock(&sessions_mutex
);
2633 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2636 * lttng_transport_unregister - LTT transport unregistration
2637 * @transport: transport structure
2639 void lttng_transport_unregister(struct lttng_transport
*transport
)
2641 mutex_lock(&sessions_mutex
);
2642 list_del(&transport
->node
);
2643 mutex_unlock(&sessions_mutex
);
2645 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2647 static int __init
lttng_events_init(void)
2651 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2654 ret
= wrapper_get_pfnblock_flags_mask_init();
2657 ret
= wrapper_get_pageblock_flags_mask_init();
2660 ret
= lttng_probes_init();
2663 ret
= lttng_context_init();
2666 ret
= lttng_tracepoint_init();
2669 event_cache
= KMEM_CACHE(lttng_event
, 0);
2674 ret
= lttng_abi_init();
2677 ret
= lttng_logger_init();
2685 kmem_cache_destroy(event_cache
);
2687 lttng_tracepoint_exit();
2689 lttng_context_exit();
2693 module_init(lttng_events_init
);
2695 static void __exit
lttng_events_exit(void)
2697 struct lttng_session
*session
, *tmpsession
;
2699 lttng_logger_exit();
2701 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
2702 lttng_session_destroy(session
);
2703 kmem_cache_destroy(event_cache
);
2704 lttng_tracepoint_exit();
2705 lttng_context_exit();
2708 module_exit(lttng_events_exit
);
2710 MODULE_LICENSE("GPL and additional rights");
2711 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2712 MODULE_DESCRIPTION("LTTng Events");
2713 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
2714 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
2715 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
2716 LTTNG_MODULES_EXTRAVERSION
);