1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
5 * Holds LTTng per-session event registry.
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
12 * overrides a function with a define.
14 #include "wrapper/page_alloc.h"
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <linux/utsname.h>
22 #include <linux/err.h>
23 #include <linux/seq_file.h>
24 #include <linux/file.h>
25 #include <linux/anon_inodes.h>
26 #include <wrapper/file.h>
27 #include <linux/jhash.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/dmi.h>
32 #include <wrapper/uuid.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <wrapper/random.h>
35 #include <wrapper/tracepoint.h>
36 #include <wrapper/list.h>
37 #include <wrapper/types.h>
38 #include <lttng/kernel-version.h>
39 #include <lttng/events.h>
40 #include <lttng/tracer.h>
41 #include <lttng/abi-old.h>
42 #include <lttng/endian.h>
43 #include <lttng/string-utils.h>
44 #include <ringbuffer/backend.h>
45 #include <ringbuffer/frontend.h>
46 #include <wrapper/time.h>
48 #define METADATA_CACHE_DEFAULT_SIZE 4096
50 static LIST_HEAD(sessions
);
51 static LIST_HEAD(lttng_transport_list
);
53 * Protect the sessions and metadata caches.
55 static DEFINE_MUTEX(sessions_mutex
);
56 static struct kmem_cache
*event_cache
;
58 static void lttng_session_lazy_sync_event_enablers(struct lttng_session
*session
);
59 static void lttng_session_sync_event_enablers(struct lttng_session
*session
);
60 static void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
);
62 static void _lttng_event_destroy(struct lttng_event
*event
);
63 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
64 static int _lttng_event_unregister(struct lttng_event
*event
);
66 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
67 struct lttng_channel
*chan
,
68 struct lttng_event
*event
);
70 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
72 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
74 int _lttng_type_statedump(struct lttng_session
*session
,
75 const struct lttng_type
*type
,
78 int _lttng_field_statedump(struct lttng_session
*session
,
79 const struct lttng_event_field
*field
,
82 void synchronize_trace(void)
84 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
90 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
91 #ifdef CONFIG_PREEMPT_RT_FULL
94 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
95 #ifdef CONFIG_PREEMPT_RT
98 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
101 void lttng_lock_sessions(void)
103 mutex_lock(&sessions_mutex
);
106 void lttng_unlock_sessions(void)
108 mutex_unlock(&sessions_mutex
);
112 * Called with sessions lock held.
114 int lttng_session_active(void)
116 struct lttng_session
*iter
;
118 list_for_each_entry(iter
, &sessions
, list
) {
125 struct lttng_session
*lttng_session_create(void)
127 struct lttng_session
*session
;
128 struct lttng_metadata_cache
*metadata_cache
;
131 mutex_lock(&sessions_mutex
);
132 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
135 INIT_LIST_HEAD(&session
->chan
);
136 INIT_LIST_HEAD(&session
->events
);
137 lttng_guid_gen(&session
->uuid
);
139 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
142 goto err_free_session
;
143 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
144 if (!metadata_cache
->data
)
146 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
147 kref_init(&metadata_cache
->refcount
);
148 mutex_init(&metadata_cache
->lock
);
149 session
->metadata_cache
= metadata_cache
;
150 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
151 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
152 sizeof(metadata_cache
->uuid
));
153 INIT_LIST_HEAD(&session
->enablers_head
);
154 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
155 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
156 list_add(&session
->list
, &sessions
);
157 session
->pid_tracker
.session
= session
;
158 session
->pid_tracker
.tracker_type
= TRACKER_PID
;
159 session
->vpid_tracker
.session
= session
;
160 session
->vpid_tracker
.tracker_type
= TRACKER_VPID
;
161 session
->uid_tracker
.session
= session
;
162 session
->uid_tracker
.tracker_type
= TRACKER_UID
;
163 session
->vuid_tracker
.session
= session
;
164 session
->vuid_tracker
.tracker_type
= TRACKER_VUID
;
165 session
->gid_tracker
.session
= session
;
166 session
->gid_tracker
.tracker_type
= TRACKER_GID
;
167 session
->vgid_tracker
.session
= session
;
168 session
->vgid_tracker
.tracker_type
= TRACKER_VGID
;
169 mutex_unlock(&sessions_mutex
);
173 kfree(metadata_cache
);
175 lttng_kvfree(session
);
177 mutex_unlock(&sessions_mutex
);
181 void metadata_cache_destroy(struct kref
*kref
)
183 struct lttng_metadata_cache
*cache
=
184 container_of(kref
, struct lttng_metadata_cache
, refcount
);
189 void lttng_session_destroy(struct lttng_session
*session
)
191 struct lttng_channel
*chan
, *tmpchan
;
192 struct lttng_event
*event
, *tmpevent
;
193 struct lttng_metadata_stream
*metadata_stream
;
194 struct lttng_event_enabler
*event_enabler
, *tmp_event_enabler
;
197 mutex_lock(&sessions_mutex
);
198 WRITE_ONCE(session
->active
, 0);
199 list_for_each_entry(chan
, &session
->chan
, list
) {
200 ret
= lttng_syscalls_unregister(chan
);
203 list_for_each_entry(event
, &session
->events
, list
) {
204 ret
= _lttng_event_unregister(event
);
207 synchronize_trace(); /* Wait for in-flight events to complete */
208 list_for_each_entry(chan
, &session
->chan
, list
) {
209 ret
= lttng_syscalls_destroy(chan
);
212 list_for_each_entry_safe(event_enabler
, tmp_event_enabler
,
213 &session
->enablers_head
, node
)
214 lttng_event_enabler_destroy(event_enabler
);
215 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
216 _lttng_event_destroy(event
);
217 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
218 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
219 _lttng_channel_destroy(chan
);
221 mutex_lock(&session
->metadata_cache
->lock
);
222 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
223 _lttng_metadata_channel_hangup(metadata_stream
);
224 mutex_unlock(&session
->metadata_cache
->lock
);
225 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
226 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
227 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
228 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
229 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
230 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
231 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
232 list_del(&session
->list
);
233 mutex_unlock(&sessions_mutex
);
234 lttng_kvfree(session
);
237 int lttng_session_statedump(struct lttng_session
*session
)
241 mutex_lock(&sessions_mutex
);
242 ret
= lttng_statedump_start(session
);
243 mutex_unlock(&sessions_mutex
);
247 int lttng_session_enable(struct lttng_session
*session
)
250 struct lttng_channel
*chan
;
252 mutex_lock(&sessions_mutex
);
253 if (session
->active
) {
258 /* Set transient enabler state to "enabled" */
261 /* We need to sync enablers with session before activation. */
262 lttng_session_sync_event_enablers(session
);
265 * Snapshot the number of events per channel to know the type of header
268 list_for_each_entry(chan
, &session
->chan
, list
) {
269 if (chan
->header_type
)
270 continue; /* don't change it if session stop/restart */
271 if (chan
->free_event_id
< 31)
272 chan
->header_type
= 1; /* compact */
274 chan
->header_type
= 2; /* large */
277 /* Clear each stream's quiescent state. */
278 list_for_each_entry(chan
, &session
->chan
, list
) {
279 if (chan
->channel_type
!= METADATA_CHANNEL
)
280 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
283 WRITE_ONCE(session
->active
, 1);
284 WRITE_ONCE(session
->been_active
, 1);
285 ret
= _lttng_session_metadata_statedump(session
);
287 WRITE_ONCE(session
->active
, 0);
290 ret
= lttng_statedump_start(session
);
292 WRITE_ONCE(session
->active
, 0);
294 mutex_unlock(&sessions_mutex
);
298 int lttng_session_disable(struct lttng_session
*session
)
301 struct lttng_channel
*chan
;
303 mutex_lock(&sessions_mutex
);
304 if (!session
->active
) {
308 WRITE_ONCE(session
->active
, 0);
310 /* Set transient enabler state to "disabled" */
312 lttng_session_sync_event_enablers(session
);
314 /* Set each stream's quiescent state. */
315 list_for_each_entry(chan
, &session
->chan
, list
) {
316 if (chan
->channel_type
!= METADATA_CHANNEL
)
317 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
320 mutex_unlock(&sessions_mutex
);
324 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
327 struct lttng_channel
*chan
;
328 struct lttng_event
*event
;
329 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
330 struct lttng_metadata_stream
*stream
;
332 mutex_lock(&sessions_mutex
);
333 if (!session
->active
) {
338 mutex_lock(&cache
->lock
);
339 memset(cache
->data
, 0, cache
->cache_alloc
);
340 cache
->metadata_written
= 0;
342 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
343 stream
->metadata_out
= 0;
344 stream
->metadata_in
= 0;
346 mutex_unlock(&cache
->lock
);
348 session
->metadata_dumped
= 0;
349 list_for_each_entry(chan
, &session
->chan
, list
) {
350 chan
->metadata_dumped
= 0;
353 list_for_each_entry(event
, &session
->events
, list
) {
354 event
->metadata_dumped
= 0;
357 ret
= _lttng_session_metadata_statedump(session
);
360 mutex_unlock(&sessions_mutex
);
364 int lttng_channel_enable(struct lttng_channel
*channel
)
368 mutex_lock(&sessions_mutex
);
369 if (channel
->channel_type
== METADATA_CHANNEL
) {
373 if (channel
->enabled
) {
377 /* Set transient enabler state to "enabled" */
379 lttng_session_sync_event_enablers(channel
->session
);
380 /* Set atomically the state to "enabled" */
381 WRITE_ONCE(channel
->enabled
, 1);
383 mutex_unlock(&sessions_mutex
);
387 int lttng_channel_disable(struct lttng_channel
*channel
)
391 mutex_lock(&sessions_mutex
);
392 if (channel
->channel_type
== METADATA_CHANNEL
) {
396 if (!channel
->enabled
) {
400 /* Set atomically the state to "disabled" */
401 WRITE_ONCE(channel
->enabled
, 0);
402 /* Set transient enabler state to "enabled" */
404 lttng_session_sync_event_enablers(channel
->session
);
406 mutex_unlock(&sessions_mutex
);
410 int lttng_event_enable(struct lttng_event
*event
)
414 mutex_lock(&sessions_mutex
);
415 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
419 if (event
->enabled
) {
423 switch (event
->instrumentation
) {
424 case LTTNG_KERNEL_TRACEPOINT
:
425 case LTTNG_KERNEL_SYSCALL
:
428 case LTTNG_KERNEL_KPROBE
:
429 case LTTNG_KERNEL_UPROBE
:
430 case LTTNG_KERNEL_NOOP
:
431 WRITE_ONCE(event
->enabled
, 1);
433 case LTTNG_KERNEL_KRETPROBE
:
434 ret
= lttng_kretprobes_event_enable_state(event
, 1);
436 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
442 mutex_unlock(&sessions_mutex
);
446 int lttng_event_disable(struct lttng_event
*event
)
450 mutex_lock(&sessions_mutex
);
451 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
455 if (!event
->enabled
) {
459 switch (event
->instrumentation
) {
460 case LTTNG_KERNEL_TRACEPOINT
:
461 case LTTNG_KERNEL_SYSCALL
:
464 case LTTNG_KERNEL_KPROBE
:
465 case LTTNG_KERNEL_UPROBE
:
466 case LTTNG_KERNEL_NOOP
:
467 WRITE_ONCE(event
->enabled
, 0);
469 case LTTNG_KERNEL_KRETPROBE
:
470 ret
= lttng_kretprobes_event_enable_state(event
, 0);
472 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
478 mutex_unlock(&sessions_mutex
);
482 static struct lttng_transport
*lttng_transport_find(const char *name
)
484 struct lttng_transport
*transport
;
486 list_for_each_entry(transport
, <tng_transport_list
, node
) {
487 if (!strcmp(transport
->name
, name
))
493 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
494 const char *transport_name
,
496 size_t subbuf_size
, size_t num_subbuf
,
497 unsigned int switch_timer_interval
,
498 unsigned int read_timer_interval
,
499 enum channel_type channel_type
)
501 struct lttng_channel
*chan
;
502 struct lttng_transport
*transport
= NULL
;
504 mutex_lock(&sessions_mutex
);
505 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
506 goto active
; /* Refuse to add channel to active session */
507 transport
= lttng_transport_find(transport_name
);
509 printk(KERN_WARNING
"LTTng: transport %s not found\n",
513 if (!try_module_get(transport
->owner
)) {
514 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
517 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
520 chan
->session
= session
;
521 chan
->id
= session
->free_chan_id
++;
522 chan
->ops
= &transport
->ops
;
524 * Note: the channel creation op already writes into the packet
525 * headers. Therefore the "chan" information used as input
526 * should be already accessible.
528 chan
->chan
= transport
->ops
.channel_create(transport_name
,
529 chan
, buf_addr
, subbuf_size
, num_subbuf
,
530 switch_timer_interval
, read_timer_interval
);
535 chan
->transport
= transport
;
536 chan
->channel_type
= channel_type
;
537 list_add(&chan
->list
, &session
->chan
);
538 mutex_unlock(&sessions_mutex
);
545 module_put(transport
->owner
);
548 mutex_unlock(&sessions_mutex
);
553 * Only used internally at session destruction for per-cpu channels, and
554 * when metadata channel is released.
555 * Needs to be called with sessions mutex held.
558 void _lttng_channel_destroy(struct lttng_channel
*chan
)
560 chan
->ops
->channel_destroy(chan
->chan
);
561 module_put(chan
->transport
->owner
);
562 list_del(&chan
->list
);
563 lttng_destroy_context(chan
->ctx
);
567 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
569 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
571 /* Protect the metadata cache with the sessions_mutex. */
572 mutex_lock(&sessions_mutex
);
573 _lttng_channel_destroy(chan
);
574 mutex_unlock(&sessions_mutex
);
576 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
579 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
581 stream
->finalized
= 1;
582 wake_up_interruptible(&stream
->read_wait
);
586 * Supports event creation while tracing session is active.
587 * Needs to be called with sessions mutex held.
589 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
590 struct lttng_kernel_event
*event_param
,
592 const struct lttng_event_desc
*event_desc
,
593 enum lttng_kernel_instrumentation itype
)
595 struct lttng_session
*session
= chan
->session
;
596 struct lttng_event
*event
;
597 const char *event_name
;
598 struct hlist_head
*head
;
603 if (chan
->free_event_id
== -1U) {
609 case LTTNG_KERNEL_TRACEPOINT
:
610 event_name
= event_desc
->name
;
612 case LTTNG_KERNEL_KPROBE
:
613 case LTTNG_KERNEL_UPROBE
:
614 case LTTNG_KERNEL_KRETPROBE
:
615 case LTTNG_KERNEL_NOOP
:
616 case LTTNG_KERNEL_SYSCALL
:
617 event_name
= event_param
->name
;
619 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
625 name_len
= strlen(event_name
);
626 hash
= jhash(event_name
, name_len
, 0);
627 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
628 lttng_hlist_for_each_entry(event
, head
, hlist
) {
629 WARN_ON_ONCE(!event
->desc
);
630 if (!strncmp(event
->desc
->name
, event_name
,
631 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
632 && chan
== event
->chan
) {
638 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
644 event
->filter
= filter
;
645 event
->id
= chan
->free_event_id
++;
646 event
->instrumentation
= itype
;
647 event
->evtype
= LTTNG_TYPE_EVENT
;
648 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
649 INIT_LIST_HEAD(&event
->enablers_ref_head
);
652 case LTTNG_KERNEL_TRACEPOINT
:
653 /* Event will be enabled by enabler sync. */
655 event
->registered
= 0;
656 event
->desc
= lttng_event_get(event_name
);
661 /* Populate lttng_event structure before event registration. */
664 case LTTNG_KERNEL_KPROBE
:
666 * Needs to be explicitly enabled after creation, since
667 * we may want to apply filters.
670 event
->registered
= 1;
672 * Populate lttng_event structure before event
676 ret
= lttng_kprobes_register(event_name
,
677 event_param
->u
.kprobe
.symbol_name
,
678 event_param
->u
.kprobe
.offset
,
679 event_param
->u
.kprobe
.addr
,
685 ret
= try_module_get(event
->desc
->owner
);
688 case LTTNG_KERNEL_KRETPROBE
:
690 struct lttng_event
*event_return
;
692 /* kretprobe defines 2 events */
694 * Needs to be explicitly enabled after creation, since
695 * we may want to apply filters.
698 event
->registered
= 1;
700 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
705 event_return
->chan
= chan
;
706 event_return
->filter
= filter
;
707 event_return
->id
= chan
->free_event_id
++;
708 event_return
->enabled
= 0;
709 event_return
->registered
= 1;
710 event_return
->instrumentation
= itype
;
712 * Populate lttng_event structure before kretprobe registration.
715 ret
= lttng_kretprobes_register(event_name
,
716 event_param
->u
.kretprobe
.symbol_name
,
717 event_param
->u
.kretprobe
.offset
,
718 event_param
->u
.kretprobe
.addr
,
719 event
, event_return
);
721 kmem_cache_free(event_cache
, event_return
);
725 /* Take 2 refs on the module: one per event. */
726 ret
= try_module_get(event
->desc
->owner
);
728 ret
= try_module_get(event
->desc
->owner
);
730 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
732 WARN_ON_ONCE(ret
> 0);
734 kmem_cache_free(event_cache
, event_return
);
735 module_put(event
->desc
->owner
);
736 module_put(event
->desc
->owner
);
737 goto statedump_error
;
739 list_add(&event_return
->list
, &chan
->session
->events
);
742 case LTTNG_KERNEL_NOOP
:
743 case LTTNG_KERNEL_SYSCALL
:
745 * Needs to be explicitly enabled after creation, since
746 * we may want to apply filters.
749 event
->registered
= 0;
750 event
->desc
= event_desc
;
751 switch (event_param
->u
.syscall
.entryexit
) {
752 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
755 case LTTNG_KERNEL_SYSCALL_ENTRY
:
756 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
758 case LTTNG_KERNEL_SYSCALL_EXIT
:
759 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
762 switch (event_param
->u
.syscall
.abi
) {
763 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
766 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
767 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
769 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
770 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
778 case LTTNG_KERNEL_UPROBE
:
780 * Needs to be explicitly enabled after creation, since
781 * we may want to apply filters.
784 event
->registered
= 1;
787 * Populate lttng_event structure before event
792 ret
= lttng_uprobes_register(event_param
->name
,
793 event_param
->u
.uprobe
.fd
,
797 ret
= try_module_get(event
->desc
->owner
);
800 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
806 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
807 WARN_ON_ONCE(ret
> 0);
809 goto statedump_error
;
811 hlist_add_head(&event
->hlist
, head
);
812 list_add(&event
->list
, &chan
->session
->events
);
816 /* If a statedump error occurs, events will not be readable. */
818 kmem_cache_free(event_cache
, event
);
826 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
827 struct lttng_kernel_event
*event_param
,
829 const struct lttng_event_desc
*event_desc
,
830 enum lttng_kernel_instrumentation itype
)
832 struct lttng_event
*event
;
834 mutex_lock(&sessions_mutex
);
835 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
837 mutex_unlock(&sessions_mutex
);
841 /* Only used for tracepoints for now. */
843 void register_event(struct lttng_event
*event
)
845 const struct lttng_event_desc
*desc
;
848 if (event
->registered
)
852 switch (event
->instrumentation
) {
853 case LTTNG_KERNEL_TRACEPOINT
:
854 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
855 desc
->probe_callback
,
858 case LTTNG_KERNEL_SYSCALL
:
859 ret
= lttng_syscall_filter_enable(event
->chan
, event
);
861 case LTTNG_KERNEL_KPROBE
:
862 case LTTNG_KERNEL_UPROBE
:
863 case LTTNG_KERNEL_KRETPROBE
:
864 case LTTNG_KERNEL_NOOP
:
867 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
872 event
->registered
= 1;
876 * Only used internally at session destruction.
878 int _lttng_event_unregister(struct lttng_event
*event
)
880 const struct lttng_event_desc
*desc
;
883 if (!event
->registered
)
887 switch (event
->instrumentation
) {
888 case LTTNG_KERNEL_TRACEPOINT
:
889 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
890 event
->desc
->probe_callback
,
893 case LTTNG_KERNEL_KPROBE
:
894 lttng_kprobes_unregister(event
);
897 case LTTNG_KERNEL_KRETPROBE
:
898 lttng_kretprobes_unregister(event
);
901 case LTTNG_KERNEL_SYSCALL
:
902 ret
= lttng_syscall_filter_disable(event
->chan
, event
);
904 case LTTNG_KERNEL_NOOP
:
907 case LTTNG_KERNEL_UPROBE
:
908 lttng_uprobes_unregister(event
);
911 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
916 event
->registered
= 0;
921 * Only used internally at session destruction.
924 void _lttng_event_destroy(struct lttng_event
*event
)
926 switch (event
->instrumentation
) {
927 case LTTNG_KERNEL_TRACEPOINT
:
928 lttng_event_put(event
->desc
);
930 case LTTNG_KERNEL_KPROBE
:
931 module_put(event
->desc
->owner
);
932 lttng_kprobes_destroy_private(event
);
934 case LTTNG_KERNEL_KRETPROBE
:
935 module_put(event
->desc
->owner
);
936 lttng_kretprobes_destroy_private(event
);
938 case LTTNG_KERNEL_NOOP
:
939 case LTTNG_KERNEL_SYSCALL
:
941 case LTTNG_KERNEL_UPROBE
:
942 module_put(event
->desc
->owner
);
943 lttng_uprobes_destroy_private(event
);
945 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
949 list_del(&event
->list
);
950 lttng_destroy_context(event
->ctx
);
951 kmem_cache_free(event_cache
, event
);
954 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
955 enum tracker_type tracker_type
)
957 switch (tracker_type
) {
959 return &session
->pid_tracker
;
961 return &session
->vpid_tracker
;
963 return &session
->uid_tracker
;
965 return &session
->vuid_tracker
;
967 return &session
->gid_tracker
;
969 return &session
->vgid_tracker
;
976 int lttng_session_track_id(struct lttng_session
*session
,
977 enum tracker_type tracker_type
, int id
)
979 struct lttng_id_tracker
*tracker
;
982 tracker
= get_tracker(session
, tracker_type
);
987 mutex_lock(&sessions_mutex
);
989 /* track all ids: destroy tracker. */
990 lttng_id_tracker_destroy(tracker
, true);
993 ret
= lttng_id_tracker_add(tracker
, id
);
995 mutex_unlock(&sessions_mutex
);
999 int lttng_session_untrack_id(struct lttng_session
*session
,
1000 enum tracker_type tracker_type
, int id
)
1002 struct lttng_id_tracker
*tracker
;
1005 tracker
= get_tracker(session
, tracker_type
);
1010 mutex_lock(&sessions_mutex
);
1012 /* untrack all ids: replace by empty tracker. */
1013 ret
= lttng_id_tracker_empty_set(tracker
);
1015 ret
= lttng_id_tracker_del(tracker
, id
);
1017 mutex_unlock(&sessions_mutex
);
1022 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
1024 struct lttng_id_tracker
*id_tracker
= m
->private;
1025 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1026 struct lttng_id_hash_node
*e
;
1029 mutex_lock(&sessions_mutex
);
1031 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1032 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1034 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1040 /* ID tracker disabled. */
1041 if (iter
>= *pos
&& iter
== 0) {
1042 return id_tracker_p
; /* empty tracker */
1050 /* Called with sessions_mutex held. */
1052 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1054 struct lttng_id_tracker
*id_tracker
= m
->private;
1055 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1056 struct lttng_id_hash_node
*e
;
1061 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1062 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1064 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1065 if (iter
++ >= *ppos
)
1070 /* ID tracker disabled. */
1071 if (iter
>= *ppos
&& iter
== 0)
1072 return p
; /* empty tracker */
1081 void id_list_stop(struct seq_file
*m
, void *p
)
1083 mutex_unlock(&sessions_mutex
);
1087 int id_list_show(struct seq_file
*m
, void *p
)
1089 struct lttng_id_tracker
*id_tracker
= m
->private;
1090 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1093 if (p
== id_tracker_p
) {
1094 /* Tracker disabled. */
1097 const struct lttng_id_hash_node
*e
= p
;
1099 id
= lttng_id_tracker_get_node_id(e
);
1101 switch (id_tracker
->tracker_type
) {
1103 seq_printf(m
, "process { pid = %d; };\n", id
);
1106 seq_printf(m
, "process { vpid = %d; };\n", id
);
1109 seq_printf(m
, "user { uid = %d; };\n", id
);
1112 seq_printf(m
, "user { vuid = %d; };\n", id
);
1115 seq_printf(m
, "group { gid = %d; };\n", id
);
1118 seq_printf(m
, "group { vgid = %d; };\n", id
);
1121 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1127 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1128 .start
= id_list_start
,
1129 .next
= id_list_next
,
1130 .stop
= id_list_stop
,
1131 .show
= id_list_show
,
1135 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1137 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1141 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1143 struct seq_file
*m
= file
->private_data
;
1144 struct lttng_id_tracker
*id_tracker
= m
->private;
1147 WARN_ON_ONCE(!id_tracker
);
1148 ret
= seq_release(inode
, file
);
1150 fput(id_tracker
->session
->file
);
1154 const struct file_operations lttng_tracker_ids_list_fops
= {
1155 .owner
= THIS_MODULE
,
1156 .open
= lttng_tracker_ids_list_open
,
1158 .llseek
= seq_lseek
,
1159 .release
= lttng_tracker_ids_list_release
,
1162 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1163 enum tracker_type tracker_type
)
1165 struct file
*tracker_ids_list_file
;
1169 file_fd
= lttng_get_unused_fd();
1175 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1176 <tng_tracker_ids_list_fops
,
1178 if (IS_ERR(tracker_ids_list_file
)) {
1179 ret
= PTR_ERR(tracker_ids_list_file
);
1182 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1184 goto refcount_error
;
1186 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1189 m
= tracker_ids_list_file
->private_data
;
1191 m
->private = get_tracker(session
, tracker_type
);
1192 BUG_ON(!m
->private);
1193 fd_install(file_fd
, tracker_ids_list_file
);
1198 atomic_long_dec(&session
->file
->f_count
);
1200 fput(tracker_ids_list_file
);
1202 put_unused_fd(file_fd
);
1208 * Enabler management.
1211 int lttng_match_enabler_star_glob(const char *desc_name
,
1212 const char *pattern
)
1214 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1215 desc_name
, LTTNG_SIZE_MAX
))
1221 int lttng_match_enabler_name(const char *desc_name
,
1224 if (strcmp(desc_name
, name
))
1230 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1231 struct lttng_enabler
*enabler
)
1233 const char *desc_name
, *enabler_name
;
1234 bool compat
= false, entry
= false;
1236 enabler_name
= enabler
->event_param
.name
;
1237 switch (enabler
->event_param
.instrumentation
) {
1238 case LTTNG_KERNEL_TRACEPOINT
:
1239 desc_name
= desc
->name
;
1240 switch (enabler
->format_type
) {
1241 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
1242 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1243 case LTTNG_ENABLER_FORMAT_NAME
:
1244 return lttng_match_enabler_name(desc_name
, enabler_name
);
1249 case LTTNG_KERNEL_SYSCALL
:
1250 desc_name
= desc
->name
;
1251 if (!strncmp(desc_name
, "compat_", strlen("compat_"))) {
1252 desc_name
+= strlen("compat_");
1255 if (!strncmp(desc_name
, "syscall_exit_",
1256 strlen("syscall_exit_"))) {
1257 desc_name
+= strlen("syscall_exit_");
1258 } else if (!strncmp(desc_name
, "syscall_entry_",
1259 strlen("syscall_entry_"))) {
1260 desc_name
+= strlen("syscall_entry_");
1266 switch (enabler
->event_param
.u
.syscall
.entryexit
) {
1267 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1269 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1273 case LTTNG_KERNEL_SYSCALL_EXIT
:
1280 switch (enabler
->event_param
.u
.syscall
.abi
) {
1281 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1283 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1287 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1294 switch (enabler
->event_param
.u
.syscall
.match
) {
1295 case LTTNG_SYSCALL_MATCH_NAME
:
1296 switch (enabler
->format_type
) {
1297 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
1298 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1299 case LTTNG_ENABLER_FORMAT_NAME
:
1300 return lttng_match_enabler_name(desc_name
, enabler_name
);
1305 case LTTNG_SYSCALL_MATCH_NR
:
1306 return -EINVAL
; /* Not implemented. */
1318 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
1319 struct lttng_event
*event
)
1321 struct lttng_enabler
*base_enabler
= lttng_event_enabler_as_enabler(
1324 if (base_enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1326 if (lttng_desc_match_enabler(event
->desc
, base_enabler
)
1327 && event
->chan
== event_enabler
->chan
)
1334 struct lttng_enabler_ref
*lttng_enabler_ref(
1335 struct list_head
*enablers_ref_list
,
1336 struct lttng_enabler
*enabler
)
1338 struct lttng_enabler_ref
*enabler_ref
;
1340 list_for_each_entry(enabler_ref
, enablers_ref_list
, node
) {
1341 if (enabler_ref
->ref
== enabler
)
1348 void lttng_create_tracepoint_event_if_missing(struct lttng_event_enabler
*event_enabler
)
1350 struct lttng_session
*session
= event_enabler
->chan
->session
;
1351 struct lttng_probe_desc
*probe_desc
;
1352 const struct lttng_event_desc
*desc
;
1354 struct list_head
*probe_list
;
1356 probe_list
= lttng_get_probe_list_head();
1358 * For each probe event, if we find that a probe event matches
1359 * our enabler, create an associated lttng_event if not
1362 list_for_each_entry(probe_desc
, probe_list
, head
) {
1363 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1365 struct hlist_head
*head
;
1366 const char *event_name
;
1369 struct lttng_event
*event
;
1371 desc
= probe_desc
->event_desc
[i
];
1372 if (!lttng_desc_match_enabler(desc
,
1373 lttng_event_enabler_as_enabler(event_enabler
)))
1375 event_name
= desc
->name
;
1376 name_len
= strlen(event_name
);
1379 * Check if already created.
1381 hash
= jhash(event_name
, name_len
, 0);
1382 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1383 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1384 if (event
->desc
== desc
1385 && event
->chan
== event_enabler
->chan
)
1392 * We need to create an event for this
1395 event
= _lttng_event_create(event_enabler
->chan
,
1397 LTTNG_KERNEL_TRACEPOINT
);
1399 printk(KERN_INFO
"LTTng: Unable to create event %s\n",
1400 probe_desc
->event_desc
[i
]->name
);
1407 void lttng_create_syscall_event_if_missing(struct lttng_event_enabler
*event_enabler
)
1411 ret
= lttng_syscalls_register(event_enabler
->chan
, NULL
);
1416 * Create struct lttng_event if it is missing and present in the list of
1417 * tracepoint probes.
1418 * Should be called with sessions mutex held.
1421 void lttng_create_event_if_missing(struct lttng_event_enabler
*event_enabler
)
1423 switch (event_enabler
->base
.event_param
.instrumentation
) {
1424 case LTTNG_KERNEL_TRACEPOINT
:
1425 lttng_create_tracepoint_event_if_missing(event_enabler
);
1427 case LTTNG_KERNEL_SYSCALL
:
1428 lttng_create_syscall_event_if_missing(event_enabler
);
1437 * Create events associated with an event_enabler (if not already present),
1438 * and add backward reference from the event to the enabler.
1439 * Should be called with sessions mutex held.
1442 int lttng_event_enabler_ref_events(struct lttng_event_enabler
*event_enabler
)
1444 struct lttng_channel
*chan
= event_enabler
->chan
;
1445 struct lttng_session
*session
= event_enabler
->chan
->session
;
1446 struct lttng_enabler
*base_enabler
= lttng_event_enabler_as_enabler(event_enabler
);
1447 struct lttng_event
*event
;
1449 if (base_enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
1450 base_enabler
->event_param
.u
.syscall
.entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
&&
1451 base_enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
1452 base_enabler
->event_param
.u
.syscall
.match
== LTTNG_SYSCALL_MATCH_NAME
&&
1453 !strcmp(base_enabler
->event_param
.name
, "*")) {
1454 if (base_enabler
->enabled
)
1455 WRITE_ONCE(chan
->syscall_all
, 1);
1457 WRITE_ONCE(chan
->syscall_all
, 0);
1460 /* First ensure that probe events are created for this enabler. */
1461 lttng_create_event_if_missing(event_enabler
);
1463 /* For each event matching event_enabler in session event list. */
1464 list_for_each_entry(event
, &session
->events
, list
) {
1465 struct lttng_enabler_ref
*enabler_ref
;
1467 if (!lttng_event_enabler_match_event(event_enabler
, event
))
1469 enabler_ref
= lttng_enabler_ref(&event
->enablers_ref_head
,
1470 lttng_event_enabler_as_enabler(event_enabler
));
1473 * If no backward ref, create it.
1474 * Add backward ref from event to event_enabler.
1476 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1479 enabler_ref
->ref
= lttng_event_enabler_as_enabler(event_enabler
);
1480 list_add(&enabler_ref
->node
,
1481 &event
->enablers_ref_head
);
1485 * Link filter bytecodes if not linked yet.
1487 lttng_event_enabler_link_bytecode(event
, event_enabler
);
1489 /* TODO: merge event context. */
1495 * Called at module load: connect the probe on all enablers matching
1497 * Called with sessions lock held.
1499 int lttng_fix_pending_events(void)
1501 struct lttng_session
*session
;
1503 list_for_each_entry(session
, &sessions
, list
)
1504 lttng_session_lazy_sync_event_enablers(session
);
1508 struct lttng_event_enabler
*lttng_event_enabler_create(
1509 enum lttng_enabler_format_type format_type
,
1510 struct lttng_kernel_event
*event_param
,
1511 struct lttng_channel
*chan
)
1513 struct lttng_event_enabler
*event_enabler
;
1515 event_enabler
= kzalloc(sizeof(*event_enabler
), GFP_KERNEL
);
1518 event_enabler
->base
.format_type
= format_type
;
1519 INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
1520 memcpy(&event_enabler
->base
.event_param
, event_param
,
1521 sizeof(event_enabler
->base
.event_param
));
1522 event_enabler
->chan
= chan
;
1524 event_enabler
->base
.enabled
= 0;
1525 event_enabler
->base
.evtype
= LTTNG_TYPE_ENABLER
;
1526 mutex_lock(&sessions_mutex
);
1527 list_add(&event_enabler
->node
, &event_enabler
->chan
->session
->enablers_head
);
1528 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1529 mutex_unlock(&sessions_mutex
);
1530 return event_enabler
;
1533 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
1535 mutex_lock(&sessions_mutex
);
1536 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
1537 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1538 mutex_unlock(&sessions_mutex
);
1542 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
1544 mutex_lock(&sessions_mutex
);
1545 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
1546 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1547 mutex_unlock(&sessions_mutex
);
1552 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1553 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1555 struct lttng_filter_bytecode_node
*bytecode_node
;
1556 uint32_t bytecode_len
;
1559 ret
= get_user(bytecode_len
, &bytecode
->len
);
1562 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1566 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1567 sizeof(*bytecode
) + bytecode_len
);
1571 bytecode_node
->enabler
= enabler
;
1572 /* Enforce length based on allocated size */
1573 bytecode_node
->bc
.len
= bytecode_len
;
1574 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1579 kfree(bytecode_node
);
1583 int lttng_event_enabler_attach_bytecode(struct lttng_event_enabler
*event_enabler
,
1584 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1587 ret
= lttng_enabler_attach_bytecode(
1588 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
1592 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
1599 int lttng_event_add_callsite(struct lttng_event
*event
,
1600 struct lttng_kernel_event_callsite __user
*callsite
)
1603 switch (event
->instrumentation
) {
1604 case LTTNG_KERNEL_UPROBE
:
1605 return lttng_uprobes_add_callsite(event
, callsite
);
1611 int lttng_event_enabler_attach_context(struct lttng_event_enabler
*event_enabler
,
1612 struct lttng_kernel_context
*context_param
)
1618 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1620 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1622 /* Destroy filter bytecode */
1623 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1624 &enabler
->filter_bytecode_head
, node
) {
1630 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
1632 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
1634 /* Destroy contexts */
1635 lttng_destroy_context(event_enabler
->ctx
);
1637 list_del(&event_enabler
->node
);
1638 kfree(event_enabler
);
1642 * lttng_session_sync_event_enablers should be called just before starting a
1644 * Should be called with sessions mutex held.
1647 void lttng_session_sync_event_enablers(struct lttng_session
*session
)
1649 struct lttng_event_enabler
*event_enabler
;
1650 struct lttng_event
*event
;
1652 list_for_each_entry(event_enabler
, &session
->enablers_head
, node
)
1653 lttng_event_enabler_ref_events(event_enabler
);
1655 * For each event, if at least one of its enablers is enabled,
1656 * and its channel and session transient states are enabled, we
1657 * enable the event, else we disable it.
1659 list_for_each_entry(event
, &session
->events
, list
) {
1660 struct lttng_enabler_ref
*enabler_ref
;
1661 struct lttng_bytecode_runtime
*runtime
;
1662 int enabled
= 0, has_enablers_without_bytecode
= 0;
1664 switch (event
->instrumentation
) {
1665 case LTTNG_KERNEL_TRACEPOINT
:
1666 case LTTNG_KERNEL_SYSCALL
:
1668 list_for_each_entry(enabler_ref
,
1669 &event
->enablers_ref_head
, node
) {
1670 if (enabler_ref
->ref
->enabled
) {
1677 /* Not handled with lazy sync. */
1681 * Enabled state is based on union of enablers, with
1682 * intesection of session and channel transient enable
1685 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1687 WRITE_ONCE(event
->enabled
, enabled
);
1689 * Sync tracepoint registration with event enabled
1693 register_event(event
);
1695 _lttng_event_unregister(event
);
1698 /* Check if has enablers without bytecode enabled */
1699 list_for_each_entry(enabler_ref
,
1700 &event
->enablers_ref_head
, node
) {
1701 if (enabler_ref
->ref
->enabled
1702 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1703 has_enablers_without_bytecode
= 1;
1707 event
->has_enablers_without_bytecode
=
1708 has_enablers_without_bytecode
;
1710 /* Enable filters */
1711 list_for_each_entry(runtime
,
1712 &event
->bytecode_runtime_head
, node
)
1713 lttng_filter_sync_state(runtime
);
1718 * Apply enablers to session events, adding events to session if need
1719 * be. It is required after each modification applied to an active
1720 * session, and right before session "start".
1721 * "lazy" sync means we only sync if required.
1722 * Should be called with sessions mutex held.
1725 void lttng_session_lazy_sync_event_enablers(struct lttng_session
*session
)
1727 /* We can skip if session is not active */
1728 if (!session
->active
)
1730 lttng_session_sync_event_enablers(session
);
1734 * Serialize at most one packet worth of metadata into a metadata
1736 * We grab the metadata cache mutex to get exclusive access to our metadata
1737 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1738 * allows us to do racy operations such as looking for remaining space left in
1739 * packet and write, since mutual exclusion protects us from concurrent writes.
1740 * Mutual exclusion on the metadata cache allow us to read the cache content
1741 * without racing against reallocation of the cache by updates.
1742 * Returns the number of bytes written in the channel, 0 if no data
1743 * was written and a negative value on error.
1745 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1746 struct channel
*chan
, bool *coherent
)
1748 struct lib_ring_buffer_ctx ctx
;
1750 size_t len
, reserve_len
;
1753 * Ensure we support mutiple get_next / put sequences followed by
1754 * put_next. The metadata cache lock protects reading the metadata
1755 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1756 * "flush" operations on the buffer invoked by different processes.
1757 * Moreover, since the metadata cache memory can be reallocated, we
1758 * need to have exclusive access against updates even though we only
1761 mutex_lock(&stream
->metadata_cache
->lock
);
1762 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1763 if (stream
->metadata_in
!= stream
->metadata_out
)
1766 /* Metadata regenerated, change the version. */
1767 if (stream
->metadata_cache
->version
!= stream
->version
)
1768 stream
->version
= stream
->metadata_cache
->version
;
1770 len
= stream
->metadata_cache
->metadata_written
-
1771 stream
->metadata_in
;
1774 reserve_len
= min_t(size_t,
1775 stream
->transport
->ops
.packet_avail_size(chan
),
1777 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1780 * If reservation failed, return an error to the caller.
1782 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1784 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1785 stream
->coherent
= false;
1788 stream
->transport
->ops
.event_write(&ctx
,
1789 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1791 stream
->transport
->ops
.event_commit(&ctx
);
1792 stream
->metadata_in
+= reserve_len
;
1793 if (reserve_len
< len
)
1794 stream
->coherent
= false;
1796 stream
->coherent
= true;
1801 *coherent
= stream
->coherent
;
1802 mutex_unlock(&stream
->metadata_cache
->lock
);
1807 void lttng_metadata_begin(struct lttng_session
*session
)
1809 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1810 mutex_lock(&session
->metadata_cache
->lock
);
1814 void lttng_metadata_end(struct lttng_session
*session
)
1816 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1817 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1818 struct lttng_metadata_stream
*stream
;
1820 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1821 wake_up_interruptible(&stream
->read_wait
);
1822 mutex_unlock(&session
->metadata_cache
->lock
);
1827 * Write the metadata to the metadata cache.
1828 * Must be called with sessions_mutex held.
1829 * The metadata cache lock protects us from concurrent read access from
1830 * thread outputting metadata content to ring buffer.
1831 * The content of the printf is printed as a single atomic metadata
1834 int lttng_metadata_printf(struct lttng_session
*session
,
1835 const char *fmt
, ...)
1841 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
1844 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1850 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1851 if (session
->metadata_cache
->metadata_written
+ len
>
1852 session
->metadata_cache
->cache_alloc
) {
1853 char *tmp_cache_realloc
;
1854 unsigned int tmp_cache_alloc_size
;
1856 tmp_cache_alloc_size
= max_t(unsigned int,
1857 session
->metadata_cache
->cache_alloc
+ len
,
1858 session
->metadata_cache
->cache_alloc
<< 1);
1859 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1860 if (!tmp_cache_realloc
)
1862 if (session
->metadata_cache
->data
) {
1863 memcpy(tmp_cache_realloc
,
1864 session
->metadata_cache
->data
,
1865 session
->metadata_cache
->cache_alloc
);
1866 vfree(session
->metadata_cache
->data
);
1869 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1870 session
->metadata_cache
->data
= tmp_cache_realloc
;
1872 memcpy(session
->metadata_cache
->data
+
1873 session
->metadata_cache
->metadata_written
,
1875 session
->metadata_cache
->metadata_written
+= len
;
1886 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1890 for (i
= 0; i
< nesting
; i
++) {
1893 ret
= lttng_metadata_printf(session
, " ");
1902 int lttng_field_name_statedump(struct lttng_session
*session
,
1903 const struct lttng_event_field
*field
,
1906 return lttng_metadata_printf(session
, " _%s;\n", field
->name
);
1910 int _lttng_integer_type_statedump(struct lttng_session
*session
,
1911 const struct lttng_type
*type
,
1916 WARN_ON_ONCE(type
->atype
!= atype_integer
);
1917 ret
= print_tabs(session
, nesting
);
1920 ret
= lttng_metadata_printf(session
,
1921 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }",
1922 type
->u
.integer
.size
,
1923 type
->u
.integer
.alignment
,
1924 type
->u
.integer
.signedness
,
1925 (type
->u
.integer
.encoding
== lttng_encode_none
)
1927 : (type
->u
.integer
.encoding
== lttng_encode_UTF8
)
1930 type
->u
.integer
.base
,
1931 #if __BYTE_ORDER == __BIG_ENDIAN
1932 type
->u
.integer
.reverse_byte_order
? " byte_order = le;" : ""
1934 type
->u
.integer
.reverse_byte_order
? " byte_order = be;" : ""
1941 * Must be called with sessions_mutex held.
1944 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1945 const struct lttng_type
*type
,
1949 uint32_t i
, nr_fields
;
1950 unsigned int alignment
;
1952 WARN_ON_ONCE(type
->atype
!= atype_struct_nestable
);
1954 ret
= print_tabs(session
, nesting
);
1957 ret
= lttng_metadata_printf(session
,
1961 nr_fields
= type
->u
.struct_nestable
.nr_fields
;
1962 for (i
= 0; i
< nr_fields
; i
++) {
1963 const struct lttng_event_field
*iter_field
;
1965 iter_field
= &type
->u
.struct_nestable
.fields
[i
];
1966 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1970 ret
= print_tabs(session
, nesting
);
1973 alignment
= type
->u
.struct_nestable
.alignment
;
1975 ret
= lttng_metadata_printf(session
,
1979 ret
= lttng_metadata_printf(session
,
1986 * Must be called with sessions_mutex held.
1989 int _lttng_struct_field_statedump(struct lttng_session
*session
,
1990 const struct lttng_event_field
*field
,
1995 ret
= _lttng_struct_type_statedump(session
,
1996 &field
->type
, nesting
);
1999 return lttng_field_name_statedump(session
, field
, nesting
);
2003 * Must be called with sessions_mutex held.
2006 int _lttng_variant_type_statedump(struct lttng_session
*session
,
2007 const struct lttng_type
*type
,
2011 uint32_t i
, nr_choices
;
2013 WARN_ON_ONCE(type
->atype
!= atype_variant_nestable
);
2015 * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way.
2017 if (type
->u
.variant_nestable
.alignment
!= 0)
2019 ret
= print_tabs(session
, nesting
);
2022 ret
= lttng_metadata_printf(session
,
2023 "variant <_%s> {\n",
2024 type
->u
.variant_nestable
.tag_name
);
2027 nr_choices
= type
->u
.variant_nestable
.nr_choices
;
2028 for (i
= 0; i
< nr_choices
; i
++) {
2029 const struct lttng_event_field
*iter_field
;
2031 iter_field
= &type
->u
.variant_nestable
.choices
[i
];
2032 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
2036 ret
= print_tabs(session
, nesting
);
2039 ret
= lttng_metadata_printf(session
,
2045 * Must be called with sessions_mutex held.
2048 int _lttng_variant_field_statedump(struct lttng_session
*session
,
2049 const struct lttng_event_field
*field
,
2054 ret
= _lttng_variant_type_statedump(session
,
2055 &field
->type
, nesting
);
2058 return lttng_field_name_statedump(session
, field
, nesting
);
2062 * Must be called with sessions_mutex held.
2065 int _lttng_array_field_statedump(struct lttng_session
*session
,
2066 const struct lttng_event_field
*field
,
2070 const struct lttng_type
*elem_type
;
2072 WARN_ON_ONCE(field
->type
.atype
!= atype_array_nestable
);
2074 if (field
->type
.u
.array_nestable
.alignment
) {
2075 ret
= print_tabs(session
, nesting
);
2078 ret
= lttng_metadata_printf(session
,
2079 "struct { } align(%u) _%s_padding;\n",
2080 field
->type
.u
.array_nestable
.alignment
* CHAR_BIT
,
2086 * Nested compound types: Only array of structures and variants are
2087 * currently supported.
2089 elem_type
= field
->type
.u
.array_nestable
.elem_type
;
2090 switch (elem_type
->atype
) {
2092 case atype_struct_nestable
:
2093 case atype_variant_nestable
:
2094 ret
= _lttng_type_statedump(session
, elem_type
, nesting
);
2102 ret
= lttng_metadata_printf(session
,
2105 field
->type
.u
.array_nestable
.length
);
2110 * Must be called with sessions_mutex held.
2113 int _lttng_sequence_field_statedump(struct lttng_session
*session
,
2114 const struct lttng_event_field
*field
,
2118 const char *length_name
;
2119 const struct lttng_type
*elem_type
;
2121 WARN_ON_ONCE(field
->type
.atype
!= atype_sequence_nestable
);
2123 length_name
= field
->type
.u
.sequence_nestable
.length_name
;
2125 if (field
->type
.u
.sequence_nestable
.alignment
) {
2126 ret
= print_tabs(session
, nesting
);
2129 ret
= lttng_metadata_printf(session
,
2130 "struct { } align(%u) _%s_padding;\n",
2131 field
->type
.u
.sequence_nestable
.alignment
* CHAR_BIT
,
2138 * Nested compound types: Only array of structures and variants are
2139 * currently supported.
2141 elem_type
= field
->type
.u
.sequence_nestable
.elem_type
;
2142 switch (elem_type
->atype
) {
2144 case atype_struct_nestable
:
2145 case atype_variant_nestable
:
2146 ret
= _lttng_type_statedump(session
, elem_type
, nesting
);
2154 ret
= lttng_metadata_printf(session
,
2157 field
->type
.u
.sequence_nestable
.length_name
);
2162 * Must be called with sessions_mutex held.
2165 int _lttng_enum_type_statedump(struct lttng_session
*session
,
2166 const struct lttng_type
*type
,
2169 const struct lttng_enum_desc
*enum_desc
;
2170 const struct lttng_type
*container_type
;
2172 unsigned int i
, nr_entries
;
2174 container_type
= type
->u
.enum_nestable
.container_type
;
2175 if (container_type
->atype
!= atype_integer
) {
2179 enum_desc
= type
->u
.enum_nestable
.desc
;
2180 nr_entries
= enum_desc
->nr_entries
;
2182 ret
= print_tabs(session
, nesting
);
2185 ret
= lttng_metadata_printf(session
, "enum : ");
2188 ret
= _lttng_integer_type_statedump(session
, container_type
, 0);
2191 ret
= lttng_metadata_printf(session
, " {\n");
2194 /* Dump all entries */
2195 for (i
= 0; i
< nr_entries
; i
++) {
2196 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2199 ret
= print_tabs(session
, nesting
+ 1);
2202 ret
= lttng_metadata_printf(session
,
2206 len
= strlen(entry
->string
);
2207 /* Escape the character '"' */
2208 for (j
= 0; j
< len
; j
++) {
2209 char c
= entry
->string
[j
];
2213 ret
= lttng_metadata_printf(session
,
2217 ret
= lttng_metadata_printf(session
,
2221 ret
= lttng_metadata_printf(session
,
2228 ret
= lttng_metadata_printf(session
, "\"");
2232 if (entry
->options
.is_auto
) {
2233 ret
= lttng_metadata_printf(session
, ",\n");
2237 ret
= lttng_metadata_printf(session
,
2241 if (entry
->start
.signedness
)
2242 ret
= lttng_metadata_printf(session
,
2243 "%lld", (long long) entry
->start
.value
);
2245 ret
= lttng_metadata_printf(session
,
2246 "%llu", entry
->start
.value
);
2249 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2251 == entry
->end
.value
) {
2252 ret
= lttng_metadata_printf(session
,
2255 if (entry
->end
.signedness
) {
2256 ret
= lttng_metadata_printf(session
,
2258 (long long) entry
->end
.value
);
2260 ret
= lttng_metadata_printf(session
,
2269 ret
= print_tabs(session
, nesting
);
2272 ret
= lttng_metadata_printf(session
, "}");
2278 * Must be called with sessions_mutex held.
2281 int _lttng_enum_field_statedump(struct lttng_session
*session
,
2282 const struct lttng_event_field
*field
,
2287 ret
= _lttng_enum_type_statedump(session
, &field
->type
, nesting
);
2290 return lttng_field_name_statedump(session
, field
, nesting
);
2294 int _lttng_integer_field_statedump(struct lttng_session
*session
,
2295 const struct lttng_event_field
*field
,
2300 ret
= _lttng_integer_type_statedump(session
, &field
->type
, nesting
);
2303 return lttng_field_name_statedump(session
, field
, nesting
);
2307 int _lttng_string_type_statedump(struct lttng_session
*session
,
2308 const struct lttng_type
*type
,
2313 WARN_ON_ONCE(type
->atype
!= atype_string
);
2314 /* Default encoding is UTF8 */
2315 ret
= print_tabs(session
, nesting
);
2318 ret
= lttng_metadata_printf(session
,
2320 type
->u
.string
.encoding
== lttng_encode_ASCII
?
2321 " { encoding = ASCII; }" : "");
2326 int _lttng_string_field_statedump(struct lttng_session
*session
,
2327 const struct lttng_event_field
*field
,
2332 WARN_ON_ONCE(field
->type
.atype
!= atype_string
);
2333 ret
= _lttng_string_type_statedump(session
, &field
->type
, nesting
);
2336 return lttng_field_name_statedump(session
, field
, nesting
);
2340 * Must be called with sessions_mutex held.
2343 int _lttng_type_statedump(struct lttng_session
*session
,
2344 const struct lttng_type
*type
,
2349 switch (type
->atype
) {
2351 ret
= _lttng_integer_type_statedump(session
, type
, nesting
);
2353 case atype_enum_nestable
:
2354 ret
= _lttng_enum_type_statedump(session
, type
, nesting
);
2357 ret
= _lttng_string_type_statedump(session
, type
, nesting
);
2359 case atype_struct_nestable
:
2360 ret
= _lttng_struct_type_statedump(session
, type
, nesting
);
2362 case atype_variant_nestable
:
2363 ret
= _lttng_variant_type_statedump(session
, type
, nesting
);
2366 /* Nested arrays and sequences are not supported yet. */
2367 case atype_array_nestable
:
2368 case atype_sequence_nestable
:
2377 * Must be called with sessions_mutex held.
2380 int _lttng_field_statedump(struct lttng_session
*session
,
2381 const struct lttng_event_field
*field
,
2386 switch (field
->type
.atype
) {
2388 ret
= _lttng_integer_field_statedump(session
, field
, nesting
);
2390 case atype_enum_nestable
:
2391 ret
= _lttng_enum_field_statedump(session
, field
, nesting
);
2394 ret
= _lttng_string_field_statedump(session
, field
, nesting
);
2396 case atype_struct_nestable
:
2397 ret
= _lttng_struct_field_statedump(session
, field
, nesting
);
2399 case atype_array_nestable
:
2400 ret
= _lttng_array_field_statedump(session
, field
, nesting
);
2402 case atype_sequence_nestable
:
2403 ret
= _lttng_sequence_field_statedump(session
, field
, nesting
);
2405 case atype_variant_nestable
:
2406 ret
= _lttng_variant_field_statedump(session
, field
, nesting
);
2417 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2418 struct lttng_ctx
*ctx
)
2425 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2426 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2428 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2436 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2437 struct lttng_event
*event
)
2439 const struct lttng_event_desc
*desc
= event
->desc
;
2443 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2444 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2446 ret
= _lttng_field_statedump(session
, field
, 2);
2454 * Must be called with sessions_mutex held.
2455 * The entire event metadata is printed as a single atomic metadata
2459 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2460 struct lttng_channel
*chan
,
2461 struct lttng_event
*event
)
2465 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2467 if (chan
->channel_type
== METADATA_CHANNEL
)
2470 lttng_metadata_begin(session
);
2472 ret
= lttng_metadata_printf(session
,
2476 " stream_id = %u;\n",
2484 ret
= lttng_metadata_printf(session
,
2485 " context := struct {\n");
2489 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2493 ret
= lttng_metadata_printf(session
,
2499 ret
= lttng_metadata_printf(session
,
2500 " fields := struct {\n"
2505 ret
= _lttng_fields_metadata_statedump(session
, event
);
2510 * LTTng space reservation can only reserve multiples of the
2513 ret
= lttng_metadata_printf(session
,
2519 event
->metadata_dumped
= 1;
2521 lttng_metadata_end(session
);
2527 * Must be called with sessions_mutex held.
2528 * The entire channel metadata is printed as a single atomic metadata
2532 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2533 struct lttng_channel
*chan
)
2537 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2540 if (chan
->channel_type
== METADATA_CHANNEL
)
2543 lttng_metadata_begin(session
);
2545 WARN_ON_ONCE(!chan
->header_type
);
2546 ret
= lttng_metadata_printf(session
,
2549 " event.header := %s;\n"
2550 " packet.context := struct packet_context;\n",
2552 chan
->header_type
== 1 ? "struct event_header_compact" :
2553 "struct event_header_large");
2558 ret
= lttng_metadata_printf(session
,
2559 " event.context := struct {\n");
2563 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2567 ret
= lttng_metadata_printf(session
,
2573 ret
= lttng_metadata_printf(session
,
2576 chan
->metadata_dumped
= 1;
2578 lttng_metadata_end(session
);
2583 * Must be called with sessions_mutex held.
2586 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2588 return lttng_metadata_printf(session
,
2589 "struct packet_context {\n"
2590 " uint64_clock_monotonic_t timestamp_begin;\n"
2591 " uint64_clock_monotonic_t timestamp_end;\n"
2592 " uint64_t content_size;\n"
2593 " uint64_t packet_size;\n"
2594 " uint64_t packet_seq_num;\n"
2595 " unsigned long events_discarded;\n"
2596 " uint32_t cpu_id;\n"
2603 * id: range: 0 - 30.
2604 * id 31 is reserved to indicate an extended header.
2607 * id: range: 0 - 65534.
2608 * id 65535 is reserved to indicate an extended header.
2610 * Must be called with sessions_mutex held.
2613 int _lttng_event_header_declare(struct lttng_session
*session
)
2615 return lttng_metadata_printf(session
,
2616 "struct event_header_compact {\n"
2617 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2620 " uint27_clock_monotonic_t timestamp;\n"
2624 " uint64_clock_monotonic_t timestamp;\n"
2629 "struct event_header_large {\n"
2630 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2633 " uint32_clock_monotonic_t timestamp;\n"
2637 " uint64_clock_monotonic_t timestamp;\n"
2641 lttng_alignof(uint32_t) * CHAR_BIT
,
2642 lttng_alignof(uint16_t) * CHAR_BIT
2647 * Approximation of NTP time of day to clock monotonic correlation,
2648 * taken at start of trace.
2649 * Yes, this is only an approximation. Yes, we can (and will) do better
2650 * in future versions.
2651 * This function may return a negative offset. It may happen if the
2652 * system sets the REALTIME clock to 0 after boot.
2654 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2658 int64_t measure_clock_offset(void)
2660 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2661 uint64_t tcf
= trace_clock_freq();
2663 unsigned long flags
;
2664 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2665 struct timespec64 rts
= { 0, 0 };
2667 struct timespec rts
= { 0, 0 };
2670 /* Disable interrupts to increase correlation precision. */
2671 local_irq_save(flags
);
2672 monotonic
[0] = trace_clock_read64();
2673 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2674 ktime_get_real_ts64(&rts
);
2676 getnstimeofday(&rts
);
2678 monotonic
[1] = trace_clock_read64();
2679 local_irq_restore(flags
);
2681 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2682 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2683 if (tcf
== NSEC_PER_SEC
) {
2684 realtime
+= rts
.tv_nsec
;
2686 uint64_t n
= rts
.tv_nsec
* tcf
;
2688 do_div(n
, NSEC_PER_SEC
);
2691 offset
= (int64_t) realtime
- monotonic_avg
;
2696 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2704 while (cur
!= '\0') {
2707 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2711 ret
= lttng_metadata_printf(session
, "%c", '\\');
2714 /* We still print the current char */
2717 ret
= lttng_metadata_printf(session
, "%c", cur
);
2731 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2732 const char *field_value
)
2736 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2740 ret
= print_escaped_ctf_string(session
, field_value
);
2744 ret
= lttng_metadata_printf(session
, "\";\n");
2751 * Output metadata into this session's metadata buffers.
2752 * Must be called with sessions_mutex held.
2755 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2757 unsigned char *uuid_c
= session
->uuid
.b
;
2758 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2759 const char *product_uuid
;
2760 struct lttng_channel
*chan
;
2761 struct lttng_event
*event
;
2764 if (!LTTNG_READ_ONCE(session
->active
))
2767 lttng_metadata_begin(session
);
2769 if (session
->metadata_dumped
)
2772 snprintf(uuid_s
, sizeof(uuid_s
),
2773 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2774 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2775 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2776 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2777 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2779 ret
= lttng_metadata_printf(session
,
2780 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2781 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2782 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2783 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2784 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2785 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2786 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2792 " byte_order = %s;\n"
2793 " packet.header := struct {\n"
2794 " uint32_t magic;\n"
2795 " uint8_t uuid[16];\n"
2796 " uint32_t stream_id;\n"
2797 " uint64_t stream_instance_id;\n"
2800 lttng_alignof(uint8_t) * CHAR_BIT
,
2801 lttng_alignof(uint16_t) * CHAR_BIT
,
2802 lttng_alignof(uint32_t) * CHAR_BIT
,
2803 lttng_alignof(uint64_t) * CHAR_BIT
,
2804 sizeof(unsigned long) * CHAR_BIT
,
2805 lttng_alignof(unsigned long) * CHAR_BIT
,
2809 #if __BYTE_ORDER == __BIG_ENDIAN
2818 ret
= lttng_metadata_printf(session
,
2820 " hostname = \"%s\";\n"
2821 " domain = \"kernel\";\n"
2822 " sysname = \"%s\";\n"
2823 " kernel_release = \"%s\";\n"
2824 " kernel_version = \"%s\";\n"
2825 " tracer_name = \"lttng-modules\";\n"
2826 " tracer_major = %d;\n"
2827 " tracer_minor = %d;\n"
2828 " tracer_patchlevel = %d;\n"
2829 " trace_buffering_scheme = \"global\";\n",
2830 current
->nsproxy
->uts_ns
->name
.nodename
,
2834 LTTNG_MODULES_MAJOR_VERSION
,
2835 LTTNG_MODULES_MINOR_VERSION
,
2836 LTTNG_MODULES_PATCHLEVEL_VERSION
2841 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2844 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2845 session
->creation_time
);
2849 /* Add the product UUID to the 'env' section */
2850 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2852 ret
= lttng_metadata_printf(session
,
2853 " product_uuid = \"%s\";\n",
2860 /* Close the 'env' section */
2861 ret
= lttng_metadata_printf(session
, "};\n\n");
2865 ret
= lttng_metadata_printf(session
,
2867 " name = \"%s\";\n",
2873 if (!trace_clock_uuid(clock_uuid_s
)) {
2874 ret
= lttng_metadata_printf(session
,
2875 " uuid = \"%s\";\n",
2882 ret
= lttng_metadata_printf(session
,
2883 " description = \"%s\";\n"
2884 " freq = %llu; /* Frequency, in Hz */\n"
2885 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2888 trace_clock_description(),
2889 (unsigned long long) trace_clock_freq(),
2890 (long long) measure_clock_offset()
2895 ret
= lttng_metadata_printf(session
,
2896 "typealias integer {\n"
2897 " size = 27; align = 1; signed = false;\n"
2898 " map = clock.%s.value;\n"
2899 "} := uint27_clock_monotonic_t;\n"
2901 "typealias integer {\n"
2902 " size = 32; align = %u; signed = false;\n"
2903 " map = clock.%s.value;\n"
2904 "} := uint32_clock_monotonic_t;\n"
2906 "typealias integer {\n"
2907 " size = 64; align = %u; signed = false;\n"
2908 " map = clock.%s.value;\n"
2909 "} := uint64_clock_monotonic_t;\n\n",
2911 lttng_alignof(uint32_t) * CHAR_BIT
,
2913 lttng_alignof(uint64_t) * CHAR_BIT
,
2919 ret
= _lttng_stream_packet_context_declare(session
);
2923 ret
= _lttng_event_header_declare(session
);
2928 list_for_each_entry(chan
, &session
->chan
, list
) {
2929 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2934 list_for_each_entry(event
, &session
->events
, list
) {
2935 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2939 session
->metadata_dumped
= 1;
2941 lttng_metadata_end(session
);
2946 * lttng_transport_register - LTT transport registration
2947 * @transport: transport structure
2949 * Registers a transport which can be used as output to extract the data out of
2950 * LTTng. The module calling this registration function must ensure that no
2951 * trap-inducing code will be executed by the transport functions. E.g.
2952 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2953 * is made visible to the transport function. This registration acts as a
2954 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2955 * after its registration must it synchronize the TLBs.
2957 void lttng_transport_register(struct lttng_transport
*transport
)
2960 * Make sure no page fault can be triggered by the module about to be
2961 * registered. We deal with this here so we don't have to call
2962 * vmalloc_sync_mappings() in each module's init.
2964 wrapper_vmalloc_sync_mappings();
2966 mutex_lock(&sessions_mutex
);
2967 list_add_tail(&transport
->node
, <tng_transport_list
);
2968 mutex_unlock(&sessions_mutex
);
2970 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2973 * lttng_transport_unregister - LTT transport unregistration
2974 * @transport: transport structure
2976 void lttng_transport_unregister(struct lttng_transport
*transport
)
2978 mutex_lock(&sessions_mutex
);
2979 list_del(&transport
->node
);
2980 mutex_unlock(&sessions_mutex
);
2982 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2984 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2986 enum cpuhp_state lttng_hp_prepare
;
2987 enum cpuhp_state lttng_hp_online
;
2989 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2991 struct lttng_cpuhp_node
*lttng_node
;
2993 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2994 switch (lttng_node
->component
) {
2995 case LTTNG_RING_BUFFER_FRONTEND
:
2997 case LTTNG_RING_BUFFER_BACKEND
:
2998 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2999 case LTTNG_RING_BUFFER_ITER
:
3001 case LTTNG_CONTEXT_PERF_COUNTERS
:
3008 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
3010 struct lttng_cpuhp_node
*lttng_node
;
3012 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3013 switch (lttng_node
->component
) {
3014 case LTTNG_RING_BUFFER_FRONTEND
:
3015 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
3016 case LTTNG_RING_BUFFER_BACKEND
:
3018 case LTTNG_RING_BUFFER_ITER
:
3020 case LTTNG_CONTEXT_PERF_COUNTERS
:
3021 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
3027 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
3029 struct lttng_cpuhp_node
*lttng_node
;
3031 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3032 switch (lttng_node
->component
) {
3033 case LTTNG_RING_BUFFER_FRONTEND
:
3034 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
3035 case LTTNG_RING_BUFFER_BACKEND
:
3037 case LTTNG_RING_BUFFER_ITER
:
3038 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
3039 case LTTNG_CONTEXT_PERF_COUNTERS
:
3040 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
3046 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
3048 struct lttng_cpuhp_node
*lttng_node
;
3050 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3051 switch (lttng_node
->component
) {
3052 case LTTNG_RING_BUFFER_FRONTEND
:
3053 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
3054 case LTTNG_RING_BUFFER_BACKEND
:
3056 case LTTNG_RING_BUFFER_ITER
:
3058 case LTTNG_CONTEXT_PERF_COUNTERS
:
3065 static int __init
lttng_init_cpu_hotplug(void)
3069 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
3070 lttng_hotplug_prepare
,
3071 lttng_hotplug_dead
);
3075 lttng_hp_prepare
= ret
;
3076 lttng_rb_set_hp_prepare(ret
);
3078 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
3079 lttng_hotplug_online
,
3080 lttng_hotplug_offline
);
3082 cpuhp_remove_multi_state(lttng_hp_prepare
);
3083 lttng_hp_prepare
= 0;
3086 lttng_hp_online
= ret
;
3087 lttng_rb_set_hp_online(ret
);
3092 static void __exit
lttng_exit_cpu_hotplug(void)
3094 lttng_rb_set_hp_online(0);
3095 cpuhp_remove_multi_state(lttng_hp_online
);
3096 lttng_rb_set_hp_prepare(0);
3097 cpuhp_remove_multi_state(lttng_hp_prepare
);
3100 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3101 static int lttng_init_cpu_hotplug(void)
3105 static void lttng_exit_cpu_hotplug(void)
3108 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3111 static int __init
lttng_events_init(void)
3115 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
3118 ret
= wrapper_get_pfnblock_flags_mask_init();
3121 ret
= wrapper_get_pageblock_flags_mask_init();
3124 ret
= lttng_probes_init();
3127 ret
= lttng_context_init();
3130 ret
= lttng_tracepoint_init();
3133 event_cache
= KMEM_CACHE(lttng_event
, 0);
3138 ret
= lttng_abi_init();
3141 ret
= lttng_logger_init();
3144 ret
= lttng_init_cpu_hotplug();
3147 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3148 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3149 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3150 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3151 LTTNG_MODULES_EXTRAVERSION
,
3153 #ifdef LTTNG_EXTRA_VERSION_GIT
3154 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3158 #ifdef LTTNG_EXTRA_VERSION_NAME
3159 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3166 lttng_logger_exit();
3170 kmem_cache_destroy(event_cache
);
3172 lttng_tracepoint_exit();
3174 lttng_context_exit();
3175 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3176 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3177 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3178 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3179 LTTNG_MODULES_EXTRAVERSION
,
3181 #ifdef LTTNG_EXTRA_VERSION_GIT
3182 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3186 #ifdef LTTNG_EXTRA_VERSION_NAME
3187 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3194 module_init(lttng_events_init
);
3196 static void __exit
lttng_events_exit(void)
3198 struct lttng_session
*session
, *tmpsession
;
3200 lttng_exit_cpu_hotplug();
3201 lttng_logger_exit();
3203 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3204 lttng_session_destroy(session
);
3205 kmem_cache_destroy(event_cache
);
3206 lttng_tracepoint_exit();
3207 lttng_context_exit();
3208 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3209 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3210 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3211 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3212 LTTNG_MODULES_EXTRAVERSION
,
3214 #ifdef LTTNG_EXTRA_VERSION_GIT
3215 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3219 #ifdef LTTNG_EXTRA_VERSION_NAME
3220 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3226 module_exit(lttng_events_exit
);
3228 #include <generated/patches.h>
3229 #ifdef LTTNG_EXTRA_VERSION_GIT
3230 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3232 #ifdef LTTNG_EXTRA_VERSION_NAME
3233 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3235 MODULE_LICENSE("GPL and additional rights");
3236 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3237 MODULE_DESCRIPTION("LTTng tracer");
3238 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3239 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3240 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3241 LTTNG_MODULES_EXTRAVERSION
);