Fix: timer_expire_entry changed in 4.19.312
[lttng-modules.git] / src / lttng-events.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng-events.c
4 *
5 * Holds LTTng per-session event registry.
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 /*
11 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
12 * overrides a function with a define.
13 */
14 #include "wrapper/page_alloc.h"
15
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <linux/utsname.h>
22 #include <linux/err.h>
23 #include <linux/seq_file.h>
24 #include <linux/file.h>
25 #include <linux/anon_inodes.h>
26 #include <wrapper/file.h>
27 #include <linux/jhash.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/dmi.h>
31
32 #include <wrapper/uuid.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <wrapper/random.h>
35 #include <wrapper/tracepoint.h>
36 #include <wrapper/list.h>
37 #include <wrapper/types.h>
38 #include <lttng/kernel-version.h>
39 #include <lttng/events.h>
40 #include <lttng/tracer.h>
41 #include <lttng/abi-old.h>
42 #include <lttng/endian.h>
43 #include <lttng/string-utils.h>
44 #include <ringbuffer/backend.h>
45 #include <ringbuffer/frontend.h>
46 #include <wrapper/time.h>
47
48 #define METADATA_CACHE_DEFAULT_SIZE 4096
49
50 static LIST_HEAD(sessions);
51 static LIST_HEAD(lttng_transport_list);
52 /*
53 * Protect the sessions and metadata caches.
54 */
55 static DEFINE_MUTEX(sessions_mutex);
56 static struct kmem_cache *event_cache;
57
58 static void lttng_session_lazy_sync_enablers(struct lttng_session *session);
59 static void lttng_session_sync_enablers(struct lttng_session *session);
60 static void lttng_enabler_destroy(struct lttng_enabler *enabler);
61
62 static void _lttng_event_destroy(struct lttng_event *event);
63 static void _lttng_channel_destroy(struct lttng_channel *chan);
64 static int _lttng_event_unregister(struct lttng_event *event);
65 static
66 int _lttng_event_metadata_statedump(struct lttng_session *session,
67 struct lttng_channel *chan,
68 struct lttng_event *event);
69 static
70 int _lttng_session_metadata_statedump(struct lttng_session *session);
71 static
72 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
73 static
74 int _lttng_type_statedump(struct lttng_session *session,
75 const struct lttng_type *type,
76 size_t nesting);
77 static
78 int _lttng_field_statedump(struct lttng_session *session,
79 const struct lttng_event_field *field,
80 size_t nesting);
81
82 void synchronize_trace(void)
83 {
84 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
85 synchronize_rcu();
86 #else
87 synchronize_sched();
88 #endif
89
90 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
91 #ifdef CONFIG_PREEMPT_RT_FULL
92 synchronize_rcu();
93 #endif
94 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
95 #ifdef CONFIG_PREEMPT_RT
96 synchronize_rcu();
97 #endif
98 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
99 }
100
101 void lttng_lock_sessions(void)
102 {
103 mutex_lock(&sessions_mutex);
104 }
105
106 void lttng_unlock_sessions(void)
107 {
108 mutex_unlock(&sessions_mutex);
109 }
110
111 /*
112 * Called with sessions lock held.
113 */
114 int lttng_session_active(void)
115 {
116 struct lttng_session *iter;
117
118 list_for_each_entry(iter, &sessions, list) {
119 if (iter->active)
120 return 1;
121 }
122 return 0;
123 }
124
125 struct lttng_session *lttng_session_create(void)
126 {
127 struct lttng_session *session;
128 struct lttng_metadata_cache *metadata_cache;
129 int i;
130
131 mutex_lock(&sessions_mutex);
132 session = lttng_kvzalloc(sizeof(struct lttng_session), GFP_KERNEL);
133 if (!session)
134 goto err;
135 INIT_LIST_HEAD(&session->chan);
136 INIT_LIST_HEAD(&session->events);
137 lttng_guid_gen(&session->uuid);
138
139 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
140 GFP_KERNEL);
141 if (!metadata_cache)
142 goto err_free_session;
143 metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE);
144 if (!metadata_cache->data)
145 goto err_free_cache;
146 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
147 kref_init(&metadata_cache->refcount);
148 mutex_init(&metadata_cache->lock);
149 session->metadata_cache = metadata_cache;
150 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
151 memcpy(&metadata_cache->uuid, &session->uuid,
152 sizeof(metadata_cache->uuid));
153 INIT_LIST_HEAD(&session->enablers_head);
154 for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++)
155 INIT_HLIST_HEAD(&session->events_ht.table[i]);
156 list_add(&session->list, &sessions);
157 session->pid_tracker.session = session;
158 session->pid_tracker.tracker_type = TRACKER_PID;
159 session->vpid_tracker.session = session;
160 session->vpid_tracker.tracker_type = TRACKER_VPID;
161 session->uid_tracker.session = session;
162 session->uid_tracker.tracker_type = TRACKER_UID;
163 session->vuid_tracker.session = session;
164 session->vuid_tracker.tracker_type = TRACKER_VUID;
165 session->gid_tracker.session = session;
166 session->gid_tracker.tracker_type = TRACKER_GID;
167 session->vgid_tracker.session = session;
168 session->vgid_tracker.tracker_type = TRACKER_VGID;
169 mutex_unlock(&sessions_mutex);
170 return session;
171
172 err_free_cache:
173 kfree(metadata_cache);
174 err_free_session:
175 lttng_kvfree(session);
176 err:
177 mutex_unlock(&sessions_mutex);
178 return NULL;
179 }
180
181 void metadata_cache_destroy(struct kref *kref)
182 {
183 struct lttng_metadata_cache *cache =
184 container_of(kref, struct lttng_metadata_cache, refcount);
185 vfree(cache->data);
186 kfree(cache);
187 }
188
189 void lttng_session_destroy(struct lttng_session *session)
190 {
191 struct lttng_channel *chan, *tmpchan;
192 struct lttng_event *event, *tmpevent;
193 struct lttng_metadata_stream *metadata_stream;
194 struct lttng_enabler *enabler, *tmpenabler;
195 int ret;
196
197 mutex_lock(&sessions_mutex);
198 WRITE_ONCE(session->active, 0);
199 list_for_each_entry(chan, &session->chan, list) {
200 ret = lttng_syscalls_unregister(chan);
201 WARN_ON(ret);
202 }
203 list_for_each_entry(event, &session->events, list) {
204 ret = _lttng_event_unregister(event);
205 WARN_ON(ret);
206 }
207 synchronize_trace(); /* Wait for in-flight events to complete */
208 list_for_each_entry(chan, &session->chan, list) {
209 ret = lttng_syscalls_destroy(chan);
210 WARN_ON(ret);
211 }
212 list_for_each_entry_safe(enabler, tmpenabler,
213 &session->enablers_head, node)
214 lttng_enabler_destroy(enabler);
215 list_for_each_entry_safe(event, tmpevent, &session->events, list)
216 _lttng_event_destroy(event);
217 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
218 BUG_ON(chan->channel_type == METADATA_CHANNEL);
219 _lttng_channel_destroy(chan);
220 }
221 mutex_lock(&session->metadata_cache->lock);
222 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
223 _lttng_metadata_channel_hangup(metadata_stream);
224 mutex_unlock(&session->metadata_cache->lock);
225 lttng_id_tracker_destroy(&session->pid_tracker, false);
226 lttng_id_tracker_destroy(&session->vpid_tracker, false);
227 lttng_id_tracker_destroy(&session->uid_tracker, false);
228 lttng_id_tracker_destroy(&session->vuid_tracker, false);
229 lttng_id_tracker_destroy(&session->gid_tracker, false);
230 lttng_id_tracker_destroy(&session->vgid_tracker, false);
231 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
232 list_del(&session->list);
233 mutex_unlock(&sessions_mutex);
234 lttng_kvfree(session);
235 }
236
237 int lttng_session_statedump(struct lttng_session *session)
238 {
239 int ret;
240
241 mutex_lock(&sessions_mutex);
242 ret = lttng_statedump_start(session);
243 mutex_unlock(&sessions_mutex);
244 return ret;
245 }
246
247 int lttng_session_enable(struct lttng_session *session)
248 {
249 int ret = 0;
250 struct lttng_channel *chan;
251
252 mutex_lock(&sessions_mutex);
253 if (session->active) {
254 ret = -EBUSY;
255 goto end;
256 }
257
258 /* Set transient enabler state to "enabled" */
259 session->tstate = 1;
260
261 /* We need to sync enablers with session before activation. */
262 lttng_session_sync_enablers(session);
263
264 /*
265 * Snapshot the number of events per channel to know the type of header
266 * we need to use.
267 */
268 list_for_each_entry(chan, &session->chan, list) {
269 if (chan->header_type)
270 continue; /* don't change it if session stop/restart */
271 if (chan->free_event_id < 31)
272 chan->header_type = 1; /* compact */
273 else
274 chan->header_type = 2; /* large */
275 }
276
277 /* Clear each stream's quiescent state. */
278 list_for_each_entry(chan, &session->chan, list) {
279 if (chan->channel_type != METADATA_CHANNEL)
280 lib_ring_buffer_clear_quiescent_channel(chan->chan);
281 }
282
283 WRITE_ONCE(session->active, 1);
284 WRITE_ONCE(session->been_active, 1);
285 ret = _lttng_session_metadata_statedump(session);
286 if (ret) {
287 WRITE_ONCE(session->active, 0);
288 goto end;
289 }
290 ret = lttng_statedump_start(session);
291 if (ret)
292 WRITE_ONCE(session->active, 0);
293 end:
294 mutex_unlock(&sessions_mutex);
295 return ret;
296 }
297
298 int lttng_session_disable(struct lttng_session *session)
299 {
300 int ret = 0;
301 struct lttng_channel *chan;
302
303 mutex_lock(&sessions_mutex);
304 if (!session->active) {
305 ret = -EBUSY;
306 goto end;
307 }
308 WRITE_ONCE(session->active, 0);
309
310 /* Set transient enabler state to "disabled" */
311 session->tstate = 0;
312 lttng_session_sync_enablers(session);
313
314 /* Set each stream's quiescent state. */
315 list_for_each_entry(chan, &session->chan, list) {
316 if (chan->channel_type != METADATA_CHANNEL)
317 lib_ring_buffer_set_quiescent_channel(chan->chan);
318 }
319 end:
320 mutex_unlock(&sessions_mutex);
321 return ret;
322 }
323
324 int lttng_session_metadata_regenerate(struct lttng_session *session)
325 {
326 int ret = 0;
327 struct lttng_channel *chan;
328 struct lttng_event *event;
329 struct lttng_metadata_cache *cache = session->metadata_cache;
330 struct lttng_metadata_stream *stream;
331
332 mutex_lock(&sessions_mutex);
333 if (!session->active) {
334 ret = -EBUSY;
335 goto end;
336 }
337
338 mutex_lock(&cache->lock);
339 memset(cache->data, 0, cache->cache_alloc);
340 cache->metadata_written = 0;
341 cache->version++;
342 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) {
343 stream->metadata_out = 0;
344 stream->metadata_in = 0;
345 }
346 mutex_unlock(&cache->lock);
347
348 session->metadata_dumped = 0;
349 list_for_each_entry(chan, &session->chan, list) {
350 chan->metadata_dumped = 0;
351 }
352
353 list_for_each_entry(event, &session->events, list) {
354 event->metadata_dumped = 0;
355 }
356
357 ret = _lttng_session_metadata_statedump(session);
358
359 end:
360 mutex_unlock(&sessions_mutex);
361 return ret;
362 }
363
364 int lttng_channel_enable(struct lttng_channel *channel)
365 {
366 int ret = 0;
367
368 mutex_lock(&sessions_mutex);
369 if (channel->channel_type == METADATA_CHANNEL) {
370 ret = -EPERM;
371 goto end;
372 }
373 if (channel->enabled) {
374 ret = -EEXIST;
375 goto end;
376 }
377 /* Set transient enabler state to "enabled" */
378 channel->tstate = 1;
379 lttng_session_sync_enablers(channel->session);
380 /* Set atomically the state to "enabled" */
381 WRITE_ONCE(channel->enabled, 1);
382 end:
383 mutex_unlock(&sessions_mutex);
384 return ret;
385 }
386
387 int lttng_channel_disable(struct lttng_channel *channel)
388 {
389 int ret = 0;
390
391 mutex_lock(&sessions_mutex);
392 if (channel->channel_type == METADATA_CHANNEL) {
393 ret = -EPERM;
394 goto end;
395 }
396 if (!channel->enabled) {
397 ret = -EEXIST;
398 goto end;
399 }
400 /* Set atomically the state to "disabled" */
401 WRITE_ONCE(channel->enabled, 0);
402 /* Set transient enabler state to "enabled" */
403 channel->tstate = 0;
404 lttng_session_sync_enablers(channel->session);
405 end:
406 mutex_unlock(&sessions_mutex);
407 return ret;
408 }
409
410 int lttng_event_enable(struct lttng_event *event)
411 {
412 int ret = 0;
413
414 mutex_lock(&sessions_mutex);
415 if (event->chan->channel_type == METADATA_CHANNEL) {
416 ret = -EPERM;
417 goto end;
418 }
419 if (event->enabled) {
420 ret = -EEXIST;
421 goto end;
422 }
423 switch (event->instrumentation) {
424 case LTTNG_KERNEL_TRACEPOINT:
425 case LTTNG_KERNEL_SYSCALL:
426 ret = -EINVAL;
427 break;
428 case LTTNG_KERNEL_KPROBE:
429 case LTTNG_KERNEL_UPROBE:
430 case LTTNG_KERNEL_NOOP:
431 WRITE_ONCE(event->enabled, 1);
432 break;
433 case LTTNG_KERNEL_KRETPROBE:
434 ret = lttng_kretprobes_event_enable_state(event, 1);
435 break;
436 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
437 default:
438 WARN_ON_ONCE(1);
439 ret = -EINVAL;
440 }
441 end:
442 mutex_unlock(&sessions_mutex);
443 return ret;
444 }
445
446 int lttng_event_disable(struct lttng_event *event)
447 {
448 int ret = 0;
449
450 mutex_lock(&sessions_mutex);
451 if (event->chan->channel_type == METADATA_CHANNEL) {
452 ret = -EPERM;
453 goto end;
454 }
455 if (!event->enabled) {
456 ret = -EEXIST;
457 goto end;
458 }
459 switch (event->instrumentation) {
460 case LTTNG_KERNEL_TRACEPOINT:
461 case LTTNG_KERNEL_SYSCALL:
462 ret = -EINVAL;
463 break;
464 case LTTNG_KERNEL_KPROBE:
465 case LTTNG_KERNEL_UPROBE:
466 case LTTNG_KERNEL_NOOP:
467 WRITE_ONCE(event->enabled, 0);
468 break;
469 case LTTNG_KERNEL_KRETPROBE:
470 ret = lttng_kretprobes_event_enable_state(event, 0);
471 break;
472 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
473 default:
474 WARN_ON_ONCE(1);
475 ret = -EINVAL;
476 }
477 end:
478 mutex_unlock(&sessions_mutex);
479 return ret;
480 }
481
482 static struct lttng_transport *lttng_transport_find(const char *name)
483 {
484 struct lttng_transport *transport;
485
486 list_for_each_entry(transport, &lttng_transport_list, node) {
487 if (!strcmp(transport->name, name))
488 return transport;
489 }
490 return NULL;
491 }
492
493 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
494 const char *transport_name,
495 void *buf_addr,
496 size_t subbuf_size, size_t num_subbuf,
497 unsigned int switch_timer_interval,
498 unsigned int read_timer_interval,
499 enum channel_type channel_type)
500 {
501 struct lttng_channel *chan;
502 struct lttng_transport *transport = NULL;
503
504 mutex_lock(&sessions_mutex);
505 if (session->been_active && channel_type != METADATA_CHANNEL)
506 goto active; /* Refuse to add channel to active session */
507 transport = lttng_transport_find(transport_name);
508 if (!transport) {
509 printk(KERN_WARNING "LTTng: transport %s not found\n",
510 transport_name);
511 goto notransport;
512 }
513 if (!try_module_get(transport->owner)) {
514 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
515 goto notransport;
516 }
517 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
518 if (!chan)
519 goto nomem;
520 chan->session = session;
521 chan->id = session->free_chan_id++;
522 chan->ops = &transport->ops;
523 /*
524 * Note: the channel creation op already writes into the packet
525 * headers. Therefore the "chan" information used as input
526 * should be already accessible.
527 */
528 chan->chan = transport->ops.channel_create(transport_name,
529 chan, buf_addr, subbuf_size, num_subbuf,
530 switch_timer_interval, read_timer_interval);
531 if (!chan->chan)
532 goto create_error;
533 chan->tstate = 1;
534 chan->enabled = 1;
535 chan->transport = transport;
536 chan->channel_type = channel_type;
537 list_add(&chan->list, &session->chan);
538 mutex_unlock(&sessions_mutex);
539 return chan;
540
541 create_error:
542 kfree(chan);
543 nomem:
544 if (transport)
545 module_put(transport->owner);
546 notransport:
547 active:
548 mutex_unlock(&sessions_mutex);
549 return NULL;
550 }
551
552 /*
553 * Only used internally at session destruction for per-cpu channels, and
554 * when metadata channel is released.
555 * Needs to be called with sessions mutex held.
556 */
557 static
558 void _lttng_channel_destroy(struct lttng_channel *chan)
559 {
560 chan->ops->channel_destroy(chan->chan);
561 module_put(chan->transport->owner);
562 list_del(&chan->list);
563 lttng_destroy_context(chan->ctx);
564 kfree(chan);
565 }
566
567 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
568 {
569 BUG_ON(chan->channel_type != METADATA_CHANNEL);
570
571 /* Protect the metadata cache with the sessions_mutex. */
572 mutex_lock(&sessions_mutex);
573 _lttng_channel_destroy(chan);
574 mutex_unlock(&sessions_mutex);
575 }
576 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
577
578 static
579 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
580 {
581 stream->finalized = 1;
582 wake_up_interruptible(&stream->read_wait);
583 }
584
585 /*
586 * Supports event creation while tracing session is active.
587 * Needs to be called with sessions mutex held.
588 */
589 struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
590 struct lttng_kernel_event *event_param,
591 void *filter,
592 const struct lttng_event_desc *event_desc,
593 enum lttng_kernel_instrumentation itype)
594 {
595 struct lttng_session *session = chan->session;
596 struct lttng_event *event;
597 const char *event_name;
598 struct hlist_head *head;
599 size_t name_len;
600 uint32_t hash;
601 int ret;
602
603 if (chan->free_event_id == -1U) {
604 ret = -EMFILE;
605 goto full;
606 }
607
608 switch (itype) {
609 case LTTNG_KERNEL_TRACEPOINT:
610 event_name = event_desc->name;
611 break;
612 case LTTNG_KERNEL_KPROBE:
613 case LTTNG_KERNEL_UPROBE:
614 case LTTNG_KERNEL_KRETPROBE:
615 case LTTNG_KERNEL_NOOP:
616 case LTTNG_KERNEL_SYSCALL:
617 event_name = event_param->name;
618 break;
619 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
620 default:
621 WARN_ON_ONCE(1);
622 ret = -EINVAL;
623 goto type_error;
624 }
625 name_len = strlen(event_name);
626 hash = jhash(event_name, name_len, 0);
627 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
628 lttng_hlist_for_each_entry(event, head, hlist) {
629 WARN_ON_ONCE(!event->desc);
630 if (!strncmp(event->desc->name, event_name,
631 LTTNG_KERNEL_SYM_NAME_LEN - 1)
632 && chan == event->chan) {
633 ret = -EEXIST;
634 goto exist;
635 }
636 }
637
638 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
639 if (!event) {
640 ret = -ENOMEM;
641 goto cache_error;
642 }
643 event->chan = chan;
644 event->filter = filter;
645 event->id = chan->free_event_id++;
646 event->instrumentation = itype;
647 event->evtype = LTTNG_TYPE_EVENT;
648 INIT_LIST_HEAD(&event->bytecode_runtime_head);
649 INIT_LIST_HEAD(&event->enablers_ref_head);
650
651 switch (itype) {
652 case LTTNG_KERNEL_TRACEPOINT:
653 /* Event will be enabled by enabler sync. */
654 event->enabled = 0;
655 event->registered = 0;
656 event->desc = lttng_event_get(event_name);
657 if (!event->desc) {
658 ret = -ENOENT;
659 goto register_error;
660 }
661 /* Populate lttng_event structure before event registration. */
662 smp_wmb();
663 break;
664 case LTTNG_KERNEL_KPROBE:
665 /*
666 * Needs to be explicitly enabled after creation, since
667 * we may want to apply filters.
668 */
669 event->enabled = 0;
670 event->registered = 1;
671 /*
672 * Populate lttng_event structure before event
673 * registration.
674 */
675 smp_wmb();
676 ret = lttng_kprobes_register(event_name,
677 event_param->u.kprobe.symbol_name,
678 event_param->u.kprobe.offset,
679 event_param->u.kprobe.addr,
680 event);
681 if (ret) {
682 ret = -EINVAL;
683 goto register_error;
684 }
685 ret = try_module_get(event->desc->owner);
686 WARN_ON_ONCE(!ret);
687 break;
688 case LTTNG_KERNEL_KRETPROBE:
689 {
690 struct lttng_event *event_return;
691
692 /* kretprobe defines 2 events */
693 /*
694 * Needs to be explicitly enabled after creation, since
695 * we may want to apply filters.
696 */
697 event->enabled = 0;
698 event->registered = 1;
699 event_return =
700 kmem_cache_zalloc(event_cache, GFP_KERNEL);
701 if (!event_return) {
702 ret = -ENOMEM;
703 goto register_error;
704 }
705 event_return->chan = chan;
706 event_return->filter = filter;
707 event_return->id = chan->free_event_id++;
708 event_return->enabled = 0;
709 event_return->registered = 1;
710 event_return->instrumentation = itype;
711 /*
712 * Populate lttng_event structure before kretprobe registration.
713 */
714 smp_wmb();
715 ret = lttng_kretprobes_register(event_name,
716 event_param->u.kretprobe.symbol_name,
717 event_param->u.kretprobe.offset,
718 event_param->u.kretprobe.addr,
719 event, event_return);
720 if (ret) {
721 kmem_cache_free(event_cache, event_return);
722 ret = -EINVAL;
723 goto register_error;
724 }
725 /* Take 2 refs on the module: one per event. */
726 ret = try_module_get(event->desc->owner);
727 WARN_ON_ONCE(!ret);
728 ret = try_module_get(event->desc->owner);
729 WARN_ON_ONCE(!ret);
730 ret = _lttng_event_metadata_statedump(chan->session, chan,
731 event_return);
732 WARN_ON_ONCE(ret > 0);
733 if (ret) {
734 kmem_cache_free(event_cache, event_return);
735 module_put(event->desc->owner);
736 module_put(event->desc->owner);
737 goto statedump_error;
738 }
739 list_add(&event_return->list, &chan->session->events);
740 break;
741 }
742 case LTTNG_KERNEL_NOOP:
743 case LTTNG_KERNEL_SYSCALL:
744 /*
745 * Needs to be explicitly enabled after creation, since
746 * we may want to apply filters.
747 */
748 event->enabled = 0;
749 event->registered = 0;
750 event->desc = event_desc;
751 switch (event_param->u.syscall.entryexit) {
752 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
753 ret = -EINVAL;
754 goto register_error;
755 case LTTNG_KERNEL_SYSCALL_ENTRY:
756 event->u.syscall.entryexit = LTTNG_SYSCALL_ENTRY;
757 break;
758 case LTTNG_KERNEL_SYSCALL_EXIT:
759 event->u.syscall.entryexit = LTTNG_SYSCALL_EXIT;
760 break;
761 }
762 switch (event_param->u.syscall.abi) {
763 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
764 ret = -EINVAL;
765 goto register_error;
766 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE:
767 event->u.syscall.abi = LTTNG_SYSCALL_ABI_NATIVE;
768 break;
769 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT:
770 event->u.syscall.abi = LTTNG_SYSCALL_ABI_COMPAT;
771 break;
772 }
773 if (!event->desc) {
774 ret = -EINVAL;
775 goto register_error;
776 }
777 break;
778 case LTTNG_KERNEL_UPROBE:
779 /*
780 * Needs to be explicitly enabled after creation, since
781 * we may want to apply filters.
782 */
783 event->enabled = 0;
784 event->registered = 1;
785
786 /*
787 * Populate lttng_event structure before event
788 * registration.
789 */
790 smp_wmb();
791
792 ret = lttng_uprobes_register(event_param->name,
793 event_param->u.uprobe.fd,
794 event);
795 if (ret)
796 goto register_error;
797 ret = try_module_get(event->desc->owner);
798 WARN_ON_ONCE(!ret);
799 break;
800 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
801 default:
802 WARN_ON_ONCE(1);
803 ret = -EINVAL;
804 goto register_error;
805 }
806 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
807 WARN_ON_ONCE(ret > 0);
808 if (ret) {
809 goto statedump_error;
810 }
811 hlist_add_head(&event->hlist, head);
812 list_add(&event->list, &chan->session->events);
813 return event;
814
815 statedump_error:
816 /* If a statedump error occurs, events will not be readable. */
817 register_error:
818 kmem_cache_free(event_cache, event);
819 cache_error:
820 exist:
821 type_error:
822 full:
823 return ERR_PTR(ret);
824 }
825
826 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
827 struct lttng_kernel_event *event_param,
828 void *filter,
829 const struct lttng_event_desc *event_desc,
830 enum lttng_kernel_instrumentation itype)
831 {
832 struct lttng_event *event;
833
834 mutex_lock(&sessions_mutex);
835 event = _lttng_event_create(chan, event_param, filter, event_desc,
836 itype);
837 mutex_unlock(&sessions_mutex);
838 return event;
839 }
840
841 /* Only used for tracepoints for now. */
842 static
843 void register_event(struct lttng_event *event)
844 {
845 const struct lttng_event_desc *desc;
846 int ret = -EINVAL;
847
848 if (event->registered)
849 return;
850
851 desc = event->desc;
852 switch (event->instrumentation) {
853 case LTTNG_KERNEL_TRACEPOINT:
854 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
855 desc->probe_callback,
856 event);
857 break;
858 case LTTNG_KERNEL_SYSCALL:
859 ret = lttng_syscall_filter_enable(event->chan, event);
860 break;
861 case LTTNG_KERNEL_KPROBE:
862 case LTTNG_KERNEL_UPROBE:
863 case LTTNG_KERNEL_KRETPROBE:
864 case LTTNG_KERNEL_NOOP:
865 ret = 0;
866 break;
867 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
868 default:
869 WARN_ON_ONCE(1);
870 }
871 if (!ret)
872 event->registered = 1;
873 }
874
875 /*
876 * Only used internally at session destruction.
877 */
878 int _lttng_event_unregister(struct lttng_event *event)
879 {
880 const struct lttng_event_desc *desc;
881 int ret = -EINVAL;
882
883 if (!event->registered)
884 return 0;
885
886 desc = event->desc;
887 switch (event->instrumentation) {
888 case LTTNG_KERNEL_TRACEPOINT:
889 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
890 event->desc->probe_callback,
891 event);
892 break;
893 case LTTNG_KERNEL_KPROBE:
894 lttng_kprobes_unregister(event);
895 ret = 0;
896 break;
897 case LTTNG_KERNEL_KRETPROBE:
898 lttng_kretprobes_unregister(event);
899 ret = 0;
900 break;
901 case LTTNG_KERNEL_SYSCALL:
902 ret = lttng_syscall_filter_disable(event->chan, event);
903 break;
904 case LTTNG_KERNEL_NOOP:
905 ret = 0;
906 break;
907 case LTTNG_KERNEL_UPROBE:
908 lttng_uprobes_unregister(event);
909 ret = 0;
910 break;
911 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
912 default:
913 WARN_ON_ONCE(1);
914 }
915 if (!ret)
916 event->registered = 0;
917 return ret;
918 }
919
920 /*
921 * Only used internally at session destruction.
922 */
923 static
924 void _lttng_event_destroy(struct lttng_event *event)
925 {
926 switch (event->instrumentation) {
927 case LTTNG_KERNEL_TRACEPOINT:
928 lttng_event_put(event->desc);
929 break;
930 case LTTNG_KERNEL_KPROBE:
931 module_put(event->desc->owner);
932 lttng_kprobes_destroy_private(event);
933 break;
934 case LTTNG_KERNEL_KRETPROBE:
935 module_put(event->desc->owner);
936 lttng_kretprobes_destroy_private(event);
937 break;
938 case LTTNG_KERNEL_NOOP:
939 case LTTNG_KERNEL_SYSCALL:
940 break;
941 case LTTNG_KERNEL_UPROBE:
942 module_put(event->desc->owner);
943 lttng_uprobes_destroy_private(event);
944 break;
945 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
946 default:
947 WARN_ON_ONCE(1);
948 }
949 list_del(&event->list);
950 lttng_destroy_context(event->ctx);
951 kmem_cache_free(event_cache, event);
952 }
953
954 struct lttng_id_tracker *get_tracker(struct lttng_session *session,
955 enum tracker_type tracker_type)
956 {
957 switch (tracker_type) {
958 case TRACKER_PID:
959 return &session->pid_tracker;
960 case TRACKER_VPID:
961 return &session->vpid_tracker;
962 case TRACKER_UID:
963 return &session->uid_tracker;
964 case TRACKER_VUID:
965 return &session->vuid_tracker;
966 case TRACKER_GID:
967 return &session->gid_tracker;
968 case TRACKER_VGID:
969 return &session->vgid_tracker;
970 default:
971 WARN_ON_ONCE(1);
972 return NULL;
973 }
974 }
975
976 int lttng_session_track_id(struct lttng_session *session,
977 enum tracker_type tracker_type, int id)
978 {
979 struct lttng_id_tracker *tracker;
980 int ret;
981
982 tracker = get_tracker(session, tracker_type);
983 if (!tracker)
984 return -EINVAL;
985 if (id < -1)
986 return -EINVAL;
987 mutex_lock(&sessions_mutex);
988 if (id == -1) {
989 /* track all ids: destroy tracker. */
990 lttng_id_tracker_destroy(tracker, true);
991 ret = 0;
992 } else {
993 ret = lttng_id_tracker_add(tracker, id);
994 }
995 mutex_unlock(&sessions_mutex);
996 return ret;
997 }
998
999 int lttng_session_untrack_id(struct lttng_session *session,
1000 enum tracker_type tracker_type, int id)
1001 {
1002 struct lttng_id_tracker *tracker;
1003 int ret;
1004
1005 tracker = get_tracker(session, tracker_type);
1006 if (!tracker)
1007 return -EINVAL;
1008 if (id < -1)
1009 return -EINVAL;
1010 mutex_lock(&sessions_mutex);
1011 if (id == -1) {
1012 /* untrack all ids: replace by empty tracker. */
1013 ret = lttng_id_tracker_empty_set(tracker);
1014 } else {
1015 ret = lttng_id_tracker_del(tracker, id);
1016 }
1017 mutex_unlock(&sessions_mutex);
1018 return ret;
1019 }
1020
1021 static
1022 void *id_list_start(struct seq_file *m, loff_t *pos)
1023 {
1024 struct lttng_id_tracker *id_tracker = m->private;
1025 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1026 struct lttng_id_hash_node *e;
1027 int iter = 0, i;
1028
1029 mutex_lock(&sessions_mutex);
1030 if (id_tracker_p) {
1031 for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) {
1032 struct hlist_head *head = &id_tracker_p->id_hash[i];
1033
1034 lttng_hlist_for_each_entry(e, head, hlist) {
1035 if (iter++ >= *pos)
1036 return e;
1037 }
1038 }
1039 } else {
1040 /* ID tracker disabled. */
1041 if (iter >= *pos && iter == 0) {
1042 return id_tracker_p; /* empty tracker */
1043 }
1044 iter++;
1045 }
1046 /* End of list */
1047 return NULL;
1048 }
1049
1050 /* Called with sessions_mutex held. */
1051 static
1052 void *id_list_next(struct seq_file *m, void *p, loff_t *ppos)
1053 {
1054 struct lttng_id_tracker *id_tracker = m->private;
1055 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1056 struct lttng_id_hash_node *e;
1057 int iter = 0, i;
1058
1059 (*ppos)++;
1060 if (id_tracker_p) {
1061 for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) {
1062 struct hlist_head *head = &id_tracker_p->id_hash[i];
1063
1064 lttng_hlist_for_each_entry(e, head, hlist) {
1065 if (iter++ >= *ppos)
1066 return e;
1067 }
1068 }
1069 } else {
1070 /* ID tracker disabled. */
1071 if (iter >= *ppos && iter == 0)
1072 return p; /* empty tracker */
1073 iter++;
1074 }
1075
1076 /* End of list */
1077 return NULL;
1078 }
1079
1080 static
1081 void id_list_stop(struct seq_file *m, void *p)
1082 {
1083 mutex_unlock(&sessions_mutex);
1084 }
1085
1086 static
1087 int id_list_show(struct seq_file *m, void *p)
1088 {
1089 struct lttng_id_tracker *id_tracker = m->private;
1090 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1091 int id;
1092
1093 if (p == id_tracker_p) {
1094 /* Tracker disabled. */
1095 id = -1;
1096 } else {
1097 const struct lttng_id_hash_node *e = p;
1098
1099 id = lttng_id_tracker_get_node_id(e);
1100 }
1101 switch (id_tracker->tracker_type) {
1102 case TRACKER_PID:
1103 seq_printf(m, "process { pid = %d; };\n", id);
1104 break;
1105 case TRACKER_VPID:
1106 seq_printf(m, "process { vpid = %d; };\n", id);
1107 break;
1108 case TRACKER_UID:
1109 seq_printf(m, "user { uid = %d; };\n", id);
1110 break;
1111 case TRACKER_VUID:
1112 seq_printf(m, "user { vuid = %d; };\n", id);
1113 break;
1114 case TRACKER_GID:
1115 seq_printf(m, "group { gid = %d; };\n", id);
1116 break;
1117 case TRACKER_VGID:
1118 seq_printf(m, "group { vgid = %d; };\n", id);
1119 break;
1120 default:
1121 seq_printf(m, "UNKNOWN { field = %d };\n", id);
1122 }
1123 return 0;
1124 }
1125
1126 static
1127 const struct seq_operations lttng_tracker_ids_list_seq_ops = {
1128 .start = id_list_start,
1129 .next = id_list_next,
1130 .stop = id_list_stop,
1131 .show = id_list_show,
1132 };
1133
1134 static
1135 int lttng_tracker_ids_list_open(struct inode *inode, struct file *file)
1136 {
1137 return seq_open(file, &lttng_tracker_ids_list_seq_ops);
1138 }
1139
1140 static
1141 int lttng_tracker_ids_list_release(struct inode *inode, struct file *file)
1142 {
1143 struct seq_file *m = file->private_data;
1144 struct lttng_id_tracker *id_tracker = m->private;
1145 int ret;
1146
1147 WARN_ON_ONCE(!id_tracker);
1148 ret = seq_release(inode, file);
1149 if (!ret)
1150 fput(id_tracker->session->file);
1151 return ret;
1152 }
1153
1154 const struct file_operations lttng_tracker_ids_list_fops = {
1155 .owner = THIS_MODULE,
1156 .open = lttng_tracker_ids_list_open,
1157 .read = seq_read,
1158 .llseek = seq_lseek,
1159 .release = lttng_tracker_ids_list_release,
1160 };
1161
1162 int lttng_session_list_tracker_ids(struct lttng_session *session,
1163 enum tracker_type tracker_type)
1164 {
1165 struct file *tracker_ids_list_file;
1166 struct seq_file *m;
1167 int file_fd, ret;
1168
1169 file_fd = lttng_get_unused_fd();
1170 if (file_fd < 0) {
1171 ret = file_fd;
1172 goto fd_error;
1173 }
1174
1175 tracker_ids_list_file = anon_inode_getfile("[lttng_tracker_ids_list]",
1176 &lttng_tracker_ids_list_fops,
1177 NULL, O_RDWR);
1178 if (IS_ERR(tracker_ids_list_file)) {
1179 ret = PTR_ERR(tracker_ids_list_file);
1180 goto file_error;
1181 }
1182 if (!atomic_long_add_unless(&session->file->f_count, 1, LONG_MAX)) {
1183 ret = -EOVERFLOW;
1184 goto refcount_error;
1185 }
1186 ret = lttng_tracker_ids_list_fops.open(NULL, tracker_ids_list_file);
1187 if (ret < 0)
1188 goto open_error;
1189 m = tracker_ids_list_file->private_data;
1190
1191 m->private = get_tracker(session, tracker_type);
1192 BUG_ON(!m->private);
1193 fd_install(file_fd, tracker_ids_list_file);
1194
1195 return file_fd;
1196
1197 open_error:
1198 atomic_long_dec(&session->file->f_count);
1199 refcount_error:
1200 fput(tracker_ids_list_file);
1201 file_error:
1202 put_unused_fd(file_fd);
1203 fd_error:
1204 return ret;
1205 }
1206
1207 /*
1208 * Enabler management.
1209 */
1210 static
1211 int lttng_match_enabler_star_glob(const char *desc_name,
1212 const char *pattern)
1213 {
1214 if (!strutils_star_glob_match(pattern, LTTNG_SIZE_MAX,
1215 desc_name, LTTNG_SIZE_MAX))
1216 return 0;
1217 return 1;
1218 }
1219
1220 static
1221 int lttng_match_enabler_name(const char *desc_name,
1222 const char *name)
1223 {
1224 if (strcmp(desc_name, name))
1225 return 0;
1226 return 1;
1227 }
1228
1229 static
1230 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1231 struct lttng_enabler *enabler)
1232 {
1233 const char *desc_name, *enabler_name;
1234 bool compat = false, entry = false;
1235
1236 enabler_name = enabler->event_param.name;
1237 switch (enabler->event_param.instrumentation) {
1238 case LTTNG_KERNEL_TRACEPOINT:
1239 desc_name = desc->name;
1240 switch (enabler->type) {
1241 case LTTNG_ENABLER_STAR_GLOB:
1242 return lttng_match_enabler_star_glob(desc_name, enabler_name);
1243 case LTTNG_ENABLER_NAME:
1244 return lttng_match_enabler_name(desc_name, enabler_name);
1245 default:
1246 return -EINVAL;
1247 }
1248 break;
1249 case LTTNG_KERNEL_SYSCALL:
1250 desc_name = desc->name;
1251 if (!strncmp(desc_name, "compat_", strlen("compat_"))) {
1252 desc_name += strlen("compat_");
1253 compat = true;
1254 }
1255 if (!strncmp(desc_name, "syscall_exit_",
1256 strlen("syscall_exit_"))) {
1257 desc_name += strlen("syscall_exit_");
1258 } else if (!strncmp(desc_name, "syscall_entry_",
1259 strlen("syscall_entry_"))) {
1260 desc_name += strlen("syscall_entry_");
1261 entry = true;
1262 } else {
1263 WARN_ON_ONCE(1);
1264 return -EINVAL;
1265 }
1266 switch (enabler->event_param.u.syscall.entryexit) {
1267 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
1268 break;
1269 case LTTNG_KERNEL_SYSCALL_ENTRY:
1270 if (!entry)
1271 return 0;
1272 break;
1273 case LTTNG_KERNEL_SYSCALL_EXIT:
1274 if (entry)
1275 return 0;
1276 break;
1277 default:
1278 return -EINVAL;
1279 }
1280 switch (enabler->event_param.u.syscall.abi) {
1281 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
1282 break;
1283 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE:
1284 if (compat)
1285 return 0;
1286 break;
1287 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT:
1288 if (!compat)
1289 return 0;
1290 break;
1291 default:
1292 return -EINVAL;
1293 }
1294 switch (enabler->event_param.u.syscall.match) {
1295 case LTTNG_SYSCALL_MATCH_NAME:
1296 switch (enabler->type) {
1297 case LTTNG_ENABLER_STAR_GLOB:
1298 return lttng_match_enabler_star_glob(desc_name, enabler_name);
1299 case LTTNG_ENABLER_NAME:
1300 return lttng_match_enabler_name(desc_name, enabler_name);
1301 default:
1302 return -EINVAL;
1303 }
1304 break;
1305 case LTTNG_SYSCALL_MATCH_NR:
1306 return -EINVAL; /* Not implemented. */
1307 default:
1308 return -EINVAL;
1309 }
1310 break;
1311 default:
1312 WARN_ON_ONCE(1);
1313 return -EINVAL;
1314 }
1315 }
1316
1317 static
1318 int lttng_event_match_enabler(struct lttng_event *event,
1319 struct lttng_enabler *enabler)
1320 {
1321 if (enabler->event_param.instrumentation != event->instrumentation)
1322 return 0;
1323 if (lttng_desc_match_enabler(event->desc, enabler)
1324 && event->chan == enabler->chan)
1325 return 1;
1326 else
1327 return 0;
1328 }
1329
1330 static
1331 struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event,
1332 struct lttng_enabler *enabler)
1333 {
1334 struct lttng_enabler_ref *enabler_ref;
1335
1336 list_for_each_entry(enabler_ref,
1337 &event->enablers_ref_head, node) {
1338 if (enabler_ref->ref == enabler)
1339 return enabler_ref;
1340 }
1341 return NULL;
1342 }
1343
1344 static
1345 void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
1346 {
1347 struct lttng_session *session = enabler->chan->session;
1348 struct lttng_probe_desc *probe_desc;
1349 const struct lttng_event_desc *desc;
1350 int i;
1351 struct list_head *probe_list;
1352
1353 probe_list = lttng_get_probe_list_head();
1354 /*
1355 * For each probe event, if we find that a probe event matches
1356 * our enabler, create an associated lttng_event if not
1357 * already present.
1358 */
1359 list_for_each_entry(probe_desc, probe_list, head) {
1360 for (i = 0; i < probe_desc->nr_events; i++) {
1361 int found = 0;
1362 struct hlist_head *head;
1363 const char *event_name;
1364 size_t name_len;
1365 uint32_t hash;
1366 struct lttng_event *event;
1367
1368 desc = probe_desc->event_desc[i];
1369 if (!lttng_desc_match_enabler(desc, enabler))
1370 continue;
1371 event_name = desc->name;
1372 name_len = strlen(event_name);
1373
1374 /*
1375 * Check if already created.
1376 */
1377 hash = jhash(event_name, name_len, 0);
1378 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
1379 lttng_hlist_for_each_entry(event, head, hlist) {
1380 if (event->desc == desc
1381 && event->chan == enabler->chan)
1382 found = 1;
1383 }
1384 if (found)
1385 continue;
1386
1387 /*
1388 * We need to create an event for this
1389 * event probe.
1390 */
1391 event = _lttng_event_create(enabler->chan,
1392 NULL, NULL, desc,
1393 LTTNG_KERNEL_TRACEPOINT);
1394 if (!event) {
1395 printk(KERN_INFO "LTTng: Unable to create event %s\n",
1396 probe_desc->event_desc[i]->name);
1397 }
1398 }
1399 }
1400 }
1401
1402 static
1403 void lttng_create_syscall_if_missing(struct lttng_enabler *enabler)
1404 {
1405 int ret;
1406
1407 ret = lttng_syscalls_register(enabler->chan, NULL);
1408 WARN_ON_ONCE(ret);
1409 }
1410
1411 /*
1412 * Create struct lttng_event if it is missing and present in the list of
1413 * tracepoint probes.
1414 * Should be called with sessions mutex held.
1415 */
1416 static
1417 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
1418 {
1419 switch (enabler->event_param.instrumentation) {
1420 case LTTNG_KERNEL_TRACEPOINT:
1421 lttng_create_tracepoint_if_missing(enabler);
1422 break;
1423 case LTTNG_KERNEL_SYSCALL:
1424 lttng_create_syscall_if_missing(enabler);
1425 break;
1426 default:
1427 WARN_ON_ONCE(1);
1428 break;
1429 }
1430 }
1431
1432 /*
1433 * Create events associated with an enabler (if not already present),
1434 * and add backward reference from the event to the enabler.
1435 * Should be called with sessions mutex held.
1436 */
1437 static
1438 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
1439 {
1440 struct lttng_channel *chan = enabler->chan;
1441 struct lttng_session *session = chan->session;
1442 struct lttng_event *event;
1443
1444 if (enabler->event_param.instrumentation == LTTNG_KERNEL_SYSCALL &&
1445 enabler->event_param.u.syscall.entryexit == LTTNG_KERNEL_SYSCALL_ENTRYEXIT &&
1446 enabler->event_param.u.syscall.abi == LTTNG_KERNEL_SYSCALL_ABI_ALL &&
1447 enabler->event_param.u.syscall.match == LTTNG_SYSCALL_MATCH_NAME &&
1448 !strcmp(enabler->event_param.name, "*")) {
1449 if (enabler->enabled)
1450 WRITE_ONCE(chan->syscall_all, 1);
1451 else
1452 WRITE_ONCE(chan->syscall_all, 0);
1453 }
1454
1455 /* First ensure that probe events are created for this enabler. */
1456 lttng_create_event_if_missing(enabler);
1457
1458 /* For each event matching enabler in session event list. */
1459 list_for_each_entry(event, &session->events, list) {
1460 struct lttng_enabler_ref *enabler_ref;
1461
1462 if (!lttng_event_match_enabler(event, enabler))
1463 continue;
1464 enabler_ref = lttng_event_enabler_ref(event, enabler);
1465 if (!enabler_ref) {
1466 /*
1467 * If no backward ref, create it.
1468 * Add backward ref from event to enabler.
1469 */
1470 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1471 if (!enabler_ref)
1472 return -ENOMEM;
1473 enabler_ref->ref = enabler;
1474 list_add(&enabler_ref->node,
1475 &event->enablers_ref_head);
1476 }
1477
1478 /*
1479 * Link filter bytecodes if not linked yet.
1480 */
1481 lttng_enabler_event_link_bytecode(event, enabler);
1482
1483 /* TODO: merge event context. */
1484 }
1485 return 0;
1486 }
1487
1488 /*
1489 * Called at module load: connect the probe on all enablers matching
1490 * this event.
1491 * Called with sessions lock held.
1492 */
1493 int lttng_fix_pending_events(void)
1494 {
1495 struct lttng_session *session;
1496
1497 list_for_each_entry(session, &sessions, list)
1498 lttng_session_lazy_sync_enablers(session);
1499 return 0;
1500 }
1501
1502 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1503 struct lttng_kernel_event *event_param,
1504 struct lttng_channel *chan)
1505 {
1506 struct lttng_enabler *enabler;
1507
1508 enabler = kzalloc(sizeof(*enabler), GFP_KERNEL);
1509 if (!enabler)
1510 return NULL;
1511 enabler->type = type;
1512 INIT_LIST_HEAD(&enabler->filter_bytecode_head);
1513 memcpy(&enabler->event_param, event_param,
1514 sizeof(enabler->event_param));
1515 enabler->chan = chan;
1516 /* ctx left NULL */
1517 enabler->enabled = 0;
1518 enabler->evtype = LTTNG_TYPE_ENABLER;
1519 mutex_lock(&sessions_mutex);
1520 list_add(&enabler->node, &enabler->chan->session->enablers_head);
1521 lttng_session_lazy_sync_enablers(enabler->chan->session);
1522 mutex_unlock(&sessions_mutex);
1523 return enabler;
1524 }
1525
1526 int lttng_enabler_enable(struct lttng_enabler *enabler)
1527 {
1528 mutex_lock(&sessions_mutex);
1529 enabler->enabled = 1;
1530 lttng_session_lazy_sync_enablers(enabler->chan->session);
1531 mutex_unlock(&sessions_mutex);
1532 return 0;
1533 }
1534
1535 int lttng_enabler_disable(struct lttng_enabler *enabler)
1536 {
1537 mutex_lock(&sessions_mutex);
1538 enabler->enabled = 0;
1539 lttng_session_lazy_sync_enablers(enabler->chan->session);
1540 mutex_unlock(&sessions_mutex);
1541 return 0;
1542 }
1543
1544 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1545 struct lttng_kernel_filter_bytecode __user *bytecode)
1546 {
1547 struct lttng_filter_bytecode_node *bytecode_node;
1548 uint32_t bytecode_len;
1549 int ret;
1550
1551 ret = get_user(bytecode_len, &bytecode->len);
1552 if (ret)
1553 return ret;
1554 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1555 GFP_KERNEL);
1556 if (!bytecode_node)
1557 return -ENOMEM;
1558 ret = copy_from_user(&bytecode_node->bc, bytecode,
1559 sizeof(*bytecode) + bytecode_len);
1560 if (ret)
1561 goto error_free;
1562 bytecode_node->enabler = enabler;
1563 /* Enforce length based on allocated size */
1564 bytecode_node->bc.len = bytecode_len;
1565 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1566 lttng_session_lazy_sync_enablers(enabler->chan->session);
1567 return 0;
1568
1569 error_free:
1570 kfree(bytecode_node);
1571 return ret;
1572 }
1573
1574 int lttng_event_add_callsite(struct lttng_event *event,
1575 struct lttng_kernel_event_callsite __user *callsite)
1576 {
1577
1578 switch (event->instrumentation) {
1579 case LTTNG_KERNEL_UPROBE:
1580 return lttng_uprobes_add_callsite(event, callsite);
1581 default:
1582 return -EINVAL;
1583 }
1584 }
1585
1586 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1587 struct lttng_kernel_context *context_param)
1588 {
1589 return -ENOSYS;
1590 }
1591
1592 static
1593 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1594 {
1595 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1596
1597 /* Destroy filter bytecode */
1598 list_for_each_entry_safe(filter_node, tmp_filter_node,
1599 &enabler->filter_bytecode_head, node) {
1600 kfree(filter_node);
1601 }
1602
1603 /* Destroy contexts */
1604 lttng_destroy_context(enabler->ctx);
1605
1606 list_del(&enabler->node);
1607 kfree(enabler);
1608 }
1609
1610 /*
1611 * lttng_session_sync_enablers should be called just before starting a
1612 * session.
1613 * Should be called with sessions mutex held.
1614 */
1615 static
1616 void lttng_session_sync_enablers(struct lttng_session *session)
1617 {
1618 struct lttng_enabler *enabler;
1619 struct lttng_event *event;
1620
1621 list_for_each_entry(enabler, &session->enablers_head, node)
1622 lttng_enabler_ref_events(enabler);
1623 /*
1624 * For each event, if at least one of its enablers is enabled,
1625 * and its channel and session transient states are enabled, we
1626 * enable the event, else we disable it.
1627 */
1628 list_for_each_entry(event, &session->events, list) {
1629 struct lttng_enabler_ref *enabler_ref;
1630 struct lttng_bytecode_runtime *runtime;
1631 int enabled = 0, has_enablers_without_bytecode = 0;
1632
1633 switch (event->instrumentation) {
1634 case LTTNG_KERNEL_TRACEPOINT:
1635 case LTTNG_KERNEL_SYSCALL:
1636 /* Enable events */
1637 list_for_each_entry(enabler_ref,
1638 &event->enablers_ref_head, node) {
1639 if (enabler_ref->ref->enabled) {
1640 enabled = 1;
1641 break;
1642 }
1643 }
1644 break;
1645 default:
1646 /* Not handled with lazy sync. */
1647 continue;
1648 }
1649 /*
1650 * Enabled state is based on union of enablers, with
1651 * intesection of session and channel transient enable
1652 * states.
1653 */
1654 enabled = enabled && session->tstate && event->chan->tstate;
1655
1656 WRITE_ONCE(event->enabled, enabled);
1657 /*
1658 * Sync tracepoint registration with event enabled
1659 * state.
1660 */
1661 if (enabled) {
1662 register_event(event);
1663 } else {
1664 _lttng_event_unregister(event);
1665 }
1666
1667 /* Check if has enablers without bytecode enabled */
1668 list_for_each_entry(enabler_ref,
1669 &event->enablers_ref_head, node) {
1670 if (enabler_ref->ref->enabled
1671 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1672 has_enablers_without_bytecode = 1;
1673 break;
1674 }
1675 }
1676 event->has_enablers_without_bytecode =
1677 has_enablers_without_bytecode;
1678
1679 /* Enable filters */
1680 list_for_each_entry(runtime,
1681 &event->bytecode_runtime_head, node)
1682 lttng_filter_sync_state(runtime);
1683 }
1684 }
1685
1686 /*
1687 * Apply enablers to session events, adding events to session if need
1688 * be. It is required after each modification applied to an active
1689 * session, and right before session "start".
1690 * "lazy" sync means we only sync if required.
1691 * Should be called with sessions mutex held.
1692 */
1693 static
1694 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1695 {
1696 /* We can skip if session is not active */
1697 if (!session->active)
1698 return;
1699 lttng_session_sync_enablers(session);
1700 }
1701
1702 /*
1703 * Serialize at most one packet worth of metadata into a metadata
1704 * channel.
1705 * We grab the metadata cache mutex to get exclusive access to our metadata
1706 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1707 * allows us to do racy operations such as looking for remaining space left in
1708 * packet and write, since mutual exclusion protects us from concurrent writes.
1709 * Mutual exclusion on the metadata cache allow us to read the cache content
1710 * without racing against reallocation of the cache by updates.
1711 * Returns the number of bytes written in the channel, 0 if no data
1712 * was written and a negative value on error.
1713 */
1714 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1715 struct channel *chan, bool *coherent)
1716 {
1717 struct lib_ring_buffer_ctx ctx;
1718 int ret = 0;
1719 size_t len, reserve_len;
1720
1721 /*
1722 * Ensure we support mutiple get_next / put sequences followed by
1723 * put_next. The metadata cache lock protects reading the metadata
1724 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1725 * "flush" operations on the buffer invoked by different processes.
1726 * Moreover, since the metadata cache memory can be reallocated, we
1727 * need to have exclusive access against updates even though we only
1728 * read it.
1729 */
1730 mutex_lock(&stream->metadata_cache->lock);
1731 WARN_ON(stream->metadata_in < stream->metadata_out);
1732 if (stream->metadata_in != stream->metadata_out)
1733 goto end;
1734
1735 /* Metadata regenerated, change the version. */
1736 if (stream->metadata_cache->version != stream->version)
1737 stream->version = stream->metadata_cache->version;
1738
1739 len = stream->metadata_cache->metadata_written -
1740 stream->metadata_in;
1741 if (!len)
1742 goto end;
1743 reserve_len = min_t(size_t,
1744 stream->transport->ops.packet_avail_size(chan),
1745 len);
1746 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
1747 sizeof(char), -1);
1748 /*
1749 * If reservation failed, return an error to the caller.
1750 */
1751 ret = stream->transport->ops.event_reserve(&ctx, 0);
1752 if (ret != 0) {
1753 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1754 stream->coherent = false;
1755 goto end;
1756 }
1757 stream->transport->ops.event_write(&ctx,
1758 stream->metadata_cache->data + stream->metadata_in,
1759 reserve_len);
1760 stream->transport->ops.event_commit(&ctx);
1761 stream->metadata_in += reserve_len;
1762 if (reserve_len < len)
1763 stream->coherent = false;
1764 else
1765 stream->coherent = true;
1766 ret = reserve_len;
1767
1768 end:
1769 if (coherent)
1770 *coherent = stream->coherent;
1771 mutex_unlock(&stream->metadata_cache->lock);
1772 return ret;
1773 }
1774
1775 static
1776 void lttng_metadata_begin(struct lttng_session *session)
1777 {
1778 if (atomic_inc_return(&session->metadata_cache->producing) == 1)
1779 mutex_lock(&session->metadata_cache->lock);
1780 }
1781
1782 static
1783 void lttng_metadata_end(struct lttng_session *session)
1784 {
1785 WARN_ON_ONCE(!atomic_read(&session->metadata_cache->producing));
1786 if (atomic_dec_return(&session->metadata_cache->producing) == 0) {
1787 struct lttng_metadata_stream *stream;
1788
1789 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1790 wake_up_interruptible(&stream->read_wait);
1791 mutex_unlock(&session->metadata_cache->lock);
1792 }
1793 }
1794
1795 /*
1796 * Write the metadata to the metadata cache.
1797 * Must be called with sessions_mutex held.
1798 * The metadata cache lock protects us from concurrent read access from
1799 * thread outputting metadata content to ring buffer.
1800 * The content of the printf is printed as a single atomic metadata
1801 * transaction.
1802 */
1803 int lttng_metadata_printf(struct lttng_session *session,
1804 const char *fmt, ...)
1805 {
1806 char *str;
1807 size_t len;
1808 va_list ap;
1809
1810 WARN_ON_ONCE(!LTTNG_READ_ONCE(session->active));
1811
1812 va_start(ap, fmt);
1813 str = kvasprintf(GFP_KERNEL, fmt, ap);
1814 va_end(ap);
1815 if (!str)
1816 return -ENOMEM;
1817
1818 len = strlen(str);
1819 WARN_ON_ONCE(!atomic_read(&session->metadata_cache->producing));
1820 if (session->metadata_cache->metadata_written + len >
1821 session->metadata_cache->cache_alloc) {
1822 char *tmp_cache_realloc;
1823 unsigned int tmp_cache_alloc_size;
1824
1825 tmp_cache_alloc_size = max_t(unsigned int,
1826 session->metadata_cache->cache_alloc + len,
1827 session->metadata_cache->cache_alloc << 1);
1828 tmp_cache_realloc = vzalloc(tmp_cache_alloc_size);
1829 if (!tmp_cache_realloc)
1830 goto err;
1831 if (session->metadata_cache->data) {
1832 memcpy(tmp_cache_realloc,
1833 session->metadata_cache->data,
1834 session->metadata_cache->cache_alloc);
1835 vfree(session->metadata_cache->data);
1836 }
1837
1838 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1839 session->metadata_cache->data = tmp_cache_realloc;
1840 }
1841 memcpy(session->metadata_cache->data +
1842 session->metadata_cache->metadata_written,
1843 str, len);
1844 session->metadata_cache->metadata_written += len;
1845 kfree(str);
1846
1847 return 0;
1848
1849 err:
1850 kfree(str);
1851 return -ENOMEM;
1852 }
1853
1854 static
1855 int print_tabs(struct lttng_session *session, size_t nesting)
1856 {
1857 size_t i;
1858
1859 for (i = 0; i < nesting; i++) {
1860 int ret;
1861
1862 ret = lttng_metadata_printf(session, " ");
1863 if (ret) {
1864 return ret;
1865 }
1866 }
1867 return 0;
1868 }
1869
1870 static
1871 int lttng_field_name_statedump(struct lttng_session *session,
1872 const struct lttng_event_field *field,
1873 size_t nesting)
1874 {
1875 return lttng_metadata_printf(session, " _%s;\n", field->name);
1876 }
1877
1878 static
1879 int _lttng_integer_type_statedump(struct lttng_session *session,
1880 const struct lttng_type *type,
1881 size_t nesting)
1882 {
1883 int ret;
1884
1885 WARN_ON_ONCE(type->atype != atype_integer);
1886 ret = print_tabs(session, nesting);
1887 if (ret)
1888 return ret;
1889 ret = lttng_metadata_printf(session,
1890 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }",
1891 type->u.integer.size,
1892 type->u.integer.alignment,
1893 type->u.integer.signedness,
1894 (type->u.integer.encoding == lttng_encode_none)
1895 ? "none"
1896 : (type->u.integer.encoding == lttng_encode_UTF8)
1897 ? "UTF8"
1898 : "ASCII",
1899 type->u.integer.base,
1900 #if __BYTE_ORDER == __BIG_ENDIAN
1901 type->u.integer.reverse_byte_order ? " byte_order = le;" : ""
1902 #else
1903 type->u.integer.reverse_byte_order ? " byte_order = be;" : ""
1904 #endif
1905 );
1906 return ret;
1907 }
1908
1909 /*
1910 * Must be called with sessions_mutex held.
1911 */
1912 static
1913 int _lttng_struct_type_statedump(struct lttng_session *session,
1914 const struct lttng_type *type,
1915 size_t nesting)
1916 {
1917 int ret;
1918 uint32_t i, nr_fields;
1919 unsigned int alignment;
1920
1921 WARN_ON_ONCE(type->atype != atype_struct_nestable);
1922
1923 ret = print_tabs(session, nesting);
1924 if (ret)
1925 return ret;
1926 ret = lttng_metadata_printf(session,
1927 "struct {\n");
1928 if (ret)
1929 return ret;
1930 nr_fields = type->u.struct_nestable.nr_fields;
1931 for (i = 0; i < nr_fields; i++) {
1932 const struct lttng_event_field *iter_field;
1933
1934 iter_field = &type->u.struct_nestable.fields[i];
1935 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1936 if (ret)
1937 return ret;
1938 }
1939 ret = print_tabs(session, nesting);
1940 if (ret)
1941 return ret;
1942 alignment = type->u.struct_nestable.alignment;
1943 if (alignment) {
1944 ret = lttng_metadata_printf(session,
1945 "} align(%u)",
1946 alignment);
1947 } else {
1948 ret = lttng_metadata_printf(session,
1949 "}");
1950 }
1951 return ret;
1952 }
1953
1954 /*
1955 * Must be called with sessions_mutex held.
1956 */
1957 static
1958 int _lttng_struct_field_statedump(struct lttng_session *session,
1959 const struct lttng_event_field *field,
1960 size_t nesting)
1961 {
1962 int ret;
1963
1964 ret = _lttng_struct_type_statedump(session,
1965 &field->type, nesting);
1966 if (ret)
1967 return ret;
1968 return lttng_field_name_statedump(session, field, nesting);
1969 }
1970
1971 /*
1972 * Must be called with sessions_mutex held.
1973 */
1974 static
1975 int _lttng_variant_type_statedump(struct lttng_session *session,
1976 const struct lttng_type *type,
1977 size_t nesting)
1978 {
1979 int ret;
1980 uint32_t i, nr_choices;
1981
1982 WARN_ON_ONCE(type->atype != atype_variant_nestable);
1983 /*
1984 * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way.
1985 */
1986 if (type->u.variant_nestable.alignment != 0)
1987 return -EINVAL;
1988 ret = print_tabs(session, nesting);
1989 if (ret)
1990 return ret;
1991 ret = lttng_metadata_printf(session,
1992 "variant <_%s> {\n",
1993 type->u.variant_nestable.tag_name);
1994 if (ret)
1995 return ret;
1996 nr_choices = type->u.variant_nestable.nr_choices;
1997 for (i = 0; i < nr_choices; i++) {
1998 const struct lttng_event_field *iter_field;
1999
2000 iter_field = &type->u.variant_nestable.choices[i];
2001 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
2002 if (ret)
2003 return ret;
2004 }
2005 ret = print_tabs(session, nesting);
2006 if (ret)
2007 return ret;
2008 ret = lttng_metadata_printf(session,
2009 "}");
2010 return ret;
2011 }
2012
2013 /*
2014 * Must be called with sessions_mutex held.
2015 */
2016 static
2017 int _lttng_variant_field_statedump(struct lttng_session *session,
2018 const struct lttng_event_field *field,
2019 size_t nesting)
2020 {
2021 int ret;
2022
2023 ret = _lttng_variant_type_statedump(session,
2024 &field->type, nesting);
2025 if (ret)
2026 return ret;
2027 return lttng_field_name_statedump(session, field, nesting);
2028 }
2029
2030 /*
2031 * Must be called with sessions_mutex held.
2032 */
2033 static
2034 int _lttng_array_field_statedump(struct lttng_session *session,
2035 const struct lttng_event_field *field,
2036 size_t nesting)
2037 {
2038 int ret;
2039 const struct lttng_type *elem_type;
2040
2041 WARN_ON_ONCE(field->type.atype != atype_array_nestable);
2042
2043 if (field->type.u.array_nestable.alignment) {
2044 ret = print_tabs(session, nesting);
2045 if (ret)
2046 return ret;
2047 ret = lttng_metadata_printf(session,
2048 "struct { } align(%u) _%s_padding;\n",
2049 field->type.u.array_nestable.alignment * CHAR_BIT,
2050 field->name);
2051 if (ret)
2052 return ret;
2053 }
2054 /*
2055 * Nested compound types: Only array of structures and variants are
2056 * currently supported.
2057 */
2058 elem_type = field->type.u.array_nestable.elem_type;
2059 switch (elem_type->atype) {
2060 case atype_integer:
2061 case atype_struct_nestable:
2062 case atype_variant_nestable:
2063 ret = _lttng_type_statedump(session, elem_type, nesting);
2064 if (ret)
2065 return ret;
2066 break;
2067
2068 default:
2069 return -EINVAL;
2070 }
2071 ret = lttng_metadata_printf(session,
2072 " _%s[%u];\n",
2073 field->name,
2074 field->type.u.array_nestable.length);
2075 return ret;
2076 }
2077
2078 /*
2079 * Must be called with sessions_mutex held.
2080 */
2081 static
2082 int _lttng_sequence_field_statedump(struct lttng_session *session,
2083 const struct lttng_event_field *field,
2084 size_t nesting)
2085 {
2086 int ret;
2087 const char *length_name;
2088 const struct lttng_type *elem_type;
2089
2090 WARN_ON_ONCE(field->type.atype != atype_sequence_nestable);
2091
2092 length_name = field->type.u.sequence_nestable.length_name;
2093
2094 if (field->type.u.sequence_nestable.alignment) {
2095 ret = print_tabs(session, nesting);
2096 if (ret)
2097 return ret;
2098 ret = lttng_metadata_printf(session,
2099 "struct { } align(%u) _%s_padding;\n",
2100 field->type.u.sequence_nestable.alignment * CHAR_BIT,
2101 field->name);
2102 if (ret)
2103 return ret;
2104 }
2105
2106 /*
2107 * Nested compound types: Only array of structures and variants are
2108 * currently supported.
2109 */
2110 elem_type = field->type.u.sequence_nestable.elem_type;
2111 switch (elem_type->atype) {
2112 case atype_integer:
2113 case atype_struct_nestable:
2114 case atype_variant_nestable:
2115 ret = _lttng_type_statedump(session, elem_type, nesting);
2116 if (ret)
2117 return ret;
2118 break;
2119
2120 default:
2121 return -EINVAL;
2122 }
2123 ret = lttng_metadata_printf(session,
2124 " _%s[ _%s ];\n",
2125 field->name,
2126 field->type.u.sequence_nestable.length_name);
2127 return ret;
2128 }
2129
2130 /*
2131 * Must be called with sessions_mutex held.
2132 */
2133 static
2134 int _lttng_enum_type_statedump(struct lttng_session *session,
2135 const struct lttng_type *type,
2136 size_t nesting)
2137 {
2138 const struct lttng_enum_desc *enum_desc;
2139 const struct lttng_type *container_type;
2140 int ret;
2141 unsigned int i, nr_entries;
2142
2143 container_type = type->u.enum_nestable.container_type;
2144 if (container_type->atype != atype_integer) {
2145 ret = -EINVAL;
2146 goto end;
2147 }
2148 enum_desc = type->u.enum_nestable.desc;
2149 nr_entries = enum_desc->nr_entries;
2150
2151 ret = print_tabs(session, nesting);
2152 if (ret)
2153 goto end;
2154 ret = lttng_metadata_printf(session, "enum : ");
2155 if (ret)
2156 goto end;
2157 ret = _lttng_integer_type_statedump(session, container_type, 0);
2158 if (ret)
2159 goto end;
2160 ret = lttng_metadata_printf(session, " {\n");
2161 if (ret)
2162 goto end;
2163 /* Dump all entries */
2164 for (i = 0; i < nr_entries; i++) {
2165 const struct lttng_enum_entry *entry = &enum_desc->entries[i];
2166 int j, len;
2167
2168 ret = print_tabs(session, nesting + 1);
2169 if (ret)
2170 goto end;
2171 ret = lttng_metadata_printf(session,
2172 "\"");
2173 if (ret)
2174 goto end;
2175 len = strlen(entry->string);
2176 /* Escape the character '"' */
2177 for (j = 0; j < len; j++) {
2178 char c = entry->string[j];
2179
2180 switch (c) {
2181 case '"':
2182 ret = lttng_metadata_printf(session,
2183 "\\\"");
2184 break;
2185 case '\\':
2186 ret = lttng_metadata_printf(session,
2187 "\\\\");
2188 break;
2189 default:
2190 ret = lttng_metadata_printf(session,
2191 "%c", c);
2192 break;
2193 }
2194 if (ret)
2195 goto end;
2196 }
2197 ret = lttng_metadata_printf(session, "\"");
2198 if (ret)
2199 goto end;
2200
2201 if (entry->options.is_auto) {
2202 ret = lttng_metadata_printf(session, ",\n");
2203 if (ret)
2204 goto end;
2205 } else {
2206 ret = lttng_metadata_printf(session,
2207 " = ");
2208 if (ret)
2209 goto end;
2210 if (entry->start.signedness)
2211 ret = lttng_metadata_printf(session,
2212 "%lld", (long long) entry->start.value);
2213 else
2214 ret = lttng_metadata_printf(session,
2215 "%llu", entry->start.value);
2216 if (ret)
2217 goto end;
2218 if (entry->start.signedness == entry->end.signedness &&
2219 entry->start.value
2220 == entry->end.value) {
2221 ret = lttng_metadata_printf(session,
2222 ",\n");
2223 } else {
2224 if (entry->end.signedness) {
2225 ret = lttng_metadata_printf(session,
2226 " ... %lld,\n",
2227 (long long) entry->end.value);
2228 } else {
2229 ret = lttng_metadata_printf(session,
2230 " ... %llu,\n",
2231 entry->end.value);
2232 }
2233 }
2234 if (ret)
2235 goto end;
2236 }
2237 }
2238 ret = print_tabs(session, nesting);
2239 if (ret)
2240 goto end;
2241 ret = lttng_metadata_printf(session, "}");
2242 end:
2243 return ret;
2244 }
2245
2246 /*
2247 * Must be called with sessions_mutex held.
2248 */
2249 static
2250 int _lttng_enum_field_statedump(struct lttng_session *session,
2251 const struct lttng_event_field *field,
2252 size_t nesting)
2253 {
2254 int ret;
2255
2256 ret = _lttng_enum_type_statedump(session, &field->type, nesting);
2257 if (ret)
2258 return ret;
2259 return lttng_field_name_statedump(session, field, nesting);
2260 }
2261
2262 static
2263 int _lttng_integer_field_statedump(struct lttng_session *session,
2264 const struct lttng_event_field *field,
2265 size_t nesting)
2266 {
2267 int ret;
2268
2269 ret = _lttng_integer_type_statedump(session, &field->type, nesting);
2270 if (ret)
2271 return ret;
2272 return lttng_field_name_statedump(session, field, nesting);
2273 }
2274
2275 static
2276 int _lttng_string_type_statedump(struct lttng_session *session,
2277 const struct lttng_type *type,
2278 size_t nesting)
2279 {
2280 int ret;
2281
2282 WARN_ON_ONCE(type->atype != atype_string);
2283 /* Default encoding is UTF8 */
2284 ret = print_tabs(session, nesting);
2285 if (ret)
2286 return ret;
2287 ret = lttng_metadata_printf(session,
2288 "string%s",
2289 type->u.string.encoding == lttng_encode_ASCII ?
2290 " { encoding = ASCII; }" : "");
2291 return ret;
2292 }
2293
2294 static
2295 int _lttng_string_field_statedump(struct lttng_session *session,
2296 const struct lttng_event_field *field,
2297 size_t nesting)
2298 {
2299 int ret;
2300
2301 WARN_ON_ONCE(field->type.atype != atype_string);
2302 ret = _lttng_string_type_statedump(session, &field->type, nesting);
2303 if (ret)
2304 return ret;
2305 return lttng_field_name_statedump(session, field, nesting);
2306 }
2307
2308 /*
2309 * Must be called with sessions_mutex held.
2310 */
2311 static
2312 int _lttng_type_statedump(struct lttng_session *session,
2313 const struct lttng_type *type,
2314 size_t nesting)
2315 {
2316 int ret = 0;
2317
2318 switch (type->atype) {
2319 case atype_integer:
2320 ret = _lttng_integer_type_statedump(session, type, nesting);
2321 break;
2322 case atype_enum_nestable:
2323 ret = _lttng_enum_type_statedump(session, type, nesting);
2324 break;
2325 case atype_string:
2326 ret = _lttng_string_type_statedump(session, type, nesting);
2327 break;
2328 case atype_struct_nestable:
2329 ret = _lttng_struct_type_statedump(session, type, nesting);
2330 break;
2331 case atype_variant_nestable:
2332 ret = _lttng_variant_type_statedump(session, type, nesting);
2333 break;
2334
2335 /* Nested arrays and sequences are not supported yet. */
2336 case atype_array_nestable:
2337 case atype_sequence_nestable:
2338 default:
2339 WARN_ON_ONCE(1);
2340 return -EINVAL;
2341 }
2342 return ret;
2343 }
2344
2345 /*
2346 * Must be called with sessions_mutex held.
2347 */
2348 static
2349 int _lttng_field_statedump(struct lttng_session *session,
2350 const struct lttng_event_field *field,
2351 size_t nesting)
2352 {
2353 int ret = 0;
2354
2355 switch (field->type.atype) {
2356 case atype_integer:
2357 ret = _lttng_integer_field_statedump(session, field, nesting);
2358 break;
2359 case atype_enum_nestable:
2360 ret = _lttng_enum_field_statedump(session, field, nesting);
2361 break;
2362 case atype_string:
2363 ret = _lttng_string_field_statedump(session, field, nesting);
2364 break;
2365 case atype_struct_nestable:
2366 ret = _lttng_struct_field_statedump(session, field, nesting);
2367 break;
2368 case atype_array_nestable:
2369 ret = _lttng_array_field_statedump(session, field, nesting);
2370 break;
2371 case atype_sequence_nestable:
2372 ret = _lttng_sequence_field_statedump(session, field, nesting);
2373 break;
2374 case atype_variant_nestable:
2375 ret = _lttng_variant_field_statedump(session, field, nesting);
2376 break;
2377
2378 default:
2379 WARN_ON_ONCE(1);
2380 return -EINVAL;
2381 }
2382 return ret;
2383 }
2384
2385 static
2386 int _lttng_context_metadata_statedump(struct lttng_session *session,
2387 struct lttng_ctx *ctx)
2388 {
2389 int ret = 0;
2390 int i;
2391
2392 if (!ctx)
2393 return 0;
2394 for (i = 0; i < ctx->nr_fields; i++) {
2395 const struct lttng_ctx_field *field = &ctx->fields[i];
2396
2397 ret = _lttng_field_statedump(session, &field->event_field, 2);
2398 if (ret)
2399 return ret;
2400 }
2401 return ret;
2402 }
2403
2404 static
2405 int _lttng_fields_metadata_statedump(struct lttng_session *session,
2406 struct lttng_event *event)
2407 {
2408 const struct lttng_event_desc *desc = event->desc;
2409 int ret = 0;
2410 int i;
2411
2412 for (i = 0; i < desc->nr_fields; i++) {
2413 const struct lttng_event_field *field = &desc->fields[i];
2414
2415 ret = _lttng_field_statedump(session, field, 2);
2416 if (ret)
2417 return ret;
2418 }
2419 return ret;
2420 }
2421
2422 /*
2423 * Must be called with sessions_mutex held.
2424 * The entire event metadata is printed as a single atomic metadata
2425 * transaction.
2426 */
2427 static
2428 int _lttng_event_metadata_statedump(struct lttng_session *session,
2429 struct lttng_channel *chan,
2430 struct lttng_event *event)
2431 {
2432 int ret = 0;
2433
2434 if (event->metadata_dumped || !LTTNG_READ_ONCE(session->active))
2435 return 0;
2436 if (chan->channel_type == METADATA_CHANNEL)
2437 return 0;
2438
2439 lttng_metadata_begin(session);
2440
2441 ret = lttng_metadata_printf(session,
2442 "event {\n"
2443 " name = \"%s\";\n"
2444 " id = %u;\n"
2445 " stream_id = %u;\n",
2446 event->desc->name,
2447 event->id,
2448 event->chan->id);
2449 if (ret)
2450 goto end;
2451
2452 if (event->ctx) {
2453 ret = lttng_metadata_printf(session,
2454 " context := struct {\n");
2455 if (ret)
2456 goto end;
2457 }
2458 ret = _lttng_context_metadata_statedump(session, event->ctx);
2459 if (ret)
2460 goto end;
2461 if (event->ctx) {
2462 ret = lttng_metadata_printf(session,
2463 " };\n");
2464 if (ret)
2465 goto end;
2466 }
2467
2468 ret = lttng_metadata_printf(session,
2469 " fields := struct {\n"
2470 );
2471 if (ret)
2472 goto end;
2473
2474 ret = _lttng_fields_metadata_statedump(session, event);
2475 if (ret)
2476 goto end;
2477
2478 /*
2479 * LTTng space reservation can only reserve multiples of the
2480 * byte size.
2481 */
2482 ret = lttng_metadata_printf(session,
2483 " };\n"
2484 "};\n\n");
2485 if (ret)
2486 goto end;
2487
2488 event->metadata_dumped = 1;
2489 end:
2490 lttng_metadata_end(session);
2491 return ret;
2492
2493 }
2494
2495 /*
2496 * Must be called with sessions_mutex held.
2497 * The entire channel metadata is printed as a single atomic metadata
2498 * transaction.
2499 */
2500 static
2501 int _lttng_channel_metadata_statedump(struct lttng_session *session,
2502 struct lttng_channel *chan)
2503 {
2504 int ret = 0;
2505
2506 if (chan->metadata_dumped || !LTTNG_READ_ONCE(session->active))
2507 return 0;
2508
2509 if (chan->channel_type == METADATA_CHANNEL)
2510 return 0;
2511
2512 lttng_metadata_begin(session);
2513
2514 WARN_ON_ONCE(!chan->header_type);
2515 ret = lttng_metadata_printf(session,
2516 "stream {\n"
2517 " id = %u;\n"
2518 " event.header := %s;\n"
2519 " packet.context := struct packet_context;\n",
2520 chan->id,
2521 chan->header_type == 1 ? "struct event_header_compact" :
2522 "struct event_header_large");
2523 if (ret)
2524 goto end;
2525
2526 if (chan->ctx) {
2527 ret = lttng_metadata_printf(session,
2528 " event.context := struct {\n");
2529 if (ret)
2530 goto end;
2531 }
2532 ret = _lttng_context_metadata_statedump(session, chan->ctx);
2533 if (ret)
2534 goto end;
2535 if (chan->ctx) {
2536 ret = lttng_metadata_printf(session,
2537 " };\n");
2538 if (ret)
2539 goto end;
2540 }
2541
2542 ret = lttng_metadata_printf(session,
2543 "};\n\n");
2544
2545 chan->metadata_dumped = 1;
2546 end:
2547 lttng_metadata_end(session);
2548 return ret;
2549 }
2550
2551 /*
2552 * Must be called with sessions_mutex held.
2553 */
2554 static
2555 int _lttng_stream_packet_context_declare(struct lttng_session *session)
2556 {
2557 return lttng_metadata_printf(session,
2558 "struct packet_context {\n"
2559 " uint64_clock_monotonic_t timestamp_begin;\n"
2560 " uint64_clock_monotonic_t timestamp_end;\n"
2561 " uint64_t content_size;\n"
2562 " uint64_t packet_size;\n"
2563 " uint64_t packet_seq_num;\n"
2564 " unsigned long events_discarded;\n"
2565 " uint32_t cpu_id;\n"
2566 "};\n\n"
2567 );
2568 }
2569
2570 /*
2571 * Compact header:
2572 * id: range: 0 - 30.
2573 * id 31 is reserved to indicate an extended header.
2574 *
2575 * Large header:
2576 * id: range: 0 - 65534.
2577 * id 65535 is reserved to indicate an extended header.
2578 *
2579 * Must be called with sessions_mutex held.
2580 */
2581 static
2582 int _lttng_event_header_declare(struct lttng_session *session)
2583 {
2584 return lttng_metadata_printf(session,
2585 "struct event_header_compact {\n"
2586 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2587 " variant <id> {\n"
2588 " struct {\n"
2589 " uint27_clock_monotonic_t timestamp;\n"
2590 " } compact;\n"
2591 " struct {\n"
2592 " uint32_t id;\n"
2593 " uint64_clock_monotonic_t timestamp;\n"
2594 " } extended;\n"
2595 " } v;\n"
2596 "} align(%u);\n"
2597 "\n"
2598 "struct event_header_large {\n"
2599 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2600 " variant <id> {\n"
2601 " struct {\n"
2602 " uint32_clock_monotonic_t timestamp;\n"
2603 " } compact;\n"
2604 " struct {\n"
2605 " uint32_t id;\n"
2606 " uint64_clock_monotonic_t timestamp;\n"
2607 " } extended;\n"
2608 " } v;\n"
2609 "} align(%u);\n\n",
2610 lttng_alignof(uint32_t) * CHAR_BIT,
2611 lttng_alignof(uint16_t) * CHAR_BIT
2612 );
2613 }
2614
2615 /*
2616 * Approximation of NTP time of day to clock monotonic correlation,
2617 * taken at start of trace.
2618 * Yes, this is only an approximation. Yes, we can (and will) do better
2619 * in future versions.
2620 * This function may return a negative offset. It may happen if the
2621 * system sets the REALTIME clock to 0 after boot.
2622 *
2623 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2624 * y2038 compliant.
2625 */
2626 static
2627 int64_t measure_clock_offset(void)
2628 {
2629 uint64_t monotonic_avg, monotonic[2], realtime;
2630 uint64_t tcf = trace_clock_freq();
2631 int64_t offset;
2632 unsigned long flags;
2633 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2634 struct timespec64 rts = { 0, 0 };
2635 #else
2636 struct timespec rts = { 0, 0 };
2637 #endif
2638
2639 /* Disable interrupts to increase correlation precision. */
2640 local_irq_save(flags);
2641 monotonic[0] = trace_clock_read64();
2642 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2643 ktime_get_real_ts64(&rts);
2644 #else
2645 getnstimeofday(&rts);
2646 #endif
2647 monotonic[1] = trace_clock_read64();
2648 local_irq_restore(flags);
2649
2650 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2651 realtime = (uint64_t) rts.tv_sec * tcf;
2652 if (tcf == NSEC_PER_SEC) {
2653 realtime += rts.tv_nsec;
2654 } else {
2655 uint64_t n = rts.tv_nsec * tcf;
2656
2657 do_div(n, NSEC_PER_SEC);
2658 realtime += n;
2659 }
2660 offset = (int64_t) realtime - monotonic_avg;
2661 return offset;
2662 }
2663
2664 static
2665 int print_escaped_ctf_string(struct lttng_session *session, const char *string)
2666 {
2667 int ret = 0;
2668 size_t i;
2669 char cur;
2670
2671 i = 0;
2672 cur = string[i];
2673 while (cur != '\0') {
2674 switch (cur) {
2675 case '\n':
2676 ret = lttng_metadata_printf(session, "%s", "\\n");
2677 break;
2678 case '\\':
2679 case '"':
2680 ret = lttng_metadata_printf(session, "%c", '\\');
2681 if (ret)
2682 goto error;
2683 /* We still print the current char */
2684 /* Fallthrough */
2685 default:
2686 ret = lttng_metadata_printf(session, "%c", cur);
2687 break;
2688 }
2689
2690 if (ret)
2691 goto error;
2692
2693 cur = string[++i];
2694 }
2695 error:
2696 return ret;
2697 }
2698
2699 static
2700 int print_metadata_escaped_field(struct lttng_session *session, const char *field,
2701 const char *field_value)
2702 {
2703 int ret;
2704
2705 ret = lttng_metadata_printf(session, " %s = \"", field);
2706 if (ret)
2707 goto error;
2708
2709 ret = print_escaped_ctf_string(session, field_value);
2710 if (ret)
2711 goto error;
2712
2713 ret = lttng_metadata_printf(session, "\";\n");
2714
2715 error:
2716 return ret;
2717 }
2718
2719 /*
2720 * Output metadata into this session's metadata buffers.
2721 * Must be called with sessions_mutex held.
2722 */
2723 static
2724 int _lttng_session_metadata_statedump(struct lttng_session *session)
2725 {
2726 unsigned char *uuid_c = session->uuid.b;
2727 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
2728 const char *product_uuid;
2729 struct lttng_channel *chan;
2730 struct lttng_event *event;
2731 int ret = 0;
2732
2733 if (!LTTNG_READ_ONCE(session->active))
2734 return 0;
2735
2736 lttng_metadata_begin(session);
2737
2738 if (session->metadata_dumped)
2739 goto skip_session;
2740
2741 snprintf(uuid_s, sizeof(uuid_s),
2742 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2743 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2744 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2745 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2746 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
2747
2748 ret = lttng_metadata_printf(session,
2749 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2750 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2751 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2752 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2753 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2754 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2755 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2756 "\n"
2757 "trace {\n"
2758 " major = %u;\n"
2759 " minor = %u;\n"
2760 " uuid = \"%s\";\n"
2761 " byte_order = %s;\n"
2762 " packet.header := struct {\n"
2763 " uint32_t magic;\n"
2764 " uint8_t uuid[16];\n"
2765 " uint32_t stream_id;\n"
2766 " uint64_t stream_instance_id;\n"
2767 " };\n"
2768 "};\n\n",
2769 lttng_alignof(uint8_t) * CHAR_BIT,
2770 lttng_alignof(uint16_t) * CHAR_BIT,
2771 lttng_alignof(uint32_t) * CHAR_BIT,
2772 lttng_alignof(uint64_t) * CHAR_BIT,
2773 sizeof(unsigned long) * CHAR_BIT,
2774 lttng_alignof(unsigned long) * CHAR_BIT,
2775 CTF_SPEC_MAJOR,
2776 CTF_SPEC_MINOR,
2777 uuid_s,
2778 #if __BYTE_ORDER == __BIG_ENDIAN
2779 "be"
2780 #else
2781 "le"
2782 #endif
2783 );
2784 if (ret)
2785 goto end;
2786
2787 ret = lttng_metadata_printf(session,
2788 "env {\n"
2789 " hostname = \"%s\";\n"
2790 " domain = \"kernel\";\n"
2791 " sysname = \"%s\";\n"
2792 " kernel_release = \"%s\";\n"
2793 " kernel_version = \"%s\";\n"
2794 " tracer_name = \"lttng-modules\";\n"
2795 " tracer_major = %d;\n"
2796 " tracer_minor = %d;\n"
2797 " tracer_patchlevel = %d;\n"
2798 " trace_buffering_scheme = \"global\";\n",
2799 current->nsproxy->uts_ns->name.nodename,
2800 utsname()->sysname,
2801 utsname()->release,
2802 utsname()->version,
2803 LTTNG_MODULES_MAJOR_VERSION,
2804 LTTNG_MODULES_MINOR_VERSION,
2805 LTTNG_MODULES_PATCHLEVEL_VERSION
2806 );
2807 if (ret)
2808 goto end;
2809
2810 ret = print_metadata_escaped_field(session, "trace_name", session->name);
2811 if (ret)
2812 goto end;
2813 ret = print_metadata_escaped_field(session, "trace_creation_datetime",
2814 session->creation_time);
2815 if (ret)
2816 goto end;
2817
2818 /* Add the product UUID to the 'env' section */
2819 product_uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
2820 if (product_uuid) {
2821 ret = lttng_metadata_printf(session,
2822 " product_uuid = \"%s\";\n",
2823 product_uuid
2824 );
2825 if (ret)
2826 goto end;
2827 }
2828
2829 /* Close the 'env' section */
2830 ret = lttng_metadata_printf(session, "};\n\n");
2831 if (ret)
2832 goto end;
2833
2834 ret = lttng_metadata_printf(session,
2835 "clock {\n"
2836 " name = \"%s\";\n",
2837 trace_clock_name()
2838 );
2839 if (ret)
2840 goto end;
2841
2842 if (!trace_clock_uuid(clock_uuid_s)) {
2843 ret = lttng_metadata_printf(session,
2844 " uuid = \"%s\";\n",
2845 clock_uuid_s
2846 );
2847 if (ret)
2848 goto end;
2849 }
2850
2851 ret = lttng_metadata_printf(session,
2852 " description = \"%s\";\n"
2853 " freq = %llu; /* Frequency, in Hz */\n"
2854 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2855 " offset = %lld;\n"
2856 "};\n\n",
2857 trace_clock_description(),
2858 (unsigned long long) trace_clock_freq(),
2859 (long long) measure_clock_offset()
2860 );
2861 if (ret)
2862 goto end;
2863
2864 ret = lttng_metadata_printf(session,
2865 "typealias integer {\n"
2866 " size = 27; align = 1; signed = false;\n"
2867 " map = clock.%s.value;\n"
2868 "} := uint27_clock_monotonic_t;\n"
2869 "\n"
2870 "typealias integer {\n"
2871 " size = 32; align = %u; signed = false;\n"
2872 " map = clock.%s.value;\n"
2873 "} := uint32_clock_monotonic_t;\n"
2874 "\n"
2875 "typealias integer {\n"
2876 " size = 64; align = %u; signed = false;\n"
2877 " map = clock.%s.value;\n"
2878 "} := uint64_clock_monotonic_t;\n\n",
2879 trace_clock_name(),
2880 lttng_alignof(uint32_t) * CHAR_BIT,
2881 trace_clock_name(),
2882 lttng_alignof(uint64_t) * CHAR_BIT,
2883 trace_clock_name()
2884 );
2885 if (ret)
2886 goto end;
2887
2888 ret = _lttng_stream_packet_context_declare(session);
2889 if (ret)
2890 goto end;
2891
2892 ret = _lttng_event_header_declare(session);
2893 if (ret)
2894 goto end;
2895
2896 skip_session:
2897 list_for_each_entry(chan, &session->chan, list) {
2898 ret = _lttng_channel_metadata_statedump(session, chan);
2899 if (ret)
2900 goto end;
2901 }
2902
2903 list_for_each_entry(event, &session->events, list) {
2904 ret = _lttng_event_metadata_statedump(session, event->chan, event);
2905 if (ret)
2906 goto end;
2907 }
2908 session->metadata_dumped = 1;
2909 end:
2910 lttng_metadata_end(session);
2911 return ret;
2912 }
2913
2914 /**
2915 * lttng_transport_register - LTT transport registration
2916 * @transport: transport structure
2917 *
2918 * Registers a transport which can be used as output to extract the data out of
2919 * LTTng. The module calling this registration function must ensure that no
2920 * trap-inducing code will be executed by the transport functions. E.g.
2921 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2922 * is made visible to the transport function. This registration acts as a
2923 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2924 * after its registration must it synchronize the TLBs.
2925 */
2926 void lttng_transport_register(struct lttng_transport *transport)
2927 {
2928 /*
2929 * Make sure no page fault can be triggered by the module about to be
2930 * registered. We deal with this here so we don't have to call
2931 * vmalloc_sync_mappings() in each module's init.
2932 */
2933 wrapper_vmalloc_sync_mappings();
2934
2935 mutex_lock(&sessions_mutex);
2936 list_add_tail(&transport->node, &lttng_transport_list);
2937 mutex_unlock(&sessions_mutex);
2938 }
2939 EXPORT_SYMBOL_GPL(lttng_transport_register);
2940
2941 /**
2942 * lttng_transport_unregister - LTT transport unregistration
2943 * @transport: transport structure
2944 */
2945 void lttng_transport_unregister(struct lttng_transport *transport)
2946 {
2947 mutex_lock(&sessions_mutex);
2948 list_del(&transport->node);
2949 mutex_unlock(&sessions_mutex);
2950 }
2951 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
2952
2953 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2954
2955 enum cpuhp_state lttng_hp_prepare;
2956 enum cpuhp_state lttng_hp_online;
2957
2958 static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node)
2959 {
2960 struct lttng_cpuhp_node *lttng_node;
2961
2962 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2963 switch (lttng_node->component) {
2964 case LTTNG_RING_BUFFER_FRONTEND:
2965 return 0;
2966 case LTTNG_RING_BUFFER_BACKEND:
2967 return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node);
2968 case LTTNG_RING_BUFFER_ITER:
2969 return 0;
2970 case LTTNG_CONTEXT_PERF_COUNTERS:
2971 return 0;
2972 default:
2973 return -EINVAL;
2974 }
2975 }
2976
2977 static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node)
2978 {
2979 struct lttng_cpuhp_node *lttng_node;
2980
2981 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2982 switch (lttng_node->component) {
2983 case LTTNG_RING_BUFFER_FRONTEND:
2984 return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node);
2985 case LTTNG_RING_BUFFER_BACKEND:
2986 return 0;
2987 case LTTNG_RING_BUFFER_ITER:
2988 return 0;
2989 case LTTNG_CONTEXT_PERF_COUNTERS:
2990 return lttng_cpuhp_perf_counter_dead(cpu, lttng_node);
2991 default:
2992 return -EINVAL;
2993 }
2994 }
2995
2996 static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node)
2997 {
2998 struct lttng_cpuhp_node *lttng_node;
2999
3000 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
3001 switch (lttng_node->component) {
3002 case LTTNG_RING_BUFFER_FRONTEND:
3003 return lttng_cpuhp_rb_frontend_online(cpu, lttng_node);
3004 case LTTNG_RING_BUFFER_BACKEND:
3005 return 0;
3006 case LTTNG_RING_BUFFER_ITER:
3007 return lttng_cpuhp_rb_iter_online(cpu, lttng_node);
3008 case LTTNG_CONTEXT_PERF_COUNTERS:
3009 return lttng_cpuhp_perf_counter_online(cpu, lttng_node);
3010 default:
3011 return -EINVAL;
3012 }
3013 }
3014
3015 static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node)
3016 {
3017 struct lttng_cpuhp_node *lttng_node;
3018
3019 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
3020 switch (lttng_node->component) {
3021 case LTTNG_RING_BUFFER_FRONTEND:
3022 return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node);
3023 case LTTNG_RING_BUFFER_BACKEND:
3024 return 0;
3025 case LTTNG_RING_BUFFER_ITER:
3026 return 0;
3027 case LTTNG_CONTEXT_PERF_COUNTERS:
3028 return 0;
3029 default:
3030 return -EINVAL;
3031 }
3032 }
3033
3034 static int __init lttng_init_cpu_hotplug(void)
3035 {
3036 int ret;
3037
3038 ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare",
3039 lttng_hotplug_prepare,
3040 lttng_hotplug_dead);
3041 if (ret < 0) {
3042 return ret;
3043 }
3044 lttng_hp_prepare = ret;
3045 lttng_rb_set_hp_prepare(ret);
3046
3047 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online",
3048 lttng_hotplug_online,
3049 lttng_hotplug_offline);
3050 if (ret < 0) {
3051 cpuhp_remove_multi_state(lttng_hp_prepare);
3052 lttng_hp_prepare = 0;
3053 return ret;
3054 }
3055 lttng_hp_online = ret;
3056 lttng_rb_set_hp_online(ret);
3057
3058 return 0;
3059 }
3060
3061 static void __exit lttng_exit_cpu_hotplug(void)
3062 {
3063 lttng_rb_set_hp_online(0);
3064 cpuhp_remove_multi_state(lttng_hp_online);
3065 lttng_rb_set_hp_prepare(0);
3066 cpuhp_remove_multi_state(lttng_hp_prepare);
3067 }
3068
3069 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3070 static int lttng_init_cpu_hotplug(void)
3071 {
3072 return 0;
3073 }
3074 static void lttng_exit_cpu_hotplug(void)
3075 {
3076 }
3077 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3078
3079
3080 static int __init lttng_events_init(void)
3081 {
3082 int ret;
3083
3084 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
3085 if (ret)
3086 return ret;
3087 ret = wrapper_get_pfnblock_flags_mask_init();
3088 if (ret)
3089 return ret;
3090 ret = wrapper_get_pageblock_flags_mask_init();
3091 if (ret)
3092 return ret;
3093 ret = lttng_probes_init();
3094 if (ret)
3095 return ret;
3096 ret = lttng_context_init();
3097 if (ret)
3098 return ret;
3099 ret = lttng_tracepoint_init();
3100 if (ret)
3101 goto error_tp;
3102 event_cache = KMEM_CACHE(lttng_event, 0);
3103 if (!event_cache) {
3104 ret = -ENOMEM;
3105 goto error_kmem;
3106 }
3107 ret = lttng_abi_init();
3108 if (ret)
3109 goto error_abi;
3110 ret = lttng_logger_init();
3111 if (ret)
3112 goto error_logger;
3113 ret = lttng_init_cpu_hotplug();
3114 if (ret)
3115 goto error_hotplug;
3116 printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3117 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3118 __stringify(LTTNG_MODULES_MINOR_VERSION),
3119 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3120 LTTNG_MODULES_EXTRAVERSION,
3121 LTTNG_VERSION_NAME,
3122 #ifdef LTTNG_EXTRA_VERSION_GIT
3123 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3124 #else
3125 "",
3126 #endif
3127 #ifdef LTTNG_EXTRA_VERSION_NAME
3128 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3129 #else
3130 "");
3131 #endif
3132 return 0;
3133
3134 error_hotplug:
3135 lttng_logger_exit();
3136 error_logger:
3137 lttng_abi_exit();
3138 error_abi:
3139 kmem_cache_destroy(event_cache);
3140 error_kmem:
3141 lttng_tracepoint_exit();
3142 error_tp:
3143 lttng_context_exit();
3144 printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3145 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3146 __stringify(LTTNG_MODULES_MINOR_VERSION),
3147 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3148 LTTNG_MODULES_EXTRAVERSION,
3149 LTTNG_VERSION_NAME,
3150 #ifdef LTTNG_EXTRA_VERSION_GIT
3151 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3152 #else
3153 "",
3154 #endif
3155 #ifdef LTTNG_EXTRA_VERSION_NAME
3156 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3157 #else
3158 "");
3159 #endif
3160 return ret;
3161 }
3162
3163 module_init(lttng_events_init);
3164
3165 static void __exit lttng_events_exit(void)
3166 {
3167 struct lttng_session *session, *tmpsession;
3168
3169 lttng_exit_cpu_hotplug();
3170 lttng_logger_exit();
3171 lttng_abi_exit();
3172 list_for_each_entry_safe(session, tmpsession, &sessions, list)
3173 lttng_session_destroy(session);
3174 kmem_cache_destroy(event_cache);
3175 lttng_tracepoint_exit();
3176 lttng_context_exit();
3177 printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3178 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3179 __stringify(LTTNG_MODULES_MINOR_VERSION),
3180 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3181 LTTNG_MODULES_EXTRAVERSION,
3182 LTTNG_VERSION_NAME,
3183 #ifdef LTTNG_EXTRA_VERSION_GIT
3184 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3185 #else
3186 "",
3187 #endif
3188 #ifdef LTTNG_EXTRA_VERSION_NAME
3189 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3190 #else
3191 "");
3192 #endif
3193 }
3194
3195 module_exit(lttng_events_exit);
3196
3197 #include <generated/patches.h>
3198 #ifdef LTTNG_EXTRA_VERSION_GIT
3199 MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT);
3200 #endif
3201 #ifdef LTTNG_EXTRA_VERSION_NAME
3202 MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME);
3203 #endif
3204 MODULE_LICENSE("GPL and additional rights");
3205 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3206 MODULE_DESCRIPTION("LTTng tracer");
3207 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
3208 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
3209 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
3210 LTTNG_MODULES_EXTRAVERSION);
This page took 0.146582 seconds and 4 git commands to generate.