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