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