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