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