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