4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
8 * Dual LGPL v2.1/GPL v2 license.
11 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/jiffies.h>
17 #include "wrapper/uuid.h"
18 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
19 #include "ltt-events.h"
20 #include "ltt-tracer.h"
22 static LIST_HEAD(sessions);
23 static LIST_HEAD(ltt_transport_list);
24 static DEFINE_MUTEX(sessions_mutex);
25 static struct kmem_cache *event_cache;
27 static void _ltt_event_destroy(struct ltt_event *event);
28 static void _ltt_channel_destroy(struct ltt_channel *chan);
29 static int _ltt_event_unregister(struct ltt_event *event);
31 int _ltt_event_metadata_statedump(struct ltt_session *session,
32 struct ltt_channel *chan,
33 struct ltt_event *event);
35 int _ltt_session_metadata_statedump(struct ltt_session *session);
37 void synchronize_trace(void)
40 #ifdef CONFIG_PREEMPT_RT
45 struct ltt_session *ltt_session_create(void)
47 struct ltt_session *session;
49 mutex_lock(&sessions_mutex);
50 session = kzalloc(sizeof(struct ltt_session), GFP_KERNEL);
53 INIT_LIST_HEAD(&session->chan);
54 INIT_LIST_HEAD(&session->events);
55 uuid_le_gen(&session->uuid);
56 list_add(&session->list, &sessions);
57 mutex_unlock(&sessions_mutex);
61 void ltt_session_destroy(struct ltt_session *session)
63 struct ltt_channel *chan, *tmpchan;
64 struct ltt_event *event, *tmpevent;
67 mutex_lock(&sessions_mutex);
68 ACCESS_ONCE(session->active) = 0;
69 list_for_each_entry(chan, &session->chan, list) {
70 ret = lttng_syscalls_unregister(chan);
73 list_for_each_entry(event, &session->events, list) {
74 ret = _ltt_event_unregister(event);
77 synchronize_trace(); /* Wait for in-flight events to complete */
78 list_for_each_entry_safe(event, tmpevent, &session->events, list)
79 _ltt_event_destroy(event);
80 list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
81 _ltt_channel_destroy(chan);
82 list_del(&session->list);
83 mutex_unlock(&sessions_mutex);
87 int ltt_session_enable(struct ltt_session *session)
90 struct ltt_channel *chan;
92 mutex_lock(&sessions_mutex);
93 if (session->active) {
99 * Snapshot the number of events per channel to know the type of header
102 list_for_each_entry(chan, &session->chan, list) {
103 if (chan->header_type)
104 continue; /* don't change it if session stop/restart */
105 if (chan->free_event_id < 31)
106 chan->header_type = 1; /* compact */
108 chan->header_type = 2; /* large */
111 ACCESS_ONCE(session->active) = 1;
112 ACCESS_ONCE(session->been_active) = 1;
113 ret = _ltt_session_metadata_statedump(session);
115 ACCESS_ONCE(session->active) = 0;
117 mutex_unlock(&sessions_mutex);
121 int ltt_session_disable(struct ltt_session *session)
125 mutex_lock(&sessions_mutex);
126 if (!session->active) {
130 ACCESS_ONCE(session->active) = 0;
132 mutex_unlock(&sessions_mutex);
136 int ltt_channel_enable(struct ltt_channel *channel)
140 if (channel == channel->session->metadata)
142 old = xchg(&channel->enabled, 1);
148 int ltt_channel_disable(struct ltt_channel *channel)
152 if (channel == channel->session->metadata)
154 old = xchg(&channel->enabled, 0);
160 int ltt_event_enable(struct ltt_event *event)
164 if (event->chan == event->chan->session->metadata)
166 old = xchg(&event->enabled, 1);
172 int ltt_event_disable(struct ltt_event *event)
176 if (event->chan == event->chan->session->metadata)
178 old = xchg(&event->enabled, 0);
184 static struct ltt_transport *ltt_transport_find(const char *name)
186 struct ltt_transport *transport;
188 list_for_each_entry(transport, <t_transport_list, node) {
189 if (!strcmp(transport->name, name))
195 struct ltt_channel *ltt_channel_create(struct ltt_session *session,
196 const char *transport_name,
198 size_t subbuf_size, size_t num_subbuf,
199 unsigned int switch_timer_interval,
200 unsigned int read_timer_interval)
202 struct ltt_channel *chan;
203 struct ltt_transport *transport = NULL;
205 mutex_lock(&sessions_mutex);
206 if (session->been_active)
207 goto active; /* Refuse to add channel to active session */
208 transport = ltt_transport_find(transport_name);
210 printk(KERN_WARNING "LTTng transport %s not found\n",
214 if (!try_module_get(transport->owner)) {
215 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
218 chan = kzalloc(sizeof(struct ltt_channel), GFP_KERNEL);
221 chan->session = session;
222 chan->id = session->free_chan_id++;
224 * Note: the channel creation op already writes into the packet
225 * headers. Therefore the "chan" information used as input
226 * should be already accessible.
228 chan->chan = transport->ops.channel_create("[lttng]", chan, buf_addr,
229 subbuf_size, num_subbuf, switch_timer_interval,
230 read_timer_interval);
234 chan->ops = &transport->ops;
235 chan->transport = transport;
236 list_add(&chan->list, &session->chan);
237 mutex_unlock(&sessions_mutex);
244 module_put(transport->owner);
247 mutex_unlock(&sessions_mutex);
252 * Only used internally at session destruction.
255 void _ltt_channel_destroy(struct ltt_channel *chan)
257 chan->ops->channel_destroy(chan->chan);
258 module_put(chan->transport->owner);
259 list_del(&chan->list);
260 lttng_destroy_context(chan->ctx);
265 * Supports event creation while tracing session is active.
267 struct ltt_event *ltt_event_create(struct ltt_channel *chan,
268 struct lttng_kernel_event *event_param,
270 const struct lttng_event_desc *internal_desc)
272 struct ltt_event *event;
275 mutex_lock(&sessions_mutex);
276 if (chan->free_event_id == -1UL)
279 * This is O(n^2) (for each event, the loop is called at event
280 * creation). Might require a hash if we have lots of events.
282 list_for_each_entry(event, &chan->session->events, list)
283 if (!strcmp(event->desc->name, event_param->name))
285 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
289 event->filter = filter;
290 event->id = chan->free_event_id++;
292 event->instrumentation = event_param->instrumentation;
293 /* Populate ltt_event structure before tracepoint registration. */
295 switch (event_param->instrumentation) {
296 case LTTNG_KERNEL_TRACEPOINT:
297 event->desc = ltt_event_get(event_param->name);
300 ret = tracepoint_probe_register(event_param->name,
301 event->desc->probe_callback,
306 case LTTNG_KERNEL_KPROBE:
307 ret = lttng_kprobes_register(event_param->name,
308 event_param->u.kprobe.symbol_name,
309 event_param->u.kprobe.offset,
310 event_param->u.kprobe.addr,
314 ret = try_module_get(event->desc->owner);
317 case LTTNG_KERNEL_KRETPROBE:
319 struct ltt_event *event_return;
321 /* kretprobe defines 2 events */
323 kmem_cache_zalloc(event_cache, GFP_KERNEL);
326 event_return->chan = chan;
327 event_return->filter = filter;
328 event_return->id = chan->free_event_id++;
329 event_return->enabled = 1;
330 event_return->instrumentation = event_param->instrumentation;
332 * Populate ltt_event structure before kretprobe registration.
335 ret = lttng_kretprobes_register(event_param->name,
336 event_param->u.kretprobe.symbol_name,
337 event_param->u.kretprobe.offset,
338 event_param->u.kretprobe.addr,
339 event, event_return);
341 kmem_cache_free(event_cache, event_return);
344 /* Take 2 refs on the module: one per event. */
345 ret = try_module_get(event->desc->owner);
347 ret = try_module_get(event->desc->owner);
349 ret = _ltt_event_metadata_statedump(chan->session, chan,
352 kmem_cache_free(event_cache, event_return);
353 module_put(event->desc->owner);
354 module_put(event->desc->owner);
355 goto statedump_error;
357 list_add(&event_return->list, &chan->session->events);
360 case LTTNG_KERNEL_FUNCTION:
361 ret = lttng_ftrace_register(event_param->name,
362 event_param->u.ftrace.symbol_name,
366 ret = try_module_get(event->desc->owner);
369 case LTTNG_KERNEL_NOOP:
370 event->desc = internal_desc;
377 ret = _ltt_event_metadata_statedump(chan->session, chan, event);
379 goto statedump_error;
380 list_add(&event->list, &chan->session->events);
381 mutex_unlock(&sessions_mutex);
385 /* If a statedump error occurs, events will not be readable. */
387 kmem_cache_free(event_cache, event);
391 mutex_unlock(&sessions_mutex);
396 * Only used internally at session destruction.
398 int _ltt_event_unregister(struct ltt_event *event)
402 switch (event->instrumentation) {
403 case LTTNG_KERNEL_TRACEPOINT:
404 ret = tracepoint_probe_unregister(event->desc->name,
405 event->desc->probe_callback,
410 case LTTNG_KERNEL_KPROBE:
411 lttng_kprobes_unregister(event);
414 case LTTNG_KERNEL_KRETPROBE:
415 lttng_kretprobes_unregister(event);
418 case LTTNG_KERNEL_FUNCTION:
419 lttng_ftrace_unregister(event);
422 case LTTNG_KERNEL_NOOP:
432 * Only used internally at session destruction.
435 void _ltt_event_destroy(struct ltt_event *event)
437 switch (event->instrumentation) {
438 case LTTNG_KERNEL_TRACEPOINT:
439 ltt_event_put(event->desc);
441 case LTTNG_KERNEL_KPROBE:
442 module_put(event->desc->owner);
443 lttng_kprobes_destroy_private(event);
445 case LTTNG_KERNEL_KRETPROBE:
446 module_put(event->desc->owner);
447 lttng_kretprobes_destroy_private(event);
449 case LTTNG_KERNEL_FUNCTION:
450 module_put(event->desc->owner);
451 lttng_ftrace_destroy_private(event);
453 case LTTNG_KERNEL_NOOP:
458 list_del(&event->list);
459 lttng_destroy_context(event->ctx);
460 kmem_cache_free(event_cache, event);
464 * We have exclusive access to our metadata buffer (protected by the
465 * sessions_mutex), so we can do racy operations such as looking for
466 * remaining space left in packet and write, since mutual exclusion
467 * protects us from concurrent writes.
469 int lttng_metadata_printf(struct ltt_session *session,
470 const char *fmt, ...)
472 struct lib_ring_buffer_ctx ctx;
473 struct ltt_channel *chan = session->metadata;
475 int ret = 0, waitret;
476 size_t len, reserve_len, pos;
479 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
482 str = kvasprintf(GFP_KERNEL, fmt, ap);
490 for (pos = 0; pos < len; pos += reserve_len) {
491 reserve_len = min_t(size_t,
492 chan->ops->packet_avail_size(chan->chan),
494 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
497 * We don't care about metadata buffer's records lost
498 * count, because we always retry here. Report error if
499 * we need to bail out after timeout or being
502 waitret = wait_event_interruptible_timeout(*chan->ops->get_writer_buf_wait_queue(chan->chan, -1),
504 ret = chan->ops->event_reserve(&ctx, 0);
505 ret != -ENOBUFS || !ret;
507 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC));
508 if (!waitret || waitret == -ERESTARTSYS || ret) {
509 printk(KERN_WARNING "LTTng: Failure to write metadata to buffers (%s)\n",
510 waitret == -ERESTARTSYS ? "interrupted" :
511 (ret == -ENOBUFS ? "timeout" : "I/O error"));
512 if (waitret == -ERESTARTSYS)
516 chan->ops->event_write(&ctx, &str[pos], reserve_len);
517 chan->ops->event_commit(&ctx);
525 int _ltt_field_statedump(struct ltt_session *session,
526 const struct lttng_event_field *field)
530 switch (field->type.atype) {
532 ret = lttng_metadata_printf(session,
533 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
534 field->type.u.basic.integer.size,
535 field->type.u.basic.integer.alignment,
536 field->type.u.basic.integer.signedness,
537 (field->type.u.basic.integer.encoding == lttng_encode_none)
539 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
542 field->type.u.basic.integer.base,
544 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
546 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
551 ret = lttng_metadata_printf(session,
553 field->type.u.basic.enumeration.name,
558 const struct lttng_basic_type *elem_type;
560 elem_type = &field->type.u.array.elem_type;
561 ret = lttng_metadata_printf(session,
562 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
563 elem_type->u.basic.integer.size,
564 elem_type->u.basic.integer.alignment,
565 elem_type->u.basic.integer.signedness,
566 (elem_type->u.basic.integer.encoding == lttng_encode_none)
568 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
571 elem_type->u.basic.integer.base,
573 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
575 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
577 field->name, field->type.u.array.length);
582 const struct lttng_basic_type *elem_type;
583 const struct lttng_basic_type *length_type;
585 elem_type = &field->type.u.sequence.elem_type;
586 length_type = &field->type.u.sequence.length_type;
587 ret = lttng_metadata_printf(session,
588 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
589 length_type->u.basic.integer.size,
590 (unsigned int) length_type->u.basic.integer.alignment,
591 length_type->u.basic.integer.signedness,
592 (length_type->u.basic.integer.encoding == lttng_encode_none)
594 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
597 length_type->u.basic.integer.base,
599 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
601 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
607 ret = lttng_metadata_printf(session,
608 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
609 elem_type->u.basic.integer.size,
610 (unsigned int) elem_type->u.basic.integer.alignment,
611 elem_type->u.basic.integer.signedness,
612 (elem_type->u.basic.integer.encoding == lttng_encode_none)
614 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
617 elem_type->u.basic.integer.base,
619 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
621 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
629 /* Default encoding is UTF8 */
630 ret = lttng_metadata_printf(session,
632 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
633 " { encoding = ASCII; }" : "",
644 int _ltt_context_metadata_statedump(struct ltt_session *session,
645 struct lttng_ctx *ctx)
652 for (i = 0; i < ctx->nr_fields; i++) {
653 const struct lttng_ctx_field *field = &ctx->fields[i];
655 ret = _ltt_field_statedump(session, &field->event_field);
663 int _ltt_fields_metadata_statedump(struct ltt_session *session,
664 struct ltt_event *event)
666 const struct lttng_event_desc *desc = event->desc;
670 for (i = 0; i < desc->nr_fields; i++) {
671 const struct lttng_event_field *field = &desc->fields[i];
673 ret = _ltt_field_statedump(session, field);
681 int _ltt_event_metadata_statedump(struct ltt_session *session,
682 struct ltt_channel *chan,
683 struct ltt_event *event)
687 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
689 if (chan == session->metadata)
692 ret = lttng_metadata_printf(session,
696 " stream_id = %u;\n",
704 ret = lttng_metadata_printf(session,
705 " context := struct {\n");
709 ret = _ltt_context_metadata_statedump(session, event->ctx);
713 ret = lttng_metadata_printf(session,
719 ret = lttng_metadata_printf(session,
720 " fields := struct {\n"
725 ret = _ltt_fields_metadata_statedump(session, event);
730 * LTTng space reservation can only reserve multiples of the
733 ret = lttng_metadata_printf(session,
739 event->metadata_dumped = 1;
746 int _ltt_channel_metadata_statedump(struct ltt_session *session,
747 struct ltt_channel *chan)
751 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
753 if (chan == session->metadata)
756 WARN_ON_ONCE(!chan->header_type);
757 ret = lttng_metadata_printf(session,
760 " event.header := %s;\n"
761 " packet.context := struct packet_context;\n",
763 chan->header_type == 1 ? "struct event_header_compact" :
764 "struct event_header_large");
769 ret = lttng_metadata_printf(session,
770 " event.context := struct {\n");
774 ret = _ltt_context_metadata_statedump(session, chan->ctx);
778 ret = lttng_metadata_printf(session,
784 ret = lttng_metadata_printf(session,
787 chan->metadata_dumped = 1;
793 int _ltt_stream_packet_context_declare(struct ltt_session *session)
795 return lttng_metadata_printf(session,
796 "struct packet_context {\n"
797 " uint64_t timestamp_begin;\n"
798 " uint64_t timestamp_end;\n"
799 " uint32_t events_discarded;\n"
800 " uint32_t content_size;\n"
801 " uint32_t packet_size;\n"
802 " uint32_t cpu_id;\n"
810 * id 31 is reserved to indicate an extended header.
813 * id: range: 0 - 65534.
814 * id 65535 is reserved to indicate an extended header.
817 int _ltt_event_header_declare(struct ltt_session *session)
819 return lttng_metadata_printf(session,
820 "struct event_header_compact {\n"
821 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
824 " uint27_t timestamp;\n"
828 " uint64_t timestamp;\n"
833 "struct event_header_large {\n"
834 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
837 " uint32_t timestamp;\n"
841 " uint64_t timestamp;\n"
845 ltt_alignof(uint32_t) * CHAR_BIT,
846 ltt_alignof(uint16_t) * CHAR_BIT
851 * Output metadata into this session's metadata buffers.
854 int _ltt_session_metadata_statedump(struct ltt_session *session)
856 unsigned char *uuid_c = session->uuid.b;
857 unsigned char uuid_s[37];
858 struct ltt_channel *chan;
859 struct ltt_event *event;
862 if (!ACCESS_ONCE(session->active))
864 if (session->metadata_dumped)
866 if (!session->metadata) {
867 printk(KERN_WARNING "LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
871 snprintf(uuid_s, sizeof(uuid_s),
872 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
873 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
874 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
875 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
876 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
878 ret = lttng_metadata_printf(session,
879 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
880 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
881 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
882 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
883 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
884 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
890 " byte_order = %s;\n"
891 " packet.header := struct {\n"
893 " uint8_t uuid[16];\n"
894 " uint32_t stream_id;\n"
897 ltt_alignof(uint8_t) * CHAR_BIT,
898 ltt_alignof(uint16_t) * CHAR_BIT,
899 ltt_alignof(uint32_t) * CHAR_BIT,
900 ltt_alignof(uint64_t) * CHAR_BIT,
913 ret = _ltt_stream_packet_context_declare(session);
917 ret = _ltt_event_header_declare(session);
922 list_for_each_entry(chan, &session->chan, list) {
923 ret = _ltt_channel_metadata_statedump(session, chan);
928 list_for_each_entry(event, &session->events, list) {
929 ret = _ltt_event_metadata_statedump(session, event->chan, event);
933 session->metadata_dumped = 1;
939 * ltt_transport_register - LTT transport registration
940 * @transport: transport structure
942 * Registers a transport which can be used as output to extract the data out of
943 * LTTng. The module calling this registration function must ensure that no
944 * trap-inducing code will be executed by the transport functions. E.g.
945 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
946 * is made visible to the transport function. This registration acts as a
947 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
948 * after its registration must it synchronize the TLBs.
950 void ltt_transport_register(struct ltt_transport *transport)
953 * Make sure no page fault can be triggered by the module about to be
954 * registered. We deal with this here so we don't have to call
955 * vmalloc_sync_all() in each module's init.
957 wrapper_vmalloc_sync_all();
959 mutex_lock(&sessions_mutex);
960 list_add_tail(&transport->node, <t_transport_list);
961 mutex_unlock(&sessions_mutex);
963 EXPORT_SYMBOL_GPL(ltt_transport_register);
966 * ltt_transport_unregister - LTT transport unregistration
967 * @transport: transport structure
969 void ltt_transport_unregister(struct ltt_transport *transport)
971 mutex_lock(&sessions_mutex);
972 list_del(&transport->node);
973 mutex_unlock(&sessions_mutex);
975 EXPORT_SYMBOL_GPL(ltt_transport_unregister);
977 static int __init ltt_events_init(void)
981 event_cache = KMEM_CACHE(ltt_event, 0);
984 ret = ltt_debugfs_abi_init();
989 kmem_cache_destroy(event_cache);
993 module_init(ltt_events_init);
995 static void __exit ltt_events_exit(void)
997 struct ltt_session *session, *tmpsession;
999 ltt_debugfs_abi_exit();
1000 list_for_each_entry_safe(session, tmpsession, &sessions, list)
1001 ltt_session_destroy(session);
1002 kmem_cache_destroy(event_cache);
1005 module_exit(ltt_events_exit);
1007 MODULE_LICENSE("GPL and additional rights");
1008 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1009 MODULE_DESCRIPTION("LTTng Events");