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