4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/uuid.h>
16 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
17 #include "ltt-events.h"
18 #include "ltt-tracer.h"
20 static LIST_HEAD(sessions
);
21 static LIST_HEAD(ltt_transport_list
);
22 static DEFINE_MUTEX(sessions_mutex
);
23 static struct kmem_cache
*event_cache
;
25 static void _ltt_event_destroy(struct ltt_event
*event
);
26 static void _ltt_channel_destroy(struct ltt_channel
*chan
);
27 static int _ltt_event_unregister(struct ltt_event
*event
);
29 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
30 struct ltt_channel
*chan
,
31 struct ltt_event
*event
);
33 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(event
, &session
->events
, list
) {
70 ret
= _ltt_event_unregister(event
);
73 synchronize_trace(); /* Wait for in-flight events to complete */
74 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
75 _ltt_event_destroy(event
);
76 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
77 _ltt_channel_destroy(chan
);
78 list_del(&session
->list
);
79 mutex_unlock(&sessions_mutex
);
83 int ltt_session_start(struct ltt_session
*session
)
86 struct ltt_channel
*chan
;
88 mutex_lock(&sessions_mutex
);
89 if (session
->active
) {
95 * Snapshot the number of events per channel to know the type of header
98 list_for_each_entry(chan
, &session
->chan
, list
) {
99 if (chan
->header_type
)
100 continue; /* don't change it if session stop/restart */
101 if (chan
->free_event_id
< 31)
102 chan
->header_type
= 1; /* compact */
104 chan
->header_type
= 2; /* large */
107 ACCESS_ONCE(session
->active
) = 1;
108 ACCESS_ONCE(session
->been_active
) = 1;
109 synchronize_trace(); /* Wait for in-flight events to complete */
110 ret
= _ltt_session_metadata_statedump(session
);
112 ACCESS_ONCE(session
->active
) = 0;
113 synchronize_trace(); /* Wait for in-flight events to complete */
116 mutex_unlock(&sessions_mutex
);
120 int ltt_session_stop(struct ltt_session
*session
)
124 mutex_lock(&sessions_mutex
);
125 if (!session
->active
) {
129 ACCESS_ONCE(session
->active
) = 0;
130 synchronize_trace(); /* Wait for in-flight events to complete */
132 mutex_unlock(&sessions_mutex
);
136 static struct ltt_transport
*ltt_transport_find(const char *name
)
138 struct ltt_transport
*transport
;
140 list_for_each_entry(transport
, <t_transport_list
, node
) {
141 if (!strcmp(transport
->name
, name
))
147 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
148 const char *transport_name
,
150 size_t subbuf_size
, size_t num_subbuf
,
151 unsigned int switch_timer_interval
,
152 unsigned int read_timer_interval
)
154 struct ltt_channel
*chan
;
155 struct ltt_transport
*transport
;
157 mutex_lock(&sessions_mutex
);
158 if (session
->been_active
)
159 goto active
; /* Refuse to add channel to active session */
160 transport
= ltt_transport_find(transport_name
);
162 printk(KERN_WARNING
"LTTng transport %s not found\n",
166 chan
= kzalloc(sizeof(struct ltt_channel
), GFP_KERNEL
);
169 chan
->session
= session
;
170 init_waitqueue_head(&chan
->notify_wait
);
171 chan
->id
= session
->free_chan_id
++;
173 * Note: the channel creation op already writes into the packet
174 * headers. Therefore the "chan" information used as input
175 * should be already accessible.
177 chan
->chan
= transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
178 subbuf_size
, num_subbuf
, switch_timer_interval
,
179 read_timer_interval
);
182 chan
->ops
= &transport
->ops
;
183 list_add(&chan
->list
, &session
->chan
);
184 mutex_unlock(&sessions_mutex
);
192 mutex_unlock(&sessions_mutex
);
197 * Only used internally at session destruction.
200 void _ltt_channel_destroy(struct ltt_channel
*chan
)
202 chan
->ops
->channel_destroy(chan
->chan
);
203 list_del(&chan
->list
);
204 lttng_destroy_context(chan
->ctx
);
209 * Supports event creation while tracing session is active.
211 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
212 struct lttng_kernel_event
*event_param
,
215 struct ltt_event
*event
;
218 mutex_lock(&sessions_mutex
);
219 if (chan
->free_event_id
== -1UL)
222 * This is O(n^2) (for each event, the loop is called at event
223 * creation). Might require a hash if we have lots of events.
225 list_for_each_entry(event
, &chan
->session
->events
, list
)
226 if (!strcmp(event
->desc
->name
, event_param
->name
))
228 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
232 event
->filter
= filter
;
233 event
->id
= chan
->free_event_id
++;
234 event
->instrumentation
= event_param
->instrumentation
;
235 /* Populate ltt_event structure before tracepoint registration. */
237 switch (event_param
->instrumentation
) {
238 case LTTNG_KERNEL_TRACEPOINT
:
239 event
->desc
= ltt_event_get(event_param
->name
);
242 ret
= tracepoint_probe_register(event_param
->name
,
243 event
->desc
->probe_callback
,
248 case LTTNG_KERNEL_KPROBE
:
249 ret
= lttng_kprobes_register(event_param
->name
,
250 event_param
->u
.kprobe
.symbol_name
,
251 event_param
->u
.kprobe
.offset
,
252 event_param
->u
.kprobe
.addr
,
256 ret
= try_module_get(event
->desc
->owner
);
259 case LTTNG_KERNEL_FUNCTION
:
260 ret
= lttng_ftrace_register(event_param
->name
,
261 event_param
->u
.ftrace
.symbol_name
,
265 ret
= try_module_get(event
->desc
->owner
);
271 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
273 goto statedump_error
;
274 list_add(&event
->list
, &chan
->session
->events
);
275 mutex_unlock(&sessions_mutex
);
279 WARN_ON_ONCE(tracepoint_probe_unregister(event_param
->name
,
280 event
->desc
->probe_callback
,
282 ltt_event_put(event
->desc
);
284 kmem_cache_free(event_cache
, event
);
288 mutex_unlock(&sessions_mutex
);
293 * Only used internally at session destruction.
295 int _ltt_event_unregister(struct ltt_event
*event
)
299 switch (event
->instrumentation
) {
300 case LTTNG_KERNEL_TRACEPOINT
:
301 ret
= tracepoint_probe_unregister(event
->desc
->name
,
302 event
->desc
->probe_callback
,
307 case LTTNG_KERNEL_KPROBE
:
308 lttng_kprobes_unregister(event
);
311 case LTTNG_KERNEL_FUNCTION
:
312 lttng_ftrace_unregister(event
);
322 * Only used internally at session destruction.
325 void _ltt_event_destroy(struct ltt_event
*event
)
327 switch (event
->instrumentation
) {
328 case LTTNG_KERNEL_TRACEPOINT
:
329 ltt_event_put(event
->desc
);
331 case LTTNG_KERNEL_KPROBE
:
332 module_put(event
->desc
->owner
);
333 lttng_kprobes_destroy_private(event
);
335 case LTTNG_KERNEL_FUNCTION
:
336 module_put(event
->desc
->owner
);
337 lttng_ftrace_destroy_private(event
);
342 list_del(&event
->list
);
343 lttng_destroy_context(event
->ctx
);
344 kmem_cache_free(event_cache
, event
);
348 * We have exclusive access to our metadata buffer (protected by the
349 * sessions_mutex), so we can do racy operations such as looking for
350 * remaining space left in packet and write, since mutual exclusion
351 * protects us from concurrent writes.
353 int lttng_metadata_printf(struct ltt_session
*session
,
354 const char *fmt
, ...)
356 struct lib_ring_buffer_ctx ctx
;
357 struct ltt_channel
*chan
= session
->metadata
;
359 int ret
= 0, waitret
;
360 size_t len
, reserve_len
, pos
;
363 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
366 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
374 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
375 reserve_len
= min_t(size_t,
376 chan
->ops
->packet_avail_size(chan
->chan
),
378 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
381 * We don't care about metadata buffer's records lost
382 * count, because we always retry here. Report error if
383 * we need to bail out after timeout or being
386 waitret
= wait_event_interruptible_timeout(*chan
->ops
->get_reader_wait_queue(chan
),
388 ret
= chan
->ops
->event_reserve(&ctx
, 0);
389 ret
!= -ENOBUFS
|| !ret
;
391 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC
));
392 if (!waitret
|| waitret
== -ERESTARTSYS
|| ret
) {
393 printk(KERN_WARNING
"LTTng: Failure to write metadata to buffers (%s)\n",
394 waitret
== -ERESTARTSYS
? "interrupted" :
395 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
396 if (waitret
== -ERESTARTSYS
)
400 chan
->ops
->event_write(&ctx
, &str
[pos
], len
);
401 chan
->ops
->event_commit(&ctx
);
409 int _ltt_field_statedump(struct ltt_session
*session
,
410 const struct lttng_event_field
*field
)
414 switch (field
->type
.atype
) {
416 ret
= lttng_metadata_printf(session
,
417 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
418 field
->type
.u
.basic
.integer
.size
,
419 field
->type
.u
.basic
.integer
.alignment
,
420 field
->type
.u
.basic
.integer
.signedness
,
421 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
423 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
426 field
->type
.u
.basic
.integer
.base
,
428 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
430 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
435 ret
= lttng_metadata_printf(session
,
437 field
->type
.u
.basic
.enumeration
.name
,
442 const struct lttng_basic_type
*elem_type
;
444 elem_type
= &field
->type
.u
.array
.elem_type
;
445 ret
= lttng_metadata_printf(session
,
446 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
447 elem_type
->u
.basic
.integer
.size
,
448 elem_type
->u
.basic
.integer
.alignment
,
449 elem_type
->u
.basic
.integer
.signedness
,
450 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
452 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
455 elem_type
->u
.basic
.integer
.base
,
457 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
459 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
461 field
->name
, field
->type
.u
.array
.length
);
466 const struct lttng_basic_type
*elem_type
;
467 const struct lttng_basic_type
*length_type
;
469 elem_type
= &field
->type
.u
.sequence
.elem_type
;
470 length_type
= &field
->type
.u
.sequence
.length_type
;
471 ret
= lttng_metadata_printf(session
,
472 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
473 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
474 length_type
->u
.basic
.integer
.size
,
475 length_type
->u
.basic
.integer
.alignment
,
476 length_type
->u
.basic
.integer
.signedness
,
477 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
479 : (length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
482 length_type
->u
.basic
.integer
.base
,
484 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
486 length_type
->u
.basic
.integer
.reverse_byte_order
487 ? " byte_order = be;" : "",
490 elem_type
->u
.basic
.integer
.size
,
491 elem_type
->u
.basic
.integer
.alignment
,
492 elem_type
->u
.basic
.integer
.signedness
,
493 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
495 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
498 elem_type
->u
.basic
.integer
.base
,
500 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
502 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
511 /* Default encoding is UTF8 */
512 ret
= lttng_metadata_printf(session
,
514 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
515 " { encoding = ASCII; }" : "",
526 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
527 struct lttng_ctx
*ctx
)
534 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
535 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
537 ret
= _ltt_field_statedump(session
, &field
->event_field
);
545 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
546 struct ltt_event
*event
)
548 const struct lttng_event_desc
*desc
= event
->desc
;
552 for (i
= 0; i
< desc
->nr_fields
; i
++) {
553 const struct lttng_event_field
*field
= &desc
->fields
[i
];
555 ret
= _ltt_field_statedump(session
, field
);
563 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
564 struct ltt_channel
*chan
,
565 struct ltt_event
*event
)
569 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
571 if (chan
== session
->metadata
)
574 ret
= lttng_metadata_printf(session
,
579 " context := struct {\n",
586 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
590 ret
= lttng_metadata_printf(session
,
592 " fields := struct {\n"
597 ret
= _ltt_fields_metadata_statedump(session
, event
);
602 * LTTng space reservation can only reserve multiples of the
605 ret
= lttng_metadata_printf(session
,
611 event
->metadata_dumped
= 1;
618 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
619 struct ltt_channel
*chan
)
623 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
625 if (chan
== session
->metadata
)
628 WARN_ON_ONCE(!chan
->header_type
);
629 ret
= lttng_metadata_printf(session
,
632 " event.header := %s;\n"
633 " packet.context := struct packet_context;\n"
634 " event.context := {\n",
636 chan
->header_type
== 1 ? "struct event_header_compact" :
637 "struct event_header_large");
641 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
645 ret
= lttng_metadata_printf(session
,
650 chan
->metadata_dumped
= 1;
656 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
658 return lttng_metadata_printf(session
,
659 "struct packet_context {\n"
660 " uint64_t timestamp_begin;\n"
661 " uint64_t timestamp_end;\n"
662 " uint32_t events_discarded;\n"
663 " uint32_t content_size;\n"
664 " uint32_t packet_size;\n"
665 " uint32_t cpu_id;\n"
673 * id 31 is reserved to indicate an extended header.
676 * id: range: 0 - 65534.
677 * id 65535 is reserved to indicate an extended header.
680 int _ltt_event_header_declare(struct ltt_session
*session
)
682 return lttng_metadata_printf(session
,
683 "struct event_header_compact {\n"
684 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
687 " uint27_t timestamp;\n"
691 " uint64_t timestamp;\n"
696 "struct event_header_large {\n"
697 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
700 " uint32_t timestamp;\n"
704 " uint64_t timestamp;\n"
708 ltt_alignof(uint32_t) * CHAR_BIT
,
709 ltt_alignof(uint16_t) * CHAR_BIT
714 * Output metadata into this session's metadata buffers.
717 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
719 unsigned char *uuid_c
= session
->uuid
.b
;
720 unsigned char uuid_s
[37];
721 struct ltt_channel
*chan
;
722 struct ltt_event
*event
;
725 if (!ACCESS_ONCE(session
->active
))
727 if (session
->metadata_dumped
)
729 if (!session
->metadata
) {
730 printk(KERN_WARNING
"LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
734 snprintf(uuid_s
, sizeof(uuid_s
),
735 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
736 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
737 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
738 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
739 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
741 ret
= lttng_metadata_printf(session
,
742 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
743 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
744 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
745 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
746 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
747 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
753 " byte_order = %s;\n"
754 " packet.header := struct {\n"
756 " uint8_t uuid[16];\n"
757 " uint32_t stream_id;\n"
760 ltt_alignof(uint8_t) * CHAR_BIT
,
761 ltt_alignof(uint16_t) * CHAR_BIT
,
762 ltt_alignof(uint32_t) * CHAR_BIT
,
763 ltt_alignof(uint64_t) * CHAR_BIT
,
776 ret
= _ltt_stream_packet_context_declare(session
);
780 ret
= _ltt_event_header_declare(session
);
785 list_for_each_entry(chan
, &session
->chan
, list
) {
786 ret
= _ltt_channel_metadata_statedump(session
, chan
);
791 list_for_each_entry(event
, &session
->events
, list
) {
792 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
796 session
->metadata_dumped
= 1;
802 * ltt_transport_register - LTT transport registration
803 * @transport: transport structure
805 * Registers a transport which can be used as output to extract the data out of
806 * LTTng. The module calling this registration function must ensure that no
807 * trap-inducing code will be executed by the transport functions. E.g.
808 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
809 * is made visible to the transport function. This registration acts as a
810 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
811 * after its registration must it synchronize the TLBs.
813 void ltt_transport_register(struct ltt_transport
*transport
)
816 * Make sure no page fault can be triggered by the module about to be
817 * registered. We deal with this here so we don't have to call
818 * vmalloc_sync_all() in each module's init.
820 wrapper_vmalloc_sync_all();
822 mutex_lock(&sessions_mutex
);
823 list_add_tail(&transport
->node
, <t_transport_list
);
824 mutex_unlock(&sessions_mutex
);
826 EXPORT_SYMBOL_GPL(ltt_transport_register
);
829 * ltt_transport_unregister - LTT transport unregistration
830 * @transport: transport structure
832 void ltt_transport_unregister(struct ltt_transport
*transport
)
834 mutex_lock(&sessions_mutex
);
835 list_del(&transport
->node
);
836 mutex_unlock(&sessions_mutex
);
838 EXPORT_SYMBOL_GPL(ltt_transport_unregister
);
840 static int __init
ltt_events_init(void)
844 event_cache
= KMEM_CACHE(ltt_event
, 0);
847 ret
= ltt_debugfs_abi_init();
852 kmem_cache_destroy(event_cache
);
856 module_init(ltt_events_init
);
858 static void __exit
ltt_events_exit(void)
860 struct ltt_session
*session
, *tmpsession
;
862 ltt_debugfs_abi_exit();
863 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
864 ltt_session_destroy(session
);
865 kmem_cache_destroy(event_cache
);
868 module_exit(ltt_events_exit
);
870 MODULE_LICENSE("GPL and additional rights");
871 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
872 MODULE_DESCRIPTION("LTTng Events");