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
) {
508 lttng::urcu::read_lock_guard read_lock
;
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
;
552 session_put(session
);
557 * Delete ust app channel safely. RCU read lock must be held before calling
560 * The session list lock must be held by the caller.
562 static void delete_ust_app_channel(int sock
,
563 struct ust_app_channel
*ua_chan
,
565 const lsu::registry_session::locked_ptr
& locked_registry
)
568 struct lttng_ht_iter iter
;
569 struct ust_app_event
*ua_event
;
570 struct ust_app_ctx
*ua_ctx
;
571 struct ust_app_stream
*stream
, *stmp
;
573 LTTNG_ASSERT(ua_chan
);
574 ASSERT_RCU_READ_LOCKED();
576 DBG3("UST app deleting channel %s", ua_chan
->name
);
579 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
580 cds_list_del(&stream
->list
);
581 delete_ust_app_stream(sock
, stream
, app
);
585 cds_lfht_for_each_entry (ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
586 cds_list_del(&ua_ctx
->list
);
587 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
589 delete_ust_app_ctx(sock
, ua_ctx
, app
);
593 cds_lfht_for_each_entry (ua_chan
->events
->ht
, &iter
.iter
, ua_event
, node
.node
) {
594 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
596 delete_ust_app_event(sock
, ua_event
, app
);
599 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
600 /* Wipe and free registry from session registry. */
601 if (locked_registry
) {
603 locked_registry
->remove_channel(ua_chan
->key
, sock
>= 0);
604 } catch (const std::exception
& ex
) {
605 DBG("Could not find channel for removal: %s", ex
.what());
610 * A negative socket can be used by the caller when
611 * cleaning-up a ua_chan in an error path. Skip the
612 * accounting in this case.
615 save_per_pid_lost_discarded_counters(ua_chan
);
619 if (ua_chan
->obj
!= nullptr) {
620 /* Remove channel from application UST object descriptor. */
621 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
622 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
624 pthread_mutex_lock(&app
->sock_lock
);
625 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
626 pthread_mutex_unlock(&app
->sock_lock
);
628 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
629 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
633 } else if (ret
== -EAGAIN
) {
634 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
639 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
646 lttng_fd_put(LTTNG_FD_APPS
, 1);
649 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
652 int ust_app_register_done(struct ust_app
*app
)
656 pthread_mutex_lock(&app
->sock_lock
);
657 ret
= lttng_ust_ctl_register_done(app
->sock
);
658 pthread_mutex_unlock(&app
->sock_lock
);
662 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
667 pthread_mutex_lock(&app
->sock_lock
);
672 ret
= lttng_ust_ctl_release_object(sock
, data
);
674 pthread_mutex_unlock(&app
->sock_lock
);
680 * Push metadata to consumer socket.
682 * RCU read-side lock must be held to guarantee existence of socket.
683 * Must be called with the ust app session lock held.
684 * Must be called with the registry lock held.
686 * On success, return the len of metadata pushed or else a negative value.
687 * Returning a -EPIPE return value means we could not send the metadata,
688 * but it can be caused by recoverable errors (e.g. the application has
689 * terminated concurrently).
691 ssize_t
ust_app_push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
692 struct consumer_socket
*socket
,
696 char *metadata_str
= nullptr;
697 size_t len
, offset
, new_metadata_len_sent
;
699 uint64_t metadata_key
, metadata_version
;
701 LTTNG_ASSERT(locked_registry
);
702 LTTNG_ASSERT(socket
);
703 ASSERT_RCU_READ_LOCKED();
705 metadata_key
= locked_registry
->_metadata_key
;
708 * Means that no metadata was assigned to the session. This can
709 * happens if no start has been done previously.
715 offset
= locked_registry
->_metadata_len_sent
;
716 len
= locked_registry
->_metadata_len
- locked_registry
->_metadata_len_sent
;
717 new_metadata_len_sent
= locked_registry
->_metadata_len
;
718 metadata_version
= locked_registry
->_metadata_version
;
720 DBG3("No metadata to push for metadata key %" PRIu64
,
721 locked_registry
->_metadata_key
);
723 if (send_zero_data
) {
724 DBG("No metadata to push");
730 /* Allocate only what we have to send. */
731 metadata_str
= calloc
<char>(len
);
733 PERROR("zmalloc ust app metadata string");
737 /* Copy what we haven't sent out. */
738 memcpy(metadata_str
, locked_registry
->_metadata
+ offset
, len
);
741 pthread_mutex_unlock(&locked_registry
->_lock
);
743 * We need to unlock the registry while we push metadata to
744 * break a circular dependency between the consumerd metadata
745 * lock and the sessiond registry lock. Indeed, pushing metadata
746 * to the consumerd awaits that it gets pushed all the way to
747 * relayd, but doing so requires grabbing the metadata lock. If
748 * a concurrent metadata request is being performed by
749 * consumerd, this can try to grab the registry lock on the
750 * sessiond while holding the metadata lock on the consumer
751 * daemon. Those push and pull schemes are performed on two
752 * different bidirectionnal communication sockets.
754 ret
= consumer_push_metadata(
755 socket
, metadata_key
, metadata_str
, len
, offset
, metadata_version
);
756 pthread_mutex_lock(&locked_registry
->_lock
);
759 * There is an acceptable race here between the registry
760 * metadata key assignment and the creation on the
761 * consumer. The session daemon can concurrently push
762 * metadata for this registry while being created on the
763 * consumer since the metadata key of the registry is
764 * assigned *before* it is setup to avoid the consumer
765 * to ask for metadata that could possibly be not found
766 * in the session daemon.
768 * The metadata will get pushed either by the session
769 * being stopped or the consumer requesting metadata if
770 * that race is triggered.
772 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
775 ERR("Error pushing metadata to consumer");
781 * Metadata may have been concurrently pushed, since
782 * we're not holding the registry lock while pushing to
783 * consumer. This is handled by the fact that we send
784 * the metadata content, size, and the offset at which
785 * that metadata belongs. This may arrive out of order
786 * on the consumer side, and the consumer is able to
787 * deal with overlapping fragments. The consumer
788 * supports overlapping fragments, which must be
789 * contiguous starting from offset 0. We keep the
790 * largest metadata_len_sent value of the concurrent
793 locked_registry
->_metadata_len_sent
=
794 std::max(locked_registry
->_metadata_len_sent
, new_metadata_len_sent
);
803 * On error, flag the registry that the metadata is
804 * closed. We were unable to push anything and this
805 * means that either the consumer is not responding or
806 * the metadata cache has been destroyed on the
809 locked_registry
->_metadata_closed
= true;
817 * For a given application and session, push metadata to consumer.
818 * Either sock or consumer is required : if sock is NULL, the default
819 * socket to send the metadata is retrieved from consumer, if sock
820 * is not NULL we use it to send the metadata.
821 * RCU read-side lock must be held while calling this function,
822 * therefore ensuring existence of registry. It also ensures existence
823 * of socket throughout this function.
825 * Return 0 on success else a negative error.
826 * Returning a -EPIPE return value means we could not send the metadata,
827 * but it can be caused by recoverable errors (e.g. the application has
828 * terminated concurrently).
830 static int push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
831 struct consumer_output
*consumer
)
835 struct consumer_socket
*socket
;
837 LTTNG_ASSERT(locked_registry
);
838 LTTNG_ASSERT(consumer
);
839 ASSERT_RCU_READ_LOCKED();
841 if (locked_registry
->_metadata_closed
) {
846 /* Get consumer socket to use to push the metadata.*/
847 socket
= consumer_find_socket_by_bitness(locked_registry
->abi
.bits_per_long
, consumer
);
853 ret
= ust_app_push_metadata(locked_registry
, socket
, 0);
865 * Send to the consumer a close metadata command for the given session. Once
866 * done, the metadata channel is deleted and the session metadata pointer is
867 * nullified. The session lock MUST be held unless the application is
868 * in the destroy path.
870 * Do not hold the registry lock while communicating with the consumerd, because
871 * doing so causes inter-process deadlocks between consumerd and sessiond with
872 * the metadata request notification.
874 * Return 0 on success else a negative value.
876 static int close_metadata(uint64_t metadata_key
,
877 unsigned int consumer_bitness
,
878 struct consumer_output
*consumer
)
881 struct consumer_socket
*socket
;
882 lttng::urcu::read_lock_guard read_lock_guard
;
884 LTTNG_ASSERT(consumer
);
886 /* Get consumer socket to use to push the metadata. */
887 socket
= consumer_find_socket_by_bitness(consumer_bitness
, consumer
);
893 ret
= consumer_close_metadata(socket
, metadata_key
);
902 static void delete_ust_app_session_rcu(struct rcu_head
*head
)
904 struct ust_app_session
*ua_sess
=
905 lttng::utils::container_of(head
, &ust_app_session::rcu_head
);
907 lttng_ht_destroy(ua_sess
->channels
);
912 * Delete ust app session safely. RCU read lock must be held before calling
915 * The session list lock must be held by the caller.
917 static void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
, struct ust_app
*app
)
920 struct lttng_ht_iter iter
;
921 struct ust_app_channel
*ua_chan
;
923 LTTNG_ASSERT(ua_sess
);
924 ASSERT_RCU_READ_LOCKED();
926 pthread_mutex_lock(&ua_sess
->lock
);
928 LTTNG_ASSERT(!ua_sess
->deleted
);
929 ua_sess
->deleted
= true;
931 auto locked_registry
= get_locked_session_registry(ua_sess
);
932 /* Registry can be null on error path during initialization. */
933 if (locked_registry
) {
934 /* Push metadata for application before freeing the application. */
935 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
938 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
939 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
941 delete_ust_app_channel(sock
, ua_chan
, app
, locked_registry
);
944 if (locked_registry
) {
946 * Don't ask to close metadata for global per UID buffers. Close
947 * metadata only on destroy trace session in this case. Also, the
948 * previous push metadata could have flag the metadata registry to
949 * close so don't send a close command if closed.
951 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
952 const auto metadata_key
= locked_registry
->_metadata_key
;
953 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
955 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
956 locked_registry
->_metadata_closed
= true;
959 /* Release lock before communication, see comments in close_metadata(). */
960 locked_registry
.reset();
961 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
965 /* In case of per PID, the registry is kept in the session. */
966 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
967 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
970 * Registry can be null on error path during
973 buffer_reg_pid_remove(reg_pid
);
974 buffer_reg_pid_destroy(reg_pid
);
978 if (ua_sess
->handle
!= -1) {
979 pthread_mutex_lock(&app
->sock_lock
);
980 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
981 pthread_mutex_unlock(&app
->sock_lock
);
983 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
984 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
987 } else if (ret
== -EAGAIN
) {
988 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
992 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
999 /* Remove session from application UST object descriptor. */
1000 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
1001 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
1005 pthread_mutex_unlock(&ua_sess
->lock
);
1007 consumer_output_put(ua_sess
->consumer
);
1009 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1013 * Delete a traceable application structure from the global list. Never call
1014 * this function outside of a call_rcu call.
1016 static void delete_ust_app(struct ust_app
*app
)
1019 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1020 struct lttng_ht_iter iter
;
1021 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1022 bool event_notifier_write_fd_is_open
;
1025 * The session list lock must be held during this function to guarantee
1026 * the existence of ua_sess.
1028 session_lock_list();
1029 /* Delete ust app sessions info */
1034 cds_list_for_each_entry_safe (ua_sess
, tmp_ua_sess
, &app
->teardown_head
, teardown_node
) {
1035 /* Free every object in the session and the session. */
1036 lttng::urcu::read_lock_guard read_lock
;
1037 delete_ust_app_session(sock
, ua_sess
, app
);
1040 /* Remove the event notifier rules associated with this app. */
1042 lttng::urcu::read_lock_guard read_lock
;
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
);
1055 lttng_ht_destroy(app
->sessions
);
1056 lttng_ht_destroy(app
->ust_sessions_objd
);
1057 lttng_ht_destroy(app
->ust_objd
);
1058 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1061 * This could be NULL if the event notifier setup failed (e.g the app
1062 * was killed or the tracer does not support this feature).
1064 if (app
->event_notifier_group
.object
) {
1065 enum lttng_error_code ret_code
;
1066 enum event_notifier_error_accounting_status status
;
1068 const int event_notifier_read_fd
=
1069 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
);
1071 ret_code
= notification_thread_command_remove_tracer_event_source(
1072 the_notification_thread_handle
, event_notifier_read_fd
);
1073 if (ret_code
!= LTTNG_OK
) {
1074 ERR("Failed to remove application tracer event source from notification thread");
1077 status
= event_notifier_error_accounting_unregister_app(app
);
1078 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1079 ERR("Error unregistering app from event notifier error accounting");
1082 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1083 free(app
->event_notifier_group
.object
);
1086 event_notifier_write_fd_is_open
=
1087 lttng_pipe_is_write_open(app
->event_notifier_group
.event_pipe
);
1088 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1090 * Release the file descriptors reserved for the event notifier pipe.
1091 * The app could be destroyed before the write end of the pipe could be
1092 * passed to the application (and closed). In that case, both file
1093 * descriptors must be released.
1095 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1098 * Wait until we have deleted the application from the sock hash table
1099 * before closing this socket, otherwise an application could re-use the
1100 * socket ID and race with the teardown, using the same hash table entry.
1102 * It's OK to leave the close in call_rcu. We want it to stay unique for
1103 * all RCU readers that could run concurrently with unregister app,
1104 * therefore we _need_ to only close that socket after a grace period. So
1105 * it should stay in this RCU callback.
1107 * This close() is a very important step of the synchronization model so
1108 * every modification to this function must be carefully reviewed.
1114 lttng_fd_put(LTTNG_FD_APPS
, 1);
1116 DBG2("UST app pid %d deleted", app
->pid
);
1118 session_unlock_list();
1122 * URCU intermediate call to delete an UST app.
1124 static void delete_ust_app_rcu(struct rcu_head
*head
)
1126 struct lttng_ht_node_ulong
*node
=
1127 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1128 struct ust_app
*app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
1130 DBG3("Call RCU deleting app PID %d", app
->pid
);
1131 delete_ust_app(app
);
1135 * Delete the session from the application ht and delete the data structure by
1136 * freeing every object inside and releasing them.
1138 * The session list lock must be held by the caller.
1140 static void destroy_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
1143 struct lttng_ht_iter iter
;
1146 LTTNG_ASSERT(ua_sess
);
1148 iter
.iter
.node
= &ua_sess
->node
.node
;
1149 ret
= lttng_ht_del(app
->sessions
, &iter
);
1151 /* Already scheduled for teardown. */
1155 /* Once deleted, free the data structure. */
1156 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1163 * Alloc new UST app session.
1165 static struct ust_app_session
*alloc_ust_app_session()
1167 struct ust_app_session
*ua_sess
;
1169 /* Init most of the default value by allocating and zeroing */
1170 ua_sess
= zmalloc
<ust_app_session
>();
1171 if (ua_sess
== nullptr) {
1176 ua_sess
->handle
= -1;
1177 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1178 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1179 pthread_mutex_init(&ua_sess
->lock
, nullptr);
1188 * Alloc new UST app channel.
1190 static struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1191 struct ust_app_session
*ua_sess
,
1192 struct lttng_ust_abi_channel_attr
*attr
)
1194 struct ust_app_channel
*ua_chan
;
1196 /* Init most of the default value by allocating and zeroing */
1197 ua_chan
= zmalloc
<ust_app_channel
>();
1198 if (ua_chan
== nullptr) {
1203 /* Setup channel name */
1204 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1205 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1207 ua_chan
->enabled
= true;
1208 ua_chan
->handle
= -1;
1209 ua_chan
->session
= ua_sess
;
1210 ua_chan
->key
= get_next_channel_key();
1211 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1212 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1213 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1215 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1216 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1218 /* Copy attributes */
1220 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1221 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1222 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1223 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1224 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1225 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1226 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1227 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1229 /* By default, the channel is a per cpu channel. */
1230 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1232 DBG3("UST app channel %s allocated", ua_chan
->name
);
1241 * Allocate and initialize a UST app stream.
1243 * Return newly allocated stream pointer or NULL on error.
1245 struct ust_app_stream
*ust_app_alloc_stream()
1247 struct ust_app_stream
*stream
= nullptr;
1249 stream
= zmalloc
<ust_app_stream
>();
1250 if (stream
== nullptr) {
1251 PERROR("zmalloc ust app stream");
1255 /* Zero could be a valid value for a handle so flag it to -1. */
1256 stream
->handle
= -1;
1263 * Alloc new UST app event.
1265 static struct ust_app_event
*alloc_ust_app_event(char *name
, struct lttng_ust_abi_event
*attr
)
1267 struct ust_app_event
*ua_event
;
1269 /* Init most of the default value by allocating and zeroing */
1270 ua_event
= zmalloc
<ust_app_event
>();
1271 if (ua_event
== nullptr) {
1272 PERROR("Failed to allocate ust_app_event structure");
1276 ua_event
->enabled
= true;
1277 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1278 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1279 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1281 /* Copy attributes */
1283 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1286 DBG3("UST app event %s allocated", ua_event
->name
);
1295 * Allocate a new UST app event notifier rule.
1297 static struct ust_app_event_notifier_rule
*
1298 alloc_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
)
1300 enum lttng_event_rule_generate_exclusions_status generate_exclusion_status
;
1301 enum lttng_condition_status cond_status
;
1302 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1303 struct lttng_condition
*condition
= nullptr;
1304 const struct lttng_event_rule
*event_rule
= nullptr;
1306 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1307 if (ua_event_notifier_rule
== nullptr) {
1308 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1312 ua_event_notifier_rule
->enabled
= true;
1313 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1314 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, ua_event_notifier_rule
->token
);
1316 condition
= lttng_trigger_get_condition(trigger
);
1317 LTTNG_ASSERT(condition
);
1318 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1319 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1321 cond_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
1322 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1323 LTTNG_ASSERT(event_rule
);
1325 ua_event_notifier_rule
->error_counter_index
=
1326 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1327 /* Acquire the event notifier's reference to the trigger. */
1328 lttng_trigger_get(trigger
);
1330 ua_event_notifier_rule
->trigger
= trigger
;
1331 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1332 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1333 event_rule
, &ua_event_notifier_rule
->exclusion
);
1334 switch (generate_exclusion_status
) {
1335 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1336 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1339 /* Error occurred. */
1340 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1341 goto error_put_trigger
;
1344 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1345 ua_event_notifier_rule
->token
);
1347 return ua_event_notifier_rule
;
1350 lttng_trigger_put(trigger
);
1352 free(ua_event_notifier_rule
);
1357 * Alloc new UST app context.
1359 static struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1361 struct ust_app_ctx
*ua_ctx
;
1363 ua_ctx
= zmalloc
<ust_app_ctx
>();
1364 if (ua_ctx
== nullptr) {
1368 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1371 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1372 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1373 char *provider_name
= nullptr, *ctx_name
= nullptr;
1375 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1376 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1377 if (!provider_name
|| !ctx_name
) {
1378 free(provider_name
);
1383 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1384 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1388 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1396 * Create a liblttng-ust filter bytecode from given bytecode.
1398 * Return allocated filter or NULL on error.
1400 static struct lttng_ust_abi_filter_bytecode
*
1401 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1403 struct lttng_ust_abi_filter_bytecode
*filter
= nullptr;
1405 /* Copy filter bytecode. */
1406 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1408 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
1414 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) == sizeof(struct lttng_ust_abi_filter_bytecode
));
1415 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1421 * Create a liblttng-ust capture bytecode from given bytecode.
1423 * Return allocated filter or NULL on error.
1425 static struct lttng_ust_abi_capture_bytecode
*
1426 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1428 struct lttng_ust_abi_capture_bytecode
*capture
= nullptr;
1430 /* Copy capture bytecode. */
1431 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1433 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
1439 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1440 sizeof(struct lttng_ust_abi_capture_bytecode
));
1441 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1447 * Find an ust_app using the sock and return it. RCU read side lock must be
1448 * held before calling this helper function.
1450 struct ust_app
*ust_app_find_by_sock(int sock
)
1452 struct lttng_ht_node_ulong
*node
;
1453 struct lttng_ht_iter iter
;
1455 ASSERT_RCU_READ_LOCKED();
1457 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
1458 node
= lttng_ht_iter_get_node_ulong(&iter
);
1459 if (node
== nullptr) {
1460 DBG2("UST app find by sock %d not found", sock
);
1464 return lttng::utils::container_of(node
, &ust_app::sock_n
);
1471 * Find an ust_app using the notify sock and return it. RCU read side lock must
1472 * be held before calling this helper function.
1474 static struct ust_app
*find_app_by_notify_sock(int sock
)
1476 struct lttng_ht_node_ulong
*node
;
1477 struct lttng_ht_iter iter
;
1479 ASSERT_RCU_READ_LOCKED();
1481 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *) ((unsigned long) sock
), &iter
);
1482 node
= lttng_ht_iter_get_node_ulong(&iter
);
1483 if (node
== nullptr) {
1484 DBG2("UST app find by notify sock %d not found", sock
);
1488 return lttng::utils::container_of(node
, &ust_app::notify_sock_n
);
1495 * Lookup for an ust app event based on event name, filter bytecode and the
1498 * Return an ust_app_event object or NULL on error.
1500 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1502 const struct lttng_bytecode
*filter
,
1504 const struct lttng_event_exclusion
*exclusion
)
1506 struct lttng_ht_iter iter
;
1507 struct lttng_ht_node_str
*node
;
1508 struct ust_app_event
*event
= nullptr;
1509 struct ust_app_ht_key key
;
1514 /* Setup key for event lookup. */
1516 key
.filter
= filter
;
1517 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) loglevel_value
;
1518 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1519 key
.exclusion
= exclusion
;
1521 /* Lookup using the event name as hash and a custom match fct. */
1522 cds_lfht_lookup(ht
->ht
,
1523 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1524 ht_match_ust_app_event
,
1527 node
= lttng_ht_iter_get_node_str(&iter
);
1528 if (node
== nullptr) {
1532 event
= lttng::utils::container_of(node
, &ust_app_event::node
);
1539 * Look-up an event notifier rule based on its token id.
1541 * Must be called with the RCU read lock held.
1542 * Return an ust_app_event_notifier_rule object or NULL on error.
1544 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(struct lttng_ht
*ht
,
1547 struct lttng_ht_iter iter
;
1548 struct lttng_ht_node_u64
*node
;
1549 struct ust_app_event_notifier_rule
*event_notifier_rule
= nullptr;
1552 ASSERT_RCU_READ_LOCKED();
1554 lttng_ht_lookup(ht
, &token
, &iter
);
1555 node
= lttng_ht_iter_get_node_u64(&iter
);
1556 if (node
== nullptr) {
1557 DBG2("UST app event notifier rule token not found: token = %" PRIu64
, token
);
1561 event_notifier_rule
= lttng::utils::container_of(node
, &ust_app_event_notifier_rule::node
);
1563 return event_notifier_rule
;
1567 * Create the channel context on the tracer.
1569 * Called with UST app session lock held.
1571 static int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1572 struct ust_app_ctx
*ua_ctx
,
1573 struct ust_app
*app
)
1577 health_code_update();
1579 pthread_mutex_lock(&app
->sock_lock
);
1580 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
, ua_chan
->obj
, &ua_ctx
->obj
);
1581 pthread_mutex_unlock(&app
->sock_lock
);
1583 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1585 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1588 } else if (ret
== -EAGAIN
) {
1590 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1594 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1602 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1604 DBG2("UST app context handle %d created successfully for channel %s",
1609 health_code_update();
1614 * Set the filter on the tracer.
1616 static int set_ust_object_filter(struct ust_app
*app
,
1617 const struct lttng_bytecode
*bytecode
,
1618 struct lttng_ust_abi_object_data
*ust_object
)
1621 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= nullptr;
1623 health_code_update();
1625 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1626 if (!ust_bytecode
) {
1627 ret
= -LTTNG_ERR_NOMEM
;
1630 pthread_mutex_lock(&app
->sock_lock
);
1631 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
, ust_object
);
1632 pthread_mutex_unlock(&app
->sock_lock
);
1634 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1636 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1639 } else if (ret
== -EAGAIN
) {
1641 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1645 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1654 DBG2("UST filter successfully set: object = %p", ust_object
);
1657 health_code_update();
1663 * Set a capture bytecode for the passed object.
1664 * The sequence number enforces the ordering at runtime and on reception of
1665 * the captured payloads.
1667 static int set_ust_capture(struct ust_app
*app
,
1668 const struct lttng_bytecode
*bytecode
,
1669 unsigned int capture_seqnum
,
1670 struct lttng_ust_abi_object_data
*ust_object
)
1673 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= nullptr;
1675 health_code_update();
1677 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1678 if (!ust_bytecode
) {
1679 ret
= -LTTNG_ERR_NOMEM
;
1684 * Set the sequence number to ensure the capture of fields is ordered.
1686 ust_bytecode
->seqnum
= capture_seqnum
;
1688 pthread_mutex_lock(&app
->sock_lock
);
1689 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
, ust_object
);
1690 pthread_mutex_unlock(&app
->sock_lock
);
1692 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1694 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1697 } else if (ret
== -EAGAIN
) {
1699 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1703 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1712 DBG2("UST capture successfully set: object = %p", ust_object
);
1715 health_code_update();
1720 static struct lttng_ust_abi_event_exclusion
*
1721 create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion
*exclusion
)
1723 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= nullptr;
1724 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1725 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1727 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1728 if (!ust_exclusion
) {
1733 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1734 sizeof(struct lttng_ust_abi_event_exclusion
));
1735 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1737 return ust_exclusion
;
1741 * Set event exclusions on the tracer.
1743 static int set_ust_object_exclusions(struct ust_app
*app
,
1744 const struct lttng_event_exclusion
*exclusions
,
1745 struct lttng_ust_abi_object_data
*ust_object
)
1748 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= nullptr;
1750 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1752 health_code_update();
1754 ust_exclusions
= create_ust_exclusion_from_exclusion(exclusions
);
1755 if (!ust_exclusions
) {
1756 ret
= -LTTNG_ERR_NOMEM
;
1759 pthread_mutex_lock(&app
->sock_lock
);
1760 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1761 pthread_mutex_unlock(&app
->sock_lock
);
1763 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1765 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1768 } else if (ret
== -EAGAIN
) {
1770 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1774 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1783 DBG2("UST exclusions set successfully for object %p", ust_object
);
1786 health_code_update();
1787 free(ust_exclusions
);
1792 * Disable the specified event on to UST tracer for the UST session.
1794 static int disable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*object
)
1798 health_code_update();
1800 pthread_mutex_lock(&app
->sock_lock
);
1801 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1802 pthread_mutex_unlock(&app
->sock_lock
);
1804 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1806 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1809 } else if (ret
== -EAGAIN
) {
1811 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1815 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1824 DBG2("UST app object %p disabled successfully for app: pid = %d", object
, app
->pid
);
1827 health_code_update();
1832 * Disable the specified channel on to UST tracer for the UST session.
1834 static int disable_ust_channel(struct ust_app
*app
,
1835 struct ust_app_session
*ua_sess
,
1836 struct ust_app_channel
*ua_chan
)
1840 health_code_update();
1842 pthread_mutex_lock(&app
->sock_lock
);
1843 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1844 pthread_mutex_unlock(&app
->sock_lock
);
1846 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1848 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1851 } else if (ret
== -EAGAIN
) {
1853 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1857 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1867 DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1870 health_code_update();
1875 * Enable the specified channel on to UST tracer for the UST session.
1877 static int enable_ust_channel(struct ust_app
*app
,
1878 struct ust_app_session
*ua_sess
,
1879 struct ust_app_channel
*ua_chan
)
1883 health_code_update();
1885 pthread_mutex_lock(&app
->sock_lock
);
1886 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1887 pthread_mutex_unlock(&app
->sock_lock
);
1889 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1891 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1895 } else if (ret
== -EAGAIN
) {
1897 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1902 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1912 ua_chan
->enabled
= true;
1914 DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1917 health_code_update();
1922 * Enable the specified event on to UST tracer for the UST session.
1924 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1928 health_code_update();
1930 pthread_mutex_lock(&app
->sock_lock
);
1931 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1932 pthread_mutex_unlock(&app
->sock_lock
);
1934 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1936 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1939 } else if (ret
== -EAGAIN
) {
1941 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1945 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1954 DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object
, app
->pid
);
1957 health_code_update();
1962 * Send channel and stream buffer to application.
1964 * Return 0 on success. On error, a negative value is returned.
1966 static int send_channel_pid_to_ust(struct ust_app
*app
,
1967 struct ust_app_session
*ua_sess
,
1968 struct ust_app_channel
*ua_chan
)
1971 struct ust_app_stream
*stream
, *stmp
;
1974 LTTNG_ASSERT(ua_sess
);
1975 LTTNG_ASSERT(ua_chan
);
1977 health_code_update();
1979 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
, app
->sock
);
1981 /* Send channel to the application. */
1982 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1983 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1984 ret
= -ENOTCONN
; /* Caused by app exiting. */
1986 } else if (ret
== -EAGAIN
) {
1987 /* Caused by timeout. */
1988 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
1992 ua_sess
->tracing_id
);
1993 /* Treat this the same way as an application that is exiting. */
1996 } else if (ret
< 0) {
2000 health_code_update();
2002 /* Send all streams to application. */
2003 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2004 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
2005 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2006 ret
= -ENOTCONN
; /* Caused by app exiting. */
2008 } else if (ret
== -EAGAIN
) {
2009 /* Caused by timeout. */
2010 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
2015 ua_sess
->tracing_id
);
2017 * Treat this the same way as an application that is
2021 } else if (ret
< 0) {
2024 /* We don't need the stream anymore once sent to the tracer. */
2025 cds_list_del(&stream
->list
);
2026 delete_ust_app_stream(-1, stream
, app
);
2030 health_code_update();
2035 * Create the specified event onto the UST tracer for a UST session.
2037 * Should be called with session mutex held.
2039 static int create_ust_event(struct ust_app
*app
,
2040 struct ust_app_channel
*ua_chan
,
2041 struct ust_app_event
*ua_event
)
2045 health_code_update();
2047 /* Create UST event on tracer */
2048 pthread_mutex_lock(&app
->sock_lock
);
2049 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
, &ua_event
->obj
);
2050 pthread_mutex_unlock(&app
->sock_lock
);
2052 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2054 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2057 } else if (ret
== -EAGAIN
) {
2059 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2063 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2064 ua_event
->attr
.name
,
2072 ua_event
->handle
= ua_event
->obj
->handle
;
2074 DBG2("UST app event %s created successfully for pid:%d object = %p",
2075 ua_event
->attr
.name
,
2079 health_code_update();
2081 /* Set filter if one is present. */
2082 if (ua_event
->filter
) {
2083 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2089 /* Set exclusions for the event */
2090 if (ua_event
->exclusion
) {
2091 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2097 /* If event not enabled, disable it on the tracer */
2098 if (ua_event
->enabled
) {
2100 * We now need to explicitly enable the event, since it
2101 * is now disabled at creation.
2103 ret
= enable_ust_object(app
, ua_event
->obj
);
2106 * If we hit an EPERM, something is wrong with our enable call. If
2107 * we get an EEXIST, there is a problem on the tracer side since we
2111 case -LTTNG_UST_ERR_PERM
:
2112 /* Code flow problem */
2114 case -LTTNG_UST_ERR_EXIST
:
2115 /* It's OK for our use case. */
2126 health_code_update();
2131 init_ust_event_notifier_from_event_rule(const struct lttng_event_rule
*rule
,
2132 struct lttng_ust_abi_event_notifier
*event_notifier
)
2134 enum lttng_event_rule_status status
;
2135 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2136 int loglevel
= -1, ret
= 0;
2137 const char *pattern
;
2139 memset(event_notifier
, 0, sizeof(*event_notifier
));
2141 if (lttng_event_rule_targets_agent_domain(rule
)) {
2143 * Special event for agents
2144 * The actual meat of the event is in the filter that will be
2145 * attached later on.
2146 * Set the default values for the agent event.
2148 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
2150 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2152 const struct lttng_log_level_rule
*log_level_rule
;
2154 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2155 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2157 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2158 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2159 /* At this point, this is a fatal error. */
2163 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(rule
, &log_level_rule
);
2164 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2165 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2166 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2167 enum lttng_log_level_rule_status llr_status
;
2169 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2170 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2171 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2172 llr_status
= lttng_log_level_rule_exactly_get_level(log_level_rule
,
2175 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2176 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2177 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2178 log_level_rule
, &loglevel
);
2184 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2186 /* At this point this is a fatal error. */
2191 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2192 ret
= lttng_strncpy(
2193 event_notifier
->event
.name
, pattern
, sizeof(event_notifier
->event
.name
));
2195 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern
);
2199 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2200 event_notifier
->event
.loglevel
= loglevel
;
2206 * Create the specified event notifier against the user space tracer of a
2207 * given application.
2209 static int create_ust_event_notifier(struct ust_app
*app
,
2210 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2213 enum lttng_condition_status condition_status
;
2214 const struct lttng_condition
*condition
= nullptr;
2215 struct lttng_ust_abi_event_notifier event_notifier
;
2216 const struct lttng_event_rule
*event_rule
= nullptr;
2217 unsigned int capture_bytecode_count
= 0, i
;
2218 enum lttng_condition_status cond_status
;
2219 enum lttng_event_rule_type event_rule_type
;
2221 health_code_update();
2222 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2224 condition
= lttng_trigger_get_const_condition(ua_event_notifier_rule
->trigger
);
2225 LTTNG_ASSERT(condition
);
2226 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2227 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2229 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2230 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2232 LTTNG_ASSERT(event_rule
);
2234 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2235 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2236 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2237 event_rule_type
== LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2238 event_rule_type
== LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2240 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2241 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2242 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2244 /* Create UST event notifier against the tracer. */
2245 pthread_mutex_lock(&app
->sock_lock
);
2246 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
,
2248 app
->event_notifier_group
.object
,
2249 &ua_event_notifier_rule
->obj
);
2250 pthread_mutex_unlock(&app
->sock_lock
);
2252 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2254 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2257 } else if (ret
== -EAGAIN
) {
2259 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2263 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2264 event_notifier
.event
.name
,
2272 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2274 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2275 event_notifier
.event
.name
,
2278 ua_event_notifier_rule
->obj
);
2280 health_code_update();
2282 /* Set filter if one is present. */
2283 if (ua_event_notifier_rule
->filter
) {
2284 ret
= set_ust_object_filter(
2285 app
, ua_event_notifier_rule
->filter
, ua_event_notifier_rule
->obj
);
2291 /* Set exclusions for the event. */
2292 if (ua_event_notifier_rule
->exclusion
) {
2293 ret
= set_ust_object_exclusions(
2294 app
, ua_event_notifier_rule
->exclusion
, ua_event_notifier_rule
->obj
);
2300 /* Set the capture bytecodes. */
2301 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2302 condition
, &capture_bytecode_count
);
2303 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2305 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2306 const struct lttng_bytecode
*capture_bytecode
=
2307 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2310 ret
= set_ust_capture(app
, capture_bytecode
, i
, ua_event_notifier_rule
->obj
);
2317 * We now need to explicitly enable the event, since it
2318 * is disabled at creation.
2320 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2323 * If we hit an EPERM, something is wrong with our enable call.
2324 * If we get an EEXIST, there is a problem on the tracer side
2325 * since we just created it.
2328 case -LTTNG_UST_ERR_PERM
:
2329 /* Code flow problem. */
2331 case -LTTNG_UST_ERR_EXIST
:
2332 /* It's OK for our use case. */
2342 ua_event_notifier_rule
->enabled
= true;
2345 health_code_update();
2350 * Copy data between an UST app event and a LTT event.
2352 static void shadow_copy_event(struct ust_app_event
*ua_event
, struct ltt_ust_event
*uevent
)
2354 size_t exclusion_alloc_size
;
2356 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2357 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2359 ua_event
->enabled
= uevent
->enabled
;
2361 /* Copy event attributes */
2362 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2364 /* Copy filter bytecode */
2365 if (uevent
->filter
) {
2366 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2367 /* Filter might be NULL here in case of ENONEM. */
2370 /* Copy exclusion data */
2371 if (uevent
->exclusion
) {
2372 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2373 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2374 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2375 if (ua_event
->exclusion
== nullptr) {
2378 memcpy(ua_event
->exclusion
, uevent
->exclusion
, exclusion_alloc_size
);
2384 * Copy data between an UST app channel and a LTT channel.
2386 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
, struct ltt_ust_channel
*uchan
)
2388 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2390 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2391 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2393 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2394 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2396 /* Copy event attributes since the layout is different. */
2397 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2398 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2399 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2400 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2401 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2402 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2403 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2404 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2407 * Note that the attribute channel type is not set since the channel on the
2408 * tracing registry side does not have this information.
2411 ua_chan
->enabled
= uchan
->enabled
;
2412 ua_chan
->tracing_channel_id
= uchan
->id
;
2414 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2418 * Copy data between a UST app session and a regular LTT session.
2420 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2421 struct ltt_ust_session
*usess
,
2422 struct ust_app
*app
)
2424 struct tm
*timeinfo
;
2427 char tmp_shm_path
[PATH_MAX
];
2429 timeinfo
= localtime(&app
->registration_time
);
2430 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2432 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2434 ua_sess
->tracing_id
= usess
->id
;
2435 ua_sess
->id
= get_next_session_id();
2436 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2437 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2438 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2439 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2440 ua_sess
->buffer_type
= usess
->buffer_type
;
2441 ua_sess
->bits_per_long
= app
->abi
.bits_per_long
;
2443 /* There is only one consumer object per session possible. */
2444 consumer_output_get(usess
->consumer
);
2445 ua_sess
->consumer
= usess
->consumer
;
2447 ua_sess
->output_traces
= usess
->output_traces
;
2448 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2449 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &usess
->metadata_attr
);
2451 switch (ua_sess
->buffer_type
) {
2452 case LTTNG_BUFFER_PER_PID
:
2453 ret
= snprintf(ua_sess
->path
,
2454 sizeof(ua_sess
->path
),
2455 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2460 case LTTNG_BUFFER_PER_UID
:
2461 ret
= snprintf(ua_sess
->path
,
2462 sizeof(ua_sess
->path
),
2463 DEFAULT_UST_TRACE_UID_PATH
,
2464 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2465 app
->abi
.bits_per_long
);
2472 PERROR("asprintf UST shadow copy session");
2477 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
, sizeof(ua_sess
->root_shm_path
));
2478 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2479 strncpy(ua_sess
->shm_path
, usess
->shm_path
, sizeof(ua_sess
->shm_path
));
2480 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2481 if (ua_sess
->shm_path
[0]) {
2482 switch (ua_sess
->buffer_type
) {
2483 case LTTNG_BUFFER_PER_PID
:
2484 ret
= snprintf(tmp_shm_path
,
2485 sizeof(tmp_shm_path
),
2486 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2491 case LTTNG_BUFFER_PER_UID
:
2492 ret
= snprintf(tmp_shm_path
,
2493 sizeof(tmp_shm_path
),
2494 "/" DEFAULT_UST_TRACE_UID_PATH
,
2496 app
->abi
.bits_per_long
);
2503 PERROR("sprintf UST shadow copy session");
2507 strncat(ua_sess
->shm_path
,
2509 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2510 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2515 consumer_output_put(ua_sess
->consumer
);
2519 * Lookup sesison wrapper.
2521 static void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2522 struct ust_app
*app
,
2523 struct lttng_ht_iter
*iter
)
2525 /* Get right UST app session from app */
2526 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2530 * Return ust app session from the app session hashtable using the UST session
2533 static struct ust_app_session
*lookup_session_by_app(const struct ltt_ust_session
*usess
,
2534 struct ust_app
*app
)
2536 struct lttng_ht_iter iter
;
2537 struct lttng_ht_node_u64
*node
;
2539 __lookup_session_by_app(usess
, app
, &iter
);
2540 node
= lttng_ht_iter_get_node_u64(&iter
);
2541 if (node
== nullptr) {
2545 return lttng::utils::container_of(node
, &ust_app_session::node
);
2552 * Setup buffer registry per PID for the given session and application. If none
2553 * is found, a new one is created, added to the global registry and
2554 * initialized. If regp is valid, it's set with the newly created object.
2556 * Return 0 on success or else a negative value.
2558 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2559 struct ust_app
*app
,
2560 struct buffer_reg_pid
**regp
)
2563 struct buffer_reg_pid
*reg_pid
;
2565 LTTNG_ASSERT(ua_sess
);
2568 lttng::urcu::read_lock_guard read_lock
;
2570 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2573 * This is the create channel path meaning that if there is NO
2574 * registry available, we have to create one for this session.
2576 ret
= buffer_reg_pid_create(
2577 ua_sess
->id
, ®_pid
, ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2585 /* Initialize registry. */
2586 reg_pid
->registry
->reg
.ust
= ust_registry_session_per_pid_create(
2591 reg_pid
->root_shm_path
,
2593 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2594 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2595 ua_sess
->tracing_id
);
2596 if (!reg_pid
->registry
->reg
.ust
) {
2598 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2599 * destroy the buffer registry, because it is always expected
2600 * that if the buffer registry can be found, its ust registry is
2603 buffer_reg_pid_destroy(reg_pid
);
2607 buffer_reg_pid_add(reg_pid
);
2609 DBG3("UST app buffer registry per PID created successfully");
2620 * Setup buffer registry per UID for the given session and application. If none
2621 * is found, a new one is created, added to the global registry and
2622 * initialized. If regp is valid, it's set with the newly created object.
2624 * Return 0 on success or else a negative value.
2626 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2627 struct ust_app_session
*ua_sess
,
2628 struct ust_app
*app
,
2629 struct buffer_reg_uid
**regp
)
2632 struct buffer_reg_uid
*reg_uid
;
2634 LTTNG_ASSERT(usess
);
2637 lttng::urcu::read_lock_guard read_lock
;
2639 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
2642 * This is the create channel path meaning that if there is NO
2643 * registry available, we have to create one for this session.
2645 ret
= buffer_reg_uid_create(usess
->id
,
2646 app
->abi
.bits_per_long
,
2650 ua_sess
->root_shm_path
,
2659 /* Initialize registry. */
2660 reg_uid
->registry
->reg
.ust
= ust_registry_session_per_uid_create(app
->abi
,
2663 reg_uid
->root_shm_path
,
2667 ua_sess
->tracing_id
,
2669 if (!reg_uid
->registry
->reg
.ust
) {
2671 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2672 * destroy the buffer registry, because it is always expected
2673 * that if the buffer registry can be found, its ust registry is
2676 buffer_reg_uid_destroy(reg_uid
, nullptr);
2680 /* Add node to teardown list of the session. */
2681 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2683 buffer_reg_uid_add(reg_uid
);
2685 DBG3("UST app buffer registry per UID created successfully");
2695 * Create a session on the tracer side for the given app.
2697 * On success, ua_sess_ptr is populated with the session pointer or else left
2698 * untouched. If the session was created, is_created is set to 1. On error,
2699 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2702 * Returns 0 on success or else a negative code which is either -ENOMEM or
2703 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2705 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2706 struct ust_app
*app
,
2707 struct ust_app_session
**ua_sess_ptr
,
2710 int ret
, created
= 0;
2711 struct ust_app_session
*ua_sess
;
2713 LTTNG_ASSERT(usess
);
2715 LTTNG_ASSERT(ua_sess_ptr
);
2717 health_code_update();
2719 ua_sess
= lookup_session_by_app(usess
, app
);
2720 if (ua_sess
== nullptr) {
2721 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2724 ua_sess
= alloc_ust_app_session();
2725 if (ua_sess
== nullptr) {
2726 /* Only malloc can failed so something is really wrong */
2730 shadow_copy_session(ua_sess
, usess
, app
);
2734 switch (usess
->buffer_type
) {
2735 case LTTNG_BUFFER_PER_PID
:
2736 /* Init local registry. */
2737 ret
= setup_buffer_reg_pid(ua_sess
, app
, nullptr);
2739 delete_ust_app_session(-1, ua_sess
, app
);
2743 case LTTNG_BUFFER_PER_UID
:
2744 /* Look for a global registry. If none exists, create one. */
2745 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, nullptr);
2747 delete_ust_app_session(-1, ua_sess
, app
);
2757 health_code_update();
2759 if (ua_sess
->handle
== -1) {
2760 pthread_mutex_lock(&app
->sock_lock
);
2761 ret
= lttng_ust_ctl_create_session(app
->sock
);
2762 pthread_mutex_unlock(&app
->sock_lock
);
2764 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2765 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2769 } else if (ret
== -EAGAIN
) {
2770 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2775 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2780 delete_ust_app_session(-1, ua_sess
, app
);
2781 if (ret
!= -ENOMEM
) {
2783 * Tracer is probably gone or got an internal error so let's
2784 * behave like it will soon unregister or not usable.
2791 ua_sess
->handle
= ret
;
2793 /* Add ust app session to app's HT */
2794 lttng_ht_node_init_u64(&ua_sess
->node
, ua_sess
->tracing_id
);
2795 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2796 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2797 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
, &ua_sess
->ust_objd_node
);
2799 DBG2("UST app session created successfully with handle %d", ret
);
2802 *ua_sess_ptr
= ua_sess
;
2804 *is_created
= created
;
2807 /* Everything went well. */
2811 health_code_update();
2816 * Match function for a hash table lookup of ust_app_ctx.
2818 * It matches an ust app context based on the context type and, in the case
2819 * of perf counters, their name.
2821 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2823 struct ust_app_ctx
*ctx
;
2824 const struct lttng_ust_context_attr
*key
;
2829 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2830 key
= (lttng_ust_context_attr
*) _key
;
2833 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2838 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2839 if (strncmp(key
->u
.perf_counter
.name
,
2840 ctx
->ctx
.u
.perf_counter
.name
,
2841 sizeof(key
->u
.perf_counter
.name
)) != 0) {
2845 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2846 if (strcmp(key
->u
.app_ctx
.provider_name
, ctx
->ctx
.u
.app_ctx
.provider_name
) != 0 ||
2847 strcmp(key
->u
.app_ctx
.ctx_name
, ctx
->ctx
.u
.app_ctx
.ctx_name
) != 0) {
2863 * Lookup for an ust app context from an lttng_ust_context.
2865 * Must be called while holding RCU read side lock.
2866 * Return an ust_app_ctx object or NULL on error.
2868 static struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2869 struct lttng_ust_context_attr
*uctx
)
2871 struct lttng_ht_iter iter
;
2872 struct lttng_ht_node_ulong
*node
;
2873 struct ust_app_ctx
*app_ctx
= nullptr;
2877 ASSERT_RCU_READ_LOCKED();
2879 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2880 cds_lfht_lookup(ht
->ht
,
2881 ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2882 ht_match_ust_app_ctx
,
2885 node
= lttng_ht_iter_get_node_ulong(&iter
);
2890 app_ctx
= lttng::utils::container_of(node
, &ust_app_ctx::node
);
2897 * Create a context for the channel on the tracer.
2899 * Called with UST app session lock held and a RCU read side lock.
2901 static int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2902 struct lttng_ust_context_attr
*uctx
,
2903 struct ust_app
*app
)
2906 struct ust_app_ctx
*ua_ctx
;
2908 ASSERT_RCU_READ_LOCKED();
2910 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2912 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2918 ua_ctx
= alloc_ust_app_ctx(uctx
);
2919 if (ua_ctx
== nullptr) {
2925 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2926 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2927 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2929 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2939 * Enable on the tracer side a ust app event for the session and channel.
2941 * Called with UST app session lock held.
2943 static int enable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2947 ret
= enable_ust_object(app
, ua_event
->obj
);
2952 ua_event
->enabled
= true;
2959 * Disable on the tracer side a ust app event for the session and channel.
2961 static int disable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2965 ret
= disable_ust_object(app
, ua_event
->obj
);
2970 ua_event
->enabled
= false;
2977 * Lookup ust app channel for session and disable it on the tracer side.
2979 static int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2980 struct ust_app_channel
*ua_chan
,
2981 struct ust_app
*app
)
2985 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2990 ua_chan
->enabled
= false;
2997 * Lookup ust app channel for session and enable it on the tracer side. This
2998 * MUST be called with a RCU read side lock acquired.
3000 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
3001 struct ltt_ust_channel
*uchan
,
3002 struct ust_app
*app
)
3005 struct lttng_ht_iter iter
;
3006 struct lttng_ht_node_str
*ua_chan_node
;
3007 struct ust_app_channel
*ua_chan
;
3009 ASSERT_RCU_READ_LOCKED();
3011 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3012 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3013 if (ua_chan_node
== nullptr) {
3014 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
3016 ua_sess
->tracing_id
);
3020 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3022 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
3032 * Ask the consumer to create a channel and get it if successful.
3034 * Called with UST app session lock held.
3036 * Return 0 on success or else a negative value.
3038 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
3039 struct ust_app_session
*ua_sess
,
3040 struct ust_app_channel
*ua_chan
,
3042 lsu::registry_session
*registry
)
3045 unsigned int nb_fd
= 0;
3046 struct consumer_socket
*socket
;
3048 LTTNG_ASSERT(usess
);
3049 LTTNG_ASSERT(ua_sess
);
3050 LTTNG_ASSERT(ua_chan
);
3051 LTTNG_ASSERT(registry
);
3053 lttng::urcu::read_lock_guard read_lock
;
3054 health_code_update();
3056 /* Get the right consumer socket for the application. */
3057 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3063 health_code_update();
3065 /* Need one fd for the channel. */
3066 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3068 ERR("Exhausted number of available FD upon create channel");
3073 * Ask consumer to create channel. The consumer will return the number of
3074 * stream we have to expect.
3076 ret
= ust_consumer_ask_channel(
3077 ua_sess
, ua_chan
, usess
->consumer
, socket
, registry
, usess
->current_trace_chunk
);
3083 * Compute the number of fd needed before receiving them. It must be 2 per
3084 * stream (2 being the default value here).
3086 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3088 /* Reserve the amount of file descriptor we need. */
3089 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3091 ERR("Exhausted number of available FD upon create channel");
3092 goto error_fd_get_stream
;
3095 health_code_update();
3098 * Now get the channel from the consumer. This call will populate the stream
3099 * list of that channel and set the ust objects.
3101 if (usess
->consumer
->enabled
) {
3102 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3111 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3112 error_fd_get_stream
:
3114 * Initiate a destroy channel on the consumer since we had an error
3115 * handling it on our side. The return value is of no importance since we
3116 * already have a ret value set by the previous error that we need to
3119 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3121 lttng_fd_put(LTTNG_FD_APPS
, 1);
3123 health_code_update();
3128 * Duplicate the ust data object of the ust app stream and save it in the
3129 * buffer registry stream.
3131 * Return 0 on success or else a negative value.
3133 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3134 struct ust_app_stream
*stream
)
3138 LTTNG_ASSERT(reg_stream
);
3139 LTTNG_ASSERT(stream
);
3141 /* Duplicating a stream requires 2 new fds. Reserve them. */
3142 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3144 ERR("Exhausted number of available FD upon duplicate stream");
3148 /* Duplicate object for stream once the original is in the registry. */
3149 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
, reg_stream
->obj
.ust
);
3151 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3152 reg_stream
->obj
.ust
,
3155 lttng_fd_put(LTTNG_FD_APPS
, 2);
3158 stream
->handle
= stream
->obj
->handle
;
3165 * Duplicate the ust data object of the ust app. channel and save it in the
3166 * buffer registry channel.
3168 * Return 0 on success or else a negative value.
3170 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3171 struct ust_app_channel
*ua_chan
)
3175 LTTNG_ASSERT(buf_reg_chan
);
3176 LTTNG_ASSERT(ua_chan
);
3178 /* Duplicating a channel requires 1 new fd. Reserve it. */
3179 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3181 ERR("Exhausted number of available FD upon duplicate channel");
3185 /* Duplicate object for stream once the original is in the registry. */
3186 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3188 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3189 buf_reg_chan
->obj
.ust
,
3194 ua_chan
->handle
= ua_chan
->obj
->handle
;
3199 lttng_fd_put(LTTNG_FD_APPS
, 1);
3205 * For a given channel buffer registry, setup all streams of the given ust
3206 * application channel.
3208 * Return 0 on success or else a negative value.
3210 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3211 struct ust_app_channel
*ua_chan
,
3212 struct ust_app
*app
)
3215 struct ust_app_stream
*stream
, *stmp
;
3217 LTTNG_ASSERT(buf_reg_chan
);
3218 LTTNG_ASSERT(ua_chan
);
3220 DBG2("UST app setup buffer registry stream");
3222 /* Send all streams to application. */
3223 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3224 struct buffer_reg_stream
*reg_stream
;
3226 ret
= buffer_reg_stream_create(®_stream
);
3232 * Keep original pointer and nullify it in the stream so the delete
3233 * stream call does not release the object.
3235 reg_stream
->obj
.ust
= stream
->obj
;
3236 stream
->obj
= nullptr;
3237 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3239 /* We don't need the streams anymore. */
3240 cds_list_del(&stream
->list
);
3241 delete_ust_app_stream(-1, stream
, app
);
3249 * Create a buffer registry channel for the given session registry and
3250 * application channel object. If regp pointer is valid, it's set with the
3251 * created object. Important, the created object is NOT added to the session
3252 * registry hash table.
3254 * Return 0 on success else a negative value.
3256 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3257 struct ust_app_channel
*ua_chan
,
3258 struct buffer_reg_channel
**regp
)
3261 struct buffer_reg_channel
*buf_reg_chan
= nullptr;
3263 LTTNG_ASSERT(reg_sess
);
3264 LTTNG_ASSERT(ua_chan
);
3266 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3268 /* Create buffer registry channel. */
3269 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3273 LTTNG_ASSERT(buf_reg_chan
);
3274 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3275 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3276 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3278 /* Create and add a channel registry to session. */
3280 reg_sess
->reg
.ust
->add_channel(ua_chan
->tracing_channel_id
);
3281 } catch (const std::exception
& ex
) {
3282 ERR("Failed to add a channel registry to userspace registry session: %s",
3288 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3291 *regp
= buf_reg_chan
;
3297 /* Safe because the registry channel object was not added to any HT. */
3298 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3304 * Setup buffer registry channel for the given session registry and application
3305 * channel object. If regp pointer is valid, it's set with the created object.
3307 * Return 0 on success else a negative value.
3309 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3310 struct ust_app_channel
*ua_chan
,
3311 struct buffer_reg_channel
*buf_reg_chan
,
3312 struct ust_app
*app
)
3316 LTTNG_ASSERT(reg_sess
);
3317 LTTNG_ASSERT(buf_reg_chan
);
3318 LTTNG_ASSERT(ua_chan
);
3319 LTTNG_ASSERT(ua_chan
->obj
);
3321 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3323 /* Setup all streams for the registry. */
3324 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3329 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3330 ua_chan
->obj
= nullptr;
3335 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3336 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3341 * Send buffer registry channel to the application.
3343 * Return 0 on success else a negative value.
3345 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3346 struct ust_app
*app
,
3347 struct ust_app_session
*ua_sess
,
3348 struct ust_app_channel
*ua_chan
)
3351 struct buffer_reg_stream
*reg_stream
;
3353 LTTNG_ASSERT(buf_reg_chan
);
3355 LTTNG_ASSERT(ua_sess
);
3356 LTTNG_ASSERT(ua_chan
);
3358 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3360 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3365 /* Send channel to the application. */
3366 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3367 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3368 ret
= -ENOTCONN
; /* Caused by app exiting. */
3370 } else if (ret
== -EAGAIN
) {
3371 /* Caused by timeout. */
3372 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
3376 ua_sess
->tracing_id
);
3377 /* Treat this the same way as an application that is exiting. */
3380 } else if (ret
< 0) {
3384 health_code_update();
3386 /* Send all streams to application. */
3387 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3388 cds_list_for_each_entry (reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3389 struct ust_app_stream stream
= {};
3391 ret
= duplicate_stream_object(reg_stream
, &stream
);
3393 goto error_stream_unlock
;
3396 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3398 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3399 ret
= -ENOTCONN
; /* Caused by app exiting. */
3400 } else if (ret
== -EAGAIN
) {
3402 * Caused by timeout.
3403 * Treat this the same way as an application
3406 WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64
3410 ua_sess
->tracing_id
);
3413 (void) release_ust_app_stream(-1, &stream
, app
);
3414 goto error_stream_unlock
;
3418 * The return value is not important here. This function will output an
3421 (void) release_ust_app_stream(-1, &stream
, app
);
3424 error_stream_unlock
:
3425 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3431 * Create and send to the application the created buffers with per UID buffers.
3433 * This MUST be called with a RCU read side lock acquired.
3434 * The session list lock and the session's lock must be acquired.
3436 * Return 0 on success else a negative value.
3438 static int create_channel_per_uid(struct ust_app
*app
,
3439 struct ltt_ust_session
*usess
,
3440 struct ust_app_session
*ua_sess
,
3441 struct ust_app_channel
*ua_chan
)
3444 struct buffer_reg_uid
*reg_uid
;
3445 struct buffer_reg_channel
*buf_reg_chan
;
3446 struct ltt_session
*session
= nullptr;
3447 enum lttng_error_code notification_ret
;
3450 LTTNG_ASSERT(usess
);
3451 LTTNG_ASSERT(ua_sess
);
3452 LTTNG_ASSERT(ua_chan
);
3453 ASSERT_RCU_READ_LOCKED();
3455 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3457 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
3459 * The session creation handles the creation of this global registry
3460 * object. If none can be find, there is a code flow problem or a
3463 LTTNG_ASSERT(reg_uid
);
3465 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
, reg_uid
);
3470 /* Create the buffer registry channel object. */
3471 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3473 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan
->name
);
3477 session
= session_find_by_id(ua_sess
->tracing_id
);
3478 LTTNG_ASSERT(session
);
3479 ASSERT_LOCKED(session
->lock
);
3480 ASSERT_SESSION_LIST_LOCKED();
3483 * Create the buffers on the consumer side. This call populates the
3484 * ust app channel object with all streams and data object.
3486 ret
= do_consumer_create_channel(
3487 usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, reg_uid
->registry
->reg
.ust
);
3489 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3492 * Let's remove the previously created buffer registry channel so
3493 * it's not visible anymore in the session registry.
3495 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3497 locked_registry
->remove_channel(ua_chan
->tracing_channel_id
, false);
3498 } catch (const std::exception
& ex
) {
3499 DBG("Could not find channel for removal: %s", ex
.what());
3501 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3502 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3507 * Setup the streams and add it to the session registry.
3509 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, buf_reg_chan
, app
);
3511 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3516 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3517 auto& ust_reg_chan
= locked_registry
->channel(ua_chan
->tracing_channel_id
);
3519 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3522 /* Notify the notification subsystem of the channel's creation. */
3523 notification_ret
= notification_thread_command_add_channel(
3524 the_notification_thread_handle
,
3529 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3530 if (notification_ret
!= LTTNG_OK
) {
3531 ret
= -(int) notification_ret
;
3532 ERR("Failed to add channel to notification thread");
3537 /* Send buffers to the application. */
3538 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3540 if (ret
!= -ENOTCONN
) {
3541 ERR("Error sending channel to application");
3548 session_put(session
);
3554 * Create and send to the application the created buffers with per PID buffers.
3556 * Called with UST app session lock held.
3557 * The session list lock and the session's lock must be acquired.
3559 * Return 0 on success else a negative value.
3561 static int create_channel_per_pid(struct ust_app
*app
,
3562 struct ltt_ust_session
*usess
,
3563 struct ust_app_session
*ua_sess
,
3564 struct ust_app_channel
*ua_chan
)
3567 lsu::registry_session
*registry
;
3568 enum lttng_error_code cmd_ret
;
3569 struct ltt_session
*session
= nullptr;
3570 uint64_t chan_reg_key
;
3573 LTTNG_ASSERT(usess
);
3574 LTTNG_ASSERT(ua_sess
);
3575 LTTNG_ASSERT(ua_chan
);
3577 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3579 lttng::urcu::read_lock_guard read_lock
;
3581 registry
= get_session_registry(ua_sess
);
3582 /* The UST app session lock is held, registry shall not be null. */
3583 LTTNG_ASSERT(registry
);
3585 /* Create and add a new channel registry to session. */
3587 registry
->add_channel(ua_chan
->key
);
3588 } catch (const std::exception
& ex
) {
3589 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3596 session
= session_find_by_id(ua_sess
->tracing_id
);
3597 LTTNG_ASSERT(session
);
3598 ASSERT_LOCKED(session
->lock
);
3599 ASSERT_SESSION_LIST_LOCKED();
3601 /* Create and get channel on the consumer side. */
3602 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, registry
);
3604 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3605 goto error_remove_from_registry
;
3608 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3610 if (ret
!= -ENOTCONN
) {
3611 ERR("Error sending channel to application");
3613 goto error_remove_from_registry
;
3616 chan_reg_key
= ua_chan
->key
;
3618 auto locked_registry
= registry
->lock();
3620 auto& ust_reg_chan
= locked_registry
->channel(chan_reg_key
);
3621 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3624 cmd_ret
= notification_thread_command_add_channel(the_notification_thread_handle
,
3629 ua_chan
->attr
.subbuf_size
*
3630 ua_chan
->attr
.num_subbuf
);
3631 if (cmd_ret
!= LTTNG_OK
) {
3632 ret
= -(int) cmd_ret
;
3633 ERR("Failed to add channel to notification thread");
3634 goto error_remove_from_registry
;
3637 error_remove_from_registry
:
3640 auto locked_registry
= registry
->lock();
3641 locked_registry
->remove_channel(ua_chan
->key
, false);
3642 } catch (const std::exception
& ex
) {
3643 DBG("Could not find channel for removal: %s", ex
.what());
3648 session_put(session
);
3654 * From an already allocated ust app channel, create the channel buffers if
3655 * needed and send them to the application. This MUST be called with a RCU read
3656 * side lock acquired.
3658 * Called with UST app session lock held.
3660 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3661 * the application exited concurrently.
3663 static int ust_app_channel_send(struct ust_app
*app
,
3664 struct ltt_ust_session
*usess
,
3665 struct ust_app_session
*ua_sess
,
3666 struct ust_app_channel
*ua_chan
)
3671 LTTNG_ASSERT(usess
);
3672 LTTNG_ASSERT(usess
->active
);
3673 LTTNG_ASSERT(ua_sess
);
3674 LTTNG_ASSERT(ua_chan
);
3675 ASSERT_RCU_READ_LOCKED();
3677 /* Handle buffer type before sending the channel to the application. */
3678 switch (usess
->buffer_type
) {
3679 case LTTNG_BUFFER_PER_UID
:
3681 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3687 case LTTNG_BUFFER_PER_PID
:
3689 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3701 /* Initialize ust objd object using the received handle and add it. */
3702 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3703 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3705 /* If channel is not enabled, disable it on the tracer */
3706 if (!ua_chan
->enabled
) {
3707 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3718 * Create UST app channel and return it through ua_chanp if not NULL.
3720 * Called with UST app session lock and RCU read-side lock held.
3722 * Return 0 on success or else a negative value.
3724 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3725 struct ltt_ust_channel
*uchan
,
3726 enum lttng_ust_abi_chan_type type
,
3727 struct ltt_ust_session
*usess
__attribute__((unused
)),
3728 struct ust_app_channel
**ua_chanp
)
3731 struct lttng_ht_iter iter
;
3732 struct lttng_ht_node_str
*ua_chan_node
;
3733 struct ust_app_channel
*ua_chan
;
3735 ASSERT_RCU_READ_LOCKED();
3737 /* Lookup channel in the ust app session */
3738 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3739 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3740 if (ua_chan_node
!= nullptr) {
3741 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3745 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3746 if (ua_chan
== nullptr) {
3747 /* Only malloc can fail here */
3751 shadow_copy_channel(ua_chan
, uchan
);
3753 /* Set channel type. */
3754 ua_chan
->attr
.type
= type
;
3756 /* Only add the channel if successful on the tracer side. */
3757 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3760 *ua_chanp
= ua_chan
;
3763 /* Everything went well. */
3771 * Create UST app event and create it on the tracer side.
3773 * Must be called with the RCU read side lock held.
3774 * Called with ust app session mutex held.
3776 static int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3777 struct ltt_ust_event
*uevent
,
3778 struct ust_app
*app
)
3781 struct ust_app_event
*ua_event
;
3783 ASSERT_RCU_READ_LOCKED();
3785 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3786 if (ua_event
== nullptr) {
3787 /* Only failure mode of alloc_ust_app_event(). */
3791 shadow_copy_event(ua_event
, uevent
);
3793 /* Create it on the tracer side */
3794 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3797 * Not found previously means that it does not exist on the
3798 * tracer. If the application reports that the event existed,
3799 * it means there is a bug in the sessiond or lttng-ust
3800 * (or corruption, etc.)
3802 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3803 ERR("Tracer for application reported that an event being created already existed: "
3804 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3814 add_unique_ust_app_event(ua_chan
, ua_event
);
3816 DBG2("UST app create event completed: app = '%s' pid = %d", app
->name
, app
->pid
);
3822 /* Valid. Calling here is already in a read side lock */
3823 delete_ust_app_event(-1, ua_event
, app
);
3828 * Create UST app event notifier rule and create it on the tracer side.
3830 * Must be called with the RCU read side lock held.
3831 * Called with ust app session mutex held.
3833 static int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
, struct ust_app
*app
)
3836 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3838 ASSERT_RCU_READ_LOCKED();
3840 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3841 if (ua_event_notifier_rule
== nullptr) {
3846 /* Create it on the tracer side. */
3847 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3850 * Not found previously means that it does not exist on the
3851 * tracer. If the application reports that the event existed,
3852 * it means there is a bug in the sessiond or lttng-ust
3853 * (or corruption, etc.)
3855 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3856 ERR("Tracer for application reported that an event notifier being created already exists: "
3857 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3858 lttng_trigger_get_tracer_token(trigger
),
3867 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3868 &ua_event_notifier_rule
->node
);
3870 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3873 lttng_trigger_get_tracer_token(trigger
));
3878 /* The RCU read side lock is already being held by the caller. */
3879 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3885 * Create UST metadata and open it on the tracer side.
3887 * Called with UST app session lock held and RCU read side lock.
3889 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3890 struct ust_app
*app
,
3891 struct consumer_output
*consumer
)
3894 struct ust_app_channel
*metadata
;
3895 struct consumer_socket
*socket
;
3896 struct ltt_session
*session
= nullptr;
3898 LTTNG_ASSERT(ua_sess
);
3900 LTTNG_ASSERT(consumer
);
3901 ASSERT_RCU_READ_LOCKED();
3903 auto locked_registry
= get_locked_session_registry(ua_sess
);
3904 /* The UST app session is held registry shall not be null. */
3905 LTTNG_ASSERT(locked_registry
);
3907 /* Metadata already exists for this registry or it was closed previously */
3908 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3913 /* Allocate UST metadata */
3914 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, nullptr);
3916 /* malloc() failed */
3921 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3923 /* Need one fd for the channel. */
3924 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3926 ERR("Exhausted number of available FD upon create metadata");
3930 /* Get the right consumer socket for the application. */
3931 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3934 goto error_consumer
;
3938 * Keep metadata key so we can identify it on the consumer side. Assign it
3939 * to the registry *before* we ask the consumer so we avoid the race of the
3940 * consumer requesting the metadata and the ask_channel call on our side
3941 * did not returned yet.
3943 locked_registry
->_metadata_key
= metadata
->key
;
3945 session
= session_find_by_id(ua_sess
->tracing_id
);
3946 LTTNG_ASSERT(session
);
3947 ASSERT_LOCKED(session
->lock
);
3948 ASSERT_SESSION_LIST_LOCKED();
3951 * Ask the metadata channel creation to the consumer. The metadata object
3952 * will be created by the consumer and kept their. However, the stream is
3953 * never added or monitored until we do a first push metadata to the
3956 ret
= ust_consumer_ask_channel(ua_sess
,
3960 locked_registry
.get(),
3961 session
->current_trace_chunk
);
3963 /* Nullify the metadata key so we don't try to close it later on. */
3964 locked_registry
->_metadata_key
= 0;
3965 goto error_consumer
;
3969 * The setup command will make the metadata stream be sent to the relayd,
3970 * if applicable, and the thread managing the metadatas. This is important
3971 * because after this point, if an error occurs, the only way the stream
3972 * can be deleted is to be monitored in the consumer.
3974 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3976 /* Nullify the metadata key so we don't try to close it later on. */
3977 locked_registry
->_metadata_key
= 0;
3978 goto error_consumer
;
3981 DBG2("UST metadata with key %" PRIu64
" created for app pid %d", metadata
->key
, app
->pid
);
3984 lttng_fd_put(LTTNG_FD_APPS
, 1);
3985 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3988 session_put(session
);
3994 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3995 * acquired before calling this function.
3997 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3999 struct ust_app
*app
= nullptr;
4000 struct lttng_ht_node_ulong
*node
;
4001 struct lttng_ht_iter iter
;
4003 lttng_ht_lookup(ust_app_ht
, (void *) ((unsigned long) pid
), &iter
);
4004 node
= lttng_ht_iter_get_node_ulong(&iter
);
4005 if (node
== nullptr) {
4006 DBG2("UST app no found with pid %d", pid
);
4010 DBG2("Found UST app by pid %d", pid
);
4012 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
4019 * Allocate and init an UST app object using the registration information and
4020 * the command socket. This is called when the command socket connects to the
4023 * The object is returned on success or else NULL.
4025 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
4028 struct ust_app
*lta
= nullptr;
4029 struct lttng_pipe
*event_notifier_event_source_pipe
= nullptr;
4032 LTTNG_ASSERT(sock
>= 0);
4034 DBG3("UST app creating application for socket %d", sock
);
4036 if ((msg
->bits_per_long
== 64 && (uatomic_read(&the_ust_consumerd64_fd
) == -EINVAL
)) ||
4037 (msg
->bits_per_long
== 32 && (uatomic_read(&the_ust_consumerd32_fd
) == -EINVAL
))) {
4038 ERR("Registration failed: application \"%s\" (pid: %d) has "
4039 "%d-bit long, but no consumerd for this size is available.\n",
4042 msg
->bits_per_long
);
4047 * Reserve the two file descriptors of the event source pipe. The write
4048 * end will be closed once it is passed to the application, at which
4049 * point a single 'put' will be performed.
4051 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
4053 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4059 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
4060 if (!event_notifier_event_source_pipe
) {
4061 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4067 lta
= zmalloc
<ust_app
>();
4068 if (lta
== nullptr) {
4070 goto error_free_pipe
;
4073 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4075 lta
->ppid
= msg
->ppid
;
4076 lta
->uid
= msg
->uid
;
4077 lta
->gid
= msg
->gid
;
4080 .bits_per_long
= msg
->bits_per_long
,
4081 .long_alignment
= msg
->long_alignment
,
4082 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4083 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4084 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4085 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4086 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4087 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4088 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4091 lta
->v_major
= msg
->major
;
4092 lta
->v_minor
= msg
->minor
;
4093 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4094 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4095 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4096 lta
->notify_sock
= -1;
4097 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4099 /* Copy name and make sure it's NULL terminated. */
4100 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4101 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4104 * Before this can be called, when receiving the registration information,
4105 * the application compatibility is checked. So, at this point, the
4106 * application can work with this session daemon.
4108 lta
->compatible
= 1;
4110 lta
->pid
= msg
->pid
;
4111 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4113 pthread_mutex_init(<a
->sock_lock
, nullptr);
4114 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4116 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4120 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4121 lttng_fd_put(LTTNG_FD_APPS
, 2);
4127 * For a given application object, add it to every hash table.
4129 void ust_app_add(struct ust_app
*app
)
4132 LTTNG_ASSERT(app
->notify_sock
>= 0);
4134 app
->registration_time
= time(nullptr);
4136 lttng::urcu::read_lock_guard read_lock
;
4139 * On a re-registration, we want to kick out the previous registration of
4142 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4145 * The socket _should_ be unique until _we_ call close. So, a add_unique
4146 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4147 * already in the table.
4149 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4151 /* Add application to the notify socket hash table. */
4152 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4153 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4155 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4156 "notify_sock =%d (version %d.%d)",
4169 * Set the application version into the object.
4171 * Return 0 on success else a negative value either an errno code or a
4172 * LTTng-UST error code.
4174 int ust_app_version(struct ust_app
*app
)
4180 pthread_mutex_lock(&app
->sock_lock
);
4181 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4182 pthread_mutex_unlock(&app
->sock_lock
);
4184 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4185 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4188 } else if (ret
== -EAGAIN
) {
4189 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4193 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4203 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4205 return app
->v_major
>= 9;
4208 bool ust_app_supports_counters(const struct ust_app
*app
)
4210 return app
->v_major
>= 9;
4214 * Setup the base event notifier group.
4216 * Return 0 on success else a negative value either an errno code or a
4217 * LTTng-UST error code.
4219 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4222 int event_pipe_write_fd
;
4223 struct lttng_ust_abi_object_data
*event_notifier_group
= nullptr;
4224 enum lttng_error_code lttng_ret
;
4225 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4229 if (!ust_app_supports_notifiers(app
)) {
4234 /* Get the write side of the pipe. */
4235 event_pipe_write_fd
= lttng_pipe_get_writefd(app
->event_notifier_group
.event_pipe
);
4237 pthread_mutex_lock(&app
->sock_lock
);
4238 ret
= lttng_ust_ctl_create_event_notifier_group(
4239 app
->sock
, event_pipe_write_fd
, &event_notifier_group
);
4240 pthread_mutex_unlock(&app
->sock_lock
);
4242 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4244 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4247 } else if (ret
== -EAGAIN
) {
4249 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4253 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4257 event_pipe_write_fd
);
4262 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4264 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4271 * Release the file descriptor that was reserved for the write-end of
4274 lttng_fd_put(LTTNG_FD_APPS
, 1);
4276 lttng_ret
= notification_thread_command_add_tracer_event_source(
4277 the_notification_thread_handle
,
4278 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4280 if (lttng_ret
!= LTTNG_OK
) {
4281 ERR("Failed to add tracer event source to notification thread");
4286 /* Assign handle only when the complete setup is valid. */
4287 app
->event_notifier_group
.object
= event_notifier_group
;
4289 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
4290 switch (event_notifier_error_accounting_status
) {
4291 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4293 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4294 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",
4299 goto error_accounting
;
4300 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4301 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4306 goto error_accounting
;
4308 ERR("Failed to setup event notifier error accounting for app");
4310 goto error_accounting
;
4316 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4317 the_notification_thread_handle
,
4318 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
));
4319 if (lttng_ret
!= LTTNG_OK
) {
4320 ERR("Failed to remove application tracer event source from notification thread");
4324 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4325 free(app
->event_notifier_group
.object
);
4326 app
->event_notifier_group
.object
= nullptr;
4331 * Unregister app by removing it from the global traceable app list and freeing
4334 * The socket is already closed at this point so no close to sock.
4336 void ust_app_unregister(int sock
)
4338 struct ust_app
*lta
;
4339 struct lttng_ht_node_ulong
*node
;
4340 struct lttng_ht_iter ust_app_sock_iter
;
4341 struct lttng_ht_iter iter
;
4342 struct ust_app_session
*ua_sess
;
4345 lttng::urcu::read_lock_guard read_lock
;
4347 /* Get the node reference for a call_rcu */
4348 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &ust_app_sock_iter
);
4349 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4352 lta
= lttng::utils::container_of(node
, &ust_app::sock_n
);
4353 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4356 * For per-PID buffers, perform "push metadata" and flush all
4357 * application streams before removing app from hash tables,
4358 * ensuring proper behavior of data_pending check.
4359 * Remove sessions so they are not visible during deletion.
4361 cds_lfht_for_each_entry (lta
->sessions
->ht
, &iter
.iter
, ua_sess
, node
.node
) {
4362 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4364 /* The session was already removed so scheduled for teardown. */
4368 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4369 (void) ust_app_flush_app_session(lta
, ua_sess
);
4373 * Add session to list for teardown. This is safe since at this point we
4374 * are the only one using this list.
4376 pthread_mutex_lock(&ua_sess
->lock
);
4378 if (ua_sess
->deleted
) {
4379 pthread_mutex_unlock(&ua_sess
->lock
);
4384 * Normally, this is done in the delete session process which is
4385 * executed in the call rcu below. However, upon registration we can't
4386 * afford to wait for the grace period before pushing data or else the
4387 * data pending feature can race between the unregistration and stop
4388 * command where the data pending command is sent *before* the grace
4391 * The close metadata below nullifies the metadata pointer in the
4392 * session so the delete session will NOT push/close a second time.
4394 auto locked_registry
= get_locked_session_registry(ua_sess
);
4395 if (locked_registry
) {
4396 /* Push metadata for application before freeing the application. */
4397 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4400 * Don't ask to close metadata for global per UID buffers. Close
4401 * metadata only on destroy trace session in this case. Also, the
4402 * previous push metadata could have flag the metadata registry to
4403 * close so don't send a close command if closed.
4405 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4406 const auto metadata_key
= locked_registry
->_metadata_key
;
4407 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4409 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4410 locked_registry
->_metadata_closed
= true;
4413 /* Release lock before communication, see comments in
4414 * close_metadata(). */
4415 locked_registry
.reset();
4416 (void) close_metadata(
4417 metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4419 locked_registry
.reset();
4422 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4424 pthread_mutex_unlock(&ua_sess
->lock
);
4427 /* Remove application from PID hash table */
4428 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4432 * Remove application from notify hash table. The thread handling the
4433 * notify socket could have deleted the node so ignore on error because
4434 * either way it's valid. The close of that socket is handled by the
4435 * apps_notify_thread.
4437 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4438 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4441 * Ignore return value since the node might have been removed before by an
4442 * add replace during app registration because the PID can be reassigned by
4445 iter
.iter
.node
= <a
->pid_n
.node
;
4446 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4448 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", lta
->pid
);
4452 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4458 * Fill events array with all events name of all registered apps.
4460 int ust_app_list_events(struct lttng_event
**events
)
4463 size_t nbmem
, count
= 0;
4464 struct lttng_ht_iter iter
;
4465 struct ust_app
*app
;
4466 struct lttng_event
*tmp_event
;
4468 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4469 tmp_event
= calloc
<lttng_event
>(nbmem
);
4470 if (tmp_event
== nullptr) {
4471 PERROR("zmalloc ust app events");
4477 lttng::urcu::read_lock_guard read_lock
;
4479 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4480 struct lttng_ust_abi_tracepoint_iter uiter
;
4482 health_code_update();
4484 if (!app
->compatible
) {
4486 * TODO: In time, we should notice the caller of this error by
4487 * telling him that this is a version error.
4492 pthread_mutex_lock(&app
->sock_lock
);
4493 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4495 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4496 ERR("UST app list events getting handle failed for app pid %d",
4499 pthread_mutex_unlock(&app
->sock_lock
);
4503 while ((ret
= lttng_ust_ctl_tracepoint_list_get(
4504 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4505 /* Handle ustctl error. */
4509 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4510 ERR("UST app tp list get failed for app %d with ret %d",
4514 DBG3("UST app tp list get failed. Application is dead");
4520 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4521 if (release_ret
< 0 &&
4522 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4523 release_ret
!= -EPIPE
) {
4524 ERR("Error releasing app handle for app %d with ret %d",
4529 pthread_mutex_unlock(&app
->sock_lock
);
4533 health_code_update();
4534 if (count
>= nbmem
) {
4535 /* In case the realloc fails, we free the memory */
4536 struct lttng_event
*new_tmp_event
;
4539 new_nbmem
= nbmem
<< 1;
4540 DBG2("Reallocating event list from %zu to %zu entries",
4543 new_tmp_event
= (lttng_event
*) realloc(
4544 tmp_event
, new_nbmem
* sizeof(struct lttng_event
));
4545 if (new_tmp_event
== nullptr) {
4548 PERROR("realloc ust app events");
4551 release_ret
= lttng_ust_ctl_release_handle(
4553 if (release_ret
< 0 &&
4554 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4555 release_ret
!= -EPIPE
) {
4556 ERR("Error releasing app handle for app %d with ret %d",
4561 pthread_mutex_unlock(&app
->sock_lock
);
4564 /* Zero the new memory */
4565 memset(new_tmp_event
+ nbmem
,
4567 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4569 tmp_event
= new_tmp_event
;
4572 memcpy(tmp_event
[count
].name
,
4574 LTTNG_UST_ABI_SYM_NAME_LEN
);
4575 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4576 tmp_event
[count
].type
=
4577 (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4578 tmp_event
[count
].pid
= app
->pid
;
4579 tmp_event
[count
].enabled
= -1;
4583 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4584 pthread_mutex_unlock(&app
->sock_lock
);
4586 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4587 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4590 } else if (ret
== -EAGAIN
) {
4591 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4595 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4605 *events
= tmp_event
;
4607 DBG2("UST app list events done (%zu events)", count
);
4611 health_code_update();
4616 * Fill events array with all events name of all registered apps.
4618 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4621 size_t nbmem
, count
= 0;
4622 struct lttng_ht_iter iter
;
4623 struct ust_app
*app
;
4624 struct lttng_event_field
*tmp_event
;
4626 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4627 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4628 if (tmp_event
== nullptr) {
4629 PERROR("zmalloc ust app event fields");
4635 lttng::urcu::read_lock_guard read_lock
;
4637 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4638 struct lttng_ust_abi_field_iter uiter
;
4640 health_code_update();
4642 if (!app
->compatible
) {
4644 * TODO: In time, we should notice the caller of this error by
4645 * telling him that this is a version error.
4650 pthread_mutex_lock(&app
->sock_lock
);
4651 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4653 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4654 ERR("UST app list field getting handle failed for app pid %d",
4657 pthread_mutex_unlock(&app
->sock_lock
);
4661 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(
4662 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4663 /* Handle ustctl error. */
4667 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4668 ERR("UST app tp list field failed for app %d with ret %d",
4672 DBG3("UST app tp list field failed. Application is dead");
4678 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4679 pthread_mutex_unlock(&app
->sock_lock
);
4680 if (release_ret
< 0 &&
4681 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4682 release_ret
!= -EPIPE
) {
4683 ERR("Error releasing app handle for app %d with ret %d",
4691 health_code_update();
4692 if (count
>= nbmem
) {
4693 /* In case the realloc fails, we free the memory */
4694 struct lttng_event_field
*new_tmp_event
;
4697 new_nbmem
= nbmem
<< 1;
4698 DBG2("Reallocating event field list from %zu to %zu entries",
4701 new_tmp_event
= (lttng_event_field
*) realloc(
4703 new_nbmem
* sizeof(struct lttng_event_field
));
4704 if (new_tmp_event
== nullptr) {
4707 PERROR("realloc ust app event fields");
4710 release_ret
= lttng_ust_ctl_release_handle(
4712 pthread_mutex_unlock(&app
->sock_lock
);
4714 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4715 release_ret
!= -EPIPE
) {
4716 ERR("Error releasing app handle for app %d with ret %d",
4724 /* Zero the new memory */
4725 memset(new_tmp_event
+ nbmem
,
4727 (new_nbmem
- nbmem
) *
4728 sizeof(struct lttng_event_field
));
4730 tmp_event
= new_tmp_event
;
4733 memcpy(tmp_event
[count
].field_name
,
4735 LTTNG_UST_ABI_SYM_NAME_LEN
);
4736 /* Mapping between these enums matches 1 to 1. */
4737 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4738 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4740 memcpy(tmp_event
[count
].event
.name
,
4742 LTTNG_UST_ABI_SYM_NAME_LEN
);
4743 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4744 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4745 tmp_event
[count
].event
.pid
= app
->pid
;
4746 tmp_event
[count
].event
.enabled
= -1;
4750 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4751 pthread_mutex_unlock(&app
->sock_lock
);
4752 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4753 ERR("Error releasing app handle for app %d with ret %d",
4761 *fields
= tmp_event
;
4763 DBG2("UST app list event fields done (%zu events)", count
);
4767 health_code_update();
4772 * Free and clean all traceable apps of the global list.
4774 void ust_app_clean_list()
4777 struct ust_app
*app
;
4778 struct lttng_ht_iter iter
;
4780 DBG2("UST app cleaning registered apps hash table");
4782 /* Cleanup notify socket hash table */
4783 if (ust_app_ht_by_notify_sock
) {
4784 lttng::urcu::read_lock_guard read_lock
;
4786 cds_lfht_for_each_entry (
4787 ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
, notify_sock_n
.node
) {
4789 * Assert that all notifiers are gone as all triggers
4790 * are unregistered prior to this clean-up.
4792 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4794 ust_app_notify_sock_unregister(app
->notify_sock
);
4799 lttng::urcu::read_lock_guard read_lock
;
4801 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4802 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4804 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4808 /* Cleanup socket hash table */
4809 if (ust_app_ht_by_sock
) {
4810 lttng::urcu::read_lock_guard read_lock
;
4812 cds_lfht_for_each_entry (ust_app_ht_by_sock
->ht
, &iter
.iter
, app
, sock_n
.node
) {
4813 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4818 /* Destroy is done only when the ht is empty */
4820 lttng_ht_destroy(ust_app_ht
);
4822 if (ust_app_ht_by_sock
) {
4823 lttng_ht_destroy(ust_app_ht_by_sock
);
4825 if (ust_app_ht_by_notify_sock
) {
4826 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4831 * Init UST app hash table.
4833 int ust_app_ht_alloc()
4835 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4839 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4840 if (!ust_app_ht_by_sock
) {
4843 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4844 if (!ust_app_ht_by_notify_sock
) {
4851 * For a specific UST session, disable the channel for all registered apps.
4853 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4856 struct lttng_ht_iter iter
;
4857 struct lttng_ht_node_str
*ua_chan_node
;
4858 struct ust_app
*app
;
4859 struct ust_app_session
*ua_sess
;
4860 struct ust_app_channel
*ua_chan
;
4862 LTTNG_ASSERT(usess
->active
);
4863 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4868 lttng::urcu::read_lock_guard read_lock
;
4870 /* For every registered applications */
4871 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4872 struct lttng_ht_iter uiter
;
4873 if (!app
->compatible
) {
4875 * TODO: In time, we should notice the caller of this error by
4876 * telling him that this is a version error.
4880 ua_sess
= lookup_session_by_app(usess
, app
);
4881 if (ua_sess
== nullptr) {
4886 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4887 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4888 /* If the session if found for the app, the channel must be there */
4889 LTTNG_ASSERT(ua_chan_node
);
4891 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4892 /* The channel must not be already disabled */
4893 LTTNG_ASSERT(ua_chan
->enabled
);
4895 /* Disable channel onto application */
4896 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4898 /* XXX: We might want to report this error at some point... */
4908 * For a specific UST session, enable the channel for all registered apps.
4910 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4913 struct lttng_ht_iter iter
;
4914 struct ust_app
*app
;
4915 struct ust_app_session
*ua_sess
;
4917 LTTNG_ASSERT(usess
->active
);
4918 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4923 lttng::urcu::read_lock_guard read_lock
;
4925 /* For every registered applications */
4926 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4927 if (!app
->compatible
) {
4929 * TODO: In time, we should notice the caller of this error by
4930 * telling him that this is a version error.
4934 ua_sess
= lookup_session_by_app(usess
, app
);
4935 if (ua_sess
== nullptr) {
4939 /* Enable channel onto application */
4940 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4942 /* XXX: We might want to report this error at some point... */
4952 * Disable an event in a channel and for a specific session.
4954 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4955 struct ltt_ust_channel
*uchan
,
4956 struct ltt_ust_event
*uevent
)
4959 struct lttng_ht_iter iter
, uiter
;
4960 struct lttng_ht_node_str
*ua_chan_node
;
4961 struct ust_app
*app
;
4962 struct ust_app_session
*ua_sess
;
4963 struct ust_app_channel
*ua_chan
;
4964 struct ust_app_event
*ua_event
;
4966 LTTNG_ASSERT(usess
->active
);
4967 DBG("UST app disabling event %s for all apps in channel "
4968 "%s for session id %" PRIu64
,
4974 lttng::urcu::read_lock_guard read_lock
;
4976 /* For all registered applications */
4977 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4978 if (!app
->compatible
) {
4980 * TODO: In time, we should notice the caller of this error by
4981 * telling him that this is a version error.
4985 ua_sess
= lookup_session_by_app(usess
, app
);
4986 if (ua_sess
== nullptr) {
4991 /* Lookup channel in the ust app session */
4992 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4993 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4994 if (ua_chan_node
== nullptr) {
4995 DBG2("Channel %s not found in session id %" PRIu64
5003 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5005 ua_event
= find_ust_app_event(ua_chan
->events
,
5008 uevent
->attr
.loglevel
,
5010 if (ua_event
== nullptr) {
5011 DBG2("Event %s not found in channel %s for app pid %d."
5019 ret
= disable_ust_app_event(ua_event
, app
);
5021 /* XXX: Report error someday... */
5030 /* The ua_sess lock must be held by the caller. */
5031 static int ust_app_channel_create(struct ltt_ust_session
*usess
,
5032 struct ust_app_session
*ua_sess
,
5033 struct ltt_ust_channel
*uchan
,
5034 struct ust_app
*app
,
5035 struct ust_app_channel
**_ua_chan
)
5038 struct ust_app_channel
*ua_chan
= nullptr;
5040 LTTNG_ASSERT(ua_sess
);
5041 ASSERT_LOCKED(ua_sess
->lock
);
5043 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
))) {
5044 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
5047 struct ltt_ust_context
*uctx
= nullptr;
5050 * Create channel onto application and synchronize its
5053 ret
= ust_app_channel_allocate(
5054 ua_sess
, uchan
, LTTNG_UST_ABI_CHAN_PER_CPU
, usess
, &ua_chan
);
5059 ret
= ust_app_channel_send(app
, usess
, ua_sess
, ua_chan
);
5065 cds_list_for_each_entry (uctx
, &uchan
->ctx_list
, list
) {
5066 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5078 * The application's socket is not valid. Either a bad socket
5079 * or a timeout on it. We can't inform the caller that for a
5080 * specific app, the session failed so lets continue here.
5082 ret
= 0; /* Not an error. */
5090 if (ret
== 0 && _ua_chan
) {
5092 * Only return the application's channel on success. Note
5093 * that the channel can still be part of the application's
5094 * channel hashtable on error.
5096 *_ua_chan
= ua_chan
;
5102 * Enable event for a specific session and channel on the tracer.
5104 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
5105 struct ltt_ust_channel
*uchan
,
5106 struct ltt_ust_event
*uevent
)
5109 struct lttng_ht_iter iter
, uiter
;
5110 struct lttng_ht_node_str
*ua_chan_node
;
5111 struct ust_app
*app
;
5112 struct ust_app_session
*ua_sess
;
5113 struct ust_app_channel
*ua_chan
;
5114 struct ust_app_event
*ua_event
;
5116 LTTNG_ASSERT(usess
->active
);
5117 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
5122 * NOTE: At this point, this function is called only if the session and
5123 * channel passed are already created for all apps. and enabled on the
5128 lttng::urcu::read_lock_guard read_lock
;
5130 /* For all registered applications */
5131 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5132 if (!app
->compatible
) {
5134 * TODO: In time, we should notice the caller of this error by
5135 * telling him that this is a version error.
5139 ua_sess
= lookup_session_by_app(usess
, app
);
5141 /* The application has problem or is probably dead. */
5145 pthread_mutex_lock(&ua_sess
->lock
);
5147 if (ua_sess
->deleted
) {
5148 pthread_mutex_unlock(&ua_sess
->lock
);
5152 /* Lookup channel in the ust app session */
5153 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5154 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5156 * It is possible that the channel cannot be found is
5157 * the channel/event creation occurs concurrently with
5158 * an application exit.
5160 if (!ua_chan_node
) {
5161 pthread_mutex_unlock(&ua_sess
->lock
);
5165 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5167 /* Get event node */
5168 ua_event
= find_ust_app_event(ua_chan
->events
,
5171 uevent
->attr
.loglevel
,
5173 if (ua_event
== nullptr) {
5174 DBG3("UST app enable event %s not found for app PID %d."
5181 ret
= enable_ust_app_event(ua_event
, app
);
5183 pthread_mutex_unlock(&ua_sess
->lock
);
5187 pthread_mutex_unlock(&ua_sess
->lock
);
5195 * For a specific existing UST session and UST channel, creates the event for
5196 * all registered apps.
5198 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5199 struct ltt_ust_channel
*uchan
,
5200 struct ltt_ust_event
*uevent
)
5203 struct lttng_ht_iter iter
, uiter
;
5204 struct lttng_ht_node_str
*ua_chan_node
;
5205 struct ust_app
*app
;
5206 struct ust_app_session
*ua_sess
;
5207 struct ust_app_channel
*ua_chan
;
5209 LTTNG_ASSERT(usess
->active
);
5210 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5215 lttng::urcu::read_lock_guard read_lock
;
5217 /* For all registered applications */
5218 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5219 if (!app
->compatible
) {
5221 * TODO: In time, we should notice the caller of this error by
5222 * telling him that this is a version error.
5227 ua_sess
= lookup_session_by_app(usess
, app
);
5229 /* The application has problem or is probably dead. */
5233 pthread_mutex_lock(&ua_sess
->lock
);
5235 if (ua_sess
->deleted
) {
5236 pthread_mutex_unlock(&ua_sess
->lock
);
5240 /* Lookup channel in the ust app session */
5241 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5242 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5243 /* If the channel is not found, there is a code flow error */
5244 LTTNG_ASSERT(ua_chan_node
);
5246 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5248 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5249 pthread_mutex_unlock(&ua_sess
->lock
);
5251 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5252 /* Possible value at this point: -ENOMEM. If so, we stop! */
5256 DBG2("UST app event %s already exist on app PID %d",
5268 * Start tracing for a specific UST session and app.
5270 * Called with UST app session lock held.
5273 static int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5276 struct ust_app_session
*ua_sess
;
5278 DBG("Starting tracing for ust app pid %d", app
->pid
);
5280 lttng::urcu::read_lock_guard read_lock
;
5282 if (!app
->compatible
) {
5286 ua_sess
= lookup_session_by_app(usess
, app
);
5287 if (ua_sess
== nullptr) {
5288 /* The session is in teardown process. Ignore and continue. */
5292 pthread_mutex_lock(&ua_sess
->lock
);
5294 if (ua_sess
->deleted
) {
5295 pthread_mutex_unlock(&ua_sess
->lock
);
5299 if (ua_sess
->enabled
) {
5300 pthread_mutex_unlock(&ua_sess
->lock
);
5304 /* Upon restart, we skip the setup, already done */
5305 if (ua_sess
->started
) {
5309 health_code_update();
5312 /* This starts the UST tracing */
5313 pthread_mutex_lock(&app
->sock_lock
);
5314 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5315 pthread_mutex_unlock(&app
->sock_lock
);
5317 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5318 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5321 pthread_mutex_unlock(&ua_sess
->lock
);
5323 } else if (ret
== -EAGAIN
) {
5324 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5327 pthread_mutex_unlock(&ua_sess
->lock
);
5331 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5339 /* Indicate that the session has been started once */
5340 ua_sess
->started
= true;
5341 ua_sess
->enabled
= true;
5343 pthread_mutex_unlock(&ua_sess
->lock
);
5345 health_code_update();
5347 /* Quiescent wait after starting trace */
5348 pthread_mutex_lock(&app
->sock_lock
);
5349 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5350 pthread_mutex_unlock(&app
->sock_lock
);
5352 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5353 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5356 } else if (ret
== -EAGAIN
) {
5357 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5361 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5369 health_code_update();
5373 pthread_mutex_unlock(&ua_sess
->lock
);
5374 health_code_update();
5379 * Stop tracing for a specific UST session and app.
5381 static int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5384 struct ust_app_session
*ua_sess
;
5386 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5388 lttng::urcu::read_lock_guard read_lock
;
5390 if (!app
->compatible
) {
5391 goto end_no_session
;
5394 ua_sess
= lookup_session_by_app(usess
, app
);
5395 if (ua_sess
== nullptr) {
5396 goto end_no_session
;
5399 pthread_mutex_lock(&ua_sess
->lock
);
5401 if (ua_sess
->deleted
) {
5402 pthread_mutex_unlock(&ua_sess
->lock
);
5403 goto end_no_session
;
5407 * If started = 0, it means that stop trace has been called for a session
5408 * that was never started. It's possible since we can have a fail start
5409 * from either the application manager thread or the command thread. Simply
5410 * indicate that this is a stop error.
5412 if (!ua_sess
->started
) {
5413 goto error_rcu_unlock
;
5416 health_code_update();
5418 /* This inhibits UST tracing */
5419 pthread_mutex_lock(&app
->sock_lock
);
5420 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5421 pthread_mutex_unlock(&app
->sock_lock
);
5423 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5424 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5428 } else if (ret
== -EAGAIN
) {
5429 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5435 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5440 goto error_rcu_unlock
;
5443 health_code_update();
5444 ua_sess
->enabled
= false;
5446 /* Quiescent wait after stopping trace */
5447 pthread_mutex_lock(&app
->sock_lock
);
5448 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5449 pthread_mutex_unlock(&app
->sock_lock
);
5451 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5452 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5455 } else if (ret
== -EAGAIN
) {
5456 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5460 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5467 health_code_update();
5470 auto locked_registry
= get_locked_session_registry(ua_sess
);
5472 /* The UST app session is held registry shall not be null. */
5473 LTTNG_ASSERT(locked_registry
);
5475 /* Push metadata for application before freeing the application. */
5476 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5480 pthread_mutex_unlock(&ua_sess
->lock
);
5482 health_code_update();
5486 pthread_mutex_unlock(&ua_sess
->lock
);
5487 health_code_update();
5491 static int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5493 int ret
, retval
= 0;
5494 struct lttng_ht_iter iter
;
5495 struct ust_app_channel
*ua_chan
;
5496 struct consumer_socket
*socket
;
5498 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5500 if (!app
->compatible
) {
5501 goto end_not_compatible
;
5504 pthread_mutex_lock(&ua_sess
->lock
);
5506 if (ua_sess
->deleted
) {
5510 health_code_update();
5512 /* Flushing buffers */
5513 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5515 /* Flush buffers and push metadata. */
5516 switch (ua_sess
->buffer_type
) {
5517 case LTTNG_BUFFER_PER_PID
:
5519 lttng::urcu::read_lock_guard read_lock
;
5521 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5522 health_code_update();
5523 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5525 ERR("Error flushing consumer channel");
5533 case LTTNG_BUFFER_PER_UID
:
5539 health_code_update();
5542 pthread_mutex_unlock(&ua_sess
->lock
);
5545 health_code_update();
5550 * Flush buffers for all applications for a specific UST session.
5551 * Called with UST session lock held.
5553 static int ust_app_flush_session(struct ltt_ust_session
*usess
)
5558 DBG("Flushing session buffers for all ust apps");
5560 /* Flush buffers and push metadata. */
5561 switch (usess
->buffer_type
) {
5562 case LTTNG_BUFFER_PER_UID
:
5564 struct buffer_reg_uid
*reg
;
5565 struct lttng_ht_iter iter
;
5567 /* Flush all per UID buffers associated to that session. */
5568 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5569 lttng::urcu::read_lock_guard read_lock
;
5570 lsu::registry_session
*ust_session_reg
;
5571 struct buffer_reg_channel
*buf_reg_chan
;
5572 struct consumer_socket
*socket
;
5574 /* Get consumer socket to use to push the metadata.*/
5575 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5578 /* Ignore request if no consumer is found for the session. */
5582 cds_lfht_for_each_entry (
5583 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5585 * The following call will print error values so the return
5586 * code is of little importance because whatever happens, we
5587 * have to try them all.
5589 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5592 ust_session_reg
= reg
->registry
->reg
.ust
;
5593 /* Push metadata. */
5594 auto locked_registry
= ust_session_reg
->lock();
5595 (void) push_metadata(locked_registry
, usess
->consumer
);
5600 case LTTNG_BUFFER_PER_PID
:
5602 struct ust_app_session
*ua_sess
;
5603 struct lttng_ht_iter iter
;
5604 struct ust_app
*app
;
5605 lttng::urcu::read_lock_guard read_lock
;
5607 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5608 ua_sess
= lookup_session_by_app(usess
, app
);
5609 if (ua_sess
== nullptr) {
5613 (void) ust_app_flush_app_session(app
, ua_sess
);
5624 health_code_update();
5628 static int ust_app_clear_quiescent_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5631 struct lttng_ht_iter iter
;
5632 struct ust_app_channel
*ua_chan
;
5633 struct consumer_socket
*socket
;
5635 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5637 lttng::urcu::read_lock_guard read_lock
;
5639 if (!app
->compatible
) {
5640 goto end_not_compatible
;
5643 pthread_mutex_lock(&ua_sess
->lock
);
5645 if (ua_sess
->deleted
) {
5649 health_code_update();
5651 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5653 ERR("Failed to find consumer (%" PRIu32
") socket", app
->abi
.bits_per_long
);
5658 /* Clear quiescent state. */
5659 switch (ua_sess
->buffer_type
) {
5660 case LTTNG_BUFFER_PER_PID
:
5661 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5662 health_code_update();
5663 ret
= consumer_clear_quiescent_channel(socket
, ua_chan
->key
);
5665 ERR("Error clearing quiescent state for consumer channel");
5671 case LTTNG_BUFFER_PER_UID
:
5678 health_code_update();
5681 pthread_mutex_unlock(&ua_sess
->lock
);
5684 health_code_update();
5689 * Clear quiescent state in each stream for all applications for a
5690 * specific UST session.
5691 * Called with UST session lock held.
5693 static int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5698 DBG("Clearing stream quiescent state for all ust apps");
5700 switch (usess
->buffer_type
) {
5701 case LTTNG_BUFFER_PER_UID
:
5703 struct lttng_ht_iter iter
;
5704 struct buffer_reg_uid
*reg
;
5707 * Clear quiescent for all per UID buffers associated to
5710 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5711 struct consumer_socket
*socket
;
5712 struct buffer_reg_channel
*buf_reg_chan
;
5713 lttng::urcu::read_lock_guard read_lock
;
5715 /* Get associated consumer socket.*/
5716 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5720 * Ignore request if no consumer is found for
5726 cds_lfht_for_each_entry (
5727 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5729 * The following call will print error values so
5730 * the return code is of little importance
5731 * because whatever happens, we have to try them
5734 (void) consumer_clear_quiescent_channel(socket
,
5735 buf_reg_chan
->consumer_key
);
5741 case LTTNG_BUFFER_PER_PID
:
5743 struct ust_app_session
*ua_sess
;
5744 struct lttng_ht_iter iter
;
5745 struct ust_app
*app
;
5746 lttng::urcu::read_lock_guard read_lock
;
5748 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5749 ua_sess
= lookup_session_by_app(usess
, app
);
5750 if (ua_sess
== nullptr) {
5753 (void) ust_app_clear_quiescent_app_session(app
, ua_sess
);
5764 health_code_update();
5769 * Destroy a specific UST session in apps.
5771 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5774 struct ust_app_session
*ua_sess
;
5775 struct lttng_ht_iter iter
;
5776 struct lttng_ht_node_u64
*node
;
5778 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5780 lttng::urcu::read_lock_guard read_lock
;
5782 if (!app
->compatible
) {
5786 __lookup_session_by_app(usess
, app
, &iter
);
5787 node
= lttng_ht_iter_get_node_u64(&iter
);
5788 if (node
== nullptr) {
5789 /* Session is being or is deleted. */
5792 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5794 health_code_update();
5795 destroy_app_session(app
, ua_sess
);
5797 health_code_update();
5799 /* Quiescent wait after stopping trace */
5800 pthread_mutex_lock(&app
->sock_lock
);
5801 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5802 pthread_mutex_unlock(&app
->sock_lock
);
5804 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5805 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5808 } else if (ret
== -EAGAIN
) {
5809 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5813 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5820 health_code_update();
5825 * Start tracing for the UST session.
5827 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5829 struct lttng_ht_iter iter
;
5830 struct ust_app
*app
;
5832 DBG("Starting all UST traces");
5835 * Even though the start trace might fail, flag this session active so
5836 * other application coming in are started by default.
5838 usess
->active
= true;
5841 * In a start-stop-start use-case, we need to clear the quiescent state
5842 * of each channel set by the prior stop command, thus ensuring that a
5843 * following stop or destroy is sure to grab a timestamp_end near those
5844 * operations, even if the packet is empty.
5846 (void) ust_app_clear_quiescent_session(usess
);
5849 lttng::urcu::read_lock_guard read_lock
;
5851 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5852 ust_app_global_update(usess
, app
);
5860 * Start tracing for the UST session.
5861 * Called with UST session lock held.
5863 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5866 struct lttng_ht_iter iter
;
5867 struct ust_app
*app
;
5869 DBG("Stopping all UST traces");
5872 * Even though the stop trace might fail, flag this session inactive so
5873 * other application coming in are not started by default.
5875 usess
->active
= false;
5878 lttng::urcu::read_lock_guard read_lock
;
5880 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5881 ret
= ust_app_stop_trace(usess
, app
);
5883 /* Continue to next apps even on error */
5889 (void) ust_app_flush_session(usess
);
5895 * Destroy app UST session.
5897 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5900 struct lttng_ht_iter iter
;
5901 struct ust_app
*app
;
5903 DBG("Destroy all UST traces");
5906 lttng::urcu::read_lock_guard read_lock
;
5908 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5909 ret
= destroy_trace(usess
, app
);
5911 /* Continue to next apps even on error */
5920 /* The ua_sess lock must be held by the caller. */
5921 static int find_or_create_ust_app_channel(struct ltt_ust_session
*usess
,
5922 struct ust_app_session
*ua_sess
,
5923 struct ust_app
*app
,
5924 struct ltt_ust_channel
*uchan
,
5925 struct ust_app_channel
**ua_chan
)
5928 struct lttng_ht_iter iter
;
5929 struct lttng_ht_node_str
*ua_chan_node
;
5931 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5932 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5934 *ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5938 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5946 static int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5947 struct ltt_ust_event
*uevent
,
5948 struct ust_app
*app
)
5951 struct ust_app_event
*ua_event
= nullptr;
5953 ua_event
= find_ust_app_event(ua_chan
->events
,
5956 uevent
->attr
.loglevel
,
5959 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5964 if (ua_event
->enabled
!= uevent
->enabled
) {
5965 ret
= uevent
->enabled
? enable_ust_app_event(ua_event
, app
) :
5966 disable_ust_app_event(ua_event
, app
);
5974 /* Called with RCU read-side lock held. */
5975 static void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5978 enum lttng_error_code ret_code
;
5979 enum lttng_trigger_status t_status
;
5980 struct lttng_ht_iter app_trigger_iter
;
5981 struct lttng_triggers
*triggers
= nullptr;
5982 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5983 unsigned int count
, i
;
5985 ASSERT_RCU_READ_LOCKED();
5987 if (!ust_app_supports_notifiers(app
)) {
5992 * Currrently, registering or unregistering a trigger with an
5993 * event rule condition causes a full synchronization of the event
5996 * The first step attempts to add an event notifier for all registered
5997 * triggers that apply to the user space tracers. Then, the
5998 * application's event notifiers rules are all checked against the list
5999 * of registered triggers. Any event notifier that doesn't have a
6000 * matching trigger can be assumed to have been disabled.
6002 * All of this is inefficient, but is put in place to get the feature
6003 * rolling as it is simpler at this moment. It will be optimized Soon™
6004 * to allow the state of enabled
6005 * event notifiers to be synchronized in a piece-wise way.
6008 /* Get all triggers using uid 0 (root) */
6009 ret_code
= notification_thread_command_list_triggers(
6010 the_notification_thread_handle
, 0, &triggers
);
6011 if (ret_code
!= LTTNG_OK
) {
6015 LTTNG_ASSERT(triggers
);
6017 t_status
= lttng_triggers_get_count(triggers
, &count
);
6018 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
6022 for (i
= 0; i
< count
; i
++) {
6023 struct lttng_condition
*condition
;
6024 struct lttng_event_rule
*event_rule
;
6025 struct lttng_trigger
*trigger
;
6026 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
6027 enum lttng_condition_status condition_status
;
6030 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
6031 LTTNG_ASSERT(trigger
);
6033 token
= lttng_trigger_get_tracer_token(trigger
);
6034 condition
= lttng_trigger_get_condition(trigger
);
6036 if (lttng_condition_get_type(condition
) !=
6037 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
6038 /* Does not apply */
6042 condition_status
= lttng_condition_event_rule_matches_borrow_rule_mutable(
6043 condition
, &event_rule
);
6044 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
6046 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
6047 /* Skip kernel related triggers. */
6052 * Find or create the associated token event rule. The caller
6053 * holds the RCU read lock, so this is safe to call without
6054 * explicitly acquiring it here.
6056 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
6057 app
->token_to_event_notifier_rule_ht
, token
);
6058 if (!looked_up_event_notifier_rule
) {
6059 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
6067 lttng::urcu::read_lock_guard read_lock
;
6069 /* Remove all unknown event sources from the app. */
6070 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
6071 &app_trigger_iter
.iter
,
6072 event_notifier_rule
,
6074 const uint64_t app_token
= event_notifier_rule
->token
;
6078 * Check if the app event trigger still exists on the
6079 * notification side.
6081 for (i
= 0; i
< count
; i
++) {
6082 uint64_t notification_thread_token
;
6083 const struct lttng_trigger
*trigger
=
6084 lttng_triggers_get_at_index(triggers
, i
);
6086 LTTNG_ASSERT(trigger
);
6088 notification_thread_token
= lttng_trigger_get_tracer_token(trigger
);
6090 if (notification_thread_token
== app_token
) {
6102 * This trigger was unregistered, disable it on the tracer's
6105 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &app_trigger_iter
);
6106 LTTNG_ASSERT(ret
== 0);
6108 /* Callee logs errors. */
6109 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
6111 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
6116 lttng_triggers_destroy(triggers
);
6121 * RCU read lock must be held by the caller.
6123 static void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
6124 struct ust_app_session
*ua_sess
,
6125 struct ust_app
*app
)
6128 struct cds_lfht_iter uchan_iter
;
6129 struct ltt_ust_channel
*uchan
;
6131 LTTNG_ASSERT(usess
);
6132 LTTNG_ASSERT(ua_sess
);
6134 ASSERT_RCU_READ_LOCKED();
6136 cds_lfht_for_each_entry (usess
->domain_global
.channels
->ht
, &uchan_iter
, uchan
, node
.node
) {
6137 struct ust_app_channel
*ua_chan
;
6138 struct cds_lfht_iter uevent_iter
;
6139 struct ltt_ust_event
*uevent
;
6142 * Search for a matching ust_app_channel. If none is found,
6143 * create it. Creating the channel will cause the ua_chan
6144 * structure to be allocated, the channel buffers to be
6145 * allocated (if necessary) and sent to the application, and
6146 * all enabled contexts will be added to the channel.
6148 ret
= find_or_create_ust_app_channel(usess
, ua_sess
, app
, uchan
, &ua_chan
);
6150 /* Tracer is probably gone or ENOMEM. */
6155 /* ua_chan will be NULL for the metadata channel */
6159 cds_lfht_for_each_entry (uchan
->events
->ht
, &uevent_iter
, uevent
, node
.node
) {
6160 ret
= ust_app_channel_synchronize_event(ua_chan
, uevent
, app
);
6166 if (ua_chan
->enabled
!= uchan
->enabled
) {
6167 ret
= uchan
->enabled
? enable_ust_app_channel(ua_sess
, uchan
, app
) :
6168 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6179 * The caller must ensure that the application is compatible and is tracked
6180 * by the process attribute trackers.
6182 static void ust_app_synchronize(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6185 struct ust_app_session
*ua_sess
= nullptr;
6188 * The application's configuration should only be synchronized for
6191 LTTNG_ASSERT(usess
->active
);
6193 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, nullptr);
6195 /* Tracer is probably gone or ENOMEM. */
6197 destroy_app_session(app
, ua_sess
);
6201 LTTNG_ASSERT(ua_sess
);
6203 pthread_mutex_lock(&ua_sess
->lock
);
6204 if (ua_sess
->deleted
) {
6205 goto deleted_session
;
6209 lttng::urcu::read_lock_guard read_lock
;
6211 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6214 * Create the metadata for the application. This returns gracefully if a
6215 * metadata was already set for the session.
6217 * The metadata channel must be created after the data channels as the
6218 * consumer daemon assumes this ordering. When interacting with a relay
6219 * daemon, the consumer will use this assumption to send the
6220 * "STREAMS_SENT" message to the relay daemon.
6222 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6224 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6231 pthread_mutex_unlock(&ua_sess
->lock
);
6236 static void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6238 struct ust_app_session
*ua_sess
;
6240 ua_sess
= lookup_session_by_app(usess
, app
);
6241 if (ua_sess
== nullptr) {
6244 destroy_app_session(app
, ua_sess
);
6248 * Add channels/events from UST global domain to registered apps at sock.
6250 * Called with session lock held.
6251 * Called with RCU read-side lock held.
6253 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6255 LTTNG_ASSERT(usess
);
6256 LTTNG_ASSERT(usess
->active
);
6257 ASSERT_RCU_READ_LOCKED();
6259 DBG2("UST app global update for app sock %d for session id %" PRIu64
, app
->sock
, usess
->id
);
6261 if (!app
->compatible
) {
6264 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, usess
, app
->pid
) &&
6265 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
, usess
, app
->uid
) &&
6266 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
, usess
, app
->gid
)) {
6268 * Synchronize the application's internal tracing configuration
6269 * and start tracing.
6271 ust_app_synchronize(usess
, app
);
6272 ust_app_start_trace(usess
, app
);
6274 ust_app_global_destroy(usess
, app
);
6279 * Add all event notifiers to an application.
6281 * Called with session lock held.
6282 * Called with RCU read-side lock held.
6284 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6286 ASSERT_RCU_READ_LOCKED();
6288 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6292 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6296 if (app
->event_notifier_group
.object
== nullptr) {
6297 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6303 ust_app_synchronize_event_notifier_rules(app
);
6307 * Called with session lock held.
6309 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6311 struct lttng_ht_iter iter
;
6312 struct ust_app
*app
;
6315 lttng::urcu::read_lock_guard read_lock
;
6317 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6318 ust_app_global_update(usess
, app
);
6323 void ust_app_global_update_all_event_notifier_rules()
6325 struct lttng_ht_iter iter
;
6326 struct ust_app
*app
;
6328 lttng::urcu::read_lock_guard read_lock
;
6329 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6330 ust_app_global_update_event_notifier_rules(app
);
6335 * Add context to a specific channel for global UST domain.
6337 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6338 struct ltt_ust_channel
*uchan
,
6339 struct ltt_ust_context
*uctx
)
6342 struct lttng_ht_node_str
*ua_chan_node
;
6343 struct lttng_ht_iter iter
, uiter
;
6344 struct ust_app_channel
*ua_chan
= nullptr;
6345 struct ust_app_session
*ua_sess
;
6346 struct ust_app
*app
;
6348 LTTNG_ASSERT(usess
->active
);
6351 lttng::urcu::read_lock_guard read_lock
;
6352 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6353 if (!app
->compatible
) {
6355 * TODO: In time, we should notice the caller of this error by
6356 * telling him that this is a version error.
6360 ua_sess
= lookup_session_by_app(usess
, app
);
6361 if (ua_sess
== nullptr) {
6365 pthread_mutex_lock(&ua_sess
->lock
);
6367 if (ua_sess
->deleted
) {
6368 pthread_mutex_unlock(&ua_sess
->lock
);
6372 /* Lookup channel in the ust app session */
6373 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6374 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6375 if (ua_chan_node
== nullptr) {
6378 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6379 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6384 pthread_mutex_unlock(&ua_sess
->lock
);
6392 * Receive registration and populate the given msg structure.
6394 * On success return 0 else a negative value returned by the ustctl call.
6396 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6399 uint32_t pid
, ppid
, uid
, gid
;
6403 ret
= lttng_ust_ctl_recv_reg_msg(sock
,
6411 &msg
->bits_per_long
,
6412 &msg
->uint8_t_alignment
,
6413 &msg
->uint16_t_alignment
,
6414 &msg
->uint32_t_alignment
,
6415 &msg
->uint64_t_alignment
,
6416 &msg
->long_alignment
,
6423 case LTTNG_UST_ERR_EXITING
:
6424 DBG3("UST app recv reg message failed. Application died");
6426 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6427 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6430 LTTNG_UST_ABI_MAJOR_VERSION
,
6431 LTTNG_UST_ABI_MINOR_VERSION
);
6434 ERR("UST app recv reg message failed with ret %d", ret
);
6439 msg
->pid
= (pid_t
) pid
;
6440 msg
->ppid
= (pid_t
) ppid
;
6441 msg
->uid
= (uid_t
) uid
;
6442 msg
->gid
= (gid_t
) gid
;
6449 * Return a ust app session object using the application object and the
6450 * session object descriptor has a key. If not found, NULL is returned.
6451 * A RCU read side lock MUST be acquired when calling this function.
6453 static struct ust_app_session
*find_session_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_session
*ua_sess
= nullptr;
6460 ASSERT_RCU_READ_LOCKED();
6462 lttng_ht_lookup(app
->ust_sessions_objd
, (void *) ((unsigned long) objd
), &iter
);
6463 node
= lttng_ht_iter_get_node_ulong(&iter
);
6464 if (node
== nullptr) {
6465 DBG2("UST app session find by objd %d not found", objd
);
6469 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6476 * Return a ust app channel object using the application object and the channel
6477 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6478 * lock MUST be acquired before calling this function.
6480 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
, int objd
)
6482 struct lttng_ht_node_ulong
*node
;
6483 struct lttng_ht_iter iter
;
6484 struct ust_app_channel
*ua_chan
= nullptr;
6487 ASSERT_RCU_READ_LOCKED();
6489 lttng_ht_lookup(app
->ust_objd
, (void *) ((unsigned long) objd
), &iter
);
6490 node
= lttng_ht_iter_get_node_ulong(&iter
);
6491 if (node
== nullptr) {
6492 DBG2("UST app channel find by objd %d not found", objd
);
6496 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6503 * Reply to a register channel notification from an application on the notify
6504 * socket. The channel metadata is also created.
6506 * The session UST registry lock is acquired in this function.
6508 * On success 0 is returned else a negative value.
6510 static int handle_app_register_channel_notification(int sock
,
6512 struct lttng_ust_ctl_field
*raw_context_fields
,
6513 size_t context_field_count
)
6515 int ret
, ret_code
= 0;
6517 uint64_t chan_reg_key
;
6518 struct ust_app
*app
;
6519 struct ust_app_channel
*ua_chan
;
6520 struct ust_app_session
*ua_sess
;
6521 auto ust_ctl_context_fields
=
6522 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_context_fields
);
6524 lttng::urcu::read_lock_guard read_lock_guard
;
6526 /* Lookup application. If not found, there is a code flow error. */
6527 app
= find_app_by_notify_sock(sock
);
6529 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6533 /* Lookup channel by UST object descriptor. */
6534 ua_chan
= find_channel_by_objd(app
, cobjd
);
6536 DBG("Application channel is being torn down. Abort event notify");
6540 LTTNG_ASSERT(ua_chan
->session
);
6541 ua_sess
= ua_chan
->session
;
6543 /* Get right session registry depending on the session buffer type. */
6544 auto locked_registry_session
= get_locked_session_registry(ua_sess
);
6545 if (!locked_registry_session
) {
6546 DBG("Application session is being torn down. Abort event notify");
6550 /* Depending on the buffer type, a different channel key is used. */
6551 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6552 chan_reg_key
= ua_chan
->tracing_channel_id
;
6554 chan_reg_key
= ua_chan
->key
;
6557 auto& ust_reg_chan
= locked_registry_session
->channel(chan_reg_key
);
6559 /* Channel id is set during the object creation. */
6560 chan_id
= ust_reg_chan
.id
;
6563 * The application returns the typing information of the channel's
6564 * context fields. In per-PID buffering mode, this is the first and only
6565 * time we get this information. It is our chance to finalize the
6566 * initialiation of the channel and serialize it's layout's description
6567 * to the trace's metadata.
6569 * However, in per-UID buffering mode, every application will provide
6570 * this information (redundantly). The first time will allow us to
6571 * complete the initialization. The following times, we simply validate
6572 * that all apps provide the same typing for the context fields as a
6576 auto app_context_fields
= lsu::create_trace_fields_from_ust_ctl_fields(
6577 *locked_registry_session
,
6578 ust_ctl_context_fields
.get(),
6579 context_field_count
,
6580 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT
,
6581 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
);
6583 if (!ust_reg_chan
.is_registered()) {
6584 lst::type::cuptr event_context
= app_context_fields
.size() ?
6585 lttng::make_unique
<lst::structure_type
>(
6586 0, std::move(app_context_fields
)) :
6589 ust_reg_chan
.event_context(std::move(event_context
));
6592 * Validate that the context fields match between
6593 * registry and newcoming application.
6595 bool context_fields_match
;
6596 const auto *previous_event_context
= ust_reg_chan
.event_context();
6598 if (!previous_event_context
) {
6599 context_fields_match
= app_context_fields
.size() == 0;
6601 const lst::structure_type
app_event_context_struct(
6602 0, std::move(app_context_fields
));
6604 context_fields_match
= *previous_event_context
==
6605 app_event_context_struct
;
6608 if (!context_fields_match
) {
6609 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6616 } catch (std::exception
& ex
) {
6617 ERR("Failed to handle application context: %s", ex
.what());
6623 DBG3("UST app replying to register channel key %" PRIu64
" with id %u, ret = %d",
6628 ret
= lttng_ust_ctl_reply_register_channel(
6631 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6632 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6633 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6636 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6637 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6640 } else if (ret
== -EAGAIN
) {
6641 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6645 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6654 /* This channel registry's registration is completed. */
6655 ust_reg_chan
.set_as_registered();
6661 * Add event to the UST channel registry. When the event is added to the
6662 * registry, the metadata is also created. Once done, this replies to the
6663 * application with the appropriate error code.
6665 * The session UST registry lock is acquired in the function.
6667 * On success 0 is returned else a negative value.
6669 static int add_event_ust_registry(int sock
,
6673 char *raw_signature
,
6675 struct lttng_ust_ctl_field
*raw_fields
,
6677 char *raw_model_emf_uri
)
6680 uint32_t event_id
= 0;
6681 uint64_t chan_reg_key
;
6682 struct ust_app
*app
;
6683 struct ust_app_channel
*ua_chan
;
6684 struct ust_app_session
*ua_sess
;
6685 lttng::urcu::read_lock_guard rcu_lock
;
6686 auto signature
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_signature
);
6687 auto fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_fields
);
6688 auto model_emf_uri
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_model_emf_uri
);
6690 /* Lookup application. If not found, there is a code flow error. */
6691 app
= find_app_by_notify_sock(sock
);
6693 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6697 /* Lookup channel by UST object descriptor. */
6698 ua_chan
= find_channel_by_objd(app
, cobjd
);
6700 DBG("Application channel is being torn down. Abort event notify");
6704 LTTNG_ASSERT(ua_chan
->session
);
6705 ua_sess
= ua_chan
->session
;
6707 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6708 chan_reg_key
= ua_chan
->tracing_channel_id
;
6710 chan_reg_key
= ua_chan
->key
;
6714 auto locked_registry
= get_locked_session_registry(ua_sess
);
6715 if (locked_registry
) {
6717 * From this point on, this call acquires the ownership of the signature,
6718 * fields and model_emf_uri meaning any free are done inside it if needed.
6719 * These three variables MUST NOT be read/write after this.
6722 auto& channel
= locked_registry
->channel(chan_reg_key
);
6724 /* event_id is set on success. */
6730 lsu::create_trace_fields_from_ust_ctl_fields(
6734 lst::field_location::root::EVENT_RECORD_PAYLOAD
,
6735 lsu::ctl_field_quirks::
6736 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
),
6738 model_emf_uri
.get() ?
6739 nonstd::optional
<std::string
>(model_emf_uri
.get()) :
6741 ua_sess
->buffer_type
,
6745 } catch (const std::exception
& ex
) {
6746 ERR("Failed to add event `%s` to registry session: %s",
6749 /* Inform the application of the error; don't return directly. */
6753 DBG("Application session is being torn down. Abort event notify");
6759 * The return value is returned to ustctl so in case of an error, the
6760 * application can be notified. In case of an error, it's important not to
6761 * return a negative error or else the application will get closed.
6763 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6765 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6766 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6769 } else if (ret
== -EAGAIN
) {
6770 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6774 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6780 * No need to wipe the create event since the application socket will
6781 * get close on error hence cleaning up everything by itself.
6786 DBG3("UST registry event %s with id %" PRId32
" added successfully", name
, event_id
);
6791 * Add enum to the UST session registry. Once done, this replies to the
6792 * application with the appropriate error code.
6794 * The session UST registry lock is acquired within this function.
6796 * On success 0 is returned else a negative value.
6798 static int add_enum_ust_registry(int sock
,
6801 struct lttng_ust_ctl_enum_entry
*raw_entries
,
6805 struct ust_app
*app
;
6806 struct ust_app_session
*ua_sess
;
6807 uint64_t enum_id
= -1ULL;
6808 lttng::urcu::read_lock_guard read_lock_guard
;
6809 auto entries
= lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::free
>(
6812 /* Lookup application. If not found, there is a code flow error. */
6813 app
= find_app_by_notify_sock(sock
);
6815 /* Return an error since this is not an error */
6816 DBG("Application socket %d is being torn down. Aborting enum registration", sock
);
6820 /* Lookup session by UST object descriptor. */
6821 ua_sess
= find_session_by_objd(app
, sobjd
);
6823 /* Return an error since this is not an error */
6824 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6828 auto locked_registry
= get_locked_session_registry(ua_sess
);
6829 if (!locked_registry
) {
6830 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6835 * From this point on, the callee acquires the ownership of
6836 * entries. The variable entries MUST NOT be read/written after
6839 int application_reply_code
;
6841 locked_registry
->create_or_find_enum(
6842 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6843 application_reply_code
= 0;
6844 } catch (const std::exception
& ex
) {
6847 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6852 application_reply_code
= -1;
6856 * The return value is returned to ustctl so in case of an error, the
6857 * application can be notified. In case of an error, it's important not to
6858 * return a negative error or else the application will get closed.
6860 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6862 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6863 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6866 } else if (ret
== -EAGAIN
) {
6867 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6871 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6877 * No need to wipe the create enum since the application socket will
6878 * get close on error hence cleaning up everything by itself.
6883 DBG3("UST registry enum %s added successfully or already found", name
);
6888 * Handle application notification through the given notify socket.
6890 * Return 0 on success or else a negative value.
6892 int ust_app_recv_notify(int sock
)
6895 enum lttng_ust_ctl_notify_cmd cmd
;
6897 DBG3("UST app receiving notify from sock %d", sock
);
6899 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6901 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6902 DBG3("UST app recv notify failed. Application died: sock = %d", sock
);
6903 } else if (ret
== -EAGAIN
) {
6904 WARN("UST app recv notify failed. Communication time out: sock = %d", sock
);
6906 ERR("UST app recv notify failed with ret %d: sock = %d", ret
, sock
);
6912 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6914 int sobjd
, cobjd
, loglevel_value
;
6915 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6917 struct lttng_ust_ctl_field
*fields
;
6919 DBG2("UST app ustctl register event received");
6921 ret
= lttng_ust_ctl_recv_register_event(sock
,
6931 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6932 DBG3("UST app recv event failed. Application died: sock = %d",
6934 } else if (ret
== -EAGAIN
) {
6935 WARN("UST app recv event failed. Communication time out: sock = %d",
6938 ERR("UST app recv event failed with ret %d: sock = %d", ret
, sock
);
6944 lttng::urcu::read_lock_guard rcu_lock
;
6945 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6947 DBG("Application socket %d is being torn down. Abort event notify",
6954 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6955 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6964 * Add event to the UST registry coming from the notify socket. This
6965 * call will free if needed the sig, fields and model_emf_uri. This
6966 * code path loses the ownsership of these variables and transfer them
6967 * to the this function.
6969 ret
= add_event_ust_registry(sock
,
6984 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6988 struct lttng_ust_ctl_field
*context_fields
;
6990 DBG2("UST app ustctl register channel received");
6992 ret
= lttng_ust_ctl_recv_register_channel(
6993 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6995 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6996 DBG3("UST app recv channel failed. Application died: sock = %d",
6998 } else if (ret
== -EAGAIN
) {
6999 WARN("UST app recv channel failed. Communication time out: sock = %d",
7002 ERR("UST app recv channel failed with ret %d: sock = %d",
7010 * The fields ownership are transfered to this function call meaning
7011 * that if needed it will be freed. After this, it's invalid to access
7012 * fields or clean them up.
7014 ret
= handle_app_register_channel_notification(
7015 sock
, cobjd
, context_fields
, field_count
);
7022 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
7025 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
7027 struct lttng_ust_ctl_enum_entry
*entries
;
7029 DBG2("UST app ustctl register enum received");
7031 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
, &entries
, &nr_entries
);
7033 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
7034 DBG3("UST app recv enum failed. Application died: sock = %d", sock
);
7035 } else if (ret
== -EAGAIN
) {
7036 WARN("UST app recv enum failed. Communication time out: sock = %d",
7039 ERR("UST app recv enum failed with ret %d: sock = %d", ret
, sock
);
7044 /* Callee assumes ownership of entries. */
7045 ret
= add_enum_ust_registry(sock
, sobjd
, name
, entries
, nr_entries
);
7053 /* Should NEVER happen. */
7062 * Once the notify socket hangs up, this is called. First, it tries to find the
7063 * corresponding application. On failure, the call_rcu to close the socket is
7064 * executed. If an application is found, it tries to delete it from the notify
7065 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7067 * Note that an object needs to be allocated here so on ENOMEM failure, the
7068 * call RCU is not done but the rest of the cleanup is.
7070 void ust_app_notify_sock_unregister(int sock
)
7073 struct lttng_ht_iter iter
;
7074 struct ust_app
*app
;
7075 struct ust_app_notify_sock_obj
*obj
;
7077 LTTNG_ASSERT(sock
>= 0);
7079 lttng::urcu::read_lock_guard read_lock
;
7081 obj
= zmalloc
<ust_app_notify_sock_obj
>();
7084 * An ENOMEM is kind of uncool. If this strikes we continue the
7085 * procedure but the call_rcu will not be called. In this case, we
7086 * accept the fd leak rather than possibly creating an unsynchronized
7087 * state between threads.
7089 * TODO: The notify object should be created once the notify socket is
7090 * registered and stored independantely from the ust app object. The
7091 * tricky part is to synchronize the teardown of the application and
7092 * this notify object. Let's keep that in mind so we can avoid this
7093 * kind of shenanigans with ENOMEM in the teardown path.
7100 DBG("UST app notify socket unregister %d", sock
);
7103 * Lookup application by notify socket. If this fails, this means that the
7104 * hash table delete has already been done by the application
7105 * unregistration process so we can safely close the notify socket in a
7108 app
= find_app_by_notify_sock(sock
);
7113 iter
.iter
.node
= &app
->notify_sock_n
.node
;
7116 * Whatever happens here either we fail or succeed, in both cases we have
7117 * to close the socket after a grace period to continue to the call RCU
7118 * here. If the deletion is successful, the application is not visible
7119 * anymore by other threads and is it fails it means that it was already
7120 * deleted from the hash table so either way we just have to close the
7123 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
7128 * Close socket after a grace period to avoid for the socket to be reused
7129 * before the application object is freed creating potential race between
7130 * threads trying to add unique in the global hash table.
7133 call_rcu(&obj
->head
, close_notify_sock_rcu
);
7138 * Destroy a ust app data structure and free its memory.
7140 void ust_app_destroy(struct ust_app
*app
)
7146 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
7150 * Take a snapshot for a given UST session. The snapshot is sent to the given
7153 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7155 enum lttng_error_code
ust_app_snapshot_record(const struct ltt_ust_session
*usess
,
7156 const struct consumer_output
*output
,
7157 uint64_t nb_packets_per_stream
)
7160 enum lttng_error_code status
= LTTNG_OK
;
7161 struct lttng_ht_iter iter
;
7162 struct ust_app
*app
;
7163 char *trace_path
= nullptr;
7165 LTTNG_ASSERT(usess
);
7166 LTTNG_ASSERT(output
);
7168 switch (usess
->buffer_type
) {
7169 case LTTNG_BUFFER_PER_UID
:
7171 struct buffer_reg_uid
*reg
;
7173 lttng::urcu::read_lock_guard read_lock
;
7175 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7176 struct buffer_reg_channel
*buf_reg_chan
;
7177 struct consumer_socket
*socket
;
7178 char pathname
[PATH_MAX
];
7179 size_t consumer_path_offset
= 0;
7181 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7182 /* Skip since no metadata is present */
7186 /* Get consumer socket to use to push the metadata.*/
7187 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7190 status
= LTTNG_ERR_INVALID
;
7194 memset(pathname
, 0, sizeof(pathname
));
7195 ret
= snprintf(pathname
,
7197 DEFAULT_UST_TRACE_UID_PATH
,
7199 reg
->bits_per_long
);
7201 PERROR("snprintf snapshot path");
7202 status
= LTTNG_ERR_INVALID
;
7205 /* Free path allowed on previous iteration. */
7207 trace_path
= setup_channel_trace_path(
7208 usess
->consumer
, pathname
, &consumer_path_offset
);
7210 status
= LTTNG_ERR_INVALID
;
7213 /* Add the UST default trace dir to path. */
7214 cds_lfht_for_each_entry (
7215 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7217 consumer_snapshot_channel(socket
,
7218 buf_reg_chan
->consumer_key
,
7221 &trace_path
[consumer_path_offset
],
7222 nb_packets_per_stream
);
7223 if (status
!= LTTNG_OK
) {
7227 status
= consumer_snapshot_channel(socket
,
7228 reg
->registry
->reg
.ust
->_metadata_key
,
7231 &trace_path
[consumer_path_offset
],
7233 if (status
!= LTTNG_OK
) {
7240 case LTTNG_BUFFER_PER_PID
:
7242 lttng::urcu::read_lock_guard read_lock
;
7244 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7245 struct consumer_socket
*socket
;
7246 struct lttng_ht_iter chan_iter
;
7247 struct ust_app_channel
*ua_chan
;
7248 struct ust_app_session
*ua_sess
;
7249 lsu::registry_session
*registry
;
7250 char pathname
[PATH_MAX
];
7251 size_t consumer_path_offset
= 0;
7253 ua_sess
= lookup_session_by_app(usess
, app
);
7255 /* Session not associated with this app. */
7259 /* Get the right consumer socket for the application. */
7260 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, output
);
7262 status
= LTTNG_ERR_INVALID
;
7266 /* Add the UST default trace dir to path. */
7267 memset(pathname
, 0, sizeof(pathname
));
7268 ret
= snprintf(pathname
, sizeof(pathname
), "%s", ua_sess
->path
);
7270 status
= LTTNG_ERR_INVALID
;
7271 PERROR("snprintf snapshot path");
7274 /* Free path allowed on previous iteration. */
7276 trace_path
= setup_channel_trace_path(
7277 usess
->consumer
, pathname
, &consumer_path_offset
);
7279 status
= LTTNG_ERR_INVALID
;
7282 cds_lfht_for_each_entry (
7283 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7285 consumer_snapshot_channel(socket
,
7289 &trace_path
[consumer_path_offset
],
7290 nb_packets_per_stream
);
7294 case LTTNG_ERR_CHAN_NOT_FOUND
:
7301 registry
= get_session_registry(ua_sess
);
7303 DBG("Application session is being torn down. Skip application.");
7306 status
= consumer_snapshot_channel(socket
,
7307 registry
->_metadata_key
,
7310 &trace_path
[consumer_path_offset
],
7315 case LTTNG_ERR_CHAN_NOT_FOUND
:
7334 * Return the size taken by one more packet per stream.
7336 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session
*usess
,
7337 uint64_t cur_nr_packets
)
7339 uint64_t tot_size
= 0;
7340 struct ust_app
*app
;
7341 struct lttng_ht_iter iter
;
7343 LTTNG_ASSERT(usess
);
7345 switch (usess
->buffer_type
) {
7346 case LTTNG_BUFFER_PER_UID
:
7348 struct buffer_reg_uid
*reg
;
7350 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7351 struct buffer_reg_channel
*buf_reg_chan
;
7353 lttng::urcu::read_lock_guard read_lock
;
7355 cds_lfht_for_each_entry (
7356 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7357 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7359 * Don't take channel into account if we
7360 * already grab all its packets.
7364 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7369 case LTTNG_BUFFER_PER_PID
:
7371 lttng::urcu::read_lock_guard read_lock
;
7373 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7374 struct ust_app_channel
*ua_chan
;
7375 struct ust_app_session
*ua_sess
;
7376 struct lttng_ht_iter chan_iter
;
7378 ua_sess
= lookup_session_by_app(usess
, app
);
7380 /* Session not associated with this app. */
7384 cds_lfht_for_each_entry (
7385 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7386 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7388 * Don't take channel into account if we
7389 * already grab all its packets.
7393 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7406 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7407 struct cds_list_head
*buffer_reg_uid_list
,
7408 struct consumer_output
*consumer
,
7411 uint64_t *discarded
,
7415 uint64_t consumer_chan_key
;
7420 ret
= buffer_reg_uid_consumer_channel_key(
7421 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7429 ret
= consumer_get_lost_packets(ust_session_id
, consumer_chan_key
, consumer
, lost
);
7431 ret
= consumer_get_discarded_events(
7432 ust_session_id
, consumer_chan_key
, consumer
, discarded
);
7439 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7440 struct ltt_ust_channel
*uchan
,
7441 struct consumer_output
*consumer
,
7443 uint64_t *discarded
,
7447 struct lttng_ht_iter iter
;
7448 struct lttng_ht_node_str
*ua_chan_node
;
7449 struct ust_app
*app
;
7450 struct ust_app_session
*ua_sess
;
7451 struct ust_app_channel
*ua_chan
;
7457 * Iterate over every registered applications. Sum counters for
7458 * all applications containing requested session and channel.
7460 lttng::urcu::read_lock_guard read_lock
;
7462 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7463 struct lttng_ht_iter uiter
;
7465 ua_sess
= lookup_session_by_app(usess
, app
);
7466 if (ua_sess
== nullptr) {
7471 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7472 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7473 /* If the session is found for the app, the channel must be there */
7474 LTTNG_ASSERT(ua_chan_node
);
7476 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7481 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
, consumer
, &_lost
);
7487 uint64_t _discarded
;
7489 ret
= consumer_get_discarded_events(
7490 usess
->id
, ua_chan
->key
, consumer
, &_discarded
);
7494 (*discarded
) += _discarded
;
7501 static int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7504 struct ust_app_session
*ua_sess
;
7506 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7508 lttng::urcu::read_lock_guard read_lock
;
7510 ua_sess
= lookup_session_by_app(usess
, app
);
7511 if (ua_sess
== nullptr) {
7512 /* The session is in teardown process. Ignore and continue. */
7516 pthread_mutex_lock(&ua_sess
->lock
);
7518 if (ua_sess
->deleted
) {
7522 pthread_mutex_lock(&app
->sock_lock
);
7523 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7524 pthread_mutex_unlock(&app
->sock_lock
);
7527 pthread_mutex_unlock(&ua_sess
->lock
);
7530 health_code_update();
7535 * Regenerate the statedump for each app in the session.
7537 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7540 struct lttng_ht_iter iter
;
7541 struct ust_app
*app
;
7543 DBG("Regenerating the metadata for all UST apps");
7545 lttng::urcu::read_lock_guard read_lock
;
7547 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7548 if (!app
->compatible
) {
7552 ret
= ust_app_regenerate_statedump(usess
, app
);
7554 /* Continue to the next app even on error */
7563 * Rotate all the channels of a session.
7565 * Return LTTNG_OK on success or else an LTTng error code.
7567 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7570 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7571 struct lttng_ht_iter iter
;
7572 struct ust_app
*app
;
7573 struct ltt_ust_session
*usess
= session
->ust_session
;
7575 LTTNG_ASSERT(usess
);
7577 switch (usess
->buffer_type
) {
7578 case LTTNG_BUFFER_PER_UID
:
7580 struct buffer_reg_uid
*reg
;
7582 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7583 struct buffer_reg_channel
*buf_reg_chan
;
7584 struct consumer_socket
*socket
;
7585 lttng::urcu::read_lock_guard read_lock
;
7587 /* Get consumer socket to use to push the metadata.*/
7588 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7591 cmd_ret
= LTTNG_ERR_INVALID
;
7595 /* Rotate the data channels. */
7596 cds_lfht_for_each_entry (
7597 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7598 ret
= consumer_rotate_channel(socket
,
7599 buf_reg_chan
->consumer_key
,
7601 /* is_metadata_channel */ false);
7603 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7609 * The metadata channel might not be present.
7611 * Consumer stream allocation can be done
7612 * asynchronously and can fail on intermediary
7613 * operations (i.e add context) and lead to data
7614 * channels created with no metadata channel.
7616 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7617 /* Skip since no metadata is present. */
7622 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7623 (void) push_metadata(locked_registry
, usess
->consumer
);
7626 ret
= consumer_rotate_channel(socket
,
7627 reg
->registry
->reg
.ust
->_metadata_key
,
7629 /* is_metadata_channel */ true);
7631 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7637 case LTTNG_BUFFER_PER_PID
:
7639 lttng::urcu::read_lock_guard read_lock
;
7641 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7642 struct consumer_socket
*socket
;
7643 struct lttng_ht_iter chan_iter
;
7644 struct ust_app_channel
*ua_chan
;
7645 struct ust_app_session
*ua_sess
;
7646 lsu::registry_session
*registry
;
7648 ua_sess
= lookup_session_by_app(usess
, app
);
7650 /* Session not associated with this app. */
7654 /* Get the right consumer socket for the application. */
7655 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7658 cmd_ret
= LTTNG_ERR_INVALID
;
7662 registry
= get_session_registry(ua_sess
);
7664 DBG("Application session is being torn down. Skip application.");
7668 /* Rotate the data channels. */
7669 cds_lfht_for_each_entry (
7670 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7671 ret
= consumer_rotate_channel(socket
,
7674 /* is_metadata_channel */ false);
7676 /* Per-PID buffer and application going away. */
7677 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7679 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7684 /* Rotate the metadata channel. */
7686 auto locked_registry
= registry
->lock();
7688 (void) push_metadata(locked_registry
, usess
->consumer
);
7690 ret
= consumer_rotate_channel(socket
,
7691 registry
->_metadata_key
,
7693 /* is_metadata_channel */ true);
7695 /* Per-PID buffer and application going away. */
7696 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7698 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7715 enum lttng_error_code
ust_app_create_channel_subdirectories(const struct ltt_ust_session
*usess
)
7717 enum lttng_error_code ret
= LTTNG_OK
;
7718 struct lttng_ht_iter iter
;
7719 enum lttng_trace_chunk_status chunk_status
;
7720 char *pathname_index
;
7723 LTTNG_ASSERT(usess
->current_trace_chunk
);
7725 switch (usess
->buffer_type
) {
7726 case LTTNG_BUFFER_PER_UID
:
7728 struct buffer_reg_uid
*reg
;
7729 lttng::urcu::read_lock_guard read_lock
;
7731 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7732 fmt_ret
= asprintf(&pathname_index
,
7733 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
7734 "/" DEFAULT_INDEX_DIR
,
7736 reg
->bits_per_long
);
7738 ERR("Failed to format channel index directory");
7739 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7744 * Create the index subdirectory which will take care
7745 * of implicitly creating the channel's path.
7747 chunk_status
= lttng_trace_chunk_create_subdirectory(
7748 usess
->current_trace_chunk
, pathname_index
);
7749 free(pathname_index
);
7750 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7751 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7757 case LTTNG_BUFFER_PER_PID
:
7759 struct ust_app
*app
;
7760 lttng::urcu::read_lock_guard read_lock
;
7763 * Create the toplevel ust/ directory in case no apps are running.
7765 chunk_status
= lttng_trace_chunk_create_subdirectory(usess
->current_trace_chunk
,
7766 DEFAULT_UST_TRACE_DIR
);
7767 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7768 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7772 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7773 struct ust_app_session
*ua_sess
;
7774 lsu::registry_session
*registry
;
7776 ua_sess
= lookup_session_by_app(usess
, app
);
7778 /* Session not associated with this app. */
7782 registry
= get_session_registry(ua_sess
);
7784 DBG("Application session is being torn down. Skip application.");
7788 fmt_ret
= asprintf(&pathname_index
,
7789 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7792 ERR("Failed to format channel index directory");
7793 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7797 * Create the index subdirectory which will take care
7798 * of implicitly creating the channel's path.
7800 chunk_status
= lttng_trace_chunk_create_subdirectory(
7801 usess
->current_trace_chunk
, pathname_index
);
7802 free(pathname_index
);
7803 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7804 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7820 * Clear all the channels of a session.
7822 * Return LTTNG_OK on success or else an LTTng error code.
7824 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7827 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7828 struct lttng_ht_iter iter
;
7829 struct ust_app
*app
;
7830 struct ltt_ust_session
*usess
= session
->ust_session
;
7832 LTTNG_ASSERT(usess
);
7834 if (usess
->active
) {
7835 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7836 cmd_ret
= LTTNG_ERR_FATAL
;
7840 switch (usess
->buffer_type
) {
7841 case LTTNG_BUFFER_PER_UID
:
7843 struct buffer_reg_uid
*reg
;
7844 lttng::urcu::read_lock_guard read_lock
;
7846 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7847 struct buffer_reg_channel
*buf_reg_chan
;
7848 struct consumer_socket
*socket
;
7850 /* Get consumer socket to use to push the metadata.*/
7851 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7854 cmd_ret
= LTTNG_ERR_INVALID
;
7858 /* Clear the data channels. */
7859 cds_lfht_for_each_entry (
7860 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7861 ret
= consumer_clear_channel(socket
, buf_reg_chan
->consumer_key
);
7868 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7869 (void) push_metadata(locked_registry
, usess
->consumer
);
7873 * Clear the metadata channel.
7874 * Metadata channel is not cleared per se but we still need to
7875 * perform a rotation operation on it behind the scene.
7877 ret
= consumer_clear_channel(socket
, reg
->registry
->reg
.ust
->_metadata_key
);
7884 case LTTNG_BUFFER_PER_PID
:
7886 lttng::urcu::read_lock_guard read_lock
;
7888 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7889 struct consumer_socket
*socket
;
7890 struct lttng_ht_iter chan_iter
;
7891 struct ust_app_channel
*ua_chan
;
7892 struct ust_app_session
*ua_sess
;
7893 lsu::registry_session
*registry
;
7895 ua_sess
= lookup_session_by_app(usess
, app
);
7897 /* Session not associated with this app. */
7901 /* Get the right consumer socket for the application. */
7902 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7905 cmd_ret
= LTTNG_ERR_INVALID
;
7909 registry
= get_session_registry(ua_sess
);
7911 DBG("Application session is being torn down. Skip application.");
7915 /* Clear the data channels. */
7916 cds_lfht_for_each_entry (
7917 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7918 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7920 /* Per-PID buffer and application going away. */
7921 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7929 auto locked_registry
= registry
->lock();
7930 (void) push_metadata(locked_registry
, usess
->consumer
);
7934 * Clear the metadata channel.
7935 * Metadata channel is not cleared per se but we still need to
7936 * perform rotation operation on it behind the scene.
7938 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7940 /* Per-PID buffer and application going away. */
7941 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7959 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7960 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7963 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7972 * This function skips the metadata channel as the begin/end timestamps of a
7973 * metadata packet are useless.
7975 * Moreover, opening a packet after a "clear" will cause problems for live
7976 * sessions as it will introduce padding that was not part of the first trace
7977 * chunk. The relay daemon expects the content of the metadata stream of
7978 * successive metadata trace chunks to be strict supersets of one another.
7980 * For example, flushing a packet at the beginning of the metadata stream of
7981 * a trace chunk resulting from a "clear" session command will cause the
7982 * size of the metadata stream of the new trace chunk to not match the size of
7983 * the metadata stream of the original chunk. This will confuse the relay
7984 * daemon as the same "offset" in a metadata stream will no longer point
7985 * to the same content.
7987 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7989 enum lttng_error_code ret
= LTTNG_OK
;
7990 struct lttng_ht_iter iter
;
7991 struct ltt_ust_session
*usess
= session
->ust_session
;
7993 LTTNG_ASSERT(usess
);
7995 switch (usess
->buffer_type
) {
7996 case LTTNG_BUFFER_PER_UID
:
7998 struct buffer_reg_uid
*reg
;
8000 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
8001 struct buffer_reg_channel
*buf_reg_chan
;
8002 struct consumer_socket
*socket
;
8003 lttng::urcu::read_lock_guard read_lock
;
8005 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
8008 ret
= LTTNG_ERR_FATAL
;
8012 cds_lfht_for_each_entry (
8013 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
8014 const int open_ret
= consumer_open_channel_packets(
8015 socket
, buf_reg_chan
->consumer_key
);
8018 ret
= LTTNG_ERR_UNK
;
8025 case LTTNG_BUFFER_PER_PID
:
8027 struct ust_app
*app
;
8028 lttng::urcu::read_lock_guard read_lock
;
8030 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8031 struct consumer_socket
*socket
;
8032 struct lttng_ht_iter chan_iter
;
8033 struct ust_app_channel
*ua_chan
;
8034 struct ust_app_session
*ua_sess
;
8035 lsu::registry_session
*registry
;
8037 ua_sess
= lookup_session_by_app(usess
, app
);
8039 /* Session not associated with this app. */
8043 /* Get the right consumer socket for the application. */
8044 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
8047 ret
= LTTNG_ERR_FATAL
;
8051 registry
= get_session_registry(ua_sess
);
8053 DBG("Application session is being torn down. Skip application.");
8057 cds_lfht_for_each_entry (
8058 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
8059 const int open_ret
=
8060 consumer_open_channel_packets(socket
, ua_chan
->key
);
8064 * Per-PID buffer and application going
8067 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
8071 ret
= LTTNG_ERR_UNK
;
8087 lsu::ctl_field_quirks
ust_app::ctl_field_quirks() const
8090 * Application contexts are expressed as variants. LTTng-UST announces
8091 * those by registering an enumeration named `..._tag`. It then registers a
8092 * variant as part of the event context that contains the various possible
8095 * Unfortunately, the names used in the enumeration and variant don't
8096 * match: the enumeration names are all prefixed with an underscore while
8097 * the variant type tag fields aren't.
8099 * While the CTF 1.8.3 specification mentions that
8100 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8101 * 1.x (and possibly others) expect a perfect match between the names used
8102 * by tags and variants.
8104 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8105 * the variant's fields are modified to match the mappings of its tag.
8107 * From ABI version >= 10.x, the variant fields and tag mapping names
8108 * correctly match, making this quirk unnecessary.
8110 return v_major
<= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
:
8111 lsu::ctl_field_quirks::NONE
;