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