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 (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
87 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
88 #ifdef CONFIG_PREEMPT_RT_FULL
91 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
92 #ifdef CONFIG_PREEMPT_RT
95 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
98 void lttng_lock_sessions(void)
100 mutex_lock(&sessions_mutex
);
103 void lttng_unlock_sessions(void)
105 mutex_unlock(&sessions_mutex
);
109 * Called with sessions lock held.
111 int lttng_session_active(void)
113 struct lttng_session
*iter
;
115 list_for_each_entry(iter
, &sessions
, list
) {
122 struct lttng_session
*lttng_session_create(void)
124 struct lttng_session
*session
;
125 struct lttng_metadata_cache
*metadata_cache
;
128 mutex_lock(&sessions_mutex
);
129 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
132 INIT_LIST_HEAD(&session
->chan
);
133 INIT_LIST_HEAD(&session
->events
);
134 lttng_guid_gen(&session
->uuid
);
136 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
139 goto err_free_session
;
140 metadata_cache
->data
= lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
141 if (!metadata_cache
->data
)
143 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
144 kref_init(&metadata_cache
->refcount
);
145 mutex_init(&metadata_cache
->lock
);
146 session
->metadata_cache
= metadata_cache
;
147 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
148 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
149 sizeof(metadata_cache
->uuid
));
150 INIT_LIST_HEAD(&session
->enablers_head
);
151 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
152 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
153 list_add(&session
->list
, &sessions
);
154 mutex_unlock(&sessions_mutex
);
158 kfree(metadata_cache
);
160 lttng_kvfree(session
);
162 mutex_unlock(&sessions_mutex
);
166 void metadata_cache_destroy(struct kref
*kref
)
168 struct lttng_metadata_cache
*cache
=
169 container_of(kref
, struct lttng_metadata_cache
, refcount
);
174 void lttng_session_destroy(struct lttng_session
*session
)
176 struct lttng_channel
*chan
, *tmpchan
;
177 struct lttng_event
*event
, *tmpevent
;
178 struct lttng_metadata_stream
*metadata_stream
;
179 struct lttng_enabler
*enabler
, *tmpenabler
;
182 mutex_lock(&sessions_mutex
);
183 WRITE_ONCE(session
->active
, 0);
184 list_for_each_entry(chan
, &session
->chan
, list
) {
185 ret
= lttng_syscalls_unregister(chan
);
188 list_for_each_entry(event
, &session
->events
, list
) {
189 ret
= _lttng_event_unregister(event
);
192 synchronize_trace(); /* Wait for in-flight events to complete */
193 list_for_each_entry(chan
, &session
->chan
, list
) {
194 ret
= lttng_syscalls_destroy(chan
);
197 list_for_each_entry_safe(enabler
, tmpenabler
,
198 &session
->enablers_head
, node
)
199 lttng_enabler_destroy(enabler
);
200 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
201 _lttng_event_destroy(event
);
202 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
203 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
204 _lttng_channel_destroy(chan
);
206 mutex_lock(&session
->metadata_cache
->lock
);
207 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
208 _lttng_metadata_channel_hangup(metadata_stream
);
209 mutex_unlock(&session
->metadata_cache
->lock
);
210 if (session
->pid_tracker
)
211 lttng_pid_tracker_destroy(session
->pid_tracker
);
212 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
213 list_del(&session
->list
);
214 mutex_unlock(&sessions_mutex
);
215 lttng_kvfree(session
);
218 int lttng_session_statedump(struct lttng_session
*session
)
222 mutex_lock(&sessions_mutex
);
223 ret
= lttng_statedump_start(session
);
224 mutex_unlock(&sessions_mutex
);
228 int lttng_session_enable(struct lttng_session
*session
)
231 struct lttng_channel
*chan
;
233 mutex_lock(&sessions_mutex
);
234 if (session
->active
) {
239 /* Set transient enabler state to "enabled" */
243 * Snapshot the number of events per channel to know the type of header
246 list_for_each_entry(chan
, &session
->chan
, list
) {
247 if (chan
->header_type
)
248 continue; /* don't change it if session stop/restart */
249 if (chan
->free_event_id
< 31)
250 chan
->header_type
= 1; /* compact */
252 chan
->header_type
= 2; /* large */
255 /* We need to sync enablers with session before activation. */
256 lttng_session_sync_enablers(session
);
258 /* Clear each stream's quiescent state. */
259 list_for_each_entry(chan
, &session
->chan
, list
) {
260 if (chan
->channel_type
!= METADATA_CHANNEL
)
261 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
264 WRITE_ONCE(session
->active
, 1);
265 WRITE_ONCE(session
->been_active
, 1);
266 ret
= _lttng_session_metadata_statedump(session
);
268 WRITE_ONCE(session
->active
, 0);
271 ret
= lttng_statedump_start(session
);
273 WRITE_ONCE(session
->active
, 0);
275 mutex_unlock(&sessions_mutex
);
279 int lttng_session_disable(struct lttng_session
*session
)
282 struct lttng_channel
*chan
;
284 mutex_lock(&sessions_mutex
);
285 if (!session
->active
) {
289 WRITE_ONCE(session
->active
, 0);
291 /* Set transient enabler state to "disabled" */
293 lttng_session_sync_enablers(session
);
295 /* Set each stream's quiescent state. */
296 list_for_each_entry(chan
, &session
->chan
, list
) {
297 if (chan
->channel_type
!= METADATA_CHANNEL
)
298 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
301 mutex_unlock(&sessions_mutex
);
305 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
308 struct lttng_channel
*chan
;
309 struct lttng_event
*event
;
310 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
311 struct lttng_metadata_stream
*stream
;
313 mutex_lock(&sessions_mutex
);
314 if (!session
->active
) {
319 mutex_lock(&cache
->lock
);
320 memset(cache
->data
, 0, cache
->cache_alloc
);
321 cache
->metadata_written
= 0;
323 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
324 stream
->metadata_out
= 0;
325 stream
->metadata_in
= 0;
327 mutex_unlock(&cache
->lock
);
329 session
->metadata_dumped
= 0;
330 list_for_each_entry(chan
, &session
->chan
, list
) {
331 chan
->metadata_dumped
= 0;
334 list_for_each_entry(event
, &session
->events
, list
) {
335 event
->metadata_dumped
= 0;
338 ret
= _lttng_session_metadata_statedump(session
);
341 mutex_unlock(&sessions_mutex
);
345 int lttng_channel_enable(struct lttng_channel
*channel
)
349 mutex_lock(&sessions_mutex
);
350 if (channel
->channel_type
== METADATA_CHANNEL
) {
354 if (channel
->enabled
) {
358 /* Set transient enabler state to "enabled" */
360 lttng_session_sync_enablers(channel
->session
);
361 /* Set atomically the state to "enabled" */
362 WRITE_ONCE(channel
->enabled
, 1);
364 mutex_unlock(&sessions_mutex
);
368 int lttng_channel_disable(struct lttng_channel
*channel
)
372 mutex_lock(&sessions_mutex
);
373 if (channel
->channel_type
== METADATA_CHANNEL
) {
377 if (!channel
->enabled
) {
381 /* Set atomically the state to "disabled" */
382 WRITE_ONCE(channel
->enabled
, 0);
383 /* Set transient enabler state to "enabled" */
385 lttng_session_sync_enablers(channel
->session
);
387 mutex_unlock(&sessions_mutex
);
391 int lttng_event_enable(struct lttng_event
*event
)
395 mutex_lock(&sessions_mutex
);
396 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
400 if (event
->enabled
) {
404 switch (event
->instrumentation
) {
405 case LTTNG_KERNEL_TRACEPOINT
:
406 case LTTNG_KERNEL_SYSCALL
:
409 case LTTNG_KERNEL_KPROBE
:
410 case LTTNG_KERNEL_UPROBE
:
411 case LTTNG_KERNEL_NOOP
:
412 WRITE_ONCE(event
->enabled
, 1);
414 case LTTNG_KERNEL_KRETPROBE
:
415 ret
= lttng_kretprobes_event_enable_state(event
, 1);
417 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
423 mutex_unlock(&sessions_mutex
);
427 int lttng_event_disable(struct lttng_event
*event
)
431 mutex_lock(&sessions_mutex
);
432 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
436 if (!event
->enabled
) {
440 switch (event
->instrumentation
) {
441 case LTTNG_KERNEL_TRACEPOINT
:
442 case LTTNG_KERNEL_SYSCALL
:
445 case LTTNG_KERNEL_KPROBE
:
446 case LTTNG_KERNEL_UPROBE
:
447 case LTTNG_KERNEL_NOOP
:
448 WRITE_ONCE(event
->enabled
, 0);
450 case LTTNG_KERNEL_KRETPROBE
:
451 ret
= lttng_kretprobes_event_enable_state(event
, 0);
453 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
459 mutex_unlock(&sessions_mutex
);
463 static struct lttng_transport
*lttng_transport_find(const char *name
)
465 struct lttng_transport
*transport
;
467 list_for_each_entry(transport
, <tng_transport_list
, node
) {
468 if (!strcmp(transport
->name
, name
))
474 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
475 const char *transport_name
,
477 size_t subbuf_size
, size_t num_subbuf
,
478 unsigned int switch_timer_interval
,
479 unsigned int read_timer_interval
,
480 enum channel_type channel_type
)
482 struct lttng_channel
*chan
;
483 struct lttng_transport
*transport
= NULL
;
485 mutex_lock(&sessions_mutex
);
486 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
487 goto active
; /* Refuse to add channel to active session */
488 transport
= lttng_transport_find(transport_name
);
490 printk(KERN_WARNING
"LTTng transport %s not found\n",
494 if (!try_module_get(transport
->owner
)) {
495 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
498 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
501 chan
->session
= session
;
502 chan
->id
= session
->free_chan_id
++;
503 chan
->ops
= &transport
->ops
;
505 * Note: the channel creation op already writes into the packet
506 * headers. Therefore the "chan" information used as input
507 * should be already accessible.
509 chan
->chan
= transport
->ops
.channel_create(transport_name
,
510 chan
, buf_addr
, subbuf_size
, num_subbuf
,
511 switch_timer_interval
, read_timer_interval
);
516 chan
->transport
= transport
;
517 chan
->channel_type
= channel_type
;
518 list_add(&chan
->list
, &session
->chan
);
519 mutex_unlock(&sessions_mutex
);
526 module_put(transport
->owner
);
529 mutex_unlock(&sessions_mutex
);
534 * Only used internally at session destruction for per-cpu channels, and
535 * when metadata channel is released.
536 * Needs to be called with sessions mutex held.
539 void _lttng_channel_destroy(struct lttng_channel
*chan
)
541 chan
->ops
->channel_destroy(chan
->chan
);
542 module_put(chan
->transport
->owner
);
543 list_del(&chan
->list
);
544 lttng_destroy_context(chan
->ctx
);
548 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
550 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
552 /* Protect the metadata cache with the sessions_mutex. */
553 mutex_lock(&sessions_mutex
);
554 _lttng_channel_destroy(chan
);
555 mutex_unlock(&sessions_mutex
);
557 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
560 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
562 stream
->finalized
= 1;
563 wake_up_interruptible(&stream
->read_wait
);
567 * Supports event creation while tracing session is active.
568 * Needs to be called with sessions mutex held.
570 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
571 struct lttng_kernel_event
*event_param
,
573 const struct lttng_event_desc
*event_desc
,
574 enum lttng_kernel_instrumentation itype
)
576 struct lttng_session
*session
= chan
->session
;
577 struct lttng_event
*event
;
578 const char *event_name
;
579 struct hlist_head
*head
;
584 if (chan
->free_event_id
== -1U) {
590 case LTTNG_KERNEL_TRACEPOINT
:
591 event_name
= event_desc
->name
;
593 case LTTNG_KERNEL_KPROBE
:
594 case LTTNG_KERNEL_UPROBE
:
595 case LTTNG_KERNEL_KRETPROBE
:
596 case LTTNG_KERNEL_NOOP
:
597 case LTTNG_KERNEL_SYSCALL
:
598 event_name
= event_param
->name
;
600 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
606 name_len
= strlen(event_name
);
607 hash
= jhash(event_name
, name_len
, 0);
608 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
609 lttng_hlist_for_each_entry(event
, head
, hlist
) {
610 WARN_ON_ONCE(!event
->desc
);
611 if (!strncmp(event
->desc
->name
, event_name
,
612 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
613 && chan
== event
->chan
) {
619 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
625 event
->filter
= filter
;
626 event
->id
= chan
->free_event_id
++;
627 event
->instrumentation
= itype
;
628 event
->evtype
= LTTNG_TYPE_EVENT
;
629 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
630 INIT_LIST_HEAD(&event
->enablers_ref_head
);
633 case LTTNG_KERNEL_TRACEPOINT
:
634 /* Event will be enabled by enabler sync. */
636 event
->registered
= 0;
637 event
->desc
= lttng_event_get(event_name
);
642 /* Populate lttng_event structure before event registration. */
645 case LTTNG_KERNEL_KPROBE
:
647 * Needs to be explicitly enabled after creation, since
648 * we may want to apply filters.
651 event
->registered
= 1;
653 * Populate lttng_event structure before event
657 ret
= lttng_kprobes_register(event_name
,
658 event_param
->u
.kprobe
.symbol_name
,
659 event_param
->u
.kprobe
.offset
,
660 event_param
->u
.kprobe
.addr
,
666 ret
= try_module_get(event
->desc
->owner
);
669 case LTTNG_KERNEL_KRETPROBE
:
671 struct lttng_event
*event_return
;
673 /* kretprobe defines 2 events */
675 * Needs to be explicitly enabled after creation, since
676 * we may want to apply filters.
679 event
->registered
= 1;
681 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
686 event_return
->chan
= chan
;
687 event_return
->filter
= filter
;
688 event_return
->id
= chan
->free_event_id
++;
689 event_return
->enabled
= 0;
690 event_return
->registered
= 1;
691 event_return
->instrumentation
= itype
;
693 * Populate lttng_event structure before kretprobe registration.
696 ret
= lttng_kretprobes_register(event_name
,
697 event_param
->u
.kretprobe
.symbol_name
,
698 event_param
->u
.kretprobe
.offset
,
699 event_param
->u
.kretprobe
.addr
,
700 event
, event_return
);
702 kmem_cache_free(event_cache
, event_return
);
706 /* Take 2 refs on the module: one per event. */
707 ret
= try_module_get(event
->desc
->owner
);
709 ret
= try_module_get(event
->desc
->owner
);
711 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
713 WARN_ON_ONCE(ret
> 0);
715 kmem_cache_free(event_cache
, event_return
);
716 module_put(event
->desc
->owner
);
717 module_put(event
->desc
->owner
);
718 goto statedump_error
;
720 list_add(&event_return
->list
, &chan
->session
->events
);
723 case LTTNG_KERNEL_NOOP
:
724 case LTTNG_KERNEL_SYSCALL
:
726 * Needs to be explicitly enabled after creation, since
727 * we may want to apply filters.
730 event
->registered
= 0;
731 event
->desc
= event_desc
;
732 switch (event_param
->u
.syscall
.entryexit
) {
733 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
736 case LTTNG_KERNEL_SYSCALL_ENTRY
:
737 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
739 case LTTNG_KERNEL_SYSCALL_EXIT
:
740 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
743 switch (event_param
->u
.syscall
.abi
) {
744 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
747 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
748 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
750 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
751 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
759 case LTTNG_KERNEL_UPROBE
:
761 * Needs to be explicitly enabled after creation, since
762 * we may want to apply filters.
765 event
->registered
= 1;
768 * Populate lttng_event structure before event
773 ret
= lttng_uprobes_register(event_param
->name
,
774 event_param
->u
.uprobe
.fd
,
778 ret
= try_module_get(event
->desc
->owner
);
781 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
787 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
788 WARN_ON_ONCE(ret
> 0);
790 goto statedump_error
;
792 hlist_add_head(&event
->hlist
, head
);
793 list_add(&event
->list
, &chan
->session
->events
);
797 /* If a statedump error occurs, events will not be readable. */
799 kmem_cache_free(event_cache
, event
);
807 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
808 struct lttng_kernel_event
*event_param
,
810 const struct lttng_event_desc
*event_desc
,
811 enum lttng_kernel_instrumentation itype
)
813 struct lttng_event
*event
;
815 mutex_lock(&sessions_mutex
);
816 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
818 mutex_unlock(&sessions_mutex
);
822 /* Only used for tracepoints for now. */
824 void register_event(struct lttng_event
*event
)
826 const struct lttng_event_desc
*desc
;
829 if (event
->registered
)
833 switch (event
->instrumentation
) {
834 case LTTNG_KERNEL_TRACEPOINT
:
835 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
836 desc
->probe_callback
,
839 case LTTNG_KERNEL_SYSCALL
:
840 ret
= lttng_syscall_filter_enable(event
->chan
, event
);
842 case LTTNG_KERNEL_KPROBE
:
843 case LTTNG_KERNEL_UPROBE
:
844 case LTTNG_KERNEL_KRETPROBE
:
845 case LTTNG_KERNEL_NOOP
:
848 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
853 event
->registered
= 1;
857 * Only used internally at session destruction.
859 int _lttng_event_unregister(struct lttng_event
*event
)
861 const struct lttng_event_desc
*desc
;
864 if (!event
->registered
)
868 switch (event
->instrumentation
) {
869 case LTTNG_KERNEL_TRACEPOINT
:
870 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
871 event
->desc
->probe_callback
,
874 case LTTNG_KERNEL_KPROBE
:
875 lttng_kprobes_unregister(event
);
878 case LTTNG_KERNEL_KRETPROBE
:
879 lttng_kretprobes_unregister(event
);
882 case LTTNG_KERNEL_SYSCALL
:
883 ret
= lttng_syscall_filter_disable(event
->chan
, event
);
885 case LTTNG_KERNEL_NOOP
:
888 case LTTNG_KERNEL_UPROBE
:
889 lttng_uprobes_unregister(event
);
892 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
897 event
->registered
= 0;
902 * Only used internally at session destruction.
905 void _lttng_event_destroy(struct lttng_event
*event
)
907 switch (event
->instrumentation
) {
908 case LTTNG_KERNEL_TRACEPOINT
:
909 lttng_event_put(event
->desc
);
911 case LTTNG_KERNEL_KPROBE
:
912 module_put(event
->desc
->owner
);
913 lttng_kprobes_destroy_private(event
);
915 case LTTNG_KERNEL_KRETPROBE
:
916 module_put(event
->desc
->owner
);
917 lttng_kretprobes_destroy_private(event
);
919 case LTTNG_KERNEL_NOOP
:
920 case LTTNG_KERNEL_SYSCALL
:
922 case LTTNG_KERNEL_UPROBE
:
923 module_put(event
->desc
->owner
);
924 lttng_uprobes_destroy_private(event
);
926 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
930 list_del(&event
->list
);
931 lttng_destroy_context(event
->ctx
);
932 kmem_cache_free(event_cache
, event
);
935 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
941 mutex_lock(&sessions_mutex
);
943 /* track all pids: destroy tracker. */
944 if (session
->pid_tracker
) {
945 struct lttng_pid_tracker
*lpf
;
947 lpf
= session
->pid_tracker
;
948 rcu_assign_pointer(session
->pid_tracker
, NULL
);
950 lttng_pid_tracker_destroy(lpf
);
954 if (!session
->pid_tracker
) {
955 struct lttng_pid_tracker
*lpf
;
957 lpf
= lttng_pid_tracker_create();
962 ret
= lttng_pid_tracker_add(lpf
, pid
);
963 rcu_assign_pointer(session
->pid_tracker
, lpf
);
965 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
969 mutex_unlock(&sessions_mutex
);
973 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
979 mutex_lock(&sessions_mutex
);
981 /* untrack all pids: replace by empty tracker. */
982 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
983 struct lttng_pid_tracker
*lpf
;
985 lpf
= lttng_pid_tracker_create();
990 rcu_assign_pointer(session
->pid_tracker
, lpf
);
993 lttng_pid_tracker_destroy(old_lpf
);
996 if (!session
->pid_tracker
) {
1000 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
1003 mutex_unlock(&sessions_mutex
);
1008 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
1010 struct lttng_session
*session
= m
->private;
1011 struct lttng_pid_tracker
*lpf
;
1012 struct lttng_pid_hash_node
*e
;
1015 mutex_lock(&sessions_mutex
);
1016 lpf
= session
->pid_tracker
;
1018 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
1019 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
1021 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1027 /* PID tracker disabled. */
1028 if (iter
>= *pos
&& iter
== 0) {
1029 return session
; /* empty tracker */
1037 /* Called with sessions_mutex held. */
1039 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1041 struct lttng_session
*session
= m
->private;
1042 struct lttng_pid_tracker
*lpf
;
1043 struct lttng_pid_hash_node
*e
;
1047 lpf
= session
->pid_tracker
;
1049 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
1050 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
1052 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1053 if (iter
++ >= *ppos
)
1058 /* PID tracker disabled. */
1059 if (iter
>= *ppos
&& iter
== 0)
1060 return session
; /* empty tracker */
1069 void pid_list_stop(struct seq_file
*m
, void *p
)
1071 mutex_unlock(&sessions_mutex
);
1075 int pid_list_show(struct seq_file
*m
, void *p
)
1079 if (p
== m
->private) {
1080 /* Tracker disabled. */
1083 const struct lttng_pid_hash_node
*e
= p
;
1085 pid
= lttng_pid_tracker_get_node_pid(e
);
1087 seq_printf(m
, "process { pid = %d; };\n", pid
);
1092 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
1093 .start
= pid_list_start
,
1094 .next
= pid_list_next
,
1095 .stop
= pid_list_stop
,
1096 .show
= pid_list_show
,
1100 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
1102 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
1106 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
1108 struct seq_file
*m
= file
->private_data
;
1109 struct lttng_session
*session
= m
->private;
1112 WARN_ON_ONCE(!session
);
1113 ret
= seq_release(inode
, file
);
1114 if (!ret
&& session
)
1115 fput(session
->file
);
1119 const struct file_operations lttng_tracker_pids_list_fops
= {
1120 .owner
= THIS_MODULE
,
1121 .open
= lttng_tracker_pids_list_open
,
1123 .llseek
= seq_lseek
,
1124 .release
= lttng_tracker_pids_list_release
,
1127 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
1129 struct file
*tracker_pids_list_file
;
1133 file_fd
= lttng_get_unused_fd();
1139 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
1140 <tng_tracker_pids_list_fops
,
1142 if (IS_ERR(tracker_pids_list_file
)) {
1143 ret
= PTR_ERR(tracker_pids_list_file
);
1146 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1148 goto refcount_error
;
1150 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1153 m
= tracker_pids_list_file
->private_data
;
1154 m
->private = session
;
1155 fd_install(file_fd
, tracker_pids_list_file
);
1160 atomic_long_dec(&session
->file
->f_count
);
1162 fput(tracker_pids_list_file
);
1164 put_unused_fd(file_fd
);
1170 * Enabler management.
1173 int lttng_match_enabler_star_glob(const char *desc_name
,
1174 const char *pattern
)
1176 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1177 desc_name
, LTTNG_SIZE_MAX
))
1183 int lttng_match_enabler_name(const char *desc_name
,
1186 if (strcmp(desc_name
, name
))
1192 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1193 struct lttng_enabler
*enabler
)
1195 const char *desc_name
, *enabler_name
;
1196 bool compat
= false, entry
= false;
1198 enabler_name
= enabler
->event_param
.name
;
1199 switch (enabler
->event_param
.instrumentation
) {
1200 case LTTNG_KERNEL_TRACEPOINT
:
1201 desc_name
= desc
->name
;
1202 switch (enabler
->type
) {
1203 case LTTNG_ENABLER_STAR_GLOB
:
1204 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1205 case LTTNG_ENABLER_NAME
:
1206 return lttng_match_enabler_name(desc_name
, enabler_name
);
1211 case LTTNG_KERNEL_SYSCALL
:
1212 desc_name
= desc
->name
;
1213 if (!strncmp(desc_name
, "compat_", strlen("compat_"))) {
1214 desc_name
+= strlen("compat_");
1217 if (!strncmp(desc_name
, "syscall_exit_",
1218 strlen("syscall_exit_"))) {
1219 desc_name
+= strlen("syscall_exit_");
1220 } else if (!strncmp(desc_name
, "syscall_entry_",
1221 strlen("syscall_entry_"))) {
1222 desc_name
+= strlen("syscall_entry_");
1228 switch (enabler
->event_param
.u
.syscall
.entryexit
) {
1229 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1231 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1235 case LTTNG_KERNEL_SYSCALL_EXIT
:
1242 switch (enabler
->event_param
.u
.syscall
.abi
) {
1243 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1245 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1249 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1256 switch (enabler
->event_param
.u
.syscall
.match
) {
1257 case LTTNG_SYSCALL_MATCH_NAME
:
1258 switch (enabler
->type
) {
1259 case LTTNG_ENABLER_STAR_GLOB
:
1260 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1261 case LTTNG_ENABLER_NAME
:
1262 return lttng_match_enabler_name(desc_name
, enabler_name
);
1267 case LTTNG_SYSCALL_MATCH_NR
:
1268 return -EINVAL
; /* Not implemented. */
1280 int lttng_event_match_enabler(struct lttng_event
*event
,
1281 struct lttng_enabler
*enabler
)
1283 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1285 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1286 && event
->chan
== enabler
->chan
)
1293 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1294 struct lttng_enabler
*enabler
)
1296 struct lttng_enabler_ref
*enabler_ref
;
1298 list_for_each_entry(enabler_ref
,
1299 &event
->enablers_ref_head
, node
) {
1300 if (enabler_ref
->ref
== enabler
)
1307 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1309 struct lttng_session
*session
= enabler
->chan
->session
;
1310 struct lttng_probe_desc
*probe_desc
;
1311 const struct lttng_event_desc
*desc
;
1313 struct list_head
*probe_list
;
1315 probe_list
= lttng_get_probe_list_head();
1317 * For each probe event, if we find that a probe event matches
1318 * our enabler, create an associated lttng_event if not
1321 list_for_each_entry(probe_desc
, probe_list
, head
) {
1322 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1324 struct hlist_head
*head
;
1325 const char *event_name
;
1328 struct lttng_event
*event
;
1330 desc
= probe_desc
->event_desc
[i
];
1331 if (!lttng_desc_match_enabler(desc
, enabler
))
1333 event_name
= desc
->name
;
1334 name_len
= strlen(event_name
);
1337 * Check if already created.
1339 hash
= jhash(event_name
, name_len
, 0);
1340 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1341 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1342 if (event
->desc
== desc
1343 && event
->chan
== enabler
->chan
)
1350 * We need to create an event for this
1353 event
= _lttng_event_create(enabler
->chan
,
1355 LTTNG_KERNEL_TRACEPOINT
);
1357 printk(KERN_INFO
"Unable to create event %s\n",
1358 probe_desc
->event_desc
[i
]->name
);
1365 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1369 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1374 * Create struct lttng_event if it is missing and present in the list of
1375 * tracepoint probes.
1376 * Should be called with sessions mutex held.
1379 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1381 switch (enabler
->event_param
.instrumentation
) {
1382 case LTTNG_KERNEL_TRACEPOINT
:
1383 lttng_create_tracepoint_if_missing(enabler
);
1385 case LTTNG_KERNEL_SYSCALL
:
1386 lttng_create_syscall_if_missing(enabler
);
1395 * Create events associated with an enabler (if not already present),
1396 * and add backward reference from the event to the enabler.
1397 * Should be called with sessions mutex held.
1400 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1402 struct lttng_channel
*chan
= enabler
->chan
;
1403 struct lttng_session
*session
= chan
->session
;
1404 struct lttng_event
*event
;
1406 if (enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
1407 enabler
->event_param
.u
.syscall
.entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
&&
1408 enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
1409 enabler
->event_param
.u
.syscall
.match
== LTTNG_SYSCALL_MATCH_NAME
&&
1410 !strcmp(enabler
->event_param
.name
, "*")) {
1411 if (enabler
->enabled
)
1412 WRITE_ONCE(chan
->syscall_all
, 1);
1414 WRITE_ONCE(chan
->syscall_all
, 0);
1417 /* First ensure that probe events are created for this enabler. */
1418 lttng_create_event_if_missing(enabler
);
1420 /* For each event matching enabler in session event list. */
1421 list_for_each_entry(event
, &session
->events
, list
) {
1422 struct lttng_enabler_ref
*enabler_ref
;
1424 if (!lttng_event_match_enabler(event
, enabler
))
1426 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1429 * If no backward ref, create it.
1430 * Add backward ref from event to enabler.
1432 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1435 enabler_ref
->ref
= enabler
;
1436 list_add(&enabler_ref
->node
,
1437 &event
->enablers_ref_head
);
1441 * Link filter bytecodes if not linked yet.
1443 lttng_enabler_event_link_bytecode(event
, enabler
);
1445 /* TODO: merge event context. */
1451 * Called at module load: connect the probe on all enablers matching
1453 * Called with sessions lock held.
1455 int lttng_fix_pending_events(void)
1457 struct lttng_session
*session
;
1459 list_for_each_entry(session
, &sessions
, list
)
1460 lttng_session_lazy_sync_enablers(session
);
1464 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1465 struct lttng_kernel_event
*event_param
,
1466 struct lttng_channel
*chan
)
1468 struct lttng_enabler
*enabler
;
1470 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1473 enabler
->type
= type
;
1474 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1475 memcpy(&enabler
->event_param
, event_param
,
1476 sizeof(enabler
->event_param
));
1477 enabler
->chan
= chan
;
1479 enabler
->enabled
= 0;
1480 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1481 mutex_lock(&sessions_mutex
);
1482 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1483 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1484 mutex_unlock(&sessions_mutex
);
1488 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1490 mutex_lock(&sessions_mutex
);
1491 enabler
->enabled
= 1;
1492 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1493 mutex_unlock(&sessions_mutex
);
1497 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1499 mutex_lock(&sessions_mutex
);
1500 enabler
->enabled
= 0;
1501 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1502 mutex_unlock(&sessions_mutex
);
1506 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1507 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1509 struct lttng_filter_bytecode_node
*bytecode_node
;
1510 uint32_t bytecode_len
;
1513 ret
= get_user(bytecode_len
, &bytecode
->len
);
1516 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1520 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1521 sizeof(*bytecode
) + bytecode_len
);
1524 bytecode_node
->enabler
= enabler
;
1525 /* Enforce length based on allocated size */
1526 bytecode_node
->bc
.len
= bytecode_len
;
1527 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1528 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1532 kfree(bytecode_node
);
1536 int lttng_event_add_callsite(struct lttng_event
*event
,
1537 struct lttng_kernel_event_callsite __user
*callsite
)
1540 switch (event
->instrumentation
) {
1541 case LTTNG_KERNEL_UPROBE
:
1542 return lttng_uprobes_add_callsite(event
, callsite
);
1548 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1549 struct lttng_kernel_context
*context_param
)
1555 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1557 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1559 /* Destroy filter bytecode */
1560 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1561 &enabler
->filter_bytecode_head
, node
) {
1565 /* Destroy contexts */
1566 lttng_destroy_context(enabler
->ctx
);
1568 list_del(&enabler
->node
);
1573 * lttng_session_sync_enablers should be called just before starting a
1575 * Should be called with sessions mutex held.
1578 void lttng_session_sync_enablers(struct lttng_session
*session
)
1580 struct lttng_enabler
*enabler
;
1581 struct lttng_event
*event
;
1583 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1584 lttng_enabler_ref_events(enabler
);
1586 * For each event, if at least one of its enablers is enabled,
1587 * and its channel and session transient states are enabled, we
1588 * enable the event, else we disable it.
1590 list_for_each_entry(event
, &session
->events
, list
) {
1591 struct lttng_enabler_ref
*enabler_ref
;
1592 struct lttng_bytecode_runtime
*runtime
;
1593 int enabled
= 0, has_enablers_without_bytecode
= 0;
1595 switch (event
->instrumentation
) {
1596 case LTTNG_KERNEL_TRACEPOINT
:
1597 case LTTNG_KERNEL_SYSCALL
:
1599 list_for_each_entry(enabler_ref
,
1600 &event
->enablers_ref_head
, node
) {
1601 if (enabler_ref
->ref
->enabled
) {
1608 /* Not handled with lazy sync. */
1612 * Enabled state is based on union of enablers, with
1613 * intesection of session and channel transient enable
1616 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1618 WRITE_ONCE(event
->enabled
, enabled
);
1620 * Sync tracepoint registration with event enabled
1624 register_event(event
);
1626 _lttng_event_unregister(event
);
1629 /* Check if has enablers without bytecode enabled */
1630 list_for_each_entry(enabler_ref
,
1631 &event
->enablers_ref_head
, node
) {
1632 if (enabler_ref
->ref
->enabled
1633 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1634 has_enablers_without_bytecode
= 1;
1638 event
->has_enablers_without_bytecode
=
1639 has_enablers_without_bytecode
;
1641 /* Enable filters */
1642 list_for_each_entry(runtime
,
1643 &event
->bytecode_runtime_head
, node
)
1644 lttng_filter_sync_state(runtime
);
1649 * Apply enablers to session events, adding events to session if need
1650 * be. It is required after each modification applied to an active
1651 * session, and right before session "start".
1652 * "lazy" sync means we only sync if required.
1653 * Should be called with sessions mutex held.
1656 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1658 /* We can skip if session is not active */
1659 if (!session
->active
)
1661 lttng_session_sync_enablers(session
);
1665 * Serialize at most one packet worth of metadata into a metadata
1667 * We grab the metadata cache mutex to get exclusive access to our metadata
1668 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1669 * allows us to do racy operations such as looking for remaining space left in
1670 * packet and write, since mutual exclusion protects us from concurrent writes.
1671 * Mutual exclusion on the metadata cache allow us to read the cache content
1672 * without racing against reallocation of the cache by updates.
1673 * Returns the number of bytes written in the channel, 0 if no data
1674 * was written and a negative value on error.
1676 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1677 struct channel
*chan
, bool *coherent
)
1679 struct lib_ring_buffer_ctx ctx
;
1681 size_t len
, reserve_len
;
1684 * Ensure we support mutiple get_next / put sequences followed by
1685 * put_next. The metadata cache lock protects reading the metadata
1686 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1687 * "flush" operations on the buffer invoked by different processes.
1688 * Moreover, since the metadata cache memory can be reallocated, we
1689 * need to have exclusive access against updates even though we only
1692 mutex_lock(&stream
->metadata_cache
->lock
);
1693 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1694 if (stream
->metadata_in
!= stream
->metadata_out
)
1697 /* Metadata regenerated, change the version. */
1698 if (stream
->metadata_cache
->version
!= stream
->version
)
1699 stream
->version
= stream
->metadata_cache
->version
;
1701 len
= stream
->metadata_cache
->metadata_written
-
1702 stream
->metadata_in
;
1705 reserve_len
= min_t(size_t,
1706 stream
->transport
->ops
.packet_avail_size(chan
),
1708 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1711 * If reservation failed, return an error to the caller.
1713 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1715 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1716 stream
->coherent
= false;
1719 stream
->transport
->ops
.event_write(&ctx
,
1720 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1722 stream
->transport
->ops
.event_commit(&ctx
);
1723 stream
->metadata_in
+= reserve_len
;
1724 if (reserve_len
< len
)
1725 stream
->coherent
= false;
1727 stream
->coherent
= true;
1732 *coherent
= stream
->coherent
;
1733 mutex_unlock(&stream
->metadata_cache
->lock
);
1738 void lttng_metadata_begin(struct lttng_session
*session
)
1740 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1741 mutex_lock(&session
->metadata_cache
->lock
);
1745 void lttng_metadata_end(struct lttng_session
*session
)
1747 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1748 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1749 struct lttng_metadata_stream
*stream
;
1751 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1752 wake_up_interruptible(&stream
->read_wait
);
1753 mutex_unlock(&session
->metadata_cache
->lock
);
1758 * Write the metadata to the metadata cache.
1759 * Must be called with sessions_mutex held.
1760 * The metadata cache lock protects us from concurrent read access from
1761 * thread outputting metadata content to ring buffer.
1762 * The content of the printf is printed as a single atomic metadata
1765 int lttng_metadata_printf(struct lttng_session
*session
,
1766 const char *fmt
, ...)
1772 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
1775 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1781 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1782 if (session
->metadata_cache
->metadata_written
+ len
>
1783 session
->metadata_cache
->cache_alloc
) {
1784 char *tmp_cache_realloc
;
1785 unsigned int tmp_cache_alloc_size
;
1787 tmp_cache_alloc_size
= max_t(unsigned int,
1788 session
->metadata_cache
->cache_alloc
+ len
,
1789 session
->metadata_cache
->cache_alloc
<< 1);
1790 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
1791 if (!tmp_cache_realloc
)
1793 if (session
->metadata_cache
->data
) {
1794 memcpy(tmp_cache_realloc
,
1795 session
->metadata_cache
->data
,
1796 session
->metadata_cache
->cache_alloc
);
1797 vfree(session
->metadata_cache
->data
);
1800 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1801 session
->metadata_cache
->data
= tmp_cache_realloc
;
1803 memcpy(session
->metadata_cache
->data
+
1804 session
->metadata_cache
->metadata_written
,
1806 session
->metadata_cache
->metadata_written
+= len
;
1817 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1821 for (i
= 0; i
< nesting
; i
++) {
1824 ret
= lttng_metadata_printf(session
, " ");
1833 * Must be called with sessions_mutex held.
1836 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1837 const struct lttng_type
*type
,
1841 uint32_t i
, nr_fields
;
1843 ret
= print_tabs(session
, nesting
);
1846 ret
= lttng_metadata_printf(session
,
1850 nr_fields
= type
->u
._struct
.nr_fields
;
1851 for (i
= 0; i
< nr_fields
; i
++) {
1852 const struct lttng_event_field
*iter_field
;
1854 iter_field
= &type
->u
._struct
.fields
[i
];
1855 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1859 ret
= print_tabs(session
, nesting
);
1862 ret
= lttng_metadata_printf(session
,
1868 * Must be called with sessions_mutex held.
1871 int _lttng_struct_statedump(struct lttng_session
*session
,
1872 const struct lttng_event_field
*field
,
1877 ret
= _lttng_struct_type_statedump(session
,
1878 &field
->type
, nesting
);
1881 ret
= lttng_metadata_printf(session
,
1888 * Must be called with sessions_mutex held.
1891 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1892 const struct lttng_type
*type
,
1896 uint32_t i
, nr_choices
;
1898 ret
= print_tabs(session
, nesting
);
1901 ret
= lttng_metadata_printf(session
,
1902 "variant <_%s> {\n",
1903 type
->u
.variant
.tag_name
);
1906 nr_choices
= type
->u
.variant
.nr_choices
;
1907 for (i
= 0; i
< nr_choices
; i
++) {
1908 const struct lttng_event_field
*iter_field
;
1910 iter_field
= &type
->u
.variant
.choices
[i
];
1911 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1915 ret
= print_tabs(session
, nesting
);
1918 ret
= lttng_metadata_printf(session
,
1924 * Must be called with sessions_mutex held.
1927 int _lttng_variant_statedump(struct lttng_session
*session
,
1928 const struct lttng_event_field
*field
,
1933 ret
= _lttng_variant_type_statedump(session
,
1934 &field
->type
, nesting
);
1937 ret
= lttng_metadata_printf(session
,
1944 * Must be called with sessions_mutex held.
1947 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1948 const struct lttng_event_field
*field
,
1952 const struct lttng_type
*elem_type
;
1954 /* Only array of structures and variants are currently supported. */
1955 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1956 switch (elem_type
->atype
) {
1958 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1963 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1970 ret
= lttng_metadata_printf(session
,
1973 field
->type
.u
.array_compound
.length
);
1978 * Must be called with sessions_mutex held.
1981 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
1982 const struct lttng_event_field
*field
,
1986 const char *length_name
;
1987 const struct lttng_type
*elem_type
;
1989 length_name
= field
->type
.u
.sequence_compound
.length_name
;
1991 /* Only array of structures and variants are currently supported. */
1992 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
1993 switch (elem_type
->atype
) {
1995 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
2000 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
2007 ret
= lttng_metadata_printf(session
,
2015 * Must be called with sessions_mutex held.
2018 int _lttng_enum_statedump(struct lttng_session
*session
,
2019 const struct lttng_event_field
*field
,
2022 const struct lttng_enum_desc
*enum_desc
;
2023 const struct lttng_integer_type
*container_type
;
2025 unsigned int i
, nr_entries
;
2027 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
2028 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
2029 nr_entries
= enum_desc
->nr_entries
;
2031 ret
= print_tabs(session
, nesting
);
2034 ret
= lttng_metadata_printf(session
,
2035 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
2036 container_type
->size
,
2037 container_type
->alignment
,
2038 container_type
->signedness
,
2039 (container_type
->encoding
== lttng_encode_none
)
2041 : (container_type
->encoding
== lttng_encode_UTF8
)
2044 container_type
->base
,
2045 #if __BYTE_ORDER == __BIG_ENDIAN
2046 container_type
->reverse_byte_order
? " byte_order = le;" : ""
2048 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2053 /* Dump all entries */
2054 for (i
= 0; i
< nr_entries
; i
++) {
2055 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2058 ret
= print_tabs(session
, nesting
+ 1);
2061 ret
= lttng_metadata_printf(session
,
2065 len
= strlen(entry
->string
);
2066 /* Escape the character '"' */
2067 for (j
= 0; j
< len
; j
++) {
2068 char c
= entry
->string
[j
];
2072 ret
= lttng_metadata_printf(session
,
2076 ret
= lttng_metadata_printf(session
,
2080 ret
= lttng_metadata_printf(session
,
2087 ret
= lttng_metadata_printf(session
, "\"");
2091 if (entry
->options
.is_auto
) {
2092 ret
= lttng_metadata_printf(session
, ",\n");
2096 ret
= lttng_metadata_printf(session
,
2100 if (entry
->start
.signedness
)
2101 ret
= lttng_metadata_printf(session
,
2102 "%lld", (long long) entry
->start
.value
);
2104 ret
= lttng_metadata_printf(session
,
2105 "%llu", entry
->start
.value
);
2108 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2110 == entry
->end
.value
) {
2111 ret
= lttng_metadata_printf(session
,
2114 if (entry
->end
.signedness
) {
2115 ret
= lttng_metadata_printf(session
,
2117 (long long) entry
->end
.value
);
2119 ret
= lttng_metadata_printf(session
,
2128 ret
= print_tabs(session
, nesting
);
2131 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2138 * Must be called with sessions_mutex held.
2141 int _lttng_field_statedump(struct lttng_session
*session
,
2142 const struct lttng_event_field
*field
,
2147 switch (field
->type
.atype
) {
2149 ret
= print_tabs(session
, nesting
);
2152 ret
= lttng_metadata_printf(session
,
2153 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2154 field
->type
.u
.basic
.integer
.size
,
2155 field
->type
.u
.basic
.integer
.alignment
,
2156 field
->type
.u
.basic
.integer
.signedness
,
2157 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2159 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2162 field
->type
.u
.basic
.integer
.base
,
2163 #if __BYTE_ORDER == __BIG_ENDIAN
2164 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2166 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2171 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2174 case atype_array_bitfield
:
2176 const struct lttng_basic_type
*elem_type
;
2178 elem_type
= &field
->type
.u
.array
.elem_type
;
2179 if (field
->type
.u
.array
.elem_alignment
) {
2180 ret
= print_tabs(session
, nesting
);
2183 ret
= lttng_metadata_printf(session
,
2184 "struct { } align(%u) _%s_padding;\n",
2185 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2190 ret
= print_tabs(session
, nesting
);
2193 ret
= lttng_metadata_printf(session
,
2194 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2195 elem_type
->u
.basic
.integer
.size
,
2196 elem_type
->u
.basic
.integer
.alignment
,
2197 elem_type
->u
.basic
.integer
.signedness
,
2198 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2200 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2203 elem_type
->u
.basic
.integer
.base
,
2204 #if __BYTE_ORDER == __BIG_ENDIAN
2205 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2207 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2209 field
->name
, field
->type
.u
.array
.length
);
2212 case atype_sequence
:
2213 case atype_sequence_bitfield
:
2215 const struct lttng_basic_type
*elem_type
;
2216 const struct lttng_basic_type
*length_type
;
2218 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2219 length_type
= &field
->type
.u
.sequence
.length_type
;
2220 ret
= print_tabs(session
, nesting
);
2223 ret
= lttng_metadata_printf(session
,
2224 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2225 length_type
->u
.basic
.integer
.size
,
2226 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2227 length_type
->u
.basic
.integer
.signedness
,
2228 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2230 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2233 length_type
->u
.basic
.integer
.base
,
2234 #if __BYTE_ORDER == __BIG_ENDIAN
2235 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2237 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2243 if (field
->type
.u
.sequence
.elem_alignment
) {
2244 ret
= print_tabs(session
, nesting
);
2247 ret
= lttng_metadata_printf(session
,
2248 "struct { } align(%u) _%s_padding;\n",
2249 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2254 ret
= print_tabs(session
, nesting
);
2257 ret
= lttng_metadata_printf(session
,
2258 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2259 elem_type
->u
.basic
.integer
.size
,
2260 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2261 elem_type
->u
.basic
.integer
.signedness
,
2262 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2264 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2267 elem_type
->u
.basic
.integer
.base
,
2268 #if __BYTE_ORDER == __BIG_ENDIAN
2269 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2271 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2279 /* Default encoding is UTF8 */
2280 ret
= print_tabs(session
, nesting
);
2283 ret
= lttng_metadata_printf(session
,
2285 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2286 " { encoding = ASCII; }" : "",
2290 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2292 case atype_array_compound
:
2293 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2295 case atype_sequence_compound
:
2296 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2299 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2310 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2311 struct lttng_ctx
*ctx
)
2318 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2319 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2321 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2329 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2330 struct lttng_event
*event
)
2332 const struct lttng_event_desc
*desc
= event
->desc
;
2336 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2337 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2339 ret
= _lttng_field_statedump(session
, field
, 2);
2347 * Must be called with sessions_mutex held.
2348 * The entire event metadata is printed as a single atomic metadata
2352 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2353 struct lttng_channel
*chan
,
2354 struct lttng_event
*event
)
2358 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2360 if (chan
->channel_type
== METADATA_CHANNEL
)
2363 lttng_metadata_begin(session
);
2365 ret
= lttng_metadata_printf(session
,
2369 " stream_id = %u;\n",
2377 ret
= lttng_metadata_printf(session
,
2378 " context := struct {\n");
2382 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2386 ret
= lttng_metadata_printf(session
,
2392 ret
= lttng_metadata_printf(session
,
2393 " fields := struct {\n"
2398 ret
= _lttng_fields_metadata_statedump(session
, event
);
2403 * LTTng space reservation can only reserve multiples of the
2406 ret
= lttng_metadata_printf(session
,
2412 event
->metadata_dumped
= 1;
2414 lttng_metadata_end(session
);
2420 * Must be called with sessions_mutex held.
2421 * The entire channel metadata is printed as a single atomic metadata
2425 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2426 struct lttng_channel
*chan
)
2430 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
2433 if (chan
->channel_type
== METADATA_CHANNEL
)
2436 lttng_metadata_begin(session
);
2438 WARN_ON_ONCE(!chan
->header_type
);
2439 ret
= lttng_metadata_printf(session
,
2442 " event.header := %s;\n"
2443 " packet.context := struct packet_context;\n",
2445 chan
->header_type
== 1 ? "struct event_header_compact" :
2446 "struct event_header_large");
2451 ret
= lttng_metadata_printf(session
,
2452 " event.context := struct {\n");
2456 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2460 ret
= lttng_metadata_printf(session
,
2466 ret
= lttng_metadata_printf(session
,
2469 chan
->metadata_dumped
= 1;
2471 lttng_metadata_end(session
);
2476 * Must be called with sessions_mutex held.
2479 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2481 return lttng_metadata_printf(session
,
2482 "struct packet_context {\n"
2483 " uint64_clock_monotonic_t timestamp_begin;\n"
2484 " uint64_clock_monotonic_t timestamp_end;\n"
2485 " uint64_t content_size;\n"
2486 " uint64_t packet_size;\n"
2487 " uint64_t packet_seq_num;\n"
2488 " unsigned long events_discarded;\n"
2489 " uint32_t cpu_id;\n"
2496 * id: range: 0 - 30.
2497 * id 31 is reserved to indicate an extended header.
2500 * id: range: 0 - 65534.
2501 * id 65535 is reserved to indicate an extended header.
2503 * Must be called with sessions_mutex held.
2506 int _lttng_event_header_declare(struct lttng_session
*session
)
2508 return lttng_metadata_printf(session
,
2509 "struct event_header_compact {\n"
2510 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2513 " uint27_clock_monotonic_t timestamp;\n"
2517 " uint64_clock_monotonic_t timestamp;\n"
2522 "struct event_header_large {\n"
2523 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2526 " uint32_clock_monotonic_t timestamp;\n"
2530 " uint64_clock_monotonic_t timestamp;\n"
2534 lttng_alignof(uint32_t) * CHAR_BIT
,
2535 lttng_alignof(uint16_t) * CHAR_BIT
2540 * Approximation of NTP time of day to clock monotonic correlation,
2541 * taken at start of trace.
2542 * Yes, this is only an approximation. Yes, we can (and will) do better
2543 * in future versions.
2544 * This function may return a negative offset. It may happen if the
2545 * system sets the REALTIME clock to 0 after boot.
2547 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2551 int64_t measure_clock_offset(void)
2553 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2554 uint64_t tcf
= trace_clock_freq();
2556 unsigned long flags
;
2557 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2558 struct timespec64 rts
= { 0, 0 };
2560 struct timespec rts
= { 0, 0 };
2563 /* Disable interrupts to increase correlation precision. */
2564 local_irq_save(flags
);
2565 monotonic
[0] = trace_clock_read64();
2566 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2567 ktime_get_real_ts64(&rts
);
2569 getnstimeofday(&rts
);
2571 monotonic
[1] = trace_clock_read64();
2572 local_irq_restore(flags
);
2574 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2575 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2576 if (tcf
== NSEC_PER_SEC
) {
2577 realtime
+= rts
.tv_nsec
;
2579 uint64_t n
= rts
.tv_nsec
* tcf
;
2581 do_div(n
, NSEC_PER_SEC
);
2584 offset
= (int64_t) realtime
- monotonic_avg
;
2589 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2597 while (cur
!= '\0') {
2600 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2604 ret
= lttng_metadata_printf(session
, "%c", '\\');
2607 /* We still print the current char */
2610 ret
= lttng_metadata_printf(session
, "%c", cur
);
2624 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2625 const char *field_value
)
2629 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2633 ret
= print_escaped_ctf_string(session
, field_value
);
2637 ret
= lttng_metadata_printf(session
, "\";\n");
2644 * Output metadata into this session's metadata buffers.
2645 * Must be called with sessions_mutex held.
2648 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2650 unsigned char *uuid_c
= session
->uuid
.b
;
2651 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2652 struct lttng_channel
*chan
;
2653 struct lttng_event
*event
;
2656 if (!LTTNG_READ_ONCE(session
->active
))
2659 lttng_metadata_begin(session
);
2661 if (session
->metadata_dumped
)
2664 snprintf(uuid_s
, sizeof(uuid_s
),
2665 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2666 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2667 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2668 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2669 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2671 ret
= lttng_metadata_printf(session
,
2672 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2673 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2674 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2675 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2676 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2677 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2678 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2684 " byte_order = %s;\n"
2685 " packet.header := struct {\n"
2686 " uint32_t magic;\n"
2687 " uint8_t uuid[16];\n"
2688 " uint32_t stream_id;\n"
2689 " uint64_t stream_instance_id;\n"
2692 lttng_alignof(uint8_t) * CHAR_BIT
,
2693 lttng_alignof(uint16_t) * CHAR_BIT
,
2694 lttng_alignof(uint32_t) * CHAR_BIT
,
2695 lttng_alignof(uint64_t) * CHAR_BIT
,
2696 sizeof(unsigned long) * CHAR_BIT
,
2697 lttng_alignof(unsigned long) * CHAR_BIT
,
2701 #if __BYTE_ORDER == __BIG_ENDIAN
2710 ret
= lttng_metadata_printf(session
,
2712 " hostname = \"%s\";\n"
2713 " domain = \"kernel\";\n"
2714 " sysname = \"%s\";\n"
2715 " kernel_release = \"%s\";\n"
2716 " kernel_version = \"%s\";\n"
2717 " tracer_name = \"lttng-modules\";\n"
2718 " tracer_major = %d;\n"
2719 " tracer_minor = %d;\n"
2720 " tracer_patchlevel = %d;\n"
2721 " trace_buffering_scheme = \"global\";\n",
2722 current
->nsproxy
->uts_ns
->name
.nodename
,
2726 LTTNG_MODULES_MAJOR_VERSION
,
2727 LTTNG_MODULES_MINOR_VERSION
,
2728 LTTNG_MODULES_PATCHLEVEL_VERSION
2733 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2736 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2737 session
->creation_time
);
2742 ret
= lttng_metadata_printf(session
, "};\n\n");
2746 ret
= lttng_metadata_printf(session
,
2748 " name = \"%s\";\n",
2754 if (!trace_clock_uuid(clock_uuid_s
)) {
2755 ret
= lttng_metadata_printf(session
,
2756 " uuid = \"%s\";\n",
2763 ret
= lttng_metadata_printf(session
,
2764 " description = \"%s\";\n"
2765 " freq = %llu; /* Frequency, in Hz */\n"
2766 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2769 trace_clock_description(),
2770 (unsigned long long) trace_clock_freq(),
2771 (long long) measure_clock_offset()
2776 ret
= lttng_metadata_printf(session
,
2777 "typealias integer {\n"
2778 " size = 27; align = 1; signed = false;\n"
2779 " map = clock.%s.value;\n"
2780 "} := uint27_clock_monotonic_t;\n"
2782 "typealias integer {\n"
2783 " size = 32; align = %u; signed = false;\n"
2784 " map = clock.%s.value;\n"
2785 "} := uint32_clock_monotonic_t;\n"
2787 "typealias integer {\n"
2788 " size = 64; align = %u; signed = false;\n"
2789 " map = clock.%s.value;\n"
2790 "} := uint64_clock_monotonic_t;\n\n",
2792 lttng_alignof(uint32_t) * CHAR_BIT
,
2794 lttng_alignof(uint64_t) * CHAR_BIT
,
2800 ret
= _lttng_stream_packet_context_declare(session
);
2804 ret
= _lttng_event_header_declare(session
);
2809 list_for_each_entry(chan
, &session
->chan
, list
) {
2810 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2815 list_for_each_entry(event
, &session
->events
, list
) {
2816 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2820 session
->metadata_dumped
= 1;
2822 lttng_metadata_end(session
);
2827 * lttng_transport_register - LTT transport registration
2828 * @transport: transport structure
2830 * Registers a transport which can be used as output to extract the data out of
2831 * LTTng. The module calling this registration function must ensure that no
2832 * trap-inducing code will be executed by the transport functions. E.g.
2833 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2834 * is made visible to the transport function. This registration acts as a
2835 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2836 * after its registration must it synchronize the TLBs.
2838 void lttng_transport_register(struct lttng_transport
*transport
)
2841 * Make sure no page fault can be triggered by the module about to be
2842 * registered. We deal with this here so we don't have to call
2843 * vmalloc_sync_mappings() in each module's init.
2845 wrapper_vmalloc_sync_mappings();
2847 mutex_lock(&sessions_mutex
);
2848 list_add_tail(&transport
->node
, <tng_transport_list
);
2849 mutex_unlock(&sessions_mutex
);
2851 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2854 * lttng_transport_unregister - LTT transport unregistration
2855 * @transport: transport structure
2857 void lttng_transport_unregister(struct lttng_transport
*transport
)
2859 mutex_lock(&sessions_mutex
);
2860 list_del(&transport
->node
);
2861 mutex_unlock(&sessions_mutex
);
2863 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2865 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2867 enum cpuhp_state lttng_hp_prepare
;
2868 enum cpuhp_state lttng_hp_online
;
2870 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2872 struct lttng_cpuhp_node
*lttng_node
;
2874 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2875 switch (lttng_node
->component
) {
2876 case LTTNG_RING_BUFFER_FRONTEND
:
2878 case LTTNG_RING_BUFFER_BACKEND
:
2879 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2880 case LTTNG_RING_BUFFER_ITER
:
2882 case LTTNG_CONTEXT_PERF_COUNTERS
:
2889 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2891 struct lttng_cpuhp_node
*lttng_node
;
2893 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2894 switch (lttng_node
->component
) {
2895 case LTTNG_RING_BUFFER_FRONTEND
:
2896 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2897 case LTTNG_RING_BUFFER_BACKEND
:
2899 case LTTNG_RING_BUFFER_ITER
:
2901 case LTTNG_CONTEXT_PERF_COUNTERS
:
2902 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2908 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2910 struct lttng_cpuhp_node
*lttng_node
;
2912 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2913 switch (lttng_node
->component
) {
2914 case LTTNG_RING_BUFFER_FRONTEND
:
2915 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2916 case LTTNG_RING_BUFFER_BACKEND
:
2918 case LTTNG_RING_BUFFER_ITER
:
2919 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2920 case LTTNG_CONTEXT_PERF_COUNTERS
:
2921 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2927 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2929 struct lttng_cpuhp_node
*lttng_node
;
2931 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2932 switch (lttng_node
->component
) {
2933 case LTTNG_RING_BUFFER_FRONTEND
:
2934 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2935 case LTTNG_RING_BUFFER_BACKEND
:
2937 case LTTNG_RING_BUFFER_ITER
:
2939 case LTTNG_CONTEXT_PERF_COUNTERS
:
2946 static int __init
lttng_init_cpu_hotplug(void)
2950 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2951 lttng_hotplug_prepare
,
2952 lttng_hotplug_dead
);
2956 lttng_hp_prepare
= ret
;
2957 lttng_rb_set_hp_prepare(ret
);
2959 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
2960 lttng_hotplug_online
,
2961 lttng_hotplug_offline
);
2963 cpuhp_remove_multi_state(lttng_hp_prepare
);
2964 lttng_hp_prepare
= 0;
2967 lttng_hp_online
= ret
;
2968 lttng_rb_set_hp_online(ret
);
2973 static void __exit
lttng_exit_cpu_hotplug(void)
2975 lttng_rb_set_hp_online(0);
2976 cpuhp_remove_multi_state(lttng_hp_online
);
2977 lttng_rb_set_hp_prepare(0);
2978 cpuhp_remove_multi_state(lttng_hp_prepare
);
2981 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2982 static int lttng_init_cpu_hotplug(void)
2986 static void lttng_exit_cpu_hotplug(void)
2989 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2992 static int __init
lttng_events_init(void)
2996 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2999 ret
= wrapper_get_pfnblock_flags_mask_init();
3002 ret
= wrapper_get_pageblock_flags_mask_init();
3005 ret
= lttng_probes_init();
3008 ret
= lttng_context_init();
3011 ret
= lttng_tracepoint_init();
3014 event_cache
= KMEM_CACHE(lttng_event
, 0);
3019 ret
= lttng_abi_init();
3022 ret
= lttng_logger_init();
3025 ret
= lttng_init_cpu_hotplug();
3028 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3029 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3030 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3031 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3032 LTTNG_MODULES_EXTRAVERSION
,
3034 #ifdef LTTNG_EXTRA_VERSION_GIT
3035 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3039 #ifdef LTTNG_EXTRA_VERSION_NAME
3040 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3047 lttng_logger_exit();
3051 kmem_cache_destroy(event_cache
);
3053 lttng_tracepoint_exit();
3055 lttng_context_exit();
3056 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3057 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3058 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3059 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3060 LTTNG_MODULES_EXTRAVERSION
,
3062 #ifdef LTTNG_EXTRA_VERSION_GIT
3063 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3067 #ifdef LTTNG_EXTRA_VERSION_NAME
3068 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3075 module_init(lttng_events_init
);
3077 static void __exit
lttng_events_exit(void)
3079 struct lttng_session
*session
, *tmpsession
;
3081 lttng_exit_cpu_hotplug();
3082 lttng_logger_exit();
3084 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3085 lttng_session_destroy(session
);
3086 kmem_cache_destroy(event_cache
);
3087 lttng_tracepoint_exit();
3088 lttng_context_exit();
3089 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3090 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3091 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3092 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3093 LTTNG_MODULES_EXTRAVERSION
,
3095 #ifdef LTTNG_EXTRA_VERSION_GIT
3096 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3100 #ifdef LTTNG_EXTRA_VERSION_NAME
3101 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3107 module_exit(lttng_events_exit
);
3109 #include "extra_version/patches.i"
3110 #ifdef LTTNG_EXTRA_VERSION_GIT
3111 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3113 #ifdef LTTNG_EXTRA_VERSION_NAME
3114 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3116 MODULE_LICENSE("GPL and additional rights");
3117 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3118 MODULE_DESCRIPTION("LTTng tracer");
3119 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3120 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3121 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3122 LTTNG_MODULES_EXTRAVERSION
);