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