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"
54 #define METADATA_CACHE_DEFAULT_SIZE 4096
56 static LIST_HEAD(sessions
);
57 static LIST_HEAD(lttng_transport_list
);
59 * Protect the sessions and metadata caches.
61 static DEFINE_MUTEX(sessions_mutex
);
62 static struct kmem_cache
*event_cache
;
64 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
65 static void lttng_session_sync_enablers(struct lttng_session
*session
);
66 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
68 static void _lttng_event_destroy(struct lttng_event
*event
);
69 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
70 static int _lttng_event_unregister(struct lttng_event
*event
);
72 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
73 struct lttng_channel
*chan
,
74 struct lttng_event
*event
);
76 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
78 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
80 void synchronize_trace(void)
83 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
84 #ifdef CONFIG_PREEMPT_RT_FULL
87 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
88 #ifdef CONFIG_PREEMPT_RT
91 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
94 void lttng_lock_sessions(void)
96 mutex_lock(&sessions_mutex
);
99 void lttng_unlock_sessions(void)
101 mutex_unlock(&sessions_mutex
);
105 * Called with sessions lock held.
107 int lttng_session_active(void)
109 struct lttng_session
*iter
;
111 list_for_each_entry(iter
, &sessions
, list
) {
118 struct lttng_session
*lttng_session_create(void)
120 struct lttng_session
*session
;
121 struct lttng_metadata_cache
*metadata_cache
;
124 mutex_lock(&sessions_mutex
);
125 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
128 INIT_LIST_HEAD(&session
->chan
);
129 INIT_LIST_HEAD(&session
->events
);
130 uuid_le_gen(&session
->uuid
);
132 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
135 goto err_free_session
;
136 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
137 if (!metadata_cache
->data
)
139 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
140 kref_init(&metadata_cache
->refcount
);
141 mutex_init(&metadata_cache
->lock
);
142 session
->metadata_cache
= metadata_cache
;
143 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
144 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
145 sizeof(metadata_cache
->uuid
));
146 INIT_LIST_HEAD(&session
->enablers_head
);
147 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
148 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
149 list_add(&session
->list
, &sessions
);
150 mutex_unlock(&sessions_mutex
);
154 kfree(metadata_cache
);
158 mutex_unlock(&sessions_mutex
);
162 void metadata_cache_destroy(struct kref
*kref
)
164 struct lttng_metadata_cache
*cache
=
165 container_of(kref
, struct lttng_metadata_cache
, refcount
);
170 void lttng_session_destroy(struct lttng_session
*session
)
172 struct lttng_channel
*chan
, *tmpchan
;
173 struct lttng_event
*event
, *tmpevent
;
174 struct lttng_metadata_stream
*metadata_stream
;
175 struct lttng_enabler
*enabler
, *tmpenabler
;
178 mutex_lock(&sessions_mutex
);
179 ACCESS_ONCE(session
->active
) = 0;
180 list_for_each_entry(chan
, &session
->chan
, list
) {
181 ret
= lttng_syscalls_unregister(chan
);
184 list_for_each_entry(event
, &session
->events
, list
) {
185 ret
= _lttng_event_unregister(event
);
188 synchronize_trace(); /* Wait for in-flight events to complete */
189 list_for_each_entry_safe(enabler
, tmpenabler
,
190 &session
->enablers_head
, node
)
191 lttng_enabler_destroy(enabler
);
192 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
193 _lttng_event_destroy(event
);
194 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
195 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
196 _lttng_channel_destroy(chan
);
198 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
199 _lttng_metadata_channel_hangup(metadata_stream
);
200 if (session
->pid_tracker
)
201 lttng_pid_tracker_destroy(session
->pid_tracker
);
202 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
203 list_del(&session
->list
);
204 mutex_unlock(&sessions_mutex
);
208 int lttng_session_enable(struct lttng_session
*session
)
211 struct lttng_channel
*chan
;
213 mutex_lock(&sessions_mutex
);
214 if (session
->active
) {
219 /* Set transient enabler state to "enabled" */
223 * Snapshot the number of events per channel to know the type of header
226 list_for_each_entry(chan
, &session
->chan
, list
) {
227 if (chan
->header_type
)
228 continue; /* don't change it if session stop/restart */
229 if (chan
->free_event_id
< 31)
230 chan
->header_type
= 1; /* compact */
232 chan
->header_type
= 2; /* large */
235 /* We need to sync enablers with session before activation. */
236 lttng_session_sync_enablers(session
);
238 ACCESS_ONCE(session
->active
) = 1;
239 ACCESS_ONCE(session
->been_active
) = 1;
240 ret
= _lttng_session_metadata_statedump(session
);
242 ACCESS_ONCE(session
->active
) = 0;
245 ret
= lttng_statedump_start(session
);
247 ACCESS_ONCE(session
->active
) = 0;
249 mutex_unlock(&sessions_mutex
);
253 int lttng_session_disable(struct lttng_session
*session
)
257 mutex_lock(&sessions_mutex
);
258 if (!session
->active
) {
262 ACCESS_ONCE(session
->active
) = 0;
264 /* Set transient enabler state to "disabled" */
266 lttng_session_sync_enablers(session
);
268 mutex_unlock(&sessions_mutex
);
272 int lttng_channel_enable(struct lttng_channel
*channel
)
276 mutex_lock(&sessions_mutex
);
277 if (channel
->channel_type
== METADATA_CHANNEL
) {
281 if (channel
->enabled
) {
285 /* Set transient enabler state to "enabled" */
287 lttng_session_sync_enablers(channel
->session
);
288 /* Set atomically the state to "enabled" */
289 ACCESS_ONCE(channel
->enabled
) = 1;
291 mutex_unlock(&sessions_mutex
);
295 int lttng_channel_disable(struct lttng_channel
*channel
)
299 mutex_lock(&sessions_mutex
);
300 if (channel
->channel_type
== METADATA_CHANNEL
) {
304 if (!channel
->enabled
) {
308 /* Set atomically the state to "disabled" */
309 ACCESS_ONCE(channel
->enabled
) = 0;
310 /* Set transient enabler state to "enabled" */
312 lttng_session_sync_enablers(channel
->session
);
314 mutex_unlock(&sessions_mutex
);
318 int lttng_event_enable(struct lttng_event
*event
)
322 mutex_lock(&sessions_mutex
);
323 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
327 if (event
->enabled
) {
331 ACCESS_ONCE(event
->enabled
) = 1;
332 lttng_session_sync_enablers(event
->chan
->session
);
334 mutex_unlock(&sessions_mutex
);
338 int lttng_event_disable(struct lttng_event
*event
)
342 mutex_lock(&sessions_mutex
);
343 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
347 if (!event
->enabled
) {
351 ACCESS_ONCE(event
->enabled
) = 0;
352 lttng_session_sync_enablers(event
->chan
->session
);
354 mutex_unlock(&sessions_mutex
);
358 static struct lttng_transport
*lttng_transport_find(const char *name
)
360 struct lttng_transport
*transport
;
362 list_for_each_entry(transport
, <tng_transport_list
, node
) {
363 if (!strcmp(transport
->name
, name
))
369 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
370 const char *transport_name
,
372 size_t subbuf_size
, size_t num_subbuf
,
373 unsigned int switch_timer_interval
,
374 unsigned int read_timer_interval
,
375 enum channel_type channel_type
)
377 struct lttng_channel
*chan
;
378 struct lttng_transport
*transport
= NULL
;
380 mutex_lock(&sessions_mutex
);
381 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
382 goto active
; /* Refuse to add channel to active session */
383 transport
= lttng_transport_find(transport_name
);
385 printk(KERN_WARNING
"LTTng transport %s not found\n",
389 if (!try_module_get(transport
->owner
)) {
390 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
393 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
396 chan
->session
= session
;
397 chan
->id
= session
->free_chan_id
++;
398 chan
->ops
= &transport
->ops
;
400 * Note: the channel creation op already writes into the packet
401 * headers. Therefore the "chan" information used as input
402 * should be already accessible.
404 chan
->chan
= transport
->ops
.channel_create(transport_name
,
405 chan
, buf_addr
, subbuf_size
, num_subbuf
,
406 switch_timer_interval
, read_timer_interval
);
411 chan
->transport
= transport
;
412 chan
->channel_type
= channel_type
;
413 list_add(&chan
->list
, &session
->chan
);
414 mutex_unlock(&sessions_mutex
);
421 module_put(transport
->owner
);
424 mutex_unlock(&sessions_mutex
);
429 * Only used internally at session destruction for per-cpu channels, and
430 * when metadata channel is released.
431 * Needs to be called with sessions mutex held.
434 void _lttng_channel_destroy(struct lttng_channel
*chan
)
436 chan
->ops
->channel_destroy(chan
->chan
);
437 module_put(chan
->transport
->owner
);
438 list_del(&chan
->list
);
439 lttng_destroy_context(chan
->ctx
);
443 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
445 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
447 /* Protect the metadata cache with the sessions_mutex. */
448 mutex_lock(&sessions_mutex
);
449 _lttng_channel_destroy(chan
);
450 mutex_unlock(&sessions_mutex
);
452 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
455 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
457 stream
->finalized
= 1;
458 wake_up_interruptible(&stream
->read_wait
);
462 * Supports event creation while tracing session is active.
463 * Needs to be called with sessions mutex held.
465 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
466 struct lttng_kernel_event
*event_param
,
468 const struct lttng_event_desc
*event_desc
,
469 enum lttng_kernel_instrumentation itype
)
471 struct lttng_session
*session
= chan
->session
;
472 struct lttng_event
*event
;
473 const char *event_name
;
474 struct hlist_head
*head
;
479 if (chan
->free_event_id
== -1U) {
485 case LTTNG_KERNEL_TRACEPOINT
:
486 event_name
= event_desc
->name
;
488 case LTTNG_KERNEL_KPROBE
:
489 case LTTNG_KERNEL_KRETPROBE
:
490 case LTTNG_KERNEL_FUNCTION
:
491 case LTTNG_KERNEL_NOOP
:
492 case LTTNG_KERNEL_SYSCALL
:
493 event_name
= event_param
->name
;
500 name_len
= strlen(event_name
);
501 hash
= jhash(event_name
, name_len
, 0);
502 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
503 lttng_hlist_for_each_entry(event
, head
, hlist
) {
504 WARN_ON_ONCE(!event
->desc
);
505 if (!strncmp(event
->desc
->name
, event_name
,
506 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
507 && chan
== event
->chan
) {
513 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
519 event
->filter
= filter
;
520 event
->id
= chan
->free_event_id
++;
521 event
->instrumentation
= itype
;
522 event
->evtype
= LTTNG_TYPE_EVENT
;
523 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
524 INIT_LIST_HEAD(&event
->enablers_ref_head
);
527 case LTTNG_KERNEL_TRACEPOINT
:
528 /* Event will be enabled by enabler sync. */
530 event
->registered
= 0;
531 event
->desc
= lttng_event_get(event_name
);
536 /* Populate lttng_event structure before event registration. */
539 case LTTNG_KERNEL_KPROBE
:
541 event
->registered
= 1;
543 * Populate lttng_event structure before event
547 ret
= lttng_kprobes_register(event_name
,
548 event_param
->u
.kprobe
.symbol_name
,
549 event_param
->u
.kprobe
.offset
,
550 event_param
->u
.kprobe
.addr
,
556 ret
= try_module_get(event
->desc
->owner
);
559 case LTTNG_KERNEL_KRETPROBE
:
561 struct lttng_event
*event_return
;
563 /* kretprobe defines 2 events */
565 event
->registered
= 1;
567 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
572 event_return
->chan
= chan
;
573 event_return
->filter
= filter
;
574 event_return
->id
= chan
->free_event_id
++;
575 event_return
->enabled
= 1;
576 event_return
->registered
= 1;
577 event_return
->instrumentation
= itype
;
579 * Populate lttng_event structure before kretprobe registration.
582 ret
= lttng_kretprobes_register(event_name
,
583 event_param
->u
.kretprobe
.symbol_name
,
584 event_param
->u
.kretprobe
.offset
,
585 event_param
->u
.kretprobe
.addr
,
586 event
, event_return
);
588 kmem_cache_free(event_cache
, event_return
);
592 /* Take 2 refs on the module: one per event. */
593 ret
= try_module_get(event
->desc
->owner
);
595 ret
= try_module_get(event
->desc
->owner
);
597 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
599 WARN_ON_ONCE(ret
> 0);
601 kmem_cache_free(event_cache
, event_return
);
602 module_put(event
->desc
->owner
);
603 module_put(event
->desc
->owner
);
604 goto statedump_error
;
606 list_add(&event_return
->list
, &chan
->session
->events
);
609 case LTTNG_KERNEL_FUNCTION
:
611 event
->registered
= 1;
613 * Populate lttng_event structure before event
617 ret
= lttng_ftrace_register(event_name
,
618 event_param
->u
.ftrace
.symbol_name
,
623 ret
= try_module_get(event
->desc
->owner
);
626 case LTTNG_KERNEL_NOOP
:
627 case LTTNG_KERNEL_SYSCALL
:
629 event
->registered
= 0;
630 event
->desc
= event_desc
;
641 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
642 WARN_ON_ONCE(ret
> 0);
644 goto statedump_error
;
646 hlist_add_head(&event
->hlist
, head
);
647 list_add(&event
->list
, &chan
->session
->events
);
651 /* If a statedump error occurs, events will not be readable. */
653 kmem_cache_free(event_cache
, event
);
661 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
662 struct lttng_kernel_event
*event_param
,
664 const struct lttng_event_desc
*event_desc
,
665 enum lttng_kernel_instrumentation itype
)
667 struct lttng_event
*event
;
669 mutex_lock(&sessions_mutex
);
670 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
672 mutex_unlock(&sessions_mutex
);
676 /* Only used for tracepoints for now. */
678 void register_event(struct lttng_event
*event
)
680 const struct lttng_event_desc
*desc
;
683 if (event
->registered
)
687 switch (event
->instrumentation
) {
688 case LTTNG_KERNEL_TRACEPOINT
:
689 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
690 desc
->probe_callback
,
693 case LTTNG_KERNEL_SYSCALL
:
694 ret
= lttng_syscall_filter_enable(event
->chan
,
697 case LTTNG_KERNEL_KPROBE
:
698 case LTTNG_KERNEL_KRETPROBE
:
699 case LTTNG_KERNEL_FUNCTION
:
700 case LTTNG_KERNEL_NOOP
:
707 event
->registered
= 1;
711 * Only used internally at session destruction.
713 int _lttng_event_unregister(struct lttng_event
*event
)
715 const struct lttng_event_desc
*desc
;
718 if (!event
->registered
)
722 switch (event
->instrumentation
) {
723 case LTTNG_KERNEL_TRACEPOINT
:
724 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
725 event
->desc
->probe_callback
,
728 case LTTNG_KERNEL_KPROBE
:
729 lttng_kprobes_unregister(event
);
732 case LTTNG_KERNEL_KRETPROBE
:
733 lttng_kretprobes_unregister(event
);
736 case LTTNG_KERNEL_FUNCTION
:
737 lttng_ftrace_unregister(event
);
740 case LTTNG_KERNEL_SYSCALL
:
741 ret
= lttng_syscall_filter_disable(event
->chan
,
744 case LTTNG_KERNEL_NOOP
:
751 event
->registered
= 0;
756 * Only used internally at session destruction.
759 void _lttng_event_destroy(struct lttng_event
*event
)
761 switch (event
->instrumentation
) {
762 case LTTNG_KERNEL_TRACEPOINT
:
763 lttng_event_put(event
->desc
);
765 case LTTNG_KERNEL_KPROBE
:
766 module_put(event
->desc
->owner
);
767 lttng_kprobes_destroy_private(event
);
769 case LTTNG_KERNEL_KRETPROBE
:
770 module_put(event
->desc
->owner
);
771 lttng_kretprobes_destroy_private(event
);
773 case LTTNG_KERNEL_FUNCTION
:
774 module_put(event
->desc
->owner
);
775 lttng_ftrace_destroy_private(event
);
777 case LTTNG_KERNEL_NOOP
:
778 case LTTNG_KERNEL_SYSCALL
:
783 list_del(&event
->list
);
784 lttng_destroy_context(event
->ctx
);
785 kmem_cache_free(event_cache
, event
);
788 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
794 mutex_lock(&sessions_mutex
);
796 /* track all pids: destroy tracker. */
797 if (session
->pid_tracker
) {
798 struct lttng_pid_tracker
*lpf
;
800 lpf
= session
->pid_tracker
;
801 rcu_assign_pointer(session
->pid_tracker
, NULL
);
803 lttng_pid_tracker_destroy(lpf
);
807 if (!session
->pid_tracker
) {
808 struct lttng_pid_tracker
*lpf
;
810 lpf
= lttng_pid_tracker_create();
815 ret
= lttng_pid_tracker_add(lpf
, pid
);
816 rcu_assign_pointer(session
->pid_tracker
, lpf
);
818 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
822 mutex_unlock(&sessions_mutex
);
826 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
832 mutex_lock(&sessions_mutex
);
834 /* untrack all pids: replace by empty tracker. */
835 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
836 struct lttng_pid_tracker
*lpf
;
838 lpf
= lttng_pid_tracker_create();
843 rcu_assign_pointer(session
->pid_tracker
, lpf
);
846 lttng_pid_tracker_destroy(old_lpf
);
849 if (!session
->pid_tracker
) {
853 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
856 mutex_unlock(&sessions_mutex
);
861 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
863 struct lttng_session
*session
= m
->private;
864 struct lttng_pid_tracker
*lpf
;
865 struct lttng_pid_hash_node
*e
;
868 mutex_lock(&sessions_mutex
);
869 lpf
= session
->pid_tracker
;
871 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
872 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
874 lttng_hlist_for_each_entry(e
, head
, hlist
) {
880 /* PID tracker disabled. */
881 if (iter
>= *pos
&& iter
== 0) {
882 return session
; /* empty tracker */
890 /* Called with sessions_mutex held. */
892 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
894 struct lttng_session
*session
= m
->private;
895 struct lttng_pid_tracker
*lpf
;
896 struct lttng_pid_hash_node
*e
;
900 lpf
= session
->pid_tracker
;
902 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
903 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
905 lttng_hlist_for_each_entry(e
, head
, hlist
) {
911 /* PID tracker disabled. */
912 if (iter
>= *ppos
&& iter
== 0)
913 return session
; /* empty tracker */
922 void pid_list_stop(struct seq_file
*m
, void *p
)
924 mutex_unlock(&sessions_mutex
);
928 int pid_list_show(struct seq_file
*m
, void *p
)
932 if (p
== m
->private) {
933 /* Tracker disabled. */
936 const struct lttng_pid_hash_node
*e
= p
;
938 pid
= lttng_pid_tracker_get_node_pid(e
);
940 seq_printf(m
, "process { pid = %d; };\n", pid
);
945 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
946 .start
= pid_list_start
,
947 .next
= pid_list_next
,
948 .stop
= pid_list_stop
,
949 .show
= pid_list_show
,
953 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
955 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
959 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
961 struct seq_file
*m
= file
->private_data
;
962 struct lttng_session
*session
= m
->private;
965 WARN_ON_ONCE(!session
);
966 ret
= seq_release(inode
, file
);
972 const struct file_operations lttng_tracker_pids_list_fops
= {
973 .owner
= THIS_MODULE
,
974 .open
= lttng_tracker_pids_list_open
,
977 .release
= lttng_tracker_pids_list_release
,
980 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
982 struct file
*tracker_pids_list_file
;
986 file_fd
= lttng_get_unused_fd();
992 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
993 <tng_tracker_pids_list_fops
,
995 if (IS_ERR(tracker_pids_list_file
)) {
996 ret
= PTR_ERR(tracker_pids_list_file
);
999 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1002 m
= tracker_pids_list_file
->private_data
;
1003 m
->private = session
;
1004 fd_install(file_fd
, tracker_pids_list_file
);
1005 atomic_long_inc(&session
->file
->f_count
);
1010 fput(tracker_pids_list_file
);
1012 put_unused_fd(file_fd
);
1018 * Enabler management.
1021 int lttng_match_enabler_wildcard(const char *desc_name
,
1024 /* Compare excluding final '*' */
1025 if (strncmp(desc_name
, name
, strlen(name
) - 1))
1031 int lttng_match_enabler_name(const char *desc_name
,
1034 if (strcmp(desc_name
, name
))
1040 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1041 struct lttng_enabler
*enabler
)
1043 const char *desc_name
, *enabler_name
;
1045 enabler_name
= enabler
->event_param
.name
;
1046 switch (enabler
->event_param
.instrumentation
) {
1047 case LTTNG_KERNEL_TRACEPOINT
:
1048 desc_name
= desc
->name
;
1050 case LTTNG_KERNEL_SYSCALL
:
1051 desc_name
= desc
->name
;
1052 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1053 desc_name
+= strlen("compat_");
1054 if (!strncmp(desc_name
, "syscall_exit_",
1055 strlen("syscall_exit_"))) {
1056 desc_name
+= strlen("syscall_exit_");
1057 } else if (!strncmp(desc_name
, "syscall_entry_",
1058 strlen("syscall_entry_"))) {
1059 desc_name
+= strlen("syscall_entry_");
1069 switch (enabler
->type
) {
1070 case LTTNG_ENABLER_WILDCARD
:
1071 return lttng_match_enabler_wildcard(desc_name
, enabler_name
);
1072 case LTTNG_ENABLER_NAME
:
1073 return lttng_match_enabler_name(desc_name
, enabler_name
);
1080 int lttng_event_match_enabler(struct lttng_event
*event
,
1081 struct lttng_enabler
*enabler
)
1083 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1085 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1086 && event
->chan
== enabler
->chan
)
1093 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1094 struct lttng_enabler
*enabler
)
1096 struct lttng_enabler_ref
*enabler_ref
;
1098 list_for_each_entry(enabler_ref
,
1099 &event
->enablers_ref_head
, node
) {
1100 if (enabler_ref
->ref
== enabler
)
1107 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1109 struct lttng_session
*session
= enabler
->chan
->session
;
1110 struct lttng_probe_desc
*probe_desc
;
1111 const struct lttng_event_desc
*desc
;
1113 struct list_head
*probe_list
;
1115 probe_list
= lttng_get_probe_list_head();
1117 * For each probe event, if we find that a probe event matches
1118 * our enabler, create an associated lttng_event if not
1121 list_for_each_entry(probe_desc
, probe_list
, head
) {
1122 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1124 struct hlist_head
*head
;
1125 const char *event_name
;
1128 struct lttng_event
*event
;
1130 desc
= probe_desc
->event_desc
[i
];
1131 if (!lttng_desc_match_enabler(desc
, enabler
))
1133 event_name
= desc
->name
;
1134 name_len
= strlen(event_name
);
1137 * Check if already created.
1139 hash
= jhash(event_name
, name_len
, 0);
1140 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1141 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1142 if (event
->desc
== desc
1143 && event
->chan
== enabler
->chan
)
1150 * We need to create an event for this
1153 event
= _lttng_event_create(enabler
->chan
,
1155 LTTNG_KERNEL_TRACEPOINT
);
1157 printk(KERN_INFO
"Unable to create event %s\n",
1158 probe_desc
->event_desc
[i
]->name
);
1165 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1169 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1174 * Create struct lttng_event if it is missing and present in the list of
1175 * tracepoint probes.
1176 * Should be called with sessions mutex held.
1179 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1181 switch (enabler
->event_param
.instrumentation
) {
1182 case LTTNG_KERNEL_TRACEPOINT
:
1183 lttng_create_tracepoint_if_missing(enabler
);
1185 case LTTNG_KERNEL_SYSCALL
:
1186 lttng_create_syscall_if_missing(enabler
);
1195 * Create events associated with an enabler (if not already present),
1196 * and add backward reference from the event to the enabler.
1197 * Should be called with sessions mutex held.
1200 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1202 struct lttng_session
*session
= enabler
->chan
->session
;
1203 struct lttng_event
*event
;
1205 /* First ensure that probe events are created for this enabler. */
1206 lttng_create_event_if_missing(enabler
);
1208 /* For each event matching enabler in session event list. */
1209 list_for_each_entry(event
, &session
->events
, list
) {
1210 struct lttng_enabler_ref
*enabler_ref
;
1212 if (!lttng_event_match_enabler(event
, enabler
))
1214 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1217 * If no backward ref, create it.
1218 * Add backward ref from event to enabler.
1220 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1223 enabler_ref
->ref
= enabler
;
1224 list_add(&enabler_ref
->node
,
1225 &event
->enablers_ref_head
);
1229 * Link filter bytecodes if not linked yet.
1231 lttng_enabler_event_link_bytecode(event
, enabler
);
1233 /* TODO: merge event context. */
1239 * Called at module load: connect the probe on all enablers matching
1241 * Called with sessions lock held.
1243 int lttng_fix_pending_events(void)
1245 struct lttng_session
*session
;
1247 list_for_each_entry(session
, &sessions
, list
)
1248 lttng_session_lazy_sync_enablers(session
);
1252 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1253 struct lttng_kernel_event
*event_param
,
1254 struct lttng_channel
*chan
)
1256 struct lttng_enabler
*enabler
;
1258 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1261 enabler
->type
= type
;
1262 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1263 memcpy(&enabler
->event_param
, event_param
,
1264 sizeof(enabler
->event_param
));
1265 enabler
->chan
= chan
;
1267 enabler
->enabled
= 0;
1268 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1269 mutex_lock(&sessions_mutex
);
1270 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1271 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1272 mutex_unlock(&sessions_mutex
);
1276 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1278 mutex_lock(&sessions_mutex
);
1279 enabler
->enabled
= 1;
1280 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1281 mutex_unlock(&sessions_mutex
);
1285 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1287 mutex_lock(&sessions_mutex
);
1288 enabler
->enabled
= 0;
1289 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1290 mutex_unlock(&sessions_mutex
);
1294 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1295 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1297 struct lttng_filter_bytecode_node
*bytecode_node
;
1298 uint32_t bytecode_len
;
1301 ret
= get_user(bytecode_len
, &bytecode
->len
);
1304 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1308 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1309 sizeof(*bytecode
) + bytecode_len
);
1312 bytecode_node
->enabler
= enabler
;
1313 /* Enforce length based on allocated size */
1314 bytecode_node
->bc
.len
= bytecode_len
;
1315 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1316 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1320 kfree(bytecode_node
);
1324 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1325 struct lttng_kernel_context
*context_param
)
1331 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1333 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1335 /* Destroy filter bytecode */
1336 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1337 &enabler
->filter_bytecode_head
, node
) {
1341 /* Destroy contexts */
1342 lttng_destroy_context(enabler
->ctx
);
1344 list_del(&enabler
->node
);
1349 * lttng_session_sync_enablers should be called just before starting a
1351 * Should be called with sessions mutex held.
1354 void lttng_session_sync_enablers(struct lttng_session
*session
)
1356 struct lttng_enabler
*enabler
;
1357 struct lttng_event
*event
;
1359 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1360 lttng_enabler_ref_events(enabler
);
1362 * For each event, if at least one of its enablers is enabled,
1363 * and its channel and session transient states are enabled, we
1364 * enable the event, else we disable it.
1366 list_for_each_entry(event
, &session
->events
, list
) {
1367 struct lttng_enabler_ref
*enabler_ref
;
1368 struct lttng_bytecode_runtime
*runtime
;
1369 int enabled
= 0, has_enablers_without_bytecode
= 0;
1371 switch (event
->instrumentation
) {
1372 case LTTNG_KERNEL_TRACEPOINT
:
1373 case LTTNG_KERNEL_SYSCALL
:
1375 list_for_each_entry(enabler_ref
,
1376 &event
->enablers_ref_head
, node
) {
1377 if (enabler_ref
->ref
->enabled
) {
1384 /* Not handled with lazy sync. */
1388 * Enabled state is based on union of enablers, with
1389 * intesection of session and channel transient enable
1392 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1394 ACCESS_ONCE(event
->enabled
) = enabled
;
1396 * Sync tracepoint registration with event enabled
1400 register_event(event
);
1402 _lttng_event_unregister(event
);
1405 /* Check if has enablers without bytecode enabled */
1406 list_for_each_entry(enabler_ref
,
1407 &event
->enablers_ref_head
, node
) {
1408 if (enabler_ref
->ref
->enabled
1409 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1410 has_enablers_without_bytecode
= 1;
1414 event
->has_enablers_without_bytecode
=
1415 has_enablers_without_bytecode
;
1417 /* Enable filters */
1418 list_for_each_entry(runtime
,
1419 &event
->bytecode_runtime_head
, node
)
1420 lttng_filter_sync_state(runtime
);
1425 * Apply enablers to session events, adding events to session if need
1426 * be. It is required after each modification applied to an active
1427 * session, and right before session "start".
1428 * "lazy" sync means we only sync if required.
1429 * Should be called with sessions mutex held.
1432 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1434 /* We can skip if session is not active */
1435 if (!session
->active
)
1437 lttng_session_sync_enablers(session
);
1441 * Serialize at most one packet worth of metadata into a metadata
1443 * We grab the metadata cache mutex to get exclusive access to our metadata
1444 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1445 * allows us to do racy operations such as looking for remaining space left in
1446 * packet and write, since mutual exclusion protects us from concurrent writes.
1447 * Mutual exclusion on the metadata cache allow us to read the cache content
1448 * without racing against reallocation of the cache by updates.
1449 * Returns the number of bytes written in the channel, 0 if no data
1450 * was written and a negative value on error.
1452 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1453 struct channel
*chan
)
1455 struct lib_ring_buffer_ctx ctx
;
1457 size_t len
, reserve_len
;
1460 * Ensure we support mutiple get_next / put sequences followed by
1461 * put_next. The metadata cache lock protects reading the metadata
1462 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1463 * "flush" operations on the buffer invoked by different processes.
1464 * Moreover, since the metadata cache memory can be reallocated, we
1465 * need to have exclusive access against updates even though we only
1468 mutex_lock(&stream
->metadata_cache
->lock
);
1469 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1470 if (stream
->metadata_in
!= stream
->metadata_out
)
1473 len
= stream
->metadata_cache
->metadata_written
-
1474 stream
->metadata_in
;
1477 reserve_len
= min_t(size_t,
1478 stream
->transport
->ops
.packet_avail_size(chan
),
1480 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1483 * If reservation failed, return an error to the caller.
1485 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1487 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1490 stream
->transport
->ops
.event_write(&ctx
,
1491 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1493 stream
->transport
->ops
.event_commit(&ctx
);
1494 stream
->metadata_in
+= reserve_len
;
1498 mutex_unlock(&stream
->metadata_cache
->lock
);
1503 * Write the metadata to the metadata cache.
1504 * Must be called with sessions_mutex held.
1505 * The metadata cache lock protects us from concurrent read access from
1506 * thread outputting metadata content to ring buffer.
1508 int lttng_metadata_printf(struct lttng_session
*session
,
1509 const char *fmt
, ...)
1514 struct lttng_metadata_stream
*stream
;
1516 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
1519 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1525 mutex_lock(&session
->metadata_cache
->lock
);
1526 if (session
->metadata_cache
->metadata_written
+ len
>
1527 session
->metadata_cache
->cache_alloc
) {
1528 char *tmp_cache_realloc
;
1529 unsigned int tmp_cache_alloc_size
;
1531 tmp_cache_alloc_size
= max_t(unsigned int,
1532 session
->metadata_cache
->cache_alloc
+ len
,
1533 session
->metadata_cache
->cache_alloc
<< 1);
1534 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1535 if (!tmp_cache_realloc
)
1537 if (session
->metadata_cache
->data
) {
1538 memcpy(tmp_cache_realloc
,
1539 session
->metadata_cache
->data
,
1540 session
->metadata_cache
->cache_alloc
);
1541 vfree(session
->metadata_cache
->data
);
1544 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1545 session
->metadata_cache
->data
= tmp_cache_realloc
;
1547 memcpy(session
->metadata_cache
->data
+
1548 session
->metadata_cache
->metadata_written
,
1550 session
->metadata_cache
->metadata_written
+= len
;
1551 mutex_unlock(&session
->metadata_cache
->lock
);
1554 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1555 wake_up_interruptible(&stream
->read_wait
);
1560 mutex_unlock(&session
->metadata_cache
->lock
);
1566 * Must be called with sessions_mutex held.
1569 int _lttng_field_statedump(struct lttng_session
*session
,
1570 const struct lttng_event_field
*field
)
1574 switch (field
->type
.atype
) {
1576 ret
= lttng_metadata_printf(session
,
1577 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1578 field
->type
.u
.basic
.integer
.size
,
1579 field
->type
.u
.basic
.integer
.alignment
,
1580 field
->type
.u
.basic
.integer
.signedness
,
1581 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
1583 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1586 field
->type
.u
.basic
.integer
.base
,
1588 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1590 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1595 ret
= lttng_metadata_printf(session
,
1597 field
->type
.u
.basic
.enumeration
.name
,
1602 const struct lttng_basic_type
*elem_type
;
1604 elem_type
= &field
->type
.u
.array
.elem_type
;
1605 ret
= lttng_metadata_printf(session
,
1606 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1607 elem_type
->u
.basic
.integer
.size
,
1608 elem_type
->u
.basic
.integer
.alignment
,
1609 elem_type
->u
.basic
.integer
.signedness
,
1610 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1612 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1615 elem_type
->u
.basic
.integer
.base
,
1617 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1619 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1621 field
->name
, field
->type
.u
.array
.length
);
1624 case atype_sequence
:
1626 const struct lttng_basic_type
*elem_type
;
1627 const struct lttng_basic_type
*length_type
;
1629 elem_type
= &field
->type
.u
.sequence
.elem_type
;
1630 length_type
= &field
->type
.u
.sequence
.length_type
;
1631 ret
= lttng_metadata_printf(session
,
1632 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1633 length_type
->u
.basic
.integer
.size
,
1634 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
1635 length_type
->u
.basic
.integer
.signedness
,
1636 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1638 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1641 length_type
->u
.basic
.integer
.base
,
1643 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1645 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1651 ret
= lttng_metadata_printf(session
,
1652 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1653 elem_type
->u
.basic
.integer
.size
,
1654 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
1655 elem_type
->u
.basic
.integer
.signedness
,
1656 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
1658 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
1661 elem_type
->u
.basic
.integer
.base
,
1663 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
1665 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
1673 /* Default encoding is UTF8 */
1674 ret
= lttng_metadata_printf(session
,
1676 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
1677 " { encoding = ASCII; }" : "",
1688 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
1689 struct lttng_ctx
*ctx
)
1696 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
1697 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
1699 ret
= _lttng_field_statedump(session
, &field
->event_field
);
1707 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
1708 struct lttng_event
*event
)
1710 const struct lttng_event_desc
*desc
= event
->desc
;
1714 for (i
= 0; i
< desc
->nr_fields
; i
++) {
1715 const struct lttng_event_field
*field
= &desc
->fields
[i
];
1717 ret
= _lttng_field_statedump(session
, field
);
1725 * Must be called with sessions_mutex held.
1728 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
1729 struct lttng_channel
*chan
,
1730 struct lttng_event
*event
)
1734 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1736 if (chan
->channel_type
== METADATA_CHANNEL
)
1739 ret
= lttng_metadata_printf(session
,
1743 " stream_id = %u;\n",
1751 ret
= lttng_metadata_printf(session
,
1752 " context := struct {\n");
1756 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
1760 ret
= lttng_metadata_printf(session
,
1766 ret
= lttng_metadata_printf(session
,
1767 " fields := struct {\n"
1772 ret
= _lttng_fields_metadata_statedump(session
, event
);
1777 * LTTng space reservation can only reserve multiples of the
1780 ret
= lttng_metadata_printf(session
,
1786 event
->metadata_dumped
= 1;
1793 * Must be called with sessions_mutex held.
1796 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
1797 struct lttng_channel
*chan
)
1801 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
1804 if (chan
->channel_type
== METADATA_CHANNEL
)
1807 WARN_ON_ONCE(!chan
->header_type
);
1808 ret
= lttng_metadata_printf(session
,
1811 " event.header := %s;\n"
1812 " packet.context := struct packet_context;\n",
1814 chan
->header_type
== 1 ? "struct event_header_compact" :
1815 "struct event_header_large");
1820 ret
= lttng_metadata_printf(session
,
1821 " event.context := struct {\n");
1825 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
1829 ret
= lttng_metadata_printf(session
,
1835 ret
= lttng_metadata_printf(session
,
1838 chan
->metadata_dumped
= 1;
1844 * Must be called with sessions_mutex held.
1847 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
1849 return lttng_metadata_printf(session
,
1850 "struct packet_context {\n"
1851 " uint64_clock_monotonic_t timestamp_begin;\n"
1852 " uint64_clock_monotonic_t timestamp_end;\n"
1853 " uint64_t content_size;\n"
1854 " uint64_t packet_size;\n"
1855 " uint64_t packet_seq_num;\n"
1856 " unsigned long events_discarded;\n"
1857 " uint32_t cpu_id;\n"
1864 * id: range: 0 - 30.
1865 * id 31 is reserved to indicate an extended header.
1868 * id: range: 0 - 65534.
1869 * id 65535 is reserved to indicate an extended header.
1871 * Must be called with sessions_mutex held.
1874 int _lttng_event_header_declare(struct lttng_session
*session
)
1876 return lttng_metadata_printf(session
,
1877 "struct event_header_compact {\n"
1878 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1881 " uint27_clock_monotonic_t timestamp;\n"
1885 " uint64_clock_monotonic_t timestamp;\n"
1890 "struct event_header_large {\n"
1891 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1894 " uint32_clock_monotonic_t timestamp;\n"
1898 " uint64_clock_monotonic_t timestamp;\n"
1902 lttng_alignof(uint32_t) * CHAR_BIT
,
1903 lttng_alignof(uint16_t) * CHAR_BIT
1908 * Approximation of NTP time of day to clock monotonic correlation,
1909 * taken at start of trace.
1910 * Yes, this is only an approximation. Yes, we can (and will) do better
1911 * in future versions.
1914 uint64_t measure_clock_offset(void)
1916 uint64_t offset
, monotonic
[2], realtime
;
1917 struct timespec rts
= { 0, 0 };
1918 unsigned long flags
;
1920 /* Disable interrupts to increase correlation precision. */
1921 local_irq_save(flags
);
1922 monotonic
[0] = trace_clock_read64();
1923 getnstimeofday(&rts
);
1924 monotonic
[1] = trace_clock_read64();
1925 local_irq_restore(flags
);
1927 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1928 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1929 realtime
+= rts
.tv_nsec
;
1930 offset
= realtime
- offset
;
1935 * Output metadata into this session's metadata buffers.
1936 * Must be called with sessions_mutex held.
1939 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1941 unsigned char *uuid_c
= session
->uuid
.b
;
1942 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1943 struct lttng_channel
*chan
;
1944 struct lttng_event
*event
;
1947 if (!ACCESS_ONCE(session
->active
))
1949 if (session
->metadata_dumped
)
1952 snprintf(uuid_s
, sizeof(uuid_s
),
1953 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1954 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1955 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1956 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1957 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1959 ret
= lttng_metadata_printf(session
,
1960 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1961 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1962 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1963 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1964 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1965 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1966 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1972 " byte_order = %s;\n"
1973 " packet.header := struct {\n"
1974 " uint32_t magic;\n"
1975 " uint8_t uuid[16];\n"
1976 " uint32_t stream_id;\n"
1977 " uint64_t stream_instance_id;\n"
1980 lttng_alignof(uint8_t) * CHAR_BIT
,
1981 lttng_alignof(uint16_t) * CHAR_BIT
,
1982 lttng_alignof(uint32_t) * CHAR_BIT
,
1983 lttng_alignof(uint64_t) * CHAR_BIT
,
1984 sizeof(unsigned long) * CHAR_BIT
,
1985 lttng_alignof(unsigned long) * CHAR_BIT
,
1998 ret
= lttng_metadata_printf(session
,
2000 " hostname = \"%s\";\n"
2001 " domain = \"kernel\";\n"
2002 " sysname = \"%s\";\n"
2003 " kernel_release = \"%s\";\n"
2004 " kernel_version = \"%s\";\n"
2005 " tracer_name = \"lttng-modules\";\n"
2006 " tracer_major = %d;\n"
2007 " tracer_minor = %d;\n"
2008 " tracer_patchlevel = %d;\n"
2010 current
->nsproxy
->uts_ns
->name
.nodename
,
2014 LTTNG_MODULES_MAJOR_VERSION
,
2015 LTTNG_MODULES_MINOR_VERSION
,
2016 LTTNG_MODULES_PATCHLEVEL_VERSION
2021 ret
= lttng_metadata_printf(session
,
2029 if (!trace_clock_uuid(clock_uuid_s
)) {
2030 ret
= lttng_metadata_printf(session
,
2031 " uuid = \"%s\";\n",
2038 ret
= lttng_metadata_printf(session
,
2039 " description = \"Monotonic Clock\";\n"
2040 " freq = %llu; /* Frequency, in Hz */\n"
2041 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2044 (unsigned long long) trace_clock_freq(),
2045 (unsigned long long) measure_clock_offset()
2050 ret
= lttng_metadata_printf(session
,
2051 "typealias integer {\n"
2052 " size = 27; align = 1; signed = false;\n"
2053 " map = clock.monotonic.value;\n"
2054 "} := uint27_clock_monotonic_t;\n"
2056 "typealias integer {\n"
2057 " size = 32; align = %u; signed = false;\n"
2058 " map = clock.monotonic.value;\n"
2059 "} := uint32_clock_monotonic_t;\n"
2061 "typealias integer {\n"
2062 " size = 64; align = %u; signed = false;\n"
2063 " map = clock.monotonic.value;\n"
2064 "} := uint64_clock_monotonic_t;\n\n",
2065 lttng_alignof(uint32_t) * CHAR_BIT
,
2066 lttng_alignof(uint64_t) * CHAR_BIT
2071 ret
= _lttng_stream_packet_context_declare(session
);
2075 ret
= _lttng_event_header_declare(session
);
2080 list_for_each_entry(chan
, &session
->chan
, list
) {
2081 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2086 list_for_each_entry(event
, &session
->events
, list
) {
2087 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2091 session
->metadata_dumped
= 1;
2097 * lttng_transport_register - LTT transport registration
2098 * @transport: transport structure
2100 * Registers a transport which can be used as output to extract the data out of
2101 * LTTng. The module calling this registration function must ensure that no
2102 * trap-inducing code will be executed by the transport functions. E.g.
2103 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2104 * is made visible to the transport function. This registration acts as a
2105 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2106 * after its registration must it synchronize the TLBs.
2108 void lttng_transport_register(struct lttng_transport
*transport
)
2111 * Make sure no page fault can be triggered by the module about to be
2112 * registered. We deal with this here so we don't have to call
2113 * vmalloc_sync_all() in each module's init.
2115 wrapper_vmalloc_sync_all();
2117 mutex_lock(&sessions_mutex
);
2118 list_add_tail(&transport
->node
, <tng_transport_list
);
2119 mutex_unlock(&sessions_mutex
);
2121 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2124 * lttng_transport_unregister - LTT transport unregistration
2125 * @transport: transport structure
2127 void lttng_transport_unregister(struct lttng_transport
*transport
)
2129 mutex_lock(&sessions_mutex
);
2130 list_del(&transport
->node
);
2131 mutex_unlock(&sessions_mutex
);
2133 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2135 static int __init
lttng_events_init(void)
2139 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2142 ret
= wrapper_get_pfnblock_flags_mask_init();
2145 ret
= lttng_context_init();
2148 ret
= lttng_tracepoint_init();
2151 event_cache
= KMEM_CACHE(lttng_event
, 0);
2156 ret
= lttng_abi_init();
2159 ret
= lttng_logger_init();
2167 kmem_cache_destroy(event_cache
);
2169 lttng_tracepoint_exit();
2171 lttng_context_exit();
2175 module_init(lttng_events_init
);
2177 static void __exit
lttng_events_exit(void)
2179 struct lttng_session
*session
, *tmpsession
;
2181 lttng_logger_exit();
2183 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
2184 lttng_session_destroy(session
);
2185 kmem_cache_destroy(event_cache
);
2186 lttng_tracepoint_exit();
2187 lttng_context_exit();
2190 module_exit(lttng_events_exit
);
2192 MODULE_LICENSE("GPL and additional rights");
2193 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2194 MODULE_DESCRIPTION("LTTng Events");
2195 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
2196 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
2197 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
2198 LTTNG_MODULES_EXTRAVERSION
);