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