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