4 * Holds LTTng per-session event registry.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/utsname.h>
30 #include "wrapper/uuid.h"
31 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
32 #include "wrapper/random.h"
33 #include "wrapper/tracepoint.h"
34 #include "lttng-events.h"
35 #include "lttng-tracer.h"
36 #include "lttng-abi-old.h"
38 #define METADATA_CACHE_DEFAULT_SIZE 4096
40 static LIST_HEAD(sessions
);
41 static LIST_HEAD(lttng_transport_list
);
43 * Protect the sessions and metadata caches.
45 static DEFINE_MUTEX(sessions_mutex
);
46 static struct kmem_cache
*event_cache
;
48 static void _lttng_event_destroy(struct lttng_event
*event
);
49 static void _lttng_channel_destroy(struct lttng_channel
*chan
);
50 static int _lttng_event_unregister(struct lttng_event
*event
);
52 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
53 struct lttng_channel
*chan
,
54 struct lttng_event
*event
);
56 int _lttng_session_metadata_statedump(struct lttng_session
*session
);
58 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
);
60 void synchronize_trace(void)
63 #ifdef CONFIG_PREEMPT_RT
68 struct lttng_session
*lttng_session_create(void)
70 struct lttng_session
*session
;
71 struct lttng_metadata_cache
*metadata_cache
;
73 mutex_lock(&sessions_mutex
);
74 session
= kzalloc(sizeof(struct lttng_session
), GFP_KERNEL
);
77 INIT_LIST_HEAD(&session
->chan
);
78 INIT_LIST_HEAD(&session
->events
);
79 uuid_le_gen(&session
->uuid
);
81 metadata_cache
= kzalloc(sizeof(struct lttng_metadata_cache
),
84 goto err_free_session
;
85 metadata_cache
->data
= kzalloc(METADATA_CACHE_DEFAULT_SIZE
,
87 if (!metadata_cache
->data
)
89 metadata_cache
->cache_alloc
= METADATA_CACHE_DEFAULT_SIZE
;
90 kref_init(&metadata_cache
->refcount
);
91 session
->metadata_cache
= metadata_cache
;
92 INIT_LIST_HEAD(&metadata_cache
->metadata_stream
);
93 list_add(&session
->list
, &sessions
);
94 mutex_unlock(&sessions_mutex
);
98 kfree(metadata_cache
);
102 mutex_unlock(&sessions_mutex
);
106 void metadata_cache_destroy(struct kref
*kref
)
108 struct lttng_metadata_cache
*cache
=
109 container_of(kref
, struct lttng_metadata_cache
, refcount
);
114 void lttng_session_destroy(struct lttng_session
*session
)
116 struct lttng_channel
*chan
, *tmpchan
;
117 struct lttng_event
*event
, *tmpevent
;
118 struct lttng_metadata_stream
*metadata_stream
;
121 mutex_lock(&sessions_mutex
);
122 ACCESS_ONCE(session
->active
) = 0;
123 list_for_each_entry(chan
, &session
->chan
, list
) {
124 ret
= lttng_syscalls_unregister(chan
);
127 list_for_each_entry(event
, &session
->events
, list
) {
128 ret
= _lttng_event_unregister(event
);
131 synchronize_trace(); /* Wait for in-flight events to complete */
132 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
133 _lttng_event_destroy(event
);
134 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
) {
135 BUG_ON(chan
->channel_type
== METADATA_CHANNEL
);
136 _lttng_channel_destroy(chan
);
138 list_for_each_entry(metadata_stream
, &session
->metadata_cache
->metadata_stream
, list
)
139 _lttng_metadata_channel_hangup(metadata_stream
);
140 kref_put(&session
->metadata_cache
->refcount
, metadata_cache_destroy
);
141 list_del(&session
->list
);
142 mutex_unlock(&sessions_mutex
);
146 int lttng_session_enable(struct lttng_session
*session
)
149 struct lttng_channel
*chan
;
151 mutex_lock(&sessions_mutex
);
152 if (session
->active
) {
158 * Snapshot the number of events per channel to know the type of header
161 list_for_each_entry(chan
, &session
->chan
, list
) {
162 if (chan
->header_type
)
163 continue; /* don't change it if session stop/restart */
164 if (chan
->free_event_id
< 31)
165 chan
->header_type
= 1; /* compact */
167 chan
->header_type
= 2; /* large */
170 ACCESS_ONCE(session
->active
) = 1;
171 ACCESS_ONCE(session
->been_active
) = 1;
172 ret
= _lttng_session_metadata_statedump(session
);
174 ACCESS_ONCE(session
->active
) = 0;
177 ret
= lttng_statedump_start(session
);
179 ACCESS_ONCE(session
->active
) = 0;
181 mutex_unlock(&sessions_mutex
);
185 int lttng_session_disable(struct lttng_session
*session
)
189 mutex_lock(&sessions_mutex
);
190 if (!session
->active
) {
194 ACCESS_ONCE(session
->active
) = 0;
196 mutex_unlock(&sessions_mutex
);
200 int lttng_channel_enable(struct lttng_channel
*channel
)
204 if (channel
->channel_type
== METADATA_CHANNEL
)
206 old
= xchg(&channel
->enabled
, 1);
212 int lttng_channel_disable(struct lttng_channel
*channel
)
216 if (channel
->channel_type
== METADATA_CHANNEL
)
218 old
= xchg(&channel
->enabled
, 0);
224 int lttng_event_enable(struct lttng_event
*event
)
228 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
230 old
= xchg(&event
->enabled
, 1);
236 int lttng_event_disable(struct lttng_event
*event
)
240 if (event
->chan
->channel_type
== METADATA_CHANNEL
)
242 old
= xchg(&event
->enabled
, 0);
248 static struct lttng_transport
*lttng_transport_find(const char *name
)
250 struct lttng_transport
*transport
;
252 list_for_each_entry(transport
, <tng_transport_list
, node
) {
253 if (!strcmp(transport
->name
, name
))
259 struct lttng_channel
*lttng_channel_create(struct lttng_session
*session
,
260 const char *transport_name
,
262 size_t subbuf_size
, size_t num_subbuf
,
263 unsigned int switch_timer_interval
,
264 unsigned int read_timer_interval
,
265 enum channel_type channel_type
)
267 struct lttng_channel
*chan
;
268 struct lttng_transport
*transport
= NULL
;
270 mutex_lock(&sessions_mutex
);
271 if (session
->been_active
&& channel_type
!= METADATA_CHANNEL
)
272 goto active
; /* Refuse to add channel to active session */
273 transport
= lttng_transport_find(transport_name
);
275 printk(KERN_WARNING
"LTTng transport %s not found\n",
279 if (!try_module_get(transport
->owner
)) {
280 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
283 chan
= kzalloc(sizeof(struct lttng_channel
), GFP_KERNEL
);
286 chan
->session
= session
;
287 chan
->id
= session
->free_chan_id
++;
289 * Note: the channel creation op already writes into the packet
290 * headers. Therefore the "chan" information used as input
291 * should be already accessible.
293 chan
->chan
= transport
->ops
.channel_create(transport_name
,
294 chan
, buf_addr
, subbuf_size
, num_subbuf
,
295 switch_timer_interval
, read_timer_interval
);
299 chan
->ops
= &transport
->ops
;
300 chan
->transport
= transport
;
301 chan
->channel_type
= channel_type
;
302 list_add(&chan
->list
, &session
->chan
);
303 mutex_unlock(&sessions_mutex
);
310 module_put(transport
->owner
);
313 mutex_unlock(&sessions_mutex
);
318 * Only used internally at session destruction for per-cpu channels, and
319 * when metadata channel is released.
320 * Needs to be called with sessions mutex held.
323 void _lttng_channel_destroy(struct lttng_channel
*chan
)
325 chan
->ops
->channel_destroy(chan
->chan
);
326 module_put(chan
->transport
->owner
);
327 list_del(&chan
->list
);
328 lttng_destroy_context(chan
->ctx
);
332 void lttng_metadata_channel_destroy(struct lttng_channel
*chan
)
334 BUG_ON(chan
->channel_type
!= METADATA_CHANNEL
);
336 /* Protect the metadata cache with the sessions_mutex. */
337 mutex_lock(&sessions_mutex
);
338 _lttng_channel_destroy(chan
);
339 mutex_unlock(&sessions_mutex
);
341 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy
);
344 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream
*stream
)
346 stream
->finalized
= 1;
347 wake_up_interruptible(&stream
->read_wait
);
351 * Supports event creation while tracing session is active.
353 struct lttng_event
*lttng_event_create(struct lttng_channel
*chan
,
354 struct lttng_kernel_event
*event_param
,
356 const struct lttng_event_desc
*internal_desc
)
358 struct lttng_event
*event
;
361 mutex_lock(&sessions_mutex
);
362 if (chan
->free_event_id
== -1U)
365 * This is O(n^2) (for each event, the loop is called at event
366 * creation). Might require a hash if we have lots of events.
368 list_for_each_entry(event
, &chan
->session
->events
, list
)
369 if (!strcmp(event
->desc
->name
, event_param
->name
))
371 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
375 event
->filter
= filter
;
376 event
->id
= chan
->free_event_id
++;
378 event
->instrumentation
= event_param
->instrumentation
;
379 /* Populate lttng_event structure before tracepoint registration. */
381 switch (event_param
->instrumentation
) {
382 case LTTNG_KERNEL_TRACEPOINT
:
383 event
->desc
= lttng_event_get(event_param
->name
);
386 ret
= kabi_2635_tracepoint_probe_register(event_param
->name
,
387 event
->desc
->probe_callback
,
392 case LTTNG_KERNEL_KPROBE
:
393 ret
= lttng_kprobes_register(event_param
->name
,
394 event_param
->u
.kprobe
.symbol_name
,
395 event_param
->u
.kprobe
.offset
,
396 event_param
->u
.kprobe
.addr
,
400 ret
= try_module_get(event
->desc
->owner
);
403 case LTTNG_KERNEL_KRETPROBE
:
405 struct lttng_event
*event_return
;
407 /* kretprobe defines 2 events */
409 kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
412 event_return
->chan
= chan
;
413 event_return
->filter
= filter
;
414 event_return
->id
= chan
->free_event_id
++;
415 event_return
->enabled
= 1;
416 event_return
->instrumentation
= event_param
->instrumentation
;
418 * Populate lttng_event structure before kretprobe registration.
421 ret
= lttng_kretprobes_register(event_param
->name
,
422 event_param
->u
.kretprobe
.symbol_name
,
423 event_param
->u
.kretprobe
.offset
,
424 event_param
->u
.kretprobe
.addr
,
425 event
, event_return
);
427 kmem_cache_free(event_cache
, event_return
);
430 /* Take 2 refs on the module: one per event. */
431 ret
= try_module_get(event
->desc
->owner
);
433 ret
= try_module_get(event
->desc
->owner
);
435 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
,
438 kmem_cache_free(event_cache
, event_return
);
439 module_put(event
->desc
->owner
);
440 module_put(event
->desc
->owner
);
441 goto statedump_error
;
443 list_add(&event_return
->list
, &chan
->session
->events
);
446 case LTTNG_KERNEL_FUNCTION
:
447 ret
= lttng_ftrace_register(event_param
->name
,
448 event_param
->u
.ftrace
.symbol_name
,
452 ret
= try_module_get(event
->desc
->owner
);
455 case LTTNG_KERNEL_NOOP
:
456 event
->desc
= internal_desc
;
463 ret
= _lttng_event_metadata_statedump(chan
->session
, chan
, event
);
465 goto statedump_error
;
466 list_add(&event
->list
, &chan
->session
->events
);
467 mutex_unlock(&sessions_mutex
);
471 /* If a statedump error occurs, events will not be readable. */
473 kmem_cache_free(event_cache
, event
);
477 mutex_unlock(&sessions_mutex
);
482 * Only used internally at session destruction.
484 int _lttng_event_unregister(struct lttng_event
*event
)
488 switch (event
->instrumentation
) {
489 case LTTNG_KERNEL_TRACEPOINT
:
490 ret
= kabi_2635_tracepoint_probe_unregister(event
->desc
->name
,
491 event
->desc
->probe_callback
,
496 case LTTNG_KERNEL_KPROBE
:
497 lttng_kprobes_unregister(event
);
500 case LTTNG_KERNEL_KRETPROBE
:
501 lttng_kretprobes_unregister(event
);
504 case LTTNG_KERNEL_FUNCTION
:
505 lttng_ftrace_unregister(event
);
508 case LTTNG_KERNEL_NOOP
:
518 * Only used internally at session destruction.
521 void _lttng_event_destroy(struct lttng_event
*event
)
523 switch (event
->instrumentation
) {
524 case LTTNG_KERNEL_TRACEPOINT
:
525 lttng_event_put(event
->desc
);
527 case LTTNG_KERNEL_KPROBE
:
528 module_put(event
->desc
->owner
);
529 lttng_kprobes_destroy_private(event
);
531 case LTTNG_KERNEL_KRETPROBE
:
532 module_put(event
->desc
->owner
);
533 lttng_kretprobes_destroy_private(event
);
535 case LTTNG_KERNEL_FUNCTION
:
536 module_put(event
->desc
->owner
);
537 lttng_ftrace_destroy_private(event
);
539 case LTTNG_KERNEL_NOOP
:
544 list_del(&event
->list
);
545 lttng_destroy_context(event
->ctx
);
546 kmem_cache_free(event_cache
, event
);
550 * Serialize at most one packet worth of metadata into a metadata
552 * We have exclusive access to our metadata buffer (protected by the
553 * sessions_mutex), so we can do racy operations such as looking for
554 * remaining space left in packet and write, since mutual exclusion
555 * protects us from concurrent writes.
557 int lttng_metadata_output_channel(struct lttng_channel
*chan
,
558 struct lttng_metadata_stream
*stream
)
560 struct lib_ring_buffer_ctx ctx
;
562 size_t len
, reserve_len
;
564 len
= stream
->metadata_cache
->metadata_written
-
565 stream
->metadata_cache_read
;
566 reserve_len
= min_t(size_t,
567 chan
->ops
->packet_avail_size(chan
->chan
),
569 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
572 * If reservation failed, return an error to the caller.
574 ret
= chan
->ops
->event_reserve(&ctx
, 0);
576 printk(KERN_WARNING
"LTTng: Metadata event reservation failed\n");
579 chan
->ops
->event_write(&ctx
,
580 stream
->metadata_cache
->data
+ stream
->metadata_cache_read
,
582 chan
->ops
->event_commit(&ctx
);
583 stream
->metadata_cache_read
+= reserve_len
;
591 * Write the metadata to the metadata cache.
592 * Must be called with sessions_mutex held.
594 int lttng_metadata_printf(struct lttng_session
*session
,
595 const char *fmt
, ...)
600 struct lttng_metadata_stream
*stream
;
602 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
605 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
611 if (session
->metadata_cache
->metadata_written
+ len
>
612 session
->metadata_cache
->cache_alloc
) {
613 char *tmp_cache_realloc
;
614 unsigned int tmp_cache_alloc_size
;
616 tmp_cache_alloc_size
= max_t(unsigned int,
617 session
->metadata_cache
->cache_alloc
+ len
,
618 session
->metadata_cache
->cache_alloc
<< 1);
619 tmp_cache_realloc
= krealloc(session
->metadata_cache
->data
,
620 tmp_cache_alloc_size
, GFP_KERNEL
);
621 if (!tmp_cache_realloc
)
623 session
->metadata_cache
->cache_alloc
= tmp_cache_alloc_size
;
624 session
->metadata_cache
->data
= tmp_cache_realloc
;
626 memcpy(session
->metadata_cache
->data
+
627 session
->metadata_cache
->metadata_written
,
629 session
->metadata_cache
->metadata_written
+= len
;
632 list_for_each_entry(stream
, &session
->metadata_cache
->metadata_stream
, list
)
633 wake_up_interruptible(&stream
->read_wait
);
643 * Must be called with sessions_mutex held.
646 int _lttng_field_statedump(struct lttng_session
*session
,
647 const struct lttng_event_field
*field
)
651 switch (field
->type
.atype
) {
653 ret
= lttng_metadata_printf(session
,
654 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
655 field
->type
.u
.basic
.integer
.size
,
656 field
->type
.u
.basic
.integer
.alignment
,
657 field
->type
.u
.basic
.integer
.signedness
,
658 (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
660 : (field
->type
.u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
663 field
->type
.u
.basic
.integer
.base
,
665 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
667 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
672 ret
= lttng_metadata_printf(session
,
674 field
->type
.u
.basic
.enumeration
.name
,
679 const struct lttng_basic_type
*elem_type
;
681 elem_type
= &field
->type
.u
.array
.elem_type
;
682 ret
= lttng_metadata_printf(session
,
683 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
684 elem_type
->u
.basic
.integer
.size
,
685 elem_type
->u
.basic
.integer
.alignment
,
686 elem_type
->u
.basic
.integer
.signedness
,
687 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
689 : (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
692 elem_type
->u
.basic
.integer
.base
,
694 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
696 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
698 field
->name
, field
->type
.u
.array
.length
);
703 const struct lttng_basic_type
*elem_type
;
704 const struct lttng_basic_type
*length_type
;
706 elem_type
= &field
->type
.u
.sequence
.elem_type
;
707 length_type
= &field
->type
.u
.sequence
.length_type
;
708 ret
= lttng_metadata_printf(session
,
709 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
710 length_type
->u
.basic
.integer
.size
,
711 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
712 length_type
->u
.basic
.integer
.signedness
,
713 (length_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
715 : ((length_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
718 length_type
->u
.basic
.integer
.base
,
720 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
722 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
728 ret
= lttng_metadata_printf(session
,
729 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
730 elem_type
->u
.basic
.integer
.size
,
731 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
732 elem_type
->u
.basic
.integer
.signedness
,
733 (elem_type
->u
.basic
.integer
.encoding
== lttng_encode_none
)
735 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_encode_UTF8
)
738 elem_type
->u
.basic
.integer
.base
,
740 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
742 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
750 /* Default encoding is UTF8 */
751 ret
= lttng_metadata_printf(session
,
753 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
754 " { encoding = ASCII; }" : "",
765 int _lttng_context_metadata_statedump(struct lttng_session
*session
,
766 struct lttng_ctx
*ctx
)
773 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
774 const struct lttng_ctx_field
*field
= &ctx
->fields
[i
];
776 ret
= _lttng_field_statedump(session
, &field
->event_field
);
784 int _lttng_fields_metadata_statedump(struct lttng_session
*session
,
785 struct lttng_event
*event
)
787 const struct lttng_event_desc
*desc
= event
->desc
;
791 for (i
= 0; i
< desc
->nr_fields
; i
++) {
792 const struct lttng_event_field
*field
= &desc
->fields
[i
];
794 ret
= _lttng_field_statedump(session
, field
);
802 * Must be called with sessions_mutex held.
805 int _lttng_event_metadata_statedump(struct lttng_session
*session
,
806 struct lttng_channel
*chan
,
807 struct lttng_event
*event
)
811 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
813 if (chan
->channel_type
== METADATA_CHANNEL
)
816 ret
= lttng_metadata_printf(session
,
820 " stream_id = %u;\n",
828 ret
= lttng_metadata_printf(session
,
829 " context := struct {\n");
833 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
837 ret
= lttng_metadata_printf(session
,
843 ret
= lttng_metadata_printf(session
,
844 " fields := struct {\n"
849 ret
= _lttng_fields_metadata_statedump(session
, event
);
854 * LTTng space reservation can only reserve multiples of the
857 ret
= lttng_metadata_printf(session
,
863 event
->metadata_dumped
= 1;
870 * Must be called with sessions_mutex held.
873 int _lttng_channel_metadata_statedump(struct lttng_session
*session
,
874 struct lttng_channel
*chan
)
878 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
881 if (chan
->channel_type
== METADATA_CHANNEL
)
884 WARN_ON_ONCE(!chan
->header_type
);
885 ret
= lttng_metadata_printf(session
,
888 " event.header := %s;\n"
889 " packet.context := struct packet_context;\n",
891 chan
->header_type
== 1 ? "struct event_header_compact" :
892 "struct event_header_large");
897 ret
= lttng_metadata_printf(session
,
898 " event.context := struct {\n");
902 ret
= _lttng_context_metadata_statedump(session
, chan
->ctx
);
906 ret
= lttng_metadata_printf(session
,
912 ret
= lttng_metadata_printf(session
,
915 chan
->metadata_dumped
= 1;
921 * Must be called with sessions_mutex held.
924 int _lttng_stream_packet_context_declare(struct lttng_session
*session
)
926 return lttng_metadata_printf(session
,
927 "struct packet_context {\n"
928 " uint64_clock_monotonic_t timestamp_begin;\n"
929 " uint64_clock_monotonic_t timestamp_end;\n"
930 " uint64_t content_size;\n"
931 " uint64_t packet_size;\n"
932 " unsigned long events_discarded;\n"
933 " uint32_t cpu_id;\n"
941 * id 31 is reserved to indicate an extended header.
944 * id: range: 0 - 65534.
945 * id 65535 is reserved to indicate an extended header.
947 * Must be called with sessions_mutex held.
950 int _lttng_event_header_declare(struct lttng_session
*session
)
952 return lttng_metadata_printf(session
,
953 "struct event_header_compact {\n"
954 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
957 " uint27_clock_monotonic_t timestamp;\n"
961 " uint64_clock_monotonic_t timestamp;\n"
966 "struct event_header_large {\n"
967 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
970 " uint32_clock_monotonic_t timestamp;\n"
974 " uint64_clock_monotonic_t timestamp;\n"
978 lttng_alignof(uint32_t) * CHAR_BIT
,
979 lttng_alignof(uint16_t) * CHAR_BIT
984 * Approximation of NTP time of day to clock monotonic correlation,
985 * taken at start of trace.
986 * Yes, this is only an approximation. Yes, we can (and will) do better
987 * in future versions.
990 uint64_t measure_clock_offset(void)
992 uint64_t offset
, monotonic
[2], realtime
;
993 struct timespec rts
= { 0, 0 };
996 /* Disable interrupts to increase correlation precision. */
997 local_irq_save(flags
);
998 monotonic
[0] = trace_clock_read64();
999 getnstimeofday(&rts
);
1000 monotonic
[1] = trace_clock_read64();
1001 local_irq_restore(flags
);
1003 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
1004 realtime
= (uint64_t) rts
.tv_sec
* NSEC_PER_SEC
;
1005 realtime
+= rts
.tv_nsec
;
1006 offset
= realtime
- offset
;
1011 * Output metadata into this session's metadata buffers.
1012 * Must be called with sessions_mutex held.
1015 int _lttng_session_metadata_statedump(struct lttng_session
*session
)
1017 unsigned char *uuid_c
= session
->uuid
.b
;
1018 unsigned char uuid_s
[37], clock_uuid_s
[BOOT_ID_LEN
];
1019 struct lttng_channel
*chan
;
1020 struct lttng_event
*event
;
1023 if (!ACCESS_ONCE(session
->active
))
1025 if (session
->metadata_dumped
)
1028 snprintf(uuid_s
, sizeof(uuid_s
),
1029 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1030 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
1031 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
1032 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
1033 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
1035 ret
= lttng_metadata_printf(session
,
1036 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1037 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1038 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1039 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1040 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1041 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1042 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1048 " byte_order = %s;\n"
1049 " packet.header := struct {\n"
1050 " uint32_t magic;\n"
1051 " uint8_t uuid[16];\n"
1052 " uint32_t stream_id;\n"
1055 lttng_alignof(uint8_t) * CHAR_BIT
,
1056 lttng_alignof(uint16_t) * CHAR_BIT
,
1057 lttng_alignof(uint32_t) * CHAR_BIT
,
1058 lttng_alignof(uint64_t) * CHAR_BIT
,
1059 sizeof(unsigned long) * CHAR_BIT
,
1060 lttng_alignof(unsigned long) * CHAR_BIT
,
1073 ret
= lttng_metadata_printf(session
,
1075 " hostname = \"%s\";\n"
1076 " domain = \"kernel\";\n"
1077 " sysname = \"%s\";\n"
1078 " kernel_release = \"%s\";\n"
1079 " kernel_version = \"%s\";\n"
1080 " tracer_name = \"lttng-modules\";\n"
1081 " tracer_major = %d;\n"
1082 " tracer_minor = %d;\n"
1083 " tracer_patchlevel = %d;\n"
1085 current
->nsproxy
->uts_ns
->name
.nodename
,
1089 LTTNG_MODULES_MAJOR_VERSION
,
1090 LTTNG_MODULES_MINOR_VERSION
,
1091 LTTNG_MODULES_PATCHLEVEL_VERSION
1096 ret
= lttng_metadata_printf(session
,
1104 if (!trace_clock_uuid(clock_uuid_s
)) {
1105 ret
= lttng_metadata_printf(session
,
1106 " uuid = \"%s\";\n",
1113 ret
= lttng_metadata_printf(session
,
1114 " description = \"Monotonic Clock\";\n"
1115 " freq = %llu; /* Frequency, in Hz */\n"
1116 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1119 (unsigned long long) trace_clock_freq(),
1120 (unsigned long long) measure_clock_offset()
1125 ret
= lttng_metadata_printf(session
,
1126 "typealias integer {\n"
1127 " size = 27; align = 1; signed = false;\n"
1128 " map = clock.monotonic.value;\n"
1129 "} := uint27_clock_monotonic_t;\n"
1131 "typealias integer {\n"
1132 " size = 32; align = %u; signed = false;\n"
1133 " map = clock.monotonic.value;\n"
1134 "} := uint32_clock_monotonic_t;\n"
1136 "typealias integer {\n"
1137 " size = 64; align = %u; signed = false;\n"
1138 " map = clock.monotonic.value;\n"
1139 "} := uint64_clock_monotonic_t;\n\n",
1140 lttng_alignof(uint32_t) * CHAR_BIT
,
1141 lttng_alignof(uint64_t) * CHAR_BIT
1146 ret
= _lttng_stream_packet_context_declare(session
);
1150 ret
= _lttng_event_header_declare(session
);
1155 list_for_each_entry(chan
, &session
->chan
, list
) {
1156 ret
= _lttng_channel_metadata_statedump(session
, chan
);
1161 list_for_each_entry(event
, &session
->events
, list
) {
1162 ret
= _lttng_event_metadata_statedump(session
, event
->chan
, event
);
1166 session
->metadata_dumped
= 1;
1172 * lttng_transport_register - LTT transport registration
1173 * @transport: transport structure
1175 * Registers a transport which can be used as output to extract the data out of
1176 * LTTng. The module calling this registration function must ensure that no
1177 * trap-inducing code will be executed by the transport functions. E.g.
1178 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1179 * is made visible to the transport function. This registration acts as a
1180 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1181 * after its registration must it synchronize the TLBs.
1183 void lttng_transport_register(struct lttng_transport
*transport
)
1186 * Make sure no page fault can be triggered by the module about to be
1187 * registered. We deal with this here so we don't have to call
1188 * vmalloc_sync_all() in each module's init.
1190 wrapper_vmalloc_sync_all();
1192 mutex_lock(&sessions_mutex
);
1193 list_add_tail(&transport
->node
, <tng_transport_list
);
1194 mutex_unlock(&sessions_mutex
);
1196 EXPORT_SYMBOL_GPL(lttng_transport_register
);
1199 * lttng_transport_unregister - LTT transport unregistration
1200 * @transport: transport structure
1202 void lttng_transport_unregister(struct lttng_transport
*transport
)
1204 mutex_lock(&sessions_mutex
);
1205 list_del(&transport
->node
);
1206 mutex_unlock(&sessions_mutex
);
1208 EXPORT_SYMBOL_GPL(lttng_transport_unregister
);
1210 static int __init
lttng_events_init(void)
1214 event_cache
= KMEM_CACHE(lttng_event
, 0);
1217 ret
= lttng_abi_init();
1222 kmem_cache_destroy(event_cache
);
1226 module_init(lttng_events_init
);
1228 static void __exit
lttng_events_exit(void)
1230 struct lttng_session
*session
, *tmpsession
;
1233 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
1234 lttng_session_destroy(session
);
1235 kmem_cache_destroy(event_cache
);
1238 module_exit(lttng_events_exit
);
1240 MODULE_LICENSE("GPL and additional rights");
1241 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1242 MODULE_DESCRIPTION("LTTng Events");