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