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