Check for num cpus
[lttng-ust.git] / libust / ltt-events.c
... / ...
CommitLineData
1/*
2 * ltt-events.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng per-session event registry.
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
11#define _GNU_SOURCE
12#include <stdio.h>
13#include <urcu/list.h>
14#include <urcu/hlist.h>
15#include <pthread.h>
16#include <urcu-bp.h>
17#include <urcu/compiler.h>
18#include <urcu/uatomic.h>
19#include <uuid/uuid.h>
20#include <ust/tracepoint.h>
21#include <errno.h>
22#include <sys/shm.h>
23#include <sys/ipc.h>
24#include <ust/lttng-events.h>
25#include <ust/usterr-signal-safe.h>
26#include "ust/core.h"
27#include "ltt-tracer.h"
28#include "ltt-tracer-core.h"
29#include "ust/wait.h"
30#include "../libringbuffer/shm.h"
31
32typedef u32 uint32_t;
33#include <ust/kcompat/jhash.h>
34
35/*
36 * The sessions mutex is the centralized mutex across UST tracing
37 * control and probe registration. All operations within this file are
38 * called by the communication thread, under ust_lock protection.
39 */
40static DEFINE_MUTEX(sessions_mutex);
41
42void ust_lock(void)
43{
44 pthread_mutex_lock(&sessions_mutex);
45}
46
47void ust_unlock(void)
48{
49 pthread_mutex_unlock(&sessions_mutex);
50}
51
52static CDS_LIST_HEAD(sessions);
53static CDS_LIST_HEAD(ltt_transport_list);
54
55/*
56 * Pending probes hash table, containing the registered ltt events for
57 * which tracepoint probes are still missing. Protected by the sessions
58 * mutex.
59 */
60#define PENDING_PROBE_HASH_BITS 6
61#define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
62static struct cds_hlist_head pending_probe_table[PENDING_PROBE_HASH_SIZE];
63
64struct ust_pending_probe {
65 struct ltt_event *event;
66 struct cds_hlist_node node;
67 char name[];
68};
69
70static void _ltt_event_destroy(struct ltt_event *event);
71static void _ltt_channel_destroy(struct ltt_channel *chan);
72static int _ltt_event_unregister(struct ltt_event *event);
73static
74int _ltt_event_metadata_statedump(struct ltt_session *session,
75 struct ltt_channel *chan,
76 struct ltt_event *event);
77static
78int _ltt_session_metadata_statedump(struct ltt_session *session);
79
80/*
81 * called at event creation if probe is missing.
82 * called with session mutex held.
83 */
84static
85int add_pending_probe(struct ltt_event *event, const char *name)
86{
87 struct cds_hlist_head *head;
88 struct ust_pending_probe *e;
89 size_t name_len = strlen(name) + 1;
90 u32 hash = jhash(name, name_len - 1, 0);
91
92 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
93 e = zmalloc(sizeof(struct ust_pending_probe) + name_len);
94 if (!e)
95 return -ENOMEM;
96 memcpy(&e->name[0], name, name_len);
97 cds_hlist_add_head(&e->node, head);
98 e->event = event;
99 event->pending_probe = e;
100 return 0;
101}
102
103/*
104 * remove a pending probe. called when at event teardown and when an
105 * event is fixed (probe is loaded).
106 * called with session mutex held.
107 */
108static
109void remove_pending_probe(struct ust_pending_probe *e)
110{
111 if (!e)
112 return;
113 cds_hlist_del(&e->node);
114 free(e);
115}
116
117/*
118 * Called at library load: connect the probe on the events pending on
119 * probe load.
120 * called with session mutex held.
121 */
122int pending_probe_fix_events(const struct lttng_event_desc *desc)
123{
124 struct cds_hlist_head *head;
125 struct cds_hlist_node *node, *p;
126 struct ust_pending_probe *e;
127 const char *name = desc->name;
128 size_t name_len = strlen(name) + 1;
129 u32 hash = jhash(name, name_len - 1, 0);
130 int ret = 0;
131
132 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
133 cds_hlist_for_each_entry_safe(e, node, p, head, node) {
134 struct ltt_event *event;
135 struct ltt_channel *chan;
136
137 if (strcmp(name, e->name))
138 continue;
139 event = e->event;
140 chan = event->chan;
141 assert(!event->desc);
142 event->desc = desc;
143 event->pending_probe = NULL;
144 remove_pending_probe(e);
145 ret |= __tracepoint_probe_register(name,
146 event->desc->probe_callback,
147 event);
148 ret |= _ltt_event_metadata_statedump(chan->session, chan,
149 event);
150 }
151 return ret;
152}
153
154void synchronize_trace(void)
155{
156 synchronize_rcu();
157}
158
159struct ltt_session *ltt_session_create(void)
160{
161 struct ltt_session *session;
162
163 session = zmalloc(sizeof(struct ltt_session));
164 if (!session)
165 return NULL;
166 CDS_INIT_LIST_HEAD(&session->chan);
167 CDS_INIT_LIST_HEAD(&session->events);
168 uuid_generate(session->uuid);
169 cds_list_add(&session->list, &sessions);
170 return session;
171}
172
173void ltt_session_destroy(struct ltt_session *session)
174{
175 struct ltt_channel *chan, *tmpchan;
176 struct ltt_event *event, *tmpevent;
177 int ret;
178
179 CMM_ACCESS_ONCE(session->active) = 0;
180 cds_list_for_each_entry(event, &session->events, list) {
181 ret = _ltt_event_unregister(event);
182 WARN_ON(ret);
183 }
184 synchronize_trace(); /* Wait for in-flight events to complete */
185 cds_list_for_each_entry_safe(event, tmpevent, &session->events, list)
186 _ltt_event_destroy(event);
187 cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
188 _ltt_channel_destroy(chan);
189 cds_list_del(&session->list);
190 free(session);
191}
192
193int ltt_session_enable(struct ltt_session *session)
194{
195 int ret = 0;
196 struct ltt_channel *chan;
197
198 if (session->active) {
199 ret = -EBUSY;
200 goto end;
201 }
202
203 /*
204 * Snapshot the number of events per channel to know the type of header
205 * we need to use.
206 */
207 cds_list_for_each_entry(chan, &session->chan, list) {
208 if (chan->header_type)
209 continue; /* don't change it if session stop/restart */
210 if (chan->free_event_id < 31)
211 chan->header_type = 1; /* compact */
212 else
213 chan->header_type = 2; /* large */
214 }
215
216 CMM_ACCESS_ONCE(session->active) = 1;
217 CMM_ACCESS_ONCE(session->been_active) = 1;
218 ret = _ltt_session_metadata_statedump(session);
219 if (ret)
220 CMM_ACCESS_ONCE(session->active) = 0;
221end:
222 return ret;
223}
224
225int ltt_session_disable(struct ltt_session *session)
226{
227 int ret = 0;
228
229 if (!session->active) {
230 ret = -EBUSY;
231 goto end;
232 }
233 CMM_ACCESS_ONCE(session->active) = 0;
234end:
235 return ret;
236}
237
238int ltt_channel_enable(struct ltt_channel *channel)
239{
240 int old;
241
242 if (channel == channel->session->metadata)
243 return -EPERM;
244 old = uatomic_xchg(&channel->enabled, 1);
245 if (old)
246 return -EEXIST;
247 return 0;
248}
249
250int ltt_channel_disable(struct ltt_channel *channel)
251{
252 int old;
253
254 if (channel == channel->session->metadata)
255 return -EPERM;
256 old = uatomic_xchg(&channel->enabled, 0);
257 if (!old)
258 return -EEXIST;
259 return 0;
260}
261
262int ltt_event_enable(struct ltt_event *event)
263{
264 int old;
265
266 if (event->chan == event->chan->session->metadata)
267 return -EPERM;
268 old = uatomic_xchg(&event->enabled, 1);
269 if (old)
270 return -EEXIST;
271 return 0;
272}
273
274int ltt_event_disable(struct ltt_event *event)
275{
276 int old;
277
278 if (event->chan == event->chan->session->metadata)
279 return -EPERM;
280 old = uatomic_xchg(&event->enabled, 0);
281 if (!old)
282 return -EEXIST;
283 return 0;
284}
285
286static struct ltt_transport *ltt_transport_find(const char *name)
287{
288 struct ltt_transport *transport;
289
290 cds_list_for_each_entry(transport, &ltt_transport_list, node) {
291 if (!strcmp(transport->name, name))
292 return transport;
293 }
294 return NULL;
295}
296
297struct ltt_channel *ltt_channel_create(struct ltt_session *session,
298 const char *transport_name,
299 void *buf_addr,
300 size_t subbuf_size, size_t num_subbuf,
301 unsigned int switch_timer_interval,
302 unsigned int read_timer_interval,
303 int *shm_fd, int *wait_fd,
304 uint64_t *memory_map_size)
305{
306 struct ltt_channel *chan;
307 struct ltt_transport *transport;
308
309 if (session->been_active)
310 goto active; /* Refuse to add channel to active session */
311 transport = ltt_transport_find(transport_name);
312 if (!transport) {
313 DBG("LTTng transport %s not found\n",
314 transport_name);
315 goto notransport;
316 }
317 chan = zmalloc(sizeof(struct ltt_channel));
318 if (!chan)
319 goto nomem;
320 chan->session = session;
321 chan->id = session->free_chan_id++;
322 /*
323 * Note: the channel creation op already writes into the packet
324 * headers. Therefore the "chan" information used as input
325 * should be already accessible.
326 */
327 transport->ops.channel_create("[lttng]", chan, buf_addr,
328 subbuf_size, num_subbuf, switch_timer_interval,
329 read_timer_interval, shm_fd, wait_fd,
330 memory_map_size);
331 if (!chan->chan)
332 goto create_error;
333 chan->enabled = 1;
334 chan->ops = &transport->ops;
335 cds_list_add(&chan->list, &session->chan);
336 return chan;
337
338create_error:
339 free(chan);
340nomem:
341notransport:
342active:
343 return NULL;
344}
345
346/*
347 * Only used internally at session destruction.
348 */
349static
350void _ltt_channel_destroy(struct ltt_channel *chan)
351{
352 chan->ops->channel_destroy(chan);
353 cds_list_del(&chan->list);
354 lttng_destroy_context(chan->ctx);
355 free(chan);
356}
357
358/*
359 * Supports event creation while tracing session is active.
360 */
361struct ltt_event *ltt_event_create(struct ltt_channel *chan,
362 struct lttng_ust_event *event_param,
363 void *filter)
364{
365 struct ltt_event *event;
366 int ret;
367
368 if (chan->used_event_id == -1UL)
369 goto full;
370 /*
371 * This is O(n^2) (for each event, the loop is called at event
372 * creation). Might require a hash if we have lots of events.
373 */
374 cds_list_for_each_entry(event, &chan->session->events, list)
375 if (event->desc && !strcmp(event->desc->name, event_param->name))
376 goto exist;
377 event = zmalloc(sizeof(struct ltt_event));
378 if (!event)
379 goto cache_error;
380 event->chan = chan;
381 event->filter = filter;
382 /*
383 * used_event_id counts the maximum number of event IDs that can
384 * register if all probes register.
385 */
386 chan->used_event_id++;
387 event->enabled = 1;
388 event->instrumentation = event_param->instrumentation;
389 /* Populate ltt_event structure before tracepoint registration. */
390 cmm_smp_wmb();
391 switch (event_param->instrumentation) {
392 case LTTNG_UST_TRACEPOINT:
393 event->desc = ltt_event_get(event_param->name);
394 if (event->desc) {
395 ret = __tracepoint_probe_register(event_param->name,
396 event->desc->probe_callback,
397 event);
398 if (ret)
399 goto register_error;
400 event->id = chan->free_event_id++;
401 } else {
402 /*
403 * If the probe is not present, event->desc stays NULL,
404 * waiting for the probe to register, and the event->id
405 * stays unallocated.
406 */
407 ret = add_pending_probe(event, event_param->name);
408 if (ret)
409 goto add_pending_error;
410 }
411 break;
412 default:
413 WARN_ON_ONCE(1);
414 }
415 if (event->desc) {
416 ret = _ltt_event_metadata_statedump(chan->session, chan, event);
417 if (ret)
418 goto statedump_error;
419 }
420 cds_list_add(&event->list, &chan->session->events);
421 return event;
422
423statedump_error:
424 if (event->desc) {
425 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param->name,
426 event->desc->probe_callback,
427 event));
428 ltt_event_put(event->desc);
429 }
430add_pending_error:
431register_error:
432 free(event);
433cache_error:
434exist:
435full:
436 return NULL;
437}
438
439/*
440 * Only used internally at session destruction.
441 */
442int _ltt_event_unregister(struct ltt_event *event)
443{
444 int ret = -EINVAL;
445
446 switch (event->instrumentation) {
447 case LTTNG_UST_TRACEPOINT:
448 if (event->desc) {
449 ret = __tracepoint_probe_unregister(event->desc->name,
450 event->desc->probe_callback,
451 event);
452 if (ret)
453 return ret;
454 } else {
455 remove_pending_probe(event->pending_probe);
456 ret = 0;
457 }
458 break;
459 default:
460 WARN_ON_ONCE(1);
461 }
462 return ret;
463}
464
465/*
466 * Only used internally at session destruction.
467 */
468static
469void _ltt_event_destroy(struct ltt_event *event)
470{
471 switch (event->instrumentation) {
472 case LTTNG_UST_TRACEPOINT:
473 if (event->desc) {
474 ltt_event_put(event->desc);
475 }
476 break;
477 default:
478 WARN_ON_ONCE(1);
479 }
480 cds_list_del(&event->list);
481 lttng_destroy_context(event->ctx);
482 free(event);
483}
484
485/*
486 * We have exclusive access to our metadata buffer (protected by the
487 * ust_lock), so we can do racy operations such as looking for
488 * remaining space left in packet and write, since mutual exclusion
489 * protects us from concurrent writes.
490 */
491int lttng_metadata_printf(struct ltt_session *session,
492 const char *fmt, ...)
493{
494 struct lib_ring_buffer_ctx ctx;
495 struct ltt_channel *chan = session->metadata;
496 char *str = NULL;
497 int ret = 0, waitret;
498 size_t len, reserve_len, pos;
499 va_list ap;
500
501 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active));
502
503 va_start(ap, fmt);
504 ret = vasprintf(&str, fmt, ap);
505 va_end(ap);
506 if (ret < 0)
507 return -ENOMEM;
508
509 len = strlen(str);
510 pos = 0;
511
512 for (pos = 0; pos < len; pos += reserve_len) {
513 reserve_len = min_t(size_t,
514 chan->ops->packet_avail_size(chan->chan, chan->handle),
515 len - pos);
516 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
517 sizeof(char), -1, chan->handle);
518 /*
519 * We don't care about metadata buffer's records lost
520 * count, because we always retry here. Report error if
521 * we need to bail out after timeout or being
522 * interrupted.
523 */
524 waitret = wait_cond_interruptible_timeout(
525 ({
526 ret = chan->ops->event_reserve(&ctx, 0);
527 ret != -ENOBUFS || !ret;
528 }),
529 LTTNG_METADATA_TIMEOUT_MSEC);
530 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
531 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
532 waitret == -EINTR ? "interrupted" :
533 (ret == -ENOBUFS ? "timeout" : "I/O error"));
534 if (waitret == -EINTR)
535 ret = waitret;
536 goto end;
537 }
538 chan->ops->event_write(&ctx, &str[pos], reserve_len);
539 chan->ops->event_commit(&ctx);
540 }
541end:
542 free(str);
543 return ret;
544}
545
546static
547int _ltt_field_statedump(struct ltt_session *session,
548 const struct lttng_event_field *field)
549{
550 int ret = 0;
551
552 switch (field->type.atype) {
553 case atype_integer:
554 ret = lttng_metadata_printf(session,
555 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
556 field->type.u.basic.integer.size,
557 field->type.u.basic.integer.alignment,
558 field->type.u.basic.integer.signedness,
559 (field->type.u.basic.integer.encoding == lttng_encode_none)
560 ? "none"
561 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
562 ? "UTF8"
563 : "ASCII",
564 field->type.u.basic.integer.base,
565#ifdef __BIG_ENDIAN
566 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
567#else
568 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
569#endif
570 field->name);
571 break;
572 case atype_float:
573 ret = lttng_metadata_printf(session,
574 " floating_point { exp_dig = %u; mant_dig = %u; align = %u; } _%s;\n",
575 field->type.u.basic._float.exp_dig,
576 field->type.u.basic._float.mant_dig,
577 field->type.u.basic._float.alignment,
578#ifdef __BIG_ENDIAN
579 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
580#else
581 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
582#endif
583 field->name);
584 break;
585 case atype_enum:
586 ret = lttng_metadata_printf(session,
587 " %s %s;\n",
588 field->type.u.basic.enumeration.name,
589 field->name);
590 break;
591 case atype_array:
592 {
593 const struct lttng_basic_type *elem_type;
594
595 elem_type = &field->type.u.array.elem_type;
596 ret = lttng_metadata_printf(session,
597 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
598 elem_type->u.basic.integer.size,
599 elem_type->u.basic.integer.alignment,
600 elem_type->u.basic.integer.signedness,
601 (elem_type->u.basic.integer.encoding == lttng_encode_none)
602 ? "none"
603 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
604 ? "UTF8"
605 : "ASCII",
606 elem_type->u.basic.integer.base,
607#ifdef __BIG_ENDIAN
608 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
609#else
610 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
611#endif
612 field->name, field->type.u.array.length);
613 break;
614 }
615 case atype_sequence:
616 {
617 const struct lttng_basic_type *elem_type;
618 const struct lttng_basic_type *length_type;
619
620 elem_type = &field->type.u.sequence.elem_type;
621 length_type = &field->type.u.sequence.length_type;
622 ret = lttng_metadata_printf(session,
623 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
624 length_type->u.basic.integer.size,
625 (unsigned int) length_type->u.basic.integer.alignment,
626 length_type->u.basic.integer.signedness,
627 (length_type->u.basic.integer.encoding == lttng_encode_none)
628 ? "none"
629 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
630 ? "UTF8"
631 : "ASCII"),
632 length_type->u.basic.integer.base,
633#ifdef __BIG_ENDIAN
634 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
635#else
636 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
637#endif
638 field->name);
639 if (ret)
640 return ret;
641
642 ret = lttng_metadata_printf(session,
643 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
644 elem_type->u.basic.integer.size,
645 (unsigned int) elem_type->u.basic.integer.alignment,
646 elem_type->u.basic.integer.signedness,
647 (elem_type->u.basic.integer.encoding == lttng_encode_none)
648 ? "none"
649 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
650 ? "UTF8"
651 : "ASCII"),
652 elem_type->u.basic.integer.base,
653#ifdef __BIG_ENDIAN
654 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
655#else
656 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
657#endif
658 field->name,
659 field->name);
660 break;
661 }
662
663 case atype_string:
664 /* Default encoding is UTF8 */
665 ret = lttng_metadata_printf(session,
666 " string%s _%s;\n",
667 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
668 " { encoding = ASCII; }" : "",
669 field->name);
670 break;
671 default:
672 WARN_ON_ONCE(1);
673 return -EINVAL;
674 }
675 return ret;
676}
677
678static
679int _ltt_context_metadata_statedump(struct ltt_session *session,
680 struct lttng_ctx *ctx)
681{
682 int ret = 0;
683 int i;
684
685 if (!ctx)
686 return 0;
687 for (i = 0; i < ctx->nr_fields; i++) {
688 const struct lttng_ctx_field *field = &ctx->fields[i];
689
690 ret = _ltt_field_statedump(session, &field->event_field);
691 if (ret)
692 return ret;
693 }
694 return ret;
695}
696
697static
698int _ltt_fields_metadata_statedump(struct ltt_session *session,
699 struct ltt_event *event)
700{
701 const struct lttng_event_desc *desc = event->desc;
702 int ret = 0;
703 int i;
704
705 for (i = 0; i < desc->nr_fields; i++) {
706 const struct lttng_event_field *field = &desc->fields[i];
707
708 ret = _ltt_field_statedump(session, field);
709 if (ret)
710 return ret;
711 }
712 return ret;
713}
714
715static
716int _ltt_event_metadata_statedump(struct ltt_session *session,
717 struct ltt_channel *chan,
718 struct ltt_event *event)
719{
720 int ret = 0;
721
722 if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
723 return 0;
724 if (chan == session->metadata)
725 return 0;
726 /*
727 * Don't print events for which probe load is pending.
728 */
729 if (!event->desc)
730 return 0;
731
732 ret = lttng_metadata_printf(session,
733 "event {\n"
734 " name = %s;\n"
735 " id = %u;\n"
736 " stream_id = %u;\n",
737 event->desc->name,
738 event->id,
739 event->chan->id);
740 if (ret)
741 goto end;
742
743 if (event->ctx) {
744 ret = lttng_metadata_printf(session,
745 " context := struct {\n");
746 if (ret)
747 goto end;
748 }
749 ret = _ltt_context_metadata_statedump(session, event->ctx);
750 if (ret)
751 goto end;
752 if (event->ctx) {
753 ret = lttng_metadata_printf(session,
754 " };\n");
755 if (ret)
756 goto end;
757 }
758
759 ret = lttng_metadata_printf(session,
760 " fields := struct {\n"
761 );
762 if (ret)
763 goto end;
764
765 ret = _ltt_fields_metadata_statedump(session, event);
766 if (ret)
767 goto end;
768
769 /*
770 * LTTng space reservation can only reserve multiples of the
771 * byte size.
772 */
773 ret = lttng_metadata_printf(session,
774 " };\n"
775 "};\n\n");
776 if (ret)
777 goto end;
778
779 event->metadata_dumped = 1;
780end:
781 return ret;
782
783}
784
785static
786int _ltt_channel_metadata_statedump(struct ltt_session *session,
787 struct ltt_channel *chan)
788{
789 int ret = 0;
790
791 if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
792 return 0;
793 if (chan == session->metadata)
794 return 0;
795
796 WARN_ON_ONCE(!chan->header_type);
797 ret = lttng_metadata_printf(session,
798 "stream {\n"
799 " id = %u;\n"
800 " event.header := %s;\n"
801 " packet.context := struct packet_context;\n",
802 chan->id,
803 chan->header_type == 1 ? "struct event_header_compact" :
804 "struct event_header_large");
805 if (ret)
806 goto end;
807
808 if (chan->ctx) {
809 ret = lttng_metadata_printf(session,
810 " event.context := struct {\n");
811 if (ret)
812 goto end;
813 }
814 ret = _ltt_context_metadata_statedump(session, chan->ctx);
815 if (ret)
816 goto end;
817 if (chan->ctx) {
818 ret = lttng_metadata_printf(session,
819 " };\n");
820 if (ret)
821 goto end;
822 }
823
824 ret = lttng_metadata_printf(session,
825 "};\n\n");
826
827 chan->metadata_dumped = 1;
828end:
829 return ret;
830}
831
832static
833int _ltt_stream_packet_context_declare(struct ltt_session *session)
834{
835 return lttng_metadata_printf(session,
836 "struct packet_context {\n"
837 " uint64_t timestamp_begin;\n"
838 " uint64_t timestamp_end;\n"
839 " uint32_t events_discarded;\n"
840 " uint32_t content_size;\n"
841 " uint32_t packet_size;\n"
842 " uint32_t cpu_id;\n"
843 "};\n\n"
844 );
845}
846
847/*
848 * Compact header:
849 * id: range: 0 - 30.
850 * id 31 is reserved to indicate an extended header.
851 *
852 * Large header:
853 * id: range: 0 - 65534.
854 * id 65535 is reserved to indicate an extended header.
855 */
856static
857int _ltt_event_header_declare(struct ltt_session *session)
858{
859 return lttng_metadata_printf(session,
860 "struct event_header_compact {\n"
861 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
862 " variant <id> {\n"
863 " struct {\n"
864 " uint27_t timestamp;\n"
865 " } compact;\n"
866 " struct {\n"
867 " uint32_t id;\n"
868 " uint64_t timestamp;\n"
869 " } extended;\n"
870 " } v;\n"
871 "} align(%u);\n"
872 "\n"
873 "struct event_header_large {\n"
874 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
875 " variant <id> {\n"
876 " struct {\n"
877 " uint32_t timestamp;\n"
878 " } compact;\n"
879 " struct {\n"
880 " uint32_t id;\n"
881 " uint64_t timestamp;\n"
882 " } extended;\n"
883 " } v;\n"
884 "} align(%u);\n\n",
885 lttng_alignof(uint32_t) * CHAR_BIT,
886 lttng_alignof(uint16_t) * CHAR_BIT
887 );
888}
889
890/*
891 * Output metadata into this session's metadata buffers.
892 */
893static
894int _ltt_session_metadata_statedump(struct ltt_session *session)
895{
896 unsigned char *uuid_c = session->uuid;
897 char uuid_s[37];
898 struct ltt_channel *chan;
899 struct ltt_event *event;
900 int ret = 0;
901
902 if (!CMM_ACCESS_ONCE(session->active))
903 return 0;
904 if (session->metadata_dumped)
905 goto skip_session;
906 if (!session->metadata) {
907 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
908 return -EPERM;
909 }
910
911 snprintf(uuid_s, sizeof(uuid_s),
912 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
913 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
914 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
915 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
916 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
917
918 ret = lttng_metadata_printf(session,
919 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
920 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
921 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
922 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
923 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
924 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
925 "\n"
926 "trace {\n"
927 " major = %u;\n"
928 " minor = %u;\n"
929 " uuid = \"%s\";\n"
930 " byte_order = %s;\n"
931 " packet.header := struct {\n"
932 " uint32_t magic;\n"
933 " uint8_t uuid[16];\n"
934 " uint32_t stream_id;\n"
935 " };\n"
936 "};\n\n",
937 lttng_alignof(uint8_t) * CHAR_BIT,
938 lttng_alignof(uint16_t) * CHAR_BIT,
939 lttng_alignof(uint32_t) * CHAR_BIT,
940 lttng_alignof(uint64_t) * CHAR_BIT,
941 CTF_VERSION_MAJOR,
942 CTF_VERSION_MINOR,
943 uuid_s,
944#ifdef __BIG_ENDIAN
945 "be"
946#else
947 "le"
948#endif
949 );
950 if (ret)
951 goto end;
952
953 ret = _ltt_stream_packet_context_declare(session);
954 if (ret)
955 goto end;
956
957 ret = _ltt_event_header_declare(session);
958 if (ret)
959 goto end;
960
961skip_session:
962 cds_list_for_each_entry(chan, &session->chan, list) {
963 ret = _ltt_channel_metadata_statedump(session, chan);
964 if (ret)
965 goto end;
966 }
967
968 cds_list_for_each_entry(event, &session->events, list) {
969 ret = _ltt_event_metadata_statedump(session, event->chan, event);
970 if (ret)
971 goto end;
972 }
973 session->metadata_dumped = 1;
974end:
975 return ret;
976}
977
978/**
979 * ltt_transport_register - LTT transport registration
980 * @transport: transport structure
981 *
982 * Registers a transport which can be used as output to extract the data out of
983 * LTTng. Called with ust_lock held.
984 */
985void ltt_transport_register(struct ltt_transport *transport)
986{
987 cds_list_add_tail(&transport->node, &ltt_transport_list);
988}
989
990/**
991 * ltt_transport_unregister - LTT transport unregistration
992 * @transport: transport structure
993 * Called with ust_lock held.
994 */
995void ltt_transport_unregister(struct ltt_transport *transport)
996{
997 cds_list_del(&transport->node);
998}
999
1000void ltt_events_exit(void)
1001{
1002 struct ltt_session *session, *tmpsession;
1003
1004 cds_list_for_each_entry_safe(session, tmpsession, &sessions, list)
1005 ltt_session_destroy(session);
1006}
This page took 0.025962 seconds and 4 git commands to generate.