2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "buffer-registry.hpp"
12 #include "condition-internal.hpp"
13 #include "event-notifier-error-accounting.hpp"
15 #include "fd-limit.hpp"
17 #include "health-sessiond.hpp"
18 #include "lttng-sessiond.hpp"
19 #include "lttng-ust-ctl.hpp"
20 #include "lttng-ust-error.hpp"
21 #include "notification-thread-commands.hpp"
23 #include "session.hpp"
24 #include "ust-app.hpp"
25 #include "ust-consumer.hpp"
26 #include "ust-field-convert.hpp"
29 #include <common/bytecode/bytecode.hpp>
30 #include <common/common.hpp>
31 #include <common/compat/errno.hpp>
32 #include <common/exception.hpp>
33 #include <common/format.hpp>
34 #include <common/hashtable/utils.hpp>
35 #include <common/make-unique.hpp>
36 #include <common/sessiond-comm/sessiond-comm.hpp>
37 #include <common/urcu.hpp>
39 #include <lttng/condition/condition.h>
40 #include <lttng/condition/event-rule-matches-internal.hpp>
41 #include <lttng/condition/event-rule-matches.h>
42 #include <lttng/event-rule/event-rule-internal.hpp>
43 #include <lttng/event-rule/event-rule.h>
44 #include <lttng/event-rule/user-tracepoint.h>
45 #include <lttng/trigger/trigger-internal.hpp>
57 #include <sys/types.h>
59 #include <urcu/compiler.h>
62 namespace lsu
= lttng::sessiond::ust
;
63 namespace lst
= lttng::sessiond::trace
;
65 struct lttng_ht
*ust_app_ht
;
66 struct lttng_ht
*ust_app_ht_by_sock
;
67 struct lttng_ht
*ust_app_ht_by_notify_sock
;
69 static int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
71 /* Next available channel key. Access under next_channel_key_lock. */
72 static uint64_t _next_channel_key
;
73 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
75 /* Next available session ID. Access under next_session_id_lock. */
76 static uint64_t _next_session_id
;
77 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
82 * Return the session registry according to the buffer type of the given
85 * A registry per UID object MUST exists before calling this function or else
86 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
88 static lsu::registry_session
*get_session_registry(const struct ust_app_session
*ua_sess
)
90 lsu::registry_session
*registry
= nullptr;
92 LTTNG_ASSERT(ua_sess
);
94 switch (ua_sess
->buffer_type
) {
95 case LTTNG_BUFFER_PER_PID
:
97 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
101 registry
= reg_pid
->registry
->reg
.ust
;
104 case LTTNG_BUFFER_PER_UID
:
106 struct buffer_reg_uid
*reg_uid
=
107 buffer_reg_uid_find(ua_sess
->tracing_id
,
108 ua_sess
->bits_per_long
,
109 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
113 registry
= reg_uid
->registry
->reg
.ust
;
124 lsu::registry_session::locked_ptr
get_locked_session_registry(const struct ust_app_session
*ua_sess
)
126 auto session
= get_session_registry(ua_sess
);
128 pthread_mutex_lock(&session
->_lock
);
131 return lsu::registry_session::locked_ptr
{ session
};
136 * Return the incremented value of next_channel_key.
138 static uint64_t get_next_channel_key()
142 pthread_mutex_lock(&next_channel_key_lock
);
143 ret
= ++_next_channel_key
;
144 pthread_mutex_unlock(&next_channel_key_lock
);
149 * Return the atomically incremented value of next_session_id.
151 static uint64_t get_next_session_id()
155 pthread_mutex_lock(&next_session_id_lock
);
156 ret
= ++_next_session_id
;
157 pthread_mutex_unlock(&next_session_id_lock
);
161 static void copy_channel_attr_to_ustctl(struct lttng_ust_ctl_consumer_channel_attr
*attr
,
162 struct lttng_ust_abi_channel_attr
*uattr
)
164 /* Copy event attributes since the layout is different. */
165 attr
->subbuf_size
= uattr
->subbuf_size
;
166 attr
->num_subbuf
= uattr
->num_subbuf
;
167 attr
->overwrite
= uattr
->overwrite
;
168 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
169 attr
->read_timer_interval
= uattr
->read_timer_interval
;
170 attr
->output
= (lttng_ust_abi_output
) uattr
->output
;
171 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
175 * Match function for the hash table lookup.
177 * It matches an ust app event based on three attributes which are the event
178 * name, the filter bytecode and the loglevel.
180 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
182 struct ust_app_event
*event
;
183 const struct ust_app_ht_key
*key
;
184 int ev_loglevel_value
;
189 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
190 key
= (ust_app_ht_key
*) _key
;
191 ev_loglevel_value
= event
->attr
.loglevel
;
193 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
196 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
200 /* Event loglevel. */
201 if (ev_loglevel_value
!= key
->loglevel_type
) {
202 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
&&
203 key
->loglevel_type
== 0 && ev_loglevel_value
== -1) {
205 * Match is accepted. This is because on event creation, the
206 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
207 * -1 are accepted for this loglevel type since 0 is the one set by
208 * the API when receiving an enable event.
215 /* One of the filters is NULL, fail. */
216 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
220 if (key
->filter
&& event
->filter
) {
221 /* Both filters exists, check length followed by the bytecode. */
222 if (event
->filter
->len
!= key
->filter
->len
||
223 memcmp(event
->filter
->data
, key
->filter
->data
, event
->filter
->len
) != 0) {
228 /* One of the exclusions is NULL, fail. */
229 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
233 if (key
->exclusion
&& event
->exclusion
) {
234 /* Both exclusions exists, check count followed by the names. */
235 if (event
->exclusion
->count
!= key
->exclusion
->count
||
236 memcmp(event
->exclusion
->names
,
237 key
->exclusion
->names
,
238 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
251 * Unique add of an ust app event in the given ht. This uses the custom
252 * ht_match_ust_app_event match function and the event name as hash.
254 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
, struct ust_app_event
*event
)
256 struct cds_lfht_node
*node_ptr
;
257 struct ust_app_ht_key key
;
260 LTTNG_ASSERT(ua_chan
);
261 LTTNG_ASSERT(ua_chan
->events
);
264 ht
= ua_chan
->events
;
265 key
.name
= event
->attr
.name
;
266 key
.filter
= event
->filter
;
267 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel
;
268 key
.exclusion
= event
->exclusion
;
270 node_ptr
= cds_lfht_add_unique(ht
->ht
,
271 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
272 ht_match_ust_app_event
,
275 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
279 * Close the notify socket from the given RCU head object. This MUST be called
280 * through a call_rcu().
282 static void close_notify_sock_rcu(struct rcu_head
*head
)
285 struct ust_app_notify_sock_obj
*obj
=
286 lttng::utils::container_of(head
, &ust_app_notify_sock_obj::head
);
288 /* Must have a valid fd here. */
289 LTTNG_ASSERT(obj
->fd
>= 0);
291 ret
= close(obj
->fd
);
293 ERR("close notify sock %d RCU", obj
->fd
);
295 lttng_fd_put(LTTNG_FD_APPS
, 1);
301 * Delete ust context safely. RCU read lock must be held before calling
304 static void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
308 LTTNG_ASSERT(ua_ctx
);
309 ASSERT_RCU_READ_LOCKED();
312 pthread_mutex_lock(&app
->sock_lock
);
313 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
314 pthread_mutex_unlock(&app
->sock_lock
);
316 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
317 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
320 } else if (ret
== -EAGAIN
) {
321 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
325 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
338 * Delete ust app event safely. RCU read lock must be held before calling
341 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
, struct ust_app
*app
)
345 LTTNG_ASSERT(ua_event
);
346 ASSERT_RCU_READ_LOCKED();
348 free(ua_event
->filter
);
349 if (ua_event
->exclusion
!= nullptr)
350 free(ua_event
->exclusion
);
351 if (ua_event
->obj
!= nullptr) {
352 pthread_mutex_lock(&app
->sock_lock
);
353 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
354 pthread_mutex_unlock(&app
->sock_lock
);
356 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
357 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
360 } else if (ret
== -EAGAIN
) {
361 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
365 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
377 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
378 * through a call_rcu().
380 static void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
382 struct ust_app_event_notifier_rule
*obj
=
383 lttng::utils::container_of(head
, &ust_app_event_notifier_rule::rcu_head
);
389 * Delete ust app event notifier rule safely.
391 static void delete_ust_app_event_notifier_rule(
392 int sock
, struct ust_app_event_notifier_rule
*ua_event_notifier_rule
, struct ust_app
*app
)
396 LTTNG_ASSERT(ua_event_notifier_rule
);
398 if (ua_event_notifier_rule
->exclusion
!= nullptr) {
399 free(ua_event_notifier_rule
->exclusion
);
402 if (ua_event_notifier_rule
->obj
!= nullptr) {
403 pthread_mutex_lock(&app
->sock_lock
);
404 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
405 pthread_mutex_unlock(&app
->sock_lock
);
407 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
408 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
411 } else if (ret
== -EAGAIN
) {
412 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
416 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
423 free(ua_event_notifier_rule
->obj
);
426 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
427 call_rcu(&ua_event_notifier_rule
->rcu_head
, free_ust_app_event_notifier_rule_rcu
);
431 * Release ust data object of the given stream.
433 * Return 0 on success or else a negative value.
435 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
439 LTTNG_ASSERT(stream
);
442 pthread_mutex_lock(&app
->sock_lock
);
443 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
444 pthread_mutex_unlock(&app
->sock_lock
);
446 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
447 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
450 } else if (ret
== -EAGAIN
) {
451 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
455 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
461 lttng_fd_put(LTTNG_FD_APPS
, 2);
469 * Delete ust app stream safely. RCU read lock must be held before calling
472 static void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
474 LTTNG_ASSERT(stream
);
475 ASSERT_RCU_READ_LOCKED();
477 (void) release_ust_app_stream(sock
, stream
, app
);
481 static void delete_ust_app_channel_rcu(struct rcu_head
*head
)
483 struct ust_app_channel
*ua_chan
=
484 lttng::utils::container_of(head
, &ust_app_channel::rcu_head
);
486 lttng_ht_destroy(ua_chan
->ctx
);
487 lttng_ht_destroy(ua_chan
->events
);
492 * Extract the lost packet or discarded events counter when the channel is
493 * being deleted and store the value in the parent channel so we can
494 * access it from lttng list and at stop/destroy.
496 * The session list lock must be held by the caller.
498 static void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
500 uint64_t discarded
= 0, lost
= 0;
501 struct ltt_session
*session
;
502 struct ltt_ust_channel
*uchan
;
504 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
509 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
510 if (!session
|| !session
->ust_session
) {
512 * Not finding the session is not an error because there are
513 * multiple ways the channels can be torn down.
515 * 1) The session daemon can initiate the destruction of the
516 * ust app session after receiving a destroy command or
517 * during its shutdown/teardown.
518 * 2) The application, since we are in per-pid tracing, is
519 * unregistering and tearing down its ust app session.
521 * Both paths are protected by the session list lock which
522 * ensures that the accounting of lost packets and discarded
523 * events is done exactly once. The session is then unpublished
524 * from the session list, resulting in this condition.
529 if (ua_chan
->attr
.overwrite
) {
530 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
532 session
->ust_session
->consumer
,
535 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
537 session
->ust_session
->consumer
,
540 uchan
= trace_ust_find_channel_by_name(session
->ust_session
->domain_global
.channels
,
543 ERR("Missing UST channel to store discarded counters");
547 uchan
->per_pid_closed_app_discarded
+= discarded
;
548 uchan
->per_pid_closed_app_lost
+= lost
;
553 session_put(session
);
558 * Delete ust app channel safely. RCU read lock must be held before calling
561 * The session list lock must be held by the caller.
563 static void delete_ust_app_channel(int sock
,
564 struct ust_app_channel
*ua_chan
,
566 const lsu::registry_session::locked_ptr
& locked_registry
)
569 struct lttng_ht_iter iter
;
570 struct ust_app_event
*ua_event
;
571 struct ust_app_ctx
*ua_ctx
;
572 struct ust_app_stream
*stream
, *stmp
;
574 LTTNG_ASSERT(ua_chan
);
575 ASSERT_RCU_READ_LOCKED();
577 DBG3("UST app deleting channel %s", ua_chan
->name
);
580 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
581 cds_list_del(&stream
->list
);
582 delete_ust_app_stream(sock
, stream
, app
);
586 cds_lfht_for_each_entry (ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
587 cds_list_del(&ua_ctx
->list
);
588 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
590 delete_ust_app_ctx(sock
, ua_ctx
, app
);
594 cds_lfht_for_each_entry (ua_chan
->events
->ht
, &iter
.iter
, ua_event
, node
.node
) {
595 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
597 delete_ust_app_event(sock
, ua_event
, app
);
600 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
601 /* Wipe and free registry from session registry. */
602 if (locked_registry
) {
604 locked_registry
->remove_channel(ua_chan
->key
, sock
>= 0);
605 } catch (const std::exception
& ex
) {
606 DBG("Could not find channel for removal: %s", ex
.what());
611 * A negative socket can be used by the caller when
612 * cleaning-up a ua_chan in an error path. Skip the
613 * accounting in this case.
616 save_per_pid_lost_discarded_counters(ua_chan
);
620 if (ua_chan
->obj
!= nullptr) {
621 /* Remove channel from application UST object descriptor. */
622 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
623 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
625 pthread_mutex_lock(&app
->sock_lock
);
626 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
627 pthread_mutex_unlock(&app
->sock_lock
);
629 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
630 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
634 } else if (ret
== -EAGAIN
) {
635 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
640 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
647 lttng_fd_put(LTTNG_FD_APPS
, 1);
650 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
653 int ust_app_register_done(struct ust_app
*app
)
657 pthread_mutex_lock(&app
->sock_lock
);
658 ret
= lttng_ust_ctl_register_done(app
->sock
);
659 pthread_mutex_unlock(&app
->sock_lock
);
663 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
668 pthread_mutex_lock(&app
->sock_lock
);
673 ret
= lttng_ust_ctl_release_object(sock
, data
);
675 pthread_mutex_unlock(&app
->sock_lock
);
681 * Push metadata to consumer socket.
683 * RCU read-side lock must be held to guarantee existence of socket.
684 * Must be called with the ust app session lock held.
685 * Must be called with the registry lock held.
687 * On success, return the len of metadata pushed or else a negative value.
688 * Returning a -EPIPE return value means we could not send the metadata,
689 * but it can be caused by recoverable errors (e.g. the application has
690 * terminated concurrently).
692 ssize_t
ust_app_push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
693 struct consumer_socket
*socket
,
697 char *metadata_str
= nullptr;
698 size_t len
, offset
, new_metadata_len_sent
;
700 uint64_t metadata_key
, metadata_version
;
702 LTTNG_ASSERT(locked_registry
);
703 LTTNG_ASSERT(socket
);
704 ASSERT_RCU_READ_LOCKED();
706 metadata_key
= locked_registry
->_metadata_key
;
709 * Means that no metadata was assigned to the session. This can
710 * happens if no start has been done previously.
716 offset
= locked_registry
->_metadata_len_sent
;
717 len
= locked_registry
->_metadata_len
- locked_registry
->_metadata_len_sent
;
718 new_metadata_len_sent
= locked_registry
->_metadata_len
;
719 metadata_version
= locked_registry
->_metadata_version
;
721 DBG3("No metadata to push for metadata key %" PRIu64
,
722 locked_registry
->_metadata_key
);
724 if (send_zero_data
) {
725 DBG("No metadata to push");
731 /* Allocate only what we have to send. */
732 metadata_str
= calloc
<char>(len
);
734 PERROR("zmalloc ust app metadata string");
738 /* Copy what we haven't sent out. */
739 memcpy(metadata_str
, locked_registry
->_metadata
+ offset
, len
);
742 pthread_mutex_unlock(&locked_registry
->_lock
);
744 * We need to unlock the registry while we push metadata to
745 * break a circular dependency between the consumerd metadata
746 * lock and the sessiond registry lock. Indeed, pushing metadata
747 * to the consumerd awaits that it gets pushed all the way to
748 * relayd, but doing so requires grabbing the metadata lock. If
749 * a concurrent metadata request is being performed by
750 * consumerd, this can try to grab the registry lock on the
751 * sessiond while holding the metadata lock on the consumer
752 * daemon. Those push and pull schemes are performed on two
753 * different bidirectionnal communication sockets.
755 ret
= consumer_push_metadata(
756 socket
, metadata_key
, metadata_str
, len
, offset
, metadata_version
);
757 pthread_mutex_lock(&locked_registry
->_lock
);
760 * There is an acceptable race here between the registry
761 * metadata key assignment and the creation on the
762 * consumer. The session daemon can concurrently push
763 * metadata for this registry while being created on the
764 * consumer since the metadata key of the registry is
765 * assigned *before* it is setup to avoid the consumer
766 * to ask for metadata that could possibly be not found
767 * in the session daemon.
769 * The metadata will get pushed either by the session
770 * being stopped or the consumer requesting metadata if
771 * that race is triggered.
773 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
776 ERR("Error pushing metadata to consumer");
782 * Metadata may have been concurrently pushed, since
783 * we're not holding the registry lock while pushing to
784 * consumer. This is handled by the fact that we send
785 * the metadata content, size, and the offset at which
786 * that metadata belongs. This may arrive out of order
787 * on the consumer side, and the consumer is able to
788 * deal with overlapping fragments. The consumer
789 * supports overlapping fragments, which must be
790 * contiguous starting from offset 0. We keep the
791 * largest metadata_len_sent value of the concurrent
794 locked_registry
->_metadata_len_sent
=
795 std::max(locked_registry
->_metadata_len_sent
, new_metadata_len_sent
);
804 * On error, flag the registry that the metadata is
805 * closed. We were unable to push anything and this
806 * means that either the consumer is not responding or
807 * the metadata cache has been destroyed on the
810 locked_registry
->_metadata_closed
= true;
818 * For a given application and session, push metadata to consumer.
819 * Either sock or consumer is required : if sock is NULL, the default
820 * socket to send the metadata is retrieved from consumer, if sock
821 * is not NULL we use it to send the metadata.
822 * RCU read-side lock must be held while calling this function,
823 * therefore ensuring existence of registry. It also ensures existence
824 * of socket throughout this function.
826 * Return 0 on success else a negative error.
827 * Returning a -EPIPE return value means we could not send the metadata,
828 * but it can be caused by recoverable errors (e.g. the application has
829 * terminated concurrently).
831 static int push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
832 struct consumer_output
*consumer
)
836 struct consumer_socket
*socket
;
838 LTTNG_ASSERT(locked_registry
);
839 LTTNG_ASSERT(consumer
);
840 ASSERT_RCU_READ_LOCKED();
842 if (locked_registry
->_metadata_closed
) {
847 /* Get consumer socket to use to push the metadata.*/
848 socket
= consumer_find_socket_by_bitness(locked_registry
->abi
.bits_per_long
, consumer
);
854 ret
= ust_app_push_metadata(locked_registry
, socket
, 0);
866 * Send to the consumer a close metadata command for the given session. Once
867 * done, the metadata channel is deleted and the session metadata pointer is
868 * nullified. The session lock MUST be held unless the application is
869 * in the destroy path.
871 * Do not hold the registry lock while communicating with the consumerd, because
872 * doing so causes inter-process deadlocks between consumerd and sessiond with
873 * the metadata request notification.
875 * Return 0 on success else a negative value.
877 static int close_metadata(uint64_t metadata_key
,
878 unsigned int consumer_bitness
,
879 struct consumer_output
*consumer
)
882 struct consumer_socket
*socket
;
883 lttng::urcu::read_lock_guard read_lock_guard
;
885 LTTNG_ASSERT(consumer
);
887 /* Get consumer socket to use to push the metadata. */
888 socket
= consumer_find_socket_by_bitness(consumer_bitness
, consumer
);
894 ret
= consumer_close_metadata(socket
, metadata_key
);
903 static void delete_ust_app_session_rcu(struct rcu_head
*head
)
905 struct ust_app_session
*ua_sess
=
906 lttng::utils::container_of(head
, &ust_app_session::rcu_head
);
908 lttng_ht_destroy(ua_sess
->channels
);
913 * Delete ust app session safely. RCU read lock must be held before calling
916 * The session list lock must be held by the caller.
918 static void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
, struct ust_app
*app
)
921 struct lttng_ht_iter iter
;
922 struct ust_app_channel
*ua_chan
;
924 LTTNG_ASSERT(ua_sess
);
925 ASSERT_RCU_READ_LOCKED();
927 pthread_mutex_lock(&ua_sess
->lock
);
929 LTTNG_ASSERT(!ua_sess
->deleted
);
930 ua_sess
->deleted
= true;
932 auto locked_registry
= get_locked_session_registry(ua_sess
);
933 /* Registry can be null on error path during initialization. */
934 if (locked_registry
) {
935 /* Push metadata for application before freeing the application. */
936 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
939 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
940 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
942 delete_ust_app_channel(sock
, ua_chan
, app
, locked_registry
);
945 if (locked_registry
) {
947 * Don't ask to close metadata for global per UID buffers. Close
948 * metadata only on destroy trace session in this case. Also, the
949 * previous push metadata could have flag the metadata registry to
950 * close so don't send a close command if closed.
952 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
953 const auto metadata_key
= locked_registry
->_metadata_key
;
954 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
956 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
957 locked_registry
->_metadata_closed
= true;
960 /* Release lock before communication, see comments in close_metadata(). */
961 locked_registry
.reset();
962 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
966 /* In case of per PID, the registry is kept in the session. */
967 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
968 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
971 * Registry can be null on error path during
974 buffer_reg_pid_remove(reg_pid
);
975 buffer_reg_pid_destroy(reg_pid
);
979 if (ua_sess
->handle
!= -1) {
980 pthread_mutex_lock(&app
->sock_lock
);
981 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
982 pthread_mutex_unlock(&app
->sock_lock
);
984 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
985 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
988 } else if (ret
== -EAGAIN
) {
989 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
993 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
1000 /* Remove session from application UST object descriptor. */
1001 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
1002 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
1006 pthread_mutex_unlock(&ua_sess
->lock
);
1008 consumer_output_put(ua_sess
->consumer
);
1010 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1014 * Delete a traceable application structure from the global list. Never call
1015 * this function outside of a call_rcu call.
1017 static void delete_ust_app(struct ust_app
*app
)
1020 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1021 struct lttng_ht_iter iter
;
1022 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1023 bool event_notifier_write_fd_is_open
;
1026 * The session list lock must be held during this function to guarantee
1027 * the existence of ua_sess.
1029 session_lock_list();
1030 /* Delete ust app sessions info */
1035 cds_list_for_each_entry_safe (ua_sess
, tmp_ua_sess
, &app
->teardown_head
, teardown_node
) {
1036 /* Free every object in the session and the session. */
1038 delete_ust_app_session(sock
, ua_sess
, app
);
1042 /* Remove the event notifier rules associated with this app. */
1044 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1046 event_notifier_rule
,
1048 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1051 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
1056 lttng_ht_destroy(app
->sessions
);
1057 lttng_ht_destroy(app
->ust_sessions_objd
);
1058 lttng_ht_destroy(app
->ust_objd
);
1059 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1062 * This could be NULL if the event notifier setup failed (e.g the app
1063 * was killed or the tracer does not support this feature).
1065 if (app
->event_notifier_group
.object
) {
1066 enum lttng_error_code ret_code
;
1067 enum event_notifier_error_accounting_status status
;
1069 const int event_notifier_read_fd
=
1070 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
);
1072 ret_code
= notification_thread_command_remove_tracer_event_source(
1073 the_notification_thread_handle
, event_notifier_read_fd
);
1074 if (ret_code
!= LTTNG_OK
) {
1075 ERR("Failed to remove application tracer event source from notification thread");
1078 status
= event_notifier_error_accounting_unregister_app(app
);
1079 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1080 ERR("Error unregistering app from event notifier error accounting");
1083 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1084 free(app
->event_notifier_group
.object
);
1087 event_notifier_write_fd_is_open
=
1088 lttng_pipe_is_write_open(app
->event_notifier_group
.event_pipe
);
1089 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1091 * Release the file descriptors reserved for the event notifier pipe.
1092 * The app could be destroyed before the write end of the pipe could be
1093 * passed to the application (and closed). In that case, both file
1094 * descriptors must be released.
1096 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1099 * Wait until we have deleted the application from the sock hash table
1100 * before closing this socket, otherwise an application could re-use the
1101 * socket ID and race with the teardown, using the same hash table entry.
1103 * It's OK to leave the close in call_rcu. We want it to stay unique for
1104 * all RCU readers that could run concurrently with unregister app,
1105 * therefore we _need_ to only close that socket after a grace period. So
1106 * it should stay in this RCU callback.
1108 * This close() is a very important step of the synchronization model so
1109 * every modification to this function must be carefully reviewed.
1115 lttng_fd_put(LTTNG_FD_APPS
, 1);
1117 DBG2("UST app pid %d deleted", app
->pid
);
1119 session_unlock_list();
1123 * URCU intermediate call to delete an UST app.
1125 static void delete_ust_app_rcu(struct rcu_head
*head
)
1127 struct lttng_ht_node_ulong
*node
=
1128 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1129 struct ust_app
*app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
1131 DBG3("Call RCU deleting app PID %d", app
->pid
);
1132 delete_ust_app(app
);
1136 * Delete the session from the application ht and delete the data structure by
1137 * freeing every object inside and releasing them.
1139 * The session list lock must be held by the caller.
1141 static void destroy_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
1144 struct lttng_ht_iter iter
;
1147 LTTNG_ASSERT(ua_sess
);
1149 iter
.iter
.node
= &ua_sess
->node
.node
;
1150 ret
= lttng_ht_del(app
->sessions
, &iter
);
1152 /* Already scheduled for teardown. */
1156 /* Once deleted, free the data structure. */
1157 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1164 * Alloc new UST app session.
1166 static struct ust_app_session
*alloc_ust_app_session()
1168 struct ust_app_session
*ua_sess
;
1170 /* Init most of the default value by allocating and zeroing */
1171 ua_sess
= zmalloc
<ust_app_session
>();
1172 if (ua_sess
== nullptr) {
1177 ua_sess
->handle
= -1;
1178 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1179 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1180 pthread_mutex_init(&ua_sess
->lock
, nullptr);
1189 * Alloc new UST app channel.
1191 static struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1192 struct ust_app_session
*ua_sess
,
1193 struct lttng_ust_abi_channel_attr
*attr
)
1195 struct ust_app_channel
*ua_chan
;
1197 /* Init most of the default value by allocating and zeroing */
1198 ua_chan
= zmalloc
<ust_app_channel
>();
1199 if (ua_chan
== nullptr) {
1204 /* Setup channel name */
1205 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1206 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1208 ua_chan
->enabled
= 1;
1209 ua_chan
->handle
= -1;
1210 ua_chan
->session
= ua_sess
;
1211 ua_chan
->key
= get_next_channel_key();
1212 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1213 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1214 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1216 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1217 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1219 /* Copy attributes */
1221 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1222 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1223 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1224 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1225 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1226 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1227 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1228 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1230 /* By default, the channel is a per cpu channel. */
1231 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1233 DBG3("UST app channel %s allocated", ua_chan
->name
);
1242 * Allocate and initialize a UST app stream.
1244 * Return newly allocated stream pointer or NULL on error.
1246 struct ust_app_stream
*ust_app_alloc_stream()
1248 struct ust_app_stream
*stream
= nullptr;
1250 stream
= zmalloc
<ust_app_stream
>();
1251 if (stream
== nullptr) {
1252 PERROR("zmalloc ust app stream");
1256 /* Zero could be a valid value for a handle so flag it to -1. */
1257 stream
->handle
= -1;
1264 * Alloc new UST app event.
1266 static struct ust_app_event
*alloc_ust_app_event(char *name
, struct lttng_ust_abi_event
*attr
)
1268 struct ust_app_event
*ua_event
;
1270 /* Init most of the default value by allocating and zeroing */
1271 ua_event
= zmalloc
<ust_app_event
>();
1272 if (ua_event
== nullptr) {
1273 PERROR("Failed to allocate ust_app_event structure");
1277 ua_event
->enabled
= 1;
1278 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1279 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1280 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1282 /* Copy attributes */
1284 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1287 DBG3("UST app event %s allocated", ua_event
->name
);
1296 * Allocate a new UST app event notifier rule.
1298 static struct ust_app_event_notifier_rule
*
1299 alloc_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
)
1301 enum lttng_event_rule_generate_exclusions_status generate_exclusion_status
;
1302 enum lttng_condition_status cond_status
;
1303 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1304 struct lttng_condition
*condition
= nullptr;
1305 const struct lttng_event_rule
*event_rule
= nullptr;
1307 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1308 if (ua_event_notifier_rule
== nullptr) {
1309 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1313 ua_event_notifier_rule
->enabled
= 1;
1314 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1315 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, ua_event_notifier_rule
->token
);
1317 condition
= lttng_trigger_get_condition(trigger
);
1318 LTTNG_ASSERT(condition
);
1319 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1320 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1322 cond_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
1323 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1324 LTTNG_ASSERT(event_rule
);
1326 ua_event_notifier_rule
->error_counter_index
=
1327 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1328 /* Acquire the event notifier's reference to the trigger. */
1329 lttng_trigger_get(trigger
);
1331 ua_event_notifier_rule
->trigger
= trigger
;
1332 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1333 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1334 event_rule
, &ua_event_notifier_rule
->exclusion
);
1335 switch (generate_exclusion_status
) {
1336 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1337 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1340 /* Error occurred. */
1341 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1342 goto error_put_trigger
;
1345 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1346 ua_event_notifier_rule
->token
);
1348 return ua_event_notifier_rule
;
1351 lttng_trigger_put(trigger
);
1353 free(ua_event_notifier_rule
);
1358 * Alloc new UST app context.
1360 static struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1362 struct ust_app_ctx
*ua_ctx
;
1364 ua_ctx
= zmalloc
<ust_app_ctx
>();
1365 if (ua_ctx
== nullptr) {
1369 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1372 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1373 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1374 char *provider_name
= nullptr, *ctx_name
= nullptr;
1376 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1377 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1378 if (!provider_name
|| !ctx_name
) {
1379 free(provider_name
);
1384 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1385 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1389 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1397 * Create a liblttng-ust filter bytecode from given bytecode.
1399 * Return allocated filter or NULL on error.
1401 static struct lttng_ust_abi_filter_bytecode
*
1402 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1404 struct lttng_ust_abi_filter_bytecode
*filter
= nullptr;
1406 /* Copy filter bytecode. */
1407 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1409 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
1415 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) == sizeof(struct lttng_ust_abi_filter_bytecode
));
1416 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1422 * Create a liblttng-ust capture bytecode from given bytecode.
1424 * Return allocated filter or NULL on error.
1426 static struct lttng_ust_abi_capture_bytecode
*
1427 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1429 struct lttng_ust_abi_capture_bytecode
*capture
= nullptr;
1431 /* Copy capture bytecode. */
1432 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1434 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
1440 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1441 sizeof(struct lttng_ust_abi_capture_bytecode
));
1442 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1448 * Find an ust_app using the sock and return it. RCU read side lock must be
1449 * held before calling this helper function.
1451 struct ust_app
*ust_app_find_by_sock(int sock
)
1453 struct lttng_ht_node_ulong
*node
;
1454 struct lttng_ht_iter iter
;
1456 ASSERT_RCU_READ_LOCKED();
1458 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
1459 node
= lttng_ht_iter_get_node_ulong(&iter
);
1460 if (node
== nullptr) {
1461 DBG2("UST app find by sock %d not found", sock
);
1465 return lttng::utils::container_of(node
, &ust_app::sock_n
);
1472 * Find an ust_app using the notify sock and return it. RCU read side lock must
1473 * be held before calling this helper function.
1475 static struct ust_app
*find_app_by_notify_sock(int sock
)
1477 struct lttng_ht_node_ulong
*node
;
1478 struct lttng_ht_iter iter
;
1480 ASSERT_RCU_READ_LOCKED();
1482 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *) ((unsigned long) sock
), &iter
);
1483 node
= lttng_ht_iter_get_node_ulong(&iter
);
1484 if (node
== nullptr) {
1485 DBG2("UST app find by notify sock %d not found", sock
);
1489 return lttng::utils::container_of(node
, &ust_app::notify_sock_n
);
1496 * Lookup for an ust app event based on event name, filter bytecode and the
1499 * Return an ust_app_event object or NULL on error.
1501 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1503 const struct lttng_bytecode
*filter
,
1505 const struct lttng_event_exclusion
*exclusion
)
1507 struct lttng_ht_iter iter
;
1508 struct lttng_ht_node_str
*node
;
1509 struct ust_app_event
*event
= nullptr;
1510 struct ust_app_ht_key key
;
1515 /* Setup key for event lookup. */
1517 key
.filter
= filter
;
1518 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) loglevel_value
;
1519 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1520 key
.exclusion
= exclusion
;
1522 /* Lookup using the event name as hash and a custom match fct. */
1523 cds_lfht_lookup(ht
->ht
,
1524 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1525 ht_match_ust_app_event
,
1528 node
= lttng_ht_iter_get_node_str(&iter
);
1529 if (node
== nullptr) {
1533 event
= lttng::utils::container_of(node
, &ust_app_event::node
);
1540 * Look-up an event notifier rule based on its token id.
1542 * Must be called with the RCU read lock held.
1543 * Return an ust_app_event_notifier_rule object or NULL on error.
1545 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(struct lttng_ht
*ht
,
1548 struct lttng_ht_iter iter
;
1549 struct lttng_ht_node_u64
*node
;
1550 struct ust_app_event_notifier_rule
*event_notifier_rule
= nullptr;
1553 ASSERT_RCU_READ_LOCKED();
1555 lttng_ht_lookup(ht
, &token
, &iter
);
1556 node
= lttng_ht_iter_get_node_u64(&iter
);
1557 if (node
== nullptr) {
1558 DBG2("UST app event notifier rule token not found: token = %" PRIu64
, token
);
1562 event_notifier_rule
= lttng::utils::container_of(node
, &ust_app_event_notifier_rule::node
);
1564 return event_notifier_rule
;
1568 * Create the channel context on the tracer.
1570 * Called with UST app session lock held.
1572 static int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1573 struct ust_app_ctx
*ua_ctx
,
1574 struct ust_app
*app
)
1578 health_code_update();
1580 pthread_mutex_lock(&app
->sock_lock
);
1581 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
, ua_chan
->obj
, &ua_ctx
->obj
);
1582 pthread_mutex_unlock(&app
->sock_lock
);
1584 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1586 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1589 } else if (ret
== -EAGAIN
) {
1591 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1595 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1603 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1605 DBG2("UST app context handle %d created successfully for channel %s",
1610 health_code_update();
1615 * Set the filter on the tracer.
1617 static int set_ust_object_filter(struct ust_app
*app
,
1618 const struct lttng_bytecode
*bytecode
,
1619 struct lttng_ust_abi_object_data
*ust_object
)
1622 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= nullptr;
1624 health_code_update();
1626 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1627 if (!ust_bytecode
) {
1628 ret
= -LTTNG_ERR_NOMEM
;
1631 pthread_mutex_lock(&app
->sock_lock
);
1632 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
, ust_object
);
1633 pthread_mutex_unlock(&app
->sock_lock
);
1635 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1637 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1640 } else if (ret
== -EAGAIN
) {
1642 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1646 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1655 DBG2("UST filter successfully set: object = %p", ust_object
);
1658 health_code_update();
1664 * Set a capture bytecode for the passed object.
1665 * The sequence number enforces the ordering at runtime and on reception of
1666 * the captured payloads.
1668 static int set_ust_capture(struct ust_app
*app
,
1669 const struct lttng_bytecode
*bytecode
,
1670 unsigned int capture_seqnum
,
1671 struct lttng_ust_abi_object_data
*ust_object
)
1674 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= nullptr;
1676 health_code_update();
1678 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1679 if (!ust_bytecode
) {
1680 ret
= -LTTNG_ERR_NOMEM
;
1685 * Set the sequence number to ensure the capture of fields is ordered.
1687 ust_bytecode
->seqnum
= capture_seqnum
;
1689 pthread_mutex_lock(&app
->sock_lock
);
1690 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
, ust_object
);
1691 pthread_mutex_unlock(&app
->sock_lock
);
1693 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1695 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1698 } else if (ret
== -EAGAIN
) {
1700 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1704 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1713 DBG2("UST capture successfully set: object = %p", ust_object
);
1716 health_code_update();
1721 static struct lttng_ust_abi_event_exclusion
*
1722 create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion
*exclusion
)
1724 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= nullptr;
1725 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1726 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1728 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1729 if (!ust_exclusion
) {
1734 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1735 sizeof(struct lttng_ust_abi_event_exclusion
));
1736 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1738 return ust_exclusion
;
1742 * Set event exclusions on the tracer.
1744 static int set_ust_object_exclusions(struct ust_app
*app
,
1745 const struct lttng_event_exclusion
*exclusions
,
1746 struct lttng_ust_abi_object_data
*ust_object
)
1749 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= nullptr;
1751 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1753 health_code_update();
1755 ust_exclusions
= create_ust_exclusion_from_exclusion(exclusions
);
1756 if (!ust_exclusions
) {
1757 ret
= -LTTNG_ERR_NOMEM
;
1760 pthread_mutex_lock(&app
->sock_lock
);
1761 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1762 pthread_mutex_unlock(&app
->sock_lock
);
1764 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1766 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1769 } else if (ret
== -EAGAIN
) {
1771 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1775 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1784 DBG2("UST exclusions set successfully for object %p", ust_object
);
1787 health_code_update();
1788 free(ust_exclusions
);
1793 * Disable the specified event on to UST tracer for the UST session.
1795 static int disable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*object
)
1799 health_code_update();
1801 pthread_mutex_lock(&app
->sock_lock
);
1802 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1803 pthread_mutex_unlock(&app
->sock_lock
);
1805 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1807 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1810 } else if (ret
== -EAGAIN
) {
1812 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1816 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1825 DBG2("UST app object %p disabled successfully for app: pid = %d", object
, app
->pid
);
1828 health_code_update();
1833 * Disable the specified channel on to UST tracer for the UST session.
1835 static int disable_ust_channel(struct ust_app
*app
,
1836 struct ust_app_session
*ua_sess
,
1837 struct ust_app_channel
*ua_chan
)
1841 health_code_update();
1843 pthread_mutex_lock(&app
->sock_lock
);
1844 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1845 pthread_mutex_unlock(&app
->sock_lock
);
1847 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1849 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1852 } else if (ret
== -EAGAIN
) {
1854 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1858 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1868 DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1871 health_code_update();
1876 * Enable the specified channel on to UST tracer for the UST session.
1878 static int enable_ust_channel(struct ust_app
*app
,
1879 struct ust_app_session
*ua_sess
,
1880 struct ust_app_channel
*ua_chan
)
1884 health_code_update();
1886 pthread_mutex_lock(&app
->sock_lock
);
1887 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1888 pthread_mutex_unlock(&app
->sock_lock
);
1890 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1892 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1896 } else if (ret
== -EAGAIN
) {
1898 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1903 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1913 ua_chan
->enabled
= 1;
1915 DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1918 health_code_update();
1923 * Enable the specified event on to UST tracer for the UST session.
1925 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1929 health_code_update();
1931 pthread_mutex_lock(&app
->sock_lock
);
1932 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1933 pthread_mutex_unlock(&app
->sock_lock
);
1935 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1937 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1940 } else if (ret
== -EAGAIN
) {
1942 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1946 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1955 DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object
, app
->pid
);
1958 health_code_update();
1963 * Send channel and stream buffer to application.
1965 * Return 0 on success. On error, a negative value is returned.
1967 static int send_channel_pid_to_ust(struct ust_app
*app
,
1968 struct ust_app_session
*ua_sess
,
1969 struct ust_app_channel
*ua_chan
)
1972 struct ust_app_stream
*stream
, *stmp
;
1975 LTTNG_ASSERT(ua_sess
);
1976 LTTNG_ASSERT(ua_chan
);
1978 health_code_update();
1980 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
, app
->sock
);
1982 /* Send channel to the application. */
1983 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1984 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1985 ret
= -ENOTCONN
; /* Caused by app exiting. */
1987 } else if (ret
== -EAGAIN
) {
1988 /* Caused by timeout. */
1989 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
1993 ua_sess
->tracing_id
);
1994 /* Treat this the same way as an application that is exiting. */
1997 } else if (ret
< 0) {
2001 health_code_update();
2003 /* Send all streams to application. */
2004 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2005 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
2006 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2007 ret
= -ENOTCONN
; /* Caused by app exiting. */
2009 } else if (ret
== -EAGAIN
) {
2010 /* Caused by timeout. */
2011 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
2016 ua_sess
->tracing_id
);
2018 * Treat this the same way as an application that is
2022 } else if (ret
< 0) {
2025 /* We don't need the stream anymore once sent to the tracer. */
2026 cds_list_del(&stream
->list
);
2027 delete_ust_app_stream(-1, stream
, app
);
2031 health_code_update();
2036 * Create the specified event onto the UST tracer for a UST session.
2038 * Should be called with session mutex held.
2040 static int create_ust_event(struct ust_app
*app
,
2041 struct ust_app_channel
*ua_chan
,
2042 struct ust_app_event
*ua_event
)
2046 health_code_update();
2048 /* Create UST event on tracer */
2049 pthread_mutex_lock(&app
->sock_lock
);
2050 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
, &ua_event
->obj
);
2051 pthread_mutex_unlock(&app
->sock_lock
);
2053 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2055 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2058 } else if (ret
== -EAGAIN
) {
2060 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2064 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2065 ua_event
->attr
.name
,
2073 ua_event
->handle
= ua_event
->obj
->handle
;
2075 DBG2("UST app event %s created successfully for pid:%d object = %p",
2076 ua_event
->attr
.name
,
2080 health_code_update();
2082 /* Set filter if one is present. */
2083 if (ua_event
->filter
) {
2084 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2090 /* Set exclusions for the event */
2091 if (ua_event
->exclusion
) {
2092 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2098 /* If event not enabled, disable it on the tracer */
2099 if (ua_event
->enabled
) {
2101 * We now need to explicitly enable the event, since it
2102 * is now disabled at creation.
2104 ret
= enable_ust_object(app
, ua_event
->obj
);
2107 * If we hit an EPERM, something is wrong with our enable call. If
2108 * we get an EEXIST, there is a problem on the tracer side since we
2112 case -LTTNG_UST_ERR_PERM
:
2113 /* Code flow problem */
2115 case -LTTNG_UST_ERR_EXIST
:
2116 /* It's OK for our use case. */
2127 health_code_update();
2132 init_ust_event_notifier_from_event_rule(const struct lttng_event_rule
*rule
,
2133 struct lttng_ust_abi_event_notifier
*event_notifier
)
2135 enum lttng_event_rule_status status
;
2136 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2137 int loglevel
= -1, ret
= 0;
2138 const char *pattern
;
2140 memset(event_notifier
, 0, sizeof(*event_notifier
));
2142 if (lttng_event_rule_targets_agent_domain(rule
)) {
2144 * Special event for agents
2145 * The actual meat of the event is in the filter that will be
2146 * attached later on.
2147 * Set the default values for the agent event.
2149 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
2151 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2153 const struct lttng_log_level_rule
*log_level_rule
;
2155 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2156 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2158 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2159 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2160 /* At this point, this is a fatal error. */
2164 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(rule
, &log_level_rule
);
2165 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2166 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2167 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2168 enum lttng_log_level_rule_status llr_status
;
2170 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2171 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2172 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2173 llr_status
= lttng_log_level_rule_exactly_get_level(log_level_rule
,
2176 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2177 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2178 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2179 log_level_rule
, &loglevel
);
2185 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2187 /* At this point this is a fatal error. */
2192 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2193 ret
= lttng_strncpy(
2194 event_notifier
->event
.name
, pattern
, sizeof(event_notifier
->event
.name
));
2196 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern
);
2200 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2201 event_notifier
->event
.loglevel
= loglevel
;
2207 * Create the specified event notifier against the user space tracer of a
2208 * given application.
2210 static int create_ust_event_notifier(struct ust_app
*app
,
2211 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2214 enum lttng_condition_status condition_status
;
2215 const struct lttng_condition
*condition
= nullptr;
2216 struct lttng_ust_abi_event_notifier event_notifier
;
2217 const struct lttng_event_rule
*event_rule
= nullptr;
2218 unsigned int capture_bytecode_count
= 0, i
;
2219 enum lttng_condition_status cond_status
;
2220 enum lttng_event_rule_type event_rule_type
;
2222 health_code_update();
2223 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2225 condition
= lttng_trigger_get_const_condition(ua_event_notifier_rule
->trigger
);
2226 LTTNG_ASSERT(condition
);
2227 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2228 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2230 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2231 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2233 LTTNG_ASSERT(event_rule
);
2235 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2236 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2237 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2238 event_rule_type
== LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2239 event_rule_type
== LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2241 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2242 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2243 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2245 /* Create UST event notifier against the tracer. */
2246 pthread_mutex_lock(&app
->sock_lock
);
2247 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
,
2249 app
->event_notifier_group
.object
,
2250 &ua_event_notifier_rule
->obj
);
2251 pthread_mutex_unlock(&app
->sock_lock
);
2253 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2255 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2258 } else if (ret
== -EAGAIN
) {
2260 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2264 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2265 event_notifier
.event
.name
,
2273 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2275 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2276 event_notifier
.event
.name
,
2279 ua_event_notifier_rule
->obj
);
2281 health_code_update();
2283 /* Set filter if one is present. */
2284 if (ua_event_notifier_rule
->filter
) {
2285 ret
= set_ust_object_filter(
2286 app
, ua_event_notifier_rule
->filter
, ua_event_notifier_rule
->obj
);
2292 /* Set exclusions for the event. */
2293 if (ua_event_notifier_rule
->exclusion
) {
2294 ret
= set_ust_object_exclusions(
2295 app
, ua_event_notifier_rule
->exclusion
, ua_event_notifier_rule
->obj
);
2301 /* Set the capture bytecodes. */
2302 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2303 condition
, &capture_bytecode_count
);
2304 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2306 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2307 const struct lttng_bytecode
*capture_bytecode
=
2308 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2311 ret
= set_ust_capture(app
, capture_bytecode
, i
, ua_event_notifier_rule
->obj
);
2318 * We now need to explicitly enable the event, since it
2319 * is disabled at creation.
2321 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2324 * If we hit an EPERM, something is wrong with our enable call.
2325 * If we get an EEXIST, there is a problem on the tracer side
2326 * since we just created it.
2329 case -LTTNG_UST_ERR_PERM
:
2330 /* Code flow problem. */
2332 case -LTTNG_UST_ERR_EXIST
:
2333 /* It's OK for our use case. */
2343 ua_event_notifier_rule
->enabled
= true;
2346 health_code_update();
2351 * Copy data between an UST app event and a LTT event.
2353 static void shadow_copy_event(struct ust_app_event
*ua_event
, struct ltt_ust_event
*uevent
)
2355 size_t exclusion_alloc_size
;
2357 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2358 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2360 ua_event
->enabled
= uevent
->enabled
;
2362 /* Copy event attributes */
2363 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2365 /* Copy filter bytecode */
2366 if (uevent
->filter
) {
2367 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2368 /* Filter might be NULL here in case of ENONEM. */
2371 /* Copy exclusion data */
2372 if (uevent
->exclusion
) {
2373 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2374 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2375 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2376 if (ua_event
->exclusion
== nullptr) {
2379 memcpy(ua_event
->exclusion
, uevent
->exclusion
, exclusion_alloc_size
);
2385 * Copy data between an UST app channel and a LTT channel.
2387 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
, struct ltt_ust_channel
*uchan
)
2389 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2391 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2392 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2394 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2395 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2397 /* Copy event attributes since the layout is different. */
2398 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2399 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2400 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2401 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2402 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2403 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2404 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2405 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2408 * Note that the attribute channel type is not set since the channel on the
2409 * tracing registry side does not have this information.
2412 ua_chan
->enabled
= uchan
->enabled
;
2413 ua_chan
->tracing_channel_id
= uchan
->id
;
2415 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2419 * Copy data between a UST app session and a regular LTT session.
2421 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2422 struct ltt_ust_session
*usess
,
2423 struct ust_app
*app
)
2425 struct tm
*timeinfo
;
2428 char tmp_shm_path
[PATH_MAX
];
2430 timeinfo
= localtime(&app
->registration_time
);
2431 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2433 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2435 ua_sess
->tracing_id
= usess
->id
;
2436 ua_sess
->id
= get_next_session_id();
2437 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2438 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2439 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2440 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2441 ua_sess
->buffer_type
= usess
->buffer_type
;
2442 ua_sess
->bits_per_long
= app
->abi
.bits_per_long
;
2444 /* There is only one consumer object per session possible. */
2445 consumer_output_get(usess
->consumer
);
2446 ua_sess
->consumer
= usess
->consumer
;
2448 ua_sess
->output_traces
= usess
->output_traces
;
2449 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2450 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &usess
->metadata_attr
);
2452 switch (ua_sess
->buffer_type
) {
2453 case LTTNG_BUFFER_PER_PID
:
2454 ret
= snprintf(ua_sess
->path
,
2455 sizeof(ua_sess
->path
),
2456 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2461 case LTTNG_BUFFER_PER_UID
:
2462 ret
= snprintf(ua_sess
->path
,
2463 sizeof(ua_sess
->path
),
2464 DEFAULT_UST_TRACE_UID_PATH
,
2465 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2466 app
->abi
.bits_per_long
);
2473 PERROR("asprintf UST shadow copy session");
2478 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
, sizeof(ua_sess
->root_shm_path
));
2479 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2480 strncpy(ua_sess
->shm_path
, usess
->shm_path
, sizeof(ua_sess
->shm_path
));
2481 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2482 if (ua_sess
->shm_path
[0]) {
2483 switch (ua_sess
->buffer_type
) {
2484 case LTTNG_BUFFER_PER_PID
:
2485 ret
= snprintf(tmp_shm_path
,
2486 sizeof(tmp_shm_path
),
2487 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2492 case LTTNG_BUFFER_PER_UID
:
2493 ret
= snprintf(tmp_shm_path
,
2494 sizeof(tmp_shm_path
),
2495 "/" DEFAULT_UST_TRACE_UID_PATH
,
2497 app
->abi
.bits_per_long
);
2504 PERROR("sprintf UST shadow copy session");
2508 strncat(ua_sess
->shm_path
,
2510 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2511 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2516 consumer_output_put(ua_sess
->consumer
);
2520 * Lookup sesison wrapper.
2522 static void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2523 struct ust_app
*app
,
2524 struct lttng_ht_iter
*iter
)
2526 /* Get right UST app session from app */
2527 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2531 * Return ust app session from the app session hashtable using the UST session
2534 static struct ust_app_session
*lookup_session_by_app(const struct ltt_ust_session
*usess
,
2535 struct ust_app
*app
)
2537 struct lttng_ht_iter iter
;
2538 struct lttng_ht_node_u64
*node
;
2540 __lookup_session_by_app(usess
, app
, &iter
);
2541 node
= lttng_ht_iter_get_node_u64(&iter
);
2542 if (node
== nullptr) {
2546 return lttng::utils::container_of(node
, &ust_app_session::node
);
2553 * Setup buffer registry per PID for the given session and application. If none
2554 * is found, a new one is created, added to the global registry and
2555 * initialized. If regp is valid, it's set with the newly created object.
2557 * Return 0 on success or else a negative value.
2559 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2560 struct ust_app
*app
,
2561 struct buffer_reg_pid
**regp
)
2564 struct buffer_reg_pid
*reg_pid
;
2566 LTTNG_ASSERT(ua_sess
);
2571 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2574 * This is the create channel path meaning that if there is NO
2575 * registry available, we have to create one for this session.
2577 ret
= buffer_reg_pid_create(
2578 ua_sess
->id
, ®_pid
, ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2586 /* Initialize registry. */
2587 reg_pid
->registry
->reg
.ust
= ust_registry_session_per_pid_create(
2592 reg_pid
->root_shm_path
,
2594 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2595 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2596 ua_sess
->tracing_id
);
2597 if (!reg_pid
->registry
->reg
.ust
) {
2599 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2600 * destroy the buffer registry, because it is always expected
2601 * that if the buffer registry can be found, its ust registry is
2604 buffer_reg_pid_destroy(reg_pid
);
2608 buffer_reg_pid_add(reg_pid
);
2610 DBG3("UST app buffer registry per PID created successfully");
2622 * Setup buffer registry per UID for the given session and application. If none
2623 * is found, a new one is created, added to the global registry and
2624 * initialized. If regp is valid, it's set with the newly created object.
2626 * Return 0 on success or else a negative value.
2628 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2629 struct ust_app_session
*ua_sess
,
2630 struct ust_app
*app
,
2631 struct buffer_reg_uid
**regp
)
2634 struct buffer_reg_uid
*reg_uid
;
2636 LTTNG_ASSERT(usess
);
2641 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
2644 * This is the create channel path meaning that if there is NO
2645 * registry available, we have to create one for this session.
2647 ret
= buffer_reg_uid_create(usess
->id
,
2648 app
->abi
.bits_per_long
,
2652 ua_sess
->root_shm_path
,
2661 /* Initialize registry. */
2662 reg_uid
->registry
->reg
.ust
= ust_registry_session_per_uid_create(app
->abi
,
2665 reg_uid
->root_shm_path
,
2669 ua_sess
->tracing_id
,
2671 if (!reg_uid
->registry
->reg
.ust
) {
2673 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2674 * destroy the buffer registry, because it is always expected
2675 * that if the buffer registry can be found, its ust registry is
2678 buffer_reg_uid_destroy(reg_uid
, nullptr);
2682 /* Add node to teardown list of the session. */
2683 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2685 buffer_reg_uid_add(reg_uid
);
2687 DBG3("UST app buffer registry per UID created successfully");
2698 * Create a session on the tracer side for the given app.
2700 * On success, ua_sess_ptr is populated with the session pointer or else left
2701 * untouched. If the session was created, is_created is set to 1. On error,
2702 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2705 * Returns 0 on success or else a negative code which is either -ENOMEM or
2706 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2708 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2709 struct ust_app
*app
,
2710 struct ust_app_session
**ua_sess_ptr
,
2713 int ret
, created
= 0;
2714 struct ust_app_session
*ua_sess
;
2716 LTTNG_ASSERT(usess
);
2718 LTTNG_ASSERT(ua_sess_ptr
);
2720 health_code_update();
2722 ua_sess
= lookup_session_by_app(usess
, app
);
2723 if (ua_sess
== nullptr) {
2724 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2727 ua_sess
= alloc_ust_app_session();
2728 if (ua_sess
== nullptr) {
2729 /* Only malloc can failed so something is really wrong */
2733 shadow_copy_session(ua_sess
, usess
, app
);
2737 switch (usess
->buffer_type
) {
2738 case LTTNG_BUFFER_PER_PID
:
2739 /* Init local registry. */
2740 ret
= setup_buffer_reg_pid(ua_sess
, app
, nullptr);
2742 delete_ust_app_session(-1, ua_sess
, app
);
2746 case LTTNG_BUFFER_PER_UID
:
2747 /* Look for a global registry. If none exists, create one. */
2748 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, nullptr);
2750 delete_ust_app_session(-1, ua_sess
, app
);
2760 health_code_update();
2762 if (ua_sess
->handle
== -1) {
2763 pthread_mutex_lock(&app
->sock_lock
);
2764 ret
= lttng_ust_ctl_create_session(app
->sock
);
2765 pthread_mutex_unlock(&app
->sock_lock
);
2767 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2768 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2772 } else if (ret
== -EAGAIN
) {
2773 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2778 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2783 delete_ust_app_session(-1, ua_sess
, app
);
2784 if (ret
!= -ENOMEM
) {
2786 * Tracer is probably gone or got an internal error so let's
2787 * behave like it will soon unregister or not usable.
2794 ua_sess
->handle
= ret
;
2796 /* Add ust app session to app's HT */
2797 lttng_ht_node_init_u64(&ua_sess
->node
, ua_sess
->tracing_id
);
2798 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2799 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2800 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
, &ua_sess
->ust_objd_node
);
2802 DBG2("UST app session created successfully with handle %d", ret
);
2805 *ua_sess_ptr
= ua_sess
;
2807 *is_created
= created
;
2810 /* Everything went well. */
2814 health_code_update();
2819 * Match function for a hash table lookup of ust_app_ctx.
2821 * It matches an ust app context based on the context type and, in the case
2822 * of perf counters, their name.
2824 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2826 struct ust_app_ctx
*ctx
;
2827 const struct lttng_ust_context_attr
*key
;
2832 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2833 key
= (lttng_ust_context_attr
*) _key
;
2836 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2841 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2842 if (strncmp(key
->u
.perf_counter
.name
,
2843 ctx
->ctx
.u
.perf_counter
.name
,
2844 sizeof(key
->u
.perf_counter
.name
)) != 0) {
2848 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2849 if (strcmp(key
->u
.app_ctx
.provider_name
, ctx
->ctx
.u
.app_ctx
.provider_name
) != 0 ||
2850 strcmp(key
->u
.app_ctx
.ctx_name
, ctx
->ctx
.u
.app_ctx
.ctx_name
) != 0) {
2866 * Lookup for an ust app context from an lttng_ust_context.
2868 * Must be called while holding RCU read side lock.
2869 * Return an ust_app_ctx object or NULL on error.
2871 static struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2872 struct lttng_ust_context_attr
*uctx
)
2874 struct lttng_ht_iter iter
;
2875 struct lttng_ht_node_ulong
*node
;
2876 struct ust_app_ctx
*app_ctx
= nullptr;
2880 ASSERT_RCU_READ_LOCKED();
2882 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2883 cds_lfht_lookup(ht
->ht
,
2884 ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2885 ht_match_ust_app_ctx
,
2888 node
= lttng_ht_iter_get_node_ulong(&iter
);
2893 app_ctx
= lttng::utils::container_of(node
, &ust_app_ctx::node
);
2900 * Create a context for the channel on the tracer.
2902 * Called with UST app session lock held and a RCU read side lock.
2904 static int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2905 struct lttng_ust_context_attr
*uctx
,
2906 struct ust_app
*app
)
2909 struct ust_app_ctx
*ua_ctx
;
2911 ASSERT_RCU_READ_LOCKED();
2913 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2915 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2921 ua_ctx
= alloc_ust_app_ctx(uctx
);
2922 if (ua_ctx
== nullptr) {
2928 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2929 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2930 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2932 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2942 * Enable on the tracer side a ust app event for the session and channel.
2944 * Called with UST app session lock held.
2946 static int enable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2950 ret
= enable_ust_object(app
, ua_event
->obj
);
2955 ua_event
->enabled
= 1;
2962 * Disable on the tracer side a ust app event for the session and channel.
2964 static int disable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2968 ret
= disable_ust_object(app
, ua_event
->obj
);
2973 ua_event
->enabled
= 0;
2980 * Lookup ust app channel for session and disable it on the tracer side.
2982 static int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2983 struct ust_app_channel
*ua_chan
,
2984 struct ust_app
*app
)
2988 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2993 ua_chan
->enabled
= 0;
3000 * Lookup ust app channel for session and enable it on the tracer side. This
3001 * MUST be called with a RCU read side lock acquired.
3003 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
3004 struct ltt_ust_channel
*uchan
,
3005 struct ust_app
*app
)
3008 struct lttng_ht_iter iter
;
3009 struct lttng_ht_node_str
*ua_chan_node
;
3010 struct ust_app_channel
*ua_chan
;
3012 ASSERT_RCU_READ_LOCKED();
3014 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3015 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3016 if (ua_chan_node
== nullptr) {
3017 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
3019 ua_sess
->tracing_id
);
3023 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3025 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
3035 * Ask the consumer to create a channel and get it if successful.
3037 * Called with UST app session lock held.
3039 * Return 0 on success or else a negative value.
3041 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
3042 struct ust_app_session
*ua_sess
,
3043 struct ust_app_channel
*ua_chan
,
3045 lsu::registry_session
*registry
)
3048 unsigned int nb_fd
= 0;
3049 struct consumer_socket
*socket
;
3051 LTTNG_ASSERT(usess
);
3052 LTTNG_ASSERT(ua_sess
);
3053 LTTNG_ASSERT(ua_chan
);
3054 LTTNG_ASSERT(registry
);
3057 health_code_update();
3059 /* Get the right consumer socket for the application. */
3060 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3066 health_code_update();
3068 /* Need one fd for the channel. */
3069 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3071 ERR("Exhausted number of available FD upon create channel");
3076 * Ask consumer to create channel. The consumer will return the number of
3077 * stream we have to expect.
3079 ret
= ust_consumer_ask_channel(
3080 ua_sess
, ua_chan
, usess
->consumer
, socket
, registry
, usess
->current_trace_chunk
);
3086 * Compute the number of fd needed before receiving them. It must be 2 per
3087 * stream (2 being the default value here).
3089 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3091 /* Reserve the amount of file descriptor we need. */
3092 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3094 ERR("Exhausted number of available FD upon create channel");
3095 goto error_fd_get_stream
;
3098 health_code_update();
3101 * Now get the channel from the consumer. This call will populate the stream
3102 * list of that channel and set the ust objects.
3104 if (usess
->consumer
->enabled
) {
3105 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3115 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3116 error_fd_get_stream
:
3118 * Initiate a destroy channel on the consumer since we had an error
3119 * handling it on our side. The return value is of no importance since we
3120 * already have a ret value set by the previous error that we need to
3123 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3125 lttng_fd_put(LTTNG_FD_APPS
, 1);
3127 health_code_update();
3133 * Duplicate the ust data object of the ust app stream and save it in the
3134 * buffer registry stream.
3136 * Return 0 on success or else a negative value.
3138 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3139 struct ust_app_stream
*stream
)
3143 LTTNG_ASSERT(reg_stream
);
3144 LTTNG_ASSERT(stream
);
3146 /* Duplicating a stream requires 2 new fds. Reserve them. */
3147 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3149 ERR("Exhausted number of available FD upon duplicate stream");
3153 /* Duplicate object for stream once the original is in the registry. */
3154 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
, reg_stream
->obj
.ust
);
3156 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3157 reg_stream
->obj
.ust
,
3160 lttng_fd_put(LTTNG_FD_APPS
, 2);
3163 stream
->handle
= stream
->obj
->handle
;
3170 * Duplicate the ust data object of the ust app. channel and save it in the
3171 * buffer registry channel.
3173 * Return 0 on success or else a negative value.
3175 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3176 struct ust_app_channel
*ua_chan
)
3180 LTTNG_ASSERT(buf_reg_chan
);
3181 LTTNG_ASSERT(ua_chan
);
3183 /* Duplicating a channel requires 1 new fd. Reserve it. */
3184 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3186 ERR("Exhausted number of available FD upon duplicate channel");
3190 /* Duplicate object for stream once the original is in the registry. */
3191 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3193 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3194 buf_reg_chan
->obj
.ust
,
3199 ua_chan
->handle
= ua_chan
->obj
->handle
;
3204 lttng_fd_put(LTTNG_FD_APPS
, 1);
3210 * For a given channel buffer registry, setup all streams of the given ust
3211 * application channel.
3213 * Return 0 on success or else a negative value.
3215 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3216 struct ust_app_channel
*ua_chan
,
3217 struct ust_app
*app
)
3220 struct ust_app_stream
*stream
, *stmp
;
3222 LTTNG_ASSERT(buf_reg_chan
);
3223 LTTNG_ASSERT(ua_chan
);
3225 DBG2("UST app setup buffer registry stream");
3227 /* Send all streams to application. */
3228 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3229 struct buffer_reg_stream
*reg_stream
;
3231 ret
= buffer_reg_stream_create(®_stream
);
3237 * Keep original pointer and nullify it in the stream so the delete
3238 * stream call does not release the object.
3240 reg_stream
->obj
.ust
= stream
->obj
;
3241 stream
->obj
= nullptr;
3242 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3244 /* We don't need the streams anymore. */
3245 cds_list_del(&stream
->list
);
3246 delete_ust_app_stream(-1, stream
, app
);
3254 * Create a buffer registry channel for the given session registry and
3255 * application channel object. If regp pointer is valid, it's set with the
3256 * created object. Important, the created object is NOT added to the session
3257 * registry hash table.
3259 * Return 0 on success else a negative value.
3261 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3262 struct ust_app_channel
*ua_chan
,
3263 struct buffer_reg_channel
**regp
)
3266 struct buffer_reg_channel
*buf_reg_chan
= nullptr;
3268 LTTNG_ASSERT(reg_sess
);
3269 LTTNG_ASSERT(ua_chan
);
3271 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3273 /* Create buffer registry channel. */
3274 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3278 LTTNG_ASSERT(buf_reg_chan
);
3279 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3280 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3281 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3283 /* Create and add a channel registry to session. */
3285 reg_sess
->reg
.ust
->add_channel(ua_chan
->tracing_channel_id
);
3286 } catch (const std::exception
& ex
) {
3287 ERR("Failed to add a channel registry to userspace registry session: %s",
3293 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3296 *regp
= buf_reg_chan
;
3302 /* Safe because the registry channel object was not added to any HT. */
3303 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3309 * Setup buffer registry channel for the given session registry and application
3310 * channel object. If regp pointer is valid, it's set with the created object.
3312 * Return 0 on success else a negative value.
3314 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3315 struct ust_app_channel
*ua_chan
,
3316 struct buffer_reg_channel
*buf_reg_chan
,
3317 struct ust_app
*app
)
3321 LTTNG_ASSERT(reg_sess
);
3322 LTTNG_ASSERT(buf_reg_chan
);
3323 LTTNG_ASSERT(ua_chan
);
3324 LTTNG_ASSERT(ua_chan
->obj
);
3326 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3328 /* Setup all streams for the registry. */
3329 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3334 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3335 ua_chan
->obj
= nullptr;
3340 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3341 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3346 * Send buffer registry channel to the application.
3348 * Return 0 on success else a negative value.
3350 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3351 struct ust_app
*app
,
3352 struct ust_app_session
*ua_sess
,
3353 struct ust_app_channel
*ua_chan
)
3356 struct buffer_reg_stream
*reg_stream
;
3358 LTTNG_ASSERT(buf_reg_chan
);
3360 LTTNG_ASSERT(ua_sess
);
3361 LTTNG_ASSERT(ua_chan
);
3363 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3365 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3370 /* Send channel to the application. */
3371 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3372 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3373 ret
= -ENOTCONN
; /* Caused by app exiting. */
3375 } else if (ret
== -EAGAIN
) {
3376 /* Caused by timeout. */
3377 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
3381 ua_sess
->tracing_id
);
3382 /* Treat this the same way as an application that is exiting. */
3385 } else if (ret
< 0) {
3389 health_code_update();
3391 /* Send all streams to application. */
3392 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3393 cds_list_for_each_entry (reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3394 struct ust_app_stream stream
= {};
3396 ret
= duplicate_stream_object(reg_stream
, &stream
);
3398 goto error_stream_unlock
;
3401 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3403 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3404 ret
= -ENOTCONN
; /* Caused by app exiting. */
3405 } else if (ret
== -EAGAIN
) {
3407 * Caused by timeout.
3408 * Treat this the same way as an application
3411 WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64
3415 ua_sess
->tracing_id
);
3418 (void) release_ust_app_stream(-1, &stream
, app
);
3419 goto error_stream_unlock
;
3423 * The return value is not important here. This function will output an
3426 (void) release_ust_app_stream(-1, &stream
, app
);
3429 error_stream_unlock
:
3430 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3436 * Create and send to the application the created buffers with per UID buffers.
3438 * This MUST be called with a RCU read side lock acquired.
3439 * The session list lock and the session's lock must be acquired.
3441 * Return 0 on success else a negative value.
3443 static int create_channel_per_uid(struct ust_app
*app
,
3444 struct ltt_ust_session
*usess
,
3445 struct ust_app_session
*ua_sess
,
3446 struct ust_app_channel
*ua_chan
)
3449 struct buffer_reg_uid
*reg_uid
;
3450 struct buffer_reg_channel
*buf_reg_chan
;
3451 struct ltt_session
*session
= nullptr;
3452 enum lttng_error_code notification_ret
;
3455 LTTNG_ASSERT(usess
);
3456 LTTNG_ASSERT(ua_sess
);
3457 LTTNG_ASSERT(ua_chan
);
3458 ASSERT_RCU_READ_LOCKED();
3460 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3462 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
3464 * The session creation handles the creation of this global registry
3465 * object. If none can be find, there is a code flow problem or a
3468 LTTNG_ASSERT(reg_uid
);
3470 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
, reg_uid
);
3475 /* Create the buffer registry channel object. */
3476 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3478 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan
->name
);
3482 session
= session_find_by_id(ua_sess
->tracing_id
);
3483 LTTNG_ASSERT(session
);
3484 ASSERT_LOCKED(session
->lock
);
3485 ASSERT_SESSION_LIST_LOCKED();
3488 * Create the buffers on the consumer side. This call populates the
3489 * ust app channel object with all streams and data object.
3491 ret
= do_consumer_create_channel(
3492 usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, reg_uid
->registry
->reg
.ust
);
3494 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3497 * Let's remove the previously created buffer registry channel so
3498 * it's not visible anymore in the session registry.
3500 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3502 locked_registry
->remove_channel(ua_chan
->tracing_channel_id
, false);
3503 } catch (const std::exception
& ex
) {
3504 DBG("Could not find channel for removal: %s", ex
.what());
3506 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3507 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3512 * Setup the streams and add it to the session registry.
3514 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, buf_reg_chan
, app
);
3516 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3521 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3522 auto& ust_reg_chan
= locked_registry
->channel(ua_chan
->tracing_channel_id
);
3524 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3527 /* Notify the notification subsystem of the channel's creation. */
3528 notification_ret
= notification_thread_command_add_channel(
3529 the_notification_thread_handle
,
3534 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3535 if (notification_ret
!= LTTNG_OK
) {
3536 ret
= -(int) notification_ret
;
3537 ERR("Failed to add channel to notification thread");
3542 /* Send buffers to the application. */
3543 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3545 if (ret
!= -ENOTCONN
) {
3546 ERR("Error sending channel to application");
3553 session_put(session
);
3559 * Create and send to the application the created buffers with per PID buffers.
3561 * Called with UST app session lock held.
3562 * The session list lock and the session's lock must be acquired.
3564 * Return 0 on success else a negative value.
3566 static int create_channel_per_pid(struct ust_app
*app
,
3567 struct ltt_ust_session
*usess
,
3568 struct ust_app_session
*ua_sess
,
3569 struct ust_app_channel
*ua_chan
)
3572 lsu::registry_session
*registry
;
3573 enum lttng_error_code cmd_ret
;
3574 struct ltt_session
*session
= nullptr;
3575 uint64_t chan_reg_key
;
3578 LTTNG_ASSERT(usess
);
3579 LTTNG_ASSERT(ua_sess
);
3580 LTTNG_ASSERT(ua_chan
);
3582 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3586 registry
= get_session_registry(ua_sess
);
3587 /* The UST app session lock is held, registry shall not be null. */
3588 LTTNG_ASSERT(registry
);
3590 /* Create and add a new channel registry to session. */
3592 registry
->add_channel(ua_chan
->key
);
3593 } catch (const std::exception
& ex
) {
3594 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3601 session
= session_find_by_id(ua_sess
->tracing_id
);
3602 LTTNG_ASSERT(session
);
3603 ASSERT_LOCKED(session
->lock
);
3604 ASSERT_SESSION_LIST_LOCKED();
3606 /* Create and get channel on the consumer side. */
3607 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, registry
);
3609 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3610 goto error_remove_from_registry
;
3613 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3615 if (ret
!= -ENOTCONN
) {
3616 ERR("Error sending channel to application");
3618 goto error_remove_from_registry
;
3621 chan_reg_key
= ua_chan
->key
;
3623 auto locked_registry
= registry
->lock();
3625 auto& ust_reg_chan
= locked_registry
->channel(chan_reg_key
);
3626 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3629 cmd_ret
= notification_thread_command_add_channel(the_notification_thread_handle
,
3634 ua_chan
->attr
.subbuf_size
*
3635 ua_chan
->attr
.num_subbuf
);
3636 if (cmd_ret
!= LTTNG_OK
) {
3637 ret
= -(int) cmd_ret
;
3638 ERR("Failed to add channel to notification thread");
3639 goto error_remove_from_registry
;
3642 error_remove_from_registry
:
3645 auto locked_registry
= registry
->lock();
3646 locked_registry
->remove_channel(ua_chan
->key
, false);
3647 } catch (const std::exception
& ex
) {
3648 DBG("Could not find channel for removal: %s", ex
.what());
3654 session_put(session
);
3660 * From an already allocated ust app channel, create the channel buffers if
3661 * needed and send them to the application. This MUST be called with a RCU read
3662 * side lock acquired.
3664 * Called with UST app session lock held.
3666 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3667 * the application exited concurrently.
3669 static int ust_app_channel_send(struct ust_app
*app
,
3670 struct ltt_ust_session
*usess
,
3671 struct ust_app_session
*ua_sess
,
3672 struct ust_app_channel
*ua_chan
)
3677 LTTNG_ASSERT(usess
);
3678 LTTNG_ASSERT(usess
->active
);
3679 LTTNG_ASSERT(ua_sess
);
3680 LTTNG_ASSERT(ua_chan
);
3681 ASSERT_RCU_READ_LOCKED();
3683 /* Handle buffer type before sending the channel to the application. */
3684 switch (usess
->buffer_type
) {
3685 case LTTNG_BUFFER_PER_UID
:
3687 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3693 case LTTNG_BUFFER_PER_PID
:
3695 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3707 /* Initialize ust objd object using the received handle and add it. */
3708 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3709 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3711 /* If channel is not enabled, disable it on the tracer */
3712 if (!ua_chan
->enabled
) {
3713 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3724 * Create UST app channel and return it through ua_chanp if not NULL.
3726 * Called with UST app session lock and RCU read-side lock held.
3728 * Return 0 on success or else a negative value.
3730 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3731 struct ltt_ust_channel
*uchan
,
3732 enum lttng_ust_abi_chan_type type
,
3733 struct ltt_ust_session
*usess
__attribute__((unused
)),
3734 struct ust_app_channel
**ua_chanp
)
3737 struct lttng_ht_iter iter
;
3738 struct lttng_ht_node_str
*ua_chan_node
;
3739 struct ust_app_channel
*ua_chan
;
3741 ASSERT_RCU_READ_LOCKED();
3743 /* Lookup channel in the ust app session */
3744 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3745 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3746 if (ua_chan_node
!= nullptr) {
3747 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3751 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3752 if (ua_chan
== nullptr) {
3753 /* Only malloc can fail here */
3757 shadow_copy_channel(ua_chan
, uchan
);
3759 /* Set channel type. */
3760 ua_chan
->attr
.type
= type
;
3762 /* Only add the channel if successful on the tracer side. */
3763 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3766 *ua_chanp
= ua_chan
;
3769 /* Everything went well. */
3777 * Create UST app event and create it on the tracer side.
3779 * Must be called with the RCU read side lock held.
3780 * Called with ust app session mutex held.
3782 static int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3783 struct ltt_ust_event
*uevent
,
3784 struct ust_app
*app
)
3787 struct ust_app_event
*ua_event
;
3789 ASSERT_RCU_READ_LOCKED();
3791 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3792 if (ua_event
== nullptr) {
3793 /* Only failure mode of alloc_ust_app_event(). */
3797 shadow_copy_event(ua_event
, uevent
);
3799 /* Create it on the tracer side */
3800 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3803 * Not found previously means that it does not exist on the
3804 * tracer. If the application reports that the event existed,
3805 * it means there is a bug in the sessiond or lttng-ust
3806 * (or corruption, etc.)
3808 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3809 ERR("Tracer for application reported that an event being created already existed: "
3810 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3820 add_unique_ust_app_event(ua_chan
, ua_event
);
3822 DBG2("UST app create event completed: app = '%s' pid = %d", app
->name
, app
->pid
);
3828 /* Valid. Calling here is already in a read side lock */
3829 delete_ust_app_event(-1, ua_event
, app
);
3834 * Create UST app event notifier rule and create it on the tracer side.
3836 * Must be called with the RCU read side lock held.
3837 * Called with ust app session mutex held.
3839 static int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
, struct ust_app
*app
)
3842 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3844 ASSERT_RCU_READ_LOCKED();
3846 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3847 if (ua_event_notifier_rule
== nullptr) {
3852 /* Create it on the tracer side. */
3853 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3856 * Not found previously means that it does not exist on the
3857 * tracer. If the application reports that the event existed,
3858 * it means there is a bug in the sessiond or lttng-ust
3859 * (or corruption, etc.)
3861 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3862 ERR("Tracer for application reported that an event notifier being created already exists: "
3863 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3864 lttng_trigger_get_tracer_token(trigger
),
3873 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3874 &ua_event_notifier_rule
->node
);
3876 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3879 lttng_trigger_get_tracer_token(trigger
));
3884 /* The RCU read side lock is already being held by the caller. */
3885 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3891 * Create UST metadata and open it on the tracer side.
3893 * Called with UST app session lock held and RCU read side lock.
3895 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3896 struct ust_app
*app
,
3897 struct consumer_output
*consumer
)
3900 struct ust_app_channel
*metadata
;
3901 struct consumer_socket
*socket
;
3902 struct ltt_session
*session
= nullptr;
3904 LTTNG_ASSERT(ua_sess
);
3906 LTTNG_ASSERT(consumer
);
3907 ASSERT_RCU_READ_LOCKED();
3909 auto locked_registry
= get_locked_session_registry(ua_sess
);
3910 /* The UST app session is held registry shall not be null. */
3911 LTTNG_ASSERT(locked_registry
);
3913 /* Metadata already exists for this registry or it was closed previously */
3914 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3919 /* Allocate UST metadata */
3920 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, nullptr);
3922 /* malloc() failed */
3927 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3929 /* Need one fd for the channel. */
3930 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3932 ERR("Exhausted number of available FD upon create metadata");
3936 /* Get the right consumer socket for the application. */
3937 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3940 goto error_consumer
;
3944 * Keep metadata key so we can identify it on the consumer side. Assign it
3945 * to the registry *before* we ask the consumer so we avoid the race of the
3946 * consumer requesting the metadata and the ask_channel call on our side
3947 * did not returned yet.
3949 locked_registry
->_metadata_key
= metadata
->key
;
3951 session
= session_find_by_id(ua_sess
->tracing_id
);
3952 LTTNG_ASSERT(session
);
3953 ASSERT_LOCKED(session
->lock
);
3954 ASSERT_SESSION_LIST_LOCKED();
3957 * Ask the metadata channel creation to the consumer. The metadata object
3958 * will be created by the consumer and kept their. However, the stream is
3959 * never added or monitored until we do a first push metadata to the
3962 ret
= ust_consumer_ask_channel(ua_sess
,
3966 locked_registry
.get(),
3967 session
->current_trace_chunk
);
3969 /* Nullify the metadata key so we don't try to close it later on. */
3970 locked_registry
->_metadata_key
= 0;
3971 goto error_consumer
;
3975 * The setup command will make the metadata stream be sent to the relayd,
3976 * if applicable, and the thread managing the metadatas. This is important
3977 * because after this point, if an error occurs, the only way the stream
3978 * can be deleted is to be monitored in the consumer.
3980 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3982 /* Nullify the metadata key so we don't try to close it later on. */
3983 locked_registry
->_metadata_key
= 0;
3984 goto error_consumer
;
3987 DBG2("UST metadata with key %" PRIu64
" created for app pid %d", metadata
->key
, app
->pid
);
3990 lttng_fd_put(LTTNG_FD_APPS
, 1);
3991 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3994 session_put(session
);
4000 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
4001 * acquired before calling this function.
4003 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
4005 struct ust_app
*app
= nullptr;
4006 struct lttng_ht_node_ulong
*node
;
4007 struct lttng_ht_iter iter
;
4009 lttng_ht_lookup(ust_app_ht
, (void *) ((unsigned long) pid
), &iter
);
4010 node
= lttng_ht_iter_get_node_ulong(&iter
);
4011 if (node
== nullptr) {
4012 DBG2("UST app no found with pid %d", pid
);
4016 DBG2("Found UST app by pid %d", pid
);
4018 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
4025 * Allocate and init an UST app object using the registration information and
4026 * the command socket. This is called when the command socket connects to the
4029 * The object is returned on success or else NULL.
4031 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
4034 struct ust_app
*lta
= nullptr;
4035 struct lttng_pipe
*event_notifier_event_source_pipe
= nullptr;
4038 LTTNG_ASSERT(sock
>= 0);
4040 DBG3("UST app creating application for socket %d", sock
);
4042 if ((msg
->bits_per_long
== 64 && (uatomic_read(&the_ust_consumerd64_fd
) == -EINVAL
)) ||
4043 (msg
->bits_per_long
== 32 && (uatomic_read(&the_ust_consumerd32_fd
) == -EINVAL
))) {
4044 ERR("Registration failed: application \"%s\" (pid: %d) has "
4045 "%d-bit long, but no consumerd for this size is available.\n",
4048 msg
->bits_per_long
);
4053 * Reserve the two file descriptors of the event source pipe. The write
4054 * end will be closed once it is passed to the application, at which
4055 * point a single 'put' will be performed.
4057 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
4059 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4065 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
4066 if (!event_notifier_event_source_pipe
) {
4067 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4073 lta
= zmalloc
<ust_app
>();
4074 if (lta
== nullptr) {
4076 goto error_free_pipe
;
4079 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4081 lta
->ppid
= msg
->ppid
;
4082 lta
->uid
= msg
->uid
;
4083 lta
->gid
= msg
->gid
;
4086 .bits_per_long
= msg
->bits_per_long
,
4087 .long_alignment
= msg
->long_alignment
,
4088 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4089 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4090 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4091 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4092 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4093 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4094 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4097 lta
->v_major
= msg
->major
;
4098 lta
->v_minor
= msg
->minor
;
4099 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4100 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4101 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4102 lta
->notify_sock
= -1;
4103 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4105 /* Copy name and make sure it's NULL terminated. */
4106 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4107 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4110 * Before this can be called, when receiving the registration information,
4111 * the application compatibility is checked. So, at this point, the
4112 * application can work with this session daemon.
4114 lta
->compatible
= 1;
4116 lta
->pid
= msg
->pid
;
4117 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4119 pthread_mutex_init(<a
->sock_lock
, nullptr);
4120 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4122 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4126 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4127 lttng_fd_put(LTTNG_FD_APPS
, 2);
4133 * For a given application object, add it to every hash table.
4135 void ust_app_add(struct ust_app
*app
)
4138 LTTNG_ASSERT(app
->notify_sock
>= 0);
4140 app
->registration_time
= time(nullptr);
4145 * On a re-registration, we want to kick out the previous registration of
4148 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4151 * The socket _should_ be unique until _we_ call close. So, a add_unique
4152 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4153 * already in the table.
4155 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4157 /* Add application to the notify socket hash table. */
4158 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4159 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4161 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4162 "notify_sock =%d (version %d.%d)",
4177 * Set the application version into the object.
4179 * Return 0 on success else a negative value either an errno code or a
4180 * LTTng-UST error code.
4182 int ust_app_version(struct ust_app
*app
)
4188 pthread_mutex_lock(&app
->sock_lock
);
4189 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4190 pthread_mutex_unlock(&app
->sock_lock
);
4192 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4193 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4196 } else if (ret
== -EAGAIN
) {
4197 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4201 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4211 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4213 return app
->v_major
>= 9;
4216 bool ust_app_supports_counters(const struct ust_app
*app
)
4218 return app
->v_major
>= 9;
4222 * Setup the base event notifier group.
4224 * Return 0 on success else a negative value either an errno code or a
4225 * LTTng-UST error code.
4227 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4230 int event_pipe_write_fd
;
4231 struct lttng_ust_abi_object_data
*event_notifier_group
= nullptr;
4232 enum lttng_error_code lttng_ret
;
4233 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4237 if (!ust_app_supports_notifiers(app
)) {
4242 /* Get the write side of the pipe. */
4243 event_pipe_write_fd
= lttng_pipe_get_writefd(app
->event_notifier_group
.event_pipe
);
4245 pthread_mutex_lock(&app
->sock_lock
);
4246 ret
= lttng_ust_ctl_create_event_notifier_group(
4247 app
->sock
, event_pipe_write_fd
, &event_notifier_group
);
4248 pthread_mutex_unlock(&app
->sock_lock
);
4250 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4252 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4255 } else if (ret
== -EAGAIN
) {
4257 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4261 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4265 event_pipe_write_fd
);
4270 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4272 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4279 * Release the file descriptor that was reserved for the write-end of
4282 lttng_fd_put(LTTNG_FD_APPS
, 1);
4284 lttng_ret
= notification_thread_command_add_tracer_event_source(
4285 the_notification_thread_handle
,
4286 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4288 if (lttng_ret
!= LTTNG_OK
) {
4289 ERR("Failed to add tracer event source to notification thread");
4294 /* Assign handle only when the complete setup is valid. */
4295 app
->event_notifier_group
.object
= event_notifier_group
;
4297 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
4298 switch (event_notifier_error_accounting_status
) {
4299 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4301 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4302 DBG3("Failed to setup event notifier error accounting (application does not support notifier error accounting): app socket fd = %d, app name = '%s', app pid = %d",
4307 goto error_accounting
;
4308 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4309 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4314 goto error_accounting
;
4316 ERR("Failed to setup event notifier error accounting for app");
4318 goto error_accounting
;
4324 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4325 the_notification_thread_handle
,
4326 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
));
4327 if (lttng_ret
!= LTTNG_OK
) {
4328 ERR("Failed to remove application tracer event source from notification thread");
4332 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4333 free(app
->event_notifier_group
.object
);
4334 app
->event_notifier_group
.object
= nullptr;
4339 * Unregister app by removing it from the global traceable app list and freeing
4342 * The socket is already closed at this point so no close to sock.
4344 void ust_app_unregister(int sock
)
4346 struct ust_app
*lta
;
4347 struct lttng_ht_node_ulong
*node
;
4348 struct lttng_ht_iter ust_app_sock_iter
;
4349 struct lttng_ht_iter iter
;
4350 struct ust_app_session
*ua_sess
;
4355 /* Get the node reference for a call_rcu */
4356 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &ust_app_sock_iter
);
4357 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4360 lta
= lttng::utils::container_of(node
, &ust_app::sock_n
);
4361 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4364 * For per-PID buffers, perform "push metadata" and flush all
4365 * application streams before removing app from hash tables,
4366 * ensuring proper behavior of data_pending check.
4367 * Remove sessions so they are not visible during deletion.
4369 cds_lfht_for_each_entry (lta
->sessions
->ht
, &iter
.iter
, ua_sess
, node
.node
) {
4370 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4372 /* The session was already removed so scheduled for teardown. */
4376 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4377 (void) ust_app_flush_app_session(lta
, ua_sess
);
4381 * Add session to list for teardown. This is safe since at this point we
4382 * are the only one using this list.
4384 pthread_mutex_lock(&ua_sess
->lock
);
4386 if (ua_sess
->deleted
) {
4387 pthread_mutex_unlock(&ua_sess
->lock
);
4392 * Normally, this is done in the delete session process which is
4393 * executed in the call rcu below. However, upon registration we can't
4394 * afford to wait for the grace period before pushing data or else the
4395 * data pending feature can race between the unregistration and stop
4396 * command where the data pending command is sent *before* the grace
4399 * The close metadata below nullifies the metadata pointer in the
4400 * session so the delete session will NOT push/close a second time.
4402 auto locked_registry
= get_locked_session_registry(ua_sess
);
4403 if (locked_registry
) {
4404 /* Push metadata for application before freeing the application. */
4405 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4408 * Don't ask to close metadata for global per UID buffers. Close
4409 * metadata only on destroy trace session in this case. Also, the
4410 * previous push metadata could have flag the metadata registry to
4411 * close so don't send a close command if closed.
4413 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4414 const auto metadata_key
= locked_registry
->_metadata_key
;
4415 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4417 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4418 locked_registry
->_metadata_closed
= true;
4421 /* Release lock before communication, see comments in
4422 * close_metadata(). */
4423 locked_registry
.reset();
4424 (void) close_metadata(
4425 metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4427 locked_registry
.reset();
4430 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4432 pthread_mutex_unlock(&ua_sess
->lock
);
4435 /* Remove application from PID hash table */
4436 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4440 * Remove application from notify hash table. The thread handling the
4441 * notify socket could have deleted the node so ignore on error because
4442 * either way it's valid. The close of that socket is handled by the
4443 * apps_notify_thread.
4445 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4446 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4449 * Ignore return value since the node might have been removed before by an
4450 * add replace during app registration because the PID can be reassigned by
4453 iter
.iter
.node
= <a
->pid_n
.node
;
4454 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4456 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", lta
->pid
);
4460 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4467 * Fill events array with all events name of all registered apps.
4469 int ust_app_list_events(struct lttng_event
**events
)
4472 size_t nbmem
, count
= 0;
4473 struct lttng_ht_iter iter
;
4474 struct ust_app
*app
;
4475 struct lttng_event
*tmp_event
;
4477 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4478 tmp_event
= calloc
<lttng_event
>(nbmem
);
4479 if (tmp_event
== nullptr) {
4480 PERROR("zmalloc ust app events");
4487 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4488 struct lttng_ust_abi_tracepoint_iter uiter
;
4490 health_code_update();
4492 if (!app
->compatible
) {
4494 * TODO: In time, we should notice the caller of this error by
4495 * telling him that this is a version error.
4499 pthread_mutex_lock(&app
->sock_lock
);
4500 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4502 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4503 ERR("UST app list events getting handle failed for app pid %d",
4506 pthread_mutex_unlock(&app
->sock_lock
);
4510 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
, &uiter
)) !=
4511 -LTTNG_UST_ERR_NOENT
) {
4512 /* Handle ustctl error. */
4516 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4517 ERR("UST app tp list get failed for app %d with ret %d",
4521 DBG3("UST app tp list get failed. Application is dead");
4525 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4526 if (release_ret
< 0 && release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4527 release_ret
!= -EPIPE
) {
4528 ERR("Error releasing app handle for app %d with ret %d",
4532 pthread_mutex_unlock(&app
->sock_lock
);
4536 health_code_update();
4537 if (count
>= nbmem
) {
4538 /* In case the realloc fails, we free the memory */
4539 struct lttng_event
*new_tmp_event
;
4542 new_nbmem
= nbmem
<< 1;
4543 DBG2("Reallocating event list from %zu to %zu entries",
4546 new_tmp_event
= (lttng_event
*) realloc(
4547 tmp_event
, new_nbmem
* sizeof(struct lttng_event
));
4548 if (new_tmp_event
== nullptr) {
4551 PERROR("realloc ust app events");
4555 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4556 if (release_ret
< 0 &&
4557 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4558 release_ret
!= -EPIPE
) {
4559 ERR("Error releasing app handle for app %d with ret %d",
4563 pthread_mutex_unlock(&app
->sock_lock
);
4566 /* Zero the new memory */
4567 memset(new_tmp_event
+ nbmem
,
4569 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4571 tmp_event
= new_tmp_event
;
4573 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4574 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4575 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4576 tmp_event
[count
].pid
= app
->pid
;
4577 tmp_event
[count
].enabled
= -1;
4580 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4581 pthread_mutex_unlock(&app
->sock_lock
);
4583 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4584 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4587 } else if (ret
== -EAGAIN
) {
4588 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4592 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4601 *events
= tmp_event
;
4603 DBG2("UST app list events done (%zu events)", count
);
4608 health_code_update();
4613 * Fill events array with all events name of all registered apps.
4615 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4618 size_t nbmem
, count
= 0;
4619 struct lttng_ht_iter iter
;
4620 struct ust_app
*app
;
4621 struct lttng_event_field
*tmp_event
;
4623 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4624 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4625 if (tmp_event
== nullptr) {
4626 PERROR("zmalloc ust app event fields");
4633 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4634 struct lttng_ust_abi_field_iter uiter
;
4636 health_code_update();
4638 if (!app
->compatible
) {
4640 * TODO: In time, we should notice the caller of this error by
4641 * telling him that this is a version error.
4645 pthread_mutex_lock(&app
->sock_lock
);
4646 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4648 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4649 ERR("UST app list field getting handle failed for app pid %d",
4652 pthread_mutex_unlock(&app
->sock_lock
);
4656 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
, &uiter
)) !=
4657 -LTTNG_UST_ERR_NOENT
) {
4658 /* Handle ustctl error. */
4662 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4663 ERR("UST app tp list field failed for app %d with ret %d",
4667 DBG3("UST app tp list field failed. Application is dead");
4671 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4672 pthread_mutex_unlock(&app
->sock_lock
);
4673 if (release_ret
< 0 && release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4674 release_ret
!= -EPIPE
) {
4675 ERR("Error releasing app handle for app %d with ret %d",
4682 health_code_update();
4683 if (count
>= nbmem
) {
4684 /* In case the realloc fails, we free the memory */
4685 struct lttng_event_field
*new_tmp_event
;
4688 new_nbmem
= nbmem
<< 1;
4689 DBG2("Reallocating event field list from %zu to %zu entries",
4692 new_tmp_event
= (lttng_event_field
*) realloc(
4693 tmp_event
, new_nbmem
* sizeof(struct lttng_event_field
));
4694 if (new_tmp_event
== nullptr) {
4697 PERROR("realloc ust app event fields");
4701 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4702 pthread_mutex_unlock(&app
->sock_lock
);
4703 if (release_ret
&& release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4704 release_ret
!= -EPIPE
) {
4705 ERR("Error releasing app handle for app %d with ret %d",
4711 /* Zero the new memory */
4712 memset(new_tmp_event
+ nbmem
,
4714 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4716 tmp_event
= new_tmp_event
;
4719 memcpy(tmp_event
[count
].field_name
,
4721 LTTNG_UST_ABI_SYM_NAME_LEN
);
4722 /* Mapping between these enums matches 1 to 1. */
4723 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4724 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4726 memcpy(tmp_event
[count
].event
.name
,
4728 LTTNG_UST_ABI_SYM_NAME_LEN
);
4729 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4730 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4731 tmp_event
[count
].event
.pid
= app
->pid
;
4732 tmp_event
[count
].event
.enabled
= -1;
4735 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4736 pthread_mutex_unlock(&app
->sock_lock
);
4737 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4738 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4743 *fields
= tmp_event
;
4745 DBG2("UST app list event fields done (%zu events)", count
);
4750 health_code_update();
4755 * Free and clean all traceable apps of the global list.
4757 void ust_app_clean_list()
4760 struct ust_app
*app
;
4761 struct lttng_ht_iter iter
;
4763 DBG2("UST app cleaning registered apps hash table");
4767 /* Cleanup notify socket hash table */
4768 if (ust_app_ht_by_notify_sock
) {
4769 cds_lfht_for_each_entry (
4770 ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
, notify_sock_n
.node
) {
4772 * Assert that all notifiers are gone as all triggers
4773 * are unregistered prior to this clean-up.
4775 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4777 ust_app_notify_sock_unregister(app
->notify_sock
);
4782 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4783 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4785 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4789 /* Cleanup socket hash table */
4790 if (ust_app_ht_by_sock
) {
4791 cds_lfht_for_each_entry (ust_app_ht_by_sock
->ht
, &iter
.iter
, app
, sock_n
.node
) {
4792 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4799 /* Destroy is done only when the ht is empty */
4801 lttng_ht_destroy(ust_app_ht
);
4803 if (ust_app_ht_by_sock
) {
4804 lttng_ht_destroy(ust_app_ht_by_sock
);
4806 if (ust_app_ht_by_notify_sock
) {
4807 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4812 * Init UST app hash table.
4814 int ust_app_ht_alloc()
4816 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4820 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4821 if (!ust_app_ht_by_sock
) {
4824 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4825 if (!ust_app_ht_by_notify_sock
) {
4832 * For a specific UST session, disable the channel for all registered apps.
4834 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4837 struct lttng_ht_iter iter
;
4838 struct lttng_ht_node_str
*ua_chan_node
;
4839 struct ust_app
*app
;
4840 struct ust_app_session
*ua_sess
;
4841 struct ust_app_channel
*ua_chan
;
4843 LTTNG_ASSERT(usess
->active
);
4844 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4850 /* For every registered applications */
4851 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4852 struct lttng_ht_iter uiter
;
4853 if (!app
->compatible
) {
4855 * TODO: In time, we should notice the caller of this error by
4856 * telling him that this is a version error.
4860 ua_sess
= lookup_session_by_app(usess
, app
);
4861 if (ua_sess
== nullptr) {
4866 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4867 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4868 /* If the session if found for the app, the channel must be there */
4869 LTTNG_ASSERT(ua_chan_node
);
4871 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4872 /* The channel must not be already disabled */
4873 LTTNG_ASSERT(ua_chan
->enabled
== 1);
4875 /* Disable channel onto application */
4876 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4878 /* XXX: We might want to report this error at some point... */
4888 * For a specific UST session, enable the channel for all registered apps.
4890 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4893 struct lttng_ht_iter iter
;
4894 struct ust_app
*app
;
4895 struct ust_app_session
*ua_sess
;
4897 LTTNG_ASSERT(usess
->active
);
4898 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4904 /* For every registered applications */
4905 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4906 if (!app
->compatible
) {
4908 * TODO: In time, we should notice the caller of this error by
4909 * telling him that this is a version error.
4913 ua_sess
= lookup_session_by_app(usess
, app
);
4914 if (ua_sess
== nullptr) {
4918 /* Enable channel onto application */
4919 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4921 /* XXX: We might want to report this error at some point... */
4931 * Disable an event in a channel and for a specific session.
4933 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4934 struct ltt_ust_channel
*uchan
,
4935 struct ltt_ust_event
*uevent
)
4938 struct lttng_ht_iter iter
, uiter
;
4939 struct lttng_ht_node_str
*ua_chan_node
;
4940 struct ust_app
*app
;
4941 struct ust_app_session
*ua_sess
;
4942 struct ust_app_channel
*ua_chan
;
4943 struct ust_app_event
*ua_event
;
4945 LTTNG_ASSERT(usess
->active
);
4946 DBG("UST app disabling event %s for all apps in channel "
4947 "%s for session id %" PRIu64
,
4954 /* For all registered applications */
4955 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4956 if (!app
->compatible
) {
4958 * TODO: In time, we should notice the caller of this error by
4959 * telling him that this is a version error.
4963 ua_sess
= lookup_session_by_app(usess
, app
);
4964 if (ua_sess
== nullptr) {
4969 /* Lookup channel in the ust app session */
4970 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4971 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4972 if (ua_chan_node
== nullptr) {
4973 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4980 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4982 ua_event
= find_ust_app_event(ua_chan
->events
,
4985 uevent
->attr
.loglevel
,
4987 if (ua_event
== nullptr) {
4988 DBG2("Event %s not found in channel %s for app pid %d."
4996 ret
= disable_ust_app_event(ua_event
, app
);
4998 /* XXX: Report error someday... */
5007 /* The ua_sess lock must be held by the caller. */
5008 static int ust_app_channel_create(struct ltt_ust_session
*usess
,
5009 struct ust_app_session
*ua_sess
,
5010 struct ltt_ust_channel
*uchan
,
5011 struct ust_app
*app
,
5012 struct ust_app_channel
**_ua_chan
)
5015 struct ust_app_channel
*ua_chan
= nullptr;
5017 LTTNG_ASSERT(ua_sess
);
5018 ASSERT_LOCKED(ua_sess
->lock
);
5020 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
))) {
5021 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
5024 struct ltt_ust_context
*uctx
= nullptr;
5027 * Create channel onto application and synchronize its
5030 ret
= ust_app_channel_allocate(
5031 ua_sess
, uchan
, LTTNG_UST_ABI_CHAN_PER_CPU
, usess
, &ua_chan
);
5036 ret
= ust_app_channel_send(app
, usess
, ua_sess
, ua_chan
);
5042 cds_list_for_each_entry (uctx
, &uchan
->ctx_list
, list
) {
5043 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5055 * The application's socket is not valid. Either a bad socket
5056 * or a timeout on it. We can't inform the caller that for a
5057 * specific app, the session failed so lets continue here.
5059 ret
= 0; /* Not an error. */
5067 if (ret
== 0 && _ua_chan
) {
5069 * Only return the application's channel on success. Note
5070 * that the channel can still be part of the application's
5071 * channel hashtable on error.
5073 *_ua_chan
= ua_chan
;
5079 * Enable event for a specific session and channel on the tracer.
5081 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
5082 struct ltt_ust_channel
*uchan
,
5083 struct ltt_ust_event
*uevent
)
5086 struct lttng_ht_iter iter
, uiter
;
5087 struct lttng_ht_node_str
*ua_chan_node
;
5088 struct ust_app
*app
;
5089 struct ust_app_session
*ua_sess
;
5090 struct ust_app_channel
*ua_chan
;
5091 struct ust_app_event
*ua_event
;
5093 LTTNG_ASSERT(usess
->active
);
5094 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
5099 * NOTE: At this point, this function is called only if the session and
5100 * channel passed are already created for all apps. and enabled on the
5106 /* For all registered applications */
5107 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5108 if (!app
->compatible
) {
5110 * TODO: In time, we should notice the caller of this error by
5111 * telling him that this is a version error.
5115 ua_sess
= lookup_session_by_app(usess
, app
);
5117 /* The application has problem or is probably dead. */
5121 pthread_mutex_lock(&ua_sess
->lock
);
5123 if (ua_sess
->deleted
) {
5124 pthread_mutex_unlock(&ua_sess
->lock
);
5128 /* Lookup channel in the ust app session */
5129 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5130 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5132 * It is possible that the channel cannot be found is
5133 * the channel/event creation occurs concurrently with
5134 * an application exit.
5136 if (!ua_chan_node
) {
5137 pthread_mutex_unlock(&ua_sess
->lock
);
5141 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5143 /* Get event node */
5144 ua_event
= find_ust_app_event(ua_chan
->events
,
5147 uevent
->attr
.loglevel
,
5149 if (ua_event
== nullptr) {
5150 DBG3("UST app enable event %s not found for app PID %d."
5157 ret
= enable_ust_app_event(ua_event
, app
);
5159 pthread_mutex_unlock(&ua_sess
->lock
);
5163 pthread_mutex_unlock(&ua_sess
->lock
);
5172 * For a specific existing UST session and UST channel, creates the event for
5173 * all registered apps.
5175 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5176 struct ltt_ust_channel
*uchan
,
5177 struct ltt_ust_event
*uevent
)
5180 struct lttng_ht_iter iter
, uiter
;
5181 struct lttng_ht_node_str
*ua_chan_node
;
5182 struct ust_app
*app
;
5183 struct ust_app_session
*ua_sess
;
5184 struct ust_app_channel
*ua_chan
;
5186 LTTNG_ASSERT(usess
->active
);
5187 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5193 /* For all registered applications */
5194 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5195 if (!app
->compatible
) {
5197 * TODO: In time, we should notice the caller of this error by
5198 * telling him that this is a version error.
5202 ua_sess
= lookup_session_by_app(usess
, app
);
5204 /* The application has problem or is probably dead. */
5208 pthread_mutex_lock(&ua_sess
->lock
);
5210 if (ua_sess
->deleted
) {
5211 pthread_mutex_unlock(&ua_sess
->lock
);
5215 /* Lookup channel in the ust app session */
5216 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5217 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5218 /* If the channel is not found, there is a code flow error */
5219 LTTNG_ASSERT(ua_chan_node
);
5221 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5223 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5224 pthread_mutex_unlock(&ua_sess
->lock
);
5226 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5227 /* Possible value at this point: -ENOMEM. If so, we stop! */
5230 DBG2("UST app event %s already exist on app PID %d",
5242 * Start tracing for a specific UST session and app.
5244 * Called with UST app session lock held.
5247 static int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5250 struct ust_app_session
*ua_sess
;
5252 DBG("Starting tracing for ust app pid %d", app
->pid
);
5256 if (!app
->compatible
) {
5260 ua_sess
= lookup_session_by_app(usess
, app
);
5261 if (ua_sess
== nullptr) {
5262 /* The session is in teardown process. Ignore and continue. */
5266 pthread_mutex_lock(&ua_sess
->lock
);
5268 if (ua_sess
->deleted
) {
5269 pthread_mutex_unlock(&ua_sess
->lock
);
5273 if (ua_sess
->enabled
) {
5274 pthread_mutex_unlock(&ua_sess
->lock
);
5278 /* Upon restart, we skip the setup, already done */
5279 if (ua_sess
->started
) {
5283 health_code_update();
5286 /* This starts the UST tracing */
5287 pthread_mutex_lock(&app
->sock_lock
);
5288 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5289 pthread_mutex_unlock(&app
->sock_lock
);
5291 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5292 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5295 pthread_mutex_unlock(&ua_sess
->lock
);
5297 } else if (ret
== -EAGAIN
) {
5298 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5301 pthread_mutex_unlock(&ua_sess
->lock
);
5305 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5313 /* Indicate that the session has been started once */
5314 ua_sess
->started
= 1;
5315 ua_sess
->enabled
= 1;
5317 pthread_mutex_unlock(&ua_sess
->lock
);
5319 health_code_update();
5321 /* Quiescent wait after starting trace */
5322 pthread_mutex_lock(&app
->sock_lock
);
5323 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5324 pthread_mutex_unlock(&app
->sock_lock
);
5326 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5327 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5330 } else if (ret
== -EAGAIN
) {
5331 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5335 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5344 health_code_update();
5348 pthread_mutex_unlock(&ua_sess
->lock
);
5350 health_code_update();
5355 * Stop tracing for a specific UST session and app.
5357 static int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5360 struct ust_app_session
*ua_sess
;
5362 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5366 if (!app
->compatible
) {
5367 goto end_no_session
;
5370 ua_sess
= lookup_session_by_app(usess
, app
);
5371 if (ua_sess
== nullptr) {
5372 goto end_no_session
;
5375 pthread_mutex_lock(&ua_sess
->lock
);
5377 if (ua_sess
->deleted
) {
5378 pthread_mutex_unlock(&ua_sess
->lock
);
5379 goto end_no_session
;
5383 * If started = 0, it means that stop trace has been called for a session
5384 * that was never started. It's possible since we can have a fail start
5385 * from either the application manager thread or the command thread. Simply
5386 * indicate that this is a stop error.
5388 if (!ua_sess
->started
) {
5389 goto error_rcu_unlock
;
5392 health_code_update();
5394 /* This inhibits UST tracing */
5395 pthread_mutex_lock(&app
->sock_lock
);
5396 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5397 pthread_mutex_unlock(&app
->sock_lock
);
5399 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5400 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5404 } else if (ret
== -EAGAIN
) {
5405 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5411 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5416 goto error_rcu_unlock
;
5419 health_code_update();
5420 ua_sess
->enabled
= 0;
5422 /* Quiescent wait after stopping trace */
5423 pthread_mutex_lock(&app
->sock_lock
);
5424 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5425 pthread_mutex_unlock(&app
->sock_lock
);
5427 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5428 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5431 } else if (ret
== -EAGAIN
) {
5432 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5436 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5443 health_code_update();
5446 auto locked_registry
= get_locked_session_registry(ua_sess
);
5448 /* The UST app session is held registry shall not be null. */
5449 LTTNG_ASSERT(locked_registry
);
5451 /* Push metadata for application before freeing the application. */
5452 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5456 pthread_mutex_unlock(&ua_sess
->lock
);
5459 health_code_update();
5463 pthread_mutex_unlock(&ua_sess
->lock
);
5465 health_code_update();
5469 static int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5471 int ret
, retval
= 0;
5472 struct lttng_ht_iter iter
;
5473 struct ust_app_channel
*ua_chan
;
5474 struct consumer_socket
*socket
;
5476 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5480 if (!app
->compatible
) {
5481 goto end_not_compatible
;
5484 pthread_mutex_lock(&ua_sess
->lock
);
5486 if (ua_sess
->deleted
) {
5490 health_code_update();
5492 /* Flushing buffers */
5493 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5495 /* Flush buffers and push metadata. */
5496 switch (ua_sess
->buffer_type
) {
5497 case LTTNG_BUFFER_PER_PID
:
5498 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5499 health_code_update();
5500 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5502 ERR("Error flushing consumer channel");
5508 case LTTNG_BUFFER_PER_UID
:
5514 health_code_update();
5517 pthread_mutex_unlock(&ua_sess
->lock
);
5521 health_code_update();
5526 * Flush buffers for all applications for a specific UST session.
5527 * Called with UST session lock held.
5529 static int ust_app_flush_session(struct ltt_ust_session
*usess
)
5534 DBG("Flushing session buffers for all ust apps");
5538 /* Flush buffers and push metadata. */
5539 switch (usess
->buffer_type
) {
5540 case LTTNG_BUFFER_PER_UID
:
5542 struct buffer_reg_uid
*reg
;
5543 struct lttng_ht_iter iter
;
5545 /* Flush all per UID buffers associated to that session. */
5546 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5547 lsu::registry_session
*ust_session_reg
;
5548 struct buffer_reg_channel
*buf_reg_chan
;
5549 struct consumer_socket
*socket
;
5551 /* Get consumer socket to use to push the metadata.*/
5552 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5555 /* Ignore request if no consumer is found for the session. */
5559 cds_lfht_for_each_entry (
5560 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5562 * The following call will print error values so the return
5563 * code is of little importance because whatever happens, we
5564 * have to try them all.
5566 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5569 ust_session_reg
= reg
->registry
->reg
.ust
;
5570 /* Push metadata. */
5571 auto locked_registry
= ust_session_reg
->lock();
5572 (void) push_metadata(locked_registry
, usess
->consumer
);
5576 case LTTNG_BUFFER_PER_PID
:
5578 struct ust_app_session
*ua_sess
;
5579 struct lttng_ht_iter iter
;
5580 struct ust_app
*app
;
5582 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5583 ua_sess
= lookup_session_by_app(usess
, app
);
5584 if (ua_sess
== nullptr) {
5587 (void) ust_app_flush_app_session(app
, ua_sess
);
5598 health_code_update();
5602 static int ust_app_clear_quiescent_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5605 struct lttng_ht_iter iter
;
5606 struct ust_app_channel
*ua_chan
;
5607 struct consumer_socket
*socket
;
5609 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5613 if (!app
->compatible
) {
5614 goto end_not_compatible
;
5617 pthread_mutex_lock(&ua_sess
->lock
);
5619 if (ua_sess
->deleted
) {
5623 health_code_update();
5625 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5627 ERR("Failed to find consumer (%" PRIu32
") socket", app
->abi
.bits_per_long
);
5632 /* Clear quiescent state. */
5633 switch (ua_sess
->buffer_type
) {
5634 case LTTNG_BUFFER_PER_PID
:
5635 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5636 health_code_update();
5637 ret
= consumer_clear_quiescent_channel(socket
, ua_chan
->key
);
5639 ERR("Error clearing quiescent state for consumer channel");
5645 case LTTNG_BUFFER_PER_UID
:
5652 health_code_update();
5655 pthread_mutex_unlock(&ua_sess
->lock
);
5659 health_code_update();
5664 * Clear quiescent state in each stream for all applications for a
5665 * specific UST session.
5666 * Called with UST session lock held.
5668 static int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5673 DBG("Clearing stream quiescent state for all ust apps");
5677 switch (usess
->buffer_type
) {
5678 case LTTNG_BUFFER_PER_UID
:
5680 struct lttng_ht_iter iter
;
5681 struct buffer_reg_uid
*reg
;
5684 * Clear quiescent for all per UID buffers associated to
5687 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5688 struct consumer_socket
*socket
;
5689 struct buffer_reg_channel
*buf_reg_chan
;
5691 /* Get associated consumer socket.*/
5692 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5696 * Ignore request if no consumer is found for
5702 cds_lfht_for_each_entry (
5703 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5705 * The following call will print error values so
5706 * the return code is of little importance
5707 * because whatever happens, we have to try them
5710 (void) consumer_clear_quiescent_channel(socket
,
5711 buf_reg_chan
->consumer_key
);
5716 case LTTNG_BUFFER_PER_PID
:
5718 struct ust_app_session
*ua_sess
;
5719 struct lttng_ht_iter iter
;
5720 struct ust_app
*app
;
5722 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5723 ua_sess
= lookup_session_by_app(usess
, app
);
5724 if (ua_sess
== nullptr) {
5727 (void) ust_app_clear_quiescent_app_session(app
, ua_sess
);
5738 health_code_update();
5743 * Destroy a specific UST session in apps.
5745 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5748 struct ust_app_session
*ua_sess
;
5749 struct lttng_ht_iter iter
;
5750 struct lttng_ht_node_u64
*node
;
5752 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5756 if (!app
->compatible
) {
5760 __lookup_session_by_app(usess
, app
, &iter
);
5761 node
= lttng_ht_iter_get_node_u64(&iter
);
5762 if (node
== nullptr) {
5763 /* Session is being or is deleted. */
5766 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5768 health_code_update();
5769 destroy_app_session(app
, ua_sess
);
5771 health_code_update();
5773 /* Quiescent wait after stopping trace */
5774 pthread_mutex_lock(&app
->sock_lock
);
5775 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5776 pthread_mutex_unlock(&app
->sock_lock
);
5778 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5779 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5782 } else if (ret
== -EAGAIN
) {
5783 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5787 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5795 health_code_update();
5800 * Start tracing for the UST session.
5802 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5804 struct lttng_ht_iter iter
;
5805 struct ust_app
*app
;
5807 DBG("Starting all UST traces");
5810 * Even though the start trace might fail, flag this session active so
5811 * other application coming in are started by default.
5818 * In a start-stop-start use-case, we need to clear the quiescent state
5819 * of each channel set by the prior stop command, thus ensuring that a
5820 * following stop or destroy is sure to grab a timestamp_end near those
5821 * operations, even if the packet is empty.
5823 (void) ust_app_clear_quiescent_session(usess
);
5825 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5826 ust_app_global_update(usess
, app
);
5835 * Start tracing for the UST session.
5836 * Called with UST session lock held.
5838 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5841 struct lttng_ht_iter iter
;
5842 struct ust_app
*app
;
5844 DBG("Stopping all UST traces");
5847 * Even though the stop trace might fail, flag this session inactive so
5848 * other application coming in are not started by default.
5854 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5855 ret
= ust_app_stop_trace(usess
, app
);
5857 /* Continue to next apps even on error */
5862 (void) ust_app_flush_session(usess
);
5870 * Destroy app UST session.
5872 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5875 struct lttng_ht_iter iter
;
5876 struct ust_app
*app
;
5878 DBG("Destroy all UST traces");
5882 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5883 ret
= destroy_trace(usess
, app
);
5885 /* Continue to next apps even on error */
5895 /* The ua_sess lock must be held by the caller. */
5896 static int find_or_create_ust_app_channel(struct ltt_ust_session
*usess
,
5897 struct ust_app_session
*ua_sess
,
5898 struct ust_app
*app
,
5899 struct ltt_ust_channel
*uchan
,
5900 struct ust_app_channel
**ua_chan
)
5903 struct lttng_ht_iter iter
;
5904 struct lttng_ht_node_str
*ua_chan_node
;
5906 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5907 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5909 *ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5913 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5921 static int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5922 struct ltt_ust_event
*uevent
,
5923 struct ust_app
*app
)
5926 struct ust_app_event
*ua_event
= nullptr;
5928 ua_event
= find_ust_app_event(ua_chan
->events
,
5931 uevent
->attr
.loglevel
,
5934 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5939 if (ua_event
->enabled
!= uevent
->enabled
) {
5940 ret
= uevent
->enabled
? enable_ust_app_event(ua_event
, app
) :
5941 disable_ust_app_event(ua_event
, app
);
5949 /* Called with RCU read-side lock held. */
5950 static void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5953 enum lttng_error_code ret_code
;
5954 enum lttng_trigger_status t_status
;
5955 struct lttng_ht_iter app_trigger_iter
;
5956 struct lttng_triggers
*triggers
= nullptr;
5957 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5958 unsigned int count
, i
;
5960 ASSERT_RCU_READ_LOCKED();
5962 if (!ust_app_supports_notifiers(app
)) {
5967 * Currrently, registering or unregistering a trigger with an
5968 * event rule condition causes a full synchronization of the event
5971 * The first step attempts to add an event notifier for all registered
5972 * triggers that apply to the user space tracers. Then, the
5973 * application's event notifiers rules are all checked against the list
5974 * of registered triggers. Any event notifier that doesn't have a
5975 * matching trigger can be assumed to have been disabled.
5977 * All of this is inefficient, but is put in place to get the feature
5978 * rolling as it is simpler at this moment. It will be optimized Soon™
5979 * to allow the state of enabled
5980 * event notifiers to be synchronized in a piece-wise way.
5983 /* Get all triggers using uid 0 (root) */
5984 ret_code
= notification_thread_command_list_triggers(
5985 the_notification_thread_handle
, 0, &triggers
);
5986 if (ret_code
!= LTTNG_OK
) {
5990 LTTNG_ASSERT(triggers
);
5992 t_status
= lttng_triggers_get_count(triggers
, &count
);
5993 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5997 for (i
= 0; i
< count
; i
++) {
5998 struct lttng_condition
*condition
;
5999 struct lttng_event_rule
*event_rule
;
6000 struct lttng_trigger
*trigger
;
6001 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
6002 enum lttng_condition_status condition_status
;
6005 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
6006 LTTNG_ASSERT(trigger
);
6008 token
= lttng_trigger_get_tracer_token(trigger
);
6009 condition
= lttng_trigger_get_condition(trigger
);
6011 if (lttng_condition_get_type(condition
) !=
6012 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
6013 /* Does not apply */
6017 condition_status
= lttng_condition_event_rule_matches_borrow_rule_mutable(
6018 condition
, &event_rule
);
6019 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
6021 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
6022 /* Skip kernel related triggers. */
6027 * Find or create the associated token event rule. The caller
6028 * holds the RCU read lock, so this is safe to call without
6029 * explicitly acquiring it here.
6031 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
6032 app
->token_to_event_notifier_rule_ht
, token
);
6033 if (!looked_up_event_notifier_rule
) {
6034 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
6042 /* Remove all unknown event sources from the app. */
6043 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
6044 &app_trigger_iter
.iter
,
6045 event_notifier_rule
,
6047 const uint64_t app_token
= event_notifier_rule
->token
;
6051 * Check if the app event trigger still exists on the
6052 * notification side.
6054 for (i
= 0; i
< count
; i
++) {
6055 uint64_t notification_thread_token
;
6056 const struct lttng_trigger
*trigger
=
6057 lttng_triggers_get_at_index(triggers
, i
);
6059 LTTNG_ASSERT(trigger
);
6061 notification_thread_token
= lttng_trigger_get_tracer_token(trigger
);
6063 if (notification_thread_token
== app_token
) {
6075 * This trigger was unregistered, disable it on the tracer's
6078 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &app_trigger_iter
);
6079 LTTNG_ASSERT(ret
== 0);
6081 /* Callee logs errors. */
6082 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
6084 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
6090 lttng_triggers_destroy(triggers
);
6095 * RCU read lock must be held by the caller.
6097 static void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
6098 struct ust_app_session
*ua_sess
,
6099 struct ust_app
*app
)
6102 struct cds_lfht_iter uchan_iter
;
6103 struct ltt_ust_channel
*uchan
;
6105 LTTNG_ASSERT(usess
);
6106 LTTNG_ASSERT(ua_sess
);
6108 ASSERT_RCU_READ_LOCKED();
6110 cds_lfht_for_each_entry (usess
->domain_global
.channels
->ht
, &uchan_iter
, uchan
, node
.node
) {
6111 struct ust_app_channel
*ua_chan
;
6112 struct cds_lfht_iter uevent_iter
;
6113 struct ltt_ust_event
*uevent
;
6116 * Search for a matching ust_app_channel. If none is found,
6117 * create it. Creating the channel will cause the ua_chan
6118 * structure to be allocated, the channel buffers to be
6119 * allocated (if necessary) and sent to the application, and
6120 * all enabled contexts will be added to the channel.
6122 ret
= find_or_create_ust_app_channel(usess
, ua_sess
, app
, uchan
, &ua_chan
);
6124 /* Tracer is probably gone or ENOMEM. */
6129 /* ua_chan will be NULL for the metadata channel */
6133 cds_lfht_for_each_entry (uchan
->events
->ht
, &uevent_iter
, uevent
, node
.node
) {
6134 ret
= ust_app_channel_synchronize_event(ua_chan
, uevent
, app
);
6140 if (ua_chan
->enabled
!= uchan
->enabled
) {
6141 ret
= uchan
->enabled
? enable_ust_app_channel(ua_sess
, uchan
, app
) :
6142 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6153 * The caller must ensure that the application is compatible and is tracked
6154 * by the process attribute trackers.
6156 static void ust_app_synchronize(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6159 struct ust_app_session
*ua_sess
= nullptr;
6162 * The application's configuration should only be synchronized for
6165 LTTNG_ASSERT(usess
->active
);
6167 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, nullptr);
6169 /* Tracer is probably gone or ENOMEM. */
6171 destroy_app_session(app
, ua_sess
);
6175 LTTNG_ASSERT(ua_sess
);
6177 pthread_mutex_lock(&ua_sess
->lock
);
6178 if (ua_sess
->deleted
) {
6179 goto deleted_session
;
6184 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6187 * Create the metadata for the application. This returns gracefully if a
6188 * metadata was already set for the session.
6190 * The metadata channel must be created after the data channels as the
6191 * consumer daemon assumes this ordering. When interacting with a relay
6192 * daemon, the consumer will use this assumption to send the
6193 * "STREAMS_SENT" message to the relay daemon.
6195 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6197 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6205 pthread_mutex_unlock(&ua_sess
->lock
);
6210 static void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6212 struct ust_app_session
*ua_sess
;
6214 ua_sess
= lookup_session_by_app(usess
, app
);
6215 if (ua_sess
== nullptr) {
6218 destroy_app_session(app
, ua_sess
);
6222 * Add channels/events from UST global domain to registered apps at sock.
6224 * Called with session lock held.
6225 * Called with RCU read-side lock held.
6227 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6229 LTTNG_ASSERT(usess
);
6230 LTTNG_ASSERT(usess
->active
);
6231 ASSERT_RCU_READ_LOCKED();
6233 DBG2("UST app global update for app sock %d for session id %" PRIu64
, app
->sock
, usess
->id
);
6235 if (!app
->compatible
) {
6238 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, usess
, app
->pid
) &&
6239 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
, usess
, app
->uid
) &&
6240 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
, usess
, app
->gid
)) {
6242 * Synchronize the application's internal tracing configuration
6243 * and start tracing.
6245 ust_app_synchronize(usess
, app
);
6246 ust_app_start_trace(usess
, app
);
6248 ust_app_global_destroy(usess
, app
);
6253 * Add all event notifiers to an application.
6255 * Called with session lock held.
6256 * Called with RCU read-side lock held.
6258 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6260 ASSERT_RCU_READ_LOCKED();
6262 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6266 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6270 if (app
->event_notifier_group
.object
== nullptr) {
6271 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6277 ust_app_synchronize_event_notifier_rules(app
);
6281 * Called with session lock held.
6283 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6285 struct lttng_ht_iter iter
;
6286 struct ust_app
*app
;
6289 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6290 ust_app_global_update(usess
, app
);
6295 void ust_app_global_update_all_event_notifier_rules()
6297 struct lttng_ht_iter iter
;
6298 struct ust_app
*app
;
6301 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6302 ust_app_global_update_event_notifier_rules(app
);
6309 * Add context to a specific channel for global UST domain.
6311 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6312 struct ltt_ust_channel
*uchan
,
6313 struct ltt_ust_context
*uctx
)
6316 struct lttng_ht_node_str
*ua_chan_node
;
6317 struct lttng_ht_iter iter
, uiter
;
6318 struct ust_app_channel
*ua_chan
= nullptr;
6319 struct ust_app_session
*ua_sess
;
6320 struct ust_app
*app
;
6322 LTTNG_ASSERT(usess
->active
);
6325 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6326 if (!app
->compatible
) {
6328 * TODO: In time, we should notice the caller of this error by
6329 * telling him that this is a version error.
6333 ua_sess
= lookup_session_by_app(usess
, app
);
6334 if (ua_sess
== nullptr) {
6338 pthread_mutex_lock(&ua_sess
->lock
);
6340 if (ua_sess
->deleted
) {
6341 pthread_mutex_unlock(&ua_sess
->lock
);
6345 /* Lookup channel in the ust app session */
6346 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6347 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6348 if (ua_chan_node
== nullptr) {
6351 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6352 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6357 pthread_mutex_unlock(&ua_sess
->lock
);
6365 * Receive registration and populate the given msg structure.
6367 * On success return 0 else a negative value returned by the ustctl call.
6369 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6372 uint32_t pid
, ppid
, uid
, gid
;
6376 ret
= lttng_ust_ctl_recv_reg_msg(sock
,
6384 &msg
->bits_per_long
,
6385 &msg
->uint8_t_alignment
,
6386 &msg
->uint16_t_alignment
,
6387 &msg
->uint32_t_alignment
,
6388 &msg
->uint64_t_alignment
,
6389 &msg
->long_alignment
,
6396 case LTTNG_UST_ERR_EXITING
:
6397 DBG3("UST app recv reg message failed. Application died");
6399 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6400 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6403 LTTNG_UST_ABI_MAJOR_VERSION
,
6404 LTTNG_UST_ABI_MINOR_VERSION
);
6407 ERR("UST app recv reg message failed with ret %d", ret
);
6412 msg
->pid
= (pid_t
) pid
;
6413 msg
->ppid
= (pid_t
) ppid
;
6414 msg
->uid
= (uid_t
) uid
;
6415 msg
->gid
= (gid_t
) gid
;
6422 * Return a ust app session object using the application object and the
6423 * session object descriptor has a key. If not found, NULL is returned.
6424 * A RCU read side lock MUST be acquired when calling this function.
6426 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
, int objd
)
6428 struct lttng_ht_node_ulong
*node
;
6429 struct lttng_ht_iter iter
;
6430 struct ust_app_session
*ua_sess
= nullptr;
6433 ASSERT_RCU_READ_LOCKED();
6435 lttng_ht_lookup(app
->ust_sessions_objd
, (void *) ((unsigned long) objd
), &iter
);
6436 node
= lttng_ht_iter_get_node_ulong(&iter
);
6437 if (node
== nullptr) {
6438 DBG2("UST app session find by objd %d not found", objd
);
6442 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6449 * Return a ust app channel object using the application object and the channel
6450 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6451 * lock MUST be acquired before calling this function.
6453 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
, int objd
)
6455 struct lttng_ht_node_ulong
*node
;
6456 struct lttng_ht_iter iter
;
6457 struct ust_app_channel
*ua_chan
= nullptr;
6460 ASSERT_RCU_READ_LOCKED();
6462 lttng_ht_lookup(app
->ust_objd
, (void *) ((unsigned long) objd
), &iter
);
6463 node
= lttng_ht_iter_get_node_ulong(&iter
);
6464 if (node
== nullptr) {
6465 DBG2("UST app channel find by objd %d not found", objd
);
6469 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6476 * Reply to a register channel notification from an application on the notify
6477 * socket. The channel metadata is also created.
6479 * The session UST registry lock is acquired in this function.
6481 * On success 0 is returned else a negative value.
6483 static int handle_app_register_channel_notification(int sock
,
6485 struct lttng_ust_ctl_field
*raw_context_fields
,
6486 size_t context_field_count
)
6488 int ret
, ret_code
= 0;
6490 uint64_t chan_reg_key
;
6491 struct ust_app
*app
;
6492 struct ust_app_channel
*ua_chan
;
6493 struct ust_app_session
*ua_sess
;
6494 auto ust_ctl_context_fields
=
6495 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_context_fields
);
6497 lttng::urcu::read_lock_guard read_lock_guard
;
6499 /* Lookup application. If not found, there is a code flow error. */
6500 app
= find_app_by_notify_sock(sock
);
6502 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6506 /* Lookup channel by UST object descriptor. */
6507 ua_chan
= find_channel_by_objd(app
, cobjd
);
6509 DBG("Application channel is being torn down. Abort event notify");
6513 LTTNG_ASSERT(ua_chan
->session
);
6514 ua_sess
= ua_chan
->session
;
6516 /* Get right session registry depending on the session buffer type. */
6517 auto locked_registry_session
= get_locked_session_registry(ua_sess
);
6518 if (!locked_registry_session
) {
6519 DBG("Application session is being torn down. Abort event notify");
6523 /* Depending on the buffer type, a different channel key is used. */
6524 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6525 chan_reg_key
= ua_chan
->tracing_channel_id
;
6527 chan_reg_key
= ua_chan
->key
;
6530 auto& ust_reg_chan
= locked_registry_session
->channel(chan_reg_key
);
6532 /* Channel id is set during the object creation. */
6533 chan_id
= ust_reg_chan
.id
;
6536 * The application returns the typing information of the channel's
6537 * context fields. In per-PID buffering mode, this is the first and only
6538 * time we get this information. It is our chance to finalize the
6539 * initialiation of the channel and serialize it's layout's description
6540 * to the trace's metadata.
6542 * However, in per-UID buffering mode, every application will provide
6543 * this information (redundantly). The first time will allow us to
6544 * complete the initialization. The following times, we simply validate
6545 * that all apps provide the same typing for the context fields as a
6549 auto app_context_fields
= lsu::create_trace_fields_from_ust_ctl_fields(
6550 *locked_registry_session
,
6551 ust_ctl_context_fields
.get(),
6552 context_field_count
,
6553 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT
,
6554 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
);
6556 if (!ust_reg_chan
.is_registered()) {
6557 lst::type::cuptr event_context
= app_context_fields
.size() ?
6558 lttng::make_unique
<lst::structure_type
>(
6559 0, std::move(app_context_fields
)) :
6562 ust_reg_chan
.event_context(std::move(event_context
));
6565 * Validate that the context fields match between
6566 * registry and newcoming application.
6568 bool context_fields_match
;
6569 const auto *previous_event_context
= ust_reg_chan
.event_context();
6571 if (!previous_event_context
) {
6572 context_fields_match
= app_context_fields
.size() == 0;
6574 const lst::structure_type
app_event_context_struct(
6575 0, std::move(app_context_fields
));
6577 context_fields_match
= *previous_event_context
==
6578 app_event_context_struct
;
6581 if (!context_fields_match
) {
6582 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6589 } catch (std::exception
& ex
) {
6590 ERR("Failed to handle application context: %s", ex
.what());
6596 DBG3("UST app replying to register channel key %" PRIu64
" with id %u, ret = %d",
6601 ret
= lttng_ust_ctl_reply_register_channel(
6604 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6605 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6606 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6609 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6610 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6613 } else if (ret
== -EAGAIN
) {
6614 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6618 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6627 /* This channel registry's registration is completed. */
6628 ust_reg_chan
.set_as_registered();
6634 * Add event to the UST channel registry. When the event is added to the
6635 * registry, the metadata is also created. Once done, this replies to the
6636 * application with the appropriate error code.
6638 * The session UST registry lock is acquired in the function.
6640 * On success 0 is returned else a negative value.
6642 static int add_event_ust_registry(int sock
,
6646 char *raw_signature
,
6648 struct lttng_ust_ctl_field
*raw_fields
,
6650 char *raw_model_emf_uri
)
6653 uint32_t event_id
= 0;
6654 uint64_t chan_reg_key
;
6655 struct ust_app
*app
;
6656 struct ust_app_channel
*ua_chan
;
6657 struct ust_app_session
*ua_sess
;
6658 lttng::urcu::read_lock_guard rcu_lock
;
6659 auto signature
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_signature
);
6660 auto fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_fields
);
6661 auto model_emf_uri
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_model_emf_uri
);
6663 /* Lookup application. If not found, there is a code flow error. */
6664 app
= find_app_by_notify_sock(sock
);
6666 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6670 /* Lookup channel by UST object descriptor. */
6671 ua_chan
= find_channel_by_objd(app
, cobjd
);
6673 DBG("Application channel is being torn down. Abort event notify");
6677 LTTNG_ASSERT(ua_chan
->session
);
6678 ua_sess
= ua_chan
->session
;
6680 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6681 chan_reg_key
= ua_chan
->tracing_channel_id
;
6683 chan_reg_key
= ua_chan
->key
;
6687 auto locked_registry
= get_locked_session_registry(ua_sess
);
6688 if (locked_registry
) {
6690 * From this point on, this call acquires the ownership of the signature,
6691 * fields and model_emf_uri meaning any free are done inside it if needed.
6692 * These three variables MUST NOT be read/write after this.
6695 auto& channel
= locked_registry
->channel(chan_reg_key
);
6697 /* event_id is set on success. */
6703 lsu::create_trace_fields_from_ust_ctl_fields(
6707 lst::field_location::root::EVENT_RECORD_PAYLOAD
,
6708 lsu::ctl_field_quirks::
6709 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
),
6711 model_emf_uri
.get() ?
6712 nonstd::optional
<std::string
>(model_emf_uri
.get()) :
6714 ua_sess
->buffer_type
,
6718 } catch (const std::exception
& ex
) {
6719 ERR("Failed to add event `%s` to registry session: %s",
6722 /* Inform the application of the error; don't return directly. */
6726 DBG("Application session is being torn down. Abort event notify");
6732 * The return value is returned to ustctl so in case of an error, the
6733 * application can be notified. In case of an error, it's important not to
6734 * return a negative error or else the application will get closed.
6736 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6738 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6739 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6742 } else if (ret
== -EAGAIN
) {
6743 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6747 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6753 * No need to wipe the create event since the application socket will
6754 * get close on error hence cleaning up everything by itself.
6759 DBG3("UST registry event %s with id %" PRId32
" added successfully", name
, event_id
);
6764 * Add enum to the UST session registry. Once done, this replies to the
6765 * application with the appropriate error code.
6767 * The session UST registry lock is acquired within this function.
6769 * On success 0 is returned else a negative value.
6771 static int add_enum_ust_registry(int sock
,
6774 struct lttng_ust_ctl_enum_entry
*raw_entries
,
6778 struct ust_app
*app
;
6779 struct ust_app_session
*ua_sess
;
6780 uint64_t enum_id
= -1ULL;
6781 lttng::urcu::read_lock_guard read_lock_guard
;
6782 auto entries
= lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::free
>(
6785 /* Lookup application. If not found, there is a code flow error. */
6786 app
= find_app_by_notify_sock(sock
);
6788 /* Return an error since this is not an error */
6789 DBG("Application socket %d is being torn down. Aborting enum registration", sock
);
6793 /* Lookup session by UST object descriptor. */
6794 ua_sess
= find_session_by_objd(app
, sobjd
);
6796 /* Return an error since this is not an error */
6797 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6801 auto locked_registry
= get_locked_session_registry(ua_sess
);
6802 if (!locked_registry
) {
6803 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6808 * From this point on, the callee acquires the ownership of
6809 * entries. The variable entries MUST NOT be read/written after
6812 int application_reply_code
;
6814 locked_registry
->create_or_find_enum(
6815 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6816 application_reply_code
= 0;
6817 } catch (const std::exception
& ex
) {
6820 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6825 application_reply_code
= -1;
6829 * The return value is returned to ustctl so in case of an error, the
6830 * application can be notified. In case of an error, it's important not to
6831 * return a negative error or else the application will get closed.
6833 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6835 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6836 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6839 } else if (ret
== -EAGAIN
) {
6840 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6844 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6850 * No need to wipe the create enum since the application socket will
6851 * get close on error hence cleaning up everything by itself.
6856 DBG3("UST registry enum %s added successfully or already found", name
);
6861 * Handle application notification through the given notify socket.
6863 * Return 0 on success or else a negative value.
6865 int ust_app_recv_notify(int sock
)
6868 enum lttng_ust_ctl_notify_cmd cmd
;
6870 DBG3("UST app receiving notify from sock %d", sock
);
6872 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6874 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6875 DBG3("UST app recv notify failed. Application died: sock = %d", sock
);
6876 } else if (ret
== -EAGAIN
) {
6877 WARN("UST app recv notify failed. Communication time out: sock = %d", sock
);
6879 ERR("UST app recv notify failed with ret %d: sock = %d", ret
, sock
);
6885 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6887 int sobjd
, cobjd
, loglevel_value
;
6888 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6890 struct lttng_ust_ctl_field
*fields
;
6892 DBG2("UST app ustctl register event received");
6894 ret
= lttng_ust_ctl_recv_register_event(sock
,
6904 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6905 DBG3("UST app recv event failed. Application died: sock = %d",
6907 } else if (ret
== -EAGAIN
) {
6908 WARN("UST app recv event failed. Communication time out: sock = %d",
6911 ERR("UST app recv event failed with ret %d: sock = %d", ret
, sock
);
6917 lttng::urcu::read_lock_guard rcu_lock
;
6918 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6920 DBG("Application socket %d is being torn down. Abort event notify",
6927 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6928 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6937 * Add event to the UST registry coming from the notify socket. This
6938 * call will free if needed the sig, fields and model_emf_uri. This
6939 * code path loses the ownsership of these variables and transfer them
6940 * to the this function.
6942 ret
= add_event_ust_registry(sock
,
6957 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6961 struct lttng_ust_ctl_field
*context_fields
;
6963 DBG2("UST app ustctl register channel received");
6965 ret
= lttng_ust_ctl_recv_register_channel(
6966 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6968 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6969 DBG3("UST app recv channel failed. Application died: sock = %d",
6971 } else if (ret
== -EAGAIN
) {
6972 WARN("UST app recv channel failed. Communication time out: sock = %d",
6975 ERR("UST app recv channel failed with ret %d: sock = %d",
6983 * The fields ownership are transfered to this function call meaning
6984 * that if needed it will be freed. After this, it's invalid to access
6985 * fields or clean them up.
6987 ret
= handle_app_register_channel_notification(
6988 sock
, cobjd
, context_fields
, field_count
);
6995 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6998 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
7000 struct lttng_ust_ctl_enum_entry
*entries
;
7002 DBG2("UST app ustctl register enum received");
7004 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
, &entries
, &nr_entries
);
7006 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
7007 DBG3("UST app recv enum failed. Application died: sock = %d", sock
);
7008 } else if (ret
== -EAGAIN
) {
7009 WARN("UST app recv enum failed. Communication time out: sock = %d",
7012 ERR("UST app recv enum failed with ret %d: sock = %d", ret
, sock
);
7017 /* Callee assumes ownership of entries. */
7018 ret
= add_enum_ust_registry(sock
, sobjd
, name
, entries
, nr_entries
);
7026 /* Should NEVER happen. */
7035 * Once the notify socket hangs up, this is called. First, it tries to find the
7036 * corresponding application. On failure, the call_rcu to close the socket is
7037 * executed. If an application is found, it tries to delete it from the notify
7038 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7040 * Note that an object needs to be allocated here so on ENOMEM failure, the
7041 * call RCU is not done but the rest of the cleanup is.
7043 void ust_app_notify_sock_unregister(int sock
)
7046 struct lttng_ht_iter iter
;
7047 struct ust_app
*app
;
7048 struct ust_app_notify_sock_obj
*obj
;
7050 LTTNG_ASSERT(sock
>= 0);
7054 obj
= zmalloc
<ust_app_notify_sock_obj
>();
7057 * An ENOMEM is kind of uncool. If this strikes we continue the
7058 * procedure but the call_rcu will not be called. In this case, we
7059 * accept the fd leak rather than possibly creating an unsynchronized
7060 * state between threads.
7062 * TODO: The notify object should be created once the notify socket is
7063 * registered and stored independantely from the ust app object. The
7064 * tricky part is to synchronize the teardown of the application and
7065 * this notify object. Let's keep that in mind so we can avoid this
7066 * kind of shenanigans with ENOMEM in the teardown path.
7073 DBG("UST app notify socket unregister %d", sock
);
7076 * Lookup application by notify socket. If this fails, this means that the
7077 * hash table delete has already been done by the application
7078 * unregistration process so we can safely close the notify socket in a
7081 app
= find_app_by_notify_sock(sock
);
7086 iter
.iter
.node
= &app
->notify_sock_n
.node
;
7089 * Whatever happens here either we fail or succeed, in both cases we have
7090 * to close the socket after a grace period to continue to the call RCU
7091 * here. If the deletion is successful, the application is not visible
7092 * anymore by other threads and is it fails it means that it was already
7093 * deleted from the hash table so either way we just have to close the
7096 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
7102 * Close socket after a grace period to avoid for the socket to be reused
7103 * before the application object is freed creating potential race between
7104 * threads trying to add unique in the global hash table.
7107 call_rcu(&obj
->head
, close_notify_sock_rcu
);
7112 * Destroy a ust app data structure and free its memory.
7114 void ust_app_destroy(struct ust_app
*app
)
7120 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
7124 * Take a snapshot for a given UST session. The snapshot is sent to the given
7127 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7129 enum lttng_error_code
ust_app_snapshot_record(const struct ltt_ust_session
*usess
,
7130 const struct consumer_output
*output
,
7131 uint64_t nb_packets_per_stream
)
7134 enum lttng_error_code status
= LTTNG_OK
;
7135 struct lttng_ht_iter iter
;
7136 struct ust_app
*app
;
7137 char *trace_path
= nullptr;
7139 LTTNG_ASSERT(usess
);
7140 LTTNG_ASSERT(output
);
7144 switch (usess
->buffer_type
) {
7145 case LTTNG_BUFFER_PER_UID
:
7147 struct buffer_reg_uid
*reg
;
7149 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7150 struct buffer_reg_channel
*buf_reg_chan
;
7151 struct consumer_socket
*socket
;
7152 char pathname
[PATH_MAX
];
7153 size_t consumer_path_offset
= 0;
7155 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7156 /* Skip since no metadata is present */
7160 /* Get consumer socket to use to push the metadata.*/
7161 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7164 status
= LTTNG_ERR_INVALID
;
7168 memset(pathname
, 0, sizeof(pathname
));
7169 ret
= snprintf(pathname
,
7171 DEFAULT_UST_TRACE_UID_PATH
,
7173 reg
->bits_per_long
);
7175 PERROR("snprintf snapshot path");
7176 status
= LTTNG_ERR_INVALID
;
7179 /* Free path allowed on previous iteration. */
7181 trace_path
= setup_channel_trace_path(
7182 usess
->consumer
, pathname
, &consumer_path_offset
);
7184 status
= LTTNG_ERR_INVALID
;
7187 /* Add the UST default trace dir to path. */
7188 cds_lfht_for_each_entry (
7189 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7191 consumer_snapshot_channel(socket
,
7192 buf_reg_chan
->consumer_key
,
7195 &trace_path
[consumer_path_offset
],
7196 nb_packets_per_stream
);
7197 if (status
!= LTTNG_OK
) {
7201 status
= consumer_snapshot_channel(socket
,
7202 reg
->registry
->reg
.ust
->_metadata_key
,
7205 &trace_path
[consumer_path_offset
],
7207 if (status
!= LTTNG_OK
) {
7213 case LTTNG_BUFFER_PER_PID
:
7215 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7216 struct consumer_socket
*socket
;
7217 struct lttng_ht_iter chan_iter
;
7218 struct ust_app_channel
*ua_chan
;
7219 struct ust_app_session
*ua_sess
;
7220 lsu::registry_session
*registry
;
7221 char pathname
[PATH_MAX
];
7222 size_t consumer_path_offset
= 0;
7224 ua_sess
= lookup_session_by_app(usess
, app
);
7226 /* Session not associated with this app. */
7230 /* Get the right consumer socket for the application. */
7231 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, output
);
7233 status
= LTTNG_ERR_INVALID
;
7237 /* Add the UST default trace dir to path. */
7238 memset(pathname
, 0, sizeof(pathname
));
7239 ret
= snprintf(pathname
, sizeof(pathname
), "%s", ua_sess
->path
);
7241 status
= LTTNG_ERR_INVALID
;
7242 PERROR("snprintf snapshot path");
7245 /* Free path allowed on previous iteration. */
7247 trace_path
= setup_channel_trace_path(
7248 usess
->consumer
, pathname
, &consumer_path_offset
);
7250 status
= LTTNG_ERR_INVALID
;
7253 cds_lfht_for_each_entry (
7254 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7256 consumer_snapshot_channel(socket
,
7260 &trace_path
[consumer_path_offset
],
7261 nb_packets_per_stream
);
7265 case LTTNG_ERR_CHAN_NOT_FOUND
:
7272 registry
= get_session_registry(ua_sess
);
7274 DBG("Application session is being torn down. Skip application.");
7277 status
= consumer_snapshot_channel(socket
,
7278 registry
->_metadata_key
,
7281 &trace_path
[consumer_path_offset
],
7286 case LTTNG_ERR_CHAN_NOT_FOUND
:
7306 * Return the size taken by one more packet per stream.
7308 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session
*usess
,
7309 uint64_t cur_nr_packets
)
7311 uint64_t tot_size
= 0;
7312 struct ust_app
*app
;
7313 struct lttng_ht_iter iter
;
7315 LTTNG_ASSERT(usess
);
7317 switch (usess
->buffer_type
) {
7318 case LTTNG_BUFFER_PER_UID
:
7320 struct buffer_reg_uid
*reg
;
7322 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7323 struct buffer_reg_channel
*buf_reg_chan
;
7326 cds_lfht_for_each_entry (
7327 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7328 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7330 * Don't take channel into account if we
7331 * already grab all its packets.
7335 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7341 case LTTNG_BUFFER_PER_PID
:
7344 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7345 struct ust_app_channel
*ua_chan
;
7346 struct ust_app_session
*ua_sess
;
7347 struct lttng_ht_iter chan_iter
;
7349 ua_sess
= lookup_session_by_app(usess
, app
);
7351 /* Session not associated with this app. */
7355 cds_lfht_for_each_entry (
7356 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7357 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7359 * Don't take channel into account if we
7360 * already grab all its packets.
7364 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7378 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7379 struct cds_list_head
*buffer_reg_uid_list
,
7380 struct consumer_output
*consumer
,
7383 uint64_t *discarded
,
7387 uint64_t consumer_chan_key
;
7392 ret
= buffer_reg_uid_consumer_channel_key(
7393 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7401 ret
= consumer_get_lost_packets(ust_session_id
, consumer_chan_key
, consumer
, lost
);
7403 ret
= consumer_get_discarded_events(
7404 ust_session_id
, consumer_chan_key
, consumer
, discarded
);
7411 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7412 struct ltt_ust_channel
*uchan
,
7413 struct consumer_output
*consumer
,
7415 uint64_t *discarded
,
7419 struct lttng_ht_iter iter
;
7420 struct lttng_ht_node_str
*ua_chan_node
;
7421 struct ust_app
*app
;
7422 struct ust_app_session
*ua_sess
;
7423 struct ust_app_channel
*ua_chan
;
7430 * Iterate over every registered applications. Sum counters for
7431 * all applications containing requested session and channel.
7433 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7434 struct lttng_ht_iter uiter
;
7436 ua_sess
= lookup_session_by_app(usess
, app
);
7437 if (ua_sess
== nullptr) {
7442 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7443 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7444 /* If the session is found for the app, the channel must be there */
7445 LTTNG_ASSERT(ua_chan_node
);
7447 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7452 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
, consumer
, &_lost
);
7458 uint64_t _discarded
;
7460 ret
= consumer_get_discarded_events(
7461 usess
->id
, ua_chan
->key
, consumer
, &_discarded
);
7465 (*discarded
) += _discarded
;
7473 static int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7476 struct ust_app_session
*ua_sess
;
7478 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7482 ua_sess
= lookup_session_by_app(usess
, app
);
7483 if (ua_sess
== nullptr) {
7484 /* The session is in teardown process. Ignore and continue. */
7488 pthread_mutex_lock(&ua_sess
->lock
);
7490 if (ua_sess
->deleted
) {
7494 pthread_mutex_lock(&app
->sock_lock
);
7495 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7496 pthread_mutex_unlock(&app
->sock_lock
);
7499 pthread_mutex_unlock(&ua_sess
->lock
);
7503 health_code_update();
7508 * Regenerate the statedump for each app in the session.
7510 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7513 struct lttng_ht_iter iter
;
7514 struct ust_app
*app
;
7516 DBG("Regenerating the metadata for all UST apps");
7520 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7521 if (!app
->compatible
) {
7525 ret
= ust_app_regenerate_statedump(usess
, app
);
7527 /* Continue to the next app even on error */
7538 * Rotate all the channels of a session.
7540 * Return LTTNG_OK on success or else an LTTng error code.
7542 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7545 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7546 struct lttng_ht_iter iter
;
7547 struct ust_app
*app
;
7548 struct ltt_ust_session
*usess
= session
->ust_session
;
7550 LTTNG_ASSERT(usess
);
7554 switch (usess
->buffer_type
) {
7555 case LTTNG_BUFFER_PER_UID
:
7557 struct buffer_reg_uid
*reg
;
7559 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7560 struct buffer_reg_channel
*buf_reg_chan
;
7561 struct consumer_socket
*socket
;
7563 /* Get consumer socket to use to push the metadata.*/
7564 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7567 cmd_ret
= LTTNG_ERR_INVALID
;
7571 /* Rotate the data channels. */
7572 cds_lfht_for_each_entry (
7573 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7574 ret
= consumer_rotate_channel(socket
,
7575 buf_reg_chan
->consumer_key
,
7577 /* is_metadata_channel */ false);
7579 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7585 * The metadata channel might not be present.
7587 * Consumer stream allocation can be done
7588 * asynchronously and can fail on intermediary
7589 * operations (i.e add context) and lead to data
7590 * channels created with no metadata channel.
7592 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7593 /* Skip since no metadata is present. */
7598 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7599 (void) push_metadata(locked_registry
, usess
->consumer
);
7602 ret
= consumer_rotate_channel(socket
,
7603 reg
->registry
->reg
.ust
->_metadata_key
,
7605 /* is_metadata_channel */ true);
7607 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7613 case LTTNG_BUFFER_PER_PID
:
7615 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7616 struct consumer_socket
*socket
;
7617 struct lttng_ht_iter chan_iter
;
7618 struct ust_app_channel
*ua_chan
;
7619 struct ust_app_session
*ua_sess
;
7620 lsu::registry_session
*registry
;
7622 ua_sess
= lookup_session_by_app(usess
, app
);
7624 /* Session not associated with this app. */
7628 /* Get the right consumer socket for the application. */
7629 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7632 cmd_ret
= LTTNG_ERR_INVALID
;
7636 registry
= get_session_registry(ua_sess
);
7638 DBG("Application session is being torn down. Skip application.");
7642 /* Rotate the data channels. */
7643 cds_lfht_for_each_entry (
7644 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7645 ret
= consumer_rotate_channel(socket
,
7648 /* is_metadata_channel */ false);
7650 /* Per-PID buffer and application going away. */
7651 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7653 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7658 /* Rotate the metadata channel. */
7660 auto locked_registry
= registry
->lock();
7662 (void) push_metadata(locked_registry
, usess
->consumer
);
7664 ret
= consumer_rotate_channel(socket
,
7665 registry
->_metadata_key
,
7667 /* is_metadata_channel */ true);
7669 /* Per-PID buffer and application going away. */
7670 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7672 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7690 enum lttng_error_code
ust_app_create_channel_subdirectories(const struct ltt_ust_session
*usess
)
7692 enum lttng_error_code ret
= LTTNG_OK
;
7693 struct lttng_ht_iter iter
;
7694 enum lttng_trace_chunk_status chunk_status
;
7695 char *pathname_index
;
7698 LTTNG_ASSERT(usess
->current_trace_chunk
);
7701 switch (usess
->buffer_type
) {
7702 case LTTNG_BUFFER_PER_UID
:
7704 struct buffer_reg_uid
*reg
;
7706 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7707 fmt_ret
= asprintf(&pathname_index
,
7708 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
7709 "/" DEFAULT_INDEX_DIR
,
7711 reg
->bits_per_long
);
7713 ERR("Failed to format channel index directory");
7714 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7719 * Create the index subdirectory which will take care
7720 * of implicitly creating the channel's path.
7722 chunk_status
= lttng_trace_chunk_create_subdirectory(
7723 usess
->current_trace_chunk
, pathname_index
);
7724 free(pathname_index
);
7725 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7726 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7732 case LTTNG_BUFFER_PER_PID
:
7734 struct ust_app
*app
;
7737 * Create the toplevel ust/ directory in case no apps are running.
7739 chunk_status
= lttng_trace_chunk_create_subdirectory(usess
->current_trace_chunk
,
7740 DEFAULT_UST_TRACE_DIR
);
7741 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7742 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7746 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7747 struct ust_app_session
*ua_sess
;
7748 lsu::registry_session
*registry
;
7750 ua_sess
= lookup_session_by_app(usess
, app
);
7752 /* Session not associated with this app. */
7756 registry
= get_session_registry(ua_sess
);
7758 DBG("Application session is being torn down. Skip application.");
7762 fmt_ret
= asprintf(&pathname_index
,
7763 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7766 ERR("Failed to format channel index directory");
7767 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7771 * Create the index subdirectory which will take care
7772 * of implicitly creating the channel's path.
7774 chunk_status
= lttng_trace_chunk_create_subdirectory(
7775 usess
->current_trace_chunk
, pathname_index
);
7776 free(pathname_index
);
7777 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7778 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7795 * Clear all the channels of a session.
7797 * Return LTTNG_OK on success or else an LTTng error code.
7799 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7802 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7803 struct lttng_ht_iter iter
;
7804 struct ust_app
*app
;
7805 struct ltt_ust_session
*usess
= session
->ust_session
;
7807 LTTNG_ASSERT(usess
);
7811 if (usess
->active
) {
7812 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7813 cmd_ret
= LTTNG_ERR_FATAL
;
7817 switch (usess
->buffer_type
) {
7818 case LTTNG_BUFFER_PER_UID
:
7820 struct buffer_reg_uid
*reg
;
7822 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7823 struct buffer_reg_channel
*buf_reg_chan
;
7824 struct consumer_socket
*socket
;
7826 /* Get consumer socket to use to push the metadata.*/
7827 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7830 cmd_ret
= LTTNG_ERR_INVALID
;
7834 /* Clear the data channels. */
7835 cds_lfht_for_each_entry (
7836 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7837 ret
= consumer_clear_channel(socket
, buf_reg_chan
->consumer_key
);
7844 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7845 (void) push_metadata(locked_registry
, usess
->consumer
);
7849 * Clear the metadata channel.
7850 * Metadata channel is not cleared per se but we still need to
7851 * perform a rotation operation on it behind the scene.
7853 ret
= consumer_clear_channel(socket
, reg
->registry
->reg
.ust
->_metadata_key
);
7860 case LTTNG_BUFFER_PER_PID
:
7862 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7863 struct consumer_socket
*socket
;
7864 struct lttng_ht_iter chan_iter
;
7865 struct ust_app_channel
*ua_chan
;
7866 struct ust_app_session
*ua_sess
;
7867 lsu::registry_session
*registry
;
7869 ua_sess
= lookup_session_by_app(usess
, app
);
7871 /* Session not associated with this app. */
7875 /* Get the right consumer socket for the application. */
7876 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7879 cmd_ret
= LTTNG_ERR_INVALID
;
7883 registry
= get_session_registry(ua_sess
);
7885 DBG("Application session is being torn down. Skip application.");
7889 /* Clear the data channels. */
7890 cds_lfht_for_each_entry (
7891 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7892 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7894 /* Per-PID buffer and application going away. */
7895 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7903 auto locked_registry
= registry
->lock();
7904 (void) push_metadata(locked_registry
, usess
->consumer
);
7908 * Clear the metadata channel.
7909 * Metadata channel is not cleared per se but we still need to
7910 * perform rotation operation on it behind the scene.
7912 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7914 /* Per-PID buffer and application going away. */
7915 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7933 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7934 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7937 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7947 * This function skips the metadata channel as the begin/end timestamps of a
7948 * metadata packet are useless.
7950 * Moreover, opening a packet after a "clear" will cause problems for live
7951 * sessions as it will introduce padding that was not part of the first trace
7952 * chunk. The relay daemon expects the content of the metadata stream of
7953 * successive metadata trace chunks to be strict supersets of one another.
7955 * For example, flushing a packet at the beginning of the metadata stream of
7956 * a trace chunk resulting from a "clear" session command will cause the
7957 * size of the metadata stream of the new trace chunk to not match the size of
7958 * the metadata stream of the original chunk. This will confuse the relay
7959 * daemon as the same "offset" in a metadata stream will no longer point
7960 * to the same content.
7962 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7964 enum lttng_error_code ret
= LTTNG_OK
;
7965 struct lttng_ht_iter iter
;
7966 struct ltt_ust_session
*usess
= session
->ust_session
;
7968 LTTNG_ASSERT(usess
);
7972 switch (usess
->buffer_type
) {
7973 case LTTNG_BUFFER_PER_UID
:
7975 struct buffer_reg_uid
*reg
;
7977 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7978 struct buffer_reg_channel
*buf_reg_chan
;
7979 struct consumer_socket
*socket
;
7981 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7984 ret
= LTTNG_ERR_FATAL
;
7988 cds_lfht_for_each_entry (
7989 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7990 const int open_ret
= consumer_open_channel_packets(
7991 socket
, buf_reg_chan
->consumer_key
);
7994 ret
= LTTNG_ERR_UNK
;
8001 case LTTNG_BUFFER_PER_PID
:
8003 struct ust_app
*app
;
8005 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8006 struct consumer_socket
*socket
;
8007 struct lttng_ht_iter chan_iter
;
8008 struct ust_app_channel
*ua_chan
;
8009 struct ust_app_session
*ua_sess
;
8010 lsu::registry_session
*registry
;
8012 ua_sess
= lookup_session_by_app(usess
, app
);
8014 /* Session not associated with this app. */
8018 /* Get the right consumer socket for the application. */
8019 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
8022 ret
= LTTNG_ERR_FATAL
;
8026 registry
= get_session_registry(ua_sess
);
8028 DBG("Application session is being torn down. Skip application.");
8032 cds_lfht_for_each_entry (
8033 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
8034 const int open_ret
=
8035 consumer_open_channel_packets(socket
, ua_chan
->key
);
8039 * Per-PID buffer and application going
8042 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
8046 ret
= LTTNG_ERR_UNK
;
8063 lsu::ctl_field_quirks
ust_app::ctl_field_quirks() const
8066 * Application contexts are expressed as variants. LTTng-UST announces
8067 * those by registering an enumeration named `..._tag`. It then registers a
8068 * variant as part of the event context that contains the various possible
8071 * Unfortunately, the names used in the enumeration and variant don't
8072 * match: the enumeration names are all prefixed with an underscore while
8073 * the variant type tag fields aren't.
8075 * While the CTF 1.8.3 specification mentions that
8076 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8077 * 1.x (and possibly others) expect a perfect match between the names used
8078 * by tags and variants.
8080 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8081 * the variant's fields are modified to match the mappings of its tag.
8083 * From ABI version >= 10.x, the variant fields and tag mapping names
8084 * correctly match, making this quirk unnecessary.
8086 return v_major
<= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
:
8087 lsu::ctl_field_quirks::NONE
;