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