Fix: double-unlock of session mutex
[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 return event;
647
648 statedump_error:
649 /* If a statedump error occurs, events will not be readable. */
650 register_error:
651 kmem_cache_free(event_cache, event);
652 cache_error:
653 exist:
654 type_error:
655 full:
656 return ERR_PTR(ret);
657 }
658
659 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
660 struct lttng_kernel_event *event_param,
661 void *filter,
662 const struct lttng_event_desc *event_desc,
663 enum lttng_kernel_instrumentation itype)
664 {
665 struct lttng_event *event;
666
667 mutex_lock(&sessions_mutex);
668 event = _lttng_event_create(chan, event_param, filter, event_desc,
669 itype);
670 mutex_unlock(&sessions_mutex);
671 return event;
672 }
673
674 /* Only used for tracepoints for now. */
675 static
676 void register_event(struct lttng_event *event)
677 {
678 const struct lttng_event_desc *desc;
679 int ret = -EINVAL;
680
681 if (event->registered)
682 return;
683
684 desc = event->desc;
685 switch (event->instrumentation) {
686 case LTTNG_KERNEL_TRACEPOINT:
687 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
688 desc->probe_callback,
689 event);
690 break;
691 case LTTNG_KERNEL_SYSCALL:
692 ret = lttng_syscall_filter_enable(event->chan,
693 desc->name);
694 break;
695 case LTTNG_KERNEL_KPROBE:
696 case LTTNG_KERNEL_KRETPROBE:
697 case LTTNG_KERNEL_FUNCTION:
698 case LTTNG_KERNEL_NOOP:
699 ret = 0;
700 break;
701 default:
702 WARN_ON_ONCE(1);
703 }
704 if (!ret)
705 event->registered = 1;
706 }
707
708 /*
709 * Only used internally at session destruction.
710 */
711 int _lttng_event_unregister(struct lttng_event *event)
712 {
713 const struct lttng_event_desc *desc;
714 int ret = -EINVAL;
715
716 if (!event->registered)
717 return 0;
718
719 desc = event->desc;
720 switch (event->instrumentation) {
721 case LTTNG_KERNEL_TRACEPOINT:
722 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
723 event->desc->probe_callback,
724 event);
725 break;
726 case LTTNG_KERNEL_KPROBE:
727 lttng_kprobes_unregister(event);
728 ret = 0;
729 break;
730 case LTTNG_KERNEL_KRETPROBE:
731 lttng_kretprobes_unregister(event);
732 ret = 0;
733 break;
734 case LTTNG_KERNEL_FUNCTION:
735 lttng_ftrace_unregister(event);
736 ret = 0;
737 break;
738 case LTTNG_KERNEL_SYSCALL:
739 ret = lttng_syscall_filter_disable(event->chan,
740 desc->name);
741 break;
742 case LTTNG_KERNEL_NOOP:
743 ret = 0;
744 break;
745 default:
746 WARN_ON_ONCE(1);
747 }
748 if (!ret)
749 event->registered = 0;
750 return ret;
751 }
752
753 /*
754 * Only used internally at session destruction.
755 */
756 static
757 void _lttng_event_destroy(struct lttng_event *event)
758 {
759 switch (event->instrumentation) {
760 case LTTNG_KERNEL_TRACEPOINT:
761 lttng_event_put(event->desc);
762 break;
763 case LTTNG_KERNEL_KPROBE:
764 module_put(event->desc->owner);
765 lttng_kprobes_destroy_private(event);
766 break;
767 case LTTNG_KERNEL_KRETPROBE:
768 module_put(event->desc->owner);
769 lttng_kretprobes_destroy_private(event);
770 break;
771 case LTTNG_KERNEL_FUNCTION:
772 module_put(event->desc->owner);
773 lttng_ftrace_destroy_private(event);
774 break;
775 case LTTNG_KERNEL_NOOP:
776 case LTTNG_KERNEL_SYSCALL:
777 break;
778 default:
779 WARN_ON_ONCE(1);
780 }
781 list_del(&event->list);
782 lttng_destroy_context(event->ctx);
783 kmem_cache_free(event_cache, event);
784 }
785
786 int lttng_session_track_pid(struct lttng_session *session, int pid)
787 {
788 int ret;
789
790 if (pid < -1)
791 return -EINVAL;
792 mutex_lock(&sessions_mutex);
793 if (pid == -1) {
794 /* track all pids: destroy tracker. */
795 if (session->pid_tracker) {
796 struct lttng_pid_tracker *lpf;
797
798 lpf = session->pid_tracker;
799 rcu_assign_pointer(session->pid_tracker, NULL);
800 synchronize_trace();
801 lttng_pid_tracker_destroy(lpf);
802 }
803 ret = 0;
804 } else {
805 if (!session->pid_tracker) {
806 struct lttng_pid_tracker *lpf;
807
808 lpf = lttng_pid_tracker_create();
809 if (!lpf) {
810 ret = -ENOMEM;
811 goto unlock;
812 }
813 ret = lttng_pid_tracker_add(lpf, pid);
814 rcu_assign_pointer(session->pid_tracker, lpf);
815 } else {
816 ret = lttng_pid_tracker_add(session->pid_tracker, pid);
817 }
818 }
819 unlock:
820 mutex_unlock(&sessions_mutex);
821 return ret;
822 }
823
824 int lttng_session_untrack_pid(struct lttng_session *session, int pid)
825 {
826 int ret;
827
828 if (pid < -1)
829 return -EINVAL;
830 mutex_lock(&sessions_mutex);
831 if (pid == -1) {
832 /* untrack all pids: replace by empty tracker. */
833 struct lttng_pid_tracker *old_lpf = session->pid_tracker;
834 struct lttng_pid_tracker *lpf;
835
836 lpf = lttng_pid_tracker_create();
837 if (!lpf) {
838 ret = -ENOMEM;
839 goto unlock;
840 }
841 rcu_assign_pointer(session->pid_tracker, lpf);
842 synchronize_trace();
843 if (old_lpf)
844 lttng_pid_tracker_destroy(old_lpf);
845 ret = 0;
846 } else {
847 if (!session->pid_tracker) {
848 ret = -ENOENT;
849 goto unlock;
850 }
851 ret = lttng_pid_tracker_del(session->pid_tracker, pid);
852 }
853 unlock:
854 mutex_unlock(&sessions_mutex);
855 return ret;
856 }
857
858 static
859 void *pid_list_start(struct seq_file *m, loff_t *pos)
860 {
861 struct lttng_session *session = m->private;
862 struct lttng_pid_tracker *lpf;
863 struct lttng_pid_hash_node *e;
864 int iter = 0, i;
865
866 mutex_lock(&sessions_mutex);
867 lpf = session->pid_tracker;
868 if (lpf) {
869 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
870 struct hlist_head *head = &lpf->pid_hash[i];
871
872 lttng_hlist_for_each_entry(e, head, hlist) {
873 if (iter++ >= *pos)
874 return e;
875 }
876 }
877 } else {
878 /* PID tracker disabled. */
879 if (iter >= *pos && iter == 0) {
880 return session; /* empty tracker */
881 }
882 iter++;
883 }
884 /* End of list */
885 return NULL;
886 }
887
888 /* Called with sessions_mutex held. */
889 static
890 void *pid_list_next(struct seq_file *m, void *p, loff_t *ppos)
891 {
892 struct lttng_session *session = m->private;
893 struct lttng_pid_tracker *lpf;
894 struct lttng_pid_hash_node *e;
895 int iter = 0, i;
896
897 (*ppos)++;
898 lpf = session->pid_tracker;
899 if (lpf) {
900 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
901 struct hlist_head *head = &lpf->pid_hash[i];
902
903 lttng_hlist_for_each_entry(e, head, hlist) {
904 if (iter++ >= *ppos)
905 return e;
906 }
907 }
908 } else {
909 /* PID tracker disabled. */
910 if (iter >= *ppos && iter == 0)
911 return session; /* empty tracker */
912 iter++;
913 }
914
915 /* End of list */
916 return NULL;
917 }
918
919 static
920 void pid_list_stop(struct seq_file *m, void *p)
921 {
922 mutex_unlock(&sessions_mutex);
923 }
924
925 static
926 int pid_list_show(struct seq_file *m, void *p)
927 {
928 int pid;
929
930 if (p == m->private) {
931 /* Tracker disabled. */
932 pid = -1;
933 } else {
934 const struct lttng_pid_hash_node *e = p;
935
936 pid = lttng_pid_tracker_get_node_pid(e);
937 }
938 seq_printf(m, "process { pid = %d; };\n", pid);
939 return 0;
940 }
941
942 static
943 const struct seq_operations lttng_tracker_pids_list_seq_ops = {
944 .start = pid_list_start,
945 .next = pid_list_next,
946 .stop = pid_list_stop,
947 .show = pid_list_show,
948 };
949
950 static
951 int lttng_tracker_pids_list_open(struct inode *inode, struct file *file)
952 {
953 return seq_open(file, &lttng_tracker_pids_list_seq_ops);
954 }
955
956 static
957 int lttng_tracker_pids_list_release(struct inode *inode, struct file *file)
958 {
959 struct seq_file *m = file->private_data;
960 struct lttng_session *session = m->private;
961 int ret;
962
963 WARN_ON_ONCE(!session);
964 ret = seq_release(inode, file);
965 if (!ret && session)
966 fput(session->file);
967 return ret;
968 }
969
970 const struct file_operations lttng_tracker_pids_list_fops = {
971 .owner = THIS_MODULE,
972 .open = lttng_tracker_pids_list_open,
973 .read = seq_read,
974 .llseek = seq_lseek,
975 .release = lttng_tracker_pids_list_release,
976 };
977
978 int lttng_session_list_tracker_pids(struct lttng_session *session)
979 {
980 struct file *tracker_pids_list_file;
981 struct seq_file *m;
982 int file_fd, ret;
983
984 file_fd = lttng_get_unused_fd();
985 if (file_fd < 0) {
986 ret = file_fd;
987 goto fd_error;
988 }
989
990 tracker_pids_list_file = anon_inode_getfile("[lttng_tracker_pids_list]",
991 &lttng_tracker_pids_list_fops,
992 NULL, O_RDWR);
993 if (IS_ERR(tracker_pids_list_file)) {
994 ret = PTR_ERR(tracker_pids_list_file);
995 goto file_error;
996 }
997 ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file);
998 if (ret < 0)
999 goto open_error;
1000 m = tracker_pids_list_file->private_data;
1001 m->private = session;
1002 fd_install(file_fd, tracker_pids_list_file);
1003 atomic_long_inc(&session->file->f_count);
1004
1005 return file_fd;
1006
1007 open_error:
1008 fput(tracker_pids_list_file);
1009 file_error:
1010 put_unused_fd(file_fd);
1011 fd_error:
1012 return ret;
1013 }
1014
1015 /*
1016 * Enabler management.
1017 */
1018 static
1019 int lttng_match_enabler_wildcard(const char *desc_name,
1020 const char *name)
1021 {
1022 /* Compare excluding final '*' */
1023 if (strncmp(desc_name, name, strlen(name) - 1))
1024 return 0;
1025 return 1;
1026 }
1027
1028 static
1029 int lttng_match_enabler_name(const char *desc_name,
1030 const char *name)
1031 {
1032 if (strcmp(desc_name, name))
1033 return 0;
1034 return 1;
1035 }
1036
1037 static
1038 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1039 struct lttng_enabler *enabler)
1040 {
1041 const char *desc_name, *enabler_name;
1042
1043 enabler_name = enabler->event_param.name;
1044 switch (enabler->event_param.instrumentation) {
1045 case LTTNG_KERNEL_TRACEPOINT:
1046 desc_name = desc->name;
1047 break;
1048 case LTTNG_KERNEL_SYSCALL:
1049 desc_name = desc->name;
1050 if (!strncmp(desc_name, "compat_", strlen("compat_")))
1051 desc_name += strlen("compat_");
1052 if (!strncmp(desc_name, "syscall_exit_",
1053 strlen("syscall_exit_"))) {
1054 desc_name += strlen("syscall_exit_");
1055 } else if (!strncmp(desc_name, "syscall_entry_",
1056 strlen("syscall_entry_"))) {
1057 desc_name += strlen("syscall_entry_");
1058 } else {
1059 WARN_ON_ONCE(1);
1060 return -EINVAL;
1061 }
1062 break;
1063 default:
1064 WARN_ON_ONCE(1);
1065 return -EINVAL;
1066 }
1067 switch (enabler->type) {
1068 case LTTNG_ENABLER_WILDCARD:
1069 return lttng_match_enabler_wildcard(desc_name, enabler_name);
1070 case LTTNG_ENABLER_NAME:
1071 return lttng_match_enabler_name(desc_name, enabler_name);
1072 default:
1073 return -EINVAL;
1074 }
1075 }
1076
1077 static
1078 int lttng_event_match_enabler(struct lttng_event *event,
1079 struct lttng_enabler *enabler)
1080 {
1081 if (enabler->event_param.instrumentation != event->instrumentation)
1082 return 0;
1083 if (lttng_desc_match_enabler(event->desc, enabler)
1084 && event->chan == enabler->chan)
1085 return 1;
1086 else
1087 return 0;
1088 }
1089
1090 static
1091 struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event,
1092 struct lttng_enabler *enabler)
1093 {
1094 struct lttng_enabler_ref *enabler_ref;
1095
1096 list_for_each_entry(enabler_ref,
1097 &event->enablers_ref_head, node) {
1098 if (enabler_ref->ref == enabler)
1099 return enabler_ref;
1100 }
1101 return NULL;
1102 }
1103
1104 static
1105 void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
1106 {
1107 struct lttng_session *session = enabler->chan->session;
1108 struct lttng_probe_desc *probe_desc;
1109 const struct lttng_event_desc *desc;
1110 int i;
1111 struct list_head *probe_list;
1112
1113 probe_list = lttng_get_probe_list_head();
1114 /*
1115 * For each probe event, if we find that a probe event matches
1116 * our enabler, create an associated lttng_event if not
1117 * already present.
1118 */
1119 list_for_each_entry(probe_desc, probe_list, head) {
1120 for (i = 0; i < probe_desc->nr_events; i++) {
1121 int found = 0;
1122 struct hlist_head *head;
1123 const char *event_name;
1124 size_t name_len;
1125 uint32_t hash;
1126 struct lttng_event *event;
1127
1128 desc = probe_desc->event_desc[i];
1129 if (!lttng_desc_match_enabler(desc, enabler))
1130 continue;
1131 event_name = desc->name;
1132 name_len = strlen(event_name);
1133
1134 /*
1135 * Check if already created.
1136 */
1137 hash = jhash(event_name, name_len, 0);
1138 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
1139 lttng_hlist_for_each_entry(event, head, hlist) {
1140 if (event->desc == desc
1141 && event->chan == enabler->chan)
1142 found = 1;
1143 }
1144 if (found)
1145 continue;
1146
1147 /*
1148 * We need to create an event for this
1149 * event probe.
1150 */
1151 event = _lttng_event_create(enabler->chan,
1152 NULL, NULL, desc,
1153 LTTNG_KERNEL_TRACEPOINT);
1154 if (!event) {
1155 printk(KERN_INFO "Unable to create event %s\n",
1156 probe_desc->event_desc[i]->name);
1157 }
1158 }
1159 }
1160 }
1161
1162 static
1163 void lttng_create_syscall_if_missing(struct lttng_enabler *enabler)
1164 {
1165 int ret;
1166
1167 ret = lttng_syscalls_register(enabler->chan, NULL);
1168 WARN_ON_ONCE(ret);
1169 }
1170
1171 /*
1172 * Create struct lttng_event if it is missing and present in the list of
1173 * tracepoint probes.
1174 * Should be called with sessions mutex held.
1175 */
1176 static
1177 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
1178 {
1179 switch (enabler->event_param.instrumentation) {
1180 case LTTNG_KERNEL_TRACEPOINT:
1181 lttng_create_tracepoint_if_missing(enabler);
1182 break;
1183 case LTTNG_KERNEL_SYSCALL:
1184 lttng_create_syscall_if_missing(enabler);
1185 break;
1186 default:
1187 WARN_ON_ONCE(1);
1188 break;
1189 }
1190 }
1191
1192 /*
1193 * Create events associated with an enabler (if not already present),
1194 * and add backward reference from the event to the enabler.
1195 * Should be called with sessions mutex held.
1196 */
1197 static
1198 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
1199 {
1200 struct lttng_session *session = enabler->chan->session;
1201 struct lttng_event *event;
1202
1203 /* First ensure that probe events are created for this enabler. */
1204 lttng_create_event_if_missing(enabler);
1205
1206 /* For each event matching enabler in session event list. */
1207 list_for_each_entry(event, &session->events, list) {
1208 struct lttng_enabler_ref *enabler_ref;
1209
1210 if (!lttng_event_match_enabler(event, enabler))
1211 continue;
1212 enabler_ref = lttng_event_enabler_ref(event, enabler);
1213 if (!enabler_ref) {
1214 /*
1215 * If no backward ref, create it.
1216 * Add backward ref from event to enabler.
1217 */
1218 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1219 if (!enabler_ref)
1220 return -ENOMEM;
1221 enabler_ref->ref = enabler;
1222 list_add(&enabler_ref->node,
1223 &event->enablers_ref_head);
1224 }
1225
1226 /*
1227 * Link filter bytecodes if not linked yet.
1228 */
1229 lttng_enabler_event_link_bytecode(event, enabler);
1230
1231 /* TODO: merge event context. */
1232 }
1233 return 0;
1234 }
1235
1236 /*
1237 * Called at module load: connect the probe on all enablers matching
1238 * this event.
1239 * Called with sessions lock held.
1240 */
1241 int lttng_fix_pending_events(void)
1242 {
1243 struct lttng_session *session;
1244
1245 list_for_each_entry(session, &sessions, list)
1246 lttng_session_lazy_sync_enablers(session);
1247 return 0;
1248 }
1249
1250 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1251 struct lttng_kernel_event *event_param,
1252 struct lttng_channel *chan)
1253 {
1254 struct lttng_enabler *enabler;
1255
1256 enabler = kzalloc(sizeof(*enabler), GFP_KERNEL);
1257 if (!enabler)
1258 return NULL;
1259 enabler->type = type;
1260 INIT_LIST_HEAD(&enabler->filter_bytecode_head);
1261 memcpy(&enabler->event_param, event_param,
1262 sizeof(enabler->event_param));
1263 enabler->chan = chan;
1264 /* ctx left NULL */
1265 enabler->enabled = 0;
1266 enabler->evtype = LTTNG_TYPE_ENABLER;
1267 mutex_lock(&sessions_mutex);
1268 list_add(&enabler->node, &enabler->chan->session->enablers_head);
1269 lttng_session_lazy_sync_enablers(enabler->chan->session);
1270 mutex_unlock(&sessions_mutex);
1271 return enabler;
1272 }
1273
1274 int lttng_enabler_enable(struct lttng_enabler *enabler)
1275 {
1276 mutex_lock(&sessions_mutex);
1277 enabler->enabled = 1;
1278 lttng_session_lazy_sync_enablers(enabler->chan->session);
1279 mutex_unlock(&sessions_mutex);
1280 return 0;
1281 }
1282
1283 int lttng_enabler_disable(struct lttng_enabler *enabler)
1284 {
1285 mutex_lock(&sessions_mutex);
1286 enabler->enabled = 0;
1287 lttng_session_lazy_sync_enablers(enabler->chan->session);
1288 mutex_unlock(&sessions_mutex);
1289 return 0;
1290 }
1291
1292 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1293 struct lttng_kernel_filter_bytecode __user *bytecode)
1294 {
1295 struct lttng_filter_bytecode_node *bytecode_node;
1296 uint32_t bytecode_len;
1297 int ret;
1298
1299 ret = get_user(bytecode_len, &bytecode->len);
1300 if (ret)
1301 return ret;
1302 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1303 GFP_KERNEL);
1304 if (!bytecode_node)
1305 return -ENOMEM;
1306 ret = copy_from_user(&bytecode_node->bc, bytecode,
1307 sizeof(*bytecode) + bytecode_len);
1308 if (ret)
1309 goto error_free;
1310 bytecode_node->enabler = enabler;
1311 /* Enforce length based on allocated size */
1312 bytecode_node->bc.len = bytecode_len;
1313 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1314 lttng_session_lazy_sync_enablers(enabler->chan->session);
1315 return 0;
1316
1317 error_free:
1318 kfree(bytecode_node);
1319 return ret;
1320 }
1321
1322 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1323 struct lttng_kernel_context *context_param)
1324 {
1325 return -ENOSYS;
1326 }
1327
1328 static
1329 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1330 {
1331 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1332
1333 /* Destroy filter bytecode */
1334 list_for_each_entry_safe(filter_node, tmp_filter_node,
1335 &enabler->filter_bytecode_head, node) {
1336 kfree(filter_node);
1337 }
1338
1339 /* Destroy contexts */
1340 lttng_destroy_context(enabler->ctx);
1341
1342 list_del(&enabler->node);
1343 kfree(enabler);
1344 }
1345
1346 /*
1347 * lttng_session_sync_enablers should be called just before starting a
1348 * session.
1349 * Should be called with sessions mutex held.
1350 */
1351 static
1352 void lttng_session_sync_enablers(struct lttng_session *session)
1353 {
1354 struct lttng_enabler *enabler;
1355 struct lttng_event *event;
1356
1357 list_for_each_entry(enabler, &session->enablers_head, node)
1358 lttng_enabler_ref_events(enabler);
1359 /*
1360 * For each event, if at least one of its enablers is enabled,
1361 * and its channel and session transient states are enabled, we
1362 * enable the event, else we disable it.
1363 */
1364 list_for_each_entry(event, &session->events, list) {
1365 struct lttng_enabler_ref *enabler_ref;
1366 struct lttng_bytecode_runtime *runtime;
1367 int enabled = 0, has_enablers_without_bytecode = 0;
1368
1369 switch (event->instrumentation) {
1370 case LTTNG_KERNEL_TRACEPOINT:
1371 case LTTNG_KERNEL_SYSCALL:
1372 /* Enable events */
1373 list_for_each_entry(enabler_ref,
1374 &event->enablers_ref_head, node) {
1375 if (enabler_ref->ref->enabled) {
1376 enabled = 1;
1377 break;
1378 }
1379 }
1380 break;
1381 default:
1382 /* Not handled with lazy sync. */
1383 continue;
1384 }
1385 /*
1386 * Enabled state is based on union of enablers, with
1387 * intesection of session and channel transient enable
1388 * states.
1389 */
1390 enabled = enabled && session->tstate && event->chan->tstate;
1391
1392 ACCESS_ONCE(event->enabled) = enabled;
1393 /*
1394 * Sync tracepoint registration with event enabled
1395 * state.
1396 */
1397 if (enabled) {
1398 register_event(event);
1399 } else {
1400 _lttng_event_unregister(event);
1401 }
1402
1403 /* Check if has enablers without bytecode enabled */
1404 list_for_each_entry(enabler_ref,
1405 &event->enablers_ref_head, node) {
1406 if (enabler_ref->ref->enabled
1407 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1408 has_enablers_without_bytecode = 1;
1409 break;
1410 }
1411 }
1412 event->has_enablers_without_bytecode =
1413 has_enablers_without_bytecode;
1414
1415 /* Enable filters */
1416 list_for_each_entry(runtime,
1417 &event->bytecode_runtime_head, node)
1418 lttng_filter_sync_state(runtime);
1419 }
1420 }
1421
1422 /*
1423 * Apply enablers to session events, adding events to session if need
1424 * be. It is required after each modification applied to an active
1425 * session, and right before session "start".
1426 * "lazy" sync means we only sync if required.
1427 * Should be called with sessions mutex held.
1428 */
1429 static
1430 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1431 {
1432 /* We can skip if session is not active */
1433 if (!session->active)
1434 return;
1435 lttng_session_sync_enablers(session);
1436 }
1437
1438 /*
1439 * Serialize at most one packet worth of metadata into a metadata
1440 * channel.
1441 * We have exclusive access to our metadata buffer (protected by the
1442 * sessions_mutex), so we can do racy operations such as looking for
1443 * remaining space left in packet and write, since mutual exclusion
1444 * protects us from concurrent writes.
1445 * Returns the number of bytes written in the channel, 0 if no data
1446 * was written and a negative value on error.
1447 */
1448 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1449 struct channel *chan)
1450 {
1451 struct lib_ring_buffer_ctx ctx;
1452 int ret = 0;
1453 size_t len, reserve_len;
1454
1455 /*
1456 * Ensure we support mutiple get_next / put sequences followed
1457 * by put_next. The metadata stream lock internally protects
1458 * reading the metadata cache. It can indeed be read
1459 * concurrently by "get_next_subbuf" and "flush" operations on
1460 * the buffer invoked by different processes.
1461 */
1462 mutex_lock(&stream->lock);
1463 WARN_ON(stream->metadata_in < stream->metadata_out);
1464 if (stream->metadata_in != stream->metadata_out)
1465 goto end;
1466
1467 len = stream->metadata_cache->metadata_written -
1468 stream->metadata_in;
1469 if (!len)
1470 goto end;
1471 reserve_len = min_t(size_t,
1472 stream->transport->ops.packet_avail_size(chan),
1473 len);
1474 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
1475 sizeof(char), -1);
1476 /*
1477 * If reservation failed, return an error to the caller.
1478 */
1479 ret = stream->transport->ops.event_reserve(&ctx, 0);
1480 if (ret != 0) {
1481 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1482 goto end;
1483 }
1484 stream->transport->ops.event_write(&ctx,
1485 stream->metadata_cache->data + stream->metadata_in,
1486 reserve_len);
1487 stream->transport->ops.event_commit(&ctx);
1488 stream->metadata_in += reserve_len;
1489 ret = reserve_len;
1490
1491 end:
1492 mutex_unlock(&stream->lock);
1493 return ret;
1494 }
1495
1496 /*
1497 * Write the metadata to the metadata cache.
1498 * Must be called with sessions_mutex held.
1499 */
1500 int lttng_metadata_printf(struct lttng_session *session,
1501 const char *fmt, ...)
1502 {
1503 char *str;
1504 size_t len;
1505 va_list ap;
1506 struct lttng_metadata_stream *stream;
1507
1508 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
1509
1510 va_start(ap, fmt);
1511 str = kvasprintf(GFP_KERNEL, fmt, ap);
1512 va_end(ap);
1513 if (!str)
1514 return -ENOMEM;
1515
1516 len = strlen(str);
1517 if (session->metadata_cache->metadata_written + len >
1518 session->metadata_cache->cache_alloc) {
1519 char *tmp_cache_realloc;
1520 unsigned int tmp_cache_alloc_size;
1521
1522 tmp_cache_alloc_size = max_t(unsigned int,
1523 session->metadata_cache->cache_alloc + len,
1524 session->metadata_cache->cache_alloc << 1);
1525 tmp_cache_realloc = krealloc(session->metadata_cache->data,
1526 tmp_cache_alloc_size, GFP_KERNEL);
1527 if (!tmp_cache_realloc)
1528 goto err;
1529 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1530 session->metadata_cache->data = tmp_cache_realloc;
1531 }
1532 memcpy(session->metadata_cache->data +
1533 session->metadata_cache->metadata_written,
1534 str, len);
1535 session->metadata_cache->metadata_written += len;
1536 kfree(str);
1537
1538 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1539 wake_up_interruptible(&stream->read_wait);
1540
1541 return 0;
1542
1543 err:
1544 kfree(str);
1545 return -ENOMEM;
1546 }
1547
1548 /*
1549 * Must be called with sessions_mutex held.
1550 */
1551 static
1552 int _lttng_field_statedump(struct lttng_session *session,
1553 const struct lttng_event_field *field)
1554 {
1555 int ret = 0;
1556
1557 switch (field->type.atype) {
1558 case atype_integer:
1559 ret = lttng_metadata_printf(session,
1560 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
1561 field->type.u.basic.integer.size,
1562 field->type.u.basic.integer.alignment,
1563 field->type.u.basic.integer.signedness,
1564 (field->type.u.basic.integer.encoding == lttng_encode_none)
1565 ? "none"
1566 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
1567 ? "UTF8"
1568 : "ASCII",
1569 field->type.u.basic.integer.base,
1570 #ifdef __BIG_ENDIAN
1571 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1572 #else
1573 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1574 #endif
1575 field->name);
1576 break;
1577 case atype_enum:
1578 ret = lttng_metadata_printf(session,
1579 " %s _%s;\n",
1580 field->type.u.basic.enumeration.name,
1581 field->name);
1582 break;
1583 case atype_array:
1584 {
1585 const struct lttng_basic_type *elem_type;
1586
1587 elem_type = &field->type.u.array.elem_type;
1588 ret = lttng_metadata_printf(session,
1589 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
1590 elem_type->u.basic.integer.size,
1591 elem_type->u.basic.integer.alignment,
1592 elem_type->u.basic.integer.signedness,
1593 (elem_type->u.basic.integer.encoding == lttng_encode_none)
1594 ? "none"
1595 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
1596 ? "UTF8"
1597 : "ASCII",
1598 elem_type->u.basic.integer.base,
1599 #ifdef __BIG_ENDIAN
1600 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1601 #else
1602 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1603 #endif
1604 field->name, field->type.u.array.length);
1605 break;
1606 }
1607 case atype_sequence:
1608 {
1609 const struct lttng_basic_type *elem_type;
1610 const struct lttng_basic_type *length_type;
1611
1612 elem_type = &field->type.u.sequence.elem_type;
1613 length_type = &field->type.u.sequence.length_type;
1614 ret = lttng_metadata_printf(session,
1615 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
1616 length_type->u.basic.integer.size,
1617 (unsigned int) length_type->u.basic.integer.alignment,
1618 length_type->u.basic.integer.signedness,
1619 (length_type->u.basic.integer.encoding == lttng_encode_none)
1620 ? "none"
1621 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
1622 ? "UTF8"
1623 : "ASCII"),
1624 length_type->u.basic.integer.base,
1625 #ifdef __BIG_ENDIAN
1626 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1627 #else
1628 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1629 #endif
1630 field->name);
1631 if (ret)
1632 return ret;
1633
1634 ret = lttng_metadata_printf(session,
1635 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
1636 elem_type->u.basic.integer.size,
1637 (unsigned int) elem_type->u.basic.integer.alignment,
1638 elem_type->u.basic.integer.signedness,
1639 (elem_type->u.basic.integer.encoding == lttng_encode_none)
1640 ? "none"
1641 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
1642 ? "UTF8"
1643 : "ASCII"),
1644 elem_type->u.basic.integer.base,
1645 #ifdef __BIG_ENDIAN
1646 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
1647 #else
1648 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
1649 #endif
1650 field->name,
1651 field->name);
1652 break;
1653 }
1654
1655 case atype_string:
1656 /* Default encoding is UTF8 */
1657 ret = lttng_metadata_printf(session,
1658 " string%s _%s;\n",
1659 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
1660 " { encoding = ASCII; }" : "",
1661 field->name);
1662 break;
1663 default:
1664 WARN_ON_ONCE(1);
1665 return -EINVAL;
1666 }
1667 return ret;
1668 }
1669
1670 static
1671 int _lttng_context_metadata_statedump(struct lttng_session *session,
1672 struct lttng_ctx *ctx)
1673 {
1674 int ret = 0;
1675 int i;
1676
1677 if (!ctx)
1678 return 0;
1679 for (i = 0; i < ctx->nr_fields; i++) {
1680 const struct lttng_ctx_field *field = &ctx->fields[i];
1681
1682 ret = _lttng_field_statedump(session, &field->event_field);
1683 if (ret)
1684 return ret;
1685 }
1686 return ret;
1687 }
1688
1689 static
1690 int _lttng_fields_metadata_statedump(struct lttng_session *session,
1691 struct lttng_event *event)
1692 {
1693 const struct lttng_event_desc *desc = event->desc;
1694 int ret = 0;
1695 int i;
1696
1697 for (i = 0; i < desc->nr_fields; i++) {
1698 const struct lttng_event_field *field = &desc->fields[i];
1699
1700 ret = _lttng_field_statedump(session, field);
1701 if (ret)
1702 return ret;
1703 }
1704 return ret;
1705 }
1706
1707 /*
1708 * Must be called with sessions_mutex held.
1709 */
1710 static
1711 int _lttng_event_metadata_statedump(struct lttng_session *session,
1712 struct lttng_channel *chan,
1713 struct lttng_event *event)
1714 {
1715 int ret = 0;
1716
1717 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
1718 return 0;
1719 if (chan->channel_type == METADATA_CHANNEL)
1720 return 0;
1721
1722 ret = lttng_metadata_printf(session,
1723 "event {\n"
1724 " name = \"%s\";\n"
1725 " id = %u;\n"
1726 " stream_id = %u;\n",
1727 event->desc->name,
1728 event->id,
1729 event->chan->id);
1730 if (ret)
1731 goto end;
1732
1733 if (event->ctx) {
1734 ret = lttng_metadata_printf(session,
1735 " context := struct {\n");
1736 if (ret)
1737 goto end;
1738 }
1739 ret = _lttng_context_metadata_statedump(session, event->ctx);
1740 if (ret)
1741 goto end;
1742 if (event->ctx) {
1743 ret = lttng_metadata_printf(session,
1744 " };\n");
1745 if (ret)
1746 goto end;
1747 }
1748
1749 ret = lttng_metadata_printf(session,
1750 " fields := struct {\n"
1751 );
1752 if (ret)
1753 goto end;
1754
1755 ret = _lttng_fields_metadata_statedump(session, event);
1756 if (ret)
1757 goto end;
1758
1759 /*
1760 * LTTng space reservation can only reserve multiples of the
1761 * byte size.
1762 */
1763 ret = lttng_metadata_printf(session,
1764 " };\n"
1765 "};\n\n");
1766 if (ret)
1767 goto end;
1768
1769 event->metadata_dumped = 1;
1770 end:
1771 return ret;
1772
1773 }
1774
1775 /*
1776 * Must be called with sessions_mutex held.
1777 */
1778 static
1779 int _lttng_channel_metadata_statedump(struct lttng_session *session,
1780 struct lttng_channel *chan)
1781 {
1782 int ret = 0;
1783
1784 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
1785 return 0;
1786
1787 if (chan->channel_type == METADATA_CHANNEL)
1788 return 0;
1789
1790 WARN_ON_ONCE(!chan->header_type);
1791 ret = lttng_metadata_printf(session,
1792 "stream {\n"
1793 " id = %u;\n"
1794 " event.header := %s;\n"
1795 " packet.context := struct packet_context;\n",
1796 chan->id,
1797 chan->header_type == 1 ? "struct event_header_compact" :
1798 "struct event_header_large");
1799 if (ret)
1800 goto end;
1801
1802 if (chan->ctx) {
1803 ret = lttng_metadata_printf(session,
1804 " event.context := struct {\n");
1805 if (ret)
1806 goto end;
1807 }
1808 ret = _lttng_context_metadata_statedump(session, chan->ctx);
1809 if (ret)
1810 goto end;
1811 if (chan->ctx) {
1812 ret = lttng_metadata_printf(session,
1813 " };\n");
1814 if (ret)
1815 goto end;
1816 }
1817
1818 ret = lttng_metadata_printf(session,
1819 "};\n\n");
1820
1821 chan->metadata_dumped = 1;
1822 end:
1823 return ret;
1824 }
1825
1826 /*
1827 * Must be called with sessions_mutex held.
1828 */
1829 static
1830 int _lttng_stream_packet_context_declare(struct lttng_session *session)
1831 {
1832 return lttng_metadata_printf(session,
1833 "struct packet_context {\n"
1834 " uint64_clock_monotonic_t timestamp_begin;\n"
1835 " uint64_clock_monotonic_t timestamp_end;\n"
1836 " uint64_t content_size;\n"
1837 " uint64_t packet_size;\n"
1838 " unsigned long events_discarded;\n"
1839 " uint32_t cpu_id;\n"
1840 "};\n\n"
1841 );
1842 }
1843
1844 /*
1845 * Compact header:
1846 * id: range: 0 - 30.
1847 * id 31 is reserved to indicate an extended header.
1848 *
1849 * Large header:
1850 * id: range: 0 - 65534.
1851 * id 65535 is reserved to indicate an extended header.
1852 *
1853 * Must be called with sessions_mutex held.
1854 */
1855 static
1856 int _lttng_event_header_declare(struct lttng_session *session)
1857 {
1858 return lttng_metadata_printf(session,
1859 "struct event_header_compact {\n"
1860 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1861 " variant <id> {\n"
1862 " struct {\n"
1863 " uint27_clock_monotonic_t timestamp;\n"
1864 " } compact;\n"
1865 " struct {\n"
1866 " uint32_t id;\n"
1867 " uint64_clock_monotonic_t timestamp;\n"
1868 " } extended;\n"
1869 " } v;\n"
1870 "} align(%u);\n"
1871 "\n"
1872 "struct event_header_large {\n"
1873 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1874 " variant <id> {\n"
1875 " struct {\n"
1876 " uint32_clock_monotonic_t timestamp;\n"
1877 " } compact;\n"
1878 " struct {\n"
1879 " uint32_t id;\n"
1880 " uint64_clock_monotonic_t timestamp;\n"
1881 " } extended;\n"
1882 " } v;\n"
1883 "} align(%u);\n\n",
1884 lttng_alignof(uint32_t) * CHAR_BIT,
1885 lttng_alignof(uint16_t) * CHAR_BIT
1886 );
1887 }
1888
1889 /*
1890 * Approximation of NTP time of day to clock monotonic correlation,
1891 * taken at start of trace.
1892 * Yes, this is only an approximation. Yes, we can (and will) do better
1893 * in future versions.
1894 */
1895 static
1896 uint64_t measure_clock_offset(void)
1897 {
1898 uint64_t offset, monotonic[2], realtime;
1899 struct timespec rts = { 0, 0 };
1900 unsigned long flags;
1901
1902 /* Disable interrupts to increase correlation precision. */
1903 local_irq_save(flags);
1904 monotonic[0] = trace_clock_read64();
1905 getnstimeofday(&rts);
1906 monotonic[1] = trace_clock_read64();
1907 local_irq_restore(flags);
1908
1909 offset = (monotonic[0] + monotonic[1]) >> 1;
1910 realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
1911 realtime += rts.tv_nsec;
1912 offset = realtime - offset;
1913 return offset;
1914 }
1915
1916 /*
1917 * Output metadata into this session's metadata buffers.
1918 * Must be called with sessions_mutex held.
1919 */
1920 static
1921 int _lttng_session_metadata_statedump(struct lttng_session *session)
1922 {
1923 unsigned char *uuid_c = session->uuid.b;
1924 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
1925 struct lttng_channel *chan;
1926 struct lttng_event *event;
1927 int ret = 0;
1928
1929 if (!ACCESS_ONCE(session->active))
1930 return 0;
1931 if (session->metadata_dumped)
1932 goto skip_session;
1933
1934 snprintf(uuid_s, sizeof(uuid_s),
1935 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1936 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1937 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1938 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1939 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1940
1941 ret = lttng_metadata_printf(session,
1942 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1943 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1944 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1945 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1946 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1947 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1948 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1949 "\n"
1950 "trace {\n"
1951 " major = %u;\n"
1952 " minor = %u;\n"
1953 " uuid = \"%s\";\n"
1954 " byte_order = %s;\n"
1955 " packet.header := struct {\n"
1956 " uint32_t magic;\n"
1957 " uint8_t uuid[16];\n"
1958 " uint32_t stream_id;\n"
1959 " };\n"
1960 "};\n\n",
1961 lttng_alignof(uint8_t) * CHAR_BIT,
1962 lttng_alignof(uint16_t) * CHAR_BIT,
1963 lttng_alignof(uint32_t) * CHAR_BIT,
1964 lttng_alignof(uint64_t) * CHAR_BIT,
1965 sizeof(unsigned long) * CHAR_BIT,
1966 lttng_alignof(unsigned long) * CHAR_BIT,
1967 CTF_SPEC_MAJOR,
1968 CTF_SPEC_MINOR,
1969 uuid_s,
1970 #ifdef __BIG_ENDIAN
1971 "be"
1972 #else
1973 "le"
1974 #endif
1975 );
1976 if (ret)
1977 goto end;
1978
1979 ret = lttng_metadata_printf(session,
1980 "env {\n"
1981 " hostname = \"%s\";\n"
1982 " domain = \"kernel\";\n"
1983 " sysname = \"%s\";\n"
1984 " kernel_release = \"%s\";\n"
1985 " kernel_version = \"%s\";\n"
1986 " tracer_name = \"lttng-modules\";\n"
1987 " tracer_major = %d;\n"
1988 " tracer_minor = %d;\n"
1989 " tracer_patchlevel = %d;\n"
1990 "};\n\n",
1991 current->nsproxy->uts_ns->name.nodename,
1992 utsname()->sysname,
1993 utsname()->release,
1994 utsname()->version,
1995 LTTNG_MODULES_MAJOR_VERSION,
1996 LTTNG_MODULES_MINOR_VERSION,
1997 LTTNG_MODULES_PATCHLEVEL_VERSION
1998 );
1999 if (ret)
2000 goto end;
2001
2002 ret = lttng_metadata_printf(session,
2003 "clock {\n"
2004 " name = %s;\n",
2005 "monotonic"
2006 );
2007 if (ret)
2008 goto end;
2009
2010 if (!trace_clock_uuid(clock_uuid_s)) {
2011 ret = lttng_metadata_printf(session,
2012 " uuid = \"%s\";\n",
2013 clock_uuid_s
2014 );
2015 if (ret)
2016 goto end;
2017 }
2018
2019 ret = lttng_metadata_printf(session,
2020 " description = \"Monotonic Clock\";\n"
2021 " freq = %llu; /* Frequency, in Hz */\n"
2022 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2023 " offset = %llu;\n"
2024 "};\n\n",
2025 (unsigned long long) trace_clock_freq(),
2026 (unsigned long long) measure_clock_offset()
2027 );
2028 if (ret)
2029 goto end;
2030
2031 ret = lttng_metadata_printf(session,
2032 "typealias integer {\n"
2033 " size = 27; align = 1; signed = false;\n"
2034 " map = clock.monotonic.value;\n"
2035 "} := uint27_clock_monotonic_t;\n"
2036 "\n"
2037 "typealias integer {\n"
2038 " size = 32; align = %u; signed = false;\n"
2039 " map = clock.monotonic.value;\n"
2040 "} := uint32_clock_monotonic_t;\n"
2041 "\n"
2042 "typealias integer {\n"
2043 " size = 64; align = %u; signed = false;\n"
2044 " map = clock.monotonic.value;\n"
2045 "} := uint64_clock_monotonic_t;\n\n",
2046 lttng_alignof(uint32_t) * CHAR_BIT,
2047 lttng_alignof(uint64_t) * CHAR_BIT
2048 );
2049 if (ret)
2050 goto end;
2051
2052 ret = _lttng_stream_packet_context_declare(session);
2053 if (ret)
2054 goto end;
2055
2056 ret = _lttng_event_header_declare(session);
2057 if (ret)
2058 goto end;
2059
2060 skip_session:
2061 list_for_each_entry(chan, &session->chan, list) {
2062 ret = _lttng_channel_metadata_statedump(session, chan);
2063 if (ret)
2064 goto end;
2065 }
2066
2067 list_for_each_entry(event, &session->events, list) {
2068 ret = _lttng_event_metadata_statedump(session, event->chan, event);
2069 if (ret)
2070 goto end;
2071 }
2072 session->metadata_dumped = 1;
2073 end:
2074 return ret;
2075 }
2076
2077 /**
2078 * lttng_transport_register - LTT transport registration
2079 * @transport: transport structure
2080 *
2081 * Registers a transport which can be used as output to extract the data out of
2082 * LTTng. The module calling this registration function must ensure that no
2083 * trap-inducing code will be executed by the transport functions. E.g.
2084 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2085 * is made visible to the transport function. This registration acts as a
2086 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2087 * after its registration must it synchronize the TLBs.
2088 */
2089 void lttng_transport_register(struct lttng_transport *transport)
2090 {
2091 /*
2092 * Make sure no page fault can be triggered by the module about to be
2093 * registered. We deal with this here so we don't have to call
2094 * vmalloc_sync_all() in each module's init.
2095 */
2096 wrapper_vmalloc_sync_all();
2097
2098 mutex_lock(&sessions_mutex);
2099 list_add_tail(&transport->node, &lttng_transport_list);
2100 mutex_unlock(&sessions_mutex);
2101 }
2102 EXPORT_SYMBOL_GPL(lttng_transport_register);
2103
2104 /**
2105 * lttng_transport_unregister - LTT transport unregistration
2106 * @transport: transport structure
2107 */
2108 void lttng_transport_unregister(struct lttng_transport *transport)
2109 {
2110 mutex_lock(&sessions_mutex);
2111 list_del(&transport->node);
2112 mutex_unlock(&sessions_mutex);
2113 }
2114 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
2115
2116 static int __init lttng_events_init(void)
2117 {
2118 int ret;
2119
2120 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2121 if (ret)
2122 return ret;
2123 ret = wrapper_get_pfnblock_flags_mask_init();
2124 if (ret)
2125 return ret;
2126 ret = lttng_context_init();
2127 if (ret)
2128 return ret;
2129 ret = lttng_tracepoint_init();
2130 if (ret)
2131 goto error_tp;
2132 event_cache = KMEM_CACHE(lttng_event, 0);
2133 if (!event_cache) {
2134 ret = -ENOMEM;
2135 goto error_kmem;
2136 }
2137 ret = lttng_abi_init();
2138 if (ret)
2139 goto error_abi;
2140 ret = lttng_logger_init();
2141 if (ret)
2142 goto error_logger;
2143 return 0;
2144
2145 error_logger:
2146 lttng_abi_exit();
2147 error_abi:
2148 kmem_cache_destroy(event_cache);
2149 error_kmem:
2150 lttng_tracepoint_exit();
2151 error_tp:
2152 lttng_context_exit();
2153 return ret;
2154 }
2155
2156 module_init(lttng_events_init);
2157
2158 static void __exit lttng_events_exit(void)
2159 {
2160 struct lttng_session *session, *tmpsession;
2161
2162 lttng_logger_exit();
2163 lttng_abi_exit();
2164 list_for_each_entry_safe(session, tmpsession, &sessions, list)
2165 lttng_session_destroy(session);
2166 kmem_cache_destroy(event_cache);
2167 lttng_tracepoint_exit();
2168 lttng_context_exit();
2169 }
2170
2171 module_exit(lttng_events_exit);
2172
2173 MODULE_LICENSE("GPL and additional rights");
2174 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2175 MODULE_DESCRIPTION("LTTng Events");
2176 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
2177 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
2178 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
2179 LTTNG_MODULES_EXTRAVERSION);
This page took 0.104806 seconds and 5 git commands to generate.