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