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