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