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