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