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