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