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