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