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