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>
56 #define METADATA_CACHE_DEFAULT_SIZE 4096
58 static LIST_HEAD(sessions
);
59 static LIST_HEAD(lttng_transport_list
);
61 * Protect the sessions and metadata caches.
63 static DEFINE_MUTEX(sessions_mutex
);
64 static struct kmem_cache
*event_cache
;
66 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
67 static void lttng_session_sync_enablers(struct lttng_session
*session
);
68 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
70 static void _lttng_event_destroy(struct lttng_event
*event
);
71 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
72 static int _lttng_event_unregister(struct lttng_event
*event
);
74 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
75 struct lttng_channel
*chan
,
76 struct lttng_event
*event
);
78 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
80 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
82 void synchronize_trace(void)
85 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
86 #ifdef CONFIG_PREEMPT_RT_FULL
89 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
90 #ifdef CONFIG_PREEMPT_RT
93 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
96 void lttng_lock_sessions(void)
98 mutex_lock(&sessions_mutex
);
101 void lttng_unlock_sessions(void)
103 mutex_unlock(&sessions_mutex
);
107 * Called with sessions lock held.
109 int lttng_session_active(void)
111 struct lttng_session
*iter
;
113 list_for_each_entry(iter
, &sessions
, list
) {
120 struct lttng_session
*lttng_session_create(void)
122 struct lttng_session
*session
;
123 struct lttng_metadata_cache
*metadata_cache
;
126 mutex_lock(&sessions_mutex
);
127 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
130 INIT_LIST_HEAD(&session
->chan
);
131 INIT_LIST_HEAD(&session
->events
);
132 uuid_le_gen(&session
->uuid
);
134 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
137 goto err_free_session
;
138 metadata_cache
->data
= lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
139 if (!metadata_cache
->data
)
141 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
142 kref_init(&metadata_cache
->refcount
);
143 mutex_init(&metadata_cache
->lock
);
144 session
->metadata_cache
= metadata_cache
;
145 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
146 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
147 sizeof(metadata_cache
->uuid
));
148 INIT_LIST_HEAD(&session
->enablers_head
);
149 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
150 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
151 list_add(&session
->list
, &sessions
);
152 mutex_unlock(&sessions_mutex
);
156 kfree(metadata_cache
);
160 mutex_unlock(&sessions_mutex
);
164 void metadata_cache_destroy(struct kref
*kref
)
166 struct lttng_metadata_cache
*cache
=
167 container_of(kref
, struct lttng_metadata_cache
, refcount
);
172 void lttng_session_destroy(struct lttng_session
*session
)
174 struct lttng_channel
*chan
, *tmpchan
;
175 struct lttng_event
*event
, *tmpevent
;
176 struct lttng_metadata_stream
*metadata_stream
;
177 struct lttng_enabler
*enabler
, *tmpenabler
;
180 mutex_lock(&sessions_mutex
);
181 ACCESS_ONCE(session
->active
) = 0;
182 list_for_each_entry(chan
, &session
->chan
, list
) {
183 ret
= lttng_syscalls_unregister(chan
);
186 list_for_each_entry(event
, &session
->events
, list
) {
187 ret
= _lttng_event_unregister(event
);
190 synchronize_trace(); /* Wait for in-flight events to complete */
191 list_for_each_entry_safe(enabler
, tmpenabler
,
192 &session
->enablers_head
, node
)
193 lttng_enabler_destroy(enabler
);
194 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
195 _lttng_event_destroy(event
);
196 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
197 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
198 _lttng_channel_destroy(chan
);
200 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
201 _lttng_metadata_channel_hangup(metadata_stream
);
202 if (session
->pid_tracker
)
203 lttng_pid_tracker_destroy(session
->pid_tracker
);
204 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
205 list_del(&session
->list
);
206 mutex_unlock(&sessions_mutex
);
210 int lttng_session_enable(struct lttng_session
*session
)
213 struct lttng_channel
*chan
;
215 mutex_lock(&sessions_mutex
);
216 if (session
->active
) {
221 /* Set transient enabler state to "enabled" */
225 * Snapshot the number of events per channel to know the type of header
228 list_for_each_entry(chan
, &session
->chan
, list
) {
229 if (chan
->header_type
)
230 continue; /* don't change it if session stop/restart */
231 if (chan
->free_event_id
< 31)
232 chan
->header_type
= 1; /* compact */
234 chan
->header_type
= 2; /* large */
237 /* We need to sync enablers with session before activation. */
238 lttng_session_sync_enablers(session
);
240 ACCESS_ONCE(session
->active
) = 1;
241 ACCESS_ONCE(session
->been_active
) = 1;
242 ret
= _lttng_session_metadata_statedump(session
);
244 ACCESS_ONCE(session
->active
) = 0;
247 ret
= lttng_statedump_start(session
);
249 ACCESS_ONCE(session
->active
) = 0;
251 mutex_unlock(&sessions_mutex
);
255 int lttng_session_disable(struct lttng_session
*session
)
259 mutex_lock(&sessions_mutex
);
260 if (!session
->active
) {
264 ACCESS_ONCE(session
->active
) = 0;
266 /* Set transient enabler state to "disabled" */
268 lttng_session_sync_enablers(session
);
270 mutex_unlock(&sessions_mutex
);
274 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
277 struct lttng_channel
*chan
;
278 struct lttng_event
*event
;
279 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
280 struct lttng_metadata_stream
*stream
;
282 mutex_lock(&sessions_mutex
);
283 if (!session
->active
) {
288 mutex_lock(&cache
->lock
);
289 memset(cache
->data
, 0, cache
->cache_alloc
);
290 cache
->metadata_written
= 0;
292 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
293 stream
->metadata_out
= 0;
294 stream
->metadata_in
= 0;
296 mutex_unlock(&cache
->lock
);
298 session
->metadata_dumped
= 0;
299 list_for_each_entry(chan
, &session
->chan
, list
) {
300 chan
->metadata_dumped
= 0;
303 list_for_each_entry(event
, &session
->events
, list
) {
304 event
->metadata_dumped
= 0;
307 ret
= _lttng_session_metadata_statedump(session
);
310 mutex_unlock(&sessions_mutex
);
316 int lttng_channel_enable(struct lttng_channel
*channel
)
320 mutex_lock(&sessions_mutex
);
321 if (channel
->channel_type
== METADATA_CHANNEL
) {
325 if (channel
->enabled
) {
329 /* Set transient enabler state to "enabled" */
331 lttng_session_sync_enablers(channel
->session
);
332 /* Set atomically the state to "enabled" */
333 ACCESS_ONCE(channel
->enabled
) = 1;
335 mutex_unlock(&sessions_mutex
);
339 int lttng_channel_disable(struct lttng_channel
*channel
)
343 mutex_lock(&sessions_mutex
);
344 if (channel
->channel_type
== METADATA_CHANNEL
) {
348 if (!channel
->enabled
) {
352 /* Set atomically the state to "disabled" */
353 ACCESS_ONCE(channel
->enabled
) = 0;
354 /* Set transient enabler state to "enabled" */
356 lttng_session_sync_enablers(channel
->session
);
358 mutex_unlock(&sessions_mutex
);
362 int lttng_event_enable(struct lttng_event
*event
)
366 mutex_lock(&sessions_mutex
);
367 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
371 if (event
->enabled
) {
375 switch (event
->instrumentation
) {
376 case LTTNG_KERNEL_TRACEPOINT
:
377 case LTTNG_KERNEL_SYSCALL
:
380 case LTTNG_KERNEL_KPROBE
:
381 case LTTNG_KERNEL_FUNCTION
:
382 case LTTNG_KERNEL_NOOP
:
383 ACCESS_ONCE(event
->enabled
) = 1;
385 case LTTNG_KERNEL_KRETPROBE
:
386 ret
= lttng_kretprobes_event_enable_state(event
, 1);
393 mutex_unlock(&sessions_mutex
);
397 int lttng_event_disable(struct lttng_event
*event
)
401 mutex_lock(&sessions_mutex
);
402 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
406 if (!event
->enabled
) {
410 switch (event
->instrumentation
) {
411 case LTTNG_KERNEL_TRACEPOINT
:
412 case LTTNG_KERNEL_SYSCALL
:
415 case LTTNG_KERNEL_KPROBE
:
416 case LTTNG_KERNEL_FUNCTION
:
417 case LTTNG_KERNEL_NOOP
:
418 ACCESS_ONCE(event
->enabled
) = 0;
420 case LTTNG_KERNEL_KRETPROBE
:
421 ret
= lttng_kretprobes_event_enable_state(event
, 0);
428 mutex_unlock(&sessions_mutex
);
432 static struct lttng_transport
*lttng_transport_find(const char *name
)
434 struct lttng_transport
*transport
;
436 list_for_each_entry(transport
, <tng_transport_list
, node
) {
437 if (!strcmp(transport
->name
, name
))
443 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
444 const char *transport_name
,
446 size_t subbuf_size
, size_t num_subbuf
,
447 unsigned int switch_timer_interval
,
448 unsigned int read_timer_interval
,
449 enum channel_type channel_type
)
451 struct lttng_channel
*chan
;
452 struct lttng_transport
*transport
= NULL
;
454 mutex_lock(&sessions_mutex
);
455 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
456 goto active
; /* Refuse to add channel to active session */
457 transport
= lttng_transport_find(transport_name
);
459 printk(KERN_WARNING
"LTTng transport %s not found\n",
463 if (!try_module_get(transport
->owner
)) {
464 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
467 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
470 chan
->session
= session
;
471 chan
->id
= session
->free_chan_id
++;
472 chan
->ops
= &transport
->ops
;
474 * Note: the channel creation op already writes into the packet
475 * headers. Therefore the "chan" information used as input
476 * should be already accessible.
478 chan
->chan
= transport
->ops
.channel_create(transport_name
,
479 chan
, buf_addr
, subbuf_size
, num_subbuf
,
480 switch_timer_interval
, read_timer_interval
);
485 chan
->transport
= transport
;
486 chan
->channel_type
= channel_type
;
487 list_add(&chan
->list
, &session
->chan
);
488 mutex_unlock(&sessions_mutex
);
495 module_put(transport
->owner
);
498 mutex_unlock(&sessions_mutex
);
503 * Only used internally at session destruction for per-cpu channels, and
504 * when metadata channel is released.
505 * Needs to be called with sessions mutex held.
508 void _lttng_channel_destroy(struct lttng_channel
*chan
)
510 chan
->ops
->channel_destroy(chan
->chan
);
511 module_put(chan
->transport
->owner
);
512 list_del(&chan
->list
);
513 lttng_destroy_context(chan
->ctx
);
517 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
519 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
521 /* Protect the metadata cache with the sessions_mutex. */
522 mutex_lock(&sessions_mutex
);
523 _lttng_channel_destroy(chan
);
524 mutex_unlock(&sessions_mutex
);
526 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
529 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
531 stream
->finalized
= 1;
532 wake_up_interruptible(&stream
->read_wait
);
536 * Supports event creation while tracing session is active.
537 * Needs to be called with sessions mutex held.
539 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
540 struct lttng_kernel_event
*event_param
,
542 const struct lttng_event_desc
*event_desc
,
543 enum lttng_kernel_instrumentation itype
)
545 struct lttng_session
*session
= chan
->session
;
546 struct lttng_event
*event
;
547 const char *event_name
;
548 struct hlist_head
*head
;
553 if (chan
->free_event_id
== -1U) {
559 case LTTNG_KERNEL_TRACEPOINT
:
560 event_name
= event_desc
->name
;
562 case LTTNG_KERNEL_KPROBE
:
563 case LTTNG_KERNEL_KRETPROBE
:
564 case LTTNG_KERNEL_FUNCTION
:
565 case LTTNG_KERNEL_NOOP
:
566 case LTTNG_KERNEL_SYSCALL
:
567 event_name
= event_param
->name
;
574 name_len
= strlen(event_name
);
575 hash
= jhash(event_name
, name_len
, 0);
576 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
577 lttng_hlist_for_each_entry(event
, head
, hlist
) {
578 WARN_ON_ONCE(!event
->desc
);
579 if (!strncmp(event
->desc
->name
, event_name
,
580 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
581 && chan
== event
->chan
) {
587 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
593 event
->filter
= filter
;
594 event
->id
= chan
->free_event_id
++;
595 event
->instrumentation
= itype
;
596 event
->evtype
= LTTNG_TYPE_EVENT
;
597 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
598 INIT_LIST_HEAD(&event
->enablers_ref_head
);
601 case LTTNG_KERNEL_TRACEPOINT
:
602 /* Event will be enabled by enabler sync. */
604 event
->registered
= 0;
605 event
->desc
= lttng_event_get(event_name
);
610 /* Populate lttng_event structure before event registration. */
613 case LTTNG_KERNEL_KPROBE
:
615 * Needs to be explicitly enabled after creation, since
616 * we may want to apply filters.
619 event
->registered
= 1;
621 * Populate lttng_event structure before event
625 ret
= lttng_kprobes_register(event_name
,
626 event_param
->u
.kprobe
.symbol_name
,
627 event_param
->u
.kprobe
.offset
,
628 event_param
->u
.kprobe
.addr
,
634 ret
= try_module_get(event
->desc
->owner
);
637 case LTTNG_KERNEL_KRETPROBE
:
639 struct lttng_event
*event_return
;
641 /* kretprobe defines 2 events */
643 * Needs to be explicitly enabled after creation, since
644 * we may want to apply filters.
647 event
->registered
= 1;
649 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
654 event_return
->chan
= chan
;
655 event_return
->filter
= filter
;
656 event_return
->id
= chan
->free_event_id
++;
657 event_return
->enabled
= 0;
658 event_return
->registered
= 1;
659 event_return
->instrumentation
= itype
;
661 * Populate lttng_event structure before kretprobe registration.
664 ret
= lttng_kretprobes_register(event_name
,
665 event_param
->u
.kretprobe
.symbol_name
,
666 event_param
->u
.kretprobe
.offset
,
667 event_param
->u
.kretprobe
.addr
,
668 event
, event_return
);
670 kmem_cache_free(event_cache
, event_return
);
674 /* Take 2 refs on the module: one per event. */
675 ret
= try_module_get(event
->desc
->owner
);
677 ret
= try_module_get(event
->desc
->owner
);
679 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
681 WARN_ON_ONCE(ret
> 0);
683 kmem_cache_free(event_cache
, event_return
);
684 module_put(event
->desc
->owner
);
685 module_put(event
->desc
->owner
);
686 goto statedump_error
;
688 list_add(&event_return
->list
, &chan
->session
->events
);
691 case LTTNG_KERNEL_FUNCTION
:
693 * Needs to be explicitly enabled after creation, since
694 * we may want to apply filters.
697 event
->registered
= 1;
699 * Populate lttng_event structure before event
703 ret
= lttng_ftrace_register(event_name
,
704 event_param
->u
.ftrace
.symbol_name
,
709 ret
= try_module_get(event
->desc
->owner
);
712 case LTTNG_KERNEL_NOOP
:
713 case LTTNG_KERNEL_SYSCALL
:
715 * Needs to be explicitly enabled after creation, since
716 * we may want to apply filters.
719 event
->registered
= 0;
720 event
->desc
= event_desc
;
731 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
732 WARN_ON_ONCE(ret
> 0);
734 goto statedump_error
;
736 hlist_add_head(&event
->hlist
, head
);
737 list_add(&event
->list
, &chan
->session
->events
);
741 /* If a statedump error occurs, events will not be readable. */
743 kmem_cache_free(event_cache
, event
);
751 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
752 struct lttng_kernel_event
*event_param
,
754 const struct lttng_event_desc
*event_desc
,
755 enum lttng_kernel_instrumentation itype
)
757 struct lttng_event
*event
;
759 mutex_lock(&sessions_mutex
);
760 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
762 mutex_unlock(&sessions_mutex
);
766 /* Only used for tracepoints for now. */
768 void register_event(struct lttng_event
*event
)
770 const struct lttng_event_desc
*desc
;
773 if (event
->registered
)
777 switch (event
->instrumentation
) {
778 case LTTNG_KERNEL_TRACEPOINT
:
779 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
780 desc
->probe_callback
,
783 case LTTNG_KERNEL_SYSCALL
:
784 ret
= lttng_syscall_filter_enable(event
->chan
,
787 case LTTNG_KERNEL_KPROBE
:
788 case LTTNG_KERNEL_KRETPROBE
:
789 case LTTNG_KERNEL_FUNCTION
:
790 case LTTNG_KERNEL_NOOP
:
797 event
->registered
= 1;
801 * Only used internally at session destruction.
803 int _lttng_event_unregister(struct lttng_event
*event
)
805 const struct lttng_event_desc
*desc
;
808 if (!event
->registered
)
812 switch (event
->instrumentation
) {
813 case LTTNG_KERNEL_TRACEPOINT
:
814 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
815 event
->desc
->probe_callback
,
818 case LTTNG_KERNEL_KPROBE
:
819 lttng_kprobes_unregister(event
);
822 case LTTNG_KERNEL_KRETPROBE
:
823 lttng_kretprobes_unregister(event
);
826 case LTTNG_KERNEL_FUNCTION
:
827 lttng_ftrace_unregister(event
);
830 case LTTNG_KERNEL_SYSCALL
:
831 ret
= lttng_syscall_filter_disable(event
->chan
,
834 case LTTNG_KERNEL_NOOP
:
841 event
->registered
= 0;
846 * Only used internally at session destruction.
849 void _lttng_event_destroy(struct lttng_event
*event
)
851 switch (event
->instrumentation
) {
852 case LTTNG_KERNEL_TRACEPOINT
:
853 lttng_event_put(event
->desc
);
855 case LTTNG_KERNEL_KPROBE
:
856 module_put(event
->desc
->owner
);
857 lttng_kprobes_destroy_private(event
);
859 case LTTNG_KERNEL_KRETPROBE
:
860 module_put(event
->desc
->owner
);
861 lttng_kretprobes_destroy_private(event
);
863 case LTTNG_KERNEL_FUNCTION
:
864 module_put(event
->desc
->owner
);
865 lttng_ftrace_destroy_private(event
);
867 case LTTNG_KERNEL_NOOP
:
868 case LTTNG_KERNEL_SYSCALL
:
873 list_del(&event
->list
);
874 lttng_destroy_context(event
->ctx
);
875 kmem_cache_free(event_cache
, event
);
878 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
884 mutex_lock(&sessions_mutex
);
886 /* track all pids: destroy tracker. */
887 if (session
->pid_tracker
) {
888 struct lttng_pid_tracker
*lpf
;
890 lpf
= session
->pid_tracker
;
891 rcu_assign_pointer(session
->pid_tracker
, NULL
);
893 lttng_pid_tracker_destroy(lpf
);
897 if (!session
->pid_tracker
) {
898 struct lttng_pid_tracker
*lpf
;
900 lpf
= lttng_pid_tracker_create();
905 ret
= lttng_pid_tracker_add(lpf
, pid
);
906 rcu_assign_pointer(session
->pid_tracker
, lpf
);
908 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
912 mutex_unlock(&sessions_mutex
);
916 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
922 mutex_lock(&sessions_mutex
);
924 /* untrack all pids: replace by empty tracker. */
925 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
926 struct lttng_pid_tracker
*lpf
;
928 lpf
= lttng_pid_tracker_create();
933 rcu_assign_pointer(session
->pid_tracker
, lpf
);
936 lttng_pid_tracker_destroy(old_lpf
);
939 if (!session
->pid_tracker
) {
943 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
946 mutex_unlock(&sessions_mutex
);
951 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
953 struct lttng_session
*session
= m
->private;
954 struct lttng_pid_tracker
*lpf
;
955 struct lttng_pid_hash_node
*e
;
958 mutex_lock(&sessions_mutex
);
959 lpf
= session
->pid_tracker
;
961 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
962 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
964 lttng_hlist_for_each_entry(e
, head
, hlist
) {
970 /* PID tracker disabled. */
971 if (iter
>= *pos
&& iter
== 0) {
972 return session
; /* empty tracker */
980 /* Called with sessions_mutex held. */
982 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
984 struct lttng_session
*session
= m
->private;
985 struct lttng_pid_tracker
*lpf
;
986 struct lttng_pid_hash_node
*e
;
990 lpf
= session
->pid_tracker
;
992 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
993 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
995 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1001 /* PID tracker disabled. */
1002 if (iter
>= *ppos
&& iter
== 0)
1003 return session
; /* empty tracker */
1012 void pid_list_stop(struct seq_file
*m
, void *p
)
1014 mutex_unlock(&sessions_mutex
);
1018 int pid_list_show(struct seq_file
*m
, void *p
)
1022 if (p
== m
->private) {
1023 /* Tracker disabled. */
1026 const struct lttng_pid_hash_node
*e
= p
;
1028 pid
= lttng_pid_tracker_get_node_pid(e
);
1030 seq_printf(m
, "process { pid = %d; };\n", pid
);
1035 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
1036 .start
= pid_list_start
,
1037 .next
= pid_list_next
,
1038 .stop
= pid_list_stop
,
1039 .show
= pid_list_show
,
1043 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
1045 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
1049 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
1051 struct seq_file
*m
= file
->private_data
;
1052 struct lttng_session
*session
= m
->private;
1055 WARN_ON_ONCE(!session
);
1056 ret
= seq_release(inode
, file
);
1057 if (!ret
&& session
)
1058 fput(session
->file
);
1062 const struct file_operations lttng_tracker_pids_list_fops
= {
1063 .owner
= THIS_MODULE
,
1064 .open
= lttng_tracker_pids_list_open
,
1066 .llseek
= seq_lseek
,
1067 .release
= lttng_tracker_pids_list_release
,
1070 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
1072 struct file
*tracker_pids_list_file
;
1076 file_fd
= lttng_get_unused_fd();
1082 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
1083 <tng_tracker_pids_list_fops
,
1085 if (IS_ERR(tracker_pids_list_file
)) {
1086 ret
= PTR_ERR(tracker_pids_list_file
);
1089 if (atomic_long_add_unless(&session
->file
->f_count
,
1090 1, INT_MAX
) == INT_MAX
) {
1091 goto refcount_error
;
1093 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1096 m
= tracker_pids_list_file
->private_data
;
1097 m
->private = session
;
1098 fd_install(file_fd
, tracker_pids_list_file
);
1103 atomic_long_dec(&session
->file
->f_count
);
1105 fput(tracker_pids_list_file
);
1107 put_unused_fd(file_fd
);
1113 * Enabler management.
1116 int lttng_match_enabler_wildcard(const char *desc_name
,
1119 /* Compare excluding final '*' */
1120 if (strncmp(desc_name
, name
, strlen(name
) - 1))
1126 int lttng_match_enabler_name(const char *desc_name
,
1129 if (strcmp(desc_name
, name
))
1135 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1136 struct lttng_enabler
*enabler
)
1138 const char *desc_name
, *enabler_name
;
1140 enabler_name
= enabler
->event_param
.name
;
1141 switch (enabler
->event_param
.instrumentation
) {
1142 case LTTNG_KERNEL_TRACEPOINT
:
1143 desc_name
= desc
->name
;
1145 case LTTNG_KERNEL_SYSCALL
:
1146 desc_name
= desc
->name
;
1147 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1148 desc_name
+= strlen("compat_");
1149 if (!strncmp(desc_name
, "syscall_exit_",
1150 strlen("syscall_exit_"))) {
1151 desc_name
+= strlen("syscall_exit_");
1152 } else if (!strncmp(desc_name
, "syscall_entry_",
1153 strlen("syscall_entry_"))) {
1154 desc_name
+= strlen("syscall_entry_");
1164 switch (enabler
->type
) {
1165 case LTTNG_ENABLER_WILDCARD
:
1166 return lttng_match_enabler_wildcard(desc_name
, enabler_name
);
1167 case LTTNG_ENABLER_NAME
:
1168 return lttng_match_enabler_name(desc_name
, enabler_name
);
1175 int lttng_event_match_enabler(struct lttng_event
*event
,
1176 struct lttng_enabler
*enabler
)
1178 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1180 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1181 && event
->chan
== enabler
->chan
)
1188 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1189 struct lttng_enabler
*enabler
)
1191 struct lttng_enabler_ref
*enabler_ref
;
1193 list_for_each_entry(enabler_ref
,
1194 &event
->enablers_ref_head
, node
) {
1195 if (enabler_ref
->ref
== enabler
)
1202 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1204 struct lttng_session
*session
= enabler
->chan
->session
;
1205 struct lttng_probe_desc
*probe_desc
;
1206 const struct lttng_event_desc
*desc
;
1208 struct list_head
*probe_list
;
1210 probe_list
= lttng_get_probe_list_head();
1212 * For each probe event, if we find that a probe event matches
1213 * our enabler, create an associated lttng_event if not
1216 list_for_each_entry(probe_desc
, probe_list
, head
) {
1217 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1219 struct hlist_head
*head
;
1220 const char *event_name
;
1223 struct lttng_event
*event
;
1225 desc
= probe_desc
->event_desc
[i
];
1226 if (!lttng_desc_match_enabler(desc
, enabler
))
1228 event_name
= desc
->name
;
1229 name_len
= strlen(event_name
);
1232 * Check if already created.
1234 hash
= jhash(event_name
, name_len
, 0);
1235 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1236 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1237 if (event
->desc
== desc
1238 && event
->chan
== enabler
->chan
)
1245 * We need to create an event for this
1248 event
= _lttng_event_create(enabler
->chan
,
1250 LTTNG_KERNEL_TRACEPOINT
);
1252 printk(KERN_INFO
"Unable to create event %s\n",
1253 probe_desc
->event_desc
[i
]->name
);
1260 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1264 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1269 * Create struct lttng_event if it is missing and present in the list of
1270 * tracepoint probes.
1271 * Should be called with sessions mutex held.
1274 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1276 switch (enabler
->event_param
.instrumentation
) {
1277 case LTTNG_KERNEL_TRACEPOINT
:
1278 lttng_create_tracepoint_if_missing(enabler
);
1280 case LTTNG_KERNEL_SYSCALL
:
1281 lttng_create_syscall_if_missing(enabler
);
1290 * Create events associated with an enabler (if not already present),
1291 * and add backward reference from the event to the enabler.
1292 * Should be called with sessions mutex held.
1295 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1297 struct lttng_session
*session
= enabler
->chan
->session
;
1298 struct lttng_event
*event
;
1300 /* First ensure that probe events are created for this enabler. */
1301 lttng_create_event_if_missing(enabler
);
1303 /* For each event matching enabler in session event list. */
1304 list_for_each_entry(event
, &session
->events
, list
) {
1305 struct lttng_enabler_ref
*enabler_ref
;
1307 if (!lttng_event_match_enabler(event
, enabler
))
1309 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1312 * If no backward ref, create it.
1313 * Add backward ref from event to enabler.
1315 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1318 enabler_ref
->ref
= enabler
;
1319 list_add(&enabler_ref
->node
,
1320 &event
->enablers_ref_head
);
1324 * Link filter bytecodes if not linked yet.
1326 lttng_enabler_event_link_bytecode(event
, enabler
);
1328 /* TODO: merge event context. */
1334 * Called at module load: connect the probe on all enablers matching
1336 * Called with sessions lock held.
1338 int lttng_fix_pending_events(void)
1340 struct lttng_session
*session
;
1342 list_for_each_entry(session
, &sessions
, list
)
1343 lttng_session_lazy_sync_enablers(session
);
1347 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1348 struct lttng_kernel_event
*event_param
,
1349 struct lttng_channel
*chan
)
1351 struct lttng_enabler
*enabler
;
1353 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1356 enabler
->type
= type
;
1357 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1358 memcpy(&enabler
->event_param
, event_param
,
1359 sizeof(enabler
->event_param
));
1360 enabler
->chan
= chan
;
1362 enabler
->enabled
= 0;
1363 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1364 mutex_lock(&sessions_mutex
);
1365 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1366 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1367 mutex_unlock(&sessions_mutex
);
1371 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1373 mutex_lock(&sessions_mutex
);
1374 enabler
->enabled
= 1;
1375 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1376 mutex_unlock(&sessions_mutex
);
1380 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1382 mutex_lock(&sessions_mutex
);
1383 enabler
->enabled
= 0;
1384 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1385 mutex_unlock(&sessions_mutex
);
1389 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1390 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1392 struct lttng_filter_bytecode_node
*bytecode_node
;
1393 uint32_t bytecode_len
;
1396 ret
= get_user(bytecode_len
, &bytecode
->len
);
1399 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1403 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1404 sizeof(*bytecode
) + bytecode_len
);
1407 bytecode_node
->enabler
= enabler
;
1408 /* Enforce length based on allocated size */
1409 bytecode_node
->bc
.len
= bytecode_len
;
1410 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1411 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1415 kfree(bytecode_node
);
1419 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1420 struct lttng_kernel_context
*context_param
)
1426 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1428 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1430 /* Destroy filter bytecode */
1431 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1432 &enabler
->filter_bytecode_head
, node
) {
1436 /* Destroy contexts */
1437 lttng_destroy_context(enabler
->ctx
);
1439 list_del(&enabler
->node
);
1444 * lttng_session_sync_enablers should be called just before starting a
1446 * Should be called with sessions mutex held.
1449 void lttng_session_sync_enablers(struct lttng_session
*session
)
1451 struct lttng_enabler
*enabler
;
1452 struct lttng_event
*event
;
1454 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1455 lttng_enabler_ref_events(enabler
);
1457 * For each event, if at least one of its enablers is enabled,
1458 * and its channel and session transient states are enabled, we
1459 * enable the event, else we disable it.
1461 list_for_each_entry(event
, &session
->events
, list
) {
1462 struct lttng_enabler_ref
*enabler_ref
;
1463 struct lttng_bytecode_runtime
*runtime
;
1464 int enabled
= 0, has_enablers_without_bytecode
= 0;
1466 switch (event
->instrumentation
) {
1467 case LTTNG_KERNEL_TRACEPOINT
:
1468 case LTTNG_KERNEL_SYSCALL
:
1470 list_for_each_entry(enabler_ref
,
1471 &event
->enablers_ref_head
, node
) {
1472 if (enabler_ref
->ref
->enabled
) {
1479 /* Not handled with lazy sync. */
1483 * Enabled state is based on union of enablers, with
1484 * intesection of session and channel transient enable
1487 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1489 ACCESS_ONCE(event
->enabled
) = enabled
;
1491 * Sync tracepoint registration with event enabled
1495 register_event(event
);
1497 _lttng_event_unregister(event
);
1500 /* Check if has enablers without bytecode enabled */
1501 list_for_each_entry(enabler_ref
,
1502 &event
->enablers_ref_head
, node
) {
1503 if (enabler_ref
->ref
->enabled
1504 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1505 has_enablers_without_bytecode
= 1;
1509 event
->has_enablers_without_bytecode
=
1510 has_enablers_without_bytecode
;
1512 /* Enable filters */
1513 list_for_each_entry(runtime
,
1514 &event
->bytecode_runtime_head
, node
)
1515 lttng_filter_sync_state(runtime
);
1520 * Apply enablers to session events, adding events to session if need
1521 * be. It is required after each modification applied to an active
1522 * session, and right before session "start".
1523 * "lazy" sync means we only sync if required.
1524 * Should be called with sessions mutex held.
1527 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1529 /* We can skip if session is not active */
1530 if (!session
->active
)
1532 lttng_session_sync_enablers(session
);
1536 * Serialize at most one packet worth of metadata into a metadata
1538 * We grab the metadata cache mutex to get exclusive access to our metadata
1539 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1540 * allows us to do racy operations such as looking for remaining space left in
1541 * packet and write, since mutual exclusion protects us from concurrent writes.
1542 * Mutual exclusion on the metadata cache allow us to read the cache content
1543 * without racing against reallocation of the cache by updates.
1544 * Returns the number of bytes written in the channel, 0 if no data
1545 * was written and a negative value on error.
1547 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1548 struct channel
*chan
)
1550 struct lib_ring_buffer_ctx ctx
;
1552 size_t len
, reserve_len
;
1555 * Ensure we support mutiple get_next / put sequences followed by
1556 * put_next. The metadata cache lock protects reading the metadata
1557 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1558 * "flush" operations on the buffer invoked by different processes.
1559 * Moreover, since the metadata cache memory can be reallocated, we
1560 * need to have exclusive access against updates even though we only
1563 mutex_lock(&stream
->metadata_cache
->lock
);
1564 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1565 if (stream
->metadata_in
!= stream
->metadata_out
)
1568 /* Metadata regenerated, change the version. */
1569 if (stream
->metadata_cache
->version
!= stream
->version
)
1570 stream
->version
= stream
->metadata_cache
->version
;
1572 len
= stream
->metadata_cache
->metadata_written
-
1573 stream
->metadata_in
;
1576 reserve_len
= min_t(size_t,
1577 stream
->transport
->ops
.packet_avail_size(chan
),
1579 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1582 * If reservation failed, return an error to the caller.
1584 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1586 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1589 stream
->transport
->ops
.event_write(&ctx
,
1590 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1592 stream
->transport
->ops
.event_commit(&ctx
);
1593 stream
->metadata_in
+= reserve_len
;
1597 mutex_unlock(&stream
->metadata_cache
->lock
);
1602 * Write the metadata to the metadata cache.
1603 * Must be called with sessions_mutex held.
1604 * The metadata cache lock protects us from concurrent read access from
1605 * thread outputting metadata content to ring buffer.
1607 int lttng_metadata_printf(struct lttng_session
*session
,
1608 const char *fmt
, ...)
1613 struct lttng_metadata_stream
*stream
;
1615 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
1618 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1624 mutex_lock(&session
->metadata_cache
->lock
);
1625 if (session
->metadata_cache
->metadata_written
+ len
>
1626 session
->metadata_cache
->cache_alloc
) {
1627 char *tmp_cache_realloc
;
1628 unsigned int tmp_cache_alloc_size
;
1630 tmp_cache_alloc_size
= max_t(unsigned int,
1631 session
->metadata_cache
->cache_alloc
+ len
,
1632 session
->metadata_cache
->cache_alloc
<< 1);
1633 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
1634 if (!tmp_cache_realloc
)
1636 if (session
->metadata_cache
->data
) {
1637 memcpy(tmp_cache_realloc
,
1638 session
->metadata_cache
->data
,
1639 session
->metadata_cache
->cache_alloc
);
1640 vfree(session
->metadata_cache
->data
);
1643 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1644 session
->metadata_cache
->data
= tmp_cache_realloc
;
1646 memcpy(session
->metadata_cache
->data
+
1647 session
->metadata_cache
->metadata_written
,
1649 session
->metadata_cache
->metadata_written
+= len
;
1650 mutex_unlock(&session
->metadata_cache
->lock
);
1653 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1654 wake_up_interruptible(&stream
->read_wait
);
1659 mutex_unlock(&session
->metadata_cache
->lock
);
1665 * Must be called with sessions_mutex held.
1668 int _lttng_field_statedump(struct lttng_session
*session
,
1669 const struct lttng_event_field
*field
)
1673 switch (field
->type
.atype
) {
1675 ret
= lttng_metadata_printf(session
,
1676 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1677 field
->type
.u
.basic
.integer
.size
,
1678 field
->type
.u
.basic
.integer
.alignment
,
1679 field
->type
.u
.basic
.integer
.signedness
,
1680 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
1682 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1685 field
->type
.u
.basic
.integer
.base
,
1686 #if __BYTE_ORDER == __BIG_ENDIAN
1687 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1689 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1694 ret
= lttng_metadata_printf(session
,
1696 field
->type
.u
.basic
.enumeration
.name
,
1701 const struct lttng_basic_type
*elem_type
;
1703 elem_type
= &field
->type
.u
.array
.elem_type
;
1704 if (field
->type
.u
.array
.elem_alignment
) {
1705 ret
= lttng_metadata_printf(session
,
1706 " struct { } align(%u) _%s_padding;\n",
1707 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
1712 ret
= lttng_metadata_printf(session
,
1713 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1714 elem_type
->u
.basic
.integer
.size
,
1715 elem_type
->u
.basic
.integer
.alignment
,
1716 elem_type
->u
.basic
.integer
.signedness
,
1717 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1719 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1722 elem_type
->u
.basic
.integer
.base
,
1723 #if __BYTE_ORDER == __BIG_ENDIAN
1724 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1726 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1728 field
->name
, field
->type
.u
.array
.length
);
1731 case atype_sequence
:
1733 const struct lttng_basic_type
*elem_type
;
1734 const struct lttng_basic_type
*length_type
;
1736 elem_type
= &field
->type
.u
.sequence
.elem_type
;
1737 length_type
= &field
->type
.u
.sequence
.length_type
;
1738 ret
= lttng_metadata_printf(session
,
1739 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1740 length_type
->u
.basic
.integer
.size
,
1741 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
1742 length_type
->u
.basic
.integer
.signedness
,
1743 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1745 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1748 length_type
->u
.basic
.integer
.base
,
1749 #if __BYTE_ORDER == __BIG_ENDIAN
1750 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1752 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1758 if (field
->type
.u
.sequence
.elem_alignment
) {
1759 ret
= lttng_metadata_printf(session
,
1760 " struct { } align(%u) _%s_padding;\n",
1761 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
1766 ret
= lttng_metadata_printf(session
,
1767 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1768 elem_type
->u
.basic
.integer
.size
,
1769 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
1770 elem_type
->u
.basic
.integer
.signedness
,
1771 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1773 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1776 elem_type
->u
.basic
.integer
.base
,
1777 #if __BYTE_ORDER == __BIG_ENDIAN
1778 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1780 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1788 /* Default encoding is UTF8 */
1789 ret
= lttng_metadata_printf(session
,
1791 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
1792 " { encoding = ASCII; }" : "",
1803 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
1804 struct lttng_ctx
*ctx
)
1811 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
1812 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
1814 ret
= _lttng_field_statedump(session
, &field
->event_field
);
1822 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
1823 struct lttng_event
*event
)
1825 const struct lttng_event_desc
*desc
= event
->desc
;
1829 for (i
= 0; i
< desc
->nr_fields
; i
++) {
1830 const struct lttng_event_field
*field
= &desc
->fields
[i
];
1832 ret
= _lttng_field_statedump(session
, field
);
1840 * Must be called with sessions_mutex held.
1843 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
1844 struct lttng_channel
*chan
,
1845 struct lttng_event
*event
)
1849 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1851 if (chan
->channel_type
== METADATA_CHANNEL
)
1854 ret
= lttng_metadata_printf(session
,
1858 " stream_id = %u;\n",
1866 ret
= lttng_metadata_printf(session
,
1867 " context := struct {\n");
1871 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
1875 ret
= lttng_metadata_printf(session
,
1881 ret
= lttng_metadata_printf(session
,
1882 " fields := struct {\n"
1887 ret
= _lttng_fields_metadata_statedump(session
, event
);
1892 * LTTng space reservation can only reserve multiples of the
1895 ret
= lttng_metadata_printf(session
,
1901 event
->metadata_dumped
= 1;
1908 * Must be called with sessions_mutex held.
1911 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1912 struct lttng_channel
*chan
)
1916 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1919 if (chan
->channel_type
== METADATA_CHANNEL
)
1922 WARN_ON_ONCE(!chan
->header_type
);
1923 ret
= lttng_metadata_printf(session
,
1926 " event.header := %s;\n"
1927 " packet.context := struct packet_context;\n",
1929 chan
->header_type
== 1 ? "struct event_header_compact" :
1930 "struct event_header_large");
1935 ret
= lttng_metadata_printf(session
,
1936 " event.context := struct {\n");
1940 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1944 ret
= lttng_metadata_printf(session
,
1950 ret
= lttng_metadata_printf(session
,
1953 chan
->metadata_dumped
= 1;
1959 * Must be called with sessions_mutex held.
1962 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1964 return lttng_metadata_printf(session
,
1965 "struct packet_context {\n"
1966 " uint64_clock_monotonic_t timestamp_begin;\n"
1967 " uint64_clock_monotonic_t timestamp_end;\n"
1968 " uint64_t content_size;\n"
1969 " uint64_t packet_size;\n"
1970 " uint64_t packet_seq_num;\n"
1971 " unsigned long events_discarded;\n"
1972 " uint32_t cpu_id;\n"
1979 * id: range: 0 - 30.
1980 * id 31 is reserved to indicate an extended header.
1983 * id: range: 0 - 65534.
1984 * id 65535 is reserved to indicate an extended header.
1986 * Must be called with sessions_mutex held.
1989 int _lttng_event_header_declare(struct lttng_session
*session
)
1991 return lttng_metadata_printf(session
,
1992 "struct event_header_compact {\n"
1993 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1996 " uint27_clock_monotonic_t timestamp;\n"
2000 " uint64_clock_monotonic_t timestamp;\n"
2005 "struct event_header_large {\n"
2006 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2009 " uint32_clock_monotonic_t timestamp;\n"
2013 " uint64_clock_monotonic_t timestamp;\n"
2017 lttng_alignof(uint32_t) * CHAR_BIT
,
2018 lttng_alignof(uint16_t) * CHAR_BIT
2023 * Approximation of NTP time of day to clock monotonic correlation,
2024 * taken at start of trace.
2025 * Yes, this is only an approximation. Yes, we can (and will) do better
2026 * in future versions.
2027 * This function may return a negative offset. It may happen if the
2028 * system sets the REALTIME clock to 0 after boot.
2031 int64_t measure_clock_offset(void)
2033 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2034 uint64_t tcf
= trace_clock_freq();
2036 struct timespec rts
= { 0, 0 };
2037 unsigned long flags
;
2039 /* Disable interrupts to increase correlation precision. */
2040 local_irq_save(flags
);
2041 monotonic
[0] = trace_clock_read64();
2042 getnstimeofday(&rts
);
2043 monotonic
[1] = trace_clock_read64();
2044 local_irq_restore(flags
);
2046 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2047 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2048 if (tcf
== NSEC_PER_SEC
) {
2049 realtime
+= rts
.tv_nsec
;
2051 uint64_t n
= rts
.tv_nsec
* tcf
;
2053 do_div(n
, NSEC_PER_SEC
);
2056 offset
= (int64_t) realtime
- monotonic_avg
;
2061 * Output metadata into this session's metadata buffers.
2062 * Must be called with sessions_mutex held.
2065 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2067 unsigned char *uuid_c
= session
->uuid
.b
;
2068 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2069 struct lttng_channel
*chan
;
2070 struct lttng_event
*event
;
2073 if (!ACCESS_ONCE(session
->active
))
2075 if (session
->metadata_dumped
)
2078 snprintf(uuid_s
, sizeof(uuid_s
),
2079 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2080 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2081 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2082 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2083 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2085 ret
= lttng_metadata_printf(session
,
2086 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2087 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2088 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2089 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2090 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2091 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2092 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2098 " byte_order = %s;\n"
2099 " packet.header := struct {\n"
2100 " uint32_t magic;\n"
2101 " uint8_t uuid[16];\n"
2102 " uint32_t stream_id;\n"
2103 " uint64_t stream_instance_id;\n"
2106 lttng_alignof(uint8_t) * CHAR_BIT
,
2107 lttng_alignof(uint16_t) * CHAR_BIT
,
2108 lttng_alignof(uint32_t) * CHAR_BIT
,
2109 lttng_alignof(uint64_t) * CHAR_BIT
,
2110 sizeof(unsigned long) * CHAR_BIT
,
2111 lttng_alignof(unsigned long) * CHAR_BIT
,
2115 #if __BYTE_ORDER == __BIG_ENDIAN
2124 ret
= lttng_metadata_printf(session
,
2126 " hostname = \"%s\";\n"
2127 " domain = \"kernel\";\n"
2128 " sysname = \"%s\";\n"
2129 " kernel_release = \"%s\";\n"
2130 " kernel_version = \"%s\";\n"
2131 " tracer_name = \"lttng-modules\";\n"
2132 " tracer_major = %d;\n"
2133 " tracer_minor = %d;\n"
2134 " tracer_patchlevel = %d;\n"
2136 current
->nsproxy
->uts_ns
->name
.nodename
,
2140 LTTNG_MODULES_MAJOR_VERSION
,
2141 LTTNG_MODULES_MINOR_VERSION
,
2142 LTTNG_MODULES_PATCHLEVEL_VERSION
2147 ret
= lttng_metadata_printf(session
,
2149 " name = \"%s\";\n",
2155 if (!trace_clock_uuid(clock_uuid_s
)) {
2156 ret
= lttng_metadata_printf(session
,
2157 " uuid = \"%s\";\n",
2164 ret
= lttng_metadata_printf(session
,
2165 " description = \"%s\";\n"
2166 " freq = %llu; /* Frequency, in Hz */\n"
2167 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2170 trace_clock_description(),
2171 (unsigned long long) trace_clock_freq(),
2172 (long long) measure_clock_offset()
2177 ret
= lttng_metadata_printf(session
,
2178 "typealias integer {\n"
2179 " size = 27; align = 1; signed = false;\n"
2180 " map = clock.%s.value;\n"
2181 "} := uint27_clock_monotonic_t;\n"
2183 "typealias integer {\n"
2184 " size = 32; align = %u; signed = false;\n"
2185 " map = clock.%s.value;\n"
2186 "} := uint32_clock_monotonic_t;\n"
2188 "typealias integer {\n"
2189 " size = 64; align = %u; signed = false;\n"
2190 " map = clock.%s.value;\n"
2191 "} := uint64_clock_monotonic_t;\n\n",
2193 lttng_alignof(uint32_t) * CHAR_BIT
,
2195 lttng_alignof(uint64_t) * CHAR_BIT
,
2201 ret
= _lttng_stream_packet_context_declare(session
);
2205 ret
= _lttng_event_header_declare(session
);
2210 list_for_each_entry(chan
, &session
->chan
, list
) {
2211 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2216 list_for_each_entry(event
, &session
->events
, list
) {
2217 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2221 session
->metadata_dumped
= 1;
2227 * lttng_transport_register - LTT transport registration
2228 * @transport: transport structure
2230 * Registers a transport which can be used as output to extract the data out of
2231 * LTTng. The module calling this registration function must ensure that no
2232 * trap-inducing code will be executed by the transport functions. E.g.
2233 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2234 * is made visible to the transport function. This registration acts as a
2235 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2236 * after its registration must it synchronize the TLBs.
2238 void lttng_transport_register(struct lttng_transport
*transport
)
2241 * Make sure no page fault can be triggered by the module about to be
2242 * registered. We deal with this here so we don't have to call
2243 * vmalloc_sync_all() in each module's init.
2245 wrapper_vmalloc_sync_all();
2247 mutex_lock(&sessions_mutex
);
2248 list_add_tail(&transport
->node
, <tng_transport_list
);
2249 mutex_unlock(&sessions_mutex
);
2251 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2254 * lttng_transport_unregister - LTT transport unregistration
2255 * @transport: transport structure
2257 void lttng_transport_unregister(struct lttng_transport
*transport
)
2259 mutex_lock(&sessions_mutex
);
2260 list_del(&transport
->node
);
2261 mutex_unlock(&sessions_mutex
);
2263 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2265 static int __init
lttng_events_init(void)
2269 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2272 ret
= wrapper_get_pfnblock_flags_mask_init();
2275 ret
= wrapper_get_pageblock_flags_mask_init();
2278 ret
= lttng_context_init();
2281 ret
= lttng_tracepoint_init();
2284 event_cache
= KMEM_CACHE(lttng_event
, 0);
2289 ret
= lttng_abi_init();
2292 ret
= lttng_logger_init();
2300 kmem_cache_destroy(event_cache
);
2302 lttng_tracepoint_exit();
2304 lttng_context_exit();
2308 module_init(lttng_events_init
);
2310 static void __exit
lttng_events_exit(void)
2312 struct lttng_session
*session
, *tmpsession
;
2314 lttng_logger_exit();
2316 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
2317 lttng_session_destroy(session
);
2318 kmem_cache_destroy(event_cache
);
2319 lttng_tracepoint_exit();
2320 lttng_context_exit();
2323 module_exit(lttng_events_exit
);
2325 MODULE_LICENSE("GPL and additional rights");
2326 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2327 MODULE_DESCRIPTION("LTTng Events");
2328 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
2329 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
2330 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
2331 LTTNG_MODULES_EXTRAVERSION
);