Use 'ust-' prefix for all global private headers
[lttng-ust.git] / liblttng-ust / lttng-events.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng per-session event registry.
7 */
8
9 #define _LGPL_SOURCE
10 #include <stdio.h>
11 #include <assert.h>
12 #include <errno.h>
13 #include <limits.h>
14 #include <pthread.h>
15 #include <sys/shm.h>
16 #include <sys/ipc.h>
17 #include <stdint.h>
18 #include <stddef.h>
19 #include <inttypes.h>
20 #include <time.h>
21 #include <stdbool.h>
22 #include <unistd.h>
23 #include <dlfcn.h>
24 #include <lttng/ust-endian.h>
25
26 #include <urcu/arch.h>
27 #include <urcu/compiler.h>
28 #include <urcu/hlist.h>
29 #include <urcu/list.h>
30 #include <urcu/uatomic.h>
31
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
34
35 #include <usterr-signal-safe.h>
36 #include <ust-helper.h>
37 #include <lttng/ust-ctl.h>
38 #include <ust-comm.h>
39 #include <ust-fd.h>
40 #include <ust-dynamic-type.h>
41 #include <ust-context-provider.h>
42 #include "error.h"
43 #include "compat.h"
44 #include "lttng-ust-uuid.h"
45
46 #include "tracepoint-internal.h"
47 #include "string-utils.h"
48 #include "lttng-bytecode.h"
49 #include "lttng-tracer.h"
50 #include "lttng-tracer-core.h"
51 #include "lttng-ust-statedump.h"
52 #include "context-internal.h"
53 #include "ust-events-internal.h"
54 #include "wait.h"
55 #include "../libringbuffer/shm.h"
56 #include "../libcounter/counter.h"
57 #include "jhash.h"
58 #include <lttng/ust-abi.h>
59
60 /*
61 * All operations within this file are called by the communication
62 * thread, under ust_lock protection.
63 */
64
65 static CDS_LIST_HEAD(sessions);
66 static CDS_LIST_HEAD(event_notifier_groups);
67
68 struct cds_list_head *_lttng_get_sessions(void)
69 {
70 return &sessions;
71 }
72
73 static void _lttng_event_destroy(struct lttng_event *event);
74 static void _lttng_event_notifier_destroy(
75 struct lttng_event_notifier *event_notifier);
76 static void _lttng_enum_destroy(struct lttng_enum *_enum);
77
78 static
79 void lttng_session_lazy_sync_event_enablers(struct lttng_session *session);
80 static
81 void lttng_session_sync_event_enablers(struct lttng_session *session);
82 static
83 void lttng_event_notifier_group_sync_enablers(
84 struct lttng_event_notifier_group *event_notifier_group);
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 struct lttng_session *lttng_session_create(void)
133 {
134 struct lttng_session *session;
135 int i;
136
137 session = zmalloc(sizeof(struct lttng_session));
138 if (!session)
139 return NULL;
140 if (lttng_context_init_all(&session->ctx)) {
141 free(session);
142 return NULL;
143 }
144 CDS_INIT_LIST_HEAD(&session->chan_head);
145 CDS_INIT_LIST_HEAD(&session->events_head);
146 CDS_INIT_LIST_HEAD(&session->enums_head);
147 CDS_INIT_LIST_HEAD(&session->enablers_head);
148 for (i = 0; i < LTTNG_UST_EVENT_HT_SIZE; i++)
149 CDS_INIT_HLIST_HEAD(&session->events_ht.table[i]);
150 for (i = 0; i < LTTNG_UST_ENUM_HT_SIZE; i++)
151 CDS_INIT_HLIST_HEAD(&session->enums_ht.table[i]);
152 cds_list_add(&session->node, &sessions);
153 return session;
154 }
155
156 struct lttng_counter *lttng_ust_counter_create(
157 const char *counter_transport_name,
158 size_t number_dimensions, const struct lttng_counter_dimension *dimensions)
159 {
160 struct lttng_counter_transport *counter_transport = NULL;
161 struct lttng_counter *counter = NULL;
162
163 counter_transport = lttng_counter_transport_find(counter_transport_name);
164 if (!counter_transport)
165 goto notransport;
166 counter = zmalloc(sizeof(struct lttng_counter));
167 if (!counter)
168 goto nomem;
169
170 counter->ops = &counter_transport->ops;
171 counter->transport = counter_transport;
172
173 counter->counter = counter->ops->counter_create(
174 number_dimensions, dimensions, 0,
175 -1, 0, NULL, false);
176 if (!counter->counter) {
177 goto create_error;
178 }
179
180 return counter;
181
182 create_error:
183 free(counter);
184 nomem:
185 notransport:
186 return NULL;
187 }
188
189 static
190 void lttng_ust_counter_destroy(struct lttng_counter *counter)
191 {
192 counter->ops->counter_destroy(counter->counter);
193 free(counter);
194 }
195
196 struct lttng_event_notifier_group *lttng_event_notifier_group_create(void)
197 {
198 struct lttng_event_notifier_group *event_notifier_group;
199 int i;
200
201 event_notifier_group = zmalloc(sizeof(struct lttng_event_notifier_group));
202 if (!event_notifier_group)
203 return NULL;
204
205 /* Add all contexts. */
206 if (lttng_context_init_all(&event_notifier_group->ctx)) {
207 free(event_notifier_group);
208 return NULL;
209 }
210
211 CDS_INIT_LIST_HEAD(&event_notifier_group->enablers_head);
212 CDS_INIT_LIST_HEAD(&event_notifier_group->event_notifiers_head);
213 for (i = 0; i < LTTNG_UST_EVENT_NOTIFIER_HT_SIZE; i++)
214 CDS_INIT_HLIST_HEAD(&event_notifier_group->event_notifiers_ht.table[i]);
215
216 cds_list_add(&event_notifier_group->node, &event_notifier_groups);
217
218 return event_notifier_group;
219 }
220
221 /*
222 * Only used internally at session destruction.
223 */
224 static
225 void _lttng_channel_unmap(struct lttng_channel *lttng_chan)
226 {
227 struct channel *chan;
228 struct lttng_ust_shm_handle *handle;
229
230 cds_list_del(&lttng_chan->node);
231 lttng_destroy_context(lttng_chan->ctx);
232 chan = lttng_chan->chan;
233 handle = lttng_chan->handle;
234 /*
235 * note: lttng_chan is private data contained within handle. It
236 * will be freed along with the handle.
237 */
238 channel_destroy(chan, handle, 0);
239 }
240
241 static
242 void register_event(struct lttng_event *event)
243 {
244 int ret;
245 const struct lttng_event_desc *desc;
246
247 assert(event->registered == 0);
248 desc = event->desc;
249 ret = __tracepoint_probe_register_queue_release(desc->name,
250 desc->probe_callback,
251 event, desc->signature);
252 WARN_ON_ONCE(ret);
253 if (!ret)
254 event->registered = 1;
255 }
256
257 static
258 void register_event_notifier(struct lttng_event_notifier *event_notifier)
259 {
260 int ret;
261 const struct lttng_event_desc *desc;
262
263 assert(event_notifier->registered == 0);
264 desc = event_notifier->desc;
265 ret = __tracepoint_probe_register_queue_release(desc->name,
266 desc->u.ext.event_notifier_callback, event_notifier, desc->signature);
267 WARN_ON_ONCE(ret);
268 if (!ret)
269 event_notifier->registered = 1;
270 }
271
272 static
273 void unregister_event(struct lttng_event *event)
274 {
275 int ret;
276 const struct lttng_event_desc *desc;
277
278 assert(event->registered == 1);
279 desc = event->desc;
280 ret = __tracepoint_probe_unregister_queue_release(desc->name,
281 desc->probe_callback,
282 event);
283 WARN_ON_ONCE(ret);
284 if (!ret)
285 event->registered = 0;
286 }
287
288 static
289 void unregister_event_notifier(struct lttng_event_notifier *event_notifier)
290 {
291 int ret;
292 const struct lttng_event_desc *desc;
293
294 assert(event_notifier->registered == 1);
295 desc = event_notifier->desc;
296 ret = __tracepoint_probe_unregister_queue_release(desc->name,
297 desc->u.ext.event_notifier_callback, event_notifier);
298 WARN_ON_ONCE(ret);
299 if (!ret)
300 event_notifier->registered = 0;
301 }
302
303 /*
304 * Only used internally at session destruction.
305 */
306 static
307 void _lttng_event_unregister(struct lttng_event *event)
308 {
309 if (event->registered)
310 unregister_event(event);
311 }
312
313 /*
314 * Only used internally at session destruction.
315 */
316 static
317 void _lttng_event_notifier_unregister(struct lttng_event_notifier *event_notifier)
318 {
319 if (event_notifier->registered)
320 unregister_event_notifier(event_notifier);
321 }
322
323 void lttng_session_destroy(struct lttng_session *session)
324 {
325 struct lttng_channel *chan, *tmpchan;
326 struct lttng_event *event, *tmpevent;
327 struct lttng_enum *_enum, *tmp_enum;
328 struct lttng_event_enabler *event_enabler, *event_tmpenabler;
329
330 CMM_ACCESS_ONCE(session->active) = 0;
331 cds_list_for_each_entry(event, &session->events_head, node) {
332 _lttng_event_unregister(event);
333 }
334 lttng_ust_synchronize_trace(); /* Wait for in-flight events to complete */
335 __tracepoint_probe_prune_release_queue();
336 cds_list_for_each_entry_safe(event_enabler, event_tmpenabler,
337 &session->enablers_head, node)
338 lttng_event_enabler_destroy(event_enabler);
339 cds_list_for_each_entry_safe(event, tmpevent,
340 &session->events_head, node)
341 _lttng_event_destroy(event);
342 cds_list_for_each_entry_safe(_enum, tmp_enum,
343 &session->enums_head, node)
344 _lttng_enum_destroy(_enum);
345 cds_list_for_each_entry_safe(chan, tmpchan, &session->chan_head, node)
346 _lttng_channel_unmap(chan);
347 cds_list_del(&session->node);
348 lttng_destroy_context(session->ctx);
349 free(session);
350 }
351
352 void lttng_event_notifier_group_destroy(
353 struct lttng_event_notifier_group *event_notifier_group)
354 {
355 int close_ret;
356 struct lttng_event_notifier_enabler *notifier_enabler, *tmpnotifier_enabler;
357 struct lttng_event_notifier *notifier, *tmpnotifier;
358
359 if (!event_notifier_group) {
360 return;
361 }
362
363 cds_list_for_each_entry(notifier,
364 &event_notifier_group->event_notifiers_head, node)
365 _lttng_event_notifier_unregister(notifier);
366
367 lttng_ust_synchronize_trace();
368
369 cds_list_for_each_entry_safe(notifier_enabler, tmpnotifier_enabler,
370 &event_notifier_group->enablers_head, node)
371 lttng_event_notifier_enabler_destroy(notifier_enabler);
372
373 cds_list_for_each_entry_safe(notifier, tmpnotifier,
374 &event_notifier_group->event_notifiers_head, node)
375 _lttng_event_notifier_destroy(notifier);
376
377 if (event_notifier_group->error_counter)
378 lttng_ust_counter_destroy(event_notifier_group->error_counter);
379
380 /* Close the notification fd to the listener of event_notifiers. */
381
382 lttng_ust_lock_fd_tracker();
383 close_ret = close(event_notifier_group->notification_fd);
384 if (!close_ret) {
385 lttng_ust_delete_fd_from_tracker(
386 event_notifier_group->notification_fd);
387 } else {
388 PERROR("close");
389 abort();
390 }
391 lttng_ust_unlock_fd_tracker();
392
393 cds_list_del(&event_notifier_group->node);
394
395 free(event_notifier_group);
396 }
397
398 static
399 void lttng_enabler_destroy(struct lttng_enabler *enabler)
400 {
401 struct lttng_ust_bytecode_node *filter_node, *tmp_filter_node;
402 struct lttng_ust_excluder_node *excluder_node, *tmp_excluder_node;
403
404 if (!enabler) {
405 return;
406 }
407
408 /* Destroy filter bytecode */
409 cds_list_for_each_entry_safe(filter_node, tmp_filter_node,
410 &enabler->filter_bytecode_head, node) {
411 free(filter_node);
412 }
413
414 /* Destroy excluders */
415 cds_list_for_each_entry_safe(excluder_node, tmp_excluder_node,
416 &enabler->excluder_head, node) {
417 free(excluder_node);
418 }
419 }
420
421 void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler *event_notifier_enabler)
422 {
423 if (!event_notifier_enabler) {
424 return;
425 }
426
427 cds_list_del(&event_notifier_enabler->node);
428
429 lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
430
431 free(event_notifier_enabler);
432 }
433
434 static
435 int lttng_enum_create(const struct lttng_enum_desc *desc,
436 struct lttng_session *session)
437 {
438 const char *enum_name = desc->name;
439 struct lttng_enum *_enum;
440 struct cds_hlist_head *head;
441 int ret = 0;
442 size_t name_len = strlen(enum_name);
443 uint32_t hash;
444 int notify_socket;
445
446 /* Check if this enum is already registered for this session. */
447 hash = jhash(enum_name, name_len, 0);
448 head = &session->enums_ht.table[hash & (LTTNG_UST_ENUM_HT_SIZE - 1)];
449
450 _enum = lttng_ust_enum_get_from_desc(session, desc);
451 if (_enum) {
452 ret = -EEXIST;
453 goto exist;
454 }
455
456 notify_socket = lttng_get_notify_socket(session->owner);
457 if (notify_socket < 0) {
458 ret = notify_socket;
459 goto socket_error;
460 }
461
462 _enum = zmalloc(sizeof(*_enum));
463 if (!_enum) {
464 ret = -ENOMEM;
465 goto cache_error;
466 }
467 _enum->session = session;
468 _enum->desc = desc;
469
470 ret = ustcomm_register_enum(notify_socket,
471 session->objd,
472 enum_name,
473 desc->nr_entries,
474 desc->entries,
475 &_enum->id);
476 if (ret < 0) {
477 DBG("Error (%d) registering enumeration to sessiond", ret);
478 goto sessiond_register_error;
479 }
480 cds_list_add(&_enum->node, &session->enums_head);
481 cds_hlist_add_head(&_enum->hlist, head);
482 return 0;
483
484 sessiond_register_error:
485 free(_enum);
486 cache_error:
487 socket_error:
488 exist:
489 return ret;
490 }
491
492 static
493 int lttng_create_enum_check(const struct lttng_type *type,
494 struct lttng_session *session)
495 {
496 switch (type->atype) {
497 case atype_enum:
498 {
499 const struct lttng_enum_desc *enum_desc;
500 int ret;
501
502 enum_desc = type->u.legacy.basic.enumeration.desc;
503 ret = lttng_enum_create(enum_desc, session);
504 if (ret && ret != -EEXIST) {
505 DBG("Unable to create enum error: (%d)", ret);
506 return ret;
507 }
508 break;
509 }
510 case atype_enum_nestable:
511 {
512 const struct lttng_enum_desc *enum_desc;
513 int ret;
514
515 enum_desc = type->u.enum_nestable.desc;
516 ret = lttng_enum_create(enum_desc, session);
517 if (ret && ret != -EEXIST) {
518 DBG("Unable to create enum error: (%d)", ret);
519 return ret;
520 }
521 break;
522 }
523 case atype_dynamic:
524 {
525 const struct lttng_event_field *tag_field_generic;
526 const struct lttng_enum_desc *enum_desc;
527 int ret;
528
529 tag_field_generic = lttng_ust_dynamic_type_tag_field();
530 enum_desc = tag_field_generic->type.u.enum_nestable.desc;
531 ret = lttng_enum_create(enum_desc, session);
532 if (ret && ret != -EEXIST) {
533 DBG("Unable to create enum error: (%d)", ret);
534 return ret;
535 }
536 break;
537 }
538 default:
539 /* TODO: nested types when they become supported. */
540 break;
541 }
542 return 0;
543 }
544
545 static
546 int lttng_create_all_event_enums(size_t nr_fields,
547 const struct lttng_event_field *event_fields,
548 struct lttng_session *session)
549 {
550 size_t i;
551 int ret;
552
553 /* For each field, ensure enum is part of the session. */
554 for (i = 0; i < nr_fields; i++) {
555 const struct lttng_type *type = &event_fields[i].type;
556
557 ret = lttng_create_enum_check(type, session);
558 if (ret)
559 return ret;
560 }
561 return 0;
562 }
563
564 static
565 int lttng_create_all_ctx_enums(size_t nr_fields,
566 const struct lttng_ctx_field *ctx_fields,
567 struct lttng_session *session)
568 {
569 size_t i;
570 int ret;
571
572 /* For each field, ensure enum is part of the session. */
573 for (i = 0; i < nr_fields; i++) {
574 const struct lttng_type *type = &ctx_fields[i].event_field.type;
575
576 ret = lttng_create_enum_check(type, session);
577 if (ret)
578 return ret;
579 }
580 return 0;
581 }
582
583 /*
584 * Ensure that a state-dump will be performed for this session at the end
585 * of the current handle_message().
586 */
587 int lttng_session_statedump(struct lttng_session *session)
588 {
589 session->statedump_pending = 1;
590 lttng_ust_sockinfo_session_enabled(session->owner);
591 return 0;
592 }
593
594 int lttng_session_enable(struct lttng_session *session)
595 {
596 int ret = 0;
597 struct lttng_channel *chan;
598 int notify_socket;
599
600 if (session->active) {
601 ret = -EBUSY;
602 goto end;
603 }
604
605 notify_socket = lttng_get_notify_socket(session->owner);
606 if (notify_socket < 0)
607 return notify_socket;
608
609 /* Set transient enabler state to "enabled" */
610 session->tstate = 1;
611
612 /* We need to sync enablers with session before activation. */
613 lttng_session_sync_event_enablers(session);
614
615 /*
616 * Snapshot the number of events per channel to know the type of header
617 * we need to use.
618 */
619 cds_list_for_each_entry(chan, &session->chan_head, node) {
620 const struct lttng_ctx *ctx;
621 const struct lttng_ctx_field *fields = NULL;
622 size_t nr_fields = 0;
623 uint32_t chan_id;
624
625 /* don't change it if session stop/restart */
626 if (chan->header_type)
627 continue;
628 ctx = chan->ctx;
629 if (ctx) {
630 nr_fields = ctx->nr_fields;
631 fields = ctx->fields;
632 ret = lttng_create_all_ctx_enums(nr_fields, fields,
633 session);
634 if (ret < 0) {
635 DBG("Error (%d) adding enum to session", ret);
636 return ret;
637 }
638 }
639 ret = ustcomm_register_channel(notify_socket,
640 session,
641 session->objd,
642 chan->objd,
643 nr_fields,
644 fields,
645 &chan_id,
646 &chan->header_type);
647 if (ret) {
648 DBG("Error (%d) registering channel to sessiond", ret);
649 return ret;
650 }
651 if (chan_id != chan->id) {
652 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
653 chan_id, chan->id);
654 return -EINVAL;
655 }
656 }
657
658 /* Set atomically the state to "active" */
659 CMM_ACCESS_ONCE(session->active) = 1;
660 CMM_ACCESS_ONCE(session->been_active) = 1;
661
662 ret = lttng_session_statedump(session);
663 if (ret)
664 return ret;
665 end:
666 return ret;
667 }
668
669 int lttng_session_disable(struct lttng_session *session)
670 {
671 int ret = 0;
672
673 if (!session->active) {
674 ret = -EBUSY;
675 goto end;
676 }
677 /* Set atomically the state to "inactive" */
678 CMM_ACCESS_ONCE(session->active) = 0;
679
680 /* Set transient enabler state to "disabled" */
681 session->tstate = 0;
682 lttng_session_sync_event_enablers(session);
683 end:
684 return ret;
685 }
686
687 int lttng_channel_enable(struct lttng_channel *channel)
688 {
689 int ret = 0;
690
691 if (channel->enabled) {
692 ret = -EBUSY;
693 goto end;
694 }
695 /* Set transient enabler state to "enabled" */
696 channel->tstate = 1;
697 lttng_session_sync_event_enablers(channel->session);
698 /* Set atomically the state to "enabled" */
699 CMM_ACCESS_ONCE(channel->enabled) = 1;
700 end:
701 return ret;
702 }
703
704 int lttng_channel_disable(struct lttng_channel *channel)
705 {
706 int ret = 0;
707
708 if (!channel->enabled) {
709 ret = -EBUSY;
710 goto end;
711 }
712 /* Set atomically the state to "disabled" */
713 CMM_ACCESS_ONCE(channel->enabled) = 0;
714 /* Set transient enabler state to "enabled" */
715 channel->tstate = 0;
716 lttng_session_sync_event_enablers(channel->session);
717 end:
718 return ret;
719 }
720
721 static inline
722 struct cds_hlist_head *borrow_hash_table_bucket(
723 struct cds_hlist_head *hash_table,
724 unsigned int hash_table_size,
725 const struct lttng_event_desc *desc)
726 {
727 const char *event_name;
728 size_t name_len;
729 uint32_t hash;
730
731 event_name = desc->name;
732 name_len = strlen(event_name);
733
734 hash = jhash(event_name, name_len, 0);
735 return &hash_table[hash & (hash_table_size - 1)];
736 }
737
738 /*
739 * Supports event creation while tracing session is active.
740 */
741 static
742 int lttng_event_create(const struct lttng_event_desc *desc,
743 struct lttng_channel *chan)
744 {
745 struct lttng_event *event;
746 struct lttng_session *session = chan->session;
747 struct cds_hlist_head *head;
748 int ret = 0;
749 int notify_socket, loglevel;
750 const char *uri;
751
752 head = borrow_hash_table_bucket(chan->session->events_ht.table,
753 LTTNG_UST_EVENT_HT_SIZE, desc);
754
755 notify_socket = lttng_get_notify_socket(session->owner);
756 if (notify_socket < 0) {
757 ret = notify_socket;
758 goto socket_error;
759 }
760
761 ret = lttng_create_all_event_enums(desc->nr_fields, desc->fields,
762 session);
763 if (ret < 0) {
764 DBG("Error (%d) adding enum to session", ret);
765 goto create_enum_error;
766 }
767
768 /*
769 * Check if loglevel match. Refuse to connect event if not.
770 */
771 event = zmalloc(sizeof(struct lttng_event));
772 if (!event) {
773 ret = -ENOMEM;
774 goto cache_error;
775 }
776 event->chan = chan;
777
778 /* Event will be enabled by enabler sync. */
779 event->enabled = 0;
780 event->registered = 0;
781 CDS_INIT_LIST_HEAD(&event->filter_bytecode_runtime_head);
782 CDS_INIT_LIST_HEAD(&event->enablers_ref_head);
783 event->desc = desc;
784
785 if (desc->loglevel)
786 loglevel = *(*event->desc->loglevel);
787 else
788 loglevel = TRACE_DEFAULT;
789 if (desc->u.ext.model_emf_uri)
790 uri = *(desc->u.ext.model_emf_uri);
791 else
792 uri = NULL;
793
794 /* Fetch event ID from sessiond */
795 ret = ustcomm_register_event(notify_socket,
796 session,
797 session->objd,
798 chan->objd,
799 desc->name,
800 loglevel,
801 desc->signature,
802 desc->nr_fields,
803 desc->fields,
804 uri,
805 &event->id);
806 if (ret < 0) {
807 DBG("Error (%d) registering event to sessiond", ret);
808 goto sessiond_register_error;
809 }
810
811 cds_list_add(&event->node, &chan->session->events_head);
812 cds_hlist_add_head(&event->hlist, head);
813 return 0;
814
815 sessiond_register_error:
816 free(event);
817 cache_error:
818 create_enum_error:
819 socket_error:
820 return ret;
821 }
822
823 static
824 int lttng_event_notifier_create(const struct lttng_event_desc *desc,
825 uint64_t token, uint64_t error_counter_index,
826 struct lttng_event_notifier_group *event_notifier_group)
827 {
828 struct lttng_event_notifier *event_notifier;
829 struct cds_hlist_head *head;
830 int ret = 0;
831
832 /*
833 * Get the hashtable bucket the created lttng_event_notifier object
834 * should be inserted.
835 */
836 head = borrow_hash_table_bucket(
837 event_notifier_group->event_notifiers_ht.table,
838 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, desc);
839
840 event_notifier = zmalloc(sizeof(struct lttng_event_notifier));
841 if (!event_notifier) {
842 ret = -ENOMEM;
843 goto error;
844 }
845
846 event_notifier->group = event_notifier_group;
847 event_notifier->user_token = token;
848 event_notifier->error_counter_index = error_counter_index;
849
850 /* Event notifier will be enabled by enabler sync. */
851 event_notifier->enabled = 0;
852 event_notifier->registered = 0;
853
854 CDS_INIT_LIST_HEAD(&event_notifier->filter_bytecode_runtime_head);
855 CDS_INIT_LIST_HEAD(&event_notifier->capture_bytecode_runtime_head);
856 CDS_INIT_LIST_HEAD(&event_notifier->enablers_ref_head);
857 event_notifier->desc = desc;
858 event_notifier->notification_send = lttng_event_notifier_notification_send;
859
860 cds_list_add(&event_notifier->node,
861 &event_notifier_group->event_notifiers_head);
862 cds_hlist_add_head(&event_notifier->hlist, head);
863
864 return 0;
865
866 error:
867 return ret;
868 }
869
870 static
871 void _lttng_event_notifier_destroy(struct lttng_event_notifier *event_notifier)
872 {
873 struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref;
874
875 /* Remove from event_notifier list. */
876 cds_list_del(&event_notifier->node);
877 /* Remove from event_notifier hash table. */
878 cds_hlist_del(&event_notifier->hlist);
879
880 lttng_free_event_notifier_filter_runtime(event_notifier);
881
882 /* Free event_notifier enabler refs */
883 cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref,
884 &event_notifier->enablers_ref_head, node)
885 free(enabler_ref);
886 free(event_notifier);
887 }
888
889 static
890 int lttng_desc_match_star_glob_enabler(const struct lttng_event_desc *desc,
891 struct lttng_enabler *enabler)
892 {
893 int loglevel = 0;
894 unsigned int has_loglevel = 0;
895
896 assert(enabler->format_type == LTTNG_ENABLER_FORMAT_STAR_GLOB);
897 if (!strutils_star_glob_match(enabler->event_param.name, SIZE_MAX,
898 desc->name, SIZE_MAX))
899 return 0;
900 if (desc->loglevel) {
901 loglevel = *(*desc->loglevel);
902 has_loglevel = 1;
903 }
904 if (!lttng_loglevel_match(loglevel,
905 has_loglevel,
906 enabler->event_param.loglevel_type,
907 enabler->event_param.loglevel))
908 return 0;
909 return 1;
910 }
911
912 static
913 int lttng_desc_match_event_enabler(const struct lttng_event_desc *desc,
914 struct lttng_enabler *enabler)
915 {
916 int loglevel = 0;
917 unsigned int has_loglevel = 0;
918
919 assert(enabler->format_type == LTTNG_ENABLER_FORMAT_EVENT);
920 if (strcmp(desc->name, enabler->event_param.name))
921 return 0;
922 if (desc->loglevel) {
923 loglevel = *(*desc->loglevel);
924 has_loglevel = 1;
925 }
926 if (!lttng_loglevel_match(loglevel,
927 has_loglevel,
928 enabler->event_param.loglevel_type,
929 enabler->event_param.loglevel))
930 return 0;
931 return 1;
932 }
933
934 static
935 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
936 struct lttng_enabler *enabler)
937 {
938 switch (enabler->format_type) {
939 case LTTNG_ENABLER_FORMAT_STAR_GLOB:
940 {
941 struct lttng_ust_excluder_node *excluder;
942
943 if (!lttng_desc_match_star_glob_enabler(desc, enabler)) {
944 return 0;
945 }
946
947 /*
948 * If the matching event matches with an excluder,
949 * return 'does not match'
950 */
951 cds_list_for_each_entry(excluder, &enabler->excluder_head, node) {
952 int count;
953
954 for (count = 0; count < excluder->excluder.count; count++) {
955 int len;
956 char *excluder_name;
957
958 excluder_name = (char *) (excluder->excluder.names)
959 + count * LTTNG_UST_SYM_NAME_LEN;
960 len = strnlen(excluder_name, LTTNG_UST_SYM_NAME_LEN);
961 if (len > 0 && strutils_star_glob_match(excluder_name, len, desc->name, SIZE_MAX))
962 return 0;
963 }
964 }
965 return 1;
966 }
967 case LTTNG_ENABLER_FORMAT_EVENT:
968 return lttng_desc_match_event_enabler(desc, enabler);
969 default:
970 return -EINVAL;
971 }
972 }
973
974 static
975 int lttng_event_enabler_match_event(struct lttng_event_enabler *event_enabler,
976 struct lttng_event *event)
977 {
978 if (lttng_desc_match_enabler(event->desc,
979 lttng_event_enabler_as_enabler(event_enabler))
980 && event->chan == event_enabler->chan)
981 return 1;
982 else
983 return 0;
984 }
985
986 static
987 int lttng_event_notifier_enabler_match_event_notifier(
988 struct lttng_event_notifier_enabler *event_notifier_enabler,
989 struct lttng_event_notifier *event_notifier)
990 {
991 int desc_matches = lttng_desc_match_enabler(event_notifier->desc,
992 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
993
994 if (desc_matches && event_notifier->group == event_notifier_enabler->group &&
995 event_notifier->user_token == event_notifier_enabler->user_token)
996 return 1;
997 else
998 return 0;
999 }
1000
1001 static
1002 struct lttng_enabler_ref *lttng_enabler_ref(
1003 struct cds_list_head *enabler_ref_list,
1004 struct lttng_enabler *enabler)
1005 {
1006 struct lttng_enabler_ref *enabler_ref;
1007
1008 cds_list_for_each_entry(enabler_ref, enabler_ref_list, node) {
1009 if (enabler_ref->ref == enabler)
1010 return enabler_ref;
1011 }
1012 return NULL;
1013 }
1014
1015 /*
1016 * Create struct lttng_event if it is missing and present in the list of
1017 * tracepoint probes.
1018 */
1019 static
1020 void lttng_create_event_if_missing(struct lttng_event_enabler *event_enabler)
1021 {
1022 struct lttng_session *session = event_enabler->chan->session;
1023 struct lttng_probe_desc *probe_desc;
1024 const struct lttng_event_desc *desc;
1025 struct lttng_event *event;
1026 int i;
1027 struct cds_list_head *probe_list;
1028
1029 probe_list = lttng_get_probe_list_head();
1030 /*
1031 * For each probe event, if we find that a probe event matches
1032 * our enabler, create an associated lttng_event if not
1033 * already present.
1034 */
1035 cds_list_for_each_entry(probe_desc, probe_list, head) {
1036 for (i = 0; i < probe_desc->nr_events; i++) {
1037 int ret;
1038 bool found = false;
1039 struct cds_hlist_head *head;
1040 struct cds_hlist_node *node;
1041
1042 desc = probe_desc->event_desc[i];
1043 if (!lttng_desc_match_enabler(desc,
1044 lttng_event_enabler_as_enabler(event_enabler)))
1045 continue;
1046
1047 head = borrow_hash_table_bucket(
1048 session->events_ht.table,
1049 LTTNG_UST_EVENT_HT_SIZE, desc);
1050
1051 cds_hlist_for_each_entry(event, node, head, hlist) {
1052 if (event->desc == desc
1053 && event->chan == event_enabler->chan) {
1054 found = true;
1055 break;
1056 }
1057 }
1058 if (found)
1059 continue;
1060
1061 /*
1062 * We need to create an event for this
1063 * event probe.
1064 */
1065 ret = lttng_event_create(probe_desc->event_desc[i],
1066 event_enabler->chan);
1067 if (ret) {
1068 DBG("Unable to create event %s, error %d\n",
1069 probe_desc->event_desc[i]->name, ret);
1070 }
1071 }
1072 }
1073 }
1074
1075 static
1076 void probe_provider_event_for_each(struct lttng_probe_desc *provider_desc,
1077 void (*event_func)(struct lttng_session *session,
1078 struct lttng_event *event),
1079 void (*event_notifier_func)(struct lttng_event_notifier *event_notifier))
1080 {
1081 struct cds_hlist_node *node, *tmp_node;
1082 struct cds_list_head *sessionsp;
1083 unsigned int i;
1084
1085 /* Get handle on list of sessions. */
1086 sessionsp = _lttng_get_sessions();
1087
1088 /*
1089 * Iterate over all events in the probe provider descriptions and
1090 * sessions to queue the unregistration of the events.
1091 */
1092 for (i = 0; i < provider_desc->nr_events; i++) {
1093 const struct lttng_event_desc *event_desc;
1094 struct lttng_event_notifier_group *event_notifier_group;
1095 struct lttng_event_notifier *event_notifier;
1096 struct lttng_session *session;
1097 struct cds_hlist_head *head;
1098 struct lttng_event *event;
1099
1100 event_desc = provider_desc->event_desc[i];
1101
1102 /*
1103 * Iterate over all session to find the current event
1104 * description.
1105 */
1106 cds_list_for_each_entry(session, sessionsp, node) {
1107 /*
1108 * Get the list of events in the hashtable bucket and
1109 * iterate to find the event matching this descriptor.
1110 */
1111 head = borrow_hash_table_bucket(
1112 session->events_ht.table,
1113 LTTNG_UST_EVENT_HT_SIZE, event_desc);
1114
1115 cds_hlist_for_each_entry_safe(event, node, tmp_node, head, hlist) {
1116 if (event_desc == event->desc) {
1117 event_func(session, event);
1118 break;
1119 }
1120 }
1121 }
1122
1123 /*
1124 * Iterate over all event_notifier groups to find the current event
1125 * description.
1126 */
1127 cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) {
1128 /*
1129 * Get the list of event_notifiers in the hashtable bucket and
1130 * iterate to find the event_notifier matching this
1131 * descriptor.
1132 */
1133 head = borrow_hash_table_bucket(
1134 event_notifier_group->event_notifiers_ht.table,
1135 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, event_desc);
1136
1137 cds_hlist_for_each_entry_safe(event_notifier, node, tmp_node, head, hlist) {
1138 if (event_desc == event_notifier->desc) {
1139 event_notifier_func(event_notifier);
1140 break;
1141 }
1142 }
1143 }
1144 }
1145 }
1146
1147 static
1148 void _unregister_event(struct lttng_session *session,
1149 struct lttng_event *event)
1150 {
1151 _lttng_event_unregister(event);
1152 }
1153
1154 static
1155 void _event_enum_destroy(struct lttng_session *session,
1156 struct lttng_event *event)
1157 {
1158 unsigned int i;
1159
1160 /* Destroy enums of the current event. */
1161 for (i = 0; i < event->desc->nr_fields; i++) {
1162 const struct lttng_enum_desc *enum_desc;
1163 const struct lttng_event_field *field;
1164 struct lttng_enum *curr_enum;
1165
1166 field = &(event->desc->fields[i]);
1167 switch (field->type.atype) {
1168 case atype_enum:
1169 enum_desc = field->type.u.legacy.basic.enumeration.desc;
1170 break;
1171 case atype_enum_nestable:
1172 enum_desc = field->type.u.enum_nestable.desc;
1173 break;
1174 default:
1175 continue;
1176 }
1177
1178 curr_enum = lttng_ust_enum_get_from_desc(session, enum_desc);
1179 if (curr_enum) {
1180 _lttng_enum_destroy(curr_enum);
1181 }
1182 }
1183
1184 /* Destroy event. */
1185 _lttng_event_destroy(event);
1186 }
1187
1188 /*
1189 * Iterate over all the UST sessions to unregister and destroy all probes from
1190 * the probe provider descriptor received as argument. Must me called with the
1191 * ust_lock held.
1192 */
1193 void lttng_probe_provider_unregister_events(
1194 struct lttng_probe_desc *provider_desc)
1195 {
1196 /*
1197 * Iterate over all events in the probe provider descriptions and sessions
1198 * to queue the unregistration of the events.
1199 */
1200 probe_provider_event_for_each(provider_desc, _unregister_event,
1201 _lttng_event_notifier_unregister);
1202
1203 /* Wait for grace period. */
1204 lttng_ust_synchronize_trace();
1205 /* Prune the unregistration queue. */
1206 __tracepoint_probe_prune_release_queue();
1207
1208 /*
1209 * It is now safe to destroy the events and remove them from the event list
1210 * and hashtables.
1211 */
1212 probe_provider_event_for_each(provider_desc, _event_enum_destroy,
1213 _lttng_event_notifier_destroy);
1214 }
1215
1216 /*
1217 * Create events associated with an event enabler (if not already present),
1218 * and add backward reference from the event to the enabler.
1219 */
1220 static
1221 int lttng_event_enabler_ref_events(struct lttng_event_enabler *event_enabler)
1222 {
1223 struct lttng_session *session = event_enabler->chan->session;
1224 struct lttng_event *event;
1225
1226 if (!lttng_event_enabler_as_enabler(event_enabler)->enabled)
1227 goto end;
1228
1229 /* First ensure that probe events are created for this enabler. */
1230 lttng_create_event_if_missing(event_enabler);
1231
1232 /* For each event matching enabler in session event list. */
1233 cds_list_for_each_entry(event, &session->events_head, node) {
1234 struct lttng_enabler_ref *enabler_ref;
1235
1236 if (!lttng_event_enabler_match_event(event_enabler, event))
1237 continue;
1238
1239 enabler_ref = lttng_enabler_ref(&event->enablers_ref_head,
1240 lttng_event_enabler_as_enabler(event_enabler));
1241 if (!enabler_ref) {
1242 /*
1243 * If no backward ref, create it.
1244 * Add backward ref from event to enabler.
1245 */
1246 enabler_ref = zmalloc(sizeof(*enabler_ref));
1247 if (!enabler_ref)
1248 return -ENOMEM;
1249 enabler_ref->ref = lttng_event_enabler_as_enabler(
1250 event_enabler);
1251 cds_list_add(&enabler_ref->node,
1252 &event->enablers_ref_head);
1253 }
1254
1255 /*
1256 * Link filter bytecodes if not linked yet.
1257 */
1258 lttng_enabler_link_bytecode(event->desc,
1259 &session->ctx,
1260 &event->filter_bytecode_runtime_head,
1261 &lttng_event_enabler_as_enabler(event_enabler)->filter_bytecode_head);
1262
1263 /* TODO: merge event context. */
1264 }
1265 end:
1266 return 0;
1267 }
1268
1269 /*
1270 * Called at library load: connect the probe on all enablers matching
1271 * this event.
1272 * Called with session mutex held.
1273 */
1274 int lttng_fix_pending_events(void)
1275 {
1276 struct lttng_session *session;
1277
1278 cds_list_for_each_entry(session, &sessions, node) {
1279 lttng_session_lazy_sync_event_enablers(session);
1280 }
1281 return 0;
1282 }
1283
1284 int lttng_fix_pending_event_notifiers(void)
1285 {
1286 struct lttng_event_notifier_group *event_notifier_group;
1287
1288 cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) {
1289 lttng_event_notifier_group_sync_enablers(event_notifier_group);
1290 }
1291 return 0;
1292 }
1293
1294 /*
1295 * For each session of the owner thread, execute pending statedump.
1296 * Only dump state for the sessions owned by the caller thread, because
1297 * we don't keep ust_lock across the entire iteration.
1298 */
1299 void lttng_handle_pending_statedump(void *owner)
1300 {
1301 struct lttng_session *session;
1302
1303 /* Execute state dump */
1304 do_lttng_ust_statedump(owner);
1305
1306 /* Clear pending state dump */
1307 if (ust_lock()) {
1308 goto end;
1309 }
1310 cds_list_for_each_entry(session, &sessions, node) {
1311 if (session->owner != owner)
1312 continue;
1313 if (!session->statedump_pending)
1314 continue;
1315 session->statedump_pending = 0;
1316 }
1317 end:
1318 ust_unlock();
1319 return;
1320 }
1321
1322 /*
1323 * Only used internally at session destruction.
1324 */
1325 static
1326 void _lttng_event_destroy(struct lttng_event *event)
1327 {
1328 struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref;
1329
1330 /* Remove from event list. */
1331 cds_list_del(&event->node);
1332 /* Remove from event hash table. */
1333 cds_hlist_del(&event->hlist);
1334
1335 lttng_destroy_context(event->ctx);
1336 lttng_free_event_filter_runtime(event);
1337 /* Free event enabler refs */
1338 cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref,
1339 &event->enablers_ref_head, node)
1340 free(enabler_ref);
1341 free(event);
1342 }
1343
1344 static
1345 void _lttng_enum_destroy(struct lttng_enum *_enum)
1346 {
1347 cds_list_del(&_enum->node);
1348 cds_hlist_del(&_enum->hlist);
1349 free(_enum);
1350 }
1351
1352 void lttng_ust_events_exit(void)
1353 {
1354 struct lttng_session *session, *tmpsession;
1355
1356 cds_list_for_each_entry_safe(session, tmpsession, &sessions, node)
1357 lttng_session_destroy(session);
1358 }
1359
1360 /*
1361 * Enabler management.
1362 */
1363 struct lttng_event_enabler *lttng_event_enabler_create(
1364 enum lttng_enabler_format_type format_type,
1365 struct lttng_ust_event *event_param,
1366 struct lttng_channel *chan)
1367 {
1368 struct lttng_event_enabler *event_enabler;
1369
1370 event_enabler = zmalloc(sizeof(*event_enabler));
1371 if (!event_enabler)
1372 return NULL;
1373 event_enabler->base.format_type = format_type;
1374 CDS_INIT_LIST_HEAD(&event_enabler->base.filter_bytecode_head);
1375 CDS_INIT_LIST_HEAD(&event_enabler->base.excluder_head);
1376 memcpy(&event_enabler->base.event_param, event_param,
1377 sizeof(event_enabler->base.event_param));
1378 event_enabler->chan = chan;
1379 /* ctx left NULL */
1380 event_enabler->base.enabled = 0;
1381 cds_list_add(&event_enabler->node, &event_enabler->chan->session->enablers_head);
1382 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1383
1384 return event_enabler;
1385 }
1386
1387 struct lttng_event_notifier_enabler *lttng_event_notifier_enabler_create(
1388 struct lttng_event_notifier_group *event_notifier_group,
1389 enum lttng_enabler_format_type format_type,
1390 struct lttng_ust_event_notifier *event_notifier_param)
1391 {
1392 struct lttng_event_notifier_enabler *event_notifier_enabler;
1393
1394 event_notifier_enabler = zmalloc(sizeof(*event_notifier_enabler));
1395 if (!event_notifier_enabler)
1396 return NULL;
1397 event_notifier_enabler->base.format_type = format_type;
1398 CDS_INIT_LIST_HEAD(&event_notifier_enabler->base.filter_bytecode_head);
1399 CDS_INIT_LIST_HEAD(&event_notifier_enabler->capture_bytecode_head);
1400 CDS_INIT_LIST_HEAD(&event_notifier_enabler->base.excluder_head);
1401
1402 event_notifier_enabler->user_token = event_notifier_param->event.token;
1403 event_notifier_enabler->error_counter_index = event_notifier_param->error_counter_index;
1404 event_notifier_enabler->num_captures = 0;
1405
1406 memcpy(&event_notifier_enabler->base.event_param.name,
1407 event_notifier_param->event.name,
1408 sizeof(event_notifier_enabler->base.event_param.name));
1409 event_notifier_enabler->base.event_param.instrumentation =
1410 event_notifier_param->event.instrumentation;
1411 event_notifier_enabler->base.event_param.loglevel =
1412 event_notifier_param->event.loglevel;
1413 event_notifier_enabler->base.event_param.loglevel_type =
1414 event_notifier_param->event.loglevel_type;
1415
1416 event_notifier_enabler->base.enabled = 0;
1417 event_notifier_enabler->group = event_notifier_group;
1418
1419 cds_list_add(&event_notifier_enabler->node,
1420 &event_notifier_group->enablers_head);
1421
1422 lttng_event_notifier_group_sync_enablers(event_notifier_group);
1423
1424 return event_notifier_enabler;
1425 }
1426
1427 int lttng_event_enabler_enable(struct lttng_event_enabler *event_enabler)
1428 {
1429 lttng_event_enabler_as_enabler(event_enabler)->enabled = 1;
1430 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1431
1432 return 0;
1433 }
1434
1435 int lttng_event_enabler_disable(struct lttng_event_enabler *event_enabler)
1436 {
1437 lttng_event_enabler_as_enabler(event_enabler)->enabled = 0;
1438 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1439
1440 return 0;
1441 }
1442
1443 static
1444 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler *enabler,
1445 struct lttng_ust_bytecode_node **bytecode)
1446 {
1447 (*bytecode)->enabler = enabler;
1448 cds_list_add_tail(&(*bytecode)->node, &enabler->filter_bytecode_head);
1449 /* Take ownership of bytecode */
1450 *bytecode = NULL;
1451 }
1452
1453 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler *event_enabler,
1454 struct lttng_ust_bytecode_node **bytecode)
1455 {
1456 _lttng_enabler_attach_filter_bytecode(
1457 lttng_event_enabler_as_enabler(event_enabler), bytecode);
1458
1459 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1460 return 0;
1461 }
1462
1463 static
1464 void _lttng_enabler_attach_exclusion(struct lttng_enabler *enabler,
1465 struct lttng_ust_excluder_node **excluder)
1466 {
1467 (*excluder)->enabler = enabler;
1468 cds_list_add_tail(&(*excluder)->node, &enabler->excluder_head);
1469 /* Take ownership of excluder */
1470 *excluder = NULL;
1471 }
1472
1473 int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler *event_enabler,
1474 struct lttng_ust_excluder_node **excluder)
1475 {
1476 _lttng_enabler_attach_exclusion(
1477 lttng_event_enabler_as_enabler(event_enabler), excluder);
1478
1479 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1480 return 0;
1481 }
1482
1483 int lttng_event_notifier_enabler_enable(
1484 struct lttng_event_notifier_enabler *event_notifier_enabler)
1485 {
1486 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 1;
1487 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1488
1489 return 0;
1490 }
1491
1492 int lttng_event_notifier_enabler_disable(
1493 struct lttng_event_notifier_enabler *event_notifier_enabler)
1494 {
1495 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 0;
1496 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1497
1498 return 0;
1499 }
1500
1501 int lttng_event_notifier_enabler_attach_filter_bytecode(
1502 struct lttng_event_notifier_enabler *event_notifier_enabler,
1503 struct lttng_ust_bytecode_node **bytecode)
1504 {
1505 _lttng_enabler_attach_filter_bytecode(
1506 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler),
1507 bytecode);
1508
1509 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1510 return 0;
1511 }
1512
1513 int lttng_event_notifier_enabler_attach_capture_bytecode(
1514 struct lttng_event_notifier_enabler *event_notifier_enabler,
1515 struct lttng_ust_bytecode_node **bytecode)
1516 {
1517 (*bytecode)->enabler = lttng_event_notifier_enabler_as_enabler(
1518 event_notifier_enabler);
1519 cds_list_add_tail(&(*bytecode)->node,
1520 &event_notifier_enabler->capture_bytecode_head);
1521 /* Take ownership of bytecode */
1522 *bytecode = NULL;
1523 event_notifier_enabler->num_captures++;
1524
1525 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1526 return 0;
1527 }
1528
1529 int lttng_event_notifier_enabler_attach_exclusion(
1530 struct lttng_event_notifier_enabler *event_notifier_enabler,
1531 struct lttng_ust_excluder_node **excluder)
1532 {
1533 _lttng_enabler_attach_exclusion(
1534 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler),
1535 excluder);
1536
1537 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
1538 return 0;
1539 }
1540
1541 int lttng_attach_context(struct lttng_ust_context *context_param,
1542 union ust_args *uargs,
1543 struct lttng_ctx **ctx, struct lttng_session *session)
1544 {
1545 /*
1546 * We cannot attach a context after trace has been started for a
1547 * session because the metadata does not allow expressing this
1548 * information outside of the original channel scope.
1549 */
1550 if (session->been_active)
1551 return -EPERM;
1552
1553 switch (context_param->ctx) {
1554 case LTTNG_UST_CONTEXT_PTHREAD_ID:
1555 return lttng_add_pthread_id_to_ctx(ctx);
1556 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
1557 {
1558 struct lttng_ust_perf_counter_ctx *perf_ctx_param;
1559
1560 perf_ctx_param = &context_param->u.perf_counter;
1561 return lttng_add_perf_counter_to_ctx(
1562 perf_ctx_param->type,
1563 perf_ctx_param->config,
1564 perf_ctx_param->name,
1565 ctx);
1566 }
1567 case LTTNG_UST_CONTEXT_VTID:
1568 return lttng_add_vtid_to_ctx(ctx);
1569 case LTTNG_UST_CONTEXT_VPID:
1570 return lttng_add_vpid_to_ctx(ctx);
1571 case LTTNG_UST_CONTEXT_PROCNAME:
1572 return lttng_add_procname_to_ctx(ctx);
1573 case LTTNG_UST_CONTEXT_IP:
1574 return lttng_add_ip_to_ctx(ctx);
1575 case LTTNG_UST_CONTEXT_CPU_ID:
1576 return lttng_add_cpu_id_to_ctx(ctx);
1577 case LTTNG_UST_CONTEXT_APP_CONTEXT:
1578 return lttng_ust_add_app_context_to_ctx_rcu(uargs->app_context.ctxname,
1579 ctx);
1580 case LTTNG_UST_CONTEXT_CGROUP_NS:
1581 return lttng_add_cgroup_ns_to_ctx(ctx);
1582 case LTTNG_UST_CONTEXT_IPC_NS:
1583 return lttng_add_ipc_ns_to_ctx(ctx);
1584 case LTTNG_UST_CONTEXT_MNT_NS:
1585 return lttng_add_mnt_ns_to_ctx(ctx);
1586 case LTTNG_UST_CONTEXT_NET_NS:
1587 return lttng_add_net_ns_to_ctx(ctx);
1588 case LTTNG_UST_CONTEXT_PID_NS:
1589 return lttng_add_pid_ns_to_ctx(ctx);
1590 case LTTNG_UST_CONTEXT_TIME_NS:
1591 return lttng_add_time_ns_to_ctx(ctx);
1592 case LTTNG_UST_CONTEXT_USER_NS:
1593 return lttng_add_user_ns_to_ctx(ctx);
1594 case LTTNG_UST_CONTEXT_UTS_NS:
1595 return lttng_add_uts_ns_to_ctx(ctx);
1596 case LTTNG_UST_CONTEXT_VUID:
1597 return lttng_add_vuid_to_ctx(ctx);
1598 case LTTNG_UST_CONTEXT_VEUID:
1599 return lttng_add_veuid_to_ctx(ctx);
1600 case LTTNG_UST_CONTEXT_VSUID:
1601 return lttng_add_vsuid_to_ctx(ctx);
1602 case LTTNG_UST_CONTEXT_VGID:
1603 return lttng_add_vgid_to_ctx(ctx);
1604 case LTTNG_UST_CONTEXT_VEGID:
1605 return lttng_add_vegid_to_ctx(ctx);
1606 case LTTNG_UST_CONTEXT_VSGID:
1607 return lttng_add_vsgid_to_ctx(ctx);
1608 default:
1609 return -EINVAL;
1610 }
1611 }
1612
1613 int lttng_event_enabler_attach_context(struct lttng_event_enabler *enabler,
1614 struct lttng_ust_context *context_param)
1615 {
1616 return -ENOSYS;
1617 }
1618
1619 void lttng_event_enabler_destroy(struct lttng_event_enabler *event_enabler)
1620 {
1621 if (!event_enabler) {
1622 return;
1623 }
1624 cds_list_del(&event_enabler->node);
1625
1626 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler));
1627
1628 lttng_destroy_context(event_enabler->ctx);
1629 free(event_enabler);
1630 }
1631
1632 /*
1633 * lttng_session_sync_event_enablers should be called just before starting a
1634 * session.
1635 */
1636 static
1637 void lttng_session_sync_event_enablers(struct lttng_session *session)
1638 {
1639 struct lttng_event_enabler *event_enabler;
1640 struct lttng_event *event;
1641
1642 cds_list_for_each_entry(event_enabler, &session->enablers_head, node)
1643 lttng_event_enabler_ref_events(event_enabler);
1644 /*
1645 * For each event, if at least one of its enablers is enabled,
1646 * and its channel and session transient states are enabled, we
1647 * enable the event, else we disable it.
1648 */
1649 cds_list_for_each_entry(event, &session->events_head, node) {
1650 struct lttng_enabler_ref *enabler_ref;
1651 struct lttng_bytecode_runtime *runtime;
1652 int enabled = 0, has_enablers_without_bytecode = 0;
1653
1654 /* Enable events */
1655 cds_list_for_each_entry(enabler_ref,
1656 &event->enablers_ref_head, node) {
1657 if (enabler_ref->ref->enabled) {
1658 enabled = 1;
1659 break;
1660 }
1661 }
1662 /*
1663 * Enabled state is based on union of enablers, with
1664 * intesection of session and channel transient enable
1665 * states.
1666 */
1667 enabled = enabled && session->tstate && event->chan->tstate;
1668
1669 CMM_STORE_SHARED(event->enabled, enabled);
1670 /*
1671 * Sync tracepoint registration with event enabled
1672 * state.
1673 */
1674 if (enabled) {
1675 if (!event->registered)
1676 register_event(event);
1677 } else {
1678 if (event->registered)
1679 unregister_event(event);
1680 }
1681
1682 /* Check if has enablers without bytecode enabled */
1683 cds_list_for_each_entry(enabler_ref,
1684 &event->enablers_ref_head, node) {
1685 if (enabler_ref->ref->enabled
1686 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1687 has_enablers_without_bytecode = 1;
1688 break;
1689 }
1690 }
1691 event->has_enablers_without_bytecode =
1692 has_enablers_without_bytecode;
1693
1694 /* Enable filters */
1695 cds_list_for_each_entry(runtime,
1696 &event->filter_bytecode_runtime_head, node) {
1697 lttng_bytecode_filter_sync_state(runtime);
1698 }
1699 }
1700 __tracepoint_probe_prune_release_queue();
1701 }
1702
1703 /* Support for event notifier is introduced by probe provider major version 2. */
1704 static
1705 bool lttng_ust_probe_supports_event_notifier(struct lttng_probe_desc *probe_desc)
1706 {
1707 return probe_desc->major >= 2;
1708 }
1709
1710 static
1711 void lttng_create_event_notifier_if_missing(
1712 struct lttng_event_notifier_enabler *event_notifier_enabler)
1713 {
1714 struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group;
1715 struct lttng_probe_desc *probe_desc;
1716 struct cds_list_head *probe_list;
1717 int i;
1718
1719 probe_list = lttng_get_probe_list_head();
1720
1721 cds_list_for_each_entry(probe_desc, probe_list, head) {
1722 for (i = 0; i < probe_desc->nr_events; i++) {
1723 int ret;
1724 bool found = false;
1725 const struct lttng_event_desc *desc;
1726 struct lttng_event_notifier *event_notifier;
1727 struct cds_hlist_head *head;
1728 struct cds_hlist_node *node;
1729
1730 desc = probe_desc->event_desc[i];
1731
1732 if (!lttng_desc_match_enabler(desc,
1733 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)))
1734 continue;
1735
1736 /*
1737 * Given the current event_notifier group, get the bucket that
1738 * the target event_notifier would be if it was already
1739 * created.
1740 */
1741 head = borrow_hash_table_bucket(
1742 event_notifier_group->event_notifiers_ht.table,
1743 LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, desc);
1744
1745 cds_hlist_for_each_entry(event_notifier, node, head, hlist) {
1746 /*
1747 * Check if event_notifier already exists by checking
1748 * if the event_notifier and enabler share the same
1749 * description and id.
1750 */
1751 if (event_notifier->desc == desc &&
1752 event_notifier->user_token == event_notifier_enabler->user_token) {
1753 found = true;
1754 break;
1755 }
1756 }
1757
1758 if (found)
1759 continue;
1760
1761 /* Check that the probe supports event notifiers, else report the error. */
1762 if (!lttng_ust_probe_supports_event_notifier(probe_desc)) {
1763 ERR("Probe \"%s\" contains event \"%s\" which matches an enabled event notifier, "
1764 "but its version (%u.%u) is too old and does not implement event notifiers. "
1765 "It needs to be recompiled against a newer version of LTTng-UST, otherwise "
1766 "this event will not generate any notification.",
1767 probe_desc->provider,
1768 desc->name,
1769 probe_desc->major,
1770 probe_desc->minor);
1771 continue;
1772 }
1773 /*
1774 * We need to create a event_notifier for this event probe.
1775 */
1776 ret = lttng_event_notifier_create(desc,
1777 event_notifier_enabler->user_token,
1778 event_notifier_enabler->error_counter_index,
1779 event_notifier_group);
1780 if (ret) {
1781 DBG("Unable to create event_notifier %s, error %d\n",
1782 probe_desc->event_desc[i]->name, ret);
1783 }
1784 }
1785 }
1786 }
1787
1788 /*
1789 * Create event_notifiers associated with a event_notifier enabler (if not already present).
1790 */
1791 static
1792 int lttng_event_notifier_enabler_ref_event_notifiers(
1793 struct lttng_event_notifier_enabler *event_notifier_enabler)
1794 {
1795 struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group;
1796 struct lttng_event_notifier *event_notifier;
1797
1798 /*
1799 * Only try to create event_notifiers for enablers that are enabled, the user
1800 * might still be attaching filter or exclusion to the
1801 * event_notifier_enabler.
1802 */
1803 if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled)
1804 goto end;
1805
1806 /* First, ensure that probe event_notifiers are created for this enabler. */
1807 lttng_create_event_notifier_if_missing(event_notifier_enabler);
1808
1809 /* Link the created event_notifier with its associated enabler. */
1810 cds_list_for_each_entry(event_notifier, &event_notifier_group->event_notifiers_head, node) {
1811 struct lttng_enabler_ref *enabler_ref;
1812
1813 if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler, event_notifier))
1814 continue;
1815
1816 enabler_ref = lttng_enabler_ref(&event_notifier->enablers_ref_head,
1817 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
1818 if (!enabler_ref) {
1819 /*
1820 * If no backward ref, create it.
1821 * Add backward ref from event_notifier to enabler.
1822 */
1823 enabler_ref = zmalloc(sizeof(*enabler_ref));
1824 if (!enabler_ref)
1825 return -ENOMEM;
1826
1827 enabler_ref->ref = lttng_event_notifier_enabler_as_enabler(
1828 event_notifier_enabler);
1829 cds_list_add(&enabler_ref->node,
1830 &event_notifier->enablers_ref_head);
1831 }
1832
1833 /*
1834 * Link filter bytecodes if not linked yet.
1835 */
1836 lttng_enabler_link_bytecode(event_notifier->desc,
1837 &event_notifier_group->ctx,
1838 &event_notifier->filter_bytecode_runtime_head,
1839 &lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->filter_bytecode_head);
1840
1841 /*
1842 * Link capture bytecodes if not linked yet.
1843 */
1844 lttng_enabler_link_bytecode(event_notifier->desc,
1845 &event_notifier_group->ctx, &event_notifier->capture_bytecode_runtime_head,
1846 &event_notifier_enabler->capture_bytecode_head);
1847
1848 event_notifier->num_captures = event_notifier_enabler->num_captures;
1849 }
1850 end:
1851 return 0;
1852 }
1853
1854 static
1855 void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group *event_notifier_group)
1856 {
1857 struct lttng_event_notifier_enabler *event_notifier_enabler;
1858 struct lttng_event_notifier *event_notifier;
1859
1860 cds_list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head, node)
1861 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler);
1862
1863 /*
1864 * For each event_notifier, if at least one of its enablers is enabled,
1865 * we enable the event_notifier, else we disable it.
1866 */
1867 cds_list_for_each_entry(event_notifier, &event_notifier_group->event_notifiers_head, node) {
1868 struct lttng_enabler_ref *enabler_ref;
1869 struct lttng_bytecode_runtime *runtime;
1870 int enabled = 0, has_enablers_without_bytecode = 0;
1871
1872 /* Enable event_notifiers */
1873 cds_list_for_each_entry(enabler_ref,
1874 &event_notifier->enablers_ref_head, node) {
1875 if (enabler_ref->ref->enabled) {
1876 enabled = 1;
1877 break;
1878 }
1879 }
1880
1881 CMM_STORE_SHARED(event_notifier->enabled, enabled);
1882 /*
1883 * Sync tracepoint registration with event_notifier enabled
1884 * state.
1885 */
1886 if (enabled) {
1887 if (!event_notifier->registered)
1888 register_event_notifier(event_notifier);
1889 } else {
1890 if (event_notifier->registered)
1891 unregister_event_notifier(event_notifier);
1892 }
1893
1894 /* Check if has enablers without bytecode enabled */
1895 cds_list_for_each_entry(enabler_ref,
1896 &event_notifier->enablers_ref_head, node) {
1897 if (enabler_ref->ref->enabled
1898 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1899 has_enablers_without_bytecode = 1;
1900 break;
1901 }
1902 }
1903 event_notifier->has_enablers_without_bytecode =
1904 has_enablers_without_bytecode;
1905
1906 /* Enable filters */
1907 cds_list_for_each_entry(runtime,
1908 &event_notifier->filter_bytecode_runtime_head, node) {
1909 lttng_bytecode_filter_sync_state(runtime);
1910 }
1911
1912 /* Enable captures. */
1913 cds_list_for_each_entry(runtime,
1914 &event_notifier->capture_bytecode_runtime_head, node) {
1915 lttng_bytecode_capture_sync_state(runtime);
1916 }
1917 }
1918 __tracepoint_probe_prune_release_queue();
1919 }
1920
1921 /*
1922 * Apply enablers to session events, adding events to session if need
1923 * be. It is required after each modification applied to an active
1924 * session, and right before session "start".
1925 * "lazy" sync means we only sync if required.
1926 */
1927 static
1928 void lttng_session_lazy_sync_event_enablers(struct lttng_session *session)
1929 {
1930 /* We can skip if session is not active */
1931 if (!session->active)
1932 return;
1933 lttng_session_sync_event_enablers(session);
1934 }
1935
1936 /*
1937 * Update all sessions with the given app context.
1938 * Called with ust lock held.
1939 * This is invoked when an application context gets loaded/unloaded. It
1940 * ensures the context callbacks are in sync with the application
1941 * context (either app context callbacks, or dummy callbacks).
1942 */
1943 void lttng_ust_context_set_session_provider(const char *name,
1944 size_t (*get_size)(struct lttng_ctx_field *field, size_t offset),
1945 void (*record)(struct lttng_ctx_field *field,
1946 struct lttng_ust_lib_ring_buffer_ctx *ctx,
1947 struct lttng_channel *chan),
1948 void (*get_value)(struct lttng_ctx_field *field,
1949 struct lttng_ctx_value *value))
1950 {
1951 struct lttng_session *session;
1952
1953 cds_list_for_each_entry(session, &sessions, node) {
1954 struct lttng_channel *chan;
1955 struct lttng_event *event;
1956 int ret;
1957
1958 ret = lttng_ust_context_set_provider_rcu(&session->ctx,
1959 name, get_size, record, get_value);
1960 if (ret)
1961 abort();
1962 cds_list_for_each_entry(chan, &session->chan_head, node) {
1963 ret = lttng_ust_context_set_provider_rcu(&chan->ctx,
1964 name, get_size, record, get_value);
1965 if (ret)
1966 abort();
1967 }
1968 cds_list_for_each_entry(event, &session->events_head, node) {
1969 ret = lttng_ust_context_set_provider_rcu(&event->ctx,
1970 name, get_size, record, get_value);
1971 if (ret)
1972 abort();
1973 }
1974 }
1975 }
1976
1977 /*
1978 * Update all event_notifier groups with the given app context.
1979 * Called with ust lock held.
1980 * This is invoked when an application context gets loaded/unloaded. It
1981 * ensures the context callbacks are in sync with the application
1982 * context (either app context callbacks, or dummy callbacks).
1983 */
1984 void lttng_ust_context_set_event_notifier_group_provider(const char *name,
1985 size_t (*get_size)(struct lttng_ctx_field *field, size_t offset),
1986 void (*record)(struct lttng_ctx_field *field,
1987 struct lttng_ust_lib_ring_buffer_ctx *ctx,
1988 struct lttng_channel *chan),
1989 void (*get_value)(struct lttng_ctx_field *field,
1990 struct lttng_ctx_value *value))
1991 {
1992 struct lttng_event_notifier_group *event_notifier_group;
1993
1994 cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) {
1995 int ret;
1996
1997 ret = lttng_ust_context_set_provider_rcu(
1998 &event_notifier_group->ctx,
1999 name, get_size, record, get_value);
2000 if (ret)
2001 abort();
2002 }
2003 }
This page took 0.070167 seconds and 5 git commands to generate.