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