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