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 <linux/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
);
39 void synchronize_trace(void)
42 #ifdef CONFIG_PREEMPT_RT
47 struct ltt_session
*ltt_session_create(void)
49 struct ltt_session
*session
;
51 mutex_lock(&sessions_mutex
);
52 session
= kzalloc(sizeof(struct ltt_session
), GFP_KERNEL
);
55 INIT_LIST_HEAD(&session
->chan
);
56 INIT_LIST_HEAD(&session
->events
);
57 uuid_le_gen(&session
->uuid
);
58 list_add(&session
->list
, &sessions
);
59 mutex_unlock(&sessions_mutex
);
63 void ltt_session_destroy(struct ltt_session
*session
)
65 struct ltt_channel
*chan
, *tmpchan
;
66 struct ltt_event
*event
, *tmpevent
;
69 mutex_lock(&sessions_mutex
);
70 ACCESS_ONCE(session
->active
) = 0;
71 list_for_each_entry(event
, &session
->events
, list
) {
72 ret
= _ltt_event_unregister(event
);
75 synchronize_trace(); /* Wait for in-flight events to complete */
76 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
77 _ltt_event_destroy(event
);
78 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
79 _ltt_channel_destroy(chan
);
80 list_del(&session
->list
);
81 mutex_unlock(&sessions_mutex
);
85 int ltt_session_enable(struct ltt_session
*session
)
88 struct ltt_channel
*chan
;
90 mutex_lock(&sessions_mutex
);
91 if (session
->active
) {
97 * Snapshot the number of events per channel to know the type of header
100 list_for_each_entry(chan
, &session
->chan
, list
) {
101 if (chan
->header_type
)
102 continue; /* don't change it if session stop/restart */
103 if (chan
->free_event_id
< 31)
104 chan
->header_type
= 1; /* compact */
106 chan
->header_type
= 2; /* large */
109 ACCESS_ONCE(session
->active
) = 1;
110 ACCESS_ONCE(session
->been_active
) = 1;
111 synchronize_trace(); /* Wait for in-flight events to complete */
112 ret
= _ltt_session_metadata_statedump(session
);
114 ACCESS_ONCE(session
->active
) = 0;
115 synchronize_trace(); /* Wait for in-flight events to complete */
118 mutex_unlock(&sessions_mutex
);
122 int ltt_session_disable(struct ltt_session
*session
)
126 mutex_lock(&sessions_mutex
);
127 if (!session
->active
) {
131 ACCESS_ONCE(session
->active
) = 0;
132 synchronize_trace(); /* Wait for in-flight events to complete */
134 mutex_unlock(&sessions_mutex
);
138 int ltt_channel_enable(struct ltt_channel
*channel
)
142 old
= xchg(&channel
->enabled
, 1);
148 int ltt_channel_disable(struct ltt_channel
*channel
)
152 old
= xchg(&channel
->enabled
, 0);
158 int ltt_event_enable(struct ltt_event
*event
)
162 old
= xchg(&event
->enabled
, 1);
168 int ltt_event_disable(struct ltt_event
*event
)
172 old
= xchg(&event
->enabled
, 0);
178 static struct ltt_transport
*ltt_transport_find(const char *name
)
180 struct ltt_transport
*transport
;
182 list_for_each_entry(transport
, <t_transport_list
, node
) {
183 if (!strcmp(transport
->name
, name
))
189 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
190 const char *transport_name
,
192 size_t subbuf_size
, size_t num_subbuf
,
193 unsigned int switch_timer_interval
,
194 unsigned int read_timer_interval
)
196 struct ltt_channel
*chan
;
197 struct ltt_transport
*transport
;
199 mutex_lock(&sessions_mutex
);
200 if (session
->been_active
)
201 goto active
; /* Refuse to add channel to active session */
202 transport
= ltt_transport_find(transport_name
);
204 printk(KERN_WARNING
"LTTng transport %s not found\n",
208 chan
= kzalloc(sizeof(struct ltt_channel
), GFP_KERNEL
);
211 chan
->session
= session
;
212 chan
->id
= session
->free_chan_id
++;
214 * Note: the channel creation op already writes into the packet
215 * headers. Therefore the "chan" information used as input
216 * should be already accessible.
218 chan
->chan
= transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
219 subbuf_size
, num_subbuf
, switch_timer_interval
,
220 read_timer_interval
);
224 chan
->ops
= &transport
->ops
;
225 list_add(&chan
->list
, &session
->chan
);
226 mutex_unlock(&sessions_mutex
);
234 mutex_unlock(&sessions_mutex
);
239 * Only used internally at session destruction.
242 void _ltt_channel_destroy(struct ltt_channel
*chan
)
244 chan
->ops
->channel_destroy(chan
->chan
);
245 list_del(&chan
->list
);
246 lttng_destroy_context(chan
->ctx
);
251 * Supports event creation while tracing session is active.
253 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
254 struct lttng_kernel_event
*event_param
,
257 struct ltt_event
*event
;
260 mutex_lock(&sessions_mutex
);
261 if (chan
->free_event_id
== -1UL)
264 * This is O(n^2) (for each event, the loop is called at event
265 * creation). Might require a hash if we have lots of events.
267 list_for_each_entry(event
, &chan
->session
->events
, list
)
268 if (!strcmp(event
->desc
->name
, event_param
->name
))
270 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
274 event
->filter
= filter
;
275 event
->id
= chan
->free_event_id
++;
277 event
->instrumentation
= event_param
->instrumentation
;
278 /* Populate ltt_event structure before tracepoint registration. */
280 switch (event_param
->instrumentation
) {
281 case LTTNG_KERNEL_TRACEPOINT
:
282 event
->desc
= ltt_event_get(event_param
->name
);
285 ret
= tracepoint_probe_register(event_param
->name
,
286 event
->desc
->probe_callback
,
291 case LTTNG_KERNEL_KPROBE
:
292 ret
= lttng_kprobes_register(event_param
->name
,
293 event_param
->u
.kprobe
.symbol_name
,
294 event_param
->u
.kprobe
.offset
,
295 event_param
->u
.kprobe
.addr
,
299 ret
= try_module_get(event
->desc
->owner
);
302 case LTTNG_KERNEL_FUNCTION
:
303 ret
= lttng_ftrace_register(event_param
->name
,
304 event_param
->u
.ftrace
.symbol_name
,
308 ret
= try_module_get(event
->desc
->owner
);
314 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
316 goto statedump_error
;
317 list_add(&event
->list
, &chan
->session
->events
);
318 mutex_unlock(&sessions_mutex
);
322 WARN_ON_ONCE(tracepoint_probe_unregister(event_param
->name
,
323 event
->desc
->probe_callback
,
325 ltt_event_put(event
->desc
);
327 kmem_cache_free(event_cache
, event
);
331 mutex_unlock(&sessions_mutex
);
336 * Only used internally at session destruction.
338 int _ltt_event_unregister(struct ltt_event
*event
)
342 switch (event
->instrumentation
) {
343 case LTTNG_KERNEL_TRACEPOINT
:
344 ret
= tracepoint_probe_unregister(event
->desc
->name
,
345 event
->desc
->probe_callback
,
350 case LTTNG_KERNEL_KPROBE
:
351 lttng_kprobes_unregister(event
);
354 case LTTNG_KERNEL_FUNCTION
:
355 lttng_ftrace_unregister(event
);
365 * Only used internally at session destruction.
368 void _ltt_event_destroy(struct ltt_event
*event
)
370 switch (event
->instrumentation
) {
371 case LTTNG_KERNEL_TRACEPOINT
:
372 ltt_event_put(event
->desc
);
374 case LTTNG_KERNEL_KPROBE
:
375 module_put(event
->desc
->owner
);
376 lttng_kprobes_destroy_private(event
);
378 case LTTNG_KERNEL_FUNCTION
:
379 module_put(event
->desc
->owner
);
380 lttng_ftrace_destroy_private(event
);
385 list_del(&event
->list
);
386 lttng_destroy_context(event
->ctx
);
387 kmem_cache_free(event_cache
, event
);
391 * We have exclusive access to our metadata buffer (protected by the
392 * sessions_mutex), so we can do racy operations such as looking for
393 * remaining space left in packet and write, since mutual exclusion
394 * protects us from concurrent writes.
396 int lttng_metadata_printf(struct ltt_session
*session
,
397 const char *fmt
, ...)
399 struct lib_ring_buffer_ctx ctx
;
400 struct ltt_channel
*chan
= session
->metadata
;
402 int ret
= 0, waitret
;
403 size_t len
, reserve_len
, pos
;
406 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
409 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
417 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
418 reserve_len
= min_t(size_t,
419 chan
->ops
->packet_avail_size(chan
->chan
),
421 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
424 * We don't care about metadata buffer's records lost
425 * count, because we always retry here. Report error if
426 * we need to bail out after timeout or being
429 waitret
= wait_event_interruptible_timeout(*chan
->ops
->get_reader_wait_queue(chan
->chan
),
431 ret
= chan
->ops
->event_reserve(&ctx
, 0);
432 ret
!= -ENOBUFS
|| !ret
;
434 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC
));
435 if (!waitret
|| waitret
== -ERESTARTSYS
|| ret
) {
436 printk(KERN_WARNING
"LTTng: Failure to write metadata to buffers (%s)\n",
437 waitret
== -ERESTARTSYS
? "interrupted" :
438 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
439 if (waitret
== -ERESTARTSYS
)
443 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
444 chan
->ops
->event_commit(&ctx
);
452 int _ltt_field_statedump(struct ltt_session
*session
,
453 const struct lttng_event_field
*field
)
457 switch (field
->type
.atype
) {
459 ret
= lttng_metadata_printf(session
,
460 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
461 field
->type
.u
.basic
.integer
.size
,
462 field
->type
.u
.basic
.integer
.alignment
,
463 field
->type
.u
.basic
.integer
.signedness
,
464 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
466 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
469 field
->type
.u
.basic
.integer
.base
,
471 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
473 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
478 ret
= lttng_metadata_printf(session
,
480 field
->type
.u
.basic
.enumeration
.name
,
485 const struct lttng_basic_type
*elem_type
;
487 elem_type
= &field
->type
.u
.array
.elem_type
;
488 ret
= lttng_metadata_printf(session
,
489 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
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;" : "",
504 field
->name
, field
->type
.u
.array
.length
);
509 const struct lttng_basic_type
*elem_type
;
510 const struct lttng_basic_type
*length_type
;
512 elem_type
= &field
->type
.u
.sequence
.elem_type
;
513 length_type
= &field
->type
.u
.sequence
.length_type
;
514 ret
= lttng_metadata_printf(session
,
515 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
516 length_type
->u
.basic
.integer
.size
,
517 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
518 length_type
->u
.basic
.integer
.signedness
,
519 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
521 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
524 length_type
->u
.basic
.integer
.base
,
526 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
528 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
534 ret
= lttng_metadata_printf(session
,
535 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
536 elem_type
->u
.basic
.integer
.size
,
537 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
538 elem_type
->u
.basic
.integer
.signedness
,
539 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
541 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
544 elem_type
->u
.basic
.integer
.base
,
546 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
548 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
556 /* Default encoding is UTF8 */
557 ret
= lttng_metadata_printf(session
,
559 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
560 " { encoding = ASCII; }" : "",
571 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
572 struct lttng_ctx
*ctx
)
579 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
580 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
582 ret
= _ltt_field_statedump(session
, &field
->event_field
);
590 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
591 struct ltt_event
*event
)
593 const struct lttng_event_desc
*desc
= event
->desc
;
597 for (i
= 0; i
< desc
->nr_fields
; i
++) {
598 const struct lttng_event_field
*field
= &desc
->fields
[i
];
600 ret
= _ltt_field_statedump(session
, field
);
608 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
609 struct ltt_channel
*chan
,
610 struct ltt_event
*event
)
614 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
616 if (chan
== session
->metadata
)
619 ret
= lttng_metadata_printf(session
,
623 " stream_id = %u;\n",
631 ret
= lttng_metadata_printf(session
,
632 " context := struct {\n");
636 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
640 ret
= lttng_metadata_printf(session
,
646 ret
= lttng_metadata_printf(session
,
647 " fields := struct {\n"
652 ret
= _ltt_fields_metadata_statedump(session
, event
);
657 * LTTng space reservation can only reserve multiples of the
660 ret
= lttng_metadata_printf(session
,
666 event
->metadata_dumped
= 1;
673 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
674 struct ltt_channel
*chan
)
678 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
680 if (chan
== session
->metadata
)
683 WARN_ON_ONCE(!chan
->header_type
);
684 ret
= lttng_metadata_printf(session
,
687 " event.header := %s;\n"
688 " packet.context := struct packet_context;\n",
690 chan
->header_type
== 1 ? "struct event_header_compact" :
691 "struct event_header_large");
696 ret
= lttng_metadata_printf(session
,
697 " event.context := struct {\n");
701 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
705 ret
= lttng_metadata_printf(session
,
711 ret
= lttng_metadata_printf(session
,
714 chan
->metadata_dumped
= 1;
720 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
722 return lttng_metadata_printf(session
,
723 "struct packet_context {\n"
724 " uint64_t timestamp_begin;\n"
725 " uint64_t timestamp_end;\n"
726 " uint32_t events_discarded;\n"
727 " uint32_t content_size;\n"
728 " uint32_t packet_size;\n"
729 " uint32_t cpu_id;\n"
737 * id 31 is reserved to indicate an extended header.
740 * id: range: 0 - 65534.
741 * id 65535 is reserved to indicate an extended header.
744 int _ltt_event_header_declare(struct ltt_session
*session
)
746 return lttng_metadata_printf(session
,
747 "struct event_header_compact {\n"
748 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
751 " uint27_t timestamp;\n"
755 " uint64_t timestamp;\n"
760 "struct event_header_large {\n"
761 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
764 " uint32_t timestamp;\n"
768 " uint64_t timestamp;\n"
772 ltt_alignof(uint32_t) * CHAR_BIT
,
773 ltt_alignof(uint16_t) * CHAR_BIT
778 * Output metadata into this session's metadata buffers.
781 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
783 unsigned char *uuid_c
= session
->uuid
.b
;
784 unsigned char uuid_s
[37];
785 struct ltt_channel
*chan
;
786 struct ltt_event
*event
;
789 if (!ACCESS_ONCE(session
->active
))
791 if (session
->metadata_dumped
)
793 if (!session
->metadata
) {
794 printk(KERN_WARNING
"LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
798 snprintf(uuid_s
, sizeof(uuid_s
),
799 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
800 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
801 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
802 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
803 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
805 ret
= lttng_metadata_printf(session
,
806 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
807 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
808 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
809 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
810 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
811 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
817 " byte_order = %s;\n"
818 " packet.header := struct {\n"
820 " uint8_t uuid[16];\n"
821 " uint32_t stream_id;\n"
824 ltt_alignof(uint8_t) * CHAR_BIT
,
825 ltt_alignof(uint16_t) * CHAR_BIT
,
826 ltt_alignof(uint32_t) * CHAR_BIT
,
827 ltt_alignof(uint64_t) * CHAR_BIT
,
840 ret
= _ltt_stream_packet_context_declare(session
);
844 ret
= _ltt_event_header_declare(session
);
849 list_for_each_entry(chan
, &session
->chan
, list
) {
850 ret
= _ltt_channel_metadata_statedump(session
, chan
);
855 list_for_each_entry(event
, &session
->events
, list
) {
856 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
860 session
->metadata_dumped
= 1;
866 * ltt_transport_register - LTT transport registration
867 * @transport: transport structure
869 * Registers a transport which can be used as output to extract the data out of
870 * LTTng. The module calling this registration function must ensure that no
871 * trap-inducing code will be executed by the transport functions. E.g.
872 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
873 * is made visible to the transport function. This registration acts as a
874 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
875 * after its registration must it synchronize the TLBs.
877 void ltt_transport_register(struct ltt_transport
*transport
)
880 * Make sure no page fault can be triggered by the module about to be
881 * registered. We deal with this here so we don't have to call
882 * vmalloc_sync_all() in each module's init.
884 wrapper_vmalloc_sync_all();
886 mutex_lock(&sessions_mutex
);
887 list_add_tail(&transport
->node
, <t_transport_list
);
888 mutex_unlock(&sessions_mutex
);
890 EXPORT_SYMBOL_GPL(ltt_transport_register
);
893 * ltt_transport_unregister - LTT transport unregistration
894 * @transport: transport structure
896 void ltt_transport_unregister(struct ltt_transport
*transport
)
898 mutex_lock(&sessions_mutex
);
899 list_del(&transport
->node
);
900 mutex_unlock(&sessions_mutex
);
902 EXPORT_SYMBOL_GPL(ltt_transport_unregister
);
904 static int __init
ltt_events_init(void)
908 event_cache
= KMEM_CACHE(ltt_event
, 0);
911 ret
= ltt_debugfs_abi_init();
916 kmem_cache_destroy(event_cache
);
920 module_init(ltt_events_init
);
922 static void __exit
ltt_events_exit(void)
924 struct ltt_session
*session
, *tmpsession
;
926 ltt_debugfs_abi_exit();
927 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
928 ltt_session_destroy(session
);
929 kmem_cache_destroy(event_cache
);
932 module_exit(ltt_events_exit
);
934 MODULE_LICENSE("GPL and additional rights");
935 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
936 MODULE_DESCRIPTION("LTTng Events");