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