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_start(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_stop(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 static struct ltt_transport
*ltt_transport_find(const char *name
)
140 struct ltt_transport
*transport
;
142 list_for_each_entry(transport
, <t_transport_list
, node
) {
143 if (!strcmp(transport
->name
, name
))
149 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
150 const char *transport_name
,
152 size_t subbuf_size
, size_t num_subbuf
,
153 unsigned int switch_timer_interval
,
154 unsigned int read_timer_interval
)
156 struct ltt_channel
*chan
;
157 struct ltt_transport
*transport
;
159 mutex_lock(&sessions_mutex
);
160 if (session
->been_active
)
161 goto active
; /* Refuse to add channel to active session */
162 transport
= ltt_transport_find(transport_name
);
164 printk(KERN_WARNING
"LTTng transport %s not found\n",
168 chan
= kzalloc(sizeof(struct ltt_channel
), GFP_KERNEL
);
171 chan
->session
= session
;
172 init_waitqueue_head(&chan
->notify_wait
);
173 chan
->id
= session
->free_chan_id
++;
175 * Note: the channel creation op already writes into the packet
176 * headers. Therefore the "chan" information used as input
177 * should be already accessible.
179 chan
->chan
= transport
->ops
.channel_create("[lttng]", chan
, buf_addr
,
180 subbuf_size
, num_subbuf
, switch_timer_interval
,
181 read_timer_interval
);
184 chan
->ops
= &transport
->ops
;
185 list_add(&chan
->list
, &session
->chan
);
186 mutex_unlock(&sessions_mutex
);
194 mutex_unlock(&sessions_mutex
);
199 * Only used internally at session destruction.
202 void _ltt_channel_destroy(struct ltt_channel
*chan
)
204 chan
->ops
->channel_destroy(chan
->chan
);
205 list_del(&chan
->list
);
206 lttng_destroy_context(chan
->ctx
);
211 * Supports event creation while tracing session is active.
213 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
,
214 struct lttng_kernel_event
*event_param
,
217 struct ltt_event
*event
;
220 mutex_lock(&sessions_mutex
);
221 if (chan
->free_event_id
== -1UL)
224 * This is O(n^2) (for each event, the loop is called at event
225 * creation). Might require a hash if we have lots of events.
227 list_for_each_entry(event
, &chan
->session
->events
, list
)
228 if (!strcmp(event
->desc
->name
, event_param
->name
))
230 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
234 event
->filter
= filter
;
235 event
->id
= chan
->free_event_id
++;
236 event
->instrumentation
= event_param
->instrumentation
;
237 /* Populate ltt_event structure before tracepoint registration. */
239 switch (event_param
->instrumentation
) {
240 case LTTNG_KERNEL_TRACEPOINT
:
241 event
->desc
= ltt_event_get(event_param
->name
);
244 ret
= tracepoint_probe_register(event_param
->name
,
245 event
->desc
->probe_callback
,
250 case LTTNG_KERNEL_KPROBE
:
251 ret
= lttng_kprobes_register(event_param
->name
,
252 event_param
->u
.kprobe
.symbol_name
,
253 event_param
->u
.kprobe
.offset
,
254 event_param
->u
.kprobe
.addr
,
258 ret
= try_module_get(event
->desc
->owner
);
261 case LTTNG_KERNEL_FUNCTION
:
262 ret
= lttng_ftrace_register(event_param
->name
,
263 event_param
->u
.ftrace
.symbol_name
,
267 ret
= try_module_get(event
->desc
->owner
);
273 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
275 goto statedump_error
;
276 list_add(&event
->list
, &chan
->session
->events
);
277 mutex_unlock(&sessions_mutex
);
281 WARN_ON_ONCE(tracepoint_probe_unregister(event_param
->name
,
282 event
->desc
->probe_callback
,
284 ltt_event_put(event
->desc
);
286 kmem_cache_free(event_cache
, event
);
290 mutex_unlock(&sessions_mutex
);
295 * Only used internally at session destruction.
297 int _ltt_event_unregister(struct ltt_event
*event
)
301 switch (event
->instrumentation
) {
302 case LTTNG_KERNEL_TRACEPOINT
:
303 ret
= tracepoint_probe_unregister(event
->desc
->name
,
304 event
->desc
->probe_callback
,
309 case LTTNG_KERNEL_KPROBE
:
310 lttng_kprobes_unregister(event
);
313 case LTTNG_KERNEL_FUNCTION
:
314 lttng_ftrace_unregister(event
);
324 * Only used internally at session destruction.
327 void _ltt_event_destroy(struct ltt_event
*event
)
329 switch (event
->instrumentation
) {
330 case LTTNG_KERNEL_TRACEPOINT
:
331 ltt_event_put(event
->desc
);
333 case LTTNG_KERNEL_KPROBE
:
334 module_put(event
->desc
->owner
);
335 lttng_kprobes_destroy_private(event
);
337 case LTTNG_KERNEL_FUNCTION
:
338 module_put(event
->desc
->owner
);
339 lttng_ftrace_destroy_private(event
);
344 list_del(&event
->list
);
345 lttng_destroy_context(event
->ctx
);
346 kmem_cache_free(event_cache
, event
);
350 * We have exclusive access to our metadata buffer (protected by the
351 * sessions_mutex), so we can do racy operations such as looking for
352 * remaining space left in packet and write, since mutual exclusion
353 * protects us from concurrent writes.
355 int lttng_metadata_printf(struct ltt_session
*session
,
356 const char *fmt
, ...)
358 struct lib_ring_buffer_ctx ctx
;
359 struct ltt_channel
*chan
= session
->metadata
;
361 int ret
= 0, waitret
;
362 size_t len
, reserve_len
, pos
;
365 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
368 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
376 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
377 reserve_len
= min_t(size_t,
378 chan
->ops
->packet_avail_size(chan
->chan
),
380 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
383 * We don't care about metadata buffer's records lost
384 * count, because we always retry here. Report error if
385 * we need to bail out after timeout or being
388 waitret
= wait_event_interruptible_timeout(*chan
->ops
->get_reader_wait_queue(chan
),
390 ret
= chan
->ops
->event_reserve(&ctx
, 0);
391 ret
!= -ENOBUFS
|| !ret
;
393 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC
));
394 if (!waitret
|| waitret
== -ERESTARTSYS
|| ret
) {
395 printk(KERN_WARNING
"LTTng: Failure to write metadata to buffers (%s)\n",
396 waitret
== -ERESTARTSYS
? "interrupted" :
397 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
398 if (waitret
== -ERESTARTSYS
)
402 chan
->ops
->event_write(&ctx
, &str
[pos
], len
);
403 chan
->ops
->event_commit(&ctx
);
411 int _ltt_field_statedump(struct ltt_session
*session
,
412 const struct lttng_event_field
*field
)
416 switch (field
->type
.atype
) {
418 ret
= lttng_metadata_printf(session
,
419 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
420 field
->type
.u
.basic
.integer
.size
,
421 field
->type
.u
.basic
.integer
.alignment
,
422 field
->type
.u
.basic
.integer
.signedness
,
423 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
425 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
428 field
->type
.u
.basic
.integer
.base
,
430 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
432 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
437 ret
= lttng_metadata_printf(session
,
439 field
->type
.u
.basic
.enumeration
.name
,
444 const struct lttng_basic_type
*elem_type
;
446 elem_type
= &field
->type
.u
.array
.elem_type
;
447 ret
= lttng_metadata_printf(session
,
448 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
449 elem_type
->u
.basic
.integer
.size
,
450 elem_type
->u
.basic
.integer
.alignment
,
451 elem_type
->u
.basic
.integer
.signedness
,
452 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
454 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
457 elem_type
->u
.basic
.integer
.base
,
459 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
461 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
463 field
->name
, field
->type
.u
.array
.length
);
468 const struct lttng_basic_type
*elem_type
;
469 const struct lttng_basic_type
*length_type
;
471 elem_type
= &field
->type
.u
.sequence
.elem_type
;
472 length_type
= &field
->type
.u
.sequence
.length_type
;
473 ret
= lttng_metadata_printf(session
,
474 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
475 length_type
->u
.basic
.integer
.size
,
476 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
477 length_type
->u
.basic
.integer
.signedness
,
478 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
480 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
483 length_type
->u
.basic
.integer
.base
,
485 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
487 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
493 ret
= lttng_metadata_printf(session
,
494 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
495 elem_type
->u
.basic
.integer
.size
,
496 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
497 elem_type
->u
.basic
.integer
.signedness
,
498 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
500 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
503 elem_type
->u
.basic
.integer
.base
,
505 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
507 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
515 /* Default encoding is UTF8 */
516 ret
= lttng_metadata_printf(session
,
518 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
519 " { encoding = ASCII; }" : "",
530 int _ltt_context_metadata_statedump(struct ltt_session
*session
,
531 struct lttng_ctx
*ctx
)
538 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
539 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
541 ret
= _ltt_field_statedump(session
, &field
->event_field
);
549 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
550 struct ltt_event
*event
)
552 const struct lttng_event_desc
*desc
= event
->desc
;
556 for (i
= 0; i
< desc
->nr_fields
; i
++) {
557 const struct lttng_event_field
*field
= &desc
->fields
[i
];
559 ret
= _ltt_field_statedump(session
, field
);
567 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
568 struct ltt_channel
*chan
,
569 struct ltt_event
*event
)
573 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
575 if (chan
== session
->metadata
)
578 ret
= lttng_metadata_printf(session
,
582 " stream_id = %u;\n",
590 ret
= lttng_metadata_printf(session
,
591 " context := struct {\n");
595 ret
= _ltt_context_metadata_statedump(session
, event
->ctx
);
599 ret
= lttng_metadata_printf(session
,
605 ret
= lttng_metadata_printf(session
,
606 " fields := struct {\n"
611 ret
= _ltt_fields_metadata_statedump(session
, event
);
616 * LTTng space reservation can only reserve multiples of the
619 ret
= lttng_metadata_printf(session
,
625 event
->metadata_dumped
= 1;
632 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
633 struct ltt_channel
*chan
)
637 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
639 if (chan
== session
->metadata
)
642 WARN_ON_ONCE(!chan
->header_type
);
643 ret
= lttng_metadata_printf(session
,
646 " event.header := %s;\n"
647 " packet.context := struct packet_context;\n",
649 chan
->header_type
== 1 ? "struct event_header_compact" :
650 "struct event_header_large");
655 ret
= lttng_metadata_printf(session
,
656 " event.context := struct {\n");
660 ret
= _ltt_context_metadata_statedump(session
, chan
->ctx
);
664 ret
= lttng_metadata_printf(session
,
670 ret
= lttng_metadata_printf(session
,
673 chan
->metadata_dumped
= 1;
679 int _ltt_stream_packet_context_declare(struct ltt_session
*session
)
681 return lttng_metadata_printf(session
,
682 "struct packet_context {\n"
683 " uint64_t timestamp_begin;\n"
684 " uint64_t timestamp_end;\n"
685 " uint32_t events_discarded;\n"
686 " uint32_t content_size;\n"
687 " uint32_t packet_size;\n"
688 " uint32_t cpu_id;\n"
696 * id 31 is reserved to indicate an extended header.
699 * id: range: 0 - 65534.
700 * id 65535 is reserved to indicate an extended header.
703 int _ltt_event_header_declare(struct ltt_session
*session
)
705 return lttng_metadata_printf(session
,
706 "struct event_header_compact {\n"
707 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
710 " uint27_t timestamp;\n"
714 " uint64_t timestamp;\n"
719 "struct event_header_large {\n"
720 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
723 " uint32_t timestamp;\n"
727 " uint64_t timestamp;\n"
731 ltt_alignof(uint32_t) * CHAR_BIT
,
732 ltt_alignof(uint16_t) * CHAR_BIT
737 * Output metadata into this session's metadata buffers.
740 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
742 unsigned char *uuid_c
= session
->uuid
.b
;
743 unsigned char uuid_s
[37];
744 struct ltt_channel
*chan
;
745 struct ltt_event
*event
;
748 if (!ACCESS_ONCE(session
->active
))
750 if (session
->metadata_dumped
)
752 if (!session
->metadata
) {
753 printk(KERN_WARNING
"LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
757 snprintf(uuid_s
, sizeof(uuid_s
),
758 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
759 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
760 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
761 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
762 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
764 ret
= lttng_metadata_printf(session
,
765 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
766 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
767 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
768 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
769 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
770 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
776 " byte_order = %s;\n"
777 " packet.header := struct {\n"
779 " uint8_t uuid[16];\n"
780 " uint32_t stream_id;\n"
783 ltt_alignof(uint8_t) * CHAR_BIT
,
784 ltt_alignof(uint16_t) * CHAR_BIT
,
785 ltt_alignof(uint32_t) * CHAR_BIT
,
786 ltt_alignof(uint64_t) * CHAR_BIT
,
799 ret
= _ltt_stream_packet_context_declare(session
);
803 ret
= _ltt_event_header_declare(session
);
808 list_for_each_entry(chan
, &session
->chan
, list
) {
809 ret
= _ltt_channel_metadata_statedump(session
, chan
);
814 list_for_each_entry(event
, &session
->events
, list
) {
815 ret
= _ltt_event_metadata_statedump(session
, event
->chan
, event
);
819 session
->metadata_dumped
= 1;
825 * ltt_transport_register - LTT transport registration
826 * @transport: transport structure
828 * Registers a transport which can be used as output to extract the data out of
829 * LTTng. The module calling this registration function must ensure that no
830 * trap-inducing code will be executed by the transport functions. E.g.
831 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
832 * is made visible to the transport function. This registration acts as a
833 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
834 * after its registration must it synchronize the TLBs.
836 void ltt_transport_register(struct ltt_transport
*transport
)
839 * Make sure no page fault can be triggered by the module about to be
840 * registered. We deal with this here so we don't have to call
841 * vmalloc_sync_all() in each module's init.
843 wrapper_vmalloc_sync_all();
845 mutex_lock(&sessions_mutex
);
846 list_add_tail(&transport
->node
, <t_transport_list
);
847 mutex_unlock(&sessions_mutex
);
849 EXPORT_SYMBOL_GPL(ltt_transport_register
);
852 * ltt_transport_unregister - LTT transport unregistration
853 * @transport: transport structure
855 void ltt_transport_unregister(struct ltt_transport
*transport
)
857 mutex_lock(&sessions_mutex
);
858 list_del(&transport
->node
);
859 mutex_unlock(&sessions_mutex
);
861 EXPORT_SYMBOL_GPL(ltt_transport_unregister
);
863 static int __init
ltt_events_init(void)
867 event_cache
= KMEM_CACHE(ltt_event
, 0);
870 ret
= ltt_debugfs_abi_init();
875 kmem_cache_destroy(event_cache
);
879 module_init(ltt_events_init
);
881 static void __exit
ltt_events_exit(void)
883 struct ltt_session
*session
, *tmpsession
;
885 ltt_debugfs_abi_exit();
886 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
887 ltt_session_destroy(session
);
888 kmem_cache_destroy(event_cache
);
891 module_exit(ltt_events_exit
);
893 MODULE_LICENSE("GPL and additional rights");
894 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
895 MODULE_DESCRIPTION("LTTng Events");