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>
31 #include <linux/dmi.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
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/ringbuffer/backend.h>
45 #include <wrapper/ringbuffer/frontend.h>
47 #define METADATA_CACHE_DEFAULT_SIZE 4096
49 static LIST_HEAD(sessions
);
50 static LIST_HEAD(lttng_transport_list
);
52 * Protect the sessions and metadata caches.
54 static DEFINE_MUTEX(sessions_mutex
);
55 static struct kmem_cache
*event_cache
;
57 static void lttng_session_lazy_sync_enablers(struct lttng_session
*session
);
58 static void lttng_session_sync_enablers(struct lttng_session
*session
);
59 static void lttng_enabler_destroy(struct lttng_enabler
*enabler
);
61 static void _lttng_event_destroy(struct lttng_event
*event
);
62 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
63 static int _lttng_event_unregister(struct lttng_event
*event
);
65 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
66 struct lttng_channel
*chan
,
67 struct lttng_event
*event
);
69 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
71 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
73 int _lttng_field_statedump(struct lttng_session
*session
,
74 const struct lttng_event_field
*field
,
77 void synchronize_trace(void)
79 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
85 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
86 #ifdef CONFIG_PREEMPT_RT_FULL
89 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
90 #ifdef CONFIG_PREEMPT_RT
93 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
96 void lttng_lock_sessions(void)
98 mutex_lock(&sessions_mutex
);
101 void lttng_unlock_sessions(void)
103 mutex_unlock(&sessions_mutex
);
107 * Called with sessions lock held.
109 int lttng_session_active(void)
111 struct lttng_session
*iter
;
113 list_for_each_entry(iter
, &sessions
, list
) {
120 struct lttng_session
*lttng_session_create(void)
122 struct lttng_session
*session
;
123 struct lttng_metadata_cache
*metadata_cache
;
126 mutex_lock(&sessions_mutex
);
127 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
130 INIT_LIST_HEAD(&session
->chan
);
131 INIT_LIST_HEAD(&session
->events
);
132 uuid_le_gen(&session
->uuid
);
134 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
137 goto err_free_session
;
138 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
139 if (!metadata_cache
->data
)
141 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
142 kref_init(&metadata_cache
->refcount
);
143 mutex_init(&metadata_cache
->lock
);
144 session
->metadata_cache
= metadata_cache
;
145 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
146 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
147 sizeof(metadata_cache
->uuid
));
148 INIT_LIST_HEAD(&session
->enablers_head
);
149 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
150 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
151 list_add(&session
->list
, &sessions
);
152 session
->pid_tracker
.session
= session
;
153 session
->pid_tracker
.tracker_type
= TRACKER_PID
;
154 session
->vpid_tracker
.session
= session
;
155 session
->vpid_tracker
.tracker_type
= TRACKER_VPID
;
156 session
->uid_tracker
.session
= session
;
157 session
->uid_tracker
.tracker_type
= TRACKER_UID
;
158 session
->vuid_tracker
.session
= session
;
159 session
->vuid_tracker
.tracker_type
= TRACKER_VUID
;
160 session
->gid_tracker
.session
= session
;
161 session
->gid_tracker
.tracker_type
= TRACKER_GID
;
162 session
->vgid_tracker
.session
= session
;
163 session
->vgid_tracker
.tracker_type
= TRACKER_VGID
;
164 mutex_unlock(&sessions_mutex
);
168 kfree(metadata_cache
);
170 lttng_kvfree(session
);
172 mutex_unlock(&sessions_mutex
);
176 void metadata_cache_destroy(struct kref
*kref
)
178 struct lttng_metadata_cache
*cache
=
179 container_of(kref
, struct lttng_metadata_cache
, refcount
);
184 void lttng_session_destroy(struct lttng_session
*session
)
186 struct lttng_channel
*chan
, *tmpchan
;
187 struct lttng_event
*event
, *tmpevent
;
188 struct lttng_metadata_stream
*metadata_stream
;
189 struct lttng_enabler
*enabler
, *tmpenabler
;
192 mutex_lock(&sessions_mutex
);
193 WRITE_ONCE(session
->active
, 0);
194 list_for_each_entry(chan
, &session
->chan
, list
) {
195 ret
= lttng_syscalls_unregister(chan
);
198 list_for_each_entry(event
, &session
->events
, list
) {
199 ret
= _lttng_event_unregister(event
);
202 synchronize_trace(); /* Wait for in-flight events to complete */
203 list_for_each_entry_safe(enabler
, tmpenabler
,
204 &session
->enablers_head
, node
)
205 lttng_enabler_destroy(enabler
);
206 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
207 _lttng_event_destroy(event
);
208 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
209 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
210 _lttng_channel_destroy(chan
);
212 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
213 _lttng_metadata_channel_hangup(metadata_stream
);
214 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
215 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
216 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
217 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
218 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
219 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
220 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
221 list_del(&session
->list
);
222 mutex_unlock(&sessions_mutex
);
223 lttng_kvfree(session
);
226 int lttng_session_statedump(struct lttng_session
*session
)
230 mutex_lock(&sessions_mutex
);
231 ret
= lttng_statedump_start(session
);
232 mutex_unlock(&sessions_mutex
);
236 int lttng_session_enable(struct lttng_session
*session
)
239 struct lttng_channel
*chan
;
241 mutex_lock(&sessions_mutex
);
242 if (session
->active
) {
247 /* Set transient enabler state to "enabled" */
250 /* We need to sync enablers with session before activation. */
251 lttng_session_sync_enablers(session
);
254 * Snapshot the number of events per channel to know the type of header
257 list_for_each_entry(chan
, &session
->chan
, list
) {
258 if (chan
->header_type
)
259 continue; /* don't change it if session stop/restart */
260 if (chan
->free_event_id
< 31)
261 chan
->header_type
= 1; /* compact */
263 chan
->header_type
= 2; /* large */
266 /* Clear each stream's quiescent state. */
267 list_for_each_entry(chan
, &session
->chan
, list
) {
268 if (chan
->channel_type
!= METADATA_CHANNEL
)
269 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
272 WRITE_ONCE(session
->active
, 1);
273 WRITE_ONCE(session
->been_active
, 1);
274 ret
= _lttng_session_metadata_statedump(session
);
276 WRITE_ONCE(session
->active
, 0);
279 ret
= lttng_statedump_start(session
);
281 WRITE_ONCE(session
->active
, 0);
283 mutex_unlock(&sessions_mutex
);
287 int lttng_session_disable(struct lttng_session
*session
)
290 struct lttng_channel
*chan
;
292 mutex_lock(&sessions_mutex
);
293 if (!session
->active
) {
297 WRITE_ONCE(session
->active
, 0);
299 /* Set transient enabler state to "disabled" */
301 lttng_session_sync_enablers(session
);
303 /* Set each stream's quiescent state. */
304 list_for_each_entry(chan
, &session
->chan
, list
) {
305 if (chan
->channel_type
!= METADATA_CHANNEL
)
306 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
309 mutex_unlock(&sessions_mutex
);
313 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
316 struct lttng_channel
*chan
;
317 struct lttng_event
*event
;
318 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
319 struct lttng_metadata_stream
*stream
;
321 mutex_lock(&sessions_mutex
);
322 if (!session
->active
) {
327 mutex_lock(&cache
->lock
);
328 memset(cache
->data
, 0, cache
->cache_alloc
);
329 cache
->metadata_written
= 0;
331 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
332 stream
->metadata_out
= 0;
333 stream
->metadata_in
= 0;
335 mutex_unlock(&cache
->lock
);
337 session
->metadata_dumped
= 0;
338 list_for_each_entry(chan
, &session
->chan
, list
) {
339 chan
->metadata_dumped
= 0;
342 list_for_each_entry(event
, &session
->events
, list
) {
343 event
->metadata_dumped
= 0;
346 ret
= _lttng_session_metadata_statedump(session
);
349 mutex_unlock(&sessions_mutex
);
353 int lttng_channel_enable(struct lttng_channel
*channel
)
357 mutex_lock(&sessions_mutex
);
358 if (channel
->channel_type
== METADATA_CHANNEL
) {
362 if (channel
->enabled
) {
366 /* Set transient enabler state to "enabled" */
368 lttng_session_sync_enablers(channel
->session
);
369 /* Set atomically the state to "enabled" */
370 WRITE_ONCE(channel
->enabled
, 1);
372 mutex_unlock(&sessions_mutex
);
376 int lttng_channel_disable(struct lttng_channel
*channel
)
380 mutex_lock(&sessions_mutex
);
381 if (channel
->channel_type
== METADATA_CHANNEL
) {
385 if (!channel
->enabled
) {
389 /* Set atomically the state to "disabled" */
390 WRITE_ONCE(channel
->enabled
, 0);
391 /* Set transient enabler state to "enabled" */
393 lttng_session_sync_enablers(channel
->session
);
395 mutex_unlock(&sessions_mutex
);
399 int lttng_event_enable(struct lttng_event
*event
)
403 mutex_lock(&sessions_mutex
);
404 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
408 if (event
->enabled
) {
412 switch (event
->instrumentation
) {
413 case LTTNG_KERNEL_TRACEPOINT
:
414 case LTTNG_KERNEL_SYSCALL
:
417 case LTTNG_KERNEL_KPROBE
:
418 case LTTNG_KERNEL_FUNCTION
:
419 case LTTNG_KERNEL_UPROBE
:
420 case LTTNG_KERNEL_NOOP
:
421 WRITE_ONCE(event
->enabled
, 1);
423 case LTTNG_KERNEL_KRETPROBE
:
424 ret
= lttng_kretprobes_event_enable_state(event
, 1);
431 mutex_unlock(&sessions_mutex
);
435 int lttng_event_disable(struct lttng_event
*event
)
439 mutex_lock(&sessions_mutex
);
440 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
444 if (!event
->enabled
) {
448 switch (event
->instrumentation
) {
449 case LTTNG_KERNEL_TRACEPOINT
:
450 case LTTNG_KERNEL_SYSCALL
:
453 case LTTNG_KERNEL_KPROBE
:
454 case LTTNG_KERNEL_FUNCTION
:
455 case LTTNG_KERNEL_UPROBE
:
456 case LTTNG_KERNEL_NOOP
:
457 WRITE_ONCE(event
->enabled
, 0);
459 case LTTNG_KERNEL_KRETPROBE
:
460 ret
= lttng_kretprobes_event_enable_state(event
, 0);
467 mutex_unlock(&sessions_mutex
);
471 static struct lttng_transport
*lttng_transport_find(const char *name
)
473 struct lttng_transport
*transport
;
475 list_for_each_entry(transport
, <tng_transport_list
, node
) {
476 if (!strcmp(transport
->name
, name
))
482 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
483 const char *transport_name
,
485 size_t subbuf_size
, size_t num_subbuf
,
486 unsigned int switch_timer_interval
,
487 unsigned int read_timer_interval
,
488 enum channel_type channel_type
)
490 struct lttng_channel
*chan
;
491 struct lttng_transport
*transport
= NULL
;
493 mutex_lock(&sessions_mutex
);
494 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
495 goto active
; /* Refuse to add channel to active session */
496 transport
= lttng_transport_find(transport_name
);
498 printk(KERN_WARNING
"LTTng transport %s not found\n",
502 if (!try_module_get(transport
->owner
)) {
503 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
506 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
509 chan
->session
= session
;
510 chan
->id
= session
->free_chan_id
++;
511 chan
->ops
= &transport
->ops
;
513 * Note: the channel creation op already writes into the packet
514 * headers. Therefore the "chan" information used as input
515 * should be already accessible.
517 chan
->chan
= transport
->ops
.channel_create(transport_name
,
518 chan
, buf_addr
, subbuf_size
, num_subbuf
,
519 switch_timer_interval
, read_timer_interval
);
524 chan
->transport
= transport
;
525 chan
->channel_type
= channel_type
;
526 list_add(&chan
->list
, &session
->chan
);
527 mutex_unlock(&sessions_mutex
);
534 module_put(transport
->owner
);
537 mutex_unlock(&sessions_mutex
);
542 * Only used internally at session destruction for per-cpu channels, and
543 * when metadata channel is released.
544 * Needs to be called with sessions mutex held.
547 void _lttng_channel_destroy(struct lttng_channel
*chan
)
549 chan
->ops
->channel_destroy(chan
->chan
);
550 module_put(chan
->transport
->owner
);
551 list_del(&chan
->list
);
552 lttng_destroy_context(chan
->ctx
);
556 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
558 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
560 /* Protect the metadata cache with the sessions_mutex. */
561 mutex_lock(&sessions_mutex
);
562 _lttng_channel_destroy(chan
);
563 mutex_unlock(&sessions_mutex
);
565 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
568 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
570 stream
->finalized
= 1;
571 wake_up_interruptible(&stream
->read_wait
);
575 * Supports event creation while tracing session is active.
576 * Needs to be called with sessions mutex held.
578 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
579 struct lttng_kernel_event
*event_param
,
581 const struct lttng_event_desc
*event_desc
,
582 enum lttng_kernel_instrumentation itype
)
584 struct lttng_session
*session
= chan
->session
;
585 struct lttng_event
*event
;
586 const char *event_name
;
587 struct hlist_head
*head
;
592 if (chan
->free_event_id
== -1U) {
598 case LTTNG_KERNEL_TRACEPOINT
:
599 event_name
= event_desc
->name
;
601 case LTTNG_KERNEL_KPROBE
:
602 case LTTNG_KERNEL_UPROBE
:
603 case LTTNG_KERNEL_KRETPROBE
:
604 case LTTNG_KERNEL_FUNCTION
:
605 case LTTNG_KERNEL_NOOP
:
606 case LTTNG_KERNEL_SYSCALL
:
607 event_name
= event_param
->name
;
614 name_len
= strlen(event_name
);
615 hash
= jhash(event_name
, name_len
, 0);
616 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
617 lttng_hlist_for_each_entry(event
, head
, hlist
) {
618 WARN_ON_ONCE(!event
->desc
);
619 if (!strncmp(event
->desc
->name
, event_name
,
620 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
621 && chan
== event
->chan
) {
627 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
633 event
->filter
= filter
;
634 event
->id
= chan
->free_event_id
++;
635 event
->instrumentation
= itype
;
636 event
->evtype
= LTTNG_TYPE_EVENT
;
637 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
638 INIT_LIST_HEAD(&event
->enablers_ref_head
);
641 case LTTNG_KERNEL_TRACEPOINT
:
642 /* Event will be enabled by enabler sync. */
644 event
->registered
= 0;
645 event
->desc
= lttng_event_get(event_name
);
650 /* Populate lttng_event structure before event registration. */
653 case LTTNG_KERNEL_KPROBE
:
655 * Needs to be explicitly enabled after creation, since
656 * we may want to apply filters.
659 event
->registered
= 1;
661 * Populate lttng_event structure before event
665 ret
= lttng_kprobes_register(event_name
,
666 event_param
->u
.kprobe
.symbol_name
,
667 event_param
->u
.kprobe
.offset
,
668 event_param
->u
.kprobe
.addr
,
674 ret
= try_module_get(event
->desc
->owner
);
677 case LTTNG_KERNEL_KRETPROBE
:
679 struct lttng_event
*event_return
;
681 /* kretprobe defines 2 events */
683 * Needs to be explicitly enabled after creation, since
684 * we may want to apply filters.
687 event
->registered
= 1;
689 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
694 event_return
->chan
= chan
;
695 event_return
->filter
= filter
;
696 event_return
->id
= chan
->free_event_id
++;
697 event_return
->enabled
= 0;
698 event_return
->registered
= 1;
699 event_return
->instrumentation
= itype
;
701 * Populate lttng_event structure before kretprobe registration.
704 ret
= lttng_kretprobes_register(event_name
,
705 event_param
->u
.kretprobe
.symbol_name
,
706 event_param
->u
.kretprobe
.offset
,
707 event_param
->u
.kretprobe
.addr
,
708 event
, event_return
);
710 kmem_cache_free(event_cache
, event_return
);
714 /* Take 2 refs on the module: one per event. */
715 ret
= try_module_get(event
->desc
->owner
);
717 ret
= try_module_get(event
->desc
->owner
);
719 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
721 WARN_ON_ONCE(ret
> 0);
723 kmem_cache_free(event_cache
, event_return
);
724 module_put(event
->desc
->owner
);
725 module_put(event
->desc
->owner
);
726 goto statedump_error
;
728 list_add(&event_return
->list
, &chan
->session
->events
);
731 case LTTNG_KERNEL_FUNCTION
:
733 * Needs to be explicitly enabled after creation, since
734 * we may want to apply filters.
737 event
->registered
= 1;
739 * Populate lttng_event structure before event
743 ret
= lttng_ftrace_register(event_name
,
744 event_param
->u
.ftrace
.symbol_name
,
749 ret
= try_module_get(event
->desc
->owner
);
752 case LTTNG_KERNEL_NOOP
:
753 case LTTNG_KERNEL_SYSCALL
:
755 * Needs to be explicitly enabled after creation, since
756 * we may want to apply filters.
759 event
->registered
= 0;
760 event
->desc
= event_desc
;
766 case LTTNG_KERNEL_UPROBE
:
768 * Needs to be explicitly enabled after creation, since
769 * we may want to apply filters.
772 event
->registered
= 1;
775 * Populate lttng_event structure before event
780 ret
= lttng_uprobes_register(event_param
->name
,
781 event_param
->u
.uprobe
.fd
,
785 ret
= try_module_get(event
->desc
->owner
);
793 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
794 WARN_ON_ONCE(ret
> 0);
796 goto statedump_error
;
798 hlist_add_head(&event
->hlist
, head
);
799 list_add(&event
->list
, &chan
->session
->events
);
803 /* If a statedump error occurs, events will not be readable. */
805 kmem_cache_free(event_cache
, event
);
813 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
814 struct lttng_kernel_event
*event_param
,
816 const struct lttng_event_desc
*event_desc
,
817 enum lttng_kernel_instrumentation itype
)
819 struct lttng_event
*event
;
821 mutex_lock(&sessions_mutex
);
822 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
824 mutex_unlock(&sessions_mutex
);
828 /* Only used for tracepoints for now. */
830 void register_event(struct lttng_event
*event
)
832 const struct lttng_event_desc
*desc
;
835 if (event
->registered
)
839 switch (event
->instrumentation
) {
840 case LTTNG_KERNEL_TRACEPOINT
:
841 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
842 desc
->probe_callback
,
845 case LTTNG_KERNEL_SYSCALL
:
846 ret
= lttng_syscall_filter_enable(event
->chan
,
849 case LTTNG_KERNEL_KPROBE
:
850 case LTTNG_KERNEL_UPROBE
:
851 case LTTNG_KERNEL_KRETPROBE
:
852 case LTTNG_KERNEL_FUNCTION
:
853 case LTTNG_KERNEL_NOOP
:
860 event
->registered
= 1;
864 * Only used internally at session destruction.
866 int _lttng_event_unregister(struct lttng_event
*event
)
868 const struct lttng_event_desc
*desc
;
871 if (!event
->registered
)
875 switch (event
->instrumentation
) {
876 case LTTNG_KERNEL_TRACEPOINT
:
877 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
878 event
->desc
->probe_callback
,
881 case LTTNG_KERNEL_KPROBE
:
882 lttng_kprobes_unregister(event
);
885 case LTTNG_KERNEL_KRETPROBE
:
886 lttng_kretprobes_unregister(event
);
889 case LTTNG_KERNEL_FUNCTION
:
890 lttng_ftrace_unregister(event
);
893 case LTTNG_KERNEL_SYSCALL
:
894 ret
= lttng_syscall_filter_disable(event
->chan
,
897 case LTTNG_KERNEL_NOOP
:
900 case LTTNG_KERNEL_UPROBE
:
901 lttng_uprobes_unregister(event
);
908 event
->registered
= 0;
913 * Only used internally at session destruction.
916 void _lttng_event_destroy(struct lttng_event
*event
)
918 switch (event
->instrumentation
) {
919 case LTTNG_KERNEL_TRACEPOINT
:
920 lttng_event_put(event
->desc
);
922 case LTTNG_KERNEL_KPROBE
:
923 module_put(event
->desc
->owner
);
924 lttng_kprobes_destroy_private(event
);
926 case LTTNG_KERNEL_KRETPROBE
:
927 module_put(event
->desc
->owner
);
928 lttng_kretprobes_destroy_private(event
);
930 case LTTNG_KERNEL_FUNCTION
:
931 module_put(event
->desc
->owner
);
932 lttng_ftrace_destroy_private(event
);
934 case LTTNG_KERNEL_NOOP
:
935 case LTTNG_KERNEL_SYSCALL
:
937 case LTTNG_KERNEL_UPROBE
:
938 module_put(event
->desc
->owner
);
939 lttng_uprobes_destroy_private(event
);
944 list_del(&event
->list
);
945 lttng_destroy_context(event
->ctx
);
946 kmem_cache_free(event_cache
, event
);
949 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
950 enum tracker_type tracker_type
)
952 switch (tracker_type
) {
954 return &session
->pid_tracker
;
956 return &session
->vpid_tracker
;
958 return &session
->uid_tracker
;
960 return &session
->vuid_tracker
;
962 return &session
->gid_tracker
;
964 return &session
->vgid_tracker
;
971 int lttng_session_track_id(struct lttng_session
*session
,
972 enum tracker_type tracker_type
, int id
)
974 struct lttng_id_tracker
*tracker
;
977 tracker
= get_tracker(session
, tracker_type
);
982 mutex_lock(&sessions_mutex
);
984 /* track all ids: destroy tracker. */
985 lttng_id_tracker_destroy(tracker
, true);
988 ret
= lttng_id_tracker_add(tracker
, id
);
990 mutex_unlock(&sessions_mutex
);
994 int lttng_session_untrack_id(struct lttng_session
*session
,
995 enum tracker_type tracker_type
, int id
)
997 struct lttng_id_tracker
*tracker
;
1000 tracker
= get_tracker(session
, tracker_type
);
1005 mutex_lock(&sessions_mutex
);
1007 /* untrack all ids: replace by empty tracker. */
1008 ret
= lttng_id_tracker_empty_set(tracker
);
1010 ret
= lttng_id_tracker_del(tracker
, id
);
1012 mutex_unlock(&sessions_mutex
);
1017 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
1019 struct lttng_id_tracker
*id_tracker
= m
->private;
1020 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1021 struct lttng_id_hash_node
*e
;
1024 mutex_lock(&sessions_mutex
);
1026 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1027 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1029 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1035 /* ID tracker disabled. */
1036 if (iter
>= *pos
&& iter
== 0) {
1037 return id_tracker_p
; /* empty tracker */
1045 /* Called with sessions_mutex held. */
1047 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1049 struct lttng_id_tracker
*id_tracker
= m
->private;
1050 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1051 struct lttng_id_hash_node
*e
;
1056 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1057 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1059 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1060 if (iter
++ >= *ppos
)
1065 /* ID tracker disabled. */
1066 if (iter
>= *ppos
&& iter
== 0)
1067 return p
; /* empty tracker */
1076 void id_list_stop(struct seq_file
*m
, void *p
)
1078 mutex_unlock(&sessions_mutex
);
1082 int id_list_show(struct seq_file
*m
, void *p
)
1084 struct lttng_id_tracker
*id_tracker
= m
->private;
1085 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1088 if (p
== id_tracker_p
) {
1089 /* Tracker disabled. */
1092 const struct lttng_id_hash_node
*e
= p
;
1094 id
= lttng_id_tracker_get_node_id(e
);
1096 switch (id_tracker
->tracker_type
) {
1098 seq_printf(m
, "process { pid = %d; };\n", id
);
1101 seq_printf(m
, "process { vpid = %d; };\n", id
);
1104 seq_printf(m
, "user { uid = %d; };\n", id
);
1107 seq_printf(m
, "user { vuid = %d; };\n", id
);
1110 seq_printf(m
, "group { gid = %d; };\n", id
);
1113 seq_printf(m
, "group { vgid = %d; };\n", id
);
1116 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1122 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1123 .start
= id_list_start
,
1124 .next
= id_list_next
,
1125 .stop
= id_list_stop
,
1126 .show
= id_list_show
,
1130 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1132 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1136 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1138 struct seq_file
*m
= file
->private_data
;
1139 struct lttng_id_tracker
*id_tracker
= m
->private;
1142 WARN_ON_ONCE(!id_tracker
);
1143 ret
= seq_release(inode
, file
);
1145 fput(id_tracker
->session
->file
);
1149 const struct file_operations lttng_tracker_ids_list_fops
= {
1150 .owner
= THIS_MODULE
,
1151 .open
= lttng_tracker_ids_list_open
,
1153 .llseek
= seq_lseek
,
1154 .release
= lttng_tracker_ids_list_release
,
1157 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1158 enum tracker_type tracker_type
)
1160 struct file
*tracker_ids_list_file
;
1164 file_fd
= lttng_get_unused_fd();
1170 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1171 <tng_tracker_ids_list_fops
,
1173 if (IS_ERR(tracker_ids_list_file
)) {
1174 ret
= PTR_ERR(tracker_ids_list_file
);
1177 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1179 goto refcount_error
;
1181 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1184 m
= tracker_ids_list_file
->private_data
;
1186 m
->private = get_tracker(session
, tracker_type
);
1187 BUG_ON(!m
->private);
1188 fd_install(file_fd
, tracker_ids_list_file
);
1193 atomic_long_dec(&session
->file
->f_count
);
1195 fput(tracker_ids_list_file
);
1197 put_unused_fd(file_fd
);
1203 * Enabler management.
1206 int lttng_match_enabler_star_glob(const char *desc_name
,
1207 const char *pattern
)
1209 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1210 desc_name
, LTTNG_SIZE_MAX
))
1216 int lttng_match_enabler_name(const char *desc_name
,
1219 if (strcmp(desc_name
, name
))
1225 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1226 struct lttng_enabler
*enabler
)
1228 const char *desc_name
, *enabler_name
;
1230 enabler_name
= enabler
->event_param
.name
;
1231 switch (enabler
->event_param
.instrumentation
) {
1232 case LTTNG_KERNEL_TRACEPOINT
:
1233 desc_name
= desc
->name
;
1235 case LTTNG_KERNEL_SYSCALL
:
1236 desc_name
= desc
->name
;
1237 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1238 desc_name
+= strlen("compat_");
1239 if (!strncmp(desc_name
, "syscall_exit_",
1240 strlen("syscall_exit_"))) {
1241 desc_name
+= strlen("syscall_exit_");
1242 } else if (!strncmp(desc_name
, "syscall_entry_",
1243 strlen("syscall_entry_"))) {
1244 desc_name
+= strlen("syscall_entry_");
1254 switch (enabler
->type
) {
1255 case LTTNG_ENABLER_STAR_GLOB
:
1256 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1257 case LTTNG_ENABLER_NAME
:
1258 return lttng_match_enabler_name(desc_name
, enabler_name
);
1265 int lttng_event_match_enabler(struct lttng_event
*event
,
1266 struct lttng_enabler
*enabler
)
1268 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1270 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1271 && event
->chan
== enabler
->chan
)
1278 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1279 struct lttng_enabler
*enabler
)
1281 struct lttng_enabler_ref
*enabler_ref
;
1283 list_for_each_entry(enabler_ref
,
1284 &event
->enablers_ref_head
, node
) {
1285 if (enabler_ref
->ref
== enabler
)
1292 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1294 struct lttng_session
*session
= enabler
->chan
->session
;
1295 struct lttng_probe_desc
*probe_desc
;
1296 const struct lttng_event_desc
*desc
;
1298 struct list_head
*probe_list
;
1300 probe_list
= lttng_get_probe_list_head();
1302 * For each probe event, if we find that a probe event matches
1303 * our enabler, create an associated lttng_event if not
1306 list_for_each_entry(probe_desc
, probe_list
, head
) {
1307 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1309 struct hlist_head
*head
;
1310 const char *event_name
;
1313 struct lttng_event
*event
;
1315 desc
= probe_desc
->event_desc
[i
];
1316 if (!lttng_desc_match_enabler(desc
, enabler
))
1318 event_name
= desc
->name
;
1319 name_len
= strlen(event_name
);
1322 * Check if already created.
1324 hash
= jhash(event_name
, name_len
, 0);
1325 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1326 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1327 if (event
->desc
== desc
1328 && event
->chan
== enabler
->chan
)
1335 * We need to create an event for this
1338 event
= _lttng_event_create(enabler
->chan
,
1340 LTTNG_KERNEL_TRACEPOINT
);
1342 printk(KERN_INFO
"Unable to create event %s\n",
1343 probe_desc
->event_desc
[i
]->name
);
1350 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1354 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1359 * Create struct lttng_event if it is missing and present in the list of
1360 * tracepoint probes.
1361 * Should be called with sessions mutex held.
1364 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1366 switch (enabler
->event_param
.instrumentation
) {
1367 case LTTNG_KERNEL_TRACEPOINT
:
1368 lttng_create_tracepoint_if_missing(enabler
);
1370 case LTTNG_KERNEL_SYSCALL
:
1371 lttng_create_syscall_if_missing(enabler
);
1380 * Create events associated with an enabler (if not already present),
1381 * and add backward reference from the event to the enabler.
1382 * Should be called with sessions mutex held.
1385 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1387 struct lttng_session
*session
= enabler
->chan
->session
;
1388 struct lttng_event
*event
;
1390 /* First ensure that probe events are created for this enabler. */
1391 lttng_create_event_if_missing(enabler
);
1393 /* For each event matching enabler in session event list. */
1394 list_for_each_entry(event
, &session
->events
, list
) {
1395 struct lttng_enabler_ref
*enabler_ref
;
1397 if (!lttng_event_match_enabler(event
, enabler
))
1399 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1402 * If no backward ref, create it.
1403 * Add backward ref from event to enabler.
1405 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1408 enabler_ref
->ref
= enabler
;
1409 list_add(&enabler_ref
->node
,
1410 &event
->enablers_ref_head
);
1414 * Link filter bytecodes if not linked yet.
1416 lttng_enabler_event_link_bytecode(event
, enabler
);
1418 /* TODO: merge event context. */
1424 * Called at module load: connect the probe on all enablers matching
1426 * Called with sessions lock held.
1428 int lttng_fix_pending_events(void)
1430 struct lttng_session
*session
;
1432 list_for_each_entry(session
, &sessions
, list
)
1433 lttng_session_lazy_sync_enablers(session
);
1437 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1438 struct lttng_kernel_event
*event_param
,
1439 struct lttng_channel
*chan
)
1441 struct lttng_enabler
*enabler
;
1443 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1446 enabler
->type
= type
;
1447 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1448 memcpy(&enabler
->event_param
, event_param
,
1449 sizeof(enabler
->event_param
));
1450 enabler
->chan
= chan
;
1452 enabler
->enabled
= 0;
1453 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1454 mutex_lock(&sessions_mutex
);
1455 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1456 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1457 mutex_unlock(&sessions_mutex
);
1461 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1463 mutex_lock(&sessions_mutex
);
1464 enabler
->enabled
= 1;
1465 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1466 mutex_unlock(&sessions_mutex
);
1470 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1472 mutex_lock(&sessions_mutex
);
1473 enabler
->enabled
= 0;
1474 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1475 mutex_unlock(&sessions_mutex
);
1479 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1480 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1482 struct lttng_filter_bytecode_node
*bytecode_node
;
1483 uint32_t bytecode_len
;
1486 ret
= get_user(bytecode_len
, &bytecode
->len
);
1489 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1493 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1494 sizeof(*bytecode
) + bytecode_len
);
1497 bytecode_node
->enabler
= enabler
;
1498 /* Enforce length based on allocated size */
1499 bytecode_node
->bc
.len
= bytecode_len
;
1500 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1501 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1505 kfree(bytecode_node
);
1509 int lttng_event_add_callsite(struct lttng_event
*event
,
1510 struct lttng_kernel_event_callsite __user
*callsite
)
1513 switch (event
->instrumentation
) {
1514 case LTTNG_KERNEL_UPROBE
:
1515 return lttng_uprobes_add_callsite(event
, callsite
);
1521 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1522 struct lttng_kernel_context
*context_param
)
1528 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1530 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1532 /* Destroy filter bytecode */
1533 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1534 &enabler
->filter_bytecode_head
, node
) {
1538 /* Destroy contexts */
1539 lttng_destroy_context(enabler
->ctx
);
1541 list_del(&enabler
->node
);
1546 * lttng_session_sync_enablers should be called just before starting a
1548 * Should be called with sessions mutex held.
1551 void lttng_session_sync_enablers(struct lttng_session
*session
)
1553 struct lttng_enabler
*enabler
;
1554 struct lttng_event
*event
;
1556 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1557 lttng_enabler_ref_events(enabler
);
1559 * For each event, if at least one of its enablers is enabled,
1560 * and its channel and session transient states are enabled, we
1561 * enable the event, else we disable it.
1563 list_for_each_entry(event
, &session
->events
, list
) {
1564 struct lttng_enabler_ref
*enabler_ref
;
1565 struct lttng_bytecode_runtime
*runtime
;
1566 int enabled
= 0, has_enablers_without_bytecode
= 0;
1568 switch (event
->instrumentation
) {
1569 case LTTNG_KERNEL_TRACEPOINT
:
1570 case LTTNG_KERNEL_SYSCALL
:
1572 list_for_each_entry(enabler_ref
,
1573 &event
->enablers_ref_head
, node
) {
1574 if (enabler_ref
->ref
->enabled
) {
1581 /* Not handled with lazy sync. */
1585 * Enabled state is based on union of enablers, with
1586 * intesection of session and channel transient enable
1589 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1591 WRITE_ONCE(event
->enabled
, enabled
);
1593 * Sync tracepoint registration with event enabled
1597 register_event(event
);
1599 _lttng_event_unregister(event
);
1602 /* Check if has enablers without bytecode enabled */
1603 list_for_each_entry(enabler_ref
,
1604 &event
->enablers_ref_head
, node
) {
1605 if (enabler_ref
->ref
->enabled
1606 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1607 has_enablers_without_bytecode
= 1;
1611 event
->has_enablers_without_bytecode
=
1612 has_enablers_without_bytecode
;
1614 /* Enable filters */
1615 list_for_each_entry(runtime
,
1616 &event
->bytecode_runtime_head
, node
)
1617 lttng_filter_sync_state(runtime
);
1622 * Apply enablers to session events, adding events to session if need
1623 * be. It is required after each modification applied to an active
1624 * session, and right before session "start".
1625 * "lazy" sync means we only sync if required.
1626 * Should be called with sessions mutex held.
1629 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1631 /* We can skip if session is not active */
1632 if (!session
->active
)
1634 lttng_session_sync_enablers(session
);
1638 * Serialize at most one packet worth of metadata into a metadata
1640 * We grab the metadata cache mutex to get exclusive access to our metadata
1641 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1642 * allows us to do racy operations such as looking for remaining space left in
1643 * packet and write, since mutual exclusion protects us from concurrent writes.
1644 * Mutual exclusion on the metadata cache allow us to read the cache content
1645 * without racing against reallocation of the cache by updates.
1646 * Returns the number of bytes written in the channel, 0 if no data
1647 * was written and a negative value on error.
1649 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1650 struct channel
*chan
)
1652 struct lib_ring_buffer_ctx ctx
;
1654 size_t len
, reserve_len
;
1657 * Ensure we support mutiple get_next / put sequences followed by
1658 * put_next. The metadata cache lock protects reading the metadata
1659 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1660 * "flush" operations on the buffer invoked by different processes.
1661 * Moreover, since the metadata cache memory can be reallocated, we
1662 * need to have exclusive access against updates even though we only
1665 mutex_lock(&stream
->metadata_cache
->lock
);
1666 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1667 if (stream
->metadata_in
!= stream
->metadata_out
)
1670 /* Metadata regenerated, change the version. */
1671 if (stream
->metadata_cache
->version
!= stream
->version
)
1672 stream
->version
= stream
->metadata_cache
->version
;
1674 len
= stream
->metadata_cache
->metadata_written
-
1675 stream
->metadata_in
;
1678 reserve_len
= min_t(size_t,
1679 stream
->transport
->ops
.packet_avail_size(chan
),
1681 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1684 * If reservation failed, return an error to the caller.
1686 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1688 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1691 stream
->transport
->ops
.event_write(&ctx
,
1692 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1694 stream
->transport
->ops
.event_commit(&ctx
);
1695 stream
->metadata_in
+= reserve_len
;
1699 mutex_unlock(&stream
->metadata_cache
->lock
);
1704 * Write the metadata to the metadata cache.
1705 * Must be called with sessions_mutex held.
1706 * The metadata cache lock protects us from concurrent read access from
1707 * thread outputting metadata content to ring buffer.
1709 int lttng_metadata_printf(struct lttng_session
*session
,
1710 const char *fmt
, ...)
1715 struct lttng_metadata_stream
*stream
;
1717 WARN_ON_ONCE(!READ_ONCE(session
->active
));
1720 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1726 mutex_lock(&session
->metadata_cache
->lock
);
1727 if (session
->metadata_cache
->metadata_written
+ len
>
1728 session
->metadata_cache
->cache_alloc
) {
1729 char *tmp_cache_realloc
;
1730 unsigned int tmp_cache_alloc_size
;
1732 tmp_cache_alloc_size
= max_t(unsigned int,
1733 session
->metadata_cache
->cache_alloc
+ len
,
1734 session
->metadata_cache
->cache_alloc
<< 1);
1735 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
1736 if (!tmp_cache_realloc
)
1738 if (session
->metadata_cache
->data
) {
1739 memcpy(tmp_cache_realloc
,
1740 session
->metadata_cache
->data
,
1741 session
->metadata_cache
->cache_alloc
);
1742 vfree(session
->metadata_cache
->data
);
1745 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1746 session
->metadata_cache
->data
= tmp_cache_realloc
;
1748 memcpy(session
->metadata_cache
->data
+
1749 session
->metadata_cache
->metadata_written
,
1751 session
->metadata_cache
->metadata_written
+= len
;
1752 mutex_unlock(&session
->metadata_cache
->lock
);
1755 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1756 wake_up_interruptible(&stream
->read_wait
);
1761 mutex_unlock(&session
->metadata_cache
->lock
);
1767 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1771 for (i
= 0; i
< nesting
; i
++) {
1774 ret
= lttng_metadata_printf(session
, " ");
1783 * Must be called with sessions_mutex held.
1786 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1787 const struct lttng_type
*type
,
1791 uint32_t i
, nr_fields
;
1793 ret
= print_tabs(session
, nesting
);
1796 ret
= lttng_metadata_printf(session
,
1800 nr_fields
= type
->u
._struct
.nr_fields
;
1801 for (i
= 0; i
< nr_fields
; i
++) {
1802 const struct lttng_event_field
*iter_field
;
1804 iter_field
= &type
->u
._struct
.fields
[i
];
1805 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1809 ret
= print_tabs(session
, nesting
);
1812 ret
= lttng_metadata_printf(session
,
1818 * Must be called with sessions_mutex held.
1821 int _lttng_struct_statedump(struct lttng_session
*session
,
1822 const struct lttng_event_field
*field
,
1827 ret
= _lttng_struct_type_statedump(session
,
1828 &field
->type
, nesting
);
1831 ret
= lttng_metadata_printf(session
,
1838 * Must be called with sessions_mutex held.
1841 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1842 const struct lttng_type
*type
,
1846 uint32_t i
, nr_choices
;
1848 ret
= print_tabs(session
, nesting
);
1851 ret
= lttng_metadata_printf(session
,
1852 "variant <_%s> {\n",
1853 type
->u
.variant
.tag_name
);
1856 nr_choices
= type
->u
.variant
.nr_choices
;
1857 for (i
= 0; i
< nr_choices
; i
++) {
1858 const struct lttng_event_field
*iter_field
;
1860 iter_field
= &type
->u
.variant
.choices
[i
];
1861 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1865 ret
= print_tabs(session
, nesting
);
1868 ret
= lttng_metadata_printf(session
,
1874 * Must be called with sessions_mutex held.
1877 int _lttng_variant_statedump(struct lttng_session
*session
,
1878 const struct lttng_event_field
*field
,
1883 ret
= _lttng_variant_type_statedump(session
,
1884 &field
->type
, nesting
);
1887 ret
= lttng_metadata_printf(session
,
1894 * Must be called with sessions_mutex held.
1897 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1898 const struct lttng_event_field
*field
,
1902 const struct lttng_type
*elem_type
;
1904 /* Only array of structures and variants are currently supported. */
1905 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1906 switch (elem_type
->atype
) {
1908 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1913 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1920 ret
= lttng_metadata_printf(session
,
1923 field
->type
.u
.array_compound
.length
);
1928 * Must be called with sessions_mutex held.
1931 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
1932 const struct lttng_event_field
*field
,
1936 const char *length_name
;
1937 const struct lttng_type
*elem_type
;
1939 length_name
= field
->type
.u
.sequence_compound
.length_name
;
1941 /* Only array of structures and variants are currently supported. */
1942 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
1943 switch (elem_type
->atype
) {
1945 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1950 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1957 ret
= lttng_metadata_printf(session
,
1965 * Must be called with sessions_mutex held.
1968 int _lttng_enum_statedump(struct lttng_session
*session
,
1969 const struct lttng_event_field
*field
,
1972 const struct lttng_enum_desc
*enum_desc
;
1973 const struct lttng_integer_type
*container_type
;
1975 unsigned int i
, nr_entries
;
1977 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
1978 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
1979 nr_entries
= enum_desc
->nr_entries
;
1981 ret
= print_tabs(session
, nesting
);
1984 ret
= lttng_metadata_printf(session
,
1985 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
1986 container_type
->size
,
1987 container_type
->alignment
,
1988 container_type
->signedness
,
1989 (container_type
->encoding
== lttng_encode_none
)
1991 : (container_type
->encoding
== lttng_encode_UTF8
)
1994 container_type
->base
,
1995 #if __BYTE_ORDER == __BIG_ENDIAN
1996 container_type
->reverse_byte_order
? " byte_order = le;" : ""
1998 container_type
->reverse_byte_order
? " byte_order = be;" : ""
2003 /* Dump all entries */
2004 for (i
= 0; i
< nr_entries
; i
++) {
2005 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
2008 ret
= print_tabs(session
, nesting
+ 1);
2011 ret
= lttng_metadata_printf(session
,
2015 len
= strlen(entry
->string
);
2016 /* Escape the character '"' */
2017 for (j
= 0; j
< len
; j
++) {
2018 char c
= entry
->string
[j
];
2022 ret
= lttng_metadata_printf(session
,
2026 ret
= lttng_metadata_printf(session
,
2030 ret
= lttng_metadata_printf(session
,
2037 ret
= lttng_metadata_printf(session
, "\"");
2041 if (entry
->options
.is_auto
) {
2042 ret
= lttng_metadata_printf(session
, ",\n");
2046 ret
= lttng_metadata_printf(session
,
2050 if (entry
->start
.signedness
)
2051 ret
= lttng_metadata_printf(session
,
2052 "%lld", (long long) entry
->start
.value
);
2054 ret
= lttng_metadata_printf(session
,
2055 "%llu", entry
->start
.value
);
2058 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2060 == entry
->end
.value
) {
2061 ret
= lttng_metadata_printf(session
,
2064 if (entry
->end
.signedness
) {
2065 ret
= lttng_metadata_printf(session
,
2067 (long long) entry
->end
.value
);
2069 ret
= lttng_metadata_printf(session
,
2078 ret
= print_tabs(session
, nesting
);
2081 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2088 * Must be called with sessions_mutex held.
2091 int _lttng_field_statedump(struct lttng_session
*session
,
2092 const struct lttng_event_field
*field
,
2097 switch (field
->type
.atype
) {
2099 ret
= print_tabs(session
, nesting
);
2102 ret
= lttng_metadata_printf(session
,
2103 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2104 field
->type
.u
.basic
.integer
.size
,
2105 field
->type
.u
.basic
.integer
.alignment
,
2106 field
->type
.u
.basic
.integer
.signedness
,
2107 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2109 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2112 field
->type
.u
.basic
.integer
.base
,
2113 #if __BYTE_ORDER == __BIG_ENDIAN
2114 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2116 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2121 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2124 case atype_array_bitfield
:
2126 const struct lttng_basic_type
*elem_type
;
2128 elem_type
= &field
->type
.u
.array
.elem_type
;
2129 if (field
->type
.u
.array
.elem_alignment
) {
2130 ret
= print_tabs(session
, nesting
);
2133 ret
= lttng_metadata_printf(session
,
2134 "struct { } align(%u) _%s_padding;\n",
2135 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2140 ret
= print_tabs(session
, nesting
);
2143 ret
= lttng_metadata_printf(session
,
2144 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2145 elem_type
->u
.basic
.integer
.size
,
2146 elem_type
->u
.basic
.integer
.alignment
,
2147 elem_type
->u
.basic
.integer
.signedness
,
2148 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2150 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2153 elem_type
->u
.basic
.integer
.base
,
2154 #if __BYTE_ORDER == __BIG_ENDIAN
2155 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2157 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2159 field
->name
, field
->type
.u
.array
.length
);
2162 case atype_sequence
:
2163 case atype_sequence_bitfield
:
2165 const struct lttng_basic_type
*elem_type
;
2166 const struct lttng_basic_type
*length_type
;
2168 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2169 length_type
= &field
->type
.u
.sequence
.length_type
;
2170 ret
= print_tabs(session
, nesting
);
2173 ret
= lttng_metadata_printf(session
,
2174 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2175 length_type
->u
.basic
.integer
.size
,
2176 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2177 length_type
->u
.basic
.integer
.signedness
,
2178 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2180 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2183 length_type
->u
.basic
.integer
.base
,
2184 #if __BYTE_ORDER == __BIG_ENDIAN
2185 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2187 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2193 if (field
->type
.u
.sequence
.elem_alignment
) {
2194 ret
= print_tabs(session
, nesting
);
2197 ret
= lttng_metadata_printf(session
,
2198 "struct { } align(%u) _%s_padding;\n",
2199 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
2204 ret
= print_tabs(session
, nesting
);
2207 ret
= lttng_metadata_printf(session
,
2208 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2209 elem_type
->u
.basic
.integer
.size
,
2210 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2211 elem_type
->u
.basic
.integer
.signedness
,
2212 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2214 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2217 elem_type
->u
.basic
.integer
.base
,
2218 #if __BYTE_ORDER == __BIG_ENDIAN
2219 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2221 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2229 /* Default encoding is UTF8 */
2230 ret
= print_tabs(session
, nesting
);
2233 ret
= lttng_metadata_printf(session
,
2235 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2236 " { encoding = ASCII; }" : "",
2240 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2242 case atype_array_compound
:
2243 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2245 case atype_sequence_compound
:
2246 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2249 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2260 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2261 struct lttng_ctx
*ctx
)
2268 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2269 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2271 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2279 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2280 struct lttng_event
*event
)
2282 const struct lttng_event_desc
*desc
= event
->desc
;
2286 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2287 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2289 ret
= _lttng_field_statedump(session
, field
, 2);
2297 * Must be called with sessions_mutex held.
2300 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2301 struct lttng_channel
*chan
,
2302 struct lttng_event
*event
)
2306 if (event
->metadata_dumped
|| !READ_ONCE(session
->active
))
2308 if (chan
->channel_type
== METADATA_CHANNEL
)
2311 ret
= lttng_metadata_printf(session
,
2315 " stream_id = %u;\n",
2323 ret
= lttng_metadata_printf(session
,
2324 " context := struct {\n");
2328 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2332 ret
= lttng_metadata_printf(session
,
2338 ret
= lttng_metadata_printf(session
,
2339 " fields := struct {\n"
2344 ret
= _lttng_fields_metadata_statedump(session
, event
);
2349 * LTTng space reservation can only reserve multiples of the
2352 ret
= lttng_metadata_printf(session
,
2358 event
->metadata_dumped
= 1;
2365 * Must be called with sessions_mutex held.
2368 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2369 struct lttng_channel
*chan
)
2373 if (chan
->metadata_dumped
|| !READ_ONCE(session
->active
))
2376 if (chan
->channel_type
== METADATA_CHANNEL
)
2379 WARN_ON_ONCE(!chan
->header_type
);
2380 ret
= lttng_metadata_printf(session
,
2383 " event.header := %s;\n"
2384 " packet.context := struct packet_context;\n",
2386 chan
->header_type
== 1 ? "struct event_header_compact" :
2387 "struct event_header_large");
2392 ret
= lttng_metadata_printf(session
,
2393 " event.context := struct {\n");
2397 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2401 ret
= lttng_metadata_printf(session
,
2407 ret
= lttng_metadata_printf(session
,
2410 chan
->metadata_dumped
= 1;
2416 * Must be called with sessions_mutex held.
2419 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2421 return lttng_metadata_printf(session
,
2422 "struct packet_context {\n"
2423 " uint64_clock_monotonic_t timestamp_begin;\n"
2424 " uint64_clock_monotonic_t timestamp_end;\n"
2425 " uint64_t content_size;\n"
2426 " uint64_t packet_size;\n"
2427 " uint64_t packet_seq_num;\n"
2428 " unsigned long events_discarded;\n"
2429 " uint32_t cpu_id;\n"
2436 * id: range: 0 - 30.
2437 * id 31 is reserved to indicate an extended header.
2440 * id: range: 0 - 65534.
2441 * id 65535 is reserved to indicate an extended header.
2443 * Must be called with sessions_mutex held.
2446 int _lttng_event_header_declare(struct lttng_session
*session
)
2448 return lttng_metadata_printf(session
,
2449 "struct event_header_compact {\n"
2450 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2453 " uint27_clock_monotonic_t timestamp;\n"
2457 " uint64_clock_monotonic_t timestamp;\n"
2462 "struct event_header_large {\n"
2463 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2466 " uint32_clock_monotonic_t timestamp;\n"
2470 " uint64_clock_monotonic_t timestamp;\n"
2474 lttng_alignof(uint32_t) * CHAR_BIT
,
2475 lttng_alignof(uint16_t) * CHAR_BIT
2480 * Approximation of NTP time of day to clock monotonic correlation,
2481 * taken at start of trace.
2482 * Yes, this is only an approximation. Yes, we can (and will) do better
2483 * in future versions.
2484 * This function may return a negative offset. It may happen if the
2485 * system sets the REALTIME clock to 0 after boot.
2488 int64_t measure_clock_offset(void)
2490 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2491 uint64_t tcf
= trace_clock_freq();
2493 struct timespec rts
= { 0, 0 };
2494 unsigned long flags
;
2496 /* Disable interrupts to increase correlation precision. */
2497 local_irq_save(flags
);
2498 monotonic
[0] = trace_clock_read64();
2499 getnstimeofday(&rts
);
2500 monotonic
[1] = trace_clock_read64();
2501 local_irq_restore(flags
);
2503 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2504 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2505 if (tcf
== NSEC_PER_SEC
) {
2506 realtime
+= rts
.tv_nsec
;
2508 uint64_t n
= rts
.tv_nsec
* tcf
;
2510 do_div(n
, NSEC_PER_SEC
);
2513 offset
= (int64_t) realtime
- monotonic_avg
;
2518 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2526 while (cur
!= '\0') {
2529 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2533 ret
= lttng_metadata_printf(session
, "%c", '\\');
2536 /* We still print the current char */
2539 ret
= lttng_metadata_printf(session
, "%c", cur
);
2553 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2554 const char *field_value
)
2558 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2562 ret
= print_escaped_ctf_string(session
, field_value
);
2566 ret
= lttng_metadata_printf(session
, "\";\n");
2573 * Output metadata into this session's metadata buffers.
2574 * Must be called with sessions_mutex held.
2577 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2579 unsigned char *uuid_c
= session
->uuid
.b
;
2580 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2581 const char *product_uuid
;
2582 struct lttng_channel
*chan
;
2583 struct lttng_event
*event
;
2586 if (!READ_ONCE(session
->active
))
2588 if (session
->metadata_dumped
)
2591 snprintf(uuid_s
, sizeof(uuid_s
),
2592 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2593 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2594 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2595 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2596 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2598 ret
= lttng_metadata_printf(session
,
2599 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2600 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2601 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2602 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2603 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2604 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2605 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2611 " byte_order = %s;\n"
2612 " packet.header := struct {\n"
2613 " uint32_t magic;\n"
2614 " uint8_t uuid[16];\n"
2615 " uint32_t stream_id;\n"
2616 " uint64_t stream_instance_id;\n"
2619 lttng_alignof(uint8_t) * CHAR_BIT
,
2620 lttng_alignof(uint16_t) * CHAR_BIT
,
2621 lttng_alignof(uint32_t) * CHAR_BIT
,
2622 lttng_alignof(uint64_t) * CHAR_BIT
,
2623 sizeof(unsigned long) * CHAR_BIT
,
2624 lttng_alignof(unsigned long) * CHAR_BIT
,
2628 #if __BYTE_ORDER == __BIG_ENDIAN
2637 ret
= lttng_metadata_printf(session
,
2639 " hostname = \"%s\";\n"
2640 " domain = \"kernel\";\n"
2641 " sysname = \"%s\";\n"
2642 " kernel_release = \"%s\";\n"
2643 " kernel_version = \"%s\";\n"
2644 " tracer_name = \"lttng-modules\";\n"
2645 " tracer_major = %d;\n"
2646 " tracer_minor = %d;\n"
2647 " tracer_patchlevel = %d;\n"
2648 " trace_buffering_scheme = \"global\";\n",
2649 current
->nsproxy
->uts_ns
->name
.nodename
,
2653 LTTNG_MODULES_MAJOR_VERSION
,
2654 LTTNG_MODULES_MINOR_VERSION
,
2655 LTTNG_MODULES_PATCHLEVEL_VERSION
2660 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2663 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2664 session
->creation_time
);
2668 /* Add the product UUID to the 'env' section */
2669 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
2671 ret
= lttng_metadata_printf(session
,
2672 " product_uuid = \"%s\";\n",
2679 /* Close the 'env' section */
2680 ret
= lttng_metadata_printf(session
, "};\n\n");
2684 ret
= lttng_metadata_printf(session
,
2686 " name = \"%s\";\n",
2692 if (!trace_clock_uuid(clock_uuid_s
)) {
2693 ret
= lttng_metadata_printf(session
,
2694 " uuid = \"%s\";\n",
2701 ret
= lttng_metadata_printf(session
,
2702 " description = \"%s\";\n"
2703 " freq = %llu; /* Frequency, in Hz */\n"
2704 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2707 trace_clock_description(),
2708 (unsigned long long) trace_clock_freq(),
2709 (long long) measure_clock_offset()
2714 ret
= lttng_metadata_printf(session
,
2715 "typealias integer {\n"
2716 " size = 27; align = 1; signed = false;\n"
2717 " map = clock.%s.value;\n"
2718 "} := uint27_clock_monotonic_t;\n"
2720 "typealias integer {\n"
2721 " size = 32; align = %u; signed = false;\n"
2722 " map = clock.%s.value;\n"
2723 "} := uint32_clock_monotonic_t;\n"
2725 "typealias integer {\n"
2726 " size = 64; align = %u; signed = false;\n"
2727 " map = clock.%s.value;\n"
2728 "} := uint64_clock_monotonic_t;\n\n",
2730 lttng_alignof(uint32_t) * CHAR_BIT
,
2732 lttng_alignof(uint64_t) * CHAR_BIT
,
2738 ret
= _lttng_stream_packet_context_declare(session
);
2742 ret
= _lttng_event_header_declare(session
);
2747 list_for_each_entry(chan
, &session
->chan
, list
) {
2748 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2753 list_for_each_entry(event
, &session
->events
, list
) {
2754 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2758 session
->metadata_dumped
= 1;
2764 * lttng_transport_register - LTT transport registration
2765 * @transport: transport structure
2767 * Registers a transport which can be used as output to extract the data out of
2768 * LTTng. The module calling this registration function must ensure that no
2769 * trap-inducing code will be executed by the transport functions. E.g.
2770 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2771 * is made visible to the transport function. This registration acts as a
2772 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2773 * after its registration must it synchronize the TLBs.
2775 void lttng_transport_register(struct lttng_transport
*transport
)
2778 * Make sure no page fault can be triggered by the module about to be
2779 * registered. We deal with this here so we don't have to call
2780 * vmalloc_sync_all() in each module's init.
2782 wrapper_vmalloc_sync_all();
2784 mutex_lock(&sessions_mutex
);
2785 list_add_tail(&transport
->node
, <tng_transport_list
);
2786 mutex_unlock(&sessions_mutex
);
2788 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2791 * lttng_transport_unregister - LTT transport unregistration
2792 * @transport: transport structure
2794 void lttng_transport_unregister(struct lttng_transport
*transport
)
2796 mutex_lock(&sessions_mutex
);
2797 list_del(&transport
->node
);
2798 mutex_unlock(&sessions_mutex
);
2800 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2802 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2804 enum cpuhp_state lttng_hp_prepare
;
2805 enum cpuhp_state lttng_hp_online
;
2807 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2809 struct lttng_cpuhp_node
*lttng_node
;
2811 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2812 switch (lttng_node
->component
) {
2813 case LTTNG_RING_BUFFER_FRONTEND
:
2815 case LTTNG_RING_BUFFER_BACKEND
:
2816 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2817 case LTTNG_RING_BUFFER_ITER
:
2819 case LTTNG_CONTEXT_PERF_COUNTERS
:
2826 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2828 struct lttng_cpuhp_node
*lttng_node
;
2830 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2831 switch (lttng_node
->component
) {
2832 case LTTNG_RING_BUFFER_FRONTEND
:
2833 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2834 case LTTNG_RING_BUFFER_BACKEND
:
2836 case LTTNG_RING_BUFFER_ITER
:
2838 case LTTNG_CONTEXT_PERF_COUNTERS
:
2839 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2845 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2847 struct lttng_cpuhp_node
*lttng_node
;
2849 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2850 switch (lttng_node
->component
) {
2851 case LTTNG_RING_BUFFER_FRONTEND
:
2852 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2853 case LTTNG_RING_BUFFER_BACKEND
:
2855 case LTTNG_RING_BUFFER_ITER
:
2856 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2857 case LTTNG_CONTEXT_PERF_COUNTERS
:
2858 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2864 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2866 struct lttng_cpuhp_node
*lttng_node
;
2868 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2869 switch (lttng_node
->component
) {
2870 case LTTNG_RING_BUFFER_FRONTEND
:
2871 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2872 case LTTNG_RING_BUFFER_BACKEND
:
2874 case LTTNG_RING_BUFFER_ITER
:
2876 case LTTNG_CONTEXT_PERF_COUNTERS
:
2883 static int __init
lttng_init_cpu_hotplug(void)
2887 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2888 lttng_hotplug_prepare
,
2889 lttng_hotplug_dead
);
2893 lttng_hp_prepare
= ret
;
2894 lttng_rb_set_hp_prepare(ret
);
2896 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
2897 lttng_hotplug_online
,
2898 lttng_hotplug_offline
);
2900 cpuhp_remove_multi_state(lttng_hp_prepare
);
2901 lttng_hp_prepare
= 0;
2904 lttng_hp_online
= ret
;
2905 lttng_rb_set_hp_online(ret
);
2910 static void __exit
lttng_exit_cpu_hotplug(void)
2912 lttng_rb_set_hp_online(0);
2913 cpuhp_remove_multi_state(lttng_hp_online
);
2914 lttng_rb_set_hp_prepare(0);
2915 cpuhp_remove_multi_state(lttng_hp_prepare
);
2918 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2919 static int lttng_init_cpu_hotplug(void)
2923 static void lttng_exit_cpu_hotplug(void)
2926 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2929 static int __init
lttng_events_init(void)
2933 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2936 ret
= wrapper_get_pfnblock_flags_mask_init();
2939 ret
= wrapper_get_pageblock_flags_mask_init();
2942 ret
= lttng_probes_init();
2945 ret
= lttng_context_init();
2948 ret
= lttng_tracepoint_init();
2951 event_cache
= KMEM_CACHE(lttng_event
, 0);
2956 ret
= lttng_abi_init();
2959 ret
= lttng_logger_init();
2962 ret
= lttng_init_cpu_hotplug();
2965 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
2966 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
2967 __stringify(LTTNG_MODULES_MINOR_VERSION
),
2968 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
2969 LTTNG_MODULES_EXTRAVERSION
,
2971 #ifdef LTTNG_EXTRA_VERSION_GIT
2972 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
2976 #ifdef LTTNG_EXTRA_VERSION_NAME
2977 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
2984 lttng_logger_exit();
2988 kmem_cache_destroy(event_cache
);
2990 lttng_tracepoint_exit();
2992 lttng_context_exit();
2993 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
2994 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
2995 __stringify(LTTNG_MODULES_MINOR_VERSION
),
2996 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
2997 LTTNG_MODULES_EXTRAVERSION
,
2999 #ifdef LTTNG_EXTRA_VERSION_GIT
3000 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3004 #ifdef LTTNG_EXTRA_VERSION_NAME
3005 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3012 module_init(lttng_events_init
);
3014 static void __exit
lttng_events_exit(void)
3016 struct lttng_session
*session
, *tmpsession
;
3018 lttng_exit_cpu_hotplug();
3019 lttng_logger_exit();
3021 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3022 lttng_session_destroy(session
);
3023 kmem_cache_destroy(event_cache
);
3024 lttng_tracepoint_exit();
3025 lttng_context_exit();
3026 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3027 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3028 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3029 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3030 LTTNG_MODULES_EXTRAVERSION
,
3032 #ifdef LTTNG_EXTRA_VERSION_GIT
3033 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3037 #ifdef LTTNG_EXTRA_VERSION_NAME
3038 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3044 module_exit(lttng_events_exit
);
3046 #include "extra_version/patches.i"
3047 #ifdef LTTNG_EXTRA_VERSION_GIT
3048 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3050 #ifdef LTTNG_EXTRA_VERSION_NAME
3051 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3053 MODULE_LICENSE("GPL and additional rights");
3054 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3055 MODULE_DESCRIPTION("LTTng tracer");
3056 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3057 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3058 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3059 LTTNG_MODULES_EXTRAVERSION
);