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