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"
22 #include "session.hpp"
23 #include "ust-app.hpp"
24 #include "ust-consumer.hpp"
25 #include "ust-field-quirks.hpp"
28 #include <common/bytecode/bytecode.hpp>
29 #include <common/common.hpp>
30 #include <common/compat/errno.hpp>
31 #include <common/exception.hpp>
32 #include <common/format.hpp>
33 #include <common/hashtable/utils.hpp>
34 #include <common/make-unique.hpp>
35 #include <common/pthread-lock.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(ust_app
& app
, 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
;
80 * Return the session registry according to the buffer type of the given
83 * A registry per UID object MUST exists before calling this function or else
84 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
86 lsu::registry_session
*ust_app_get_session_registry(const ust_app_session::identifier
& ua_sess_id
)
88 lsu::registry_session
*registry
= nullptr;
90 switch (ua_sess_id
.allocation_policy
) {
91 case ust_app_session::identifier::buffer_allocation_policy::PER_PID
:
93 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess_id
.id
);
97 registry
= reg_pid
->registry
->reg
.ust
;
100 case ust_app_session::identifier::buffer_allocation_policy::PER_UID
:
102 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
103 ua_sess_id
.session_id
,
104 ua_sess_id
.abi
== ust_app_session::identifier::application_abi::ABI_32
?
107 lttng_credentials_get_uid(&ua_sess_id
.app_credentials
));
111 registry
= reg_uid
->registry
->reg
.ust
;
123 lsu::registry_session::locked_ref
124 get_locked_session_registry(const ust_app_session::identifier
& identifier
)
126 auto session
= ust_app_get_session_registry(identifier
);
128 pthread_mutex_lock(&session
->_lock
);
131 return lsu::registry_session::locked_ref
{ 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
;
188 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
189 key
= (ust_app_ht_key
*) _key
;
191 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
194 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
198 /* Event loglevel. */
199 if (!loglevels_match(event
->attr
.loglevel_type
,
200 event
->attr
.loglevel
,
203 LTTNG_UST_ABI_LOGLEVEL_ALL
)) {
207 /* One of the filters is NULL, fail. */
208 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
212 if (key
->filter
&& event
->filter
) {
213 /* Both filters exists, check length followed by the bytecode. */
214 if (event
->filter
->len
!= key
->filter
->len
||
215 memcmp(event
->filter
->data
, key
->filter
->data
, event
->filter
->len
) != 0) {
220 /* One of the exclusions is NULL, fail. */
221 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
225 if (key
->exclusion
&& event
->exclusion
) {
226 /* Both exclusions exists, check count followed by the names. */
227 if (event
->exclusion
->count
!= key
->exclusion
->count
||
228 memcmp(event
->exclusion
->names
,
229 key
->exclusion
->names
,
230 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
243 * Unique add of an ust app event in the given ht. This uses the custom
244 * ht_match_ust_app_event match function and the event name as hash.
246 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
, struct ust_app_event
*event
)
248 struct cds_lfht_node
*node_ptr
;
249 struct ust_app_ht_key key
;
252 LTTNG_ASSERT(ua_chan
);
253 LTTNG_ASSERT(ua_chan
->events
);
256 ht
= ua_chan
->events
;
257 key
.name
= event
->attr
.name
;
258 key
.filter
= event
->filter
;
259 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel_type
;
260 key
.loglevel_value
= event
->attr
.loglevel
;
261 key
.exclusion
= event
->exclusion
;
263 node_ptr
= cds_lfht_add_unique(ht
->ht
,
264 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
265 ht_match_ust_app_event
,
268 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
272 * Close the notify socket from the given RCU head object. This MUST be called
273 * through a call_rcu().
275 static void close_notify_sock_rcu(struct rcu_head
*head
)
278 struct ust_app_notify_sock_obj
*obj
=
279 lttng::utils::container_of(head
, &ust_app_notify_sock_obj::head
);
281 /* Must have a valid fd here. */
282 LTTNG_ASSERT(obj
->fd
>= 0);
284 ret
= close(obj
->fd
);
286 ERR("close notify sock %d RCU", obj
->fd
);
288 lttng_fd_put(LTTNG_FD_APPS
, 1);
294 * Delete ust context safely. RCU read lock must be held before calling
297 static void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
301 LTTNG_ASSERT(ua_ctx
);
302 ASSERT_RCU_READ_LOCKED();
305 pthread_mutex_lock(&app
->sock_lock
);
306 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
307 pthread_mutex_unlock(&app
->sock_lock
);
309 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
310 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
313 } else if (ret
== -EAGAIN
) {
314 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
318 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
328 if (ua_ctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
329 free(ua_ctx
->ctx
.u
.app_ctx
.provider_name
);
330 free(ua_ctx
->ctx
.u
.app_ctx
.ctx_name
);
337 * Delete ust app event safely. RCU read lock must be held before calling
340 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
, struct ust_app
*app
)
344 LTTNG_ASSERT(ua_event
);
345 ASSERT_RCU_READ_LOCKED();
347 free(ua_event
->filter
);
348 if (ua_event
->exclusion
!= nullptr)
349 free(ua_event
->exclusion
);
350 if (ua_event
->obj
!= nullptr) {
351 pthread_mutex_lock(&app
->sock_lock
);
352 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
353 pthread_mutex_unlock(&app
->sock_lock
);
355 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
356 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
359 } else if (ret
== -EAGAIN
) {
360 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
364 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
376 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
377 * through a call_rcu().
379 static void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
381 struct ust_app_event_notifier_rule
*obj
=
382 lttng::utils::container_of(head
, &ust_app_event_notifier_rule::rcu_head
);
388 * Delete ust app event notifier rule safely.
390 static void delete_ust_app_event_notifier_rule(
391 int sock
, struct ust_app_event_notifier_rule
*ua_event_notifier_rule
, struct ust_app
*app
)
395 LTTNG_ASSERT(ua_event_notifier_rule
);
397 if (ua_event_notifier_rule
->exclusion
!= nullptr) {
398 free(ua_event_notifier_rule
->exclusion
);
401 if (ua_event_notifier_rule
->obj
!= nullptr) {
402 pthread_mutex_lock(&app
->sock_lock
);
403 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
404 pthread_mutex_unlock(&app
->sock_lock
);
406 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
407 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
410 } else if (ret
== -EAGAIN
) {
411 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
415 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
422 free(ua_event_notifier_rule
->obj
);
425 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
426 call_rcu(&ua_event_notifier_rule
->rcu_head
, free_ust_app_event_notifier_rule_rcu
);
430 * Release ust data object of the given stream.
432 * Return 0 on success or else a negative value.
434 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
438 LTTNG_ASSERT(stream
);
441 pthread_mutex_lock(&app
->sock_lock
);
442 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
443 pthread_mutex_unlock(&app
->sock_lock
);
445 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
446 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
449 } else if (ret
== -EAGAIN
) {
450 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
454 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
460 lttng_fd_put(LTTNG_FD_APPS
, 2);
468 * Delete ust app stream safely. RCU read lock must be held before calling
471 static void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
473 LTTNG_ASSERT(stream
);
474 ASSERT_RCU_READ_LOCKED();
476 (void) release_ust_app_stream(sock
, stream
, app
);
480 static void delete_ust_app_channel_rcu(struct rcu_head
*head
)
482 struct ust_app_channel
*ua_chan
=
483 lttng::utils::container_of(head
, &ust_app_channel::rcu_head
);
485 lttng_ht_destroy(ua_chan
->ctx
);
486 lttng_ht_destroy(ua_chan
->events
);
491 * Extract the lost packet or discarded events counter when the channel is
492 * being deleted and store the value in the parent channel so we can
493 * access it from lttng list and at stop/destroy.
495 * The session list lock must be held by the caller.
497 static void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
499 uint64_t discarded
= 0, lost
= 0;
500 struct ltt_ust_channel
*uchan
;
502 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
506 lttng::urcu::read_lock_guard read_lock
;
509 const auto session
= ltt_session::find_session(ua_chan
->session
->tracing_id
);
511 if (!session
->ust_session
) {
513 * Not finding the session is not an error because there are
514 * multiple ways the channels can be torn down.
516 * 1) The session daemon can initiate the destruction of the
517 * ust app session after receiving a destroy command or
518 * during its shutdown/teardown.
519 * 2) The application, since we are in per-pid tracing, is
520 * unregistering and tearing down its ust app session.
522 * Both paths are protected by the session list lock which
523 * ensures that the accounting of lost packets and discarded
524 * events is done exactly once. The session is then unpublished
525 * from the session list, resulting in this condition.
530 if (ua_chan
->attr
.overwrite
) {
531 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
533 session
->ust_session
->consumer
,
536 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
538 session
->ust_session
->consumer
,
541 uchan
= trace_ust_find_channel_by_name(session
->ust_session
->domain_global
.channels
,
544 ERR("Missing UST channel to store discarded counters");
547 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
548 DBG_FMT("Failed to save per-pid lost/discarded counters: {}, location='{}'",
554 uchan
->per_pid_closed_app_discarded
+= discarded
;
555 uchan
->per_pid_closed_app_lost
+= lost
;
559 * Delete ust app channel safely. RCU read lock must be held before calling
562 * The session list lock must be held by the caller.
564 static void delete_ust_app_channel(int sock
,
565 struct ust_app_channel
*ua_chan
,
567 const lsu::registry_session::locked_ref
& locked_registry
)
570 struct lttng_ht_iter iter
;
571 struct ust_app_event
*ua_event
;
572 struct ust_app_ctx
*ua_ctx
;
573 struct ust_app_stream
*stream
, *stmp
;
575 LTTNG_ASSERT(ua_chan
);
576 ASSERT_RCU_READ_LOCKED();
578 DBG3("UST app deleting channel %s", ua_chan
->name
);
581 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
582 cds_list_del(&stream
->list
);
583 delete_ust_app_stream(sock
, stream
, app
);
587 cds_lfht_for_each_entry (ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
588 cds_list_del(&ua_ctx
->list
);
589 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
591 delete_ust_app_ctx(sock
, ua_ctx
, app
);
595 cds_lfht_for_each_entry (ua_chan
->events
->ht
, &iter
.iter
, ua_event
, node
.node
) {
596 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
598 delete_ust_app_event(sock
, ua_event
, app
);
601 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
602 /* Wipe and free registry from session registry. */
603 if (locked_registry
) {
605 locked_registry
->remove_channel(ua_chan
->key
, sock
>= 0);
606 } catch (const std::exception
& ex
) {
607 DBG("Could not find channel for removal: %s", ex
.what());
612 * A negative socket can be used by the caller when
613 * cleaning-up a ua_chan in an error path. Skip the
614 * accounting in this case.
617 save_per_pid_lost_discarded_counters(ua_chan
);
621 if (ua_chan
->obj
!= nullptr) {
622 /* Remove channel from application UST object descriptor. */
623 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
624 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
626 pthread_mutex_lock(&app
->sock_lock
);
627 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
628 pthread_mutex_unlock(&app
->sock_lock
);
630 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
631 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
635 } else if (ret
== -EAGAIN
) {
636 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
641 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
648 lttng_fd_put(LTTNG_FD_APPS
, 1);
651 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
654 int ust_app_register_done(struct ust_app
*app
)
658 pthread_mutex_lock(&app
->sock_lock
);
659 ret
= lttng_ust_ctl_register_done(app
->sock
);
660 pthread_mutex_unlock(&app
->sock_lock
);
664 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
669 pthread_mutex_lock(&app
->sock_lock
);
674 ret
= lttng_ust_ctl_release_object(sock
, data
);
676 pthread_mutex_unlock(&app
->sock_lock
);
682 * Push metadata to consumer socket.
684 * RCU read-side lock must be held to guarantee existence of socket.
685 * Must be called with the ust app session lock held.
686 * Must be called with the registry lock held.
688 * On success, return the len of metadata pushed or else a negative value.
689 * Returning a -EPIPE return value means we could not send the metadata,
690 * but it can be caused by recoverable errors (e.g. the application has
691 * terminated concurrently).
693 ssize_t
ust_app_push_metadata(const lsu::registry_session::locked_ref
& locked_registry
,
694 struct consumer_socket
*socket
,
698 char *metadata_str
= nullptr;
699 size_t len
, offset
, new_metadata_len_sent
;
701 uint64_t metadata_key
, metadata_version
;
703 LTTNG_ASSERT(locked_registry
);
704 LTTNG_ASSERT(socket
);
705 ASSERT_RCU_READ_LOCKED();
707 metadata_key
= locked_registry
->_metadata_key
;
710 * Means that no metadata was assigned to the session. This can
711 * happens if no start has been done previously.
717 offset
= locked_registry
->_metadata_len_sent
;
718 len
= locked_registry
->_metadata_len
- locked_registry
->_metadata_len_sent
;
719 new_metadata_len_sent
= locked_registry
->_metadata_len
;
720 metadata_version
= locked_registry
->_metadata_version
;
722 DBG3("No metadata to push for metadata key %" PRIu64
,
723 locked_registry
->_metadata_key
);
725 if (send_zero_data
) {
726 DBG("No metadata to push");
732 /* Allocate only what we have to send. */
733 metadata_str
= calloc
<char>(len
);
735 PERROR("zmalloc ust app metadata string");
739 /* Copy what we haven't sent out. */
740 memcpy(metadata_str
, locked_registry
->_metadata
+ offset
, len
);
743 pthread_mutex_unlock(&locked_registry
->_lock
);
745 * We need to unlock the registry while we push metadata to
746 * break a circular dependency between the consumerd metadata
747 * lock and the sessiond registry lock. Indeed, pushing metadata
748 * to the consumerd awaits that it gets pushed all the way to
749 * relayd, but doing so requires grabbing the metadata lock. If
750 * a concurrent metadata request is being performed by
751 * consumerd, this can try to grab the registry lock on the
752 * sessiond while holding the metadata lock on the consumer
753 * daemon. Those push and pull schemes are performed on two
754 * different bidirectionnal communication sockets.
756 ret
= consumer_push_metadata(
757 socket
, metadata_key
, metadata_str
, len
, offset
, metadata_version
);
758 pthread_mutex_lock(&locked_registry
->_lock
);
761 * There is an acceptable race here between the registry
762 * metadata key assignment and the creation on the
763 * consumer. The session daemon can concurrently push
764 * metadata for this registry while being created on the
765 * consumer since the metadata key of the registry is
766 * assigned *before* it is setup to avoid the consumer
767 * to ask for metadata that could possibly be not found
768 * in the session daemon.
770 * The metadata will get pushed either by the session
771 * being stopped or the consumer requesting metadata if
772 * that race is triggered.
774 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
777 ERR("Error pushing metadata to consumer");
783 * Metadata may have been concurrently pushed, since
784 * we're not holding the registry lock while pushing to
785 * consumer. This is handled by the fact that we send
786 * the metadata content, size, and the offset at which
787 * that metadata belongs. This may arrive out of order
788 * on the consumer side, and the consumer is able to
789 * deal with overlapping fragments. The consumer
790 * supports overlapping fragments, which must be
791 * contiguous starting from offset 0. We keep the
792 * largest metadata_len_sent value of the concurrent
795 locked_registry
->_metadata_len_sent
=
796 std::max(locked_registry
->_metadata_len_sent
, new_metadata_len_sent
);
805 * On error, flag the registry that the metadata is
806 * closed. We were unable to push anything and this
807 * means that either the consumer is not responding or
808 * the metadata cache has been destroyed on the
811 locked_registry
->_metadata_closed
= true;
819 * For a given application and session, push metadata to consumer.
820 * Either sock or consumer is required : if sock is NULL, the default
821 * socket to send the metadata is retrieved from consumer, if sock
822 * is not NULL we use it to send the metadata.
823 * RCU read-side lock must be held while calling this function,
824 * therefore ensuring existence of registry. It also ensures existence
825 * of socket throughout this function.
827 * Return 0 on success else a negative error.
828 * Returning a -EPIPE return value means we could not send the metadata,
829 * but it can be caused by recoverable errors (e.g. the application has
830 * terminated concurrently).
832 static int push_metadata(const lsu::registry_session::locked_ref
& locked_registry
,
833 struct consumer_output
*consumer
)
837 struct consumer_socket
*socket
;
839 LTTNG_ASSERT(locked_registry
);
840 LTTNG_ASSERT(consumer
);
841 ASSERT_RCU_READ_LOCKED();
843 if (locked_registry
->_metadata_closed
) {
848 /* Get consumer socket to use to push the metadata.*/
849 socket
= consumer_find_socket_by_bitness(locked_registry
->abi
.bits_per_long
, consumer
);
855 ret
= ust_app_push_metadata(locked_registry
, socket
, 0);
867 * Send to the consumer a close metadata command for the given session. Once
868 * done, the metadata channel is deleted and the session metadata pointer is
869 * nullified. The session lock MUST be held unless the application is
870 * in the destroy path.
872 * Do not hold the registry lock while communicating with the consumerd, because
873 * doing so causes inter-process deadlocks between consumerd and sessiond with
874 * the metadata request notification.
876 * Return 0 on success else a negative value.
878 static int close_metadata(uint64_t metadata_key
,
879 unsigned int consumer_bitness
,
880 struct consumer_output
*consumer
)
883 struct consumer_socket
*socket
;
884 lttng::urcu::read_lock_guard read_lock_guard
;
886 LTTNG_ASSERT(consumer
);
888 /* Get consumer socket to use to push the metadata. */
889 socket
= consumer_find_socket_by_bitness(consumer_bitness
, consumer
);
895 ret
= consumer_close_metadata(socket
, metadata_key
);
904 static void delete_ust_app_session_rcu(struct rcu_head
*head
)
906 struct ust_app_session
*ua_sess
=
907 lttng::utils::container_of(head
, &ust_app_session::rcu_head
);
909 lttng_ht_destroy(ua_sess
->channels
);
914 * Delete ust app session safely. RCU read lock must be held before calling
917 * The session list lock must be held by the caller.
919 static void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
, struct ust_app
*app
)
922 struct lttng_ht_iter iter
;
923 struct ust_app_channel
*ua_chan
;
925 LTTNG_ASSERT(ua_sess
);
926 ASSERT_RCU_READ_LOCKED();
928 /* Locked for the duration of the function. */
929 auto locked_ua_sess
= ua_sess
->lock();
931 LTTNG_ASSERT(!ua_sess
->deleted
);
932 ua_sess
->deleted
= true;
934 auto locked_registry
= get_locked_session_registry(locked_ua_sess
->get_identifier());
935 /* Registry can be null on error path during initialization. */
936 if (locked_registry
) {
937 /* Push metadata for application before freeing the application. */
938 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
941 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
942 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
944 delete_ust_app_channel(sock
, ua_chan
, app
, locked_registry
);
947 if (locked_registry
) {
949 * Don't ask to close metadata for global per UID buffers. Close
950 * metadata only on destroy trace session in this case. Also, the
951 * previous push metadata could have flag the metadata registry to
952 * close so don't send a close command if closed.
954 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
955 const auto metadata_key
= locked_registry
->_metadata_key
;
956 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
958 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
959 locked_registry
->_metadata_closed
= true;
962 /* Release lock before communication, see comments in close_metadata(). */
963 locked_registry
.reset();
964 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
968 /* In case of per PID, the registry is kept in the session. */
969 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
970 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
973 * Registry can be null on error path during
976 buffer_reg_pid_remove(reg_pid
);
977 buffer_reg_pid_destroy(reg_pid
);
981 if (ua_sess
->handle
!= -1) {
982 pthread_mutex_lock(&app
->sock_lock
);
983 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
984 pthread_mutex_unlock(&app
->sock_lock
);
986 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
987 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
990 } else if (ret
== -EAGAIN
) {
991 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
995 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
1002 /* Remove session from application UST object descriptor. */
1003 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
1004 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
1008 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 const auto list_lock
= lttng::sessiond::lock_session_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
);
1121 * URCU intermediate call to delete an UST app.
1123 static void delete_ust_app_rcu(struct rcu_head
*head
)
1125 struct lttng_ht_node_ulong
*node
=
1126 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1127 struct ust_app
*app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
1129 DBG3("Call RCU deleting app PID %d", app
->pid
);
1130 delete_ust_app(app
);
1134 * Delete the session from the application ht and delete the data structure by
1135 * freeing every object inside and releasing them.
1137 * The session list lock must be held by the caller.
1139 static void destroy_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
1142 struct lttng_ht_iter iter
;
1145 LTTNG_ASSERT(ua_sess
);
1147 iter
.iter
.node
= &ua_sess
->node
.node
;
1148 ret
= lttng_ht_del(app
->sessions
, &iter
);
1150 /* Already scheduled for teardown. */
1154 /* Once deleted, free the data structure. */
1155 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1162 * Alloc new UST app session.
1164 static struct ust_app_session
*alloc_ust_app_session()
1166 struct ust_app_session
*ua_sess
;
1168 /* Init most of the default value by allocating and zeroing */
1169 ua_sess
= new ust_app_session
;
1170 if (ua_sess
== nullptr) {
1175 ua_sess
->handle
= -1;
1176 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1177 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1186 * Alloc new UST app channel.
1188 static struct ust_app_channel
*
1189 alloc_ust_app_channel(const char *name
,
1190 const ust_app_session::locked_weak_ref
& ua_sess
,
1191 struct lttng_ust_abi_channel_attr
*attr
)
1193 struct ust_app_channel
*ua_chan
;
1195 /* Init most of the default value by allocating and zeroing */
1196 ua_chan
= zmalloc
<ust_app_channel
>();
1197 if (ua_chan
== nullptr) {
1202 /* Setup channel name */
1203 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1204 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1206 ua_chan
->enabled
= true;
1207 ua_chan
->handle
= -1;
1208 ua_chan
->session
= &ua_sess
.get();
1209 ua_chan
->key
= get_next_channel_key();
1210 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1211 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1212 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1214 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1215 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1217 /* Copy attributes */
1219 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1220 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1221 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1222 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1223 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1224 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1225 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1226 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1228 /* By default, the channel is a per cpu channel. */
1229 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1231 DBG3("UST app channel %s allocated", ua_chan
->name
);
1240 * Allocate and initialize a UST app stream.
1242 * Return newly allocated stream pointer or NULL on error.
1244 struct ust_app_stream
*ust_app_alloc_stream()
1246 struct ust_app_stream
*stream
= nullptr;
1248 stream
= zmalloc
<ust_app_stream
>();
1249 if (stream
== nullptr) {
1250 PERROR("zmalloc ust app stream");
1254 /* Zero could be a valid value for a handle so flag it to -1. */
1255 stream
->handle
= -1;
1262 * Alloc new UST app event.
1264 static struct ust_app_event
*alloc_ust_app_event(char *name
, struct lttng_ust_abi_event
*attr
)
1266 struct ust_app_event
*ua_event
;
1268 /* Init most of the default value by allocating and zeroing */
1269 ua_event
= zmalloc
<ust_app_event
>();
1270 if (ua_event
== nullptr) {
1271 PERROR("Failed to allocate ust_app_event structure");
1275 ua_event
->enabled
= true;
1276 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1277 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1278 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1280 /* Copy attributes */
1282 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1285 DBG3("UST app event %s allocated", ua_event
->name
);
1294 * Allocate a new UST app event notifier rule.
1296 static struct ust_app_event_notifier_rule
*
1297 alloc_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
)
1299 enum lttng_event_rule_generate_exclusions_status generate_exclusion_status
;
1300 enum lttng_condition_status cond_status
;
1301 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1302 struct lttng_condition
*condition
= nullptr;
1303 const struct lttng_event_rule
*event_rule
= nullptr;
1305 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1306 if (ua_event_notifier_rule
== nullptr) {
1307 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1311 ua_event_notifier_rule
->enabled
= true;
1312 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1313 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, ua_event_notifier_rule
->token
);
1315 condition
= lttng_trigger_get_condition(trigger
);
1316 LTTNG_ASSERT(condition
);
1317 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1318 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1320 cond_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
1321 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1322 LTTNG_ASSERT(event_rule
);
1324 ua_event_notifier_rule
->error_counter_index
=
1325 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1326 /* Acquire the event notifier's reference to the trigger. */
1327 lttng_trigger_get(trigger
);
1329 ua_event_notifier_rule
->trigger
= trigger
;
1330 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1331 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1332 event_rule
, &ua_event_notifier_rule
->exclusion
);
1333 switch (generate_exclusion_status
) {
1334 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1335 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1338 /* Error occurred. */
1339 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1340 goto error_put_trigger
;
1343 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1344 ua_event_notifier_rule
->token
);
1346 return ua_event_notifier_rule
;
1349 lttng_trigger_put(trigger
);
1351 free(ua_event_notifier_rule
);
1356 * Alloc new UST app context.
1358 static struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1360 struct ust_app_ctx
*ua_ctx
;
1362 ua_ctx
= zmalloc
<ust_app_ctx
>();
1363 if (ua_ctx
== nullptr) {
1367 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1370 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1371 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1372 char *provider_name
= nullptr, *ctx_name
= nullptr;
1374 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1375 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1376 if (!provider_name
|| !ctx_name
) {
1377 free(provider_name
);
1382 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1383 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1387 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1395 * Create a liblttng-ust filter bytecode from given bytecode.
1397 * Return allocated filter or NULL on error.
1399 static struct lttng_ust_abi_filter_bytecode
*
1400 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1402 struct lttng_ust_abi_filter_bytecode
*filter
= nullptr;
1404 /* Copy filter bytecode. */
1405 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1407 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
1413 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) == sizeof(struct lttng_ust_abi_filter_bytecode
));
1414 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1420 * Create a liblttng-ust capture bytecode from given bytecode.
1422 * Return allocated filter or NULL on error.
1424 static struct lttng_ust_abi_capture_bytecode
*
1425 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1427 struct lttng_ust_abi_capture_bytecode
*capture
= nullptr;
1429 /* Copy capture bytecode. */
1430 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1432 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
1438 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1439 sizeof(struct lttng_ust_abi_capture_bytecode
));
1440 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1446 * Find an ust_app using the sock and return it. RCU read side lock must be
1447 * held before calling this helper function.
1449 struct ust_app
*ust_app_find_by_sock(int sock
)
1451 struct lttng_ht_node_ulong
*node
;
1452 struct lttng_ht_iter iter
;
1454 ASSERT_RCU_READ_LOCKED();
1456 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
1457 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
1458 if (node
== nullptr) {
1459 DBG2("UST app find by sock %d not found", sock
);
1463 return lttng::utils::container_of(node
, &ust_app::sock_n
);
1470 * Find an ust_app using the notify sock and return it. RCU read side lock must
1471 * be held before calling this helper function.
1473 static struct ust_app
*find_app_by_notify_sock(int sock
)
1475 struct lttng_ht_node_ulong
*node
;
1476 struct lttng_ht_iter iter
;
1478 ASSERT_RCU_READ_LOCKED();
1480 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *) ((unsigned long) sock
), &iter
);
1481 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
1482 if (node
== nullptr) {
1483 DBG2("UST app find by notify sock %d not found", sock
);
1487 return lttng::utils::container_of(node
, &ust_app::notify_sock_n
);
1494 * Lookup for an ust app event based on event name, filter bytecode and the
1497 * Return an ust_app_event object or NULL on error.
1499 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1501 const struct lttng_bytecode
*filter
,
1502 lttng_ust_abi_loglevel_type loglevel_type
,
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
= loglevel_type
;
1518 key
.loglevel_value
= loglevel_value
;
1519 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1520 key
.exclusion
= exclusion
;
1522 /* Lookup using the event name as hash and a custom match fct. */
1523 cds_lfht_lookup(ht
->ht
,
1524 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1525 ht_match_ust_app_event
,
1528 node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&iter
);
1529 if (node
== nullptr) {
1533 event
= lttng::utils::container_of(node
, &ust_app_event::node
);
1540 * Look-up an event notifier rule based on its token id.
1542 * Must be called with the RCU read lock held.
1543 * Return an ust_app_event_notifier_rule object or NULL on error.
1545 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(struct lttng_ht
*ht
,
1548 struct lttng_ht_iter iter
;
1549 struct lttng_ht_node_u64
*node
;
1550 struct ust_app_event_notifier_rule
*event_notifier_rule
= nullptr;
1553 ASSERT_RCU_READ_LOCKED();
1555 lttng_ht_lookup(ht
, &token
, &iter
);
1556 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
1557 if (node
== nullptr) {
1558 DBG2("UST app event notifier rule token not found: token = %" PRIu64
, token
);
1562 event_notifier_rule
= lttng::utils::container_of(node
, &ust_app_event_notifier_rule::node
);
1564 return event_notifier_rule
;
1568 * Create the channel context on the tracer.
1570 * Called with UST app session lock held.
1572 static int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1573 struct ust_app_ctx
*ua_ctx
,
1574 struct ust_app
*app
)
1578 health_code_update();
1580 pthread_mutex_lock(&app
->sock_lock
);
1581 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
, ua_chan
->obj
, &ua_ctx
->obj
);
1582 pthread_mutex_unlock(&app
->sock_lock
);
1584 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1586 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1589 } else if (ret
== -EAGAIN
) {
1591 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1595 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1603 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1605 DBG2("UST app context handle %d created successfully for channel %s",
1610 health_code_update();
1615 * Set the filter on the tracer.
1617 static int set_ust_object_filter(struct ust_app
*app
,
1618 const struct lttng_bytecode
*bytecode
,
1619 struct lttng_ust_abi_object_data
*ust_object
)
1622 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= nullptr;
1624 health_code_update();
1626 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1627 if (!ust_bytecode
) {
1628 ret
= -LTTNG_ERR_NOMEM
;
1631 pthread_mutex_lock(&app
->sock_lock
);
1632 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
, ust_object
);
1633 pthread_mutex_unlock(&app
->sock_lock
);
1635 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1637 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1640 } else if (ret
== -EAGAIN
) {
1642 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1646 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1655 DBG2("UST filter successfully set: object = %p", ust_object
);
1658 health_code_update();
1664 * Set a capture bytecode for the passed object.
1665 * The sequence number enforces the ordering at runtime and on reception of
1666 * the captured payloads.
1668 static int set_ust_capture(struct ust_app
*app
,
1669 const struct lttng_bytecode
*bytecode
,
1670 unsigned int capture_seqnum
,
1671 struct lttng_ust_abi_object_data
*ust_object
)
1674 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= nullptr;
1676 health_code_update();
1678 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1679 if (!ust_bytecode
) {
1680 ret
= -LTTNG_ERR_NOMEM
;
1685 * Set the sequence number to ensure the capture of fields is ordered.
1687 ust_bytecode
->seqnum
= capture_seqnum
;
1689 pthread_mutex_lock(&app
->sock_lock
);
1690 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
, ust_object
);
1691 pthread_mutex_unlock(&app
->sock_lock
);
1693 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1695 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1698 } else if (ret
== -EAGAIN
) {
1700 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1704 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1713 DBG2("UST capture successfully set: object = %p", ust_object
);
1716 health_code_update();
1721 static struct lttng_ust_abi_event_exclusion
*
1722 create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion
*exclusion
)
1724 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= nullptr;
1725 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1726 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1728 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1729 if (!ust_exclusion
) {
1734 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1735 sizeof(struct lttng_ust_abi_event_exclusion
));
1736 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1738 return ust_exclusion
;
1742 * Set event exclusions on the tracer.
1744 static int set_ust_object_exclusions(struct ust_app
*app
,
1745 const struct lttng_event_exclusion
*exclusions
,
1746 struct lttng_ust_abi_object_data
*ust_object
)
1749 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= nullptr;
1751 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1753 health_code_update();
1755 ust_exclusions
= create_ust_exclusion_from_exclusion(exclusions
);
1756 if (!ust_exclusions
) {
1757 ret
= -LTTNG_ERR_NOMEM
;
1760 pthread_mutex_lock(&app
->sock_lock
);
1761 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1762 pthread_mutex_unlock(&app
->sock_lock
);
1764 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1766 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1769 } else if (ret
== -EAGAIN
) {
1771 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1775 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1784 DBG2("UST exclusions set successfully for object %p", ust_object
);
1787 health_code_update();
1788 free(ust_exclusions
);
1793 * Disable the specified event on to UST tracer for the UST session.
1795 static int disable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*object
)
1799 health_code_update();
1801 pthread_mutex_lock(&app
->sock_lock
);
1802 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1803 pthread_mutex_unlock(&app
->sock_lock
);
1805 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1807 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1810 } else if (ret
== -EAGAIN
) {
1812 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1816 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1825 DBG2("UST app object %p disabled successfully for app: pid = %d", object
, app
->pid
);
1828 health_code_update();
1833 * Disable the specified channel on to UST tracer for the UST session.
1835 static int disable_ust_channel(struct ust_app
*app
,
1836 const ust_app_session::locked_weak_ref
& ua_sess
,
1837 struct ust_app_channel
*ua_chan
)
1841 health_code_update();
1843 pthread_mutex_lock(&app
->sock_lock
);
1844 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1845 pthread_mutex_unlock(&app
->sock_lock
);
1847 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1849 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1852 } else if (ret
== -EAGAIN
) {
1854 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1858 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1868 DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1871 health_code_update();
1876 * Enable the specified channel on to UST tracer for the UST session.
1878 static int enable_ust_channel(struct ust_app
*app
,
1879 const ust_app_session::locked_weak_ref
& ua_sess
,
1880 struct ust_app_channel
*ua_chan
)
1884 health_code_update();
1886 pthread_mutex_lock(&app
->sock_lock
);
1887 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1888 pthread_mutex_unlock(&app
->sock_lock
);
1890 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1892 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1896 } else if (ret
== -EAGAIN
) {
1898 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1903 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1913 ua_chan
->enabled
= true;
1915 DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1918 health_code_update();
1923 * Enable the specified event on to UST tracer for the UST session.
1925 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1929 health_code_update();
1931 pthread_mutex_lock(&app
->sock_lock
);
1932 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1933 pthread_mutex_unlock(&app
->sock_lock
);
1935 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1937 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1940 } else if (ret
== -EAGAIN
) {
1942 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1946 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1955 DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object
, app
->pid
);
1958 health_code_update();
1963 * Send channel and stream buffer to application.
1965 * Return 0 on success. On error, a negative value is returned.
1967 static int send_channel_pid_to_ust(struct ust_app
*app
,
1968 struct ust_app_session
*ua_sess
,
1969 struct ust_app_channel
*ua_chan
)
1972 struct ust_app_stream
*stream
, *stmp
;
1975 LTTNG_ASSERT(ua_sess
);
1976 LTTNG_ASSERT(ua_chan
);
1978 health_code_update();
1980 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
, app
->sock
);
1982 /* Send channel to the application. */
1983 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1984 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1985 ret
= -ENOTCONN
; /* Caused by app exiting. */
1987 } else if (ret
== -EAGAIN
) {
1988 /* Caused by timeout. */
1989 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
1993 ua_sess
->tracing_id
);
1994 /* Treat this the same way as an application that is exiting. */
1997 } else if (ret
< 0) {
2001 health_code_update();
2003 /* Send all streams to application. */
2004 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2005 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
2006 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2007 ret
= -ENOTCONN
; /* Caused by app exiting. */
2009 } else if (ret
== -EAGAIN
) {
2010 /* Caused by timeout. */
2011 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
2016 ua_sess
->tracing_id
);
2018 * Treat this the same way as an application that is
2022 } else if (ret
< 0) {
2025 /* We don't need the stream anymore once sent to the tracer. */
2026 cds_list_del(&stream
->list
);
2027 delete_ust_app_stream(-1, stream
, app
);
2031 health_code_update();
2036 * Create the specified event onto the UST tracer for a UST session.
2038 * Should be called with session mutex held.
2040 static int create_ust_event(struct ust_app
*app
,
2041 struct ust_app_channel
*ua_chan
,
2042 struct ust_app_event
*ua_event
)
2046 health_code_update();
2048 /* Create UST event on tracer */
2049 pthread_mutex_lock(&app
->sock_lock
);
2050 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
, &ua_event
->obj
);
2051 pthread_mutex_unlock(&app
->sock_lock
);
2053 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2055 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2058 } else if (ret
== -EAGAIN
) {
2060 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2064 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2065 ua_event
->attr
.name
,
2073 ua_event
->handle
= ua_event
->obj
->handle
;
2075 DBG2("UST app event %s created successfully for pid:%d object = %p",
2076 ua_event
->attr
.name
,
2080 health_code_update();
2082 /* Set filter if one is present. */
2083 if (ua_event
->filter
) {
2084 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2090 /* Set exclusions for the event */
2091 if (ua_event
->exclusion
) {
2092 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2098 /* If event not enabled, disable it on the tracer */
2099 if (ua_event
->enabled
) {
2101 * We now need to explicitly enable the event, since it
2102 * is now disabled at creation.
2104 ret
= enable_ust_object(app
, ua_event
->obj
);
2107 * If we hit an EPERM, something is wrong with our enable call. If
2108 * we get an EEXIST, there is a problem on the tracer side since we
2112 case -LTTNG_UST_ERR_PERM
:
2113 /* Code flow problem */
2115 case -LTTNG_UST_ERR_EXIST
:
2116 /* It's OK for our use case. */
2127 health_code_update();
2132 init_ust_event_notifier_from_event_rule(const struct lttng_event_rule
*rule
,
2133 struct lttng_ust_abi_event_notifier
*event_notifier
)
2135 enum lttng_event_rule_status status
;
2136 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2137 int loglevel
= -1, ret
= 0;
2138 const char *pattern
;
2140 memset(event_notifier
, 0, sizeof(*event_notifier
));
2142 if (lttng_event_rule_targets_agent_domain(rule
)) {
2144 * Special event for agents
2145 * The actual meat of the event is in the filter that will be
2146 * attached later on.
2147 * Set the default values for the agent event.
2149 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
2151 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2153 const struct lttng_log_level_rule
*log_level_rule
;
2155 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2156 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2158 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2159 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2160 /* At this point, this is a fatal error. */
2164 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(rule
, &log_level_rule
);
2165 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2166 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2167 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2168 enum lttng_log_level_rule_status llr_status
;
2170 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2171 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2172 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2173 llr_status
= lttng_log_level_rule_exactly_get_level(log_level_rule
,
2176 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2177 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2178 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2179 log_level_rule
, &loglevel
);
2185 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2187 /* At this point this is a fatal error. */
2192 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2193 ret
= lttng_strncpy(
2194 event_notifier
->event
.name
, pattern
, sizeof(event_notifier
->event
.name
));
2196 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern
);
2200 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2201 event_notifier
->event
.loglevel
= loglevel
;
2207 * Create the specified event notifier against the user space tracer of a
2208 * given application.
2210 static int create_ust_event_notifier(struct ust_app
*app
,
2211 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2214 enum lttng_condition_status condition_status
;
2215 const struct lttng_condition
*condition
= nullptr;
2216 struct lttng_ust_abi_event_notifier event_notifier
;
2217 const struct lttng_event_rule
*event_rule
= nullptr;
2218 unsigned int capture_bytecode_count
= 0, i
;
2219 enum lttng_condition_status cond_status
;
2220 enum lttng_event_rule_type event_rule_type
;
2222 health_code_update();
2223 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2225 condition
= lttng_trigger_get_const_condition(ua_event_notifier_rule
->trigger
);
2226 LTTNG_ASSERT(condition
);
2227 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2228 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2230 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2231 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2233 LTTNG_ASSERT(event_rule
);
2235 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2236 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2237 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2238 event_rule_type
== LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2239 event_rule_type
== LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2241 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2242 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2243 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2245 /* Create UST event notifier against the tracer. */
2246 pthread_mutex_lock(&app
->sock_lock
);
2247 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
,
2249 app
->event_notifier_group
.object
,
2250 &ua_event_notifier_rule
->obj
);
2251 pthread_mutex_unlock(&app
->sock_lock
);
2253 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2255 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2258 } else if (ret
== -EAGAIN
) {
2260 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2264 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2265 event_notifier
.event
.name
,
2273 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2275 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2276 event_notifier
.event
.name
,
2279 ua_event_notifier_rule
->obj
);
2281 health_code_update();
2283 /* Set filter if one is present. */
2284 if (ua_event_notifier_rule
->filter
) {
2285 ret
= set_ust_object_filter(
2286 app
, ua_event_notifier_rule
->filter
, ua_event_notifier_rule
->obj
);
2292 /* Set exclusions for the event. */
2293 if (ua_event_notifier_rule
->exclusion
) {
2294 ret
= set_ust_object_exclusions(
2295 app
, ua_event_notifier_rule
->exclusion
, ua_event_notifier_rule
->obj
);
2301 /* Set the capture bytecodes. */
2302 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2303 condition
, &capture_bytecode_count
);
2304 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2306 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2307 const struct lttng_bytecode
*capture_bytecode
=
2308 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2311 ret
= set_ust_capture(app
, capture_bytecode
, i
, ua_event_notifier_rule
->obj
);
2318 * We now need to explicitly enable the event, since it
2319 * is disabled at creation.
2321 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2324 * If we hit an EPERM, something is wrong with our enable call.
2325 * If we get an EEXIST, there is a problem on the tracer side
2326 * since we just created it.
2329 case -LTTNG_UST_ERR_PERM
:
2330 /* Code flow problem. */
2332 case -LTTNG_UST_ERR_EXIST
:
2333 /* It's OK for our use case. */
2343 ua_event_notifier_rule
->enabled
= true;
2346 health_code_update();
2351 * Copy data between an UST app event and a LTT event.
2353 static void shadow_copy_event(struct ust_app_event
*ua_event
, struct ltt_ust_event
*uevent
)
2355 size_t exclusion_alloc_size
;
2357 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2358 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2360 ua_event
->enabled
= uevent
->enabled
;
2362 /* Copy event attributes */
2363 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2365 /* Copy filter bytecode */
2366 if (uevent
->filter
) {
2367 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2368 /* Filter might be NULL here in case of ENONEM. */
2371 /* Copy exclusion data */
2372 if (uevent
->exclusion
) {
2373 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2374 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2375 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2376 if (ua_event
->exclusion
== nullptr) {
2379 memcpy(ua_event
->exclusion
, uevent
->exclusion
, exclusion_alloc_size
);
2385 * Copy data between an UST app channel and a LTT channel.
2387 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
, struct ltt_ust_channel
*uchan
)
2389 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2391 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2392 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2394 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2395 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2397 /* Copy event attributes since the layout is different. */
2398 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2399 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2400 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2401 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2402 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2403 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2404 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2405 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2408 * Note that the attribute channel type is not set since the channel on the
2409 * tracing registry side does not have this information.
2412 ua_chan
->enabled
= uchan
->enabled
;
2413 ua_chan
->tracing_channel_id
= uchan
->id
;
2415 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2419 * Copy data between a UST app session and a regular LTT session.
2421 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2422 struct ltt_ust_session
*usess
,
2423 struct ust_app
*app
)
2425 struct tm
*timeinfo
;
2428 char tmp_shm_path
[PATH_MAX
];
2430 timeinfo
= localtime(&app
->registration_time
);
2431 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2433 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2435 ua_sess
->tracing_id
= usess
->id
;
2436 ua_sess
->id
= get_next_session_id();
2437 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2438 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2439 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2440 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2441 ua_sess
->buffer_type
= usess
->buffer_type
;
2442 ua_sess
->bits_per_long
= app
->abi
.bits_per_long
;
2444 /* There is only one consumer object per session possible. */
2445 consumer_output_get(usess
->consumer
);
2446 ua_sess
->consumer
= usess
->consumer
;
2448 ua_sess
->output_traces
= usess
->output_traces
;
2449 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2450 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &usess
->metadata_attr
);
2452 switch (ua_sess
->buffer_type
) {
2453 case LTTNG_BUFFER_PER_PID
:
2454 ret
= snprintf(ua_sess
->path
,
2455 sizeof(ua_sess
->path
),
2456 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2461 case LTTNG_BUFFER_PER_UID
:
2462 ret
= snprintf(ua_sess
->path
,
2463 sizeof(ua_sess
->path
),
2464 DEFAULT_UST_TRACE_UID_PATH
,
2465 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2466 app
->abi
.bits_per_long
);
2473 PERROR("asprintf UST shadow copy session");
2478 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
, sizeof(ua_sess
->root_shm_path
));
2479 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2480 strncpy(ua_sess
->shm_path
, usess
->shm_path
, sizeof(ua_sess
->shm_path
));
2481 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2482 if (ua_sess
->shm_path
[0]) {
2483 switch (ua_sess
->buffer_type
) {
2484 case LTTNG_BUFFER_PER_PID
:
2485 ret
= snprintf(tmp_shm_path
,
2486 sizeof(tmp_shm_path
),
2487 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2492 case LTTNG_BUFFER_PER_UID
:
2493 ret
= snprintf(tmp_shm_path
,
2494 sizeof(tmp_shm_path
),
2495 "/" DEFAULT_UST_TRACE_UID_PATH
,
2497 app
->abi
.bits_per_long
);
2504 PERROR("sprintf UST shadow copy session");
2508 strncat(ua_sess
->shm_path
,
2510 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2511 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2516 consumer_output_put(ua_sess
->consumer
);
2520 * Lookup sesison wrapper.
2523 __lookup_session_by_app(const ltt_ust_session
*usess
, const ust_app
*app
, 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 ust_app_session
*ust_app_lookup_app_session(const struct ltt_ust_session
*usess
,
2534 const 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
<lttng_ht_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
= ust_app_lookup_app_session(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
<lttng_ht_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(const ust_app_session::locked_weak_ref
& 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(const ust_app_session::locked_weak_ref
& 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
<lttng_ht_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 enum lttng_error_code notification_ret
;
3449 LTTNG_ASSERT(usess
);
3450 LTTNG_ASSERT(ua_sess
);
3451 LTTNG_ASSERT(ua_chan
);
3452 ASSERT_RCU_READ_LOCKED();
3454 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3456 /* Guaranteed to exist; will not throw. */
3457 const auto session
= ltt_session::find_session(ua_sess
->tracing_id
);
3458 ASSERT_SESSION_LIST_LOCKED();
3460 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
3462 * The session creation handles the creation of this global registry
3463 * object. If none can be find, there is a code flow problem or a
3466 LTTNG_ASSERT(reg_uid
);
3468 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
, reg_uid
);
3473 /* Create the buffer registry channel object. */
3474 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3476 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan
->name
);
3481 * Create the buffers on the consumer side. This call populates the
3482 * ust app channel object with all streams and data object.
3484 ret
= do_consumer_create_channel(
3485 usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, reg_uid
->registry
->reg
.ust
);
3487 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3490 * Let's remove the previously created buffer registry channel so
3491 * it's not visible anymore in the session registry.
3493 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3495 locked_registry
->remove_channel(ua_chan
->tracing_channel_id
, false);
3496 } catch (const std::exception
& ex
) {
3497 DBG("Could not find channel for removal: %s", ex
.what());
3499 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3500 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3505 * Setup the streams and add it to the session registry.
3507 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, buf_reg_chan
, app
);
3509 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3514 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3515 auto& ust_reg_chan
= locked_registry
->channel(ua_chan
->tracing_channel_id
);
3517 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3520 /* Notify the notification subsystem of the channel's creation. */
3521 notification_ret
= notification_thread_command_add_channel(
3522 the_notification_thread_handle
,
3527 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3528 if (notification_ret
!= LTTNG_OK
) {
3529 ret
= -(int) notification_ret
;
3530 ERR("Failed to add channel to notification thread");
3535 /* Send buffers to the application. */
3536 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3538 if (ret
!= -ENOTCONN
) {
3539 ERR("Error sending channel to application");
3549 * Create and send to the application the created buffers with per PID buffers.
3551 * Called with UST app session lock held.
3552 * The session list lock and the session's lock must be acquired.
3554 * Return 0 on success else a negative value.
3556 static int create_channel_per_pid(struct ust_app
*app
,
3557 struct ltt_ust_session
*usess
,
3558 const ust_app_session::locked_weak_ref
& ua_sess
,
3559 struct ust_app_channel
*ua_chan
)
3562 lsu::registry_session
*registry
;
3563 enum lttng_error_code cmd_ret
;
3564 uint64_t chan_reg_key
;
3567 LTTNG_ASSERT(usess
);
3568 LTTNG_ASSERT(ua_chan
);
3570 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3572 lttng::urcu::read_lock_guard read_lock
;
3574 registry
= ust_app_get_session_registry(ua_sess
->get_identifier());
3575 /* The UST app session lock is held, registry shall not be null. */
3576 LTTNG_ASSERT(registry
);
3578 /* Guaranteed to exist; will not throw. */
3579 const auto session
= ltt_session::find_session(ua_sess
->tracing_id
);
3580 ASSERT_LOCKED(session
->_lock
);
3581 ASSERT_SESSION_LIST_LOCKED();
3583 /* Create and add a new channel registry to session. */
3585 registry
->add_channel(ua_chan
->key
);
3586 } catch (const std::exception
& ex
) {
3587 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3594 /* Create and get channel on the consumer side. */
3595 ret
= do_consumer_create_channel(
3596 usess
, &ua_sess
.get(), ua_chan
, app
->abi
.bits_per_long
, registry
);
3598 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3599 goto error_remove_from_registry
;
3602 ret
= send_channel_pid_to_ust(app
, &ua_sess
.get(), ua_chan
);
3604 if (ret
!= -ENOTCONN
) {
3605 ERR("Error sending channel to application");
3607 goto error_remove_from_registry
;
3610 chan_reg_key
= ua_chan
->key
;
3612 auto locked_registry
= registry
->lock();
3614 auto& ust_reg_chan
= locked_registry
->channel(chan_reg_key
);
3615 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3618 cmd_ret
= notification_thread_command_add_channel(the_notification_thread_handle
,
3623 ua_chan
->attr
.subbuf_size
*
3624 ua_chan
->attr
.num_subbuf
);
3625 if (cmd_ret
!= LTTNG_OK
) {
3626 ret
= -(int) cmd_ret
;
3627 ERR("Failed to add channel to notification thread");
3628 goto error_remove_from_registry
;
3631 error_remove_from_registry
:
3634 auto locked_registry
= registry
->lock();
3635 locked_registry
->remove_channel(ua_chan
->key
, false);
3636 } catch (const std::exception
& ex
) {
3637 DBG("Could not find channel for removal: %s", ex
.what());
3645 * From an already allocated ust app channel, create the channel buffers if
3646 * needed and send them to the application. This MUST be called with a RCU read
3647 * side lock acquired.
3649 * Called with UST app session lock held.
3651 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3652 * the application exited concurrently.
3654 static int ust_app_channel_send(struct ust_app
*app
,
3655 struct ltt_ust_session
*usess
,
3656 const ust_app_session::locked_weak_ref
& ua_sess
,
3657 struct ust_app_channel
*ua_chan
)
3662 LTTNG_ASSERT(usess
);
3663 LTTNG_ASSERT(usess
->active
);
3664 LTTNG_ASSERT(ua_chan
);
3665 ASSERT_RCU_READ_LOCKED();
3667 /* Handle buffer type before sending the channel to the application. */
3668 switch (usess
->buffer_type
) {
3669 case LTTNG_BUFFER_PER_UID
:
3671 ret
= create_channel_per_uid(app
, usess
, &ua_sess
.get(), ua_chan
);
3677 case LTTNG_BUFFER_PER_PID
:
3679 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3691 /* Initialize ust objd object using the received handle and add it. */
3692 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3693 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3695 /* If channel is not enabled, disable it on the tracer */
3696 if (!ua_chan
->enabled
) {
3697 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3708 * Create UST app channel and return it through ua_chanp if not NULL.
3710 * Called with UST app session lock and RCU read-side lock held.
3712 * Return 0 on success or else a negative value.
3714 static int ust_app_channel_allocate(const ust_app_session::locked_weak_ref
& ua_sess
,
3715 struct ltt_ust_channel
*uchan
,
3716 enum lttng_ust_abi_chan_type type
,
3717 struct ltt_ust_session
*usess
__attribute__((unused
)),
3718 struct ust_app_channel
**ua_chanp
)
3721 struct lttng_ht_iter iter
;
3722 struct lttng_ht_node_str
*ua_chan_node
;
3723 struct ust_app_channel
*ua_chan
;
3725 ASSERT_RCU_READ_LOCKED();
3727 /* Lookup channel in the ust app session */
3728 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3729 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&iter
);
3730 if (ua_chan_node
!= nullptr) {
3731 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3735 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3736 if (ua_chan
== nullptr) {
3737 /* Only malloc can fail here */
3741 shadow_copy_channel(ua_chan
, uchan
);
3743 /* Set channel type. */
3744 ua_chan
->attr
.type
= type
;
3746 /* Only add the channel if successful on the tracer side. */
3747 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3750 *ua_chanp
= ua_chan
;
3753 /* Everything went well. */
3761 * Create UST app event and create it on the tracer side.
3763 * Must be called with the RCU read side lock held.
3764 * Called with ust app session mutex held.
3766 static int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3767 struct ltt_ust_event
*uevent
,
3768 struct ust_app
*app
)
3771 struct ust_app_event
*ua_event
;
3773 ASSERT_RCU_READ_LOCKED();
3775 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3776 if (ua_event
== nullptr) {
3777 /* Only failure mode of alloc_ust_app_event(). */
3781 shadow_copy_event(ua_event
, uevent
);
3783 /* Create it on the tracer side */
3784 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3787 * Not found previously means that it does not exist on the
3788 * tracer. If the application reports that the event existed,
3789 * it means there is a bug in the sessiond or lttng-ust
3790 * (or corruption, etc.)
3792 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3793 ERR("Tracer for application reported that an event being created already existed: "
3794 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3804 add_unique_ust_app_event(ua_chan
, ua_event
);
3806 DBG2("UST app create event completed: app = '%s' pid = %d", app
->name
, app
->pid
);
3812 /* Valid. Calling here is already in a read side lock */
3813 delete_ust_app_event(-1, ua_event
, app
);
3818 * Create UST app event notifier rule and create it on the tracer side.
3820 * Must be called with the RCU read side lock held.
3821 * Called with ust app session mutex held.
3823 static int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
, struct ust_app
*app
)
3826 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3828 ASSERT_RCU_READ_LOCKED();
3830 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3831 if (ua_event_notifier_rule
== nullptr) {
3836 /* Create it on the tracer side. */
3837 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3840 * Not found previously means that it does not exist on the
3841 * tracer. If the application reports that the event existed,
3842 * it means there is a bug in the sessiond or lttng-ust
3843 * (or corruption, etc.)
3845 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3846 ERR("Tracer for application reported that an event notifier being created already exists: "
3847 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3848 lttng_trigger_get_tracer_token(trigger
),
3857 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3858 &ua_event_notifier_rule
->node
);
3860 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3863 lttng_trigger_get_tracer_token(trigger
));
3868 /* The RCU read side lock is already being held by the caller. */
3869 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3875 * Create UST metadata and open it on the tracer side.
3877 * Called with UST app session lock held and RCU read side lock.
3879 static int create_ust_app_metadata(const ust_app_session::locked_weak_ref
& ua_sess
,
3880 struct ust_app
*app
,
3881 struct consumer_output
*consumer
)
3884 struct ust_app_channel
*metadata
;
3885 struct consumer_socket
*socket
;
3888 LTTNG_ASSERT(consumer
);
3889 ASSERT_RCU_READ_LOCKED();
3891 auto locked_registry
= get_locked_session_registry(ua_sess
->get_identifier());
3892 /* The UST app session is held registry shall not be null. */
3893 LTTNG_ASSERT(locked_registry
);
3895 /* Guaranteed to exist; will not throw. */
3896 const auto session
= ltt_session::find_session(ua_sess
->tracing_id
);
3897 ASSERT_LOCKED(session
->_lock
);
3898 ASSERT_SESSION_LIST_LOCKED();
3900 /* Metadata already exists for this registry or it was closed previously */
3901 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3906 /* Allocate UST metadata */
3907 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, nullptr);
3909 /* malloc() failed */
3914 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3916 /* Need one fd for the channel. */
3917 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3919 ERR("Exhausted number of available FD upon create metadata");
3923 /* Get the right consumer socket for the application. */
3924 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3927 goto error_consumer
;
3931 * Keep metadata key so we can identify it on the consumer side. Assign it
3932 * to the registry *before* we ask the consumer so we avoid the race of the
3933 * consumer requesting the metadata and the ask_channel call on our side
3934 * did not returned yet.
3936 locked_registry
->_metadata_key
= metadata
->key
;
3939 * Ask the metadata channel creation to the consumer. The metadata object
3940 * will be created by the consumer and kept their. However, the stream is
3941 * never added or monitored until we do a first push metadata to the
3944 ret
= ust_consumer_ask_channel(&ua_sess
.get(),
3948 locked_registry
.get(),
3949 session
->current_trace_chunk
);
3951 /* Nullify the metadata key so we don't try to close it later on. */
3952 locked_registry
->_metadata_key
= 0;
3953 goto error_consumer
;
3957 * The setup command will make the metadata stream be sent to the relayd,
3958 * if applicable, and the thread managing the metadatas. This is important
3959 * because after this point, if an error occurs, the only way the stream
3960 * can be deleted is to be monitored in the consumer.
3962 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3964 /* Nullify the metadata key so we don't try to close it later on. */
3965 locked_registry
->_metadata_key
= 0;
3966 goto error_consumer
;
3969 DBG2("UST metadata with key %" PRIu64
" created for app pid %d", metadata
->key
, app
->pid
);
3972 lttng_fd_put(LTTNG_FD_APPS
, 1);
3973 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3979 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3980 * acquired before calling this function.
3982 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3984 struct ust_app
*app
= nullptr;
3985 struct lttng_ht_node_ulong
*node
;
3986 struct lttng_ht_iter iter
;
3988 lttng_ht_lookup(ust_app_ht
, (void *) ((unsigned long) pid
), &iter
);
3989 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
3990 if (node
== nullptr) {
3991 DBG2("UST app no found with pid %d", pid
);
3995 DBG2("Found UST app by pid %d", pid
);
3997 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
4004 * Allocate and init an UST app object using the registration information and
4005 * the command socket. This is called when the command socket connects to the
4008 * The object is returned on success or else NULL.
4010 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
4013 struct ust_app
*lta
= nullptr;
4014 struct lttng_pipe
*event_notifier_event_source_pipe
= nullptr;
4017 LTTNG_ASSERT(sock
>= 0);
4019 DBG3("UST app creating application for socket %d", sock
);
4021 if ((msg
->bits_per_long
== 64 && (uatomic_read(&the_ust_consumerd64_fd
) == -EINVAL
)) ||
4022 (msg
->bits_per_long
== 32 && (uatomic_read(&the_ust_consumerd32_fd
) == -EINVAL
))) {
4023 ERR("Registration failed: application \"%s\" (pid: %d) has "
4024 "%d-bit long, but no consumerd for this size is available.\n",
4027 msg
->bits_per_long
);
4032 * Reserve the two file descriptors of the event source pipe. The write
4033 * end will be closed once it is passed to the application, at which
4034 * point a single 'put' will be performed.
4036 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
4038 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4044 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
4045 if (!event_notifier_event_source_pipe
) {
4046 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4052 lta
= zmalloc
<ust_app
>();
4053 if (lta
== nullptr) {
4055 goto error_free_pipe
;
4058 urcu_ref_init(<a
->ref
);
4060 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4062 lta
->ppid
= msg
->ppid
;
4063 lta
->uid
= msg
->uid
;
4064 lta
->gid
= msg
->gid
;
4067 .bits_per_long
= msg
->bits_per_long
,
4068 .long_alignment
= msg
->long_alignment
,
4069 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4070 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4071 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4072 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4073 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4074 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4075 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4078 lta
->v_major
= msg
->major
;
4079 lta
->v_minor
= msg
->minor
;
4080 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4081 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4082 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4083 lta
->notify_sock
= -1;
4084 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4086 /* Copy name and make sure it's NULL terminated. */
4087 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4088 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4091 * Before this can be called, when receiving the registration information,
4092 * the application compatibility is checked. So, at this point, the
4093 * application can work with this session daemon.
4095 lta
->compatible
= 1;
4097 lta
->pid
= msg
->pid
;
4098 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4100 pthread_mutex_init(<a
->sock_lock
, nullptr);
4101 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4103 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4107 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4108 lttng_fd_put(LTTNG_FD_APPS
, 2);
4114 * For a given application object, add it to every hash table.
4116 void ust_app_add(struct ust_app
*app
)
4119 LTTNG_ASSERT(app
->notify_sock
>= 0);
4121 app
->registration_time
= time(nullptr);
4123 lttng::urcu::read_lock_guard read_lock
;
4126 * On a re-registration, we want to kick out the previous registration of
4129 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4132 * The socket _should_ be unique until _we_ call close. So, a add_unique
4133 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4134 * already in the table.
4136 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4138 /* Add application to the notify socket hash table. */
4139 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4140 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4142 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4143 "notify_sock =%d (version %d.%d)",
4156 * Set the application version into the object.
4158 * Return 0 on success else a negative value either an errno code or a
4159 * LTTng-UST error code.
4161 int ust_app_version(struct ust_app
*app
)
4167 pthread_mutex_lock(&app
->sock_lock
);
4168 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4169 pthread_mutex_unlock(&app
->sock_lock
);
4171 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4172 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4175 } else if (ret
== -EAGAIN
) {
4176 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4180 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4190 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4192 return app
->v_major
>= 9;
4195 bool ust_app_supports_counters(const struct ust_app
*app
)
4197 return app
->v_major
>= 9;
4201 * Setup the base event notifier group.
4203 * Return 0 on success else a negative value either an errno code or a
4204 * LTTng-UST error code.
4206 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4209 int event_pipe_write_fd
;
4210 struct lttng_ust_abi_object_data
*event_notifier_group
= nullptr;
4211 enum lttng_error_code lttng_ret
;
4212 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4216 if (!ust_app_supports_notifiers(app
)) {
4221 /* Get the write side of the pipe. */
4222 event_pipe_write_fd
= lttng_pipe_get_writefd(app
->event_notifier_group
.event_pipe
);
4224 pthread_mutex_lock(&app
->sock_lock
);
4225 ret
= lttng_ust_ctl_create_event_notifier_group(
4226 app
->sock
, event_pipe_write_fd
, &event_notifier_group
);
4227 pthread_mutex_unlock(&app
->sock_lock
);
4229 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4231 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4234 } else if (ret
== -EAGAIN
) {
4236 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4240 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4244 event_pipe_write_fd
);
4249 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4251 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4258 * Release the file descriptor that was reserved for the write-end of
4261 lttng_fd_put(LTTNG_FD_APPS
, 1);
4263 lttng_ret
= notification_thread_command_add_tracer_event_source(
4264 the_notification_thread_handle
,
4265 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4267 if (lttng_ret
!= LTTNG_OK
) {
4268 ERR("Failed to add tracer event source to notification thread");
4273 /* Assign handle only when the complete setup is valid. */
4274 app
->event_notifier_group
.object
= event_notifier_group
;
4276 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
4277 switch (event_notifier_error_accounting_status
) {
4278 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4280 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4281 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",
4286 goto error_accounting
;
4287 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4288 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4293 goto error_accounting
;
4295 ERR("Failed to setup event notifier error accounting for app");
4297 goto error_accounting
;
4303 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4304 the_notification_thread_handle
,
4305 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
));
4306 if (lttng_ret
!= LTTNG_OK
) {
4307 ERR("Failed to remove application tracer event source from notification thread");
4311 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4312 free(app
->event_notifier_group
.object
);
4313 app
->event_notifier_group
.object
= nullptr;
4317 static void ust_app_unregister(ust_app
& app
)
4319 struct lttng_ht_iter iter
;
4320 struct ust_app_session
*ua_sess
;
4322 lttng::urcu::read_lock_guard read_lock
;
4325 * For per-PID buffers, perform "push metadata" and flush all
4326 * application streams before removing app from hash tables,
4327 * ensuring proper behavior of data_pending check.
4328 * Remove sessions so they are not visible during deletion.
4330 cds_lfht_for_each_entry (app
.sessions
->ht
, &iter
.iter
, ua_sess
, node
.node
) {
4331 const auto del_ret
= lttng_ht_del(app
.sessions
, &iter
);
4333 /* The session was already removed so scheduled for teardown. */
4337 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4338 (void) ust_app_flush_app_session(app
, *ua_sess
);
4342 * Add session to list for teardown. This is safe since at this point we
4343 * are the only one using this list.
4345 auto locked_ua_sess
= ua_sess
->lock();
4347 if (ua_sess
->deleted
) {
4352 * Normally, this is done in the delete session process which is
4353 * executed in the call rcu below. However, upon registration we can't
4354 * afford to wait for the grace period before pushing data or else the
4355 * data pending feature can race between the unregistration and stop
4356 * command where the data pending command is sent *before* the grace
4359 * The close metadata below nullifies the metadata pointer in the
4360 * session so the delete session will NOT push/close a second time.
4362 auto locked_registry
=
4363 get_locked_session_registry(locked_ua_sess
->get_identifier());
4364 if (locked_registry
) {
4365 /* Push metadata for application before freeing the application. */
4366 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4369 * Don't ask to close metadata for global per UID buffers. Close
4370 * metadata only on destroy trace session in this case. Also, the
4371 * previous push metadata could have flag the metadata registry to
4372 * close so don't send a close command if closed.
4374 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4375 const auto metadata_key
= locked_registry
->_metadata_key
;
4376 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4378 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4379 locked_registry
->_metadata_closed
= true;
4382 /* Release lock before communication, see comments in
4383 * close_metadata(). */
4384 locked_registry
.reset();
4385 (void) close_metadata(
4386 metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4388 locked_registry
.reset();
4392 cds_list_add(&ua_sess
->teardown_node
, &app
.teardown_head
);
4396 * Remove application from notify hash table. The thread handling the
4397 * notify socket could have deleted the node so ignore on error because
4398 * either way it's valid. The close of that socket is handled by the
4399 * apps_notify_thread.
4401 iter
.iter
.node
= &app
.notify_sock_n
.node
;
4402 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4405 * Ignore return value since the node might have been removed before by an
4406 * add replace during app registration because the PID can be reassigned by
4409 iter
.iter
.node
= &app
.pid_n
.node
;
4410 if (lttng_ht_del(ust_app_ht
, &iter
)) {
4411 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", app
.pid
);
4416 * Unregister app by removing it from the global traceable app list and freeing
4419 * The socket is already closed at this point, so there is no need to close it.
4421 void ust_app_unregister_by_socket(int sock_fd
)
4423 struct ust_app
*app
;
4424 struct lttng_ht_node_ulong
*node
;
4425 struct lttng_ht_iter ust_app_sock_iter
;
4428 lttng::urcu::read_lock_guard read_lock
;
4430 /* Get the node reference for a call_rcu */
4431 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock_fd
), &ust_app_sock_iter
);
4432 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&ust_app_sock_iter
);
4435 app
= caa_container_of(node
, struct ust_app
, sock_n
);
4437 DBG_FMT("Application unregistering after socket activity: pid={}, socket_fd={}",
4441 /* Remove application from socket hash table */
4442 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4446 * The socket is closed: release its reference to the application
4447 * to trigger its eventual teardown.
4453 * Fill events array with all events name of all registered apps.
4455 int ust_app_list_events(struct lttng_event
**events
)
4458 size_t nbmem
, count
= 0;
4459 struct lttng_ht_iter iter
;
4460 struct ust_app
*app
;
4461 struct lttng_event
*tmp_event
;
4463 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4464 tmp_event
= calloc
<lttng_event
>(nbmem
);
4465 if (tmp_event
== nullptr) {
4466 PERROR("zmalloc ust app events");
4472 lttng::urcu::read_lock_guard read_lock
;
4474 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4475 struct lttng_ust_abi_tracepoint_iter uiter
;
4477 health_code_update();
4479 if (!app
->compatible
) {
4481 * TODO: In time, we should notice the caller of this error by
4482 * telling him that this is a version error.
4487 pthread_mutex_lock(&app
->sock_lock
);
4488 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4490 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4491 ERR("UST app list events getting handle failed for app pid %d",
4494 pthread_mutex_unlock(&app
->sock_lock
);
4498 while ((ret
= lttng_ust_ctl_tracepoint_list_get(
4499 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4500 /* Handle ustctl error. */
4504 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4505 ERR("UST app tp list get failed for app %d with ret %d",
4509 DBG3("UST app tp list get failed. Application is dead");
4515 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4516 if (release_ret
< 0 &&
4517 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4518 release_ret
!= -EPIPE
) {
4519 ERR("Error releasing app handle for app %d with ret %d",
4524 pthread_mutex_unlock(&app
->sock_lock
);
4528 health_code_update();
4529 if (count
>= nbmem
) {
4530 /* In case the realloc fails, we free the memory */
4531 struct lttng_event
*new_tmp_event
;
4534 new_nbmem
= nbmem
<< 1;
4535 DBG2("Reallocating event list from %zu to %zu entries",
4538 new_tmp_event
= (lttng_event
*) realloc(
4539 tmp_event
, new_nbmem
* sizeof(struct lttng_event
));
4540 if (new_tmp_event
== nullptr) {
4543 PERROR("realloc ust app events");
4546 release_ret
= lttng_ust_ctl_release_handle(
4548 if (release_ret
< 0 &&
4549 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4550 release_ret
!= -EPIPE
) {
4551 ERR("Error releasing app handle for app %d with ret %d",
4556 pthread_mutex_unlock(&app
->sock_lock
);
4559 /* Zero the new memory */
4560 memset(new_tmp_event
+ nbmem
,
4562 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4564 tmp_event
= new_tmp_event
;
4567 memcpy(tmp_event
[count
].name
,
4569 LTTNG_UST_ABI_SYM_NAME_LEN
);
4570 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4571 tmp_event
[count
].type
=
4572 (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4573 tmp_event
[count
].pid
= app
->pid
;
4574 tmp_event
[count
].enabled
= -1;
4578 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4579 pthread_mutex_unlock(&app
->sock_lock
);
4581 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4582 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4585 } else if (ret
== -EAGAIN
) {
4586 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4590 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4600 *events
= tmp_event
;
4602 DBG2("UST app list events done (%zu events)", count
);
4606 health_code_update();
4611 * Fill events array with all events name of all registered apps.
4613 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4616 size_t nbmem
, count
= 0;
4617 struct lttng_ht_iter iter
;
4618 struct ust_app
*app
;
4619 struct lttng_event_field
*tmp_event
;
4621 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4622 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4623 if (tmp_event
== nullptr) {
4624 PERROR("zmalloc ust app event fields");
4630 lttng::urcu::read_lock_guard read_lock
;
4632 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4633 struct lttng_ust_abi_field_iter uiter
;
4635 health_code_update();
4637 if (!app
->compatible
) {
4639 * TODO: In time, we should notice the caller of this error by
4640 * telling him that this is a version error.
4645 pthread_mutex_lock(&app
->sock_lock
);
4646 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4648 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4649 ERR("UST app list field getting handle failed for app pid %d",
4652 pthread_mutex_unlock(&app
->sock_lock
);
4656 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(
4657 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4658 /* Handle ustctl error. */
4662 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4663 ERR("UST app tp list field failed for app %d with ret %d",
4667 DBG3("UST app tp list field failed. Application is dead");
4673 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4674 pthread_mutex_unlock(&app
->sock_lock
);
4675 if (release_ret
< 0 &&
4676 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4677 release_ret
!= -EPIPE
) {
4678 ERR("Error releasing app handle for app %d with ret %d",
4686 health_code_update();
4687 if (count
>= nbmem
) {
4688 /* In case the realloc fails, we free the memory */
4689 struct lttng_event_field
*new_tmp_event
;
4692 new_nbmem
= nbmem
<< 1;
4693 DBG2("Reallocating event field list from %zu to %zu entries",
4696 new_tmp_event
= (lttng_event_field
*) realloc(
4698 new_nbmem
* sizeof(struct lttng_event_field
));
4699 if (new_tmp_event
== nullptr) {
4702 PERROR("realloc ust app event fields");
4705 release_ret
= lttng_ust_ctl_release_handle(
4707 pthread_mutex_unlock(&app
->sock_lock
);
4709 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4710 release_ret
!= -EPIPE
) {
4711 ERR("Error releasing app handle for app %d with ret %d",
4719 /* Zero the new memory */
4720 memset(new_tmp_event
+ nbmem
,
4722 (new_nbmem
- nbmem
) *
4723 sizeof(struct lttng_event_field
));
4725 tmp_event
= new_tmp_event
;
4728 memcpy(tmp_event
[count
].field_name
,
4730 LTTNG_UST_ABI_SYM_NAME_LEN
);
4731 /* Mapping between these enums matches 1 to 1. */
4732 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4733 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4735 memcpy(tmp_event
[count
].event
.name
,
4737 LTTNG_UST_ABI_SYM_NAME_LEN
);
4738 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4739 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4740 tmp_event
[count
].event
.pid
= app
->pid
;
4741 tmp_event
[count
].event
.enabled
= -1;
4745 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4746 pthread_mutex_unlock(&app
->sock_lock
);
4747 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4748 ERR("Error releasing app handle for app %d with ret %d",
4756 *fields
= tmp_event
;
4758 DBG2("UST app list event fields done (%zu events)", count
);
4762 health_code_update();
4767 * Free and clean all traceable apps of the global list.
4769 void ust_app_clean_list()
4772 struct ust_app
*app
;
4773 struct lttng_ht_iter iter
;
4775 DBG2("UST app cleaning registered apps hash table");
4777 /* Cleanup notify socket hash table */
4778 if (ust_app_ht_by_notify_sock
) {
4779 lttng::urcu::read_lock_guard read_lock
;
4781 cds_lfht_for_each_entry (
4782 ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
, notify_sock_n
.node
) {
4784 * Assert that all notifiers are gone as all triggers
4785 * are unregistered prior to this clean-up.
4787 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4788 ust_app_notify_sock_unregister(app
->notify_sock
);
4792 /* Cleanup socket hash table */
4793 if (ust_app_ht_by_sock
) {
4794 lttng::urcu::read_lock_guard read_lock
;
4796 cds_lfht_for_each_entry (ust_app_ht_by_sock
->ht
, &iter
.iter
, app
, sock_n
.node
) {
4797 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4803 /* Destroy is done only when the ht is empty */
4805 lttng_ht_destroy(ust_app_ht
);
4807 if (ust_app_ht_by_sock
) {
4808 lttng_ht_destroy(ust_app_ht_by_sock
);
4810 if (ust_app_ht_by_notify_sock
) {
4811 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4816 * Init UST app hash table.
4818 int ust_app_ht_alloc()
4820 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4824 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4825 if (!ust_app_ht_by_sock
) {
4828 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4829 if (!ust_app_ht_by_notify_sock
) {
4836 * For a specific UST session, disable the channel for all registered apps.
4838 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4841 struct lttng_ht_iter iter
;
4842 struct lttng_ht_node_str
*ua_chan_node
;
4843 struct ust_app
*app
;
4844 struct ust_app_session
*ua_sess
;
4845 struct ust_app_channel
*ua_chan
;
4847 LTTNG_ASSERT(usess
->active
);
4848 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4853 lttng::urcu::read_lock_guard read_lock
;
4855 /* For every registered applications */
4856 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4857 struct lttng_ht_iter uiter
;
4858 if (!app
->compatible
) {
4860 * TODO: In time, we should notice the caller of this error by
4861 * telling him that this is a version error.
4865 ua_sess
= ust_app_lookup_app_session(usess
, app
);
4866 if (ua_sess
== nullptr) {
4871 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4872 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&uiter
);
4873 /* If the session if found for the app, the channel must be there */
4874 LTTNG_ASSERT(ua_chan_node
);
4876 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4877 /* The channel must not be already disabled */
4878 LTTNG_ASSERT(ua_chan
->enabled
);
4880 /* Disable channel onto application */
4881 ret
= disable_ust_app_channel(ua_sess
->lock(), ua_chan
, app
);
4883 /* XXX: We might want to report this error at some point... */
4893 * For a specific UST session, enable the channel for all registered apps.
4895 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4898 struct lttng_ht_iter iter
;
4899 struct ust_app
*app
;
4900 struct ust_app_session
*ua_sess
;
4902 LTTNG_ASSERT(usess
->active
);
4903 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4908 lttng::urcu::read_lock_guard read_lock
;
4910 /* For every registered applications */
4911 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4912 if (!app
->compatible
) {
4914 * TODO: In time, we should notice the caller of this error by
4915 * telling him that this is a version error.
4919 ua_sess
= ust_app_lookup_app_session(usess
, app
);
4920 if (ua_sess
== nullptr) {
4924 /* Enable channel onto application */
4925 ret
= enable_ust_app_channel(ua_sess
->lock(), uchan
, app
);
4927 /* XXX: We might want to report this error at some point... */
4937 * Disable an event in a channel and for a specific session.
4939 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4940 struct ltt_ust_channel
*uchan
,
4941 struct ltt_ust_event
*uevent
)
4944 struct lttng_ht_iter iter
, uiter
;
4945 struct lttng_ht_node_str
*ua_chan_node
;
4946 struct ust_app
*app
;
4947 struct ust_app_session
*ua_sess
;
4948 struct ust_app_channel
*ua_chan
;
4949 struct ust_app_event
*ua_event
;
4951 LTTNG_ASSERT(usess
->active
);
4952 DBG("UST app disabling event %s for all apps in channel "
4953 "%s for session id %" PRIu64
,
4959 lttng::urcu::read_lock_guard read_lock
;
4961 /* For all registered applications */
4962 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4963 if (!app
->compatible
) {
4965 * TODO: In time, we should notice the caller of this error by
4966 * telling him that this is a version error.
4970 ua_sess
= ust_app_lookup_app_session(usess
, app
);
4971 if (ua_sess
== nullptr) {
4976 /* Lookup channel in the ust app session */
4977 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4978 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&uiter
);
4979 if (ua_chan_node
== nullptr) {
4980 DBG2("Channel %s not found in session id %" PRIu64
4988 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4990 ua_event
= find_ust_app_event(
4994 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
4995 uevent
->attr
.loglevel
,
4997 if (ua_event
== nullptr) {
4998 DBG2("Event %s not found in channel %s for app pid %d."
5006 ret
= disable_ust_app_event(ua_event
, app
);
5008 /* XXX: Report error someday... */
5017 /* The ua_sess lock must be held by the caller. */
5018 static int ust_app_channel_create(struct ltt_ust_session
*usess
,
5019 const ust_app_session::locked_weak_ref
& ua_sess
,
5020 struct ltt_ust_channel
*uchan
,
5021 struct ust_app
*app
,
5022 struct ust_app_channel
**_ua_chan
)
5025 struct ust_app_channel
*ua_chan
= nullptr;
5027 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
))) {
5028 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
5031 struct ltt_ust_context
*uctx
= nullptr;
5034 * Create channel onto application and synchronize its
5037 ret
= ust_app_channel_allocate(
5038 ua_sess
, uchan
, LTTNG_UST_ABI_CHAN_PER_CPU
, usess
, &ua_chan
);
5043 ret
= ust_app_channel_send(app
, usess
, ua_sess
, ua_chan
);
5049 cds_list_for_each_entry (uctx
, &uchan
->ctx_list
, list
) {
5050 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5062 * The application's socket is not valid. Either a bad socket
5063 * or a timeout on it. We can't inform the caller that for a
5064 * specific app, the session failed so lets continue here.
5066 ret
= 0; /* Not an error. */
5074 if (ret
== 0 && _ua_chan
) {
5076 * Only return the application's channel on success. Note
5077 * that the channel can still be part of the application's
5078 * channel hashtable on error.
5080 *_ua_chan
= ua_chan
;
5086 * Enable event for a specific session and channel on the tracer.
5088 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
5089 struct ltt_ust_channel
*uchan
,
5090 struct ltt_ust_event
*uevent
)
5093 struct lttng_ht_iter iter
, uiter
;
5094 struct lttng_ht_node_str
*ua_chan_node
;
5095 struct ust_app
*app
;
5096 struct ust_app_session
*ua_sess
;
5097 struct ust_app_channel
*ua_chan
;
5098 struct ust_app_event
*ua_event
;
5100 LTTNG_ASSERT(usess
->active
);
5101 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
5106 * NOTE: At this point, this function is called only if the session and
5107 * channel passed are already created for all apps. and enabled on the
5112 lttng::urcu::read_lock_guard read_lock
;
5114 /* For all registered applications */
5115 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5116 if (!app
->compatible
) {
5118 * TODO: In time, we should notice the caller of this error by
5119 * telling him that this is a version error.
5123 ua_sess
= ust_app_lookup_app_session(usess
, app
);
5125 /* The application has problem or is probably dead. */
5129 auto locked_ua_sess
= ua_sess
->lock();
5130 if (ua_sess
->deleted
) {
5134 /* Lookup channel in the ust app session */
5135 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5136 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&uiter
);
5138 * It is possible that the channel cannot be found is
5139 * the channel/event creation occurs concurrently with
5140 * an application exit.
5142 if (!ua_chan_node
) {
5146 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5148 /* Get event node */
5149 ua_event
= find_ust_app_event(
5153 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
5154 uevent
->attr
.loglevel
,
5156 if (ua_event
== nullptr) {
5157 DBG3("UST app enable event %s not found for app PID %d."
5164 ret
= enable_ust_app_event(ua_event
, app
);
5175 * For a specific existing UST session and UST channel, creates the event for
5176 * all registered apps.
5178 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5179 struct ltt_ust_channel
*uchan
,
5180 struct ltt_ust_event
*uevent
)
5183 struct lttng_ht_iter iter
, uiter
;
5184 struct lttng_ht_node_str
*ua_chan_node
;
5185 struct ust_app
*app
;
5186 struct ust_app_session
*ua_sess
;
5187 struct ust_app_channel
*ua_chan
;
5189 LTTNG_ASSERT(usess
->active
);
5190 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5195 lttng::urcu::read_lock_guard read_lock
;
5197 /* For all registered applications */
5198 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5199 if (!app
->compatible
) {
5201 * TODO: In time, we should notice the caller of this error by
5202 * telling him that this is a version error.
5207 ua_sess
= ust_app_lookup_app_session(usess
, app
);
5209 /* The application has problem or is probably dead. */
5213 auto locked_ua_sess
= ua_sess
->lock();
5215 if (locked_ua_sess
->deleted
) {
5219 /* Lookup channel in the ust app session */
5220 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5221 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&uiter
);
5222 /* If the channel is not found, there is a code flow error */
5223 LTTNG_ASSERT(ua_chan_node
);
5225 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5227 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5229 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5230 /* Possible value at this point: -ENOMEM. If so, we stop! */
5234 DBG2("UST app event %s already exist on app PID %d",
5246 * Start tracing for a specific UST session and app.
5248 * Called with UST app session lock held.
5251 static int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5254 struct ust_app_session
*ua_sess
;
5256 DBG("Starting tracing for ust app pid %d", app
->pid
);
5258 lttng::urcu::read_lock_guard read_lock
;
5259 const auto update_health_code_on_exit
=
5260 lttng::make_scope_exit([]() noexcept
{ health_code_update(); });
5262 if (!app
->compatible
) {
5266 ua_sess
= ust_app_lookup_app_session(usess
, app
);
5267 if (ua_sess
== nullptr) {
5268 /* The session is in teardown process. Ignore and continue. */
5272 auto locked_ua_sess
= ua_sess
->lock();
5274 if (locked_ua_sess
->deleted
) {
5278 if (locked_ua_sess
->enabled
) {
5282 /* This starts the UST tracing */
5283 pthread_mutex_lock(&app
->sock_lock
);
5284 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5285 pthread_mutex_unlock(&app
->sock_lock
);
5287 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5288 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5292 } else if (ret
== -EAGAIN
) {
5293 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5299 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5308 /* Indicate that the session has been started once */
5309 ua_sess
->started
= true;
5310 ua_sess
->enabled
= true;
5312 health_code_update();
5314 /* Quiescent wait after starting trace */
5315 pthread_mutex_lock(&app
->sock_lock
);
5316 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5317 pthread_mutex_unlock(&app
->sock_lock
);
5319 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5320 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5323 } else if (ret
== -EAGAIN
) {
5324 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5328 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5339 * Stop tracing for a specific UST session and app.
5341 static int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5344 struct ust_app_session
*ua_sess
;
5346 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5348 lttng::urcu::read_lock_guard read_lock
;
5349 const auto update_health_code_on_exit
=
5350 lttng::make_scope_exit([]() noexcept
{ health_code_update(); });
5352 if (!app
->compatible
) {
5356 ua_sess
= ust_app_lookup_app_session(usess
, app
);
5357 if (ua_sess
== nullptr) {
5361 auto locked_ua_sess
= ua_sess
->lock();
5363 if (ua_sess
->deleted
) {
5368 * If started = 0, it means that stop trace has been called for a session
5369 * that was never started. It's possible since we can have a fail start
5370 * from either the application manager thread or the command thread. Simply
5371 * indicate that this is a stop error.
5373 if (!ua_sess
->started
) {
5377 health_code_update();
5379 /* This inhibits UST tracing */
5380 pthread_mutex_lock(&app
->sock_lock
);
5381 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5382 pthread_mutex_unlock(&app
->sock_lock
);
5384 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5385 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5389 } else if (ret
== -EAGAIN
) {
5390 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5396 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5405 health_code_update();
5406 ua_sess
->enabled
= false;
5408 /* Quiescent wait after stopping trace */
5409 pthread_mutex_lock(&app
->sock_lock
);
5410 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5411 pthread_mutex_unlock(&app
->sock_lock
);
5413 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5414 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5417 } else if (ret
== -EAGAIN
) {
5418 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5422 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5429 health_code_update();
5432 auto locked_registry
=
5433 get_locked_session_registry(locked_ua_sess
->get_identifier());
5435 /* The UST app session is held registry shall not be null. */
5436 LTTNG_ASSERT(locked_registry
);
5438 /* Push metadata for application before freeing the application. */
5439 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5445 static int ust_app_flush_app_session(ust_app
& app
, ust_app_session
& ua_sess
)
5447 int ret
, retval
= 0;
5448 struct lttng_ht_iter iter
;
5449 struct ust_app_channel
*ua_chan
;
5450 struct consumer_socket
*socket
;
5452 const auto update_health_code_on_exit
=
5453 lttng::make_scope_exit([]() noexcept
{ health_code_update(); });
5455 DBG("Flushing app session buffers for ust app pid %d", app
.pid
);
5457 if (!app
.compatible
) {
5461 const auto locked_ua_sess
= ua_sess
.lock();
5462 if (locked_ua_sess
->deleted
) {
5466 health_code_update();
5468 /* Flushing buffers */
5469 socket
= consumer_find_socket_by_bitness(app
.abi
.bits_per_long
, ua_sess
.consumer
);
5471 /* Flush buffers and push metadata. */
5472 switch (ua_sess
.buffer_type
) {
5473 case LTTNG_BUFFER_PER_PID
:
5475 lttng::urcu::read_lock_guard read_lock
;
5477 cds_lfht_for_each_entry (ua_sess
.channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5478 health_code_update();
5479 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5481 ERR("Error flushing consumer channel");
5489 case LTTNG_BUFFER_PER_UID
:
5499 * Flush buffers for all applications for a specific UST session.
5500 * Called with UST session lock held.
5502 static int ust_app_flush_session(struct ltt_ust_session
*usess
)
5507 DBG("Flushing session buffers for all ust apps");
5509 /* Flush buffers and push metadata. */
5510 switch (usess
->buffer_type
) {
5511 case LTTNG_BUFFER_PER_UID
:
5513 struct buffer_reg_uid
*reg
;
5514 struct lttng_ht_iter iter
;
5516 /* Flush all per UID buffers associated to that session. */
5517 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5518 lttng::urcu::read_lock_guard read_lock
;
5519 lsu::registry_session
*ust_session_reg
;
5520 struct buffer_reg_channel
*buf_reg_chan
;
5521 struct consumer_socket
*socket
;
5523 /* Get consumer socket to use to push the metadata.*/
5524 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5527 /* Ignore request if no consumer is found for the session. */
5531 cds_lfht_for_each_entry (
5532 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5534 * The following call will print error values so the return
5535 * code is of little importance because whatever happens, we
5536 * have to try them all.
5538 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5541 ust_session_reg
= reg
->registry
->reg
.ust
;
5542 /* Push metadata. */
5543 auto locked_registry
= ust_session_reg
->lock();
5544 (void) push_metadata(locked_registry
, usess
->consumer
);
5549 case LTTNG_BUFFER_PER_PID
:
5551 struct ust_app_session
*ua_sess
;
5552 struct lttng_ht_iter iter
;
5553 struct ust_app
*app
;
5554 lttng::urcu::read_lock_guard read_lock
;
5556 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5557 ua_sess
= ust_app_lookup_app_session(usess
, app
);
5558 if (ua_sess
== nullptr) {
5562 (void) ust_app_flush_app_session(*app
, *ua_sess
);
5573 health_code_update();
5577 static int ust_app_clear_quiescent_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5580 struct lttng_ht_iter iter
;
5581 struct ust_app_channel
*ua_chan
;
5582 struct consumer_socket
*socket
;
5584 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5586 lttng::urcu::read_lock_guard read_lock
;
5587 const auto update_health_code_on_exit
=
5588 lttng::make_scope_exit([]() noexcept
{ health_code_update(); });
5590 if (!app
->compatible
) {
5594 const auto locked_ua_sess
= ua_sess
->lock();
5595 if (locked_ua_sess
->deleted
) {
5599 health_code_update();
5601 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5603 ERR("Failed to find consumer (%" PRIu32
") socket", app
->abi
.bits_per_long
);
5607 /* Clear quiescent state. */
5608 switch (ua_sess
->buffer_type
) {
5609 case LTTNG_BUFFER_PER_PID
:
5610 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5611 health_code_update();
5612 ret
= consumer_clear_quiescent_channel(socket
, ua_chan
->key
);
5614 ERR("Error clearing quiescent state for consumer channel");
5620 case LTTNG_BUFFER_PER_UID
:
5631 * Clear quiescent state in each stream for all applications for a
5632 * specific UST session.
5633 * Called with UST session lock held.
5635 static int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5640 DBG("Clearing stream quiescent state for all ust apps");
5642 switch (usess
->buffer_type
) {
5643 case LTTNG_BUFFER_PER_UID
:
5645 struct lttng_ht_iter iter
;
5646 struct buffer_reg_uid
*reg
;
5649 * Clear quiescent for all per UID buffers associated to
5652 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5653 struct consumer_socket
*socket
;
5654 struct buffer_reg_channel
*buf_reg_chan
;
5655 lttng::urcu::read_lock_guard read_lock
;
5657 /* Get associated consumer socket.*/
5658 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5662 * Ignore request if no consumer is found for
5668 cds_lfht_for_each_entry (
5669 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5671 * The following call will print error values so
5672 * the return code is of little importance
5673 * because whatever happens, we have to try them
5676 (void) consumer_clear_quiescent_channel(socket
,
5677 buf_reg_chan
->consumer_key
);
5683 case LTTNG_BUFFER_PER_PID
:
5685 struct ust_app_session
*ua_sess
;
5686 struct lttng_ht_iter iter
;
5687 struct ust_app
*app
;
5688 lttng::urcu::read_lock_guard read_lock
;
5690 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5691 ua_sess
= ust_app_lookup_app_session(usess
, app
);
5692 if (ua_sess
== nullptr) {
5695 (void) ust_app_clear_quiescent_app_session(app
, ua_sess
);
5706 health_code_update();
5711 * Destroy a specific UST session in apps.
5713 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5716 struct ust_app_session
*ua_sess
;
5717 struct lttng_ht_iter iter
;
5718 struct lttng_ht_node_u64
*node
;
5720 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5722 lttng::urcu::read_lock_guard read_lock
;
5724 if (!app
->compatible
) {
5728 __lookup_session_by_app(usess
, app
, &iter
);
5729 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
5730 if (node
== nullptr) {
5731 /* Session is being or is deleted. */
5734 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5736 health_code_update();
5737 destroy_app_session(app
, ua_sess
);
5739 health_code_update();
5741 /* Quiescent wait after stopping trace */
5742 pthread_mutex_lock(&app
->sock_lock
);
5743 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5744 pthread_mutex_unlock(&app
->sock_lock
);
5746 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5747 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5750 } else if (ret
== -EAGAIN
) {
5751 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5755 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5762 health_code_update();
5767 * Start tracing for the UST session.
5769 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5771 struct lttng_ht_iter iter
;
5772 struct ust_app
*app
;
5774 DBG("Starting all UST traces");
5777 * Even though the start trace might fail, flag this session active so
5778 * other application coming in are started by default.
5780 usess
->active
= true;
5783 * In a start-stop-start use-case, we need to clear the quiescent state
5784 * of each channel set by the prior stop command, thus ensuring that a
5785 * following stop or destroy is sure to grab a timestamp_end near those
5786 * operations, even if the packet is empty.
5788 (void) ust_app_clear_quiescent_session(usess
);
5791 lttng::urcu::read_lock_guard read_lock
;
5793 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5794 ust_app_global_update(usess
, app
);
5802 * Start tracing for the UST session.
5803 * Called with UST session lock held.
5805 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5808 struct lttng_ht_iter iter
;
5809 struct ust_app
*app
;
5811 DBG("Stopping all UST traces");
5814 * Even though the stop trace might fail, flag this session inactive so
5815 * other application coming in are not started by default.
5817 usess
->active
= false;
5820 lttng::urcu::read_lock_guard read_lock
;
5822 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5823 ret
= ust_app_stop_trace(usess
, app
);
5825 /* Continue to next apps even on error */
5831 (void) ust_app_flush_session(usess
);
5837 * Destroy app UST session.
5839 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5842 struct lttng_ht_iter iter
;
5843 struct ust_app
*app
;
5845 DBG("Destroy all UST traces");
5848 lttng::urcu::read_lock_guard read_lock
;
5850 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5851 ret
= destroy_trace(usess
, app
);
5853 /* Continue to next apps even on error */
5862 /* The ua_sess lock must be held by the caller. */
5863 static int find_or_create_ust_app_channel(struct ltt_ust_session
*usess
,
5864 const ust_app_session::locked_weak_ref
& ua_sess
,
5865 struct ust_app
*app
,
5866 struct ltt_ust_channel
*uchan
,
5867 struct ust_app_channel
**ua_chan
)
5870 struct lttng_ht_iter iter
;
5871 struct lttng_ht_node_str
*ua_chan_node
;
5873 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5874 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&iter
);
5876 *ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5880 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5888 static int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5889 struct ltt_ust_event
*uevent
,
5890 struct ust_app
*app
)
5893 struct ust_app_event
*ua_event
= nullptr;
5895 ua_event
= find_ust_app_event(ua_chan
->events
,
5898 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
5899 uevent
->attr
.loglevel
,
5902 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5907 if (ua_event
->enabled
!= uevent
->enabled
) {
5908 ret
= uevent
->enabled
? enable_ust_app_event(ua_event
, app
) :
5909 disable_ust_app_event(ua_event
, app
);
5917 /* Called with RCU read-side lock held. */
5918 static void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5921 enum lttng_error_code ret_code
;
5922 enum lttng_trigger_status t_status
;
5923 struct lttng_ht_iter app_trigger_iter
;
5924 struct lttng_triggers
*triggers
= nullptr;
5925 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5926 unsigned int count
, i
;
5928 ASSERT_RCU_READ_LOCKED();
5930 if (!ust_app_supports_notifiers(app
)) {
5935 * Currrently, registering or unregistering a trigger with an
5936 * event rule condition causes a full synchronization of the event
5939 * The first step attempts to add an event notifier for all registered
5940 * triggers that apply to the user space tracers. Then, the
5941 * application's event notifiers rules are all checked against the list
5942 * of registered triggers. Any event notifier that doesn't have a
5943 * matching trigger can be assumed to have been disabled.
5945 * All of this is inefficient, but is put in place to get the feature
5946 * rolling as it is simpler at this moment. It will be optimized Soon™
5947 * to allow the state of enabled
5948 * event notifiers to be synchronized in a piece-wise way.
5951 /* Get all triggers using uid 0 (root) */
5952 ret_code
= notification_thread_command_list_triggers(
5953 the_notification_thread_handle
, 0, &triggers
);
5954 if (ret_code
!= LTTNG_OK
) {
5958 LTTNG_ASSERT(triggers
);
5960 t_status
= lttng_triggers_get_count(triggers
, &count
);
5961 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5965 for (i
= 0; i
< count
; i
++) {
5966 const struct lttng_condition
*condition
;
5967 const struct lttng_event_rule
*event_rule
;
5968 struct lttng_trigger
*trigger
;
5969 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5970 enum lttng_condition_status condition_status
;
5973 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5974 LTTNG_ASSERT(trigger
);
5976 token
= lttng_trigger_get_tracer_token(trigger
);
5977 condition
= lttng_trigger_get_const_condition(trigger
);
5979 if (lttng_condition_get_type(condition
) !=
5980 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5981 /* Does not apply */
5986 lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
5987 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5989 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5990 /* Skip kernel related triggers. */
5995 * Find or create the associated token event rule. The caller
5996 * holds the RCU read lock, so this is safe to call without
5997 * explicitly acquiring it here.
5999 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
6000 app
->token_to_event_notifier_rule_ht
, token
);
6001 if (!looked_up_event_notifier_rule
) {
6002 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
6010 lttng::urcu::read_lock_guard read_lock
;
6012 /* Remove all unknown event sources from the app. */
6013 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
6014 &app_trigger_iter
.iter
,
6015 event_notifier_rule
,
6017 const uint64_t app_token
= event_notifier_rule
->token
;
6021 * Check if the app event trigger still exists on the
6022 * notification side.
6024 for (i
= 0; i
< count
; i
++) {
6025 uint64_t notification_thread_token
;
6026 const struct lttng_trigger
*trigger
=
6027 lttng_triggers_get_at_index(triggers
, i
);
6029 LTTNG_ASSERT(trigger
);
6031 notification_thread_token
= lttng_trigger_get_tracer_token(trigger
);
6033 if (notification_thread_token
== app_token
) {
6045 * This trigger was unregistered, disable it on the tracer's
6048 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &app_trigger_iter
);
6049 LTTNG_ASSERT(ret
== 0);
6051 /* Callee logs errors. */
6052 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
6054 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
6059 lttng_triggers_destroy(triggers
);
6064 * RCU read lock must be held by the caller.
6066 static void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
6067 const ust_app_session::locked_weak_ref
& ua_sess
,
6068 struct ust_app
*app
)
6071 struct cds_lfht_iter uchan_iter
;
6072 struct ltt_ust_channel
*uchan
;
6074 LTTNG_ASSERT(usess
);
6076 ASSERT_RCU_READ_LOCKED();
6078 cds_lfht_for_each_entry (usess
->domain_global
.channels
->ht
, &uchan_iter
, uchan
, node
.node
) {
6079 struct ust_app_channel
*ua_chan
;
6080 struct cds_lfht_iter uevent_iter
;
6081 struct ltt_ust_event
*uevent
;
6084 * Search for a matching ust_app_channel. If none is found,
6085 * create it. Creating the channel will cause the ua_chan
6086 * structure to be allocated, the channel buffers to be
6087 * allocated (if necessary) and sent to the application, and
6088 * all enabled contexts will be added to the channel.
6090 ret
= find_or_create_ust_app_channel(usess
, ua_sess
, app
, uchan
, &ua_chan
);
6092 /* Tracer is probably gone or ENOMEM. */
6097 /* ua_chan will be NULL for the metadata channel */
6101 cds_lfht_for_each_entry (uchan
->events
->ht
, &uevent_iter
, uevent
, node
.node
) {
6102 ret
= ust_app_channel_synchronize_event(ua_chan
, uevent
, app
);
6108 if (ua_chan
->enabled
!= uchan
->enabled
) {
6109 ret
= uchan
->enabled
? enable_ust_app_channel(ua_sess
, uchan
, app
) :
6110 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6121 * The caller must ensure that the application is compatible and is tracked
6122 * by the process attribute trackers.
6124 static void ust_app_synchronize(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6127 struct ust_app_session
*ua_sess
= nullptr;
6130 * The application's configuration should only be synchronized for
6133 LTTNG_ASSERT(usess
->active
);
6135 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, nullptr);
6137 /* Tracer is probably gone or ENOMEM. */
6141 LTTNG_ASSERT(ua_sess
);
6143 const auto locked_ua_sess
= ua_sess
->lock();
6144 if (locked_ua_sess
->deleted
) {
6149 lttng::urcu::read_lock_guard read_lock
;
6151 ust_app_synchronize_all_channels(usess
, locked_ua_sess
, app
);
6154 * Create the metadata for the application. This returns gracefully if a
6155 * metadata was already set for the session.
6157 * The metadata channel must be created after the data channels as the
6158 * consumer daemon assumes this ordering. When interacting with a relay
6159 * daemon, the consumer will use this assumption to send the
6160 * "STREAMS_SENT" message to the relay daemon.
6162 ret
= create_ust_app_metadata(locked_ua_sess
, app
, usess
->consumer
);
6164 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6171 static void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6173 struct ust_app_session
*ua_sess
;
6175 ua_sess
= ust_app_lookup_app_session(usess
, app
);
6176 if (ua_sess
== nullptr) {
6179 destroy_app_session(app
, ua_sess
);
6183 * Add channels/events from UST global domain to registered apps at sock.
6185 * Called with session lock held.
6186 * Called with RCU read-side lock held.
6188 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6190 LTTNG_ASSERT(usess
);
6191 LTTNG_ASSERT(usess
->active
);
6192 ASSERT_RCU_READ_LOCKED();
6194 DBG2("UST app global update for app sock %d for session id %" PRIu64
, app
->sock
, usess
->id
);
6196 if (!app
->compatible
) {
6199 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, usess
, app
->pid
) &&
6200 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
, usess
, app
->uid
) &&
6201 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
, usess
, app
->gid
)) {
6203 * Synchronize the application's internal tracing configuration
6204 * and start tracing.
6206 ust_app_synchronize(usess
, app
);
6207 ust_app_start_trace(usess
, app
);
6209 ust_app_global_destroy(usess
, app
);
6214 * Add all event notifiers to an application.
6216 * Called with session lock held.
6217 * Called with RCU read-side lock held.
6219 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6221 ASSERT_RCU_READ_LOCKED();
6223 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6227 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6231 if (app
->event_notifier_group
.object
== nullptr) {
6232 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6238 ust_app_synchronize_event_notifier_rules(app
);
6242 * Called with session lock held.
6244 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6246 struct lttng_ht_iter iter
;
6247 struct ust_app
*app
;
6250 lttng::urcu::read_lock_guard read_lock
;
6252 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6253 ust_app_global_update(usess
, app
);
6258 void ust_app_global_update_all_event_notifier_rules()
6260 struct lttng_ht_iter iter
;
6261 struct ust_app
*app
;
6263 lttng::urcu::read_lock_guard read_lock
;
6264 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6265 ust_app_global_update_event_notifier_rules(app
);
6270 * Add context to a specific channel for global UST domain.
6272 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6273 struct ltt_ust_channel
*uchan
,
6274 struct ltt_ust_context
*uctx
)
6277 struct lttng_ht_node_str
*ua_chan_node
;
6278 struct lttng_ht_iter iter
, uiter
;
6279 struct ust_app_channel
*ua_chan
= nullptr;
6280 struct ust_app_session
*ua_sess
;
6281 struct ust_app
*app
;
6283 LTTNG_ASSERT(usess
->active
);
6286 lttng::urcu::read_lock_guard read_lock
;
6287 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6288 if (!app
->compatible
) {
6290 * TODO: In time, we should notice the caller of this error by
6291 * telling him that this is a version error.
6295 ua_sess
= ust_app_lookup_app_session(usess
, app
);
6296 if (ua_sess
== nullptr) {
6300 const auto locked_ua_sess
= ua_sess
->lock();
6301 if (locked_ua_sess
->deleted
) {
6305 /* Lookup channel in the ust app session */
6306 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6307 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&uiter
);
6308 if (ua_chan_node
== nullptr) {
6311 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6312 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6323 * Receive registration and populate the given msg structure.
6325 * On success return 0 else a negative value returned by the ustctl call.
6327 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6330 uint32_t pid
, ppid
, uid
, gid
;
6334 ret
= lttng_ust_ctl_recv_reg_msg(sock
,
6342 &msg
->bits_per_long
,
6343 &msg
->uint8_t_alignment
,
6344 &msg
->uint16_t_alignment
,
6345 &msg
->uint32_t_alignment
,
6346 &msg
->uint64_t_alignment
,
6347 &msg
->long_alignment
,
6354 case LTTNG_UST_ERR_EXITING
:
6355 DBG3("UST app recv reg message failed. Application died");
6357 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6358 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6361 LTTNG_UST_ABI_MAJOR_VERSION
,
6362 LTTNG_UST_ABI_MINOR_VERSION
);
6365 ERR("UST app recv reg message failed with ret %d", ret
);
6370 msg
->pid
= (pid_t
) pid
;
6371 msg
->ppid
= (pid_t
) ppid
;
6372 msg
->uid
= (uid_t
) uid
;
6373 msg
->gid
= (gid_t
) gid
;
6380 * Return a ust app session object using the application object and the
6381 * session object descriptor has a key. If not found, NULL is returned.
6382 * A RCU read side lock MUST be acquired when calling this function.
6384 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
, int objd
)
6386 struct lttng_ht_node_ulong
*node
;
6387 struct lttng_ht_iter iter
;
6388 struct ust_app_session
*ua_sess
= nullptr;
6391 ASSERT_RCU_READ_LOCKED();
6393 lttng_ht_lookup(app
->ust_sessions_objd
, (void *) ((unsigned long) objd
), &iter
);
6394 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
6395 if (node
== nullptr) {
6396 DBG2("UST app session find by objd %d not found", objd
);
6400 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6407 * Return a ust app channel object using the application object and the channel
6408 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6409 * lock MUST be acquired before calling this function.
6411 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
, int objd
)
6413 struct lttng_ht_node_ulong
*node
;
6414 struct lttng_ht_iter iter
;
6415 struct ust_app_channel
*ua_chan
= nullptr;
6418 ASSERT_RCU_READ_LOCKED();
6420 lttng_ht_lookup(app
->ust_objd
, (void *) ((unsigned long) objd
), &iter
);
6421 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
6422 if (node
== nullptr) {
6423 DBG2("UST app channel find by objd %d not found", objd
);
6427 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6434 * Reply to a register channel notification from an application on the notify
6435 * socket. The channel metadata is also created.
6437 * The session UST registry lock is acquired in this function.
6439 * On success 0 is returned else a negative value.
6441 static int handle_app_register_channel_notification(int sock
,
6443 struct lttng_ust_ctl_field
*raw_context_fields
,
6444 size_t context_field_count
)
6446 int ret
, ret_code
= 0;
6448 uint64_t chan_reg_key
;
6449 struct ust_app
*app
;
6450 struct ust_app_channel
*ua_chan
;
6451 struct ust_app_session
*ua_sess
;
6452 auto ust_ctl_context_fields
=
6453 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::memory::free
>(
6454 raw_context_fields
);
6456 lttng::urcu::read_lock_guard read_lock_guard
;
6458 /* Lookup application. If not found, there is a code flow error. */
6459 app
= find_app_by_notify_sock(sock
);
6461 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6465 /* Lookup channel by UST object descriptor. */
6466 ua_chan
= find_channel_by_objd(app
, cobjd
);
6468 DBG("Application channel is being torn down. Abort event notify");
6472 LTTNG_ASSERT(ua_chan
->session
);
6473 ua_sess
= ua_chan
->session
;
6475 /* Get right session registry depending on the session buffer type. */
6478 * HACK: ua_sess is already locked by the client thread. This is called
6479 * in the context of the handling of a notification from the application.
6481 auto locked_ua_sess
= ust_app_session::make_locked_weak_ref(*ua_sess
);
6482 auto locked_registry_session
=
6483 get_locked_session_registry(locked_ua_sess
->get_identifier());
6484 locked_ua_sess
.release();
6485 if (!locked_registry_session
) {
6486 DBG("Application session is being torn down. Abort event notify");
6490 /* Depending on the buffer type, a different channel key is used. */
6491 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6492 chan_reg_key
= ua_chan
->tracing_channel_id
;
6494 chan_reg_key
= ua_chan
->key
;
6497 auto& ust_reg_chan
= locked_registry_session
->channel(chan_reg_key
);
6499 /* Channel id is set during the object creation. */
6500 chan_id
= ust_reg_chan
.id
;
6503 * The application returns the typing information of the channel's
6504 * context fields. In per-PID buffering mode, this is the first and only
6505 * time we get this information. It is our chance to finalize the
6506 * initialiation of the channel and serialize it's layout's description
6507 * to the trace's metadata.
6509 * However, in per-UID buffering mode, every application will provide
6510 * this information (redundantly). The first time will allow us to
6511 * complete the initialization. The following times, we simply validate
6512 * that all apps provide the same typing for the context fields as a
6516 auto app_context_fields
= lsu::create_trace_fields_from_ust_ctl_fields(
6517 *locked_registry_session
,
6518 ust_ctl_context_fields
.get(),
6519 context_field_count
,
6520 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT
,
6521 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
);
6523 if (!ust_reg_chan
.is_registered()) {
6524 lst::type::cuptr event_context
= app_context_fields
.size() ?
6525 lttng::make_unique
<lst::structure_type
>(
6526 0, std::move(app_context_fields
)) :
6529 ust_reg_chan
.event_context(std::move(event_context
));
6532 * Validate that the context fields match between
6533 * registry and newcoming application.
6535 bool context_fields_match
;
6536 const auto *previous_event_context
= ust_reg_chan
.event_context();
6538 if (!previous_event_context
) {
6539 context_fields_match
= app_context_fields
.size() == 0;
6541 const lst::structure_type
app_event_context_struct(
6542 0, std::move(app_context_fields
));
6544 context_fields_match
= *previous_event_context
==
6545 app_event_context_struct
;
6548 if (!context_fields_match
) {
6549 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6556 } catch (const std::exception
& ex
) {
6557 ERR("Failed to handle application context: %s", ex
.what());
6563 DBG3("UST app replying to register channel key %" PRIu64
" with id %u, ret = %d",
6568 ret
= lttng_ust_ctl_reply_register_channel(
6571 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6572 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6573 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6576 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6577 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6580 } else if (ret
== -EAGAIN
) {
6581 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6585 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6594 /* This channel registry's registration is completed. */
6595 ust_reg_chan
.set_as_registered();
6601 * Add event to the UST channel registry. When the event is added to the
6602 * registry, the metadata is also created. Once done, this replies to the
6603 * application with the appropriate error code.
6605 * The session UST registry lock is acquired in the function.
6607 * On success 0 is returned else a negative value.
6609 static int add_event_ust_registry(int sock
,
6613 char *raw_signature
,
6615 struct lttng_ust_ctl_field
*raw_fields
,
6617 char *raw_model_emf_uri
)
6620 uint32_t event_id
= 0;
6621 uint64_t chan_reg_key
;
6622 struct ust_app
*app
;
6623 struct ust_app_channel
*ua_chan
;
6624 struct ust_app_session
*ua_sess
;
6625 lttng::urcu::read_lock_guard rcu_lock
;
6626 auto signature
= lttng::make_unique_wrapper
<char, lttng::memory::free
>(raw_signature
);
6628 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::memory::free
>(raw_fields
);
6629 auto model_emf_uri
=
6630 lttng::make_unique_wrapper
<char, lttng::memory::free
>(raw_model_emf_uri
);
6632 /* Lookup application. If not found, there is a code flow error. */
6633 app
= find_app_by_notify_sock(sock
);
6635 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6639 /* Lookup channel by UST object descriptor. */
6640 ua_chan
= find_channel_by_objd(app
, cobjd
);
6642 DBG("Application channel is being torn down. Abort event notify");
6646 LTTNG_ASSERT(ua_chan
->session
);
6647 ua_sess
= ua_chan
->session
;
6649 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6650 chan_reg_key
= ua_chan
->tracing_channel_id
;
6652 chan_reg_key
= ua_chan
->key
;
6656 auto locked_registry
= get_locked_session_registry(ua_sess
->get_identifier());
6657 if (locked_registry
) {
6659 * From this point on, this call acquires the ownership of the signature,
6660 * fields and model_emf_uri meaning any free are done inside it if needed.
6661 * These three variables MUST NOT be read/write after this.
6664 auto& channel
= locked_registry
->channel(chan_reg_key
);
6666 /* event_id is set on success. */
6672 lsu::create_trace_fields_from_ust_ctl_fields(
6676 lst::field_location::root::EVENT_RECORD_PAYLOAD
,
6677 lsu::ctl_field_quirks::
6678 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
),
6680 model_emf_uri
.get() ?
6681 nonstd::optional
<std::string
>(model_emf_uri
.get()) :
6683 ua_sess
->buffer_type
,
6687 } catch (const std::exception
& ex
) {
6688 ERR("Failed to add event `%s` to registry session: %s",
6691 /* Inform the application of the error; don't return directly. */
6695 DBG("Application session is being torn down. Abort event notify");
6701 * The return value is returned to ustctl so in case of an error, the
6702 * application can be notified. In case of an error, it's important not to
6703 * return a negative error or else the application will get closed.
6705 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6707 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6708 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6711 } else if (ret
== -EAGAIN
) {
6712 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6716 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6722 * No need to wipe the create event since the application socket will
6723 * get close on error hence cleaning up everything by itself.
6728 DBG3("UST registry event %s with id %" PRId32
" added successfully", name
, event_id
);
6733 * Add enum to the UST session registry. Once done, this replies to the
6734 * application with the appropriate error code.
6736 * The session UST registry lock is acquired within this function.
6738 * On success 0 is returned else a negative value.
6740 static int add_enum_ust_registry(int sock
,
6743 struct lttng_ust_ctl_enum_entry
*raw_entries
,
6747 struct ust_app
*app
;
6748 struct ust_app_session
*ua_sess
;
6749 uint64_t enum_id
= -1ULL;
6750 lttng::urcu::read_lock_guard read_lock_guard
;
6752 lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::memory::free
>(
6755 /* Lookup application. If not found, there is a code flow error. */
6756 app
= find_app_by_notify_sock(sock
);
6758 /* Return an error since this is not an error */
6759 DBG("Application socket %d is being torn down. Aborting enum registration", sock
);
6763 /* Lookup session by UST object descriptor. */
6764 ua_sess
= find_session_by_objd(app
, sobjd
);
6766 /* Return an error since this is not an error */
6767 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6771 auto locked_registry
= get_locked_session_registry(ua_sess
->get_identifier());
6772 if (!locked_registry
) {
6773 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6778 * From this point on, the callee acquires the ownership of
6779 * entries. The variable entries MUST NOT be read/written after
6782 int application_reply_code
;
6784 locked_registry
->create_or_find_enum(
6785 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6786 application_reply_code
= 0;
6787 } catch (const std::exception
& ex
) {
6790 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6795 application_reply_code
= -1;
6799 * The return value is returned to ustctl so in case of an error, the
6800 * application can be notified. In case of an error, it's important not to
6801 * return a negative error or else the application will get closed.
6803 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6805 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6806 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6809 } else if (ret
== -EAGAIN
) {
6810 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6814 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6820 * No need to wipe the create enum since the application socket will
6821 * get close on error hence cleaning up everything by itself.
6826 DBG3("UST registry enum %s added successfully or already found", name
);
6831 * Handle application notification through the given notify socket.
6833 * Return 0 on success or else a negative value.
6835 int ust_app_recv_notify(int sock
)
6838 enum lttng_ust_ctl_notify_cmd cmd
;
6840 DBG3("UST app receiving notify from sock %d", sock
);
6842 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6844 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6845 DBG3("UST app recv notify failed. Application died: sock = %d", sock
);
6846 } else if (ret
== -EAGAIN
) {
6847 WARN("UST app recv notify failed. Communication time out: sock = %d", sock
);
6849 ERR("UST app recv notify failed with ret %d: sock = %d", ret
, sock
);
6855 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6857 int sobjd
, cobjd
, loglevel_value
;
6858 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6860 struct lttng_ust_ctl_field
*fields
;
6862 DBG2("UST app ustctl register event received");
6864 ret
= lttng_ust_ctl_recv_register_event(sock
,
6874 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6875 DBG3("UST app recv event failed. Application died: sock = %d",
6877 } else if (ret
== -EAGAIN
) {
6878 WARN("UST app recv event failed. Communication time out: sock = %d",
6881 ERR("UST app recv event failed with ret %d: sock = %d", ret
, sock
);
6887 lttng::urcu::read_lock_guard rcu_lock
;
6888 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6890 DBG("Application socket %d is being torn down. Abort event notify",
6897 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6898 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6907 * Add event to the UST registry coming from the notify socket. This
6908 * call will free if needed the sig, fields and model_emf_uri. This
6909 * code path loses the ownsership of these variables and transfer them
6910 * to the this function.
6912 ret
= add_event_ust_registry(sock
,
6927 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6931 struct lttng_ust_ctl_field
*context_fields
;
6933 DBG2("UST app ustctl register channel received");
6935 ret
= lttng_ust_ctl_recv_register_channel(
6936 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6938 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6939 DBG3("UST app recv channel failed. Application died: sock = %d",
6941 } else if (ret
== -EAGAIN
) {
6942 WARN("UST app recv channel failed. Communication time out: sock = %d",
6945 ERR("UST app recv channel failed with ret %d: sock = %d",
6953 * The fields ownership are transfered to this function call meaning
6954 * that if needed it will be freed. After this, it's invalid to access
6955 * fields or clean them up.
6957 ret
= handle_app_register_channel_notification(
6958 sock
, cobjd
, context_fields
, field_count
);
6965 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6968 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6970 struct lttng_ust_ctl_enum_entry
*entries
;
6972 DBG2("UST app ustctl register enum received");
6974 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
, &entries
, &nr_entries
);
6976 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6977 DBG3("UST app recv enum failed. Application died: sock = %d", sock
);
6978 } else if (ret
== -EAGAIN
) {
6979 WARN("UST app recv enum failed. Communication time out: sock = %d",
6982 ERR("UST app recv enum failed with ret %d: sock = %d", ret
, sock
);
6987 /* Callee assumes ownership of entries. */
6988 ret
= add_enum_ust_registry(sock
, sobjd
, name
, entries
, nr_entries
);
6996 /* Should NEVER happen. */
7005 * Once the notify socket hangs up, this is called. First, it tries to find the
7006 * corresponding application. On failure, the call_rcu to close the socket is
7007 * executed. If an application is found, it tries to delete it from the notify
7008 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7010 * Note that an object needs to be allocated here so on ENOMEM failure, the
7011 * call RCU is not done but the rest of the cleanup is.
7013 void ust_app_notify_sock_unregister(int sock
)
7016 struct lttng_ht_iter iter
;
7017 struct ust_app
*app
;
7018 struct ust_app_notify_sock_obj
*obj
;
7020 LTTNG_ASSERT(sock
>= 0);
7022 lttng::urcu::read_lock_guard read_lock
;
7024 obj
= zmalloc
<ust_app_notify_sock_obj
>();
7027 * An ENOMEM is kind of uncool. If this strikes we continue the
7028 * procedure but the call_rcu will not be called. In this case, we
7029 * accept the fd leak rather than possibly creating an unsynchronized
7030 * state between threads.
7032 * TODO: The notify object should be created once the notify socket is
7033 * registered and stored independantely from the ust app object. The
7034 * tricky part is to synchronize the teardown of the application and
7035 * this notify object. Let's keep that in mind so we can avoid this
7036 * kind of shenanigans with ENOMEM in the teardown path.
7043 DBG("UST app notify socket unregister %d", sock
);
7046 * Lookup application by notify socket. If this fails, this means that the
7047 * hash table delete has already been done by the application
7048 * unregistration process so we can safely close the notify socket in a
7051 app
= find_app_by_notify_sock(sock
);
7056 iter
.iter
.node
= &app
->notify_sock_n
.node
;
7059 * Whatever happens here either we fail or succeed, in both cases we have
7060 * to close the socket after a grace period to continue to the call RCU
7061 * here. If the deletion is successful, the application is not visible
7062 * anymore by other threads and is it fails it means that it was already
7063 * deleted from the hash table so either way we just have to close the
7066 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
7071 * Close socket after a grace period to avoid for the socket to be reused
7072 * before the application object is freed creating potential race between
7073 * threads trying to add unique in the global hash table.
7076 call_rcu(&obj
->head
, close_notify_sock_rcu
);
7081 * Destroy a ust app data structure and free its memory.
7083 static void ust_app_destroy(ust_app
& app
)
7085 call_rcu(&app
.pid_n
.head
, delete_ust_app_rcu
);
7089 * Take a snapshot for a given UST session. The snapshot is sent to the given
7092 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7094 enum lttng_error_code
ust_app_snapshot_record(const struct ltt_ust_session
*usess
,
7095 const struct consumer_output
*output
,
7096 uint64_t nb_packets_per_stream
)
7099 enum lttng_error_code status
= LTTNG_OK
;
7100 struct lttng_ht_iter iter
;
7101 struct ust_app
*app
;
7102 char *trace_path
= nullptr;
7104 LTTNG_ASSERT(usess
);
7105 LTTNG_ASSERT(output
);
7107 switch (usess
->buffer_type
) {
7108 case LTTNG_BUFFER_PER_UID
:
7110 struct buffer_reg_uid
*reg
;
7112 lttng::urcu::read_lock_guard read_lock
;
7114 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7115 struct buffer_reg_channel
*buf_reg_chan
;
7116 struct consumer_socket
*socket
;
7117 char pathname
[PATH_MAX
];
7118 size_t consumer_path_offset
= 0;
7120 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7121 /* Skip since no metadata is present */
7125 /* Get consumer socket to use to push the metadata.*/
7126 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7129 status
= LTTNG_ERR_INVALID
;
7133 memset(pathname
, 0, sizeof(pathname
));
7134 ret
= snprintf(pathname
,
7136 DEFAULT_UST_TRACE_UID_PATH
,
7138 reg
->bits_per_long
);
7140 PERROR("snprintf snapshot path");
7141 status
= LTTNG_ERR_INVALID
;
7144 /* Free path allowed on previous iteration. */
7146 trace_path
= setup_channel_trace_path(
7147 usess
->consumer
, pathname
, &consumer_path_offset
);
7149 status
= LTTNG_ERR_INVALID
;
7152 /* Add the UST default trace dir to path. */
7153 cds_lfht_for_each_entry (
7154 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7156 consumer_snapshot_channel(socket
,
7157 buf_reg_chan
->consumer_key
,
7160 &trace_path
[consumer_path_offset
],
7161 nb_packets_per_stream
);
7162 if (status
!= LTTNG_OK
) {
7166 status
= consumer_snapshot_channel(socket
,
7167 reg
->registry
->reg
.ust
->_metadata_key
,
7170 &trace_path
[consumer_path_offset
],
7172 if (status
!= LTTNG_OK
) {
7179 case LTTNG_BUFFER_PER_PID
:
7181 lttng::urcu::read_lock_guard read_lock
;
7183 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7184 struct consumer_socket
*socket
;
7185 struct lttng_ht_iter chan_iter
;
7186 struct ust_app_channel
*ua_chan
;
7187 struct ust_app_session
*ua_sess
;
7188 lsu::registry_session
*registry
;
7189 char pathname
[PATH_MAX
];
7190 size_t consumer_path_offset
= 0;
7192 ua_sess
= ust_app_lookup_app_session(usess
, app
);
7194 /* Session not associated with this app. */
7198 /* Get the right consumer socket for the application. */
7199 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, output
);
7201 status
= LTTNG_ERR_INVALID
;
7205 /* Add the UST default trace dir to path. */
7206 memset(pathname
, 0, sizeof(pathname
));
7207 ret
= snprintf(pathname
, sizeof(pathname
), "%s", ua_sess
->path
);
7209 status
= LTTNG_ERR_INVALID
;
7210 PERROR("snprintf snapshot path");
7213 /* Free path allowed on previous iteration. */
7215 trace_path
= setup_channel_trace_path(
7216 usess
->consumer
, pathname
, &consumer_path_offset
);
7218 status
= LTTNG_ERR_INVALID
;
7221 cds_lfht_for_each_entry (
7222 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7224 consumer_snapshot_channel(socket
,
7228 &trace_path
[consumer_path_offset
],
7229 nb_packets_per_stream
);
7233 case LTTNG_ERR_CHAN_NOT_FOUND
:
7240 registry
= ust_app_get_session_registry(ua_sess
->get_identifier());
7242 DBG("Application session is being torn down. Skip application.");
7245 status
= consumer_snapshot_channel(socket
,
7246 registry
->_metadata_key
,
7249 &trace_path
[consumer_path_offset
],
7254 case LTTNG_ERR_CHAN_NOT_FOUND
:
7273 * Return the size taken by one more packet per stream.
7275 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session
*usess
,
7276 uint64_t cur_nr_packets
)
7278 uint64_t tot_size
= 0;
7279 struct ust_app
*app
;
7280 struct lttng_ht_iter iter
;
7282 LTTNG_ASSERT(usess
);
7284 switch (usess
->buffer_type
) {
7285 case LTTNG_BUFFER_PER_UID
:
7287 struct buffer_reg_uid
*reg
;
7289 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7290 struct buffer_reg_channel
*buf_reg_chan
;
7292 lttng::urcu::read_lock_guard read_lock
;
7294 cds_lfht_for_each_entry (
7295 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7296 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7298 * Don't take channel into account if we
7299 * already grab all its packets.
7303 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7308 case LTTNG_BUFFER_PER_PID
:
7310 lttng::urcu::read_lock_guard read_lock
;
7312 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7313 struct ust_app_channel
*ua_chan
;
7314 struct ust_app_session
*ua_sess
;
7315 struct lttng_ht_iter chan_iter
;
7317 ua_sess
= ust_app_lookup_app_session(usess
, app
);
7319 /* Session not associated with this app. */
7323 cds_lfht_for_each_entry (
7324 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7325 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7327 * Don't take channel into account if we
7328 * already grab all its packets.
7332 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7345 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7346 struct cds_list_head
*buffer_reg_uid_list
,
7347 struct consumer_output
*consumer
,
7350 uint64_t *discarded
,
7354 uint64_t consumer_chan_key
;
7359 ret
= buffer_reg_uid_consumer_channel_key(
7360 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7368 ret
= consumer_get_lost_packets(ust_session_id
, consumer_chan_key
, consumer
, lost
);
7370 ret
= consumer_get_discarded_events(
7371 ust_session_id
, consumer_chan_key
, consumer
, discarded
);
7378 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7379 struct ltt_ust_channel
*uchan
,
7380 struct consumer_output
*consumer
,
7382 uint64_t *discarded
,
7386 struct lttng_ht_iter iter
;
7387 struct lttng_ht_node_str
*ua_chan_node
;
7388 struct ust_app
*app
;
7389 struct ust_app_session
*ua_sess
;
7390 struct ust_app_channel
*ua_chan
;
7396 * Iterate over every registered applications. Sum counters for
7397 * all applications containing requested session and channel.
7399 lttng::urcu::read_lock_guard read_lock
;
7401 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7402 struct lttng_ht_iter uiter
;
7404 ua_sess
= ust_app_lookup_app_session(usess
, app
);
7405 if (ua_sess
== nullptr) {
7410 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7411 ua_chan_node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&uiter
);
7412 /* If the session is found for the app, the channel must be there */
7413 LTTNG_ASSERT(ua_chan_node
);
7415 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7420 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
, consumer
, &_lost
);
7426 uint64_t _discarded
;
7428 ret
= consumer_get_discarded_events(
7429 usess
->id
, ua_chan
->key
, consumer
, &_discarded
);
7433 (*discarded
) += _discarded
;
7440 static int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7443 struct ust_app_session
*ua_sess
;
7445 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7447 lttng::urcu::read_lock_guard read_lock
;
7448 const auto update_health_code_on_exit
=
7449 lttng::make_scope_exit([]() noexcept
{ health_code_update(); });
7451 ua_sess
= ust_app_lookup_app_session(usess
, app
);
7452 if (ua_sess
== nullptr) {
7453 /* The session is in teardown process. Ignore and continue. */
7457 const auto locked_ua_sess
= ua_sess
->lock();
7458 if (locked_ua_sess
->deleted
) {
7462 pthread_mutex_lock(&app
->sock_lock
);
7463 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7464 pthread_mutex_unlock(&app
->sock_lock
);
7469 * Regenerate the statedump for each app in the session.
7471 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7474 struct lttng_ht_iter iter
;
7475 struct ust_app
*app
;
7477 DBG("Regenerating the metadata for all UST apps");
7479 lttng::urcu::read_lock_guard read_lock
;
7481 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7482 if (!app
->compatible
) {
7486 ret
= ust_app_regenerate_statedump(usess
, app
);
7488 /* Continue to the next app even on error */
7497 * Rotate all the channels of a session.
7499 * Return LTTNG_OK on success or else an LTTng error code.
7501 enum lttng_error_code
ust_app_rotate_session(const ltt_session::locked_ref
& session
)
7504 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7505 struct lttng_ht_iter iter
;
7506 struct ltt_ust_session
*usess
= session
->ust_session
;
7508 LTTNG_ASSERT(usess
);
7510 switch (usess
->buffer_type
) {
7511 case LTTNG_BUFFER_PER_UID
:
7513 struct buffer_reg_uid
*reg
;
7515 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7516 struct buffer_reg_channel
*buf_reg_chan
;
7517 struct consumer_socket
*socket
;
7518 lttng::urcu::read_lock_guard read_lock
;
7520 /* Get consumer socket to use to push the metadata.*/
7521 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7524 cmd_ret
= LTTNG_ERR_INVALID
;
7528 /* Rotate the data channels. */
7529 cds_lfht_for_each_entry (
7530 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7531 ret
= consumer_rotate_channel(socket
,
7532 buf_reg_chan
->consumer_key
,
7534 /* is_metadata_channel */ false);
7536 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7542 * The metadata channel might not be present.
7544 * Consumer stream allocation can be done
7545 * asynchronously and can fail on intermediary
7546 * operations (i.e add context) and lead to data
7547 * channels created with no metadata channel.
7549 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7550 /* Skip since no metadata is present. */
7555 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7556 (void) push_metadata(locked_registry
, usess
->consumer
);
7559 ret
= consumer_rotate_channel(socket
,
7560 reg
->registry
->reg
.ust
->_metadata_key
,
7562 /* is_metadata_channel */ true);
7564 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7570 case LTTNG_BUFFER_PER_PID
:
7572 lttng::urcu::read_lock_guard read_lock
;
7575 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, raw_app
, pid_n
.node
) {
7576 struct consumer_socket
*socket
;
7577 struct lttng_ht_iter chan_iter
;
7578 struct ust_app_channel
*ua_chan
;
7579 struct ust_app_session
*ua_sess
;
7580 lsu::registry_session
*registry
;
7581 bool app_reference_taken
;
7583 app_reference_taken
= ust_app_get(*raw_app
);
7584 if (!app_reference_taken
) {
7585 /* Application unregistered concurrently, skip it. */
7586 DBG("Could not get application reference as it is being torn down; skipping application");
7590 ust_app_reference
app(raw_app
);
7593 ua_sess
= ust_app_lookup_app_session(usess
, app
.get());
7595 /* Session not associated with this app. */
7599 /* Get the right consumer socket for the application. */
7600 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7603 cmd_ret
= LTTNG_ERR_INVALID
;
7607 registry
= ust_app_get_session_registry(ua_sess
->get_identifier());
7608 LTTNG_ASSERT(registry
);
7610 /* Rotate the data channels. */
7611 cds_lfht_for_each_entry (
7612 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7613 ret
= consumer_rotate_channel(socket
,
7616 /* is_metadata_channel */ false);
7618 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7623 /* Rotate the metadata channel. */
7625 auto locked_registry
= registry
->lock();
7627 (void) push_metadata(locked_registry
, usess
->consumer
);
7630 ret
= consumer_rotate_channel(socket
,
7631 registry
->_metadata_key
,
7633 /* is_metadata_channel */ true);
7635 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7653 enum lttng_error_code
ust_app_create_channel_subdirectories(const struct ltt_ust_session
*usess
)
7655 enum lttng_error_code ret
= LTTNG_OK
;
7656 struct lttng_ht_iter iter
;
7657 enum lttng_trace_chunk_status chunk_status
;
7658 char *pathname_index
;
7661 LTTNG_ASSERT(usess
->current_trace_chunk
);
7663 switch (usess
->buffer_type
) {
7664 case LTTNG_BUFFER_PER_UID
:
7666 struct buffer_reg_uid
*reg
;
7667 lttng::urcu::read_lock_guard read_lock
;
7669 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7670 fmt_ret
= asprintf(&pathname_index
,
7671 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
7672 "/" DEFAULT_INDEX_DIR
,
7674 reg
->bits_per_long
);
7676 ERR("Failed to format channel index directory");
7677 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7682 * Create the index subdirectory which will take care
7683 * of implicitly creating the channel's path.
7685 chunk_status
= lttng_trace_chunk_create_subdirectory(
7686 usess
->current_trace_chunk
, pathname_index
);
7687 free(pathname_index
);
7688 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7689 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7695 case LTTNG_BUFFER_PER_PID
:
7697 struct ust_app
*app
;
7698 lttng::urcu::read_lock_guard read_lock
;
7701 * Create the toplevel ust/ directory in case no apps are running.
7703 chunk_status
= lttng_trace_chunk_create_subdirectory(usess
->current_trace_chunk
,
7704 DEFAULT_UST_TRACE_DIR
);
7705 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7706 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7710 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7711 struct ust_app_session
*ua_sess
;
7712 lsu::registry_session
*registry
;
7714 ua_sess
= ust_app_lookup_app_session(usess
, app
);
7716 /* Session not associated with this app. */
7720 registry
= ust_app_get_session_registry(ua_sess
->get_identifier());
7722 DBG("Application session is being torn down. Skip application.");
7726 fmt_ret
= asprintf(&pathname_index
,
7727 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7730 ERR("Failed to format channel index directory");
7731 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7735 * Create the index subdirectory which will take care
7736 * of implicitly creating the channel's path.
7738 chunk_status
= lttng_trace_chunk_create_subdirectory(
7739 usess
->current_trace_chunk
, pathname_index
);
7740 free(pathname_index
);
7741 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7742 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7758 * Clear all the channels of a session.
7760 * Return LTTNG_OK on success or else an LTTng error code.
7762 enum lttng_error_code
ust_app_clear_session(const ltt_session::locked_ref
& session
)
7765 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7766 struct lttng_ht_iter iter
;
7767 struct ust_app
*app
;
7768 struct ltt_ust_session
*usess
= session
->ust_session
;
7770 LTTNG_ASSERT(usess
);
7772 if (usess
->active
) {
7773 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7774 cmd_ret
= LTTNG_ERR_FATAL
;
7778 switch (usess
->buffer_type
) {
7779 case LTTNG_BUFFER_PER_UID
:
7781 struct buffer_reg_uid
*reg
;
7782 lttng::urcu::read_lock_guard read_lock
;
7784 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7785 struct buffer_reg_channel
*buf_reg_chan
;
7786 struct consumer_socket
*socket
;
7788 /* Get consumer socket to use to push the metadata.*/
7789 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7792 cmd_ret
= LTTNG_ERR_INVALID
;
7796 /* Clear the data channels. */
7797 cds_lfht_for_each_entry (
7798 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7799 ret
= consumer_clear_channel(socket
, buf_reg_chan
->consumer_key
);
7806 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7807 (void) push_metadata(locked_registry
, usess
->consumer
);
7811 * Clear the metadata channel.
7812 * Metadata channel is not cleared per se but we still need to
7813 * perform a rotation operation on it behind the scene.
7815 ret
= consumer_clear_channel(socket
, reg
->registry
->reg
.ust
->_metadata_key
);
7822 case LTTNG_BUFFER_PER_PID
:
7824 lttng::urcu::read_lock_guard read_lock
;
7826 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7827 struct consumer_socket
*socket
;
7828 struct lttng_ht_iter chan_iter
;
7829 struct ust_app_channel
*ua_chan
;
7830 struct ust_app_session
*ua_sess
;
7831 lsu::registry_session
*registry
;
7833 ua_sess
= ust_app_lookup_app_session(usess
, app
);
7835 /* Session not associated with this app. */
7839 /* Get the right consumer socket for the application. */
7840 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7843 cmd_ret
= LTTNG_ERR_INVALID
;
7847 registry
= ust_app_get_session_registry(ua_sess
->get_identifier());
7849 DBG("Application session is being torn down. Skip application.");
7853 /* Clear the data channels. */
7854 cds_lfht_for_each_entry (
7855 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7856 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7858 /* Per-PID buffer and application going away. */
7859 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7867 auto locked_registry
= registry
->lock();
7868 (void) push_metadata(locked_registry
, usess
->consumer
);
7872 * Clear the metadata channel.
7873 * Metadata channel is not cleared per se but we still need to
7874 * perform rotation operation on it behind the scene.
7876 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7878 /* Per-PID buffer and application going away. */
7879 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7897 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7898 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7901 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7910 * This function skips the metadata channel as the begin/end timestamps of a
7911 * metadata packet are useless.
7913 * Moreover, opening a packet after a "clear" will cause problems for live
7914 * sessions as it will introduce padding that was not part of the first trace
7915 * chunk. The relay daemon expects the content of the metadata stream of
7916 * successive metadata trace chunks to be strict supersets of one another.
7918 * For example, flushing a packet at the beginning of the metadata stream of
7919 * a trace chunk resulting from a "clear" session command will cause the
7920 * size of the metadata stream of the new trace chunk to not match the size of
7921 * the metadata stream of the original chunk. This will confuse the relay
7922 * daemon as the same "offset" in a metadata stream will no longer point
7923 * to the same content.
7925 enum lttng_error_code
ust_app_open_packets(const ltt_session::locked_ref
& session
)
7927 enum lttng_error_code ret
= LTTNG_OK
;
7928 struct lttng_ht_iter iter
;
7929 struct ltt_ust_session
*usess
= session
->ust_session
;
7931 LTTNG_ASSERT(usess
);
7933 switch (usess
->buffer_type
) {
7934 case LTTNG_BUFFER_PER_UID
:
7936 struct buffer_reg_uid
*reg
;
7938 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7939 struct buffer_reg_channel
*buf_reg_chan
;
7940 struct consumer_socket
*socket
;
7941 lttng::urcu::read_lock_guard read_lock
;
7943 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7946 ret
= LTTNG_ERR_FATAL
;
7950 cds_lfht_for_each_entry (
7951 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7952 const int open_ret
= consumer_open_channel_packets(
7953 socket
, buf_reg_chan
->consumer_key
);
7956 ret
= LTTNG_ERR_UNK
;
7963 case LTTNG_BUFFER_PER_PID
:
7965 struct ust_app
*app
;
7966 lttng::urcu::read_lock_guard read_lock
;
7968 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7969 struct consumer_socket
*socket
;
7970 struct lttng_ht_iter chan_iter
;
7971 struct ust_app_channel
*ua_chan
;
7972 struct ust_app_session
*ua_sess
;
7973 lsu::registry_session
*registry
;
7975 ua_sess
= ust_app_lookup_app_session(usess
, app
);
7977 /* Session not associated with this app. */
7981 /* Get the right consumer socket for the application. */
7982 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7985 ret
= LTTNG_ERR_FATAL
;
7989 registry
= ust_app_get_session_registry(ua_sess
->get_identifier());
7991 DBG("Application session is being torn down. Skip application.");
7995 cds_lfht_for_each_entry (
7996 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7997 const int open_ret
=
7998 consumer_open_channel_packets(socket
, ua_chan
->key
);
8002 * Per-PID buffer and application going
8005 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
8009 ret
= LTTNG_ERR_UNK
;
8025 lsu::ctl_field_quirks
ust_app::ctl_field_quirks() const
8028 * Application contexts are expressed as variants. LTTng-UST announces
8029 * those by registering an enumeration named `..._tag`. It then registers a
8030 * variant as part of the event context that contains the various possible
8033 * Unfortunately, the names used in the enumeration and variant don't
8034 * match: the enumeration names are all prefixed with an underscore while
8035 * the variant type tag fields aren't.
8037 * While the CTF 1.8.3 specification mentions that
8038 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8039 * 1.x (and possibly others) expect a perfect match between the names used
8040 * by tags and variants.
8042 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8043 * the variant's fields are modified to match the mappings of its tag.
8045 * From ABI version >= 10.x, the variant fields and tag mapping names
8046 * correctly match, making this quirk unnecessary.
8048 return v_major
<= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
:
8049 lsu::ctl_field_quirks::NONE
;
8052 static void ust_app_release(urcu_ref
*ref
)
8054 auto& app
= *lttng::utils::container_of(ref
, &ust_app::ref
);
8056 ust_app_unregister(app
);
8057 ust_app_destroy(app
);
8060 bool ust_app_get(ust_app
& app
)
8062 return urcu_ref_get_unless_zero(&app
.ref
);
8065 void ust_app_put(struct ust_app
*app
)
8071 urcu_ref_put(&app
->ref
, ust_app_release
);
8074 lttng_ht
*ust_app_get_all()