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