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