06d2f89714feeeaa3771361efd15bff5a003f2ad
[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 int ltt_event_create(struct ltt_channel *chan,
354 struct lttng_ust_event *event_param,
355 void *filter,
356 struct ltt_event **_event)
357 {
358 struct ltt_event *event;
359 int ret = 0;
360
361 if (chan->used_event_id == -1UL) {
362 ret = -ENOMEM;
363 goto full;
364 }
365 /*
366 * This is O(n^2) (for each event, the loop is called at event
367 * creation). Might require a hash if we have lots of events.
368 */
369 cds_list_for_each_entry(event, &chan->session->events, list) {
370 if (event->desc && !strcmp(event->desc->name, event_param->name)) {
371 ret = -EEXIST;
372 goto exist;
373 }
374 }
375 event = zmalloc(sizeof(struct ltt_event));
376 if (!event) {
377 ret = -ENOMEM;
378 goto cache_error;
379 }
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 case LTTNG_UST_TRACEPOINT_LOGLEVEL:
413 /*
414 * TODO: add tracepoint loglevel to hash table, with
415 * event info. Enable all events corresponding to
416 * loglevel.
417 */
418 break;
419 default:
420 WARN_ON_ONCE(1);
421 }
422 if (event->desc) {
423 ret = _ltt_event_metadata_statedump(chan->session, chan, event);
424 if (ret)
425 goto statedump_error;
426 }
427 cds_list_add(&event->list, &chan->session->events);
428 *_event = event;
429 return 0;
430
431 statedump_error:
432 if (event->desc) {
433 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param->name,
434 event->desc->probe_callback,
435 event));
436 ltt_event_put(event->desc);
437 }
438 add_pending_error:
439 register_error:
440 free(event);
441 cache_error:
442 exist:
443 full:
444 return ret;
445 }
446
447 /*
448 * Only used internally at session destruction.
449 */
450 int _ltt_event_unregister(struct ltt_event *event)
451 {
452 int ret = -EINVAL;
453
454 switch (event->instrumentation) {
455 case LTTNG_UST_TRACEPOINT:
456 if (event->desc) {
457 ret = __tracepoint_probe_unregister(event->desc->name,
458 event->desc->probe_callback,
459 event);
460 if (ret)
461 return ret;
462 } else {
463 remove_pending_probe(event->pending_probe);
464 ret = 0;
465 }
466 break;
467 default:
468 WARN_ON_ONCE(1);
469 }
470 return ret;
471 }
472
473 /*
474 * Only used internally at session destruction.
475 */
476 static
477 void _ltt_event_destroy(struct ltt_event *event)
478 {
479 switch (event->instrumentation) {
480 case LTTNG_UST_TRACEPOINT:
481 if (event->desc) {
482 ltt_event_put(event->desc);
483 }
484 break;
485 default:
486 WARN_ON_ONCE(1);
487 }
488 cds_list_del(&event->list);
489 lttng_destroy_context(event->ctx);
490 free(event);
491 }
492
493 /*
494 * We have exclusive access to our metadata buffer (protected by the
495 * ust_lock), so we can do racy operations such as looking for
496 * remaining space left in packet and write, since mutual exclusion
497 * protects us from concurrent writes.
498 */
499 int lttng_metadata_printf(struct ltt_session *session,
500 const char *fmt, ...)
501 {
502 struct lttng_ust_lib_ring_buffer_ctx ctx;
503 struct ltt_channel *chan = session->metadata;
504 char *str = NULL;
505 int ret = 0, waitret;
506 size_t len, reserve_len, pos;
507 va_list ap;
508
509 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active));
510
511 va_start(ap, fmt);
512 ret = vasprintf(&str, fmt, ap);
513 va_end(ap);
514 if (ret < 0)
515 return -ENOMEM;
516
517 len = strlen(str);
518 pos = 0;
519
520 for (pos = 0; pos < len; pos += reserve_len) {
521 reserve_len = min_t(size_t,
522 chan->ops->packet_avail_size(chan->chan, chan->handle),
523 len - pos);
524 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
525 sizeof(char), -1, chan->handle);
526 /*
527 * We don't care about metadata buffer's records lost
528 * count, because we always retry here. Report error if
529 * we need to bail out after timeout or being
530 * interrupted.
531 */
532 waitret = wait_cond_interruptible_timeout(
533 ({
534 ret = chan->ops->event_reserve(&ctx, 0);
535 ret != -ENOBUFS || !ret;
536 }),
537 LTTNG_METADATA_TIMEOUT_MSEC);
538 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
539 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
540 waitret == -EINTR ? "interrupted" :
541 (ret == -ENOBUFS ? "timeout" : "I/O error"));
542 if (waitret == -EINTR)
543 ret = waitret;
544 goto end;
545 }
546 chan->ops->event_write(&ctx, &str[pos], reserve_len);
547 chan->ops->event_commit(&ctx);
548 }
549 end:
550 free(str);
551 return ret;
552 }
553
554 static
555 int _ltt_field_statedump(struct ltt_session *session,
556 const struct lttng_event_field *field)
557 {
558 int ret = 0;
559
560 switch (field->type.atype) {
561 case atype_integer:
562 ret = lttng_metadata_printf(session,
563 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
564 field->type.u.basic.integer.size,
565 field->type.u.basic.integer.alignment,
566 field->type.u.basic.integer.signedness,
567 (field->type.u.basic.integer.encoding == lttng_encode_none)
568 ? "none"
569 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
570 ? "UTF8"
571 : "ASCII",
572 field->type.u.basic.integer.base,
573 #if (BYTE_ORDER == BIG_ENDIAN)
574 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
575 #else
576 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
577 #endif
578 field->name);
579 break;
580 case atype_float:
581 ret = lttng_metadata_printf(session,
582 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
583 field->type.u.basic._float.exp_dig,
584 field->type.u.basic._float.mant_dig,
585 field->type.u.basic._float.alignment,
586 #if (BYTE_ORDER == BIG_ENDIAN)
587 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
588 #else
589 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
590 #endif
591 field->name);
592 break;
593 case atype_enum:
594 ret = lttng_metadata_printf(session,
595 " %s %s;\n",
596 field->type.u.basic.enumeration.name,
597 field->name);
598 break;
599 case atype_array:
600 {
601 const struct lttng_basic_type *elem_type;
602
603 elem_type = &field->type.u.array.elem_type;
604 ret = lttng_metadata_printf(session,
605 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
606 elem_type->u.basic.integer.size,
607 elem_type->u.basic.integer.alignment,
608 elem_type->u.basic.integer.signedness,
609 (elem_type->u.basic.integer.encoding == lttng_encode_none)
610 ? "none"
611 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
612 ? "UTF8"
613 : "ASCII",
614 elem_type->u.basic.integer.base,
615 #if (BYTE_ORDER == BIG_ENDIAN)
616 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
617 #else
618 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
619 #endif
620 field->name, field->type.u.array.length);
621 break;
622 }
623 case atype_sequence:
624 {
625 const struct lttng_basic_type *elem_type;
626 const struct lttng_basic_type *length_type;
627
628 elem_type = &field->type.u.sequence.elem_type;
629 length_type = &field->type.u.sequence.length_type;
630 ret = lttng_metadata_printf(session,
631 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
632 length_type->u.basic.integer.size,
633 (unsigned int) length_type->u.basic.integer.alignment,
634 length_type->u.basic.integer.signedness,
635 (length_type->u.basic.integer.encoding == lttng_encode_none)
636 ? "none"
637 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
638 ? "UTF8"
639 : "ASCII"),
640 length_type->u.basic.integer.base,
641 #if (BYTE_ORDER == BIG_ENDIAN)
642 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
643 #else
644 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
645 #endif
646 field->name);
647 if (ret)
648 return ret;
649
650 ret = lttng_metadata_printf(session,
651 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
652 elem_type->u.basic.integer.size,
653 (unsigned int) elem_type->u.basic.integer.alignment,
654 elem_type->u.basic.integer.signedness,
655 (elem_type->u.basic.integer.encoding == lttng_encode_none)
656 ? "none"
657 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
658 ? "UTF8"
659 : "ASCII"),
660 elem_type->u.basic.integer.base,
661 #if (BYTE_ORDER == BIG_ENDIAN)
662 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
663 #else
664 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
665 #endif
666 field->name,
667 field->name);
668 break;
669 }
670
671 case atype_string:
672 /* Default encoding is UTF8 */
673 ret = lttng_metadata_printf(session,
674 " string%s _%s;\n",
675 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
676 " { encoding = ASCII; }" : "",
677 field->name);
678 break;
679 default:
680 WARN_ON_ONCE(1);
681 return -EINVAL;
682 }
683 return ret;
684 }
685
686 static
687 int _ltt_context_metadata_statedump(struct ltt_session *session,
688 struct lttng_ctx *ctx)
689 {
690 int ret = 0;
691 int i;
692
693 if (!ctx)
694 return 0;
695 for (i = 0; i < ctx->nr_fields; i++) {
696 const struct lttng_ctx_field *field = &ctx->fields[i];
697
698 ret = _ltt_field_statedump(session, &field->event_field);
699 if (ret)
700 return ret;
701 }
702 return ret;
703 }
704
705 static
706 int _ltt_fields_metadata_statedump(struct ltt_session *session,
707 struct ltt_event *event)
708 {
709 const struct lttng_event_desc *desc = event->desc;
710 int ret = 0;
711 int i;
712
713 for (i = 0; i < desc->nr_fields; i++) {
714 const struct lttng_event_field *field = &desc->fields[i];
715
716 ret = _ltt_field_statedump(session, field);
717 if (ret)
718 return ret;
719 }
720 return ret;
721 }
722
723 static
724 int _ltt_event_metadata_statedump(struct ltt_session *session,
725 struct ltt_channel *chan,
726 struct ltt_event *event)
727 {
728 int ret = 0;
729
730 if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
731 return 0;
732 if (chan == session->metadata)
733 return 0;
734 /*
735 * Don't print events for which probe load is pending.
736 */
737 if (!event->desc)
738 return 0;
739
740 ret = lttng_metadata_printf(session,
741 "event {\n"
742 " name = \"%s\";\n"
743 " id = %u;\n"
744 " stream_id = %u;\n",
745 event->desc->name,
746 event->id,
747 event->chan->id);
748 if (ret)
749 goto end;
750
751 if (event->ctx) {
752 ret = lttng_metadata_printf(session,
753 " context := struct {\n");
754 if (ret)
755 goto end;
756 }
757 ret = _ltt_context_metadata_statedump(session, event->ctx);
758 if (ret)
759 goto end;
760 if (event->ctx) {
761 ret = lttng_metadata_printf(session,
762 " };\n");
763 if (ret)
764 goto end;
765 }
766
767 ret = lttng_metadata_printf(session,
768 " fields := struct {\n"
769 );
770 if (ret)
771 goto end;
772
773 ret = _ltt_fields_metadata_statedump(session, event);
774 if (ret)
775 goto end;
776
777 /*
778 * LTTng space reservation can only reserve multiples of the
779 * byte size.
780 */
781 ret = lttng_metadata_printf(session,
782 " };\n"
783 "};\n\n");
784 if (ret)
785 goto end;
786
787 event->metadata_dumped = 1;
788 end:
789 return ret;
790
791 }
792
793 static
794 int _ltt_channel_metadata_statedump(struct ltt_session *session,
795 struct ltt_channel *chan)
796 {
797 int ret = 0;
798
799 if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
800 return 0;
801 if (chan == session->metadata)
802 return 0;
803
804 WARN_ON_ONCE(!chan->header_type);
805 ret = lttng_metadata_printf(session,
806 "stream {\n"
807 " id = %u;\n"
808 " event.header := %s;\n"
809 " packet.context := struct packet_context;\n",
810 chan->id,
811 chan->header_type == 1 ? "struct event_header_compact" :
812 "struct event_header_large");
813 if (ret)
814 goto end;
815
816 if (chan->ctx) {
817 ret = lttng_metadata_printf(session,
818 " event.context := struct {\n");
819 if (ret)
820 goto end;
821 }
822 ret = _ltt_context_metadata_statedump(session, chan->ctx);
823 if (ret)
824 goto end;
825 if (chan->ctx) {
826 ret = lttng_metadata_printf(session,
827 " };\n");
828 if (ret)
829 goto end;
830 }
831
832 ret = lttng_metadata_printf(session,
833 "};\n\n");
834
835 chan->metadata_dumped = 1;
836 end:
837 return ret;
838 }
839
840 static
841 int _ltt_stream_packet_context_declare(struct ltt_session *session)
842 {
843 return lttng_metadata_printf(session,
844 "struct packet_context {\n"
845 " uint64_t timestamp_begin;\n"
846 " uint64_t timestamp_end;\n"
847 " uint32_t events_discarded;\n"
848 " uint32_t content_size;\n"
849 " uint32_t packet_size;\n"
850 " uint32_t cpu_id;\n"
851 "};\n\n"
852 );
853 }
854
855 /*
856 * Compact header:
857 * id: range: 0 - 30.
858 * id 31 is reserved to indicate an extended header.
859 *
860 * Large header:
861 * id: range: 0 - 65534.
862 * id 65535 is reserved to indicate an extended header.
863 */
864 static
865 int _ltt_event_header_declare(struct ltt_session *session)
866 {
867 return lttng_metadata_printf(session,
868 "struct event_header_compact {\n"
869 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
870 " variant <id> {\n"
871 " struct {\n"
872 " uint27_t timestamp;\n"
873 " } compact;\n"
874 " struct {\n"
875 " uint32_t id;\n"
876 " uint64_t timestamp;\n"
877 " } extended;\n"
878 " } v;\n"
879 "} align(%u);\n"
880 "\n"
881 "struct event_header_large {\n"
882 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
883 " variant <id> {\n"
884 " struct {\n"
885 " uint32_t timestamp;\n"
886 " } compact;\n"
887 " struct {\n"
888 " uint32_t id;\n"
889 " uint64_t timestamp;\n"
890 " } extended;\n"
891 " } v;\n"
892 "} align(%u);\n\n",
893 lttng_alignof(uint32_t) * CHAR_BIT,
894 lttng_alignof(uint16_t) * CHAR_BIT
895 );
896 }
897
898 /*
899 * Output metadata into this session's metadata buffers.
900 */
901 static
902 int _ltt_session_metadata_statedump(struct ltt_session *session)
903 {
904 unsigned char *uuid_c = session->uuid;
905 char uuid_s[37];
906 struct ltt_channel *chan;
907 struct ltt_event *event;
908 int ret = 0;
909
910 if (!CMM_ACCESS_ONCE(session->active))
911 return 0;
912 if (session->metadata_dumped)
913 goto skip_session;
914 if (!session->metadata) {
915 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
916 return -EPERM;
917 }
918
919 snprintf(uuid_s, sizeof(uuid_s),
920 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
921 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
922 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
923 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
924 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
925
926 ret = lttng_metadata_printf(session,
927 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
928 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
929 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
930 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
931 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
932 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
933 "\n"
934 "trace {\n"
935 " major = %u;\n"
936 " minor = %u;\n"
937 " uuid = \"%s\";\n"
938 " byte_order = %s;\n"
939 " packet.header := struct {\n"
940 " uint32_t magic;\n"
941 " uint8_t uuid[16];\n"
942 " uint32_t stream_id;\n"
943 " };\n"
944 "};\n\n",
945 lttng_alignof(uint8_t) * CHAR_BIT,
946 lttng_alignof(uint16_t) * CHAR_BIT,
947 lttng_alignof(uint32_t) * CHAR_BIT,
948 lttng_alignof(uint64_t) * CHAR_BIT,
949 CTF_VERSION_MAJOR,
950 CTF_VERSION_MINOR,
951 uuid_s,
952 #if (BYTE_ORDER == BIG_ENDIAN)
953 "be"
954 #else
955 "le"
956 #endif
957 );
958 if (ret)
959 goto end;
960
961 ret = _ltt_stream_packet_context_declare(session);
962 if (ret)
963 goto end;
964
965 ret = _ltt_event_header_declare(session);
966 if (ret)
967 goto end;
968
969 skip_session:
970 cds_list_for_each_entry(chan, &session->chan, list) {
971 ret = _ltt_channel_metadata_statedump(session, chan);
972 if (ret)
973 goto end;
974 }
975
976 cds_list_for_each_entry(event, &session->events, list) {
977 ret = _ltt_event_metadata_statedump(session, event->chan, event);
978 if (ret)
979 goto end;
980 }
981 session->metadata_dumped = 1;
982 end:
983 return ret;
984 }
985
986 void lttng_ust_events_exit(void)
987 {
988 struct ltt_session *session, *tmpsession;
989
990 cds_list_for_each_entry_safe(session, tmpsession, &sessions, list)
991 ltt_session_destroy(session);
992 }
This page took 0.04932 seconds and 4 git commands to generate.