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