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(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_enable(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 ret
= _ltt_session_metadata_statedump(session
);
111 ACCESS_ONCE(session
->active
) = 0;
113 mutex_unlock(&sessions_mutex
);
117 int ltt_session_disable(struct ltt_session
*session
)
121 mutex_lock(&sessions_mutex
);
122 if (!session
->active
) {
126 ACCESS_ONCE(session
->active
) = 0;
128 mutex_unlock(&sessions_mutex
);
132 int ltt_channel_enable(struct ltt_channel
*channel
)
136 if (channel
== channel
->session
->metadata
)
138 old
= xchg(&channel
->enabled
, 1);
144 int ltt_channel_disable(struct ltt_channel
*channel
)
148 if (channel
== channel
->session
->metadata
)
150 old
= xchg(&channel
->enabled
, 0);
156 int ltt_event_enable(struct ltt_event
*event
)
160 if (event
->chan
== event
->chan
->session
->metadata
)
162 old
= xchg(&event
->enabled
, 1);
168 int ltt_event_disable(struct ltt_event
*event
)
172 if (event
->chan
== event
->chan
->session
->metadata
)
174 old
= xchg(&event
->enabled
, 0);
180 static struct ltt_transport
*ltt_transport_find(const char *name
)
182 struct ltt_transport
*transport
;
184 list_for_each_entry(transport
, <t_transport_list
, node
) {
185 if (!strcmp(transport
->name
, name
))
191 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
192 const char *transport_name
,
194 size_t subbuf_size
, size_t num_subbuf
,
195 unsigned int switch_timer_interval
,
196 unsigned int read_timer_interval
)
198 struct ltt_channel
*chan
;
199 struct ltt_transport
*transport
;
201 mutex_lock(&sessions_mutex
);
202 if (session
->been_active
)
203 goto active
; /* Refuse to add channel to active session */
204 transport
= ltt_transport_find(transport_name
);
206 printk(KERN_WARNING
"LTTng transport %s not found\n",
210 chan
= kzalloc(sizeof(struct ltt_channel
), GFP_KERNEL
);
213 chan
->session
= session
;
214 chan
->id
= session
->free_chan_id
++;
216 * Note: the channel creation op already writes into the packet
217 * headers. Therefore the "chan" information used as input
218 * should be already accessible.
220 chan
->chan
= transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
221 subbuf_size
, num_subbuf
, switch_timer_interval
,
222 read_timer_interval
);
226 chan
->ops
= &transport
->ops
;
227 list_add(&chan
->list
, &session
->chan
);
228 mutex_unlock(&sessions_mutex
);
236 mutex_unlock(&sessions_mutex
);
241 * Only used internally at session destruction.
244 void _ltt_channel_destroy(struct ltt_channel
*chan
)
246 chan
->ops
->channel_destroy(chan
->chan
);
247 list_del(&chan
->list
);
248 lttng_destroy_context(chan
->ctx
);
253 * Supports event creation while tracing session is active.
255 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
256 struct lttng_kernel_event
*event_param
,
259 struct ltt_event
*event
;
262 mutex_lock(&sessions_mutex
);
263 if (chan
->free_event_id
== -1UL)
266 * This is O(n^2) (for each event, the loop is called at event
267 * creation). Might require a hash if we have lots of events.
269 list_for_each_entry(event
, &chan
->session
->events
, list
)
270 if (!strcmp(event
->desc
->name
, event_param
->name
))
272 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
276 event
->filter
= filter
;
277 event
->id
= chan
->free_event_id
++;
279 event
->instrumentation
= event_param
->instrumentation
;
280 /* Populate ltt_event structure before tracepoint registration. */
282 switch (event_param
->instrumentation
) {
283 case LTTNG_KERNEL_TRACEPOINT
:
284 event
->desc
= ltt_event_get(event_param
->name
);
287 ret
= tracepoint_probe_register(event_param
->name
,
288 event
->desc
->probe_callback
,
293 case LTTNG_KERNEL_KPROBE
:
294 ret
= lttng_kprobes_register(event_param
->name
,
295 event_param
->u
.kprobe
.symbol_name
,
296 event_param
->u
.kprobe
.offset
,
297 event_param
->u
.kprobe
.addr
,
301 ret
= try_module_get(event
->desc
->owner
);
304 case LTTNG_KERNEL_FUNCTION
:
305 ret
= lttng_ftrace_register(event_param
->name
,
306 event_param
->u
.ftrace
.symbol_name
,
310 ret
= try_module_get(event
->desc
->owner
);
316 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
318 goto statedump_error
;
319 list_add(&event
->list
, &chan
->session
->events
);
320 mutex_unlock(&sessions_mutex
);
324 WARN_ON_ONCE(tracepoint_probe_unregister(event_param
->name
,
325 event
->desc
->probe_callback
,
327 ltt_event_put(event
->desc
);
329 kmem_cache_free(event_cache
, event
);
333 mutex_unlock(&sessions_mutex
);
338 * Only used internally at session destruction.
340 int _ltt_event_unregister(struct ltt_event
*event
)
344 switch (event
->instrumentation
) {
345 case LTTNG_KERNEL_TRACEPOINT
:
346 ret
= tracepoint_probe_unregister(event
->desc
->name
,
347 event
->desc
->probe_callback
,
352 case LTTNG_KERNEL_KPROBE
:
353 lttng_kprobes_unregister(event
);
356 case LTTNG_KERNEL_FUNCTION
:
357 lttng_ftrace_unregister(event
);
367 * Only used internally at session destruction.
370 void _ltt_event_destroy(struct ltt_event
*event
)
372 switch (event
->instrumentation
) {
373 case LTTNG_KERNEL_TRACEPOINT
:
374 ltt_event_put(event
->desc
);
376 case LTTNG_KERNEL_KPROBE
:
377 module_put(event
->desc
->owner
);
378 lttng_kprobes_destroy_private(event
);
380 case LTTNG_KERNEL_FUNCTION
:
381 module_put(event
->desc
->owner
);
382 lttng_ftrace_destroy_private(event
);
387 list_del(&event
->list
);
388 lttng_destroy_context(event
->ctx
);
389 kmem_cache_free(event_cache
, event
);
393 * We have exclusive access to our metadata buffer (protected by the
394 * sessions_mutex), so we can do racy operations such as looking for
395 * remaining space left in packet and write, since mutual exclusion
396 * protects us from concurrent writes.
398 int lttng_metadata_printf(struct ltt_session
*session
,
399 const char *fmt
, ...)
401 struct lib_ring_buffer_ctx ctx
;
402 struct ltt_channel
*chan
= session
->metadata
;
404 int ret
= 0, waitret
;
405 size_t len
, reserve_len
, pos
;
408 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
411 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
419 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
420 reserve_len
= min_t(size_t,
421 chan
->ops
->packet_avail_size(chan
->chan
),
423 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
426 * We don't care about metadata buffer's records lost
427 * count, because we always retry here. Report error if
428 * we need to bail out after timeout or being
431 waitret
= wait_event_interruptible_timeout(*chan
->ops
->get_reader_wait_queue(chan
->chan
),
433 ret
= chan
->ops
->event_reserve(&ctx
, 0);
434 ret
!= -ENOBUFS
|| !ret
;
436 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC
));
437 if (!waitret
|| waitret
== -ERESTARTSYS
|| ret
) {
438 printk(KERN_WARNING
"LTTng: Failure to write metadata to buffers (%s)\n",
439 waitret
== -ERESTARTSYS
? "interrupted" :
440 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
441 if (waitret
== -ERESTARTSYS
)
445 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
446 chan
->ops
->event_commit(&ctx
);
454 int _ltt_field_statedump(struct ltt_session
*session
,
455 const struct lttng_event_field
*field
)
459 switch (field
->type
.atype
) {
461 ret
= lttng_metadata_printf(session
,
462 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
463 field
->type
.u
.basic
.integer
.size
,
464 field
->type
.u
.basic
.integer
.alignment
,
465 field
->type
.u
.basic
.integer
.signedness
,
466 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
468 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
471 field
->type
.u
.basic
.integer
.base
,
473 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
475 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
480 ret
= lttng_metadata_printf(session
,
482 field
->type
.u
.basic
.enumeration
.name
,
487 const struct lttng_basic_type
*elem_type
;
489 elem_type
= &field
->type
.u
.array
.elem_type
;
490 ret
= lttng_metadata_printf(session
,
491 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
492 elem_type
->u
.basic
.integer
.size
,
493 elem_type
->u
.basic
.integer
.alignment
,
494 elem_type
->u
.basic
.integer
.signedness
,
495 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
497 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
500 elem_type
->u
.basic
.integer
.base
,
502 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
504 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
506 field
->name
, field
->type
.u
.array
.length
);
511 const struct lttng_basic_type
*elem_type
;
512 const struct lttng_basic_type
*length_type
;
514 elem_type
= &field
->type
.u
.sequence
.elem_type
;
515 length_type
= &field
->type
.u
.sequence
.length_type
;
516 ret
= lttng_metadata_printf(session
,
517 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
518 length_type
->u
.basic
.integer
.size
,
519 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
520 length_type
->u
.basic
.integer
.signedness
,
521 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
523 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
526 length_type
->u
.basic
.integer
.base
,
528 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
530 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
536 ret
= lttng_metadata_printf(session
,
537 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
538 elem_type
->u
.basic
.integer
.size
,
539 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
540 elem_type
->u
.basic
.integer
.signedness
,
541 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
543 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
546 elem_type
->u
.basic
.integer
.base
,
548 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
550 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
558 /* Default encoding is UTF8 */
559 ret
= lttng_metadata_printf(session
,
561 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
562 " { encoding = ASCII; }" : "",
573 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
574 struct lttng_ctx
*ctx
)
581 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
582 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
584 ret
= _ltt_field_statedump(session
, &field
->event_field
);
592 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
593 struct ltt_event
*event
)
595 const struct lttng_event_desc
*desc
= event
->desc
;
599 for (i
= 0; i
< desc
->nr_fields
; i
++) {
600 const struct lttng_event_field
*field
= &desc
->fields
[i
];
602 ret
= _ltt_field_statedump(session
, field
);
610 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
611 struct ltt_channel
*chan
,
612 struct ltt_event
*event
)
616 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
618 if (chan
== session
->metadata
)
621 ret
= lttng_metadata_printf(session
,
625 " stream_id = %u;\n",
633 ret
= lttng_metadata_printf(session
,
634 " context := struct {\n");
638 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
642 ret
= lttng_metadata_printf(session
,
648 ret
= lttng_metadata_printf(session
,
649 " fields := struct {\n"
654 ret
= _ltt_fields_metadata_statedump(session
, event
);
659 * LTTng space reservation can only reserve multiples of the
662 ret
= lttng_metadata_printf(session
,
668 event
->metadata_dumped
= 1;
675 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
676 struct ltt_channel
*chan
)
680 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
682 if (chan
== session
->metadata
)
685 WARN_ON_ONCE(!chan
->header_type
);
686 ret
= lttng_metadata_printf(session
,
689 " event.header := %s;\n"
690 " packet.context := struct packet_context;\n",
692 chan
->header_type
== 1 ? "struct event_header_compact" :
693 "struct event_header_large");
698 ret
= lttng_metadata_printf(session
,
699 " event.context := struct {\n");
703 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
707 ret
= lttng_metadata_printf(session
,
713 ret
= lttng_metadata_printf(session
,
716 chan
->metadata_dumped
= 1;
722 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
724 return lttng_metadata_printf(session
,
725 "struct packet_context {\n"
726 " uint64_t timestamp_begin;\n"
727 " uint64_t timestamp_end;\n"
728 " uint32_t events_discarded;\n"
729 " uint32_t content_size;\n"
730 " uint32_t packet_size;\n"
731 " uint32_t cpu_id;\n"
739 * id 31 is reserved to indicate an extended header.
742 * id: range: 0 - 65534.
743 * id 65535 is reserved to indicate an extended header.
746 int _ltt_event_header_declare(struct ltt_session
*session
)
748 return lttng_metadata_printf(session
,
749 "struct event_header_compact {\n"
750 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
753 " uint27_t timestamp;\n"
757 " uint64_t timestamp;\n"
762 "struct event_header_large {\n"
763 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
766 " uint32_t timestamp;\n"
770 " uint64_t timestamp;\n"
774 ltt_alignof(uint32_t) * CHAR_BIT
,
775 ltt_alignof(uint16_t) * CHAR_BIT
780 * Output metadata into this session's metadata buffers.
783 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
785 unsigned char *uuid_c
= session
->uuid
.b
;
786 unsigned char uuid_s
[37];
787 struct ltt_channel
*chan
;
788 struct ltt_event
*event
;
791 if (!ACCESS_ONCE(session
->active
))
793 if (session
->metadata_dumped
)
795 if (!session
->metadata
) {
796 printk(KERN_WARNING
"LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
800 snprintf(uuid_s
, sizeof(uuid_s
),
801 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
802 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
803 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
804 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
805 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
807 ret
= lttng_metadata_printf(session
,
808 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
809 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
810 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
811 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
812 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
813 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
819 " byte_order = %s;\n"
820 " packet.header := struct {\n"
822 " uint8_t uuid[16];\n"
823 " uint32_t stream_id;\n"
826 ltt_alignof(uint8_t) * CHAR_BIT
,
827 ltt_alignof(uint16_t) * CHAR_BIT
,
828 ltt_alignof(uint32_t) * CHAR_BIT
,
829 ltt_alignof(uint64_t) * CHAR_BIT
,
842 ret
= _ltt_stream_packet_context_declare(session
);
846 ret
= _ltt_event_header_declare(session
);
851 list_for_each_entry(chan
, &session
->chan
, list
) {
852 ret
= _ltt_channel_metadata_statedump(session
, chan
);
857 list_for_each_entry(event
, &session
->events
, list
) {
858 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
862 session
->metadata_dumped
= 1;
868 * ltt_transport_register - LTT transport registration
869 * @transport: transport structure
871 * Registers a transport which can be used as output to extract the data out of
872 * LTTng. The module calling this registration function must ensure that no
873 * trap-inducing code will be executed by the transport functions. E.g.
874 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
875 * is made visible to the transport function. This registration acts as a
876 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
877 * after its registration must it synchronize the TLBs.
879 void ltt_transport_register(struct ltt_transport
*transport
)
882 * Make sure no page fault can be triggered by the module about to be
883 * registered. We deal with this here so we don't have to call
884 * vmalloc_sync_all() in each module's init.
886 wrapper_vmalloc_sync_all();
888 mutex_lock(&sessions_mutex
);
889 list_add_tail(&transport
->node
, <t_transport_list
);
890 mutex_unlock(&sessions_mutex
);
892 EXPORT_SYMBOL_GPL(ltt_transport_register
);
895 * ltt_transport_unregister - LTT transport unregistration
896 * @transport: transport structure
898 void ltt_transport_unregister(struct ltt_transport
*transport
)
900 mutex_lock(&sessions_mutex
);
901 list_del(&transport
->node
);
902 mutex_unlock(&sessions_mutex
);
904 EXPORT_SYMBOL_GPL(ltt_transport_unregister
);
906 static int __init
ltt_events_init(void)
910 event_cache
= KMEM_CACHE(ltt_event
, 0);
913 ret
= ltt_debugfs_abi_init();
918 kmem_cache_destroy(event_cache
);
922 module_init(ltt_events_init
);
924 static void __exit
ltt_events_exit(void)
926 struct ltt_session
*session
, *tmpsession
;
928 ltt_debugfs_abi_exit();
929 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
930 ltt_session_destroy(session
);
931 kmem_cache_destroy(event_cache
);
934 module_exit(ltt_events_exit
);
936 MODULE_LICENSE("GPL and additional rights");
937 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
938 MODULE_DESCRIPTION("LTTng Events");