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 * Ensure that a state-dump will be performed for this session at the end
390 * of the current handle_message().
391 */
392 int lttng_session_statedump(struct lttng_session *session)
393 {
394 session->statedump_pending = 1;
395 lttng_ust_sockinfo_session_enabled(session->owner);
396 return 0;
397 }
398
399 int lttng_session_enable(struct lttng_session *session)
400 {
401 int ret = 0;
402 struct lttng_channel *chan;
403 int notify_socket;
404
405 if (session->active) {
406 ret = -EBUSY;
407 goto end;
408 }
409
410 notify_socket = lttng_get_notify_socket(session->owner);
411 if (notify_socket < 0)
412 return notify_socket;
413
414 /* Set transient enabler state to "enabled" */
415 session->tstate = 1;
416
417 /*
418 * Snapshot the number of events per channel to know the type of header
419 * we need to use.
420 */
421 cds_list_for_each_entry(chan, &session->chan_head, node) {
422 const struct lttng_ctx *ctx;
423 const struct lttng_ctx_field *fields = NULL;
424 size_t nr_fields = 0;
425 uint32_t chan_id;
426
427 /* don't change it if session stop/restart */
428 if (chan->header_type)
429 continue;
430 ctx = chan->ctx;
431 if (ctx) {
432 nr_fields = ctx->nr_fields;
433 fields = ctx->fields;
434 ret = lttng_create_all_ctx_enums(nr_fields, fields,
435 session);
436 if (ret < 0) {
437 DBG("Error (%d) adding enum to session", ret);
438 return ret;
439 }
440 }
441 ret = ustcomm_register_channel(notify_socket,
442 session,
443 session->objd,
444 chan->objd,
445 nr_fields,
446 fields,
447 &chan_id,
448 &chan->header_type);
449 if (ret) {
450 DBG("Error (%d) registering channel to sessiond", ret);
451 return ret;
452 }
453 if (chan_id != chan->id) {
454 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
455 chan_id, chan->id);
456 return -EINVAL;
457 }
458 }
459
460 /* We need to sync enablers with session before activation. */
461 lttng_session_sync_enablers(session);
462
463 /* Set atomically the state to "active" */
464 CMM_ACCESS_ONCE(session->active) = 1;
465 CMM_ACCESS_ONCE(session->been_active) = 1;
466
467 ret = lttng_session_statedump(session);
468 if (ret)
469 return ret;
470 end:
471 return ret;
472 }
473
474 int lttng_session_disable(struct lttng_session *session)
475 {
476 int ret = 0;
477
478 if (!session->active) {
479 ret = -EBUSY;
480 goto end;
481 }
482 /* Set atomically the state to "inactive" */
483 CMM_ACCESS_ONCE(session->active) = 0;
484
485 /* Set transient enabler state to "disabled" */
486 session->tstate = 0;
487 lttng_session_sync_enablers(session);
488 end:
489 return ret;
490 }
491
492 int lttng_channel_enable(struct lttng_channel *channel)
493 {
494 int ret = 0;
495
496 if (channel->enabled) {
497 ret = -EBUSY;
498 goto end;
499 }
500 /* Set transient enabler state to "enabled" */
501 channel->tstate = 1;
502 lttng_session_sync_enablers(channel->session);
503 /* Set atomically the state to "enabled" */
504 CMM_ACCESS_ONCE(channel->enabled) = 1;
505 end:
506 return ret;
507 }
508
509 int lttng_channel_disable(struct lttng_channel *channel)
510 {
511 int ret = 0;
512
513 if (!channel->enabled) {
514 ret = -EBUSY;
515 goto end;
516 }
517 /* Set atomically the state to "disabled" */
518 CMM_ACCESS_ONCE(channel->enabled) = 0;
519 /* Set transient enabler state to "enabled" */
520 channel->tstate = 0;
521 lttng_session_sync_enablers(channel->session);
522 end:
523 return ret;
524 }
525
526 /*
527 * Supports event creation while tracing session is active.
528 */
529 static
530 int lttng_event_create(const struct lttng_event_desc *desc,
531 struct lttng_channel *chan)
532 {
533 const char *event_name = desc->name;
534 struct lttng_event *event;
535 struct lttng_session *session = chan->session;
536 struct cds_hlist_head *head;
537 struct cds_hlist_node *node;
538 int ret = 0;
539 size_t name_len = strlen(event_name);
540 uint32_t hash;
541 int notify_socket, loglevel;
542 const char *uri;
543
544 hash = jhash(event_name, name_len, 0);
545 head = &chan->session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)];
546 cds_hlist_for_each_entry(event, node, head, hlist) {
547 assert(event->desc);
548 if (!strncmp(event->desc->name, desc->name,
549 LTTNG_UST_SYM_NAME_LEN - 1)
550 && chan == event->chan) {
551 ret = -EEXIST;
552 goto exist;
553 }
554 }
555
556 notify_socket = lttng_get_notify_socket(session->owner);
557 if (notify_socket < 0) {
558 ret = notify_socket;
559 goto socket_error;
560 }
561
562 ret = lttng_create_all_event_enums(desc->nr_fields, desc->fields,
563 session);
564 if (ret < 0) {
565 DBG("Error (%d) adding enum to session", ret);
566 goto create_enum_error;
567 }
568
569 /*
570 * Check if loglevel match. Refuse to connect event if not.
571 */
572 event = zmalloc(sizeof(struct lttng_event));
573 if (!event) {
574 ret = -ENOMEM;
575 goto cache_error;
576 }
577 event->chan = chan;
578
579 /* Event will be enabled by enabler sync. */
580 event->enabled = 0;
581 event->registered = 0;
582 CDS_INIT_LIST_HEAD(&event->bytecode_runtime_head);
583 CDS_INIT_LIST_HEAD(&event->enablers_ref_head);
584 event->desc = desc;
585
586 if (desc->loglevel)
587 loglevel = *(*event->desc->loglevel);
588 else
589 loglevel = TRACE_DEFAULT;
590 if (desc->u.ext.model_emf_uri)
591 uri = *(desc->u.ext.model_emf_uri);
592 else
593 uri = NULL;
594
595 /* Fetch event ID from sessiond */
596 ret = ustcomm_register_event(notify_socket,
597 session,
598 session->objd,
599 chan->objd,
600 event_name,
601 loglevel,
602 desc->signature,
603 desc->nr_fields,
604 desc->fields,
605 uri,
606 &event->id);
607 if (ret < 0) {
608 DBG("Error (%d) registering event to sessiond", ret);
609 goto sessiond_register_error;
610 }
611
612 /* Populate lttng_event structure before tracepoint registration. */
613 cmm_smp_wmb();
614 cds_list_add(&event->node, &chan->session->events_head);
615 cds_hlist_add_head(&event->hlist, head);
616 return 0;
617
618 sessiond_register_error:
619 free(event);
620 cache_error:
621 create_enum_error:
622 socket_error:
623 exist:
624 return ret;
625 }
626
627 static
628 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc *desc,
629 struct lttng_enabler *enabler)
630 {
631 int loglevel = 0;
632 unsigned int has_loglevel = 0;
633
634 assert(enabler->type == LTTNG_ENABLER_WILDCARD);
635 /* Compare excluding final '*' */
636 if (strncmp(desc->name, enabler->event_param.name,
637 strlen(enabler->event_param.name) - 1))
638 return 0;
639 if (desc->loglevel) {
640 loglevel = *(*desc->loglevel);
641 has_loglevel = 1;
642 }
643 if (!lttng_loglevel_match(loglevel,
644 has_loglevel,
645 enabler->event_param.loglevel_type,
646 enabler->event_param.loglevel))
647 return 0;
648 return 1;
649 }
650
651 static
652 int lttng_desc_match_event_enabler(const struct lttng_event_desc *desc,
653 struct lttng_enabler *enabler)
654 {
655 int loglevel = 0;
656 unsigned int has_loglevel = 0;
657
658 assert(enabler->type == LTTNG_ENABLER_EVENT);
659 if (strcmp(desc->name, enabler->event_param.name))
660 return 0;
661 if (desc->loglevel) {
662 loglevel = *(*desc->loglevel);
663 has_loglevel = 1;
664 }
665 if (!lttng_loglevel_match(loglevel,
666 has_loglevel,
667 enabler->event_param.loglevel_type,
668 enabler->event_param.loglevel))
669 return 0;
670 return 1;
671 }
672
673 static
674 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
675 struct lttng_enabler *enabler)
676 {
677 struct lttng_ust_excluder_node *excluder;
678
679 /* If event matches with an excluder, return 'does not match' */
680 cds_list_for_each_entry(excluder, &enabler->excluder_head, node) {
681 int count;
682
683 for (count = 0; count < excluder->excluder.count; count++) {
684 int found, len;
685 char *excluder_name;
686
687 excluder_name = (char *) (excluder->excluder.names)
688 + count * LTTNG_UST_SYM_NAME_LEN;
689 len = strnlen(excluder_name, LTTNG_UST_SYM_NAME_LEN);
690 if (len > 0 && excluder_name[len - 1] == '*') {
691 found = !strncmp(desc->name, excluder_name,
692 len - 1);
693 } else {
694 found = !strncmp(desc->name, excluder_name,
695 LTTNG_UST_SYM_NAME_LEN - 1);
696 }
697 if (found) {
698 return 0;
699 }
700 }
701 }
702 switch (enabler->type) {
703 case LTTNG_ENABLER_WILDCARD:
704 return lttng_desc_match_wildcard_enabler(desc, enabler);
705 case LTTNG_ENABLER_EVENT:
706 return lttng_desc_match_event_enabler(desc, enabler);
707 default:
708 return -EINVAL;
709 }
710 }
711
712 static
713 int lttng_event_match_enabler(struct lttng_event *event,
714 struct lttng_enabler *enabler)
715 {
716 if (lttng_desc_match_enabler(event->desc, enabler)
717 && event->chan == enabler->chan)
718 return 1;
719 else
720 return 0;
721 }
722
723 static
724 struct lttng_enabler_ref * lttng_event_enabler_ref(struct lttng_event *event,
725 struct lttng_enabler *enabler)
726 {
727 struct lttng_enabler_ref *enabler_ref;
728
729 cds_list_for_each_entry(enabler_ref,
730 &event->enablers_ref_head, node) {
731 if (enabler_ref->ref == enabler)
732 return enabler_ref;
733 }
734 return NULL;
735 }
736
737 /*
738 * Create struct lttng_event if it is missing and present in the list of
739 * tracepoint probes.
740 */
741 static
742 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
743 {
744 struct lttng_session *session = enabler->chan->session;
745 struct lttng_probe_desc *probe_desc;
746 const struct lttng_event_desc *desc;
747 struct lttng_event *event;
748 int i;
749 struct cds_list_head *probe_list;
750
751 probe_list = lttng_get_probe_list_head();
752 /*
753 * For each probe event, if we find that a probe event matches
754 * our enabler, create an associated lttng_event if not
755 * already present.
756 */
757 cds_list_for_each_entry(probe_desc, probe_list, head) {
758 for (i = 0; i < probe_desc->nr_events; i++) {
759 int found = 0, ret;
760 struct cds_hlist_head *head;
761 struct cds_hlist_node *node;
762 const char *event_name;
763 size_t name_len;
764 uint32_t hash;
765
766 desc = probe_desc->event_desc[i];
767 if (!lttng_desc_match_enabler(desc, enabler))
768 continue;
769 event_name = desc->name;
770 name_len = strlen(event_name);
771
772 /*
773 * Check if already created.
774 */
775 hash = jhash(event_name, name_len, 0);
776 head = &session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)];
777 cds_hlist_for_each_entry(event, node, head, hlist) {
778 if (event->desc == desc
779 && event->chan == enabler->chan)
780 found = 1;
781 }
782 if (found)
783 continue;
784
785 /*
786 * We need to create an event for this
787 * event probe.
788 */
789 ret = lttng_event_create(probe_desc->event_desc[i],
790 enabler->chan);
791 if (ret) {
792 DBG("Unable to create event %s, error %d\n",
793 probe_desc->event_desc[i]->name, ret);
794 }
795 }
796 }
797 }
798
799 /*
800 * Create events associated with an enabler (if not already present),
801 * and add backward reference from the event to the enabler.
802 */
803 static
804 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
805 {
806 struct lttng_session *session = enabler->chan->session;
807 struct lttng_event *event;
808
809 /* First ensure that probe events are created for this enabler. */
810 lttng_create_event_if_missing(enabler);
811
812 /* For each event matching enabler in session event list. */
813 cds_list_for_each_entry(event, &session->events_head, node) {
814 struct lttng_enabler_ref *enabler_ref;
815
816 if (!lttng_event_match_enabler(event, enabler))
817 continue;
818
819 enabler_ref = lttng_event_enabler_ref(event, enabler);
820 if (!enabler_ref) {
821 /*
822 * If no backward ref, create it.
823 * Add backward ref from event to enabler.
824 */
825 enabler_ref = zmalloc(sizeof(*enabler_ref));
826 if (!enabler_ref)
827 return -ENOMEM;
828 enabler_ref->ref = enabler;
829 cds_list_add(&enabler_ref->node,
830 &event->enablers_ref_head);
831 }
832
833 /*
834 * Link filter bytecodes if not linked yet.
835 */
836 lttng_enabler_event_link_bytecode(event, enabler);
837
838 /* TODO: merge event context. */
839 }
840 return 0;
841 }
842
843 /*
844 * Called at library load: connect the probe on all enablers matching
845 * this event.
846 * Called with session mutex held.
847 */
848 int lttng_fix_pending_events(void)
849 {
850 struct lttng_session *session;
851
852 cds_list_for_each_entry(session, &sessions, node) {
853 lttng_session_lazy_sync_enablers(session);
854 }
855 return 0;
856 }
857
858 /*
859 * For each session of the owner thread, execute pending statedump.
860 * Only dump state for the sessions owned by the caller thread, because
861 * we don't keep ust_lock across the entire iteration.
862 */
863 void lttng_handle_pending_statedump(void *owner)
864 {
865 struct lttng_session *session;
866
867 /* Execute state dump */
868 do_lttng_ust_statedump(owner);
869
870 /* Clear pending state dump */
871 if (ust_lock()) {
872 goto end;
873 }
874 cds_list_for_each_entry(session, &sessions, node) {
875 if (session->owner != owner)
876 continue;
877 if (!session->statedump_pending)
878 continue;
879 session->statedump_pending = 0;
880 }
881 end:
882 ust_unlock();
883 return;
884 }
885
886 /*
887 * Only used internally at session destruction.
888 */
889 static
890 void _lttng_event_destroy(struct lttng_event *event)
891 {
892 struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref;
893
894 cds_list_del(&event->node);
895 lttng_destroy_context(event->ctx);
896 lttng_free_event_filter_runtime(event);
897 /* Free event enabler refs */
898 cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref,
899 &event->enablers_ref_head, node)
900 free(enabler_ref);
901 free(event);
902 }
903
904 static
905 void _lttng_enum_destroy(struct lttng_enum *_enum)
906 {
907 cds_list_del(&_enum->node);
908 free(_enum);
909 }
910
911 void lttng_ust_events_exit(void)
912 {
913 struct lttng_session *session, *tmpsession;
914
915 cds_list_for_each_entry_safe(session, tmpsession, &sessions, node)
916 lttng_session_destroy(session);
917 }
918
919 /*
920 * Enabler management.
921 */
922 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
923 struct lttng_ust_event *event_param,
924 struct lttng_channel *chan)
925 {
926 struct lttng_enabler *enabler;
927
928 enabler = zmalloc(sizeof(*enabler));
929 if (!enabler)
930 return NULL;
931 enabler->type = type;
932 CDS_INIT_LIST_HEAD(&enabler->filter_bytecode_head);
933 CDS_INIT_LIST_HEAD(&enabler->excluder_head);
934 memcpy(&enabler->event_param, event_param,
935 sizeof(enabler->event_param));
936 enabler->chan = chan;
937 /* ctx left NULL */
938 enabler->enabled = 0;
939 cds_list_add(&enabler->node, &enabler->chan->session->enablers_head);
940 lttng_session_lazy_sync_enablers(enabler->chan->session);
941 return enabler;
942 }
943
944 int lttng_enabler_enable(struct lttng_enabler *enabler)
945 {
946 enabler->enabled = 1;
947 lttng_session_lazy_sync_enablers(enabler->chan->session);
948 return 0;
949 }
950
951 int lttng_enabler_disable(struct lttng_enabler *enabler)
952 {
953 enabler->enabled = 0;
954 lttng_session_lazy_sync_enablers(enabler->chan->session);
955 return 0;
956 }
957
958 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
959 struct lttng_ust_filter_bytecode_node *bytecode)
960 {
961 bytecode->enabler = enabler;
962 cds_list_add_tail(&bytecode->node, &enabler->filter_bytecode_head);
963 lttng_session_lazy_sync_enablers(enabler->chan->session);
964 return 0;
965 }
966
967 int lttng_enabler_attach_exclusion(struct lttng_enabler *enabler,
968 struct lttng_ust_excluder_node *excluder)
969 {
970 excluder->enabler = enabler;
971 cds_list_add_tail(&excluder->node, &enabler->excluder_head);
972 lttng_session_lazy_sync_enablers(enabler->chan->session);
973 return 0;
974 }
975
976 int lttng_attach_context(struct lttng_ust_context *context_param,
977 union ust_args *uargs,
978 struct lttng_ctx **ctx, struct lttng_session *session)
979 {
980 /*
981 * We cannot attach a context after trace has been started for a
982 * session because the metadata does not allow expressing this
983 * information outside of the original channel scope.
984 */
985 if (session->been_active)
986 return -EPERM;
987
988 switch (context_param->ctx) {
989 case LTTNG_UST_CONTEXT_PTHREAD_ID:
990 return lttng_add_pthread_id_to_ctx(ctx);
991 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
992 {
993 struct lttng_ust_perf_counter_ctx *perf_ctx_param;
994
995 perf_ctx_param = &context_param->u.perf_counter;
996 return lttng_add_perf_counter_to_ctx(
997 perf_ctx_param->type,
998 perf_ctx_param->config,
999 perf_ctx_param->name,
1000 ctx);
1001 }
1002 case LTTNG_UST_CONTEXT_VTID:
1003 return lttng_add_vtid_to_ctx(ctx);
1004 case LTTNG_UST_CONTEXT_VPID:
1005 return lttng_add_vpid_to_ctx(ctx);
1006 case LTTNG_UST_CONTEXT_PROCNAME:
1007 return lttng_add_procname_to_ctx(ctx);
1008 case LTTNG_UST_CONTEXT_IP:
1009 return lttng_add_ip_to_ctx(ctx);
1010 case LTTNG_UST_CONTEXT_CPU_ID:
1011 return lttng_add_cpu_id_to_ctx(ctx);
1012 case LTTNG_UST_CONTEXT_APP_CONTEXT:
1013 return lttng_ust_add_app_context_to_ctx_rcu(uargs->app_context.ctxname,
1014 ctx);
1015 default:
1016 return -EINVAL;
1017 }
1018 }
1019
1020 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1021 struct lttng_ust_context *context_param)
1022 {
1023 #if 0 // disabled for now.
1024 struct lttng_session *session = enabler->chan->session;
1025 int ret;
1026
1027 ret = lttng_attach_context(context_param, &enabler->ctx,
1028 session);
1029 if (ret)
1030 return ret;
1031 lttng_session_lazy_sync_enablers(enabler->chan->session);
1032 #endif
1033 return -ENOSYS;
1034 }
1035
1036 static
1037 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1038 {
1039 struct lttng_ust_filter_bytecode_node *filter_node, *tmp_filter_node;
1040 struct lttng_ust_excluder_node *excluder_node, *tmp_excluder_node;
1041
1042 /* Destroy filter bytecode */
1043 cds_list_for_each_entry_safe(filter_node, tmp_filter_node,
1044 &enabler->filter_bytecode_head, node) {
1045 free(filter_node);
1046 }
1047
1048 /* Destroy excluders */
1049 cds_list_for_each_entry_safe(excluder_node, tmp_excluder_node,
1050 &enabler->excluder_head, node) {
1051 free(excluder_node);
1052 }
1053
1054 /* Destroy contexts */
1055 lttng_destroy_context(enabler->ctx);
1056
1057 cds_list_del(&enabler->node);
1058 free(enabler);
1059 }
1060
1061 /*
1062 * lttng_session_sync_enablers should be called just before starting a
1063 * session.
1064 */
1065 static
1066 void lttng_session_sync_enablers(struct lttng_session *session)
1067 {
1068 struct lttng_enabler *enabler;
1069 struct lttng_event *event;
1070
1071 cds_list_for_each_entry(enabler, &session->enablers_head, node)
1072 lttng_enabler_ref_events(enabler);
1073 /*
1074 * For each event, if at least one of its enablers is enabled,
1075 * and its channel and session transient states are enabled, we
1076 * enable the event, else we disable it.
1077 */
1078 cds_list_for_each_entry(event, &session->events_head, node) {
1079 struct lttng_enabler_ref *enabler_ref;
1080 struct lttng_bytecode_runtime *runtime;
1081 int enabled = 0, has_enablers_without_bytecode = 0;
1082
1083 /* Enable events */
1084 cds_list_for_each_entry(enabler_ref,
1085 &event->enablers_ref_head, node) {
1086 if (enabler_ref->ref->enabled) {
1087 enabled = 1;
1088 break;
1089 }
1090 }
1091 /*
1092 * Enabled state is based on union of enablers, with
1093 * intesection of session and channel transient enable
1094 * states.
1095 */
1096 enabled = enabled && session->tstate && event->chan->tstate;
1097
1098 CMM_STORE_SHARED(event->enabled, enabled);
1099 /*
1100 * Sync tracepoint registration with event enabled
1101 * state.
1102 */
1103 if (enabled) {
1104 if (!event->registered)
1105 register_event(event);
1106 } else {
1107 if (event->registered)
1108 unregister_event(event);
1109 }
1110
1111 /* Check if has enablers without bytecode enabled */
1112 cds_list_for_each_entry(enabler_ref,
1113 &event->enablers_ref_head, node) {
1114 if (enabler_ref->ref->enabled
1115 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1116 has_enablers_without_bytecode = 1;
1117 break;
1118 }
1119 }
1120 event->has_enablers_without_bytecode =
1121 has_enablers_without_bytecode;
1122
1123 /* Enable filters */
1124 cds_list_for_each_entry(runtime,
1125 &event->bytecode_runtime_head, node) {
1126 lttng_filter_sync_state(runtime);
1127 }
1128 }
1129 __tracepoint_probe_prune_release_queue();
1130 }
1131
1132 /*
1133 * Apply enablers to session events, adding events to session if need
1134 * be. It is required after each modification applied to an active
1135 * session, and right before session "start".
1136 * "lazy" sync means we only sync if required.
1137 */
1138 static
1139 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1140 {
1141 /* We can skip if session is not active */
1142 if (!session->active)
1143 return;
1144 lttng_session_sync_enablers(session);
1145 }
1146
1147 /*
1148 * Update all sessions with the given app context.
1149 * Called with ust lock held.
1150 * This is invoked when an application context gets loaded/unloaded. It
1151 * ensures the context callbacks are in sync with the application
1152 * context (either app context callbacks, or dummy callbacks).
1153 */
1154 void lttng_ust_context_set_session_provider(const char *name,
1155 size_t (*get_size)(struct lttng_ctx_field *field, size_t offset),
1156 void (*record)(struct lttng_ctx_field *field,
1157 struct lttng_ust_lib_ring_buffer_ctx *ctx,
1158 struct lttng_channel *chan),
1159 void (*get_value)(struct lttng_ctx_field *field,
1160 struct lttng_ctx_value *value))
1161 {
1162 struct lttng_session *session;
1163
1164 cds_list_for_each_entry(session, &sessions, node) {
1165 struct lttng_channel *chan;
1166 struct lttng_event *event;
1167 int ret;
1168
1169 ret = lttng_ust_context_set_provider_rcu(&session->ctx,
1170 name, get_size, record, get_value);
1171 if (ret)
1172 abort();
1173 cds_list_for_each_entry(chan, &session->chan_head, node) {
1174 ret = lttng_ust_context_set_provider_rcu(&chan->ctx,
1175 name, get_size, record, get_value);
1176 if (ret)
1177 abort();
1178 }
1179 cds_list_for_each_entry(event, &session->events_head, node) {
1180 ret = lttng_ust_context_set_provider_rcu(&event->ctx,
1181 name, get_size, record, get_value);
1182 if (ret)
1183 abort();
1184 }
1185 }
1186 }
This page took 0.058648 seconds and 5 git commands to generate.