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