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 void synchronize_trace(void)
87 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
88 #ifdef CONFIG_PREEMPT_RT_FULL
91 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
92 #ifdef CONFIG_PREEMPT_RT
95 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
98 void lttng_lock_sessions(void)
100 mutex_lock(&sessions_mutex
);
103 void lttng_unlock_sessions(void)
105 mutex_unlock(&sessions_mutex
);
109 * Called with sessions lock held.
111 int lttng_session_active(void)
113 struct lttng_session
*iter
;
115 list_for_each_entry(iter
, &sessions
, list
) {
122 struct lttng_session
*lttng_session_create(void)
124 struct lttng_session
*session
;
125 struct lttng_metadata_cache
*metadata_cache
;
128 mutex_lock(&sessions_mutex
);
129 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
132 INIT_LIST_HEAD(&session
->chan
);
133 INIT_LIST_HEAD(&session
->events
);
134 uuid_le_gen(&session
->uuid
);
136 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
139 goto err_free_session
;
140 metadata_cache
->data
= lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
141 if (!metadata_cache
->data
)
143 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
144 kref_init(&metadata_cache
->refcount
);
145 mutex_init(&metadata_cache
->lock
);
146 session
->metadata_cache
= metadata_cache
;
147 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
148 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
149 sizeof(metadata_cache
->uuid
));
150 INIT_LIST_HEAD(&session
->enablers_head
);
151 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
152 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
153 list_add(&session
->list
, &sessions
);
154 mutex_unlock(&sessions_mutex
);
158 kfree(metadata_cache
);
162 mutex_unlock(&sessions_mutex
);
166 void metadata_cache_destroy(struct kref
*kref
)
168 struct lttng_metadata_cache
*cache
=
169 container_of(kref
, struct lttng_metadata_cache
, refcount
);
174 void lttng_session_destroy(struct lttng_session
*session
)
176 struct lttng_channel
*chan
, *tmpchan
;
177 struct lttng_event
*event
, *tmpevent
;
178 struct lttng_metadata_stream
*metadata_stream
;
179 struct lttng_enabler
*enabler
, *tmpenabler
;
182 mutex_lock(&sessions_mutex
);
183 ACCESS_ONCE(session
->active
) = 0;
184 list_for_each_entry(chan
, &session
->chan
, list
) {
185 ret
= lttng_syscalls_unregister(chan
);
188 list_for_each_entry(event
, &session
->events
, list
) {
189 ret
= _lttng_event_unregister(event
);
192 synchronize_trace(); /* Wait for in-flight events to complete */
193 list_for_each_entry_safe(enabler
, tmpenabler
,
194 &session
->enablers_head
, node
)
195 lttng_enabler_destroy(enabler
);
196 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
197 _lttng_event_destroy(event
);
198 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
199 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
200 _lttng_channel_destroy(chan
);
202 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
203 _lttng_metadata_channel_hangup(metadata_stream
);
204 if (session
->pid_tracker
)
205 lttng_pid_tracker_destroy(session
->pid_tracker
);
206 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
207 list_del(&session
->list
);
208 mutex_unlock(&sessions_mutex
);
212 int lttng_session_enable(struct lttng_session
*session
)
215 struct lttng_channel
*chan
;
217 mutex_lock(&sessions_mutex
);
218 if (session
->active
) {
223 /* Set transient enabler state to "enabled" */
227 * Snapshot the number of events per channel to know the type of header
230 list_for_each_entry(chan
, &session
->chan
, list
) {
231 if (chan
->header_type
)
232 continue; /* don't change it if session stop/restart */
233 if (chan
->free_event_id
< 31)
234 chan
->header_type
= 1; /* compact */
236 chan
->header_type
= 2; /* large */
239 /* We need to sync enablers with session before activation. */
240 lttng_session_sync_enablers(session
);
242 /* Clear each stream's quiescent state. */
243 list_for_each_entry(chan
, &session
->chan
, list
) {
244 if (chan
->channel_type
!= METADATA_CHANNEL
)
245 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
248 ACCESS_ONCE(session
->active
) = 1;
249 ACCESS_ONCE(session
->been_active
) = 1;
250 ret
= _lttng_session_metadata_statedump(session
);
252 ACCESS_ONCE(session
->active
) = 0;
255 ret
= lttng_statedump_start(session
);
257 ACCESS_ONCE(session
->active
) = 0;
259 mutex_unlock(&sessions_mutex
);
263 int lttng_session_disable(struct lttng_session
*session
)
266 struct lttng_channel
*chan
;
268 mutex_lock(&sessions_mutex
);
269 if (!session
->active
) {
273 ACCESS_ONCE(session
->active
) = 0;
275 /* Set transient enabler state to "disabled" */
277 lttng_session_sync_enablers(session
);
279 /* Set each stream's quiescent state. */
280 list_for_each_entry(chan
, &session
->chan
, list
) {
281 if (chan
->channel_type
!= METADATA_CHANNEL
)
282 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
285 mutex_unlock(&sessions_mutex
);
289 int lttng_channel_enable(struct lttng_channel
*channel
)
293 mutex_lock(&sessions_mutex
);
294 if (channel
->channel_type
== METADATA_CHANNEL
) {
298 if (channel
->enabled
) {
302 /* Set transient enabler state to "enabled" */
304 lttng_session_sync_enablers(channel
->session
);
305 /* Set atomically the state to "enabled" */
306 ACCESS_ONCE(channel
->enabled
) = 1;
308 mutex_unlock(&sessions_mutex
);
312 int lttng_channel_disable(struct lttng_channel
*channel
)
316 mutex_lock(&sessions_mutex
);
317 if (channel
->channel_type
== METADATA_CHANNEL
) {
321 if (!channel
->enabled
) {
325 /* Set atomically the state to "disabled" */
326 ACCESS_ONCE(channel
->enabled
) = 0;
327 /* Set transient enabler state to "enabled" */
329 lttng_session_sync_enablers(channel
->session
);
331 mutex_unlock(&sessions_mutex
);
335 int lttng_event_enable(struct lttng_event
*event
)
339 mutex_lock(&sessions_mutex
);
340 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
344 if (event
->enabled
) {
348 switch (event
->instrumentation
) {
349 case LTTNG_KERNEL_TRACEPOINT
:
350 case LTTNG_KERNEL_SYSCALL
:
353 case LTTNG_KERNEL_KPROBE
:
354 case LTTNG_KERNEL_FUNCTION
:
355 case LTTNG_KERNEL_NOOP
:
356 ACCESS_ONCE(event
->enabled
) = 1;
358 case LTTNG_KERNEL_KRETPROBE
:
359 ret
= lttng_kretprobes_event_enable_state(event
, 1);
366 mutex_unlock(&sessions_mutex
);
370 int lttng_event_disable(struct lttng_event
*event
)
374 mutex_lock(&sessions_mutex
);
375 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
379 if (!event
->enabled
) {
383 switch (event
->instrumentation
) {
384 case LTTNG_KERNEL_TRACEPOINT
:
385 case LTTNG_KERNEL_SYSCALL
:
388 case LTTNG_KERNEL_KPROBE
:
389 case LTTNG_KERNEL_FUNCTION
:
390 case LTTNG_KERNEL_NOOP
:
391 ACCESS_ONCE(event
->enabled
) = 0;
393 case LTTNG_KERNEL_KRETPROBE
:
394 ret
= lttng_kretprobes_event_enable_state(event
, 0);
401 mutex_unlock(&sessions_mutex
);
405 static struct lttng_transport
*lttng_transport_find(const char *name
)
407 struct lttng_transport
*transport
;
409 list_for_each_entry(transport
, <tng_transport_list
, node
) {
410 if (!strcmp(transport
->name
, name
))
416 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
417 const char *transport_name
,
419 size_t subbuf_size
, size_t num_subbuf
,
420 unsigned int switch_timer_interval
,
421 unsigned int read_timer_interval
,
422 enum channel_type channel_type
)
424 struct lttng_channel
*chan
;
425 struct lttng_transport
*transport
= NULL
;
427 mutex_lock(&sessions_mutex
);
428 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
429 goto active
; /* Refuse to add channel to active session */
430 transport
= lttng_transport_find(transport_name
);
432 printk(KERN_WARNING
"LTTng transport %s not found\n",
436 if (!try_module_get(transport
->owner
)) {
437 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
440 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
443 chan
->session
= session
;
444 chan
->id
= session
->free_chan_id
++;
445 chan
->ops
= &transport
->ops
;
447 * Note: the channel creation op already writes into the packet
448 * headers. Therefore the "chan" information used as input
449 * should be already accessible.
451 chan
->chan
= transport
->ops
.channel_create(transport_name
,
452 chan
, buf_addr
, subbuf_size
, num_subbuf
,
453 switch_timer_interval
, read_timer_interval
);
458 chan
->transport
= transport
;
459 chan
->channel_type
= channel_type
;
460 list_add(&chan
->list
, &session
->chan
);
461 mutex_unlock(&sessions_mutex
);
468 module_put(transport
->owner
);
471 mutex_unlock(&sessions_mutex
);
476 * Only used internally at session destruction for per-cpu channels, and
477 * when metadata channel is released.
478 * Needs to be called with sessions mutex held.
481 void _lttng_channel_destroy(struct lttng_channel
*chan
)
483 chan
->ops
->channel_destroy(chan
->chan
);
484 module_put(chan
->transport
->owner
);
485 list_del(&chan
->list
);
486 lttng_destroy_context(chan
->ctx
);
490 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
492 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
494 /* Protect the metadata cache with the sessions_mutex. */
495 mutex_lock(&sessions_mutex
);
496 _lttng_channel_destroy(chan
);
497 mutex_unlock(&sessions_mutex
);
499 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
502 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
504 stream
->finalized
= 1;
505 wake_up_interruptible(&stream
->read_wait
);
509 * Supports event creation while tracing session is active.
510 * Needs to be called with sessions mutex held.
512 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
513 struct lttng_kernel_event
*event_param
,
515 const struct lttng_event_desc
*event_desc
,
516 enum lttng_kernel_instrumentation itype
)
518 struct lttng_session
*session
= chan
->session
;
519 struct lttng_event
*event
;
520 const char *event_name
;
521 struct hlist_head
*head
;
526 if (chan
->free_event_id
== -1U) {
532 case LTTNG_KERNEL_TRACEPOINT
:
533 event_name
= event_desc
->name
;
535 case LTTNG_KERNEL_KPROBE
:
536 case LTTNG_KERNEL_KRETPROBE
:
537 case LTTNG_KERNEL_FUNCTION
:
538 case LTTNG_KERNEL_NOOP
:
539 case LTTNG_KERNEL_SYSCALL
:
540 event_name
= event_param
->name
;
547 name_len
= strlen(event_name
);
548 hash
= jhash(event_name
, name_len
, 0);
549 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
550 lttng_hlist_for_each_entry(event
, head
, hlist
) {
551 WARN_ON_ONCE(!event
->desc
);
552 if (!strncmp(event
->desc
->name
, event_name
,
553 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
554 && chan
== event
->chan
) {
560 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
566 event
->filter
= filter
;
567 event
->id
= chan
->free_event_id
++;
568 event
->instrumentation
= itype
;
569 event
->evtype
= LTTNG_TYPE_EVENT
;
570 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
571 INIT_LIST_HEAD(&event
->enablers_ref_head
);
574 case LTTNG_KERNEL_TRACEPOINT
:
575 /* Event will be enabled by enabler sync. */
577 event
->registered
= 0;
578 event
->desc
= lttng_event_get(event_name
);
583 /* Populate lttng_event structure before event registration. */
586 case LTTNG_KERNEL_KPROBE
:
588 * Needs to be explicitly enabled after creation, since
589 * we may want to apply filters.
592 event
->registered
= 1;
594 * Populate lttng_event structure before event
598 ret
= lttng_kprobes_register(event_name
,
599 event_param
->u
.kprobe
.symbol_name
,
600 event_param
->u
.kprobe
.offset
,
601 event_param
->u
.kprobe
.addr
,
607 ret
= try_module_get(event
->desc
->owner
);
610 case LTTNG_KERNEL_KRETPROBE
:
612 struct lttng_event
*event_return
;
614 /* kretprobe defines 2 events */
616 * Needs to be explicitly enabled after creation, since
617 * we may want to apply filters.
620 event
->registered
= 1;
622 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
627 event_return
->chan
= chan
;
628 event_return
->filter
= filter
;
629 event_return
->id
= chan
->free_event_id
++;
630 event_return
->enabled
= 0;
631 event_return
->registered
= 1;
632 event_return
->instrumentation
= itype
;
634 * Populate lttng_event structure before kretprobe registration.
637 ret
= lttng_kretprobes_register(event_name
,
638 event_param
->u
.kretprobe
.symbol_name
,
639 event_param
->u
.kretprobe
.offset
,
640 event_param
->u
.kretprobe
.addr
,
641 event
, event_return
);
643 kmem_cache_free(event_cache
, event_return
);
647 /* Take 2 refs on the module: one per event. */
648 ret
= try_module_get(event
->desc
->owner
);
650 ret
= try_module_get(event
->desc
->owner
);
652 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
654 WARN_ON_ONCE(ret
> 0);
656 kmem_cache_free(event_cache
, event_return
);
657 module_put(event
->desc
->owner
);
658 module_put(event
->desc
->owner
);
659 goto statedump_error
;
661 list_add(&event_return
->list
, &chan
->session
->events
);
664 case LTTNG_KERNEL_FUNCTION
:
666 * Needs to be explicitly enabled after creation, since
667 * we may want to apply filters.
670 event
->registered
= 1;
672 * Populate lttng_event structure before event
676 ret
= lttng_ftrace_register(event_name
,
677 event_param
->u
.ftrace
.symbol_name
,
682 ret
= try_module_get(event
->desc
->owner
);
685 case LTTNG_KERNEL_NOOP
:
686 case LTTNG_KERNEL_SYSCALL
:
688 * Needs to be explicitly enabled after creation, since
689 * we may want to apply filters.
692 event
->registered
= 0;
693 event
->desc
= event_desc
;
704 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
705 WARN_ON_ONCE(ret
> 0);
707 goto statedump_error
;
709 hlist_add_head(&event
->hlist
, head
);
710 list_add(&event
->list
, &chan
->session
->events
);
714 /* If a statedump error occurs, events will not be readable. */
716 kmem_cache_free(event_cache
, event
);
724 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
725 struct lttng_kernel_event
*event_param
,
727 const struct lttng_event_desc
*event_desc
,
728 enum lttng_kernel_instrumentation itype
)
730 struct lttng_event
*event
;
732 mutex_lock(&sessions_mutex
);
733 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
735 mutex_unlock(&sessions_mutex
);
739 /* Only used for tracepoints for now. */
741 void register_event(struct lttng_event
*event
)
743 const struct lttng_event_desc
*desc
;
746 if (event
->registered
)
750 switch (event
->instrumentation
) {
751 case LTTNG_KERNEL_TRACEPOINT
:
752 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
753 desc
->probe_callback
,
756 case LTTNG_KERNEL_SYSCALL
:
757 ret
= lttng_syscall_filter_enable(event
->chan
,
760 case LTTNG_KERNEL_KPROBE
:
761 case LTTNG_KERNEL_KRETPROBE
:
762 case LTTNG_KERNEL_FUNCTION
:
763 case LTTNG_KERNEL_NOOP
:
770 event
->registered
= 1;
774 * Only used internally at session destruction.
776 int _lttng_event_unregister(struct lttng_event
*event
)
778 const struct lttng_event_desc
*desc
;
781 if (!event
->registered
)
785 switch (event
->instrumentation
) {
786 case LTTNG_KERNEL_TRACEPOINT
:
787 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
788 event
->desc
->probe_callback
,
791 case LTTNG_KERNEL_KPROBE
:
792 lttng_kprobes_unregister(event
);
795 case LTTNG_KERNEL_KRETPROBE
:
796 lttng_kretprobes_unregister(event
);
799 case LTTNG_KERNEL_FUNCTION
:
800 lttng_ftrace_unregister(event
);
803 case LTTNG_KERNEL_SYSCALL
:
804 ret
= lttng_syscall_filter_disable(event
->chan
,
807 case LTTNG_KERNEL_NOOP
:
814 event
->registered
= 0;
819 * Only used internally at session destruction.
822 void _lttng_event_destroy(struct lttng_event
*event
)
824 switch (event
->instrumentation
) {
825 case LTTNG_KERNEL_TRACEPOINT
:
826 lttng_event_put(event
->desc
);
828 case LTTNG_KERNEL_KPROBE
:
829 module_put(event
->desc
->owner
);
830 lttng_kprobes_destroy_private(event
);
832 case LTTNG_KERNEL_KRETPROBE
:
833 module_put(event
->desc
->owner
);
834 lttng_kretprobes_destroy_private(event
);
836 case LTTNG_KERNEL_FUNCTION
:
837 module_put(event
->desc
->owner
);
838 lttng_ftrace_destroy_private(event
);
840 case LTTNG_KERNEL_NOOP
:
841 case LTTNG_KERNEL_SYSCALL
:
846 list_del(&event
->list
);
847 lttng_destroy_context(event
->ctx
);
848 kmem_cache_free(event_cache
, event
);
851 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
857 mutex_lock(&sessions_mutex
);
859 /* track all pids: destroy tracker. */
860 if (session
->pid_tracker
) {
861 struct lttng_pid_tracker
*lpf
;
863 lpf
= session
->pid_tracker
;
864 rcu_assign_pointer(session
->pid_tracker
, NULL
);
866 lttng_pid_tracker_destroy(lpf
);
870 if (!session
->pid_tracker
) {
871 struct lttng_pid_tracker
*lpf
;
873 lpf
= lttng_pid_tracker_create();
878 ret
= lttng_pid_tracker_add(lpf
, pid
);
879 rcu_assign_pointer(session
->pid_tracker
, lpf
);
881 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
885 mutex_unlock(&sessions_mutex
);
889 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
895 mutex_lock(&sessions_mutex
);
897 /* untrack all pids: replace by empty tracker. */
898 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
899 struct lttng_pid_tracker
*lpf
;
901 lpf
= lttng_pid_tracker_create();
906 rcu_assign_pointer(session
->pid_tracker
, lpf
);
909 lttng_pid_tracker_destroy(old_lpf
);
912 if (!session
->pid_tracker
) {
916 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
919 mutex_unlock(&sessions_mutex
);
924 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
926 struct lttng_session
*session
= m
->private;
927 struct lttng_pid_tracker
*lpf
;
928 struct lttng_pid_hash_node
*e
;
931 mutex_lock(&sessions_mutex
);
932 lpf
= session
->pid_tracker
;
934 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
935 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
937 lttng_hlist_for_each_entry(e
, head
, hlist
) {
943 /* PID tracker disabled. */
944 if (iter
>= *pos
&& iter
== 0) {
945 return session
; /* empty tracker */
953 /* Called with sessions_mutex held. */
955 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
957 struct lttng_session
*session
= m
->private;
958 struct lttng_pid_tracker
*lpf
;
959 struct lttng_pid_hash_node
*e
;
963 lpf
= session
->pid_tracker
;
965 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
966 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
968 lttng_hlist_for_each_entry(e
, head
, hlist
) {
974 /* PID tracker disabled. */
975 if (iter
>= *ppos
&& iter
== 0)
976 return session
; /* empty tracker */
985 void pid_list_stop(struct seq_file
*m
, void *p
)
987 mutex_unlock(&sessions_mutex
);
991 int pid_list_show(struct seq_file
*m
, void *p
)
995 if (p
== m
->private) {
996 /* Tracker disabled. */
999 const struct lttng_pid_hash_node
*e
= p
;
1001 pid
= lttng_pid_tracker_get_node_pid(e
);
1003 seq_printf(m
, "process { pid = %d; };\n", pid
);
1008 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
1009 .start
= pid_list_start
,
1010 .next
= pid_list_next
,
1011 .stop
= pid_list_stop
,
1012 .show
= pid_list_show
,
1016 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
1018 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
1022 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
1024 struct seq_file
*m
= file
->private_data
;
1025 struct lttng_session
*session
= m
->private;
1028 WARN_ON_ONCE(!session
);
1029 ret
= seq_release(inode
, file
);
1030 if (!ret
&& session
)
1031 fput(session
->file
);
1035 const struct file_operations lttng_tracker_pids_list_fops
= {
1036 .owner
= THIS_MODULE
,
1037 .open
= lttng_tracker_pids_list_open
,
1039 .llseek
= seq_lseek
,
1040 .release
= lttng_tracker_pids_list_release
,
1043 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
1045 struct file
*tracker_pids_list_file
;
1049 file_fd
= lttng_get_unused_fd();
1055 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
1056 <tng_tracker_pids_list_fops
,
1058 if (IS_ERR(tracker_pids_list_file
)) {
1059 ret
= PTR_ERR(tracker_pids_list_file
);
1062 if (atomic_long_add_unless(&session
->file
->f_count
,
1063 1, INT_MAX
) == INT_MAX
) {
1064 goto refcount_error
;
1066 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1069 m
= tracker_pids_list_file
->private_data
;
1070 m
->private = session
;
1071 fd_install(file_fd
, tracker_pids_list_file
);
1076 atomic_long_dec(&session
->file
->f_count
);
1078 fput(tracker_pids_list_file
);
1080 put_unused_fd(file_fd
);
1086 * Enabler management.
1089 int lttng_match_enabler_wildcard(const char *desc_name
,
1092 /* Compare excluding final '*' */
1093 if (strncmp(desc_name
, name
, strlen(name
) - 1))
1099 int lttng_match_enabler_name(const char *desc_name
,
1102 if (strcmp(desc_name
, name
))
1108 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1109 struct lttng_enabler
*enabler
)
1111 const char *desc_name
, *enabler_name
;
1113 enabler_name
= enabler
->event_param
.name
;
1114 switch (enabler
->event_param
.instrumentation
) {
1115 case LTTNG_KERNEL_TRACEPOINT
:
1116 desc_name
= desc
->name
;
1118 case LTTNG_KERNEL_SYSCALL
:
1119 desc_name
= desc
->name
;
1120 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1121 desc_name
+= strlen("compat_");
1122 if (!strncmp(desc_name
, "syscall_exit_",
1123 strlen("syscall_exit_"))) {
1124 desc_name
+= strlen("syscall_exit_");
1125 } else if (!strncmp(desc_name
, "syscall_entry_",
1126 strlen("syscall_entry_"))) {
1127 desc_name
+= strlen("syscall_entry_");
1137 switch (enabler
->type
) {
1138 case LTTNG_ENABLER_WILDCARD
:
1139 return lttng_match_enabler_wildcard(desc_name
, enabler_name
);
1140 case LTTNG_ENABLER_NAME
:
1141 return lttng_match_enabler_name(desc_name
, enabler_name
);
1148 int lttng_event_match_enabler(struct lttng_event
*event
,
1149 struct lttng_enabler
*enabler
)
1151 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1153 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1154 && event
->chan
== enabler
->chan
)
1161 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1162 struct lttng_enabler
*enabler
)
1164 struct lttng_enabler_ref
*enabler_ref
;
1166 list_for_each_entry(enabler_ref
,
1167 &event
->enablers_ref_head
, node
) {
1168 if (enabler_ref
->ref
== enabler
)
1175 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1177 struct lttng_session
*session
= enabler
->chan
->session
;
1178 struct lttng_probe_desc
*probe_desc
;
1179 const struct lttng_event_desc
*desc
;
1181 struct list_head
*probe_list
;
1183 probe_list
= lttng_get_probe_list_head();
1185 * For each probe event, if we find that a probe event matches
1186 * our enabler, create an associated lttng_event if not
1189 list_for_each_entry(probe_desc
, probe_list
, head
) {
1190 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1192 struct hlist_head
*head
;
1193 const char *event_name
;
1196 struct lttng_event
*event
;
1198 desc
= probe_desc
->event_desc
[i
];
1199 if (!lttng_desc_match_enabler(desc
, enabler
))
1201 event_name
= desc
->name
;
1202 name_len
= strlen(event_name
);
1205 * Check if already created.
1207 hash
= jhash(event_name
, name_len
, 0);
1208 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1209 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1210 if (event
->desc
== desc
1211 && event
->chan
== enabler
->chan
)
1218 * We need to create an event for this
1221 event
= _lttng_event_create(enabler
->chan
,
1223 LTTNG_KERNEL_TRACEPOINT
);
1225 printk(KERN_INFO
"Unable to create event %s\n",
1226 probe_desc
->event_desc
[i
]->name
);
1233 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1237 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1242 * Create struct lttng_event if it is missing and present in the list of
1243 * tracepoint probes.
1244 * Should be called with sessions mutex held.
1247 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1249 switch (enabler
->event_param
.instrumentation
) {
1250 case LTTNG_KERNEL_TRACEPOINT
:
1251 lttng_create_tracepoint_if_missing(enabler
);
1253 case LTTNG_KERNEL_SYSCALL
:
1254 lttng_create_syscall_if_missing(enabler
);
1263 * Create events associated with an enabler (if not already present),
1264 * and add backward reference from the event to the enabler.
1265 * Should be called with sessions mutex held.
1268 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1270 struct lttng_session
*session
= enabler
->chan
->session
;
1271 struct lttng_event
*event
;
1273 /* First ensure that probe events are created for this enabler. */
1274 lttng_create_event_if_missing(enabler
);
1276 /* For each event matching enabler in session event list. */
1277 list_for_each_entry(event
, &session
->events
, list
) {
1278 struct lttng_enabler_ref
*enabler_ref
;
1280 if (!lttng_event_match_enabler(event
, enabler
))
1282 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1285 * If no backward ref, create it.
1286 * Add backward ref from event to enabler.
1288 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1291 enabler_ref
->ref
= enabler
;
1292 list_add(&enabler_ref
->node
,
1293 &event
->enablers_ref_head
);
1297 * Link filter bytecodes if not linked yet.
1299 lttng_enabler_event_link_bytecode(event
, enabler
);
1301 /* TODO: merge event context. */
1307 * Called at module load: connect the probe on all enablers matching
1309 * Called with sessions lock held.
1311 int lttng_fix_pending_events(void)
1313 struct lttng_session
*session
;
1315 list_for_each_entry(session
, &sessions
, list
)
1316 lttng_session_lazy_sync_enablers(session
);
1320 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1321 struct lttng_kernel_event
*event_param
,
1322 struct lttng_channel
*chan
)
1324 struct lttng_enabler
*enabler
;
1326 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1329 enabler
->type
= type
;
1330 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1331 memcpy(&enabler
->event_param
, event_param
,
1332 sizeof(enabler
->event_param
));
1333 enabler
->chan
= chan
;
1335 enabler
->enabled
= 0;
1336 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1337 mutex_lock(&sessions_mutex
);
1338 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1339 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1340 mutex_unlock(&sessions_mutex
);
1344 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1346 mutex_lock(&sessions_mutex
);
1347 enabler
->enabled
= 1;
1348 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1349 mutex_unlock(&sessions_mutex
);
1353 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1355 mutex_lock(&sessions_mutex
);
1356 enabler
->enabled
= 0;
1357 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1358 mutex_unlock(&sessions_mutex
);
1362 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1363 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1365 struct lttng_filter_bytecode_node
*bytecode_node
;
1366 uint32_t bytecode_len
;
1369 ret
= get_user(bytecode_len
, &bytecode
->len
);
1372 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1376 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1377 sizeof(*bytecode
) + bytecode_len
);
1380 bytecode_node
->enabler
= enabler
;
1381 /* Enforce length based on allocated size */
1382 bytecode_node
->bc
.len
= bytecode_len
;
1383 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1384 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1388 kfree(bytecode_node
);
1392 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1393 struct lttng_kernel_context
*context_param
)
1399 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1401 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1403 /* Destroy filter bytecode */
1404 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1405 &enabler
->filter_bytecode_head
, node
) {
1409 /* Destroy contexts */
1410 lttng_destroy_context(enabler
->ctx
);
1412 list_del(&enabler
->node
);
1417 * lttng_session_sync_enablers should be called just before starting a
1419 * Should be called with sessions mutex held.
1422 void lttng_session_sync_enablers(struct lttng_session
*session
)
1424 struct lttng_enabler
*enabler
;
1425 struct lttng_event
*event
;
1427 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1428 lttng_enabler_ref_events(enabler
);
1430 * For each event, if at least one of its enablers is enabled,
1431 * and its channel and session transient states are enabled, we
1432 * enable the event, else we disable it.
1434 list_for_each_entry(event
, &session
->events
, list
) {
1435 struct lttng_enabler_ref
*enabler_ref
;
1436 struct lttng_bytecode_runtime
*runtime
;
1437 int enabled
= 0, has_enablers_without_bytecode
= 0;
1439 switch (event
->instrumentation
) {
1440 case LTTNG_KERNEL_TRACEPOINT
:
1441 case LTTNG_KERNEL_SYSCALL
:
1443 list_for_each_entry(enabler_ref
,
1444 &event
->enablers_ref_head
, node
) {
1445 if (enabler_ref
->ref
->enabled
) {
1452 /* Not handled with lazy sync. */
1456 * Enabled state is based on union of enablers, with
1457 * intesection of session and channel transient enable
1460 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1462 ACCESS_ONCE(event
->enabled
) = enabled
;
1464 * Sync tracepoint registration with event enabled
1468 register_event(event
);
1470 _lttng_event_unregister(event
);
1473 /* Check if has enablers without bytecode enabled */
1474 list_for_each_entry(enabler_ref
,
1475 &event
->enablers_ref_head
, node
) {
1476 if (enabler_ref
->ref
->enabled
1477 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1478 has_enablers_without_bytecode
= 1;
1482 event
->has_enablers_without_bytecode
=
1483 has_enablers_without_bytecode
;
1485 /* Enable filters */
1486 list_for_each_entry(runtime
,
1487 &event
->bytecode_runtime_head
, node
)
1488 lttng_filter_sync_state(runtime
);
1493 * Apply enablers to session events, adding events to session if need
1494 * be. It is required after each modification applied to an active
1495 * session, and right before session "start".
1496 * "lazy" sync means we only sync if required.
1497 * Should be called with sessions mutex held.
1500 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1502 /* We can skip if session is not active */
1503 if (!session
->active
)
1505 lttng_session_sync_enablers(session
);
1509 * Serialize at most one packet worth of metadata into a metadata
1511 * We grab the metadata cache mutex to get exclusive access to our metadata
1512 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1513 * allows us to do racy operations such as looking for remaining space left in
1514 * packet and write, since mutual exclusion protects us from concurrent writes.
1515 * Mutual exclusion on the metadata cache allow us to read the cache content
1516 * without racing against reallocation of the cache by updates.
1517 * Returns the number of bytes written in the channel, 0 if no data
1518 * was written and a negative value on error.
1520 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1521 struct channel
*chan
)
1523 struct lib_ring_buffer_ctx ctx
;
1525 size_t len
, reserve_len
;
1528 * Ensure we support mutiple get_next / put sequences followed by
1529 * put_next. The metadata cache lock protects reading the metadata
1530 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1531 * "flush" operations on the buffer invoked by different processes.
1532 * Moreover, since the metadata cache memory can be reallocated, we
1533 * need to have exclusive access against updates even though we only
1536 mutex_lock(&stream
->metadata_cache
->lock
);
1537 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1538 if (stream
->metadata_in
!= stream
->metadata_out
)
1541 len
= stream
->metadata_cache
->metadata_written
-
1542 stream
->metadata_in
;
1545 reserve_len
= min_t(size_t,
1546 stream
->transport
->ops
.packet_avail_size(chan
),
1548 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1551 * If reservation failed, return an error to the caller.
1553 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1555 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1558 stream
->transport
->ops
.event_write(&ctx
,
1559 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1561 stream
->transport
->ops
.event_commit(&ctx
);
1562 stream
->metadata_in
+= reserve_len
;
1566 mutex_unlock(&stream
->metadata_cache
->lock
);
1571 * Write the metadata to the metadata cache.
1572 * Must be called with sessions_mutex held.
1573 * The metadata cache lock protects us from concurrent read access from
1574 * thread outputting metadata content to ring buffer.
1576 int lttng_metadata_printf(struct lttng_session
*session
,
1577 const char *fmt
, ...)
1582 struct lttng_metadata_stream
*stream
;
1584 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
1587 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1593 mutex_lock(&session
->metadata_cache
->lock
);
1594 if (session
->metadata_cache
->metadata_written
+ len
>
1595 session
->metadata_cache
->cache_alloc
) {
1596 char *tmp_cache_realloc
;
1597 unsigned int tmp_cache_alloc_size
;
1599 tmp_cache_alloc_size
= max_t(unsigned int,
1600 session
->metadata_cache
->cache_alloc
+ len
,
1601 session
->metadata_cache
->cache_alloc
<< 1);
1602 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
1603 if (!tmp_cache_realloc
)
1605 if (session
->metadata_cache
->data
) {
1606 memcpy(tmp_cache_realloc
,
1607 session
->metadata_cache
->data
,
1608 session
->metadata_cache
->cache_alloc
);
1609 vfree(session
->metadata_cache
->data
);
1612 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1613 session
->metadata_cache
->data
= tmp_cache_realloc
;
1615 memcpy(session
->metadata_cache
->data
+
1616 session
->metadata_cache
->metadata_written
,
1618 session
->metadata_cache
->metadata_written
+= len
;
1619 mutex_unlock(&session
->metadata_cache
->lock
);
1622 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1623 wake_up_interruptible(&stream
->read_wait
);
1628 mutex_unlock(&session
->metadata_cache
->lock
);
1634 * Must be called with sessions_mutex held.
1637 int _lttng_field_statedump(struct lttng_session
*session
,
1638 const struct lttng_event_field
*field
)
1642 switch (field
->type
.atype
) {
1644 ret
= lttng_metadata_printf(session
,
1645 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1646 field
->type
.u
.basic
.integer
.size
,
1647 field
->type
.u
.basic
.integer
.alignment
,
1648 field
->type
.u
.basic
.integer
.signedness
,
1649 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
1651 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1654 field
->type
.u
.basic
.integer
.base
,
1655 #if __BYTE_ORDER == __BIG_ENDIAN
1656 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1658 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1663 ret
= lttng_metadata_printf(session
,
1665 field
->type
.u
.basic
.enumeration
.name
,
1670 const struct lttng_basic_type
*elem_type
;
1672 elem_type
= &field
->type
.u
.array
.elem_type
;
1673 ret
= lttng_metadata_printf(session
,
1674 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1675 elem_type
->u
.basic
.integer
.size
,
1676 elem_type
->u
.basic
.integer
.alignment
,
1677 elem_type
->u
.basic
.integer
.signedness
,
1678 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1680 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1683 elem_type
->u
.basic
.integer
.base
,
1684 #if __BYTE_ORDER == __BIG_ENDIAN
1685 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1687 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1689 field
->name
, field
->type
.u
.array
.length
);
1692 case atype_sequence
:
1694 const struct lttng_basic_type
*elem_type
;
1695 const struct lttng_basic_type
*length_type
;
1697 elem_type
= &field
->type
.u
.sequence
.elem_type
;
1698 length_type
= &field
->type
.u
.sequence
.length_type
;
1699 ret
= lttng_metadata_printf(session
,
1700 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1701 length_type
->u
.basic
.integer
.size
,
1702 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
1703 length_type
->u
.basic
.integer
.signedness
,
1704 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1706 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1709 length_type
->u
.basic
.integer
.base
,
1710 #if __BYTE_ORDER == __BIG_ENDIAN
1711 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1713 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1719 ret
= lttng_metadata_printf(session
,
1720 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1721 elem_type
->u
.basic
.integer
.size
,
1722 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
1723 elem_type
->u
.basic
.integer
.signedness
,
1724 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1726 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1729 elem_type
->u
.basic
.integer
.base
,
1730 #if __BYTE_ORDER == __BIG_ENDIAN
1731 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1733 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1741 /* Default encoding is UTF8 */
1742 ret
= lttng_metadata_printf(session
,
1744 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
1745 " { encoding = ASCII; }" : "",
1756 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
1757 struct lttng_ctx
*ctx
)
1764 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
1765 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
1767 ret
= _lttng_field_statedump(session
, &field
->event_field
);
1775 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
1776 struct lttng_event
*event
)
1778 const struct lttng_event_desc
*desc
= event
->desc
;
1782 for (i
= 0; i
< desc
->nr_fields
; i
++) {
1783 const struct lttng_event_field
*field
= &desc
->fields
[i
];
1785 ret
= _lttng_field_statedump(session
, field
);
1793 * Must be called with sessions_mutex held.
1796 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
1797 struct lttng_channel
*chan
,
1798 struct lttng_event
*event
)
1802 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1804 if (chan
->channel_type
== METADATA_CHANNEL
)
1807 ret
= lttng_metadata_printf(session
,
1811 " stream_id = %u;\n",
1819 ret
= lttng_metadata_printf(session
,
1820 " context := struct {\n");
1824 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
1828 ret
= lttng_metadata_printf(session
,
1834 ret
= lttng_metadata_printf(session
,
1835 " fields := struct {\n"
1840 ret
= _lttng_fields_metadata_statedump(session
, event
);
1845 * LTTng space reservation can only reserve multiples of the
1848 ret
= lttng_metadata_printf(session
,
1854 event
->metadata_dumped
= 1;
1861 * Must be called with sessions_mutex held.
1864 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1865 struct lttng_channel
*chan
)
1869 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1872 if (chan
->channel_type
== METADATA_CHANNEL
)
1875 WARN_ON_ONCE(!chan
->header_type
);
1876 ret
= lttng_metadata_printf(session
,
1879 " event.header := %s;\n"
1880 " packet.context := struct packet_context;\n",
1882 chan
->header_type
== 1 ? "struct event_header_compact" :
1883 "struct event_header_large");
1888 ret
= lttng_metadata_printf(session
,
1889 " event.context := struct {\n");
1893 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1897 ret
= lttng_metadata_printf(session
,
1903 ret
= lttng_metadata_printf(session
,
1906 chan
->metadata_dumped
= 1;
1912 * Must be called with sessions_mutex held.
1915 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1917 return lttng_metadata_printf(session
,
1918 "struct packet_context {\n"
1919 " uint64_clock_monotonic_t timestamp_begin;\n"
1920 " uint64_clock_monotonic_t timestamp_end;\n"
1921 " uint64_t content_size;\n"
1922 " uint64_t packet_size;\n"
1923 " unsigned long events_discarded;\n"
1924 " uint32_t cpu_id;\n"
1931 * id: range: 0 - 30.
1932 * id 31 is reserved to indicate an extended header.
1935 * id: range: 0 - 65534.
1936 * id 65535 is reserved to indicate an extended header.
1938 * Must be called with sessions_mutex held.
1941 int _lttng_event_header_declare(struct lttng_session
*session
)
1943 return lttng_metadata_printf(session
,
1944 "struct event_header_compact {\n"
1945 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1948 " uint27_clock_monotonic_t timestamp;\n"
1952 " uint64_clock_monotonic_t timestamp;\n"
1957 "struct event_header_large {\n"
1958 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1961 " uint32_clock_monotonic_t timestamp;\n"
1965 " uint64_clock_monotonic_t timestamp;\n"
1969 lttng_alignof(uint32_t) * CHAR_BIT
,
1970 lttng_alignof(uint16_t) * CHAR_BIT
1975 * Approximation of NTP time of day to clock monotonic correlation,
1976 * taken at start of trace.
1977 * Yes, this is only an approximation. Yes, we can (and will) do better
1978 * in future versions.
1979 * Return 0 if offset is negative. It may happen if the system sets
1980 * the REALTIME clock to 0 after boot.
1983 uint64_t measure_clock_offset(void)
1985 uint64_t monotonic_avg
, monotonic
[2], realtime
;
1987 struct timespec rts
= { 0, 0 };
1988 unsigned long flags
;
1990 /* Disable interrupts to increase correlation precision. */
1991 local_irq_save(flags
);
1992 monotonic
[0] = trace_clock_read64();
1993 getnstimeofday(&rts
);
1994 monotonic
[1] = trace_clock_read64();
1995 local_irq_restore(flags
);
1997 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
1998 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1999 realtime
+= rts
.tv_nsec
;
2000 offset
= (int64_t) realtime
- monotonic_avg
;
2007 * Output metadata into this session's metadata buffers.
2008 * Must be called with sessions_mutex held.
2011 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2013 unsigned char *uuid_c
= session
->uuid
.b
;
2014 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2015 struct lttng_channel
*chan
;
2016 struct lttng_event
*event
;
2019 if (!ACCESS_ONCE(session
->active
))
2021 if (session
->metadata_dumped
)
2024 snprintf(uuid_s
, sizeof(uuid_s
),
2025 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2026 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2027 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2028 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2029 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2031 ret
= lttng_metadata_printf(session
,
2032 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2033 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2034 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2035 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2036 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2037 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2038 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2044 " byte_order = %s;\n"
2045 " packet.header := struct {\n"
2046 " uint32_t magic;\n"
2047 " uint8_t uuid[16];\n"
2048 " uint32_t stream_id;\n"
2051 lttng_alignof(uint8_t) * CHAR_BIT
,
2052 lttng_alignof(uint16_t) * CHAR_BIT
,
2053 lttng_alignof(uint32_t) * CHAR_BIT
,
2054 lttng_alignof(uint64_t) * CHAR_BIT
,
2055 sizeof(unsigned long) * CHAR_BIT
,
2056 lttng_alignof(unsigned long) * CHAR_BIT
,
2060 #if __BYTE_ORDER == __BIG_ENDIAN
2069 ret
= lttng_metadata_printf(session
,
2071 " hostname = \"%s\";\n"
2072 " domain = \"kernel\";\n"
2073 " sysname = \"%s\";\n"
2074 " kernel_release = \"%s\";\n"
2075 " kernel_version = \"%s\";\n"
2076 " tracer_name = \"lttng-modules\";\n"
2077 " tracer_major = %d;\n"
2078 " tracer_minor = %d;\n"
2079 " tracer_patchlevel = %d;\n"
2081 current
->nsproxy
->uts_ns
->name
.nodename
,
2085 LTTNG_MODULES_MAJOR_VERSION
,
2086 LTTNG_MODULES_MINOR_VERSION
,
2087 LTTNG_MODULES_PATCHLEVEL_VERSION
2092 ret
= lttng_metadata_printf(session
,
2100 if (!trace_clock_uuid(clock_uuid_s
)) {
2101 ret
= lttng_metadata_printf(session
,
2102 " uuid = \"%s\";\n",
2109 ret
= lttng_metadata_printf(session
,
2110 " description = \"Monotonic Clock\";\n"
2111 " freq = %llu; /* Frequency, in Hz */\n"
2112 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2115 (unsigned long long) trace_clock_freq(),
2116 (unsigned long long) measure_clock_offset()
2121 ret
= lttng_metadata_printf(session
,
2122 "typealias integer {\n"
2123 " size = 27; align = 1; signed = false;\n"
2124 " map = clock.monotonic.value;\n"
2125 "} := uint27_clock_monotonic_t;\n"
2127 "typealias integer {\n"
2128 " size = 32; align = %u; signed = false;\n"
2129 " map = clock.monotonic.value;\n"
2130 "} := uint32_clock_monotonic_t;\n"
2132 "typealias integer {\n"
2133 " size = 64; align = %u; signed = false;\n"
2134 " map = clock.monotonic.value;\n"
2135 "} := uint64_clock_monotonic_t;\n\n",
2136 lttng_alignof(uint32_t) * CHAR_BIT
,
2137 lttng_alignof(uint64_t) * CHAR_BIT
2142 ret
= _lttng_stream_packet_context_declare(session
);
2146 ret
= _lttng_event_header_declare(session
);
2151 list_for_each_entry(chan
, &session
->chan
, list
) {
2152 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2157 list_for_each_entry(event
, &session
->events
, list
) {
2158 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2162 session
->metadata_dumped
= 1;
2168 * lttng_transport_register - LTT transport registration
2169 * @transport: transport structure
2171 * Registers a transport which can be used as output to extract the data out of
2172 * LTTng. The module calling this registration function must ensure that no
2173 * trap-inducing code will be executed by the transport functions. E.g.
2174 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2175 * is made visible to the transport function. This registration acts as a
2176 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2177 * after its registration must it synchronize the TLBs.
2179 void lttng_transport_register(struct lttng_transport
*transport
)
2182 * Make sure no page fault can be triggered by the module about to be
2183 * registered. We deal with this here so we don't have to call
2184 * vmalloc_sync_all() in each module's init.
2186 wrapper_vmalloc_sync_all();
2188 mutex_lock(&sessions_mutex
);
2189 list_add_tail(&transport
->node
, <tng_transport_list
);
2190 mutex_unlock(&sessions_mutex
);
2192 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2195 * lttng_transport_unregister - LTT transport unregistration
2196 * @transport: transport structure
2198 void lttng_transport_unregister(struct lttng_transport
*transport
)
2200 mutex_lock(&sessions_mutex
);
2201 list_del(&transport
->node
);
2202 mutex_unlock(&sessions_mutex
);
2204 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2206 static int __init
lttng_events_init(void)
2210 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2213 ret
= wrapper_get_pfnblock_flags_mask_init();
2216 ret
= wrapper_get_pageblock_flags_mask_init();
2219 ret
= lttng_context_init();
2222 ret
= lttng_tracepoint_init();
2225 event_cache
= KMEM_CACHE(lttng_event
, 0);
2230 ret
= lttng_abi_init();
2233 ret
= lttng_logger_init();
2241 kmem_cache_destroy(event_cache
);
2243 lttng_tracepoint_exit();
2245 lttng_context_exit();
2249 module_init(lttng_events_init
);
2251 static void __exit
lttng_events_exit(void)
2253 struct lttng_session
*session
, *tmpsession
;
2255 lttng_logger_exit();
2257 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
2258 lttng_session_destroy(session
);
2259 kmem_cache_destroy(event_cache
);
2260 lttng_tracepoint_exit();
2261 lttng_context_exit();
2264 module_exit(lttng_events_exit
);
2266 MODULE_LICENSE("GPL and additional rights");
2267 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2268 MODULE_DESCRIPTION("LTTng Events");
2269 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
2270 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
2271 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
2272 LTTNG_MODULES_EXTRAVERSION
);