1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
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 <wrapper/compiler_attributes.h>
45 #include <wrapper/ringbuffer/backend.h>
46 #include <wrapper/ringbuffer/frontend.h>
47 #include <wrapper/time.h>
49 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,16,0))
50 #include <linux/stdarg.h>
55 #define METADATA_CACHE_DEFAULT_SIZE 4096
57 static LIST_HEAD(sessions
);
58 static LIST_HEAD(lttng_transport_list
);
60 * Protect the sessions and metadata caches.
62 static DEFINE_MUTEX(sessions_mutex
);
63 static struct kmem_cache
*event_cache
;
65 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
66 static void lttng_session_sync_enablers(struct lttng_session
*session
);
67 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
69 static void _lttng_event_destroy(struct lttng_event
*event
);
70 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
71 static int _lttng_event_unregister(struct lttng_event
*event
);
73 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
74 struct lttng_channel
*chan
,
75 struct lttng_event
*event
);
77 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
79 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
81 int _lttng_field_statedump(struct lttng_session
*session
,
82 const struct lttng_event_field
*field
,
85 void synchronize_trace(void)
87 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \
88 LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0))
94 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0))
95 #ifdef CONFIG_PREEMPT_RT_FULL
98 #else /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */
99 #ifdef CONFIG_PREEMPT_RT
102 #endif /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */
105 void lttng_lock_sessions(void)
107 mutex_lock(&sessions_mutex
);
110 void lttng_unlock_sessions(void)
112 mutex_unlock(&sessions_mutex
);
116 * Called with sessions lock held.
118 int lttng_session_active(void)
120 struct lttng_session
*iter
;
122 list_for_each_entry(iter
, &sessions
, list
) {
129 struct lttng_session
*lttng_session_create(void)
131 struct lttng_session
*session
;
132 struct lttng_metadata_cache
*metadata_cache
;
135 mutex_lock(&sessions_mutex
);
136 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
139 INIT_LIST_HEAD(&session
->chan
);
140 INIT_LIST_HEAD(&session
->events
);
141 lttng_guid_gen(&session
->uuid
);
143 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
146 goto err_free_session
;
147 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
148 if (!metadata_cache
->data
)
150 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
151 kref_init(&metadata_cache
->refcount
);
152 mutex_init(&metadata_cache
->lock
);
153 session
->metadata_cache
= metadata_cache
;
154 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
155 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
156 sizeof(metadata_cache
->uuid
));
157 INIT_LIST_HEAD(&session
->enablers_head
);
158 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
159 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
160 list_add(&session
->list
, &sessions
);
161 session
->pid_tracker
.session
= session
;
162 session
->pid_tracker
.tracker_type
= TRACKER_PID
;
163 session
->vpid_tracker
.session
= session
;
164 session
->vpid_tracker
.tracker_type
= TRACKER_VPID
;
165 session
->uid_tracker
.session
= session
;
166 session
->uid_tracker
.tracker_type
= TRACKER_UID
;
167 session
->vuid_tracker
.session
= session
;
168 session
->vuid_tracker
.tracker_type
= TRACKER_VUID
;
169 session
->gid_tracker
.session
= session
;
170 session
->gid_tracker
.tracker_type
= TRACKER_GID
;
171 session
->vgid_tracker
.session
= session
;
172 session
->vgid_tracker
.tracker_type
= TRACKER_VGID
;
173 mutex_unlock(&sessions_mutex
);
177 kfree(metadata_cache
);
179 lttng_kvfree(session
);
181 mutex_unlock(&sessions_mutex
);
185 void metadata_cache_destroy(struct kref
*kref
)
187 struct lttng_metadata_cache
*cache
=
188 container_of(kref
, struct lttng_metadata_cache
, refcount
);
193 void lttng_session_destroy(struct lttng_session
*session
)
195 struct lttng_channel
*chan
, *tmpchan
;
196 struct lttng_event
*event
, *tmpevent
;
197 struct lttng_metadata_stream
*metadata_stream
;
198 struct lttng_enabler
*enabler
, *tmpenabler
;
201 mutex_lock(&sessions_mutex
);
202 WRITE_ONCE(session
->active
, 0);
203 list_for_each_entry(chan
, &session
->chan
, list
) {
204 ret
= lttng_syscalls_unregister(chan
);
207 list_for_each_entry(event
, &session
->events
, list
) {
208 ret
= _lttng_event_unregister(event
);
211 synchronize_trace(); /* Wait for in-flight events to complete */
212 list_for_each_entry(chan
, &session
->chan
, list
) {
213 ret
= lttng_syscalls_destroy(chan
);
216 list_for_each_entry_safe(enabler
, tmpenabler
,
217 &session
->enablers_head
, node
)
218 lttng_enabler_destroy(enabler
);
219 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
220 _lttng_event_destroy(event
);
221 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
222 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
223 _lttng_channel_destroy(chan
);
225 mutex_lock(&session
->metadata_cache
->lock
);
226 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
227 _lttng_metadata_channel_hangup(metadata_stream
);
228 mutex_unlock(&session
->metadata_cache
->lock
);
229 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
230 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
231 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
232 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
233 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
234 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
235 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
236 list_del(&session
->list
);
237 mutex_unlock(&sessions_mutex
);
238 lttng_kvfree(session
);
241 int lttng_session_statedump(struct lttng_session
*session
)
245 mutex_lock(&sessions_mutex
);
246 ret
= lttng_statedump_start(session
);
247 mutex_unlock(&sessions_mutex
);
251 int lttng_session_enable(struct lttng_session
*session
)
254 struct lttng_channel
*chan
;
256 mutex_lock(&sessions_mutex
);
257 if (session
->active
) {
262 /* Set transient enabler state to "enabled" */
265 /* We need to sync enablers with session before activation. */
266 lttng_session_sync_enablers(session
);
269 * Snapshot the number of events per channel to know the type of header
272 list_for_each_entry(chan
, &session
->chan
, list
) {
273 if (chan
->header_type
)
274 continue; /* don't change it if session stop/restart */
275 if (chan
->free_event_id
< 31)
276 chan
->header_type
= 1; /* compact */
278 chan
->header_type
= 2; /* large */
281 /* Clear each stream's quiescent state. */
282 list_for_each_entry(chan
, &session
->chan
, list
) {
283 if (chan
->channel_type
!= METADATA_CHANNEL
)
284 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
287 WRITE_ONCE(session
->active
, 1);
288 WRITE_ONCE(session
->been_active
, 1);
289 ret
= _lttng_session_metadata_statedump(session
);
291 WRITE_ONCE(session
->active
, 0);
294 ret
= lttng_statedump_start(session
);
296 WRITE_ONCE(session
->active
, 0);
298 mutex_unlock(&sessions_mutex
);
302 int lttng_session_disable(struct lttng_session
*session
)
305 struct lttng_channel
*chan
;
307 mutex_lock(&sessions_mutex
);
308 if (!session
->active
) {
312 WRITE_ONCE(session
->active
, 0);
314 /* Set transient enabler state to "disabled" */
316 lttng_session_sync_enablers(session
);
318 /* Set each stream's quiescent state. */
319 list_for_each_entry(chan
, &session
->chan
, list
) {
320 if (chan
->channel_type
!= METADATA_CHANNEL
)
321 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
324 mutex_unlock(&sessions_mutex
);
328 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
331 struct lttng_channel
*chan
;
332 struct lttng_event
*event
;
333 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
334 struct lttng_metadata_stream
*stream
;
336 mutex_lock(&sessions_mutex
);
337 if (!session
->active
) {
342 mutex_lock(&cache
->lock
);
343 memset(cache
->data
, 0, cache
->cache_alloc
);
344 cache
->metadata_written
= 0;
346 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
347 stream
->metadata_out
= 0;
348 stream
->metadata_in
= 0;
350 mutex_unlock(&cache
->lock
);
352 session
->metadata_dumped
= 0;
353 list_for_each_entry(chan
, &session
->chan
, list
) {
354 chan
->metadata_dumped
= 0;
357 list_for_each_entry(event
, &session
->events
, list
) {
358 event
->metadata_dumped
= 0;
361 ret
= _lttng_session_metadata_statedump(session
);
364 mutex_unlock(&sessions_mutex
);
368 int lttng_channel_enable(struct lttng_channel
*channel
)
372 mutex_lock(&sessions_mutex
);
373 if (channel
->channel_type
== METADATA_CHANNEL
) {
377 if (channel
->enabled
) {
381 /* Set transient enabler state to "enabled" */
383 lttng_session_sync_enablers(channel
->session
);
384 /* Set atomically the state to "enabled" */
385 WRITE_ONCE(channel
->enabled
, 1);
387 mutex_unlock(&sessions_mutex
);
391 int lttng_channel_disable(struct lttng_channel
*channel
)
395 mutex_lock(&sessions_mutex
);
396 if (channel
->channel_type
== METADATA_CHANNEL
) {
400 if (!channel
->enabled
) {
404 /* Set atomically the state to "disabled" */
405 WRITE_ONCE(channel
->enabled
, 0);
406 /* Set transient enabler state to "enabled" */
408 lttng_session_sync_enablers(channel
->session
);
410 mutex_unlock(&sessions_mutex
);
414 int lttng_event_enable(struct lttng_event
*event
)
418 mutex_lock(&sessions_mutex
);
419 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
423 if (event
->enabled
) {
427 switch (event
->instrumentation
) {
428 case LTTNG_KERNEL_TRACEPOINT
:
429 case LTTNG_KERNEL_SYSCALL
:
432 case LTTNG_KERNEL_KPROBE
:
433 case LTTNG_KERNEL_UPROBE
:
434 case LTTNG_KERNEL_NOOP
:
435 WRITE_ONCE(event
->enabled
, 1);
437 case LTTNG_KERNEL_KRETPROBE
:
438 ret
= lttng_kretprobes_event_enable_state(event
, 1);
440 case LTTNG_KERNEL_FUNCTION
:
447 mutex_unlock(&sessions_mutex
);
451 int lttng_event_disable(struct lttng_event
*event
)
455 mutex_lock(&sessions_mutex
);
456 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
460 if (!event
->enabled
) {
464 switch (event
->instrumentation
) {
465 case LTTNG_KERNEL_TRACEPOINT
:
466 case LTTNG_KERNEL_SYSCALL
:
469 case LTTNG_KERNEL_KPROBE
:
470 case LTTNG_KERNEL_UPROBE
:
471 case LTTNG_KERNEL_NOOP
:
472 WRITE_ONCE(event
->enabled
, 0);
474 case LTTNG_KERNEL_KRETPROBE
:
475 ret
= lttng_kretprobes_event_enable_state(event
, 0);
477 case LTTNG_KERNEL_FUNCTION
:
484 mutex_unlock(&sessions_mutex
);
488 static struct lttng_transport
*lttng_transport_find(const char *name
)
490 struct lttng_transport
*transport
;
492 list_for_each_entry(transport
, <tng_transport_list
, node
) {
493 if (!strcmp(transport
->name
, name
))
499 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
500 const char *transport_name
,
502 size_t subbuf_size
, size_t num_subbuf
,
503 unsigned int switch_timer_interval
,
504 unsigned int read_timer_interval
,
505 enum channel_type channel_type
)
507 struct lttng_channel
*chan
;
508 struct lttng_transport
*transport
= NULL
;
510 mutex_lock(&sessions_mutex
);
511 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
512 goto active
; /* Refuse to add channel to active session */
513 transport
= lttng_transport_find(transport_name
);
515 printk(KERN_WARNING
"LTTng transport %s not found\n",
519 if (!try_module_get(transport
->owner
)) {
520 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
523 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
526 chan
->session
= session
;
527 chan
->id
= session
->free_chan_id
++;
528 chan
->ops
= &transport
->ops
;
530 * Note: the channel creation op already writes into the packet
531 * headers. Therefore the "chan" information used as input
532 * should be already accessible.
534 chan
->chan
= transport
->ops
.channel_create(transport_name
,
535 chan
, buf_addr
, subbuf_size
, num_subbuf
,
536 switch_timer_interval
, read_timer_interval
);
541 chan
->transport
= transport
;
542 chan
->channel_type
= channel_type
;
543 list_add(&chan
->list
, &session
->chan
);
544 mutex_unlock(&sessions_mutex
);
551 module_put(transport
->owner
);
554 mutex_unlock(&sessions_mutex
);
559 * Only used internally at session destruction for per-cpu channels, and
560 * when metadata channel is released.
561 * Needs to be called with sessions mutex held.
564 void _lttng_channel_destroy(struct lttng_channel
*chan
)
566 chan
->ops
->channel_destroy(chan
->chan
);
567 module_put(chan
->transport
->owner
);
568 list_del(&chan
->list
);
569 lttng_destroy_context(chan
->ctx
);
573 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
575 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
577 /* Protect the metadata cache with the sessions_mutex. */
578 mutex_lock(&sessions_mutex
);
579 _lttng_channel_destroy(chan
);
580 mutex_unlock(&sessions_mutex
);
582 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
585 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
587 stream
->finalized
= 1;
588 wake_up_interruptible(&stream
->read_wait
);
592 * Supports event creation while tracing session is active.
593 * Needs to be called with sessions mutex held.
595 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
596 struct lttng_kernel_event
*event_param
,
598 const struct lttng_event_desc
*event_desc
,
599 enum lttng_kernel_instrumentation itype
)
601 struct lttng_session
*session
= chan
->session
;
602 struct lttng_event
*event
;
603 const char *event_name
;
604 struct hlist_head
*head
;
609 if (chan
->free_event_id
== -1U) {
615 case LTTNG_KERNEL_TRACEPOINT
:
616 event_name
= event_desc
->name
;
618 case LTTNG_KERNEL_KPROBE
:
619 case LTTNG_KERNEL_UPROBE
:
620 case LTTNG_KERNEL_KRETPROBE
:
621 case LTTNG_KERNEL_NOOP
:
622 case LTTNG_KERNEL_SYSCALL
:
623 event_name
= event_param
->name
;
625 case LTTNG_KERNEL_FUNCTION
:
632 name_len
= strlen(event_name
);
633 hash
= jhash(event_name
, name_len
, 0);
634 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
635 lttng_hlist_for_each_entry(event
, head
, hlist
) {
636 WARN_ON_ONCE(!event
->desc
);
637 if (!strncmp(event
->desc
->name
, event_name
,
638 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
639 && chan
== event
->chan
) {
645 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
651 event
->filter
= filter
;
652 event
->id
= chan
->free_event_id
++;
653 event
->instrumentation
= itype
;
654 event
->evtype
= LTTNG_TYPE_EVENT
;
655 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
656 INIT_LIST_HEAD(&event
->enablers_ref_head
);
659 case LTTNG_KERNEL_TRACEPOINT
:
660 /* Event will be enabled by enabler sync. */
662 event
->registered
= 0;
663 event
->desc
= lttng_event_get(event_name
);
668 /* Populate lttng_event structure before event registration. */
671 case LTTNG_KERNEL_KPROBE
:
673 * Needs to be explicitly enabled after creation, since
674 * we may want to apply filters.
677 event
->registered
= 1;
679 * Populate lttng_event structure before event
683 ret
= lttng_kprobes_register(event_name
,
684 event_param
->u
.kprobe
.symbol_name
,
685 event_param
->u
.kprobe
.offset
,
686 event_param
->u
.kprobe
.addr
,
692 ret
= try_module_get(event
->desc
->owner
);
695 case LTTNG_KERNEL_KRETPROBE
:
697 struct lttng_event
*event_return
;
699 /* kretprobe defines 2 events */
701 * Needs to be explicitly enabled after creation, since
702 * we may want to apply filters.
705 event
->registered
= 1;
707 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
712 event_return
->chan
= chan
;
713 event_return
->filter
= filter
;
714 event_return
->id
= chan
->free_event_id
++;
715 event_return
->enabled
= 0;
716 event_return
->registered
= 1;
717 event_return
->instrumentation
= itype
;
718 INIT_LIST_HEAD(&event_return
->bytecode_runtime_head
);
719 INIT_LIST_HEAD(&event_return
->enablers_ref_head
);
721 * Populate lttng_event structure before kretprobe registration.
724 ret
= lttng_kretprobes_register(event_name
,
725 event_param
->u
.kretprobe
.symbol_name
,
726 event_param
->u
.kretprobe
.offset
,
727 event_param
->u
.kretprobe
.addr
,
728 event
, event_return
);
730 kmem_cache_free(event_cache
, event_return
);
734 /* Take 2 refs on the module: one per event. */
735 ret
= try_module_get(event
->desc
->owner
);
737 ret
= try_module_get(event
->desc
->owner
);
739 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
741 WARN_ON_ONCE(ret
> 0);
743 kmem_cache_free(event_cache
, event_return
);
744 module_put(event
->desc
->owner
);
745 module_put(event
->desc
->owner
);
746 goto statedump_error
;
748 list_add(&event_return
->list
, &chan
->session
->events
);
751 case LTTNG_KERNEL_NOOP
:
752 case LTTNG_KERNEL_SYSCALL
:
754 * Needs to be explicitly enabled after creation, since
755 * we may want to apply filters.
758 event
->registered
= 0;
759 event
->desc
= event_desc
;
760 switch (event_param
->u
.syscall
.entryexit
) {
761 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
764 case LTTNG_KERNEL_SYSCALL_ENTRY
:
765 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
767 case LTTNG_KERNEL_SYSCALL_EXIT
:
768 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
771 switch (event_param
->u
.syscall
.abi
) {
772 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
775 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
776 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
778 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
779 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
787 case LTTNG_KERNEL_UPROBE
:
789 * Needs to be explicitly enabled after creation, since
790 * we may want to apply filters.
793 event
->registered
= 1;
796 * Populate lttng_event structure before event
801 ret
= lttng_uprobes_register(event_param
->name
,
802 event_param
->u
.uprobe
.fd
,
806 ret
= try_module_get(event
->desc
->owner
);
809 case LTTNG_KERNEL_FUNCTION
:
816 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
817 WARN_ON_ONCE(ret
> 0);
819 goto statedump_error
;
821 hlist_add_head(&event
->hlist
, head
);
822 list_add(&event
->list
, &chan
->session
->events
);
826 /* If a statedump error occurs, events will not be readable. */
828 kmem_cache_free(event_cache
, event
);
836 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
837 struct lttng_kernel_event
*event_param
,
839 const struct lttng_event_desc
*event_desc
,
840 enum lttng_kernel_instrumentation itype
)
842 struct lttng_event
*event
;
844 mutex_lock(&sessions_mutex
);
845 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
847 mutex_unlock(&sessions_mutex
);
851 /* Only used for tracepoints for now. */
853 void register_event(struct lttng_event
*event
)
855 const struct lttng_event_desc
*desc
;
858 if (event
->registered
)
862 switch (event
->instrumentation
) {
863 case LTTNG_KERNEL_TRACEPOINT
:
864 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
865 desc
->probe_callback
,
868 case LTTNG_KERNEL_SYSCALL
:
869 ret
= lttng_syscall_filter_enable(event
->chan
, event
);
871 case LTTNG_KERNEL_KPROBE
:
872 case LTTNG_KERNEL_UPROBE
:
873 case LTTNG_KERNEL_KRETPROBE
:
874 case LTTNG_KERNEL_NOOP
:
877 case LTTNG_KERNEL_FUNCTION
:
883 event
->registered
= 1;
887 * Only used internally at session destruction.
889 int _lttng_event_unregister(struct lttng_event
*event
)
891 const struct lttng_event_desc
*desc
;
894 if (!event
->registered
)
898 switch (event
->instrumentation
) {
899 case LTTNG_KERNEL_TRACEPOINT
:
900 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
901 event
->desc
->probe_callback
,
904 case LTTNG_KERNEL_KPROBE
:
905 lttng_kprobes_unregister(event
);
908 case LTTNG_KERNEL_KRETPROBE
:
909 lttng_kretprobes_unregister(event
);
912 case LTTNG_KERNEL_SYSCALL
:
913 ret
= lttng_syscall_filter_disable(event
->chan
, event
);
915 case LTTNG_KERNEL_NOOP
:
918 case LTTNG_KERNEL_UPROBE
:
919 lttng_uprobes_unregister(event
);
922 case LTTNG_KERNEL_FUNCTION
:
928 event
->registered
= 0;
933 * Only used internally at session destruction.
936 void _lttng_event_destroy(struct lttng_event
*event
)
938 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
940 switch (event
->instrumentation
) {
941 case LTTNG_KERNEL_TRACEPOINT
:
942 lttng_event_put(event
->desc
);
944 case LTTNG_KERNEL_KPROBE
:
945 module_put(event
->desc
->owner
);
946 lttng_kprobes_destroy_private(event
);
948 case LTTNG_KERNEL_KRETPROBE
:
949 module_put(event
->desc
->owner
);
950 lttng_kretprobes_destroy_private(event
);
952 case LTTNG_KERNEL_NOOP
:
953 case LTTNG_KERNEL_SYSCALL
:
955 case LTTNG_KERNEL_UPROBE
:
956 module_put(event
->desc
->owner
);
957 lttng_uprobes_destroy_private(event
);
959 case LTTNG_KERNEL_FUNCTION
:
964 list_del(&event
->list
);
965 lttng_destroy_context(event
->ctx
);
966 lttng_free_event_filter_runtime(event
);
967 /* Free event enabler refs */
968 list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
969 &event
->enablers_ref_head
, node
)
971 kmem_cache_free(event_cache
, event
);
974 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
975 enum tracker_type tracker_type
)
977 switch (tracker_type
) {
979 return &session
->pid_tracker
;
981 return &session
->vpid_tracker
;
983 return &session
->uid_tracker
;
985 return &session
->vuid_tracker
;
987 return &session
->gid_tracker
;
989 return &session
->vgid_tracker
;
996 int lttng_session_track_id(struct lttng_session
*session
,
997 enum tracker_type tracker_type
, int id
)
999 struct lttng_id_tracker
*tracker
;
1002 tracker
= get_tracker(session
, tracker_type
);
1007 mutex_lock(&sessions_mutex
);
1009 /* track all ids: destroy tracker. */
1010 lttng_id_tracker_destroy(tracker
, true);
1013 ret
= lttng_id_tracker_add(tracker
, id
);
1015 mutex_unlock(&sessions_mutex
);
1019 int lttng_session_untrack_id(struct lttng_session
*session
,
1020 enum tracker_type tracker_type
, int id
)
1022 struct lttng_id_tracker
*tracker
;
1025 tracker
= get_tracker(session
, tracker_type
);
1030 mutex_lock(&sessions_mutex
);
1032 /* untrack all ids: replace by empty tracker. */
1033 ret
= lttng_id_tracker_empty_set(tracker
);
1035 ret
= lttng_id_tracker_del(tracker
, id
);
1037 mutex_unlock(&sessions_mutex
);
1042 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
1044 struct lttng_id_tracker
*id_tracker
= m
->private;
1045 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1046 struct lttng_id_hash_node
*e
;
1049 mutex_lock(&sessions_mutex
);
1051 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1052 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1054 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1060 /* ID tracker disabled. */
1061 if (iter
>= *pos
&& iter
== 0) {
1062 return id_tracker_p
; /* empty tracker */
1070 /* Called with sessions_mutex held. */
1072 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1074 struct lttng_id_tracker
*id_tracker
= m
->private;
1075 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1076 struct lttng_id_hash_node
*e
;
1081 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1082 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1084 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1085 if (iter
++ >= *ppos
)
1090 /* ID tracker disabled. */
1091 if (iter
>= *ppos
&& iter
== 0)
1092 return p
; /* empty tracker */
1101 void id_list_stop(struct seq_file
*m
, void *p
)
1103 mutex_unlock(&sessions_mutex
);
1107 int id_list_show(struct seq_file
*m
, void *p
)
1109 struct lttng_id_tracker
*id_tracker
= m
->private;
1110 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1113 if (p
== id_tracker_p
) {
1114 /* Tracker disabled. */
1117 const struct lttng_id_hash_node
*e
= p
;
1119 id
= lttng_id_tracker_get_node_id(e
);
1121 switch (id_tracker
->tracker_type
) {
1123 seq_printf(m
, "process { pid = %d; };\n", id
);
1126 seq_printf(m
, "process { vpid = %d; };\n", id
);
1129 seq_printf(m
, "user { uid = %d; };\n", id
);
1132 seq_printf(m
, "user { vuid = %d; };\n", id
);
1135 seq_printf(m
, "group { gid = %d; };\n", id
);
1138 seq_printf(m
, "group { vgid = %d; };\n", id
);
1141 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1147 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1148 .start
= id_list_start
,
1149 .next
= id_list_next
,
1150 .stop
= id_list_stop
,
1151 .show
= id_list_show
,
1155 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1157 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1161 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1163 struct seq_file
*m
= file
->private_data
;
1164 struct lttng_id_tracker
*id_tracker
= m
->private;
1167 WARN_ON_ONCE(!id_tracker
);
1168 ret
= seq_release(inode
, file
);
1170 fput(id_tracker
->session
->file
);
1174 const struct file_operations lttng_tracker_ids_list_fops
= {
1175 .owner
= THIS_MODULE
,
1176 .open
= lttng_tracker_ids_list_open
,
1178 .llseek
= seq_lseek
,
1179 .release
= lttng_tracker_ids_list_release
,
1182 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1183 enum tracker_type tracker_type
)
1185 struct file
*tracker_ids_list_file
;
1189 file_fd
= lttng_get_unused_fd();
1195 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1196 <tng_tracker_ids_list_fops
,
1198 if (IS_ERR(tracker_ids_list_file
)) {
1199 ret
= PTR_ERR(tracker_ids_list_file
);
1202 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1204 goto refcount_error
;
1206 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1209 m
= tracker_ids_list_file
->private_data
;
1211 m
->private = get_tracker(session
, tracker_type
);
1212 BUG_ON(!m
->private);
1213 fd_install(file_fd
, tracker_ids_list_file
);
1218 atomic_long_dec(&session
->file
->f_count
);
1220 fput(tracker_ids_list_file
);
1222 put_unused_fd(file_fd
);
1228 * Enabler management.
1231 int lttng_match_enabler_star_glob(const char *desc_name
,
1232 const char *pattern
)
1234 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1235 desc_name
, LTTNG_SIZE_MAX
))
1241 int lttng_match_enabler_name(const char *desc_name
,
1244 if (strcmp(desc_name
, name
))
1250 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1251 struct lttng_enabler
*enabler
)
1253 const char *desc_name
, *enabler_name
;
1254 bool compat
= false, entry
= false;
1256 enabler_name
= enabler
->event_param
.name
;
1257 switch (enabler
->event_param
.instrumentation
) {
1258 case LTTNG_KERNEL_TRACEPOINT
:
1259 desc_name
= desc
->name
;
1260 switch (enabler
->type
) {
1261 case LTTNG_ENABLER_STAR_GLOB
:
1262 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1263 case LTTNG_ENABLER_NAME
:
1264 return lttng_match_enabler_name(desc_name
, enabler_name
);
1269 case LTTNG_KERNEL_SYSCALL
:
1270 desc_name
= desc
->name
;
1271 if (!strncmp(desc_name
, "compat_", strlen("compat_"))) {
1272 desc_name
+= strlen("compat_");
1275 if (!strncmp(desc_name
, "syscall_exit_",
1276 strlen("syscall_exit_"))) {
1277 desc_name
+= strlen("syscall_exit_");
1278 } else if (!strncmp(desc_name
, "syscall_entry_",
1279 strlen("syscall_entry_"))) {
1280 desc_name
+= strlen("syscall_entry_");
1286 switch (enabler
->event_param
.u
.syscall
.entryexit
) {
1287 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1289 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1293 case LTTNG_KERNEL_SYSCALL_EXIT
:
1300 switch (enabler
->event_param
.u
.syscall
.abi
) {
1301 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1303 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1307 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1314 switch (enabler
->event_param
.u
.syscall
.match
) {
1315 case LTTNG_SYSCALL_MATCH_NAME
:
1316 switch (enabler
->type
) {
1317 case LTTNG_ENABLER_STAR_GLOB
:
1318 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1319 case LTTNG_ENABLER_NAME
:
1320 return lttng_match_enabler_name(desc_name
, enabler_name
);
1325 case LTTNG_SYSCALL_MATCH_NR
:
1326 return -EINVAL
; /* Not implemented. */
1338 int lttng_event_match_enabler(struct lttng_event
*event
,
1339 struct lttng_enabler
*enabler
)
1341 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1343 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1344 && event
->chan
== enabler
->chan
)
1351 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1352 struct lttng_enabler
*enabler
)
1354 struct lttng_enabler_ref
*enabler_ref
;
1356 list_for_each_entry(enabler_ref
,
1357 &event
->enablers_ref_head
, node
) {
1358 if (enabler_ref
->ref
== enabler
)
1365 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1367 struct lttng_session
*session
= enabler
->chan
->session
;
1368 struct lttng_probe_desc
*probe_desc
;
1369 const struct lttng_event_desc
*desc
;
1371 struct list_head
*probe_list
;
1373 probe_list
= lttng_get_probe_list_head();
1375 * For each probe event, if we find that a probe event matches
1376 * our enabler, create an associated lttng_event if not
1379 list_for_each_entry(probe_desc
, probe_list
, head
) {
1380 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1382 struct hlist_head
*head
;
1383 const char *event_name
;
1386 struct lttng_event
*event
;
1388 desc
= probe_desc
->event_desc
[i
];
1389 if (!lttng_desc_match_enabler(desc
, enabler
))
1391 event_name
= desc
->name
;
1392 name_len
= strlen(event_name
);
1395 * Check if already created.
1397 hash
= jhash(event_name
, name_len
, 0);
1398 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1399 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1400 if (event
->desc
== desc
1401 && event
->chan
== enabler
->chan
)
1408 * We need to create an event for this
1411 event
= _lttng_event_create(enabler
->chan
,
1413 LTTNG_KERNEL_TRACEPOINT
);
1415 printk(KERN_INFO
"Unable to create event %s\n",
1416 probe_desc
->event_desc
[i
]->name
);
1423 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1427 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1432 * Create struct lttng_event if it is missing and present in the list of
1433 * tracepoint probes.
1434 * Should be called with sessions mutex held.
1437 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1439 switch (enabler
->event_param
.instrumentation
) {
1440 case LTTNG_KERNEL_TRACEPOINT
:
1441 lttng_create_tracepoint_if_missing(enabler
);
1443 case LTTNG_KERNEL_SYSCALL
:
1444 lttng_create_syscall_if_missing(enabler
);
1453 * Create events associated with an enabler (if not already present),
1454 * and add backward reference from the event to the enabler.
1455 * Should be called with sessions mutex held.
1458 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1460 struct lttng_channel
*chan
= enabler
->chan
;
1461 struct lttng_session
*session
= chan
->session
;
1462 struct lttng_event
*event
;
1464 if (enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
1465 enabler
->event_param
.u
.syscall
.entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
&&
1466 enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
1467 enabler
->event_param
.u
.syscall
.match
== LTTNG_SYSCALL_MATCH_NAME
&&
1468 !strcmp(enabler
->event_param
.name
, "*")) {
1469 if (enabler
->enabled
)
1470 WRITE_ONCE(chan
->syscall_all
, 1);
1472 WRITE_ONCE(chan
->syscall_all
, 0);
1475 /* First ensure that probe events are created for this enabler. */
1476 lttng_create_event_if_missing(enabler
);
1478 /* For each event matching enabler in session event list. */
1479 list_for_each_entry(event
, &session
->events
, list
) {
1480 struct lttng_enabler_ref
*enabler_ref
;
1482 if (!lttng_event_match_enabler(event
, enabler
))
1484 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1487 * If no backward ref, create it.
1488 * Add backward ref from event to enabler.
1490 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1493 enabler_ref
->ref
= enabler
;
1494 list_add(&enabler_ref
->node
,
1495 &event
->enablers_ref_head
);
1499 * Link filter bytecodes if not linked yet.
1501 lttng_enabler_event_link_bytecode(event
, enabler
);
1503 /* TODO: merge event context. */
1509 * Called at module load: connect the probe on all enablers matching
1511 * Called with sessions lock held.
1513 int lttng_fix_pending_events(void)
1515 struct lttng_session
*session
;
1517 list_for_each_entry(session
, &sessions
, list
)
1518 lttng_session_lazy_sync_enablers(session
);
1522 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1523 struct lttng_kernel_event
*event_param
,
1524 struct lttng_channel
*chan
)
1526 struct lttng_enabler
*enabler
;
1528 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1531 enabler
->type
= type
;
1532 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1533 memcpy(&enabler
->event_param
, event_param
,
1534 sizeof(enabler
->event_param
));
1535 enabler
->chan
= chan
;
1537 enabler
->enabled
= 0;
1538 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1539 mutex_lock(&sessions_mutex
);
1540 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1541 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1542 mutex_unlock(&sessions_mutex
);
1546 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1548 mutex_lock(&sessions_mutex
);
1549 enabler
->enabled
= 1;
1550 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1551 mutex_unlock(&sessions_mutex
);
1555 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1557 mutex_lock(&sessions_mutex
);
1558 enabler
->enabled
= 0;
1559 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1560 mutex_unlock(&sessions_mutex
);
1564 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1565 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1567 struct lttng_filter_bytecode_node
*bytecode_node
;
1568 uint32_t bytecode_len
;
1571 ret
= get_user(bytecode_len
, &bytecode
->len
);
1574 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1578 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1579 sizeof(*bytecode
) + bytecode_len
);
1582 bytecode_node
->enabler
= enabler
;
1583 /* Enforce length based on allocated size */
1584 bytecode_node
->bc
.len
= bytecode_len
;
1585 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1586 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1590 kfree(bytecode_node
);
1594 int lttng_event_add_callsite(struct lttng_event
*event
,
1595 struct lttng_kernel_event_callsite __user
*callsite
)
1598 switch (event
->instrumentation
) {
1599 case LTTNG_KERNEL_UPROBE
:
1600 return lttng_uprobes_add_callsite(event
, callsite
);
1606 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1607 struct lttng_kernel_context
*context_param
)
1613 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1615 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1617 /* Destroy filter bytecode */
1618 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1619 &enabler
->filter_bytecode_head
, node
) {
1623 /* Destroy contexts */
1624 lttng_destroy_context(enabler
->ctx
);
1626 list_del(&enabler
->node
);
1631 * lttng_session_sync_enablers should be called just before starting a
1633 * Should be called with sessions mutex held.
1636 void lttng_session_sync_enablers(struct lttng_session
*session
)
1638 struct lttng_enabler
*enabler
;
1639 struct lttng_event
*event
;
1641 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1642 lttng_enabler_ref_events(enabler
);
1644 * For each event, if at least one of its enablers is enabled,
1645 * and its channel and session transient states are enabled, we
1646 * enable the event, else we disable it.
1648 list_for_each_entry(event
, &session
->events
, list
) {
1649 struct lttng_enabler_ref
*enabler_ref
;
1650 struct lttng_bytecode_runtime
*runtime
;
1651 int enabled
= 0, has_enablers_without_bytecode
= 0;
1653 switch (event
->instrumentation
) {
1654 case LTTNG_KERNEL_TRACEPOINT
:
1655 case LTTNG_KERNEL_SYSCALL
:
1657 list_for_each_entry(enabler_ref
,
1658 &event
->enablers_ref_head
, node
) {
1659 if (enabler_ref
->ref
->enabled
) {
1666 /* Not handled with lazy sync. */
1670 * Enabled state is based on union of enablers, with
1671 * intesection of session and channel transient enable
1674 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1676 WRITE_ONCE(event
->enabled
, enabled
);
1678 * Sync tracepoint registration with event enabled
1682 register_event(event
);
1684 _lttng_event_unregister(event
);
1687 /* Check if has enablers without bytecode enabled */
1688 list_for_each_entry(enabler_ref
,
1689 &event
->enablers_ref_head
, node
) {
1690 if (enabler_ref
->ref
->enabled
1691 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1692 has_enablers_without_bytecode
= 1;
1696 event
->has_enablers_without_bytecode
=
1697 has_enablers_without_bytecode
;
1699 /* Enable filters */
1700 list_for_each_entry(runtime
,
1701 &event
->bytecode_runtime_head
, node
)
1702 lttng_filter_sync_state(runtime
);
1707 * Apply enablers to session events, adding events to session if need
1708 * be. It is required after each modification applied to an active
1709 * session, and right before session "start".
1710 * "lazy" sync means we only sync if required.
1711 * Should be called with sessions mutex held.
1714 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1716 /* We can skip if session is not active */
1717 if (!session
->active
)
1719 lttng_session_sync_enablers(session
);
1723 * Serialize at most one packet worth of metadata into a metadata
1725 * We grab the metadata cache mutex to get exclusive access to our metadata
1726 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1727 * allows us to do racy operations such as looking for remaining space left in
1728 * packet and write, since mutual exclusion protects us from concurrent writes.
1729 * Mutual exclusion on the metadata cache allow us to read the cache content
1730 * without racing against reallocation of the cache by updates.
1731 * Returns the number of bytes written in the channel, 0 if no data
1732 * was written and a negative value on error.
1734 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1735 struct channel
*chan
, bool *coherent
)
1737 struct lib_ring_buffer_ctx ctx
;
1739 size_t len
, reserve_len
;
1742 * Ensure we support mutiple get_next / put sequences followed by
1743 * put_next. The metadata cache lock protects reading the metadata
1744 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1745 * "flush" operations on the buffer invoked by different processes.
1746 * Moreover, since the metadata cache memory can be reallocated, we
1747 * need to have exclusive access against updates even though we only
1750 mutex_lock(&stream
->metadata_cache
->lock
);
1751 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1752 if (stream
->metadata_in
!= stream
->metadata_out
)
1755 /* Metadata regenerated, change the version. */
1756 if (stream
->metadata_cache
->version
!= stream
->version
)
1757 stream
->version
= stream
->metadata_cache
->version
;
1759 len
= stream
->metadata_cache
->metadata_written
-
1760 stream
->metadata_in
;
1763 reserve_len
= min_t(size_t,
1764 stream
->transport
->ops
.packet_avail_size(chan
),
1766 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1769 * If reservation failed, return an error to the caller.
1771 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1773 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1774 stream
->coherent
= false;
1777 stream
->transport
->ops
.event_write(&ctx
,
1778 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1780 stream
->transport
->ops
.event_commit(&ctx
);
1781 stream
->metadata_in
+= reserve_len
;
1782 if (reserve_len
< len
)
1783 stream
->coherent
= false;
1785 stream
->coherent
= true;
1790 *coherent
= stream
->coherent
;
1791 mutex_unlock(&stream
->metadata_cache
->lock
);
1796 void lttng_metadata_begin(struct lttng_session
*session
)
1798 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1799 mutex_lock(&session
->metadata_cache
->lock
);
1803 void lttng_metadata_end(struct lttng_session
*session
)
1805 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1806 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1807 struct lttng_metadata_stream
*stream
;
1809 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1810 wake_up_interruptible(&stream
->read_wait
);
1811 mutex_unlock(&session
->metadata_cache
->lock
);
1816 * Write the metadata to the metadata cache.
1817 * Must be called with sessions_mutex held.
1818 * The metadata cache lock protects us from concurrent read access from
1819 * thread outputting metadata content to ring buffer.
1820 * The content of the printf is printed as a single atomic metadata
1823 int lttng_metadata_printf(struct lttng_session
*session
,
1824 const char *fmt
, ...)
1830 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
1833 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1839 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1840 if (session
->metadata_cache
->metadata_written
+ len
>
1841 session
->metadata_cache
->cache_alloc
) {
1842 char *tmp_cache_realloc
;
1843 unsigned int tmp_cache_alloc_size
;
1845 tmp_cache_alloc_size
= max_t(unsigned int,
1846 session
->metadata_cache
->cache_alloc
+ len
,
1847 session
->metadata_cache
->cache_alloc
<< 1);
1848 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1849 if (!tmp_cache_realloc
)
1851 if (session
->metadata_cache
->data
) {
1852 memcpy(tmp_cache_realloc
,
1853 session
->metadata_cache
->data
,
1854 session
->metadata_cache
->cache_alloc
);
1855 vfree(session
->metadata_cache
->data
);
1858 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1859 session
->metadata_cache
->data
= tmp_cache_realloc
;
1861 memcpy(session
->metadata_cache
->data
+
1862 session
->metadata_cache
->metadata_written
,
1864 session
->metadata_cache
->metadata_written
+= len
;
1875 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1879 for (i
= 0; i
< nesting
; i
++) {
1882 ret
= lttng_metadata_printf(session
, " ");
1891 * Must be called with sessions_mutex held.
1894 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1895 const struct lttng_type
*type
,
1899 uint32_t i
, nr_fields
;
1901 ret
= print_tabs(session
, nesting
);
1904 ret
= lttng_metadata_printf(session
,
1908 nr_fields
= type
->u
._struct
.nr_fields
;
1909 for (i
= 0; i
< nr_fields
; i
++) {
1910 const struct lttng_event_field
*iter_field
;
1912 iter_field
= &type
->u
._struct
.fields
[i
];
1913 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1917 ret
= print_tabs(session
, nesting
);
1920 ret
= lttng_metadata_printf(session
,
1926 * Must be called with sessions_mutex held.
1929 int _lttng_struct_statedump(struct lttng_session
*session
,
1930 const struct lttng_event_field
*field
,
1935 ret
= _lttng_struct_type_statedump(session
,
1936 &field
->type
, nesting
);
1939 ret
= lttng_metadata_printf(session
,
1946 * Must be called with sessions_mutex held.
1949 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1950 const struct lttng_type
*type
,
1954 uint32_t i
, nr_choices
;
1956 ret
= print_tabs(session
, nesting
);
1959 ret
= lttng_metadata_printf(session
,
1960 "variant <_%s> {\n",
1961 type
->u
.variant
.tag_name
);
1964 nr_choices
= type
->u
.variant
.nr_choices
;
1965 for (i
= 0; i
< nr_choices
; i
++) {
1966 const struct lttng_event_field
*iter_field
;
1968 iter_field
= &type
->u
.variant
.choices
[i
];
1969 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1973 ret
= print_tabs(session
, nesting
);
1976 ret
= lttng_metadata_printf(session
,
1982 * Must be called with sessions_mutex held.
1985 int _lttng_variant_statedump(struct lttng_session
*session
,
1986 const struct lttng_event_field
*field
,
1991 ret
= _lttng_variant_type_statedump(session
,
1992 &field
->type
, nesting
);
1995 ret
= lttng_metadata_printf(session
,
2002 * Must be called with sessions_mutex held.
2005 int _lttng_array_compound_statedump(struct lttng_session
*session
,
2006 const struct lttng_event_field
*field
,
2010 const struct lttng_type
*elem_type
;
2012 /* Only array of structures and variants are currently supported. */
2013 elem_type
= field
->type
.u
.array_compound
.elem_type
;
2014 switch (elem_type
->atype
) {
2016 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
2021 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2028 ret
= lttng_metadata_printf(session
,
2031 field
->type
.u
.array_compound
.length
);
2036 * Must be called with sessions_mutex held.
2039 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
2040 const struct lttng_event_field
*field
,
2044 const char *length_name
;
2045 const struct lttng_type
*elem_type
;
2047 length_name
= field
->type
.u
.sequence_compound
.length_name
;
2049 /* Only array of structures and variants are currently supported. */
2050 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
2051 switch (elem_type
->atype
) {
2053 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
2058 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2065 ret
= lttng_metadata_printf(session
,
2073 * Must be called with sessions_mutex held.
2076 int _lttng_enum_statedump(struct lttng_session
*session
,
2077 const struct lttng_event_field
*field
,
2080 const struct lttng_enum_desc
*enum_desc
;
2081 const struct lttng_integer_type
*container_type
;
2083 unsigned int i
, nr_entries
;
2085 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
2086 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
2087 nr_entries
= enum_desc
->nr_entries
;
2089 ret
= print_tabs(session
, nesting
);
2092 ret
= lttng_metadata_printf(session
,
2093 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
2094 container_type
->size
,
2095 container_type
->alignment
,
2096 container_type
->signedness
,
2097 (container_type
->encoding
== lttng_encode_none
)
2099 : (container_type
->encoding
== lttng_encode_UTF8
)
2102 container_type
->base
,
2103 #if __BYTE_ORDER == __BIG_ENDIAN
2104 container_type
->reverse_byte_order
? " byte_order = le;" : ""
2106 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2111 /* Dump all entries */
2112 for (i
= 0; i
< nr_entries
; i
++) {
2113 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2116 ret
= print_tabs(session
, nesting
+ 1);
2119 ret
= lttng_metadata_printf(session
,
2123 len
= strlen(entry
->string
);
2124 /* Escape the character '"' */
2125 for (j
= 0; j
< len
; j
++) {
2126 char c
= entry
->string
[j
];
2130 ret
= lttng_metadata_printf(session
,
2134 ret
= lttng_metadata_printf(session
,
2138 ret
= lttng_metadata_printf(session
,
2145 ret
= lttng_metadata_printf(session
, "\"");
2149 if (entry
->options
.is_auto
) {
2150 ret
= lttng_metadata_printf(session
, ",\n");
2154 ret
= lttng_metadata_printf(session
,
2158 if (entry
->start
.signedness
)
2159 ret
= lttng_metadata_printf(session
,
2160 "%lld", (long long) entry
->start
.value
);
2162 ret
= lttng_metadata_printf(session
,
2163 "%llu", entry
->start
.value
);
2166 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2168 == entry
->end
.value
) {
2169 ret
= lttng_metadata_printf(session
,
2172 if (entry
->end
.signedness
) {
2173 ret
= lttng_metadata_printf(session
,
2175 (long long) entry
->end
.value
);
2177 ret
= lttng_metadata_printf(session
,
2186 ret
= print_tabs(session
, nesting
);
2189 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2196 * Must be called with sessions_mutex held.
2199 int _lttng_field_statedump(struct lttng_session
*session
,
2200 const struct lttng_event_field
*field
,
2205 switch (field
->type
.atype
) {
2207 ret
= print_tabs(session
, nesting
);
2210 ret
= lttng_metadata_printf(session
,
2211 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2212 field
->type
.u
.basic
.integer
.size
,
2213 field
->type
.u
.basic
.integer
.alignment
,
2214 field
->type
.u
.basic
.integer
.signedness
,
2215 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2217 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2220 field
->type
.u
.basic
.integer
.base
,
2221 #if __BYTE_ORDER == __BIG_ENDIAN
2222 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2224 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2229 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2232 case atype_array_bitfield
:
2234 const struct lttng_basic_type
*elem_type
;
2236 elem_type
= &field
->type
.u
.array
.elem_type
;
2237 if (field
->type
.u
.array
.elem_alignment
) {
2238 ret
= print_tabs(session
, nesting
);
2241 ret
= lttng_metadata_printf(session
,
2242 "struct { } align(%u) _%s_padding;\n",
2243 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2248 ret
= print_tabs(session
, nesting
);
2251 ret
= lttng_metadata_printf(session
,
2252 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2253 elem_type
->u
.basic
.integer
.size
,
2254 elem_type
->u
.basic
.integer
.alignment
,
2255 elem_type
->u
.basic
.integer
.signedness
,
2256 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2258 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2261 elem_type
->u
.basic
.integer
.base
,
2262 #if __BYTE_ORDER == __BIG_ENDIAN
2263 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2265 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2267 field
->name
, field
->type
.u
.array
.length
);
2270 case atype_sequence
:
2271 case atype_sequence_bitfield
:
2273 const struct lttng_basic_type
*elem_type
;
2274 const struct lttng_basic_type
*length_type
;
2276 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2277 length_type
= &field
->type
.u
.sequence
.length_type
;
2278 ret
= print_tabs(session
, nesting
);
2281 ret
= lttng_metadata_printf(session
,
2282 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2283 length_type
->u
.basic
.integer
.size
,
2284 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2285 length_type
->u
.basic
.integer
.signedness
,
2286 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2288 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2291 length_type
->u
.basic
.integer
.base
,
2292 #if __BYTE_ORDER == __BIG_ENDIAN
2293 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2295 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2301 if (field
->type
.u
.sequence
.elem_alignment
) {
2302 ret
= print_tabs(session
, nesting
);
2305 ret
= lttng_metadata_printf(session
,
2306 "struct { } align(%u) _%s_padding;\n",
2307 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2312 ret
= print_tabs(session
, nesting
);
2315 ret
= lttng_metadata_printf(session
,
2316 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2317 elem_type
->u
.basic
.integer
.size
,
2318 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2319 elem_type
->u
.basic
.integer
.signedness
,
2320 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2322 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2325 elem_type
->u
.basic
.integer
.base
,
2326 #if __BYTE_ORDER == __BIG_ENDIAN
2327 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2329 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2337 /* Default encoding is UTF8 */
2338 ret
= print_tabs(session
, nesting
);
2341 ret
= lttng_metadata_printf(session
,
2343 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2344 " { encoding = ASCII; }" : "",
2348 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2350 case atype_array_compound
:
2351 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2353 case atype_sequence_compound
:
2354 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2357 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2368 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2369 struct lttng_ctx
*ctx
)
2376 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2377 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2379 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2387 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2388 struct lttng_event
*event
)
2390 const struct lttng_event_desc
*desc
= event
->desc
;
2394 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2395 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2397 ret
= _lttng_field_statedump(session
, field
, 2);
2405 * Must be called with sessions_mutex held.
2406 * The entire event metadata is printed as a single atomic metadata
2410 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2411 struct lttng_channel
*chan
,
2412 struct lttng_event
*event
)
2416 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2418 if (chan
->channel_type
== METADATA_CHANNEL
)
2421 lttng_metadata_begin(session
);
2423 ret
= lttng_metadata_printf(session
,
2427 " stream_id = %u;\n",
2435 ret
= lttng_metadata_printf(session
,
2436 " context := struct {\n");
2440 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2444 ret
= lttng_metadata_printf(session
,
2450 ret
= lttng_metadata_printf(session
,
2451 " fields := struct {\n"
2456 ret
= _lttng_fields_metadata_statedump(session
, event
);
2461 * LTTng space reservation can only reserve multiples of the
2464 ret
= lttng_metadata_printf(session
,
2470 event
->metadata_dumped
= 1;
2472 lttng_metadata_end(session
);
2478 * Must be called with sessions_mutex held.
2479 * The entire channel metadata is printed as a single atomic metadata
2483 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2484 struct lttng_channel
*chan
)
2488 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2491 if (chan
->channel_type
== METADATA_CHANNEL
)
2494 lttng_metadata_begin(session
);
2496 WARN_ON_ONCE(!chan
->header_type
);
2497 ret
= lttng_metadata_printf(session
,
2500 " event.header := %s;\n"
2501 " packet.context := struct packet_context;\n",
2503 chan
->header_type
== 1 ? "struct event_header_compact" :
2504 "struct event_header_large");
2509 ret
= lttng_metadata_printf(session
,
2510 " event.context := struct {\n");
2514 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2518 ret
= lttng_metadata_printf(session
,
2524 ret
= lttng_metadata_printf(session
,
2527 chan
->metadata_dumped
= 1;
2529 lttng_metadata_end(session
);
2534 * Must be called with sessions_mutex held.
2537 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2539 return lttng_metadata_printf(session
,
2540 "struct packet_context {\n"
2541 " uint64_clock_monotonic_t timestamp_begin;\n"
2542 " uint64_clock_monotonic_t timestamp_end;\n"
2543 " uint64_t content_size;\n"
2544 " uint64_t packet_size;\n"
2545 " uint64_t packet_seq_num;\n"
2546 " unsigned long events_discarded;\n"
2547 " uint32_t cpu_id;\n"
2554 * id: range: 0 - 30.
2555 * id 31 is reserved to indicate an extended header.
2558 * id: range: 0 - 65534.
2559 * id 65535 is reserved to indicate an extended header.
2561 * Must be called with sessions_mutex held.
2564 int _lttng_event_header_declare(struct lttng_session
*session
)
2566 return lttng_metadata_printf(session
,
2567 "struct event_header_compact {\n"
2568 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2571 " uint27_clock_monotonic_t timestamp;\n"
2575 " uint64_clock_monotonic_t timestamp;\n"
2580 "struct event_header_large {\n"
2581 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2584 " uint32_clock_monotonic_t timestamp;\n"
2588 " uint64_clock_monotonic_t timestamp;\n"
2592 lttng_alignof(uint32_t) * CHAR_BIT
,
2593 lttng_alignof(uint16_t) * CHAR_BIT
2598 * Approximation of NTP time of day to clock monotonic correlation,
2599 * taken at start of trace.
2600 * Yes, this is only an approximation. Yes, we can (and will) do better
2601 * in future versions.
2602 * This function may return a negative offset. It may happen if the
2603 * system sets the REALTIME clock to 0 after boot.
2605 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2609 int64_t measure_clock_offset(void)
2611 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2612 uint64_t tcf
= trace_clock_freq();
2614 unsigned long flags
;
2615 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2616 struct timespec64 rts
= { 0, 0 };
2618 struct timespec rts
= { 0, 0 };
2621 /* Disable interrupts to increase correlation precision. */
2622 local_irq_save(flags
);
2623 monotonic
[0] = trace_clock_read64();
2624 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2625 ktime_get_real_ts64(&rts
);
2627 getnstimeofday(&rts
);
2629 monotonic
[1] = trace_clock_read64();
2630 local_irq_restore(flags
);
2632 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2633 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2634 if (tcf
== NSEC_PER_SEC
) {
2635 realtime
+= rts
.tv_nsec
;
2637 uint64_t n
= rts
.tv_nsec
* tcf
;
2639 do_div(n
, NSEC_PER_SEC
);
2642 offset
= (int64_t) realtime
- monotonic_avg
;
2647 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2655 while (cur
!= '\0') {
2658 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2662 ret
= lttng_metadata_printf(session
, "%c", '\\');
2665 /* We still print the current char */
2668 ret
= lttng_metadata_printf(session
, "%c", cur
);
2682 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2683 const char *field_value
)
2687 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2691 ret
= print_escaped_ctf_string(session
, field_value
);
2695 ret
= lttng_metadata_printf(session
, "\";\n");
2702 * Output metadata into this session's metadata buffers.
2703 * Must be called with sessions_mutex held.
2706 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2708 unsigned char *uuid_c
= session
->uuid
.b
;
2709 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2710 const char *product_uuid
;
2711 struct lttng_channel
*chan
;
2712 struct lttng_event
*event
;
2715 if (!LTTNG_READ_ONCE(session
->active
))
2718 lttng_metadata_begin(session
);
2720 if (session
->metadata_dumped
)
2723 snprintf(uuid_s
, sizeof(uuid_s
),
2724 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2725 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2726 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2727 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2728 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2730 ret
= lttng_metadata_printf(session
,
2731 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2732 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2733 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2734 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2735 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2736 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2737 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2743 " byte_order = %s;\n"
2744 " packet.header := struct {\n"
2745 " uint32_t magic;\n"
2746 " uint8_t uuid[16];\n"
2747 " uint32_t stream_id;\n"
2748 " uint64_t stream_instance_id;\n"
2751 lttng_alignof(uint8_t) * CHAR_BIT
,
2752 lttng_alignof(uint16_t) * CHAR_BIT
,
2753 lttng_alignof(uint32_t) * CHAR_BIT
,
2754 lttng_alignof(uint64_t) * CHAR_BIT
,
2755 sizeof(unsigned long) * CHAR_BIT
,
2756 lttng_alignof(unsigned long) * CHAR_BIT
,
2760 #if __BYTE_ORDER == __BIG_ENDIAN
2769 ret
= lttng_metadata_printf(session
,
2771 " hostname = \"%s\";\n"
2772 " domain = \"kernel\";\n"
2773 " sysname = \"%s\";\n"
2774 " kernel_release = \"%s\";\n"
2775 " kernel_version = \"%s\";\n"
2776 " tracer_name = \"lttng-modules\";\n"
2777 " tracer_major = %d;\n"
2778 " tracer_minor = %d;\n"
2779 " tracer_patchlevel = %d;\n"
2780 " trace_buffering_scheme = \"global\";\n",
2781 current
->nsproxy
->uts_ns
->name
.nodename
,
2785 LTTNG_MODULES_MAJOR_VERSION
,
2786 LTTNG_MODULES_MINOR_VERSION
,
2787 LTTNG_MODULES_PATCHLEVEL_VERSION
2792 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2795 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2796 session
->creation_time
);
2800 /* Add the product UUID to the 'env' section */
2801 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2803 ret
= lttng_metadata_printf(session
,
2804 " product_uuid = \"%s\";\n",
2811 /* Close the 'env' section */
2812 ret
= lttng_metadata_printf(session
, "};\n\n");
2816 ret
= lttng_metadata_printf(session
,
2818 " name = \"%s\";\n",
2824 if (!trace_clock_uuid(clock_uuid_s
)) {
2825 ret
= lttng_metadata_printf(session
,
2826 " uuid = \"%s\";\n",
2833 ret
= lttng_metadata_printf(session
,
2834 " description = \"%s\";\n"
2835 " freq = %llu; /* Frequency, in Hz */\n"
2836 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2839 trace_clock_description(),
2840 (unsigned long long) trace_clock_freq(),
2841 (long long) measure_clock_offset()
2846 ret
= lttng_metadata_printf(session
,
2847 "typealias integer {\n"
2848 " size = 27; align = 1; signed = false;\n"
2849 " map = clock.%s.value;\n"
2850 "} := uint27_clock_monotonic_t;\n"
2852 "typealias integer {\n"
2853 " size = 32; align = %u; signed = false;\n"
2854 " map = clock.%s.value;\n"
2855 "} := uint32_clock_monotonic_t;\n"
2857 "typealias integer {\n"
2858 " size = 64; align = %u; signed = false;\n"
2859 " map = clock.%s.value;\n"
2860 "} := uint64_clock_monotonic_t;\n\n",
2862 lttng_alignof(uint32_t) * CHAR_BIT
,
2864 lttng_alignof(uint64_t) * CHAR_BIT
,
2870 ret
= _lttng_stream_packet_context_declare(session
);
2874 ret
= _lttng_event_header_declare(session
);
2879 list_for_each_entry(chan
, &session
->chan
, list
) {
2880 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2885 list_for_each_entry(event
, &session
->events
, list
) {
2886 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2890 session
->metadata_dumped
= 1;
2892 lttng_metadata_end(session
);
2897 * lttng_transport_register - LTT transport registration
2898 * @transport: transport structure
2900 * Registers a transport which can be used as output to extract the data out of
2901 * LTTng. The module calling this registration function must ensure that no
2902 * trap-inducing code will be executed by the transport functions. E.g.
2903 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2904 * is made visible to the transport function. This registration acts as a
2905 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2906 * after its registration must it synchronize the TLBs.
2908 void lttng_transport_register(struct lttng_transport
*transport
)
2911 * Make sure no page fault can be triggered by the module about to be
2912 * registered. We deal with this here so we don't have to call
2913 * vmalloc_sync_mappings() in each module's init.
2915 wrapper_vmalloc_sync_mappings();
2917 mutex_lock(&sessions_mutex
);
2918 list_add_tail(&transport
->node
, <tng_transport_list
);
2919 mutex_unlock(&sessions_mutex
);
2921 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2924 * lttng_transport_unregister - LTT transport unregistration
2925 * @transport: transport structure
2927 void lttng_transport_unregister(struct lttng_transport
*transport
)
2929 mutex_lock(&sessions_mutex
);
2930 list_del(&transport
->node
);
2931 mutex_unlock(&sessions_mutex
);
2933 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2935 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
2937 enum cpuhp_state lttng_hp_prepare
;
2938 enum cpuhp_state lttng_hp_online
;
2940 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2942 struct lttng_cpuhp_node
*lttng_node
;
2944 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2945 switch (lttng_node
->component
) {
2946 case LTTNG_RING_BUFFER_FRONTEND
:
2948 case LTTNG_RING_BUFFER_BACKEND
:
2949 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2950 case LTTNG_RING_BUFFER_ITER
:
2952 case LTTNG_CONTEXT_PERF_COUNTERS
:
2959 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2961 struct lttng_cpuhp_node
*lttng_node
;
2963 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2964 switch (lttng_node
->component
) {
2965 case LTTNG_RING_BUFFER_FRONTEND
:
2966 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2967 case LTTNG_RING_BUFFER_BACKEND
:
2969 case LTTNG_RING_BUFFER_ITER
:
2971 case LTTNG_CONTEXT_PERF_COUNTERS
:
2972 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2978 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2980 struct lttng_cpuhp_node
*lttng_node
;
2982 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2983 switch (lttng_node
->component
) {
2984 case LTTNG_RING_BUFFER_FRONTEND
:
2985 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2986 case LTTNG_RING_BUFFER_BACKEND
:
2988 case LTTNG_RING_BUFFER_ITER
:
2989 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2990 case LTTNG_CONTEXT_PERF_COUNTERS
:
2991 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2997 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2999 struct lttng_cpuhp_node
*lttng_node
;
3001 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3002 switch (lttng_node
->component
) {
3003 case LTTNG_RING_BUFFER_FRONTEND
:
3004 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
3005 case LTTNG_RING_BUFFER_BACKEND
:
3007 case LTTNG_RING_BUFFER_ITER
:
3009 case LTTNG_CONTEXT_PERF_COUNTERS
:
3016 static int __init
lttng_init_cpu_hotplug(void)
3020 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
3021 lttng_hotplug_prepare
,
3022 lttng_hotplug_dead
);
3026 lttng_hp_prepare
= ret
;
3027 lttng_rb_set_hp_prepare(ret
);
3029 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
3030 lttng_hotplug_online
,
3031 lttng_hotplug_offline
);
3033 cpuhp_remove_multi_state(lttng_hp_prepare
);
3034 lttng_hp_prepare
= 0;
3037 lttng_hp_online
= ret
;
3038 lttng_rb_set_hp_online(ret
);
3043 static void __exit
lttng_exit_cpu_hotplug(void)
3045 lttng_rb_set_hp_online(0);
3046 cpuhp_remove_multi_state(lttng_hp_online
);
3047 lttng_rb_set_hp_prepare(0);
3048 cpuhp_remove_multi_state(lttng_hp_prepare
);
3051 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
3052 static int lttng_init_cpu_hotplug(void)
3056 static void lttng_exit_cpu_hotplug(void)
3059 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
3062 static int __init
lttng_events_init(void)
3066 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
3069 ret
= wrapper_get_pfnblock_flags_mask_init();
3072 ret
= wrapper_get_pageblock_flags_mask_init();
3075 ret
= lttng_probes_init();
3078 ret
= lttng_context_init();
3081 ret
= lttng_tracepoint_init();
3084 event_cache
= KMEM_CACHE(lttng_event
, 0);
3089 ret
= lttng_abi_init();
3092 ret
= lttng_logger_init();
3095 ret
= lttng_init_cpu_hotplug();
3098 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3099 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3100 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3101 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3102 LTTNG_MODULES_EXTRAVERSION
,
3104 #ifdef LTTNG_EXTRA_VERSION_GIT
3105 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3109 #ifdef LTTNG_EXTRA_VERSION_NAME
3110 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3114 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
3115 printk(KERN_NOTICE
"LTTng: Experimental bitwise enum enabled.\n");
3116 #endif /* CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM */
3120 lttng_logger_exit();
3124 kmem_cache_destroy(event_cache
);
3126 lttng_tracepoint_exit();
3128 lttng_context_exit();
3129 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3130 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3131 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3132 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3133 LTTNG_MODULES_EXTRAVERSION
,
3135 #ifdef LTTNG_EXTRA_VERSION_GIT
3136 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3140 #ifdef LTTNG_EXTRA_VERSION_NAME
3141 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3148 module_init(lttng_events_init
);
3150 static void __exit
lttng_events_exit(void)
3152 struct lttng_session
*session
, *tmpsession
;
3154 lttng_exit_cpu_hotplug();
3155 lttng_logger_exit();
3157 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3158 lttng_session_destroy(session
);
3159 kmem_cache_destroy(event_cache
);
3160 lttng_tracepoint_exit();
3161 lttng_context_exit();
3162 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3163 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3164 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3165 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3166 LTTNG_MODULES_EXTRAVERSION
,
3168 #ifdef LTTNG_EXTRA_VERSION_GIT
3169 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3173 #ifdef LTTNG_EXTRA_VERSION_NAME
3174 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3180 module_exit(lttng_events_exit
);
3182 #include "extra_version/patches.i"
3183 #ifdef LTTNG_EXTRA_VERSION_GIT
3184 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3186 #ifdef LTTNG_EXTRA_VERSION_NAME
3187 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3189 MODULE_LICENSE("GPL and additional rights");
3190 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3191 MODULE_DESCRIPTION("LTTng tracer");
3192 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3193 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3194 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3195 LTTNG_MODULES_EXTRAVERSION
);