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