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