Version 2.7.7
[lttng-modules.git] / lttng-events.c
1 /*
2 * lttng-events.c
3 *
4 * Holds LTTng per-session event registry.
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /*
24 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
25 * overrides a function with a define.
26 */
27 #include "wrapper/page_alloc.h"
28
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/jiffies.h>
34 #include <linux/utsname.h>
35 #include <linux/err.h>
36 #include <linux/seq_file.h>
37 #include <linux/file.h>
38 #include <linux/anon_inodes.h>
39 #include "wrapper/file.h"
40 #include <linux/jhash.h>
41 #include <linux/uaccess.h>
42 #include <linux/vmalloc.h>
43
44 #include "wrapper/uuid.h"
45 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
46 #include "wrapper/random.h"
47 #include "wrapper/tracepoint.h"
48 #include "wrapper/list.h"
49 #include "lttng-kernel-version.h"
50 #include "lttng-events.h"
51 #include "lttng-tracer.h"
52 #include "lttng-abi-old.h"
53 #include "lttng-endian.h"
54 #include "wrapper/vzalloc.h"
55 #include "wrapper/ringbuffer/backend.h"
56 #include "wrapper/ringbuffer/frontend.h"
57
58 #define METADATA_CACHE_DEFAULT_SIZE 4096
59
60 static LIST_HEAD(sessions);
61 static LIST_HEAD(lttng_transport_list);
62 /*
63 * Protect the sessions and metadata caches.
64 */
65 static DEFINE_MUTEX(sessions_mutex);
66 static struct kmem_cache *event_cache;
67
68 static void lttng_session_lazy_sync_enablers(struct lttng_session *session);
69 static void lttng_session_sync_enablers(struct lttng_session *session);
70 static void lttng_enabler_destroy(struct lttng_enabler *enabler);
71
72 static void _lttng_event_destroy(struct lttng_event *event);
73 static void _lttng_channel_destroy(struct lttng_channel *chan);
74 static int _lttng_event_unregister(struct lttng_event *event);
75 static
76 int _lttng_event_metadata_statedump(struct lttng_session *session,
77 struct lttng_channel *chan,
78 struct lttng_event *event);
79 static
80 int _lttng_session_metadata_statedump(struct lttng_session *session);
81 static
82 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
83
84 void synchronize_trace(void)
85 {
86 synchronize_sched();
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 = kzalloc(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 uuid_le_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 kfree(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 ACCESS_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 kfree(session);
210 }
211
212 int lttng_session_enable(struct lttng_session *session)
213 {
214 int ret = 0;
215 struct lttng_channel *chan;
216
217 mutex_lock(&sessions_mutex);
218 if (session->active) {
219 ret = -EBUSY;
220 goto end;
221 }
222
223 /* Set transient enabler state to "enabled" */
224 session->tstate = 1;
225
226 /*
227 * Snapshot the number of events per channel to know the type of header
228 * we need to use.
229 */
230 list_for_each_entry(chan, &session->chan, list) {
231 if (chan->header_type)
232 continue; /* don't change it if session stop/restart */
233 if (chan->free_event_id < 31)
234 chan->header_type = 1; /* compact */
235 else
236 chan->header_type = 2; /* large */
237 }
238
239 /* We need to sync enablers with session before activation. */
240 lttng_session_sync_enablers(session);
241
242 /* Clear each stream's quiescent state. */
243 list_for_each_entry(chan, &session->chan, list) {
244 if (chan->channel_type != METADATA_CHANNEL)
245 lib_ring_buffer_clear_quiescent_channel(chan->chan);
246 }
247
248 ACCESS_ONCE(session->active) = 1;
249 ACCESS_ONCE(session->been_active) = 1;
250 ret = _lttng_session_metadata_statedump(session);
251 if (ret) {
252 ACCESS_ONCE(session->active) = 0;
253 goto end;
254 }
255 ret = lttng_statedump_start(session);
256 if (ret)
257 ACCESS_ONCE(session->active) = 0;
258 end:
259 mutex_unlock(&sessions_mutex);
260 return ret;
261 }
262
263 int lttng_session_disable(struct lttng_session *session)
264 {
265 int ret = 0;
266 struct lttng_channel *chan;
267
268 mutex_lock(&sessions_mutex);
269 if (!session->active) {
270 ret = -EBUSY;
271 goto end;
272 }
273 ACCESS_ONCE(session->active) = 0;
274
275 /* Set transient enabler state to "disabled" */
276 session->tstate = 0;
277 lttng_session_sync_enablers(session);
278
279 /* Set each stream's quiescent state. */
280 list_for_each_entry(chan, &session->chan, list) {
281 if (chan->channel_type != METADATA_CHANNEL)
282 lib_ring_buffer_set_quiescent_channel(chan->chan);
283 }
284 end:
285 mutex_unlock(&sessions_mutex);
286 return ret;
287 }
288
289 int lttng_channel_enable(struct lttng_channel *channel)
290 {
291 int ret = 0;
292
293 mutex_lock(&sessions_mutex);
294 if (channel->channel_type == METADATA_CHANNEL) {
295 ret = -EPERM;
296 goto end;
297 }
298 if (channel->enabled) {
299 ret = -EEXIST;
300 goto end;
301 }
302 /* Set transient enabler state to "enabled" */
303 channel->tstate = 1;
304 lttng_session_sync_enablers(channel->session);
305 /* Set atomically the state to "enabled" */
306 ACCESS_ONCE(channel->enabled) = 1;
307 end:
308 mutex_unlock(&sessions_mutex);
309 return ret;
310 }
311
312 int lttng_channel_disable(struct lttng_channel *channel)
313 {
314 int ret = 0;
315
316 mutex_lock(&sessions_mutex);
317 if (channel->channel_type == METADATA_CHANNEL) {
318 ret = -EPERM;
319 goto end;
320 }
321 if (!channel->enabled) {
322 ret = -EEXIST;
323 goto end;
324 }
325 /* Set atomically the state to "disabled" */
326 ACCESS_ONCE(channel->enabled) = 0;
327 /* Set transient enabler state to "enabled" */
328 channel->tstate = 0;
329 lttng_session_sync_enablers(channel->session);
330 end:
331 mutex_unlock(&sessions_mutex);
332 return ret;
333 }
334
335 int lttng_event_enable(struct lttng_event *event)
336 {
337 int ret = 0;
338
339 mutex_lock(&sessions_mutex);
340 if (event->chan->channel_type == METADATA_CHANNEL) {
341 ret = -EPERM;
342 goto end;
343 }
344 if (event->enabled) {
345 ret = -EEXIST;
346 goto end;
347 }
348 switch (event->instrumentation) {
349 case LTTNG_KERNEL_TRACEPOINT:
350 case LTTNG_KERNEL_SYSCALL:
351 ret = -EINVAL;
352 break;
353 case LTTNG_KERNEL_KPROBE:
354 case LTTNG_KERNEL_FUNCTION:
355 case LTTNG_KERNEL_NOOP:
356 ACCESS_ONCE(event->enabled) = 1;
357 break;
358 case LTTNG_KERNEL_KRETPROBE:
359 ret = lttng_kretprobes_event_enable_state(event, 1);
360 break;
361 default:
362 WARN_ON_ONCE(1);
363 ret = -EINVAL;
364 }
365 end:
366 mutex_unlock(&sessions_mutex);
367 return ret;
368 }
369
370 int lttng_event_disable(struct lttng_event *event)
371 {
372 int ret = 0;
373
374 mutex_lock(&sessions_mutex);
375 if (event->chan->channel_type == METADATA_CHANNEL) {
376 ret = -EPERM;
377 goto end;
378 }
379 if (!event->enabled) {
380 ret = -EEXIST;
381 goto end;
382 }
383 switch (event->instrumentation) {
384 case LTTNG_KERNEL_TRACEPOINT:
385 case LTTNG_KERNEL_SYSCALL:
386 ret = -EINVAL;
387 break;
388 case LTTNG_KERNEL_KPROBE:
389 case LTTNG_KERNEL_FUNCTION:
390 case LTTNG_KERNEL_NOOP:
391 ACCESS_ONCE(event->enabled) = 0;
392 break;
393 case LTTNG_KERNEL_KRETPROBE:
394 ret = lttng_kretprobes_event_enable_state(event, 0);
395 break;
396 default:
397 WARN_ON_ONCE(1);
398 ret = -EINVAL;
399 }
400 end:
401 mutex_unlock(&sessions_mutex);
402 return ret;
403 }
404
405 static struct lttng_transport *lttng_transport_find(const char *name)
406 {
407 struct lttng_transport *transport;
408
409 list_for_each_entry(transport, &lttng_transport_list, node) {
410 if (!strcmp(transport->name, name))
411 return transport;
412 }
413 return NULL;
414 }
415
416 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
417 const char *transport_name,
418 void *buf_addr,
419 size_t subbuf_size, size_t num_subbuf,
420 unsigned int switch_timer_interval,
421 unsigned int read_timer_interval,
422 enum channel_type channel_type)
423 {
424 struct lttng_channel *chan;
425 struct lttng_transport *transport = NULL;
426
427 mutex_lock(&sessions_mutex);
428 if (session->been_active && channel_type != METADATA_CHANNEL)
429 goto active; /* Refuse to add channel to active session */
430 transport = lttng_transport_find(transport_name);
431 if (!transport) {
432 printk(KERN_WARNING "LTTng transport %s not found\n",
433 transport_name);
434 goto notransport;
435 }
436 if (!try_module_get(transport->owner)) {
437 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
438 goto notransport;
439 }
440 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
441 if (!chan)
442 goto nomem;
443 chan->session = session;
444 chan->id = session->free_chan_id++;
445 chan->ops = &transport->ops;
446 /*
447 * Note: the channel creation op already writes into the packet
448 * headers. Therefore the "chan" information used as input
449 * should be already accessible.
450 */
451 chan->chan = transport->ops.channel_create(transport_name,
452 chan, buf_addr, subbuf_size, num_subbuf,
453 switch_timer_interval, read_timer_interval);
454 if (!chan->chan)
455 goto create_error;
456 chan->tstate = 1;
457 chan->enabled = 1;
458 chan->transport = transport;
459 chan->channel_type = channel_type;
460 list_add(&chan->list, &session->chan);
461 mutex_unlock(&sessions_mutex);
462 return chan;
463
464 create_error:
465 kfree(chan);
466 nomem:
467 if (transport)
468 module_put(transport->owner);
469 notransport:
470 active:
471 mutex_unlock(&sessions_mutex);
472 return NULL;
473 }
474
475 /*
476 * Only used internally at session destruction for per-cpu channels, and
477 * when metadata channel is released.
478 * Needs to be called with sessions mutex held.
479 */
480 static
481 void _lttng_channel_destroy(struct lttng_channel *chan)
482 {
483 chan->ops->channel_destroy(chan->chan);
484 module_put(chan->transport->owner);
485 list_del(&chan->list);
486 lttng_destroy_context(chan->ctx);
487 kfree(chan);
488 }
489
490 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
491 {
492 BUG_ON(chan->channel_type != METADATA_CHANNEL);
493
494 /* Protect the metadata cache with the sessions_mutex. */
495 mutex_lock(&sessions_mutex);
496 _lttng_channel_destroy(chan);
497 mutex_unlock(&sessions_mutex);
498 }
499 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
500
501 static
502 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
503 {
504 stream->finalized = 1;
505 wake_up_interruptible(&stream->read_wait);
506 }
507
508 /*
509 * Supports event creation while tracing session is active.
510 * Needs to be called with sessions mutex held.
511 */
512 struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
513 struct lttng_kernel_event *event_param,
514 void *filter,
515 const struct lttng_event_desc *event_desc,
516 enum lttng_kernel_instrumentation itype)
517 {
518 struct lttng_session *session = chan->session;
519 struct lttng_event *event;
520 const char *event_name;
521 struct hlist_head *head;
522 size_t name_len;
523 uint32_t hash;
524 int ret;
525
526 if (chan->free_event_id == -1U) {
527 ret = -EMFILE;
528 goto full;
529 }
530
531 switch (itype) {
532 case LTTNG_KERNEL_TRACEPOINT:
533 event_name = event_desc->name;
534 break;
535 case LTTNG_KERNEL_KPROBE:
536 case LTTNG_KERNEL_KRETPROBE:
537 case LTTNG_KERNEL_FUNCTION:
538 case LTTNG_KERNEL_NOOP:
539 case LTTNG_KERNEL_SYSCALL:
540 event_name = event_param->name;
541 break;
542 default:
543 WARN_ON_ONCE(1);
544 ret = -EINVAL;
545 goto type_error;
546 }
547 name_len = strlen(event_name);
548 hash = jhash(event_name, name_len, 0);
549 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
550 lttng_hlist_for_each_entry(event, head, hlist) {
551 WARN_ON_ONCE(!event->desc);
552 if (!strncmp(event->desc->name, event_name,
553 LTTNG_KERNEL_SYM_NAME_LEN - 1)
554 && chan == event->chan) {
555 ret = -EEXIST;
556 goto exist;
557 }
558 }
559
560 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
561 if (!event) {
562 ret = -ENOMEM;
563 goto cache_error;
564 }
565 event->chan = chan;
566 event->filter = filter;
567 event->id = chan->free_event_id++;
568 event->instrumentation = itype;
569 event->evtype = LTTNG_TYPE_EVENT;
570 INIT_LIST_HEAD(&event->bytecode_runtime_head);
571 INIT_LIST_HEAD(&event->enablers_ref_head);
572
573 switch (itype) {
574 case LTTNG_KERNEL_TRACEPOINT:
575 /* Event will be enabled by enabler sync. */
576 event->enabled = 0;
577 event->registered = 0;
578 event->desc = lttng_event_get(event_name);
579 if (!event->desc) {
580 ret = -ENOENT;
581 goto register_error;
582 }
583 /* Populate lttng_event structure before event registration. */
584 smp_wmb();
585 break;
586 case LTTNG_KERNEL_KPROBE:
587 /*
588 * Needs to be explicitly enabled after creation, since
589 * we may want to apply filters.
590 */
591 event->enabled = 0;
592 event->registered = 1;
593 /*
594 * Populate lttng_event structure before event
595 * registration.
596 */
597 smp_wmb();
598 ret = lttng_kprobes_register(event_name,
599 event_param->u.kprobe.symbol_name,
600 event_param->u.kprobe.offset,
601 event_param->u.kprobe.addr,
602 event);
603 if (ret) {
604 ret = -EINVAL;
605 goto register_error;
606 }
607 ret = try_module_get(event->desc->owner);
608 WARN_ON_ONCE(!ret);
609 break;
610 case LTTNG_KERNEL_KRETPROBE:
611 {
612 struct lttng_event *event_return;
613
614 /* kretprobe defines 2 events */
615 /*
616 * Needs to be explicitly enabled after creation, since
617 * we may want to apply filters.
618 */
619 event->enabled = 0;
620 event->registered = 1;
621 event_return =
622 kmem_cache_zalloc(event_cache, GFP_KERNEL);
623 if (!event_return) {
624 ret = -ENOMEM;
625 goto register_error;
626 }
627 event_return->chan = chan;
628 event_return->filter = filter;
629 event_return->id = chan->free_event_id++;
630 event_return->enabled = 0;
631 event_return->registered = 1;
632 event_return->instrumentation = itype;
633 /*
634 * Populate lttng_event structure before kretprobe registration.
635 */
636 smp_wmb();
637 ret = lttng_kretprobes_register(event_name,
638 event_param->u.kretprobe.symbol_name,
639 event_param->u.kretprobe.offset,
640 event_param->u.kretprobe.addr,
641 event, event_return);
642 if (ret) {
643 kmem_cache_free(event_cache, event_return);
644 ret = -EINVAL;
645 goto register_error;
646 }
647 /* Take 2 refs on the module: one per event. */
648 ret = try_module_get(event->desc->owner);
649 WARN_ON_ONCE(!ret);
650 ret = try_module_get(event->desc->owner);
651 WARN_ON_ONCE(!ret);
652 ret = _lttng_event_metadata_statedump(chan->session, chan,
653 event_return);
654 WARN_ON_ONCE(ret > 0);
655 if (ret) {
656 kmem_cache_free(event_cache, event_return);
657 module_put(event->desc->owner);
658 module_put(event->desc->owner);
659 goto statedump_error;
660 }
661 list_add(&event_return->list, &chan->session->events);
662 break;
663 }
664 case LTTNG_KERNEL_FUNCTION:
665 /*
666 * Needs to be explicitly enabled after creation, since
667 * we may want to apply filters.
668 */
669 event->enabled = 0;
670 event->registered = 1;
671 /*
672 * Populate lttng_event structure before event
673 * registration.
674 */
675 smp_wmb();
676 ret = lttng_ftrace_register(event_name,
677 event_param->u.ftrace.symbol_name,
678 event);
679 if (ret) {
680 goto register_error;
681 }
682 ret = try_module_get(event->desc->owner);
683 WARN_ON_ONCE(!ret);
684 break;
685 case LTTNG_KERNEL_NOOP:
686 case LTTNG_KERNEL_SYSCALL:
687 /*
688 * Needs to be explicitly enabled after creation, since
689 * we may want to apply filters.
690 */
691 event->enabled = 0;
692 event->registered = 0;
693 event->desc = event_desc;
694 if (!event->desc) {
695 ret = -EINVAL;
696 goto register_error;
697 }
698 break;
699 default:
700 WARN_ON_ONCE(1);
701 ret = -EINVAL;
702 goto register_error;
703 }
704 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
705 WARN_ON_ONCE(ret > 0);
706 if (ret) {
707 goto statedump_error;
708 }
709 hlist_add_head(&event->hlist, head);
710 list_add(&event->list, &chan->session->events);
711 return event;
712
713 statedump_error:
714 /* If a statedump error occurs, events will not be readable. */
715 register_error:
716 kmem_cache_free(event_cache, event);
717 cache_error:
718 exist:
719 type_error:
720 full:
721 return ERR_PTR(ret);
722 }
723
724 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
725 struct lttng_kernel_event *event_param,
726 void *filter,
727 const struct lttng_event_desc *event_desc,
728 enum lttng_kernel_instrumentation itype)
729 {
730 struct lttng_event *event;
731
732 mutex_lock(&sessions_mutex);
733 event = _lttng_event_create(chan, event_param, filter, event_desc,
734 itype);
735 mutex_unlock(&sessions_mutex);
736 return event;
737 }
738
739 /* Only used for tracepoints for now. */
740 static
741 void register_event(struct lttng_event *event)
742 {
743 const struct lttng_event_desc *desc;
744 int ret = -EINVAL;
745
746 if (event->registered)
747 return;
748
749 desc = event->desc;
750 switch (event->instrumentation) {
751 case LTTNG_KERNEL_TRACEPOINT:
752 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
753 desc->probe_callback,
754 event);
755 break;
756 case LTTNG_KERNEL_SYSCALL:
757 ret = lttng_syscall_filter_enable(event->chan,
758 desc->name);
759 break;
760 case LTTNG_KERNEL_KPROBE:
761 case LTTNG_KERNEL_KRETPROBE:
762 case LTTNG_KERNEL_FUNCTION:
763 case LTTNG_KERNEL_NOOP:
764 ret = 0;
765 break;
766 default:
767 WARN_ON_ONCE(1);
768 }
769 if (!ret)
770 event->registered = 1;
771 }
772
773 /*
774 * Only used internally at session destruction.
775 */
776 int _lttng_event_unregister(struct lttng_event *event)
777 {
778 const struct lttng_event_desc *desc;
779 int ret = -EINVAL;
780
781 if (!event->registered)
782 return 0;
783
784 desc = event->desc;
785 switch (event->instrumentation) {
786 case LTTNG_KERNEL_TRACEPOINT:
787 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
788 event->desc->probe_callback,
789 event);
790 break;
791 case LTTNG_KERNEL_KPROBE:
792 lttng_kprobes_unregister(event);
793 ret = 0;
794 break;
795 case LTTNG_KERNEL_KRETPROBE:
796 lttng_kretprobes_unregister(event);
797 ret = 0;
798 break;
799 case LTTNG_KERNEL_FUNCTION:
800 lttng_ftrace_unregister(event);
801 ret = 0;
802 break;
803 case LTTNG_KERNEL_SYSCALL:
804 ret = lttng_syscall_filter_disable(event->chan,
805 desc->name);
806 break;
807 case LTTNG_KERNEL_NOOP:
808 ret = 0;
809 break;
810 default:
811 WARN_ON_ONCE(1);
812 }
813 if (!ret)
814 event->registered = 0;
815 return ret;
816 }
817
818 /*
819 * Only used internally at session destruction.
820 */
821 static
822 void _lttng_event_destroy(struct lttng_event *event)
823 {
824 switch (event->instrumentation) {
825 case LTTNG_KERNEL_TRACEPOINT:
826 lttng_event_put(event->desc);
827 break;
828 case LTTNG_KERNEL_KPROBE:
829 module_put(event->desc->owner);
830 lttng_kprobes_destroy_private(event);
831 break;
832 case LTTNG_KERNEL_KRETPROBE:
833 module_put(event->desc->owner);
834 lttng_kretprobes_destroy_private(event);
835 break;
836 case LTTNG_KERNEL_FUNCTION:
837 module_put(event->desc->owner);
838 lttng_ftrace_destroy_private(event);
839 break;
840 case LTTNG_KERNEL_NOOP:
841 case LTTNG_KERNEL_SYSCALL:
842 break;
843 default:
844 WARN_ON_ONCE(1);
845 }
846 list_del(&event->list);
847 lttng_destroy_context(event->ctx);
848 kmem_cache_free(event_cache, event);
849 }
850
851 int lttng_session_track_pid(struct lttng_session *session, int pid)
852 {
853 int ret;
854
855 if (pid < -1)
856 return -EINVAL;
857 mutex_lock(&sessions_mutex);
858 if (pid == -1) {
859 /* track all pids: destroy tracker. */
860 if (session->pid_tracker) {
861 struct lttng_pid_tracker *lpf;
862
863 lpf = session->pid_tracker;
864 rcu_assign_pointer(session->pid_tracker, NULL);
865 synchronize_trace();
866 lttng_pid_tracker_destroy(lpf);
867 }
868 ret = 0;
869 } else {
870 if (!session->pid_tracker) {
871 struct lttng_pid_tracker *lpf;
872
873 lpf = lttng_pid_tracker_create();
874 if (!lpf) {
875 ret = -ENOMEM;
876 goto unlock;
877 }
878 ret = lttng_pid_tracker_add(lpf, pid);
879 rcu_assign_pointer(session->pid_tracker, lpf);
880 } else {
881 ret = lttng_pid_tracker_add(session->pid_tracker, pid);
882 }
883 }
884 unlock:
885 mutex_unlock(&sessions_mutex);
886 return ret;
887 }
888
889 int lttng_session_untrack_pid(struct lttng_session *session, int pid)
890 {
891 int ret;
892
893 if (pid < -1)
894 return -EINVAL;
895 mutex_lock(&sessions_mutex);
896 if (pid == -1) {
897 /* untrack all pids: replace by empty tracker. */
898 struct lttng_pid_tracker *old_lpf = session->pid_tracker;
899 struct lttng_pid_tracker *lpf;
900
901 lpf = lttng_pid_tracker_create();
902 if (!lpf) {
903 ret = -ENOMEM;
904 goto unlock;
905 }
906 rcu_assign_pointer(session->pid_tracker, lpf);
907 synchronize_trace();
908 if (old_lpf)
909 lttng_pid_tracker_destroy(old_lpf);
910 ret = 0;
911 } else {
912 if (!session->pid_tracker) {
913 ret = -ENOENT;
914 goto unlock;
915 }
916 ret = lttng_pid_tracker_del(session->pid_tracker, pid);
917 }
918 unlock:
919 mutex_unlock(&sessions_mutex);
920 return ret;
921 }
922
923 static
924 void *pid_list_start(struct seq_file *m, loff_t *pos)
925 {
926 struct lttng_session *session = m->private;
927 struct lttng_pid_tracker *lpf;
928 struct lttng_pid_hash_node *e;
929 int iter = 0, i;
930
931 mutex_lock(&sessions_mutex);
932 lpf = session->pid_tracker;
933 if (lpf) {
934 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
935 struct hlist_head *head = &lpf->pid_hash[i];
936
937 lttng_hlist_for_each_entry(e, head, hlist) {
938 if (iter++ >= *pos)
939 return e;
940 }
941 }
942 } else {
943 /* PID tracker disabled. */
944 if (iter >= *pos && iter == 0) {
945 return session; /* empty tracker */
946 }
947 iter++;
948 }
949 /* End of list */
950 return NULL;
951 }
952
953 /* Called with sessions_mutex held. */
954 static
955 void *pid_list_next(struct seq_file *m, void *p, loff_t *ppos)
956 {
957 struct lttng_session *session = m->private;
958 struct lttng_pid_tracker *lpf;
959 struct lttng_pid_hash_node *e;
960 int iter = 0, i;
961
962 (*ppos)++;
963 lpf = session->pid_tracker;
964 if (lpf) {
965 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
966 struct hlist_head *head = &lpf->pid_hash[i];
967
968 lttng_hlist_for_each_entry(e, head, hlist) {
969 if (iter++ >= *ppos)
970 return e;
971 }
972 }
973 } else {
974 /* PID tracker disabled. */
975 if (iter >= *ppos && iter == 0)
976 return session; /* empty tracker */
977 iter++;
978 }
979
980 /* End of list */
981 return NULL;
982 }
983
984 static
985 void pid_list_stop(struct seq_file *m, void *p)
986 {
987 mutex_unlock(&sessions_mutex);
988 }
989
990 static
991 int pid_list_show(struct seq_file *m, void *p)
992 {
993 int pid;
994
995 if (p == m->private) {
996 /* Tracker disabled. */
997 pid = -1;
998 } else {
999 const struct lttng_pid_hash_node *e = p;
1000
1001 pid = lttng_pid_tracker_get_node_pid(e);
1002 }
1003 seq_printf(m, "process { pid = %d; };\n", pid);
1004 return 0;
1005 }
1006
1007 static
1008 const struct seq_operations lttng_tracker_pids_list_seq_ops = {
1009 .start = pid_list_start,
1010 .next = pid_list_next,
1011 .stop = pid_list_stop,
1012 .show = pid_list_show,
1013 };
1014
1015 static
1016 int lttng_tracker_pids_list_open(struct inode *inode, struct file *file)
1017 {
1018 return seq_open(file, &lttng_tracker_pids_list_seq_ops);
1019 }
1020
1021 static
1022 int lttng_tracker_pids_list_release(struct inode *inode, struct file *file)
1023 {
1024 struct seq_file *m = file->private_data;
1025 struct lttng_session *session = m->private;
1026 int ret;
1027
1028 WARN_ON_ONCE(!session);
1029 ret = seq_release(inode, file);
1030 if (!ret && session)
1031 fput(session->file);
1032 return ret;
1033 }
1034
1035 const struct file_operations lttng_tracker_pids_list_fops = {
1036 .owner = THIS_MODULE,
1037 .open = lttng_tracker_pids_list_open,
1038 .read = seq_read,
1039 .llseek = seq_lseek,
1040 .release = lttng_tracker_pids_list_release,
1041 };
1042
1043 int lttng_session_list_tracker_pids(struct lttng_session *session)
1044 {
1045 struct file *tracker_pids_list_file;
1046 struct seq_file *m;
1047 int file_fd, ret;
1048
1049 file_fd = lttng_get_unused_fd();
1050 if (file_fd < 0) {
1051 ret = file_fd;
1052 goto fd_error;
1053 }
1054
1055 tracker_pids_list_file = anon_inode_getfile("[lttng_tracker_pids_list]",
1056 &lttng_tracker_pids_list_fops,
1057 NULL, O_RDWR);
1058 if (IS_ERR(tracker_pids_list_file)) {
1059 ret = PTR_ERR(tracker_pids_list_file);
1060 goto file_error;
1061 }
1062 if (atomic_long_add_unless(&session->file->f_count,
1063 1, INT_MAX) == INT_MAX) {
1064 goto refcount_error;
1065 }
1066 ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file);
1067 if (ret < 0)
1068 goto open_error;
1069 m = tracker_pids_list_file->private_data;
1070 m->private = session;
1071 fd_install(file_fd, tracker_pids_list_file);
1072
1073 return file_fd;
1074
1075 open_error:
1076 atomic_long_dec(&session->file->f_count);
1077 refcount_error:
1078 fput(tracker_pids_list_file);
1079 file_error:
1080 put_unused_fd(file_fd);
1081 fd_error:
1082 return ret;
1083 }
1084
1085 /*
1086 * Enabler management.
1087 */
1088 static
1089 int lttng_match_enabler_wildcard(const char *desc_name,
1090 const char *name)
1091 {
1092 /* Compare excluding final '*' */
1093 if (strncmp(desc_name, name, strlen(name) - 1))
1094 return 0;
1095 return 1;
1096 }
1097
1098 static
1099 int lttng_match_enabler_name(const char *desc_name,
1100 const char *name)
1101 {
1102 if (strcmp(desc_name, name))
1103 return 0;
1104 return 1;
1105 }
1106
1107 static
1108 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1109 struct lttng_enabler *enabler)
1110 {
1111 const char *desc_name, *enabler_name;
1112
1113 enabler_name = enabler->event_param.name;
1114 switch (enabler->event_param.instrumentation) {
1115 case LTTNG_KERNEL_TRACEPOINT:
1116 desc_name = desc->name;
1117 break;
1118 case LTTNG_KERNEL_SYSCALL:
1119 desc_name = desc->name;
1120 if (!strncmp(desc_name, "compat_", strlen("compat_")))
1121 desc_name += strlen("compat_");
1122 if (!strncmp(desc_name, "syscall_exit_",
1123 strlen("syscall_exit_"))) {
1124 desc_name += strlen("syscall_exit_");
1125 } else if (!strncmp(desc_name, "syscall_entry_",
1126 strlen("syscall_entry_"))) {
1127 desc_name += strlen("syscall_entry_");
1128 } else {
1129 WARN_ON_ONCE(1);
1130 return -EINVAL;
1131 }
1132 break;
1133 default:
1134 WARN_ON_ONCE(1);
1135 return -EINVAL;
1136 }
1137 switch (enabler->type) {
1138 case LTTNG_ENABLER_WILDCARD:
1139 return lttng_match_enabler_wildcard(desc_name, enabler_name);
1140 case LTTNG_ENABLER_NAME:
1141 return lttng_match_enabler_name(desc_name, enabler_name);
1142 default:
1143 return -EINVAL;
1144 }
1145 }
1146
1147 static
1148 int lttng_event_match_enabler(struct lttng_event *event,
1149 struct lttng_enabler *enabler)
1150 {
1151 if (enabler->event_param.instrumentation != event->instrumentation)
1152 return 0;
1153 if (lttng_desc_match_enabler(event->desc, enabler)
1154 && event->chan == enabler->chan)
1155 return 1;
1156 else
1157 return 0;
1158 }
1159
1160 static
1161 struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event,
1162 struct lttng_enabler *enabler)
1163 {
1164 struct lttng_enabler_ref *enabler_ref;
1165
1166 list_for_each_entry(enabler_ref,
1167 &event->enablers_ref_head, node) {
1168 if (enabler_ref->ref == enabler)
1169 return enabler_ref;
1170 }
1171 return NULL;
1172 }
1173
1174 static
1175 void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
1176 {
1177 struct lttng_session *session = enabler->chan->session;
1178 struct lttng_probe_desc *probe_desc;
1179 const struct lttng_event_desc *desc;
1180 int i;
1181 struct list_head *probe_list;
1182
1183 probe_list = lttng_get_probe_list_head();
1184 /*
1185 * For each probe event, if we find that a probe event matches
1186 * our enabler, create an associated lttng_event if not
1187 * already present.
1188 */
1189 list_for_each_entry(probe_desc, probe_list, head) {
1190 for (i = 0; i < probe_desc->nr_events; i++) {
1191 int found = 0;
1192 struct hlist_head *head;
1193 const char *event_name;
1194 size_t name_len;
1195 uint32_t hash;
1196 struct lttng_event *event;
1197
1198 desc = probe_desc->event_desc[i];
1199 if (!lttng_desc_match_enabler(desc, enabler))
1200 continue;
1201 event_name = desc->name;
1202 name_len = strlen(event_name);
1203
1204 /*
1205 * Check if already created.
1206 */
1207 hash = jhash(event_name, name_len, 0);
1208 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
1209 lttng_hlist_for_each_entry(event, head, hlist) {
1210 if (event->desc == desc
1211 && event->chan == enabler->chan)
1212 found = 1;
1213 }
1214 if (found)
1215 continue;
1216
1217 /*
1218 * We need to create an event for this
1219 * event probe.
1220 */
1221 event = _lttng_event_create(enabler->chan,
1222 NULL, NULL, desc,
1223 LTTNG_KERNEL_TRACEPOINT);
1224 if (!event) {
1225 printk(KERN_INFO "Unable to create event %s\n",
1226 probe_desc->event_desc[i]->name);
1227 }
1228 }
1229 }
1230 }
1231
1232 static
1233 void lttng_create_syscall_if_missing(struct lttng_enabler *enabler)
1234 {
1235 int ret;
1236
1237 ret = lttng_syscalls_register(enabler->chan, NULL);
1238 WARN_ON_ONCE(ret);
1239 }
1240
1241 /*
1242 * Create struct lttng_event if it is missing and present in the list of
1243 * tracepoint probes.
1244 * Should be called with sessions mutex held.
1245 */
1246 static
1247 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
1248 {
1249 switch (enabler->event_param.instrumentation) {
1250 case LTTNG_KERNEL_TRACEPOINT:
1251 lttng_create_tracepoint_if_missing(enabler);
1252 break;
1253 case LTTNG_KERNEL_SYSCALL:
1254 lttng_create_syscall_if_missing(enabler);
1255 break;
1256 default:
1257 WARN_ON_ONCE(1);
1258 break;
1259 }
1260 }
1261
1262 /*
1263 * Create events associated with an enabler (if not already present),
1264 * and add backward reference from the event to the enabler.
1265 * Should be called with sessions mutex held.
1266 */
1267 static
1268 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
1269 {
1270 struct lttng_session *session = enabler->chan->session;
1271 struct lttng_event *event;
1272
1273 /* First ensure that probe events are created for this enabler. */
1274 lttng_create_event_if_missing(enabler);
1275
1276 /* For each event matching enabler in session event list. */
1277 list_for_each_entry(event, &session->events, list) {
1278 struct lttng_enabler_ref *enabler_ref;
1279
1280 if (!lttng_event_match_enabler(event, enabler))
1281 continue;
1282 enabler_ref = lttng_event_enabler_ref(event, enabler);
1283 if (!enabler_ref) {
1284 /*
1285 * If no backward ref, create it.
1286 * Add backward ref from event to enabler.
1287 */
1288 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1289 if (!enabler_ref)
1290 return -ENOMEM;
1291 enabler_ref->ref = enabler;
1292 list_add(&enabler_ref->node,
1293 &event->enablers_ref_head);
1294 }
1295
1296 /*
1297 * Link filter bytecodes if not linked yet.
1298 */
1299 lttng_enabler_event_link_bytecode(event, enabler);
1300
1301 /* TODO: merge event context. */
1302 }
1303 return 0;
1304 }
1305
1306 /*
1307 * Called at module load: connect the probe on all enablers matching
1308 * this event.
1309 * Called with sessions lock held.
1310 */
1311 int lttng_fix_pending_events(void)
1312 {
1313 struct lttng_session *session;
1314
1315 list_for_each_entry(session, &sessions, list)
1316 lttng_session_lazy_sync_enablers(session);
1317 return 0;
1318 }
1319
1320 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1321 struct lttng_kernel_event *event_param,
1322 struct lttng_channel *chan)
1323 {
1324 struct lttng_enabler *enabler;
1325
1326 enabler = kzalloc(sizeof(*enabler), GFP_KERNEL);
1327 if (!enabler)
1328 return NULL;
1329 enabler->type = type;
1330 INIT_LIST_HEAD(&enabler->filter_bytecode_head);
1331 memcpy(&enabler->event_param, event_param,
1332 sizeof(enabler->event_param));
1333 enabler->chan = chan;
1334 /* ctx left NULL */
1335 enabler->enabled = 0;
1336 enabler->evtype = LTTNG_TYPE_ENABLER;
1337 mutex_lock(&sessions_mutex);
1338 list_add(&enabler->node, &enabler->chan->session->enablers_head);
1339 lttng_session_lazy_sync_enablers(enabler->chan->session);
1340 mutex_unlock(&sessions_mutex);
1341 return enabler;
1342 }
1343
1344 int lttng_enabler_enable(struct lttng_enabler *enabler)
1345 {
1346 mutex_lock(&sessions_mutex);
1347 enabler->enabled = 1;
1348 lttng_session_lazy_sync_enablers(enabler->chan->session);
1349 mutex_unlock(&sessions_mutex);
1350 return 0;
1351 }
1352
1353 int lttng_enabler_disable(struct lttng_enabler *enabler)
1354 {
1355 mutex_lock(&sessions_mutex);
1356 enabler->enabled = 0;
1357 lttng_session_lazy_sync_enablers(enabler->chan->session);
1358 mutex_unlock(&sessions_mutex);
1359 return 0;
1360 }
1361
1362 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1363 struct lttng_kernel_filter_bytecode __user *bytecode)
1364 {
1365 struct lttng_filter_bytecode_node *bytecode_node;
1366 uint32_t bytecode_len;
1367 int ret;
1368
1369 ret = get_user(bytecode_len, &bytecode->len);
1370 if (ret)
1371 return ret;
1372 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1373 GFP_KERNEL);
1374 if (!bytecode_node)
1375 return -ENOMEM;
1376 ret = copy_from_user(&bytecode_node->bc, bytecode,
1377 sizeof(*bytecode) + bytecode_len);
1378 if (ret)
1379 goto error_free;
1380 bytecode_node->enabler = enabler;
1381 /* Enforce length based on allocated size */
1382 bytecode_node->bc.len = bytecode_len;
1383 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1384 lttng_session_lazy_sync_enablers(enabler->chan->session);
1385 return 0;
1386
1387 error_free:
1388 kfree(bytecode_node);
1389 return ret;
1390 }
1391
1392 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1393 struct lttng_kernel_context *context_param)
1394 {
1395 return -ENOSYS;
1396 }
1397
1398 static
1399 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1400 {
1401 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1402
1403 /* Destroy filter bytecode */
1404 list_for_each_entry_safe(filter_node, tmp_filter_node,
1405 &enabler->filter_bytecode_head, node) {
1406 kfree(filter_node);
1407 }
1408
1409 /* Destroy contexts */
1410 lttng_destroy_context(enabler->ctx);
1411
1412 list_del(&enabler->node);
1413 kfree(enabler);
1414 }
1415
1416 /*
1417 * lttng_session_sync_enablers should be called just before starting a
1418 * session.
1419 * Should be called with sessions mutex held.
1420 */
1421 static
1422 void lttng_session_sync_enablers(struct lttng_session *session)
1423 {
1424 struct lttng_enabler *enabler;
1425 struct lttng_event *event;
1426
1427 list_for_each_entry(enabler, &session->enablers_head, node)
1428 lttng_enabler_ref_events(enabler);
1429 /*
1430 * For each event, if at least one of its enablers is enabled,
1431 * and its channel and session transient states are enabled, we
1432 * enable the event, else we disable it.
1433 */
1434 list_for_each_entry(event, &session->events, list) {
1435 struct lttng_enabler_ref *enabler_ref;
1436 struct lttng_bytecode_runtime *runtime;
1437 int enabled = 0, has_enablers_without_bytecode = 0;
1438
1439 switch (event->instrumentation) {
1440 case LTTNG_KERNEL_TRACEPOINT:
1441 case LTTNG_KERNEL_SYSCALL:
1442 /* Enable events */
1443 list_for_each_entry(enabler_ref,
1444 &event->enablers_ref_head, node) {
1445 if (enabler_ref->ref->enabled) {
1446 enabled = 1;
1447 break;
1448 }
1449 }
1450 break;
1451 default:
1452 /* Not handled with lazy sync. */
1453 continue;
1454 }
1455 /*
1456 * Enabled state is based on union of enablers, with
1457 * intesection of session and channel transient enable
1458 * states.
1459 */
1460 enabled = enabled && session->tstate && event->chan->tstate;
1461
1462 ACCESS_ONCE(event->enabled) = enabled;
1463 /*
1464 * Sync tracepoint registration with event enabled
1465 * state.
1466 */
1467 if (enabled) {
1468 register_event(event);
1469 } else {
1470 _lttng_event_unregister(event);
1471 }
1472
1473 /* Check if has enablers without bytecode enabled */
1474 list_for_each_entry(enabler_ref,
1475 &event->enablers_ref_head, node) {
1476 if (enabler_ref->ref->enabled
1477 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1478 has_enablers_without_bytecode = 1;
1479 break;
1480 }
1481 }
1482 event->has_enablers_without_bytecode =
1483 has_enablers_without_bytecode;
1484
1485 /* Enable filters */
1486 list_for_each_entry(runtime,
1487 &event->bytecode_runtime_head, node)
1488 lttng_filter_sync_state(runtime);
1489 }
1490 }
1491
1492 /*
1493 * Apply enablers to session events, adding events to session if need
1494 * be. It is required after each modification applied to an active
1495 * session, and right before session "start".
1496 * "lazy" sync means we only sync if required.
1497 * Should be called with sessions mutex held.
1498 */
1499 static
1500 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1501 {
1502 /* We can skip if session is not active */
1503 if (!session->active)
1504 return;
1505 lttng_session_sync_enablers(session);
1506 }
1507
1508 /*
1509 * Serialize at most one packet worth of metadata into a metadata
1510 * channel.
1511 * We grab the metadata cache mutex to get exclusive access to our metadata
1512 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1513 * allows us to do racy operations such as looking for remaining space left in
1514 * packet and write, since mutual exclusion protects us from concurrent writes.
1515 * Mutual exclusion on the metadata cache allow us to read the cache content
1516 * without racing against reallocation of the cache by updates.
1517 * Returns the number of bytes written in the channel, 0 if no data
1518 * was written and a negative value on error.
1519 */
1520 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1521 struct channel *chan)
1522 {
1523 struct lib_ring_buffer_ctx ctx;
1524 int ret = 0;
1525 size_t len, reserve_len;
1526
1527 /*
1528 * Ensure we support mutiple get_next / put sequences followed by
1529 * put_next. The metadata cache lock protects reading the metadata
1530 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1531 * "flush" operations on the buffer invoked by different processes.
1532 * Moreover, since the metadata cache memory can be reallocated, we
1533 * need to have exclusive access against updates even though we only
1534 * read it.
1535 */
1536 mutex_lock(&stream->metadata_cache->lock);
1537 WARN_ON(stream->metadata_in < stream->metadata_out);
1538 if (stream->metadata_in != stream->metadata_out)
1539 goto end;
1540
1541 len = stream->metadata_cache->metadata_written -
1542 stream->metadata_in;
1543 if (!len)
1544 goto end;
1545 reserve_len = min_t(size_t,
1546 stream->transport->ops.packet_avail_size(chan),
1547 len);
1548 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
1549 sizeof(char), -1);
1550 /*
1551 * If reservation failed, return an error to the caller.
1552 */
1553 ret = stream->transport->ops.event_reserve(&ctx, 0);
1554 if (ret != 0) {
1555 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1556 goto end;
1557 }
1558 stream->transport->ops.event_write(&ctx,
1559 stream->metadata_cache->data + stream->metadata_in,
1560 reserve_len);
1561 stream->transport->ops.event_commit(&ctx);
1562 stream->metadata_in += reserve_len;
1563 ret = reserve_len;
1564
1565 end:
1566 mutex_unlock(&stream->metadata_cache->lock);
1567 return ret;
1568 }
1569
1570 /*
1571 * Write the metadata to the metadata cache.
1572 * Must be called with sessions_mutex held.
1573 * The metadata cache lock protects us from concurrent read access from
1574 * thread outputting metadata content to ring buffer.
1575 */
1576 int lttng_metadata_printf(struct lttng_session *session,
1577 const char *fmt, ...)
1578 {
1579 char *str;
1580 size_t len;
1581 va_list ap;
1582 struct lttng_metadata_stream *stream;
1583
1584 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
1585
1586 va_start(ap, fmt);
1587 str = kvasprintf(GFP_KERNEL, fmt, ap);
1588 va_end(ap);
1589 if (!str)
1590 return -ENOMEM;
1591
1592 len = strlen(str);
1593 mutex_lock(&session->metadata_cache->lock);
1594 if (session->metadata_cache->metadata_written + len >
1595 session->metadata_cache->cache_alloc) {
1596 char *tmp_cache_realloc;
1597 unsigned int tmp_cache_alloc_size;
1598
1599 tmp_cache_alloc_size = max_t(unsigned int,
1600 session->metadata_cache->cache_alloc + len,
1601 session->metadata_cache->cache_alloc << 1);
1602 tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size);
1603 if (!tmp_cache_realloc)
1604 goto err;
1605 if (session->metadata_cache->data) {
1606 memcpy(tmp_cache_realloc,
1607 session->metadata_cache->data,
1608 session->metadata_cache->cache_alloc);
1609 vfree(session->metadata_cache->data);
1610 }
1611
1612 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1613 session->metadata_cache->data = tmp_cache_realloc;
1614 }
1615 memcpy(session->metadata_cache->data +
1616 session->metadata_cache->metadata_written,
1617 str, len);
1618 session->metadata_cache->metadata_written += len;
1619 mutex_unlock(&session->metadata_cache->lock);
1620 kfree(str);
1621
1622 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1623 wake_up_interruptible(&stream->read_wait);
1624
1625 return 0;
1626
1627 err:
1628 mutex_unlock(&session->metadata_cache->lock);
1629 kfree(str);
1630 return -ENOMEM;
1631 }
1632
1633 /*
1634 * Must be called with sessions_mutex held.
1635 */
1636 static
1637 int _lttng_field_statedump(struct lttng_session *session,
1638 const struct lttng_event_field *field)
1639 {
1640 int ret = 0;
1641
1642 switch (field->type.atype) {
1643 case atype_integer:
1644 ret = lttng_metadata_printf(session,
1645 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1646 field->type.u.basic.integer.size,
1647 field->type.u.basic.integer.alignment,
1648 field->type.u.basic.integer.signedness,
1649 (field->type.u.basic.integer.encoding == lttng_encode_none)
1650 ? "none"
1651 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
1652 ? "UTF8"
1653 : "ASCII",
1654 field->type.u.basic.integer.base,
1655 #if __BYTE_ORDER == __BIG_ENDIAN
1656 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1657 #else
1658 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1659 #endif
1660 field->name);
1661 break;
1662 case atype_enum:
1663 ret = lttng_metadata_printf(session,
1664 " %s _%s;\n",
1665 field->type.u.basic.enumeration.name,
1666 field->name);
1667 break;
1668 case atype_array:
1669 {
1670 const struct lttng_basic_type *elem_type;
1671
1672 elem_type = &field->type.u.array.elem_type;
1673 ret = lttng_metadata_printf(session,
1674 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1675 elem_type->u.basic.integer.size,
1676 elem_type->u.basic.integer.alignment,
1677 elem_type->u.basic.integer.signedness,
1678 (elem_type->u.basic.integer.encoding == lttng_encode_none)
1679 ? "none"
1680 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
1681 ? "UTF8"
1682 : "ASCII",
1683 elem_type->u.basic.integer.base,
1684 #if __BYTE_ORDER == __BIG_ENDIAN
1685 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1686 #else
1687 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1688 #endif
1689 field->name, field->type.u.array.length);
1690 break;
1691 }
1692 case atype_sequence:
1693 {
1694 const struct lttng_basic_type *elem_type;
1695 const struct lttng_basic_type *length_type;
1696
1697 elem_type = &field->type.u.sequence.elem_type;
1698 length_type = &field->type.u.sequence.length_type;
1699 ret = lttng_metadata_printf(session,
1700 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1701 length_type->u.basic.integer.size,
1702 (unsigned int) length_type->u.basic.integer.alignment,
1703 length_type->u.basic.integer.signedness,
1704 (length_type->u.basic.integer.encoding == lttng_encode_none)
1705 ? "none"
1706 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
1707 ? "UTF8"
1708 : "ASCII"),
1709 length_type->u.basic.integer.base,
1710 #if __BYTE_ORDER == __BIG_ENDIAN
1711 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1712 #else
1713 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1714 #endif
1715 field->name);
1716 if (ret)
1717 return ret;
1718
1719 ret = lttng_metadata_printf(session,
1720 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1721 elem_type->u.basic.integer.size,
1722 (unsigned int) elem_type->u.basic.integer.alignment,
1723 elem_type->u.basic.integer.signedness,
1724 (elem_type->u.basic.integer.encoding == lttng_encode_none)
1725 ? "none"
1726 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
1727 ? "UTF8"
1728 : "ASCII"),
1729 elem_type->u.basic.integer.base,
1730 #if __BYTE_ORDER == __BIG_ENDIAN
1731 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1732 #else
1733 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1734 #endif
1735 field->name,
1736 field->name);
1737 break;
1738 }
1739
1740 case atype_string:
1741 /* Default encoding is UTF8 */
1742 ret = lttng_metadata_printf(session,
1743 " string%s _%s;\n",
1744 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
1745 " { encoding = ASCII; }" : "",
1746 field->name);
1747 break;
1748 default:
1749 WARN_ON_ONCE(1);
1750 return -EINVAL;
1751 }
1752 return ret;
1753 }
1754
1755 static
1756 int _lttng_context_metadata_statedump(struct lttng_session *session,
1757 struct lttng_ctx *ctx)
1758 {
1759 int ret = 0;
1760 int i;
1761
1762 if (!ctx)
1763 return 0;
1764 for (i = 0; i < ctx->nr_fields; i++) {
1765 const struct lttng_ctx_field *field = &ctx->fields[i];
1766
1767 ret = _lttng_field_statedump(session, &field->event_field);
1768 if (ret)
1769 return ret;
1770 }
1771 return ret;
1772 }
1773
1774 static
1775 int _lttng_fields_metadata_statedump(struct lttng_session *session,
1776 struct lttng_event *event)
1777 {
1778 const struct lttng_event_desc *desc = event->desc;
1779 int ret = 0;
1780 int i;
1781
1782 for (i = 0; i < desc->nr_fields; i++) {
1783 const struct lttng_event_field *field = &desc->fields[i];
1784
1785 ret = _lttng_field_statedump(session, field);
1786 if (ret)
1787 return ret;
1788 }
1789 return ret;
1790 }
1791
1792 /*
1793 * Must be called with sessions_mutex held.
1794 */
1795 static
1796 int _lttng_event_metadata_statedump(struct lttng_session *session,
1797 struct lttng_channel *chan,
1798 struct lttng_event *event)
1799 {
1800 int ret = 0;
1801
1802 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
1803 return 0;
1804 if (chan->channel_type == METADATA_CHANNEL)
1805 return 0;
1806
1807 ret = lttng_metadata_printf(session,
1808 "event {\n"
1809 " name = \"%s\";\n"
1810 " id = %u;\n"
1811 " stream_id = %u;\n",
1812 event->desc->name,
1813 event->id,
1814 event->chan->id);
1815 if (ret)
1816 goto end;
1817
1818 if (event->ctx) {
1819 ret = lttng_metadata_printf(session,
1820 " context := struct {\n");
1821 if (ret)
1822 goto end;
1823 }
1824 ret = _lttng_context_metadata_statedump(session, event->ctx);
1825 if (ret)
1826 goto end;
1827 if (event->ctx) {
1828 ret = lttng_metadata_printf(session,
1829 " };\n");
1830 if (ret)
1831 goto end;
1832 }
1833
1834 ret = lttng_metadata_printf(session,
1835 " fields := struct {\n"
1836 );
1837 if (ret)
1838 goto end;
1839
1840 ret = _lttng_fields_metadata_statedump(session, event);
1841 if (ret)
1842 goto end;
1843
1844 /*
1845 * LTTng space reservation can only reserve multiples of the
1846 * byte size.
1847 */
1848 ret = lttng_metadata_printf(session,
1849 " };\n"
1850 "};\n\n");
1851 if (ret)
1852 goto end;
1853
1854 event->metadata_dumped = 1;
1855 end:
1856 return ret;
1857
1858 }
1859
1860 /*
1861 * Must be called with sessions_mutex held.
1862 */
1863 static
1864 int _lttng_channel_metadata_statedump(struct lttng_session *session,
1865 struct lttng_channel *chan)
1866 {
1867 int ret = 0;
1868
1869 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
1870 return 0;
1871
1872 if (chan->channel_type == METADATA_CHANNEL)
1873 return 0;
1874
1875 WARN_ON_ONCE(!chan->header_type);
1876 ret = lttng_metadata_printf(session,
1877 "stream {\n"
1878 " id = %u;\n"
1879 " event.header := %s;\n"
1880 " packet.context := struct packet_context;\n",
1881 chan->id,
1882 chan->header_type == 1 ? "struct event_header_compact" :
1883 "struct event_header_large");
1884 if (ret)
1885 goto end;
1886
1887 if (chan->ctx) {
1888 ret = lttng_metadata_printf(session,
1889 " event.context := struct {\n");
1890 if (ret)
1891 goto end;
1892 }
1893 ret = _lttng_context_metadata_statedump(session, chan->ctx);
1894 if (ret)
1895 goto end;
1896 if (chan->ctx) {
1897 ret = lttng_metadata_printf(session,
1898 " };\n");
1899 if (ret)
1900 goto end;
1901 }
1902
1903 ret = lttng_metadata_printf(session,
1904 "};\n\n");
1905
1906 chan->metadata_dumped = 1;
1907 end:
1908 return ret;
1909 }
1910
1911 /*
1912 * Must be called with sessions_mutex held.
1913 */
1914 static
1915 int _lttng_stream_packet_context_declare(struct lttng_session *session)
1916 {
1917 return lttng_metadata_printf(session,
1918 "struct packet_context {\n"
1919 " uint64_clock_monotonic_t timestamp_begin;\n"
1920 " uint64_clock_monotonic_t timestamp_end;\n"
1921 " uint64_t content_size;\n"
1922 " uint64_t packet_size;\n"
1923 " unsigned long events_discarded;\n"
1924 " uint32_t cpu_id;\n"
1925 "};\n\n"
1926 );
1927 }
1928
1929 /*
1930 * Compact header:
1931 * id: range: 0 - 30.
1932 * id 31 is reserved to indicate an extended header.
1933 *
1934 * Large header:
1935 * id: range: 0 - 65534.
1936 * id 65535 is reserved to indicate an extended header.
1937 *
1938 * Must be called with sessions_mutex held.
1939 */
1940 static
1941 int _lttng_event_header_declare(struct lttng_session *session)
1942 {
1943 return lttng_metadata_printf(session,
1944 "struct event_header_compact {\n"
1945 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1946 " variant <id> {\n"
1947 " struct {\n"
1948 " uint27_clock_monotonic_t timestamp;\n"
1949 " } compact;\n"
1950 " struct {\n"
1951 " uint32_t id;\n"
1952 " uint64_clock_monotonic_t timestamp;\n"
1953 " } extended;\n"
1954 " } v;\n"
1955 "} align(%u);\n"
1956 "\n"
1957 "struct event_header_large {\n"
1958 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1959 " variant <id> {\n"
1960 " struct {\n"
1961 " uint32_clock_monotonic_t timestamp;\n"
1962 " } compact;\n"
1963 " struct {\n"
1964 " uint32_t id;\n"
1965 " uint64_clock_monotonic_t timestamp;\n"
1966 " } extended;\n"
1967 " } v;\n"
1968 "} align(%u);\n\n",
1969 lttng_alignof(uint32_t) * CHAR_BIT,
1970 lttng_alignof(uint16_t) * CHAR_BIT
1971 );
1972 }
1973
1974 /*
1975 * Approximation of NTP time of day to clock monotonic correlation,
1976 * taken at start of trace.
1977 * Yes, this is only an approximation. Yes, we can (and will) do better
1978 * in future versions.
1979 * Return 0 if offset is negative. It may happen if the system sets
1980 * the REALTIME clock to 0 after boot.
1981 */
1982 static
1983 uint64_t measure_clock_offset(void)
1984 {
1985 uint64_t monotonic_avg, monotonic[2], realtime;
1986 int64_t offset;
1987 struct timespec rts = { 0, 0 };
1988 unsigned long flags;
1989
1990 /* Disable interrupts to increase correlation precision. */
1991 local_irq_save(flags);
1992 monotonic[0] = trace_clock_read64();
1993 getnstimeofday(&rts);
1994 monotonic[1] = trace_clock_read64();
1995 local_irq_restore(flags);
1996
1997 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
1998 realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
1999 realtime += rts.tv_nsec;
2000 offset = (int64_t) realtime - monotonic_avg;
2001 if (offset < 0)
2002 return 0;
2003 return offset;
2004 }
2005
2006 /*
2007 * Output metadata into this session's metadata buffers.
2008 * Must be called with sessions_mutex held.
2009 */
2010 static
2011 int _lttng_session_metadata_statedump(struct lttng_session *session)
2012 {
2013 unsigned char *uuid_c = session->uuid.b;
2014 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
2015 struct lttng_channel *chan;
2016 struct lttng_event *event;
2017 int ret = 0;
2018
2019 if (!ACCESS_ONCE(session->active))
2020 return 0;
2021 if (session->metadata_dumped)
2022 goto skip_session;
2023
2024 snprintf(uuid_s, sizeof(uuid_s),
2025 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2026 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2027 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2028 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2029 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
2030
2031 ret = lttng_metadata_printf(session,
2032 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2033 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2034 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2035 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2036 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2037 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2038 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2039 "\n"
2040 "trace {\n"
2041 " major = %u;\n"
2042 " minor = %u;\n"
2043 " uuid = \"%s\";\n"
2044 " byte_order = %s;\n"
2045 " packet.header := struct {\n"
2046 " uint32_t magic;\n"
2047 " uint8_t uuid[16];\n"
2048 " uint32_t stream_id;\n"
2049 " };\n"
2050 "};\n\n",
2051 lttng_alignof(uint8_t) * CHAR_BIT,
2052 lttng_alignof(uint16_t) * CHAR_BIT,
2053 lttng_alignof(uint32_t) * CHAR_BIT,
2054 lttng_alignof(uint64_t) * CHAR_BIT,
2055 sizeof(unsigned long) * CHAR_BIT,
2056 lttng_alignof(unsigned long) * CHAR_BIT,
2057 CTF_SPEC_MAJOR,
2058 CTF_SPEC_MINOR,
2059 uuid_s,
2060 #if __BYTE_ORDER == __BIG_ENDIAN
2061 "be"
2062 #else
2063 "le"
2064 #endif
2065 );
2066 if (ret)
2067 goto end;
2068
2069 ret = lttng_metadata_printf(session,
2070 "env {\n"
2071 " hostname = \"%s\";\n"
2072 " domain = \"kernel\";\n"
2073 " sysname = \"%s\";\n"
2074 " kernel_release = \"%s\";\n"
2075 " kernel_version = \"%s\";\n"
2076 " tracer_name = \"lttng-modules\";\n"
2077 " tracer_major = %d;\n"
2078 " tracer_minor = %d;\n"
2079 " tracer_patchlevel = %d;\n"
2080 "};\n\n",
2081 current->nsproxy->uts_ns->name.nodename,
2082 utsname()->sysname,
2083 utsname()->release,
2084 utsname()->version,
2085 LTTNG_MODULES_MAJOR_VERSION,
2086 LTTNG_MODULES_MINOR_VERSION,
2087 LTTNG_MODULES_PATCHLEVEL_VERSION
2088 );
2089 if (ret)
2090 goto end;
2091
2092 ret = lttng_metadata_printf(session,
2093 "clock {\n"
2094 " name = %s;\n",
2095 "monotonic"
2096 );
2097 if (ret)
2098 goto end;
2099
2100 if (!trace_clock_uuid(clock_uuid_s)) {
2101 ret = lttng_metadata_printf(session,
2102 " uuid = \"%s\";\n",
2103 clock_uuid_s
2104 );
2105 if (ret)
2106 goto end;
2107 }
2108
2109 ret = lttng_metadata_printf(session,
2110 " description = \"Monotonic Clock\";\n"
2111 " freq = %llu; /* Frequency, in Hz */\n"
2112 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2113 " offset = %llu;\n"
2114 "};\n\n",
2115 (unsigned long long) trace_clock_freq(),
2116 (unsigned long long) measure_clock_offset()
2117 );
2118 if (ret)
2119 goto end;
2120
2121 ret = lttng_metadata_printf(session,
2122 "typealias integer {\n"
2123 " size = 27; align = 1; signed = false;\n"
2124 " map = clock.monotonic.value;\n"
2125 "} := uint27_clock_monotonic_t;\n"
2126 "\n"
2127 "typealias integer {\n"
2128 " size = 32; align = %u; signed = false;\n"
2129 " map = clock.monotonic.value;\n"
2130 "} := uint32_clock_monotonic_t;\n"
2131 "\n"
2132 "typealias integer {\n"
2133 " size = 64; align = %u; signed = false;\n"
2134 " map = clock.monotonic.value;\n"
2135 "} := uint64_clock_monotonic_t;\n\n",
2136 lttng_alignof(uint32_t) * CHAR_BIT,
2137 lttng_alignof(uint64_t) * CHAR_BIT
2138 );
2139 if (ret)
2140 goto end;
2141
2142 ret = _lttng_stream_packet_context_declare(session);
2143 if (ret)
2144 goto end;
2145
2146 ret = _lttng_event_header_declare(session);
2147 if (ret)
2148 goto end;
2149
2150 skip_session:
2151 list_for_each_entry(chan, &session->chan, list) {
2152 ret = _lttng_channel_metadata_statedump(session, chan);
2153 if (ret)
2154 goto end;
2155 }
2156
2157 list_for_each_entry(event, &session->events, list) {
2158 ret = _lttng_event_metadata_statedump(session, event->chan, event);
2159 if (ret)
2160 goto end;
2161 }
2162 session->metadata_dumped = 1;
2163 end:
2164 return ret;
2165 }
2166
2167 /**
2168 * lttng_transport_register - LTT transport registration
2169 * @transport: transport structure
2170 *
2171 * Registers a transport which can be used as output to extract the data out of
2172 * LTTng. The module calling this registration function must ensure that no
2173 * trap-inducing code will be executed by the transport functions. E.g.
2174 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2175 * is made visible to the transport function. This registration acts as a
2176 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2177 * after its registration must it synchronize the TLBs.
2178 */
2179 void lttng_transport_register(struct lttng_transport *transport)
2180 {
2181 /*
2182 * Make sure no page fault can be triggered by the module about to be
2183 * registered. We deal with this here so we don't have to call
2184 * vmalloc_sync_all() in each module's init.
2185 */
2186 wrapper_vmalloc_sync_all();
2187
2188 mutex_lock(&sessions_mutex);
2189 list_add_tail(&transport->node, &lttng_transport_list);
2190 mutex_unlock(&sessions_mutex);
2191 }
2192 EXPORT_SYMBOL_GPL(lttng_transport_register);
2193
2194 /**
2195 * lttng_transport_unregister - LTT transport unregistration
2196 * @transport: transport structure
2197 */
2198 void lttng_transport_unregister(struct lttng_transport *transport)
2199 {
2200 mutex_lock(&sessions_mutex);
2201 list_del(&transport->node);
2202 mutex_unlock(&sessions_mutex);
2203 }
2204 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
2205
2206 static int __init lttng_events_init(void)
2207 {
2208 int ret;
2209
2210 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2211 if (ret)
2212 return ret;
2213 ret = wrapper_get_pfnblock_flags_mask_init();
2214 if (ret)
2215 return ret;
2216 ret = wrapper_get_pageblock_flags_mask_init();
2217 if (ret)
2218 return ret;
2219 ret = lttng_context_init();
2220 if (ret)
2221 return ret;
2222 ret = lttng_tracepoint_init();
2223 if (ret)
2224 goto error_tp;
2225 event_cache = KMEM_CACHE(lttng_event, 0);
2226 if (!event_cache) {
2227 ret = -ENOMEM;
2228 goto error_kmem;
2229 }
2230 ret = lttng_abi_init();
2231 if (ret)
2232 goto error_abi;
2233 ret = lttng_logger_init();
2234 if (ret)
2235 goto error_logger;
2236 return 0;
2237
2238 error_logger:
2239 lttng_abi_exit();
2240 error_abi:
2241 kmem_cache_destroy(event_cache);
2242 error_kmem:
2243 lttng_tracepoint_exit();
2244 error_tp:
2245 lttng_context_exit();
2246 return ret;
2247 }
2248
2249 module_init(lttng_events_init);
2250
2251 static void __exit lttng_events_exit(void)
2252 {
2253 struct lttng_session *session, *tmpsession;
2254
2255 lttng_logger_exit();
2256 lttng_abi_exit();
2257 list_for_each_entry_safe(session, tmpsession, &sessions, list)
2258 lttng_session_destroy(session);
2259 kmem_cache_destroy(event_cache);
2260 lttng_tracepoint_exit();
2261 lttng_context_exit();
2262 }
2263
2264 module_exit(lttng_events_exit);
2265
2266 MODULE_LICENSE("GPL and additional rights");
2267 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2268 MODULE_DESCRIPTION("LTTng Events");
2269 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
2270 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
2271 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
2272 LTTNG_MODULES_EXTRAVERSION);
This page took 0.105479 seconds and 4 git commands to generate.