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