6b9170a03ed63b0feb5f680c41cedd2b728d4345
[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 " ip = %p;\n"
1020 " file = \"%s\";\n"
1021 " line = %u;\n"
1022 "};\n\n",
1023 callsite->tp_cs->name,
1024 callsite->tp_cs->func,
1025 callsite->tp_cs->ip,
1026 callsite->tp_cs->file,
1027 callsite->tp_cs->lineno);
1028 if (ret)
1029 goto end;
1030 end:
1031 return ret;
1032 }
1033
1034 static
1035 int _ltt_event_metadata_statedump(struct ltt_session *session,
1036 struct ltt_channel *chan,
1037 struct ltt_event *event)
1038 {
1039 int ret = 0;
1040 int loglevel = TRACE_DEFAULT;
1041
1042 if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
1043 return 0;
1044 if (chan == session->metadata)
1045 return 0;
1046 /*
1047 * Don't print events for which probe load is pending.
1048 */
1049 if (!event->desc)
1050 return 0;
1051
1052 ret = lttng_metadata_printf(session,
1053 "event {\n"
1054 " name = \"%s\";\n"
1055 " id = %u;\n"
1056 " stream_id = %u;\n",
1057 event->desc->name,
1058 event->id,
1059 event->chan->id);
1060 if (ret)
1061 goto end;
1062
1063 if (event->desc->loglevel)
1064 loglevel = *(*event->desc->loglevel);
1065
1066 ret = lttng_metadata_printf(session,
1067 " loglevel = %d;\n",
1068 loglevel);
1069 if (ret)
1070 goto end;
1071
1072 if (event->desc->u.ext.model_emf_uri) {
1073 ret = lttng_metadata_printf(session,
1074 " model.emf.uri = \"%s\";\n",
1075 *(event->desc->u.ext.model_emf_uri));
1076 if (ret)
1077 goto end;
1078 }
1079
1080 if (event->ctx) {
1081 ret = lttng_metadata_printf(session,
1082 " context := struct {\n");
1083 if (ret)
1084 goto end;
1085 }
1086 ret = _ltt_context_metadata_statedump(session, event->ctx);
1087 if (ret)
1088 goto end;
1089 if (event->ctx) {
1090 ret = lttng_metadata_printf(session,
1091 " };\n");
1092 if (ret)
1093 goto end;
1094 }
1095
1096 ret = lttng_metadata_printf(session,
1097 " fields := struct {\n"
1098 );
1099 if (ret)
1100 goto end;
1101
1102 ret = _ltt_fields_metadata_statedump(session, event);
1103 if (ret)
1104 goto end;
1105
1106 /*
1107 * LTTng space reservation can only reserve multiples of the
1108 * byte size.
1109 */
1110 ret = lttng_metadata_printf(session,
1111 " };\n"
1112 "};\n\n");
1113 if (ret)
1114 goto end;
1115
1116 event->metadata_dumped = 1;
1117 end:
1118 return ret;
1119
1120 }
1121
1122 static
1123 int _ltt_channel_metadata_statedump(struct ltt_session *session,
1124 struct ltt_channel *chan)
1125 {
1126 int ret = 0;
1127
1128 if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
1129 return 0;
1130 if (chan == session->metadata)
1131 return 0;
1132
1133 WARN_ON_ONCE(!chan->header_type);
1134 ret = lttng_metadata_printf(session,
1135 "stream {\n"
1136 " id = %u;\n"
1137 " event.header := %s;\n"
1138 " packet.context := struct packet_context;\n",
1139 chan->id,
1140 chan->header_type == 1 ? "struct event_header_compact" :
1141 "struct event_header_large");
1142 if (ret)
1143 goto end;
1144
1145 if (chan->ctx) {
1146 ret = lttng_metadata_printf(session,
1147 " event.context := struct {\n");
1148 if (ret)
1149 goto end;
1150 }
1151 ret = _ltt_context_metadata_statedump(session, chan->ctx);
1152 if (ret)
1153 goto end;
1154 if (chan->ctx) {
1155 ret = lttng_metadata_printf(session,
1156 " };\n");
1157 if (ret)
1158 goto end;
1159 }
1160
1161 ret = lttng_metadata_printf(session,
1162 "};\n\n");
1163
1164 chan->metadata_dumped = 1;
1165 end:
1166 return ret;
1167 }
1168
1169 static
1170 int _ltt_stream_packet_context_declare(struct ltt_session *session)
1171 {
1172 return lttng_metadata_printf(session,
1173 "struct packet_context {\n"
1174 " uint64_clock_monotonic_t timestamp_begin;\n"
1175 " uint64_clock_monotonic_t timestamp_end;\n"
1176 " uint64_t content_size;\n"
1177 " uint64_t packet_size;\n"
1178 " unsigned long events_discarded;\n"
1179 " uint32_t cpu_id;\n"
1180 "};\n\n"
1181 );
1182 }
1183
1184 /*
1185 * Compact header:
1186 * id: range: 0 - 30.
1187 * id 31 is reserved to indicate an extended header.
1188 *
1189 * Large header:
1190 * id: range: 0 - 65534.
1191 * id 65535 is reserved to indicate an extended header.
1192 */
1193 static
1194 int _ltt_event_header_declare(struct ltt_session *session)
1195 {
1196 return lttng_metadata_printf(session,
1197 "struct event_header_compact {\n"
1198 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1199 " variant <id> {\n"
1200 " struct {\n"
1201 " uint27_clock_monotonic_t timestamp;\n"
1202 " } compact;\n"
1203 " struct {\n"
1204 " uint32_t id;\n"
1205 " uint64_clock_monotonic_t timestamp;\n"
1206 " } extended;\n"
1207 " } v;\n"
1208 "} align(%u);\n"
1209 "\n"
1210 "struct event_header_large {\n"
1211 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1212 " variant <id> {\n"
1213 " struct {\n"
1214 " uint32_clock_monotonic_t timestamp;\n"
1215 " } compact;\n"
1216 " struct {\n"
1217 " uint32_t id;\n"
1218 " uint64_clock_monotonic_t timestamp;\n"
1219 " } extended;\n"
1220 " } v;\n"
1221 "} align(%u);\n\n",
1222 lttng_alignof(uint32_t) * CHAR_BIT,
1223 lttng_alignof(uint16_t) * CHAR_BIT
1224 );
1225 }
1226
1227 /*
1228 * Approximation of NTP time of day to clock monotonic correlation,
1229 * taken at start of trace.
1230 * Yes, this is only an approximation. Yes, we can (and will) do better
1231 * in future versions.
1232 */
1233 static
1234 uint64_t measure_clock_offset(void)
1235 {
1236 uint64_t offset, monotonic[2], realtime;
1237 struct timespec rts = { 0, 0 };
1238 int ret;
1239
1240 monotonic[0] = trace_clock_read64();
1241 ret = clock_gettime(CLOCK_REALTIME, &rts);
1242 if (ret < 0)
1243 return 0;
1244 monotonic[1] = trace_clock_read64();
1245 offset = (monotonic[0] + monotonic[1]) >> 1;
1246 realtime = (uint64_t) rts.tv_sec * 1000000000ULL;
1247 realtime += rts.tv_nsec;
1248 offset = realtime - offset;
1249 return offset;
1250 }
1251
1252 /*
1253 * Output metadata into this session's metadata buffers.
1254 */
1255 static
1256 int _ltt_session_metadata_statedump(struct ltt_session *session)
1257 {
1258 unsigned char *uuid_c = session->uuid;
1259 char uuid_s[LTTNG_UST_UUID_STR_LEN],
1260 clock_uuid_s[LTTNG_UST_UUID_STR_LEN];
1261 struct ltt_channel *chan;
1262 struct ltt_event *event;
1263 int ret = 0;
1264 char procname[LTTNG_UST_PROCNAME_LEN] = "";
1265 char hostname[HOST_NAME_MAX];
1266
1267 if (!CMM_ACCESS_ONCE(session->active))
1268 return 0;
1269 if (session->metadata_dumped)
1270 goto skip_session;
1271 if (!session->metadata) {
1272 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1273 return -EPERM;
1274 }
1275
1276 snprintf(uuid_s, sizeof(uuid_s),
1277 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1278 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1279 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1280 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1281 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1282
1283 ret = lttng_metadata_printf(session,
1284 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1285 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1286 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1287 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1288 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1289 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1290 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1291 "\n"
1292 "trace {\n"
1293 " major = %u;\n"
1294 " minor = %u;\n"
1295 " uuid = \"%s\";\n"
1296 " byte_order = %s;\n"
1297 " packet.header := struct {\n"
1298 " uint32_t magic;\n"
1299 " uint8_t uuid[16];\n"
1300 " uint32_t stream_id;\n"
1301 " };\n"
1302 "};\n\n",
1303 lttng_alignof(uint8_t) * CHAR_BIT,
1304 lttng_alignof(uint16_t) * CHAR_BIT,
1305 lttng_alignof(uint32_t) * CHAR_BIT,
1306 lttng_alignof(uint64_t) * CHAR_BIT,
1307 sizeof(unsigned long) * CHAR_BIT,
1308 lttng_alignof(unsigned long) * CHAR_BIT,
1309 CTF_SPEC_MAJOR,
1310 CTF_SPEC_MINOR,
1311 uuid_s,
1312 #if (BYTE_ORDER == BIG_ENDIAN)
1313 "be"
1314 #else
1315 "le"
1316 #endif
1317 );
1318 if (ret)
1319 goto end;
1320
1321 /* ignore error, just use empty string if error. */
1322 hostname[0] = '\0';
1323 ret = gethostname(hostname, sizeof(hostname));
1324 if (ret && errno == ENAMETOOLONG)
1325 hostname[HOST_NAME_MAX - 1] = '\0';
1326 lttng_ust_getprocname(procname);
1327 procname[LTTNG_UST_PROCNAME_LEN - 1] = '\0';
1328 ret = lttng_metadata_printf(session,
1329 "env {\n"
1330 " hostname = \"%s\";\n"
1331 " vpid = %d;\n"
1332 " procname = \"%s\";\n"
1333 " domain = \"ust\";\n"
1334 " tracer_name = \"lttng-ust\";\n"
1335 " tracer_major = %u;\n"
1336 " tracer_minor = %u;\n"
1337 " tracer_patchlevel = %u;\n"
1338 "};\n\n",
1339 hostname,
1340 (int) getpid(),
1341 procname,
1342 LTTNG_UST_MAJOR_VERSION,
1343 LTTNG_UST_MINOR_VERSION,
1344 LTTNG_UST_PATCHLEVEL_VERSION
1345 );
1346 if (ret)
1347 goto end;
1348
1349 ret = lttng_metadata_printf(session,
1350 "clock {\n"
1351 " name = %s;\n",
1352 "monotonic"
1353 );
1354 if (ret)
1355 goto end;
1356
1357 if (!trace_clock_uuid(clock_uuid_s)) {
1358 ret = lttng_metadata_printf(session,
1359 " uuid = \"%s\";\n",
1360 clock_uuid_s
1361 );
1362 if (ret)
1363 goto end;
1364 }
1365
1366 ret = lttng_metadata_printf(session,
1367 " description = \"Monotonic Clock\";\n"
1368 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1369 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1370 " offset = %" PRIu64 ";\n"
1371 "};\n\n",
1372 trace_clock_freq(),
1373 measure_clock_offset()
1374 );
1375 if (ret)
1376 goto end;
1377
1378 ret = lttng_metadata_printf(session,
1379 "typealias integer {\n"
1380 " size = 27; align = 1; signed = false;\n"
1381 " map = clock.monotonic.value;\n"
1382 "} := uint27_clock_monotonic_t;\n"
1383 "\n"
1384 "typealias integer {\n"
1385 " size = 32; align = %u; signed = false;\n"
1386 " map = clock.monotonic.value;\n"
1387 "} := uint32_clock_monotonic_t;\n"
1388 "\n"
1389 "typealias integer {\n"
1390 " size = 64; align = %u; signed = false;\n"
1391 " map = clock.monotonic.value;\n"
1392 "} := uint64_clock_monotonic_t;\n\n",
1393 lttng_alignof(uint32_t) * CHAR_BIT,
1394 lttng_alignof(uint64_t) * CHAR_BIT
1395 );
1396 if (ret)
1397 goto end;
1398
1399 ret = _ltt_stream_packet_context_declare(session);
1400 if (ret)
1401 goto end;
1402
1403 ret = _ltt_event_header_declare(session);
1404 if (ret)
1405 goto end;
1406 ret = _callsite_session_metadata_statedump(session);
1407 if (ret)
1408 goto end;
1409
1410 skip_session:
1411 cds_list_for_each_entry(chan, &session->chan, list) {
1412 ret = _ltt_channel_metadata_statedump(session, chan);
1413 if (ret)
1414 goto end;
1415 }
1416
1417 cds_list_for_each_entry(event, &session->events, list) {
1418 ret = _ltt_event_metadata_statedump(session, event->chan, event);
1419 if (ret)
1420 goto end;
1421 }
1422 session->metadata_dumped = 1;
1423 end:
1424 return ret;
1425 }
1426
1427 void lttng_ust_events_exit(void)
1428 {
1429 struct ltt_session *session, *tmpsession;
1430
1431 cds_list_for_each_entry_safe(session, tmpsession, &sessions, list)
1432 ltt_session_destroy(session);
1433 }
1434
1435 /* WILDCARDS */
1436
1437 static
1438 int wildcard_same_loglevel(struct wildcard_entry *e,
1439 enum lttng_ust_loglevel_type loglevel_type,
1440 int loglevel)
1441 {
1442 if (e->loglevel_type == loglevel_type && e->loglevel == loglevel)
1443 return 1;
1444 else
1445 return 0;
1446 }
1447
1448 #if 0
1449 static
1450 int wildcard_is_within(struct wildcard_entry *e,
1451 enum lttng_ust_loglevel_type loglevel_type,
1452 int loglevel)
1453 {
1454 if (e->loglevel_type == LTTNG_UST_LOGLEVEL_ALL
1455 || e->loglevel == -1)
1456 return 1;
1457 switch (e->loglevel_type) {
1458 case LTTNG_UST_LOGLEVEL_RANGE:
1459 switch (loglevel_type) {
1460 case LTTNG_UST_LOGLEVEL_RANGE:
1461 if (e->loglevel >= loglevel)
1462 return 1;
1463 else
1464 return 0;
1465 case LTTNG_UST_LOGLEVEL_SINGLE:
1466 if (e->loglevel <= 0 && loglevel == 0)
1467 return 1;
1468 else
1469 return 0;
1470 }
1471 case LTTNG_UST_LOGLEVEL_SINGLE:
1472 switch (loglevel_type) {
1473 case LTTNG_UST_LOGLEVEL_RANGE:
1474 if (loglevel <= 0)
1475 return 1;
1476 else
1477 return 0;
1478 case LTTNG_UST_LOGLEVEL_SINGLE:
1479 if (e->loglevel == loglevel)
1480 return 1;
1481 else
1482 return 0;
1483 }
1484 }
1485 }
1486 #endif
1487
1488 /*
1489 * Add the wildcard to the wildcard list. Must be called with
1490 * ust lock held.
1491 */
1492 static
1493 struct session_wildcard *add_wildcard(struct ltt_channel *chan,
1494 struct lttng_ust_event *event_param)
1495 {
1496 struct wildcard_entry *e;
1497 struct session_wildcard *sw;
1498 size_t name_len = strlen(event_param->name) + 1;
1499 int found = 0;
1500
1501 /*
1502 * Try to find global wildcard entry. Given that this is shared
1503 * across all sessions, we need to check for exact loglevel
1504 * match, not just whether contained within the existing ones.
1505 */
1506 cds_list_for_each_entry(e, &wildcard_list, list) {
1507 if (!strncmp(event_param->name, e->name,
1508 LTTNG_UST_SYM_NAME_LEN - 1)) {
1509 if (wildcard_same_loglevel(e,
1510 event_param->loglevel_type,
1511 event_param->loglevel)) {
1512 found = 1;
1513 break;
1514 }
1515 }
1516 }
1517
1518 if (!found) {
1519 /*
1520 * Create global wildcard entry if not found. Using
1521 * zmalloc here to allocate a variable length element.
1522 * Could cause some memory fragmentation if overused.
1523 */
1524 e = zmalloc(sizeof(struct wildcard_entry) + name_len);
1525 if (!e)
1526 return ERR_PTR(-ENOMEM);
1527 memcpy(&e->name[0], event_param->name, name_len);
1528 e->loglevel_type = event_param->loglevel_type;
1529 e->loglevel = event_param->loglevel;
1530 cds_list_add(&e->list, &wildcard_list);
1531 CDS_INIT_LIST_HEAD(&e->session_list);
1532 }
1533
1534 /* session wildcard */
1535 cds_list_for_each_entry(sw, &e->session_list, session_list) {
1536 if (chan == sw->chan) {
1537 DBG("wildcard %s busy for this channel",
1538 event_param->name);
1539 return ERR_PTR(-EEXIST); /* Already there */
1540 }
1541 }
1542 sw = zmalloc(sizeof(struct session_wildcard));
1543 if (!sw)
1544 return ERR_PTR(-ENOMEM);
1545 sw->chan = chan;
1546 sw->enabled = 1;
1547 memcpy(&sw->event_param, event_param, sizeof(sw->event_param));
1548 sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
1549 sw->event_param.loglevel_type = event_param->loglevel_type;
1550 sw->event_param.loglevel = event_param->loglevel;
1551 CDS_INIT_LIST_HEAD(&sw->events);
1552 cds_list_add(&sw->list, &chan->session->wildcards);
1553 cds_list_add(&sw->session_list, &e->session_list);
1554 sw->entry = e;
1555 ltt_probes_create_wildcard_events(e, sw);
1556 return sw;
1557 }
1558
1559 /*
1560 * Remove the wildcard from the wildcard list. Must be called with
1561 * ust_lock held. Only called at session teardown.
1562 */
1563 static
1564 void _remove_wildcard(struct session_wildcard *wildcard)
1565 {
1566 struct ltt_event *ev, *tmp;
1567
1568 /*
1569 * Just remove the events owned (for enable/disable) by this
1570 * wildcard from the list. The session teardown will take care
1571 * of freeing the event memory.
1572 */
1573 cds_list_for_each_entry_safe(ev, tmp, &wildcard->events,
1574 wildcard_list) {
1575 cds_list_del(&ev->wildcard_list);
1576 }
1577 cds_list_del(&wildcard->session_list);
1578 cds_list_del(&wildcard->list);
1579 if (cds_list_empty(&wildcard->entry->session_list)) {
1580 cds_list_del(&wildcard->entry->list);
1581 free(wildcard->entry);
1582 }
1583 free(wildcard->filter_bytecode);
1584 free(wildcard);
1585 }
1586
1587 int ltt_wildcard_create(struct ltt_channel *chan,
1588 struct lttng_ust_event *event_param,
1589 struct session_wildcard **_sw)
1590 {
1591 struct session_wildcard *sw;
1592
1593 sw = add_wildcard(chan, event_param);
1594 if (!sw || IS_ERR(sw)) {
1595 return PTR_ERR(sw);
1596 }
1597 *_sw = sw;
1598 return 0;
1599 }
1600
1601 static
1602 void _ltt_wildcard_destroy(struct session_wildcard *sw)
1603 {
1604 _remove_wildcard(sw);
1605 }
1606
1607 int ltt_wildcard_enable(struct session_wildcard *wildcard)
1608 {
1609 struct ltt_event *ev;
1610 int ret;
1611
1612 if (wildcard->enabled)
1613 return -EEXIST;
1614 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1615 ret = ltt_event_enable(ev);
1616 if (ret) {
1617 DBG("Error: enable error.\n");
1618 return ret;
1619 }
1620 }
1621 wildcard->enabled = 1;
1622 return 0;
1623 }
1624
1625 int ltt_wildcard_disable(struct session_wildcard *wildcard)
1626 {
1627 struct ltt_event *ev;
1628 int ret;
1629
1630 if (!wildcard->enabled)
1631 return -EEXIST;
1632 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1633 ret = ltt_event_disable(ev);
1634 if (ret) {
1635 DBG("Error: disable error.\n");
1636 return ret;
1637 }
1638 }
1639 wildcard->enabled = 0;
1640 return 0;
1641 }
1642
1643 /*
1644 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1645 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1646 * taken in constructors, which are called with dynamic linker mutex
1647 * held.
1648 */
1649 void lttng_fixup_event_tls(void)
1650 {
1651 unsigned char uuid[LTTNG_UST_UUID_STR_LEN];
1652
1653 (void) lttng_ust_uuid_generate(uuid);
1654 }
This page took 0.062197 seconds and 4 git commands to generate.