Update for kernel 5.7: use vmalloc_sync_mappings on kernels >= 5.7
[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)
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 goto end;
1631 }
1632 stream->transport->ops.event_write(&ctx,
1633 stream->metadata_cache->data + stream->metadata_in,
1634 reserve_len);
1635 stream->transport->ops.event_commit(&ctx);
1636 stream->metadata_in += reserve_len;
1637 ret = reserve_len;
1638
1639 end:
1640 mutex_unlock(&stream->metadata_cache->lock);
1641 return ret;
1642 }
1643
1644 /*
1645 * Write the metadata to the metadata cache.
1646 * Must be called with sessions_mutex held.
1647 * The metadata cache lock protects us from concurrent read access from
1648 * thread outputting metadata content to ring buffer.
1649 */
1650 int lttng_metadata_printf(struct lttng_session *session,
1651 const char *fmt, ...)
1652 {
1653 char *str;
1654 size_t len;
1655 va_list ap;
1656 struct lttng_metadata_stream *stream;
1657
1658 WARN_ON_ONCE(!READ_ONCE(session->active));
1659
1660 va_start(ap, fmt);
1661 str = kvasprintf(GFP_KERNEL, fmt, ap);
1662 va_end(ap);
1663 if (!str)
1664 return -ENOMEM;
1665
1666 len = strlen(str);
1667 mutex_lock(&session->metadata_cache->lock);
1668 if (session->metadata_cache->metadata_written + len >
1669 session->metadata_cache->cache_alloc) {
1670 char *tmp_cache_realloc;
1671 unsigned int tmp_cache_alloc_size;
1672
1673 tmp_cache_alloc_size = max_t(unsigned int,
1674 session->metadata_cache->cache_alloc + len,
1675 session->metadata_cache->cache_alloc << 1);
1676 tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size);
1677 if (!tmp_cache_realloc)
1678 goto err;
1679 if (session->metadata_cache->data) {
1680 memcpy(tmp_cache_realloc,
1681 session->metadata_cache->data,
1682 session->metadata_cache->cache_alloc);
1683 vfree(session->metadata_cache->data);
1684 }
1685
1686 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1687 session->metadata_cache->data = tmp_cache_realloc;
1688 }
1689 memcpy(session->metadata_cache->data +
1690 session->metadata_cache->metadata_written,
1691 str, len);
1692 session->metadata_cache->metadata_written += len;
1693 mutex_unlock(&session->metadata_cache->lock);
1694 kfree(str);
1695
1696 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1697 wake_up_interruptible(&stream->read_wait);
1698
1699 return 0;
1700
1701 err:
1702 mutex_unlock(&session->metadata_cache->lock);
1703 kfree(str);
1704 return -ENOMEM;
1705 }
1706
1707 static
1708 int print_tabs(struct lttng_session *session, size_t nesting)
1709 {
1710 size_t i;
1711
1712 for (i = 0; i < nesting; i++) {
1713 int ret;
1714
1715 ret = lttng_metadata_printf(session, " ");
1716 if (ret) {
1717 return ret;
1718 }
1719 }
1720 return 0;
1721 }
1722
1723 /*
1724 * Must be called with sessions_mutex held.
1725 */
1726 static
1727 int _lttng_struct_type_statedump(struct lttng_session *session,
1728 const struct lttng_type *type,
1729 size_t nesting)
1730 {
1731 int ret;
1732 uint32_t i, nr_fields;
1733
1734 ret = print_tabs(session, nesting);
1735 if (ret)
1736 return ret;
1737 ret = lttng_metadata_printf(session,
1738 "struct {\n");
1739 if (ret)
1740 return ret;
1741 nr_fields = type->u._struct.nr_fields;
1742 for (i = 0; i < nr_fields; i++) {
1743 const struct lttng_event_field *iter_field;
1744
1745 iter_field = &type->u._struct.fields[i];
1746 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1747 if (ret)
1748 return ret;
1749 }
1750 ret = print_tabs(session, nesting);
1751 if (ret)
1752 return ret;
1753 ret = lttng_metadata_printf(session,
1754 "}");
1755 return ret;
1756 }
1757
1758 /*
1759 * Must be called with sessions_mutex held.
1760 */
1761 static
1762 int _lttng_struct_statedump(struct lttng_session *session,
1763 const struct lttng_event_field *field,
1764 size_t nesting)
1765 {
1766 int ret;
1767
1768 ret = _lttng_struct_type_statedump(session,
1769 &field->type, nesting);
1770 if (ret)
1771 return ret;
1772 ret = lttng_metadata_printf(session,
1773 "_%s;\n",
1774 field->name);
1775 return ret;
1776 }
1777
1778 /*
1779 * Must be called with sessions_mutex held.
1780 */
1781 static
1782 int _lttng_variant_type_statedump(struct lttng_session *session,
1783 const struct lttng_type *type,
1784 size_t nesting)
1785 {
1786 int ret;
1787 uint32_t i, nr_choices;
1788
1789 ret = print_tabs(session, nesting);
1790 if (ret)
1791 return ret;
1792 ret = lttng_metadata_printf(session,
1793 "variant <_%s> {\n",
1794 type->u.variant.tag_name);
1795 if (ret)
1796 return ret;
1797 nr_choices = type->u.variant.nr_choices;
1798 for (i = 0; i < nr_choices; i++) {
1799 const struct lttng_event_field *iter_field;
1800
1801 iter_field = &type->u.variant.choices[i];
1802 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1803 if (ret)
1804 return ret;
1805 }
1806 ret = print_tabs(session, nesting);
1807 if (ret)
1808 return ret;
1809 ret = lttng_metadata_printf(session,
1810 "}");
1811 return ret;
1812 }
1813
1814 /*
1815 * Must be called with sessions_mutex held.
1816 */
1817 static
1818 int _lttng_variant_statedump(struct lttng_session *session,
1819 const struct lttng_event_field *field,
1820 size_t nesting)
1821 {
1822 int ret;
1823
1824 ret = _lttng_variant_type_statedump(session,
1825 &field->type, nesting);
1826 if (ret)
1827 return ret;
1828 ret = lttng_metadata_printf(session,
1829 "_%s;\n",
1830 field->name);
1831 return ret;
1832 }
1833
1834 /*
1835 * Must be called with sessions_mutex held.
1836 */
1837 static
1838 int _lttng_array_compound_statedump(struct lttng_session *session,
1839 const struct lttng_event_field *field,
1840 size_t nesting)
1841 {
1842 int ret;
1843 const struct lttng_type *elem_type;
1844
1845 /* Only array of structures and variants are currently supported. */
1846 elem_type = field->type.u.array_compound.elem_type;
1847 switch (elem_type->atype) {
1848 case atype_struct:
1849 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1850 if (ret)
1851 return ret;
1852 break;
1853 case atype_variant:
1854 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1855 if (ret)
1856 return ret;
1857 break;
1858 default:
1859 return -EINVAL;
1860 }
1861 ret = lttng_metadata_printf(session,
1862 " _%s[%u];\n",
1863 field->name,
1864 field->type.u.array_compound.length);
1865 return ret;
1866 }
1867
1868 /*
1869 * Must be called with sessions_mutex held.
1870 */
1871 static
1872 int _lttng_sequence_compound_statedump(struct lttng_session *session,
1873 const struct lttng_event_field *field,
1874 size_t nesting)
1875 {
1876 int ret;
1877 const char *length_name;
1878 const struct lttng_type *elem_type;
1879
1880 length_name = field->type.u.sequence_compound.length_name;
1881
1882 /* Only array of structures and variants are currently supported. */
1883 elem_type = field->type.u.sequence_compound.elem_type;
1884 switch (elem_type->atype) {
1885 case atype_struct:
1886 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1887 if (ret)
1888 return ret;
1889 break;
1890 case atype_variant:
1891 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1892 if (ret)
1893 return ret;
1894 break;
1895 default:
1896 return -EINVAL;
1897 }
1898 ret = lttng_metadata_printf(session,
1899 " _%s[ _%s ];\n",
1900 field->name,
1901 length_name);
1902 return ret;
1903 }
1904
1905 /*
1906 * Must be called with sessions_mutex held.
1907 */
1908 static
1909 int _lttng_enum_statedump(struct lttng_session *session,
1910 const struct lttng_event_field *field,
1911 size_t nesting)
1912 {
1913 const struct lttng_enum_desc *enum_desc;
1914 const struct lttng_integer_type *container_type;
1915 int ret;
1916 unsigned int i, nr_entries;
1917
1918 enum_desc = field->type.u.basic.enumeration.desc;
1919 container_type = &field->type.u.basic.enumeration.container_type;
1920 nr_entries = enum_desc->nr_entries;
1921
1922 ret = print_tabs(session, nesting);
1923 if (ret)
1924 goto end;
1925 ret = lttng_metadata_printf(session,
1926 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
1927 container_type->size,
1928 container_type->alignment,
1929 container_type->signedness,
1930 (container_type->encoding == lttng_encode_none)
1931 ? "none"
1932 : (container_type->encoding == lttng_encode_UTF8)
1933 ? "UTF8"
1934 : "ASCII",
1935 container_type->base,
1936 #if __BYTE_ORDER == __BIG_ENDIAN
1937 container_type->reverse_byte_order ? " byte_order = le;" : ""
1938 #else
1939 container_type->reverse_byte_order ? " byte_order = be;" : ""
1940 #endif
1941 );
1942 if (ret)
1943 goto end;
1944 /* Dump all entries */
1945 for (i = 0; i < nr_entries; i++) {
1946 const struct lttng_enum_entry *entry = &enum_desc->entries[i];
1947 int j, len;
1948
1949 ret = print_tabs(session, nesting + 1);
1950 if (ret)
1951 goto end;
1952 ret = lttng_metadata_printf(session,
1953 "\"");
1954 if (ret)
1955 goto end;
1956 len = strlen(entry->string);
1957 /* Escape the character '"' */
1958 for (j = 0; j < len; j++) {
1959 char c = entry->string[j];
1960
1961 switch (c) {
1962 case '"':
1963 ret = lttng_metadata_printf(session,
1964 "\\\"");
1965 break;
1966 case '\\':
1967 ret = lttng_metadata_printf(session,
1968 "\\\\");
1969 break;
1970 default:
1971 ret = lttng_metadata_printf(session,
1972 "%c", c);
1973 break;
1974 }
1975 if (ret)
1976 goto end;
1977 }
1978 ret = lttng_metadata_printf(session, "\"");
1979 if (ret)
1980 goto end;
1981
1982 if (entry->options.is_auto) {
1983 ret = lttng_metadata_printf(session, ",\n");
1984 if (ret)
1985 goto end;
1986 } else {
1987 ret = lttng_metadata_printf(session,
1988 " = ");
1989 if (ret)
1990 goto end;
1991 if (entry->start.signedness)
1992 ret = lttng_metadata_printf(session,
1993 "%lld", (long long) entry->start.value);
1994 else
1995 ret = lttng_metadata_printf(session,
1996 "%llu", entry->start.value);
1997 if (ret)
1998 goto end;
1999 if (entry->start.signedness == entry->end.signedness &&
2000 entry->start.value
2001 == entry->end.value) {
2002 ret = lttng_metadata_printf(session,
2003 ",\n");
2004 } else {
2005 if (entry->end.signedness) {
2006 ret = lttng_metadata_printf(session,
2007 " ... %lld,\n",
2008 (long long) entry->end.value);
2009 } else {
2010 ret = lttng_metadata_printf(session,
2011 " ... %llu,\n",
2012 entry->end.value);
2013 }
2014 }
2015 if (ret)
2016 goto end;
2017 }
2018 }
2019 ret = print_tabs(session, nesting);
2020 if (ret)
2021 goto end;
2022 ret = lttng_metadata_printf(session, "} _%s;\n",
2023 field->name);
2024 end:
2025 return ret;
2026 }
2027
2028 /*
2029 * Must be called with sessions_mutex held.
2030 */
2031 static
2032 int _lttng_field_statedump(struct lttng_session *session,
2033 const struct lttng_event_field *field,
2034 size_t nesting)
2035 {
2036 int ret = 0;
2037
2038 switch (field->type.atype) {
2039 case atype_integer:
2040 ret = print_tabs(session, nesting);
2041 if (ret)
2042 return ret;
2043 ret = lttng_metadata_printf(session,
2044 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2045 field->type.u.basic.integer.size,
2046 field->type.u.basic.integer.alignment,
2047 field->type.u.basic.integer.signedness,
2048 (field->type.u.basic.integer.encoding == lttng_encode_none)
2049 ? "none"
2050 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
2051 ? "UTF8"
2052 : "ASCII",
2053 field->type.u.basic.integer.base,
2054 #if __BYTE_ORDER == __BIG_ENDIAN
2055 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2056 #else
2057 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2058 #endif
2059 field->name);
2060 break;
2061 case atype_enum:
2062 ret = _lttng_enum_statedump(session, field, nesting);
2063 break;
2064 case atype_array:
2065 case atype_array_bitfield:
2066 {
2067 const struct lttng_basic_type *elem_type;
2068
2069 elem_type = &field->type.u.array.elem_type;
2070 if (field->type.u.array.elem_alignment) {
2071 ret = print_tabs(session, nesting);
2072 if (ret)
2073 return ret;
2074 ret = lttng_metadata_printf(session,
2075 "struct { } align(%u) _%s_padding;\n",
2076 field->type.u.array.elem_alignment * CHAR_BIT,
2077 field->name);
2078 if (ret)
2079 return ret;
2080 }
2081 ret = print_tabs(session, nesting);
2082 if (ret)
2083 return ret;
2084 ret = lttng_metadata_printf(session,
2085 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2086 elem_type->u.basic.integer.size,
2087 elem_type->u.basic.integer.alignment,
2088 elem_type->u.basic.integer.signedness,
2089 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2090 ? "none"
2091 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
2092 ? "UTF8"
2093 : "ASCII",
2094 elem_type->u.basic.integer.base,
2095 #if __BYTE_ORDER == __BIG_ENDIAN
2096 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2097 #else
2098 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2099 #endif
2100 field->name, field->type.u.array.length);
2101 break;
2102 }
2103 case atype_sequence:
2104 case atype_sequence_bitfield:
2105 {
2106 const struct lttng_basic_type *elem_type;
2107 const struct lttng_basic_type *length_type;
2108
2109 elem_type = &field->type.u.sequence.elem_type;
2110 length_type = &field->type.u.sequence.length_type;
2111 ret = print_tabs(session, nesting);
2112 if (ret)
2113 return ret;
2114 ret = lttng_metadata_printf(session,
2115 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2116 length_type->u.basic.integer.size,
2117 (unsigned int) length_type->u.basic.integer.alignment,
2118 length_type->u.basic.integer.signedness,
2119 (length_type->u.basic.integer.encoding == lttng_encode_none)
2120 ? "none"
2121 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
2122 ? "UTF8"
2123 : "ASCII"),
2124 length_type->u.basic.integer.base,
2125 #if __BYTE_ORDER == __BIG_ENDIAN
2126 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2127 #else
2128 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2129 #endif
2130 field->name);
2131 if (ret)
2132 return ret;
2133
2134 if (field->type.u.sequence.elem_alignment) {
2135 ret = print_tabs(session, nesting);
2136 if (ret)
2137 return ret;
2138 ret = lttng_metadata_printf(session,
2139 "struct { } align(%u) _%s_padding;\n",
2140 field->type.u.sequence.elem_alignment * CHAR_BIT,
2141 field->name);
2142 if (ret)
2143 return ret;
2144 }
2145 ret = print_tabs(session, nesting);
2146 if (ret)
2147 return ret;
2148 ret = lttng_metadata_printf(session,
2149 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2150 elem_type->u.basic.integer.size,
2151 (unsigned int) elem_type->u.basic.integer.alignment,
2152 elem_type->u.basic.integer.signedness,
2153 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2154 ? "none"
2155 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
2156 ? "UTF8"
2157 : "ASCII"),
2158 elem_type->u.basic.integer.base,
2159 #if __BYTE_ORDER == __BIG_ENDIAN
2160 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2161 #else
2162 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2163 #endif
2164 field->name,
2165 field->name);
2166 break;
2167 }
2168
2169 case atype_string:
2170 /* Default encoding is UTF8 */
2171 ret = print_tabs(session, nesting);
2172 if (ret)
2173 return ret;
2174 ret = lttng_metadata_printf(session,
2175 "string%s _%s;\n",
2176 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
2177 " { encoding = ASCII; }" : "",
2178 field->name);
2179 break;
2180 case atype_struct:
2181 ret = _lttng_struct_statedump(session, field, nesting);
2182 break;
2183 case atype_array_compound:
2184 ret = _lttng_array_compound_statedump(session, field, nesting);
2185 break;
2186 case atype_sequence_compound:
2187 ret = _lttng_sequence_compound_statedump(session, field, nesting);
2188 break;
2189 case atype_variant:
2190 ret = _lttng_variant_statedump(session, field, nesting);
2191 break;
2192
2193 default:
2194 WARN_ON_ONCE(1);
2195 return -EINVAL;
2196 }
2197 return ret;
2198 }
2199
2200 static
2201 int _lttng_context_metadata_statedump(struct lttng_session *session,
2202 struct lttng_ctx *ctx)
2203 {
2204 int ret = 0;
2205 int i;
2206
2207 if (!ctx)
2208 return 0;
2209 for (i = 0; i < ctx->nr_fields; i++) {
2210 const struct lttng_ctx_field *field = &ctx->fields[i];
2211
2212 ret = _lttng_field_statedump(session, &field->event_field, 2);
2213 if (ret)
2214 return ret;
2215 }
2216 return ret;
2217 }
2218
2219 static
2220 int _lttng_fields_metadata_statedump(struct lttng_session *session,
2221 struct lttng_event *event)
2222 {
2223 const struct lttng_event_desc *desc = event->desc;
2224 int ret = 0;
2225 int i;
2226
2227 for (i = 0; i < desc->nr_fields; i++) {
2228 const struct lttng_event_field *field = &desc->fields[i];
2229
2230 ret = _lttng_field_statedump(session, field, 2);
2231 if (ret)
2232 return ret;
2233 }
2234 return ret;
2235 }
2236
2237 /*
2238 * Must be called with sessions_mutex held.
2239 */
2240 static
2241 int _lttng_event_metadata_statedump(struct lttng_session *session,
2242 struct lttng_channel *chan,
2243 struct lttng_event *event)
2244 {
2245 int ret = 0;
2246
2247 if (event->metadata_dumped || !READ_ONCE(session->active))
2248 return 0;
2249 if (chan->channel_type == METADATA_CHANNEL)
2250 return 0;
2251
2252 ret = lttng_metadata_printf(session,
2253 "event {\n"
2254 " name = \"%s\";\n"
2255 " id = %u;\n"
2256 " stream_id = %u;\n",
2257 event->desc->name,
2258 event->id,
2259 event->chan->id);
2260 if (ret)
2261 goto end;
2262
2263 if (event->ctx) {
2264 ret = lttng_metadata_printf(session,
2265 " context := struct {\n");
2266 if (ret)
2267 goto end;
2268 }
2269 ret = _lttng_context_metadata_statedump(session, event->ctx);
2270 if (ret)
2271 goto end;
2272 if (event->ctx) {
2273 ret = lttng_metadata_printf(session,
2274 " };\n");
2275 if (ret)
2276 goto end;
2277 }
2278
2279 ret = lttng_metadata_printf(session,
2280 " fields := struct {\n"
2281 );
2282 if (ret)
2283 goto end;
2284
2285 ret = _lttng_fields_metadata_statedump(session, event);
2286 if (ret)
2287 goto end;
2288
2289 /*
2290 * LTTng space reservation can only reserve multiples of the
2291 * byte size.
2292 */
2293 ret = lttng_metadata_printf(session,
2294 " };\n"
2295 "};\n\n");
2296 if (ret)
2297 goto end;
2298
2299 event->metadata_dumped = 1;
2300 end:
2301 return ret;
2302
2303 }
2304
2305 /*
2306 * Must be called with sessions_mutex held.
2307 */
2308 static
2309 int _lttng_channel_metadata_statedump(struct lttng_session *session,
2310 struct lttng_channel *chan)
2311 {
2312 int ret = 0;
2313
2314 if (chan->metadata_dumped || !READ_ONCE(session->active))
2315 return 0;
2316
2317 if (chan->channel_type == METADATA_CHANNEL)
2318 return 0;
2319
2320 WARN_ON_ONCE(!chan->header_type);
2321 ret = lttng_metadata_printf(session,
2322 "stream {\n"
2323 " id = %u;\n"
2324 " event.header := %s;\n"
2325 " packet.context := struct packet_context;\n",
2326 chan->id,
2327 chan->header_type == 1 ? "struct event_header_compact" :
2328 "struct event_header_large");
2329 if (ret)
2330 goto end;
2331
2332 if (chan->ctx) {
2333 ret = lttng_metadata_printf(session,
2334 " event.context := struct {\n");
2335 if (ret)
2336 goto end;
2337 }
2338 ret = _lttng_context_metadata_statedump(session, chan->ctx);
2339 if (ret)
2340 goto end;
2341 if (chan->ctx) {
2342 ret = lttng_metadata_printf(session,
2343 " };\n");
2344 if (ret)
2345 goto end;
2346 }
2347
2348 ret = lttng_metadata_printf(session,
2349 "};\n\n");
2350
2351 chan->metadata_dumped = 1;
2352 end:
2353 return ret;
2354 }
2355
2356 /*
2357 * Must be called with sessions_mutex held.
2358 */
2359 static
2360 int _lttng_stream_packet_context_declare(struct lttng_session *session)
2361 {
2362 return lttng_metadata_printf(session,
2363 "struct packet_context {\n"
2364 " uint64_clock_monotonic_t timestamp_begin;\n"
2365 " uint64_clock_monotonic_t timestamp_end;\n"
2366 " uint64_t content_size;\n"
2367 " uint64_t packet_size;\n"
2368 " uint64_t packet_seq_num;\n"
2369 " unsigned long events_discarded;\n"
2370 " uint32_t cpu_id;\n"
2371 "};\n\n"
2372 );
2373 }
2374
2375 /*
2376 * Compact header:
2377 * id: range: 0 - 30.
2378 * id 31 is reserved to indicate an extended header.
2379 *
2380 * Large header:
2381 * id: range: 0 - 65534.
2382 * id 65535 is reserved to indicate an extended header.
2383 *
2384 * Must be called with sessions_mutex held.
2385 */
2386 static
2387 int _lttng_event_header_declare(struct lttng_session *session)
2388 {
2389 return lttng_metadata_printf(session,
2390 "struct event_header_compact {\n"
2391 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2392 " variant <id> {\n"
2393 " struct {\n"
2394 " uint27_clock_monotonic_t timestamp;\n"
2395 " } compact;\n"
2396 " struct {\n"
2397 " uint32_t id;\n"
2398 " uint64_clock_monotonic_t timestamp;\n"
2399 " } extended;\n"
2400 " } v;\n"
2401 "} align(%u);\n"
2402 "\n"
2403 "struct event_header_large {\n"
2404 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2405 " variant <id> {\n"
2406 " struct {\n"
2407 " uint32_clock_monotonic_t timestamp;\n"
2408 " } compact;\n"
2409 " struct {\n"
2410 " uint32_t id;\n"
2411 " uint64_clock_monotonic_t timestamp;\n"
2412 " } extended;\n"
2413 " } v;\n"
2414 "} align(%u);\n\n",
2415 lttng_alignof(uint32_t) * CHAR_BIT,
2416 lttng_alignof(uint16_t) * CHAR_BIT
2417 );
2418 }
2419
2420 /*
2421 * Approximation of NTP time of day to clock monotonic correlation,
2422 * taken at start of trace.
2423 * Yes, this is only an approximation. Yes, we can (and will) do better
2424 * in future versions.
2425 * This function may return a negative offset. It may happen if the
2426 * system sets the REALTIME clock to 0 after boot.
2427 *
2428 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2429 * y2038 compliant.
2430 */
2431 static
2432 int64_t measure_clock_offset(void)
2433 {
2434 uint64_t monotonic_avg, monotonic[2], realtime;
2435 uint64_t tcf = trace_clock_freq();
2436 int64_t offset;
2437 unsigned long flags;
2438 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2439 struct timespec64 rts = { 0, 0 };
2440 #else
2441 struct timespec rts = { 0, 0 };
2442 #endif
2443
2444 /* Disable interrupts to increase correlation precision. */
2445 local_irq_save(flags);
2446 monotonic[0] = trace_clock_read64();
2447 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2448 ktime_get_real_ts64(&rts);
2449 #else
2450 getnstimeofday(&rts);
2451 #endif
2452 monotonic[1] = trace_clock_read64();
2453 local_irq_restore(flags);
2454
2455 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2456 realtime = (uint64_t) rts.tv_sec * tcf;
2457 if (tcf == NSEC_PER_SEC) {
2458 realtime += rts.tv_nsec;
2459 } else {
2460 uint64_t n = rts.tv_nsec * tcf;
2461
2462 do_div(n, NSEC_PER_SEC);
2463 realtime += n;
2464 }
2465 offset = (int64_t) realtime - monotonic_avg;
2466 return offset;
2467 }
2468
2469 static
2470 int print_escaped_ctf_string(struct lttng_session *session, const char *string)
2471 {
2472 int ret;
2473 size_t i;
2474 char cur;
2475
2476 i = 0;
2477 cur = string[i];
2478 while (cur != '\0') {
2479 switch (cur) {
2480 case '\n':
2481 ret = lttng_metadata_printf(session, "%s", "\\n");
2482 break;
2483 case '\\':
2484 case '"':
2485 ret = lttng_metadata_printf(session, "%c", '\\');
2486 if (ret)
2487 goto error;
2488 /* We still print the current char */
2489 /* Fallthrough */
2490 default:
2491 ret = lttng_metadata_printf(session, "%c", cur);
2492 break;
2493 }
2494
2495 if (ret)
2496 goto error;
2497
2498 cur = string[++i];
2499 }
2500 error:
2501 return ret;
2502 }
2503
2504 static
2505 int print_metadata_escaped_field(struct lttng_session *session, const char *field,
2506 const char *field_value)
2507 {
2508 int ret;
2509
2510 ret = lttng_metadata_printf(session, " %s = \"", field);
2511 if (ret)
2512 goto error;
2513
2514 ret = print_escaped_ctf_string(session, field_value);
2515 if (ret)
2516 goto error;
2517
2518 ret = lttng_metadata_printf(session, "\";\n");
2519
2520 error:
2521 return ret;
2522 }
2523
2524 /*
2525 * Output metadata into this session's metadata buffers.
2526 * Must be called with sessions_mutex held.
2527 */
2528 static
2529 int _lttng_session_metadata_statedump(struct lttng_session *session)
2530 {
2531 unsigned char *uuid_c = session->uuid.b;
2532 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
2533 struct lttng_channel *chan;
2534 struct lttng_event *event;
2535 int ret = 0;
2536
2537 if (!READ_ONCE(session->active))
2538 return 0;
2539 if (session->metadata_dumped)
2540 goto skip_session;
2541
2542 snprintf(uuid_s, sizeof(uuid_s),
2543 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2544 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2545 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2546 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2547 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
2548
2549 ret = lttng_metadata_printf(session,
2550 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2551 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2552 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2553 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2554 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2555 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2556 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2557 "\n"
2558 "trace {\n"
2559 " major = %u;\n"
2560 " minor = %u;\n"
2561 " uuid = \"%s\";\n"
2562 " byte_order = %s;\n"
2563 " packet.header := struct {\n"
2564 " uint32_t magic;\n"
2565 " uint8_t uuid[16];\n"
2566 " uint32_t stream_id;\n"
2567 " uint64_t stream_instance_id;\n"
2568 " };\n"
2569 "};\n\n",
2570 lttng_alignof(uint8_t) * CHAR_BIT,
2571 lttng_alignof(uint16_t) * CHAR_BIT,
2572 lttng_alignof(uint32_t) * CHAR_BIT,
2573 lttng_alignof(uint64_t) * CHAR_BIT,
2574 sizeof(unsigned long) * CHAR_BIT,
2575 lttng_alignof(unsigned long) * CHAR_BIT,
2576 CTF_SPEC_MAJOR,
2577 CTF_SPEC_MINOR,
2578 uuid_s,
2579 #if __BYTE_ORDER == __BIG_ENDIAN
2580 "be"
2581 #else
2582 "le"
2583 #endif
2584 );
2585 if (ret)
2586 goto end;
2587
2588 ret = lttng_metadata_printf(session,
2589 "env {\n"
2590 " hostname = \"%s\";\n"
2591 " domain = \"kernel\";\n"
2592 " sysname = \"%s\";\n"
2593 " kernel_release = \"%s\";\n"
2594 " kernel_version = \"%s\";\n"
2595 " tracer_name = \"lttng-modules\";\n"
2596 " tracer_major = %d;\n"
2597 " tracer_minor = %d;\n"
2598 " tracer_patchlevel = %d;\n"
2599 " trace_buffering_scheme = \"global\";\n",
2600 current->nsproxy->uts_ns->name.nodename,
2601 utsname()->sysname,
2602 utsname()->release,
2603 utsname()->version,
2604 LTTNG_MODULES_MAJOR_VERSION,
2605 LTTNG_MODULES_MINOR_VERSION,
2606 LTTNG_MODULES_PATCHLEVEL_VERSION
2607 );
2608 if (ret)
2609 goto end;
2610
2611 ret = print_metadata_escaped_field(session, "trace_name", session->name);
2612 if (ret)
2613 goto end;
2614 ret = print_metadata_escaped_field(session, "trace_creation_datetime",
2615 session->creation_time);
2616 if (ret)
2617 goto end;
2618
2619 /* Close env */
2620 ret = lttng_metadata_printf(session, "};\n\n");
2621 if (ret)
2622 goto end;
2623
2624 ret = lttng_metadata_printf(session,
2625 "clock {\n"
2626 " name = \"%s\";\n",
2627 trace_clock_name()
2628 );
2629 if (ret)
2630 goto end;
2631
2632 if (!trace_clock_uuid(clock_uuid_s)) {
2633 ret = lttng_metadata_printf(session,
2634 " uuid = \"%s\";\n",
2635 clock_uuid_s
2636 );
2637 if (ret)
2638 goto end;
2639 }
2640
2641 ret = lttng_metadata_printf(session,
2642 " description = \"%s\";\n"
2643 " freq = %llu; /* Frequency, in Hz */\n"
2644 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2645 " offset = %lld;\n"
2646 "};\n\n",
2647 trace_clock_description(),
2648 (unsigned long long) trace_clock_freq(),
2649 (long long) measure_clock_offset()
2650 );
2651 if (ret)
2652 goto end;
2653
2654 ret = lttng_metadata_printf(session,
2655 "typealias integer {\n"
2656 " size = 27; align = 1; signed = false;\n"
2657 " map = clock.%s.value;\n"
2658 "} := uint27_clock_monotonic_t;\n"
2659 "\n"
2660 "typealias integer {\n"
2661 " size = 32; align = %u; signed = false;\n"
2662 " map = clock.%s.value;\n"
2663 "} := uint32_clock_monotonic_t;\n"
2664 "\n"
2665 "typealias integer {\n"
2666 " size = 64; align = %u; signed = false;\n"
2667 " map = clock.%s.value;\n"
2668 "} := uint64_clock_monotonic_t;\n\n",
2669 trace_clock_name(),
2670 lttng_alignof(uint32_t) * CHAR_BIT,
2671 trace_clock_name(),
2672 lttng_alignof(uint64_t) * CHAR_BIT,
2673 trace_clock_name()
2674 );
2675 if (ret)
2676 goto end;
2677
2678 ret = _lttng_stream_packet_context_declare(session);
2679 if (ret)
2680 goto end;
2681
2682 ret = _lttng_event_header_declare(session);
2683 if (ret)
2684 goto end;
2685
2686 skip_session:
2687 list_for_each_entry(chan, &session->chan, list) {
2688 ret = _lttng_channel_metadata_statedump(session, chan);
2689 if (ret)
2690 goto end;
2691 }
2692
2693 list_for_each_entry(event, &session->events, list) {
2694 ret = _lttng_event_metadata_statedump(session, event->chan, event);
2695 if (ret)
2696 goto end;
2697 }
2698 session->metadata_dumped = 1;
2699 end:
2700 return ret;
2701 }
2702
2703 /**
2704 * lttng_transport_register - LTT transport registration
2705 * @transport: transport structure
2706 *
2707 * Registers a transport which can be used as output to extract the data out of
2708 * LTTng. The module calling this registration function must ensure that no
2709 * trap-inducing code will be executed by the transport functions. E.g.
2710 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2711 * is made visible to the transport function. This registration acts as a
2712 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2713 * after its registration must it synchronize the TLBs.
2714 */
2715 void lttng_transport_register(struct lttng_transport *transport)
2716 {
2717 /*
2718 * Make sure no page fault can be triggered by the module about to be
2719 * registered. We deal with this here so we don't have to call
2720 * vmalloc_sync_mappings() in each module's init.
2721 */
2722 wrapper_vmalloc_sync_mappings();
2723
2724 mutex_lock(&sessions_mutex);
2725 list_add_tail(&transport->node, &lttng_transport_list);
2726 mutex_unlock(&sessions_mutex);
2727 }
2728 EXPORT_SYMBOL_GPL(lttng_transport_register);
2729
2730 /**
2731 * lttng_transport_unregister - LTT transport unregistration
2732 * @transport: transport structure
2733 */
2734 void lttng_transport_unregister(struct lttng_transport *transport)
2735 {
2736 mutex_lock(&sessions_mutex);
2737 list_del(&transport->node);
2738 mutex_unlock(&sessions_mutex);
2739 }
2740 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
2741
2742 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2743
2744 enum cpuhp_state lttng_hp_prepare;
2745 enum cpuhp_state lttng_hp_online;
2746
2747 static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node)
2748 {
2749 struct lttng_cpuhp_node *lttng_node;
2750
2751 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2752 switch (lttng_node->component) {
2753 case LTTNG_RING_BUFFER_FRONTEND:
2754 return 0;
2755 case LTTNG_RING_BUFFER_BACKEND:
2756 return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node);
2757 case LTTNG_RING_BUFFER_ITER:
2758 return 0;
2759 case LTTNG_CONTEXT_PERF_COUNTERS:
2760 return 0;
2761 default:
2762 return -EINVAL;
2763 }
2764 }
2765
2766 static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node)
2767 {
2768 struct lttng_cpuhp_node *lttng_node;
2769
2770 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2771 switch (lttng_node->component) {
2772 case LTTNG_RING_BUFFER_FRONTEND:
2773 return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node);
2774 case LTTNG_RING_BUFFER_BACKEND:
2775 return 0;
2776 case LTTNG_RING_BUFFER_ITER:
2777 return 0;
2778 case LTTNG_CONTEXT_PERF_COUNTERS:
2779 return lttng_cpuhp_perf_counter_dead(cpu, lttng_node);
2780 default:
2781 return -EINVAL;
2782 }
2783 }
2784
2785 static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node)
2786 {
2787 struct lttng_cpuhp_node *lttng_node;
2788
2789 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2790 switch (lttng_node->component) {
2791 case LTTNG_RING_BUFFER_FRONTEND:
2792 return lttng_cpuhp_rb_frontend_online(cpu, lttng_node);
2793 case LTTNG_RING_BUFFER_BACKEND:
2794 return 0;
2795 case LTTNG_RING_BUFFER_ITER:
2796 return lttng_cpuhp_rb_iter_online(cpu, lttng_node);
2797 case LTTNG_CONTEXT_PERF_COUNTERS:
2798 return lttng_cpuhp_perf_counter_online(cpu, lttng_node);
2799 default:
2800 return -EINVAL;
2801 }
2802 }
2803
2804 static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node)
2805 {
2806 struct lttng_cpuhp_node *lttng_node;
2807
2808 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2809 switch (lttng_node->component) {
2810 case LTTNG_RING_BUFFER_FRONTEND:
2811 return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node);
2812 case LTTNG_RING_BUFFER_BACKEND:
2813 return 0;
2814 case LTTNG_RING_BUFFER_ITER:
2815 return 0;
2816 case LTTNG_CONTEXT_PERF_COUNTERS:
2817 return 0;
2818 default:
2819 return -EINVAL;
2820 }
2821 }
2822
2823 static int __init lttng_init_cpu_hotplug(void)
2824 {
2825 int ret;
2826
2827 ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare",
2828 lttng_hotplug_prepare,
2829 lttng_hotplug_dead);
2830 if (ret < 0) {
2831 return ret;
2832 }
2833 lttng_hp_prepare = ret;
2834 lttng_rb_set_hp_prepare(ret);
2835
2836 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online",
2837 lttng_hotplug_online,
2838 lttng_hotplug_offline);
2839 if (ret < 0) {
2840 cpuhp_remove_multi_state(lttng_hp_prepare);
2841 lttng_hp_prepare = 0;
2842 return ret;
2843 }
2844 lttng_hp_online = ret;
2845 lttng_rb_set_hp_online(ret);
2846
2847 return 0;
2848 }
2849
2850 static void __exit lttng_exit_cpu_hotplug(void)
2851 {
2852 lttng_rb_set_hp_online(0);
2853 cpuhp_remove_multi_state(lttng_hp_online);
2854 lttng_rb_set_hp_prepare(0);
2855 cpuhp_remove_multi_state(lttng_hp_prepare);
2856 }
2857
2858 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2859 static int lttng_init_cpu_hotplug(void)
2860 {
2861 return 0;
2862 }
2863 static void lttng_exit_cpu_hotplug(void)
2864 {
2865 }
2866 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2867
2868
2869 static int __init lttng_events_init(void)
2870 {
2871 int ret;
2872
2873 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2874 if (ret)
2875 return ret;
2876 ret = wrapper_get_pfnblock_flags_mask_init();
2877 if (ret)
2878 return ret;
2879 ret = wrapper_get_pageblock_flags_mask_init();
2880 if (ret)
2881 return ret;
2882 ret = lttng_probes_init();
2883 if (ret)
2884 return ret;
2885 ret = lttng_context_init();
2886 if (ret)
2887 return ret;
2888 ret = lttng_tracepoint_init();
2889 if (ret)
2890 goto error_tp;
2891 event_cache = KMEM_CACHE(lttng_event, 0);
2892 if (!event_cache) {
2893 ret = -ENOMEM;
2894 goto error_kmem;
2895 }
2896 ret = lttng_abi_init();
2897 if (ret)
2898 goto error_abi;
2899 ret = lttng_logger_init();
2900 if (ret)
2901 goto error_logger;
2902 ret = lttng_init_cpu_hotplug();
2903 if (ret)
2904 goto error_hotplug;
2905 printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
2906 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2907 __stringify(LTTNG_MODULES_MINOR_VERSION),
2908 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2909 LTTNG_MODULES_EXTRAVERSION,
2910 LTTNG_VERSION_NAME,
2911 #ifdef LTTNG_EXTRA_VERSION_GIT
2912 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
2913 #else
2914 "",
2915 #endif
2916 #ifdef LTTNG_EXTRA_VERSION_NAME
2917 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
2918 #else
2919 "");
2920 #endif
2921 return 0;
2922
2923 error_hotplug:
2924 lttng_logger_exit();
2925 error_logger:
2926 lttng_abi_exit();
2927 error_abi:
2928 kmem_cache_destroy(event_cache);
2929 error_kmem:
2930 lttng_tracepoint_exit();
2931 error_tp:
2932 lttng_context_exit();
2933 printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
2934 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2935 __stringify(LTTNG_MODULES_MINOR_VERSION),
2936 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2937 LTTNG_MODULES_EXTRAVERSION,
2938 LTTNG_VERSION_NAME,
2939 #ifdef LTTNG_EXTRA_VERSION_GIT
2940 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
2941 #else
2942 "",
2943 #endif
2944 #ifdef LTTNG_EXTRA_VERSION_NAME
2945 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
2946 #else
2947 "");
2948 #endif
2949 return ret;
2950 }
2951
2952 module_init(lttng_events_init);
2953
2954 static void __exit lttng_events_exit(void)
2955 {
2956 struct lttng_session *session, *tmpsession;
2957
2958 lttng_exit_cpu_hotplug();
2959 lttng_logger_exit();
2960 lttng_abi_exit();
2961 list_for_each_entry_safe(session, tmpsession, &sessions, list)
2962 lttng_session_destroy(session);
2963 kmem_cache_destroy(event_cache);
2964 lttng_tracepoint_exit();
2965 lttng_context_exit();
2966 printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
2967 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2968 __stringify(LTTNG_MODULES_MINOR_VERSION),
2969 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2970 LTTNG_MODULES_EXTRAVERSION,
2971 LTTNG_VERSION_NAME,
2972 #ifdef LTTNG_EXTRA_VERSION_GIT
2973 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
2974 #else
2975 "",
2976 #endif
2977 #ifdef LTTNG_EXTRA_VERSION_NAME
2978 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
2979 #else
2980 "");
2981 #endif
2982 }
2983
2984 module_exit(lttng_events_exit);
2985
2986 #include "extra_version/patches.i"
2987 #ifdef LTTNG_EXTRA_VERSION_GIT
2988 MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT);
2989 #endif
2990 #ifdef LTTNG_EXTRA_VERSION_NAME
2991 MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME);
2992 #endif
2993 MODULE_LICENSE("GPL and additional rights");
2994 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2995 MODULE_DESCRIPTION("LTTng tracer");
2996 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
2997 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
2998 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
2999 LTTNG_MODULES_EXTRAVERSION);
This page took 0.100887 seconds and 4 git commands to generate.