Fix: handle negative clock offset
[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 if (atomic_long_add_unless(&session->file->f_count,
1089 1, INT_MAX) == INT_MAX) {
1090 goto refcount_error;
1091 }
1092 ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file);
1093 if (ret < 0)
1094 goto open_error;
1095 m = tracker_pids_list_file->private_data;
1096 m->private = session;
1097 fd_install(file_fd, tracker_pids_list_file);
1098
1099 return file_fd;
1100
1101 open_error:
1102 atomic_long_dec(&session->file->f_count);
1103 refcount_error:
1104 fput(tracker_pids_list_file);
1105 file_error:
1106 put_unused_fd(file_fd);
1107 fd_error:
1108 return ret;
1109 }
1110
1111 /*
1112 * Enabler management.
1113 */
1114 static
1115 int lttng_match_enabler_wildcard(const char *desc_name,
1116 const char *name)
1117 {
1118 /* Compare excluding final '*' */
1119 if (strncmp(desc_name, name, strlen(name) - 1))
1120 return 0;
1121 return 1;
1122 }
1123
1124 static
1125 int lttng_match_enabler_name(const char *desc_name,
1126 const char *name)
1127 {
1128 if (strcmp(desc_name, name))
1129 return 0;
1130 return 1;
1131 }
1132
1133 static
1134 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1135 struct lttng_enabler *enabler)
1136 {
1137 const char *desc_name, *enabler_name;
1138
1139 enabler_name = enabler->event_param.name;
1140 switch (enabler->event_param.instrumentation) {
1141 case LTTNG_KERNEL_TRACEPOINT:
1142 desc_name = desc->name;
1143 break;
1144 case LTTNG_KERNEL_SYSCALL:
1145 desc_name = desc->name;
1146 if (!strncmp(desc_name, "compat_", strlen("compat_")))
1147 desc_name += strlen("compat_");
1148 if (!strncmp(desc_name, "syscall_exit_",
1149 strlen("syscall_exit_"))) {
1150 desc_name += strlen("syscall_exit_");
1151 } else if (!strncmp(desc_name, "syscall_entry_",
1152 strlen("syscall_entry_"))) {
1153 desc_name += strlen("syscall_entry_");
1154 } else {
1155 WARN_ON_ONCE(1);
1156 return -EINVAL;
1157 }
1158 break;
1159 default:
1160 WARN_ON_ONCE(1);
1161 return -EINVAL;
1162 }
1163 switch (enabler->type) {
1164 case LTTNG_ENABLER_WILDCARD:
1165 return lttng_match_enabler_wildcard(desc_name, enabler_name);
1166 case LTTNG_ENABLER_NAME:
1167 return lttng_match_enabler_name(desc_name, enabler_name);
1168 default:
1169 return -EINVAL;
1170 }
1171 }
1172
1173 static
1174 int lttng_event_match_enabler(struct lttng_event *event,
1175 struct lttng_enabler *enabler)
1176 {
1177 if (enabler->event_param.instrumentation != event->instrumentation)
1178 return 0;
1179 if (lttng_desc_match_enabler(event->desc, enabler)
1180 && event->chan == enabler->chan)
1181 return 1;
1182 else
1183 return 0;
1184 }
1185
1186 static
1187 struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event,
1188 struct lttng_enabler *enabler)
1189 {
1190 struct lttng_enabler_ref *enabler_ref;
1191
1192 list_for_each_entry(enabler_ref,
1193 &event->enablers_ref_head, node) {
1194 if (enabler_ref->ref == enabler)
1195 return enabler_ref;
1196 }
1197 return NULL;
1198 }
1199
1200 static
1201 void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
1202 {
1203 struct lttng_session *session = enabler->chan->session;
1204 struct lttng_probe_desc *probe_desc;
1205 const struct lttng_event_desc *desc;
1206 int i;
1207 struct list_head *probe_list;
1208
1209 probe_list = lttng_get_probe_list_head();
1210 /*
1211 * For each probe event, if we find that a probe event matches
1212 * our enabler, create an associated lttng_event if not
1213 * already present.
1214 */
1215 list_for_each_entry(probe_desc, probe_list, head) {
1216 for (i = 0; i < probe_desc->nr_events; i++) {
1217 int found = 0;
1218 struct hlist_head *head;
1219 const char *event_name;
1220 size_t name_len;
1221 uint32_t hash;
1222 struct lttng_event *event;
1223
1224 desc = probe_desc->event_desc[i];
1225 if (!lttng_desc_match_enabler(desc, enabler))
1226 continue;
1227 event_name = desc->name;
1228 name_len = strlen(event_name);
1229
1230 /*
1231 * Check if already created.
1232 */
1233 hash = jhash(event_name, name_len, 0);
1234 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
1235 lttng_hlist_for_each_entry(event, head, hlist) {
1236 if (event->desc == desc
1237 && event->chan == enabler->chan)
1238 found = 1;
1239 }
1240 if (found)
1241 continue;
1242
1243 /*
1244 * We need to create an event for this
1245 * event probe.
1246 */
1247 event = _lttng_event_create(enabler->chan,
1248 NULL, NULL, desc,
1249 LTTNG_KERNEL_TRACEPOINT);
1250 if (!event) {
1251 printk(KERN_INFO "Unable to create event %s\n",
1252 probe_desc->event_desc[i]->name);
1253 }
1254 }
1255 }
1256 }
1257
1258 static
1259 void lttng_create_syscall_if_missing(struct lttng_enabler *enabler)
1260 {
1261 int ret;
1262
1263 ret = lttng_syscalls_register(enabler->chan, NULL);
1264 WARN_ON_ONCE(ret);
1265 }
1266
1267 /*
1268 * Create struct lttng_event if it is missing and present in the list of
1269 * tracepoint probes.
1270 * Should be called with sessions mutex held.
1271 */
1272 static
1273 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
1274 {
1275 switch (enabler->event_param.instrumentation) {
1276 case LTTNG_KERNEL_TRACEPOINT:
1277 lttng_create_tracepoint_if_missing(enabler);
1278 break;
1279 case LTTNG_KERNEL_SYSCALL:
1280 lttng_create_syscall_if_missing(enabler);
1281 break;
1282 default:
1283 WARN_ON_ONCE(1);
1284 break;
1285 }
1286 }
1287
1288 /*
1289 * Create events associated with an enabler (if not already present),
1290 * and add backward reference from the event to the enabler.
1291 * Should be called with sessions mutex held.
1292 */
1293 static
1294 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
1295 {
1296 struct lttng_session *session = enabler->chan->session;
1297 struct lttng_event *event;
1298
1299 /* First ensure that probe events are created for this enabler. */
1300 lttng_create_event_if_missing(enabler);
1301
1302 /* For each event matching enabler in session event list. */
1303 list_for_each_entry(event, &session->events, list) {
1304 struct lttng_enabler_ref *enabler_ref;
1305
1306 if (!lttng_event_match_enabler(event, enabler))
1307 continue;
1308 enabler_ref = lttng_event_enabler_ref(event, enabler);
1309 if (!enabler_ref) {
1310 /*
1311 * If no backward ref, create it.
1312 * Add backward ref from event to enabler.
1313 */
1314 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1315 if (!enabler_ref)
1316 return -ENOMEM;
1317 enabler_ref->ref = enabler;
1318 list_add(&enabler_ref->node,
1319 &event->enablers_ref_head);
1320 }
1321
1322 /*
1323 * Link filter bytecodes if not linked yet.
1324 */
1325 lttng_enabler_event_link_bytecode(event, enabler);
1326
1327 /* TODO: merge event context. */
1328 }
1329 return 0;
1330 }
1331
1332 /*
1333 * Called at module load: connect the probe on all enablers matching
1334 * this event.
1335 * Called with sessions lock held.
1336 */
1337 int lttng_fix_pending_events(void)
1338 {
1339 struct lttng_session *session;
1340
1341 list_for_each_entry(session, &sessions, list)
1342 lttng_session_lazy_sync_enablers(session);
1343 return 0;
1344 }
1345
1346 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1347 struct lttng_kernel_event *event_param,
1348 struct lttng_channel *chan)
1349 {
1350 struct lttng_enabler *enabler;
1351
1352 enabler = kzalloc(sizeof(*enabler), GFP_KERNEL);
1353 if (!enabler)
1354 return NULL;
1355 enabler->type = type;
1356 INIT_LIST_HEAD(&enabler->filter_bytecode_head);
1357 memcpy(&enabler->event_param, event_param,
1358 sizeof(enabler->event_param));
1359 enabler->chan = chan;
1360 /* ctx left NULL */
1361 enabler->enabled = 0;
1362 enabler->evtype = LTTNG_TYPE_ENABLER;
1363 mutex_lock(&sessions_mutex);
1364 list_add(&enabler->node, &enabler->chan->session->enablers_head);
1365 lttng_session_lazy_sync_enablers(enabler->chan->session);
1366 mutex_unlock(&sessions_mutex);
1367 return enabler;
1368 }
1369
1370 int lttng_enabler_enable(struct lttng_enabler *enabler)
1371 {
1372 mutex_lock(&sessions_mutex);
1373 enabler->enabled = 1;
1374 lttng_session_lazy_sync_enablers(enabler->chan->session);
1375 mutex_unlock(&sessions_mutex);
1376 return 0;
1377 }
1378
1379 int lttng_enabler_disable(struct lttng_enabler *enabler)
1380 {
1381 mutex_lock(&sessions_mutex);
1382 enabler->enabled = 0;
1383 lttng_session_lazy_sync_enablers(enabler->chan->session);
1384 mutex_unlock(&sessions_mutex);
1385 return 0;
1386 }
1387
1388 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1389 struct lttng_kernel_filter_bytecode __user *bytecode)
1390 {
1391 struct lttng_filter_bytecode_node *bytecode_node;
1392 uint32_t bytecode_len;
1393 int ret;
1394
1395 ret = get_user(bytecode_len, &bytecode->len);
1396 if (ret)
1397 return ret;
1398 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1399 GFP_KERNEL);
1400 if (!bytecode_node)
1401 return -ENOMEM;
1402 ret = copy_from_user(&bytecode_node->bc, bytecode,
1403 sizeof(*bytecode) + bytecode_len);
1404 if (ret)
1405 goto error_free;
1406 bytecode_node->enabler = enabler;
1407 /* Enforce length based on allocated size */
1408 bytecode_node->bc.len = bytecode_len;
1409 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1410 lttng_session_lazy_sync_enablers(enabler->chan->session);
1411 return 0;
1412
1413 error_free:
1414 kfree(bytecode_node);
1415 return ret;
1416 }
1417
1418 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1419 struct lttng_kernel_context *context_param)
1420 {
1421 return -ENOSYS;
1422 }
1423
1424 static
1425 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1426 {
1427 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1428
1429 /* Destroy filter bytecode */
1430 list_for_each_entry_safe(filter_node, tmp_filter_node,
1431 &enabler->filter_bytecode_head, node) {
1432 kfree(filter_node);
1433 }
1434
1435 /* Destroy contexts */
1436 lttng_destroy_context(enabler->ctx);
1437
1438 list_del(&enabler->node);
1439 kfree(enabler);
1440 }
1441
1442 /*
1443 * lttng_session_sync_enablers should be called just before starting a
1444 * session.
1445 * Should be called with sessions mutex held.
1446 */
1447 static
1448 void lttng_session_sync_enablers(struct lttng_session *session)
1449 {
1450 struct lttng_enabler *enabler;
1451 struct lttng_event *event;
1452
1453 list_for_each_entry(enabler, &session->enablers_head, node)
1454 lttng_enabler_ref_events(enabler);
1455 /*
1456 * For each event, if at least one of its enablers is enabled,
1457 * and its channel and session transient states are enabled, we
1458 * enable the event, else we disable it.
1459 */
1460 list_for_each_entry(event, &session->events, list) {
1461 struct lttng_enabler_ref *enabler_ref;
1462 struct lttng_bytecode_runtime *runtime;
1463 int enabled = 0, has_enablers_without_bytecode = 0;
1464
1465 switch (event->instrumentation) {
1466 case LTTNG_KERNEL_TRACEPOINT:
1467 case LTTNG_KERNEL_SYSCALL:
1468 /* Enable events */
1469 list_for_each_entry(enabler_ref,
1470 &event->enablers_ref_head, node) {
1471 if (enabler_ref->ref->enabled) {
1472 enabled = 1;
1473 break;
1474 }
1475 }
1476 break;
1477 default:
1478 /* Not handled with lazy sync. */
1479 continue;
1480 }
1481 /*
1482 * Enabled state is based on union of enablers, with
1483 * intesection of session and channel transient enable
1484 * states.
1485 */
1486 enabled = enabled && session->tstate && event->chan->tstate;
1487
1488 ACCESS_ONCE(event->enabled) = enabled;
1489 /*
1490 * Sync tracepoint registration with event enabled
1491 * state.
1492 */
1493 if (enabled) {
1494 register_event(event);
1495 } else {
1496 _lttng_event_unregister(event);
1497 }
1498
1499 /* Check if has enablers without bytecode enabled */
1500 list_for_each_entry(enabler_ref,
1501 &event->enablers_ref_head, node) {
1502 if (enabler_ref->ref->enabled
1503 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1504 has_enablers_without_bytecode = 1;
1505 break;
1506 }
1507 }
1508 event->has_enablers_without_bytecode =
1509 has_enablers_without_bytecode;
1510
1511 /* Enable filters */
1512 list_for_each_entry(runtime,
1513 &event->bytecode_runtime_head, node)
1514 lttng_filter_sync_state(runtime);
1515 }
1516 }
1517
1518 /*
1519 * Apply enablers to session events, adding events to session if need
1520 * be. It is required after each modification applied to an active
1521 * session, and right before session "start".
1522 * "lazy" sync means we only sync if required.
1523 * Should be called with sessions mutex held.
1524 */
1525 static
1526 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1527 {
1528 /* We can skip if session is not active */
1529 if (!session->active)
1530 return;
1531 lttng_session_sync_enablers(session);
1532 }
1533
1534 /*
1535 * Serialize at most one packet worth of metadata into a metadata
1536 * channel.
1537 * We grab the metadata cache mutex to get exclusive access to our metadata
1538 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1539 * allows us to do racy operations such as looking for remaining space left in
1540 * packet and write, since mutual exclusion protects us from concurrent writes.
1541 * Mutual exclusion on the metadata cache allow us to read the cache content
1542 * without racing against reallocation of the cache by updates.
1543 * Returns the number of bytes written in the channel, 0 if no data
1544 * was written and a negative value on error.
1545 */
1546 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1547 struct channel *chan)
1548 {
1549 struct lib_ring_buffer_ctx ctx;
1550 int ret = 0;
1551 size_t len, reserve_len;
1552
1553 /*
1554 * Ensure we support mutiple get_next / put sequences followed by
1555 * put_next. The metadata cache lock protects reading the metadata
1556 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1557 * "flush" operations on the buffer invoked by different processes.
1558 * Moreover, since the metadata cache memory can be reallocated, we
1559 * need to have exclusive access against updates even though we only
1560 * read it.
1561 */
1562 mutex_lock(&stream->metadata_cache->lock);
1563 WARN_ON(stream->metadata_in < stream->metadata_out);
1564 if (stream->metadata_in != stream->metadata_out)
1565 goto end;
1566
1567 /* Metadata regenerated, change the version. */
1568 if (stream->metadata_cache->version != stream->version)
1569 stream->version = stream->metadata_cache->version;
1570
1571 len = stream->metadata_cache->metadata_written -
1572 stream->metadata_in;
1573 if (!len)
1574 goto end;
1575 reserve_len = min_t(size_t,
1576 stream->transport->ops.packet_avail_size(chan),
1577 len);
1578 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
1579 sizeof(char), -1);
1580 /*
1581 * If reservation failed, return an error to the caller.
1582 */
1583 ret = stream->transport->ops.event_reserve(&ctx, 0);
1584 if (ret != 0) {
1585 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1586 goto end;
1587 }
1588 stream->transport->ops.event_write(&ctx,
1589 stream->metadata_cache->data + stream->metadata_in,
1590 reserve_len);
1591 stream->transport->ops.event_commit(&ctx);
1592 stream->metadata_in += reserve_len;
1593 ret = reserve_len;
1594
1595 end:
1596 mutex_unlock(&stream->metadata_cache->lock);
1597 return ret;
1598 }
1599
1600 /*
1601 * Write the metadata to the metadata cache.
1602 * Must be called with sessions_mutex held.
1603 * The metadata cache lock protects us from concurrent read access from
1604 * thread outputting metadata content to ring buffer.
1605 */
1606 int lttng_metadata_printf(struct lttng_session *session,
1607 const char *fmt, ...)
1608 {
1609 char *str;
1610 size_t len;
1611 va_list ap;
1612 struct lttng_metadata_stream *stream;
1613
1614 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
1615
1616 va_start(ap, fmt);
1617 str = kvasprintf(GFP_KERNEL, fmt, ap);
1618 va_end(ap);
1619 if (!str)
1620 return -ENOMEM;
1621
1622 len = strlen(str);
1623 mutex_lock(&session->metadata_cache->lock);
1624 if (session->metadata_cache->metadata_written + len >
1625 session->metadata_cache->cache_alloc) {
1626 char *tmp_cache_realloc;
1627 unsigned int tmp_cache_alloc_size;
1628
1629 tmp_cache_alloc_size = max_t(unsigned int,
1630 session->metadata_cache->cache_alloc + len,
1631 session->metadata_cache->cache_alloc << 1);
1632 tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size);
1633 if (!tmp_cache_realloc)
1634 goto err;
1635 if (session->metadata_cache->data) {
1636 memcpy(tmp_cache_realloc,
1637 session->metadata_cache->data,
1638 session->metadata_cache->cache_alloc);
1639 vfree(session->metadata_cache->data);
1640 }
1641
1642 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1643 session->metadata_cache->data = tmp_cache_realloc;
1644 }
1645 memcpy(session->metadata_cache->data +
1646 session->metadata_cache->metadata_written,
1647 str, len);
1648 session->metadata_cache->metadata_written += len;
1649 mutex_unlock(&session->metadata_cache->lock);
1650 kfree(str);
1651
1652 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1653 wake_up_interruptible(&stream->read_wait);
1654
1655 return 0;
1656
1657 err:
1658 mutex_unlock(&session->metadata_cache->lock);
1659 kfree(str);
1660 return -ENOMEM;
1661 }
1662
1663 /*
1664 * Must be called with sessions_mutex held.
1665 */
1666 static
1667 int _lttng_field_statedump(struct lttng_session *session,
1668 const struct lttng_event_field *field)
1669 {
1670 int ret = 0;
1671
1672 switch (field->type.atype) {
1673 case atype_integer:
1674 ret = lttng_metadata_printf(session,
1675 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1676 field->type.u.basic.integer.size,
1677 field->type.u.basic.integer.alignment,
1678 field->type.u.basic.integer.signedness,
1679 (field->type.u.basic.integer.encoding == lttng_encode_none)
1680 ? "none"
1681 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
1682 ? "UTF8"
1683 : "ASCII",
1684 field->type.u.basic.integer.base,
1685 #ifdef __BIG_ENDIAN
1686 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1687 #else
1688 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1689 #endif
1690 field->name);
1691 break;
1692 case atype_enum:
1693 ret = lttng_metadata_printf(session,
1694 " %s _%s;\n",
1695 field->type.u.basic.enumeration.name,
1696 field->name);
1697 break;
1698 case atype_array:
1699 {
1700 const struct lttng_basic_type *elem_type;
1701
1702 elem_type = &field->type.u.array.elem_type;
1703 if (field->type.u.array.elem_alignment) {
1704 ret = lttng_metadata_printf(session,
1705 " struct { } align(%u) _%s_padding;\n",
1706 field->type.u.array.elem_alignment * CHAR_BIT,
1707 field->name);
1708 if (ret)
1709 return ret;
1710 }
1711 ret = lttng_metadata_printf(session,
1712 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1713 elem_type->u.basic.integer.size,
1714 elem_type->u.basic.integer.alignment,
1715 elem_type->u.basic.integer.signedness,
1716 (elem_type->u.basic.integer.encoding == lttng_encode_none)
1717 ? "none"
1718 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
1719 ? "UTF8"
1720 : "ASCII",
1721 elem_type->u.basic.integer.base,
1722 #ifdef __BIG_ENDIAN
1723 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1724 #else
1725 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1726 #endif
1727 field->name, field->type.u.array.length);
1728 break;
1729 }
1730 case atype_sequence:
1731 {
1732 const struct lttng_basic_type *elem_type;
1733 const struct lttng_basic_type *length_type;
1734
1735 elem_type = &field->type.u.sequence.elem_type;
1736 length_type = &field->type.u.sequence.length_type;
1737 ret = lttng_metadata_printf(session,
1738 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1739 length_type->u.basic.integer.size,
1740 (unsigned int) length_type->u.basic.integer.alignment,
1741 length_type->u.basic.integer.signedness,
1742 (length_type->u.basic.integer.encoding == lttng_encode_none)
1743 ? "none"
1744 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
1745 ? "UTF8"
1746 : "ASCII"),
1747 length_type->u.basic.integer.base,
1748 #ifdef __BIG_ENDIAN
1749 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1750 #else
1751 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1752 #endif
1753 field->name);
1754 if (ret)
1755 return ret;
1756
1757 if (field->type.u.sequence.elem_alignment) {
1758 ret = lttng_metadata_printf(session,
1759 " struct { } align(%u) _%s_padding;\n",
1760 field->type.u.sequence.elem_alignment * CHAR_BIT,
1761 field->name);
1762 if (ret)
1763 return ret;
1764 }
1765 ret = lttng_metadata_printf(session,
1766 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1767 elem_type->u.basic.integer.size,
1768 (unsigned int) elem_type->u.basic.integer.alignment,
1769 elem_type->u.basic.integer.signedness,
1770 (elem_type->u.basic.integer.encoding == lttng_encode_none)
1771 ? "none"
1772 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
1773 ? "UTF8"
1774 : "ASCII"),
1775 elem_type->u.basic.integer.base,
1776 #ifdef __BIG_ENDIAN
1777 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1778 #else
1779 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1780 #endif
1781 field->name,
1782 field->name);
1783 break;
1784 }
1785
1786 case atype_string:
1787 /* Default encoding is UTF8 */
1788 ret = lttng_metadata_printf(session,
1789 " string%s _%s;\n",
1790 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
1791 " { encoding = ASCII; }" : "",
1792 field->name);
1793 break;
1794 default:
1795 WARN_ON_ONCE(1);
1796 return -EINVAL;
1797 }
1798 return ret;
1799 }
1800
1801 static
1802 int _lttng_context_metadata_statedump(struct lttng_session *session,
1803 struct lttng_ctx *ctx)
1804 {
1805 int ret = 0;
1806 int i;
1807
1808 if (!ctx)
1809 return 0;
1810 for (i = 0; i < ctx->nr_fields; i++) {
1811 const struct lttng_ctx_field *field = &ctx->fields[i];
1812
1813 ret = _lttng_field_statedump(session, &field->event_field);
1814 if (ret)
1815 return ret;
1816 }
1817 return ret;
1818 }
1819
1820 static
1821 int _lttng_fields_metadata_statedump(struct lttng_session *session,
1822 struct lttng_event *event)
1823 {
1824 const struct lttng_event_desc *desc = event->desc;
1825 int ret = 0;
1826 int i;
1827
1828 for (i = 0; i < desc->nr_fields; i++) {
1829 const struct lttng_event_field *field = &desc->fields[i];
1830
1831 ret = _lttng_field_statedump(session, field);
1832 if (ret)
1833 return ret;
1834 }
1835 return ret;
1836 }
1837
1838 /*
1839 * Must be called with sessions_mutex held.
1840 */
1841 static
1842 int _lttng_event_metadata_statedump(struct lttng_session *session,
1843 struct lttng_channel *chan,
1844 struct lttng_event *event)
1845 {
1846 int ret = 0;
1847
1848 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
1849 return 0;
1850 if (chan->channel_type == METADATA_CHANNEL)
1851 return 0;
1852
1853 ret = lttng_metadata_printf(session,
1854 "event {\n"
1855 " name = \"%s\";\n"
1856 " id = %u;\n"
1857 " stream_id = %u;\n",
1858 event->desc->name,
1859 event->id,
1860 event->chan->id);
1861 if (ret)
1862 goto end;
1863
1864 if (event->ctx) {
1865 ret = lttng_metadata_printf(session,
1866 " context := struct {\n");
1867 if (ret)
1868 goto end;
1869 }
1870 ret = _lttng_context_metadata_statedump(session, event->ctx);
1871 if (ret)
1872 goto end;
1873 if (event->ctx) {
1874 ret = lttng_metadata_printf(session,
1875 " };\n");
1876 if (ret)
1877 goto end;
1878 }
1879
1880 ret = lttng_metadata_printf(session,
1881 " fields := struct {\n"
1882 );
1883 if (ret)
1884 goto end;
1885
1886 ret = _lttng_fields_metadata_statedump(session, event);
1887 if (ret)
1888 goto end;
1889
1890 /*
1891 * LTTng space reservation can only reserve multiples of the
1892 * byte size.
1893 */
1894 ret = lttng_metadata_printf(session,
1895 " };\n"
1896 "};\n\n");
1897 if (ret)
1898 goto end;
1899
1900 event->metadata_dumped = 1;
1901 end:
1902 return ret;
1903
1904 }
1905
1906 /*
1907 * Must be called with sessions_mutex held.
1908 */
1909 static
1910 int _lttng_channel_metadata_statedump(struct lttng_session *session,
1911 struct lttng_channel *chan)
1912 {
1913 int ret = 0;
1914
1915 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
1916 return 0;
1917
1918 if (chan->channel_type == METADATA_CHANNEL)
1919 return 0;
1920
1921 WARN_ON_ONCE(!chan->header_type);
1922 ret = lttng_metadata_printf(session,
1923 "stream {\n"
1924 " id = %u;\n"
1925 " event.header := %s;\n"
1926 " packet.context := struct packet_context;\n",
1927 chan->id,
1928 chan->header_type == 1 ? "struct event_header_compact" :
1929 "struct event_header_large");
1930 if (ret)
1931 goto end;
1932
1933 if (chan->ctx) {
1934 ret = lttng_metadata_printf(session,
1935 " event.context := struct {\n");
1936 if (ret)
1937 goto end;
1938 }
1939 ret = _lttng_context_metadata_statedump(session, chan->ctx);
1940 if (ret)
1941 goto end;
1942 if (chan->ctx) {
1943 ret = lttng_metadata_printf(session,
1944 " };\n");
1945 if (ret)
1946 goto end;
1947 }
1948
1949 ret = lttng_metadata_printf(session,
1950 "};\n\n");
1951
1952 chan->metadata_dumped = 1;
1953 end:
1954 return ret;
1955 }
1956
1957 /*
1958 * Must be called with sessions_mutex held.
1959 */
1960 static
1961 int _lttng_stream_packet_context_declare(struct lttng_session *session)
1962 {
1963 return lttng_metadata_printf(session,
1964 "struct packet_context {\n"
1965 " uint64_clock_monotonic_t timestamp_begin;\n"
1966 " uint64_clock_monotonic_t timestamp_end;\n"
1967 " uint64_t content_size;\n"
1968 " uint64_t packet_size;\n"
1969 " uint64_t packet_seq_num;\n"
1970 " unsigned long events_discarded;\n"
1971 " uint32_t cpu_id;\n"
1972 "};\n\n"
1973 );
1974 }
1975
1976 /*
1977 * Compact header:
1978 * id: range: 0 - 30.
1979 * id 31 is reserved to indicate an extended header.
1980 *
1981 * Large header:
1982 * id: range: 0 - 65534.
1983 * id 65535 is reserved to indicate an extended header.
1984 *
1985 * Must be called with sessions_mutex held.
1986 */
1987 static
1988 int _lttng_event_header_declare(struct lttng_session *session)
1989 {
1990 return lttng_metadata_printf(session,
1991 "struct event_header_compact {\n"
1992 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1993 " variant <id> {\n"
1994 " struct {\n"
1995 " uint27_clock_monotonic_t timestamp;\n"
1996 " } compact;\n"
1997 " struct {\n"
1998 " uint32_t id;\n"
1999 " uint64_clock_monotonic_t timestamp;\n"
2000 " } extended;\n"
2001 " } v;\n"
2002 "} align(%u);\n"
2003 "\n"
2004 "struct event_header_large {\n"
2005 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2006 " variant <id> {\n"
2007 " struct {\n"
2008 " uint32_clock_monotonic_t timestamp;\n"
2009 " } compact;\n"
2010 " struct {\n"
2011 " uint32_t id;\n"
2012 " uint64_clock_monotonic_t timestamp;\n"
2013 " } extended;\n"
2014 " } v;\n"
2015 "} align(%u);\n\n",
2016 lttng_alignof(uint32_t) * CHAR_BIT,
2017 lttng_alignof(uint16_t) * CHAR_BIT
2018 );
2019 }
2020
2021 /*
2022 * Approximation of NTP time of day to clock monotonic correlation,
2023 * taken at start of trace.
2024 * Yes, this is only an approximation. Yes, we can (and will) do better
2025 * in future versions.
2026 * Return 0 if offset is negative. It may happen if the system sets
2027 * the REALTIME clock to 0 after boot.
2028 */
2029 static
2030 uint64_t measure_clock_offset(void)
2031 {
2032 uint64_t monotonic_avg, monotonic[2], realtime;
2033 uint64_t tcf = trace_clock_freq();
2034 int64_t offset;
2035 struct timespec rts = { 0, 0 };
2036 unsigned long flags;
2037
2038 /* Disable interrupts to increase correlation precision. */
2039 local_irq_save(flags);
2040 monotonic[0] = trace_clock_read64();
2041 getnstimeofday(&rts);
2042 monotonic[1] = trace_clock_read64();
2043 local_irq_restore(flags);
2044
2045 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2046 realtime = (uint64_t) rts.tv_sec * tcf;
2047 if (tcf == NSEC_PER_SEC) {
2048 realtime += rts.tv_nsec;
2049 } else {
2050 uint64_t n = rts.tv_nsec * tcf;
2051
2052 do_div(n, NSEC_PER_SEC);
2053 realtime += n;
2054 }
2055 offset = (int64_t) realtime - monotonic_avg;
2056 if (offset < 0)
2057 return 0;
2058 return offset;
2059 }
2060
2061 /*
2062 * Output metadata into this session's metadata buffers.
2063 * Must be called with sessions_mutex held.
2064 */
2065 static
2066 int _lttng_session_metadata_statedump(struct lttng_session *session)
2067 {
2068 unsigned char *uuid_c = session->uuid.b;
2069 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
2070 struct lttng_channel *chan;
2071 struct lttng_event *event;
2072 int ret = 0;
2073
2074 if (!ACCESS_ONCE(session->active))
2075 return 0;
2076 if (session->metadata_dumped)
2077 goto skip_session;
2078
2079 snprintf(uuid_s, sizeof(uuid_s),
2080 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2081 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2082 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2083 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2084 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
2085
2086 ret = lttng_metadata_printf(session,
2087 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2088 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2089 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2090 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2091 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2092 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2093 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2094 "\n"
2095 "trace {\n"
2096 " major = %u;\n"
2097 " minor = %u;\n"
2098 " uuid = \"%s\";\n"
2099 " byte_order = %s;\n"
2100 " packet.header := struct {\n"
2101 " uint32_t magic;\n"
2102 " uint8_t uuid[16];\n"
2103 " uint32_t stream_id;\n"
2104 " uint64_t stream_instance_id;\n"
2105 " };\n"
2106 "};\n\n",
2107 lttng_alignof(uint8_t) * CHAR_BIT,
2108 lttng_alignof(uint16_t) * CHAR_BIT,
2109 lttng_alignof(uint32_t) * CHAR_BIT,
2110 lttng_alignof(uint64_t) * CHAR_BIT,
2111 sizeof(unsigned long) * CHAR_BIT,
2112 lttng_alignof(unsigned long) * CHAR_BIT,
2113 CTF_SPEC_MAJOR,
2114 CTF_SPEC_MINOR,
2115 uuid_s,
2116 #ifdef __BIG_ENDIAN
2117 "be"
2118 #else
2119 "le"
2120 #endif
2121 );
2122 if (ret)
2123 goto end;
2124
2125 ret = lttng_metadata_printf(session,
2126 "env {\n"
2127 " hostname = \"%s\";\n"
2128 " domain = \"kernel\";\n"
2129 " sysname = \"%s\";\n"
2130 " kernel_release = \"%s\";\n"
2131 " kernel_version = \"%s\";\n"
2132 " tracer_name = \"lttng-modules\";\n"
2133 " tracer_major = %d;\n"
2134 " tracer_minor = %d;\n"
2135 " tracer_patchlevel = %d;\n"
2136 "};\n\n",
2137 current->nsproxy->uts_ns->name.nodename,
2138 utsname()->sysname,
2139 utsname()->release,
2140 utsname()->version,
2141 LTTNG_MODULES_MAJOR_VERSION,
2142 LTTNG_MODULES_MINOR_VERSION,
2143 LTTNG_MODULES_PATCHLEVEL_VERSION
2144 );
2145 if (ret)
2146 goto end;
2147
2148 ret = lttng_metadata_printf(session,
2149 "clock {\n"
2150 " name = \"%s\";\n",
2151 trace_clock_name()
2152 );
2153 if (ret)
2154 goto end;
2155
2156 if (!trace_clock_uuid(clock_uuid_s)) {
2157 ret = lttng_metadata_printf(session,
2158 " uuid = \"%s\";\n",
2159 clock_uuid_s
2160 );
2161 if (ret)
2162 goto end;
2163 }
2164
2165 ret = lttng_metadata_printf(session,
2166 " description = \"%s\";\n"
2167 " freq = %llu; /* Frequency, in Hz */\n"
2168 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2169 " offset = %llu;\n"
2170 "};\n\n",
2171 trace_clock_description(),
2172 (unsigned long long) trace_clock_freq(),
2173 (unsigned long long) measure_clock_offset()
2174 );
2175 if (ret)
2176 goto end;
2177
2178 ret = lttng_metadata_printf(session,
2179 "typealias integer {\n"
2180 " size = 27; align = 1; signed = false;\n"
2181 " map = clock.%s.value;\n"
2182 "} := uint27_clock_monotonic_t;\n"
2183 "\n"
2184 "typealias integer {\n"
2185 " size = 32; align = %u; signed = false;\n"
2186 " map = clock.%s.value;\n"
2187 "} := uint32_clock_monotonic_t;\n"
2188 "\n"
2189 "typealias integer {\n"
2190 " size = 64; align = %u; signed = false;\n"
2191 " map = clock.%s.value;\n"
2192 "} := uint64_clock_monotonic_t;\n\n",
2193 trace_clock_name(),
2194 lttng_alignof(uint32_t) * CHAR_BIT,
2195 trace_clock_name(),
2196 lttng_alignof(uint64_t) * CHAR_BIT,
2197 trace_clock_name()
2198 );
2199 if (ret)
2200 goto end;
2201
2202 ret = _lttng_stream_packet_context_declare(session);
2203 if (ret)
2204 goto end;
2205
2206 ret = _lttng_event_header_declare(session);
2207 if (ret)
2208 goto end;
2209
2210 skip_session:
2211 list_for_each_entry(chan, &session->chan, list) {
2212 ret = _lttng_channel_metadata_statedump(session, chan);
2213 if (ret)
2214 goto end;
2215 }
2216
2217 list_for_each_entry(event, &session->events, list) {
2218 ret = _lttng_event_metadata_statedump(session, event->chan, event);
2219 if (ret)
2220 goto end;
2221 }
2222 session->metadata_dumped = 1;
2223 end:
2224 return ret;
2225 }
2226
2227 /**
2228 * lttng_transport_register - LTT transport registration
2229 * @transport: transport structure
2230 *
2231 * Registers a transport which can be used as output to extract the data out of
2232 * LTTng. The module calling this registration function must ensure that no
2233 * trap-inducing code will be executed by the transport functions. E.g.
2234 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2235 * is made visible to the transport function. This registration acts as a
2236 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2237 * after its registration must it synchronize the TLBs.
2238 */
2239 void lttng_transport_register(struct lttng_transport *transport)
2240 {
2241 /*
2242 * Make sure no page fault can be triggered by the module about to be
2243 * registered. We deal with this here so we don't have to call
2244 * vmalloc_sync_all() in each module's init.
2245 */
2246 wrapper_vmalloc_sync_all();
2247
2248 mutex_lock(&sessions_mutex);
2249 list_add_tail(&transport->node, &lttng_transport_list);
2250 mutex_unlock(&sessions_mutex);
2251 }
2252 EXPORT_SYMBOL_GPL(lttng_transport_register);
2253
2254 /**
2255 * lttng_transport_unregister - LTT transport unregistration
2256 * @transport: transport structure
2257 */
2258 void lttng_transport_unregister(struct lttng_transport *transport)
2259 {
2260 mutex_lock(&sessions_mutex);
2261 list_del(&transport->node);
2262 mutex_unlock(&sessions_mutex);
2263 }
2264 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
2265
2266 static int __init lttng_events_init(void)
2267 {
2268 int ret;
2269
2270 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2271 if (ret)
2272 return ret;
2273 ret = wrapper_get_pfnblock_flags_mask_init();
2274 if (ret)
2275 return ret;
2276 ret = lttng_context_init();
2277 if (ret)
2278 return ret;
2279 ret = lttng_tracepoint_init();
2280 if (ret)
2281 goto error_tp;
2282 event_cache = KMEM_CACHE(lttng_event, 0);
2283 if (!event_cache) {
2284 ret = -ENOMEM;
2285 goto error_kmem;
2286 }
2287 ret = lttng_abi_init();
2288 if (ret)
2289 goto error_abi;
2290 ret = lttng_logger_init();
2291 if (ret)
2292 goto error_logger;
2293 return 0;
2294
2295 error_logger:
2296 lttng_abi_exit();
2297 error_abi:
2298 kmem_cache_destroy(event_cache);
2299 error_kmem:
2300 lttng_tracepoint_exit();
2301 error_tp:
2302 lttng_context_exit();
2303 return ret;
2304 }
2305
2306 module_init(lttng_events_init);
2307
2308 static void __exit lttng_events_exit(void)
2309 {
2310 struct lttng_session *session, *tmpsession;
2311
2312 lttng_logger_exit();
2313 lttng_abi_exit();
2314 list_for_each_entry_safe(session, tmpsession, &sessions, list)
2315 lttng_session_destroy(session);
2316 kmem_cache_destroy(event_cache);
2317 lttng_tracepoint_exit();
2318 lttng_context_exit();
2319 }
2320
2321 module_exit(lttng_events_exit);
2322
2323 MODULE_LICENSE("GPL and additional rights");
2324 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2325 MODULE_DESCRIPTION("LTTng Events");
2326 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
2327 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
2328 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
2329 LTTNG_MODULES_EXTRAVERSION);
This page took 0.10268 seconds and 5 git commands to generate.