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