1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
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/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <linux/dmi.h>
31 #include <wrapper/uuid.h>
32 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
33 #include <wrapper/random.h>
34 #include <wrapper/tracepoint.h>
35 #include <wrapper/list.h>
36 #include <wrapper/types.h>
37 #include <lttng/kernel-version.h>
38 #include <lttng/events.h>
39 #include <lttng/lttng-bytecode.h>
40 #include <lttng/tracer.h>
41 #include <lttng/event-notifier-notification.h>
42 #include <lttng/abi-old.h>
43 #include <lttng/endian.h>
44 #include <lttng/string-utils.h>
45 #include <lttng/utils.h>
46 #include <ringbuffer/backend.h>
47 #include <ringbuffer/frontend.h>
48 #include <wrapper/time.h>
50 #define METADATA_CACHE_DEFAULT_SIZE 4096
52 static LIST_HEAD(sessions
);
53 static LIST_HEAD(event_notifier_groups
);
54 static LIST_HEAD(lttng_transport_list
);
56 * Protect the sessions and metadata caches.
58 static DEFINE_MUTEX(sessions_mutex
);
59 static struct kmem_cache
*event_cache
;
60 static struct kmem_cache
*event_notifier_cache
;
62 static void lttng_session_lazy_sync_event_enablers(struct lttng_session
*session
);
63 static void lttng_session_sync_event_enablers(struct lttng_session
*session
);
64 static void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
);
65 static void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler
*event_notifier_enabler
);
66 static void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
);
68 static void _lttng_event_destroy(struct lttng_event
*event
);
69 static void _lttng_event_notifier_destroy(struct lttng_event_notifier
*event_notifier
);
70 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
71 static int _lttng_event_unregister(struct lttng_event
*event
);
72 static int _lttng_event_notifier_unregister(struct lttng_event_notifier
*event_notifier
);
74 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
75 struct lttng_channel
*chan
,
76 struct lttng_event
*event
);
78 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
80 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
82 int _lttng_type_statedump(struct lttng_session
*session
,
83 const struct lttng_type
*type
,
86 int _lttng_field_statedump(struct lttng_session
*session
,
87 const struct lttng_event_field
*field
,
90 void synchronize_trace(void)
92 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
98 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
99 #ifdef CONFIG_PREEMPT_RT_FULL
102 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
103 #ifdef CONFIG_PREEMPT_RT
106 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
109 void lttng_lock_sessions(void)
111 mutex_lock(&sessions_mutex
);
114 void lttng_unlock_sessions(void)
116 mutex_unlock(&sessions_mutex
);
119 static struct lttng_transport
*lttng_transport_find(const char *name
)
121 struct lttng_transport
*transport
;
123 list_for_each_entry(transport
, <tng_transport_list
, node
) {
124 if (!strcmp(transport
->name
, name
))
131 * Called with sessions lock held.
133 int lttng_session_active(void)
135 struct lttng_session
*iter
;
137 list_for_each_entry(iter
, &sessions
, list
) {
144 struct lttng_session
*lttng_session_create(void)
146 struct lttng_session
*session
;
147 struct lttng_metadata_cache
*metadata_cache
;
150 mutex_lock(&sessions_mutex
);
151 session
= lttng_kvzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
154 INIT_LIST_HEAD(&session
->chan
);
155 INIT_LIST_HEAD(&session
->events
);
156 lttng_guid_gen(&session
->uuid
);
158 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
161 goto err_free_session
;
162 metadata_cache
->data
= vzalloc(METADATA_CACHE_DEFAULT_SIZE
);
163 if (!metadata_cache
->data
)
165 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
166 kref_init(&metadata_cache
->refcount
);
167 mutex_init(&metadata_cache
->lock
);
168 session
->metadata_cache
= metadata_cache
;
169 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
170 memcpy(&metadata_cache
->uuid
, &session
->uuid
,
171 sizeof(metadata_cache
->uuid
));
172 INIT_LIST_HEAD(&session
->enablers_head
);
173 for (i
= 0; i
< LTTNG_EVENT_HT_SIZE
; i
++)
174 INIT_HLIST_HEAD(&session
->events_ht
.table
[i
]);
175 list_add(&session
->list
, &sessions
);
176 session
->pid_tracker
.session
= session
;
177 session
->pid_tracker
.tracker_type
= TRACKER_PID
;
178 session
->vpid_tracker
.session
= session
;
179 session
->vpid_tracker
.tracker_type
= TRACKER_VPID
;
180 session
->uid_tracker
.session
= session
;
181 session
->uid_tracker
.tracker_type
= TRACKER_UID
;
182 session
->vuid_tracker
.session
= session
;
183 session
->vuid_tracker
.tracker_type
= TRACKER_VUID
;
184 session
->gid_tracker
.session
= session
;
185 session
->gid_tracker
.tracker_type
= TRACKER_GID
;
186 session
->vgid_tracker
.session
= session
;
187 session
->vgid_tracker
.tracker_type
= TRACKER_VGID
;
188 mutex_unlock(&sessions_mutex
);
192 kfree(metadata_cache
);
194 lttng_kvfree(session
);
196 mutex_unlock(&sessions_mutex
);
200 struct lttng_event_notifier_group
*lttng_event_notifier_group_create(void)
202 struct lttng_transport
*transport
= NULL
;
203 struct lttng_event_notifier_group
*event_notifier_group
;
204 const char *transport_name
= "relay-event-notifier";
205 size_t subbuf_size
= 4096; //TODO
206 size_t num_subbuf
= 16; //TODO
207 unsigned int switch_timer_interval
= 0;
208 unsigned int read_timer_interval
= 0;
211 mutex_lock(&sessions_mutex
);
213 transport
= lttng_transport_find(transport_name
);
215 printk(KERN_WARNING
"LTTng: transport %s not found\n",
219 if (!try_module_get(transport
->owner
)) {
220 printk(KERN_WARNING
"LTTng: Can't lock transport %s module.\n",
225 event_notifier_group
= lttng_kvzalloc(sizeof(struct lttng_event_notifier_group
),
227 if (!event_notifier_group
)
231 * Initialize the ring buffer used to store event notifier
234 event_notifier_group
->ops
= &transport
->ops
;
235 event_notifier_group
->chan
= transport
->ops
.channel_create(
236 transport_name
, event_notifier_group
, NULL
,
237 subbuf_size
, num_subbuf
, switch_timer_interval
,
238 read_timer_interval
);
239 if (!event_notifier_group
->chan
)
242 event_notifier_group
->transport
= transport
;
244 INIT_LIST_HEAD(&event_notifier_group
->enablers_head
);
245 INIT_LIST_HEAD(&event_notifier_group
->event_notifiers_head
);
246 for (i
= 0; i
< LTTNG_EVENT_NOTIFIER_HT_SIZE
; i
++)
247 INIT_HLIST_HEAD(&event_notifier_group
->event_notifiers_ht
.table
[i
]);
249 list_add(&event_notifier_group
->node
, &event_notifier_groups
);
251 mutex_unlock(&sessions_mutex
);
253 return event_notifier_group
;
256 lttng_kvfree(event_notifier_group
);
259 module_put(transport
->owner
);
261 mutex_unlock(&sessions_mutex
);
265 void metadata_cache_destroy(struct kref
*kref
)
267 struct lttng_metadata_cache
*cache
=
268 container_of(kref
, struct lttng_metadata_cache
, refcount
);
273 void lttng_session_destroy(struct lttng_session
*session
)
275 struct lttng_channel
*chan
, *tmpchan
;
276 struct lttng_event
*event
, *tmpevent
;
277 struct lttng_metadata_stream
*metadata_stream
;
278 struct lttng_event_enabler
*event_enabler
, *tmp_event_enabler
;
281 mutex_lock(&sessions_mutex
);
282 WRITE_ONCE(session
->active
, 0);
283 list_for_each_entry(chan
, &session
->chan
, list
) {
284 ret
= lttng_syscalls_unregister_event(chan
);
287 list_for_each_entry(event
, &session
->events
, list
) {
288 ret
= _lttng_event_unregister(event
);
291 synchronize_trace(); /* Wait for in-flight events to complete */
292 list_for_each_entry(chan
, &session
->chan
, list
) {
293 ret
= lttng_syscalls_destroy_event(chan
);
296 list_for_each_entry_safe(event_enabler
, tmp_event_enabler
,
297 &session
->enablers_head
, node
)
298 lttng_event_enabler_destroy(event_enabler
);
299 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
300 _lttng_event_destroy(event
);
301 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
302 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
303 _lttng_channel_destroy(chan
);
305 mutex_lock(&session
->metadata_cache
->lock
);
306 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
307 _lttng_metadata_channel_hangup(metadata_stream
);
308 mutex_unlock(&session
->metadata_cache
->lock
);
309 lttng_id_tracker_destroy(&session
->pid_tracker
, false);
310 lttng_id_tracker_destroy(&session
->vpid_tracker
, false);
311 lttng_id_tracker_destroy(&session
->uid_tracker
, false);
312 lttng_id_tracker_destroy(&session
->vuid_tracker
, false);
313 lttng_id_tracker_destroy(&session
->gid_tracker
, false);
314 lttng_id_tracker_destroy(&session
->vgid_tracker
, false);
315 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
316 list_del(&session
->list
);
317 mutex_unlock(&sessions_mutex
);
318 lttng_kvfree(session
);
321 void lttng_event_notifier_group_destroy(
322 struct lttng_event_notifier_group
*event_notifier_group
)
324 struct lttng_event_notifier_enabler
*event_notifier_enabler
, *tmp_event_notifier_enabler
;
325 struct lttng_event_notifier
*event_notifier
, *tmpevent_notifier
;
328 if (!event_notifier_group
)
331 mutex_lock(&sessions_mutex
);
333 ret
= lttng_syscalls_unregister_event_notifier(event_notifier_group
);
336 list_for_each_entry_safe(event_notifier
, tmpevent_notifier
,
337 &event_notifier_group
->event_notifiers_head
, list
) {
338 ret
= _lttng_event_notifier_unregister(event_notifier
);
342 /* Wait for in-flight event notifier to complete */
345 irq_work_sync(&event_notifier_group
->wakeup_pending
);
347 kfree(event_notifier_group
->sc_filter
);
349 list_for_each_entry_safe(event_notifier_enabler
, tmp_event_notifier_enabler
,
350 &event_notifier_group
->enablers_head
, node
)
351 lttng_event_notifier_enabler_destroy(event_notifier_enabler
);
353 list_for_each_entry_safe(event_notifier
, tmpevent_notifier
,
354 &event_notifier_group
->event_notifiers_head
, list
)
355 _lttng_event_notifier_destroy(event_notifier
);
357 event_notifier_group
->ops
->channel_destroy(event_notifier_group
->chan
);
358 module_put(event_notifier_group
->transport
->owner
);
359 list_del(&event_notifier_group
->node
);
361 mutex_unlock(&sessions_mutex
);
362 lttng_kvfree(event_notifier_group
);
365 int lttng_session_statedump(struct lttng_session
*session
)
369 mutex_lock(&sessions_mutex
);
370 ret
= lttng_statedump_start(session
);
371 mutex_unlock(&sessions_mutex
);
375 int lttng_session_enable(struct lttng_session
*session
)
378 struct lttng_channel
*chan
;
380 mutex_lock(&sessions_mutex
);
381 if (session
->active
) {
386 /* Set transient enabler state to "enabled" */
389 /* We need to sync enablers with session before activation. */
390 lttng_session_sync_event_enablers(session
);
393 * Snapshot the number of events per channel to know the type of header
396 list_for_each_entry(chan
, &session
->chan
, list
) {
397 if (chan
->header_type
)
398 continue; /* don't change it if session stop/restart */
399 if (chan
->free_event_id
< 31)
400 chan
->header_type
= 1; /* compact */
402 chan
->header_type
= 2; /* large */
405 /* Clear each stream's quiescent state. */
406 list_for_each_entry(chan
, &session
->chan
, list
) {
407 if (chan
->channel_type
!= METADATA_CHANNEL
)
408 lib_ring_buffer_clear_quiescent_channel(chan
->chan
);
411 WRITE_ONCE(session
->active
, 1);
412 WRITE_ONCE(session
->been_active
, 1);
413 ret
= _lttng_session_metadata_statedump(session
);
415 WRITE_ONCE(session
->active
, 0);
418 ret
= lttng_statedump_start(session
);
420 WRITE_ONCE(session
->active
, 0);
422 mutex_unlock(&sessions_mutex
);
426 int lttng_session_disable(struct lttng_session
*session
)
429 struct lttng_channel
*chan
;
431 mutex_lock(&sessions_mutex
);
432 if (!session
->active
) {
436 WRITE_ONCE(session
->active
, 0);
438 /* Set transient enabler state to "disabled" */
440 lttng_session_sync_event_enablers(session
);
442 /* Set each stream's quiescent state. */
443 list_for_each_entry(chan
, &session
->chan
, list
) {
444 if (chan
->channel_type
!= METADATA_CHANNEL
)
445 lib_ring_buffer_set_quiescent_channel(chan
->chan
);
448 mutex_unlock(&sessions_mutex
);
452 int lttng_session_metadata_regenerate(struct lttng_session
*session
)
455 struct lttng_channel
*chan
;
456 struct lttng_event
*event
;
457 struct lttng_metadata_cache
*cache
= session
->metadata_cache
;
458 struct lttng_metadata_stream
*stream
;
460 mutex_lock(&sessions_mutex
);
461 if (!session
->active
) {
466 mutex_lock(&cache
->lock
);
467 memset(cache
->data
, 0, cache
->cache_alloc
);
468 cache
->metadata_written
= 0;
470 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
) {
471 stream
->metadata_out
= 0;
472 stream
->metadata_in
= 0;
474 mutex_unlock(&cache
->lock
);
476 session
->metadata_dumped
= 0;
477 list_for_each_entry(chan
, &session
->chan
, list
) {
478 chan
->metadata_dumped
= 0;
481 list_for_each_entry(event
, &session
->events
, list
) {
482 event
->metadata_dumped
= 0;
485 ret
= _lttng_session_metadata_statedump(session
);
488 mutex_unlock(&sessions_mutex
);
492 int lttng_channel_enable(struct lttng_channel
*channel
)
496 mutex_lock(&sessions_mutex
);
497 if (channel
->channel_type
== METADATA_CHANNEL
) {
501 if (channel
->enabled
) {
505 /* Set transient enabler state to "enabled" */
507 lttng_session_sync_event_enablers(channel
->session
);
508 /* Set atomically the state to "enabled" */
509 WRITE_ONCE(channel
->enabled
, 1);
511 mutex_unlock(&sessions_mutex
);
515 int lttng_channel_disable(struct lttng_channel
*channel
)
519 mutex_lock(&sessions_mutex
);
520 if (channel
->channel_type
== METADATA_CHANNEL
) {
524 if (!channel
->enabled
) {
528 /* Set atomically the state to "disabled" */
529 WRITE_ONCE(channel
->enabled
, 0);
530 /* Set transient enabler state to "enabled" */
532 lttng_session_sync_event_enablers(channel
->session
);
534 mutex_unlock(&sessions_mutex
);
538 int lttng_event_enable(struct lttng_event
*event
)
542 mutex_lock(&sessions_mutex
);
543 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
547 if (event
->enabled
) {
551 switch (event
->instrumentation
) {
552 case LTTNG_KERNEL_TRACEPOINT
:
553 case LTTNG_KERNEL_SYSCALL
:
556 case LTTNG_KERNEL_KPROBE
:
557 case LTTNG_KERNEL_UPROBE
:
558 case LTTNG_KERNEL_NOOP
:
559 WRITE_ONCE(event
->enabled
, 1);
561 case LTTNG_KERNEL_KRETPROBE
:
562 ret
= lttng_kretprobes_event_enable_state(event
, 1);
564 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
570 mutex_unlock(&sessions_mutex
);
574 int lttng_event_disable(struct lttng_event
*event
)
578 mutex_lock(&sessions_mutex
);
579 if (event
->chan
->channel_type
== METADATA_CHANNEL
) {
583 if (!event
->enabled
) {
587 switch (event
->instrumentation
) {
588 case LTTNG_KERNEL_TRACEPOINT
:
589 case LTTNG_KERNEL_SYSCALL
:
592 case LTTNG_KERNEL_KPROBE
:
593 case LTTNG_KERNEL_UPROBE
:
594 case LTTNG_KERNEL_NOOP
:
595 WRITE_ONCE(event
->enabled
, 0);
597 case LTTNG_KERNEL_KRETPROBE
:
598 ret
= lttng_kretprobes_event_enable_state(event
, 0);
600 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
606 mutex_unlock(&sessions_mutex
);
610 int lttng_event_notifier_enable(struct lttng_event_notifier
*event_notifier
)
614 mutex_lock(&sessions_mutex
);
615 if (event_notifier
->enabled
) {
619 switch (event_notifier
->instrumentation
) {
620 case LTTNG_KERNEL_TRACEPOINT
:
621 case LTTNG_KERNEL_SYSCALL
:
624 case LTTNG_KERNEL_KPROBE
:
625 case LTTNG_KERNEL_UPROBE
:
626 WRITE_ONCE(event_notifier
->enabled
, 1);
628 case LTTNG_KERNEL_FUNCTION
:
629 case LTTNG_KERNEL_NOOP
:
630 case LTTNG_KERNEL_KRETPROBE
:
636 mutex_unlock(&sessions_mutex
);
640 int lttng_event_notifier_disable(struct lttng_event_notifier
*event_notifier
)
644 mutex_lock(&sessions_mutex
);
645 if (!event_notifier
->enabled
) {
649 switch (event_notifier
->instrumentation
) {
650 case LTTNG_KERNEL_TRACEPOINT
:
651 case LTTNG_KERNEL_SYSCALL
:
654 case LTTNG_KERNEL_KPROBE
:
655 case LTTNG_KERNEL_UPROBE
:
656 WRITE_ONCE(event_notifier
->enabled
, 0);
658 case LTTNG_KERNEL_FUNCTION
:
659 case LTTNG_KERNEL_NOOP
:
660 case LTTNG_KERNEL_KRETPROBE
:
666 mutex_unlock(&sessions_mutex
);
670 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
671 const char *transport_name
,
673 size_t subbuf_size
, size_t num_subbuf
,
674 unsigned int switch_timer_interval
,
675 unsigned int read_timer_interval
,
676 enum channel_type channel_type
)
678 struct lttng_channel
*chan
;
679 struct lttng_transport
*transport
= NULL
;
681 mutex_lock(&sessions_mutex
);
682 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
683 goto active
; /* Refuse to add channel to active session */
684 transport
= lttng_transport_find(transport_name
);
686 printk(KERN_WARNING
"LTTng: transport %s not found\n",
690 if (!try_module_get(transport
->owner
)) {
691 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
694 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
697 chan
->session
= session
;
698 chan
->id
= session
->free_chan_id
++;
699 chan
->ops
= &transport
->ops
;
701 * Note: the channel creation op already writes into the packet
702 * headers. Therefore the "chan" information used as input
703 * should be already accessible.
705 chan
->chan
= transport
->ops
.channel_create(transport_name
,
706 chan
, buf_addr
, subbuf_size
, num_subbuf
,
707 switch_timer_interval
, read_timer_interval
);
712 chan
->transport
= transport
;
713 chan
->channel_type
= channel_type
;
714 list_add(&chan
->list
, &session
->chan
);
715 mutex_unlock(&sessions_mutex
);
722 module_put(transport
->owner
);
725 mutex_unlock(&sessions_mutex
);
730 * Only used internally at session destruction for per-cpu channels, and
731 * when metadata channel is released.
732 * Needs to be called with sessions mutex held.
735 void _lttng_channel_destroy(struct lttng_channel
*chan
)
737 chan
->ops
->channel_destroy(chan
->chan
);
738 module_put(chan
->transport
->owner
);
739 list_del(&chan
->list
);
740 lttng_destroy_context(chan
->ctx
);
744 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
746 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
748 /* Protect the metadata cache with the sessions_mutex. */
749 mutex_lock(&sessions_mutex
);
750 _lttng_channel_destroy(chan
);
751 mutex_unlock(&sessions_mutex
);
753 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
756 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
758 stream
->finalized
= 1;
759 wake_up_interruptible(&stream
->read_wait
);
763 * Supports event creation while tracing session is active.
764 * Needs to be called with sessions mutex held.
766 struct lttng_event
*_lttng_event_create(struct lttng_channel
*chan
,
767 struct lttng_kernel_event
*event_param
,
769 const struct lttng_event_desc
*event_desc
,
770 enum lttng_kernel_instrumentation itype
)
772 struct lttng_session
*session
= chan
->session
;
773 struct lttng_event
*event
;
774 const char *event_name
;
775 struct hlist_head
*head
;
778 if (chan
->free_event_id
== -1U) {
784 case LTTNG_KERNEL_TRACEPOINT
:
785 event_name
= event_desc
->name
;
787 case LTTNG_KERNEL_KPROBE
:
788 case LTTNG_KERNEL_UPROBE
:
789 case LTTNG_KERNEL_KRETPROBE
:
790 case LTTNG_KERNEL_NOOP
:
791 case LTTNG_KERNEL_SYSCALL
:
792 event_name
= event_param
->name
;
794 case LTTNG_KERNEL_FUNCTION
: /* Fall-through. */
801 head
= utils_borrow_hash_table_bucket(session
->events_ht
.table
,
802 LTTNG_EVENT_HT_SIZE
, event_name
);
803 lttng_hlist_for_each_entry(event
, head
, hlist
) {
804 WARN_ON_ONCE(!event
->desc
);
805 if (!strncmp(event
->desc
->name
, event_name
,
806 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
807 && chan
== event
->chan
) {
813 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
819 event
->filter
= filter
;
820 event
->id
= chan
->free_event_id
++;
821 event
->instrumentation
= itype
;
822 event
->evtype
= LTTNG_TYPE_EVENT
;
823 INIT_LIST_HEAD(&event
->filter_bytecode_runtime_head
);
824 INIT_LIST_HEAD(&event
->enablers_ref_head
);
827 case LTTNG_KERNEL_TRACEPOINT
:
828 /* Event will be enabled by enabler sync. */
830 event
->registered
= 0;
831 event
->desc
= lttng_event_desc_get(event_name
);
836 /* Populate lttng_event structure before event registration. */
839 case LTTNG_KERNEL_KPROBE
:
841 * Needs to be explicitly enabled after creation, since
842 * we may want to apply filters.
845 event
->registered
= 1;
847 * Populate lttng_event structure before event
851 ret
= lttng_kprobes_register_event(event_name
,
852 event_param
->u
.kprobe
.symbol_name
,
853 event_param
->u
.kprobe
.offset
,
854 event_param
->u
.kprobe
.addr
,
860 ret
= try_module_get(event
->desc
->owner
);
863 case LTTNG_KERNEL_KRETPROBE
:
865 struct lttng_event
*event_return
;
867 /* kretprobe defines 2 events */
869 * Needs to be explicitly enabled after creation, since
870 * we may want to apply filters.
873 event
->registered
= 1;
875 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
880 event_return
->chan
= chan
;
881 event_return
->filter
= filter
;
882 event_return
->id
= chan
->free_event_id
++;
883 event_return
->enabled
= 0;
884 event_return
->registered
= 1;
885 event_return
->instrumentation
= itype
;
887 * Populate lttng_event structure before kretprobe registration.
890 ret
= lttng_kretprobes_register(event_name
,
891 event_param
->u
.kretprobe
.symbol_name
,
892 event_param
->u
.kretprobe
.offset
,
893 event_param
->u
.kretprobe
.addr
,
894 event
, event_return
);
896 kmem_cache_free(event_cache
, event_return
);
900 /* Take 2 refs on the module: one per event. */
901 ret
= try_module_get(event
->desc
->owner
);
903 ret
= try_module_get(event
->desc
->owner
);
905 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
907 WARN_ON_ONCE(ret
> 0);
909 kmem_cache_free(event_cache
, event_return
);
910 module_put(event
->desc
->owner
);
911 module_put(event
->desc
->owner
);
912 goto statedump_error
;
914 list_add(&event_return
->list
, &chan
->session
->events
);
917 case LTTNG_KERNEL_NOOP
:
918 case LTTNG_KERNEL_SYSCALL
:
920 * Needs to be explicitly enabled after creation, since
921 * we may want to apply filters.
924 event
->registered
= 0;
925 event
->desc
= event_desc
;
926 switch (event_param
->u
.syscall
.entryexit
) {
927 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
930 case LTTNG_KERNEL_SYSCALL_ENTRY
:
931 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
933 case LTTNG_KERNEL_SYSCALL_EXIT
:
934 event
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
937 switch (event_param
->u
.syscall
.abi
) {
938 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
941 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
942 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
944 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
945 event
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
953 case LTTNG_KERNEL_UPROBE
:
955 * Needs to be explicitly enabled after creation, since
956 * we may want to apply filters.
959 event
->registered
= 1;
962 * Populate lttng_event structure before event
967 ret
= lttng_uprobes_register_event(event_param
->name
,
968 event_param
->u
.uprobe
.fd
,
972 ret
= try_module_get(event
->desc
->owner
);
975 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
981 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
982 WARN_ON_ONCE(ret
> 0);
984 goto statedump_error
;
986 hlist_add_head(&event
->hlist
, head
);
987 list_add(&event
->list
, &chan
->session
->events
);
991 /* If a statedump error occurs, events will not be readable. */
993 kmem_cache_free(event_cache
, event
);
1001 struct lttng_event_notifier
*_lttng_event_notifier_create(
1002 const struct lttng_event_desc
*event_desc
,
1003 uint64_t token
, struct lttng_event_notifier_group
*event_notifier_group
,
1004 struct lttng_kernel_event_notifier
*event_notifier_param
,
1005 void *filter
, enum lttng_kernel_instrumentation itype
)
1007 struct lttng_event_notifier
*event_notifier
;
1008 const char *event_name
;
1009 struct hlist_head
*head
;
1013 case LTTNG_KERNEL_TRACEPOINT
:
1014 event_name
= event_desc
->name
;
1016 case LTTNG_KERNEL_KPROBE
:
1017 case LTTNG_KERNEL_UPROBE
:
1018 case LTTNG_KERNEL_SYSCALL
:
1019 event_name
= event_notifier_param
->event
.name
;
1021 case LTTNG_KERNEL_KRETPROBE
:
1022 case LTTNG_KERNEL_FUNCTION
:
1023 case LTTNG_KERNEL_NOOP
:
1030 head
= utils_borrow_hash_table_bucket(event_notifier_group
->event_notifiers_ht
.table
,
1031 LTTNG_EVENT_NOTIFIER_HT_SIZE
, event_name
);
1032 lttng_hlist_for_each_entry(event_notifier
, head
, hlist
) {
1033 WARN_ON_ONCE(!event_notifier
->desc
);
1034 if (!strncmp(event_notifier
->desc
->name
, event_name
,
1035 LTTNG_KERNEL_SYM_NAME_LEN
- 1)
1036 && event_notifier_group
== event_notifier
->group
1037 && token
== event_notifier
->user_token
) {
1043 event_notifier
= kmem_cache_zalloc(event_notifier_cache
, GFP_KERNEL
);
1044 if (!event_notifier
) {
1049 event_notifier
->group
= event_notifier_group
;
1050 event_notifier
->user_token
= token
;
1051 event_notifier
->filter
= filter
;
1052 event_notifier
->instrumentation
= itype
;
1053 event_notifier
->evtype
= LTTNG_TYPE_EVENT
;
1054 event_notifier
->send_notification
= lttng_event_notifier_notification_send
;
1055 INIT_LIST_HEAD(&event_notifier
->filter_bytecode_runtime_head
);
1056 INIT_LIST_HEAD(&event_notifier
->enablers_ref_head
);
1059 case LTTNG_KERNEL_TRACEPOINT
:
1060 /* Event will be enabled by enabler sync. */
1061 event_notifier
->enabled
= 0;
1062 event_notifier
->registered
= 0;
1063 event_notifier
->desc
= lttng_event_desc_get(event_name
);
1064 if (!event_notifier
->desc
) {
1066 goto register_error
;
1068 /* Populate lttng_event_notifier structure before event registration. */
1071 case LTTNG_KERNEL_KPROBE
:
1073 * Needs to be explicitly enabled after creation, since
1074 * we may want to apply filters.
1076 event_notifier
->enabled
= 0;
1077 event_notifier
->registered
= 1;
1079 * Populate lttng_event_notifier structure before event
1083 ret
= lttng_kprobes_register_event_notifier(
1084 event_notifier_param
->event
.u
.kprobe
.symbol_name
,
1085 event_notifier_param
->event
.u
.kprobe
.offset
,
1086 event_notifier_param
->event
.u
.kprobe
.addr
,
1090 goto register_error
;
1092 ret
= try_module_get(event_notifier
->desc
->owner
);
1095 case LTTNG_KERNEL_NOOP
:
1096 case LTTNG_KERNEL_SYSCALL
:
1098 * Needs to be explicitly enabled after creation, since
1099 * we may want to apply filters.
1101 event_notifier
->enabled
= 0;
1102 event_notifier
->registered
= 0;
1103 event_notifier
->desc
= event_desc
;
1104 switch (event_notifier_param
->event
.u
.syscall
.entryexit
) {
1105 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1107 goto register_error
;
1108 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1109 event_notifier
->u
.syscall
.entryexit
= LTTNG_SYSCALL_ENTRY
;
1111 case LTTNG_KERNEL_SYSCALL_EXIT
:
1112 event_notifier
->u
.syscall
.entryexit
= LTTNG_SYSCALL_EXIT
;
1115 switch (event_notifier_param
->event
.u
.syscall
.abi
) {
1116 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1118 goto register_error
;
1119 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1120 event_notifier
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_NATIVE
;
1122 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1123 event_notifier
->u
.syscall
.abi
= LTTNG_SYSCALL_ABI_COMPAT
;
1127 if (!event_notifier
->desc
) {
1129 goto register_error
;
1132 case LTTNG_KERNEL_UPROBE
:
1134 * Needs to be explicitly enabled after creation, since
1135 * we may want to apply filters.
1137 event_notifier
->enabled
= 0;
1138 event_notifier
->registered
= 1;
1141 * Populate lttng_event_notifier structure before
1142 * event_notifier registration.
1146 ret
= lttng_uprobes_register_event_notifier(
1147 event_notifier_param
->event
.name
,
1148 event_notifier_param
->event
.u
.uprobe
.fd
,
1151 goto register_error
;
1152 ret
= try_module_get(event_notifier
->desc
->owner
);
1155 case LTTNG_KERNEL_KRETPROBE
:
1156 case LTTNG_KERNEL_FUNCTION
:
1160 goto register_error
;
1163 list_add(&event_notifier
->list
, &event_notifier_group
->event_notifiers_head
);
1164 hlist_add_head(&event_notifier
->hlist
, head
);
1165 return event_notifier
;
1168 kmem_cache_free(event_notifier_cache
, event_notifier
);
1172 return ERR_PTR(ret
);
1175 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
1176 struct lttng_kernel_event
*event_param
,
1178 const struct lttng_event_desc
*event_desc
,
1179 enum lttng_kernel_instrumentation itype
)
1181 struct lttng_event
*event
;
1183 mutex_lock(&sessions_mutex
);
1184 event
= _lttng_event_create(chan
, event_param
, filter
, event_desc
,
1186 mutex_unlock(&sessions_mutex
);
1190 struct lttng_event_notifier
*lttng_event_notifier_create(
1191 const struct lttng_event_desc
*event_desc
,
1192 uint64_t id
, struct lttng_event_notifier_group
*event_notifier_group
,
1193 struct lttng_kernel_event_notifier
*event_notifier_param
,
1194 void *filter
, enum lttng_kernel_instrumentation itype
)
1196 struct lttng_event_notifier
*event_notifier
;
1198 mutex_lock(&sessions_mutex
);
1199 event_notifier
= _lttng_event_notifier_create(event_desc
, id
,
1200 event_notifier_group
, event_notifier_param
, filter
, itype
);
1201 mutex_unlock(&sessions_mutex
);
1202 return event_notifier
;
1205 /* Only used for tracepoints for now. */
1207 void register_event(struct lttng_event
*event
)
1209 const struct lttng_event_desc
*desc
;
1212 if (event
->registered
)
1216 switch (event
->instrumentation
) {
1217 case LTTNG_KERNEL_TRACEPOINT
:
1218 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
1219 desc
->probe_callback
,
1222 case LTTNG_KERNEL_SYSCALL
:
1223 ret
= lttng_syscall_filter_enable_event(event
->chan
, event
);
1225 case LTTNG_KERNEL_KPROBE
:
1226 case LTTNG_KERNEL_UPROBE
:
1227 case LTTNG_KERNEL_KRETPROBE
:
1228 case LTTNG_KERNEL_NOOP
:
1231 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
1236 event
->registered
= 1;
1240 * Only used internally at session destruction.
1242 int _lttng_event_unregister(struct lttng_event
*event
)
1244 const struct lttng_event_desc
*desc
;
1247 if (!event
->registered
)
1251 switch (event
->instrumentation
) {
1252 case LTTNG_KERNEL_TRACEPOINT
:
1253 ret
= lttng_wrapper_tracepoint_probe_unregister(event
->desc
->kname
,
1254 event
->desc
->probe_callback
,
1257 case LTTNG_KERNEL_KPROBE
:
1258 lttng_kprobes_unregister_event(event
);
1261 case LTTNG_KERNEL_KRETPROBE
:
1262 lttng_kretprobes_unregister(event
);
1265 case LTTNG_KERNEL_SYSCALL
:
1266 ret
= lttng_syscall_filter_disable_event(event
->chan
, event
);
1268 case LTTNG_KERNEL_NOOP
:
1271 case LTTNG_KERNEL_UPROBE
:
1272 lttng_uprobes_unregister_event(event
);
1275 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
1280 event
->registered
= 0;
1284 /* Only used for tracepoints for now. */
1286 void register_event_notifier(struct lttng_event_notifier
*event_notifier
)
1288 const struct lttng_event_desc
*desc
;
1291 if (event_notifier
->registered
)
1294 desc
= event_notifier
->desc
;
1295 switch (event_notifier
->instrumentation
) {
1296 case LTTNG_KERNEL_TRACEPOINT
:
1297 ret
= lttng_wrapper_tracepoint_probe_register(desc
->kname
,
1298 desc
->event_notifier_callback
,
1301 case LTTNG_KERNEL_SYSCALL
:
1302 ret
= lttng_syscall_filter_enable_event_notifier(event_notifier
);
1304 case LTTNG_KERNEL_KPROBE
:
1305 case LTTNG_KERNEL_UPROBE
:
1308 case LTTNG_KERNEL_KRETPROBE
:
1309 case LTTNG_KERNEL_FUNCTION
:
1310 case LTTNG_KERNEL_NOOP
:
1315 event_notifier
->registered
= 1;
1319 int _lttng_event_notifier_unregister(
1320 struct lttng_event_notifier
*event_notifier
)
1322 const struct lttng_event_desc
*desc
;
1325 if (!event_notifier
->registered
)
1328 desc
= event_notifier
->desc
;
1329 switch (event_notifier
->instrumentation
) {
1330 case LTTNG_KERNEL_TRACEPOINT
:
1331 ret
= lttng_wrapper_tracepoint_probe_unregister(event_notifier
->desc
->kname
,
1332 event_notifier
->desc
->event_notifier_callback
,
1335 case LTTNG_KERNEL_KPROBE
:
1336 lttng_kprobes_unregister_event_notifier(event_notifier
);
1339 case LTTNG_KERNEL_UPROBE
:
1340 lttng_uprobes_unregister_event_notifier(event_notifier
);
1343 case LTTNG_KERNEL_SYSCALL
:
1344 ret
= lttng_syscall_filter_disable_event_notifier(event_notifier
);
1346 case LTTNG_KERNEL_KRETPROBE
:
1347 case LTTNG_KERNEL_FUNCTION
:
1348 case LTTNG_KERNEL_NOOP
:
1353 event_notifier
->registered
= 0;
1358 * Only used internally at session destruction.
1361 void _lttng_event_destroy(struct lttng_event
*event
)
1363 switch (event
->instrumentation
) {
1364 case LTTNG_KERNEL_TRACEPOINT
:
1365 lttng_event_desc_put(event
->desc
);
1367 case LTTNG_KERNEL_KPROBE
:
1368 module_put(event
->desc
->owner
);
1369 lttng_kprobes_destroy_event_private(event
);
1371 case LTTNG_KERNEL_KRETPROBE
:
1372 module_put(event
->desc
->owner
);
1373 lttng_kretprobes_destroy_private(event
);
1375 case LTTNG_KERNEL_NOOP
:
1376 case LTTNG_KERNEL_SYSCALL
:
1378 case LTTNG_KERNEL_UPROBE
:
1379 module_put(event
->desc
->owner
);
1380 lttng_uprobes_destroy_event_private(event
);
1382 case LTTNG_KERNEL_FUNCTION
: /* Fall-through */
1386 list_del(&event
->list
);
1387 lttng_destroy_context(event
->ctx
);
1388 kmem_cache_free(event_cache
, event
);
1392 * Only used internally at session destruction.
1395 void _lttng_event_notifier_destroy(struct lttng_event_notifier
*event_notifier
)
1397 switch (event_notifier
->instrumentation
) {
1398 case LTTNG_KERNEL_TRACEPOINT
:
1399 lttng_event_desc_put(event_notifier
->desc
);
1401 case LTTNG_KERNEL_KPROBE
:
1402 module_put(event_notifier
->desc
->owner
);
1403 lttng_kprobes_destroy_event_notifier_private(event_notifier
);
1405 case LTTNG_KERNEL_NOOP
:
1406 case LTTNG_KERNEL_SYSCALL
:
1408 case LTTNG_KERNEL_UPROBE
:
1409 module_put(event_notifier
->desc
->owner
);
1410 lttng_uprobes_destroy_event_notifier_private(event_notifier
);
1412 case LTTNG_KERNEL_KRETPROBE
:
1413 case LTTNG_KERNEL_FUNCTION
:
1417 list_del(&event_notifier
->list
);
1418 kmem_cache_free(event_notifier_cache
, event_notifier
);
1421 struct lttng_id_tracker
*get_tracker(struct lttng_session
*session
,
1422 enum tracker_type tracker_type
)
1424 switch (tracker_type
) {
1426 return &session
->pid_tracker
;
1428 return &session
->vpid_tracker
;
1430 return &session
->uid_tracker
;
1432 return &session
->vuid_tracker
;
1434 return &session
->gid_tracker
;
1436 return &session
->vgid_tracker
;
1443 int lttng_session_track_id(struct lttng_session
*session
,
1444 enum tracker_type tracker_type
, int id
)
1446 struct lttng_id_tracker
*tracker
;
1449 tracker
= get_tracker(session
, tracker_type
);
1454 mutex_lock(&sessions_mutex
);
1456 /* track all ids: destroy tracker. */
1457 lttng_id_tracker_destroy(tracker
, true);
1460 ret
= lttng_id_tracker_add(tracker
, id
);
1462 mutex_unlock(&sessions_mutex
);
1466 int lttng_session_untrack_id(struct lttng_session
*session
,
1467 enum tracker_type tracker_type
, int id
)
1469 struct lttng_id_tracker
*tracker
;
1472 tracker
= get_tracker(session
, tracker_type
);
1477 mutex_lock(&sessions_mutex
);
1479 /* untrack all ids: replace by empty tracker. */
1480 ret
= lttng_id_tracker_empty_set(tracker
);
1482 ret
= lttng_id_tracker_del(tracker
, id
);
1484 mutex_unlock(&sessions_mutex
);
1489 void *id_list_start(struct seq_file
*m
, loff_t
*pos
)
1491 struct lttng_id_tracker
*id_tracker
= m
->private;
1492 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1493 struct lttng_id_hash_node
*e
;
1496 mutex_lock(&sessions_mutex
);
1498 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1499 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1501 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1507 /* ID tracker disabled. */
1508 if (iter
>= *pos
&& iter
== 0) {
1509 return id_tracker_p
; /* empty tracker */
1517 /* Called with sessions_mutex held. */
1519 void *id_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1521 struct lttng_id_tracker
*id_tracker
= m
->private;
1522 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1523 struct lttng_id_hash_node
*e
;
1528 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
1529 struct hlist_head
*head
= &id_tracker_p
->id_hash
[i
];
1531 lttng_hlist_for_each_entry(e
, head
, hlist
) {
1532 if (iter
++ >= *ppos
)
1537 /* ID tracker disabled. */
1538 if (iter
>= *ppos
&& iter
== 0)
1539 return p
; /* empty tracker */
1548 void id_list_stop(struct seq_file
*m
, void *p
)
1550 mutex_unlock(&sessions_mutex
);
1554 int id_list_show(struct seq_file
*m
, void *p
)
1556 struct lttng_id_tracker
*id_tracker
= m
->private;
1557 struct lttng_id_tracker_rcu
*id_tracker_p
= id_tracker
->p
;
1560 if (p
== id_tracker_p
) {
1561 /* Tracker disabled. */
1564 const struct lttng_id_hash_node
*e
= p
;
1566 id
= lttng_id_tracker_get_node_id(e
);
1568 switch (id_tracker
->tracker_type
) {
1570 seq_printf(m
, "process { pid = %d; };\n", id
);
1573 seq_printf(m
, "process { vpid = %d; };\n", id
);
1576 seq_printf(m
, "user { uid = %d; };\n", id
);
1579 seq_printf(m
, "user { vuid = %d; };\n", id
);
1582 seq_printf(m
, "group { gid = %d; };\n", id
);
1585 seq_printf(m
, "group { vgid = %d; };\n", id
);
1588 seq_printf(m
, "UNKNOWN { field = %d };\n", id
);
1594 const struct seq_operations lttng_tracker_ids_list_seq_ops
= {
1595 .start
= id_list_start
,
1596 .next
= id_list_next
,
1597 .stop
= id_list_stop
,
1598 .show
= id_list_show
,
1602 int lttng_tracker_ids_list_open(struct inode
*inode
, struct file
*file
)
1604 return seq_open(file
, <tng_tracker_ids_list_seq_ops
);
1608 int lttng_tracker_ids_list_release(struct inode
*inode
, struct file
*file
)
1610 struct seq_file
*m
= file
->private_data
;
1611 struct lttng_id_tracker
*id_tracker
= m
->private;
1614 WARN_ON_ONCE(!id_tracker
);
1615 ret
= seq_release(inode
, file
);
1617 fput(id_tracker
->session
->file
);
1621 const struct file_operations lttng_tracker_ids_list_fops
= {
1622 .owner
= THIS_MODULE
,
1623 .open
= lttng_tracker_ids_list_open
,
1625 .llseek
= seq_lseek
,
1626 .release
= lttng_tracker_ids_list_release
,
1629 int lttng_session_list_tracker_ids(struct lttng_session
*session
,
1630 enum tracker_type tracker_type
)
1632 struct file
*tracker_ids_list_file
;
1636 file_fd
= lttng_get_unused_fd();
1642 tracker_ids_list_file
= anon_inode_getfile("[lttng_tracker_ids_list]",
1643 <tng_tracker_ids_list_fops
,
1645 if (IS_ERR(tracker_ids_list_file
)) {
1646 ret
= PTR_ERR(tracker_ids_list_file
);
1649 if (!atomic_long_add_unless(&session
->file
->f_count
, 1, LONG_MAX
)) {
1651 goto refcount_error
;
1653 ret
= lttng_tracker_ids_list_fops
.open(NULL
, tracker_ids_list_file
);
1656 m
= tracker_ids_list_file
->private_data
;
1658 m
->private = get_tracker(session
, tracker_type
);
1659 BUG_ON(!m
->private);
1660 fd_install(file_fd
, tracker_ids_list_file
);
1665 atomic_long_dec(&session
->file
->f_count
);
1667 fput(tracker_ids_list_file
);
1669 put_unused_fd(file_fd
);
1675 * Enabler management.
1678 int lttng_match_enabler_star_glob(const char *desc_name
,
1679 const char *pattern
)
1681 if (!strutils_star_glob_match(pattern
, LTTNG_SIZE_MAX
,
1682 desc_name
, LTTNG_SIZE_MAX
))
1688 int lttng_match_enabler_name(const char *desc_name
,
1691 if (strcmp(desc_name
, name
))
1696 int lttng_desc_match_enabler(const struct lttng_event_desc
*desc
,
1697 struct lttng_enabler
*enabler
)
1699 const char *desc_name
, *enabler_name
;
1700 bool compat
= false, entry
= false;
1702 enabler_name
= enabler
->event_param
.name
;
1703 switch (enabler
->event_param
.instrumentation
) {
1704 case LTTNG_KERNEL_TRACEPOINT
:
1705 desc_name
= desc
->name
;
1706 switch (enabler
->format_type
) {
1707 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
1708 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1709 case LTTNG_ENABLER_FORMAT_NAME
:
1710 return lttng_match_enabler_name(desc_name
, enabler_name
);
1715 case LTTNG_KERNEL_SYSCALL
:
1716 desc_name
= desc
->name
;
1717 if (!strncmp(desc_name
, "compat_", strlen("compat_"))) {
1718 desc_name
+= strlen("compat_");
1721 if (!strncmp(desc_name
, "syscall_exit_",
1722 strlen("syscall_exit_"))) {
1723 desc_name
+= strlen("syscall_exit_");
1724 } else if (!strncmp(desc_name
, "syscall_entry_",
1725 strlen("syscall_entry_"))) {
1726 desc_name
+= strlen("syscall_entry_");
1732 switch (enabler
->event_param
.u
.syscall
.entryexit
) {
1733 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT
:
1735 case LTTNG_KERNEL_SYSCALL_ENTRY
:
1739 case LTTNG_KERNEL_SYSCALL_EXIT
:
1746 switch (enabler
->event_param
.u
.syscall
.abi
) {
1747 case LTTNG_KERNEL_SYSCALL_ABI_ALL
:
1749 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE
:
1753 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT
:
1760 switch (enabler
->event_param
.u
.syscall
.match
) {
1761 case LTTNG_KERNEL_SYSCALL_MATCH_NAME
:
1762 switch (enabler
->format_type
) {
1763 case LTTNG_ENABLER_FORMAT_STAR_GLOB
:
1764 return lttng_match_enabler_star_glob(desc_name
, enabler_name
);
1765 case LTTNG_ENABLER_FORMAT_NAME
:
1766 return lttng_match_enabler_name(desc_name
, enabler_name
);
1771 case LTTNG_KERNEL_SYSCALL_MATCH_NR
:
1772 return -EINVAL
; /* Not implemented. */
1784 int lttng_event_enabler_match_event(struct lttng_event_enabler
*event_enabler
,
1785 struct lttng_event
*event
)
1787 struct lttng_enabler
*base_enabler
= lttng_event_enabler_as_enabler(
1790 if (base_enabler
->event_param
.instrumentation
!= event
->instrumentation
)
1792 if (lttng_desc_match_enabler(event
->desc
, base_enabler
)
1793 && event
->chan
== event_enabler
->chan
)
1800 int lttng_event_notifier_enabler_match_event_notifier(struct lttng_event_notifier_enabler
*event_notifier_enabler
,
1801 struct lttng_event_notifier
*event_notifier
)
1803 struct lttng_enabler
*base_enabler
= lttng_event_notifier_enabler_as_enabler(
1804 event_notifier_enabler
);
1806 if (base_enabler
->event_param
.instrumentation
!= event_notifier
->instrumentation
)
1808 if (lttng_desc_match_enabler(event_notifier
->desc
, base_enabler
)
1809 && event_notifier
->group
== event_notifier_enabler
->group
1810 && event_notifier
->user_token
== event_notifier_enabler
->base
.user_token
)
1817 struct lttng_enabler_ref
*lttng_enabler_ref(
1818 struct list_head
*enablers_ref_list
,
1819 struct lttng_enabler
*enabler
)
1821 struct lttng_enabler_ref
*enabler_ref
;
1823 list_for_each_entry(enabler_ref
, enablers_ref_list
, node
) {
1824 if (enabler_ref
->ref
== enabler
)
1831 void lttng_create_tracepoint_event_if_missing(struct lttng_event_enabler
*event_enabler
)
1833 struct lttng_session
*session
= event_enabler
->chan
->session
;
1834 struct lttng_probe_desc
*probe_desc
;
1835 const struct lttng_event_desc
*desc
;
1837 struct list_head
*probe_list
;
1839 probe_list
= lttng_get_probe_list_head();
1841 * For each probe event, if we find that a probe event matches
1842 * our enabler, create an associated lttng_event if not
1845 list_for_each_entry(probe_desc
, probe_list
, head
) {
1846 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1848 struct hlist_head
*head
;
1849 struct lttng_event
*event
;
1851 desc
= probe_desc
->event_desc
[i
];
1852 if (!lttng_desc_match_enabler(desc
,
1853 lttng_event_enabler_as_enabler(event_enabler
)))
1857 * Check if already created.
1859 head
= utils_borrow_hash_table_bucket(
1860 session
->events_ht
.table
, LTTNG_EVENT_HT_SIZE
,
1862 lttng_hlist_for_each_entry(event
, head
, hlist
) {
1863 if (event
->desc
== desc
1864 && event
->chan
== event_enabler
->chan
)
1871 * We need to create an event for this
1874 event
= _lttng_event_create(event_enabler
->chan
,
1876 LTTNG_KERNEL_TRACEPOINT
);
1878 printk(KERN_INFO
"LTTng: Unable to create event %s\n",
1879 probe_desc
->event_desc
[i
]->name
);
1886 void lttng_create_tracepoint_event_notifier_if_missing(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1888 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
1889 struct lttng_probe_desc
*probe_desc
;
1890 const struct lttng_event_desc
*desc
;
1892 struct list_head
*probe_list
;
1894 probe_list
= lttng_get_probe_list_head();
1896 * For each probe event, if we find that a probe event matches
1897 * our enabler, create an associated lttng_event_notifier if not
1900 list_for_each_entry(probe_desc
, probe_list
, head
) {
1901 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
1903 struct hlist_head
*head
;
1904 struct lttng_event_notifier
*event_notifier
;
1906 desc
= probe_desc
->event_desc
[i
];
1907 if (!lttng_desc_match_enabler(desc
,
1908 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)))
1912 * Check if already created.
1914 head
= utils_borrow_hash_table_bucket(
1915 event_notifier_group
->event_notifiers_ht
.table
,
1916 LTTNG_EVENT_NOTIFIER_HT_SIZE
, desc
->name
);
1917 lttng_hlist_for_each_entry(event_notifier
, head
, hlist
) {
1918 if (event_notifier
->desc
== desc
1919 && event_notifier
->user_token
== event_notifier_enabler
->base
.user_token
)
1926 * We need to create a event_notifier for this event probe.
1928 event_notifier
= _lttng_event_notifier_create(desc
,
1929 event_notifier_enabler
->base
.user_token
,
1930 event_notifier_group
, NULL
, NULL
,
1931 LTTNG_KERNEL_TRACEPOINT
);
1932 if (IS_ERR(event_notifier
)) {
1933 printk(KERN_INFO
"Unable to create event_notifier %s\n",
1934 probe_desc
->event_desc
[i
]->name
);
1941 void lttng_create_syscall_event_if_missing(struct lttng_event_enabler
*event_enabler
)
1945 ret
= lttng_syscalls_register_event(event_enabler
->chan
, NULL
);
1950 void lttng_create_syscall_event_notifier_if_missing(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
1954 ret
= lttng_syscalls_register_event_notifier(event_notifier_enabler
, NULL
);
1956 ret
= lttng_syscals_create_matching_event_notifiers(event_notifier_enabler
, NULL
);
1961 * Create struct lttng_event if it is missing and present in the list of
1962 * tracepoint probes.
1963 * Should be called with sessions mutex held.
1966 void lttng_create_event_if_missing(struct lttng_event_enabler
*event_enabler
)
1968 switch (event_enabler
->base
.event_param
.instrumentation
) {
1969 case LTTNG_KERNEL_TRACEPOINT
:
1970 lttng_create_tracepoint_event_if_missing(event_enabler
);
1972 case LTTNG_KERNEL_SYSCALL
:
1973 lttng_create_syscall_event_if_missing(event_enabler
);
1982 * Create events associated with an event_enabler (if not already present),
1983 * and add backward reference from the event to the enabler.
1984 * Should be called with sessions mutex held.
1987 int lttng_event_enabler_ref_events(struct lttng_event_enabler
*event_enabler
)
1989 struct lttng_channel
*chan
= event_enabler
->chan
;
1990 struct lttng_session
*session
= event_enabler
->chan
->session
;
1991 struct lttng_enabler
*base_enabler
= lttng_event_enabler_as_enabler(event_enabler
);
1992 struct lttng_event
*event
;
1994 if (base_enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
1995 base_enabler
->event_param
.u
.syscall
.entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
&&
1996 base_enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
1997 base_enabler
->event_param
.u
.syscall
.match
== LTTNG_KERNEL_SYSCALL_MATCH_NAME
&&
1998 !strcmp(base_enabler
->event_param
.name
, "*")) {
1999 if (base_enabler
->enabled
)
2000 WRITE_ONCE(chan
->syscall_all
, 1);
2002 WRITE_ONCE(chan
->syscall_all
, 0);
2005 /* First ensure that probe events are created for this enabler. */
2006 lttng_create_event_if_missing(event_enabler
);
2008 /* For each event matching event_enabler in session event list. */
2009 list_for_each_entry(event
, &session
->events
, list
) {
2010 struct lttng_enabler_ref
*enabler_ref
;
2012 if (!lttng_event_enabler_match_event(event_enabler
, event
))
2014 enabler_ref
= lttng_enabler_ref(&event
->enablers_ref_head
,
2015 lttng_event_enabler_as_enabler(event_enabler
));
2018 * If no backward ref, create it.
2019 * Add backward ref from event to event_enabler.
2021 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
2024 enabler_ref
->ref
= lttng_event_enabler_as_enabler(event_enabler
);
2025 list_add(&enabler_ref
->node
,
2026 &event
->enablers_ref_head
);
2030 * Link filter bytecodes if not linked yet.
2032 lttng_enabler_link_bytecode(event
->desc
,
2034 &event
->filter_bytecode_runtime_head
,
2035 lttng_event_enabler_as_enabler(event_enabler
));
2037 /* TODO: merge event context. */
2043 * Create struct lttng_event_notifier if it is missing and present in the list of
2044 * tracepoint probes.
2045 * Should be called with sessions mutex held.
2048 void lttng_create_event_notifier_if_missing(struct lttng_event_notifier_enabler
*event_notifier_enabler
)
2050 switch (event_notifier_enabler
->base
.event_param
.instrumentation
) {
2051 case LTTNG_KERNEL_TRACEPOINT
:
2052 lttng_create_tracepoint_event_notifier_if_missing(event_notifier_enabler
);
2054 case LTTNG_KERNEL_SYSCALL
:
2055 lttng_create_syscall_event_notifier_if_missing(event_notifier_enabler
);
2064 * Create event_notifiers associated with a event_notifier enabler (if not already present).
2067 int lttng_event_notifier_enabler_ref_event_notifiers(
2068 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
2070 struct lttng_event_notifier_group
*event_notifier_group
= event_notifier_enabler
->group
;
2071 struct lttng_enabler
*base_enabler
= lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
);
2072 struct lttng_event_notifier
*event_notifier
;
2074 if (base_enabler
->event_param
.instrumentation
== LTTNG_KERNEL_SYSCALL
&&
2075 base_enabler
->event_param
.u
.syscall
.abi
== LTTNG_KERNEL_SYSCALL_ABI_ALL
&&
2076 base_enabler
->event_param
.u
.syscall
.match
== LTTNG_KERNEL_SYSCALL_MATCH_NAME
&&
2077 !strcmp(base_enabler
->event_param
.name
, "*")) {
2079 int enabled
= base_enabler
->enabled
;
2080 enum lttng_kernel_syscall_entryexit entryexit
= base_enabler
->event_param
.u
.syscall
.entryexit
;
2082 if (entryexit
== LTTNG_KERNEL_SYSCALL_ENTRY
|| entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
)
2083 WRITE_ONCE(event_notifier_group
->syscall_all_entry
, enabled
);
2085 if (entryexit
== LTTNG_KERNEL_SYSCALL_EXIT
|| entryexit
== LTTNG_KERNEL_SYSCALL_ENTRYEXIT
)
2086 WRITE_ONCE(event_notifier_group
->syscall_all_exit
, enabled
);
2090 /* First ensure that probe event_notifiers are created for this enabler. */
2091 lttng_create_event_notifier_if_missing(event_notifier_enabler
);
2093 /* Link the created event_notifier with its associated enabler. */
2094 list_for_each_entry(event_notifier
, &event_notifier_group
->event_notifiers_head
, list
) {
2095 struct lttng_enabler_ref
*enabler_ref
;
2097 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler
, event_notifier
))
2100 enabler_ref
= lttng_enabler_ref(&event_notifier
->enablers_ref_head
,
2101 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
2104 * If no backward ref, create it.
2105 * Add backward ref from event_notifier to enabler.
2107 enabler_ref
= kzalloc(sizeof(*enabler_ref
), GFP_KERNEL
);
2111 enabler_ref
->ref
= lttng_event_notifier_enabler_as_enabler(
2112 event_notifier_enabler
);
2113 list_add(&enabler_ref
->node
,
2114 &event_notifier
->enablers_ref_head
);
2118 * Link filter bytecodes if not linked yet.
2120 lttng_enabler_link_bytecode(event_notifier
->desc
,
2121 lttng_static_ctx
, &event_notifier
->filter_bytecode_runtime_head
,
2122 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
2128 * Called at module load: connect the probe on all enablers matching
2130 * Called with sessions lock held.
2132 int lttng_fix_pending_events(void)
2134 struct lttng_session
*session
;
2136 list_for_each_entry(session
, &sessions
, list
)
2137 lttng_session_lazy_sync_event_enablers(session
);
2141 static bool lttng_event_notifier_group_has_active_event_notifiers(
2142 struct lttng_event_notifier_group
*event_notifier_group
)
2144 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
2146 list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
,
2148 if (event_notifier_enabler
->base
.enabled
)
2154 bool lttng_event_notifier_active(void)
2156 struct lttng_event_notifier_group
*event_notifier_group
;
2158 list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
) {
2159 if (lttng_event_notifier_group_has_active_event_notifiers(event_notifier_group
))
2165 int lttng_fix_pending_event_notifiers(void)
2167 struct lttng_event_notifier_group
*event_notifier_group
;
2169 list_for_each_entry(event_notifier_group
, &event_notifier_groups
, node
)
2170 lttng_event_notifier_group_sync_enablers(event_notifier_group
);
2174 struct lttng_event_enabler
*lttng_event_enabler_create(
2175 enum lttng_enabler_format_type format_type
,
2176 struct lttng_kernel_event
*event_param
,
2177 struct lttng_channel
*chan
)
2179 struct lttng_event_enabler
*event_enabler
;
2181 event_enabler
= kzalloc(sizeof(*event_enabler
), GFP_KERNEL
);
2184 event_enabler
->base
.format_type
= format_type
;
2185 INIT_LIST_HEAD(&event_enabler
->base
.filter_bytecode_head
);
2186 memcpy(&event_enabler
->base
.event_param
, event_param
,
2187 sizeof(event_enabler
->base
.event_param
));
2188 event_enabler
->chan
= chan
;
2190 event_enabler
->base
.enabled
= 0;
2191 event_enabler
->base
.evtype
= LTTNG_TYPE_ENABLER
;
2192 mutex_lock(&sessions_mutex
);
2193 list_add(&event_enabler
->node
, &event_enabler
->chan
->session
->enablers_head
);
2194 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
2195 mutex_unlock(&sessions_mutex
);
2196 return event_enabler
;
2199 int lttng_event_enabler_enable(struct lttng_event_enabler
*event_enabler
)
2201 mutex_lock(&sessions_mutex
);
2202 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 1;
2203 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
2204 mutex_unlock(&sessions_mutex
);
2208 int lttng_event_enabler_disable(struct lttng_event_enabler
*event_enabler
)
2210 mutex_lock(&sessions_mutex
);
2211 lttng_event_enabler_as_enabler(event_enabler
)->enabled
= 0;
2212 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
2213 mutex_unlock(&sessions_mutex
);
2218 int lttng_enabler_attach_filter_bytecode(struct lttng_enabler
*enabler
,
2219 struct lttng_kernel_filter_bytecode __user
*bytecode
)
2221 struct lttng_bytecode_node
*bytecode_node
;
2222 uint32_t bytecode_len
;
2225 ret
= get_user(bytecode_len
, &bytecode
->len
);
2228 bytecode_node
= kzalloc(sizeof(*bytecode_node
) + bytecode_len
,
2232 ret
= copy_from_user(&bytecode_node
->bc
, bytecode
,
2233 sizeof(*bytecode
) + bytecode_len
);
2237 bytecode_node
->type
= LTTNG_BYTECODE_NODE_TYPE_FILTER
;
2238 bytecode_node
->enabler
= enabler
;
2239 /* Enforce length based on allocated size */
2240 bytecode_node
->bc
.len
= bytecode_len
;
2241 list_add_tail(&bytecode_node
->node
, &enabler
->filter_bytecode_head
);
2246 kfree(bytecode_node
);
2250 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler
*event_enabler
,
2251 struct lttng_kernel_filter_bytecode __user
*bytecode
)
2254 ret
= lttng_enabler_attach_filter_bytecode(
2255 lttng_event_enabler_as_enabler(event_enabler
), bytecode
);
2259 lttng_session_lazy_sync_event_enablers(event_enabler
->chan
->session
);
2266 int lttng_event_add_callsite(struct lttng_event
*event
,
2267 struct lttng_kernel_event_callsite __user
*callsite
)
2270 switch (event
->instrumentation
) {
2271 case LTTNG_KERNEL_UPROBE
:
2272 return lttng_uprobes_event_add_callsite(event
, callsite
);
2278 int lttng_event_enabler_attach_context(struct lttng_event_enabler
*event_enabler
,
2279 struct lttng_kernel_context
*context_param
)
2285 void lttng_enabler_destroy(struct lttng_enabler
*enabler
)
2287 struct lttng_bytecode_node
*filter_node
, *tmp_filter_node
;
2289 /* Destroy filter bytecode */
2290 list_for_each_entry_safe(filter_node
, tmp_filter_node
,
2291 &enabler
->filter_bytecode_head
, node
) {
2297 void lttng_event_enabler_destroy(struct lttng_event_enabler
*event_enabler
)
2299 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler
));
2301 /* Destroy contexts */
2302 lttng_destroy_context(event_enabler
->ctx
);
2304 list_del(&event_enabler
->node
);
2305 kfree(event_enabler
);
2308 struct lttng_event_notifier_enabler
*lttng_event_notifier_enabler_create(
2309 struct lttng_event_notifier_group
*event_notifier_group
,
2310 enum lttng_enabler_format_type format_type
,
2311 struct lttng_kernel_event_notifier
*event_notifier_param
)
2313 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
2315 event_notifier_enabler
= kzalloc(sizeof(*event_notifier_enabler
), GFP_KERNEL
);
2316 if (!event_notifier_enabler
)
2319 event_notifier_enabler
->base
.format_type
= format_type
;
2320 INIT_LIST_HEAD(&event_notifier_enabler
->base
.filter_bytecode_head
);
2322 memcpy(&event_notifier_enabler
->base
.event_param
, &event_notifier_param
->event
,
2323 sizeof(event_notifier_enabler
->base
.event_param
));
2324 event_notifier_enabler
->base
.evtype
= LTTNG_TYPE_ENABLER
;
2326 event_notifier_enabler
->base
.enabled
= 0;
2327 event_notifier_enabler
->base
.user_token
= event_notifier_param
->event
.token
;
2328 event_notifier_enabler
->group
= event_notifier_group
;
2330 mutex_lock(&sessions_mutex
);
2331 list_add(&event_notifier_enabler
->node
, &event_notifier_enabler
->group
->enablers_head
);
2332 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
2334 mutex_unlock(&sessions_mutex
);
2336 return event_notifier_enabler
;
2339 int lttng_event_notifier_enabler_enable(
2340 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
2342 mutex_lock(&sessions_mutex
);
2343 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 1;
2344 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
2345 mutex_unlock(&sessions_mutex
);
2349 int lttng_event_notifier_enabler_disable(
2350 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
2352 mutex_lock(&sessions_mutex
);
2353 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
)->enabled
= 0;
2354 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
2355 mutex_unlock(&sessions_mutex
);
2359 int lttng_event_notifier_enabler_attach_filter_bytecode(
2360 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
2361 struct lttng_kernel_filter_bytecode __user
*bytecode
)
2365 ret
= lttng_enabler_attach_filter_bytecode(
2366 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
),
2371 lttng_event_notifier_group_sync_enablers(event_notifier_enabler
->group
);
2378 int lttng_event_notifier_add_callsite(struct lttng_event_notifier
*event_notifier
,
2379 struct lttng_kernel_event_callsite __user
*callsite
)
2382 switch (event_notifier
->instrumentation
) {
2383 case LTTNG_KERNEL_UPROBE
:
2384 return lttng_uprobes_event_notifier_add_callsite(event_notifier
,
2391 int lttng_event_notifier_enabler_attach_context(
2392 struct lttng_event_notifier_enabler
*event_notifier_enabler
,
2393 struct lttng_kernel_context
*context_param
)
2399 void lttng_event_notifier_enabler_destroy(
2400 struct lttng_event_notifier_enabler
*event_notifier_enabler
)
2402 if (!event_notifier_enabler
) {
2406 list_del(&event_notifier_enabler
->node
);
2408 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler
));
2409 kfree(event_notifier_enabler
);
2413 * lttng_session_sync_event_enablers should be called just before starting a
2415 * Should be called with sessions mutex held.
2418 void lttng_session_sync_event_enablers(struct lttng_session
*session
)
2420 struct lttng_event_enabler
*event_enabler
;
2421 struct lttng_event
*event
;
2423 list_for_each_entry(event_enabler
, &session
->enablers_head
, node
)
2424 lttng_event_enabler_ref_events(event_enabler
);
2426 * For each event, if at least one of its enablers is enabled,
2427 * and its channel and session transient states are enabled, we
2428 * enable the event, else we disable it.
2430 list_for_each_entry(event
, &session
->events
, list
) {
2431 struct lttng_enabler_ref
*enabler_ref
;
2432 struct lttng_bytecode_runtime
*runtime
;
2433 int enabled
= 0, has_enablers_without_bytecode
= 0;
2435 switch (event
->instrumentation
) {
2436 case LTTNG_KERNEL_TRACEPOINT
:
2437 case LTTNG_KERNEL_SYSCALL
:
2439 list_for_each_entry(enabler_ref
,
2440 &event
->enablers_ref_head
, node
) {
2441 if (enabler_ref
->ref
->enabled
) {
2448 /* Not handled with lazy sync. */
2452 * Enabled state is based on union of enablers, with
2453 * intesection of session and channel transient enable
2456 enabled
= enabled
&& session
->tstate
&& event
->chan
->tstate
;
2458 WRITE_ONCE(event
->enabled
, enabled
);
2460 * Sync tracepoint registration with event enabled
2464 register_event(event
);
2466 _lttng_event_unregister(event
);
2469 /* Check if has enablers without bytecode enabled */
2470 list_for_each_entry(enabler_ref
,
2471 &event
->enablers_ref_head
, node
) {
2472 if (enabler_ref
->ref
->enabled
2473 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
2474 has_enablers_without_bytecode
= 1;
2478 event
->has_enablers_without_bytecode
=
2479 has_enablers_without_bytecode
;
2481 /* Enable filters */
2482 list_for_each_entry(runtime
,
2483 &event
->filter_bytecode_runtime_head
, node
)
2484 lttng_bytecode_filter_sync_state(runtime
);
2489 * Apply enablers to session events, adding events to session if need
2490 * be. It is required after each modification applied to an active
2491 * session, and right before session "start".
2492 * "lazy" sync means we only sync if required.
2493 * Should be called with sessions mutex held.
2496 void lttng_session_lazy_sync_event_enablers(struct lttng_session
*session
)
2498 /* We can skip if session is not active */
2499 if (!session
->active
)
2501 lttng_session_sync_event_enablers(session
);
2505 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group
*event_notifier_group
)
2507 struct lttng_event_notifier_enabler
*event_notifier_enabler
;
2508 struct lttng_event_notifier
*event_notifier
;
2510 list_for_each_entry(event_notifier_enabler
, &event_notifier_group
->enablers_head
, node
)
2511 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler
);
2514 * For each event_notifier, if at least one of its enablers is enabled,
2515 * we enable the event_notifier, else we disable it.
2517 list_for_each_entry(event_notifier
, &event_notifier_group
->event_notifiers_head
, list
) {
2518 struct lttng_enabler_ref
*enabler_ref
;
2519 struct lttng_bytecode_runtime
*runtime
;
2520 int enabled
= 0, has_enablers_without_bytecode
= 0;
2522 switch (event_notifier
->instrumentation
) {
2523 case LTTNG_KERNEL_TRACEPOINT
:
2524 case LTTNG_KERNEL_SYSCALL
:
2525 /* Enable event_notifiers */
2526 list_for_each_entry(enabler_ref
,
2527 &event_notifier
->enablers_ref_head
, node
) {
2528 if (enabler_ref
->ref
->enabled
) {
2535 /* Not handled with sync. */
2539 WRITE_ONCE(event_notifier
->enabled
, enabled
);
2541 * Sync tracepoint registration with event_notifier enabled
2545 if (!event_notifier
->registered
)
2546 register_event_notifier(event_notifier
);
2548 if (event_notifier
->registered
)
2549 _lttng_event_notifier_unregister(event_notifier
);
2552 /* Check if has enablers without bytecode enabled */
2553 list_for_each_entry(enabler_ref
,
2554 &event_notifier
->enablers_ref_head
, node
) {
2555 if (enabler_ref
->ref
->enabled
2556 && list_empty(&enabler_ref
->ref
->filter_bytecode_head
)) {
2557 has_enablers_without_bytecode
= 1;
2561 event_notifier
->has_enablers_without_bytecode
=
2562 has_enablers_without_bytecode
;
2564 /* Enable filters */
2565 list_for_each_entry(runtime
,
2566 &event_notifier
->filter_bytecode_runtime_head
, node
)
2567 lttng_bytecode_filter_sync_state(runtime
);
2572 * Serialize at most one packet worth of metadata into a metadata
2574 * We grab the metadata cache mutex to get exclusive access to our metadata
2575 * buffer and to the metadata cache. Exclusive access to the metadata buffer
2576 * allows us to do racy operations such as looking for remaining space left in
2577 * packet and write, since mutual exclusion protects us from concurrent writes.
2578 * Mutual exclusion on the metadata cache allow us to read the cache content
2579 * without racing against reallocation of the cache by updates.
2580 * Returns the number of bytes written in the channel, 0 if no data
2581 * was written and a negative value on error.
2583 int lttng_metadata_output_channel(struct lttng_metadata_stream
*stream
,
2584 struct channel
*chan
, bool *coherent
)
2586 struct lib_ring_buffer_ctx ctx
;
2588 size_t len
, reserve_len
;
2591 * Ensure we support mutiple get_next / put sequences followed by
2592 * put_next. The metadata cache lock protects reading the metadata
2593 * cache. It can indeed be read concurrently by "get_next_subbuf" and
2594 * "flush" operations on the buffer invoked by different processes.
2595 * Moreover, since the metadata cache memory can be reallocated, we
2596 * need to have exclusive access against updates even though we only
2599 mutex_lock(&stream
->metadata_cache
->lock
);
2600 WARN_ON(stream
->metadata_in
< stream
->metadata_out
);
2601 if (stream
->metadata_in
!= stream
->metadata_out
)
2604 /* Metadata regenerated, change the version. */
2605 if (stream
->metadata_cache
->version
!= stream
->version
)
2606 stream
->version
= stream
->metadata_cache
->version
;
2608 len
= stream
->metadata_cache
->metadata_written
-
2609 stream
->metadata_in
;
2612 reserve_len
= min_t(size_t,
2613 stream
->transport
->ops
.packet_avail_size(chan
),
2615 lib_ring_buffer_ctx_init(&ctx
, chan
, NULL
, reserve_len
,
2618 * If reservation failed, return an error to the caller.
2620 ret
= stream
->transport
->ops
.event_reserve(&ctx
, 0);
2622 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
2623 stream
->coherent
= false;
2626 stream
->transport
->ops
.event_write(&ctx
,
2627 stream
->metadata_cache
->data
+ stream
->metadata_in
,
2629 stream
->transport
->ops
.event_commit(&ctx
);
2630 stream
->metadata_in
+= reserve_len
;
2631 if (reserve_len
< len
)
2632 stream
->coherent
= false;
2634 stream
->coherent
= true;
2639 *coherent
= stream
->coherent
;
2640 mutex_unlock(&stream
->metadata_cache
->lock
);
2645 void lttng_metadata_begin(struct lttng_session
*session
)
2647 if (atomic_inc_return(&session
->metadata_cache
->producing
) == 1)
2648 mutex_lock(&session
->metadata_cache
->lock
);
2652 void lttng_metadata_end(struct lttng_session
*session
)
2654 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
2655 if (atomic_dec_return(&session
->metadata_cache
->producing
) == 0) {
2656 struct lttng_metadata_stream
*stream
;
2658 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
2659 wake_up_interruptible(&stream
->read_wait
);
2660 mutex_unlock(&session
->metadata_cache
->lock
);
2665 * Write the metadata to the metadata cache.
2666 * Must be called with sessions_mutex held.
2667 * The metadata cache lock protects us from concurrent read access from
2668 * thread outputting metadata content to ring buffer.
2669 * The content of the printf is printed as a single atomic metadata
2672 int lttng_metadata_printf(struct lttng_session
*session
,
2673 const char *fmt
, ...)
2679 WARN_ON_ONCE(!LTTNG_READ_ONCE(session
->active
));
2682 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
2688 WARN_ON_ONCE(!atomic_read(&session
->metadata_cache
->producing
));
2689 if (session
->metadata_cache
->metadata_written
+ len
>
2690 session
->metadata_cache
->cache_alloc
) {
2691 char *tmp_cache_realloc
;
2692 unsigned int tmp_cache_alloc_size
;
2694 tmp_cache_alloc_size
= max_t(unsigned int,
2695 session
->metadata_cache
->cache_alloc
+ len
,
2696 session
->metadata_cache
->cache_alloc
<< 1);
2697 tmp_cache_realloc
= vzalloc(tmp_cache_alloc_size
);
2698 if (!tmp_cache_realloc
)
2700 if (session
->metadata_cache
->data
) {
2701 memcpy(tmp_cache_realloc
,
2702 session
->metadata_cache
->data
,
2703 session
->metadata_cache
->cache_alloc
);
2704 vfree(session
->metadata_cache
->data
);
2707 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
2708 session
->metadata_cache
->data
= tmp_cache_realloc
;
2710 memcpy(session
->metadata_cache
->data
+
2711 session
->metadata_cache
->metadata_written
,
2713 session
->metadata_cache
->metadata_written
+= len
;
2724 int print_tabs(struct lttng_session
*session
, size_t nesting
)
2728 for (i
= 0; i
< nesting
; i
++) {
2731 ret
= lttng_metadata_printf(session
, " ");
2740 int lttng_field_name_statedump(struct lttng_session
*session
,
2741 const struct lttng_event_field
*field
,
2744 return lttng_metadata_printf(session
, " _%s;\n", field
->name
);
2748 int _lttng_integer_type_statedump(struct lttng_session
*session
,
2749 const struct lttng_type
*type
,
2754 WARN_ON_ONCE(type
->atype
!= atype_integer
);
2755 ret
= print_tabs(session
, nesting
);
2758 ret
= lttng_metadata_printf(session
,
2759 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }",
2760 type
->u
.integer
.size
,
2761 type
->u
.integer
.alignment
,
2762 type
->u
.integer
.signedness
,
2763 (type
->u
.integer
.encoding
== lttng_encode_none
)
2765 : (type
->u
.integer
.encoding
== lttng_encode_UTF8
)
2768 type
->u
.integer
.base
,
2769 #if __BYTE_ORDER == __BIG_ENDIAN
2770 type
->u
.integer
.reverse_byte_order
? " byte_order = le;" : ""
2772 type
->u
.integer
.reverse_byte_order
? " byte_order = be;" : ""
2779 * Must be called with sessions_mutex held.
2782 int _lttng_struct_type_statedump(struct lttng_session
*session
,
2783 const struct lttng_type
*type
,
2787 uint32_t i
, nr_fields
;
2788 unsigned int alignment
;
2790 WARN_ON_ONCE(type
->atype
!= atype_struct_nestable
);
2792 ret
= print_tabs(session
, nesting
);
2795 ret
= lttng_metadata_printf(session
,
2799 nr_fields
= type
->u
.struct_nestable
.nr_fields
;
2800 for (i
= 0; i
< nr_fields
; i
++) {
2801 const struct lttng_event_field
*iter_field
;
2803 iter_field
= &type
->u
.struct_nestable
.fields
[i
];
2804 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
2808 ret
= print_tabs(session
, nesting
);
2811 alignment
= type
->u
.struct_nestable
.alignment
;
2813 ret
= lttng_metadata_printf(session
,
2817 ret
= lttng_metadata_printf(session
,
2824 * Must be called with sessions_mutex held.
2827 int _lttng_struct_field_statedump(struct lttng_session
*session
,
2828 const struct lttng_event_field
*field
,
2833 ret
= _lttng_struct_type_statedump(session
,
2834 &field
->type
, nesting
);
2837 return lttng_field_name_statedump(session
, field
, nesting
);
2841 * Must be called with sessions_mutex held.
2844 int _lttng_variant_type_statedump(struct lttng_session
*session
,
2845 const struct lttng_type
*type
,
2849 uint32_t i
, nr_choices
;
2851 WARN_ON_ONCE(type
->atype
!= atype_variant_nestable
);
2853 * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way.
2855 if (type
->u
.variant_nestable
.alignment
!= 0)
2857 ret
= print_tabs(session
, nesting
);
2860 ret
= lttng_metadata_printf(session
,
2861 "variant <_%s> {\n",
2862 type
->u
.variant_nestable
.tag_name
);
2865 nr_choices
= type
->u
.variant_nestable
.nr_choices
;
2866 for (i
= 0; i
< nr_choices
; i
++) {
2867 const struct lttng_event_field
*iter_field
;
2869 iter_field
= &type
->u
.variant_nestable
.choices
[i
];
2870 ret
= _lttng_field_statedump(session
, iter_field
, nesting
+ 1);
2874 ret
= print_tabs(session
, nesting
);
2877 ret
= lttng_metadata_printf(session
,
2883 * Must be called with sessions_mutex held.
2886 int _lttng_variant_field_statedump(struct lttng_session
*session
,
2887 const struct lttng_event_field
*field
,
2892 ret
= _lttng_variant_type_statedump(session
,
2893 &field
->type
, nesting
);
2896 return lttng_field_name_statedump(session
, field
, nesting
);
2900 * Must be called with sessions_mutex held.
2903 int _lttng_array_field_statedump(struct lttng_session
*session
,
2904 const struct lttng_event_field
*field
,
2908 const struct lttng_type
*elem_type
;
2910 WARN_ON_ONCE(field
->type
.atype
!= atype_array_nestable
);
2912 if (field
->type
.u
.array_nestable
.alignment
) {
2913 ret
= print_tabs(session
, nesting
);
2916 ret
= lttng_metadata_printf(session
,
2917 "struct { } align(%u) _%s_padding;\n",
2918 field
->type
.u
.array_nestable
.alignment
* CHAR_BIT
,
2924 * Nested compound types: Only array of structures and variants are
2925 * currently supported.
2927 elem_type
= field
->type
.u
.array_nestable
.elem_type
;
2928 switch (elem_type
->atype
) {
2930 case atype_struct_nestable
:
2931 case atype_variant_nestable
:
2932 ret
= _lttng_type_statedump(session
, elem_type
, nesting
);
2940 ret
= lttng_metadata_printf(session
,
2943 field
->type
.u
.array_nestable
.length
);
2948 * Must be called with sessions_mutex held.
2951 int _lttng_sequence_field_statedump(struct lttng_session
*session
,
2952 const struct lttng_event_field
*field
,
2956 const char *length_name
;
2957 const struct lttng_type
*elem_type
;
2959 WARN_ON_ONCE(field
->type
.atype
!= atype_sequence_nestable
);
2961 length_name
= field
->type
.u
.sequence_nestable
.length_name
;
2963 if (field
->type
.u
.sequence_nestable
.alignment
) {
2964 ret
= print_tabs(session
, nesting
);
2967 ret
= lttng_metadata_printf(session
,
2968 "struct { } align(%u) _%s_padding;\n",
2969 field
->type
.u
.sequence_nestable
.alignment
* CHAR_BIT
,
2976 * Nested compound types: Only array of structures and variants are
2977 * currently supported.
2979 elem_type
= field
->type
.u
.sequence_nestable
.elem_type
;
2980 switch (elem_type
->atype
) {
2982 case atype_struct_nestable
:
2983 case atype_variant_nestable
:
2984 ret
= _lttng_type_statedump(session
, elem_type
, nesting
);
2992 ret
= lttng_metadata_printf(session
,
2995 field
->type
.u
.sequence_nestable
.length_name
);
3000 * Must be called with sessions_mutex held.
3003 int _lttng_enum_type_statedump(struct lttng_session
*session
,
3004 const struct lttng_type
*type
,
3007 const struct lttng_enum_desc
*enum_desc
;
3008 const struct lttng_type
*container_type
;
3010 unsigned int i
, nr_entries
;
3012 container_type
= type
->u
.enum_nestable
.container_type
;
3013 if (container_type
->atype
!= atype_integer
) {
3017 enum_desc
= type
->u
.enum_nestable
.desc
;
3018 nr_entries
= enum_desc
->nr_entries
;
3020 ret
= print_tabs(session
, nesting
);
3023 ret
= lttng_metadata_printf(session
, "enum : ");
3026 ret
= _lttng_integer_type_statedump(session
, container_type
, 0);
3029 ret
= lttng_metadata_printf(session
, " {\n");
3032 /* Dump all entries */
3033 for (i
= 0; i
< nr_entries
; i
++) {
3034 const struct lttng_enum_entry
*entry
= &enum_desc
->entries
[i
];
3037 ret
= print_tabs(session
, nesting
+ 1);
3040 ret
= lttng_metadata_printf(session
,
3044 len
= strlen(entry
->string
);
3045 /* Escape the character '"' */
3046 for (j
= 0; j
< len
; j
++) {
3047 char c
= entry
->string
[j
];
3051 ret
= lttng_metadata_printf(session
,
3055 ret
= lttng_metadata_printf(session
,
3059 ret
= lttng_metadata_printf(session
,
3066 ret
= lttng_metadata_printf(session
, "\"");
3070 if (entry
->options
.is_auto
) {
3071 ret
= lttng_metadata_printf(session
, ",\n");
3075 ret
= lttng_metadata_printf(session
,
3079 if (entry
->start
.signedness
)
3080 ret
= lttng_metadata_printf(session
,
3081 "%lld", (long long) entry
->start
.value
);
3083 ret
= lttng_metadata_printf(session
,
3084 "%llu", entry
->start
.value
);
3087 if (entry
->start
.signedness
== entry
->end
.signedness
&&
3089 == entry
->end
.value
) {
3090 ret
= lttng_metadata_printf(session
,
3093 if (entry
->end
.signedness
) {
3094 ret
= lttng_metadata_printf(session
,
3096 (long long) entry
->end
.value
);
3098 ret
= lttng_metadata_printf(session
,
3107 ret
= print_tabs(session
, nesting
);
3110 ret
= lttng_metadata_printf(session
, "}");
3116 * Must be called with sessions_mutex held.
3119 int _lttng_enum_field_statedump(struct lttng_session
*session
,
3120 const struct lttng_event_field
*field
,
3125 ret
= _lttng_enum_type_statedump(session
, &field
->type
, nesting
);
3128 return lttng_field_name_statedump(session
, field
, nesting
);
3132 int _lttng_integer_field_statedump(struct lttng_session
*session
,
3133 const struct lttng_event_field
*field
,
3138 ret
= _lttng_integer_type_statedump(session
, &field
->type
, nesting
);
3141 return lttng_field_name_statedump(session
, field
, nesting
);
3145 int _lttng_string_type_statedump(struct lttng_session
*session
,
3146 const struct lttng_type
*type
,
3151 WARN_ON_ONCE(type
->atype
!= atype_string
);
3152 /* Default encoding is UTF8 */
3153 ret
= print_tabs(session
, nesting
);
3156 ret
= lttng_metadata_printf(session
,
3158 type
->u
.string
.encoding
== lttng_encode_ASCII
?
3159 " { encoding = ASCII; }" : "");
3164 int _lttng_string_field_statedump(struct lttng_session
*session
,
3165 const struct lttng_event_field
*field
,
3170 WARN_ON_ONCE(field
->type
.atype
!= atype_string
);
3171 ret
= _lttng_string_type_statedump(session
, &field
->type
, nesting
);
3174 return lttng_field_name_statedump(session
, field
, nesting
);
3178 * Must be called with sessions_mutex held.
3181 int _lttng_type_statedump(struct lttng_session
*session
,
3182 const struct lttng_type
*type
,
3187 switch (type
->atype
) {
3189 ret
= _lttng_integer_type_statedump(session
, type
, nesting
);
3191 case atype_enum_nestable
:
3192 ret
= _lttng_enum_type_statedump(session
, type
, nesting
);
3195 ret
= _lttng_string_type_statedump(session
, type
, nesting
);
3197 case atype_struct_nestable
:
3198 ret
= _lttng_struct_type_statedump(session
, type
, nesting
);
3200 case atype_variant_nestable
:
3201 ret
= _lttng_variant_type_statedump(session
, type
, nesting
);
3204 /* Nested arrays and sequences are not supported yet. */
3205 case atype_array_nestable
:
3206 case atype_sequence_nestable
:
3215 * Must be called with sessions_mutex held.
3218 int _lttng_field_statedump(struct lttng_session
*session
,
3219 const struct lttng_event_field
*field
,
3224 switch (field
->type
.atype
) {
3226 ret
= _lttng_integer_field_statedump(session
, field
, nesting
);
3228 case atype_enum_nestable
:
3229 ret
= _lttng_enum_field_statedump(session
, field
, nesting
);
3232 ret
= _lttng_string_field_statedump(session
, field
, nesting
);
3234 case atype_struct_nestable
:
3235 ret
= _lttng_struct_field_statedump(session
, field
, nesting
);
3237 case atype_array_nestable
:
3238 ret
= _lttng_array_field_statedump(session
, field
, nesting
);
3240 case atype_sequence_nestable
:
3241 ret
= _lttng_sequence_field_statedump(session
, field
, nesting
);
3243 case atype_variant_nestable
:
3244 ret
= _lttng_variant_field_statedump(session
, field
, nesting
);
3255 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
3256 struct lttng_ctx
*ctx
)
3263 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
3264 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
3266 ret
= _lttng_field_statedump(session
, &field
->event_field
, 2);
3274 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
3275 struct lttng_event
*event
)
3277 const struct lttng_event_desc
*desc
= event
->desc
;
3281 for (i
= 0; i
< desc
->nr_fields
; i
++) {
3282 const struct lttng_event_field
*field
= &desc
->fields
[i
];
3284 ret
= _lttng_field_statedump(session
, field
, 2);
3292 * Must be called with sessions_mutex held.
3293 * The entire event metadata is printed as a single atomic metadata
3297 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
3298 struct lttng_channel
*chan
,
3299 struct lttng_event
*event
)
3303 if (event
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
3305 if (chan
->channel_type
== METADATA_CHANNEL
)
3308 lttng_metadata_begin(session
);
3310 ret
= lttng_metadata_printf(session
,
3314 " stream_id = %u;\n",
3322 ret
= lttng_metadata_printf(session
,
3323 " context := struct {\n");
3327 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
3331 ret
= lttng_metadata_printf(session
,
3337 ret
= lttng_metadata_printf(session
,
3338 " fields := struct {\n"
3343 ret
= _lttng_fields_metadata_statedump(session
, event
);
3348 * LTTng space reservation can only reserve multiples of the
3351 ret
= lttng_metadata_printf(session
,
3357 event
->metadata_dumped
= 1;
3359 lttng_metadata_end(session
);
3365 * Must be called with sessions_mutex held.
3366 * The entire channel metadata is printed as a single atomic metadata
3370 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
3371 struct lttng_channel
*chan
)
3375 if (chan
->metadata_dumped
|| !LTTNG_READ_ONCE(session
->active
))
3378 if (chan
->channel_type
== METADATA_CHANNEL
)
3381 lttng_metadata_begin(session
);
3383 WARN_ON_ONCE(!chan
->header_type
);
3384 ret
= lttng_metadata_printf(session
,
3387 " event.header := %s;\n"
3388 " packet.context := struct packet_context;\n",
3390 chan
->header_type
== 1 ? "struct event_header_compact" :
3391 "struct event_header_large");
3396 ret
= lttng_metadata_printf(session
,
3397 " event.context := struct {\n");
3401 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
3405 ret
= lttng_metadata_printf(session
,
3411 ret
= lttng_metadata_printf(session
,
3414 chan
->metadata_dumped
= 1;
3416 lttng_metadata_end(session
);
3421 * Must be called with sessions_mutex held.
3424 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
3426 return lttng_metadata_printf(session
,
3427 "struct packet_context {\n"
3428 " uint64_clock_monotonic_t timestamp_begin;\n"
3429 " uint64_clock_monotonic_t timestamp_end;\n"
3430 " uint64_t content_size;\n"
3431 " uint64_t packet_size;\n"
3432 " uint64_t packet_seq_num;\n"
3433 " unsigned long events_discarded;\n"
3434 " uint32_t cpu_id;\n"
3441 * id: range: 0 - 30.
3442 * id 31 is reserved to indicate an extended header.
3445 * id: range: 0 - 65534.
3446 * id 65535 is reserved to indicate an extended header.
3448 * Must be called with sessions_mutex held.
3451 int _lttng_event_header_declare(struct lttng_session
*session
)
3453 return lttng_metadata_printf(session
,
3454 "struct event_header_compact {\n"
3455 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
3458 " uint27_clock_monotonic_t timestamp;\n"
3462 " uint64_clock_monotonic_t timestamp;\n"
3467 "struct event_header_large {\n"
3468 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
3471 " uint32_clock_monotonic_t timestamp;\n"
3475 " uint64_clock_monotonic_t timestamp;\n"
3479 lttng_alignof(uint32_t) * CHAR_BIT
,
3480 lttng_alignof(uint16_t) * CHAR_BIT
3485 * Approximation of NTP time of day to clock monotonic correlation,
3486 * taken at start of trace.
3487 * Yes, this is only an approximation. Yes, we can (and will) do better
3488 * in future versions.
3489 * This function may return a negative offset. It may happen if the
3490 * system sets the REALTIME clock to 0 after boot.
3492 * Use 64bit timespec on kernels that have it, this makes 32bit arch
3496 int64_t measure_clock_offset(void)
3498 uint64_t monotonic_avg
, monotonic
[2], realtime
;
3499 uint64_t tcf
= trace_clock_freq();
3501 unsigned long flags
;
3502 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
3503 struct timespec64 rts
= { 0, 0 };
3505 struct timespec rts
= { 0, 0 };
3508 /* Disable interrupts to increase correlation precision. */
3509 local_irq_save(flags
);
3510 monotonic
[0] = trace_clock_read64();
3511 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
3512 ktime_get_real_ts64(&rts
);
3514 getnstimeofday(&rts
);
3516 monotonic
[1] = trace_clock_read64();
3517 local_irq_restore(flags
);
3519 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
3520 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
3521 if (tcf
== NSEC_PER_SEC
) {
3522 realtime
+= rts
.tv_nsec
;
3524 uint64_t n
= rts
.tv_nsec
* tcf
;
3526 do_div(n
, NSEC_PER_SEC
);
3529 offset
= (int64_t) realtime
- monotonic_avg
;
3534 int print_escaped_ctf_string(struct lttng_session
*session
, const char *string
)
3542 while (cur
!= '\0') {
3545 ret
= lttng_metadata_printf(session
, "%s", "\\n");
3549 ret
= lttng_metadata_printf(session
, "%c", '\\');
3552 /* We still print the current char */
3555 ret
= lttng_metadata_printf(session
, "%c", cur
);
3569 int print_metadata_escaped_field(struct lttng_session
*session
, const char *field
,
3570 const char *field_value
)
3574 ret
= lttng_metadata_printf(session
, " %s = \"", field
);
3578 ret
= print_escaped_ctf_string(session
, field_value
);
3582 ret
= lttng_metadata_printf(session
, "\";\n");
3589 * Output metadata into this session's metadata buffers.
3590 * Must be called with sessions_mutex held.
3593 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
3595 unsigned char *uuid_c
= session
->uuid
.b
;
3596 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
3597 const char *product_uuid
;
3598 struct lttng_channel
*chan
;
3599 struct lttng_event
*event
;
3602 if (!LTTNG_READ_ONCE(session
->active
))
3605 lttng_metadata_begin(session
);
3607 if (session
->metadata_dumped
)
3610 snprintf(uuid_s
, sizeof(uuid_s
),
3611 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
3612 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
3613 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
3614 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
3615 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
3617 ret
= lttng_metadata_printf(session
,
3618 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
3619 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
3620 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
3621 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
3622 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
3623 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
3624 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
3630 " byte_order = %s;\n"
3631 " packet.header := struct {\n"
3632 " uint32_t magic;\n"
3633 " uint8_t uuid[16];\n"
3634 " uint32_t stream_id;\n"
3635 " uint64_t stream_instance_id;\n"
3638 lttng_alignof(uint8_t) * CHAR_BIT
,
3639 lttng_alignof(uint16_t) * CHAR_BIT
,
3640 lttng_alignof(uint32_t) * CHAR_BIT
,
3641 lttng_alignof(uint64_t) * CHAR_BIT
,
3642 sizeof(unsigned long) * CHAR_BIT
,
3643 lttng_alignof(unsigned long) * CHAR_BIT
,
3647 #if __BYTE_ORDER == __BIG_ENDIAN
3656 ret
= lttng_metadata_printf(session
,
3658 " hostname = \"%s\";\n"
3659 " domain = \"kernel\";\n"
3660 " sysname = \"%s\";\n"
3661 " kernel_release = \"%s\";\n"
3662 " kernel_version = \"%s\";\n"
3663 " tracer_name = \"lttng-modules\";\n"
3664 " tracer_major = %d;\n"
3665 " tracer_minor = %d;\n"
3666 " tracer_patchlevel = %d;\n"
3667 " trace_buffering_scheme = \"global\";\n",
3668 current
->nsproxy
->uts_ns
->name
.nodename
,
3672 LTTNG_MODULES_MAJOR_VERSION
,
3673 LTTNG_MODULES_MINOR_VERSION
,
3674 LTTNG_MODULES_PATCHLEVEL_VERSION
3679 ret
= print_metadata_escaped_field(session
, "trace_name", session
->name
);
3682 ret
= print_metadata_escaped_field(session
, "trace_creation_datetime",
3683 session
->creation_time
);
3687 /* Add the product UUID to the 'env' section */
3688 product_uuid
= dmi_get_system_info(DMI_PRODUCT_UUID
);
3690 ret
= lttng_metadata_printf(session
,
3691 " product_uuid = \"%s\";\n",
3698 /* Close the 'env' section */
3699 ret
= lttng_metadata_printf(session
, "};\n\n");
3703 ret
= lttng_metadata_printf(session
,
3705 " name = \"%s\";\n",
3711 if (!trace_clock_uuid(clock_uuid_s
)) {
3712 ret
= lttng_metadata_printf(session
,
3713 " uuid = \"%s\";\n",
3720 ret
= lttng_metadata_printf(session
,
3721 " description = \"%s\";\n"
3722 " freq = %llu; /* Frequency, in Hz */\n"
3723 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
3726 trace_clock_description(),
3727 (unsigned long long) trace_clock_freq(),
3728 (long long) measure_clock_offset()
3733 ret
= lttng_metadata_printf(session
,
3734 "typealias integer {\n"
3735 " size = 27; align = 1; signed = false;\n"
3736 " map = clock.%s.value;\n"
3737 "} := uint27_clock_monotonic_t;\n"
3739 "typealias integer {\n"
3740 " size = 32; align = %u; signed = false;\n"
3741 " map = clock.%s.value;\n"
3742 "} := uint32_clock_monotonic_t;\n"
3744 "typealias integer {\n"
3745 " size = 64; align = %u; signed = false;\n"
3746 " map = clock.%s.value;\n"
3747 "} := uint64_clock_monotonic_t;\n\n",
3749 lttng_alignof(uint32_t) * CHAR_BIT
,
3751 lttng_alignof(uint64_t) * CHAR_BIT
,
3757 ret
= _lttng_stream_packet_context_declare(session
);
3761 ret
= _lttng_event_header_declare(session
);
3766 list_for_each_entry(chan
, &session
->chan
, list
) {
3767 ret
= _lttng_channel_metadata_statedump(session
, chan
);
3772 list_for_each_entry(event
, &session
->events
, list
) {
3773 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
3777 session
->metadata_dumped
= 1;
3779 lttng_metadata_end(session
);
3784 * lttng_transport_register - LTT transport registration
3785 * @transport: transport structure
3787 * Registers a transport which can be used as output to extract the data out of
3788 * LTTng. The module calling this registration function must ensure that no
3789 * trap-inducing code will be executed by the transport functions. E.g.
3790 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
3791 * is made visible to the transport function. This registration acts as a
3792 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
3793 * after its registration must it synchronize the TLBs.
3795 void lttng_transport_register(struct lttng_transport
*transport
)
3798 * Make sure no page fault can be triggered by the module about to be
3799 * registered. We deal with this here so we don't have to call
3800 * vmalloc_sync_mappings() in each module's init.
3802 wrapper_vmalloc_sync_mappings();
3804 mutex_lock(&sessions_mutex
);
3805 list_add_tail(&transport
->node
, <tng_transport_list
);
3806 mutex_unlock(&sessions_mutex
);
3808 EXPORT_SYMBOL_GPL(lttng_transport_register
);
3811 * lttng_transport_unregister - LTT transport unregistration
3812 * @transport: transport structure
3814 void lttng_transport_unregister(struct lttng_transport
*transport
)
3816 mutex_lock(&sessions_mutex
);
3817 list_del(&transport
->node
);
3818 mutex_unlock(&sessions_mutex
);
3820 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
3822 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
3824 enum cpuhp_state lttng_hp_prepare
;
3825 enum cpuhp_state lttng_hp_online
;
3827 static int lttng_hotplug_prepare(unsigned int cpu
, struct hlist_node
*node
)
3829 struct lttng_cpuhp_node
*lttng_node
;
3831 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3832 switch (lttng_node
->component
) {
3833 case LTTNG_RING_BUFFER_FRONTEND
:
3835 case LTTNG_RING_BUFFER_BACKEND
:
3836 return lttng_cpuhp_rb_backend_prepare(cpu
, lttng_node
);
3837 case LTTNG_RING_BUFFER_ITER
:
3839 case LTTNG_CONTEXT_PERF_COUNTERS
:
3846 static int lttng_hotplug_dead(unsigned int cpu
, struct hlist_node
*node
)
3848 struct lttng_cpuhp_node
*lttng_node
;
3850 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3851 switch (lttng_node
->component
) {
3852 case LTTNG_RING_BUFFER_FRONTEND
:
3853 return lttng_cpuhp_rb_frontend_dead(cpu
, lttng_node
);
3854 case LTTNG_RING_BUFFER_BACKEND
:
3856 case LTTNG_RING_BUFFER_ITER
:
3858 case LTTNG_CONTEXT_PERF_COUNTERS
:
3859 return lttng_cpuhp_perf_counter_dead(cpu
, lttng_node
);
3865 static int lttng_hotplug_online(unsigned int cpu
, struct hlist_node
*node
)
3867 struct lttng_cpuhp_node
*lttng_node
;
3869 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3870 switch (lttng_node
->component
) {
3871 case LTTNG_RING_BUFFER_FRONTEND
:
3872 return lttng_cpuhp_rb_frontend_online(cpu
, lttng_node
);
3873 case LTTNG_RING_BUFFER_BACKEND
:
3875 case LTTNG_RING_BUFFER_ITER
:
3876 return lttng_cpuhp_rb_iter_online(cpu
, lttng_node
);
3877 case LTTNG_CONTEXT_PERF_COUNTERS
:
3878 return lttng_cpuhp_perf_counter_online(cpu
, lttng_node
);
3884 static int lttng_hotplug_offline(unsigned int cpu
, struct hlist_node
*node
)
3886 struct lttng_cpuhp_node
*lttng_node
;
3888 lttng_node
= container_of(node
, struct lttng_cpuhp_node
, node
);
3889 switch (lttng_node
->component
) {
3890 case LTTNG_RING_BUFFER_FRONTEND
:
3891 return lttng_cpuhp_rb_frontend_offline(cpu
, lttng_node
);
3892 case LTTNG_RING_BUFFER_BACKEND
:
3894 case LTTNG_RING_BUFFER_ITER
:
3896 case LTTNG_CONTEXT_PERF_COUNTERS
:
3903 static int __init
lttng_init_cpu_hotplug(void)
3907 ret
= cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN
, "lttng:prepare",
3908 lttng_hotplug_prepare
,
3909 lttng_hotplug_dead
);
3913 lttng_hp_prepare
= ret
;
3914 lttng_rb_set_hp_prepare(ret
);
3916 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "lttng:online",
3917 lttng_hotplug_online
,
3918 lttng_hotplug_offline
);
3920 cpuhp_remove_multi_state(lttng_hp_prepare
);
3921 lttng_hp_prepare
= 0;
3924 lttng_hp_online
= ret
;
3925 lttng_rb_set_hp_online(ret
);
3930 static void __exit
lttng_exit_cpu_hotplug(void)
3932 lttng_rb_set_hp_online(0);
3933 cpuhp_remove_multi_state(lttng_hp_online
);
3934 lttng_rb_set_hp_prepare(0);
3935 cpuhp_remove_multi_state(lttng_hp_prepare
);
3938 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3939 static int lttng_init_cpu_hotplug(void)
3943 static void lttng_exit_cpu_hotplug(void)
3946 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3949 static int __init
lttng_events_init(void)
3953 ret
= wrapper_lttng_fixup_sig(THIS_MODULE
);
3956 ret
= wrapper_get_pfnblock_flags_mask_init();
3959 ret
= wrapper_get_pageblock_flags_mask_init();
3962 ret
= lttng_probes_init();
3965 ret
= lttng_context_init();
3968 ret
= lttng_tracepoint_init();
3971 event_cache
= KMEM_CACHE(lttng_event
, 0);
3974 goto error_kmem_event
;
3976 event_notifier_cache
= KMEM_CACHE(lttng_event_notifier
, 0);
3977 if (!event_notifier_cache
) {
3979 goto error_kmem_event_notifier
;
3981 ret
= lttng_abi_init();
3984 ret
= lttng_logger_init();
3987 ret
= lttng_init_cpu_hotplug();
3990 printk(KERN_NOTICE
"LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3991 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
3992 __stringify(LTTNG_MODULES_MINOR_VERSION
),
3993 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
3994 LTTNG_MODULES_EXTRAVERSION
,
3996 #ifdef LTTNG_EXTRA_VERSION_GIT
3997 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
4001 #ifdef LTTNG_EXTRA_VERSION_NAME
4002 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
4009 lttng_logger_exit();
4013 kmem_cache_destroy(event_notifier_cache
);
4014 error_kmem_event_notifier
:
4015 kmem_cache_destroy(event_cache
);
4017 lttng_tracepoint_exit();
4019 lttng_context_exit();
4020 printk(KERN_NOTICE
"LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
4021 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
4022 __stringify(LTTNG_MODULES_MINOR_VERSION
),
4023 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
4024 LTTNG_MODULES_EXTRAVERSION
,
4026 #ifdef LTTNG_EXTRA_VERSION_GIT
4027 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
4031 #ifdef LTTNG_EXTRA_VERSION_NAME
4032 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
4039 module_init(lttng_events_init
);
4041 static void __exit
lttng_events_exit(void)
4043 struct lttng_session
*session
, *tmpsession
;
4045 lttng_exit_cpu_hotplug();
4046 lttng_logger_exit();
4048 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
4049 lttng_session_destroy(session
);
4050 kmem_cache_destroy(event_cache
);
4051 kmem_cache_destroy(event_notifier_cache
);
4052 lttng_tracepoint_exit();
4053 lttng_context_exit();
4054 printk(KERN_NOTICE
"LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
4055 __stringify(LTTNG_MODULES_MAJOR_VERSION
),
4056 __stringify(LTTNG_MODULES_MINOR_VERSION
),
4057 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
),
4058 LTTNG_MODULES_EXTRAVERSION
,
4060 #ifdef LTTNG_EXTRA_VERSION_GIT
4061 LTTNG_EXTRA_VERSION_GIT
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT
,
4065 #ifdef LTTNG_EXTRA_VERSION_NAME
4066 LTTNG_EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME
);
4072 module_exit(lttng_events_exit
);
4074 #include <generated/patches.h>
4075 #ifdef LTTNG_EXTRA_VERSION_GIT
4076 MODULE_INFO(extra_version_git
, LTTNG_EXTRA_VERSION_GIT
);
4078 #ifdef LTTNG_EXTRA_VERSION_NAME
4079 MODULE_INFO(extra_version_name
, LTTNG_EXTRA_VERSION_NAME
);
4081 MODULE_LICENSE("GPL and additional rights");
4082 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
4083 MODULE_DESCRIPTION("LTTng tracer");
4084 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
4085 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
4086 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
4087 LTTNG_MODULES_EXTRAVERSION
);