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