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