Implement PID tracking
[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/list.h>
25 #include <linux/mutex.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/utsname.h>
30 #include <linux/err.h>
31 #include "wrapper/uuid.h"
32 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
33 #include "wrapper/random.h"
34 #include "wrapper/tracepoint.h"
35 #include "lttng-kernel-version.h"
36 #include "lttng-events.h"
37 #include "lttng-tracer.h"
38 #include "lttng-abi-old.h"
39
40 #define METADATA_CACHE_DEFAULT_SIZE 4096
41
42 static LIST_HEAD(sessions);
43 static LIST_HEAD(lttng_transport_list);
44 /*
45 * Protect the sessions and metadata caches.
46 */
47 static DEFINE_MUTEX(sessions_mutex);
48 static struct kmem_cache *event_cache;
49
50 static void _lttng_event_destroy(struct lttng_event *event);
51 static void _lttng_channel_destroy(struct lttng_channel *chan);
52 static int _lttng_event_unregister(struct lttng_event *event);
53 static
54 int _lttng_event_metadata_statedump(struct lttng_session *session,
55 struct lttng_channel *chan,
56 struct lttng_event *event);
57 static
58 int _lttng_session_metadata_statedump(struct lttng_session *session);
59 static
60 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
61
62 void synchronize_trace(void)
63 {
64 synchronize_sched();
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
66 #ifdef CONFIG_PREEMPT_RT_FULL
67 synchronize_rcu();
68 #endif
69 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
70 #ifdef CONFIG_PREEMPT_RT
71 synchronize_rcu();
72 #endif
73 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
74 }
75
76 struct lttng_session *lttng_session_create(void)
77 {
78 struct lttng_session *session;
79 struct lttng_metadata_cache *metadata_cache;
80
81 mutex_lock(&sessions_mutex);
82 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
83 if (!session)
84 goto err;
85 INIT_LIST_HEAD(&session->chan);
86 INIT_LIST_HEAD(&session->events);
87 uuid_le_gen(&session->uuid);
88
89 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
90 GFP_KERNEL);
91 if (!metadata_cache)
92 goto err_free_session;
93 metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE,
94 GFP_KERNEL);
95 if (!metadata_cache->data)
96 goto err_free_cache;
97 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
98 kref_init(&metadata_cache->refcount);
99 session->metadata_cache = metadata_cache;
100 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
101 memcpy(&metadata_cache->uuid, &session->uuid,
102 sizeof(metadata_cache->uuid));
103 list_add(&session->list, &sessions);
104 mutex_unlock(&sessions_mutex);
105 return session;
106
107 err_free_cache:
108 kfree(metadata_cache);
109 err_free_session:
110 kfree(session);
111 err:
112 mutex_unlock(&sessions_mutex);
113 return NULL;
114 }
115
116 void metadata_cache_destroy(struct kref *kref)
117 {
118 struct lttng_metadata_cache *cache =
119 container_of(kref, struct lttng_metadata_cache, refcount);
120 kfree(cache->data);
121 kfree(cache);
122 }
123
124 void lttng_session_destroy(struct lttng_session *session)
125 {
126 struct lttng_channel *chan, *tmpchan;
127 struct lttng_event *event, *tmpevent;
128 struct lttng_metadata_stream *metadata_stream;
129 int ret;
130
131 mutex_lock(&sessions_mutex);
132 ACCESS_ONCE(session->active) = 0;
133 list_for_each_entry(chan, &session->chan, list) {
134 ret = lttng_syscalls_unregister(chan);
135 WARN_ON(ret);
136 }
137 list_for_each_entry(event, &session->events, list) {
138 ret = _lttng_event_unregister(event);
139 WARN_ON(ret);
140 }
141 synchronize_trace(); /* Wait for in-flight events to complete */
142 list_for_each_entry_safe(event, tmpevent, &session->events, list)
143 _lttng_event_destroy(event);
144 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
145 BUG_ON(chan->channel_type == METADATA_CHANNEL);
146 _lttng_channel_destroy(chan);
147 }
148 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
149 _lttng_metadata_channel_hangup(metadata_stream);
150 if (session->pid_tracker)
151 lttng_pid_tracker_destroy(session->pid_tracker);
152 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
153 list_del(&session->list);
154 mutex_unlock(&sessions_mutex);
155 kfree(session);
156 }
157
158 int lttng_session_enable(struct lttng_session *session)
159 {
160 int ret = 0;
161 struct lttng_channel *chan;
162
163 mutex_lock(&sessions_mutex);
164 if (session->active) {
165 ret = -EBUSY;
166 goto end;
167 }
168
169 /*
170 * Snapshot the number of events per channel to know the type of header
171 * we need to use.
172 */
173 list_for_each_entry(chan, &session->chan, list) {
174 if (chan->header_type)
175 continue; /* don't change it if session stop/restart */
176 if (chan->free_event_id < 31)
177 chan->header_type = 1; /* compact */
178 else
179 chan->header_type = 2; /* large */
180 }
181
182 ACCESS_ONCE(session->active) = 1;
183 ACCESS_ONCE(session->been_active) = 1;
184 ret = _lttng_session_metadata_statedump(session);
185 if (ret) {
186 ACCESS_ONCE(session->active) = 0;
187 goto end;
188 }
189 ret = lttng_statedump_start(session);
190 if (ret)
191 ACCESS_ONCE(session->active) = 0;
192 end:
193 mutex_unlock(&sessions_mutex);
194 return ret;
195 }
196
197 int lttng_session_disable(struct lttng_session *session)
198 {
199 int ret = 0;
200
201 mutex_lock(&sessions_mutex);
202 if (!session->active) {
203 ret = -EBUSY;
204 goto end;
205 }
206 ACCESS_ONCE(session->active) = 0;
207 end:
208 mutex_unlock(&sessions_mutex);
209 return ret;
210 }
211
212 int lttng_channel_enable(struct lttng_channel *channel)
213 {
214 int old;
215
216 if (channel->channel_type == METADATA_CHANNEL)
217 return -EPERM;
218 old = xchg(&channel->enabled, 1);
219 if (old)
220 return -EEXIST;
221 return 0;
222 }
223
224 int lttng_channel_disable(struct lttng_channel *channel)
225 {
226 int old;
227
228 if (channel->channel_type == METADATA_CHANNEL)
229 return -EPERM;
230 old = xchg(&channel->enabled, 0);
231 if (!old)
232 return -EEXIST;
233 return 0;
234 }
235
236 int lttng_event_enable(struct lttng_event *event)
237 {
238 int old;
239
240 if (event->chan->channel_type == METADATA_CHANNEL)
241 return -EPERM;
242 old = xchg(&event->enabled, 1);
243 if (old)
244 return -EEXIST;
245 return 0;
246 }
247
248 int lttng_event_disable(struct lttng_event *event)
249 {
250 int old;
251
252 if (event->chan->channel_type == METADATA_CHANNEL)
253 return -EPERM;
254 old = xchg(&event->enabled, 0);
255 if (!old)
256 return -EEXIST;
257 return 0;
258 }
259
260 static struct lttng_transport *lttng_transport_find(const char *name)
261 {
262 struct lttng_transport *transport;
263
264 list_for_each_entry(transport, &lttng_transport_list, node) {
265 if (!strcmp(transport->name, name))
266 return transport;
267 }
268 return NULL;
269 }
270
271 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
272 const char *transport_name,
273 void *buf_addr,
274 size_t subbuf_size, size_t num_subbuf,
275 unsigned int switch_timer_interval,
276 unsigned int read_timer_interval,
277 enum channel_type channel_type)
278 {
279 struct lttng_channel *chan;
280 struct lttng_transport *transport = NULL;
281
282 mutex_lock(&sessions_mutex);
283 if (session->been_active && channel_type != METADATA_CHANNEL)
284 goto active; /* Refuse to add channel to active session */
285 transport = lttng_transport_find(transport_name);
286 if (!transport) {
287 printk(KERN_WARNING "LTTng transport %s not found\n",
288 transport_name);
289 goto notransport;
290 }
291 if (!try_module_get(transport->owner)) {
292 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
293 goto notransport;
294 }
295 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
296 if (!chan)
297 goto nomem;
298 chan->session = session;
299 chan->id = session->free_chan_id++;
300 chan->ops = &transport->ops;
301 /*
302 * Note: the channel creation op already writes into the packet
303 * headers. Therefore the "chan" information used as input
304 * should be already accessible.
305 */
306 chan->chan = transport->ops.channel_create(transport_name,
307 chan, buf_addr, subbuf_size, num_subbuf,
308 switch_timer_interval, read_timer_interval);
309 if (!chan->chan)
310 goto create_error;
311 chan->enabled = 1;
312 chan->transport = transport;
313 chan->channel_type = channel_type;
314 list_add(&chan->list, &session->chan);
315 mutex_unlock(&sessions_mutex);
316 return chan;
317
318 create_error:
319 kfree(chan);
320 nomem:
321 if (transport)
322 module_put(transport->owner);
323 notransport:
324 active:
325 mutex_unlock(&sessions_mutex);
326 return NULL;
327 }
328
329 /*
330 * Only used internally at session destruction for per-cpu channels, and
331 * when metadata channel is released.
332 * Needs to be called with sessions mutex held.
333 */
334 static
335 void _lttng_channel_destroy(struct lttng_channel *chan)
336 {
337 chan->ops->channel_destroy(chan->chan);
338 module_put(chan->transport->owner);
339 list_del(&chan->list);
340 lttng_destroy_context(chan->ctx);
341 kfree(chan);
342 }
343
344 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
345 {
346 BUG_ON(chan->channel_type != METADATA_CHANNEL);
347
348 /* Protect the metadata cache with the sessions_mutex. */
349 mutex_lock(&sessions_mutex);
350 _lttng_channel_destroy(chan);
351 mutex_unlock(&sessions_mutex);
352 }
353 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
354
355 static
356 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
357 {
358 stream->finalized = 1;
359 wake_up_interruptible(&stream->read_wait);
360 }
361
362 /*
363 * Supports event creation while tracing session is active.
364 */
365 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
366 struct lttng_kernel_event *event_param,
367 void *filter,
368 const struct lttng_event_desc *internal_desc)
369 {
370 struct lttng_event *event;
371 int ret;
372
373 mutex_lock(&sessions_mutex);
374 if (chan->free_event_id == -1U) {
375 ret = -EMFILE;
376 goto full;
377 }
378 /*
379 * This is O(n^2) (for each event, the loop is called at event
380 * creation). Might require a hash if we have lots of events.
381 */
382 list_for_each_entry(event, &chan->session->events, list) {
383 if (!strcmp(event->desc->name, event_param->name)) {
384 /*
385 * Allow events with the same name to appear in
386 * different channels.
387 */
388 if (event->chan == chan) {
389 ret = -EEXIST;
390 goto exist;
391 }
392 }
393 }
394 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
395 if (!event) {
396 ret = -ENOMEM;
397 goto cache_error;
398 }
399 event->chan = chan;
400 event->filter = filter;
401 event->id = chan->free_event_id++;
402 event->enabled = 1;
403 event->instrumentation = event_param->instrumentation;
404 /* Populate lttng_event structure before tracepoint registration. */
405 smp_wmb();
406 switch (event_param->instrumentation) {
407 case LTTNG_KERNEL_TRACEPOINT:
408 event->desc = lttng_event_get(event_param->name);
409 if (!event->desc) {
410 ret = -ENOENT;
411 goto register_error;
412 }
413 ret = lttng_wrapper_tracepoint_probe_register(event->desc->kname,
414 event->desc->probe_callback,
415 event);
416 if (ret) {
417 ret = -EINVAL;
418 goto register_error;
419 }
420 break;
421 case LTTNG_KERNEL_KPROBE:
422 ret = lttng_kprobes_register(event_param->name,
423 event_param->u.kprobe.symbol_name,
424 event_param->u.kprobe.offset,
425 event_param->u.kprobe.addr,
426 event);
427 if (ret) {
428 ret = -EINVAL;
429 goto register_error;
430 }
431 ret = try_module_get(event->desc->owner);
432 WARN_ON_ONCE(!ret);
433 break;
434 case LTTNG_KERNEL_KRETPROBE:
435 {
436 struct lttng_event *event_return;
437
438 /* kretprobe defines 2 events */
439 event_return =
440 kmem_cache_zalloc(event_cache, GFP_KERNEL);
441 if (!event_return) {
442 ret = -ENOMEM;
443 goto register_error;
444 }
445 event_return->chan = chan;
446 event_return->filter = filter;
447 event_return->id = chan->free_event_id++;
448 event_return->enabled = 1;
449 event_return->instrumentation = event_param->instrumentation;
450 /*
451 * Populate lttng_event structure before kretprobe registration.
452 */
453 smp_wmb();
454 ret = lttng_kretprobes_register(event_param->name,
455 event_param->u.kretprobe.symbol_name,
456 event_param->u.kretprobe.offset,
457 event_param->u.kretprobe.addr,
458 event, event_return);
459 if (ret) {
460 kmem_cache_free(event_cache, event_return);
461 ret = -EINVAL;
462 goto register_error;
463 }
464 /* Take 2 refs on the module: one per event. */
465 ret = try_module_get(event->desc->owner);
466 WARN_ON_ONCE(!ret);
467 ret = try_module_get(event->desc->owner);
468 WARN_ON_ONCE(!ret);
469 ret = _lttng_event_metadata_statedump(chan->session, chan,
470 event_return);
471 WARN_ON_ONCE(ret > 0);
472 if (ret) {
473 kmem_cache_free(event_cache, event_return);
474 module_put(event->desc->owner);
475 module_put(event->desc->owner);
476 goto statedump_error;
477 }
478 list_add(&event_return->list, &chan->session->events);
479 break;
480 }
481 case LTTNG_KERNEL_FUNCTION:
482 ret = lttng_ftrace_register(event_param->name,
483 event_param->u.ftrace.symbol_name,
484 event);
485 if (ret) {
486 goto register_error;
487 }
488 ret = try_module_get(event->desc->owner);
489 WARN_ON_ONCE(!ret);
490 break;
491 case LTTNG_KERNEL_NOOP:
492 event->desc = internal_desc;
493 if (!event->desc) {
494 ret = -EINVAL;
495 goto register_error;
496 }
497 break;
498 default:
499 WARN_ON_ONCE(1);
500 ret = -EINVAL;
501 goto register_error;
502 }
503 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
504 WARN_ON_ONCE(ret > 0);
505 if (ret) {
506 goto statedump_error;
507 }
508 list_add(&event->list, &chan->session->events);
509 mutex_unlock(&sessions_mutex);
510 return event;
511
512 statedump_error:
513 /* If a statedump error occurs, events will not be readable. */
514 register_error:
515 kmem_cache_free(event_cache, event);
516 cache_error:
517 exist:
518 full:
519 mutex_unlock(&sessions_mutex);
520 return ERR_PTR(ret);
521 }
522
523 /*
524 * Only used internally at session destruction.
525 */
526 int _lttng_event_unregister(struct lttng_event *event)
527 {
528 int ret = -EINVAL;
529
530 switch (event->instrumentation) {
531 case LTTNG_KERNEL_TRACEPOINT:
532 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
533 event->desc->probe_callback,
534 event);
535 if (ret)
536 return ret;
537 break;
538 case LTTNG_KERNEL_KPROBE:
539 lttng_kprobes_unregister(event);
540 ret = 0;
541 break;
542 case LTTNG_KERNEL_KRETPROBE:
543 lttng_kretprobes_unregister(event);
544 ret = 0;
545 break;
546 case LTTNG_KERNEL_FUNCTION:
547 lttng_ftrace_unregister(event);
548 ret = 0;
549 break;
550 case LTTNG_KERNEL_NOOP:
551 ret = 0;
552 break;
553 default:
554 WARN_ON_ONCE(1);
555 }
556 return ret;
557 }
558
559 /*
560 * Only used internally at session destruction.
561 */
562 static
563 void _lttng_event_destroy(struct lttng_event *event)
564 {
565 switch (event->instrumentation) {
566 case LTTNG_KERNEL_TRACEPOINT:
567 lttng_event_put(event->desc);
568 break;
569 case LTTNG_KERNEL_KPROBE:
570 module_put(event->desc->owner);
571 lttng_kprobes_destroy_private(event);
572 break;
573 case LTTNG_KERNEL_KRETPROBE:
574 module_put(event->desc->owner);
575 lttng_kretprobes_destroy_private(event);
576 break;
577 case LTTNG_KERNEL_FUNCTION:
578 module_put(event->desc->owner);
579 lttng_ftrace_destroy_private(event);
580 break;
581 case LTTNG_KERNEL_NOOP:
582 break;
583 default:
584 WARN_ON_ONCE(1);
585 }
586 list_del(&event->list);
587 lttng_destroy_context(event->ctx);
588 kmem_cache_free(event_cache, event);
589 }
590
591 int lttng_session_track_pid(struct lttng_session *session, int pid)
592 {
593 int ret;
594
595 if (pid < -1)
596 return -EINVAL;
597 mutex_lock(&sessions_mutex);
598 if (pid == -1) {
599 /* track all pids: destroy tracker. */
600 if (session->pid_tracker) {
601 struct lttng_pid_tracker *lpf;
602
603 lpf = session->pid_tracker;
604 rcu_assign_pointer(session->pid_tracker, NULL);
605 synchronize_trace();
606 lttng_pid_tracker_destroy(lpf);
607 }
608 ret = 0;
609 } else {
610 if (!session->pid_tracker) {
611 struct lttng_pid_tracker *lpf;
612
613 lpf = lttng_pid_tracker_create();
614 if (!lpf) {
615 ret = -ENOMEM;
616 goto unlock;
617 }
618 ret = lttng_pid_tracker_add(lpf, pid);
619 rcu_assign_pointer(session->pid_tracker, lpf);
620 } else {
621 ret = lttng_pid_tracker_add(session->pid_tracker, pid);
622 }
623 }
624 unlock:
625 mutex_unlock(&sessions_mutex);
626 return ret;
627 }
628
629 int lttng_session_untrack_pid(struct lttng_session *session, int pid)
630 {
631 int ret;
632
633 if (pid < -1)
634 return -EINVAL;
635 mutex_lock(&sessions_mutex);
636 if (pid == -1) {
637 /* untrack all pids: replace by empty tracker. */
638 struct lttng_pid_tracker *old_lpf = session->pid_tracker;
639 struct lttng_pid_tracker *lpf;
640
641 lpf = lttng_pid_tracker_create();
642 if (!lpf) {
643 ret = -ENOMEM;
644 goto unlock;
645 }
646 rcu_assign_pointer(session->pid_tracker, lpf);
647 synchronize_trace();
648 if (old_lpf)
649 lttng_pid_tracker_destroy(old_lpf);
650 ret = 0;
651 } else {
652 if (!session->pid_tracker) {
653 ret = -ENOENT;
654 goto unlock;
655 }
656 ret = lttng_pid_tracker_del(session->pid_tracker, pid);
657 }
658 unlock:
659 mutex_unlock(&sessions_mutex);
660 return ret;
661 }
662
663 /*
664 * Serialize at most one packet worth of metadata into a metadata
665 * channel.
666 * We have exclusive access to our metadata buffer (protected by the
667 * sessions_mutex), so we can do racy operations such as looking for
668 * remaining space left in packet and write, since mutual exclusion
669 * protects us from concurrent writes.
670 * Returns the number of bytes written in the channel, 0 if no data
671 * was written and a negative value on error.
672 */
673 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
674 struct channel *chan)
675 {
676 struct lib_ring_buffer_ctx ctx;
677 int ret = 0;
678 size_t len, reserve_len;
679
680 /*
681 * Ensure we support mutiple get_next / put sequences followed
682 * by put_next. The metadata stream lock internally protects
683 * reading the metadata cache. It can indeed be read
684 * concurrently by "get_next_subbuf" and "flush" operations on
685 * the buffer invoked by different processes.
686 */
687 mutex_lock(&stream->lock);
688 WARN_ON(stream->metadata_in < stream->metadata_out);
689 if (stream->metadata_in != stream->metadata_out)
690 goto end;
691
692 len = stream->metadata_cache->metadata_written -
693 stream->metadata_in;
694 if (!len)
695 goto end;
696 reserve_len = min_t(size_t,
697 stream->transport->ops.packet_avail_size(chan),
698 len);
699 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
700 sizeof(char), -1);
701 /*
702 * If reservation failed, return an error to the caller.
703 */
704 ret = stream->transport->ops.event_reserve(&ctx, 0);
705 if (ret != 0) {
706 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
707 goto end;
708 }
709 stream->transport->ops.event_write(&ctx,
710 stream->metadata_cache->data + stream->metadata_in,
711 reserve_len);
712 stream->transport->ops.event_commit(&ctx);
713 stream->metadata_in += reserve_len;
714 ret = reserve_len;
715
716 end:
717 mutex_unlock(&stream->lock);
718 return ret;
719 }
720
721 /*
722 * Write the metadata to the metadata cache.
723 * Must be called with sessions_mutex held.
724 */
725 int lttng_metadata_printf(struct lttng_session *session,
726 const char *fmt, ...)
727 {
728 char *str;
729 size_t len;
730 va_list ap;
731 struct lttng_metadata_stream *stream;
732
733 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
734
735 va_start(ap, fmt);
736 str = kvasprintf(GFP_KERNEL, fmt, ap);
737 va_end(ap);
738 if (!str)
739 return -ENOMEM;
740
741 len = strlen(str);
742 if (session->metadata_cache->metadata_written + len >
743 session->metadata_cache->cache_alloc) {
744 char *tmp_cache_realloc;
745 unsigned int tmp_cache_alloc_size;
746
747 tmp_cache_alloc_size = max_t(unsigned int,
748 session->metadata_cache->cache_alloc + len,
749 session->metadata_cache->cache_alloc << 1);
750 tmp_cache_realloc = krealloc(session->metadata_cache->data,
751 tmp_cache_alloc_size, GFP_KERNEL);
752 if (!tmp_cache_realloc)
753 goto err;
754 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
755 session->metadata_cache->data = tmp_cache_realloc;
756 }
757 memcpy(session->metadata_cache->data +
758 session->metadata_cache->metadata_written,
759 str, len);
760 session->metadata_cache->metadata_written += len;
761 kfree(str);
762
763 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
764 wake_up_interruptible(&stream->read_wait);
765
766 return 0;
767
768 err:
769 kfree(str);
770 return -ENOMEM;
771 }
772
773 /*
774 * Must be called with sessions_mutex held.
775 */
776 static
777 int _lttng_field_statedump(struct lttng_session *session,
778 const struct lttng_event_field *field)
779 {
780 int ret = 0;
781
782 switch (field->type.atype) {
783 case atype_integer:
784 ret = lttng_metadata_printf(session,
785 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
786 field->type.u.basic.integer.size,
787 field->type.u.basic.integer.alignment,
788 field->type.u.basic.integer.signedness,
789 (field->type.u.basic.integer.encoding == lttng_encode_none)
790 ? "none"
791 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
792 ? "UTF8"
793 : "ASCII",
794 field->type.u.basic.integer.base,
795 #ifdef __BIG_ENDIAN
796 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
797 #else
798 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
799 #endif
800 field->name);
801 break;
802 case atype_enum:
803 ret = lttng_metadata_printf(session,
804 " %s _%s;\n",
805 field->type.u.basic.enumeration.name,
806 field->name);
807 break;
808 case atype_array:
809 {
810 const struct lttng_basic_type *elem_type;
811
812 elem_type = &field->type.u.array.elem_type;
813 ret = lttng_metadata_printf(session,
814 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
815 elem_type->u.basic.integer.size,
816 elem_type->u.basic.integer.alignment,
817 elem_type->u.basic.integer.signedness,
818 (elem_type->u.basic.integer.encoding == lttng_encode_none)
819 ? "none"
820 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
821 ? "UTF8"
822 : "ASCII",
823 elem_type->u.basic.integer.base,
824 #ifdef __BIG_ENDIAN
825 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
826 #else
827 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
828 #endif
829 field->name, field->type.u.array.length);
830 break;
831 }
832 case atype_sequence:
833 {
834 const struct lttng_basic_type *elem_type;
835 const struct lttng_basic_type *length_type;
836
837 elem_type = &field->type.u.sequence.elem_type;
838 length_type = &field->type.u.sequence.length_type;
839 ret = lttng_metadata_printf(session,
840 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
841 length_type->u.basic.integer.size,
842 (unsigned int) length_type->u.basic.integer.alignment,
843 length_type->u.basic.integer.signedness,
844 (length_type->u.basic.integer.encoding == lttng_encode_none)
845 ? "none"
846 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
847 ? "UTF8"
848 : "ASCII"),
849 length_type->u.basic.integer.base,
850 #ifdef __BIG_ENDIAN
851 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
852 #else
853 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
854 #endif
855 field->name);
856 if (ret)
857 return ret;
858
859 ret = lttng_metadata_printf(session,
860 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
861 elem_type->u.basic.integer.size,
862 (unsigned int) elem_type->u.basic.integer.alignment,
863 elem_type->u.basic.integer.signedness,
864 (elem_type->u.basic.integer.encoding == lttng_encode_none)
865 ? "none"
866 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
867 ? "UTF8"
868 : "ASCII"),
869 elem_type->u.basic.integer.base,
870 #ifdef __BIG_ENDIAN
871 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
872 #else
873 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
874 #endif
875 field->name,
876 field->name);
877 break;
878 }
879
880 case atype_string:
881 /* Default encoding is UTF8 */
882 ret = lttng_metadata_printf(session,
883 " string%s _%s;\n",
884 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
885 " { encoding = ASCII; }" : "",
886 field->name);
887 break;
888 default:
889 WARN_ON_ONCE(1);
890 return -EINVAL;
891 }
892 return ret;
893 }
894
895 static
896 int _lttng_context_metadata_statedump(struct lttng_session *session,
897 struct lttng_ctx *ctx)
898 {
899 int ret = 0;
900 int i;
901
902 if (!ctx)
903 return 0;
904 for (i = 0; i < ctx->nr_fields; i++) {
905 const struct lttng_ctx_field *field = &ctx->fields[i];
906
907 ret = _lttng_field_statedump(session, &field->event_field);
908 if (ret)
909 return ret;
910 }
911 return ret;
912 }
913
914 static
915 int _lttng_fields_metadata_statedump(struct lttng_session *session,
916 struct lttng_event *event)
917 {
918 const struct lttng_event_desc *desc = event->desc;
919 int ret = 0;
920 int i;
921
922 for (i = 0; i < desc->nr_fields; i++) {
923 const struct lttng_event_field *field = &desc->fields[i];
924
925 ret = _lttng_field_statedump(session, field);
926 if (ret)
927 return ret;
928 }
929 return ret;
930 }
931
932 /*
933 * Must be called with sessions_mutex held.
934 */
935 static
936 int _lttng_event_metadata_statedump(struct lttng_session *session,
937 struct lttng_channel *chan,
938 struct lttng_event *event)
939 {
940 int ret = 0;
941
942 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
943 return 0;
944 if (chan->channel_type == METADATA_CHANNEL)
945 return 0;
946
947 ret = lttng_metadata_printf(session,
948 "event {\n"
949 " name = \"%s\";\n"
950 " id = %u;\n"
951 " stream_id = %u;\n",
952 event->desc->name,
953 event->id,
954 event->chan->id);
955 if (ret)
956 goto end;
957
958 if (event->ctx) {
959 ret = lttng_metadata_printf(session,
960 " context := struct {\n");
961 if (ret)
962 goto end;
963 }
964 ret = _lttng_context_metadata_statedump(session, event->ctx);
965 if (ret)
966 goto end;
967 if (event->ctx) {
968 ret = lttng_metadata_printf(session,
969 " };\n");
970 if (ret)
971 goto end;
972 }
973
974 ret = lttng_metadata_printf(session,
975 " fields := struct {\n"
976 );
977 if (ret)
978 goto end;
979
980 ret = _lttng_fields_metadata_statedump(session, event);
981 if (ret)
982 goto end;
983
984 /*
985 * LTTng space reservation can only reserve multiples of the
986 * byte size.
987 */
988 ret = lttng_metadata_printf(session,
989 " };\n"
990 "};\n\n");
991 if (ret)
992 goto end;
993
994 event->metadata_dumped = 1;
995 end:
996 return ret;
997
998 }
999
1000 /*
1001 * Must be called with sessions_mutex held.
1002 */
1003 static
1004 int _lttng_channel_metadata_statedump(struct lttng_session *session,
1005 struct lttng_channel *chan)
1006 {
1007 int ret = 0;
1008
1009 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
1010 return 0;
1011
1012 if (chan->channel_type == METADATA_CHANNEL)
1013 return 0;
1014
1015 WARN_ON_ONCE(!chan->header_type);
1016 ret = lttng_metadata_printf(session,
1017 "stream {\n"
1018 " id = %u;\n"
1019 " event.header := %s;\n"
1020 " packet.context := struct packet_context;\n",
1021 chan->id,
1022 chan->header_type == 1 ? "struct event_header_compact" :
1023 "struct event_header_large");
1024 if (ret)
1025 goto end;
1026
1027 if (chan->ctx) {
1028 ret = lttng_metadata_printf(session,
1029 " event.context := struct {\n");
1030 if (ret)
1031 goto end;
1032 }
1033 ret = _lttng_context_metadata_statedump(session, chan->ctx);
1034 if (ret)
1035 goto end;
1036 if (chan->ctx) {
1037 ret = lttng_metadata_printf(session,
1038 " };\n");
1039 if (ret)
1040 goto end;
1041 }
1042
1043 ret = lttng_metadata_printf(session,
1044 "};\n\n");
1045
1046 chan->metadata_dumped = 1;
1047 end:
1048 return ret;
1049 }
1050
1051 /*
1052 * Must be called with sessions_mutex held.
1053 */
1054 static
1055 int _lttng_stream_packet_context_declare(struct lttng_session *session)
1056 {
1057 return lttng_metadata_printf(session,
1058 "struct packet_context {\n"
1059 " uint64_clock_monotonic_t timestamp_begin;\n"
1060 " uint64_clock_monotonic_t timestamp_end;\n"
1061 " uint64_t content_size;\n"
1062 " uint64_t packet_size;\n"
1063 " unsigned long events_discarded;\n"
1064 " uint32_t cpu_id;\n"
1065 "};\n\n"
1066 );
1067 }
1068
1069 /*
1070 * Compact header:
1071 * id: range: 0 - 30.
1072 * id 31 is reserved to indicate an extended header.
1073 *
1074 * Large header:
1075 * id: range: 0 - 65534.
1076 * id 65535 is reserved to indicate an extended header.
1077 *
1078 * Must be called with sessions_mutex held.
1079 */
1080 static
1081 int _lttng_event_header_declare(struct lttng_session *session)
1082 {
1083 return lttng_metadata_printf(session,
1084 "struct event_header_compact {\n"
1085 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1086 " variant <id> {\n"
1087 " struct {\n"
1088 " uint27_clock_monotonic_t timestamp;\n"
1089 " } compact;\n"
1090 " struct {\n"
1091 " uint32_t id;\n"
1092 " uint64_clock_monotonic_t timestamp;\n"
1093 " } extended;\n"
1094 " } v;\n"
1095 "} align(%u);\n"
1096 "\n"
1097 "struct event_header_large {\n"
1098 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1099 " variant <id> {\n"
1100 " struct {\n"
1101 " uint32_clock_monotonic_t timestamp;\n"
1102 " } compact;\n"
1103 " struct {\n"
1104 " uint32_t id;\n"
1105 " uint64_clock_monotonic_t timestamp;\n"
1106 " } extended;\n"
1107 " } v;\n"
1108 "} align(%u);\n\n",
1109 lttng_alignof(uint32_t) * CHAR_BIT,
1110 lttng_alignof(uint16_t) * CHAR_BIT
1111 );
1112 }
1113
1114 /*
1115 * Approximation of NTP time of day to clock monotonic correlation,
1116 * taken at start of trace.
1117 * Yes, this is only an approximation. Yes, we can (and will) do better
1118 * in future versions.
1119 */
1120 static
1121 uint64_t measure_clock_offset(void)
1122 {
1123 uint64_t offset, monotonic[2], realtime;
1124 struct timespec rts = { 0, 0 };
1125 unsigned long flags;
1126
1127 /* Disable interrupts to increase correlation precision. */
1128 local_irq_save(flags);
1129 monotonic[0] = trace_clock_read64();
1130 getnstimeofday(&rts);
1131 monotonic[1] = trace_clock_read64();
1132 local_irq_restore(flags);
1133
1134 offset = (monotonic[0] + monotonic[1]) >> 1;
1135 realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
1136 realtime += rts.tv_nsec;
1137 offset = realtime - offset;
1138 return offset;
1139 }
1140
1141 /*
1142 * Output metadata into this session's metadata buffers.
1143 * Must be called with sessions_mutex held.
1144 */
1145 static
1146 int _lttng_session_metadata_statedump(struct lttng_session *session)
1147 {
1148 unsigned char *uuid_c = session->uuid.b;
1149 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
1150 struct lttng_channel *chan;
1151 struct lttng_event *event;
1152 int ret = 0;
1153
1154 if (!ACCESS_ONCE(session->active))
1155 return 0;
1156 if (session->metadata_dumped)
1157 goto skip_session;
1158
1159 snprintf(uuid_s, sizeof(uuid_s),
1160 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1161 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1162 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1163 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1164 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1165
1166 ret = lttng_metadata_printf(session,
1167 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1168 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1169 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1170 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1171 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1172 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1173 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1174 "\n"
1175 "trace {\n"
1176 " major = %u;\n"
1177 " minor = %u;\n"
1178 " uuid = \"%s\";\n"
1179 " byte_order = %s;\n"
1180 " packet.header := struct {\n"
1181 " uint32_t magic;\n"
1182 " uint8_t uuid[16];\n"
1183 " uint32_t stream_id;\n"
1184 " };\n"
1185 "};\n\n",
1186 lttng_alignof(uint8_t) * CHAR_BIT,
1187 lttng_alignof(uint16_t) * CHAR_BIT,
1188 lttng_alignof(uint32_t) * CHAR_BIT,
1189 lttng_alignof(uint64_t) * CHAR_BIT,
1190 sizeof(unsigned long) * CHAR_BIT,
1191 lttng_alignof(unsigned long) * CHAR_BIT,
1192 CTF_SPEC_MAJOR,
1193 CTF_SPEC_MINOR,
1194 uuid_s,
1195 #ifdef __BIG_ENDIAN
1196 "be"
1197 #else
1198 "le"
1199 #endif
1200 );
1201 if (ret)
1202 goto end;
1203
1204 ret = lttng_metadata_printf(session,
1205 "env {\n"
1206 " hostname = \"%s\";\n"
1207 " domain = \"kernel\";\n"
1208 " sysname = \"%s\";\n"
1209 " kernel_release = \"%s\";\n"
1210 " kernel_version = \"%s\";\n"
1211 " tracer_name = \"lttng-modules\";\n"
1212 " tracer_major = %d;\n"
1213 " tracer_minor = %d;\n"
1214 " tracer_patchlevel = %d;\n"
1215 "};\n\n",
1216 current->nsproxy->uts_ns->name.nodename,
1217 utsname()->sysname,
1218 utsname()->release,
1219 utsname()->version,
1220 LTTNG_MODULES_MAJOR_VERSION,
1221 LTTNG_MODULES_MINOR_VERSION,
1222 LTTNG_MODULES_PATCHLEVEL_VERSION
1223 );
1224 if (ret)
1225 goto end;
1226
1227 ret = lttng_metadata_printf(session,
1228 "clock {\n"
1229 " name = %s;\n",
1230 "monotonic"
1231 );
1232 if (ret)
1233 goto end;
1234
1235 if (!trace_clock_uuid(clock_uuid_s)) {
1236 ret = lttng_metadata_printf(session,
1237 " uuid = \"%s\";\n",
1238 clock_uuid_s
1239 );
1240 if (ret)
1241 goto end;
1242 }
1243
1244 ret = lttng_metadata_printf(session,
1245 " description = \"Monotonic Clock\";\n"
1246 " freq = %llu; /* Frequency, in Hz */\n"
1247 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1248 " offset = %llu;\n"
1249 "};\n\n",
1250 (unsigned long long) trace_clock_freq(),
1251 (unsigned long long) measure_clock_offset()
1252 );
1253 if (ret)
1254 goto end;
1255
1256 ret = lttng_metadata_printf(session,
1257 "typealias integer {\n"
1258 " size = 27; align = 1; signed = false;\n"
1259 " map = clock.monotonic.value;\n"
1260 "} := uint27_clock_monotonic_t;\n"
1261 "\n"
1262 "typealias integer {\n"
1263 " size = 32; align = %u; signed = false;\n"
1264 " map = clock.monotonic.value;\n"
1265 "} := uint32_clock_monotonic_t;\n"
1266 "\n"
1267 "typealias integer {\n"
1268 " size = 64; align = %u; signed = false;\n"
1269 " map = clock.monotonic.value;\n"
1270 "} := uint64_clock_monotonic_t;\n\n",
1271 lttng_alignof(uint32_t) * CHAR_BIT,
1272 lttng_alignof(uint64_t) * CHAR_BIT
1273 );
1274 if (ret)
1275 goto end;
1276
1277 ret = _lttng_stream_packet_context_declare(session);
1278 if (ret)
1279 goto end;
1280
1281 ret = _lttng_event_header_declare(session);
1282 if (ret)
1283 goto end;
1284
1285 skip_session:
1286 list_for_each_entry(chan, &session->chan, list) {
1287 ret = _lttng_channel_metadata_statedump(session, chan);
1288 if (ret)
1289 goto end;
1290 }
1291
1292 list_for_each_entry(event, &session->events, list) {
1293 ret = _lttng_event_metadata_statedump(session, event->chan, event);
1294 if (ret)
1295 goto end;
1296 }
1297 session->metadata_dumped = 1;
1298 end:
1299 return ret;
1300 }
1301
1302 /**
1303 * lttng_transport_register - LTT transport registration
1304 * @transport: transport structure
1305 *
1306 * Registers a transport which can be used as output to extract the data out of
1307 * LTTng. The module calling this registration function must ensure that no
1308 * trap-inducing code will be executed by the transport functions. E.g.
1309 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1310 * is made visible to the transport function. This registration acts as a
1311 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1312 * after its registration must it synchronize the TLBs.
1313 */
1314 void lttng_transport_register(struct lttng_transport *transport)
1315 {
1316 /*
1317 * Make sure no page fault can be triggered by the module about to be
1318 * registered. We deal with this here so we don't have to call
1319 * vmalloc_sync_all() in each module's init.
1320 */
1321 wrapper_vmalloc_sync_all();
1322
1323 mutex_lock(&sessions_mutex);
1324 list_add_tail(&transport->node, &lttng_transport_list);
1325 mutex_unlock(&sessions_mutex);
1326 }
1327 EXPORT_SYMBOL_GPL(lttng_transport_register);
1328
1329 /**
1330 * lttng_transport_unregister - LTT transport unregistration
1331 * @transport: transport structure
1332 */
1333 void lttng_transport_unregister(struct lttng_transport *transport)
1334 {
1335 mutex_lock(&sessions_mutex);
1336 list_del(&transport->node);
1337 mutex_unlock(&sessions_mutex);
1338 }
1339 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
1340
1341 static int __init lttng_events_init(void)
1342 {
1343 int ret;
1344
1345 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
1346 if (ret)
1347 return ret;
1348
1349 ret = lttng_tracepoint_init();
1350 if (ret)
1351 return ret;
1352 event_cache = KMEM_CACHE(lttng_event, 0);
1353 if (!event_cache) {
1354 ret = -ENOMEM;
1355 goto error_kmem;
1356 }
1357 ret = lttng_abi_init();
1358 if (ret)
1359 goto error_abi;
1360 ret = lttng_logger_init();
1361 if (ret)
1362 goto error_logger;
1363 return 0;
1364
1365 error_logger:
1366 lttng_abi_exit();
1367 error_abi:
1368 kmem_cache_destroy(event_cache);
1369 error_kmem:
1370 lttng_tracepoint_exit();
1371 return ret;
1372 }
1373
1374 module_init(lttng_events_init);
1375
1376 static void __exit lttng_events_exit(void)
1377 {
1378 struct lttng_session *session, *tmpsession;
1379
1380 lttng_logger_exit();
1381 lttng_abi_exit();
1382 list_for_each_entry_safe(session, tmpsession, &sessions, list)
1383 lttng_session_destroy(session);
1384 kmem_cache_destroy(event_cache);
1385 lttng_tracepoint_exit();
1386 }
1387
1388 module_exit(lttng_events_exit);
1389
1390 MODULE_LICENSE("GPL and additional rights");
1391 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1392 MODULE_DESCRIPTION("LTTng Events");
1393 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
1394 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
1395 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
1396 LTTNG_MODULES_EXTRAVERSION);
This page took 0.084654 seconds and 5 git commands to generate.