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