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