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