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