Metadata flush writes data from the cache
[lttng-modules.git] / lttng-events.c
1 /*
2 * lttng-events.c
3 *
4 * Holds LTTng per-session event registry.
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
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"
37
38 #define METADATA_CACHE_DEFAULT_SIZE 4096
39
40 static LIST_HEAD(sessions);
41 static LIST_HEAD(lttng_transport_list);
42 /*
43 * Protect the sessions and metadata caches.
44 */
45 static DEFINE_MUTEX(sessions_mutex);
46 static struct kmem_cache *event_cache;
47
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);
51 static
52 int _lttng_event_metadata_statedump(struct lttng_session *session,
53 struct lttng_channel *chan,
54 struct lttng_event *event);
55 static
56 int _lttng_session_metadata_statedump(struct lttng_session *session);
57 static
58 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
59
60 void synchronize_trace(void)
61 {
62 synchronize_sched();
63 #ifdef CONFIG_PREEMPT_RT
64 synchronize_rcu();
65 #endif
66 }
67
68 struct lttng_session *lttng_session_create(void)
69 {
70 struct lttng_session *session;
71 struct lttng_metadata_cache *metadata_cache;
72
73 mutex_lock(&sessions_mutex);
74 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
75 if (!session)
76 goto err;
77 INIT_LIST_HEAD(&session->chan);
78 INIT_LIST_HEAD(&session->events);
79 uuid_le_gen(&session->uuid);
80
81 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
82 GFP_KERNEL);
83 if (!metadata_cache)
84 goto err_free_session;
85 metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE,
86 GFP_KERNEL);
87 if (!metadata_cache->data)
88 goto err_free_cache;
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);
95 return session;
96
97 err_free_cache:
98 kfree(metadata_cache);
99 err_free_session:
100 kfree(session);
101 err:
102 mutex_unlock(&sessions_mutex);
103 return NULL;
104 }
105
106 void metadata_cache_destroy(struct kref *kref)
107 {
108 struct lttng_metadata_cache *cache =
109 container_of(kref, struct lttng_metadata_cache, refcount);
110 kfree(cache->data);
111 kfree(cache);
112 }
113
114 void lttng_session_destroy(struct lttng_session *session)
115 {
116 struct lttng_channel *chan, *tmpchan;
117 struct lttng_event *event, *tmpevent;
118 struct lttng_metadata_stream *metadata_stream;
119 int ret;
120
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);
125 WARN_ON(ret);
126 }
127 list_for_each_entry(event, &session->events, list) {
128 ret = _lttng_event_unregister(event);
129 WARN_ON(ret);
130 }
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);
137 }
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);
143 kfree(session);
144 }
145
146 int lttng_session_enable(struct lttng_session *session)
147 {
148 int ret = 0;
149 struct lttng_channel *chan;
150
151 mutex_lock(&sessions_mutex);
152 if (session->active) {
153 ret = -EBUSY;
154 goto end;
155 }
156
157 /*
158 * Snapshot the number of events per channel to know the type of header
159 * we need to use.
160 */
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 */
166 else
167 chan->header_type = 2; /* large */
168 }
169
170 ACCESS_ONCE(session->active) = 1;
171 ACCESS_ONCE(session->been_active) = 1;
172 ret = _lttng_session_metadata_statedump(session);
173 if (ret) {
174 ACCESS_ONCE(session->active) = 0;
175 goto end;
176 }
177 ret = lttng_statedump_start(session);
178 if (ret)
179 ACCESS_ONCE(session->active) = 0;
180 end:
181 mutex_unlock(&sessions_mutex);
182 return ret;
183 }
184
185 int lttng_session_disable(struct lttng_session *session)
186 {
187 int ret = 0;
188
189 mutex_lock(&sessions_mutex);
190 if (!session->active) {
191 ret = -EBUSY;
192 goto end;
193 }
194 ACCESS_ONCE(session->active) = 0;
195 end:
196 mutex_unlock(&sessions_mutex);
197 return ret;
198 }
199
200 int lttng_channel_enable(struct lttng_channel *channel)
201 {
202 int old;
203
204 if (channel->channel_type == METADATA_CHANNEL)
205 return -EPERM;
206 old = xchg(&channel->enabled, 1);
207 if (old)
208 return -EEXIST;
209 return 0;
210 }
211
212 int lttng_channel_disable(struct lttng_channel *channel)
213 {
214 int old;
215
216 if (channel->channel_type == METADATA_CHANNEL)
217 return -EPERM;
218 old = xchg(&channel->enabled, 0);
219 if (!old)
220 return -EEXIST;
221 return 0;
222 }
223
224 int lttng_event_enable(struct lttng_event *event)
225 {
226 int old;
227
228 if (event->chan->channel_type == METADATA_CHANNEL)
229 return -EPERM;
230 old = xchg(&event->enabled, 1);
231 if (old)
232 return -EEXIST;
233 return 0;
234 }
235
236 int lttng_event_disable(struct lttng_event *event)
237 {
238 int old;
239
240 if (event->chan->channel_type == METADATA_CHANNEL)
241 return -EPERM;
242 old = xchg(&event->enabled, 0);
243 if (!old)
244 return -EEXIST;
245 return 0;
246 }
247
248 static struct lttng_transport *lttng_transport_find(const char *name)
249 {
250 struct lttng_transport *transport;
251
252 list_for_each_entry(transport, &lttng_transport_list, node) {
253 if (!strcmp(transport->name, name))
254 return transport;
255 }
256 return NULL;
257 }
258
259 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
260 const char *transport_name,
261 void *buf_addr,
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)
266 {
267 struct lttng_channel *chan;
268 struct lttng_transport *transport = NULL;
269
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);
274 if (!transport) {
275 printk(KERN_WARNING "LTTng transport %s not found\n",
276 transport_name);
277 goto notransport;
278 }
279 if (!try_module_get(transport->owner)) {
280 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
281 goto notransport;
282 }
283 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
284 if (!chan)
285 goto nomem;
286 chan->session = session;
287 chan->id = session->free_chan_id++;
288 chan->ops = &transport->ops;
289 /*
290 * Note: the channel creation op already writes into the packet
291 * headers. Therefore the "chan" information used as input
292 * should be already accessible.
293 */
294 chan->chan = transport->ops.channel_create(transport_name,
295 chan, buf_addr, subbuf_size, num_subbuf,
296 switch_timer_interval, read_timer_interval);
297 if (!chan->chan)
298 goto create_error;
299 chan->enabled = 1;
300 chan->transport = transport;
301 chan->channel_type = channel_type;
302 list_add(&chan->list, &session->chan);
303 mutex_unlock(&sessions_mutex);
304 return chan;
305
306 create_error:
307 kfree(chan);
308 nomem:
309 if (transport)
310 module_put(transport->owner);
311 notransport:
312 active:
313 mutex_unlock(&sessions_mutex);
314 return NULL;
315 }
316
317 /*
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.
321 */
322 static
323 void _lttng_channel_destroy(struct lttng_channel *chan)
324 {
325 chan->ops->channel_destroy(chan->chan);
326 module_put(chan->transport->owner);
327 list_del(&chan->list);
328 lttng_destroy_context(chan->ctx);
329 kfree(chan);
330 }
331
332 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
333 {
334 BUG_ON(chan->channel_type != METADATA_CHANNEL);
335
336 /* Protect the metadata cache with the sessions_mutex. */
337 mutex_lock(&sessions_mutex);
338 _lttng_channel_destroy(chan);
339 mutex_unlock(&sessions_mutex);
340 }
341 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
342
343 static
344 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
345 {
346 stream->finalized = 1;
347 wake_up_interruptible(&stream->read_wait);
348 }
349
350 /*
351 * Supports event creation while tracing session is active.
352 */
353 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
354 struct lttng_kernel_event *event_param,
355 void *filter,
356 const struct lttng_event_desc *internal_desc)
357 {
358 struct lttng_event *event;
359 int ret;
360
361 mutex_lock(&sessions_mutex);
362 if (chan->free_event_id == -1U)
363 goto full;
364 /*
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.
367 */
368 list_for_each_entry(event, &chan->session->events, list)
369 if (!strcmp(event->desc->name, event_param->name))
370 goto exist;
371 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
372 if (!event)
373 goto cache_error;
374 event->chan = chan;
375 event->filter = filter;
376 event->id = chan->free_event_id++;
377 event->enabled = 1;
378 event->instrumentation = event_param->instrumentation;
379 /* Populate lttng_event structure before tracepoint registration. */
380 smp_wmb();
381 switch (event_param->instrumentation) {
382 case LTTNG_KERNEL_TRACEPOINT:
383 event->desc = lttng_event_get(event_param->name);
384 if (!event->desc)
385 goto register_error;
386 ret = kabi_2635_tracepoint_probe_register(event_param->name,
387 event->desc->probe_callback,
388 event);
389 if (ret)
390 goto register_error;
391 break;
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,
397 event);
398 if (ret)
399 goto register_error;
400 ret = try_module_get(event->desc->owner);
401 WARN_ON_ONCE(!ret);
402 break;
403 case LTTNG_KERNEL_KRETPROBE:
404 {
405 struct lttng_event *event_return;
406
407 /* kretprobe defines 2 events */
408 event_return =
409 kmem_cache_zalloc(event_cache, GFP_KERNEL);
410 if (!event_return)
411 goto register_error;
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;
417 /*
418 * Populate lttng_event structure before kretprobe registration.
419 */
420 smp_wmb();
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);
426 if (ret) {
427 kmem_cache_free(event_cache, event_return);
428 goto register_error;
429 }
430 /* Take 2 refs on the module: one per event. */
431 ret = try_module_get(event->desc->owner);
432 WARN_ON_ONCE(!ret);
433 ret = try_module_get(event->desc->owner);
434 WARN_ON_ONCE(!ret);
435 ret = _lttng_event_metadata_statedump(chan->session, chan,
436 event_return);
437 if (ret) {
438 kmem_cache_free(event_cache, event_return);
439 module_put(event->desc->owner);
440 module_put(event->desc->owner);
441 goto statedump_error;
442 }
443 list_add(&event_return->list, &chan->session->events);
444 break;
445 }
446 case LTTNG_KERNEL_FUNCTION:
447 ret = lttng_ftrace_register(event_param->name,
448 event_param->u.ftrace.symbol_name,
449 event);
450 if (ret)
451 goto register_error;
452 ret = try_module_get(event->desc->owner);
453 WARN_ON_ONCE(!ret);
454 break;
455 case LTTNG_KERNEL_NOOP:
456 event->desc = internal_desc;
457 if (!event->desc)
458 goto register_error;
459 break;
460 default:
461 WARN_ON_ONCE(1);
462 }
463 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
464 if (ret)
465 goto statedump_error;
466 list_add(&event->list, &chan->session->events);
467 mutex_unlock(&sessions_mutex);
468 return event;
469
470 statedump_error:
471 /* If a statedump error occurs, events will not be readable. */
472 register_error:
473 kmem_cache_free(event_cache, event);
474 cache_error:
475 exist:
476 full:
477 mutex_unlock(&sessions_mutex);
478 return NULL;
479 }
480
481 /*
482 * Only used internally at session destruction.
483 */
484 int _lttng_event_unregister(struct lttng_event *event)
485 {
486 int ret = -EINVAL;
487
488 switch (event->instrumentation) {
489 case LTTNG_KERNEL_TRACEPOINT:
490 ret = kabi_2635_tracepoint_probe_unregister(event->desc->name,
491 event->desc->probe_callback,
492 event);
493 if (ret)
494 return ret;
495 break;
496 case LTTNG_KERNEL_KPROBE:
497 lttng_kprobes_unregister(event);
498 ret = 0;
499 break;
500 case LTTNG_KERNEL_KRETPROBE:
501 lttng_kretprobes_unregister(event);
502 ret = 0;
503 break;
504 case LTTNG_KERNEL_FUNCTION:
505 lttng_ftrace_unregister(event);
506 ret = 0;
507 break;
508 case LTTNG_KERNEL_NOOP:
509 ret = 0;
510 break;
511 default:
512 WARN_ON_ONCE(1);
513 }
514 return ret;
515 }
516
517 /*
518 * Only used internally at session destruction.
519 */
520 static
521 void _lttng_event_destroy(struct lttng_event *event)
522 {
523 switch (event->instrumentation) {
524 case LTTNG_KERNEL_TRACEPOINT:
525 lttng_event_put(event->desc);
526 break;
527 case LTTNG_KERNEL_KPROBE:
528 module_put(event->desc->owner);
529 lttng_kprobes_destroy_private(event);
530 break;
531 case LTTNG_KERNEL_KRETPROBE:
532 module_put(event->desc->owner);
533 lttng_kretprobes_destroy_private(event);
534 break;
535 case LTTNG_KERNEL_FUNCTION:
536 module_put(event->desc->owner);
537 lttng_ftrace_destroy_private(event);
538 break;
539 case LTTNG_KERNEL_NOOP:
540 break;
541 default:
542 WARN_ON_ONCE(1);
543 }
544 list_del(&event->list);
545 lttng_destroy_context(event->ctx);
546 kmem_cache_free(event_cache, event);
547 }
548
549 /*
550 * Serialize at most one packet worth of metadata into a metadata
551 * channel.
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.
556 * Returns the number of bytes written in the channel, 0 if no data
557 * was written and a negative value on error.
558 */
559 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
560 struct channel *chan)
561 {
562 struct lib_ring_buffer_ctx ctx;
563 int ret = 0;
564 size_t len, reserve_len;
565
566 /*
567 * Ensure we support mutiple get_next / put sequences followed
568 * by put_next.
569 */
570 WARN_ON(stream->metadata_in < stream->metadata_out);
571 if (stream->metadata_in != stream->metadata_out)
572 return 0;
573
574 len = stream->metadata_cache->metadata_written -
575 stream->metadata_in;
576 if (!len)
577 return 0;
578 reserve_len = min_t(size_t,
579 stream->transport->ops.packet_avail_size(chan),
580 len);
581 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
582 sizeof(char), -1);
583 /*
584 * If reservation failed, return an error to the caller.
585 */
586 ret = stream->transport->ops.event_reserve(&ctx, 0);
587 if (ret != 0) {
588 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
589 goto end;
590 }
591 stream->transport->ops.event_write(&ctx,
592 stream->metadata_cache->data + stream->metadata_in,
593 reserve_len);
594 stream->transport->ops.event_commit(&ctx);
595 stream->metadata_in += reserve_len;
596 ret = reserve_len;
597
598 end:
599 return ret;
600 }
601
602 /*
603 * Write the metadata to the metadata cache.
604 * Must be called with sessions_mutex held.
605 */
606 int lttng_metadata_printf(struct lttng_session *session,
607 const char *fmt, ...)
608 {
609 char *str;
610 size_t len;
611 va_list ap;
612 struct lttng_metadata_stream *stream;
613
614 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
615
616 va_start(ap, fmt);
617 str = kvasprintf(GFP_KERNEL, fmt, ap);
618 va_end(ap);
619 if (!str)
620 return -ENOMEM;
621
622 len = strlen(str);
623 if (session->metadata_cache->metadata_written + len >
624 session->metadata_cache->cache_alloc) {
625 char *tmp_cache_realloc;
626 unsigned int tmp_cache_alloc_size;
627
628 tmp_cache_alloc_size = max_t(unsigned int,
629 session->metadata_cache->cache_alloc + len,
630 session->metadata_cache->cache_alloc << 1);
631 tmp_cache_realloc = krealloc(session->metadata_cache->data,
632 tmp_cache_alloc_size, GFP_KERNEL);
633 if (!tmp_cache_realloc)
634 goto err;
635 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
636 session->metadata_cache->data = tmp_cache_realloc;
637 }
638 memcpy(session->metadata_cache->data +
639 session->metadata_cache->metadata_written,
640 str, len);
641 session->metadata_cache->metadata_written += len;
642 kfree(str);
643
644 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
645 wake_up_interruptible(&stream->read_wait);
646
647 return 0;
648
649 err:
650 kfree(str);
651 return -ENOMEM;
652 }
653
654 /*
655 * Must be called with sessions_mutex held.
656 */
657 static
658 int _lttng_field_statedump(struct lttng_session *session,
659 const struct lttng_event_field *field)
660 {
661 int ret = 0;
662
663 switch (field->type.atype) {
664 case atype_integer:
665 ret = lttng_metadata_printf(session,
666 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
667 field->type.u.basic.integer.size,
668 field->type.u.basic.integer.alignment,
669 field->type.u.basic.integer.signedness,
670 (field->type.u.basic.integer.encoding == lttng_encode_none)
671 ? "none"
672 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
673 ? "UTF8"
674 : "ASCII",
675 field->type.u.basic.integer.base,
676 #ifdef __BIG_ENDIAN
677 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
678 #else
679 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
680 #endif
681 field->name);
682 break;
683 case atype_enum:
684 ret = lttng_metadata_printf(session,
685 " %s _%s;\n",
686 field->type.u.basic.enumeration.name,
687 field->name);
688 break;
689 case atype_array:
690 {
691 const struct lttng_basic_type *elem_type;
692
693 elem_type = &field->type.u.array.elem_type;
694 ret = lttng_metadata_printf(session,
695 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
696 elem_type->u.basic.integer.size,
697 elem_type->u.basic.integer.alignment,
698 elem_type->u.basic.integer.signedness,
699 (elem_type->u.basic.integer.encoding == lttng_encode_none)
700 ? "none"
701 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
702 ? "UTF8"
703 : "ASCII",
704 elem_type->u.basic.integer.base,
705 #ifdef __BIG_ENDIAN
706 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
707 #else
708 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
709 #endif
710 field->name, field->type.u.array.length);
711 break;
712 }
713 case atype_sequence:
714 {
715 const struct lttng_basic_type *elem_type;
716 const struct lttng_basic_type *length_type;
717
718 elem_type = &field->type.u.sequence.elem_type;
719 length_type = &field->type.u.sequence.length_type;
720 ret = lttng_metadata_printf(session,
721 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
722 length_type->u.basic.integer.size,
723 (unsigned int) length_type->u.basic.integer.alignment,
724 length_type->u.basic.integer.signedness,
725 (length_type->u.basic.integer.encoding == lttng_encode_none)
726 ? "none"
727 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
728 ? "UTF8"
729 : "ASCII"),
730 length_type->u.basic.integer.base,
731 #ifdef __BIG_ENDIAN
732 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
733 #else
734 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
735 #endif
736 field->name);
737 if (ret)
738 return ret;
739
740 ret = lttng_metadata_printf(session,
741 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
742 elem_type->u.basic.integer.size,
743 (unsigned int) elem_type->u.basic.integer.alignment,
744 elem_type->u.basic.integer.signedness,
745 (elem_type->u.basic.integer.encoding == lttng_encode_none)
746 ? "none"
747 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
748 ? "UTF8"
749 : "ASCII"),
750 elem_type->u.basic.integer.base,
751 #ifdef __BIG_ENDIAN
752 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
753 #else
754 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
755 #endif
756 field->name,
757 field->name);
758 break;
759 }
760
761 case atype_string:
762 /* Default encoding is UTF8 */
763 ret = lttng_metadata_printf(session,
764 " string%s _%s;\n",
765 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
766 " { encoding = ASCII; }" : "",
767 field->name);
768 break;
769 default:
770 WARN_ON_ONCE(1);
771 return -EINVAL;
772 }
773 return ret;
774 }
775
776 static
777 int _lttng_context_metadata_statedump(struct lttng_session *session,
778 struct lttng_ctx *ctx)
779 {
780 int ret = 0;
781 int i;
782
783 if (!ctx)
784 return 0;
785 for (i = 0; i < ctx->nr_fields; i++) {
786 const struct lttng_ctx_field *field = &ctx->fields[i];
787
788 ret = _lttng_field_statedump(session, &field->event_field);
789 if (ret)
790 return ret;
791 }
792 return ret;
793 }
794
795 static
796 int _lttng_fields_metadata_statedump(struct lttng_session *session,
797 struct lttng_event *event)
798 {
799 const struct lttng_event_desc *desc = event->desc;
800 int ret = 0;
801 int i;
802
803 for (i = 0; i < desc->nr_fields; i++) {
804 const struct lttng_event_field *field = &desc->fields[i];
805
806 ret = _lttng_field_statedump(session, field);
807 if (ret)
808 return ret;
809 }
810 return ret;
811 }
812
813 /*
814 * Must be called with sessions_mutex held.
815 */
816 static
817 int _lttng_event_metadata_statedump(struct lttng_session *session,
818 struct lttng_channel *chan,
819 struct lttng_event *event)
820 {
821 int ret = 0;
822
823 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
824 return 0;
825 if (chan->channel_type == METADATA_CHANNEL)
826 return 0;
827
828 ret = lttng_metadata_printf(session,
829 "event {\n"
830 " name = %s;\n"
831 " id = %u;\n"
832 " stream_id = %u;\n",
833 event->desc->name,
834 event->id,
835 event->chan->id);
836 if (ret)
837 goto end;
838
839 if (event->ctx) {
840 ret = lttng_metadata_printf(session,
841 " context := struct {\n");
842 if (ret)
843 goto end;
844 }
845 ret = _lttng_context_metadata_statedump(session, event->ctx);
846 if (ret)
847 goto end;
848 if (event->ctx) {
849 ret = lttng_metadata_printf(session,
850 " };\n");
851 if (ret)
852 goto end;
853 }
854
855 ret = lttng_metadata_printf(session,
856 " fields := struct {\n"
857 );
858 if (ret)
859 goto end;
860
861 ret = _lttng_fields_metadata_statedump(session, event);
862 if (ret)
863 goto end;
864
865 /*
866 * LTTng space reservation can only reserve multiples of the
867 * byte size.
868 */
869 ret = lttng_metadata_printf(session,
870 " };\n"
871 "};\n\n");
872 if (ret)
873 goto end;
874
875 event->metadata_dumped = 1;
876 end:
877 return ret;
878
879 }
880
881 /*
882 * Must be called with sessions_mutex held.
883 */
884 static
885 int _lttng_channel_metadata_statedump(struct lttng_session *session,
886 struct lttng_channel *chan)
887 {
888 int ret = 0;
889
890 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
891 return 0;
892
893 if (chan->channel_type == METADATA_CHANNEL)
894 return 0;
895
896 WARN_ON_ONCE(!chan->header_type);
897 ret = lttng_metadata_printf(session,
898 "stream {\n"
899 " id = %u;\n"
900 " event.header := %s;\n"
901 " packet.context := struct packet_context;\n",
902 chan->id,
903 chan->header_type == 1 ? "struct event_header_compact" :
904 "struct event_header_large");
905 if (ret)
906 goto end;
907
908 if (chan->ctx) {
909 ret = lttng_metadata_printf(session,
910 " event.context := struct {\n");
911 if (ret)
912 goto end;
913 }
914 ret = _lttng_context_metadata_statedump(session, chan->ctx);
915 if (ret)
916 goto end;
917 if (chan->ctx) {
918 ret = lttng_metadata_printf(session,
919 " };\n");
920 if (ret)
921 goto end;
922 }
923
924 ret = lttng_metadata_printf(session,
925 "};\n\n");
926
927 chan->metadata_dumped = 1;
928 end:
929 return ret;
930 }
931
932 /*
933 * Must be called with sessions_mutex held.
934 */
935 static
936 int _lttng_stream_packet_context_declare(struct lttng_session *session)
937 {
938 return lttng_metadata_printf(session,
939 "struct packet_context {\n"
940 " uint64_clock_monotonic_t timestamp_begin;\n"
941 " uint64_clock_monotonic_t timestamp_end;\n"
942 " uint64_t content_size;\n"
943 " uint64_t packet_size;\n"
944 " unsigned long events_discarded;\n"
945 " uint32_t cpu_id;\n"
946 "};\n\n"
947 );
948 }
949
950 /*
951 * Compact header:
952 * id: range: 0 - 30.
953 * id 31 is reserved to indicate an extended header.
954 *
955 * Large header:
956 * id: range: 0 - 65534.
957 * id 65535 is reserved to indicate an extended header.
958 *
959 * Must be called with sessions_mutex held.
960 */
961 static
962 int _lttng_event_header_declare(struct lttng_session *session)
963 {
964 return lttng_metadata_printf(session,
965 "struct event_header_compact {\n"
966 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
967 " variant <id> {\n"
968 " struct {\n"
969 " uint27_clock_monotonic_t timestamp;\n"
970 " } compact;\n"
971 " struct {\n"
972 " uint32_t id;\n"
973 " uint64_clock_monotonic_t timestamp;\n"
974 " } extended;\n"
975 " } v;\n"
976 "} align(%u);\n"
977 "\n"
978 "struct event_header_large {\n"
979 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
980 " variant <id> {\n"
981 " struct {\n"
982 " uint32_clock_monotonic_t timestamp;\n"
983 " } compact;\n"
984 " struct {\n"
985 " uint32_t id;\n"
986 " uint64_clock_monotonic_t timestamp;\n"
987 " } extended;\n"
988 " } v;\n"
989 "} align(%u);\n\n",
990 lttng_alignof(uint32_t) * CHAR_BIT,
991 lttng_alignof(uint16_t) * CHAR_BIT
992 );
993 }
994
995 /*
996 * Approximation of NTP time of day to clock monotonic correlation,
997 * taken at start of trace.
998 * Yes, this is only an approximation. Yes, we can (and will) do better
999 * in future versions.
1000 */
1001 static
1002 uint64_t measure_clock_offset(void)
1003 {
1004 uint64_t offset, monotonic[2], realtime;
1005 struct timespec rts = { 0, 0 };
1006 unsigned long flags;
1007
1008 /* Disable interrupts to increase correlation precision. */
1009 local_irq_save(flags);
1010 monotonic[0] = trace_clock_read64();
1011 getnstimeofday(&rts);
1012 monotonic[1] = trace_clock_read64();
1013 local_irq_restore(flags);
1014
1015 offset = (monotonic[0] + monotonic[1]) >> 1;
1016 realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
1017 realtime += rts.tv_nsec;
1018 offset = realtime - offset;
1019 return offset;
1020 }
1021
1022 /*
1023 * Output metadata into this session's metadata buffers.
1024 * Must be called with sessions_mutex held.
1025 */
1026 static
1027 int _lttng_session_metadata_statedump(struct lttng_session *session)
1028 {
1029 unsigned char *uuid_c = session->uuid.b;
1030 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
1031 struct lttng_channel *chan;
1032 struct lttng_event *event;
1033 int ret = 0;
1034
1035 if (!ACCESS_ONCE(session->active))
1036 return 0;
1037 if (session->metadata_dumped)
1038 goto skip_session;
1039
1040 snprintf(uuid_s, sizeof(uuid_s),
1041 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1042 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1043 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1044 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1045 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1046
1047 ret = lttng_metadata_printf(session,
1048 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1049 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1050 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1051 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1052 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1053 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1054 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1055 "\n"
1056 "trace {\n"
1057 " major = %u;\n"
1058 " minor = %u;\n"
1059 " uuid = \"%s\";\n"
1060 " byte_order = %s;\n"
1061 " packet.header := struct {\n"
1062 " uint32_t magic;\n"
1063 " uint8_t uuid[16];\n"
1064 " uint32_t stream_id;\n"
1065 " };\n"
1066 "};\n\n",
1067 lttng_alignof(uint8_t) * CHAR_BIT,
1068 lttng_alignof(uint16_t) * CHAR_BIT,
1069 lttng_alignof(uint32_t) * CHAR_BIT,
1070 lttng_alignof(uint64_t) * CHAR_BIT,
1071 sizeof(unsigned long) * CHAR_BIT,
1072 lttng_alignof(unsigned long) * CHAR_BIT,
1073 CTF_SPEC_MAJOR,
1074 CTF_SPEC_MINOR,
1075 uuid_s,
1076 #ifdef __BIG_ENDIAN
1077 "be"
1078 #else
1079 "le"
1080 #endif
1081 );
1082 if (ret)
1083 goto end;
1084
1085 ret = lttng_metadata_printf(session,
1086 "env {\n"
1087 " hostname = \"%s\";\n"
1088 " domain = \"kernel\";\n"
1089 " sysname = \"%s\";\n"
1090 " kernel_release = \"%s\";\n"
1091 " kernel_version = \"%s\";\n"
1092 " tracer_name = \"lttng-modules\";\n"
1093 " tracer_major = %d;\n"
1094 " tracer_minor = %d;\n"
1095 " tracer_patchlevel = %d;\n"
1096 "};\n\n",
1097 current->nsproxy->uts_ns->name.nodename,
1098 utsname()->sysname,
1099 utsname()->release,
1100 utsname()->version,
1101 LTTNG_MODULES_MAJOR_VERSION,
1102 LTTNG_MODULES_MINOR_VERSION,
1103 LTTNG_MODULES_PATCHLEVEL_VERSION
1104 );
1105 if (ret)
1106 goto end;
1107
1108 ret = lttng_metadata_printf(session,
1109 "clock {\n"
1110 " name = %s;\n",
1111 "monotonic"
1112 );
1113 if (ret)
1114 goto end;
1115
1116 if (!trace_clock_uuid(clock_uuid_s)) {
1117 ret = lttng_metadata_printf(session,
1118 " uuid = \"%s\";\n",
1119 clock_uuid_s
1120 );
1121 if (ret)
1122 goto end;
1123 }
1124
1125 ret = lttng_metadata_printf(session,
1126 " description = \"Monotonic Clock\";\n"
1127 " freq = %llu; /* Frequency, in Hz */\n"
1128 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1129 " offset = %llu;\n"
1130 "};\n\n",
1131 (unsigned long long) trace_clock_freq(),
1132 (unsigned long long) measure_clock_offset()
1133 );
1134 if (ret)
1135 goto end;
1136
1137 ret = lttng_metadata_printf(session,
1138 "typealias integer {\n"
1139 " size = 27; align = 1; signed = false;\n"
1140 " map = clock.monotonic.value;\n"
1141 "} := uint27_clock_monotonic_t;\n"
1142 "\n"
1143 "typealias integer {\n"
1144 " size = 32; align = %u; signed = false;\n"
1145 " map = clock.monotonic.value;\n"
1146 "} := uint32_clock_monotonic_t;\n"
1147 "\n"
1148 "typealias integer {\n"
1149 " size = 64; align = %u; signed = false;\n"
1150 " map = clock.monotonic.value;\n"
1151 "} := uint64_clock_monotonic_t;\n\n",
1152 lttng_alignof(uint32_t) * CHAR_BIT,
1153 lttng_alignof(uint64_t) * CHAR_BIT
1154 );
1155 if (ret)
1156 goto end;
1157
1158 ret = _lttng_stream_packet_context_declare(session);
1159 if (ret)
1160 goto end;
1161
1162 ret = _lttng_event_header_declare(session);
1163 if (ret)
1164 goto end;
1165
1166 skip_session:
1167 list_for_each_entry(chan, &session->chan, list) {
1168 ret = _lttng_channel_metadata_statedump(session, chan);
1169 if (ret)
1170 goto end;
1171 }
1172
1173 list_for_each_entry(event, &session->events, list) {
1174 ret = _lttng_event_metadata_statedump(session, event->chan, event);
1175 if (ret)
1176 goto end;
1177 }
1178 session->metadata_dumped = 1;
1179 end:
1180 return ret;
1181 }
1182
1183 /**
1184 * lttng_transport_register - LTT transport registration
1185 * @transport: transport structure
1186 *
1187 * Registers a transport which can be used as output to extract the data out of
1188 * LTTng. The module calling this registration function must ensure that no
1189 * trap-inducing code will be executed by the transport functions. E.g.
1190 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1191 * is made visible to the transport function. This registration acts as a
1192 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1193 * after its registration must it synchronize the TLBs.
1194 */
1195 void lttng_transport_register(struct lttng_transport *transport)
1196 {
1197 /*
1198 * Make sure no page fault can be triggered by the module about to be
1199 * registered. We deal with this here so we don't have to call
1200 * vmalloc_sync_all() in each module's init.
1201 */
1202 wrapper_vmalloc_sync_all();
1203
1204 mutex_lock(&sessions_mutex);
1205 list_add_tail(&transport->node, &lttng_transport_list);
1206 mutex_unlock(&sessions_mutex);
1207 }
1208 EXPORT_SYMBOL_GPL(lttng_transport_register);
1209
1210 /**
1211 * lttng_transport_unregister - LTT transport unregistration
1212 * @transport: transport structure
1213 */
1214 void lttng_transport_unregister(struct lttng_transport *transport)
1215 {
1216 mutex_lock(&sessions_mutex);
1217 list_del(&transport->node);
1218 mutex_unlock(&sessions_mutex);
1219 }
1220 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
1221
1222 static int __init lttng_events_init(void)
1223 {
1224 int ret;
1225
1226 event_cache = KMEM_CACHE(lttng_event, 0);
1227 if (!event_cache)
1228 return -ENOMEM;
1229 ret = lttng_abi_init();
1230 if (ret)
1231 goto error_abi;
1232 return 0;
1233 error_abi:
1234 kmem_cache_destroy(event_cache);
1235 return ret;
1236 }
1237
1238 module_init(lttng_events_init);
1239
1240 static void __exit lttng_events_exit(void)
1241 {
1242 struct lttng_session *session, *tmpsession;
1243
1244 lttng_abi_exit();
1245 list_for_each_entry_safe(session, tmpsession, &sessions, list)
1246 lttng_session_destroy(session);
1247 kmem_cache_destroy(event_cache);
1248 }
1249
1250 module_exit(lttng_events_exit);
1251
1252 MODULE_LICENSE("GPL and additional rights");
1253 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1254 MODULE_DESCRIPTION("LTTng Events");
This page took 0.058996 seconds and 5 git commands to generate.