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_safe(enabler
, tmpenabler
,
194 &session
->enablers_head
, node
)
195 lttng_enabler_destroy(enabler
);
196 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
197 _lttng_event_destroy(event
);
198 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
199 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
200 _lttng_channel_destroy(chan
);
202 mutex_lock(&session
->metadata_cache
->lock
);
203 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
204 _lttng_metadata_channel_hangup(metadata_stream
);
205 mutex_unlock(&session
->metadata_cache
->lock
);
206 if (session
->pid_tracker
)
207 lttng_pid_tracker_destroy(session
->pid_tracker
);
208 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
209 list_del(&session
->list
);
210 mutex_unlock(&sessions_mutex
);
211 lttng_kvfree(session
);
214 int lttng_session_statedump(struct lttng_session
*session
)
218 mutex_lock(&sessions_mutex
);
219 ret
= lttng_statedump_start(session
);
220 mutex_unlock(&sessions_mutex
);
224 int lttng_session_enable(struct lttng_session
*session
)
227 struct lttng_channel
*chan
;
229 mutex_lock(&sessions_mutex
);
230 if (session
->active
) {
235 /* Set transient enabler state to "enabled" */
239 * Snapshot the number of events per channel to know the type of header
242 list_for_each_entry(chan
, &session
->chan
, list
) {
243 if (chan
->header_type
)
244 continue; /* don't change it if session stop/restart */
245 if (chan
->free_event_id
< 31)
246 chan
->header_type
= 1; /* compact */
248 chan
->header_type
= 2; /* large */
251 /* We need to sync enablers with session before activation. */
252 lttng_session_sync_enablers(session
);
254 /* Clear each stream's quiescent state. */
255 list_for_each_entry(chan
, &session
->chan
, list
) {
256 if (chan
->channel_type
!= METADATA_CHANNEL
)
257 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
260 WRITE_ONCE(session
->active
, 1);
261 WRITE_ONCE(session
->been_active
, 1);
262 ret
= _lttng_session_metadata_statedump(session
);
264 WRITE_ONCE(session
->active
, 0);
267 ret
= lttng_statedump_start(session
);
269 WRITE_ONCE(session
->active
, 0);
271 mutex_unlock(&sessions_mutex
);
275 int lttng_session_disable(struct lttng_session
*session
)
278 struct lttng_channel
*chan
;
280 mutex_lock(&sessions_mutex
);
281 if (!session
->active
) {
285 WRITE_ONCE(session
->active
, 0);
287 /* Set transient enabler state to "disabled" */
289 lttng_session_sync_enablers(session
);
291 /* Set each stream's quiescent state. */
292 list_for_each_entry(chan
, &session
->chan
, list
) {
293 if (chan
->channel_type
!= METADATA_CHANNEL
)
294 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
297 mutex_unlock(&sessions_mutex
);
301 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
304 struct lttng_channel
*chan
;
305 struct lttng_event
*event
;
306 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
307 struct lttng_metadata_stream
*stream
;
309 mutex_lock(&sessions_mutex
);
310 if (!session
->active
) {
315 mutex_lock(&cache
->lock
);
316 memset(cache
->data
, 0, cache
->cache_alloc
);
317 cache
->metadata_written
= 0;
319 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
320 stream
->metadata_out
= 0;
321 stream
->metadata_in
= 0;
323 mutex_unlock(&cache
->lock
);
325 session
->metadata_dumped
= 0;
326 list_for_each_entry(chan
, &session
->chan
, list
) {
327 chan
->metadata_dumped
= 0;
330 list_for_each_entry(event
, &session
->events
, list
) {
331 event
->metadata_dumped
= 0;
334 ret
= _lttng_session_metadata_statedump(session
);
337 mutex_unlock(&sessions_mutex
);
341 int lttng_channel_enable(struct lttng_channel
*channel
)
345 mutex_lock(&sessions_mutex
);
346 if (channel
->channel_type
== METADATA_CHANNEL
) {
350 if (channel
->enabled
) {
354 /* Set transient enabler state to "enabled" */
356 lttng_session_sync_enablers(channel
->session
);
357 /* Set atomically the state to "enabled" */
358 WRITE_ONCE(channel
->enabled
, 1);
360 mutex_unlock(&sessions_mutex
);
364 int lttng_channel_disable(struct lttng_channel
*channel
)
368 mutex_lock(&sessions_mutex
);
369 if (channel
->channel_type
== METADATA_CHANNEL
) {
373 if (!channel
->enabled
) {
377 /* Set atomically the state to "disabled" */
378 WRITE_ONCE(channel
->enabled
, 0);
379 /* Set transient enabler state to "enabled" */
381 lttng_session_sync_enablers(channel
->session
);
383 mutex_unlock(&sessions_mutex
);
387 int lttng_event_enable(struct lttng_event
*event
)
391 mutex_lock(&sessions_mutex
);
392 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
396 if (event
->enabled
) {
400 switch (event
->instrumentation
) {
401 case LTTNG_KERNEL_TRACEPOINT
:
402 case LTTNG_KERNEL_SYSCALL
:
405 case LTTNG_KERNEL_KPROBE
:
406 case LTTNG_KERNEL_UPROBE
:
407 case LTTNG_KERNEL_NOOP
:
408 WRITE_ONCE(event
->enabled
, 1);
410 case LTTNG_KERNEL_KRETPROBE
:
411 ret
= lttng_kretprobes_event_enable_state(event
, 1);
413 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
419 mutex_unlock(&sessions_mutex
);
423 int lttng_event_disable(struct lttng_event
*event
)
427 mutex_lock(&sessions_mutex
);
428 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
432 if (!event
->enabled
) {
436 switch (event
->instrumentation
) {
437 case LTTNG_KERNEL_TRACEPOINT
:
438 case LTTNG_KERNEL_SYSCALL
:
441 case LTTNG_KERNEL_KPROBE
:
442 case LTTNG_KERNEL_UPROBE
:
443 case LTTNG_KERNEL_NOOP
:
444 WRITE_ONCE(event
->enabled
, 0);
446 case LTTNG_KERNEL_KRETPROBE
:
447 ret
= lttng_kretprobes_event_enable_state(event
, 0);
449 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
455 mutex_unlock(&sessions_mutex
);
459 static struct lttng_transport
*lttng_transport_find(const char *name
)
461 struct lttng_transport
*transport
;
463 list_for_each_entry(transport
, <tng_transport_list
, node
) {
464 if (!strcmp(transport
->name
, name
))
470 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
471 const char *transport_name
,
473 size_t subbuf_size
, size_t num_subbuf
,
474 unsigned int switch_timer_interval
,
475 unsigned int read_timer_interval
,
476 enum channel_type channel_type
)
478 struct lttng_channel
*chan
;
479 struct lttng_transport
*transport
= NULL
;
481 mutex_lock(&sessions_mutex
);
482 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
483 goto active
; /* Refuse to add channel to active session */
484 transport
= lttng_transport_find(transport_name
);
486 printk(KERN_WARNING
"LTTng transport %s not found\n",
490 if (!try_module_get(transport
->owner
)) {
491 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
494 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
497 chan
->session
= session
;
498 chan
->id
= session
->free_chan_id
++;
499 chan
->ops
= &transport
->ops
;
501 * Note: the channel creation op already writes into the packet
502 * headers. Therefore the "chan" information used as input
503 * should be already accessible.
505 chan
->chan
= transport
->ops
.channel_create(transport_name
,
506 chan
, buf_addr
, subbuf_size
, num_subbuf
,
507 switch_timer_interval
, read_timer_interval
);
512 chan
->transport
= transport
;
513 chan
->channel_type
= channel_type
;
514 list_add(&chan
->list
, &session
->chan
);
515 mutex_unlock(&sessions_mutex
);
522 module_put(transport
->owner
);
525 mutex_unlock(&sessions_mutex
);
530 * Only used internally at session destruction for per-cpu channels, and
531 * when metadata channel is released.
532 * Needs to be called with sessions mutex held.
535 void _lttng_channel_destroy(struct lttng_channel
*chan
)
537 chan
->ops
->channel_destroy(chan
->chan
);
538 module_put(chan
->transport
->owner
);
539 list_del(&chan
->list
);
540 lttng_destroy_context(chan
->ctx
);
544 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
546 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
548 /* Protect the metadata cache with the sessions_mutex. */
549 mutex_lock(&sessions_mutex
);
550 _lttng_channel_destroy(chan
);
551 mutex_unlock(&sessions_mutex
);
553 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
556 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
558 stream
->finalized
= 1;
559 wake_up_interruptible(&stream
->read_wait
);
563 * Supports event creation while tracing session is active.
564 * Needs to be called with sessions mutex held.
566 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
567 struct lttng_kernel_event
*event_param
,
569 const struct lttng_event_desc
*event_desc
,
570 enum lttng_kernel_instrumentation itype
)
572 struct lttng_session
*session
= chan
->session
;
573 struct lttng_event
*event
;
574 const char *event_name
;
575 struct hlist_head
*head
;
580 if (chan
->free_event_id
== -1U) {
586 case LTTNG_KERNEL_TRACEPOINT
:
587 event_name
= event_desc
->name
;
589 case LTTNG_KERNEL_KPROBE
:
590 case LTTNG_KERNEL_UPROBE
:
591 case LTTNG_KERNEL_KRETPROBE
:
592 case LTTNG_KERNEL_NOOP
:
593 case LTTNG_KERNEL_SYSCALL
:
594 event_name
= event_param
->name
;
596 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
602 name_len
= strlen(event_name
);
603 hash
= jhash(event_name
, name_len
, 0);
604 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
605 lttng_hlist_for_each_entry(event
, head
, hlist
) {
606 WARN_ON_ONCE(!event
->desc
);
607 if (!strncmp(event
->desc
->name
, event_name
,
608 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
609 && chan
== event
->chan
) {
615 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
621 event
->filter
= filter
;
622 event
->id
= chan
->free_event_id
++;
623 event
->instrumentation
= itype
;
624 event
->evtype
= LTTNG_TYPE_EVENT
;
625 INIT_LIST_HEAD(&event
->bytecode_runtime_head
);
626 INIT_LIST_HEAD(&event
->enablers_ref_head
);
629 case LTTNG_KERNEL_TRACEPOINT
:
630 /* Event will be enabled by enabler sync. */
632 event
->registered
= 0;
633 event
->desc
= lttng_event_get(event_name
);
638 /* Populate lttng_event structure before event registration. */
641 case LTTNG_KERNEL_KPROBE
:
643 * Needs to be explicitly enabled after creation, since
644 * we may want to apply filters.
647 event
->registered
= 1;
649 * Populate lttng_event structure before event
653 ret
= lttng_kprobes_register(event_name
,
654 event_param
->u
.kprobe
.symbol_name
,
655 event_param
->u
.kprobe
.offset
,
656 event_param
->u
.kprobe
.addr
,
662 ret
= try_module_get(event
->desc
->owner
);
665 case LTTNG_KERNEL_KRETPROBE
:
667 struct lttng_event
*event_return
;
669 /* kretprobe defines 2 events */
671 * Needs to be explicitly enabled after creation, since
672 * we may want to apply filters.
675 event
->registered
= 1;
677 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
682 event_return
->chan
= chan
;
683 event_return
->filter
= filter
;
684 event_return
->id
= chan
->free_event_id
++;
685 event_return
->enabled
= 0;
686 event_return
->registered
= 1;
687 event_return
->instrumentation
= itype
;
689 * Populate lttng_event structure before kretprobe registration.
692 ret
= lttng_kretprobes_register(event_name
,
693 event_param
->u
.kretprobe
.symbol_name
,
694 event_param
->u
.kretprobe
.offset
,
695 event_param
->u
.kretprobe
.addr
,
696 event
, event_return
);
698 kmem_cache_free(event_cache
, event_return
);
702 /* Take 2 refs on the module: one per event. */
703 ret
= try_module_get(event
->desc
->owner
);
705 ret
= try_module_get(event
->desc
->owner
);
707 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
709 WARN_ON_ONCE(ret
> 0);
711 kmem_cache_free(event_cache
, event_return
);
712 module_put(event
->desc
->owner
);
713 module_put(event
->desc
->owner
);
714 goto statedump_error
;
716 list_add(&event_return
->list
, &chan
->session
->events
);
719 case LTTNG_KERNEL_NOOP
:
720 case LTTNG_KERNEL_SYSCALL
:
722 * Needs to be explicitly enabled after creation, since
723 * we may want to apply filters.
726 event
->registered
= 0;
727 event
->desc
= event_desc
;
733 case LTTNG_KERNEL_UPROBE
:
735 * Needs to be explicitly enabled after creation, since
736 * we may want to apply filters.
739 event
->registered
= 1;
742 * Populate lttng_event structure before event
747 ret
= lttng_uprobes_register(event_param
->name
,
748 event_param
->u
.uprobe
.fd
,
752 ret
= try_module_get(event
->desc
->owner
);
755 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
761 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
762 WARN_ON_ONCE(ret
> 0);
764 goto statedump_error
;
766 hlist_add_head(&event
->hlist
, head
);
767 list_add(&event
->list
, &chan
->session
->events
);
771 /* If a statedump error occurs, events will not be readable. */
773 kmem_cache_free(event_cache
, event
);
781 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
782 struct lttng_kernel_event
*event_param
,
784 const struct lttng_event_desc
*event_desc
,
785 enum lttng_kernel_instrumentation itype
)
787 struct lttng_event
*event
;
789 mutex_lock(&sessions_mutex
);
790 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
792 mutex_unlock(&sessions_mutex
);
796 /* Only used for tracepoints for now. */
798 void register_event(struct lttng_event
*event
)
800 const struct lttng_event_desc
*desc
;
803 if (event
->registered
)
807 switch (event
->instrumentation
) {
808 case LTTNG_KERNEL_TRACEPOINT
:
809 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
810 desc
->probe_callback
,
813 case LTTNG_KERNEL_SYSCALL
:
814 ret
= lttng_syscall_filter_enable(event
->chan
,
817 case LTTNG_KERNEL_KPROBE
:
818 case LTTNG_KERNEL_UPROBE
:
819 case LTTNG_KERNEL_KRETPROBE
:
820 case LTTNG_KERNEL_NOOP
:
823 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
828 event
->registered
= 1;
832 * Only used internally at session destruction.
834 int _lttng_event_unregister(struct lttng_event
*event
)
836 const struct lttng_event_desc
*desc
;
839 if (!event
->registered
)
843 switch (event
->instrumentation
) {
844 case LTTNG_KERNEL_TRACEPOINT
:
845 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
846 event
->desc
->probe_callback
,
849 case LTTNG_KERNEL_KPROBE
:
850 lttng_kprobes_unregister(event
);
853 case LTTNG_KERNEL_KRETPROBE
:
854 lttng_kretprobes_unregister(event
);
857 case LTTNG_KERNEL_SYSCALL
:
858 ret
= lttng_syscall_filter_disable(event
->chan
,
861 case LTTNG_KERNEL_NOOP
:
864 case LTTNG_KERNEL_UPROBE
:
865 lttng_uprobes_unregister(event
);
868 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
873 event
->registered
= 0;
878 * Only used internally at session destruction.
881 void _lttng_event_destroy(struct lttng_event
*event
)
883 switch (event
->instrumentation
) {
884 case LTTNG_KERNEL_TRACEPOINT
:
885 lttng_event_put(event
->desc
);
887 case LTTNG_KERNEL_KPROBE
:
888 module_put(event
->desc
->owner
);
889 lttng_kprobes_destroy_private(event
);
891 case LTTNG_KERNEL_KRETPROBE
:
892 module_put(event
->desc
->owner
);
893 lttng_kretprobes_destroy_private(event
);
895 case LTTNG_KERNEL_NOOP
:
896 case LTTNG_KERNEL_SYSCALL
:
898 case LTTNG_KERNEL_UPROBE
:
899 module_put(event
->desc
->owner
);
900 lttng_uprobes_destroy_private(event
);
902 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
906 list_del(&event
->list
);
907 lttng_destroy_context(event
->ctx
);
908 kmem_cache_free(event_cache
, event
);
911 int lttng_session_track_pid(struct lttng_session
*session
, int pid
)
917 mutex_lock(&sessions_mutex
);
919 /* track all pids: destroy tracker. */
920 if (session
->pid_tracker
) {
921 struct lttng_pid_tracker
*lpf
;
923 lpf
= session
->pid_tracker
;
924 rcu_assign_pointer(session
->pid_tracker
, NULL
);
926 lttng_pid_tracker_destroy(lpf
);
930 if (!session
->pid_tracker
) {
931 struct lttng_pid_tracker
*lpf
;
933 lpf
= lttng_pid_tracker_create();
938 ret
= lttng_pid_tracker_add(lpf
, pid
);
939 rcu_assign_pointer(session
->pid_tracker
, lpf
);
941 ret
= lttng_pid_tracker_add(session
->pid_tracker
, pid
);
945 mutex_unlock(&sessions_mutex
);
949 int lttng_session_untrack_pid(struct lttng_session
*session
, int pid
)
955 mutex_lock(&sessions_mutex
);
957 /* untrack all pids: replace by empty tracker. */
958 struct lttng_pid_tracker
*old_lpf
= session
->pid_tracker
;
959 struct lttng_pid_tracker
*lpf
;
961 lpf
= lttng_pid_tracker_create();
966 rcu_assign_pointer(session
->pid_tracker
, lpf
);
969 lttng_pid_tracker_destroy(old_lpf
);
972 if (!session
->pid_tracker
) {
976 ret
= lttng_pid_tracker_del(session
->pid_tracker
, pid
);
979 mutex_unlock(&sessions_mutex
);
984 void *pid_list_start(struct seq_file
*m
, loff_t
*pos
)
986 struct lttng_session
*session
= m
->private;
987 struct lttng_pid_tracker
*lpf
;
988 struct lttng_pid_hash_node
*e
;
991 mutex_lock(&sessions_mutex
);
992 lpf
= session
->pid_tracker
;
994 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
995 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
997 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1003 /* PID tracker disabled. */
1004 if (iter
>= *pos
&& iter
== 0) {
1005 return session
; /* empty tracker */
1013 /* Called with sessions_mutex held. */
1015 void *pid_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1017 struct lttng_session
*session
= m
->private;
1018 struct lttng_pid_tracker
*lpf
;
1019 struct lttng_pid_hash_node
*e
;
1023 lpf
= session
->pid_tracker
;
1025 for (i
= 0; i
< LTTNG_PID_TABLE_SIZE
; i
++) {
1026 struct hlist_head
*head
= &lpf
->pid_hash
[i
];
1028 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1029 if (iter
++ >= *ppos
)
1034 /* PID tracker disabled. */
1035 if (iter
>= *ppos
&& iter
== 0)
1036 return session
; /* empty tracker */
1045 void pid_list_stop(struct seq_file
*m
, void *p
)
1047 mutex_unlock(&sessions_mutex
);
1051 int pid_list_show(struct seq_file
*m
, void *p
)
1055 if (p
== m
->private) {
1056 /* Tracker disabled. */
1059 const struct lttng_pid_hash_node
*e
= p
;
1061 pid
= lttng_pid_tracker_get_node_pid(e
);
1063 seq_printf(m
, "process { pid = %d; };\n", pid
);
1068 const struct seq_operations lttng_tracker_pids_list_seq_ops
= {
1069 .start
= pid_list_start
,
1070 .next
= pid_list_next
,
1071 .stop
= pid_list_stop
,
1072 .show
= pid_list_show
,
1076 int lttng_tracker_pids_list_open(struct inode
*inode
, struct file
*file
)
1078 return seq_open(file
, <tng_tracker_pids_list_seq_ops
);
1082 int lttng_tracker_pids_list_release(struct inode
*inode
, struct file
*file
)
1084 struct seq_file
*m
= file
->private_data
;
1085 struct lttng_session
*session
= m
->private;
1088 WARN_ON_ONCE(!session
);
1089 ret
= seq_release(inode
, file
);
1090 if (!ret
&& session
)
1091 fput(session
->file
);
1095 const struct file_operations lttng_tracker_pids_list_fops
= {
1096 .owner
= THIS_MODULE
,
1097 .open
= lttng_tracker_pids_list_open
,
1099 .llseek
= seq_lseek
,
1100 .release
= lttng_tracker_pids_list_release
,
1103 int lttng_session_list_tracker_pids(struct lttng_session
*session
)
1105 struct file
*tracker_pids_list_file
;
1109 file_fd
= lttng_get_unused_fd();
1115 tracker_pids_list_file
= anon_inode_getfile("[lttng_tracker_pids_list]",
1116 <tng_tracker_pids_list_fops
,
1118 if (IS_ERR(tracker_pids_list_file
)) {
1119 ret
= PTR_ERR(tracker_pids_list_file
);
1122 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1124 goto refcount_error
;
1126 ret
= lttng_tracker_pids_list_fops
.open(NULL
, tracker_pids_list_file
);
1129 m
= tracker_pids_list_file
->private_data
;
1130 m
->private = session
;
1131 fd_install(file_fd
, tracker_pids_list_file
);
1136 atomic_long_dec(&session
->file
->f_count
);
1138 fput(tracker_pids_list_file
);
1140 put_unused_fd(file_fd
);
1146 * Enabler management.
1149 int lttng_match_enabler_star_glob(const char *desc_name
,
1150 const char *pattern
)
1152 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1153 desc_name
, LTTNG_SIZE_MAX
))
1159 int lttng_match_enabler_name(const char *desc_name
,
1162 if (strcmp(desc_name
, name
))
1168 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1169 struct lttng_enabler
*enabler
)
1171 const char *desc_name
, *enabler_name
;
1173 enabler_name
= enabler
->event_param
.name
;
1174 switch (enabler
->event_param
.instrumentation
) {
1175 case LTTNG_KERNEL_TRACEPOINT
:
1176 desc_name
= desc
->name
;
1178 case LTTNG_KERNEL_SYSCALL
:
1179 desc_name
= desc
->name
;
1180 if (!strncmp(desc_name
, "compat_", strlen("compat_")))
1181 desc_name
+= strlen("compat_");
1182 if (!strncmp(desc_name
, "syscall_exit_",
1183 strlen("syscall_exit_"))) {
1184 desc_name
+= strlen("syscall_exit_");
1185 } else if (!strncmp(desc_name
, "syscall_entry_",
1186 strlen("syscall_entry_"))) {
1187 desc_name
+= strlen("syscall_entry_");
1197 switch (enabler
->type
) {
1198 case LTTNG_ENABLER_STAR_GLOB
:
1199 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1200 case LTTNG_ENABLER_NAME
:
1201 return lttng_match_enabler_name(desc_name
, enabler_name
);
1208 int lttng_event_match_enabler(struct lttng_event
*event
,
1209 struct lttng_enabler
*enabler
)
1211 if (enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1213 if (lttng_desc_match_enabler(event
->desc
, enabler
)
1214 && event
->chan
== enabler
->chan
)
1221 struct lttng_enabler_ref
*lttng_event_enabler_ref(struct lttng_event
*event
,
1222 struct lttng_enabler
*enabler
)
1224 struct lttng_enabler_ref
*enabler_ref
;
1226 list_for_each_entry(enabler_ref
,
1227 &event
->enablers_ref_head
, node
) {
1228 if (enabler_ref
->ref
== enabler
)
1235 void lttng_create_tracepoint_if_missing(struct lttng_enabler
*enabler
)
1237 struct lttng_session
*session
= enabler
->chan
->session
;
1238 struct lttng_probe_desc
*probe_desc
;
1239 const struct lttng_event_desc
*desc
;
1241 struct list_head
*probe_list
;
1243 probe_list
= lttng_get_probe_list_head();
1245 * For each probe event, if we find that a probe event matches
1246 * our enabler, create an associated lttng_event if not
1249 list_for_each_entry(probe_desc
, probe_list
, head
) {
1250 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1252 struct hlist_head
*head
;
1253 const char *event_name
;
1256 struct lttng_event
*event
;
1258 desc
= probe_desc
->event_desc
[i
];
1259 if (!lttng_desc_match_enabler(desc
, enabler
))
1261 event_name
= desc
->name
;
1262 name_len
= strlen(event_name
);
1265 * Check if already created.
1267 hash
= jhash(event_name
, name_len
, 0);
1268 head
= &session
->events_ht
.table
[hash
& (LTTNG_EVENT_HT_SIZE
- 1)];
1269 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1270 if (event
->desc
== desc
1271 && event
->chan
== enabler
->chan
)
1278 * We need to create an event for this
1281 event
= _lttng_event_create(enabler
->chan
,
1283 LTTNG_KERNEL_TRACEPOINT
);
1285 printk(KERN_INFO
"Unable to create event %s\n",
1286 probe_desc
->event_desc
[i
]->name
);
1293 void lttng_create_syscall_if_missing(struct lttng_enabler
*enabler
)
1297 ret
= lttng_syscalls_register(enabler
->chan
, NULL
);
1302 * Create struct lttng_event if it is missing and present in the list of
1303 * tracepoint probes.
1304 * Should be called with sessions mutex held.
1307 void lttng_create_event_if_missing(struct lttng_enabler
*enabler
)
1309 switch (enabler
->event_param
.instrumentation
) {
1310 case LTTNG_KERNEL_TRACEPOINT
:
1311 lttng_create_tracepoint_if_missing(enabler
);
1313 case LTTNG_KERNEL_SYSCALL
:
1314 lttng_create_syscall_if_missing(enabler
);
1323 * Create events associated with an enabler (if not already present),
1324 * and add backward reference from the event to the enabler.
1325 * Should be called with sessions mutex held.
1328 int lttng_enabler_ref_events(struct lttng_enabler
*enabler
)
1330 struct lttng_session
*session
= enabler
->chan
->session
;
1331 struct lttng_event
*event
;
1333 /* First ensure that probe events are created for this enabler. */
1334 lttng_create_event_if_missing(enabler
);
1336 /* For each event matching enabler in session event list. */
1337 list_for_each_entry(event
, &session
->events
, list
) {
1338 struct lttng_enabler_ref
*enabler_ref
;
1340 if (!lttng_event_match_enabler(event
, enabler
))
1342 enabler_ref
= lttng_event_enabler_ref(event
, enabler
);
1345 * If no backward ref, create it.
1346 * Add backward ref from event to enabler.
1348 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
1351 enabler_ref
->ref
= enabler
;
1352 list_add(&enabler_ref
->node
,
1353 &event
->enablers_ref_head
);
1357 * Link filter bytecodes if not linked yet.
1359 lttng_enabler_event_link_bytecode(event
, enabler
);
1361 /* TODO: merge event context. */
1367 * Called at module load: connect the probe on all enablers matching
1369 * Called with sessions lock held.
1371 int lttng_fix_pending_events(void)
1373 struct lttng_session
*session
;
1375 list_for_each_entry(session
, &sessions
, list
)
1376 lttng_session_lazy_sync_enablers(session
);
1380 struct lttng_enabler
*lttng_enabler_create(enum lttng_enabler_type type
,
1381 struct lttng_kernel_event
*event_param
,
1382 struct lttng_channel
*chan
)
1384 struct lttng_enabler
*enabler
;
1386 enabler
= kzalloc(sizeof(*enabler
), GFP_KERNEL
);
1389 enabler
->type
= type
;
1390 INIT_LIST_HEAD(&enabler
->filter_bytecode_head
);
1391 memcpy(&enabler
->event_param
, event_param
,
1392 sizeof(enabler
->event_param
));
1393 enabler
->chan
= chan
;
1395 enabler
->enabled
= 0;
1396 enabler
->evtype
= LTTNG_TYPE_ENABLER
;
1397 mutex_lock(&sessions_mutex
);
1398 list_add(&enabler
->node
, &enabler
->chan
->session
->enablers_head
);
1399 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1400 mutex_unlock(&sessions_mutex
);
1404 int lttng_enabler_enable(struct lttng_enabler
*enabler
)
1406 mutex_lock(&sessions_mutex
);
1407 enabler
->enabled
= 1;
1408 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1409 mutex_unlock(&sessions_mutex
);
1413 int lttng_enabler_disable(struct lttng_enabler
*enabler
)
1415 mutex_lock(&sessions_mutex
);
1416 enabler
->enabled
= 0;
1417 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1418 mutex_unlock(&sessions_mutex
);
1422 int lttng_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
1423 struct lttng_kernel_filter_bytecode __user
*bytecode
)
1425 struct lttng_filter_bytecode_node
*bytecode_node
;
1426 uint32_t bytecode_len
;
1429 ret
= get_user(bytecode_len
, &bytecode
->len
);
1432 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
1436 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
1437 sizeof(*bytecode
) + bytecode_len
);
1440 bytecode_node
->enabler
= enabler
;
1441 /* Enforce length based on allocated size */
1442 bytecode_node
->bc
.len
= bytecode_len
;
1443 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
1444 lttng_session_lazy_sync_enablers(enabler
->chan
->session
);
1448 kfree(bytecode_node
);
1452 int lttng_event_add_callsite(struct lttng_event
*event
,
1453 struct lttng_kernel_event_callsite __user
*callsite
)
1456 switch (event
->instrumentation
) {
1457 case LTTNG_KERNEL_UPROBE
:
1458 return lttng_uprobes_add_callsite(event
, callsite
);
1464 int lttng_enabler_attach_context(struct lttng_enabler
*enabler
,
1465 struct lttng_kernel_context
*context_param
)
1471 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
1473 struct lttng_filter_bytecode_node
*filter_node
, *tmp_filter_node
;
1475 /* Destroy filter bytecode */
1476 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
1477 &enabler
->filter_bytecode_head
, node
) {
1481 /* Destroy contexts */
1482 lttng_destroy_context(enabler
->ctx
);
1484 list_del(&enabler
->node
);
1489 * lttng_session_sync_enablers should be called just before starting a
1491 * Should be called with sessions mutex held.
1494 void lttng_session_sync_enablers(struct lttng_session
*session
)
1496 struct lttng_enabler
*enabler
;
1497 struct lttng_event
*event
;
1499 list_for_each_entry(enabler
, &session
->enablers_head
, node
)
1500 lttng_enabler_ref_events(enabler
);
1502 * For each event, if at least one of its enablers is enabled,
1503 * and its channel and session transient states are enabled, we
1504 * enable the event, else we disable it.
1506 list_for_each_entry(event
, &session
->events
, list
) {
1507 struct lttng_enabler_ref
*enabler_ref
;
1508 struct lttng_bytecode_runtime
*runtime
;
1509 int enabled
= 0, has_enablers_without_bytecode
= 0;
1511 switch (event
->instrumentation
) {
1512 case LTTNG_KERNEL_TRACEPOINT
:
1513 case LTTNG_KERNEL_SYSCALL
:
1515 list_for_each_entry(enabler_ref
,
1516 &event
->enablers_ref_head
, node
) {
1517 if (enabler_ref
->ref
->enabled
) {
1524 /* Not handled with lazy sync. */
1528 * Enabled state is based on union of enablers, with
1529 * intesection of session and channel transient enable
1532 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
1534 WRITE_ONCE(event
->enabled
, enabled
);
1536 * Sync tracepoint registration with event enabled
1540 register_event(event
);
1542 _lttng_event_unregister(event
);
1545 /* Check if has enablers without bytecode enabled */
1546 list_for_each_entry(enabler_ref
,
1547 &event
->enablers_ref_head
, node
) {
1548 if (enabler_ref
->ref
->enabled
1549 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
1550 has_enablers_without_bytecode
= 1;
1554 event
->has_enablers_without_bytecode
=
1555 has_enablers_without_bytecode
;
1557 /* Enable filters */
1558 list_for_each_entry(runtime
,
1559 &event
->bytecode_runtime_head
, node
)
1560 lttng_filter_sync_state(runtime
);
1565 * Apply enablers to session events, adding events to session if need
1566 * be. It is required after each modification applied to an active
1567 * session, and right before session "start".
1568 * "lazy" sync means we only sync if required.
1569 * Should be called with sessions mutex held.
1572 void lttng_session_lazy_sync_enablers(struct lttng_session
*session
)
1574 /* We can skip if session is not active */
1575 if (!session
->active
)
1577 lttng_session_sync_enablers(session
);
1581 * Serialize at most one packet worth of metadata into a metadata
1583 * We grab the metadata cache mutex to get exclusive access to our metadata
1584 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1585 * allows us to do racy operations such as looking for remaining space left in
1586 * packet and write, since mutual exclusion protects us from concurrent writes.
1587 * Mutual exclusion on the metadata cache allow us to read the cache content
1588 * without racing against reallocation of the cache by updates.
1589 * Returns the number of bytes written in the channel, 0 if no data
1590 * was written and a negative value on error.
1592 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
1593 struct channel
*chan
, bool *coherent
)
1595 struct lib_ring_buffer_ctx ctx
;
1597 size_t len
, reserve_len
;
1600 * Ensure we support mutiple get_next / put sequences followed by
1601 * put_next. The metadata cache lock protects reading the metadata
1602 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1603 * "flush" operations on the buffer invoked by different processes.
1604 * Moreover, since the metadata cache memory can be reallocated, we
1605 * need to have exclusive access against updates even though we only
1608 mutex_lock(&stream
->metadata_cache
->lock
);
1609 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
1610 if (stream
->metadata_in
!= stream
->metadata_out
)
1613 /* Metadata regenerated, change the version. */
1614 if (stream
->metadata_cache
->version
!= stream
->version
)
1615 stream
->version
= stream
->metadata_cache
->version
;
1617 len
= stream
->metadata_cache
->metadata_written
-
1618 stream
->metadata_in
;
1621 reserve_len
= min_t(size_t,
1622 stream
->transport
->ops
.packet_avail_size(chan
),
1624 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
1627 * If reservation failed, return an error to the caller.
1629 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
1631 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
1632 stream
->coherent
= false;
1635 stream
->transport
->ops
.event_write(&ctx
,
1636 stream
->metadata_cache
->data
+ stream
->metadata_in
,
1638 stream
->transport
->ops
.event_commit(&ctx
);
1639 stream
->metadata_in
+= reserve_len
;
1640 if (reserve_len
< len
)
1641 stream
->coherent
= false;
1643 stream
->coherent
= true;
1648 *coherent
= stream
->coherent
;
1649 mutex_unlock(&stream
->metadata_cache
->lock
);
1654 void lttng_metadata_begin(struct lttng_session
*session
)
1656 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
1657 mutex_lock(&session
->metadata_cache
->lock
);
1661 void lttng_metadata_end(struct lttng_session
*session
)
1663 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1664 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
1665 struct lttng_metadata_stream
*stream
;
1667 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
1668 wake_up_interruptible(&stream
->read_wait
);
1669 mutex_unlock(&session
->metadata_cache
->lock
);
1674 * Write the metadata to the metadata cache.
1675 * Must be called with sessions_mutex held.
1676 * The metadata cache lock protects us from concurrent read access from
1677 * thread outputting metadata content to ring buffer.
1678 * The content of the printf is printed as a single atomic metadata
1681 int lttng_metadata_printf(struct lttng_session
*session
,
1682 const char *fmt
, ...)
1688 WARN_ON_ONCE(!READ_ONCE(session
->active
));
1691 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
1697 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
1698 if (session
->metadata_cache
->metadata_written
+ len
>
1699 session
->metadata_cache
->cache_alloc
) {
1700 char *tmp_cache_realloc
;
1701 unsigned int tmp_cache_alloc_size
;
1703 tmp_cache_alloc_size
= max_t(unsigned int,
1704 session
->metadata_cache
->cache_alloc
+ len
,
1705 session
->metadata_cache
->cache_alloc
<< 1);
1706 tmp_cache_realloc
= lttng_vzalloc(tmp_cache_alloc_size
);
1707 if (!tmp_cache_realloc
)
1709 if (session
->metadata_cache
->data
) {
1710 memcpy(tmp_cache_realloc
,
1711 session
->metadata_cache
->data
,
1712 session
->metadata_cache
->cache_alloc
);
1713 vfree(session
->metadata_cache
->data
);
1716 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
1717 session
->metadata_cache
->data
= tmp_cache_realloc
;
1719 memcpy(session
->metadata_cache
->data
+
1720 session
->metadata_cache
->metadata_written
,
1722 session
->metadata_cache
->metadata_written
+= len
;
1733 int print_tabs(struct lttng_session
*session
, size_t nesting
)
1737 for (i
= 0; i
< nesting
; i
++) {
1740 ret
= lttng_metadata_printf(session
, " ");
1749 * Must be called with sessions_mutex held.
1752 int _lttng_struct_type_statedump(struct lttng_session
*session
,
1753 const struct lttng_type
*type
,
1757 uint32_t i
, nr_fields
;
1759 ret
= print_tabs(session
, nesting
);
1762 ret
= lttng_metadata_printf(session
,
1766 nr_fields
= type
->u
._struct
.nr_fields
;
1767 for (i
= 0; i
< nr_fields
; i
++) {
1768 const struct lttng_event_field
*iter_field
;
1770 iter_field
= &type
->u
._struct
.fields
[i
];
1771 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1775 ret
= print_tabs(session
, nesting
);
1778 ret
= lttng_metadata_printf(session
,
1784 * Must be called with sessions_mutex held.
1787 int _lttng_struct_statedump(struct lttng_session
*session
,
1788 const struct lttng_event_field
*field
,
1793 ret
= _lttng_struct_type_statedump(session
,
1794 &field
->type
, nesting
);
1797 ret
= lttng_metadata_printf(session
,
1804 * Must be called with sessions_mutex held.
1807 int _lttng_variant_type_statedump(struct lttng_session
*session
,
1808 const struct lttng_type
*type
,
1812 uint32_t i
, nr_choices
;
1814 ret
= print_tabs(session
, nesting
);
1817 ret
= lttng_metadata_printf(session
,
1818 "variant <_%s> {\n",
1819 type
->u
.variant
.tag_name
);
1822 nr_choices
= type
->u
.variant
.nr_choices
;
1823 for (i
= 0; i
< nr_choices
; i
++) {
1824 const struct lttng_event_field
*iter_field
;
1826 iter_field
= &type
->u
.variant
.choices
[i
];
1827 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
1831 ret
= print_tabs(session
, nesting
);
1834 ret
= lttng_metadata_printf(session
,
1840 * Must be called with sessions_mutex held.
1843 int _lttng_variant_statedump(struct lttng_session
*session
,
1844 const struct lttng_event_field
*field
,
1849 ret
= _lttng_variant_type_statedump(session
,
1850 &field
->type
, nesting
);
1853 ret
= lttng_metadata_printf(session
,
1860 * Must be called with sessions_mutex held.
1863 int _lttng_array_compound_statedump(struct lttng_session
*session
,
1864 const struct lttng_event_field
*field
,
1868 const struct lttng_type
*elem_type
;
1870 /* Only array of structures and variants are currently supported. */
1871 elem_type
= field
->type
.u
.array_compound
.elem_type
;
1872 switch (elem_type
->atype
) {
1874 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1879 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1886 ret
= lttng_metadata_printf(session
,
1889 field
->type
.u
.array_compound
.length
);
1894 * Must be called with sessions_mutex held.
1897 int _lttng_sequence_compound_statedump(struct lttng_session
*session
,
1898 const struct lttng_event_field
*field
,
1902 const char *length_name
;
1903 const struct lttng_type
*elem_type
;
1905 length_name
= field
->type
.u
.sequence_compound
.length_name
;
1907 /* Only array of structures and variants are currently supported. */
1908 elem_type
= field
->type
.u
.sequence_compound
.elem_type
;
1909 switch (elem_type
->atype
) {
1911 ret
= _lttng_struct_type_statedump(session
, elem_type
, nesting
);
1916 ret
= _lttng_variant_type_statedump(session
, elem_type
, nesting
);
1923 ret
= lttng_metadata_printf(session
,
1931 * Must be called with sessions_mutex held.
1934 int _lttng_enum_statedump(struct lttng_session
*session
,
1935 const struct lttng_event_field
*field
,
1938 const struct lttng_enum_desc
*enum_desc
;
1939 const struct lttng_integer_type
*container_type
;
1941 unsigned int i
, nr_entries
;
1943 enum_desc
= field
->type
.u
.basic
.enumeration
.desc
;
1944 container_type
= &field
->type
.u
.basic
.enumeration
.container_type
;
1945 nr_entries
= enum_desc
->nr_entries
;
1947 ret
= print_tabs(session
, nesting
);
1950 ret
= lttng_metadata_printf(session
,
1951 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
1952 container_type
->size
,
1953 container_type
->alignment
,
1954 container_type
->signedness
,
1955 (container_type
->encoding
== lttng_encode_none
)
1957 : (container_type
->encoding
== lttng_encode_UTF8
)
1960 container_type
->base
,
1961 #if __BYTE_ORDER == __BIG_ENDIAN
1962 container_type
->reverse_byte_order
? " byte_order = le;" : ""
1964 container_type
->reverse_byte_order
? " byte_order = be;" : ""
1969 /* Dump all entries */
1970 for (i
= 0; i
< nr_entries
; i
++) {
1971 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
1974 ret
= print_tabs(session
, nesting
+ 1);
1977 ret
= lttng_metadata_printf(session
,
1981 len
= strlen(entry
->string
);
1982 /* Escape the character '"' */
1983 for (j
= 0; j
< len
; j
++) {
1984 char c
= entry
->string
[j
];
1988 ret
= lttng_metadata_printf(session
,
1992 ret
= lttng_metadata_printf(session
,
1996 ret
= lttng_metadata_printf(session
,
2003 ret
= lttng_metadata_printf(session
, "\"");
2007 if (entry
->options
.is_auto
) {
2008 ret
= lttng_metadata_printf(session
, ",\n");
2012 ret
= lttng_metadata_printf(session
,
2016 if (entry
->start
.signedness
)
2017 ret
= lttng_metadata_printf(session
,
2018 "%lld", (long long) entry
->start
.value
);
2020 ret
= lttng_metadata_printf(session
,
2021 "%llu", entry
->start
.value
);
2024 if (entry
->start
.signedness
== entry
->end
.signedness
&&
2026 == entry
->end
.value
) {
2027 ret
= lttng_metadata_printf(session
,
2030 if (entry
->end
.signedness
) {
2031 ret
= lttng_metadata_printf(session
,
2033 (long long) entry
->end
.value
);
2035 ret
= lttng_metadata_printf(session
,
2044 ret
= print_tabs(session
, nesting
);
2047 ret
= lttng_metadata_printf(session
, "} _%s;\n",
2054 * Must be called with sessions_mutex held.
2057 int _lttng_field_statedump(struct lttng_session
*session
,
2058 const struct lttng_event_field
*field
,
2063 switch (field
->type
.atype
) {
2065 ret
= print_tabs(session
, nesting
);
2068 ret
= lttng_metadata_printf(session
,
2069 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2070 field
->type
.u
.basic
.integer
.size
,
2071 field
->type
.u
.basic
.integer
.alignment
,
2072 field
->type
.u
.basic
.integer
.signedness
,
2073 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
2075 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2078 field
->type
.u
.basic
.integer
.base
,
2079 #if __BYTE_ORDER == __BIG_ENDIAN
2080 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2082 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2087 ret
= _lttng_enum_statedump(session
, field
, nesting
);
2090 case atype_array_bitfield
:
2092 const struct lttng_basic_type
*elem_type
;
2094 elem_type
= &field
->type
.u
.array
.elem_type
;
2095 if (field
->type
.u
.array
.elem_alignment
) {
2096 ret
= print_tabs(session
, nesting
);
2099 ret
= lttng_metadata_printf(session
,
2100 "struct { } align(%u) _%s_padding;\n",
2101 field
->type
.u
.array
.elem_alignment
* CHAR_BIT
,
2106 ret
= print_tabs(session
, nesting
);
2109 ret
= lttng_metadata_printf(session
,
2110 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2111 elem_type
->u
.basic
.integer
.size
,
2112 elem_type
->u
.basic
.integer
.alignment
,
2113 elem_type
->u
.basic
.integer
.signedness
,
2114 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2116 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2119 elem_type
->u
.basic
.integer
.base
,
2120 #if __BYTE_ORDER == __BIG_ENDIAN
2121 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2123 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2125 field
->name
, field
->type
.u
.array
.length
);
2128 case atype_sequence
:
2129 case atype_sequence_bitfield
:
2131 const struct lttng_basic_type
*elem_type
;
2132 const struct lttng_basic_type
*length_type
;
2134 elem_type
= &field
->type
.u
.sequence
.elem_type
;
2135 length_type
= &field
->type
.u
.sequence
.length_type
;
2136 ret
= print_tabs(session
, nesting
);
2139 ret
= lttng_metadata_printf(session
,
2140 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2141 length_type
->u
.basic
.integer
.size
,
2142 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
2143 length_type
->u
.basic
.integer
.signedness
,
2144 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2146 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2149 length_type
->u
.basic
.integer
.base
,
2150 #if __BYTE_ORDER == __BIG_ENDIAN
2151 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2153 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2159 if (field
->type
.u
.sequence
.elem_alignment
) {
2160 ret
= print_tabs(session
, nesting
);
2163 ret
= lttng_metadata_printf(session
,
2164 "struct { } align(%u) _%s_padding;\n",
2165 field
->type
.u
.sequence
.elem_alignment
* CHAR_BIT
,
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[ __%s_length ];\n",
2175 elem_type
->u
.basic
.integer
.size
,
2176 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
2177 elem_type
->u
.basic
.integer
.signedness
,
2178 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
2180 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
2183 elem_type
->u
.basic
.integer
.base
,
2184 #if __BYTE_ORDER == __BIG_ENDIAN
2185 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
2187 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
2195 /* Default encoding is UTF8 */
2196 ret
= print_tabs(session
, nesting
);
2199 ret
= lttng_metadata_printf(session
,
2201 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
2202 " { encoding = ASCII; }" : "",
2206 ret
= _lttng_struct_statedump(session
, field
, nesting
);
2208 case atype_array_compound
:
2209 ret
= _lttng_array_compound_statedump(session
, field
, nesting
);
2211 case atype_sequence_compound
:
2212 ret
= _lttng_sequence_compound_statedump(session
, field
, nesting
);
2215 ret
= _lttng_variant_statedump(session
, field
, nesting
);
2226 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
2227 struct lttng_ctx
*ctx
)
2234 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
2235 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
2237 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
2245 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
2246 struct lttng_event
*event
)
2248 const struct lttng_event_desc
*desc
= event
->desc
;
2252 for (i
= 0; i
< desc
->nr_fields
; i
++) {
2253 const struct lttng_event_field
*field
= &desc
->fields
[i
];
2255 ret
= _lttng_field_statedump(session
, field
, 2);
2263 * Must be called with sessions_mutex held.
2264 * The entire event metadata is printed as a single atomic metadata
2268 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
2269 struct lttng_channel
*chan
,
2270 struct lttng_event
*event
)
2274 if (event
->metadata_dumped
|| !READ_ONCE(session
->active
))
2276 if (chan
->channel_type
== METADATA_CHANNEL
)
2279 lttng_metadata_begin(session
);
2281 ret
= lttng_metadata_printf(session
,
2285 " stream_id = %u;\n",
2293 ret
= lttng_metadata_printf(session
,
2294 " context := struct {\n");
2298 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
2302 ret
= lttng_metadata_printf(session
,
2308 ret
= lttng_metadata_printf(session
,
2309 " fields := struct {\n"
2314 ret
= _lttng_fields_metadata_statedump(session
, event
);
2319 * LTTng space reservation can only reserve multiples of the
2322 ret
= lttng_metadata_printf(session
,
2328 event
->metadata_dumped
= 1;
2330 lttng_metadata_end(session
);
2336 * Must be called with sessions_mutex held.
2337 * The entire channel metadata is printed as a single atomic metadata
2341 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
2342 struct lttng_channel
*chan
)
2346 if (chan
->metadata_dumped
|| !READ_ONCE(session
->active
))
2349 if (chan
->channel_type
== METADATA_CHANNEL
)
2352 lttng_metadata_begin(session
);
2354 WARN_ON_ONCE(!chan
->header_type
);
2355 ret
= lttng_metadata_printf(session
,
2358 " event.header := %s;\n"
2359 " packet.context := struct packet_context;\n",
2361 chan
->header_type
== 1 ? "struct event_header_compact" :
2362 "struct event_header_large");
2367 ret
= lttng_metadata_printf(session
,
2368 " event.context := struct {\n");
2372 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
2376 ret
= lttng_metadata_printf(session
,
2382 ret
= lttng_metadata_printf(session
,
2385 chan
->metadata_dumped
= 1;
2387 lttng_metadata_end(session
);
2392 * Must be called with sessions_mutex held.
2395 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
2397 return lttng_metadata_printf(session
,
2398 "struct packet_context {\n"
2399 " uint64_clock_monotonic_t timestamp_begin;\n"
2400 " uint64_clock_monotonic_t timestamp_end;\n"
2401 " uint64_t content_size;\n"
2402 " uint64_t packet_size;\n"
2403 " uint64_t packet_seq_num;\n"
2404 " unsigned long events_discarded;\n"
2405 " uint32_t cpu_id;\n"
2412 * id: range: 0 - 30.
2413 * id 31 is reserved to indicate an extended header.
2416 * id: range: 0 - 65534.
2417 * id 65535 is reserved to indicate an extended header.
2419 * Must be called with sessions_mutex held.
2422 int _lttng_event_header_declare(struct lttng_session
*session
)
2424 return lttng_metadata_printf(session
,
2425 "struct event_header_compact {\n"
2426 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2429 " uint27_clock_monotonic_t timestamp;\n"
2433 " uint64_clock_monotonic_t timestamp;\n"
2438 "struct event_header_large {\n"
2439 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2442 " uint32_clock_monotonic_t timestamp;\n"
2446 " uint64_clock_monotonic_t timestamp;\n"
2450 lttng_alignof(uint32_t) * CHAR_BIT
,
2451 lttng_alignof(uint16_t) * CHAR_BIT
2456 * Approximation of NTP time of day to clock monotonic correlation,
2457 * taken at start of trace.
2458 * Yes, this is only an approximation. Yes, we can (and will) do better
2459 * in future versions.
2460 * This function may return a negative offset. It may happen if the
2461 * system sets the REALTIME clock to 0 after boot.
2463 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2467 int64_t measure_clock_offset(void)
2469 uint64_t monotonic_avg
, monotonic
[2], realtime
;
2470 uint64_t tcf
= trace_clock_freq();
2472 unsigned long flags
;
2473 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2474 struct timespec64 rts
= { 0, 0 };
2476 struct timespec rts
= { 0, 0 };
2479 /* Disable interrupts to increase correlation precision. */
2480 local_irq_save(flags
);
2481 monotonic
[0] = trace_clock_read64();
2482 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2483 ktime_get_real_ts64(&rts
);
2485 getnstimeofday(&rts
);
2487 monotonic
[1] = trace_clock_read64();
2488 local_irq_restore(flags
);
2490 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
2491 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
2492 if (tcf
== NSEC_PER_SEC
) {
2493 realtime
+= rts
.tv_nsec
;
2495 uint64_t n
= rts
.tv_nsec
* tcf
;
2497 do_div(n
, NSEC_PER_SEC
);
2500 offset
= (int64_t) realtime
- monotonic_avg
;
2505 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
2513 while (cur
!= '\0') {
2516 ret
= lttng_metadata_printf(session
, "%s", "\\n");
2520 ret
= lttng_metadata_printf(session
, "%c", '\\');
2523 /* We still print the current char */
2526 ret
= lttng_metadata_printf(session
, "%c", cur
);
2540 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
2541 const char *field_value
)
2545 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
2549 ret
= print_escaped_ctf_string(session
, field_value
);
2553 ret
= lttng_metadata_printf(session
, "\";\n");
2560 * Output metadata into this session's metadata buffers.
2561 * Must be called with sessions_mutex held.
2564 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
2566 unsigned char *uuid_c
= session
->uuid
.b
;
2567 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
2568 struct lttng_channel
*chan
;
2569 struct lttng_event
*event
;
2572 if (!READ_ONCE(session
->active
))
2575 lttng_metadata_begin(session
);
2577 if (session
->metadata_dumped
)
2580 snprintf(uuid_s
, sizeof(uuid_s
),
2581 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2582 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
2583 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
2584 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
2585 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
2587 ret
= lttng_metadata_printf(session
,
2588 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2589 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2590 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2591 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2592 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2593 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2594 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2600 " byte_order = %s;\n"
2601 " packet.header := struct {\n"
2602 " uint32_t magic;\n"
2603 " uint8_t uuid[16];\n"
2604 " uint32_t stream_id;\n"
2605 " uint64_t stream_instance_id;\n"
2608 lttng_alignof(uint8_t) * CHAR_BIT
,
2609 lttng_alignof(uint16_t) * CHAR_BIT
,
2610 lttng_alignof(uint32_t) * CHAR_BIT
,
2611 lttng_alignof(uint64_t) * CHAR_BIT
,
2612 sizeof(unsigned long) * CHAR_BIT
,
2613 lttng_alignof(unsigned long) * CHAR_BIT
,
2617 #if __BYTE_ORDER == __BIG_ENDIAN
2626 ret
= lttng_metadata_printf(session
,
2628 " hostname = \"%s\";\n"
2629 " domain = \"kernel\";\n"
2630 " sysname = \"%s\";\n"
2631 " kernel_release = \"%s\";\n"
2632 " kernel_version = \"%s\";\n"
2633 " tracer_name = \"lttng-modules\";\n"
2634 " tracer_major = %d;\n"
2635 " tracer_minor = %d;\n"
2636 " tracer_patchlevel = %d;\n"
2637 " trace_buffering_scheme = \"global\";\n",
2638 current
->nsproxy
->uts_ns
->name
.nodename
,
2642 LTTNG_MODULES_MAJOR_VERSION
,
2643 LTTNG_MODULES_MINOR_VERSION
,
2644 LTTNG_MODULES_PATCHLEVEL_VERSION
2649 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
2652 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
2653 session
->creation_time
);
2658 ret
= lttng_metadata_printf(session
, "};\n\n");
2662 ret
= lttng_metadata_printf(session
,
2664 " name = \"%s\";\n",
2670 if (!trace_clock_uuid(clock_uuid_s
)) {
2671 ret
= lttng_metadata_printf(session
,
2672 " uuid = \"%s\";\n",
2679 ret
= lttng_metadata_printf(session
,
2680 " description = \"%s\";\n"
2681 " freq = %llu; /* Frequency, in Hz */\n"
2682 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2685 trace_clock_description(),
2686 (unsigned long long) trace_clock_freq(),
2687 (long long) measure_clock_offset()
2692 ret
= lttng_metadata_printf(session
,
2693 "typealias integer {\n"
2694 " size = 27; align = 1; signed = false;\n"
2695 " map = clock.%s.value;\n"
2696 "} := uint27_clock_monotonic_t;\n"
2698 "typealias integer {\n"
2699 " size = 32; align = %u; signed = false;\n"
2700 " map = clock.%s.value;\n"
2701 "} := uint32_clock_monotonic_t;\n"
2703 "typealias integer {\n"
2704 " size = 64; align = %u; signed = false;\n"
2705 " map = clock.%s.value;\n"
2706 "} := uint64_clock_monotonic_t;\n\n",
2708 lttng_alignof(uint32_t) * CHAR_BIT
,
2710 lttng_alignof(uint64_t) * CHAR_BIT
,
2716 ret
= _lttng_stream_packet_context_declare(session
);
2720 ret
= _lttng_event_header_declare(session
);
2725 list_for_each_entry(chan
, &session
->chan
, list
) {
2726 ret
= _lttng_channel_metadata_statedump(session
, chan
);
2731 list_for_each_entry(event
, &session
->events
, list
) {
2732 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
2736 session
->metadata_dumped
= 1;
2738 lttng_metadata_end(session
);
2743 * lttng_transport_register - LTT transport registration
2744 * @transport: transport structure
2746 * Registers a transport which can be used as output to extract the data out of
2747 * LTTng. The module calling this registration function must ensure that no
2748 * trap-inducing code will be executed by the transport functions. E.g.
2749 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2750 * is made visible to the transport function. This registration acts as a
2751 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2752 * after its registration must it synchronize the TLBs.
2754 void lttng_transport_register(struct lttng_transport
*transport
)
2757 * Make sure no page fault can be triggered by the module about to be
2758 * registered. We deal with this here so we don't have to call
2759 * vmalloc_sync_mappings() in each module's init.
2761 wrapper_vmalloc_sync_mappings();
2763 mutex_lock(&sessions_mutex
);
2764 list_add_tail(&transport
->node
, <tng_transport_list
);
2765 mutex_unlock(&sessions_mutex
);
2767 EXPORT_SYMBOL_GPL(lttng_transport_register
);
2770 * lttng_transport_unregister - LTT transport unregistration
2771 * @transport: transport structure
2773 void lttng_transport_unregister(struct lttng_transport
*transport
)
2775 mutex_lock(&sessions_mutex
);
2776 list_del(&transport
->node
);
2777 mutex_unlock(&sessions_mutex
);
2779 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
2781 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2783 enum cpuhp_state lttng_hp_prepare
;
2784 enum cpuhp_state lttng_hp_online
;
2786 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
2788 struct lttng_cpuhp_node
*lttng_node
;
2790 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2791 switch (lttng_node
->component
) {
2792 case LTTNG_RING_BUFFER_FRONTEND
:
2794 case LTTNG_RING_BUFFER_BACKEND
:
2795 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
2796 case LTTNG_RING_BUFFER_ITER
:
2798 case LTTNG_CONTEXT_PERF_COUNTERS
:
2805 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
2807 struct lttng_cpuhp_node
*lttng_node
;
2809 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2810 switch (lttng_node
->component
) {
2811 case LTTNG_RING_BUFFER_FRONTEND
:
2812 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
2813 case LTTNG_RING_BUFFER_BACKEND
:
2815 case LTTNG_RING_BUFFER_ITER
:
2817 case LTTNG_CONTEXT_PERF_COUNTERS
:
2818 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
2824 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
2826 struct lttng_cpuhp_node
*lttng_node
;
2828 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2829 switch (lttng_node
->component
) {
2830 case LTTNG_RING_BUFFER_FRONTEND
:
2831 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
2832 case LTTNG_RING_BUFFER_BACKEND
:
2834 case LTTNG_RING_BUFFER_ITER
:
2835 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
2836 case LTTNG_CONTEXT_PERF_COUNTERS
:
2837 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
2843 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
2845 struct lttng_cpuhp_node
*lttng_node
;
2847 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
2848 switch (lttng_node
->component
) {
2849 case LTTNG_RING_BUFFER_FRONTEND
:
2850 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
2851 case LTTNG_RING_BUFFER_BACKEND
:
2853 case LTTNG_RING_BUFFER_ITER
:
2855 case LTTNG_CONTEXT_PERF_COUNTERS
:
2862 static int __init
lttng_init_cpu_hotplug(void)
2866 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
2867 lttng_hotplug_prepare
,
2868 lttng_hotplug_dead
);
2872 lttng_hp_prepare
= ret
;
2873 lttng_rb_set_hp_prepare(ret
);
2875 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
2876 lttng_hotplug_online
,
2877 lttng_hotplug_offline
);
2879 cpuhp_remove_multi_state(lttng_hp_prepare
);
2880 lttng_hp_prepare
= 0;
2883 lttng_hp_online
= ret
;
2884 lttng_rb_set_hp_online(ret
);
2889 static void __exit
lttng_exit_cpu_hotplug(void)
2891 lttng_rb_set_hp_online(0);
2892 cpuhp_remove_multi_state(lttng_hp_online
);
2893 lttng_rb_set_hp_prepare(0);
2894 cpuhp_remove_multi_state(lttng_hp_prepare
);
2897 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2898 static int lttng_init_cpu_hotplug(void)
2902 static void lttng_exit_cpu_hotplug(void)
2905 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2908 static int __init
lttng_events_init(void)
2912 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
2915 ret
= wrapper_get_pfnblock_flags_mask_init();
2918 ret
= wrapper_get_pageblock_flags_mask_init();
2921 ret
= lttng_probes_init();
2924 ret
= lttng_context_init();
2927 ret
= lttng_tracepoint_init();
2930 event_cache
= KMEM_CACHE(lttng_event
, 0);
2935 ret
= lttng_abi_init();
2938 ret
= lttng_logger_init();
2941 ret
= lttng_init_cpu_hotplug();
2944 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
2945 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
2946 __stringify(LTTNG_MODULES_MINOR_VERSION
),
2947 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
2948 LTTNG_MODULES_EXTRAVERSION
,
2950 #ifdef LTTNG_EXTRA_VERSION_GIT
2951 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
2955 #ifdef LTTNG_EXTRA_VERSION_NAME
2956 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
2963 lttng_logger_exit();
2967 kmem_cache_destroy(event_cache
);
2969 lttng_tracepoint_exit();
2971 lttng_context_exit();
2972 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
2973 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
2974 __stringify(LTTNG_MODULES_MINOR_VERSION
),
2975 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
2976 LTTNG_MODULES_EXTRAVERSION
,
2978 #ifdef LTTNG_EXTRA_VERSION_GIT
2979 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
2983 #ifdef LTTNG_EXTRA_VERSION_NAME
2984 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
2991 module_init(lttng_events_init
);
2993 static void __exit
lttng_events_exit(void)
2995 struct lttng_session
*session
, *tmpsession
;
2997 lttng_exit_cpu_hotplug();
2998 lttng_logger_exit();
3000 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
3001 lttng_session_destroy(session
);
3002 kmem_cache_destroy(event_cache
);
3003 lttng_tracepoint_exit();
3004 lttng_context_exit();
3005 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3006 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3007 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3008 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3009 LTTNG_MODULES_EXTRAVERSION
,
3011 #ifdef LTTNG_EXTRA_VERSION_GIT
3012 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
3016 #ifdef LTTNG_EXTRA_VERSION_NAME
3017 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
3023 module_exit(lttng_events_exit
);
3025 #include "extra_version/patches.i"
3026 #ifdef LTTNG_EXTRA_VERSION_GIT
3027 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
3029 #ifdef LTTNG_EXTRA_VERSION_NAME
3030 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
3032 MODULE_LICENSE("GPL and additional rights");
3033 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3034 MODULE_DESCRIPTION("LTTng tracer");
3035 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
3036 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
3037 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
3038 LTTNG_MODULES_EXTRAVERSION
);