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