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