Fix: take the ust lock around session iteration in statedump
[lttng-ust.git] / liblttng-ust / lttng-events.c
1 /*
2 * lttng-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 <lttng/ust-ctl.h>
49 #include <ust-comm.h>
50 #include "error.h"
51 #include "compat.h"
52 #include "lttng-ust-uuid.h"
53
54 #include "tracepoint-internal.h"
55 #include "lttng-tracer.h"
56 #include "lttng-tracer-core.h"
57 #include "lttng-ust-baddr.h"
58 #include "wait.h"
59 #include "../libringbuffer/shm.h"
60 #include "jhash.h"
61
62 /*
63 * The sessions mutex is the centralized mutex across UST tracing
64 * control and probe registration. All operations within this file are
65 * called by the communication thread, under ust_lock protection.
66 */
67 static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER;
68
69 void ust_lock(void)
70 {
71 pthread_mutex_lock(&sessions_mutex);
72 }
73
74 void ust_unlock(void)
75 {
76 pthread_mutex_unlock(&sessions_mutex);
77 }
78
79 static CDS_LIST_HEAD(sessions);
80
81 struct cds_list_head *_lttng_get_sessions(void)
82 {
83 return &sessions;
84 }
85
86 static void _lttng_event_destroy(struct lttng_event *event);
87
88 static
89 void lttng_session_lazy_sync_enablers(struct lttng_session *session);
90 static
91 void lttng_session_sync_enablers(struct lttng_session *session);
92 static
93 void lttng_enabler_destroy(struct lttng_enabler *enabler);
94
95 /*
96 * Called with ust lock held.
97 */
98 int lttng_session_active(void)
99 {
100 struct lttng_session *iter;
101
102 cds_list_for_each_entry(iter, &sessions, node) {
103 if (iter->active)
104 return 1;
105 }
106 return 0;
107 }
108
109 static
110 int lttng_loglevel_match(int loglevel,
111 unsigned int has_loglevel,
112 enum lttng_ust_loglevel_type req_type,
113 int req_loglevel)
114 {
115 if (req_type == LTTNG_UST_LOGLEVEL_ALL)
116 return 1;
117 if (!has_loglevel)
118 loglevel = TRACE_DEFAULT;
119 switch (req_type) {
120 case LTTNG_UST_LOGLEVEL_RANGE:
121 if (loglevel <= req_loglevel || req_loglevel == -1)
122 return 1;
123 else
124 return 0;
125 case LTTNG_UST_LOGLEVEL_SINGLE:
126 if (loglevel == req_loglevel || req_loglevel == -1)
127 return 1;
128 else
129 return 0;
130 case LTTNG_UST_LOGLEVEL_ALL:
131 default:
132 return 1;
133 }
134 }
135
136 void synchronize_trace(void)
137 {
138 synchronize_rcu();
139 }
140
141 struct lttng_session *lttng_session_create(void)
142 {
143 struct lttng_session *session;
144 int i;
145
146 session = zmalloc(sizeof(struct lttng_session));
147 if (!session)
148 return NULL;
149 CDS_INIT_LIST_HEAD(&session->chan_head);
150 CDS_INIT_LIST_HEAD(&session->events_head);
151 CDS_INIT_LIST_HEAD(&session->enablers_head);
152 for (i = 0; i < LTTNG_UST_EVENT_HT_SIZE; i++)
153 CDS_INIT_HLIST_HEAD(&session->events_ht.table[i]);
154 cds_list_add(&session->node, &sessions);
155 return session;
156 }
157
158 /*
159 * Only used internally at session destruction.
160 */
161 static
162 void _lttng_channel_unmap(struct lttng_channel *lttng_chan)
163 {
164 struct channel *chan;
165 struct lttng_ust_shm_handle *handle;
166
167 cds_list_del(&lttng_chan->node);
168 lttng_destroy_context(lttng_chan->ctx);
169 chan = lttng_chan->chan;
170 handle = lttng_chan->handle;
171 /*
172 * note: lttng_chan is private data contained within handle. It
173 * will be freed along with the handle.
174 */
175 channel_destroy(chan, handle, 0);
176 }
177
178 static
179 void register_event(struct lttng_event *event)
180 {
181 int ret;
182 const struct lttng_event_desc *desc;
183
184 assert(event->registered == 0);
185 desc = event->desc;
186 ret = __tracepoint_probe_register(desc->name,
187 desc->probe_callback,
188 event, desc->signature);
189 WARN_ON_ONCE(ret);
190 if (!ret)
191 event->registered = 1;
192 }
193
194 static
195 void unregister_event(struct lttng_event *event)
196 {
197 int ret;
198 const struct lttng_event_desc *desc;
199
200 assert(event->registered == 1);
201 desc = event->desc;
202 ret = __tracepoint_probe_unregister(desc->name,
203 desc->probe_callback,
204 event);
205 WARN_ON_ONCE(ret);
206 if (!ret)
207 event->registered = 0;
208 }
209
210 /*
211 * Only used internally at session destruction.
212 */
213 static
214 void _lttng_event_unregister(struct lttng_event *event)
215 {
216 if (event->registered)
217 unregister_event(event);
218 }
219
220 void lttng_session_destroy(struct lttng_session *session)
221 {
222 struct lttng_channel *chan, *tmpchan;
223 struct lttng_event *event, *tmpevent;
224 struct lttng_enabler *enabler, *tmpenabler;
225
226 CMM_ACCESS_ONCE(session->active) = 0;
227 cds_list_for_each_entry(event, &session->events_head, node) {
228 _lttng_event_unregister(event);
229 }
230 synchronize_trace(); /* Wait for in-flight events to complete */
231 cds_list_for_each_entry_safe(enabler, tmpenabler,
232 &session->enablers_head, node)
233 lttng_enabler_destroy(enabler);
234 cds_list_for_each_entry_safe(event, tmpevent,
235 &session->events_head, node)
236 _lttng_event_destroy(event);
237 cds_list_for_each_entry_safe(chan, tmpchan, &session->chan_head, node)
238 _lttng_channel_unmap(chan);
239 cds_list_del(&session->node);
240 free(session);
241 }
242
243 int lttng_session_enable(struct lttng_session *session)
244 {
245 int ret = 0;
246 struct lttng_channel *chan;
247 int notify_socket;
248
249 if (session->active) {
250 ret = -EBUSY;
251 goto end;
252 }
253
254 notify_socket = lttng_get_notify_socket(session->owner);
255 if (notify_socket < 0)
256 return notify_socket;
257
258 /* Set transient enabler state to "enabled" */
259 session->tstate = 1;
260 /* We need to sync enablers with session before activation. */
261 lttng_session_sync_enablers(session);
262
263 /*
264 * Snapshot the number of events per channel to know the type of header
265 * we need to use.
266 */
267 cds_list_for_each_entry(chan, &session->chan_head, node) {
268 const struct lttng_ctx *ctx;
269 const struct lttng_ctx_field *fields = NULL;
270 size_t nr_fields = 0;
271 uint32_t chan_id;
272
273 /* don't change it if session stop/restart */
274 if (chan->header_type)
275 continue;
276 ctx = chan->ctx;
277 if (ctx) {
278 nr_fields = ctx->nr_fields;
279 fields = ctx->fields;
280 }
281 ret = ustcomm_register_channel(notify_socket,
282 session->objd,
283 chan->objd,
284 nr_fields,
285 fields,
286 &chan_id,
287 &chan->header_type);
288 if (ret) {
289 DBG("Error (%d) registering channel to sessiond", ret);
290 return ret;
291 }
292 if (chan_id != chan->id) {
293 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
294 chan_id, chan->id);
295 return -EINVAL;
296 }
297 }
298
299 /* Set atomically the state to "active" */
300 CMM_ACCESS_ONCE(session->active) = 1;
301 CMM_ACCESS_ONCE(session->been_active) = 1;
302
303 session->statedump_pending = 1;
304 lttng_ust_sockinfo_session_enabled(session->owner);
305 end:
306 return ret;
307 }
308
309 int lttng_session_disable(struct lttng_session *session)
310 {
311 int ret = 0;
312
313 if (!session->active) {
314 ret = -EBUSY;
315 goto end;
316 }
317 /* Set atomically the state to "inactive" */
318 CMM_ACCESS_ONCE(session->active) = 0;
319
320 /* Set transient enabler state to "disabled" */
321 session->tstate = 0;
322 lttng_session_sync_enablers(session);
323 end:
324 return ret;
325 }
326
327 int lttng_channel_enable(struct lttng_channel *channel)
328 {
329 int ret = 0;
330
331 if (channel->enabled) {
332 ret = -EBUSY;
333 goto end;
334 }
335 /* Set transient enabler state to "enabled" */
336 channel->tstate = 1;
337 lttng_session_sync_enablers(channel->session);
338 /* Set atomically the state to "enabled" */
339 CMM_ACCESS_ONCE(channel->enabled) = 1;
340 end:
341 return ret;
342 }
343
344 int lttng_channel_disable(struct lttng_channel *channel)
345 {
346 int ret = 0;
347
348 if (!channel->enabled) {
349 ret = -EBUSY;
350 goto end;
351 }
352 /* Set atomically the state to "disabled" */
353 CMM_ACCESS_ONCE(channel->enabled) = 0;
354 /* Set transient enabler state to "enabled" */
355 channel->tstate = 0;
356 lttng_session_sync_enablers(channel->session);
357 end:
358 return ret;
359 }
360
361 /*
362 * Supports event creation while tracing session is active.
363 */
364 static
365 int lttng_event_create(const struct lttng_event_desc *desc,
366 struct lttng_channel *chan)
367 {
368 const char *event_name = desc->name;
369 struct lttng_event *event;
370 struct lttng_session *session = chan->session;
371 struct cds_hlist_head *head;
372 struct cds_hlist_node *node;
373 int ret = 0;
374 size_t name_len = strlen(event_name);
375 uint32_t hash;
376 int notify_socket, loglevel;
377 const char *uri;
378
379 hash = jhash(event_name, name_len, 0);
380 head = &chan->session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)];
381 cds_hlist_for_each_entry(event, node, head, hlist) {
382 assert(event->desc);
383 if (!strncmp(event->desc->name, desc->name,
384 LTTNG_UST_SYM_NAME_LEN - 1)
385 && chan == event->chan) {
386 ret = -EEXIST;
387 goto exist;
388 }
389 }
390
391 notify_socket = lttng_get_notify_socket(session->owner);
392 if (notify_socket < 0) {
393 ret = notify_socket;
394 goto socket_error;
395 }
396
397 /*
398 * Check if loglevel match. Refuse to connect event if not.
399 */
400 event = zmalloc(sizeof(struct lttng_event));
401 if (!event) {
402 ret = -ENOMEM;
403 goto cache_error;
404 }
405 event->chan = chan;
406
407 /* Event will be enabled by enabler sync. */
408 event->enabled = 0;
409 event->registered = 0;
410 CDS_INIT_LIST_HEAD(&event->bytecode_runtime_head);
411 CDS_INIT_LIST_HEAD(&event->enablers_ref_head);
412 event->desc = desc;
413
414 if (desc->loglevel)
415 loglevel = *(*event->desc->loglevel);
416 else
417 loglevel = TRACE_DEFAULT;
418 if (desc->u.ext.model_emf_uri)
419 uri = *(desc->u.ext.model_emf_uri);
420 else
421 uri = NULL;
422
423 /* Fetch event ID from sessiond */
424 ret = ustcomm_register_event(notify_socket,
425 session->objd,
426 chan->objd,
427 event_name,
428 loglevel,
429 desc->signature,
430 desc->nr_fields,
431 desc->fields,
432 uri,
433 &event->id);
434 if (ret < 0) {
435 DBG("Error (%d) registering event to sessiond", ret);
436 goto sessiond_register_error;
437 }
438
439 /* Populate lttng_event structure before tracepoint registration. */
440 cmm_smp_wmb();
441 cds_list_add(&event->node, &chan->session->events_head);
442 cds_hlist_add_head(&event->hlist, head);
443 return 0;
444
445 sessiond_register_error:
446 free(event);
447 cache_error:
448 socket_error:
449 exist:
450 return ret;
451 }
452
453 static
454 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc *desc,
455 struct lttng_enabler *enabler)
456 {
457 int loglevel = 0;
458 unsigned int has_loglevel = 0;
459
460 assert(enabler->type == LTTNG_ENABLER_WILDCARD);
461 /* Compare excluding final '*' */
462 if (strncmp(desc->name, enabler->event_param.name,
463 strlen(enabler->event_param.name) - 1))
464 return 0;
465 if (desc->loglevel) {
466 loglevel = *(*desc->loglevel);
467 has_loglevel = 1;
468 }
469 if (!lttng_loglevel_match(loglevel,
470 has_loglevel,
471 enabler->event_param.loglevel_type,
472 enabler->event_param.loglevel))
473 return 0;
474 return 1;
475 }
476
477 static
478 int lttng_desc_match_event_enabler(const struct lttng_event_desc *desc,
479 struct lttng_enabler *enabler)
480 {
481 int loglevel = 0;
482 unsigned int has_loglevel = 0;
483
484 assert(enabler->type == LTTNG_ENABLER_EVENT);
485 if (strcmp(desc->name, enabler->event_param.name))
486 return 0;
487 if (desc->loglevel) {
488 loglevel = *(*desc->loglevel);
489 has_loglevel = 1;
490 }
491 if (!lttng_loglevel_match(loglevel,
492 has_loglevel,
493 enabler->event_param.loglevel_type,
494 enabler->event_param.loglevel))
495 return 0;
496 return 1;
497 }
498
499 static
500 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
501 struct lttng_enabler *enabler)
502 {
503 struct lttng_ust_excluder_node *excluder;
504
505 /* If event matches with an excluder, return 'does not match' */
506 cds_list_for_each_entry(excluder, &enabler->excluder_head, node) {
507 int count;
508
509 for (count = 0; count < excluder->excluder.count; count++) {
510 int found, len;
511 char *excluder_name;
512
513 excluder_name = (char *) (excluder->excluder.names)
514 + count * LTTNG_UST_SYM_NAME_LEN;
515 len = strnlen(excluder_name, LTTNG_UST_SYM_NAME_LEN);
516 if (len > 0 && excluder_name[len - 1] == '*') {
517 found = !strncmp(desc->name, excluder_name,
518 len - 1);
519 } else {
520 found = !strncmp(desc->name, excluder_name,
521 LTTNG_UST_SYM_NAME_LEN - 1);
522 }
523 if (found) {
524 return 0;
525 }
526 }
527 }
528 switch (enabler->type) {
529 case LTTNG_ENABLER_WILDCARD:
530 return lttng_desc_match_wildcard_enabler(desc, enabler);
531 case LTTNG_ENABLER_EVENT:
532 return lttng_desc_match_event_enabler(desc, enabler);
533 default:
534 return -EINVAL;
535 }
536 }
537
538 static
539 int lttng_event_match_enabler(struct lttng_event *event,
540 struct lttng_enabler *enabler)
541 {
542 if (lttng_desc_match_enabler(event->desc, enabler)
543 && event->chan == enabler->chan)
544 return 1;
545 else
546 return 0;
547 }
548
549 static
550 struct lttng_enabler_ref * lttng_event_enabler_ref(struct lttng_event *event,
551 struct lttng_enabler *enabler)
552 {
553 struct lttng_enabler_ref *enabler_ref;
554
555 cds_list_for_each_entry(enabler_ref,
556 &event->enablers_ref_head, node) {
557 if (enabler_ref->ref == enabler)
558 return enabler_ref;
559 }
560 return NULL;
561 }
562
563 /*
564 * Create struct lttng_event if it is missing and present in the list of
565 * tracepoint probes.
566 */
567 static
568 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
569 {
570 struct lttng_session *session = enabler->chan->session;
571 struct lttng_probe_desc *probe_desc;
572 const struct lttng_event_desc *desc;
573 struct lttng_event *event;
574 int i;
575 struct cds_list_head *probe_list;
576
577 probe_list = lttng_get_probe_list_head();
578 /*
579 * For each probe event, if we find that a probe event matches
580 * our enabler, create an associated lttng_event if not
581 * already present.
582 */
583 cds_list_for_each_entry(probe_desc, probe_list, head) {
584 for (i = 0; i < probe_desc->nr_events; i++) {
585 int found = 0, ret;
586 struct cds_hlist_head *head;
587 struct cds_hlist_node *node;
588 const char *event_name;
589 size_t name_len;
590 uint32_t hash;
591
592 desc = probe_desc->event_desc[i];
593 if (!lttng_desc_match_enabler(desc, enabler))
594 continue;
595 event_name = desc->name;
596 name_len = strlen(event_name);
597
598 /*
599 * Check if already created.
600 */
601 hash = jhash(event_name, name_len, 0);
602 head = &session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)];
603 cds_hlist_for_each_entry(event, node, head, hlist) {
604 if (event->desc == desc
605 && event->chan == enabler->chan)
606 found = 1;
607 }
608 if (found)
609 continue;
610
611 /*
612 * We need to create an event for this
613 * event probe.
614 */
615 ret = lttng_event_create(probe_desc->event_desc[i],
616 enabler->chan);
617 if (ret) {
618 DBG("Unable to create event %s, error %d\n",
619 probe_desc->event_desc[i]->name, ret);
620 }
621 }
622 }
623 }
624
625 /*
626 * Create events associated with an enabler (if not already present),
627 * and add backward reference from the event to the enabler.
628 */
629 static
630 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
631 {
632 struct lttng_session *session = enabler->chan->session;
633 struct lttng_event *event;
634
635 /* First ensure that probe events are created for this enabler. */
636 lttng_create_event_if_missing(enabler);
637
638 /* For each event matching enabler in session event list. */
639 cds_list_for_each_entry(event, &session->events_head, node) {
640 struct lttng_enabler_ref *enabler_ref;
641
642 if (!lttng_event_match_enabler(event, enabler))
643 continue;
644
645 enabler_ref = lttng_event_enabler_ref(event, enabler);
646 if (!enabler_ref) {
647 /*
648 * If no backward ref, create it.
649 * Add backward ref from event to enabler.
650 */
651 enabler_ref = zmalloc(sizeof(*enabler_ref));
652 if (!enabler_ref)
653 return -ENOMEM;
654 enabler_ref->ref = enabler;
655 cds_list_add(&enabler_ref->node,
656 &event->enablers_ref_head);
657 }
658
659 /*
660 * Link filter bytecodes if not linked yet.
661 */
662 lttng_enabler_event_link_bytecode(event, enabler);
663
664 /* TODO: merge event context. */
665 }
666 return 0;
667 }
668
669 /*
670 * Called at library load: connect the probe on all enablers matching
671 * this event.
672 * Called with session mutex held.
673 */
674 int lttng_fix_pending_events(void)
675 {
676 struct lttng_session *session;
677
678 cds_list_for_each_entry(session, &sessions, node) {
679 lttng_session_lazy_sync_enablers(session);
680 }
681 return 0;
682 }
683
684 /*
685 * For each session of the owner thread, execute pending statedump.
686 * Only dump state for the sessions owned by the caller thread, because
687 * we don't keep ust_lock across the entire iteration.
688 */
689 int lttng_handle_pending_statedump(void *owner)
690 {
691 struct lttng_session *session;
692
693 /* Execute state dump */
694 lttng_ust_baddr_statedump(owner);
695
696 /* Clear pending state dump */
697 ust_lock();
698 cds_list_for_each_entry(session, &sessions, node) {
699 if (session->owner != owner)
700 continue;
701 if (!session->statedump_pending)
702 continue;
703 session->statedump_pending = 0;
704 }
705 ust_unlock();
706 return 0;
707 }
708
709 /*
710 * Only used internally at session destruction.
711 */
712 static
713 void _lttng_event_destroy(struct lttng_event *event)
714 {
715 struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref;
716
717 cds_list_del(&event->node);
718 lttng_destroy_context(event->ctx);
719 lttng_free_event_filter_runtime(event);
720 /* Free event enabler refs */
721 cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref,
722 &event->enablers_ref_head, node)
723 free(enabler_ref);
724 free(event);
725 }
726
727 void lttng_ust_events_exit(void)
728 {
729 struct lttng_session *session, *tmpsession;
730
731 cds_list_for_each_entry_safe(session, tmpsession, &sessions, node)
732 lttng_session_destroy(session);
733 }
734
735 /*
736 * Enabler management.
737 */
738 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
739 struct lttng_ust_event *event_param,
740 struct lttng_channel *chan)
741 {
742 struct lttng_enabler *enabler;
743
744 enabler = zmalloc(sizeof(*enabler));
745 if (!enabler)
746 return NULL;
747 enabler->type = type;
748 CDS_INIT_LIST_HEAD(&enabler->filter_bytecode_head);
749 CDS_INIT_LIST_HEAD(&enabler->excluder_head);
750 memcpy(&enabler->event_param, event_param,
751 sizeof(enabler->event_param));
752 enabler->chan = chan;
753 /* ctx left NULL */
754 enabler->enabled = 1;
755 cds_list_add(&enabler->node, &enabler->chan->session->enablers_head);
756 lttng_session_lazy_sync_enablers(enabler->chan->session);
757 return enabler;
758 }
759
760 int lttng_enabler_enable(struct lttng_enabler *enabler)
761 {
762 enabler->enabled = 1;
763 lttng_session_lazy_sync_enablers(enabler->chan->session);
764 return 0;
765 }
766
767 int lttng_enabler_disable(struct lttng_enabler *enabler)
768 {
769 enabler->enabled = 0;
770 lttng_session_lazy_sync_enablers(enabler->chan->session);
771 return 0;
772 }
773
774 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
775 struct lttng_ust_filter_bytecode_node *bytecode)
776 {
777 bytecode->enabler = enabler;
778 cds_list_add_tail(&bytecode->node, &enabler->filter_bytecode_head);
779 lttng_session_lazy_sync_enablers(enabler->chan->session);
780 return 0;
781 }
782
783 int lttng_enabler_attach_exclusion(struct lttng_enabler *enabler,
784 struct lttng_ust_excluder_node *excluder)
785 {
786 excluder->enabler = enabler;
787 cds_list_add_tail(&excluder->node, &enabler->excluder_head);
788 lttng_session_lazy_sync_enablers(enabler->chan->session);
789 return 0;
790 }
791
792 int lttng_attach_context(struct lttng_ust_context *context_param,
793 struct lttng_ctx **ctx, struct lttng_session *session)
794 {
795 /*
796 * We cannot attach a context after trace has been started for a
797 * session because the metadata does not allow expressing this
798 * information outside of the original channel scope.
799 */
800 if (session->been_active)
801 return -EPERM;
802
803 switch (context_param->ctx) {
804 case LTTNG_UST_CONTEXT_PTHREAD_ID:
805 return lttng_add_pthread_id_to_ctx(ctx);
806 case LTTNG_UST_CONTEXT_VTID:
807 return lttng_add_vtid_to_ctx(ctx);
808 case LTTNG_UST_CONTEXT_VPID:
809 return lttng_add_vpid_to_ctx(ctx);
810 case LTTNG_UST_CONTEXT_PROCNAME:
811 return lttng_add_procname_to_ctx(ctx);
812 case LTTNG_UST_CONTEXT_IP:
813 return lttng_add_ip_to_ctx(ctx);
814 default:
815 return -EINVAL;
816 }
817 }
818
819 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
820 struct lttng_ust_context *context_param)
821 {
822 #if 0 // disabled for now.
823 struct lttng_session *session = enabler->chan->session;
824 int ret;
825
826 ret = lttng_attach_context(context_param, &enabler->ctx,
827 session);
828 if (ret)
829 return ret;
830 lttng_session_lazy_sync_enablers(enabler->chan->session);
831 #endif
832 return -ENOSYS;
833 }
834
835 static
836 void lttng_enabler_destroy(struct lttng_enabler *enabler)
837 {
838 struct lttng_ust_filter_bytecode_node *filter_node, *tmp_filter_node;
839 struct lttng_ust_excluder_node *excluder_node, *tmp_excluder_node;
840
841 /* Destroy filter bytecode */
842 cds_list_for_each_entry_safe(filter_node, tmp_filter_node,
843 &enabler->filter_bytecode_head, node) {
844 free(filter_node);
845 }
846
847 /* Destroy excluders */
848 cds_list_for_each_entry_safe(excluder_node, tmp_excluder_node,
849 &enabler->excluder_head, node) {
850 free(excluder_node);
851 }
852
853 /* Destroy contexts */
854 lttng_destroy_context(enabler->ctx);
855
856 cds_list_del(&enabler->node);
857 free(enabler);
858 }
859
860 /*
861 * lttng_session_sync_enablers should be called just before starting a
862 * session.
863 */
864 static
865 void lttng_session_sync_enablers(struct lttng_session *session)
866 {
867 struct lttng_enabler *enabler;
868 struct lttng_event *event;
869
870 cds_list_for_each_entry(enabler, &session->enablers_head, node)
871 lttng_enabler_ref_events(enabler);
872 /*
873 * For each event, if at least one of its enablers is enabled,
874 * and its channel and session transient states are enabled, we
875 * enable the event, else we disable it.
876 */
877 cds_list_for_each_entry(event, &session->events_head, node) {
878 struct lttng_enabler_ref *enabler_ref;
879 struct lttng_bytecode_runtime *runtime;
880 int enabled = 0, has_enablers_without_bytecode = 0;
881
882 /* Enable events */
883 cds_list_for_each_entry(enabler_ref,
884 &event->enablers_ref_head, node) {
885 if (enabler_ref->ref->enabled) {
886 enabled = 1;
887 break;
888 }
889 }
890 /*
891 * Enabled state is based on union of enablers, with
892 * intesection of session and channel transient enable
893 * states.
894 */
895 enabled = enabled && session->tstate && event->chan->tstate;
896
897 CMM_STORE_SHARED(event->enabled, enabled);
898 /*
899 * Sync tracepoint registration with event enabled
900 * state.
901 */
902 if (enabled) {
903 if (!event->registered)
904 register_event(event);
905 } else {
906 if (event->registered)
907 unregister_event(event);
908 }
909
910 /* Check if has enablers without bytecode enabled */
911 cds_list_for_each_entry(enabler_ref,
912 &event->enablers_ref_head, node) {
913 if (enabler_ref->ref->enabled
914 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
915 has_enablers_without_bytecode = 1;
916 break;
917 }
918 }
919 event->has_enablers_without_bytecode =
920 has_enablers_without_bytecode;
921
922 /* Enable filters */
923 cds_list_for_each_entry(runtime,
924 &event->bytecode_runtime_head, node) {
925 lttng_filter_sync_state(runtime);
926 }
927 }
928 }
929
930 /*
931 * Apply enablers to session events, adding events to session if need
932 * be. It is required after each modification applied to an active
933 * session, and right before session "start".
934 * "lazy" sync means we only sync if required.
935 */
936 static
937 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
938 {
939 /* We can skip if session is not active */
940 if (!session->active)
941 return;
942 lttng_session_sync_enablers(session);
943 }
This page took 0.049 seconds and 5 git commands to generate.