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