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