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