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