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