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