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/uuid.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/vzalloc.h>
45 #include <wrapper/ringbuffer/backend.h>
46 #include <wrapper/ringbuffer/frontend.h>
47 #include <wrapper/time.h>
49 #define METADATA_CACHE_DEFAULT_SIZE 4096
51 static LIST_HEAD(sessions
);
52 static LIST_HEAD(lttng_transport_list
);
54 * Protect the sessions and metadata caches.
56 static DEFINE_MUTEX(sessions_mutex
);
57 static struct kmem_cache
*event_cache
;
59 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
60 static void lttng_session_sync_enablers(struct lttng_session
*session
);
61 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
63 static void _lttng_event_destroy(struct lttng_event
*event
);
64 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
65 static int _lttng_event_unregister(struct lttng_event
*event
);
67 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
68 struct lttng_channel
*chan
,
69 struct lttng_event
*event
);
71 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
73 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
75 int _lttng_field_statedump(struct lttng_session
*session
,
76 const struct lttng_event_field
*field
,
79 void synchronize_trace(void)
81 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \
82 LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0))
88 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0))
89 #ifdef CONFIG_PREEMPT_RT_FULL
92 #else /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */
93 #ifdef CONFIG_PREEMPT_RT
96 #endif /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */
99 void lttng_lock_sessions(void)
101 mutex_lock(&sessions_mutex
);
104 void lttng_unlock_sessions(void)
106 mutex_unlock(&sessions_mutex
);
110 * Called with sessions lock held.
112 int lttng_session_active(void)
114 struct lttng_session
*iter
;
116 list_for_each_entry(iter
, &sessions
, list
) {
123 struct lttng_session
*lttng_session_create(void)
125 struct lttng_session
*session
;
126 struct lttng_metadata_cache
*metadata_cache
;
129 mutex_lock(&sessions_mutex
);
130 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
133 INIT_LIST_HEAD(&session
->chan
);
134 INIT_LIST_HEAD(&session
->events
);
135 lttng_guid_gen(&session
->uuid
);
137 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
140 goto err_free_session
;
141 metadata_cache
->data
= lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
142 if (!metadata_cache
->data
)
144 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
145 kref_init(&metadata_cache
->refcount
);
146 mutex_init(&metadata_cache
->lock
);
147 session
->metadata_cache
= metadata_cache
;
148 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
149 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
150 sizeof(metadata_cache
->uuid
));
151 INIT_LIST_HEAD(&session
->enablers_head
);
152 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
153 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
154 list_add(&session
->list
, &sessions
);
155 mutex_unlock(&sessions_mutex
);
159 kfree(metadata_cache
);
161 lttng_kvfree(session
);
163 mutex_unlock(&sessions_mutex
);
167 void metadata_cache_destroy(struct kref
*kref
)
169 struct lttng_metadata_cache
*cache
=
170 container_of(kref
, struct lttng_metadata_cache
, refcount
);
175 void lttng_session_destroy(struct lttng_session
*session
)
177 struct lttng_channel
*chan
, *tmpchan
;
178 struct lttng_event
*event
, *tmpevent
;
179 struct lttng_metadata_stream
*metadata_stream
;
180 struct lttng_enabler
*enabler
, *tmpenabler
;
183 mutex_lock(&sessions_mutex
);
184 WRITE_ONCE(session
->active
, 0);
185 list_for_each_entry(chan
, &session
->chan
, list
) {
186 ret
= lttng_syscalls_unregister(chan
);
189 list_for_each_entry(event
, &session
->events
, list
) {
190 ret
= _lttng_event_unregister(event
);
193 synchronize_trace(); /* Wait for in-flight events to complete */
194 list_for_each_entry(chan
, &session
->chan
, list
) {
195 ret
= lttng_syscalls_destroy(chan
);
198 list_for_each_entry_safe(enabler
, tmpenabler
,
199 &session
->enablers_head
, node
)
200 lttng_enabler_destroy(enabler
);
201 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
202 _lttng_event_destroy(event
);
203 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
204 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
205 _lttng_channel_destroy(chan
);
207 mutex_lock(&session
->metadata_cache
->lock
);
208 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
209 _lttng_metadata_channel_hangup(metadata_stream
);
210 mutex_unlock(&session
->metadata_cache
->lock
);
211 if (session
->pid_tracker
)
212 lttng_pid_tracker_destroy(session
->pid_tracker
);
213 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
214 list_del(&session
->list
);
215 mutex_unlock(&sessions_mutex
);
216 lttng_kvfree(session
);
219 int lttng_session_statedump(struct lttng_session
*session
)
223 mutex_lock(&sessions_mutex
);
224 ret
= lttng_statedump_start(session
);
225 mutex_unlock(&sessions_mutex
);
229 int lttng_session_enable(struct lttng_session
*session
)
232 struct lttng_channel
*chan
;
234 mutex_lock(&sessions_mutex
);
235 if (session
->active
) {
240 /* Set transient enabler state to "enabled" */
244 * Snapshot the number of events per channel to know the type of header
247 list_for_each_entry(chan
, &session
->chan
, list
) {
248 if (chan
->header_type
)
249 continue; /* don't change it if session stop/restart */
250 if (chan
->free_event_id
< 31)
251 chan
->header_type
= 1; /* compact */
253 chan
->header_type
= 2; /* large */
256 /* We need to sync enablers with session before activation. */
257 lttng_session_sync_enablers(session
);
259 /* Clear each stream's quiescent state. */
260 list_for_each_entry(chan
, &session
->chan
, list
) {
261 if (chan
->channel_type
!= METADATA_CHANNEL
)
262 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
265 WRITE_ONCE(session
->active
, 1);
266 WRITE_ONCE(session
->been_active
, 1);
267 ret
= _lttng_session_metadata_statedump(session
);
269 WRITE_ONCE(session
->active
, 0);
272 ret
= lttng_statedump_start(session
);
274 WRITE_ONCE(session
->active
, 0);
276 mutex_unlock(&sessions_mutex
);
280 int lttng_session_disable(struct lttng_session
*session
)
283 struct lttng_channel
*chan
;
285 mutex_lock(&sessions_mutex
);
286 if (!session
->active
) {
290 WRITE_ONCE(session
->active
, 0);
292 /* Set transient enabler state to "disabled" */
294 lttng_session_sync_enablers(session
);
296 /* Set each stream's quiescent state. */
297 list_for_each_entry(chan
, &session
->chan
, list
) {
298 if (chan
->channel_type
!= METADATA_CHANNEL
)
299 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
302 mutex_unlock(&sessions_mutex
);
306 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
309 struct lttng_channel
*chan
;
310 struct lttng_event
*event
;
311 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
312 struct lttng_metadata_stream
*stream
;
314 mutex_lock(&sessions_mutex
);
315 if (!session
->active
) {
320 mutex_lock(&cache
->lock
);
321 memset(cache
->data
, 0, cache
->cache_alloc
);
322 cache
->metadata_written
= 0;
324 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
325 stream
->metadata_out
= 0;
326 stream
->metadata_in
= 0;
328 mutex_unlock(&cache
->lock
);
330 session
->metadata_dumped
= 0;
331 list_for_each_entry(chan
, &session
->chan
, list
) {
332 chan
->metadata_dumped
= 0;
335 list_for_each_entry(event
, &session
->events
, list
) {
336 event
->metadata_dumped
= 0;
339 ret
= _lttng_session_metadata_statedump(session
);
342 mutex_unlock(&sessions_mutex
);
346 int lttng_channel_enable(struct lttng_channel
*channel
)
350 mutex_lock(&sessions_mutex
);
351 if (channel
->channel_type
== METADATA_CHANNEL
) {
355 if (channel
->enabled
) {
359 /* Set transient enabler state to "enabled" */
361 lttng_session_sync_enablers(channel
->session
);
362 /* Set atomically the state to "enabled" */
363 WRITE_ONCE(channel
->enabled
, 1);
365 mutex_unlock(&sessions_mutex
);
369 int lttng_channel_disable(struct lttng_channel
*channel
)
373 mutex_lock(&sessions_mutex
);
374 if (channel
->channel_type
== METADATA_CHANNEL
) {
378 if (!channel
->enabled
) {
382 /* Set atomically the state to "disabled" */
383 WRITE_ONCE(channel
->enabled
, 0);
384 /* Set transient enabler state to "enabled" */
386 lttng_session_sync_enablers(channel
->session
);
388 mutex_unlock(&sessions_mutex
);
392 int lttng_event_enable(struct lttng_event
*event
)
396 mutex_lock(&sessions_mutex
);
397 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
401 if (event
->enabled
) {
405 switch (event
->instrumentation
) {
406 case LTTNG_KERNEL_TRACEPOINT
:
407 case LTTNG_KERNEL_SYSCALL
:
410 case LTTNG_KERNEL_KPROBE
:
411 case LTTNG_KERNEL_UPROBE
:
412 case LTTNG_KERNEL_NOOP
:
413 WRITE_ONCE(event
->enabled
, 1);
415 case LTTNG_KERNEL_KRETPROBE
:
416 ret
= lttng_kretprobes_event_enable_state(event
, 1);
418 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
424 mutex_unlock(&sessions_mutex
);
428 int lttng_event_disable(struct lttng_event
*event
)
432 mutex_lock(&sessions_mutex
);
433 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
437 if (!event
->enabled
) {
441 switch (event
->instrumentation
) {
442 case LTTNG_KERNEL_TRACEPOINT
:
443 case LTTNG_KERNEL_SYSCALL
:
446 case LTTNG_KERNEL_KPROBE
:
447 case LTTNG_KERNEL_UPROBE
:
448 case LTTNG_KERNEL_NOOP
:
449 WRITE_ONCE(event
->enabled
, 0);
451 case LTTNG_KERNEL_KRETPROBE
:
452 ret
= lttng_kretprobes_event_enable_state(event
, 0);
454 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
460 mutex_unlock(&sessions_mutex
);
464 static struct lttng_transport
*lttng_transport_find(const char *name
)
466 struct lttng_transport
*transport
;
468 list_for_each_entry(transport
, <tng_transport_list
, node
) {
469 if (!strcmp(transport
->name
, name
))
475 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
476 const char *transport_name
,
478 size_t subbuf_size
, size_t num_subbuf
,
479 unsigned int switch_timer_interval
,
480 unsigned int read_timer_interval
,
481 enum channel_type channel_type
)
483 struct lttng_channel
*chan
;
484 struct lttng_transport
*transport
= NULL
;
486 mutex_lock(&sessions_mutex
);
487 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
488 goto active
; /* Refuse to add channel to active session */
489 transport
= lttng_transport_find(transport_name
);
491 printk(KERN_WARNING
"LTTng transport %s not found\n",
495 if (!try_module_get(transport
->owner
)) {
496 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
499 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
502 chan
->session
= session
;
503 chan
->id
= session
->free_chan_id
++;
504 chan
->ops
= &transport
->ops
;
506 * Note: the channel creation op already writes into the packet
507 * headers. Therefore the "chan" information used as input
508 * should be already accessible.
510 chan
->chan
= transport
->ops
.channel_create(transport_name
,
511 chan
, buf_addr
, subbuf_size
, num_subbuf
,
512 switch_timer_interval
, read_timer_interval
);
517 chan
->transport
= transport
;
518 chan
->channel_type
= channel_type
;
519 list_add(&chan
->list
, &session
->chan
);
520 mutex_unlock(&sessions_mutex
);
527 module_put(transport
->owner
);
530 mutex_unlock(&sessions_mutex
);
535 * Only used internally at session destruction for per-cpu channels, and
536 * when metadata channel is released.
537 * Needs to be called with sessions mutex held.
540 void _lttng_channel_destroy(struct lttng_channel
*chan
)
542 chan
->ops
->channel_destroy(chan
->chan
);
543 module_put(chan
->transport
->owner
);
544 list_del(&chan
->list
);
545 lttng_destroy_context(chan
->ctx
);
549 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
551 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
553 /* Protect the metadata cache with the sessions_mutex. */
554 mutex_lock(&sessions_mutex
);
555 _lttng_channel_destroy(chan
);
556 mutex_unlock(&sessions_mutex
);
558 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
561 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
563 stream
->finalized
= 1;
564 wake_up_interruptible(&stream
->read_wait
);
568 * Supports event creation while tracing session is active.
569 * Needs to be called with sessions mutex held.
571 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
572 struct lttng_kernel_event
*event_param
,
574 const struct lttng_event_desc
*event_desc
,
575 enum lttng_kernel_instrumentation itype
)
577 struct lttng_session
*session
= chan
->session
;
578 struct lttng_event
*event
;
579 const char *event_name
;
580 struct hlist_head
*head
;
585 if (chan
->free_event_id
== -1U) {
591 case LTTNG_KERNEL_TRACEPOINT
:
592 event_name
= event_desc
->name
;
594 case LTTNG_KERNEL_KPROBE
:
595 case LTTNG_KERNEL_UPROBE
:
596 case LTTNG_KERNEL_KRETPROBE
:
597 case LTTNG_KERNEL_NOOP
:
598 case LTTNG_KERNEL_SYSCALL
:
599 event_name
= event_param
->name
;
601 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
607 name_len
= strlen(event_name
);
608 hash
= jhash(event_name
, name_len
, 0);
609 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
610 lttng_hlist_for_each_entry(event
, head
, hlist
) {
611 WARN_ON_ONCE(!event
->desc
);
612 if (!strncmp(event
->desc
->name
, event_name
,
613 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
614 && chan
== event
->chan
) {
620 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
626 event
->filter
= filter
;
627 event
->id
= chan
->free_event_id
++;
628 event
->instrumentation
= itype
;
629 event
->evtype
= LTTNG_TYPE_EVENT
;
630 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
631 INIT_LIST_HEAD(&event
->enablers_ref_head
);
634 case LTTNG_KERNEL_TRACEPOINT
:
635 /* Event will be enabled by enabler sync. */
637 event
->registered
= 0;
638 event
->desc
= lttng_event_get(event_name
);
643 /* Populate lttng_event structure before event registration. */
646 case LTTNG_KERNEL_KPROBE
:
648 * Needs to be explicitly enabled after creation, since
649 * we may want to apply filters.
652 event
->registered
= 1;
654 * Populate lttng_event structure before event
658 ret
= lttng_kprobes_register(event_name
,
659 event_param
->u
.kprobe
.symbol_name
,
660 event_param
->u
.kprobe
.offset
,
661 event_param
->u
.kprobe
.addr
,
667 ret
= try_module_get(event
->desc
->owner
);
670 case LTTNG_KERNEL_KRETPROBE
:
672 struct lttng_event
*event_return
;
674 /* kretprobe defines 2 events */
676 * Needs to be explicitly enabled after creation, since
677 * we may want to apply filters.
680 event
->registered
= 1;
682 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
687 event_return
->chan
= chan
;
688 event_return
->filter
= filter
;
689 event_return
->id
= chan
->free_event_id
++;
690 event_return
->enabled
= 0;
691 event_return
->registered
= 1;
692 event_return
->instrumentation
= itype
;
693 INIT_LIST_HEAD(&event_return
->bytecode_runtime_head
);
694 INIT_LIST_HEAD(&event_return
->enablers_ref_head
);
696 * Populate lttng_event structure before kretprobe registration.
699 ret
= lttng_kretprobes_register(event_name
,
700 event_param
->u
.kretprobe
.symbol_name
,
701 event_param
->u
.kretprobe
.offset
,
702 event_param
->u
.kretprobe
.addr
,
703 event
, event_return
);
705 kmem_cache_free(event_cache
, event_return
);
709 /* Take 2 refs on the module: one per event. */
710 ret
= try_module_get(event
->desc
->owner
);
712 ret
= try_module_get(event
->desc
->owner
);
714 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
716 WARN_ON_ONCE(ret
> 0);
718 kmem_cache_free(event_cache
, event_return
);
719 module_put(event
->desc
->owner
);
720 module_put(event
->desc
->owner
);
721 goto statedump_error
;
723 list_add(&event_return
->list
, &chan
->session
->events
);
726 case LTTNG_KERNEL_NOOP
:
727 case LTTNG_KERNEL_SYSCALL
:
729 * Needs to be explicitly enabled after creation, since
730 * we may want to apply filters.
733 event
->registered
= 0;
734 event
->desc
= event_desc
;
735 switch (event_param
->u
.syscall
.entryexit
) {
736 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
739 case LTTNG_KERNEL_SYSCALL_ENTRY
:
740 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
742 case LTTNG_KERNEL_SYSCALL_EXIT
:
743 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
746 switch (event_param
->u
.syscall
.abi
) {
747 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
750 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
751 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
753 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
754 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
762 case LTTNG_KERNEL_UPROBE
:
764 * Needs to be explicitly enabled after creation, since
765 * we may want to apply filters.
768 event
->registered
= 1;
771 * Populate lttng_event structure before event
776 ret
= lttng_uprobes_register(event_param
->name
,
777 event_param
->u
.uprobe
.fd
,
781 ret
= try_module_get(event
->desc
->owner
);
784 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
790 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
791 WARN_ON_ONCE(ret
> 0);
793 goto statedump_error
;
795 hlist_add_head(&event
->hlist
, head
);
796 list_add(&event
->list
, &chan
->session
->events
);
800 /* If a statedump error occurs, events will not be readable. */
802 kmem_cache_free(event_cache
, event
);
810 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
811 struct lttng_kernel_event
*event_param
,
813 const struct lttng_event_desc
*event_desc
,
814 enum lttng_kernel_instrumentation itype
)
816 struct lttng_event
*event
;
818 mutex_lock(&sessions_mutex
);
819 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
821 mutex_unlock(&sessions_mutex
);
825 /* Only used for tracepoints for now. */
827 void register_event(struct lttng_event
*event
)
829 const struct lttng_event_desc
*desc
;
832 if (event
->registered
)
836 switch (event
->instrumentation
) {
837 case LTTNG_KERNEL_TRACEPOINT
:
838 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
839 desc
->probe_callback
,
842 case LTTNG_KERNEL_SYSCALL
:
843 ret
= lttng_syscall_filter_enable(event
->chan
, event
);
845 case LTTNG_KERNEL_KPROBE
:
846 case LTTNG_KERNEL_UPROBE
:
847 case LTTNG_KERNEL_KRETPROBE
:
848 case LTTNG_KERNEL_NOOP
:
851 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
856 event
->registered
= 1;
860 * Only used internally at session destruction.
862 int _lttng_event_unregister(struct lttng_event
*event
)
864 const struct lttng_event_desc
*desc
;
867 if (!event
->registered
)
871 switch (event
->instrumentation
) {
872 case LTTNG_KERNEL_TRACEPOINT
:
873 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
874 event
->desc
->probe_callback
,
877 case LTTNG_KERNEL_KPROBE
:
878 lttng_kprobes_unregister(event
);
881 case LTTNG_KERNEL_KRETPROBE
:
882 lttng_kretprobes_unregister(event
);
885 case LTTNG_KERNEL_SYSCALL
:
886 ret
= lttng_syscall_filter_disable(event
->chan
, event
);
888 case LTTNG_KERNEL_NOOP
:
891 case LTTNG_KERNEL_UPROBE
:
892 lttng_uprobes_unregister(event
);
895 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
900 event
->registered
= 0;
905 * Only used internally at session destruction.
908 void _lttng_event_destroy(struct lttng_event
*event
)
910 struct lttng_enabler_ref
*enabler_ref
, *tmp_enabler_ref
;
912 switch (event
->instrumentation
) {
913 case LTTNG_KERNEL_TRACEPOINT
:
914 lttng_event_put(event
->desc
);
916 case LTTNG_KERNEL_KPROBE
:
917 module_put(event
->desc
->owner
);
918 lttng_kprobes_destroy_private(event
);
920 case LTTNG_KERNEL_KRETPROBE
:
921 module_put(event
->desc
->owner
);
922 lttng_kretprobes_destroy_private(event
);
924 case LTTNG_KERNEL_NOOP
:
925 case LTTNG_KERNEL_SYSCALL
:
927 case LTTNG_KERNEL_UPROBE
:
928 module_put(event
->desc
->owner
);
929 lttng_uprobes_destroy_private(event
);
931 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
935 list_del(&event
->list
);
936 lttng_destroy_context(event
->ctx
);
937 lttng_free_event_filter_runtime(event
);
938 /* Free event enabler refs */
939 list_for_each_entry_safe(enabler_ref
, tmp_enabler_ref
,
940 &event
->enablers_ref_head
, node
)
942 kmem_cache_free(event_cache
, event
);
945 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
951 mutex_lock(&sessions_mutex
);
953 /* track all pids: destroy tracker. */
954 if (session
->pid_tracker
) {
955 struct lttng_pid_tracker
*lpf
;
957 lpf
= session
->pid_tracker
;
958 rcu_assign_pointer(session
->pid_tracker
, NULL
);
960 lttng_pid_tracker_destroy(lpf
);
964 if (!session
->pid_tracker
) {
965 struct lttng_pid_tracker
*lpf
;
967 lpf
= lttng_pid_tracker_create();
972 ret
= lttng_pid_tracker_add(lpf
, pid
);
973 rcu_assign_pointer(session
->pid_tracker
, lpf
);
975 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
979 mutex_unlock(&sessions_mutex
);
983 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
989 mutex_lock(&sessions_mutex
);
991 /* untrack all pids: replace by empty tracker. */
992 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
993 struct lttng_pid_tracker
*lpf
;
995 lpf
= lttng_pid_tracker_create();
1000 rcu_assign_pointer(session
->pid_tracker
, lpf
);
1001 synchronize_trace();
1003 lttng_pid_tracker_destroy(old_lpf
);
1006 if (!session
->pid_tracker
) {
1010 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
1013 mutex_unlock(&sessions_mutex
);
1018 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
1020 struct lttng_session
*session
= m
->private;
1021 struct lttng_pid_tracker
*lpf
;
1022 struct lttng_pid_hash_node
*e
;
1025 mutex_lock(&sessions_mutex
);
1026 lpf
= session
->pid_tracker
;
1028 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
1029 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
1031 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1037 /* PID tracker disabled. */
1038 if (iter
>= *pos
&& iter
== 0) {
1039 return session
; /* empty tracker */
1047 /* Called with sessions_mutex held. */
1049 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1051 struct lttng_session
*session
= m
->private;
1052 struct lttng_pid_tracker
*lpf
;
1053 struct lttng_pid_hash_node
*e
;
1057 lpf
= session
->pid_tracker
;
1059 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
1060 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
1062 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1063 if (iter
++ >= *ppos
)
1068 /* PID tracker disabled. */
1069 if (iter
>= *ppos
&& iter
== 0)
1070 return session
; /* empty tracker */
1079 void pid_list_stop(struct seq_file
*m
, void *p
)
1081 mutex_unlock(&sessions_mutex
);
1085 int pid_list_show(struct seq_file
*m
, void *p
)
1089 if (p
== m
->private) {
1090 /* Tracker disabled. */
1093 const struct lttng_pid_hash_node
*e
= p
;
1095 pid
= lttng_pid_tracker_get_node_pid(e
);
1097 seq_printf(m
, "process { pid = %d; };\n", pid
);
1102 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
1103 .start
= pid_list_start
,
1104 .next
= pid_list_next
,
1105 .stop
= pid_list_stop
,
1106 .show
= pid_list_show
,
1110 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
1112 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
1116 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
1118 struct seq_file
*m
= file
->private_data
;
1119 struct lttng_session
*session
= m
->private;
1122 WARN_ON_ONCE(!session
);
1123 ret
= seq_release(inode
, file
);
1124 if (!ret
&& session
)
1125 fput(session
->file
);
1129 const struct file_operations lttng_tracker_pids_list_fops
= {
1130 .owner
= THIS_MODULE
,
1131 .open
= lttng_tracker_pids_list_open
,
1133 .llseek
= seq_lseek
,
1134 .release
= lttng_tracker_pids_list_release
,
1137 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
1139 struct file
*tracker_pids_list_file
;
1143 file_fd
= lttng_get_unused_fd();
1149 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
1150 <tng_tracker_pids_list_fops
,
1152 if (IS_ERR(tracker_pids_list_file
)) {
1153 ret
= PTR_ERR(tracker_pids_list_file
);
1156 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1158 goto refcount_error
;
1160 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1163 m
= tracker_pids_list_file
->private_data
;
1164 m
->private = session
;
1165 fd_install(file_fd
, tracker_pids_list_file
);
1170 atomic_long_dec(&session
->file
->f_count
);
1172 fput(tracker_pids_list_file
);
1174 put_unused_fd(file_fd
);
1180 * Enabler management.
1183 int lttng_match_enabler_star_glob(const char *desc_name
,
1184 const char *pattern
)
1186 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1187 desc_name
, LTTNG_SIZE_MAX
))
1193 int lttng_match_enabler_name(const char *desc_name
,
1196 if (strcmp(desc_name
, name
))
1202 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1203 struct lttng_enabler
*enabler
)
1205 const char *desc_name
, *enabler_name
;
1206 bool compat
= false, entry
= false;
1208 enabler_name
= enabler
->event_param
.name
;
1209 switch (enabler
->event_param
.instrumentation
) {
1210 case LTTNG_KERNEL_TRACEPOINT
:
1211 desc_name
= desc
->name
;
1212 switch (enabler
->type
) {
1213 case LTTNG_ENABLER_STAR_GLOB
:
1214 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1215 case LTTNG_ENABLER_NAME
:
1216 return lttng_match_enabler_name(desc_name
, enabler_name
);
1221 case LTTNG_KERNEL_SYSCALL
:
1222 desc_name
= desc
->name
;
1223 if (!strncmp(desc_name
, "compat_", strlen("compat_"))) {
1224 desc_name
+= strlen("compat_");
1227 if (!strncmp(desc_name
, "syscall_exit_",
1228 strlen("syscall_exit_"))) {
1229 desc_name
+= strlen("syscall_exit_");
1230 } else if (!strncmp(desc_name
, "syscall_entry_",
1231 strlen("syscall_entry_"))) {
1232 desc_name
+= strlen("syscall_entry_");
1238 switch (enabler
->event_param
.u
.syscall
.entryexit
) {
1239 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1241 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1245 case LTTNG_KERNEL_SYSCALL_EXIT
:
1252 switch (enabler
->event_param
.u
.syscall
.abi
) {
1253 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1255 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1259 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1266 switch (enabler
->event_param
.u
.syscall
.match
) {
1267 case LTTNG_SYSCALL_MATCH_NAME
:
1268 switch (enabler
->type
) {
1269 case LTTNG_ENABLER_STAR_GLOB
:
1270 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1271 case LTTNG_ENABLER_NAME
:
1272 return lttng_match_enabler_name(desc_name
, enabler_name
);
1277 case LTTNG_SYSCALL_MATCH_NR
:
1278 return -EINVAL
; /* Not implemented. */
1290 int lttng_event_match_enabler(struct lttng_event
*event
,
1291 struct lttng_enabler
*enabler
)
1293 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1295 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1296 && event
->chan
== enabler
->chan
)
1303 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1304 struct lttng_enabler
*enabler
)
1306 struct lttng_enabler_ref
*enabler_ref
;
1308 list_for_each_entry(enabler_ref
,
1309 &event
->enablers_ref_head
, node
) {
1310 if (enabler_ref
->ref
== enabler
)
1317 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1319 struct lttng_session
*session
= enabler
->chan
->session
;
1320 struct lttng_probe_desc
*probe_desc
;
1321 const struct lttng_event_desc
*desc
;
1323 struct list_head
*probe_list
;
1325 probe_list
= lttng_get_probe_list_head();
1327 * For each probe event, if we find that a probe event matches
1328 * our enabler, create an associated lttng_event if not
1331 list_for_each_entry(probe_desc
, probe_list
, head
) {
1332 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1334 struct hlist_head
*head
;
1335 const char *event_name
;
1338 struct lttng_event
*event
;
1340 desc
= probe_desc
->event_desc
[i
];
1341 if (!lttng_desc_match_enabler(desc
, enabler
))
1343 event_name
= desc
->name
;
1344 name_len
= strlen(event_name
);
1347 * Check if already created.
1349 hash
= jhash(event_name
, name_len
, 0);
1350 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1351 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1352 if (event
->desc
== desc
1353 && event
->chan
== enabler
->chan
)
1360 * We need to create an event for this
1363 event
= _lttng_event_create(enabler
->chan
,
1365 LTTNG_KERNEL_TRACEPOINT
);
1367 printk(KERN_INFO
"Unable to create event %s\n",
1368 probe_desc
->event_desc
[i
]->name
);
1375 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1379 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1384 * Create struct lttng_event if it is missing and present in the list of
1385 * tracepoint probes.
1386 * Should be called with sessions mutex held.
1389 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1391 switch (enabler
->event_param
.instrumentation
) {
1392 case LTTNG_KERNEL_TRACEPOINT
:
1393 lttng_create_tracepoint_if_missing(enabler
);
1395 case LTTNG_KERNEL_SYSCALL
:
1396 lttng_create_syscall_if_missing(enabler
);
1405 * Create events associated with an enabler (if not already present),
1406 * and add backward reference from the event to the enabler.
1407 * Should be called with sessions mutex held.
1410 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1412 struct lttng_channel
*chan
= enabler
->chan
;
1413 struct lttng_session
*session
= chan
->session
;
1414 struct lttng_event
*event
;
1416 if (enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
1417 enabler
->event_param
.u
.syscall
.entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
&&
1418 enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
1419 enabler
->event_param
.u
.syscall
.match
== LTTNG_SYSCALL_MATCH_NAME
&&
1420 !strcmp(enabler
->event_param
.name
, "*")) {
1421 if (enabler
->enabled
)
1422 WRITE_ONCE(chan
->syscall_all
, 1);
1424 WRITE_ONCE(chan
->syscall_all
, 0);
1427 /* First ensure that probe events are created for this enabler. */
1428 lttng_create_event_if_missing(enabler
);
1430 /* For each event matching enabler in session event list. */
1431 list_for_each_entry(event
, &session
->events
, list
) {
1432 struct lttng_enabler_ref
*enabler_ref
;
1434 if (!lttng_event_match_enabler(event
, enabler
))
1436 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1439 * If no backward ref, create it.
1440 * Add backward ref from event to enabler.
1442 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1445 enabler_ref
->ref
= enabler
;
1446 list_add(&enabler_ref
->node
,
1447 &event
->enablers_ref_head
);
1451 * Link filter bytecodes if not linked yet.
1453 lttng_enabler_event_link_bytecode(event
, enabler
);
1455 /* TODO: merge event context. */
1461 * Called at module load: connect the probe on all enablers matching
1463 * Called with sessions lock held.
1465 int lttng_fix_pending_events(void)
1467 struct lttng_session
*session
;
1469 list_for_each_entry(session
, &sessions
, list
)
1470 lttng_session_lazy_sync_enablers(session
);
1474 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1475 struct lttng_kernel_event
*event_param
,
1476 struct lttng_channel
*chan
)
1478 struct lttng_enabler
*enabler
;
1480 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1483 enabler
->type
= type
;
1484 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1485 memcpy(&enabler
->event_param
, event_param
,
1486 sizeof(enabler
->event_param
));
1487 enabler
->chan
= chan
;
1489 enabler
->enabled
= 0;
1490 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1491 mutex_lock(&sessions_mutex
);
1492 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1493 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1494 mutex_unlock(&sessions_mutex
);
1498 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1500 mutex_lock(&sessions_mutex
);
1501 enabler
->enabled
= 1;
1502 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1503 mutex_unlock(&sessions_mutex
);
1507 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1509 mutex_lock(&sessions_mutex
);
1510 enabler
->enabled
= 0;
1511 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1512 mutex_unlock(&sessions_mutex
);
1516 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1517 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1519 struct lttng_filter_bytecode_node
*bytecode_node
;
1520 uint32_t bytecode_len
;
1523 ret
= get_user(bytecode_len
, &bytecode
->len
);
1526 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1530 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1531 sizeof(*bytecode
) + bytecode_len
);
1534 bytecode_node
->enabler
= enabler
;
1535 /* Enforce length based on allocated size */
1536 bytecode_node
->bc
.len
= bytecode_len
;
1537 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1538 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1542 kfree(bytecode_node
);
1546 int lttng_event_add_callsite(struct lttng_event
*event
,
1547 struct lttng_kernel_event_callsite __user
*callsite
)
1550 switch (event
->instrumentation
) {
1551 case LTTNG_KERNEL_UPROBE
:
1552 return lttng_uprobes_add_callsite(event
, callsite
);
1558 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1559 struct lttng_kernel_context
*context_param
)
1565 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1567 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1569 /* Destroy filter bytecode */
1570 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1571 &enabler
->filter_bytecode_head
, node
) {
1575 /* Destroy contexts */
1576 lttng_destroy_context(enabler
->ctx
);
1578 list_del(&enabler
->node
);
1583 * lttng_session_sync_enablers should be called just before starting a
1585 * Should be called with sessions mutex held.
1588 void lttng_session_sync_enablers(struct lttng_session
*session
)
1590 struct lttng_enabler
*enabler
;
1591 struct lttng_event
*event
;
1593 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1594 lttng_enabler_ref_events(enabler
);
1596 * For each event, if at least one of its enablers is enabled,
1597 * and its channel and session transient states are enabled, we
1598 * enable the event, else we disable it.
1600 list_for_each_entry(event
, &session
->events
, list
) {
1601 struct lttng_enabler_ref
*enabler_ref
;
1602 struct lttng_bytecode_runtime
*runtime
;
1603 int enabled
= 0, has_enablers_without_bytecode
= 0;
1605 switch (event
->instrumentation
) {
1606 case LTTNG_KERNEL_TRACEPOINT
:
1607 case LTTNG_KERNEL_SYSCALL
:
1609 list_for_each_entry(enabler_ref
,
1610 &event
->enablers_ref_head
, node
) {
1611 if (enabler_ref
->ref
->enabled
) {
1618 /* Not handled with lazy sync. */
1622 * Enabled state is based on union of enablers, with
1623 * intesection of session and channel transient enable
1626 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1628 WRITE_ONCE(event
->enabled
, enabled
);
1630 * Sync tracepoint registration with event enabled
1634 register_event(event
);
1636 _lttng_event_unregister(event
);
1639 /* Check if has enablers without bytecode enabled */
1640 list_for_each_entry(enabler_ref
,
1641 &event
->enablers_ref_head
, node
) {
1642 if (enabler_ref
->ref
->enabled
1643 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1644 has_enablers_without_bytecode
= 1;
1648 event
->has_enablers_without_bytecode
=
1649 has_enablers_without_bytecode
;
1651 /* Enable filters */
1652 list_for_each_entry(runtime
,
1653 &event
->bytecode_runtime_head
, node
)
1654 lttng_filter_sync_state(runtime
);
1659 * Apply enablers to session events, adding events to session if need
1660 * be. It is required after each modification applied to an active
1661 * session, and right before session "start".
1662 * "lazy" sync means we only sync if required.
1663 * Should be called with sessions mutex held.
1666 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1668 /* We can skip if session is not active */
1669 if (!session
->active
)
1671 lttng_session_sync_enablers(session
);
1675 * Serialize at most one packet worth of metadata into a metadata
1677 * We grab the metadata cache mutex to get exclusive access to our metadata
1678 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1679 * allows us to do racy operations such as looking for remaining space left in
1680 * packet and write, since mutual exclusion protects us from concurrent writes.
1681 * Mutual exclusion on the metadata cache allow us to read the cache content
1682 * without racing against reallocation of the cache by updates.
1683 * Returns the number of bytes written in the channel, 0 if no data
1684 * was written and a negative value on error.
1686 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1687 struct channel
*chan
, bool *coherent
)
1689 struct lib_ring_buffer_ctx ctx
;
1691 size_t len
, reserve_len
;
1694 * Ensure we support mutiple get_next / put sequences followed by
1695 * put_next. The metadata cache lock protects reading the metadata
1696 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1697 * "flush" operations on the buffer invoked by different processes.
1698 * Moreover, since the metadata cache memory can be reallocated, we
1699 * need to have exclusive access against updates even though we only
1702 mutex_lock(&stream
->metadata_cache
->lock
);
1703 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1704 if (stream
->metadata_in
!= stream
->metadata_out
)
1707 /* Metadata regenerated, change the version. */
1708 if (stream
->metadata_cache
->version
!= stream
->version
)
1709 stream
->version
= stream
->metadata_cache
->version
;
1711 len
= stream
->metadata_cache
->metadata_written
-
1712 stream
->metadata_in
;
1715 reserve_len
= min_t(size_t,
1716 stream
->transport
->ops
.packet_avail_size(chan
),
1718 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1721 * If reservation failed, return an error to the caller.
1723 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1725 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1726 stream
->coherent
= false;
1729 stream
->transport
->ops
.event_write(&ctx
,
1730 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1732 stream
->transport
->ops
.event_commit(&ctx
);
1733 stream
->metadata_in
+= reserve_len
;
1734 if (reserve_len
< len
)
1735 stream
->coherent
= false;
1737 stream
->coherent
= true;
1742 *coherent
= stream
->coherent
;
1743 mutex_unlock(&stream
->metadata_cache
->lock
);
1748 void lttng_metadata_begin(struct lttng_session
*session
)
1750 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1751 mutex_lock(&session
->metadata_cache
->lock
);
1755 void lttng_metadata_end(struct lttng_session
*session
)
1757 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1758 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1759 struct lttng_metadata_stream
*stream
;
1761 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1762 wake_up_interruptible(&stream
->read_wait
);
1763 mutex_unlock(&session
->metadata_cache
->lock
);
1768 * Write the metadata to the metadata cache.
1769 * Must be called with sessions_mutex held.
1770 * The metadata cache lock protects us from concurrent read access from
1771 * thread outputting metadata content to ring buffer.
1772 * The content of the printf is printed as a single atomic metadata
1775 int lttng_metadata_printf(struct lttng_session
*session
,
1776 const char *fmt
, ...)
1782 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
1785 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1791 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1792 if (session
->metadata_cache
->metadata_written
+ len
>
1793 session
->metadata_cache
->cache_alloc
) {
1794 char *tmp_cache_realloc
;
1795 unsigned int tmp_cache_alloc_size
;
1797 tmp_cache_alloc_size
= max_t(unsigned int,
1798 session
->metadata_cache
->cache_alloc
+ len
,
1799 session
->metadata_cache
->cache_alloc
<< 1);
1800 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
1801 if (!tmp_cache_realloc
)
1803 if (session
->metadata_cache
->data
) {
1804 memcpy(tmp_cache_realloc
,
1805 session
->metadata_cache
->data
,
1806 session
->metadata_cache
->cache_alloc
);
1807 vfree(session
->metadata_cache
->data
);
1810 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1811 session
->metadata_cache
->data
= tmp_cache_realloc
;
1813 memcpy(session
->metadata_cache
->data
+
1814 session
->metadata_cache
->metadata_written
,
1816 session
->metadata_cache
->metadata_written
+= len
;
1827 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1831 for (i
= 0; i
< nesting
; i
++) {
1834 ret
= lttng_metadata_printf(session
, " ");
1843 * Must be called with sessions_mutex held.
1846 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1847 const struct lttng_type
*type
,
1851 uint32_t i
, nr_fields
;
1853 ret
= print_tabs(session
, nesting
);
1856 ret
= lttng_metadata_printf(session
,
1860 nr_fields
= type
->u
._struct
.nr_fields
;
1861 for (i
= 0; i
< nr_fields
; i
++) {
1862 const struct lttng_event_field
*iter_field
;
1864 iter_field
= &type
->u
._struct
.fields
[i
];
1865 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1869 ret
= print_tabs(session
, nesting
);
1872 ret
= lttng_metadata_printf(session
,
1878 * Must be called with sessions_mutex held.
1881 int _lttng_struct_statedump(struct lttng_session
*session
,
1882 const struct lttng_event_field
*field
,
1887 ret
= _lttng_struct_type_statedump(session
,
1888 &field
->type
, nesting
);
1891 ret
= lttng_metadata_printf(session
,
1898 * Must be called with sessions_mutex held.
1901 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1902 const struct lttng_type
*type
,
1906 uint32_t i
, nr_choices
;
1908 ret
= print_tabs(session
, nesting
);
1911 ret
= lttng_metadata_printf(session
,
1912 "variant <_%s> {\n",
1913 type
->u
.variant
.tag_name
);
1916 nr_choices
= type
->u
.variant
.nr_choices
;
1917 for (i
= 0; i
< nr_choices
; i
++) {
1918 const struct lttng_event_field
*iter_field
;
1920 iter_field
= &type
->u
.variant
.choices
[i
];
1921 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1925 ret
= print_tabs(session
, nesting
);
1928 ret
= lttng_metadata_printf(session
,
1934 * Must be called with sessions_mutex held.
1937 int _lttng_variant_statedump(struct lttng_session
*session
,
1938 const struct lttng_event_field
*field
,
1943 ret
= _lttng_variant_type_statedump(session
,
1944 &field
->type
, nesting
);
1947 ret
= lttng_metadata_printf(session
,
1954 * Must be called with sessions_mutex held.
1957 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1958 const struct lttng_event_field
*field
,
1962 const struct lttng_type
*elem_type
;
1964 /* Only array of structures and variants are currently supported. */
1965 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1966 switch (elem_type
->atype
) {
1968 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1973 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1980 ret
= lttng_metadata_printf(session
,
1983 field
->type
.u
.array_compound
.length
);
1988 * Must be called with sessions_mutex held.
1991 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
1992 const struct lttng_event_field
*field
,
1996 const char *length_name
;
1997 const struct lttng_type
*elem_type
;
1999 length_name
= field
->type
.u
.sequence_compound
.length_name
;
2001 /* Only array of structures and variants are currently supported. */
2002 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
2003 switch (elem_type
->atype
) {
2005 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
2010 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2017 ret
= lttng_metadata_printf(session
,
2025 * Must be called with sessions_mutex held.
2028 int _lttng_enum_statedump(struct lttng_session
*session
,
2029 const struct lttng_event_field
*field
,
2032 const struct lttng_enum_desc
*enum_desc
;
2033 const struct lttng_integer_type
*container_type
;
2035 unsigned int i
, nr_entries
;
2037 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
2038 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
2039 nr_entries
= enum_desc
->nr_entries
;
2041 ret
= print_tabs(session
, nesting
);
2044 ret
= lttng_metadata_printf(session
,
2045 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
2046 container_type
->size
,
2047 container_type
->alignment
,
2048 container_type
->signedness
,
2049 (container_type
->encoding
== lttng_encode_none
)
2051 : (container_type
->encoding
== lttng_encode_UTF8
)
2054 container_type
->base
,
2055 #if __BYTE_ORDER == __BIG_ENDIAN
2056 container_type
->reverse_byte_order
? " byte_order = le;" : ""
2058 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2063 /* Dump all entries */
2064 for (i
= 0; i
< nr_entries
; i
++) {
2065 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2068 ret
= print_tabs(session
, nesting
+ 1);
2071 ret
= lttng_metadata_printf(session
,
2075 len
= strlen(entry
->string
);
2076 /* Escape the character '"' */
2077 for (j
= 0; j
< len
; j
++) {
2078 char c
= entry
->string
[j
];
2082 ret
= lttng_metadata_printf(session
,
2086 ret
= lttng_metadata_printf(session
,
2090 ret
= lttng_metadata_printf(session
,
2097 ret
= lttng_metadata_printf(session
, "\"");
2101 if (entry
->options
.is_auto
) {
2102 ret
= lttng_metadata_printf(session
, ",\n");
2106 ret
= lttng_metadata_printf(session
,
2110 if (entry
->start
.signedness
)
2111 ret
= lttng_metadata_printf(session
,
2112 "%lld", (long long) entry
->start
.value
);
2114 ret
= lttng_metadata_printf(session
,
2115 "%llu", entry
->start
.value
);
2118 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2120 == entry
->end
.value
) {
2121 ret
= lttng_metadata_printf(session
,
2124 if (entry
->end
.signedness
) {
2125 ret
= lttng_metadata_printf(session
,
2127 (long long) entry
->end
.value
);
2129 ret
= lttng_metadata_printf(session
,
2138 ret
= print_tabs(session
, nesting
);
2141 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2148 * Must be called with sessions_mutex held.
2151 int _lttng_field_statedump(struct lttng_session
*session
,
2152 const struct lttng_event_field
*field
,
2157 switch (field
->type
.atype
) {
2159 ret
= print_tabs(session
, nesting
);
2162 ret
= lttng_metadata_printf(session
,
2163 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2164 field
->type
.u
.basic
.integer
.size
,
2165 field
->type
.u
.basic
.integer
.alignment
,
2166 field
->type
.u
.basic
.integer
.signedness
,
2167 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2169 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2172 field
->type
.u
.basic
.integer
.base
,
2173 #if __BYTE_ORDER == __BIG_ENDIAN
2174 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2176 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2181 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2184 case atype_array_bitfield
:
2186 const struct lttng_basic_type
*elem_type
;
2188 elem_type
= &field
->type
.u
.array
.elem_type
;
2189 if (field
->type
.u
.array
.elem_alignment
) {
2190 ret
= print_tabs(session
, nesting
);
2193 ret
= lttng_metadata_printf(session
,
2194 "struct { } align(%u) _%s_padding;\n",
2195 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2200 ret
= print_tabs(session
, nesting
);
2203 ret
= lttng_metadata_printf(session
,
2204 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2205 elem_type
->u
.basic
.integer
.size
,
2206 elem_type
->u
.basic
.integer
.alignment
,
2207 elem_type
->u
.basic
.integer
.signedness
,
2208 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2210 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2213 elem_type
->u
.basic
.integer
.base
,
2214 #if __BYTE_ORDER == __BIG_ENDIAN
2215 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2217 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2219 field
->name
, field
->type
.u
.array
.length
);
2222 case atype_sequence
:
2223 case atype_sequence_bitfield
:
2225 const struct lttng_basic_type
*elem_type
;
2226 const struct lttng_basic_type
*length_type
;
2228 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2229 length_type
= &field
->type
.u
.sequence
.length_type
;
2230 ret
= print_tabs(session
, nesting
);
2233 ret
= lttng_metadata_printf(session
,
2234 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2235 length_type
->u
.basic
.integer
.size
,
2236 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2237 length_type
->u
.basic
.integer
.signedness
,
2238 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2240 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2243 length_type
->u
.basic
.integer
.base
,
2244 #if __BYTE_ORDER == __BIG_ENDIAN
2245 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2247 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2253 if (field
->type
.u
.sequence
.elem_alignment
) {
2254 ret
= print_tabs(session
, nesting
);
2257 ret
= lttng_metadata_printf(session
,
2258 "struct { } align(%u) _%s_padding;\n",
2259 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2264 ret
= print_tabs(session
, nesting
);
2267 ret
= lttng_metadata_printf(session
,
2268 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2269 elem_type
->u
.basic
.integer
.size
,
2270 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2271 elem_type
->u
.basic
.integer
.signedness
,
2272 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2274 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2277 elem_type
->u
.basic
.integer
.base
,
2278 #if __BYTE_ORDER == __BIG_ENDIAN
2279 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2281 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2289 /* Default encoding is UTF8 */
2290 ret
= print_tabs(session
, nesting
);
2293 ret
= lttng_metadata_printf(session
,
2295 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2296 " { encoding = ASCII; }" : "",
2300 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2302 case atype_array_compound
:
2303 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2305 case atype_sequence_compound
:
2306 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2309 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2320 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2321 struct lttng_ctx
*ctx
)
2328 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2329 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2331 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2339 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2340 struct lttng_event
*event
)
2342 const struct lttng_event_desc
*desc
= event
->desc
;
2346 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2347 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2349 ret
= _lttng_field_statedump(session
, field
, 2);
2357 * Must be called with sessions_mutex held.
2358 * The entire event metadata is printed as a single atomic metadata
2362 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2363 struct lttng_channel
*chan
,
2364 struct lttng_event
*event
)
2368 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2370 if (chan
->channel_type
== METADATA_CHANNEL
)
2373 lttng_metadata_begin(session
);
2375 ret
= lttng_metadata_printf(session
,
2379 " stream_id = %u;\n",
2387 ret
= lttng_metadata_printf(session
,
2388 " context := struct {\n");
2392 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2396 ret
= lttng_metadata_printf(session
,
2402 ret
= lttng_metadata_printf(session
,
2403 " fields := struct {\n"
2408 ret
= _lttng_fields_metadata_statedump(session
, event
);
2413 * LTTng space reservation can only reserve multiples of the
2416 ret
= lttng_metadata_printf(session
,
2422 event
->metadata_dumped
= 1;
2424 lttng_metadata_end(session
);
2430 * Must be called with sessions_mutex held.
2431 * The entire channel metadata is printed as a single atomic metadata
2435 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2436 struct lttng_channel
*chan
)
2440 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2443 if (chan
->channel_type
== METADATA_CHANNEL
)
2446 lttng_metadata_begin(session
);
2448 WARN_ON_ONCE(!chan
->header_type
);
2449 ret
= lttng_metadata_printf(session
,
2452 " event.header := %s;\n"
2453 " packet.context := struct packet_context;\n",
2455 chan
->header_type
== 1 ? "struct event_header_compact" :
2456 "struct event_header_large");
2461 ret
= lttng_metadata_printf(session
,
2462 " event.context := struct {\n");
2466 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2470 ret
= lttng_metadata_printf(session
,
2476 ret
= lttng_metadata_printf(session
,
2479 chan
->metadata_dumped
= 1;
2481 lttng_metadata_end(session
);
2486 * Must be called with sessions_mutex held.
2489 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2491 return lttng_metadata_printf(session
,
2492 "struct packet_context {\n"
2493 " uint64_clock_monotonic_t timestamp_begin;\n"
2494 " uint64_clock_monotonic_t timestamp_end;\n"
2495 " uint64_t content_size;\n"
2496 " uint64_t packet_size;\n"
2497 " uint64_t packet_seq_num;\n"
2498 " unsigned long events_discarded;\n"
2499 " uint32_t cpu_id;\n"
2506 * id: range: 0 - 30.
2507 * id 31 is reserved to indicate an extended header.
2510 * id: range: 0 - 65534.
2511 * id 65535 is reserved to indicate an extended header.
2513 * Must be called with sessions_mutex held.
2516 int _lttng_event_header_declare(struct lttng_session
*session
)
2518 return lttng_metadata_printf(session
,
2519 "struct event_header_compact {\n"
2520 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2523 " uint27_clock_monotonic_t timestamp;\n"
2527 " uint64_clock_monotonic_t timestamp;\n"
2532 "struct event_header_large {\n"
2533 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2536 " uint32_clock_monotonic_t timestamp;\n"
2540 " uint64_clock_monotonic_t timestamp;\n"
2544 lttng_alignof(uint32_t) * CHAR_BIT
,
2545 lttng_alignof(uint16_t) * CHAR_BIT
2550 * Approximation of NTP time of day to clock monotonic correlation,
2551 * taken at start of trace.
2552 * Yes, this is only an approximation. Yes, we can (and will) do better
2553 * in future versions.
2554 * This function may return a negative offset. It may happen if the
2555 * system sets the REALTIME clock to 0 after boot.
2557 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2561 int64_t measure_clock_offset(void)
2563 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2564 uint64_t tcf
= trace_clock_freq();
2566 unsigned long flags
;
2567 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2568 struct timespec64 rts
= { 0, 0 };
2570 struct timespec rts
= { 0, 0 };
2573 /* Disable interrupts to increase correlation precision. */
2574 local_irq_save(flags
);
2575 monotonic
[0] = trace_clock_read64();
2576 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2577 ktime_get_real_ts64(&rts
);
2579 getnstimeofday(&rts
);
2581 monotonic
[1] = trace_clock_read64();
2582 local_irq_restore(flags
);
2584 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2585 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2586 if (tcf
== NSEC_PER_SEC
) {
2587 realtime
+= rts
.tv_nsec
;
2589 uint64_t n
= rts
.tv_nsec
* tcf
;
2591 do_div(n
, NSEC_PER_SEC
);
2594 offset
= (int64_t) realtime
- monotonic_avg
;
2599 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2607 while (cur
!= '\0') {
2610 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2614 ret
= lttng_metadata_printf(session
, "%c", '\\');
2617 /* We still print the current char */
2620 ret
= lttng_metadata_printf(session
, "%c", cur
);
2634 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2635 const char *field_value
)
2639 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2643 ret
= print_escaped_ctf_string(session
, field_value
);
2647 ret
= lttng_metadata_printf(session
, "\";\n");
2654 * Output metadata into this session's metadata buffers.
2655 * Must be called with sessions_mutex held.
2658 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2660 unsigned char *uuid_c
= session
->uuid
.b
;
2661 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2662 struct lttng_channel
*chan
;
2663 struct lttng_event
*event
;
2666 if (!LTTNG_READ_ONCE(session
->active
))
2669 lttng_metadata_begin(session
);
2671 if (session
->metadata_dumped
)
2674 snprintf(uuid_s
, sizeof(uuid_s
),
2675 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2676 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2677 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2678 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2679 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2681 ret
= lttng_metadata_printf(session
,
2682 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2683 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2684 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2685 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2686 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2687 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2688 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2694 " byte_order = %s;\n"
2695 " packet.header := struct {\n"
2696 " uint32_t magic;\n"
2697 " uint8_t uuid[16];\n"
2698 " uint32_t stream_id;\n"
2699 " uint64_t stream_instance_id;\n"
2702 lttng_alignof(uint8_t) * CHAR_BIT
,
2703 lttng_alignof(uint16_t) * CHAR_BIT
,
2704 lttng_alignof(uint32_t) * CHAR_BIT
,
2705 lttng_alignof(uint64_t) * CHAR_BIT
,
2706 sizeof(unsigned long) * CHAR_BIT
,
2707 lttng_alignof(unsigned long) * CHAR_BIT
,
2711 #if __BYTE_ORDER == __BIG_ENDIAN
2720 ret
= lttng_metadata_printf(session
,
2722 " hostname = \"%s\";\n"
2723 " domain = \"kernel\";\n"
2724 " sysname = \"%s\";\n"
2725 " kernel_release = \"%s\";\n"
2726 " kernel_version = \"%s\";\n"
2727 " tracer_name = \"lttng-modules\";\n"
2728 " tracer_major = %d;\n"
2729 " tracer_minor = %d;\n"
2730 " tracer_patchlevel = %d;\n"
2731 " trace_buffering_scheme = \"global\";\n",
2732 current
->nsproxy
->uts_ns
->name
.nodename
,
2736 LTTNG_MODULES_MAJOR_VERSION
,
2737 LTTNG_MODULES_MINOR_VERSION
,
2738 LTTNG_MODULES_PATCHLEVEL_VERSION
2743 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2746 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2747 session
->creation_time
);
2752 ret
= lttng_metadata_printf(session
, "};\n\n");
2756 ret
= lttng_metadata_printf(session
,
2758 " name = \"%s\";\n",
2764 if (!trace_clock_uuid(clock_uuid_s
)) {
2765 ret
= lttng_metadata_printf(session
,
2766 " uuid = \"%s\";\n",
2773 ret
= lttng_metadata_printf(session
,
2774 " description = \"%s\";\n"
2775 " freq = %llu; /* Frequency, in Hz */\n"
2776 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2779 trace_clock_description(),
2780 (unsigned long long) trace_clock_freq(),
2781 (long long) measure_clock_offset()
2786 ret
= lttng_metadata_printf(session
,
2787 "typealias integer {\n"
2788 " size = 27; align = 1; signed = false;\n"
2789 " map = clock.%s.value;\n"
2790 "} := uint27_clock_monotonic_t;\n"
2792 "typealias integer {\n"
2793 " size = 32; align = %u; signed = false;\n"
2794 " map = clock.%s.value;\n"
2795 "} := uint32_clock_monotonic_t;\n"
2797 "typealias integer {\n"
2798 " size = 64; align = %u; signed = false;\n"
2799 " map = clock.%s.value;\n"
2800 "} := uint64_clock_monotonic_t;\n\n",
2802 lttng_alignof(uint32_t) * CHAR_BIT
,
2804 lttng_alignof(uint64_t) * CHAR_BIT
,
2810 ret
= _lttng_stream_packet_context_declare(session
);
2814 ret
= _lttng_event_header_declare(session
);
2819 list_for_each_entry(chan
, &session
->chan
, list
) {
2820 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2825 list_for_each_entry(event
, &session
->events
, list
) {
2826 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2830 session
->metadata_dumped
= 1;
2832 lttng_metadata_end(session
);
2837 * lttng_transport_register - LTT transport registration
2838 * @transport: transport structure
2840 * Registers a transport which can be used as output to extract the data out of
2841 * LTTng. The module calling this registration function must ensure that no
2842 * trap-inducing code will be executed by the transport functions. E.g.
2843 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2844 * is made visible to the transport function. This registration acts as a
2845 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2846 * after its registration must it synchronize the TLBs.
2848 void lttng_transport_register(struct lttng_transport
*transport
)
2851 * Make sure no page fault can be triggered by the module about to be
2852 * registered. We deal with this here so we don't have to call
2853 * vmalloc_sync_mappings() in each module's init.
2855 wrapper_vmalloc_sync_mappings();
2857 mutex_lock(&sessions_mutex
);
2858 list_add_tail(&transport
->node
, <tng_transport_list
);
2859 mutex_unlock(&sessions_mutex
);
2861 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2864 * lttng_transport_unregister - LTT transport unregistration
2865 * @transport: transport structure
2867 void lttng_transport_unregister(struct lttng_transport
*transport
)
2869 mutex_lock(&sessions_mutex
);
2870 list_del(&transport
->node
);
2871 mutex_unlock(&sessions_mutex
);
2873 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2875 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
2877 enum cpuhp_state lttng_hp_prepare
;
2878 enum cpuhp_state lttng_hp_online
;
2880 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2882 struct lttng_cpuhp_node
*lttng_node
;
2884 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2885 switch (lttng_node
->component
) {
2886 case LTTNG_RING_BUFFER_FRONTEND
:
2888 case LTTNG_RING_BUFFER_BACKEND
:
2889 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2890 case LTTNG_RING_BUFFER_ITER
:
2892 case LTTNG_CONTEXT_PERF_COUNTERS
:
2899 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2901 struct lttng_cpuhp_node
*lttng_node
;
2903 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2904 switch (lttng_node
->component
) {
2905 case LTTNG_RING_BUFFER_FRONTEND
:
2906 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2907 case LTTNG_RING_BUFFER_BACKEND
:
2909 case LTTNG_RING_BUFFER_ITER
:
2911 case LTTNG_CONTEXT_PERF_COUNTERS
:
2912 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2918 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2920 struct lttng_cpuhp_node
*lttng_node
;
2922 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2923 switch (lttng_node
->component
) {
2924 case LTTNG_RING_BUFFER_FRONTEND
:
2925 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2926 case LTTNG_RING_BUFFER_BACKEND
:
2928 case LTTNG_RING_BUFFER_ITER
:
2929 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2930 case LTTNG_CONTEXT_PERF_COUNTERS
:
2931 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2937 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2939 struct lttng_cpuhp_node
*lttng_node
;
2941 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2942 switch (lttng_node
->component
) {
2943 case LTTNG_RING_BUFFER_FRONTEND
:
2944 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2945 case LTTNG_RING_BUFFER_BACKEND
:
2947 case LTTNG_RING_BUFFER_ITER
:
2949 case LTTNG_CONTEXT_PERF_COUNTERS
:
2956 static int __init
lttng_init_cpu_hotplug(void)
2960 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2961 lttng_hotplug_prepare
,
2962 lttng_hotplug_dead
);
2966 lttng_hp_prepare
= ret
;
2967 lttng_rb_set_hp_prepare(ret
);
2969 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
2970 lttng_hotplug_online
,
2971 lttng_hotplug_offline
);
2973 cpuhp_remove_multi_state(lttng_hp_prepare
);
2974 lttng_hp_prepare
= 0;
2977 lttng_hp_online
= ret
;
2978 lttng_rb_set_hp_online(ret
);
2983 static void __exit
lttng_exit_cpu_hotplug(void)
2985 lttng_rb_set_hp_online(0);
2986 cpuhp_remove_multi_state(lttng_hp_online
);
2987 lttng_rb_set_hp_prepare(0);
2988 cpuhp_remove_multi_state(lttng_hp_prepare
);
2991 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
2992 static int lttng_init_cpu_hotplug(void)
2996 static void lttng_exit_cpu_hotplug(void)
2999 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
3002 static int __init
lttng_events_init(void)
3006 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
3009 ret
= wrapper_get_pfnblock_flags_mask_init();
3012 ret
= wrapper_get_pageblock_flags_mask_init();
3015 ret
= lttng_probes_init();
3018 ret
= lttng_context_init();
3021 ret
= lttng_tracepoint_init();
3024 event_cache
= KMEM_CACHE(lttng_event
, 0);
3029 ret
= lttng_abi_init();
3032 ret
= lttng_logger_init();
3035 ret
= lttng_init_cpu_hotplug();
3038 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3039 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3040 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3041 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3042 LTTNG_MODULES_EXTRAVERSION
,
3044 #ifdef LTTNG_EXTRA_VERSION_GIT
3045 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3049 #ifdef LTTNG_EXTRA_VERSION_NAME
3050 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3057 lttng_logger_exit();
3061 kmem_cache_destroy(event_cache
);
3063 lttng_tracepoint_exit();
3065 lttng_context_exit();
3066 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3067 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3068 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3069 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3070 LTTNG_MODULES_EXTRAVERSION
,
3072 #ifdef LTTNG_EXTRA_VERSION_GIT
3073 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3077 #ifdef LTTNG_EXTRA_VERSION_NAME
3078 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3085 module_init(lttng_events_init
);
3087 static void __exit
lttng_events_exit(void)
3089 struct lttng_session
*session
, *tmpsession
;
3091 lttng_exit_cpu_hotplug();
3092 lttng_logger_exit();
3094 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3095 lttng_session_destroy(session
);
3096 kmem_cache_destroy(event_cache
);
3097 lttng_tracepoint_exit();
3098 lttng_context_exit();
3099 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3100 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3101 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3102 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3103 LTTNG_MODULES_EXTRAVERSION
,
3105 #ifdef LTTNG_EXTRA_VERSION_GIT
3106 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3110 #ifdef LTTNG_EXTRA_VERSION_NAME
3111 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3117 module_exit(lttng_events_exit
);
3119 #include "extra_version/patches.i"
3120 #ifdef LTTNG_EXTRA_VERSION_GIT
3121 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3123 #ifdef LTTNG_EXTRA_VERSION_NAME
3124 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3126 MODULE_LICENSE("GPL and additional rights");
3127 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3128 MODULE_DESCRIPTION("LTTng tracer");
3129 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3130 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3131 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3132 LTTNG_MODULES_EXTRAVERSION
);