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 <wrapper/vzalloc.h>
55 #define METADATA_CACHE_DEFAULT_SIZE 4096
57 static LIST_HEAD(sessions
);
58 static LIST_HEAD(lttng_transport_list
);
60 * Protect the sessions and metadata caches.
62 static DEFINE_MUTEX(sessions_mutex
);
63 static struct kmem_cache
*event_cache
;
65 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
66 static void lttng_session_sync_enablers(struct lttng_session
*session
);
67 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
69 static void _lttng_event_destroy(struct lttng_event
*event
);
70 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
71 static int _lttng_event_unregister(struct lttng_event
*event
);
73 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
74 struct lttng_channel
*chan
,
75 struct lttng_event
*event
);
77 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
79 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
81 void synchronize_trace(void)
84 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
85 #ifdef CONFIG_PREEMPT_RT_FULL
88 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
89 #ifdef CONFIG_PREEMPT_RT
92 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
95 void lttng_lock_sessions(void)
97 mutex_lock(&sessions_mutex
);
100 void lttng_unlock_sessions(void)
102 mutex_unlock(&sessions_mutex
);
106 * Called with sessions lock held.
108 int lttng_session_active(void)
110 struct lttng_session
*iter
;
112 list_for_each_entry(iter
, &sessions
, list
) {
119 struct lttng_session
*lttng_session_create(void)
121 struct lttng_session
*session
;
122 struct lttng_metadata_cache
*metadata_cache
;
125 mutex_lock(&sessions_mutex
);
126 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
129 INIT_LIST_HEAD(&session
->chan
);
130 INIT_LIST_HEAD(&session
->events
);
131 uuid_le_gen(&session
->uuid
);
133 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
136 goto err_free_session
;
137 metadata_cache
->data
= lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
138 if (!metadata_cache
->data
)
140 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
141 kref_init(&metadata_cache
->refcount
);
142 mutex_init(&metadata_cache
->lock
);
143 session
->metadata_cache
= metadata_cache
;
144 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
145 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
146 sizeof(metadata_cache
->uuid
));
147 INIT_LIST_HEAD(&session
->enablers_head
);
148 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
149 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
150 list_add(&session
->list
, &sessions
);
151 mutex_unlock(&sessions_mutex
);
155 kfree(metadata_cache
);
159 mutex_unlock(&sessions_mutex
);
163 void metadata_cache_destroy(struct kref
*kref
)
165 struct lttng_metadata_cache
*cache
=
166 container_of(kref
, struct lttng_metadata_cache
, refcount
);
171 void lttng_session_destroy(struct lttng_session
*session
)
173 struct lttng_channel
*chan
, *tmpchan
;
174 struct lttng_event
*event
, *tmpevent
;
175 struct lttng_metadata_stream
*metadata_stream
;
176 struct lttng_enabler
*enabler
, *tmpenabler
;
179 mutex_lock(&sessions_mutex
);
180 ACCESS_ONCE(session
->active
) = 0;
181 list_for_each_entry(chan
, &session
->chan
, list
) {
182 ret
= lttng_syscalls_unregister(chan
);
185 list_for_each_entry(event
, &session
->events
, list
) {
186 ret
= _lttng_event_unregister(event
);
189 synchronize_trace(); /* Wait for in-flight events to complete */
190 list_for_each_entry_safe(enabler
, tmpenabler
,
191 &session
->enablers_head
, node
)
192 lttng_enabler_destroy(enabler
);
193 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
194 _lttng_event_destroy(event
);
195 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
196 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
197 _lttng_channel_destroy(chan
);
199 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
200 _lttng_metadata_channel_hangup(metadata_stream
);
201 if (session
->pid_tracker
)
202 lttng_pid_tracker_destroy(session
->pid_tracker
);
203 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
204 list_del(&session
->list
);
205 mutex_unlock(&sessions_mutex
);
209 int lttng_session_enable(struct lttng_session
*session
)
212 struct lttng_channel
*chan
;
214 mutex_lock(&sessions_mutex
);
215 if (session
->active
) {
220 /* Set transient enabler state to "enabled" */
224 * Snapshot the number of events per channel to know the type of header
227 list_for_each_entry(chan
, &session
->chan
, list
) {
228 if (chan
->header_type
)
229 continue; /* don't change it if session stop/restart */
230 if (chan
->free_event_id
< 31)
231 chan
->header_type
= 1; /* compact */
233 chan
->header_type
= 2; /* large */
236 /* We need to sync enablers with session before activation. */
237 lttng_session_sync_enablers(session
);
239 ACCESS_ONCE(session
->active
) = 1;
240 ACCESS_ONCE(session
->been_active
) = 1;
241 ret
= _lttng_session_metadata_statedump(session
);
243 ACCESS_ONCE(session
->active
) = 0;
246 ret
= lttng_statedump_start(session
);
248 ACCESS_ONCE(session
->active
) = 0;
250 mutex_unlock(&sessions_mutex
);
254 int lttng_session_disable(struct lttng_session
*session
)
258 mutex_lock(&sessions_mutex
);
259 if (!session
->active
) {
263 ACCESS_ONCE(session
->active
) = 0;
265 /* Set transient enabler state to "disabled" */
267 lttng_session_sync_enablers(session
);
269 mutex_unlock(&sessions_mutex
);
273 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
276 struct lttng_channel
*chan
;
277 struct lttng_event
*event
;
278 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
279 struct lttng_metadata_stream
*stream
;
281 mutex_lock(&sessions_mutex
);
282 if (!session
->active
) {
287 mutex_lock(&cache
->lock
);
288 memset(cache
->data
, 0, cache
->cache_alloc
);
289 cache
->metadata_written
= 0;
291 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
292 stream
->metadata_out
= 0;
293 stream
->metadata_in
= 0;
295 mutex_unlock(&cache
->lock
);
297 session
->metadata_dumped
= 0;
298 list_for_each_entry(chan
, &session
->chan
, list
) {
299 chan
->metadata_dumped
= 0;
302 list_for_each_entry(event
, &session
->events
, list
) {
303 event
->metadata_dumped
= 0;
306 ret
= _lttng_session_metadata_statedump(session
);
309 mutex_unlock(&sessions_mutex
);
315 int lttng_channel_enable(struct lttng_channel
*channel
)
319 mutex_lock(&sessions_mutex
);
320 if (channel
->channel_type
== METADATA_CHANNEL
) {
324 if (channel
->enabled
) {
328 /* Set transient enabler state to "enabled" */
330 lttng_session_sync_enablers(channel
->session
);
331 /* Set atomically the state to "enabled" */
332 ACCESS_ONCE(channel
->enabled
) = 1;
334 mutex_unlock(&sessions_mutex
);
338 int lttng_channel_disable(struct lttng_channel
*channel
)
342 mutex_lock(&sessions_mutex
);
343 if (channel
->channel_type
== METADATA_CHANNEL
) {
347 if (!channel
->enabled
) {
351 /* Set atomically the state to "disabled" */
352 ACCESS_ONCE(channel
->enabled
) = 0;
353 /* Set transient enabler state to "enabled" */
355 lttng_session_sync_enablers(channel
->session
);
357 mutex_unlock(&sessions_mutex
);
361 int lttng_event_enable(struct lttng_event
*event
)
365 mutex_lock(&sessions_mutex
);
366 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
370 if (event
->enabled
) {
374 switch (event
->instrumentation
) {
375 case LTTNG_KERNEL_TRACEPOINT
:
376 case LTTNG_KERNEL_SYSCALL
:
379 case LTTNG_KERNEL_KPROBE
:
380 case LTTNG_KERNEL_FUNCTION
:
381 case LTTNG_KERNEL_NOOP
:
382 ACCESS_ONCE(event
->enabled
) = 1;
384 case LTTNG_KERNEL_KRETPROBE
:
385 ret
= lttng_kretprobes_event_enable_state(event
, 1);
392 mutex_unlock(&sessions_mutex
);
396 int lttng_event_disable(struct lttng_event
*event
)
400 mutex_lock(&sessions_mutex
);
401 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
405 if (!event
->enabled
) {
409 switch (event
->instrumentation
) {
410 case LTTNG_KERNEL_TRACEPOINT
:
411 case LTTNG_KERNEL_SYSCALL
:
414 case LTTNG_KERNEL_KPROBE
:
415 case LTTNG_KERNEL_FUNCTION
:
416 case LTTNG_KERNEL_NOOP
:
417 ACCESS_ONCE(event
->enabled
) = 0;
419 case LTTNG_KERNEL_KRETPROBE
:
420 ret
= lttng_kretprobes_event_enable_state(event
, 0);
427 mutex_unlock(&sessions_mutex
);
431 static struct lttng_transport
*lttng_transport_find(const char *name
)
433 struct lttng_transport
*transport
;
435 list_for_each_entry(transport
, <tng_transport_list
, node
) {
436 if (!strcmp(transport
->name
, name
))
442 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
443 const char *transport_name
,
445 size_t subbuf_size
, size_t num_subbuf
,
446 unsigned int switch_timer_interval
,
447 unsigned int read_timer_interval
,
448 enum channel_type channel_type
)
450 struct lttng_channel
*chan
;
451 struct lttng_transport
*transport
= NULL
;
453 mutex_lock(&sessions_mutex
);
454 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
455 goto active
; /* Refuse to add channel to active session */
456 transport
= lttng_transport_find(transport_name
);
458 printk(KERN_WARNING
"LTTng transport %s not found\n",
462 if (!try_module_get(transport
->owner
)) {
463 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
466 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
469 chan
->session
= session
;
470 chan
->id
= session
->free_chan_id
++;
471 chan
->ops
= &transport
->ops
;
473 * Note: the channel creation op already writes into the packet
474 * headers. Therefore the "chan" information used as input
475 * should be already accessible.
477 chan
->chan
= transport
->ops
.channel_create(transport_name
,
478 chan
, buf_addr
, subbuf_size
, num_subbuf
,
479 switch_timer_interval
, read_timer_interval
);
484 chan
->transport
= transport
;
485 chan
->channel_type
= channel_type
;
486 list_add(&chan
->list
, &session
->chan
);
487 mutex_unlock(&sessions_mutex
);
494 module_put(transport
->owner
);
497 mutex_unlock(&sessions_mutex
);
502 * Only used internally at session destruction for per-cpu channels, and
503 * when metadata channel is released.
504 * Needs to be called with sessions mutex held.
507 void _lttng_channel_destroy(struct lttng_channel
*chan
)
509 chan
->ops
->channel_destroy(chan
->chan
);
510 module_put(chan
->transport
->owner
);
511 list_del(&chan
->list
);
512 lttng_destroy_context(chan
->ctx
);
516 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
518 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
520 /* Protect the metadata cache with the sessions_mutex. */
521 mutex_lock(&sessions_mutex
);
522 _lttng_channel_destroy(chan
);
523 mutex_unlock(&sessions_mutex
);
525 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
528 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
530 stream
->finalized
= 1;
531 wake_up_interruptible(&stream
->read_wait
);
535 * Supports event creation while tracing session is active.
536 * Needs to be called with sessions mutex held.
538 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
539 struct lttng_kernel_event
*event_param
,
541 const struct lttng_event_desc
*event_desc
,
542 enum lttng_kernel_instrumentation itype
)
544 struct lttng_session
*session
= chan
->session
;
545 struct lttng_event
*event
;
546 const char *event_name
;
547 struct hlist_head
*head
;
552 if (chan
->free_event_id
== -1U) {
558 case LTTNG_KERNEL_TRACEPOINT
:
559 event_name
= event_desc
->name
;
561 case LTTNG_KERNEL_KPROBE
:
562 case LTTNG_KERNEL_KRETPROBE
:
563 case LTTNG_KERNEL_FUNCTION
:
564 case LTTNG_KERNEL_NOOP
:
565 case LTTNG_KERNEL_SYSCALL
:
566 event_name
= event_param
->name
;
573 name_len
= strlen(event_name
);
574 hash
= jhash(event_name
, name_len
, 0);
575 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
576 lttng_hlist_for_each_entry(event
, head
, hlist
) {
577 WARN_ON_ONCE(!event
->desc
);
578 if (!strncmp(event
->desc
->name
, event_name
,
579 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
580 && chan
== event
->chan
) {
586 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
592 event
->filter
= filter
;
593 event
->id
= chan
->free_event_id
++;
594 event
->instrumentation
= itype
;
595 event
->evtype
= LTTNG_TYPE_EVENT
;
596 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
597 INIT_LIST_HEAD(&event
->enablers_ref_head
);
600 case LTTNG_KERNEL_TRACEPOINT
:
601 /* Event will be enabled by enabler sync. */
603 event
->registered
= 0;
604 event
->desc
= lttng_event_get(event_name
);
609 /* Populate lttng_event structure before event registration. */
612 case LTTNG_KERNEL_KPROBE
:
614 * Needs to be explicitly enabled after creation, since
615 * we may want to apply filters.
618 event
->registered
= 1;
620 * Populate lttng_event structure before event
624 ret
= lttng_kprobes_register(event_name
,
625 event_param
->u
.kprobe
.symbol_name
,
626 event_param
->u
.kprobe
.offset
,
627 event_param
->u
.kprobe
.addr
,
633 ret
= try_module_get(event
->desc
->owner
);
636 case LTTNG_KERNEL_KRETPROBE
:
638 struct lttng_event
*event_return
;
640 /* kretprobe defines 2 events */
642 * Needs to be explicitly enabled after creation, since
643 * we may want to apply filters.
646 event
->registered
= 1;
648 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
653 event_return
->chan
= chan
;
654 event_return
->filter
= filter
;
655 event_return
->id
= chan
->free_event_id
++;
656 event_return
->enabled
= 0;
657 event_return
->registered
= 1;
658 event_return
->instrumentation
= itype
;
660 * Populate lttng_event structure before kretprobe registration.
663 ret
= lttng_kretprobes_register(event_name
,
664 event_param
->u
.kretprobe
.symbol_name
,
665 event_param
->u
.kretprobe
.offset
,
666 event_param
->u
.kretprobe
.addr
,
667 event
, event_return
);
669 kmem_cache_free(event_cache
, event_return
);
673 /* Take 2 refs on the module: one per event. */
674 ret
= try_module_get(event
->desc
->owner
);
676 ret
= try_module_get(event
->desc
->owner
);
678 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
680 WARN_ON_ONCE(ret
> 0);
682 kmem_cache_free(event_cache
, event_return
);
683 module_put(event
->desc
->owner
);
684 module_put(event
->desc
->owner
);
685 goto statedump_error
;
687 list_add(&event_return
->list
, &chan
->session
->events
);
690 case LTTNG_KERNEL_FUNCTION
:
692 * Needs to be explicitly enabled after creation, since
693 * we may want to apply filters.
696 event
->registered
= 1;
698 * Populate lttng_event structure before event
702 ret
= lttng_ftrace_register(event_name
,
703 event_param
->u
.ftrace
.symbol_name
,
708 ret
= try_module_get(event
->desc
->owner
);
711 case LTTNG_KERNEL_NOOP
:
712 case LTTNG_KERNEL_SYSCALL
:
714 * Needs to be explicitly enabled after creation, since
715 * we may want to apply filters.
718 event
->registered
= 0;
719 event
->desc
= event_desc
;
730 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
731 WARN_ON_ONCE(ret
> 0);
733 goto statedump_error
;
735 hlist_add_head(&event
->hlist
, head
);
736 list_add(&event
->list
, &chan
->session
->events
);
740 /* If a statedump error occurs, events will not be readable. */
742 kmem_cache_free(event_cache
, event
);
750 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
751 struct lttng_kernel_event
*event_param
,
753 const struct lttng_event_desc
*event_desc
,
754 enum lttng_kernel_instrumentation itype
)
756 struct lttng_event
*event
;
758 mutex_lock(&sessions_mutex
);
759 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
761 mutex_unlock(&sessions_mutex
);
765 /* Only used for tracepoints for now. */
767 void register_event(struct lttng_event
*event
)
769 const struct lttng_event_desc
*desc
;
772 if (event
->registered
)
776 switch (event
->instrumentation
) {
777 case LTTNG_KERNEL_TRACEPOINT
:
778 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
779 desc
->probe_callback
,
782 case LTTNG_KERNEL_SYSCALL
:
783 ret
= lttng_syscall_filter_enable(event
->chan
,
786 case LTTNG_KERNEL_KPROBE
:
787 case LTTNG_KERNEL_KRETPROBE
:
788 case LTTNG_KERNEL_FUNCTION
:
789 case LTTNG_KERNEL_NOOP
:
796 event
->registered
= 1;
800 * Only used internally at session destruction.
802 int _lttng_event_unregister(struct lttng_event
*event
)
804 const struct lttng_event_desc
*desc
;
807 if (!event
->registered
)
811 switch (event
->instrumentation
) {
812 case LTTNG_KERNEL_TRACEPOINT
:
813 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
814 event
->desc
->probe_callback
,
817 case LTTNG_KERNEL_KPROBE
:
818 lttng_kprobes_unregister(event
);
821 case LTTNG_KERNEL_KRETPROBE
:
822 lttng_kretprobes_unregister(event
);
825 case LTTNG_KERNEL_FUNCTION
:
826 lttng_ftrace_unregister(event
);
829 case LTTNG_KERNEL_SYSCALL
:
830 ret
= lttng_syscall_filter_disable(event
->chan
,
833 case LTTNG_KERNEL_NOOP
:
840 event
->registered
= 0;
845 * Only used internally at session destruction.
848 void _lttng_event_destroy(struct lttng_event
*event
)
850 switch (event
->instrumentation
) {
851 case LTTNG_KERNEL_TRACEPOINT
:
852 lttng_event_put(event
->desc
);
854 case LTTNG_KERNEL_KPROBE
:
855 module_put(event
->desc
->owner
);
856 lttng_kprobes_destroy_private(event
);
858 case LTTNG_KERNEL_KRETPROBE
:
859 module_put(event
->desc
->owner
);
860 lttng_kretprobes_destroy_private(event
);
862 case LTTNG_KERNEL_FUNCTION
:
863 module_put(event
->desc
->owner
);
864 lttng_ftrace_destroy_private(event
);
866 case LTTNG_KERNEL_NOOP
:
867 case LTTNG_KERNEL_SYSCALL
:
872 list_del(&event
->list
);
873 lttng_destroy_context(event
->ctx
);
874 kmem_cache_free(event_cache
, event
);
877 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
883 mutex_lock(&sessions_mutex
);
885 /* track all pids: destroy tracker. */
886 if (session
->pid_tracker
) {
887 struct lttng_pid_tracker
*lpf
;
889 lpf
= session
->pid_tracker
;
890 rcu_assign_pointer(session
->pid_tracker
, NULL
);
892 lttng_pid_tracker_destroy(lpf
);
896 if (!session
->pid_tracker
) {
897 struct lttng_pid_tracker
*lpf
;
899 lpf
= lttng_pid_tracker_create();
904 ret
= lttng_pid_tracker_add(lpf
, pid
);
905 rcu_assign_pointer(session
->pid_tracker
, lpf
);
907 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
911 mutex_unlock(&sessions_mutex
);
915 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
921 mutex_lock(&sessions_mutex
);
923 /* untrack all pids: replace by empty tracker. */
924 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
925 struct lttng_pid_tracker
*lpf
;
927 lpf
= lttng_pid_tracker_create();
932 rcu_assign_pointer(session
->pid_tracker
, lpf
);
935 lttng_pid_tracker_destroy(old_lpf
);
938 if (!session
->pid_tracker
) {
942 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
945 mutex_unlock(&sessions_mutex
);
950 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
952 struct lttng_session
*session
= m
->private;
953 struct lttng_pid_tracker
*lpf
;
954 struct lttng_pid_hash_node
*e
;
957 mutex_lock(&sessions_mutex
);
958 lpf
= session
->pid_tracker
;
960 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
961 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
963 lttng_hlist_for_each_entry(e
, head
, hlist
) {
969 /* PID tracker disabled. */
970 if (iter
>= *pos
&& iter
== 0) {
971 return session
; /* empty tracker */
979 /* Called with sessions_mutex held. */
981 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
983 struct lttng_session
*session
= m
->private;
984 struct lttng_pid_tracker
*lpf
;
985 struct lttng_pid_hash_node
*e
;
989 lpf
= session
->pid_tracker
;
991 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
992 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
994 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1000 /* PID tracker disabled. */
1001 if (iter
>= *ppos
&& iter
== 0)
1002 return session
; /* empty tracker */
1011 void pid_list_stop(struct seq_file
*m
, void *p
)
1013 mutex_unlock(&sessions_mutex
);
1017 int pid_list_show(struct seq_file
*m
, void *p
)
1021 if (p
== m
->private) {
1022 /* Tracker disabled. */
1025 const struct lttng_pid_hash_node
*e
= p
;
1027 pid
= lttng_pid_tracker_get_node_pid(e
);
1029 seq_printf(m
, "process { pid = %d; };\n", pid
);
1034 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
1035 .start
= pid_list_start
,
1036 .next
= pid_list_next
,
1037 .stop
= pid_list_stop
,
1038 .show
= pid_list_show
,
1042 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
1044 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
1048 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
1050 struct seq_file
*m
= file
->private_data
;
1051 struct lttng_session
*session
= m
->private;
1054 WARN_ON_ONCE(!session
);
1055 ret
= seq_release(inode
, file
);
1056 if (!ret
&& session
)
1057 fput(session
->file
);
1061 const struct file_operations lttng_tracker_pids_list_fops
= {
1062 .owner
= THIS_MODULE
,
1063 .open
= lttng_tracker_pids_list_open
,
1065 .llseek
= seq_lseek
,
1066 .release
= lttng_tracker_pids_list_release
,
1069 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
1071 struct file
*tracker_pids_list_file
;
1075 file_fd
= lttng_get_unused_fd();
1081 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
1082 <tng_tracker_pids_list_fops
,
1084 if (IS_ERR(tracker_pids_list_file
)) {
1085 ret
= PTR_ERR(tracker_pids_list_file
);
1088 if (atomic_long_add_unless(&session
->file
->f_count
,
1089 1, INT_MAX
) == INT_MAX
) {
1090 goto refcount_error
;
1092 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1095 m
= tracker_pids_list_file
->private_data
;
1096 m
->private = session
;
1097 fd_install(file_fd
, tracker_pids_list_file
);
1102 atomic_long_dec(&session
->file
->f_count
);
1104 fput(tracker_pids_list_file
);
1106 put_unused_fd(file_fd
);
1112 * Enabler management.
1115 int lttng_match_enabler_wildcard(const char *desc_name
,
1118 /* Compare excluding final '*' */
1119 if (strncmp(desc_name
, name
, strlen(name
) - 1))
1125 int lttng_match_enabler_name(const char *desc_name
,
1128 if (strcmp(desc_name
, name
))
1134 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1135 struct lttng_enabler
*enabler
)
1137 const char *desc_name
, *enabler_name
;
1139 enabler_name
= enabler
->event_param
.name
;
1140 switch (enabler
->event_param
.instrumentation
) {
1141 case LTTNG_KERNEL_TRACEPOINT
:
1142 desc_name
= desc
->name
;
1144 case LTTNG_KERNEL_SYSCALL
:
1145 desc_name
= desc
->name
;
1146 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1147 desc_name
+= strlen("compat_");
1148 if (!strncmp(desc_name
, "syscall_exit_",
1149 strlen("syscall_exit_"))) {
1150 desc_name
+= strlen("syscall_exit_");
1151 } else if (!strncmp(desc_name
, "syscall_entry_",
1152 strlen("syscall_entry_"))) {
1153 desc_name
+= strlen("syscall_entry_");
1163 switch (enabler
->type
) {
1164 case LTTNG_ENABLER_WILDCARD
:
1165 return lttng_match_enabler_wildcard(desc_name
, enabler_name
);
1166 case LTTNG_ENABLER_NAME
:
1167 return lttng_match_enabler_name(desc_name
, enabler_name
);
1174 int lttng_event_match_enabler(struct lttng_event
*event
,
1175 struct lttng_enabler
*enabler
)
1177 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1179 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1180 && event
->chan
== enabler
->chan
)
1187 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1188 struct lttng_enabler
*enabler
)
1190 struct lttng_enabler_ref
*enabler_ref
;
1192 list_for_each_entry(enabler_ref
,
1193 &event
->enablers_ref_head
, node
) {
1194 if (enabler_ref
->ref
== enabler
)
1201 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1203 struct lttng_session
*session
= enabler
->chan
->session
;
1204 struct lttng_probe_desc
*probe_desc
;
1205 const struct lttng_event_desc
*desc
;
1207 struct list_head
*probe_list
;
1209 probe_list
= lttng_get_probe_list_head();
1211 * For each probe event, if we find that a probe event matches
1212 * our enabler, create an associated lttng_event if not
1215 list_for_each_entry(probe_desc
, probe_list
, head
) {
1216 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1218 struct hlist_head
*head
;
1219 const char *event_name
;
1222 struct lttng_event
*event
;
1224 desc
= probe_desc
->event_desc
[i
];
1225 if (!lttng_desc_match_enabler(desc
, enabler
))
1227 event_name
= desc
->name
;
1228 name_len
= strlen(event_name
);
1231 * Check if already created.
1233 hash
= jhash(event_name
, name_len
, 0);
1234 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1235 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1236 if (event
->desc
== desc
1237 && event
->chan
== enabler
->chan
)
1244 * We need to create an event for this
1247 event
= _lttng_event_create(enabler
->chan
,
1249 LTTNG_KERNEL_TRACEPOINT
);
1251 printk(KERN_INFO
"Unable to create event %s\n",
1252 probe_desc
->event_desc
[i
]->name
);
1259 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1263 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1268 * Create struct lttng_event if it is missing and present in the list of
1269 * tracepoint probes.
1270 * Should be called with sessions mutex held.
1273 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1275 switch (enabler
->event_param
.instrumentation
) {
1276 case LTTNG_KERNEL_TRACEPOINT
:
1277 lttng_create_tracepoint_if_missing(enabler
);
1279 case LTTNG_KERNEL_SYSCALL
:
1280 lttng_create_syscall_if_missing(enabler
);
1289 * Create events associated with an enabler (if not already present),
1290 * and add backward reference from the event to the enabler.
1291 * Should be called with sessions mutex held.
1294 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1296 struct lttng_session
*session
= enabler
->chan
->session
;
1297 struct lttng_event
*event
;
1299 /* First ensure that probe events are created for this enabler. */
1300 lttng_create_event_if_missing(enabler
);
1302 /* For each event matching enabler in session event list. */
1303 list_for_each_entry(event
, &session
->events
, list
) {
1304 struct lttng_enabler_ref
*enabler_ref
;
1306 if (!lttng_event_match_enabler(event
, enabler
))
1308 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1311 * If no backward ref, create it.
1312 * Add backward ref from event to enabler.
1314 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1317 enabler_ref
->ref
= enabler
;
1318 list_add(&enabler_ref
->node
,
1319 &event
->enablers_ref_head
);
1323 * Link filter bytecodes if not linked yet.
1325 lttng_enabler_event_link_bytecode(event
, enabler
);
1327 /* TODO: merge event context. */
1333 * Called at module load: connect the probe on all enablers matching
1335 * Called with sessions lock held.
1337 int lttng_fix_pending_events(void)
1339 struct lttng_session
*session
;
1341 list_for_each_entry(session
, &sessions
, list
)
1342 lttng_session_lazy_sync_enablers(session
);
1346 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1347 struct lttng_kernel_event
*event_param
,
1348 struct lttng_channel
*chan
)
1350 struct lttng_enabler
*enabler
;
1352 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1355 enabler
->type
= type
;
1356 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1357 memcpy(&enabler
->event_param
, event_param
,
1358 sizeof(enabler
->event_param
));
1359 enabler
->chan
= chan
;
1361 enabler
->enabled
= 0;
1362 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1363 mutex_lock(&sessions_mutex
);
1364 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1365 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1366 mutex_unlock(&sessions_mutex
);
1370 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1372 mutex_lock(&sessions_mutex
);
1373 enabler
->enabled
= 1;
1374 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1375 mutex_unlock(&sessions_mutex
);
1379 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1381 mutex_lock(&sessions_mutex
);
1382 enabler
->enabled
= 0;
1383 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1384 mutex_unlock(&sessions_mutex
);
1388 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1389 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1391 struct lttng_filter_bytecode_node
*bytecode_node
;
1392 uint32_t bytecode_len
;
1395 ret
= get_user(bytecode_len
, &bytecode
->len
);
1398 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1402 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1403 sizeof(*bytecode
) + bytecode_len
);
1406 bytecode_node
->enabler
= enabler
;
1407 /* Enforce length based on allocated size */
1408 bytecode_node
->bc
.len
= bytecode_len
;
1409 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1410 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1414 kfree(bytecode_node
);
1418 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1419 struct lttng_kernel_context
*context_param
)
1425 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1427 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1429 /* Destroy filter bytecode */
1430 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1431 &enabler
->filter_bytecode_head
, node
) {
1435 /* Destroy contexts */
1436 lttng_destroy_context(enabler
->ctx
);
1438 list_del(&enabler
->node
);
1443 * lttng_session_sync_enablers should be called just before starting a
1445 * Should be called with sessions mutex held.
1448 void lttng_session_sync_enablers(struct lttng_session
*session
)
1450 struct lttng_enabler
*enabler
;
1451 struct lttng_event
*event
;
1453 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1454 lttng_enabler_ref_events(enabler
);
1456 * For each event, if at least one of its enablers is enabled,
1457 * and its channel and session transient states are enabled, we
1458 * enable the event, else we disable it.
1460 list_for_each_entry(event
, &session
->events
, list
) {
1461 struct lttng_enabler_ref
*enabler_ref
;
1462 struct lttng_bytecode_runtime
*runtime
;
1463 int enabled
= 0, has_enablers_without_bytecode
= 0;
1465 switch (event
->instrumentation
) {
1466 case LTTNG_KERNEL_TRACEPOINT
:
1467 case LTTNG_KERNEL_SYSCALL
:
1469 list_for_each_entry(enabler_ref
,
1470 &event
->enablers_ref_head
, node
) {
1471 if (enabler_ref
->ref
->enabled
) {
1478 /* Not handled with lazy sync. */
1482 * Enabled state is based on union of enablers, with
1483 * intesection of session and channel transient enable
1486 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1488 ACCESS_ONCE(event
->enabled
) = enabled
;
1490 * Sync tracepoint registration with event enabled
1494 register_event(event
);
1496 _lttng_event_unregister(event
);
1499 /* Check if has enablers without bytecode enabled */
1500 list_for_each_entry(enabler_ref
,
1501 &event
->enablers_ref_head
, node
) {
1502 if (enabler_ref
->ref
->enabled
1503 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1504 has_enablers_without_bytecode
= 1;
1508 event
->has_enablers_without_bytecode
=
1509 has_enablers_without_bytecode
;
1511 /* Enable filters */
1512 list_for_each_entry(runtime
,
1513 &event
->bytecode_runtime_head
, node
)
1514 lttng_filter_sync_state(runtime
);
1519 * Apply enablers to session events, adding events to session if need
1520 * be. It is required after each modification applied to an active
1521 * session, and right before session "start".
1522 * "lazy" sync means we only sync if required.
1523 * Should be called with sessions mutex held.
1526 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1528 /* We can skip if session is not active */
1529 if (!session
->active
)
1531 lttng_session_sync_enablers(session
);
1535 * Serialize at most one packet worth of metadata into a metadata
1537 * We grab the metadata cache mutex to get exclusive access to our metadata
1538 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1539 * allows us to do racy operations such as looking for remaining space left in
1540 * packet and write, since mutual exclusion protects us from concurrent writes.
1541 * Mutual exclusion on the metadata cache allow us to read the cache content
1542 * without racing against reallocation of the cache by updates.
1543 * Returns the number of bytes written in the channel, 0 if no data
1544 * was written and a negative value on error.
1546 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1547 struct channel
*chan
)
1549 struct lib_ring_buffer_ctx ctx
;
1551 size_t len
, reserve_len
;
1554 * Ensure we support mutiple get_next / put sequences followed by
1555 * put_next. The metadata cache lock protects reading the metadata
1556 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1557 * "flush" operations on the buffer invoked by different processes.
1558 * Moreover, since the metadata cache memory can be reallocated, we
1559 * need to have exclusive access against updates even though we only
1562 mutex_lock(&stream
->metadata_cache
->lock
);
1563 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1564 if (stream
->metadata_in
!= stream
->metadata_out
)
1567 /* Metadata regenerated, change the version. */
1568 if (stream
->metadata_cache
->version
!= stream
->version
)
1569 stream
->version
= stream
->metadata_cache
->version
;
1571 len
= stream
->metadata_cache
->metadata_written
-
1572 stream
->metadata_in
;
1575 reserve_len
= min_t(size_t,
1576 stream
->transport
->ops
.packet_avail_size(chan
),
1578 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1581 * If reservation failed, return an error to the caller.
1583 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1585 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1588 stream
->transport
->ops
.event_write(&ctx
,
1589 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1591 stream
->transport
->ops
.event_commit(&ctx
);
1592 stream
->metadata_in
+= reserve_len
;
1596 mutex_unlock(&stream
->metadata_cache
->lock
);
1601 * Write the metadata to the metadata cache.
1602 * Must be called with sessions_mutex held.
1603 * The metadata cache lock protects us from concurrent read access from
1604 * thread outputting metadata content to ring buffer.
1606 int lttng_metadata_printf(struct lttng_session
*session
,
1607 const char *fmt
, ...)
1612 struct lttng_metadata_stream
*stream
;
1614 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
1617 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1623 mutex_lock(&session
->metadata_cache
->lock
);
1624 if (session
->metadata_cache
->metadata_written
+ len
>
1625 session
->metadata_cache
->cache_alloc
) {
1626 char *tmp_cache_realloc
;
1627 unsigned int tmp_cache_alloc_size
;
1629 tmp_cache_alloc_size
= max_t(unsigned int,
1630 session
->metadata_cache
->cache_alloc
+ len
,
1631 session
->metadata_cache
->cache_alloc
<< 1);
1632 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
1633 if (!tmp_cache_realloc
)
1635 if (session
->metadata_cache
->data
) {
1636 memcpy(tmp_cache_realloc
,
1637 session
->metadata_cache
->data
,
1638 session
->metadata_cache
->cache_alloc
);
1639 vfree(session
->metadata_cache
->data
);
1642 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1643 session
->metadata_cache
->data
= tmp_cache_realloc
;
1645 memcpy(session
->metadata_cache
->data
+
1646 session
->metadata_cache
->metadata_written
,
1648 session
->metadata_cache
->metadata_written
+= len
;
1649 mutex_unlock(&session
->metadata_cache
->lock
);
1652 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1653 wake_up_interruptible(&stream
->read_wait
);
1658 mutex_unlock(&session
->metadata_cache
->lock
);
1664 * Must be called with sessions_mutex held.
1667 int _lttng_field_statedump(struct lttng_session
*session
,
1668 const struct lttng_event_field
*field
)
1672 switch (field
->type
.atype
) {
1674 ret
= lttng_metadata_printf(session
,
1675 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1676 field
->type
.u
.basic
.integer
.size
,
1677 field
->type
.u
.basic
.integer
.alignment
,
1678 field
->type
.u
.basic
.integer
.signedness
,
1679 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
1681 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1684 field
->type
.u
.basic
.integer
.base
,
1686 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1688 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1693 ret
= lttng_metadata_printf(session
,
1695 field
->type
.u
.basic
.enumeration
.name
,
1700 const struct lttng_basic_type
*elem_type
;
1702 elem_type
= &field
->type
.u
.array
.elem_type
;
1703 if (field
->type
.u
.array
.elem_alignment
) {
1704 ret
= lttng_metadata_printf(session
,
1705 " struct { } align(%u) _%s_padding;\n",
1706 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
1711 ret
= lttng_metadata_printf(session
,
1712 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1713 elem_type
->u
.basic
.integer
.size
,
1714 elem_type
->u
.basic
.integer
.alignment
,
1715 elem_type
->u
.basic
.integer
.signedness
,
1716 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1718 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1721 elem_type
->u
.basic
.integer
.base
,
1723 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1725 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1727 field
->name
, field
->type
.u
.array
.length
);
1730 case atype_sequence
:
1732 const struct lttng_basic_type
*elem_type
;
1733 const struct lttng_basic_type
*length_type
;
1735 elem_type
= &field
->type
.u
.sequence
.elem_type
;
1736 length_type
= &field
->type
.u
.sequence
.length_type
;
1737 ret
= lttng_metadata_printf(session
,
1738 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1739 length_type
->u
.basic
.integer
.size
,
1740 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
1741 length_type
->u
.basic
.integer
.signedness
,
1742 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1744 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1747 length_type
->u
.basic
.integer
.base
,
1749 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1751 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1757 if (field
->type
.u
.sequence
.elem_alignment
) {
1758 ret
= lttng_metadata_printf(session
,
1759 " struct { } align(%u) _%s_padding;\n",
1760 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
1765 ret
= lttng_metadata_printf(session
,
1766 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1767 elem_type
->u
.basic
.integer
.size
,
1768 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
1769 elem_type
->u
.basic
.integer
.signedness
,
1770 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1772 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1775 elem_type
->u
.basic
.integer
.base
,
1777 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1779 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1787 /* Default encoding is UTF8 */
1788 ret
= lttng_metadata_printf(session
,
1790 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
1791 " { encoding = ASCII; }" : "",
1802 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
1803 struct lttng_ctx
*ctx
)
1810 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
1811 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
1813 ret
= _lttng_field_statedump(session
, &field
->event_field
);
1821 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
1822 struct lttng_event
*event
)
1824 const struct lttng_event_desc
*desc
= event
->desc
;
1828 for (i
= 0; i
< desc
->nr_fields
; i
++) {
1829 const struct lttng_event_field
*field
= &desc
->fields
[i
];
1831 ret
= _lttng_field_statedump(session
, field
);
1839 * Must be called with sessions_mutex held.
1842 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
1843 struct lttng_channel
*chan
,
1844 struct lttng_event
*event
)
1848 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1850 if (chan
->channel_type
== METADATA_CHANNEL
)
1853 ret
= lttng_metadata_printf(session
,
1857 " stream_id = %u;\n",
1865 ret
= lttng_metadata_printf(session
,
1866 " context := struct {\n");
1870 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
1874 ret
= lttng_metadata_printf(session
,
1880 ret
= lttng_metadata_printf(session
,
1881 " fields := struct {\n"
1886 ret
= _lttng_fields_metadata_statedump(session
, event
);
1891 * LTTng space reservation can only reserve multiples of the
1894 ret
= lttng_metadata_printf(session
,
1900 event
->metadata_dumped
= 1;
1907 * Must be called with sessions_mutex held.
1910 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1911 struct lttng_channel
*chan
)
1915 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1918 if (chan
->channel_type
== METADATA_CHANNEL
)
1921 WARN_ON_ONCE(!chan
->header_type
);
1922 ret
= lttng_metadata_printf(session
,
1925 " event.header := %s;\n"
1926 " packet.context := struct packet_context;\n",
1928 chan
->header_type
== 1 ? "struct event_header_compact" :
1929 "struct event_header_large");
1934 ret
= lttng_metadata_printf(session
,
1935 " event.context := struct {\n");
1939 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1943 ret
= lttng_metadata_printf(session
,
1949 ret
= lttng_metadata_printf(session
,
1952 chan
->metadata_dumped
= 1;
1958 * Must be called with sessions_mutex held.
1961 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1963 return lttng_metadata_printf(session
,
1964 "struct packet_context {\n"
1965 " uint64_clock_monotonic_t timestamp_begin;\n"
1966 " uint64_clock_monotonic_t timestamp_end;\n"
1967 " uint64_t content_size;\n"
1968 " uint64_t packet_size;\n"
1969 " uint64_t packet_seq_num;\n"
1970 " unsigned long events_discarded;\n"
1971 " uint32_t cpu_id;\n"
1978 * id: range: 0 - 30.
1979 * id 31 is reserved to indicate an extended header.
1982 * id: range: 0 - 65534.
1983 * id 65535 is reserved to indicate an extended header.
1985 * Must be called with sessions_mutex held.
1988 int _lttng_event_header_declare(struct lttng_session
*session
)
1990 return lttng_metadata_printf(session
,
1991 "struct event_header_compact {\n"
1992 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1995 " uint27_clock_monotonic_t timestamp;\n"
1999 " uint64_clock_monotonic_t timestamp;\n"
2004 "struct event_header_large {\n"
2005 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2008 " uint32_clock_monotonic_t timestamp;\n"
2012 " uint64_clock_monotonic_t timestamp;\n"
2016 lttng_alignof(uint32_t) * CHAR_BIT
,
2017 lttng_alignof(uint16_t) * CHAR_BIT
2022 * Approximation of NTP time of day to clock monotonic correlation,
2023 * taken at start of trace.
2024 * Yes, this is only an approximation. Yes, we can (and will) do better
2025 * in future versions.
2026 * This function may return a negative offset. It may happen if the
2027 * system sets the REALTIME clock to 0 after boot.
2030 int64_t measure_clock_offset(void)
2032 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2033 uint64_t tcf
= trace_clock_freq();
2035 struct timespec rts
= { 0, 0 };
2036 unsigned long flags
;
2038 /* Disable interrupts to increase correlation precision. */
2039 local_irq_save(flags
);
2040 monotonic
[0] = trace_clock_read64();
2041 getnstimeofday(&rts
);
2042 monotonic
[1] = trace_clock_read64();
2043 local_irq_restore(flags
);
2045 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2046 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2047 if (tcf
== NSEC_PER_SEC
) {
2048 realtime
+= rts
.tv_nsec
;
2050 uint64_t n
= rts
.tv_nsec
* tcf
;
2052 do_div(n
, NSEC_PER_SEC
);
2055 offset
= (int64_t) realtime
- monotonic_avg
;
2060 * Output metadata into this session's metadata buffers.
2061 * Must be called with sessions_mutex held.
2064 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2066 unsigned char *uuid_c
= session
->uuid
.b
;
2067 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2068 struct lttng_channel
*chan
;
2069 struct lttng_event
*event
;
2072 if (!ACCESS_ONCE(session
->active
))
2074 if (session
->metadata_dumped
)
2077 snprintf(uuid_s
, sizeof(uuid_s
),
2078 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2079 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2080 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2081 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2082 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2084 ret
= lttng_metadata_printf(session
,
2085 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2086 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2087 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2088 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2089 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2090 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2091 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2097 " byte_order = %s;\n"
2098 " packet.header := struct {\n"
2099 " uint32_t magic;\n"
2100 " uint8_t uuid[16];\n"
2101 " uint32_t stream_id;\n"
2102 " uint64_t stream_instance_id;\n"
2105 lttng_alignof(uint8_t) * CHAR_BIT
,
2106 lttng_alignof(uint16_t) * CHAR_BIT
,
2107 lttng_alignof(uint32_t) * CHAR_BIT
,
2108 lttng_alignof(uint64_t) * CHAR_BIT
,
2109 sizeof(unsigned long) * CHAR_BIT
,
2110 lttng_alignof(unsigned long) * CHAR_BIT
,
2123 ret
= lttng_metadata_printf(session
,
2125 " hostname = \"%s\";\n"
2126 " domain = \"kernel\";\n"
2127 " sysname = \"%s\";\n"
2128 " kernel_release = \"%s\";\n"
2129 " kernel_version = \"%s\";\n"
2130 " tracer_name = \"lttng-modules\";\n"
2131 " tracer_major = %d;\n"
2132 " tracer_minor = %d;\n"
2133 " tracer_patchlevel = %d;\n"
2135 current
->nsproxy
->uts_ns
->name
.nodename
,
2139 LTTNG_MODULES_MAJOR_VERSION
,
2140 LTTNG_MODULES_MINOR_VERSION
,
2141 LTTNG_MODULES_PATCHLEVEL_VERSION
2146 ret
= lttng_metadata_printf(session
,
2148 " name = \"%s\";\n",
2154 if (!trace_clock_uuid(clock_uuid_s
)) {
2155 ret
= lttng_metadata_printf(session
,
2156 " uuid = \"%s\";\n",
2163 ret
= lttng_metadata_printf(session
,
2164 " description = \"%s\";\n"
2165 " freq = %llu; /* Frequency, in Hz */\n"
2166 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2169 trace_clock_description(),
2170 (unsigned long long) trace_clock_freq(),
2171 (long long) measure_clock_offset()
2176 ret
= lttng_metadata_printf(session
,
2177 "typealias integer {\n"
2178 " size = 27; align = 1; signed = false;\n"
2179 " map = clock.%s.value;\n"
2180 "} := uint27_clock_monotonic_t;\n"
2182 "typealias integer {\n"
2183 " size = 32; align = %u; signed = false;\n"
2184 " map = clock.%s.value;\n"
2185 "} := uint32_clock_monotonic_t;\n"
2187 "typealias integer {\n"
2188 " size = 64; align = %u; signed = false;\n"
2189 " map = clock.%s.value;\n"
2190 "} := uint64_clock_monotonic_t;\n\n",
2192 lttng_alignof(uint32_t) * CHAR_BIT
,
2194 lttng_alignof(uint64_t) * CHAR_BIT
,
2200 ret
= _lttng_stream_packet_context_declare(session
);
2204 ret
= _lttng_event_header_declare(session
);
2209 list_for_each_entry(chan
, &session
->chan
, list
) {
2210 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2215 list_for_each_entry(event
, &session
->events
, list
) {
2216 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2220 session
->metadata_dumped
= 1;
2226 * lttng_transport_register - LTT transport registration
2227 * @transport: transport structure
2229 * Registers a transport which can be used as output to extract the data out of
2230 * LTTng. The module calling this registration function must ensure that no
2231 * trap-inducing code will be executed by the transport functions. E.g.
2232 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2233 * is made visible to the transport function. This registration acts as a
2234 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2235 * after its registration must it synchronize the TLBs.
2237 void lttng_transport_register(struct lttng_transport
*transport
)
2240 * Make sure no page fault can be triggered by the module about to be
2241 * registered. We deal with this here so we don't have to call
2242 * vmalloc_sync_all() in each module's init.
2244 wrapper_vmalloc_sync_all();
2246 mutex_lock(&sessions_mutex
);
2247 list_add_tail(&transport
->node
, <tng_transport_list
);
2248 mutex_unlock(&sessions_mutex
);
2250 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2253 * lttng_transport_unregister - LTT transport unregistration
2254 * @transport: transport structure
2256 void lttng_transport_unregister(struct lttng_transport
*transport
)
2258 mutex_lock(&sessions_mutex
);
2259 list_del(&transport
->node
);
2260 mutex_unlock(&sessions_mutex
);
2262 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2264 static int __init
lttng_events_init(void)
2268 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2271 ret
= wrapper_get_pfnblock_flags_mask_init();
2274 ret
= wrapper_get_pageblock_flags_mask_init();
2277 ret
= lttng_context_init();
2280 ret
= lttng_tracepoint_init();
2283 event_cache
= KMEM_CACHE(lttng_event
, 0);
2288 ret
= lttng_abi_init();
2291 ret
= lttng_logger_init();
2299 kmem_cache_destroy(event_cache
);
2301 lttng_tracepoint_exit();
2303 lttng_context_exit();
2307 module_init(lttng_events_init
);
2309 static void __exit
lttng_events_exit(void)
2311 struct lttng_session
*session
, *tmpsession
;
2313 lttng_logger_exit();
2315 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
2316 lttng_session_destroy(session
);
2317 kmem_cache_destroy(event_cache
);
2318 lttng_tracepoint_exit();
2319 lttng_context_exit();
2322 module_exit(lttng_events_exit
);
2324 MODULE_LICENSE("GPL and additional rights");
2325 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2326 MODULE_DESCRIPTION("LTTng Events");
2327 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
2328 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
2329 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
2330 LTTNG_MODULES_EXTRAVERSION
);