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>
42 #include "wrapper/uuid.h"
43 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
44 #include "wrapper/random.h"
45 #include "wrapper/tracepoint.h"
46 #include "wrapper/list.h"
47 #include "lttng-kernel-version.h"
48 #include "lttng-events.h"
49 #include "lttng-tracer.h"
50 #include "lttng-abi-old.h"
52 #define METADATA_CACHE_DEFAULT_SIZE 4096
54 static LIST_HEAD(sessions
);
55 static LIST_HEAD(lttng_transport_list
);
57 * Protect the sessions and metadata caches.
59 static DEFINE_MUTEX(sessions_mutex
);
60 static struct kmem_cache
*event_cache
;
62 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
63 static void lttng_session_sync_enablers(struct lttng_session
*session
);
64 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
66 static void _lttng_event_destroy(struct lttng_event
*event
);
67 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
68 static int _lttng_event_unregister(struct lttng_event
*event
);
70 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
71 struct lttng_channel
*chan
,
72 struct lttng_event
*event
);
74 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
76 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
78 void synchronize_trace(void)
81 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
82 #ifdef CONFIG_PREEMPT_RT_FULL
85 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
86 #ifdef CONFIG_PREEMPT_RT
89 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
92 void lttng_lock_sessions(void)
94 mutex_lock(&sessions_mutex
);
97 void lttng_unlock_sessions(void)
99 mutex_unlock(&sessions_mutex
);
103 * Called with sessions lock held.
105 int lttng_session_active(void)
107 struct lttng_session
*iter
;
109 list_for_each_entry(iter
, &sessions
, list
) {
116 struct lttng_session
*lttng_session_create(void)
118 struct lttng_session
*session
;
119 struct lttng_metadata_cache
*metadata_cache
;
122 mutex_lock(&sessions_mutex
);
123 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
126 INIT_LIST_HEAD(&session
->chan
);
127 INIT_LIST_HEAD(&session
->events
);
128 uuid_le_gen(&session
->uuid
);
130 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
133 goto err_free_session
;
134 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
136 if (!metadata_cache
->data
)
138 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
139 kref_init(&metadata_cache
->refcount
);
140 session
->metadata_cache
= metadata_cache
;
141 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
142 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
143 sizeof(metadata_cache
->uuid
));
144 INIT_LIST_HEAD(&session
->enablers_head
);
145 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
146 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
147 list_add(&session
->list
, &sessions
);
148 mutex_unlock(&sessions_mutex
);
152 kfree(metadata_cache
);
156 mutex_unlock(&sessions_mutex
);
160 void metadata_cache_destroy(struct kref
*kref
)
162 struct lttng_metadata_cache
*cache
=
163 container_of(kref
, struct lttng_metadata_cache
, refcount
);
168 void lttng_session_destroy(struct lttng_session
*session
)
170 struct lttng_channel
*chan
, *tmpchan
;
171 struct lttng_event
*event
, *tmpevent
;
172 struct lttng_metadata_stream
*metadata_stream
;
173 struct lttng_enabler
*enabler
, *tmpenabler
;
176 mutex_lock(&sessions_mutex
);
177 ACCESS_ONCE(session
->active
) = 0;
178 list_for_each_entry(chan
, &session
->chan
, list
) {
179 ret
= lttng_syscalls_unregister(chan
);
182 list_for_each_entry(event
, &session
->events
, list
) {
183 ret
= _lttng_event_unregister(event
);
186 synchronize_trace(); /* Wait for in-flight events to complete */
187 list_for_each_entry_safe(enabler
, tmpenabler
,
188 &session
->enablers_head
, node
)
189 lttng_enabler_destroy(enabler
);
190 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
191 _lttng_event_destroy(event
);
192 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
193 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
194 _lttng_channel_destroy(chan
);
196 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
197 _lttng_metadata_channel_hangup(metadata_stream
);
198 if (session
->pid_tracker
)
199 lttng_pid_tracker_destroy(session
->pid_tracker
);
200 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
201 list_del(&session
->list
);
202 mutex_unlock(&sessions_mutex
);
206 int lttng_session_enable(struct lttng_session
*session
)
209 struct lttng_channel
*chan
;
211 mutex_lock(&sessions_mutex
);
212 if (session
->active
) {
217 /* Set transient enabler state to "enabled" */
221 * Snapshot the number of events per channel to know the type of header
224 list_for_each_entry(chan
, &session
->chan
, list
) {
225 if (chan
->header_type
)
226 continue; /* don't change it if session stop/restart */
227 if (chan
->free_event_id
< 31)
228 chan
->header_type
= 1; /* compact */
230 chan
->header_type
= 2; /* large */
233 /* We need to sync enablers with session before activation. */
234 lttng_session_sync_enablers(session
);
236 ACCESS_ONCE(session
->active
) = 1;
237 ACCESS_ONCE(session
->been_active
) = 1;
238 ret
= _lttng_session_metadata_statedump(session
);
240 ACCESS_ONCE(session
->active
) = 0;
243 ret
= lttng_statedump_start(session
);
245 ACCESS_ONCE(session
->active
) = 0;
247 mutex_unlock(&sessions_mutex
);
251 int lttng_session_disable(struct lttng_session
*session
)
255 mutex_lock(&sessions_mutex
);
256 if (!session
->active
) {
260 ACCESS_ONCE(session
->active
) = 0;
262 /* Set transient enabler state to "disabled" */
264 lttng_session_sync_enablers(session
);
266 mutex_unlock(&sessions_mutex
);
270 int lttng_channel_enable(struct lttng_channel
*channel
)
274 mutex_lock(&sessions_mutex
);
275 if (channel
->channel_type
== METADATA_CHANNEL
) {
279 if (channel
->enabled
) {
283 /* Set transient enabler state to "enabled" */
285 lttng_session_sync_enablers(channel
->session
);
286 /* Set atomically the state to "enabled" */
287 ACCESS_ONCE(channel
->enabled
) = 1;
289 mutex_unlock(&sessions_mutex
);
293 int lttng_channel_disable(struct lttng_channel
*channel
)
297 mutex_lock(&sessions_mutex
);
298 if (channel
->channel_type
== METADATA_CHANNEL
) {
302 if (!channel
->enabled
) {
306 /* Set atomically the state to "disabled" */
307 ACCESS_ONCE(channel
->enabled
) = 0;
308 /* Set transient enabler state to "enabled" */
310 lttng_session_sync_enablers(channel
->session
);
312 mutex_unlock(&sessions_mutex
);
316 int lttng_event_enable(struct lttng_event
*event
)
320 mutex_lock(&sessions_mutex
);
321 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
325 if (event
->enabled
) {
329 ACCESS_ONCE(event
->enabled
) = 1;
330 lttng_session_sync_enablers(event
->chan
->session
);
332 mutex_unlock(&sessions_mutex
);
336 int lttng_event_disable(struct lttng_event
*event
)
340 mutex_lock(&sessions_mutex
);
341 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
345 if (!event
->enabled
) {
349 ACCESS_ONCE(event
->enabled
) = 0;
350 lttng_session_sync_enablers(event
->chan
->session
);
352 mutex_unlock(&sessions_mutex
);
356 static struct lttng_transport
*lttng_transport_find(const char *name
)
358 struct lttng_transport
*transport
;
360 list_for_each_entry(transport
, <tng_transport_list
, node
) {
361 if (!strcmp(transport
->name
, name
))
367 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
368 const char *transport_name
,
370 size_t subbuf_size
, size_t num_subbuf
,
371 unsigned int switch_timer_interval
,
372 unsigned int read_timer_interval
,
373 enum channel_type channel_type
)
375 struct lttng_channel
*chan
;
376 struct lttng_transport
*transport
= NULL
;
378 mutex_lock(&sessions_mutex
);
379 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
380 goto active
; /* Refuse to add channel to active session */
381 transport
= lttng_transport_find(transport_name
);
383 printk(KERN_WARNING
"LTTng transport %s not found\n",
387 if (!try_module_get(transport
->owner
)) {
388 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
391 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
394 chan
->session
= session
;
395 chan
->id
= session
->free_chan_id
++;
396 chan
->ops
= &transport
->ops
;
398 * Note: the channel creation op already writes into the packet
399 * headers. Therefore the "chan" information used as input
400 * should be already accessible.
402 chan
->chan
= transport
->ops
.channel_create(transport_name
,
403 chan
, buf_addr
, subbuf_size
, num_subbuf
,
404 switch_timer_interval
, read_timer_interval
);
409 chan
->transport
= transport
;
410 chan
->channel_type
= channel_type
;
411 list_add(&chan
->list
, &session
->chan
);
412 mutex_unlock(&sessions_mutex
);
419 module_put(transport
->owner
);
422 mutex_unlock(&sessions_mutex
);
427 * Only used internally at session destruction for per-cpu channels, and
428 * when metadata channel is released.
429 * Needs to be called with sessions mutex held.
432 void _lttng_channel_destroy(struct lttng_channel
*chan
)
434 chan
->ops
->channel_destroy(chan
->chan
);
435 module_put(chan
->transport
->owner
);
436 list_del(&chan
->list
);
437 lttng_destroy_context(chan
->ctx
);
441 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
443 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
445 /* Protect the metadata cache with the sessions_mutex. */
446 mutex_lock(&sessions_mutex
);
447 _lttng_channel_destroy(chan
);
448 mutex_unlock(&sessions_mutex
);
450 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
453 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
455 stream
->finalized
= 1;
456 wake_up_interruptible(&stream
->read_wait
);
460 * Supports event creation while tracing session is active.
461 * Needs to be called with sessions mutex held.
463 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
464 struct lttng_kernel_event
*event_param
,
466 const struct lttng_event_desc
*event_desc
,
467 enum lttng_kernel_instrumentation itype
)
469 struct lttng_session
*session
= chan
->session
;
470 struct lttng_event
*event
;
471 const char *event_name
;
472 struct hlist_head
*head
;
477 if (chan
->free_event_id
== -1U) {
483 case LTTNG_KERNEL_TRACEPOINT
:
484 event_name
= event_desc
->name
;
486 case LTTNG_KERNEL_KPROBE
:
487 case LTTNG_KERNEL_KRETPROBE
:
488 case LTTNG_KERNEL_FUNCTION
:
489 case LTTNG_KERNEL_NOOP
:
490 case LTTNG_KERNEL_SYSCALL
:
491 event_name
= event_param
->name
;
498 name_len
= strlen(event_name
);
499 hash
= jhash(event_name
, name_len
, 0);
500 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
501 lttng_hlist_for_each_entry(event
, head
, hlist
) {
502 WARN_ON_ONCE(!event
->desc
);
503 if (!strncmp(event
->desc
->name
, event_name
,
504 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
505 && chan
== event
->chan
) {
511 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
517 event
->filter
= filter
;
518 event
->id
= chan
->free_event_id
++;
519 event
->instrumentation
= itype
;
520 event
->evtype
= LTTNG_TYPE_EVENT
;
521 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
522 INIT_LIST_HEAD(&event
->enablers_ref_head
);
525 case LTTNG_KERNEL_TRACEPOINT
:
526 /* Event will be enabled by enabler sync. */
528 event
->registered
= 0;
529 event
->desc
= lttng_event_get(event_name
);
534 /* Populate lttng_event structure before event registration. */
537 case LTTNG_KERNEL_KPROBE
:
539 event
->registered
= 1;
541 * Populate lttng_event structure before event
545 ret
= lttng_kprobes_register(event_name
,
546 event_param
->u
.kprobe
.symbol_name
,
547 event_param
->u
.kprobe
.offset
,
548 event_param
->u
.kprobe
.addr
,
554 ret
= try_module_get(event
->desc
->owner
);
557 case LTTNG_KERNEL_KRETPROBE
:
559 struct lttng_event
*event_return
;
561 /* kretprobe defines 2 events */
563 event
->registered
= 1;
565 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
570 event_return
->chan
= chan
;
571 event_return
->filter
= filter
;
572 event_return
->id
= chan
->free_event_id
++;
573 event_return
->enabled
= 1;
574 event_return
->registered
= 1;
575 event_return
->instrumentation
= itype
;
577 * Populate lttng_event structure before kretprobe registration.
580 ret
= lttng_kretprobes_register(event_name
,
581 event_param
->u
.kretprobe
.symbol_name
,
582 event_param
->u
.kretprobe
.offset
,
583 event_param
->u
.kretprobe
.addr
,
584 event
, event_return
);
586 kmem_cache_free(event_cache
, event_return
);
590 /* Take 2 refs on the module: one per event. */
591 ret
= try_module_get(event
->desc
->owner
);
593 ret
= try_module_get(event
->desc
->owner
);
595 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
597 WARN_ON_ONCE(ret
> 0);
599 kmem_cache_free(event_cache
, event_return
);
600 module_put(event
->desc
->owner
);
601 module_put(event
->desc
->owner
);
602 goto statedump_error
;
604 list_add(&event_return
->list
, &chan
->session
->events
);
607 case LTTNG_KERNEL_FUNCTION
:
609 event
->registered
= 1;
611 * Populate lttng_event structure before event
615 ret
= lttng_ftrace_register(event_name
,
616 event_param
->u
.ftrace
.symbol_name
,
621 ret
= try_module_get(event
->desc
->owner
);
624 case LTTNG_KERNEL_NOOP
:
625 case LTTNG_KERNEL_SYSCALL
:
627 event
->registered
= 0;
628 event
->desc
= event_desc
;
639 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
640 WARN_ON_ONCE(ret
> 0);
642 goto statedump_error
;
644 hlist_add_head(&event
->hlist
, head
);
645 list_add(&event
->list
, &chan
->session
->events
);
646 mutex_unlock(&sessions_mutex
);
650 /* If a statedump error occurs, events will not be readable. */
652 kmem_cache_free(event_cache
, event
);
660 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
661 struct lttng_kernel_event
*event_param
,
663 const struct lttng_event_desc
*event_desc
,
664 enum lttng_kernel_instrumentation itype
)
666 struct lttng_event
*event
;
668 mutex_lock(&sessions_mutex
);
669 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
671 mutex_unlock(&sessions_mutex
);
675 /* Only used for tracepoints for now. */
677 void register_event(struct lttng_event
*event
)
679 const struct lttng_event_desc
*desc
;
682 if (event
->registered
)
686 switch (event
->instrumentation
) {
687 case LTTNG_KERNEL_TRACEPOINT
:
688 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
689 desc
->probe_callback
,
692 case LTTNG_KERNEL_SYSCALL
:
693 ret
= lttng_syscall_filter_enable(event
->chan
,
696 case LTTNG_KERNEL_KPROBE
:
697 case LTTNG_KERNEL_KRETPROBE
:
698 case LTTNG_KERNEL_FUNCTION
:
699 case LTTNG_KERNEL_NOOP
:
706 event
->registered
= 1;
710 * Only used internally at session destruction.
712 int _lttng_event_unregister(struct lttng_event
*event
)
714 const struct lttng_event_desc
*desc
;
717 if (!event
->registered
)
721 switch (event
->instrumentation
) {
722 case LTTNG_KERNEL_TRACEPOINT
:
723 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
724 event
->desc
->probe_callback
,
727 case LTTNG_KERNEL_KPROBE
:
728 lttng_kprobes_unregister(event
);
731 case LTTNG_KERNEL_KRETPROBE
:
732 lttng_kretprobes_unregister(event
);
735 case LTTNG_KERNEL_FUNCTION
:
736 lttng_ftrace_unregister(event
);
739 case LTTNG_KERNEL_SYSCALL
:
740 ret
= lttng_syscall_filter_disable(event
->chan
,
743 case LTTNG_KERNEL_NOOP
:
750 event
->registered
= 0;
755 * Only used internally at session destruction.
758 void _lttng_event_destroy(struct lttng_event
*event
)
760 switch (event
->instrumentation
) {
761 case LTTNG_KERNEL_TRACEPOINT
:
762 lttng_event_put(event
->desc
);
764 case LTTNG_KERNEL_KPROBE
:
765 module_put(event
->desc
->owner
);
766 lttng_kprobes_destroy_private(event
);
768 case LTTNG_KERNEL_KRETPROBE
:
769 module_put(event
->desc
->owner
);
770 lttng_kretprobes_destroy_private(event
);
772 case LTTNG_KERNEL_FUNCTION
:
773 module_put(event
->desc
->owner
);
774 lttng_ftrace_destroy_private(event
);
776 case LTTNG_KERNEL_NOOP
:
777 case LTTNG_KERNEL_SYSCALL
:
782 list_del(&event
->list
);
783 lttng_destroy_context(event
->ctx
);
784 kmem_cache_free(event_cache
, event
);
787 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
793 mutex_lock(&sessions_mutex
);
795 /* track all pids: destroy tracker. */
796 if (session
->pid_tracker
) {
797 struct lttng_pid_tracker
*lpf
;
799 lpf
= session
->pid_tracker
;
800 rcu_assign_pointer(session
->pid_tracker
, NULL
);
802 lttng_pid_tracker_destroy(lpf
);
806 if (!session
->pid_tracker
) {
807 struct lttng_pid_tracker
*lpf
;
809 lpf
= lttng_pid_tracker_create();
814 ret
= lttng_pid_tracker_add(lpf
, pid
);
815 rcu_assign_pointer(session
->pid_tracker
, lpf
);
817 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
821 mutex_unlock(&sessions_mutex
);
825 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
831 mutex_lock(&sessions_mutex
);
833 /* untrack all pids: replace by empty tracker. */
834 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
835 struct lttng_pid_tracker
*lpf
;
837 lpf
= lttng_pid_tracker_create();
842 rcu_assign_pointer(session
->pid_tracker
, lpf
);
845 lttng_pid_tracker_destroy(old_lpf
);
848 if (!session
->pid_tracker
) {
852 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
855 mutex_unlock(&sessions_mutex
);
860 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
862 struct lttng_session
*session
= m
->private;
863 struct lttng_pid_tracker
*lpf
;
864 struct lttng_pid_hash_node
*e
;
867 mutex_lock(&sessions_mutex
);
868 lpf
= session
->pid_tracker
;
870 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
871 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
873 lttng_hlist_for_each_entry(e
, head
, hlist
) {
879 /* PID tracker disabled. */
880 if (iter
>= *pos
&& iter
== 0) {
881 return session
; /* empty tracker */
889 /* Called with sessions_mutex held. */
891 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
893 struct lttng_session
*session
= m
->private;
894 struct lttng_pid_tracker
*lpf
;
895 struct lttng_pid_hash_node
*e
;
899 lpf
= session
->pid_tracker
;
901 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
902 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
904 lttng_hlist_for_each_entry(e
, head
, hlist
) {
910 /* PID tracker disabled. */
911 if (iter
>= *ppos
&& iter
== 0)
912 return session
; /* empty tracker */
921 void pid_list_stop(struct seq_file
*m
, void *p
)
923 mutex_unlock(&sessions_mutex
);
927 int pid_list_show(struct seq_file
*m
, void *p
)
931 if (p
== m
->private) {
932 /* Tracker disabled. */
935 const struct lttng_pid_hash_node
*e
= p
;
937 pid
= lttng_pid_tracker_get_node_pid(e
);
939 seq_printf(m
, "process { pid = %d; };\n", pid
);
944 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
945 .start
= pid_list_start
,
946 .next
= pid_list_next
,
947 .stop
= pid_list_stop
,
948 .show
= pid_list_show
,
952 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
954 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
958 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
960 struct seq_file
*m
= file
->private_data
;
961 struct lttng_session
*session
= m
->private;
964 WARN_ON_ONCE(!session
);
965 ret
= seq_release(inode
, file
);
971 const struct file_operations lttng_tracker_pids_list_fops
= {
972 .owner
= THIS_MODULE
,
973 .open
= lttng_tracker_pids_list_open
,
976 .release
= lttng_tracker_pids_list_release
,
979 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
981 struct file
*tracker_pids_list_file
;
985 file_fd
= lttng_get_unused_fd();
991 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
992 <tng_tracker_pids_list_fops
,
994 if (IS_ERR(tracker_pids_list_file
)) {
995 ret
= PTR_ERR(tracker_pids_list_file
);
998 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1001 m
= tracker_pids_list_file
->private_data
;
1002 m
->private = session
;
1003 fd_install(file_fd
, tracker_pids_list_file
);
1004 atomic_long_inc(&session
->file
->f_count
);
1009 fput(tracker_pids_list_file
);
1011 put_unused_fd(file_fd
);
1017 * Enabler management.
1020 int lttng_match_enabler_wildcard(const char *desc_name
,
1023 /* Compare excluding final '*' */
1024 if (strncmp(desc_name
, name
, strlen(name
) - 1))
1030 int lttng_match_enabler_name(const char *desc_name
,
1033 if (strcmp(desc_name
, name
))
1039 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1040 struct lttng_enabler
*enabler
)
1042 const char *desc_name
, *enabler_name
;
1044 enabler_name
= enabler
->event_param
.name
;
1045 switch (enabler
->event_param
.instrumentation
) {
1046 case LTTNG_KERNEL_TRACEPOINT
:
1047 desc_name
= desc
->name
;
1049 case LTTNG_KERNEL_SYSCALL
:
1050 desc_name
= desc
->name
;
1051 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1052 desc_name
+= strlen("compat_");
1053 if (!strncmp(desc_name
, "syscall_exit_",
1054 strlen("syscall_exit_"))) {
1055 desc_name
+= strlen("syscall_exit_");
1056 } else if (!strncmp(desc_name
, "syscall_entry_",
1057 strlen("syscall_entry_"))) {
1058 desc_name
+= strlen("syscall_entry_");
1068 switch (enabler
->type
) {
1069 case LTTNG_ENABLER_WILDCARD
:
1070 return lttng_match_enabler_wildcard(desc_name
, enabler_name
);
1071 case LTTNG_ENABLER_NAME
:
1072 return lttng_match_enabler_name(desc_name
, enabler_name
);
1079 int lttng_event_match_enabler(struct lttng_event
*event
,
1080 struct lttng_enabler
*enabler
)
1082 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1084 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1085 && event
->chan
== enabler
->chan
)
1092 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1093 struct lttng_enabler
*enabler
)
1095 struct lttng_enabler_ref
*enabler_ref
;
1097 list_for_each_entry(enabler_ref
,
1098 &event
->enablers_ref_head
, node
) {
1099 if (enabler_ref
->ref
== enabler
)
1106 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1108 struct lttng_session
*session
= enabler
->chan
->session
;
1109 struct lttng_probe_desc
*probe_desc
;
1110 const struct lttng_event_desc
*desc
;
1112 struct list_head
*probe_list
;
1114 probe_list
= lttng_get_probe_list_head();
1116 * For each probe event, if we find that a probe event matches
1117 * our enabler, create an associated lttng_event if not
1120 list_for_each_entry(probe_desc
, probe_list
, head
) {
1121 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1123 struct hlist_head
*head
;
1124 const char *event_name
;
1127 struct lttng_event
*event
;
1129 desc
= probe_desc
->event_desc
[i
];
1130 if (!lttng_desc_match_enabler(desc
, enabler
))
1132 event_name
= desc
->name
;
1133 name_len
= strlen(event_name
);
1136 * Check if already created.
1138 hash
= jhash(event_name
, name_len
, 0);
1139 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1140 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1141 if (event
->desc
== desc
1142 && event
->chan
== enabler
->chan
)
1149 * We need to create an event for this
1152 event
= _lttng_event_create(enabler
->chan
,
1154 LTTNG_KERNEL_TRACEPOINT
);
1156 printk(KERN_INFO
"Unable to create event %s\n",
1157 probe_desc
->event_desc
[i
]->name
);
1164 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1168 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1173 * Create struct lttng_event if it is missing and present in the list of
1174 * tracepoint probes.
1175 * Should be called with sessions mutex held.
1178 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1180 switch (enabler
->event_param
.instrumentation
) {
1181 case LTTNG_KERNEL_TRACEPOINT
:
1182 lttng_create_tracepoint_if_missing(enabler
);
1184 case LTTNG_KERNEL_SYSCALL
:
1185 lttng_create_syscall_if_missing(enabler
);
1194 * Create events associated with an enabler (if not already present),
1195 * and add backward reference from the event to the enabler.
1196 * Should be called with sessions mutex held.
1199 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1201 struct lttng_session
*session
= enabler
->chan
->session
;
1202 struct lttng_event
*event
;
1204 /* First ensure that probe events are created for this enabler. */
1205 lttng_create_event_if_missing(enabler
);
1207 /* For each event matching enabler in session event list. */
1208 list_for_each_entry(event
, &session
->events
, list
) {
1209 struct lttng_enabler_ref
*enabler_ref
;
1211 if (!lttng_event_match_enabler(event
, enabler
))
1213 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1216 * If no backward ref, create it.
1217 * Add backward ref from event to enabler.
1219 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1222 enabler_ref
->ref
= enabler
;
1223 list_add(&enabler_ref
->node
,
1224 &event
->enablers_ref_head
);
1228 * Link filter bytecodes if not linked yet.
1230 lttng_enabler_event_link_bytecode(event
, enabler
);
1232 /* TODO: merge event context. */
1238 * Called at module load: connect the probe on all enablers matching
1240 * Called with sessions lock held.
1242 int lttng_fix_pending_events(void)
1244 struct lttng_session
*session
;
1246 list_for_each_entry(session
, &sessions
, list
)
1247 lttng_session_lazy_sync_enablers(session
);
1251 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1252 struct lttng_kernel_event
*event_param
,
1253 struct lttng_channel
*chan
)
1255 struct lttng_enabler
*enabler
;
1257 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1260 enabler
->type
= type
;
1261 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1262 memcpy(&enabler
->event_param
, event_param
,
1263 sizeof(enabler
->event_param
));
1264 enabler
->chan
= chan
;
1266 enabler
->enabled
= 0;
1267 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1268 mutex_lock(&sessions_mutex
);
1269 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1270 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1271 mutex_unlock(&sessions_mutex
);
1275 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1277 mutex_lock(&sessions_mutex
);
1278 enabler
->enabled
= 1;
1279 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1280 mutex_unlock(&sessions_mutex
);
1284 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1286 mutex_lock(&sessions_mutex
);
1287 enabler
->enabled
= 0;
1288 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1289 mutex_unlock(&sessions_mutex
);
1293 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1294 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1296 struct lttng_filter_bytecode_node
*bytecode_node
;
1297 uint32_t bytecode_len
;
1300 ret
= get_user(bytecode_len
, &bytecode
->len
);
1303 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1307 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1308 sizeof(*bytecode
) + bytecode_len
);
1311 bytecode_node
->enabler
= enabler
;
1312 /* Enforce length based on allocated size */
1313 bytecode_node
->bc
.len
= bytecode_len
;
1314 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1315 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1319 kfree(bytecode_node
);
1323 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1324 struct lttng_kernel_context
*context_param
)
1330 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1332 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1334 /* Destroy filter bytecode */
1335 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1336 &enabler
->filter_bytecode_head
, node
) {
1340 /* Destroy contexts */
1341 lttng_destroy_context(enabler
->ctx
);
1343 list_del(&enabler
->node
);
1348 * lttng_session_sync_enablers should be called just before starting a
1350 * Should be called with sessions mutex held.
1353 void lttng_session_sync_enablers(struct lttng_session
*session
)
1355 struct lttng_enabler
*enabler
;
1356 struct lttng_event
*event
;
1358 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1359 lttng_enabler_ref_events(enabler
);
1361 * For each event, if at least one of its enablers is enabled,
1362 * and its channel and session transient states are enabled, we
1363 * enable the event, else we disable it.
1365 list_for_each_entry(event
, &session
->events
, list
) {
1366 struct lttng_enabler_ref
*enabler_ref
;
1367 struct lttng_bytecode_runtime
*runtime
;
1368 int enabled
= 0, has_enablers_without_bytecode
= 0;
1370 switch (event
->instrumentation
) {
1371 case LTTNG_KERNEL_TRACEPOINT
:
1372 case LTTNG_KERNEL_SYSCALL
:
1374 list_for_each_entry(enabler_ref
,
1375 &event
->enablers_ref_head
, node
) {
1376 if (enabler_ref
->ref
->enabled
) {
1383 /* Not handled with lazy sync. */
1387 * Enabled state is based on union of enablers, with
1388 * intesection of session and channel transient enable
1391 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1393 ACCESS_ONCE(event
->enabled
) = enabled
;
1395 * Sync tracepoint registration with event enabled
1399 register_event(event
);
1401 _lttng_event_unregister(event
);
1404 /* Check if has enablers without bytecode enabled */
1405 list_for_each_entry(enabler_ref
,
1406 &event
->enablers_ref_head
, node
) {
1407 if (enabler_ref
->ref
->enabled
1408 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1409 has_enablers_without_bytecode
= 1;
1413 event
->has_enablers_without_bytecode
=
1414 has_enablers_without_bytecode
;
1416 /* Enable filters */
1417 list_for_each_entry(runtime
,
1418 &event
->bytecode_runtime_head
, node
)
1419 lttng_filter_sync_state(runtime
);
1424 * Apply enablers to session events, adding events to session if need
1425 * be. It is required after each modification applied to an active
1426 * session, and right before session "start".
1427 * "lazy" sync means we only sync if required.
1428 * Should be called with sessions mutex held.
1431 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1433 /* We can skip if session is not active */
1434 if (!session
->active
)
1436 lttng_session_sync_enablers(session
);
1440 * Serialize at most one packet worth of metadata into a metadata
1442 * We have exclusive access to our metadata buffer (protected by the
1443 * sessions_mutex), so we can do racy operations such as looking for
1444 * remaining space left in packet and write, since mutual exclusion
1445 * protects us from concurrent writes.
1446 * Returns the number of bytes written in the channel, 0 if no data
1447 * was written and a negative value on error.
1449 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1450 struct channel
*chan
)
1452 struct lib_ring_buffer_ctx ctx
;
1454 size_t len
, reserve_len
;
1457 * Ensure we support mutiple get_next / put sequences followed
1458 * by put_next. The metadata stream lock internally protects
1459 * reading the metadata cache. It can indeed be read
1460 * concurrently by "get_next_subbuf" and "flush" operations on
1461 * the buffer invoked by different processes.
1463 mutex_lock(&stream
->lock
);
1464 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1465 if (stream
->metadata_in
!= stream
->metadata_out
)
1468 len
= stream
->metadata_cache
->metadata_written
-
1469 stream
->metadata_in
;
1472 reserve_len
= min_t(size_t,
1473 stream
->transport
->ops
.packet_avail_size(chan
),
1475 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1478 * If reservation failed, return an error to the caller.
1480 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1482 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1485 stream
->transport
->ops
.event_write(&ctx
,
1486 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1488 stream
->transport
->ops
.event_commit(&ctx
);
1489 stream
->metadata_in
+= reserve_len
;
1493 mutex_unlock(&stream
->lock
);
1498 * Write the metadata to the metadata cache.
1499 * Must be called with sessions_mutex held.
1501 int lttng_metadata_printf(struct lttng_session
*session
,
1502 const char *fmt
, ...)
1507 struct lttng_metadata_stream
*stream
;
1509 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
1512 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1518 if (session
->metadata_cache
->metadata_written
+ len
>
1519 session
->metadata_cache
->cache_alloc
) {
1520 char *tmp_cache_realloc
;
1521 unsigned int tmp_cache_alloc_size
;
1523 tmp_cache_alloc_size
= max_t(unsigned int,
1524 session
->metadata_cache
->cache_alloc
+ len
,
1525 session
->metadata_cache
->cache_alloc
<< 1);
1526 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
1527 tmp_cache_alloc_size
, GFP_KERNEL
);
1528 if (!tmp_cache_realloc
)
1530 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1531 session
->metadata_cache
->data
= tmp_cache_realloc
;
1533 memcpy(session
->metadata_cache
->data
+
1534 session
->metadata_cache
->metadata_written
,
1536 session
->metadata_cache
->metadata_written
+= len
;
1539 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1540 wake_up_interruptible(&stream
->read_wait
);
1550 * Must be called with sessions_mutex held.
1553 int _lttng_field_statedump(struct lttng_session
*session
,
1554 const struct lttng_event_field
*field
)
1558 switch (field
->type
.atype
) {
1560 ret
= lttng_metadata_printf(session
,
1561 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1562 field
->type
.u
.basic
.integer
.size
,
1563 field
->type
.u
.basic
.integer
.alignment
,
1564 field
->type
.u
.basic
.integer
.signedness
,
1565 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
1567 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1570 field
->type
.u
.basic
.integer
.base
,
1572 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1574 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1579 ret
= lttng_metadata_printf(session
,
1581 field
->type
.u
.basic
.enumeration
.name
,
1586 const struct lttng_basic_type
*elem_type
;
1588 elem_type
= &field
->type
.u
.array
.elem_type
;
1589 ret
= lttng_metadata_printf(session
,
1590 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1591 elem_type
->u
.basic
.integer
.size
,
1592 elem_type
->u
.basic
.integer
.alignment
,
1593 elem_type
->u
.basic
.integer
.signedness
,
1594 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1596 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1599 elem_type
->u
.basic
.integer
.base
,
1601 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1603 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1605 field
->name
, field
->type
.u
.array
.length
);
1608 case atype_sequence
:
1610 const struct lttng_basic_type
*elem_type
;
1611 const struct lttng_basic_type
*length_type
;
1613 elem_type
= &field
->type
.u
.sequence
.elem_type
;
1614 length_type
= &field
->type
.u
.sequence
.length_type
;
1615 ret
= lttng_metadata_printf(session
,
1616 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1617 length_type
->u
.basic
.integer
.size
,
1618 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
1619 length_type
->u
.basic
.integer
.signedness
,
1620 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1622 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1625 length_type
->u
.basic
.integer
.base
,
1627 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1629 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1635 ret
= lttng_metadata_printf(session
,
1636 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1637 elem_type
->u
.basic
.integer
.size
,
1638 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
1639 elem_type
->u
.basic
.integer
.signedness
,
1640 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1642 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1645 elem_type
->u
.basic
.integer
.base
,
1647 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1649 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1657 /* Default encoding is UTF8 */
1658 ret
= lttng_metadata_printf(session
,
1660 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
1661 " { encoding = ASCII; }" : "",
1672 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
1673 struct lttng_ctx
*ctx
)
1680 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
1681 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
1683 ret
= _lttng_field_statedump(session
, &field
->event_field
);
1691 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
1692 struct lttng_event
*event
)
1694 const struct lttng_event_desc
*desc
= event
->desc
;
1698 for (i
= 0; i
< desc
->nr_fields
; i
++) {
1699 const struct lttng_event_field
*field
= &desc
->fields
[i
];
1701 ret
= _lttng_field_statedump(session
, field
);
1709 * Must be called with sessions_mutex held.
1712 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
1713 struct lttng_channel
*chan
,
1714 struct lttng_event
*event
)
1718 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1720 if (chan
->channel_type
== METADATA_CHANNEL
)
1723 ret
= lttng_metadata_printf(session
,
1727 " stream_id = %u;\n",
1735 ret
= lttng_metadata_printf(session
,
1736 " context := struct {\n");
1740 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
1744 ret
= lttng_metadata_printf(session
,
1750 ret
= lttng_metadata_printf(session
,
1751 " fields := struct {\n"
1756 ret
= _lttng_fields_metadata_statedump(session
, event
);
1761 * LTTng space reservation can only reserve multiples of the
1764 ret
= lttng_metadata_printf(session
,
1770 event
->metadata_dumped
= 1;
1777 * Must be called with sessions_mutex held.
1780 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1781 struct lttng_channel
*chan
)
1785 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1788 if (chan
->channel_type
== METADATA_CHANNEL
)
1791 WARN_ON_ONCE(!chan
->header_type
);
1792 ret
= lttng_metadata_printf(session
,
1795 " event.header := %s;\n"
1796 " packet.context := struct packet_context;\n",
1798 chan
->header_type
== 1 ? "struct event_header_compact" :
1799 "struct event_header_large");
1804 ret
= lttng_metadata_printf(session
,
1805 " event.context := struct {\n");
1809 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1813 ret
= lttng_metadata_printf(session
,
1819 ret
= lttng_metadata_printf(session
,
1822 chan
->metadata_dumped
= 1;
1828 * Must be called with sessions_mutex held.
1831 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1833 return lttng_metadata_printf(session
,
1834 "struct packet_context {\n"
1835 " uint64_clock_monotonic_t timestamp_begin;\n"
1836 " uint64_clock_monotonic_t timestamp_end;\n"
1837 " uint64_t content_size;\n"
1838 " uint64_t packet_size;\n"
1839 " unsigned long events_discarded;\n"
1840 " uint32_t cpu_id;\n"
1847 * id: range: 0 - 30.
1848 * id 31 is reserved to indicate an extended header.
1851 * id: range: 0 - 65534.
1852 * id 65535 is reserved to indicate an extended header.
1854 * Must be called with sessions_mutex held.
1857 int _lttng_event_header_declare(struct lttng_session
*session
)
1859 return lttng_metadata_printf(session
,
1860 "struct event_header_compact {\n"
1861 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1864 " uint27_clock_monotonic_t timestamp;\n"
1868 " uint64_clock_monotonic_t timestamp;\n"
1873 "struct event_header_large {\n"
1874 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1877 " uint32_clock_monotonic_t timestamp;\n"
1881 " uint64_clock_monotonic_t timestamp;\n"
1885 lttng_alignof(uint32_t) * CHAR_BIT
,
1886 lttng_alignof(uint16_t) * CHAR_BIT
1891 * Approximation of NTP time of day to clock monotonic correlation,
1892 * taken at start of trace.
1893 * Yes, this is only an approximation. Yes, we can (and will) do better
1894 * in future versions.
1897 uint64_t measure_clock_offset(void)
1899 uint64_t offset
, monotonic
[2], realtime
;
1900 struct timespec rts
= { 0, 0 };
1901 unsigned long flags
;
1903 /* Disable interrupts to increase correlation precision. */
1904 local_irq_save(flags
);
1905 monotonic
[0] = trace_clock_read64();
1906 getnstimeofday(&rts
);
1907 monotonic
[1] = trace_clock_read64();
1908 local_irq_restore(flags
);
1910 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1911 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1912 realtime
+= rts
.tv_nsec
;
1913 offset
= realtime
- offset
;
1918 * Output metadata into this session's metadata buffers.
1919 * Must be called with sessions_mutex held.
1922 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1924 unsigned char *uuid_c
= session
->uuid
.b
;
1925 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1926 struct lttng_channel
*chan
;
1927 struct lttng_event
*event
;
1930 if (!ACCESS_ONCE(session
->active
))
1932 if (session
->metadata_dumped
)
1935 snprintf(uuid_s
, sizeof(uuid_s
),
1936 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1937 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1938 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1939 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1940 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1942 ret
= lttng_metadata_printf(session
,
1943 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1944 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1945 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1946 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1947 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1948 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1949 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1955 " byte_order = %s;\n"
1956 " packet.header := struct {\n"
1957 " uint32_t magic;\n"
1958 " uint8_t uuid[16];\n"
1959 " uint32_t stream_id;\n"
1962 lttng_alignof(uint8_t) * CHAR_BIT
,
1963 lttng_alignof(uint16_t) * CHAR_BIT
,
1964 lttng_alignof(uint32_t) * CHAR_BIT
,
1965 lttng_alignof(uint64_t) * CHAR_BIT
,
1966 sizeof(unsigned long) * CHAR_BIT
,
1967 lttng_alignof(unsigned long) * CHAR_BIT
,
1980 ret
= lttng_metadata_printf(session
,
1982 " hostname = \"%s\";\n"
1983 " domain = \"kernel\";\n"
1984 " sysname = \"%s\";\n"
1985 " kernel_release = \"%s\";\n"
1986 " kernel_version = \"%s\";\n"
1987 " tracer_name = \"lttng-modules\";\n"
1988 " tracer_major = %d;\n"
1989 " tracer_minor = %d;\n"
1990 " tracer_patchlevel = %d;\n"
1992 current
->nsproxy
->uts_ns
->name
.nodename
,
1996 LTTNG_MODULES_MAJOR_VERSION
,
1997 LTTNG_MODULES_MINOR_VERSION
,
1998 LTTNG_MODULES_PATCHLEVEL_VERSION
2003 ret
= lttng_metadata_printf(session
,
2011 if (!trace_clock_uuid(clock_uuid_s
)) {
2012 ret
= lttng_metadata_printf(session
,
2013 " uuid = \"%s\";\n",
2020 ret
= lttng_metadata_printf(session
,
2021 " description = \"Monotonic Clock\";\n"
2022 " freq = %llu; /* Frequency, in Hz */\n"
2023 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2026 (unsigned long long) trace_clock_freq(),
2027 (unsigned long long) measure_clock_offset()
2032 ret
= lttng_metadata_printf(session
,
2033 "typealias integer {\n"
2034 " size = 27; align = 1; signed = false;\n"
2035 " map = clock.monotonic.value;\n"
2036 "} := uint27_clock_monotonic_t;\n"
2038 "typealias integer {\n"
2039 " size = 32; align = %u; signed = false;\n"
2040 " map = clock.monotonic.value;\n"
2041 "} := uint32_clock_monotonic_t;\n"
2043 "typealias integer {\n"
2044 " size = 64; align = %u; signed = false;\n"
2045 " map = clock.monotonic.value;\n"
2046 "} := uint64_clock_monotonic_t;\n\n",
2047 lttng_alignof(uint32_t) * CHAR_BIT
,
2048 lttng_alignof(uint64_t) * CHAR_BIT
2053 ret
= _lttng_stream_packet_context_declare(session
);
2057 ret
= _lttng_event_header_declare(session
);
2062 list_for_each_entry(chan
, &session
->chan
, list
) {
2063 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2068 list_for_each_entry(event
, &session
->events
, list
) {
2069 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2073 session
->metadata_dumped
= 1;
2079 * lttng_transport_register - LTT transport registration
2080 * @transport: transport structure
2082 * Registers a transport which can be used as output to extract the data out of
2083 * LTTng. The module calling this registration function must ensure that no
2084 * trap-inducing code will be executed by the transport functions. E.g.
2085 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2086 * is made visible to the transport function. This registration acts as a
2087 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2088 * after its registration must it synchronize the TLBs.
2090 void lttng_transport_register(struct lttng_transport
*transport
)
2093 * Make sure no page fault can be triggered by the module about to be
2094 * registered. We deal with this here so we don't have to call
2095 * vmalloc_sync_all() in each module's init.
2097 wrapper_vmalloc_sync_all();
2099 mutex_lock(&sessions_mutex
);
2100 list_add_tail(&transport
->node
, <tng_transport_list
);
2101 mutex_unlock(&sessions_mutex
);
2103 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2106 * lttng_transport_unregister - LTT transport unregistration
2107 * @transport: transport structure
2109 void lttng_transport_unregister(struct lttng_transport
*transport
)
2111 mutex_lock(&sessions_mutex
);
2112 list_del(&transport
->node
);
2113 mutex_unlock(&sessions_mutex
);
2115 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2117 static int __init
lttng_events_init(void)
2121 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2124 ret
= wrapper_get_pfnblock_flags_mask_init();
2127 ret
= lttng_context_init();
2130 ret
= lttng_tracepoint_init();
2133 event_cache
= KMEM_CACHE(lttng_event
, 0);
2138 ret
= lttng_abi_init();
2141 ret
= lttng_logger_init();
2149 kmem_cache_destroy(event_cache
);
2151 lttng_tracepoint_exit();
2153 lttng_context_exit();
2157 module_init(lttng_events_init
);
2159 static void __exit
lttng_events_exit(void)
2161 struct lttng_session
*session
, *tmpsession
;
2163 lttng_logger_exit();
2165 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
2166 lttng_session_destroy(session
);
2167 kmem_cache_destroy(event_cache
);
2168 lttng_tracepoint_exit();
2169 lttng_context_exit();
2172 module_exit(lttng_events_exit
);
2174 MODULE_LICENSE("GPL and additional rights");
2175 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2176 MODULE_DESCRIPTION("LTTng Events");
2177 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
2178 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
2179 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
2180 LTTNG_MODULES_EXTRAVERSION
);