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