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