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