Rename liblttng-ust-libc to liblttng-ust-libc-wrapper
[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>
28b12049
MD
23#include <inttypes.h>
24#include <time.h>
b020e504 25#include <sys/prctl.h>
28b12049 26#include "clock.h"
44c72f10
MD
27
28#include <urcu-bp.h>
29#include <urcu/compiler.h>
30#include <urcu/uatomic.h>
31#include <urcu/arch.h>
32
33#include <lttng/tracepoint.h>
4318ae1b 34#include <lttng/ust-events.h>
44c72f10
MD
35
36#include <usterr-signal-safe.h>
37#include <helper.h>
1f18504e 38#include "error.h"
44c72f10 39
457a6b58 40#include "tracepoint-internal.h"
8020ceb5 41#include "ltt-tracer.h"
8165c8da 42#include "ltt-tracer-core.h"
b728d87e 43#include "wait.h"
8d8a24c8 44#include "../libringbuffer/shm.h"
596c4223 45#include "jhash.h"
8165c8da 46
b020e504
MD
47#define PROCNAME_LEN 17
48
8165c8da
MD
49/*
50 * The sessions mutex is the centralized mutex across UST tracing
17dfb34b
MD
51 * control and probe registration. All operations within this file are
52 * called by the communication thread, under ust_lock protection.
8165c8da 53 */
2b2d6ff7 54static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER;
8165c8da 55
17dfb34b 56void ust_lock(void)
8165c8da
MD
57{
58 pthread_mutex_lock(&sessions_mutex);
59}
60
17dfb34b 61void ust_unlock(void)
8165c8da
MD
62{
63 pthread_mutex_unlock(&sessions_mutex);
64}
65
b5234c06 66static CDS_LIST_HEAD(sessions);
8165c8da 67
457a6b58
MD
68/*
69 * Wildcard list, containing the active wildcards.
70 * Protected by ust lock.
71 */
72static CDS_LIST_HEAD(wildcard_list);
73
8165c8da
MD
74/*
75 * Pending probes hash table, containing the registered ltt events for
76 * which tracepoint probes are still missing. Protected by the sessions
77 * mutex.
78 */
79#define PENDING_PROBE_HASH_BITS 6
80#define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
81static struct cds_hlist_head pending_probe_table[PENDING_PROBE_HASH_SIZE];
82
83struct ust_pending_probe {
84 struct ltt_event *event;
85 struct cds_hlist_node node;
457a6b58
MD
86 enum lttng_ust_loglevel_type loglevel_type;
87 int loglevel;
8165c8da
MD
88 char name[];
89};
8020ceb5
MD
90
91static void _ltt_event_destroy(struct ltt_event *event);
e6c12e3d 92static void _ltt_wildcard_destroy(struct session_wildcard *sw);
8020ceb5
MD
93static void _ltt_channel_destroy(struct ltt_channel *chan);
94static int _ltt_event_unregister(struct ltt_event *event);
95static
96int _ltt_event_metadata_statedump(struct ltt_session *session,
97 struct ltt_channel *chan,
98 struct ltt_event *event);
99static
100int _ltt_session_metadata_statedump(struct ltt_session *session);
101
457a6b58
MD
102int ltt_loglevel_match(const struct lttng_event_desc *desc,
103 enum lttng_ust_loglevel_type req_type,
104 int req_loglevel)
105{
106 int ev_loglevel;
107
108 if (req_type == LTTNG_UST_LOGLEVEL_ALL)
109 return 1;
110 if (!desc->loglevel)
111 ev_loglevel = TRACE_DEFAULT;
112 else
113 ev_loglevel = *(*desc->loglevel);
114 switch (req_type) {
115 case LTTNG_UST_LOGLEVEL_RANGE:
116 if (ev_loglevel <= req_loglevel || req_loglevel == -1)
117 return 1;
118 else
119 return 0;
120 case LTTNG_UST_LOGLEVEL_SINGLE:
121 if (ev_loglevel == req_loglevel || req_loglevel == -1)
122 return 1;
123 else
124 return 0;
125 case LTTNG_UST_LOGLEVEL_ALL:
126 default:
127 return 1;
128 }
129}
130
131/*
132 * Return wildcard for a given event name if the event name match the
133 * one of the wildcards.
134 * Must be called with ust lock held.
135 * Returns NULL if not present.
136 */
137static
138struct wildcard_entry *match_wildcard(const struct lttng_event_desc *desc)
139{
140 struct wildcard_entry *e;
141
142 cds_list_for_each_entry(e, &wildcard_list, list) {
143 /* If only contain '*' */
144 if (strlen(e->name) == 1)
145 goto possible_match;
146 /* Compare excluding final '*' */
147 if (!strncmp(desc->name, e->name, strlen(e->name) - 1))
148 goto possible_match;
149 continue; /* goto next, no match */
150 possible_match:
151 if (ltt_loglevel_match(desc,
152 e->loglevel_type,
153 e->loglevel)) {
154 return e;
155 }
156 /* no match, loop to next */
157 }
158 return NULL;
159}
160
8165c8da
MD
161/*
162 * called at event creation if probe is missing.
163 * called with session mutex held.
164 */
165static
457a6b58
MD
166int add_pending_probe(struct ltt_event *event, const char *name,
167 enum lttng_ust_loglevel_type loglevel_type,
168 int loglevel)
8165c8da
MD
169{
170 struct cds_hlist_head *head;
8165c8da 171 struct ust_pending_probe *e;
ff412fb5
MD
172 size_t name_len = strlen(name);
173 uint32_t hash;
8165c8da 174
ff412fb5
MD
175 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
176 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
177 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
178 }
179 hash = jhash(name, name_len, 0);
8165c8da
MD
180 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
181 e = zmalloc(sizeof(struct ust_pending_probe) + name_len);
182 if (!e)
183 return -ENOMEM;
ff412fb5
MD
184 memcpy(&e->name[0], name, name_len + 1);
185 e->name[name_len] = '\0';
457a6b58
MD
186 e->loglevel_type = loglevel_type;
187 e->loglevel = loglevel;
8165c8da
MD
188 cds_hlist_add_head(&e->node, head);
189 e->event = event;
190 event->pending_probe = e;
191 return 0;
192}
193
194/*
195 * remove a pending probe. called when at event teardown and when an
196 * event is fixed (probe is loaded).
197 * called with session mutex held.
198 */
199static
200void remove_pending_probe(struct ust_pending_probe *e)
201{
202 if (!e)
203 return;
204 cds_hlist_del(&e->node);
205 free(e);
206}
207
208/*
209 * Called at library load: connect the probe on the events pending on
210 * probe load.
211 * called with session mutex held.
212 */
213int pending_probe_fix_events(const struct lttng_event_desc *desc)
214{
215 struct cds_hlist_head *head;
216 struct cds_hlist_node *node, *p;
217 struct ust_pending_probe *e;
218 const char *name = desc->name;
8165c8da 219 int ret = 0;
7ee3cc56 220 struct lttng_ust_event event_param;
ff412fb5
MD
221 size_t name_len = strlen(name);
222 uint32_t hash;
8165c8da 223
e6c12e3d
MD
224 /* Wildcard */
225 {
226 struct wildcard_entry *wildcard;
227
457a6b58 228 wildcard = match_wildcard(desc);
e6c12e3d
MD
229 if (strcmp(desc->name, "lttng_ust:metadata") && wildcard) {
230 struct session_wildcard *sw;
231
232 cds_list_for_each_entry(sw, &wildcard->session_list,
233 session_list) {
234 struct ltt_event *ev;
235 int ret;
236
237 memcpy(&event_param, &sw->event_param,
238 sizeof(event_param));
239 memcpy(event_param.name,
240 desc->name,
241 sizeof(event_param.name));
242 /* create event */
243 ret = ltt_event_create(sw->chan,
244 &event_param, NULL,
245 &ev);
246 if (ret) {
247 DBG("Error creating event");
248 continue;
249 }
250 cds_list_add(&ev->wildcard_list,
251 &sw->events);
252 }
253 }
254 }
255
ff412fb5
MD
256 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
257 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
258 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
259 }
260 hash = jhash(name, name_len, 0);
8165c8da
MD
261 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
262 cds_hlist_for_each_entry_safe(e, node, p, head, node) {
263 struct ltt_event *event;
264 struct ltt_channel *chan;
265
fe2d02f4 266 if (!ltt_loglevel_match(desc,
457a6b58
MD
267 e->loglevel_type,
268 e->loglevel)) {
269 continue;
270 }
271 if (strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
272 continue;
273 }
fe2d02f4 274 event = e->event;
8165c8da
MD
275 chan = event->chan;
276 assert(!event->desc);
277 event->desc = desc;
278 event->pending_probe = NULL;
279 remove_pending_probe(e);
280 ret |= __tracepoint_probe_register(name,
281 event->desc->probe_callback,
282 event);
636c5aa4
MD
283 if (ret)
284 continue;
285 event->id = chan->free_event_id++;
8165c8da
MD
286 ret |= _ltt_event_metadata_statedump(chan->session, chan,
287 event);
288 }
289 return ret;
290}
291
8020ceb5
MD
292void synchronize_trace(void)
293{
8020ceb5 294 synchronize_rcu();
8020ceb5
MD
295}
296
297struct ltt_session *ltt_session_create(void)
298{
299 struct ltt_session *session;
300
b5234c06 301 session = zmalloc(sizeof(struct ltt_session));
8020ceb5
MD
302 if (!session)
303 return NULL;
b5234c06
MD
304 CDS_INIT_LIST_HEAD(&session->chan);
305 CDS_INIT_LIST_HEAD(&session->events);
e6c12e3d 306 CDS_INIT_LIST_HEAD(&session->wildcards);
b5234c06
MD
307 uuid_generate(session->uuid);
308 cds_list_add(&session->list, &sessions);
8020ceb5
MD
309 return session;
310}
311
312void ltt_session_destroy(struct ltt_session *session)
313{
314 struct ltt_channel *chan, *tmpchan;
315 struct ltt_event *event, *tmpevent;
e6c12e3d 316 struct session_wildcard *wildcard, *tmpwildcard;
8020ceb5
MD
317 int ret;
318
b5234c06
MD
319 CMM_ACCESS_ONCE(session->active) = 0;
320 cds_list_for_each_entry(event, &session->events, list) {
8020ceb5
MD
321 ret = _ltt_event_unregister(event);
322 WARN_ON(ret);
323 }
324 synchronize_trace(); /* Wait for in-flight events to complete */
e6c12e3d
MD
325 cds_list_for_each_entry_safe(wildcard, tmpwildcard, &session->wildcards, list)
326 _ltt_wildcard_destroy(wildcard);
b5234c06 327 cds_list_for_each_entry_safe(event, tmpevent, &session->events, list)
8020ceb5 328 _ltt_event_destroy(event);
b5234c06 329 cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
8020ceb5 330 _ltt_channel_destroy(chan);
b5234c06 331 cds_list_del(&session->list);
b5234c06 332 free(session);
8020ceb5
MD
333}
334
976fe9ea 335int ltt_session_enable(struct ltt_session *session)
8020ceb5
MD
336{
337 int ret = 0;
338 struct ltt_channel *chan;
339
8020ceb5
MD
340 if (session->active) {
341 ret = -EBUSY;
342 goto end;
343 }
344
345 /*
346 * Snapshot the number of events per channel to know the type of header
347 * we need to use.
348 */
b5234c06 349 cds_list_for_each_entry(chan, &session->chan, list) {
8020ceb5
MD
350 if (chan->header_type)
351 continue; /* don't change it if session stop/restart */
352 if (chan->free_event_id < 31)
353 chan->header_type = 1; /* compact */
354 else
355 chan->header_type = 2; /* large */
356 }
357
b5234c06
MD
358 CMM_ACCESS_ONCE(session->active) = 1;
359 CMM_ACCESS_ONCE(session->been_active) = 1;
8020ceb5 360 ret = _ltt_session_metadata_statedump(session);
4b4de73e 361 if (ret)
b5234c06 362 CMM_ACCESS_ONCE(session->active) = 0;
8020ceb5 363end:
8020ceb5
MD
364 return ret;
365}
366
976fe9ea 367int ltt_session_disable(struct ltt_session *session)
8020ceb5
MD
368{
369 int ret = 0;
370
8020ceb5
MD
371 if (!session->active) {
372 ret = -EBUSY;
373 goto end;
374 }
b5234c06 375 CMM_ACCESS_ONCE(session->active) = 0;
8020ceb5 376end:
8020ceb5
MD
377 return ret;
378}
379
976fe9ea
MD
380int ltt_channel_enable(struct ltt_channel *channel)
381{
382 int old;
383
9beb36ba
MD
384 if (channel == channel->session->metadata)
385 return -EPERM;
b5234c06 386 old = uatomic_xchg(&channel->enabled, 1);
976fe9ea
MD
387 if (old)
388 return -EEXIST;
389 return 0;
390}
391
392int ltt_channel_disable(struct ltt_channel *channel)
393{
394 int old;
395
9beb36ba
MD
396 if (channel == channel->session->metadata)
397 return -EPERM;
b5234c06 398 old = uatomic_xchg(&channel->enabled, 0);
976fe9ea
MD
399 if (!old)
400 return -EEXIST;
401 return 0;
402}
403
404int ltt_event_enable(struct ltt_event *event)
405{
406 int old;
407
9beb36ba
MD
408 if (event->chan == event->chan->session->metadata)
409 return -EPERM;
b5234c06 410 old = uatomic_xchg(&event->enabled, 1);
976fe9ea
MD
411 if (old)
412 return -EEXIST;
413 return 0;
414}
415
416int ltt_event_disable(struct ltt_event *event)
417{
418 int old;
419
9beb36ba
MD
420 if (event->chan == event->chan->session->metadata)
421 return -EPERM;
b5234c06 422 old = uatomic_xchg(&event->enabled, 0);
976fe9ea
MD
423 if (!old)
424 return -EEXIST;
425 return 0;
426}
427
8020ceb5
MD
428struct ltt_channel *ltt_channel_create(struct ltt_session *session,
429 const char *transport_name,
430 void *buf_addr,
431 size_t subbuf_size, size_t num_subbuf,
432 unsigned int switch_timer_interval,
193183fb 433 unsigned int read_timer_interval,
ef9ff354
MD
434 int **shm_fd, int **wait_fd,
435 uint64_t **memory_map_size,
d028eddb 436 struct ltt_channel *chan_priv_init)
8020ceb5 437{
a3f61e7f 438 struct ltt_channel *chan = NULL;
8020ceb5
MD
439 struct ltt_transport *transport;
440
8020ceb5
MD
441 if (session->been_active)
442 goto active; /* Refuse to add channel to active session */
443 transport = ltt_transport_find(transport_name);
444 if (!transport) {
b5234c06 445 DBG("LTTng transport %s not found\n",
8020ceb5
MD
446 transport_name);
447 goto notransport;
448 }
d028eddb
MD
449 chan_priv_init->id = session->free_chan_id++;
450 chan_priv_init->session = session;
8020ceb5
MD
451 /*
452 * Note: the channel creation op already writes into the packet
453 * headers. Therefore the "chan" information used as input
454 * should be already accessible.
455 */
a3f61e7f 456 chan = transport->ops.channel_create("[lttng]", buf_addr,
8020ceb5 457 subbuf_size, num_subbuf, switch_timer_interval,
193183fb 458 read_timer_interval, shm_fd, wait_fd,
d028eddb 459 memory_map_size, chan_priv_init);
a3f61e7f 460 if (!chan)
8020ceb5 461 goto create_error;
976fe9ea 462 chan->enabled = 1;
8020ceb5 463 chan->ops = &transport->ops;
b5234c06 464 cds_list_add(&chan->list, &session->chan);
8020ceb5
MD
465 return chan;
466
467create_error:
8020ceb5
MD
468notransport:
469active:
8020ceb5
MD
470 return NULL;
471}
472
473/*
474 * Only used internally at session destruction.
475 */
476static
477void _ltt_channel_destroy(struct ltt_channel *chan)
478{
b5234c06 479 cds_list_del(&chan->list);
8020ceb5 480 lttng_destroy_context(chan->ctx);
a3f61e7f 481 chan->ops->channel_destroy(chan);
8020ceb5
MD
482}
483
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 491{
457a6b58 492 const struct lttng_event_desc *desc = NULL; /* silence gcc */
8020ceb5 493 struct ltt_event *event;
576599a0 494 int ret = 0;
8020ceb5 495
576599a0
MD
496 if (chan->used_event_id == -1UL) {
497 ret = -ENOMEM;
8020ceb5 498 goto full;
576599a0 499 }
8020ceb5
MD
500 /*
501 * This is O(n^2) (for each event, the loop is called at event
502 * creation). Might require a hash if we have lots of events.
503 */
576599a0 504 cds_list_for_each_entry(event, &chan->session->events, list) {
ff412fb5
MD
505 if (event->desc && !strncmp(event->desc->name,
506 event_param->name,
507 LTTNG_UST_SYM_NAME_LEN - 1)) {
576599a0 508 ret = -EEXIST;
8020ceb5 509 goto exist;
576599a0
MD
510 }
511 }
457a6b58
MD
512
513 /*
514 * Check if loglevel match. Refuse to connect event if not.
515 */
516 if (event_param->instrumentation == LTTNG_UST_TRACEPOINT) {
517 desc = ltt_event_get(event_param->name);
fe2d02f4
MD
518 if (desc) {
519 if (!ltt_loglevel_match(desc,
520 event_param->loglevel_type,
521 event_param->loglevel)) {
522 ret = -EPERM;
523 goto no_loglevel_match;
524 }
457a6b58 525 }
fe2d02f4
MD
526 /*
527 * If descriptor is not there, it will be added to
528 * pending probes.
529 */
457a6b58 530 }
b5234c06 531 event = zmalloc(sizeof(struct ltt_event));
576599a0
MD
532 if (!event) {
533 ret = -ENOMEM;
8020ceb5 534 goto cache_error;
576599a0 535 }
8020ceb5
MD
536 event->chan = chan;
537 event->filter = filter;
8165c8da
MD
538 /*
539 * used_event_id counts the maximum number of event IDs that can
540 * register if all probes register.
541 */
542 chan->used_event_id++;
976fe9ea 543 event->enabled = 1;
8020ceb5
MD
544 event->instrumentation = event_param->instrumentation;
545 /* Populate ltt_event structure before tracepoint registration. */
b5234c06 546 cmm_smp_wmb();
8020ceb5 547 switch (event_param->instrumentation) {
b5234c06 548 case LTTNG_UST_TRACEPOINT:
457a6b58 549 event->desc = desc;
8165c8da
MD
550 if (event->desc) {
551 ret = __tracepoint_probe_register(event_param->name,
552 event->desc->probe_callback,
553 event);
554 if (ret)
555 goto register_error;
556 event->id = chan->free_event_id++;
557 } else {
558 /*
559 * If the probe is not present, event->desc stays NULL,
560 * waiting for the probe to register, and the event->id
561 * stays unallocated.
562 */
457a6b58
MD
563 ret = add_pending_probe(event, event_param->name,
564 event_param->loglevel_type,
565 event_param->loglevel);
8165c8da
MD
566 if (ret)
567 goto add_pending_error;
568 }
8020ceb5 569 break;
8020ceb5
MD
570 default:
571 WARN_ON_ONCE(1);
572 }
8165c8da
MD
573 if (event->desc) {
574 ret = _ltt_event_metadata_statedump(chan->session, chan, event);
575 if (ret)
576 goto statedump_error;
577 }
b5234c06 578 cds_list_add(&event->list, &chan->session->events);
576599a0
MD
579 *_event = event;
580 return 0;
8020ceb5
MD
581
582statedump_error:
8165c8da
MD
583 if (event->desc) {
584 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param->name,
585 event->desc->probe_callback,
586 event));
587 ltt_event_put(event->desc);
588 }
589add_pending_error:
8020ceb5 590register_error:
b5234c06 591 free(event);
8020ceb5 592cache_error:
457a6b58 593no_loglevel_match:
8020ceb5
MD
594exist:
595full:
576599a0 596 return ret;
8020ceb5
MD
597}
598
599/*
600 * Only used internally at session destruction.
601 */
602int _ltt_event_unregister(struct ltt_event *event)
603{
604 int ret = -EINVAL;
605
606 switch (event->instrumentation) {
b5234c06 607 case LTTNG_UST_TRACEPOINT:
8165c8da
MD
608 if (event->desc) {
609 ret = __tracepoint_probe_unregister(event->desc->name,
610 event->desc->probe_callback,
611 event);
612 if (ret)
613 return ret;
614 } else {
615 remove_pending_probe(event->pending_probe);
616 ret = 0;
617 }
8020ceb5 618 break;
8020ceb5
MD
619 default:
620 WARN_ON_ONCE(1);
621 }
622 return ret;
623}
624
625/*
626 * Only used internally at session destruction.
627 */
628static
629void _ltt_event_destroy(struct ltt_event *event)
630{
631 switch (event->instrumentation) {
b5234c06 632 case LTTNG_UST_TRACEPOINT:
8165c8da
MD
633 if (event->desc) {
634 ltt_event_put(event->desc);
635 }
8020ceb5 636 break;
8020ceb5
MD
637 default:
638 WARN_ON_ONCE(1);
639 }
b5234c06 640 cds_list_del(&event->list);
8020ceb5 641 lttng_destroy_context(event->ctx);
b5234c06 642 free(event);
8020ceb5
MD
643}
644
645/*
646 * We have exclusive access to our metadata buffer (protected by the
17dfb34b 647 * ust_lock), so we can do racy operations such as looking for
8020ceb5
MD
648 * remaining space left in packet and write, since mutual exclusion
649 * protects us from concurrent writes.
650 */
651int lttng_metadata_printf(struct ltt_session *session,
652 const char *fmt, ...)
653{
4cfec15c 654 struct lttng_ust_lib_ring_buffer_ctx ctx;
8020ceb5 655 struct ltt_channel *chan = session->metadata;
b5234c06 656 char *str = NULL;
8020ceb5
MD
657 int ret = 0, waitret;
658 size_t len, reserve_len, pos;
659 va_list ap;
660
b5234c06 661 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active));
8020ceb5
MD
662
663 va_start(ap, fmt);
b5234c06 664 ret = vasprintf(&str, fmt, ap);
8020ceb5 665 va_end(ap);
b5234c06 666 if (ret < 0)
8020ceb5
MD
667 return -ENOMEM;
668
669 len = strlen(str);
670 pos = 0;
671
672 for (pos = 0; pos < len; pos += reserve_len) {
673 reserve_len = min_t(size_t,
1d498196 674 chan->ops->packet_avail_size(chan->chan, chan->handle),
8020ceb5
MD
675 len - pos);
676 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1d498196 677 sizeof(char), -1, chan->handle);
8020ceb5
MD
678 /*
679 * We don't care about metadata buffer's records lost
680 * count, because we always retry here. Report error if
681 * we need to bail out after timeout or being
682 * interrupted.
683 */
b5234c06 684 waitret = wait_cond_interruptible_timeout(
8020ceb5
MD
685 ({
686 ret = chan->ops->event_reserve(&ctx, 0);
687 ret != -ENOBUFS || !ret;
688 }),
b5234c06 689 LTTNG_METADATA_TIMEOUT_MSEC);
b472cfc0 690 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
b5234c06
MD
691 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
692 waitret == -EINTR ? "interrupted" :
8020ceb5 693 (ret == -ENOBUFS ? "timeout" : "I/O error"));
b5234c06 694 if (waitret == -EINTR)
8020ceb5
MD
695 ret = waitret;
696 goto end;
697 }
698 chan->ops->event_write(&ctx, &str[pos], reserve_len);
699 chan->ops->event_commit(&ctx);
700 }
701end:
b5234c06 702 free(str);
8020ceb5
MD
703 return ret;
704}
705
706static
707int _ltt_field_statedump(struct ltt_session *session,
708 const struct lttng_event_field *field)
709{
710 int ret = 0;
711
712 switch (field->type.atype) {
713 case atype_integer:
714 ret = lttng_metadata_printf(session,
4e2ee1f5 715 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
8020ceb5
MD
716 field->type.u.basic.integer.size,
717 field->type.u.basic.integer.alignment,
718 field->type.u.basic.integer.signedness,
719 (field->type.u.basic.integer.encoding == lttng_encode_none)
720 ? "none"
721 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
722 ? "UTF8"
723 : "ASCII",
724 field->type.u.basic.integer.base,
8a98a75d 725#if (BYTE_ORDER == BIG_ENDIAN)
403c40b4
MD
726 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
727#else
728 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
729#endif
730 field->name);
731 break;
732 case atype_float:
733 ret = lttng_metadata_printf(session,
8a98a75d 734 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
403c40b4
MD
735 field->type.u.basic._float.exp_dig,
736 field->type.u.basic._float.mant_dig,
737 field->type.u.basic._float.alignment,
8a98a75d 738#if (BYTE_ORDER == BIG_ENDIAN)
8020ceb5
MD
739 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
740#else
741 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
742#endif
743 field->name);
744 break;
745 case atype_enum:
746 ret = lttng_metadata_printf(session,
747 " %s %s;\n",
748 field->type.u.basic.enumeration.name,
749 field->name);
750 break;
751 case atype_array:
752 {
753 const struct lttng_basic_type *elem_type;
754
755 elem_type = &field->type.u.array.elem_type;
756 ret = lttng_metadata_printf(session,
4e2ee1f5 757 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
8020ceb5
MD
758 elem_type->u.basic.integer.size,
759 elem_type->u.basic.integer.alignment,
760 elem_type->u.basic.integer.signedness,
761 (elem_type->u.basic.integer.encoding == lttng_encode_none)
762 ? "none"
763 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
764 ? "UTF8"
765 : "ASCII",
766 elem_type->u.basic.integer.base,
8a98a75d 767#if (BYTE_ORDER == BIG_ENDIAN)
8020ceb5
MD
768 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
769#else
770 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
771#endif
772 field->name, field->type.u.array.length);
773 break;
774 }
775 case atype_sequence:
776 {
777 const struct lttng_basic_type *elem_type;
778 const struct lttng_basic_type *length_type;
779
780 elem_type = &field->type.u.sequence.elem_type;
781 length_type = &field->type.u.sequence.length_type;
782 ret = lttng_metadata_printf(session,
783 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
784 length_type->u.basic.integer.size,
785 (unsigned int) length_type->u.basic.integer.alignment,
786 length_type->u.basic.integer.signedness,
787 (length_type->u.basic.integer.encoding == lttng_encode_none)
788 ? "none"
789 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
790 ? "UTF8"
791 : "ASCII"),
792 length_type->u.basic.integer.base,
8a98a75d 793#if (BYTE_ORDER == BIG_ENDIAN)
8020ceb5
MD
794 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
795#else
796 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
797#endif
798 field->name);
799 if (ret)
800 return ret;
801
802 ret = lttng_metadata_printf(session,
4e2ee1f5 803 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
8020ceb5
MD
804 elem_type->u.basic.integer.size,
805 (unsigned int) elem_type->u.basic.integer.alignment,
806 elem_type->u.basic.integer.signedness,
807 (elem_type->u.basic.integer.encoding == lttng_encode_none)
808 ? "none"
809 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
810 ? "UTF8"
811 : "ASCII"),
812 elem_type->u.basic.integer.base,
8a98a75d 813#if (BYTE_ORDER == BIG_ENDIAN)
8020ceb5
MD
814 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
815#else
816 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
817#endif
818 field->name,
819 field->name);
820 break;
821 }
822
823 case atype_string:
824 /* Default encoding is UTF8 */
825 ret = lttng_metadata_printf(session,
4e2ee1f5 826 " string%s _%s;\n",
8020ceb5
MD
827 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
828 " { encoding = ASCII; }" : "",
829 field->name);
830 break;
831 default:
832 WARN_ON_ONCE(1);
833 return -EINVAL;
834 }
835 return ret;
836}
837
838static
839int _ltt_context_metadata_statedump(struct ltt_session *session,
840 struct lttng_ctx *ctx)
841{
842 int ret = 0;
843 int i;
844
845 if (!ctx)
846 return 0;
847 for (i = 0; i < ctx->nr_fields; i++) {
848 const struct lttng_ctx_field *field = &ctx->fields[i];
849
850 ret = _ltt_field_statedump(session, &field->event_field);
851 if (ret)
852 return ret;
853 }
854 return ret;
855}
856
857static
858int _ltt_fields_metadata_statedump(struct ltt_session *session,
859 struct ltt_event *event)
860{
861 const struct lttng_event_desc *desc = event->desc;
862 int ret = 0;
863 int i;
864
865 for (i = 0; i < desc->nr_fields; i++) {
866 const struct lttng_event_field *field = &desc->fields[i];
867
868 ret = _ltt_field_statedump(session, field);
869 if (ret)
870 return ret;
871 }
872 return ret;
873}
874
875static
876int _ltt_event_metadata_statedump(struct ltt_session *session,
877 struct ltt_channel *chan,
878 struct ltt_event *event)
879{
880 int ret = 0;
881
b5234c06 882 if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
8020ceb5
MD
883 return 0;
884 if (chan == session->metadata)
885 return 0;
8165c8da
MD
886 /*
887 * Don't print events for which probe load is pending.
888 */
889 if (!event->desc)
890 return 0;
8020ceb5
MD
891
892 ret = lttng_metadata_printf(session,
893 "event {\n"
7083f0fe 894 " name = \"%s\";\n"
8020ceb5
MD
895 " id = %u;\n"
896 " stream_id = %u;\n",
897 event->desc->name,
898 event->id,
899 event->chan->id);
900 if (ret)
901 goto end;
902
e6c12e3d 903 if (event->desc->loglevel) {
e6c12e3d 904 ret = lttng_metadata_printf(session,
882a56d7 905 " loglevel = %d;\n",
457a6b58 906 *(*event->desc->loglevel));
e6c12e3d
MD
907 if (ret)
908 goto end;
909 }
910
8020ceb5
MD
911 if (event->ctx) {
912 ret = lttng_metadata_printf(session,
913 " context := struct {\n");
914 if (ret)
915 goto end;
916 }
917 ret = _ltt_context_metadata_statedump(session, event->ctx);
918 if (ret)
919 goto end;
920 if (event->ctx) {
921 ret = lttng_metadata_printf(session,
922 " };\n");
923 if (ret)
924 goto end;
925 }
926
927 ret = lttng_metadata_printf(session,
928 " fields := struct {\n"
929 );
930 if (ret)
931 goto end;
932
933 ret = _ltt_fields_metadata_statedump(session, event);
934 if (ret)
935 goto end;
936
937 /*
938 * LTTng space reservation can only reserve multiples of the
939 * byte size.
940 */
941 ret = lttng_metadata_printf(session,
942 " };\n"
943 "};\n\n");
944 if (ret)
945 goto end;
946
947 event->metadata_dumped = 1;
948end:
949 return ret;
950
951}
952
953static
954int _ltt_channel_metadata_statedump(struct ltt_session *session,
955 struct ltt_channel *chan)
956{
957 int ret = 0;
958
b5234c06 959 if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
8020ceb5
MD
960 return 0;
961 if (chan == session->metadata)
962 return 0;
963
964 WARN_ON_ONCE(!chan->header_type);
965 ret = lttng_metadata_printf(session,
966 "stream {\n"
967 " id = %u;\n"
968 " event.header := %s;\n"
969 " packet.context := struct packet_context;\n",
970 chan->id,
971 chan->header_type == 1 ? "struct event_header_compact" :
972 "struct event_header_large");
973 if (ret)
974 goto end;
975
976 if (chan->ctx) {
977 ret = lttng_metadata_printf(session,
978 " event.context := struct {\n");
979 if (ret)
980 goto end;
981 }
982 ret = _ltt_context_metadata_statedump(session, chan->ctx);
983 if (ret)
984 goto end;
985 if (chan->ctx) {
986 ret = lttng_metadata_printf(session,
987 " };\n");
988 if (ret)
989 goto end;
990 }
991
992 ret = lttng_metadata_printf(session,
993 "};\n\n");
994
995 chan->metadata_dumped = 1;
996end:
997 return ret;
998}
999
1000static
1001int _ltt_stream_packet_context_declare(struct ltt_session *session)
1002{
1003 return lttng_metadata_printf(session,
1004 "struct packet_context {\n"
28b12049
MD
1005 " uint64_clock_monotonic_t timestamp_begin;\n"
1006 " uint64_clock_monotonic_t timestamp_end;\n"
8020ceb5
MD
1007 " uint32_t events_discarded;\n"
1008 " uint32_t content_size;\n"
1009 " uint32_t packet_size;\n"
1010 " uint32_t cpu_id;\n"
1011 "};\n\n"
1012 );
1013}
1014
1015/*
1016 * Compact header:
1017 * id: range: 0 - 30.
1018 * id 31 is reserved to indicate an extended header.
1019 *
1020 * Large header:
1021 * id: range: 0 - 65534.
1022 * id 65535 is reserved to indicate an extended header.
1023 */
1024static
1025int _ltt_event_header_declare(struct ltt_session *session)
1026{
1027 return lttng_metadata_printf(session,
1028 "struct event_header_compact {\n"
1029 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1030 " variant <id> {\n"
1031 " struct {\n"
28b12049 1032 " uint27_clock_monotonic_t timestamp;\n"
8020ceb5
MD
1033 " } compact;\n"
1034 " struct {\n"
1035 " uint32_t id;\n"
28b12049 1036 " uint64_clock_monotonic_t timestamp;\n"
8020ceb5
MD
1037 " } extended;\n"
1038 " } v;\n"
1039 "} align(%u);\n"
1040 "\n"
1041 "struct event_header_large {\n"
1042 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1043 " variant <id> {\n"
1044 " struct {\n"
28b12049 1045 " uint32_clock_monotonic_t timestamp;\n"
8020ceb5
MD
1046 " } compact;\n"
1047 " struct {\n"
1048 " uint32_t id;\n"
28b12049 1049 " uint64_clock_monotonic_t timestamp;\n"
8020ceb5
MD
1050 " } extended;\n"
1051 " } v;\n"
1052 "} align(%u);\n\n",
1dbfff0c
MD
1053 lttng_alignof(uint32_t) * CHAR_BIT,
1054 lttng_alignof(uint16_t) * CHAR_BIT
8020ceb5
MD
1055 );
1056}
1057
28b12049
MD
1058/*
1059 * Approximation of NTP time of day to clock monotonic correlation,
1060 * taken at start of trace.
1061 * Yes, this is only an approximation. Yes, we can (and will) do better
1062 * in future versions.
1063 */
1064static
1065uint64_t measure_clock_offset(void)
1066{
1067 uint64_t offset, monotonic[2], realtime;
1068 struct timespec rts = { 0, 0 };
1069 int ret;
1070
1071 monotonic[0] = trace_clock_read64();
1072 ret = clock_gettime(CLOCK_REALTIME, &rts);
1073 if (ret < 0)
1074 return 0;
1075 monotonic[1] = trace_clock_read64();
1076 offset = (monotonic[0] + monotonic[1]) >> 1;
1077 realtime = rts.tv_sec * 1000000000ULL;
1078 realtime += rts.tv_nsec;
1079 offset = realtime - offset;
1080 return offset;
1081}
1082
8020ceb5
MD
1083/*
1084 * Output metadata into this session's metadata buffers.
1085 */
1086static
1087int _ltt_session_metadata_statedump(struct ltt_session *session)
1088{
b5234c06 1089 unsigned char *uuid_c = session->uuid;
939950af 1090 char uuid_s[37], clock_uuid_s[CLOCK_UUID_LEN];
8020ceb5
MD
1091 struct ltt_channel *chan;
1092 struct ltt_event *event;
1093 int ret = 0;
b020e504 1094 char procname[PROCNAME_LEN] = "";
8020ceb5 1095
b5234c06 1096 if (!CMM_ACCESS_ONCE(session->active))
8020ceb5
MD
1097 return 0;
1098 if (session->metadata_dumped)
1099 goto skip_session;
1100 if (!session->metadata) {
b5234c06 1101 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
8020ceb5
MD
1102 return -EPERM;
1103 }
1104
1105 snprintf(uuid_s, sizeof(uuid_s),
1106 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1107 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1108 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1109 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1110 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1111
1112 ret = lttng_metadata_printf(session,
1113 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1114 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1115 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1116 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1117 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1118 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1119 "\n"
1120 "trace {\n"
1121 " major = %u;\n"
1122 " minor = %u;\n"
1123 " uuid = \"%s\";\n"
1124 " byte_order = %s;\n"
1125 " packet.header := struct {\n"
1126 " uint32_t magic;\n"
1127 " uint8_t uuid[16];\n"
1128 " uint32_t stream_id;\n"
1129 " };\n"
1130 "};\n\n",
1dbfff0c
MD
1131 lttng_alignof(uint8_t) * CHAR_BIT,
1132 lttng_alignof(uint16_t) * CHAR_BIT,
1133 lttng_alignof(uint32_t) * CHAR_BIT,
1134 lttng_alignof(uint64_t) * CHAR_BIT,
8020ceb5
MD
1135 CTF_VERSION_MAJOR,
1136 CTF_VERSION_MINOR,
1137 uuid_s,
8a98a75d 1138#if (BYTE_ORDER == BIG_ENDIAN)
8020ceb5
MD
1139 "be"
1140#else
1141 "le"
1142#endif
1143 );
1144 if (ret)
1145 goto end;
1146
b020e504
MD
1147 /* ignore error, just use empty string if error. */
1148 (void) prctl(PR_GET_NAME, (unsigned long) procname, 0, 0, 0);
1149 procname[PROCNAME_LEN - 1] = '\0';
1150 ret = lttng_metadata_printf(session,
1151 "env {\n"
1152 " vpid = %d;\n"
1153 " procname = \"%s\";\n"
1154 " domain = %s;\n"
1155 "};\n\n",
1156 (int) getpid(),
1157 procname,
1158 "ust"
1159 );
1160 if (ret)
1161 goto end;
1162
28b12049
MD
1163 ret = lttng_metadata_printf(session,
1164 "clock {\n"
939950af
MD
1165 " name = %s;\n",
1166 "monotonic"
1167 );
1168 if (ret)
1169 goto end;
1170
1171 if (!trace_clock_uuid(clock_uuid_s)) {
1172 ret = lttng_metadata_printf(session,
1173 " uuid = \"%s\";\n",
1174 clock_uuid_s
1175 );
1176 if (ret)
1177 goto end;
1178 }
1179
1180 ret = lttng_metadata_printf(session,
28b12049
MD
1181 " description = \"Monotonic Clock\";\n"
1182 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1183 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1184 " offset = %" PRIu64 ";\n"
1185 "};\n\n",
28b12049
MD
1186 trace_clock_freq(),
1187 measure_clock_offset()
1188 );
1189 if (ret)
1190 goto end;
1191
1192 ret = lttng_metadata_printf(session,
1193 "typealias integer {\n"
1194 " size = 27; align = 1; signed = false;\n"
1195 " map = clock.monotonic.value;\n"
1196 "} := uint27_clock_monotonic_t;\n"
1197 "\n"
1198 "typealias integer {\n"
1199 " size = 32; align = %u; signed = false;\n"
1200 " map = clock.monotonic.value;\n"
1201 "} := uint32_clock_monotonic_t;\n"
1202 "\n"
1203 "typealias integer {\n"
1204 " size = 64; align = %u; signed = false;\n"
1205 " map = clock.monotonic.value;\n"
1206 "} := uint64_clock_monotonic_t;\n\n",
1207 lttng_alignof(uint32_t) * CHAR_BIT,
1208 lttng_alignof(uint64_t) * CHAR_BIT
1209 );
1210 if (ret)
1211 goto end;
1212
8020ceb5
MD
1213 ret = _ltt_stream_packet_context_declare(session);
1214 if (ret)
1215 goto end;
1216
1217 ret = _ltt_event_header_declare(session);
1218 if (ret)
1219 goto end;
1220
1221skip_session:
b5234c06 1222 cds_list_for_each_entry(chan, &session->chan, list) {
8020ceb5
MD
1223 ret = _ltt_channel_metadata_statedump(session, chan);
1224 if (ret)
1225 goto end;
1226 }
1227
b5234c06 1228 cds_list_for_each_entry(event, &session->events, list) {
8020ceb5
MD
1229 ret = _ltt_event_metadata_statedump(session, event->chan, event);
1230 if (ret)
1231 goto end;
1232 }
1233 session->metadata_dumped = 1;
1234end:
1235 return ret;
1236}
1237
003fedf4 1238void lttng_ust_events_exit(void)
8020ceb5
MD
1239{
1240 struct ltt_session *session, *tmpsession;
1241
b5234c06 1242 cds_list_for_each_entry_safe(session, tmpsession, &sessions, list)
8020ceb5 1243 ltt_session_destroy(session);
8020ceb5 1244}
457a6b58
MD
1245
1246/* WILDCARDS */
1247
1248static
1249int wildcard_same_loglevel(struct wildcard_entry *e,
1250 enum lttng_ust_loglevel_type loglevel_type,
1251 int loglevel)
1252{
1253 if (e->loglevel_type == loglevel_type && e->loglevel == loglevel)
1254 return 1;
1255 else
1256 return 0;
1257}
1258
1259#if 0
1260static
1261int wildcard_is_within(struct wildcard_entry *e,
1262 enum lttng_ust_loglevel_type loglevel_type,
1263 int loglevel)
1264{
1265 if (e->loglevel_type == LTTNG_UST_LOGLEVEL_ALL
1266 || e->loglevel == -1)
1267 return 1;
1268 switch (e->loglevel_type) {
1269 case LTTNG_UST_LOGLEVEL_RANGE:
1270 switch (loglevel_type) {
1271 case LTTNG_UST_LOGLEVEL_RANGE:
1272 if (e->loglevel >= loglevel)
1273 return 1;
1274 else
1275 return 0;
1276 case LTTNG_UST_LOGLEVEL_SINGLE:
1277 if (e->loglevel <= 0 && loglevel == 0)
1278 return 1;
1279 else
1280 return 0;
1281 }
1282 case LTTNG_UST_LOGLEVEL_SINGLE:
1283 switch (loglevel_type) {
1284 case LTTNG_UST_LOGLEVEL_RANGE:
1285 if (loglevel <= 0)
1286 return 1;
1287 else
1288 return 0;
1289 case LTTNG_UST_LOGLEVEL_SINGLE:
1290 if (e->loglevel == loglevel)
1291 return 1;
1292 else
1293 return 0;
1294 }
1295 }
1296}
1297#endif
1298
1299/*
1300 * Add the wildcard to the wildcard list. Must be called with
1301 * ust lock held.
1302 */
1303static
1304struct session_wildcard *add_wildcard(struct ltt_channel *chan,
1305 struct lttng_ust_event *event_param)
1306{
1307 struct wildcard_entry *e;
1308 struct session_wildcard *sw;
1309 size_t name_len = strlen(event_param->name) + 1;
1310 int found = 0;
1311
1312 /*
1313 * Try to find global wildcard entry. Given that this is shared
1314 * across all sessions, we need to check for exact loglevel
1315 * match, not just whether contained within the existing ones.
1316 */
1317 cds_list_for_each_entry(e, &wildcard_list, list) {
1318 if (!strncmp(event_param->name, e->name,
1319 LTTNG_UST_SYM_NAME_LEN - 1)) {
1320 if (wildcard_same_loglevel(e,
1321 event_param->loglevel_type,
1322 event_param->loglevel)) {
1323 found = 1;
1324 break;
1325 }
1326 }
1327 }
1328
1329 if (!found) {
1330 /*
1331 * Create global wildcard entry if not found. Using
1332 * zmalloc here to allocate a variable length element.
1333 * Could cause some memory fragmentation if overused.
1334 */
1335 e = zmalloc(sizeof(struct wildcard_entry) + name_len);
1336 if (!e)
1337 return ERR_PTR(-ENOMEM);
1338 memcpy(&e->name[0], event_param->name, name_len);
1339 cds_list_add(&e->list, &wildcard_list);
1340 CDS_INIT_LIST_HEAD(&e->session_list);
1341 }
1342
1343 /* session wildcard */
1344 cds_list_for_each_entry(sw, &e->session_list, session_list) {
1345 if (chan == sw->chan) {
1346 DBG("wildcard %s busy for this channel",
1347 event_param->name);
1348 return ERR_PTR(-EEXIST); /* Already there */
1349 }
1350 }
1351 sw = zmalloc(sizeof(struct session_wildcard));
1352 if (!sw)
1353 return ERR_PTR(-ENOMEM);
1354 sw->chan = chan;
1355 sw->enabled = 1;
1356 memcpy(&sw->event_param, event_param, sizeof(sw->event_param));
1357 sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
1358 sw->event_param.loglevel_type = event_param->loglevel_type;
1359 sw->event_param.loglevel = event_param->loglevel;
1360 CDS_INIT_LIST_HEAD(&sw->events);
1361 cds_list_add(&sw->list, &chan->session->wildcards);
1362 cds_list_add(&sw->session_list, &e->session_list);
1363 sw->entry = e;
1364 ltt_probes_create_wildcard_events(e, sw);
1365 return sw;
1366}
1367
1368/*
1369 * Remove the wildcard from the wildcard list. Must be called with
1370 * ust_lock held. Only called at session teardown.
1371 */
1372static
1373void _remove_wildcard(struct session_wildcard *wildcard)
1374{
1375 struct ltt_event *ev, *tmp;
1376
1377 /*
1378 * Just remove the events owned (for enable/disable) by this
1379 * wildcard from the list. The session teardown will take care
1380 * of freeing the event memory.
1381 */
1382 cds_list_for_each_entry_safe(ev, tmp, &wildcard->events,
1383 wildcard_list) {
1384 cds_list_del(&ev->wildcard_list);
1385 }
1386 cds_list_del(&wildcard->session_list);
1387 cds_list_del(&wildcard->list);
1388 if (cds_list_empty(&wildcard->entry->session_list)) {
1389 cds_list_del(&wildcard->entry->list);
1390 free(wildcard->entry);
1391 }
1392 free(wildcard);
1393}
1394
1395int ltt_wildcard_create(struct ltt_channel *chan,
1396 struct lttng_ust_event *event_param,
1397 struct session_wildcard **_sw)
1398{
1399 struct session_wildcard *sw;
1400
1401 sw = add_wildcard(chan, event_param);
1402 if (!sw || IS_ERR(sw)) {
1403 return PTR_ERR(sw);
1404 }
1405 *_sw = sw;
1406 return 0;
1407}
1408
1409static
1410void _ltt_wildcard_destroy(struct session_wildcard *sw)
1411{
1412 _remove_wildcard(sw);
1413}
1414
1415int ltt_wildcard_enable(struct session_wildcard *wildcard)
1416{
1417 struct ltt_event *ev;
1418 int ret;
1419
1420 if (wildcard->enabled)
1421 return -EEXIST;
1422 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1423 ret = ltt_event_enable(ev);
1424 if (ret) {
1425 DBG("Error: enable error.\n");
1426 return ret;
1427 }
1428 }
1429 wildcard->enabled = 1;
1430 return 0;
1431}
1432
1433int ltt_wildcard_disable(struct session_wildcard *wildcard)
1434{
1435 struct ltt_event *ev;
1436 int ret;
1437
1438 if (!wildcard->enabled)
1439 return -EEXIST;
1440 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1441 ret = ltt_event_disable(ev);
1442 if (ret) {
1443 DBG("Error: disable error.\n");
1444 return ret;
1445 }
1446 }
1447 wildcard->enabled = 0;
1448 return 0;
1449}
This page took 0.133189 seconds and 4 git commands to generate.