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