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-convert.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/sessiond-comm/sessiond-comm.hpp>
36 #include <common/urcu.hpp>
38 #include <lttng/condition/condition.h>
39 #include <lttng/condition/event-rule-matches-internal.hpp>
40 #include <lttng/condition/event-rule-matches.h>
41 #include <lttng/event-rule/event-rule-internal.hpp>
42 #include <lttng/event-rule/event-rule.h>
43 #include <lttng/event-rule/user-tracepoint.h>
44 #include <lttng/trigger/trigger-internal.hpp>
56 #include <sys/types.h>
58 #include <urcu/compiler.h>
61 namespace lsu
= lttng::sessiond::ust
;
62 namespace lst
= lttng::sessiond::trace
;
64 struct lttng_ht
*ust_app_ht
;
65 struct lttng_ht
*ust_app_ht_by_sock
;
66 struct lttng_ht
*ust_app_ht_by_notify_sock
;
68 static int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
70 /* Next available channel key. Access under next_channel_key_lock. */
71 static uint64_t _next_channel_key
;
72 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
74 /* Next available session ID. Access under next_session_id_lock. */
75 static uint64_t _next_session_id
;
76 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
81 * Return the session registry according to the buffer type of the given
84 * A registry per UID object MUST exists before calling this function or else
85 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
87 static lsu::registry_session
*get_session_registry(const struct ust_app_session
*ua_sess
)
89 lsu::registry_session
*registry
= nullptr;
91 LTTNG_ASSERT(ua_sess
);
93 switch (ua_sess
->buffer_type
) {
94 case LTTNG_BUFFER_PER_PID
:
96 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
100 registry
= reg_pid
->registry
->reg
.ust
;
103 case LTTNG_BUFFER_PER_UID
:
105 struct buffer_reg_uid
*reg_uid
=
106 buffer_reg_uid_find(ua_sess
->tracing_id
,
107 ua_sess
->bits_per_long
,
108 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
112 registry
= reg_uid
->registry
->reg
.ust
;
123 lsu::registry_session::locked_ptr
get_locked_session_registry(const struct ust_app_session
*ua_sess
)
125 auto session
= get_session_registry(ua_sess
);
127 pthread_mutex_lock(&session
->_lock
);
130 return lsu::registry_session::locked_ptr
{ session
};
135 * Return the incremented value of next_channel_key.
137 static uint64_t get_next_channel_key()
141 pthread_mutex_lock(&next_channel_key_lock
);
142 ret
= ++_next_channel_key
;
143 pthread_mutex_unlock(&next_channel_key_lock
);
148 * Return the atomically incremented value of next_session_id.
150 static uint64_t get_next_session_id()
154 pthread_mutex_lock(&next_session_id_lock
);
155 ret
= ++_next_session_id
;
156 pthread_mutex_unlock(&next_session_id_lock
);
160 static void copy_channel_attr_to_ustctl(struct lttng_ust_ctl_consumer_channel_attr
*attr
,
161 struct lttng_ust_abi_channel_attr
*uattr
)
163 /* Copy event attributes since the layout is different. */
164 attr
->subbuf_size
= uattr
->subbuf_size
;
165 attr
->num_subbuf
= uattr
->num_subbuf
;
166 attr
->overwrite
= uattr
->overwrite
;
167 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
168 attr
->read_timer_interval
= uattr
->read_timer_interval
;
169 attr
->output
= (lttng_ust_abi_output
) uattr
->output
;
170 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
174 * Match function for the hash table lookup.
176 * It matches an ust app event based on three attributes which are the event
177 * name, the filter bytecode and the loglevel.
179 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
181 struct ust_app_event
*event
;
182 const struct ust_app_ht_key
*key
;
187 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
188 key
= (ust_app_ht_key
*) _key
;
190 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
193 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
197 /* Event loglevel. */
198 if (!loglevels_match(event
->attr
.loglevel_type
,
199 event
->attr
.loglevel
,
202 LTTNG_UST_ABI_LOGLEVEL_ALL
)) {
206 /* One of the filters is NULL, fail. */
207 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
211 if (key
->filter
&& event
->filter
) {
212 /* Both filters exists, check length followed by the bytecode. */
213 if (event
->filter
->len
!= key
->filter
->len
||
214 memcmp(event
->filter
->data
, key
->filter
->data
, event
->filter
->len
) != 0) {
219 /* One of the exclusions is NULL, fail. */
220 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
224 if (key
->exclusion
&& event
->exclusion
) {
225 /* Both exclusions exists, check count followed by the names. */
226 if (event
->exclusion
->count
!= key
->exclusion
->count
||
227 memcmp(event
->exclusion
->names
,
228 key
->exclusion
->names
,
229 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
242 * Unique add of an ust app event in the given ht. This uses the custom
243 * ht_match_ust_app_event match function and the event name as hash.
245 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
, struct ust_app_event
*event
)
247 struct cds_lfht_node
*node_ptr
;
248 struct ust_app_ht_key key
;
251 LTTNG_ASSERT(ua_chan
);
252 LTTNG_ASSERT(ua_chan
->events
);
255 ht
= ua_chan
->events
;
256 key
.name
= event
->attr
.name
;
257 key
.filter
= event
->filter
;
258 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel_type
;
259 key
.loglevel_value
= event
->attr
.loglevel
;
260 key
.exclusion
= event
->exclusion
;
262 node_ptr
= cds_lfht_add_unique(ht
->ht
,
263 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
264 ht_match_ust_app_event
,
267 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
271 * Close the notify socket from the given RCU head object. This MUST be called
272 * through a call_rcu().
274 static void close_notify_sock_rcu(struct rcu_head
*head
)
277 struct ust_app_notify_sock_obj
*obj
=
278 lttng::utils::container_of(head
, &ust_app_notify_sock_obj::head
);
280 /* Must have a valid fd here. */
281 LTTNG_ASSERT(obj
->fd
>= 0);
283 ret
= close(obj
->fd
);
285 ERR("close notify sock %d RCU", obj
->fd
);
287 lttng_fd_put(LTTNG_FD_APPS
, 1);
293 * Delete ust context safely. RCU read lock must be held before calling
296 static void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
300 LTTNG_ASSERT(ua_ctx
);
301 ASSERT_RCU_READ_LOCKED();
304 pthread_mutex_lock(&app
->sock_lock
);
305 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
306 pthread_mutex_unlock(&app
->sock_lock
);
308 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
309 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
312 } else if (ret
== -EAGAIN
) {
313 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
317 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
330 * Delete ust app event safely. RCU read lock must be held before calling
333 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
, struct ust_app
*app
)
337 LTTNG_ASSERT(ua_event
);
338 ASSERT_RCU_READ_LOCKED();
340 free(ua_event
->filter
);
341 if (ua_event
->exclusion
!= nullptr)
342 free(ua_event
->exclusion
);
343 if (ua_event
->obj
!= nullptr) {
344 pthread_mutex_lock(&app
->sock_lock
);
345 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
346 pthread_mutex_unlock(&app
->sock_lock
);
348 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
349 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
352 } else if (ret
== -EAGAIN
) {
353 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
357 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
369 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
370 * through a call_rcu().
372 static void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
374 struct ust_app_event_notifier_rule
*obj
=
375 lttng::utils::container_of(head
, &ust_app_event_notifier_rule::rcu_head
);
381 * Delete ust app event notifier rule safely.
383 static void delete_ust_app_event_notifier_rule(
384 int sock
, struct ust_app_event_notifier_rule
*ua_event_notifier_rule
, struct ust_app
*app
)
388 LTTNG_ASSERT(ua_event_notifier_rule
);
390 if (ua_event_notifier_rule
->exclusion
!= nullptr) {
391 free(ua_event_notifier_rule
->exclusion
);
394 if (ua_event_notifier_rule
->obj
!= nullptr) {
395 pthread_mutex_lock(&app
->sock_lock
);
396 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
397 pthread_mutex_unlock(&app
->sock_lock
);
399 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
400 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
403 } else if (ret
== -EAGAIN
) {
404 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
408 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
415 free(ua_event_notifier_rule
->obj
);
418 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
419 call_rcu(&ua_event_notifier_rule
->rcu_head
, free_ust_app_event_notifier_rule_rcu
);
423 * Release ust data object of the given stream.
425 * Return 0 on success or else a negative value.
427 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
431 LTTNG_ASSERT(stream
);
434 pthread_mutex_lock(&app
->sock_lock
);
435 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
436 pthread_mutex_unlock(&app
->sock_lock
);
438 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
439 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
442 } else if (ret
== -EAGAIN
) {
443 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
447 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
453 lttng_fd_put(LTTNG_FD_APPS
, 2);
461 * Delete ust app stream safely. RCU read lock must be held before calling
464 static void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
466 LTTNG_ASSERT(stream
);
467 ASSERT_RCU_READ_LOCKED();
469 (void) release_ust_app_stream(sock
, stream
, app
);
473 static void delete_ust_app_channel_rcu(struct rcu_head
*head
)
475 struct ust_app_channel
*ua_chan
=
476 lttng::utils::container_of(head
, &ust_app_channel::rcu_head
);
478 lttng_ht_destroy(ua_chan
->ctx
);
479 lttng_ht_destroy(ua_chan
->events
);
484 * Extract the lost packet or discarded events counter when the channel is
485 * being deleted and store the value in the parent channel so we can
486 * access it from lttng list and at stop/destroy.
488 * The session list lock must be held by the caller.
490 static void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
492 uint64_t discarded
= 0, lost
= 0;
493 struct ltt_session
*session
;
494 struct ltt_ust_channel
*uchan
;
496 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
500 lttng::urcu::read_lock_guard read_lock
;
501 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
502 if (!session
|| !session
->ust_session
) {
504 * Not finding the session is not an error because there are
505 * multiple ways the channels can be torn down.
507 * 1) The session daemon can initiate the destruction of the
508 * ust app session after receiving a destroy command or
509 * during its shutdown/teardown.
510 * 2) The application, since we are in per-pid tracing, is
511 * unregistering and tearing down its ust app session.
513 * Both paths are protected by the session list lock which
514 * ensures that the accounting of lost packets and discarded
515 * events is done exactly once. The session is then unpublished
516 * from the session list, resulting in this condition.
521 if (ua_chan
->attr
.overwrite
) {
522 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
524 session
->ust_session
->consumer
,
527 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
529 session
->ust_session
->consumer
,
532 uchan
= trace_ust_find_channel_by_name(session
->ust_session
->domain_global
.channels
,
535 ERR("Missing UST channel to store discarded counters");
539 uchan
->per_pid_closed_app_discarded
+= discarded
;
540 uchan
->per_pid_closed_app_lost
+= lost
;
544 session_put(session
);
549 * Delete ust app channel safely. RCU read lock must be held before calling
552 * The session list lock must be held by the caller.
554 static void delete_ust_app_channel(int sock
,
555 struct ust_app_channel
*ua_chan
,
557 const lsu::registry_session::locked_ptr
& locked_registry
)
560 struct lttng_ht_iter iter
;
561 struct ust_app_event
*ua_event
;
562 struct ust_app_ctx
*ua_ctx
;
563 struct ust_app_stream
*stream
, *stmp
;
565 LTTNG_ASSERT(ua_chan
);
566 ASSERT_RCU_READ_LOCKED();
568 DBG3("UST app deleting channel %s", ua_chan
->name
);
571 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
572 cds_list_del(&stream
->list
);
573 delete_ust_app_stream(sock
, stream
, app
);
577 cds_lfht_for_each_entry (ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
578 cds_list_del(&ua_ctx
->list
);
579 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
581 delete_ust_app_ctx(sock
, ua_ctx
, app
);
585 cds_lfht_for_each_entry (ua_chan
->events
->ht
, &iter
.iter
, ua_event
, node
.node
) {
586 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
588 delete_ust_app_event(sock
, ua_event
, app
);
591 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
592 /* Wipe and free registry from session registry. */
593 if (locked_registry
) {
595 locked_registry
->remove_channel(ua_chan
->key
, sock
>= 0);
596 } catch (const std::exception
& ex
) {
597 DBG("Could not find channel for removal: %s", ex
.what());
602 * A negative socket can be used by the caller when
603 * cleaning-up a ua_chan in an error path. Skip the
604 * accounting in this case.
607 save_per_pid_lost_discarded_counters(ua_chan
);
611 if (ua_chan
->obj
!= nullptr) {
612 /* Remove channel from application UST object descriptor. */
613 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
614 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
616 pthread_mutex_lock(&app
->sock_lock
);
617 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
618 pthread_mutex_unlock(&app
->sock_lock
);
620 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
621 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
625 } else if (ret
== -EAGAIN
) {
626 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
631 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
638 lttng_fd_put(LTTNG_FD_APPS
, 1);
641 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
644 int ust_app_register_done(struct ust_app
*app
)
648 pthread_mutex_lock(&app
->sock_lock
);
649 ret
= lttng_ust_ctl_register_done(app
->sock
);
650 pthread_mutex_unlock(&app
->sock_lock
);
654 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
659 pthread_mutex_lock(&app
->sock_lock
);
664 ret
= lttng_ust_ctl_release_object(sock
, data
);
666 pthread_mutex_unlock(&app
->sock_lock
);
672 * Push metadata to consumer socket.
674 * RCU read-side lock must be held to guarantee existence of socket.
675 * Must be called with the ust app session lock held.
676 * Must be called with the registry lock held.
678 * On success, return the len of metadata pushed or else a negative value.
679 * Returning a -EPIPE return value means we could not send the metadata,
680 * but it can be caused by recoverable errors (e.g. the application has
681 * terminated concurrently).
683 ssize_t
ust_app_push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
684 struct consumer_socket
*socket
,
688 char *metadata_str
= nullptr;
689 size_t len
, offset
, new_metadata_len_sent
;
691 uint64_t metadata_key
, metadata_version
;
693 LTTNG_ASSERT(locked_registry
);
694 LTTNG_ASSERT(socket
);
695 ASSERT_RCU_READ_LOCKED();
697 metadata_key
= locked_registry
->_metadata_key
;
700 * Means that no metadata was assigned to the session. This can
701 * happens if no start has been done previously.
707 offset
= locked_registry
->_metadata_len_sent
;
708 len
= locked_registry
->_metadata_len
- locked_registry
->_metadata_len_sent
;
709 new_metadata_len_sent
= locked_registry
->_metadata_len
;
710 metadata_version
= locked_registry
->_metadata_version
;
712 DBG3("No metadata to push for metadata key %" PRIu64
,
713 locked_registry
->_metadata_key
);
715 if (send_zero_data
) {
716 DBG("No metadata to push");
722 /* Allocate only what we have to send. */
723 metadata_str
= calloc
<char>(len
);
725 PERROR("zmalloc ust app metadata string");
729 /* Copy what we haven't sent out. */
730 memcpy(metadata_str
, locked_registry
->_metadata
+ offset
, len
);
733 pthread_mutex_unlock(&locked_registry
->_lock
);
735 * We need to unlock the registry while we push metadata to
736 * break a circular dependency between the consumerd metadata
737 * lock and the sessiond registry lock. Indeed, pushing metadata
738 * to the consumerd awaits that it gets pushed all the way to
739 * relayd, but doing so requires grabbing the metadata lock. If
740 * a concurrent metadata request is being performed by
741 * consumerd, this can try to grab the registry lock on the
742 * sessiond while holding the metadata lock on the consumer
743 * daemon. Those push and pull schemes are performed on two
744 * different bidirectionnal communication sockets.
746 ret
= consumer_push_metadata(
747 socket
, metadata_key
, metadata_str
, len
, offset
, metadata_version
);
748 pthread_mutex_lock(&locked_registry
->_lock
);
751 * There is an acceptable race here between the registry
752 * metadata key assignment and the creation on the
753 * consumer. The session daemon can concurrently push
754 * metadata for this registry while being created on the
755 * consumer since the metadata key of the registry is
756 * assigned *before* it is setup to avoid the consumer
757 * to ask for metadata that could possibly be not found
758 * in the session daemon.
760 * The metadata will get pushed either by the session
761 * being stopped or the consumer requesting metadata if
762 * that race is triggered.
764 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
767 ERR("Error pushing metadata to consumer");
773 * Metadata may have been concurrently pushed, since
774 * we're not holding the registry lock while pushing to
775 * consumer. This is handled by the fact that we send
776 * the metadata content, size, and the offset at which
777 * that metadata belongs. This may arrive out of order
778 * on the consumer side, and the consumer is able to
779 * deal with overlapping fragments. The consumer
780 * supports overlapping fragments, which must be
781 * contiguous starting from offset 0. We keep the
782 * largest metadata_len_sent value of the concurrent
785 locked_registry
->_metadata_len_sent
=
786 std::max(locked_registry
->_metadata_len_sent
, new_metadata_len_sent
);
795 * On error, flag the registry that the metadata is
796 * closed. We were unable to push anything and this
797 * means that either the consumer is not responding or
798 * the metadata cache has been destroyed on the
801 locked_registry
->_metadata_closed
= true;
809 * For a given application and session, push metadata to consumer.
810 * Either sock or consumer is required : if sock is NULL, the default
811 * socket to send the metadata is retrieved from consumer, if sock
812 * is not NULL we use it to send the metadata.
813 * RCU read-side lock must be held while calling this function,
814 * therefore ensuring existence of registry. It also ensures existence
815 * of socket throughout this function.
817 * Return 0 on success else a negative error.
818 * Returning a -EPIPE return value means we could not send the metadata,
819 * but it can be caused by recoverable errors (e.g. the application has
820 * terminated concurrently).
822 static int push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
823 struct consumer_output
*consumer
)
827 struct consumer_socket
*socket
;
829 LTTNG_ASSERT(locked_registry
);
830 LTTNG_ASSERT(consumer
);
831 ASSERT_RCU_READ_LOCKED();
833 if (locked_registry
->_metadata_closed
) {
838 /* Get consumer socket to use to push the metadata.*/
839 socket
= consumer_find_socket_by_bitness(locked_registry
->abi
.bits_per_long
, consumer
);
845 ret
= ust_app_push_metadata(locked_registry
, socket
, 0);
857 * Send to the consumer a close metadata command for the given session. Once
858 * done, the metadata channel is deleted and the session metadata pointer is
859 * nullified. The session lock MUST be held unless the application is
860 * in the destroy path.
862 * Do not hold the registry lock while communicating with the consumerd, because
863 * doing so causes inter-process deadlocks between consumerd and sessiond with
864 * the metadata request notification.
866 * Return 0 on success else a negative value.
868 static int close_metadata(uint64_t metadata_key
,
869 unsigned int consumer_bitness
,
870 struct consumer_output
*consumer
)
873 struct consumer_socket
*socket
;
874 lttng::urcu::read_lock_guard read_lock_guard
;
876 LTTNG_ASSERT(consumer
);
878 /* Get consumer socket to use to push the metadata. */
879 socket
= consumer_find_socket_by_bitness(consumer_bitness
, consumer
);
885 ret
= consumer_close_metadata(socket
, metadata_key
);
894 static void delete_ust_app_session_rcu(struct rcu_head
*head
)
896 struct ust_app_session
*ua_sess
=
897 lttng::utils::container_of(head
, &ust_app_session::rcu_head
);
899 lttng_ht_destroy(ua_sess
->channels
);
904 * Delete ust app session safely. RCU read lock must be held before calling
907 * The session list lock must be held by the caller.
909 static void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
, struct ust_app
*app
)
912 struct lttng_ht_iter iter
;
913 struct ust_app_channel
*ua_chan
;
915 LTTNG_ASSERT(ua_sess
);
916 ASSERT_RCU_READ_LOCKED();
918 pthread_mutex_lock(&ua_sess
->lock
);
920 LTTNG_ASSERT(!ua_sess
->deleted
);
921 ua_sess
->deleted
= true;
923 auto locked_registry
= get_locked_session_registry(ua_sess
);
924 /* Registry can be null on error path during initialization. */
925 if (locked_registry
) {
926 /* Push metadata for application before freeing the application. */
927 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
930 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
931 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
933 delete_ust_app_channel(sock
, ua_chan
, app
, locked_registry
);
936 if (locked_registry
) {
938 * Don't ask to close metadata for global per UID buffers. Close
939 * metadata only on destroy trace session in this case. Also, the
940 * previous push metadata could have flag the metadata registry to
941 * close so don't send a close command if closed.
943 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
944 const auto metadata_key
= locked_registry
->_metadata_key
;
945 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
947 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
948 locked_registry
->_metadata_closed
= true;
951 /* Release lock before communication, see comments in close_metadata(). */
952 locked_registry
.reset();
953 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
957 /* In case of per PID, the registry is kept in the session. */
958 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
959 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
962 * Registry can be null on error path during
965 buffer_reg_pid_remove(reg_pid
);
966 buffer_reg_pid_destroy(reg_pid
);
970 if (ua_sess
->handle
!= -1) {
971 pthread_mutex_lock(&app
->sock_lock
);
972 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
973 pthread_mutex_unlock(&app
->sock_lock
);
975 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
976 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
979 } else if (ret
== -EAGAIN
) {
980 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
984 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
991 /* Remove session from application UST object descriptor. */
992 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
993 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
997 pthread_mutex_unlock(&ua_sess
->lock
);
999 consumer_output_put(ua_sess
->consumer
);
1001 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1005 * Delete a traceable application structure from the global list. Never call
1006 * this function outside of a call_rcu call.
1008 static void delete_ust_app(struct ust_app
*app
)
1011 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1012 struct lttng_ht_iter iter
;
1013 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1014 bool event_notifier_write_fd_is_open
;
1017 * The session list lock must be held during this function to guarantee
1018 * the existence of ua_sess.
1020 session_lock_list();
1021 /* Delete ust app sessions info */
1026 cds_list_for_each_entry_safe (ua_sess
, tmp_ua_sess
, &app
->teardown_head
, teardown_node
) {
1027 /* Free every object in the session and the session. */
1028 lttng::urcu::read_lock_guard read_lock
;
1029 delete_ust_app_session(sock
, ua_sess
, app
);
1032 /* Remove the event notifier rules associated with this app. */
1034 lttng::urcu::read_lock_guard read_lock
;
1036 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1038 event_notifier_rule
,
1040 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1043 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
1047 lttng_ht_destroy(app
->sessions
);
1048 lttng_ht_destroy(app
->ust_sessions_objd
);
1049 lttng_ht_destroy(app
->ust_objd
);
1050 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1053 * This could be NULL if the event notifier setup failed (e.g the app
1054 * was killed or the tracer does not support this feature).
1056 if (app
->event_notifier_group
.object
) {
1057 enum lttng_error_code ret_code
;
1058 enum event_notifier_error_accounting_status status
;
1060 const int event_notifier_read_fd
=
1061 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
);
1063 ret_code
= notification_thread_command_remove_tracer_event_source(
1064 the_notification_thread_handle
, event_notifier_read_fd
);
1065 if (ret_code
!= LTTNG_OK
) {
1066 ERR("Failed to remove application tracer event source from notification thread");
1069 status
= event_notifier_error_accounting_unregister_app(app
);
1070 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1071 ERR("Error unregistering app from event notifier error accounting");
1074 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1075 free(app
->event_notifier_group
.object
);
1078 event_notifier_write_fd_is_open
=
1079 lttng_pipe_is_write_open(app
->event_notifier_group
.event_pipe
);
1080 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1082 * Release the file descriptors reserved for the event notifier pipe.
1083 * The app could be destroyed before the write end of the pipe could be
1084 * passed to the application (and closed). In that case, both file
1085 * descriptors must be released.
1087 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1090 * Wait until we have deleted the application from the sock hash table
1091 * before closing this socket, otherwise an application could re-use the
1092 * socket ID and race with the teardown, using the same hash table entry.
1094 * It's OK to leave the close in call_rcu. We want it to stay unique for
1095 * all RCU readers that could run concurrently with unregister app,
1096 * therefore we _need_ to only close that socket after a grace period. So
1097 * it should stay in this RCU callback.
1099 * This close() is a very important step of the synchronization model so
1100 * every modification to this function must be carefully reviewed.
1106 lttng_fd_put(LTTNG_FD_APPS
, 1);
1108 DBG2("UST app pid %d deleted", app
->pid
);
1110 session_unlock_list();
1114 * URCU intermediate call to delete an UST app.
1116 static void delete_ust_app_rcu(struct rcu_head
*head
)
1118 struct lttng_ht_node_ulong
*node
=
1119 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1120 struct ust_app
*app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
1122 DBG3("Call RCU deleting app PID %d", app
->pid
);
1123 delete_ust_app(app
);
1127 * Delete the session from the application ht and delete the data structure by
1128 * freeing every object inside and releasing them.
1130 * The session list lock must be held by the caller.
1132 static void destroy_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
1135 struct lttng_ht_iter iter
;
1138 LTTNG_ASSERT(ua_sess
);
1140 iter
.iter
.node
= &ua_sess
->node
.node
;
1141 ret
= lttng_ht_del(app
->sessions
, &iter
);
1143 /* Already scheduled for teardown. */
1147 /* Once deleted, free the data structure. */
1148 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1155 * Alloc new UST app session.
1157 static struct ust_app_session
*alloc_ust_app_session()
1159 struct ust_app_session
*ua_sess
;
1161 /* Init most of the default value by allocating and zeroing */
1162 ua_sess
= zmalloc
<ust_app_session
>();
1163 if (ua_sess
== nullptr) {
1168 ua_sess
->handle
= -1;
1169 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1170 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1171 pthread_mutex_init(&ua_sess
->lock
, nullptr);
1180 * Alloc new UST app channel.
1182 static struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1183 struct ust_app_session
*ua_sess
,
1184 struct lttng_ust_abi_channel_attr
*attr
)
1186 struct ust_app_channel
*ua_chan
;
1188 /* Init most of the default value by allocating and zeroing */
1189 ua_chan
= zmalloc
<ust_app_channel
>();
1190 if (ua_chan
== nullptr) {
1195 /* Setup channel name */
1196 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1197 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1199 ua_chan
->enabled
= true;
1200 ua_chan
->handle
= -1;
1201 ua_chan
->session
= ua_sess
;
1202 ua_chan
->key
= get_next_channel_key();
1203 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1204 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1205 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1207 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1208 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1210 /* Copy attributes */
1212 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1213 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1214 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1215 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1216 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1217 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1218 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1219 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1221 /* By default, the channel is a per cpu channel. */
1222 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1224 DBG3("UST app channel %s allocated", ua_chan
->name
);
1233 * Allocate and initialize a UST app stream.
1235 * Return newly allocated stream pointer or NULL on error.
1237 struct ust_app_stream
*ust_app_alloc_stream()
1239 struct ust_app_stream
*stream
= nullptr;
1241 stream
= zmalloc
<ust_app_stream
>();
1242 if (stream
== nullptr) {
1243 PERROR("zmalloc ust app stream");
1247 /* Zero could be a valid value for a handle so flag it to -1. */
1248 stream
->handle
= -1;
1255 * Alloc new UST app event.
1257 static struct ust_app_event
*alloc_ust_app_event(char *name
, struct lttng_ust_abi_event
*attr
)
1259 struct ust_app_event
*ua_event
;
1261 /* Init most of the default value by allocating and zeroing */
1262 ua_event
= zmalloc
<ust_app_event
>();
1263 if (ua_event
== nullptr) {
1264 PERROR("Failed to allocate ust_app_event structure");
1268 ua_event
->enabled
= true;
1269 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1270 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1271 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1273 /* Copy attributes */
1275 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1278 DBG3("UST app event %s allocated", ua_event
->name
);
1287 * Allocate a new UST app event notifier rule.
1289 static struct ust_app_event_notifier_rule
*
1290 alloc_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
)
1292 enum lttng_event_rule_generate_exclusions_status generate_exclusion_status
;
1293 enum lttng_condition_status cond_status
;
1294 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1295 struct lttng_condition
*condition
= nullptr;
1296 const struct lttng_event_rule
*event_rule
= nullptr;
1298 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1299 if (ua_event_notifier_rule
== nullptr) {
1300 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1304 ua_event_notifier_rule
->enabled
= true;
1305 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1306 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, ua_event_notifier_rule
->token
);
1308 condition
= lttng_trigger_get_condition(trigger
);
1309 LTTNG_ASSERT(condition
);
1310 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1311 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1313 cond_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
1314 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1315 LTTNG_ASSERT(event_rule
);
1317 ua_event_notifier_rule
->error_counter_index
=
1318 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1319 /* Acquire the event notifier's reference to the trigger. */
1320 lttng_trigger_get(trigger
);
1322 ua_event_notifier_rule
->trigger
= trigger
;
1323 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1324 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1325 event_rule
, &ua_event_notifier_rule
->exclusion
);
1326 switch (generate_exclusion_status
) {
1327 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1328 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1331 /* Error occurred. */
1332 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1333 goto error_put_trigger
;
1336 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1337 ua_event_notifier_rule
->token
);
1339 return ua_event_notifier_rule
;
1342 lttng_trigger_put(trigger
);
1344 free(ua_event_notifier_rule
);
1349 * Alloc new UST app context.
1351 static struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1353 struct ust_app_ctx
*ua_ctx
;
1355 ua_ctx
= zmalloc
<ust_app_ctx
>();
1356 if (ua_ctx
== nullptr) {
1360 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1363 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1364 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1365 char *provider_name
= nullptr, *ctx_name
= nullptr;
1367 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1368 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1369 if (!provider_name
|| !ctx_name
) {
1370 free(provider_name
);
1375 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1376 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1380 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1388 * Create a liblttng-ust filter bytecode from given bytecode.
1390 * Return allocated filter or NULL on error.
1392 static struct lttng_ust_abi_filter_bytecode
*
1393 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1395 struct lttng_ust_abi_filter_bytecode
*filter
= nullptr;
1397 /* Copy filter bytecode. */
1398 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1400 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
1406 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) == sizeof(struct lttng_ust_abi_filter_bytecode
));
1407 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1413 * Create a liblttng-ust capture bytecode from given bytecode.
1415 * Return allocated filter or NULL on error.
1417 static struct lttng_ust_abi_capture_bytecode
*
1418 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1420 struct lttng_ust_abi_capture_bytecode
*capture
= nullptr;
1422 /* Copy capture bytecode. */
1423 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1425 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
1431 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1432 sizeof(struct lttng_ust_abi_capture_bytecode
));
1433 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1439 * Find an ust_app using the sock and return it. RCU read side lock must be
1440 * held before calling this helper function.
1442 struct ust_app
*ust_app_find_by_sock(int sock
)
1444 struct lttng_ht_node_ulong
*node
;
1445 struct lttng_ht_iter iter
;
1447 ASSERT_RCU_READ_LOCKED();
1449 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
1450 node
= lttng_ht_iter_get_node_ulong(&iter
);
1451 if (node
== nullptr) {
1452 DBG2("UST app find by sock %d not found", sock
);
1456 return lttng::utils::container_of(node
, &ust_app::sock_n
);
1463 * Find an ust_app using the notify sock and return it. RCU read side lock must
1464 * be held before calling this helper function.
1466 static struct ust_app
*find_app_by_notify_sock(int sock
)
1468 struct lttng_ht_node_ulong
*node
;
1469 struct lttng_ht_iter iter
;
1471 ASSERT_RCU_READ_LOCKED();
1473 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *) ((unsigned long) sock
), &iter
);
1474 node
= lttng_ht_iter_get_node_ulong(&iter
);
1475 if (node
== nullptr) {
1476 DBG2("UST app find by notify sock %d not found", sock
);
1480 return lttng::utils::container_of(node
, &ust_app::notify_sock_n
);
1487 * Lookup for an ust app event based on event name, filter bytecode and the
1490 * Return an ust_app_event object or NULL on error.
1492 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1494 const struct lttng_bytecode
*filter
,
1495 lttng_ust_abi_loglevel_type loglevel_type
,
1497 const struct lttng_event_exclusion
*exclusion
)
1499 struct lttng_ht_iter iter
;
1500 struct lttng_ht_node_str
*node
;
1501 struct ust_app_event
*event
= nullptr;
1502 struct ust_app_ht_key key
;
1507 /* Setup key for event lookup. */
1509 key
.filter
= filter
;
1510 key
.loglevel_type
= loglevel_type
;
1511 key
.loglevel_value
= loglevel_value
;
1512 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1513 key
.exclusion
= exclusion
;
1515 /* Lookup using the event name as hash and a custom match fct. */
1516 cds_lfht_lookup(ht
->ht
,
1517 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1518 ht_match_ust_app_event
,
1521 node
= lttng_ht_iter_get_node_str(&iter
);
1522 if (node
== nullptr) {
1526 event
= lttng::utils::container_of(node
, &ust_app_event::node
);
1533 * Look-up an event notifier rule based on its token id.
1535 * Must be called with the RCU read lock held.
1536 * Return an ust_app_event_notifier_rule object or NULL on error.
1538 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(struct lttng_ht
*ht
,
1541 struct lttng_ht_iter iter
;
1542 struct lttng_ht_node_u64
*node
;
1543 struct ust_app_event_notifier_rule
*event_notifier_rule
= nullptr;
1546 ASSERT_RCU_READ_LOCKED();
1548 lttng_ht_lookup(ht
, &token
, &iter
);
1549 node
= lttng_ht_iter_get_node_u64(&iter
);
1550 if (node
== nullptr) {
1551 DBG2("UST app event notifier rule token not found: token = %" PRIu64
, token
);
1555 event_notifier_rule
= lttng::utils::container_of(node
, &ust_app_event_notifier_rule::node
);
1557 return event_notifier_rule
;
1561 * Create the channel context on the tracer.
1563 * Called with UST app session lock held.
1565 static int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1566 struct ust_app_ctx
*ua_ctx
,
1567 struct ust_app
*app
)
1571 health_code_update();
1573 pthread_mutex_lock(&app
->sock_lock
);
1574 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
, ua_chan
->obj
, &ua_ctx
->obj
);
1575 pthread_mutex_unlock(&app
->sock_lock
);
1577 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1579 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1582 } else if (ret
== -EAGAIN
) {
1584 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1588 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1596 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1598 DBG2("UST app context handle %d created successfully for channel %s",
1603 health_code_update();
1608 * Set the filter on the tracer.
1610 static int set_ust_object_filter(struct ust_app
*app
,
1611 const struct lttng_bytecode
*bytecode
,
1612 struct lttng_ust_abi_object_data
*ust_object
)
1615 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= nullptr;
1617 health_code_update();
1619 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1620 if (!ust_bytecode
) {
1621 ret
= -LTTNG_ERR_NOMEM
;
1624 pthread_mutex_lock(&app
->sock_lock
);
1625 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
, ust_object
);
1626 pthread_mutex_unlock(&app
->sock_lock
);
1628 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1630 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1633 } else if (ret
== -EAGAIN
) {
1635 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1639 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1648 DBG2("UST filter successfully set: object = %p", ust_object
);
1651 health_code_update();
1657 * Set a capture bytecode for the passed object.
1658 * The sequence number enforces the ordering at runtime and on reception of
1659 * the captured payloads.
1661 static int set_ust_capture(struct ust_app
*app
,
1662 const struct lttng_bytecode
*bytecode
,
1663 unsigned int capture_seqnum
,
1664 struct lttng_ust_abi_object_data
*ust_object
)
1667 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= nullptr;
1669 health_code_update();
1671 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1672 if (!ust_bytecode
) {
1673 ret
= -LTTNG_ERR_NOMEM
;
1678 * Set the sequence number to ensure the capture of fields is ordered.
1680 ust_bytecode
->seqnum
= capture_seqnum
;
1682 pthread_mutex_lock(&app
->sock_lock
);
1683 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
, ust_object
);
1684 pthread_mutex_unlock(&app
->sock_lock
);
1686 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1688 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1691 } else if (ret
== -EAGAIN
) {
1693 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1697 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1706 DBG2("UST capture successfully set: object = %p", ust_object
);
1709 health_code_update();
1714 static struct lttng_ust_abi_event_exclusion
*
1715 create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion
*exclusion
)
1717 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= nullptr;
1718 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1719 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1721 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1722 if (!ust_exclusion
) {
1727 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1728 sizeof(struct lttng_ust_abi_event_exclusion
));
1729 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1731 return ust_exclusion
;
1735 * Set event exclusions on the tracer.
1737 static int set_ust_object_exclusions(struct ust_app
*app
,
1738 const struct lttng_event_exclusion
*exclusions
,
1739 struct lttng_ust_abi_object_data
*ust_object
)
1742 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= nullptr;
1744 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1746 health_code_update();
1748 ust_exclusions
= create_ust_exclusion_from_exclusion(exclusions
);
1749 if (!ust_exclusions
) {
1750 ret
= -LTTNG_ERR_NOMEM
;
1753 pthread_mutex_lock(&app
->sock_lock
);
1754 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1755 pthread_mutex_unlock(&app
->sock_lock
);
1757 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1759 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1762 } else if (ret
== -EAGAIN
) {
1764 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1768 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1777 DBG2("UST exclusions set successfully for object %p", ust_object
);
1780 health_code_update();
1781 free(ust_exclusions
);
1786 * Disable the specified event on to UST tracer for the UST session.
1788 static int disable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*object
)
1792 health_code_update();
1794 pthread_mutex_lock(&app
->sock_lock
);
1795 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1796 pthread_mutex_unlock(&app
->sock_lock
);
1798 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1800 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1803 } else if (ret
== -EAGAIN
) {
1805 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1809 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1818 DBG2("UST app object %p disabled successfully for app: pid = %d", object
, app
->pid
);
1821 health_code_update();
1826 * Disable the specified channel on to UST tracer for the UST session.
1828 static int disable_ust_channel(struct ust_app
*app
,
1829 struct ust_app_session
*ua_sess
,
1830 struct ust_app_channel
*ua_chan
)
1834 health_code_update();
1836 pthread_mutex_lock(&app
->sock_lock
);
1837 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1838 pthread_mutex_unlock(&app
->sock_lock
);
1840 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1842 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1845 } else if (ret
== -EAGAIN
) {
1847 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1851 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1861 DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1864 health_code_update();
1869 * Enable the specified channel on to UST tracer for the UST session.
1871 static int enable_ust_channel(struct ust_app
*app
,
1872 struct ust_app_session
*ua_sess
,
1873 struct ust_app_channel
*ua_chan
)
1877 health_code_update();
1879 pthread_mutex_lock(&app
->sock_lock
);
1880 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1881 pthread_mutex_unlock(&app
->sock_lock
);
1883 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1885 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1889 } else if (ret
== -EAGAIN
) {
1891 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1896 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1906 ua_chan
->enabled
= true;
1908 DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1911 health_code_update();
1916 * Enable the specified event on to UST tracer for the UST session.
1918 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1922 health_code_update();
1924 pthread_mutex_lock(&app
->sock_lock
);
1925 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1926 pthread_mutex_unlock(&app
->sock_lock
);
1928 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1930 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1933 } else if (ret
== -EAGAIN
) {
1935 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1939 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1948 DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object
, app
->pid
);
1951 health_code_update();
1956 * Send channel and stream buffer to application.
1958 * Return 0 on success. On error, a negative value is returned.
1960 static int send_channel_pid_to_ust(struct ust_app
*app
,
1961 struct ust_app_session
*ua_sess
,
1962 struct ust_app_channel
*ua_chan
)
1965 struct ust_app_stream
*stream
, *stmp
;
1968 LTTNG_ASSERT(ua_sess
);
1969 LTTNG_ASSERT(ua_chan
);
1971 health_code_update();
1973 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
, app
->sock
);
1975 /* Send channel to the application. */
1976 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1977 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1978 ret
= -ENOTCONN
; /* Caused by app exiting. */
1980 } else if (ret
== -EAGAIN
) {
1981 /* Caused by timeout. */
1982 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
1986 ua_sess
->tracing_id
);
1987 /* Treat this the same way as an application that is exiting. */
1990 } else if (ret
< 0) {
1994 health_code_update();
1996 /* Send all streams to application. */
1997 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1998 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1999 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2000 ret
= -ENOTCONN
; /* Caused by app exiting. */
2002 } else if (ret
== -EAGAIN
) {
2003 /* Caused by timeout. */
2004 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
2009 ua_sess
->tracing_id
);
2011 * Treat this the same way as an application that is
2015 } else if (ret
< 0) {
2018 /* We don't need the stream anymore once sent to the tracer. */
2019 cds_list_del(&stream
->list
);
2020 delete_ust_app_stream(-1, stream
, app
);
2024 health_code_update();
2029 * Create the specified event onto the UST tracer for a UST session.
2031 * Should be called with session mutex held.
2033 static int create_ust_event(struct ust_app
*app
,
2034 struct ust_app_channel
*ua_chan
,
2035 struct ust_app_event
*ua_event
)
2039 health_code_update();
2041 /* Create UST event on tracer */
2042 pthread_mutex_lock(&app
->sock_lock
);
2043 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
, &ua_event
->obj
);
2044 pthread_mutex_unlock(&app
->sock_lock
);
2046 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2048 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2051 } else if (ret
== -EAGAIN
) {
2053 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2057 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2058 ua_event
->attr
.name
,
2066 ua_event
->handle
= ua_event
->obj
->handle
;
2068 DBG2("UST app event %s created successfully for pid:%d object = %p",
2069 ua_event
->attr
.name
,
2073 health_code_update();
2075 /* Set filter if one is present. */
2076 if (ua_event
->filter
) {
2077 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2083 /* Set exclusions for the event */
2084 if (ua_event
->exclusion
) {
2085 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2091 /* If event not enabled, disable it on the tracer */
2092 if (ua_event
->enabled
) {
2094 * We now need to explicitly enable the event, since it
2095 * is now disabled at creation.
2097 ret
= enable_ust_object(app
, ua_event
->obj
);
2100 * If we hit an EPERM, something is wrong with our enable call. If
2101 * we get an EEXIST, there is a problem on the tracer side since we
2105 case -LTTNG_UST_ERR_PERM
:
2106 /* Code flow problem */
2108 case -LTTNG_UST_ERR_EXIST
:
2109 /* It's OK for our use case. */
2120 health_code_update();
2125 init_ust_event_notifier_from_event_rule(const struct lttng_event_rule
*rule
,
2126 struct lttng_ust_abi_event_notifier
*event_notifier
)
2128 enum lttng_event_rule_status status
;
2129 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2130 int loglevel
= -1, ret
= 0;
2131 const char *pattern
;
2133 memset(event_notifier
, 0, sizeof(*event_notifier
));
2135 if (lttng_event_rule_targets_agent_domain(rule
)) {
2137 * Special event for agents
2138 * The actual meat of the event is in the filter that will be
2139 * attached later on.
2140 * Set the default values for the agent event.
2142 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
2144 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2146 const struct lttng_log_level_rule
*log_level_rule
;
2148 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2149 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2151 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2152 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2153 /* At this point, this is a fatal error. */
2157 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(rule
, &log_level_rule
);
2158 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2159 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2160 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2161 enum lttng_log_level_rule_status llr_status
;
2163 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2164 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2165 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2166 llr_status
= lttng_log_level_rule_exactly_get_level(log_level_rule
,
2169 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2170 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2171 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2172 log_level_rule
, &loglevel
);
2178 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2180 /* At this point this is a fatal error. */
2185 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2186 ret
= lttng_strncpy(
2187 event_notifier
->event
.name
, pattern
, sizeof(event_notifier
->event
.name
));
2189 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern
);
2193 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2194 event_notifier
->event
.loglevel
= loglevel
;
2200 * Create the specified event notifier against the user space tracer of a
2201 * given application.
2203 static int create_ust_event_notifier(struct ust_app
*app
,
2204 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2207 enum lttng_condition_status condition_status
;
2208 const struct lttng_condition
*condition
= nullptr;
2209 struct lttng_ust_abi_event_notifier event_notifier
;
2210 const struct lttng_event_rule
*event_rule
= nullptr;
2211 unsigned int capture_bytecode_count
= 0, i
;
2212 enum lttng_condition_status cond_status
;
2213 enum lttng_event_rule_type event_rule_type
;
2215 health_code_update();
2216 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2218 condition
= lttng_trigger_get_const_condition(ua_event_notifier_rule
->trigger
);
2219 LTTNG_ASSERT(condition
);
2220 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2221 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2223 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2224 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2226 LTTNG_ASSERT(event_rule
);
2228 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2229 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2230 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2231 event_rule_type
== LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2232 event_rule_type
== LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2234 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2235 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2236 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2238 /* Create UST event notifier against the tracer. */
2239 pthread_mutex_lock(&app
->sock_lock
);
2240 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
,
2242 app
->event_notifier_group
.object
,
2243 &ua_event_notifier_rule
->obj
);
2244 pthread_mutex_unlock(&app
->sock_lock
);
2246 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2248 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2251 } else if (ret
== -EAGAIN
) {
2253 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2257 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2258 event_notifier
.event
.name
,
2266 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2268 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2269 event_notifier
.event
.name
,
2272 ua_event_notifier_rule
->obj
);
2274 health_code_update();
2276 /* Set filter if one is present. */
2277 if (ua_event_notifier_rule
->filter
) {
2278 ret
= set_ust_object_filter(
2279 app
, ua_event_notifier_rule
->filter
, ua_event_notifier_rule
->obj
);
2285 /* Set exclusions for the event. */
2286 if (ua_event_notifier_rule
->exclusion
) {
2287 ret
= set_ust_object_exclusions(
2288 app
, ua_event_notifier_rule
->exclusion
, ua_event_notifier_rule
->obj
);
2294 /* Set the capture bytecodes. */
2295 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2296 condition
, &capture_bytecode_count
);
2297 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2299 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2300 const struct lttng_bytecode
*capture_bytecode
=
2301 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2304 ret
= set_ust_capture(app
, capture_bytecode
, i
, ua_event_notifier_rule
->obj
);
2311 * We now need to explicitly enable the event, since it
2312 * is disabled at creation.
2314 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2317 * If we hit an EPERM, something is wrong with our enable call.
2318 * If we get an EEXIST, there is a problem on the tracer side
2319 * since we just created it.
2322 case -LTTNG_UST_ERR_PERM
:
2323 /* Code flow problem. */
2325 case -LTTNG_UST_ERR_EXIST
:
2326 /* It's OK for our use case. */
2336 ua_event_notifier_rule
->enabled
= true;
2339 health_code_update();
2344 * Copy data between an UST app event and a LTT event.
2346 static void shadow_copy_event(struct ust_app_event
*ua_event
, struct ltt_ust_event
*uevent
)
2348 size_t exclusion_alloc_size
;
2350 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2351 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2353 ua_event
->enabled
= uevent
->enabled
;
2355 /* Copy event attributes */
2356 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2358 /* Copy filter bytecode */
2359 if (uevent
->filter
) {
2360 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2361 /* Filter might be NULL here in case of ENONEM. */
2364 /* Copy exclusion data */
2365 if (uevent
->exclusion
) {
2366 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2367 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2368 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2369 if (ua_event
->exclusion
== nullptr) {
2372 memcpy(ua_event
->exclusion
, uevent
->exclusion
, exclusion_alloc_size
);
2378 * Copy data between an UST app channel and a LTT channel.
2380 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
, struct ltt_ust_channel
*uchan
)
2382 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2384 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2385 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2387 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2388 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2390 /* Copy event attributes since the layout is different. */
2391 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2392 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2393 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2394 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2395 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2396 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2397 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2398 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2401 * Note that the attribute channel type is not set since the channel on the
2402 * tracing registry side does not have this information.
2405 ua_chan
->enabled
= uchan
->enabled
;
2406 ua_chan
->tracing_channel_id
= uchan
->id
;
2408 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2412 * Copy data between a UST app session and a regular LTT session.
2414 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2415 struct ltt_ust_session
*usess
,
2416 struct ust_app
*app
)
2418 struct tm
*timeinfo
;
2421 char tmp_shm_path
[PATH_MAX
];
2423 timeinfo
= localtime(&app
->registration_time
);
2424 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2426 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2428 ua_sess
->tracing_id
= usess
->id
;
2429 ua_sess
->id
= get_next_session_id();
2430 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2431 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2432 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2433 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2434 ua_sess
->buffer_type
= usess
->buffer_type
;
2435 ua_sess
->bits_per_long
= app
->abi
.bits_per_long
;
2437 /* There is only one consumer object per session possible. */
2438 consumer_output_get(usess
->consumer
);
2439 ua_sess
->consumer
= usess
->consumer
;
2441 ua_sess
->output_traces
= usess
->output_traces
;
2442 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2443 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &usess
->metadata_attr
);
2445 switch (ua_sess
->buffer_type
) {
2446 case LTTNG_BUFFER_PER_PID
:
2447 ret
= snprintf(ua_sess
->path
,
2448 sizeof(ua_sess
->path
),
2449 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2454 case LTTNG_BUFFER_PER_UID
:
2455 ret
= snprintf(ua_sess
->path
,
2456 sizeof(ua_sess
->path
),
2457 DEFAULT_UST_TRACE_UID_PATH
,
2458 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2459 app
->abi
.bits_per_long
);
2466 PERROR("asprintf UST shadow copy session");
2471 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
, sizeof(ua_sess
->root_shm_path
));
2472 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2473 strncpy(ua_sess
->shm_path
, usess
->shm_path
, sizeof(ua_sess
->shm_path
));
2474 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2475 if (ua_sess
->shm_path
[0]) {
2476 switch (ua_sess
->buffer_type
) {
2477 case LTTNG_BUFFER_PER_PID
:
2478 ret
= snprintf(tmp_shm_path
,
2479 sizeof(tmp_shm_path
),
2480 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2485 case LTTNG_BUFFER_PER_UID
:
2486 ret
= snprintf(tmp_shm_path
,
2487 sizeof(tmp_shm_path
),
2488 "/" DEFAULT_UST_TRACE_UID_PATH
,
2490 app
->abi
.bits_per_long
);
2497 PERROR("sprintf UST shadow copy session");
2501 strncat(ua_sess
->shm_path
,
2503 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2504 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2509 consumer_output_put(ua_sess
->consumer
);
2513 * Lookup sesison wrapper.
2515 static void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2516 struct ust_app
*app
,
2517 struct lttng_ht_iter
*iter
)
2519 /* Get right UST app session from app */
2520 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2524 * Return ust app session from the app session hashtable using the UST session
2527 static struct ust_app_session
*lookup_session_by_app(const struct ltt_ust_session
*usess
,
2528 struct ust_app
*app
)
2530 struct lttng_ht_iter iter
;
2531 struct lttng_ht_node_u64
*node
;
2533 __lookup_session_by_app(usess
, app
, &iter
);
2534 node
= lttng_ht_iter_get_node_u64(&iter
);
2535 if (node
== nullptr) {
2539 return lttng::utils::container_of(node
, &ust_app_session::node
);
2546 * Setup buffer registry per PID for the given session and application. If none
2547 * is found, a new one is created, added to the global registry and
2548 * initialized. If regp is valid, it's set with the newly created object.
2550 * Return 0 on success or else a negative value.
2552 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2553 struct ust_app
*app
,
2554 struct buffer_reg_pid
**regp
)
2557 struct buffer_reg_pid
*reg_pid
;
2559 LTTNG_ASSERT(ua_sess
);
2562 lttng::urcu::read_lock_guard read_lock
;
2564 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2567 * This is the create channel path meaning that if there is NO
2568 * registry available, we have to create one for this session.
2570 ret
= buffer_reg_pid_create(
2571 ua_sess
->id
, ®_pid
, ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2579 /* Initialize registry. */
2580 reg_pid
->registry
->reg
.ust
= ust_registry_session_per_pid_create(
2585 reg_pid
->root_shm_path
,
2587 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2588 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2589 ua_sess
->tracing_id
);
2590 if (!reg_pid
->registry
->reg
.ust
) {
2592 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2593 * destroy the buffer registry, because it is always expected
2594 * that if the buffer registry can be found, its ust registry is
2597 buffer_reg_pid_destroy(reg_pid
);
2601 buffer_reg_pid_add(reg_pid
);
2603 DBG3("UST app buffer registry per PID created successfully");
2614 * Setup buffer registry per UID for the given session and application. If none
2615 * is found, a new one is created, added to the global registry and
2616 * initialized. If regp is valid, it's set with the newly created object.
2618 * Return 0 on success or else a negative value.
2620 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2621 struct ust_app_session
*ua_sess
,
2622 struct ust_app
*app
,
2623 struct buffer_reg_uid
**regp
)
2626 struct buffer_reg_uid
*reg_uid
;
2628 LTTNG_ASSERT(usess
);
2631 lttng::urcu::read_lock_guard read_lock
;
2633 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
2636 * This is the create channel path meaning that if there is NO
2637 * registry available, we have to create one for this session.
2639 ret
= buffer_reg_uid_create(usess
->id
,
2640 app
->abi
.bits_per_long
,
2644 ua_sess
->root_shm_path
,
2653 /* Initialize registry. */
2654 reg_uid
->registry
->reg
.ust
= ust_registry_session_per_uid_create(app
->abi
,
2657 reg_uid
->root_shm_path
,
2661 ua_sess
->tracing_id
,
2663 if (!reg_uid
->registry
->reg
.ust
) {
2665 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2666 * destroy the buffer registry, because it is always expected
2667 * that if the buffer registry can be found, its ust registry is
2670 buffer_reg_uid_destroy(reg_uid
, nullptr);
2674 /* Add node to teardown list of the session. */
2675 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2677 buffer_reg_uid_add(reg_uid
);
2679 DBG3("UST app buffer registry per UID created successfully");
2689 * Create a session on the tracer side for the given app.
2691 * On success, ua_sess_ptr is populated with the session pointer or else left
2692 * untouched. If the session was created, is_created is set to 1. On error,
2693 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2696 * Returns 0 on success or else a negative code which is either -ENOMEM or
2697 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2699 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2700 struct ust_app
*app
,
2701 struct ust_app_session
**ua_sess_ptr
,
2704 int ret
, created
= 0;
2705 struct ust_app_session
*ua_sess
;
2707 LTTNG_ASSERT(usess
);
2709 LTTNG_ASSERT(ua_sess_ptr
);
2711 health_code_update();
2713 ua_sess
= lookup_session_by_app(usess
, app
);
2714 if (ua_sess
== nullptr) {
2715 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2718 ua_sess
= alloc_ust_app_session();
2719 if (ua_sess
== nullptr) {
2720 /* Only malloc can failed so something is really wrong */
2724 shadow_copy_session(ua_sess
, usess
, app
);
2728 switch (usess
->buffer_type
) {
2729 case LTTNG_BUFFER_PER_PID
:
2730 /* Init local registry. */
2731 ret
= setup_buffer_reg_pid(ua_sess
, app
, nullptr);
2733 delete_ust_app_session(-1, ua_sess
, app
);
2737 case LTTNG_BUFFER_PER_UID
:
2738 /* Look for a global registry. If none exists, create one. */
2739 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, nullptr);
2741 delete_ust_app_session(-1, ua_sess
, app
);
2751 health_code_update();
2753 if (ua_sess
->handle
== -1) {
2754 pthread_mutex_lock(&app
->sock_lock
);
2755 ret
= lttng_ust_ctl_create_session(app
->sock
);
2756 pthread_mutex_unlock(&app
->sock_lock
);
2758 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2759 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2763 } else if (ret
== -EAGAIN
) {
2764 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2769 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2774 delete_ust_app_session(-1, ua_sess
, app
);
2775 if (ret
!= -ENOMEM
) {
2777 * Tracer is probably gone or got an internal error so let's
2778 * behave like it will soon unregister or not usable.
2785 ua_sess
->handle
= ret
;
2787 /* Add ust app session to app's HT */
2788 lttng_ht_node_init_u64(&ua_sess
->node
, ua_sess
->tracing_id
);
2789 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2790 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2791 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
, &ua_sess
->ust_objd_node
);
2793 DBG2("UST app session created successfully with handle %d", ret
);
2796 *ua_sess_ptr
= ua_sess
;
2798 *is_created
= created
;
2801 /* Everything went well. */
2805 health_code_update();
2810 * Match function for a hash table lookup of ust_app_ctx.
2812 * It matches an ust app context based on the context type and, in the case
2813 * of perf counters, their name.
2815 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2817 struct ust_app_ctx
*ctx
;
2818 const struct lttng_ust_context_attr
*key
;
2823 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2824 key
= (lttng_ust_context_attr
*) _key
;
2827 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2832 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2833 if (strncmp(key
->u
.perf_counter
.name
,
2834 ctx
->ctx
.u
.perf_counter
.name
,
2835 sizeof(key
->u
.perf_counter
.name
)) != 0) {
2839 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2840 if (strcmp(key
->u
.app_ctx
.provider_name
, ctx
->ctx
.u
.app_ctx
.provider_name
) != 0 ||
2841 strcmp(key
->u
.app_ctx
.ctx_name
, ctx
->ctx
.u
.app_ctx
.ctx_name
) != 0) {
2857 * Lookup for an ust app context from an lttng_ust_context.
2859 * Must be called while holding RCU read side lock.
2860 * Return an ust_app_ctx object or NULL on error.
2862 static struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2863 struct lttng_ust_context_attr
*uctx
)
2865 struct lttng_ht_iter iter
;
2866 struct lttng_ht_node_ulong
*node
;
2867 struct ust_app_ctx
*app_ctx
= nullptr;
2871 ASSERT_RCU_READ_LOCKED();
2873 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2874 cds_lfht_lookup(ht
->ht
,
2875 ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2876 ht_match_ust_app_ctx
,
2879 node
= lttng_ht_iter_get_node_ulong(&iter
);
2884 app_ctx
= lttng::utils::container_of(node
, &ust_app_ctx::node
);
2891 * Create a context for the channel on the tracer.
2893 * Called with UST app session lock held and a RCU read side lock.
2895 static int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2896 struct lttng_ust_context_attr
*uctx
,
2897 struct ust_app
*app
)
2900 struct ust_app_ctx
*ua_ctx
;
2902 ASSERT_RCU_READ_LOCKED();
2904 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2906 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2912 ua_ctx
= alloc_ust_app_ctx(uctx
);
2913 if (ua_ctx
== nullptr) {
2919 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2920 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2921 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2923 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2933 * Enable on the tracer side a ust app event for the session and channel.
2935 * Called with UST app session lock held.
2937 static int enable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2941 ret
= enable_ust_object(app
, ua_event
->obj
);
2946 ua_event
->enabled
= true;
2953 * Disable on the tracer side a ust app event for the session and channel.
2955 static int disable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2959 ret
= disable_ust_object(app
, ua_event
->obj
);
2964 ua_event
->enabled
= false;
2971 * Lookup ust app channel for session and disable it on the tracer side.
2973 static int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2974 struct ust_app_channel
*ua_chan
,
2975 struct ust_app
*app
)
2979 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2984 ua_chan
->enabled
= false;
2991 * Lookup ust app channel for session and enable it on the tracer side. This
2992 * MUST be called with a RCU read side lock acquired.
2994 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2995 struct ltt_ust_channel
*uchan
,
2996 struct ust_app
*app
)
2999 struct lttng_ht_iter iter
;
3000 struct lttng_ht_node_str
*ua_chan_node
;
3001 struct ust_app_channel
*ua_chan
;
3003 ASSERT_RCU_READ_LOCKED();
3005 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3006 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3007 if (ua_chan_node
== nullptr) {
3008 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
3010 ua_sess
->tracing_id
);
3014 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3016 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
3026 * Ask the consumer to create a channel and get it if successful.
3028 * Called with UST app session lock held.
3030 * Return 0 on success or else a negative value.
3032 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
3033 struct ust_app_session
*ua_sess
,
3034 struct ust_app_channel
*ua_chan
,
3036 lsu::registry_session
*registry
)
3039 unsigned int nb_fd
= 0;
3040 struct consumer_socket
*socket
;
3042 LTTNG_ASSERT(usess
);
3043 LTTNG_ASSERT(ua_sess
);
3044 LTTNG_ASSERT(ua_chan
);
3045 LTTNG_ASSERT(registry
);
3047 lttng::urcu::read_lock_guard read_lock
;
3048 health_code_update();
3050 /* Get the right consumer socket for the application. */
3051 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3057 health_code_update();
3059 /* Need one fd for the channel. */
3060 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3062 ERR("Exhausted number of available FD upon create channel");
3067 * Ask consumer to create channel. The consumer will return the number of
3068 * stream we have to expect.
3070 ret
= ust_consumer_ask_channel(
3071 ua_sess
, ua_chan
, usess
->consumer
, socket
, registry
, usess
->current_trace_chunk
);
3077 * Compute the number of fd needed before receiving them. It must be 2 per
3078 * stream (2 being the default value here).
3080 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3082 /* Reserve the amount of file descriptor we need. */
3083 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3085 ERR("Exhausted number of available FD upon create channel");
3086 goto error_fd_get_stream
;
3089 health_code_update();
3092 * Now get the channel from the consumer. This call will populate the stream
3093 * list of that channel and set the ust objects.
3095 if (usess
->consumer
->enabled
) {
3096 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3105 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3106 error_fd_get_stream
:
3108 * Initiate a destroy channel on the consumer since we had an error
3109 * handling it on our side. The return value is of no importance since we
3110 * already have a ret value set by the previous error that we need to
3113 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3115 lttng_fd_put(LTTNG_FD_APPS
, 1);
3117 health_code_update();
3122 * Duplicate the ust data object of the ust app stream and save it in the
3123 * buffer registry stream.
3125 * Return 0 on success or else a negative value.
3127 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3128 struct ust_app_stream
*stream
)
3132 LTTNG_ASSERT(reg_stream
);
3133 LTTNG_ASSERT(stream
);
3135 /* Duplicating a stream requires 2 new fds. Reserve them. */
3136 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3138 ERR("Exhausted number of available FD upon duplicate stream");
3142 /* Duplicate object for stream once the original is in the registry. */
3143 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
, reg_stream
->obj
.ust
);
3145 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3146 reg_stream
->obj
.ust
,
3149 lttng_fd_put(LTTNG_FD_APPS
, 2);
3152 stream
->handle
= stream
->obj
->handle
;
3159 * Duplicate the ust data object of the ust app. channel and save it in the
3160 * buffer registry channel.
3162 * Return 0 on success or else a negative value.
3164 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3165 struct ust_app_channel
*ua_chan
)
3169 LTTNG_ASSERT(buf_reg_chan
);
3170 LTTNG_ASSERT(ua_chan
);
3172 /* Duplicating a channel requires 1 new fd. Reserve it. */
3173 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3175 ERR("Exhausted number of available FD upon duplicate channel");
3179 /* Duplicate object for stream once the original is in the registry. */
3180 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3182 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3183 buf_reg_chan
->obj
.ust
,
3188 ua_chan
->handle
= ua_chan
->obj
->handle
;
3193 lttng_fd_put(LTTNG_FD_APPS
, 1);
3199 * For a given channel buffer registry, setup all streams of the given ust
3200 * application channel.
3202 * Return 0 on success or else a negative value.
3204 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3205 struct ust_app_channel
*ua_chan
,
3206 struct ust_app
*app
)
3209 struct ust_app_stream
*stream
, *stmp
;
3211 LTTNG_ASSERT(buf_reg_chan
);
3212 LTTNG_ASSERT(ua_chan
);
3214 DBG2("UST app setup buffer registry stream");
3216 /* Send all streams to application. */
3217 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3218 struct buffer_reg_stream
*reg_stream
;
3220 ret
= buffer_reg_stream_create(®_stream
);
3226 * Keep original pointer and nullify it in the stream so the delete
3227 * stream call does not release the object.
3229 reg_stream
->obj
.ust
= stream
->obj
;
3230 stream
->obj
= nullptr;
3231 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3233 /* We don't need the streams anymore. */
3234 cds_list_del(&stream
->list
);
3235 delete_ust_app_stream(-1, stream
, app
);
3243 * Create a buffer registry channel for the given session registry and
3244 * application channel object. If regp pointer is valid, it's set with the
3245 * created object. Important, the created object is NOT added to the session
3246 * registry hash table.
3248 * Return 0 on success else a negative value.
3250 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3251 struct ust_app_channel
*ua_chan
,
3252 struct buffer_reg_channel
**regp
)
3255 struct buffer_reg_channel
*buf_reg_chan
= nullptr;
3257 LTTNG_ASSERT(reg_sess
);
3258 LTTNG_ASSERT(ua_chan
);
3260 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3262 /* Create buffer registry channel. */
3263 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3267 LTTNG_ASSERT(buf_reg_chan
);
3268 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3269 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3270 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3272 /* Create and add a channel registry to session. */
3274 reg_sess
->reg
.ust
->add_channel(ua_chan
->tracing_channel_id
);
3275 } catch (const std::exception
& ex
) {
3276 ERR("Failed to add a channel registry to userspace registry session: %s",
3282 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3285 *regp
= buf_reg_chan
;
3291 /* Safe because the registry channel object was not added to any HT. */
3292 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3298 * Setup buffer registry channel for the given session registry and application
3299 * channel object. If regp pointer is valid, it's set with the created object.
3301 * Return 0 on success else a negative value.
3303 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3304 struct ust_app_channel
*ua_chan
,
3305 struct buffer_reg_channel
*buf_reg_chan
,
3306 struct ust_app
*app
)
3310 LTTNG_ASSERT(reg_sess
);
3311 LTTNG_ASSERT(buf_reg_chan
);
3312 LTTNG_ASSERT(ua_chan
);
3313 LTTNG_ASSERT(ua_chan
->obj
);
3315 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3317 /* Setup all streams for the registry. */
3318 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3323 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3324 ua_chan
->obj
= nullptr;
3329 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3330 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3335 * Send buffer registry channel to the application.
3337 * Return 0 on success else a negative value.
3339 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3340 struct ust_app
*app
,
3341 struct ust_app_session
*ua_sess
,
3342 struct ust_app_channel
*ua_chan
)
3345 struct buffer_reg_stream
*reg_stream
;
3347 LTTNG_ASSERT(buf_reg_chan
);
3349 LTTNG_ASSERT(ua_sess
);
3350 LTTNG_ASSERT(ua_chan
);
3352 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3354 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3359 /* Send channel to the application. */
3360 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3361 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3362 ret
= -ENOTCONN
; /* Caused by app exiting. */
3364 } else if (ret
== -EAGAIN
) {
3365 /* Caused by timeout. */
3366 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
3370 ua_sess
->tracing_id
);
3371 /* Treat this the same way as an application that is exiting. */
3374 } else if (ret
< 0) {
3378 health_code_update();
3380 /* Send all streams to application. */
3381 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3382 cds_list_for_each_entry (reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3383 struct ust_app_stream stream
= {};
3385 ret
= duplicate_stream_object(reg_stream
, &stream
);
3387 goto error_stream_unlock
;
3390 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3392 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3393 ret
= -ENOTCONN
; /* Caused by app exiting. */
3394 } else if (ret
== -EAGAIN
) {
3396 * Caused by timeout.
3397 * Treat this the same way as an application
3400 WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64
3404 ua_sess
->tracing_id
);
3407 (void) release_ust_app_stream(-1, &stream
, app
);
3408 goto error_stream_unlock
;
3412 * The return value is not important here. This function will output an
3415 (void) release_ust_app_stream(-1, &stream
, app
);
3418 error_stream_unlock
:
3419 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3425 * Create and send to the application the created buffers with per UID buffers.
3427 * This MUST be called with a RCU read side lock acquired.
3428 * The session list lock and the session's lock must be acquired.
3430 * Return 0 on success else a negative value.
3432 static int create_channel_per_uid(struct ust_app
*app
,
3433 struct ltt_ust_session
*usess
,
3434 struct ust_app_session
*ua_sess
,
3435 struct ust_app_channel
*ua_chan
)
3438 struct buffer_reg_uid
*reg_uid
;
3439 struct buffer_reg_channel
*buf_reg_chan
;
3440 struct ltt_session
*session
= nullptr;
3441 enum lttng_error_code notification_ret
;
3444 LTTNG_ASSERT(usess
);
3445 LTTNG_ASSERT(ua_sess
);
3446 LTTNG_ASSERT(ua_chan
);
3447 ASSERT_RCU_READ_LOCKED();
3449 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3451 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
3453 * The session creation handles the creation of this global registry
3454 * object. If none can be find, there is a code flow problem or a
3457 LTTNG_ASSERT(reg_uid
);
3459 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
, reg_uid
);
3464 /* Create the buffer registry channel object. */
3465 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3467 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan
->name
);
3471 session
= session_find_by_id(ua_sess
->tracing_id
);
3472 LTTNG_ASSERT(session
);
3473 ASSERT_LOCKED(session
->lock
);
3474 ASSERT_SESSION_LIST_LOCKED();
3477 * Create the buffers on the consumer side. This call populates the
3478 * ust app channel object with all streams and data object.
3480 ret
= do_consumer_create_channel(
3481 usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, reg_uid
->registry
->reg
.ust
);
3483 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3486 * Let's remove the previously created buffer registry channel so
3487 * it's not visible anymore in the session registry.
3489 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3491 locked_registry
->remove_channel(ua_chan
->tracing_channel_id
, false);
3492 } catch (const std::exception
& ex
) {
3493 DBG("Could not find channel for removal: %s", ex
.what());
3495 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3496 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3501 * Setup the streams and add it to the session registry.
3503 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, buf_reg_chan
, app
);
3505 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3510 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3511 auto& ust_reg_chan
= locked_registry
->channel(ua_chan
->tracing_channel_id
);
3513 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3516 /* Notify the notification subsystem of the channel's creation. */
3517 notification_ret
= notification_thread_command_add_channel(
3518 the_notification_thread_handle
,
3523 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3524 if (notification_ret
!= LTTNG_OK
) {
3525 ret
= -(int) notification_ret
;
3526 ERR("Failed to add channel to notification thread");
3531 /* Send buffers to the application. */
3532 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3534 if (ret
!= -ENOTCONN
) {
3535 ERR("Error sending channel to application");
3542 session_put(session
);
3548 * Create and send to the application the created buffers with per PID buffers.
3550 * Called with UST app session lock held.
3551 * The session list lock and the session's lock must be acquired.
3553 * Return 0 on success else a negative value.
3555 static int create_channel_per_pid(struct ust_app
*app
,
3556 struct ltt_ust_session
*usess
,
3557 struct ust_app_session
*ua_sess
,
3558 struct ust_app_channel
*ua_chan
)
3561 lsu::registry_session
*registry
;
3562 enum lttng_error_code cmd_ret
;
3563 struct ltt_session
*session
= nullptr;
3564 uint64_t chan_reg_key
;
3567 LTTNG_ASSERT(usess
);
3568 LTTNG_ASSERT(ua_sess
);
3569 LTTNG_ASSERT(ua_chan
);
3571 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3573 lttng::urcu::read_lock_guard read_lock
;
3575 registry
= get_session_registry(ua_sess
);
3576 /* The UST app session lock is held, registry shall not be null. */
3577 LTTNG_ASSERT(registry
);
3579 /* Create and add a new channel registry to session. */
3581 registry
->add_channel(ua_chan
->key
);
3582 } catch (const std::exception
& ex
) {
3583 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3590 session
= session_find_by_id(ua_sess
->tracing_id
);
3591 LTTNG_ASSERT(session
);
3592 ASSERT_LOCKED(session
->lock
);
3593 ASSERT_SESSION_LIST_LOCKED();
3595 /* Create and get channel on the consumer side. */
3596 ret
= do_consumer_create_channel(usess
, ua_sess
, 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
, 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());
3642 session_put(session
);
3648 * From an already allocated ust app channel, create the channel buffers if
3649 * needed and send them to the application. This MUST be called with a RCU read
3650 * side lock acquired.
3652 * Called with UST app session lock held.
3654 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3655 * the application exited concurrently.
3657 static int ust_app_channel_send(struct ust_app
*app
,
3658 struct ltt_ust_session
*usess
,
3659 struct ust_app_session
*ua_sess
,
3660 struct ust_app_channel
*ua_chan
)
3665 LTTNG_ASSERT(usess
);
3666 LTTNG_ASSERT(usess
->active
);
3667 LTTNG_ASSERT(ua_sess
);
3668 LTTNG_ASSERT(ua_chan
);
3669 ASSERT_RCU_READ_LOCKED();
3671 /* Handle buffer type before sending the channel to the application. */
3672 switch (usess
->buffer_type
) {
3673 case LTTNG_BUFFER_PER_UID
:
3675 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3681 case LTTNG_BUFFER_PER_PID
:
3683 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3695 /* Initialize ust objd object using the received handle and add it. */
3696 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3697 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3699 /* If channel is not enabled, disable it on the tracer */
3700 if (!ua_chan
->enabled
) {
3701 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3712 * Create UST app channel and return it through ua_chanp if not NULL.
3714 * Called with UST app session lock and RCU read-side lock held.
3716 * Return 0 on success or else a negative value.
3718 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3719 struct ltt_ust_channel
*uchan
,
3720 enum lttng_ust_abi_chan_type type
,
3721 struct ltt_ust_session
*usess
__attribute__((unused
)),
3722 struct ust_app_channel
**ua_chanp
)
3725 struct lttng_ht_iter iter
;
3726 struct lttng_ht_node_str
*ua_chan_node
;
3727 struct ust_app_channel
*ua_chan
;
3729 ASSERT_RCU_READ_LOCKED();
3731 /* Lookup channel in the ust app session */
3732 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3733 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3734 if (ua_chan_node
!= nullptr) {
3735 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3739 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3740 if (ua_chan
== nullptr) {
3741 /* Only malloc can fail here */
3745 shadow_copy_channel(ua_chan
, uchan
);
3747 /* Set channel type. */
3748 ua_chan
->attr
.type
= type
;
3750 /* Only add the channel if successful on the tracer side. */
3751 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3754 *ua_chanp
= ua_chan
;
3757 /* Everything went well. */
3765 * Create UST app event and create it on the tracer side.
3767 * Must be called with the RCU read side lock held.
3768 * Called with ust app session mutex held.
3770 static int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3771 struct ltt_ust_event
*uevent
,
3772 struct ust_app
*app
)
3775 struct ust_app_event
*ua_event
;
3777 ASSERT_RCU_READ_LOCKED();
3779 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3780 if (ua_event
== nullptr) {
3781 /* Only failure mode of alloc_ust_app_event(). */
3785 shadow_copy_event(ua_event
, uevent
);
3787 /* Create it on the tracer side */
3788 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3791 * Not found previously means that it does not exist on the
3792 * tracer. If the application reports that the event existed,
3793 * it means there is a bug in the sessiond or lttng-ust
3794 * (or corruption, etc.)
3796 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3797 ERR("Tracer for application reported that an event being created already existed: "
3798 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3808 add_unique_ust_app_event(ua_chan
, ua_event
);
3810 DBG2("UST app create event completed: app = '%s' pid = %d", app
->name
, app
->pid
);
3816 /* Valid. Calling here is already in a read side lock */
3817 delete_ust_app_event(-1, ua_event
, app
);
3822 * Create UST app event notifier rule and create it on the tracer side.
3824 * Must be called with the RCU read side lock held.
3825 * Called with ust app session mutex held.
3827 static int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
, struct ust_app
*app
)
3830 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3832 ASSERT_RCU_READ_LOCKED();
3834 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3835 if (ua_event_notifier_rule
== nullptr) {
3840 /* Create it on the tracer side. */
3841 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3844 * Not found previously means that it does not exist on the
3845 * tracer. If the application reports that the event existed,
3846 * it means there is a bug in the sessiond or lttng-ust
3847 * (or corruption, etc.)
3849 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3850 ERR("Tracer for application reported that an event notifier being created already exists: "
3851 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3852 lttng_trigger_get_tracer_token(trigger
),
3861 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3862 &ua_event_notifier_rule
->node
);
3864 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3867 lttng_trigger_get_tracer_token(trigger
));
3872 /* The RCU read side lock is already being held by the caller. */
3873 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3879 * Create UST metadata and open it on the tracer side.
3881 * Called with UST app session lock held and RCU read side lock.
3883 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3884 struct ust_app
*app
,
3885 struct consumer_output
*consumer
)
3888 struct ust_app_channel
*metadata
;
3889 struct consumer_socket
*socket
;
3890 struct ltt_session
*session
= nullptr;
3892 LTTNG_ASSERT(ua_sess
);
3894 LTTNG_ASSERT(consumer
);
3895 ASSERT_RCU_READ_LOCKED();
3897 auto locked_registry
= get_locked_session_registry(ua_sess
);
3898 /* The UST app session is held registry shall not be null. */
3899 LTTNG_ASSERT(locked_registry
);
3901 /* Metadata already exists for this registry or it was closed previously */
3902 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3907 /* Allocate UST metadata */
3908 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, nullptr);
3910 /* malloc() failed */
3915 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3917 /* Need one fd for the channel. */
3918 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3920 ERR("Exhausted number of available FD upon create metadata");
3924 /* Get the right consumer socket for the application. */
3925 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3928 goto error_consumer
;
3932 * Keep metadata key so we can identify it on the consumer side. Assign it
3933 * to the registry *before* we ask the consumer so we avoid the race of the
3934 * consumer requesting the metadata and the ask_channel call on our side
3935 * did not returned yet.
3937 locked_registry
->_metadata_key
= metadata
->key
;
3939 session
= session_find_by_id(ua_sess
->tracing_id
);
3940 LTTNG_ASSERT(session
);
3941 ASSERT_LOCKED(session
->lock
);
3942 ASSERT_SESSION_LIST_LOCKED();
3945 * Ask the metadata channel creation to the consumer. The metadata object
3946 * will be created by the consumer and kept their. However, the stream is
3947 * never added or monitored until we do a first push metadata to the
3950 ret
= ust_consumer_ask_channel(ua_sess
,
3954 locked_registry
.get(),
3955 session
->current_trace_chunk
);
3957 /* Nullify the metadata key so we don't try to close it later on. */
3958 locked_registry
->_metadata_key
= 0;
3959 goto error_consumer
;
3963 * The setup command will make the metadata stream be sent to the relayd,
3964 * if applicable, and the thread managing the metadatas. This is important
3965 * because after this point, if an error occurs, the only way the stream
3966 * can be deleted is to be monitored in the consumer.
3968 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3970 /* Nullify the metadata key so we don't try to close it later on. */
3971 locked_registry
->_metadata_key
= 0;
3972 goto error_consumer
;
3975 DBG2("UST metadata with key %" PRIu64
" created for app pid %d", metadata
->key
, app
->pid
);
3978 lttng_fd_put(LTTNG_FD_APPS
, 1);
3979 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3982 session_put(session
);
3988 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3989 * acquired before calling this function.
3991 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3993 struct ust_app
*app
= nullptr;
3994 struct lttng_ht_node_ulong
*node
;
3995 struct lttng_ht_iter iter
;
3997 lttng_ht_lookup(ust_app_ht
, (void *) ((unsigned long) pid
), &iter
);
3998 node
= lttng_ht_iter_get_node_ulong(&iter
);
3999 if (node
== nullptr) {
4000 DBG2("UST app no found with pid %d", pid
);
4004 DBG2("Found UST app by pid %d", pid
);
4006 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
4013 * Allocate and init an UST app object using the registration information and
4014 * the command socket. This is called when the command socket connects to the
4017 * The object is returned on success or else NULL.
4019 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
4022 struct ust_app
*lta
= nullptr;
4023 struct lttng_pipe
*event_notifier_event_source_pipe
= nullptr;
4026 LTTNG_ASSERT(sock
>= 0);
4028 DBG3("UST app creating application for socket %d", sock
);
4030 if ((msg
->bits_per_long
== 64 && (uatomic_read(&the_ust_consumerd64_fd
) == -EINVAL
)) ||
4031 (msg
->bits_per_long
== 32 && (uatomic_read(&the_ust_consumerd32_fd
) == -EINVAL
))) {
4032 ERR("Registration failed: application \"%s\" (pid: %d) has "
4033 "%d-bit long, but no consumerd for this size is available.\n",
4036 msg
->bits_per_long
);
4041 * Reserve the two file descriptors of the event source pipe. The write
4042 * end will be closed once it is passed to the application, at which
4043 * point a single 'put' will be performed.
4045 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
4047 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4053 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
4054 if (!event_notifier_event_source_pipe
) {
4055 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4061 lta
= zmalloc
<ust_app
>();
4062 if (lta
== nullptr) {
4064 goto error_free_pipe
;
4067 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4069 lta
->ppid
= msg
->ppid
;
4070 lta
->uid
= msg
->uid
;
4071 lta
->gid
= msg
->gid
;
4074 .bits_per_long
= msg
->bits_per_long
,
4075 .long_alignment
= msg
->long_alignment
,
4076 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4077 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4078 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4079 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4080 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4081 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4082 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4085 lta
->v_major
= msg
->major
;
4086 lta
->v_minor
= msg
->minor
;
4087 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4088 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4089 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4090 lta
->notify_sock
= -1;
4091 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4093 /* Copy name and make sure it's NULL terminated. */
4094 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4095 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4098 * Before this can be called, when receiving the registration information,
4099 * the application compatibility is checked. So, at this point, the
4100 * application can work with this session daemon.
4102 lta
->compatible
= 1;
4104 lta
->pid
= msg
->pid
;
4105 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4107 pthread_mutex_init(<a
->sock_lock
, nullptr);
4108 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4110 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4114 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4115 lttng_fd_put(LTTNG_FD_APPS
, 2);
4121 * For a given application object, add it to every hash table.
4123 void ust_app_add(struct ust_app
*app
)
4126 LTTNG_ASSERT(app
->notify_sock
>= 0);
4128 app
->registration_time
= time(nullptr);
4130 lttng::urcu::read_lock_guard read_lock
;
4133 * On a re-registration, we want to kick out the previous registration of
4136 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4139 * The socket _should_ be unique until _we_ call close. So, a add_unique
4140 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4141 * already in the table.
4143 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4145 /* Add application to the notify socket hash table. */
4146 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4147 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4149 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4150 "notify_sock =%d (version %d.%d)",
4163 * Set the application version into the object.
4165 * Return 0 on success else a negative value either an errno code or a
4166 * LTTng-UST error code.
4168 int ust_app_version(struct ust_app
*app
)
4174 pthread_mutex_lock(&app
->sock_lock
);
4175 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4176 pthread_mutex_unlock(&app
->sock_lock
);
4178 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4179 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4182 } else if (ret
== -EAGAIN
) {
4183 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4187 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4197 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4199 return app
->v_major
>= 9;
4202 bool ust_app_supports_counters(const struct ust_app
*app
)
4204 return app
->v_major
>= 9;
4208 * Setup the base event notifier group.
4210 * Return 0 on success else a negative value either an errno code or a
4211 * LTTng-UST error code.
4213 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4216 int event_pipe_write_fd
;
4217 struct lttng_ust_abi_object_data
*event_notifier_group
= nullptr;
4218 enum lttng_error_code lttng_ret
;
4219 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4223 if (!ust_app_supports_notifiers(app
)) {
4228 /* Get the write side of the pipe. */
4229 event_pipe_write_fd
= lttng_pipe_get_writefd(app
->event_notifier_group
.event_pipe
);
4231 pthread_mutex_lock(&app
->sock_lock
);
4232 ret
= lttng_ust_ctl_create_event_notifier_group(
4233 app
->sock
, event_pipe_write_fd
, &event_notifier_group
);
4234 pthread_mutex_unlock(&app
->sock_lock
);
4236 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4238 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4241 } else if (ret
== -EAGAIN
) {
4243 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4247 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4251 event_pipe_write_fd
);
4256 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4258 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4265 * Release the file descriptor that was reserved for the write-end of
4268 lttng_fd_put(LTTNG_FD_APPS
, 1);
4270 lttng_ret
= notification_thread_command_add_tracer_event_source(
4271 the_notification_thread_handle
,
4272 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4274 if (lttng_ret
!= LTTNG_OK
) {
4275 ERR("Failed to add tracer event source to notification thread");
4280 /* Assign handle only when the complete setup is valid. */
4281 app
->event_notifier_group
.object
= event_notifier_group
;
4283 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
4284 switch (event_notifier_error_accounting_status
) {
4285 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4287 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4288 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",
4293 goto error_accounting
;
4294 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4295 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4300 goto error_accounting
;
4302 ERR("Failed to setup event notifier error accounting for app");
4304 goto error_accounting
;
4310 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4311 the_notification_thread_handle
,
4312 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
));
4313 if (lttng_ret
!= LTTNG_OK
) {
4314 ERR("Failed to remove application tracer event source from notification thread");
4318 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4319 free(app
->event_notifier_group
.object
);
4320 app
->event_notifier_group
.object
= nullptr;
4325 * Unregister app by removing it from the global traceable app list and freeing
4328 * The socket is already closed at this point so no close to sock.
4330 void ust_app_unregister(int sock
)
4332 struct ust_app
*lta
;
4333 struct lttng_ht_node_ulong
*node
;
4334 struct lttng_ht_iter ust_app_sock_iter
;
4335 struct lttng_ht_iter iter
;
4336 struct ust_app_session
*ua_sess
;
4339 lttng::urcu::read_lock_guard read_lock
;
4341 /* Get the node reference for a call_rcu */
4342 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &ust_app_sock_iter
);
4343 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4346 lta
= lttng::utils::container_of(node
, &ust_app::sock_n
);
4347 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4350 * For per-PID buffers, perform "push metadata" and flush all
4351 * application streams before removing app from hash tables,
4352 * ensuring proper behavior of data_pending check.
4353 * Remove sessions so they are not visible during deletion.
4355 cds_lfht_for_each_entry (lta
->sessions
->ht
, &iter
.iter
, ua_sess
, node
.node
) {
4356 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4358 /* The session was already removed so scheduled for teardown. */
4362 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4363 (void) ust_app_flush_app_session(lta
, ua_sess
);
4367 * Add session to list for teardown. This is safe since at this point we
4368 * are the only one using this list.
4370 pthread_mutex_lock(&ua_sess
->lock
);
4372 if (ua_sess
->deleted
) {
4373 pthread_mutex_unlock(&ua_sess
->lock
);
4378 * Normally, this is done in the delete session process which is
4379 * executed in the call rcu below. However, upon registration we can't
4380 * afford to wait for the grace period before pushing data or else the
4381 * data pending feature can race between the unregistration and stop
4382 * command where the data pending command is sent *before* the grace
4385 * The close metadata below nullifies the metadata pointer in the
4386 * session so the delete session will NOT push/close a second time.
4388 auto locked_registry
= get_locked_session_registry(ua_sess
);
4389 if (locked_registry
) {
4390 /* Push metadata for application before freeing the application. */
4391 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4394 * Don't ask to close metadata for global per UID buffers. Close
4395 * metadata only on destroy trace session in this case. Also, the
4396 * previous push metadata could have flag the metadata registry to
4397 * close so don't send a close command if closed.
4399 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4400 const auto metadata_key
= locked_registry
->_metadata_key
;
4401 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4403 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4404 locked_registry
->_metadata_closed
= true;
4407 /* Release lock before communication, see comments in
4408 * close_metadata(). */
4409 locked_registry
.reset();
4410 (void) close_metadata(
4411 metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4413 locked_registry
.reset();
4416 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4418 pthread_mutex_unlock(&ua_sess
->lock
);
4421 /* Remove application from PID hash table */
4422 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4426 * Remove application from notify hash table. The thread handling the
4427 * notify socket could have deleted the node so ignore on error because
4428 * either way it's valid. The close of that socket is handled by the
4429 * apps_notify_thread.
4431 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4432 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4435 * Ignore return value since the node might have been removed before by an
4436 * add replace during app registration because the PID can be reassigned by
4439 iter
.iter
.node
= <a
->pid_n
.node
;
4440 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4442 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", lta
->pid
);
4446 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4452 * Fill events array with all events name of all registered apps.
4454 int ust_app_list_events(struct lttng_event
**events
)
4457 size_t nbmem
, count
= 0;
4458 struct lttng_ht_iter iter
;
4459 struct ust_app
*app
;
4460 struct lttng_event
*tmp_event
;
4462 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4463 tmp_event
= calloc
<lttng_event
>(nbmem
);
4464 if (tmp_event
== nullptr) {
4465 PERROR("zmalloc ust app events");
4471 lttng::urcu::read_lock_guard read_lock
;
4473 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4474 struct lttng_ust_abi_tracepoint_iter uiter
;
4476 health_code_update();
4478 if (!app
->compatible
) {
4480 * TODO: In time, we should notice the caller of this error by
4481 * telling him that this is a version error.
4486 pthread_mutex_lock(&app
->sock_lock
);
4487 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4489 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4490 ERR("UST app list events getting handle failed for app pid %d",
4493 pthread_mutex_unlock(&app
->sock_lock
);
4497 while ((ret
= lttng_ust_ctl_tracepoint_list_get(
4498 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4499 /* Handle ustctl error. */
4503 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4504 ERR("UST app tp list get failed for app %d with ret %d",
4508 DBG3("UST app tp list get failed. Application is dead");
4514 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4515 if (release_ret
< 0 &&
4516 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4517 release_ret
!= -EPIPE
) {
4518 ERR("Error releasing app handle for app %d with ret %d",
4523 pthread_mutex_unlock(&app
->sock_lock
);
4527 health_code_update();
4528 if (count
>= nbmem
) {
4529 /* In case the realloc fails, we free the memory */
4530 struct lttng_event
*new_tmp_event
;
4533 new_nbmem
= nbmem
<< 1;
4534 DBG2("Reallocating event list from %zu to %zu entries",
4537 new_tmp_event
= (lttng_event
*) realloc(
4538 tmp_event
, new_nbmem
* sizeof(struct lttng_event
));
4539 if (new_tmp_event
== nullptr) {
4542 PERROR("realloc ust app events");
4545 release_ret
= lttng_ust_ctl_release_handle(
4547 if (release_ret
< 0 &&
4548 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4549 release_ret
!= -EPIPE
) {
4550 ERR("Error releasing app handle for app %d with ret %d",
4555 pthread_mutex_unlock(&app
->sock_lock
);
4558 /* Zero the new memory */
4559 memset(new_tmp_event
+ nbmem
,
4561 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4563 tmp_event
= new_tmp_event
;
4566 memcpy(tmp_event
[count
].name
,
4568 LTTNG_UST_ABI_SYM_NAME_LEN
);
4569 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4570 tmp_event
[count
].type
=
4571 (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4572 tmp_event
[count
].pid
= app
->pid
;
4573 tmp_event
[count
].enabled
= -1;
4577 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4578 pthread_mutex_unlock(&app
->sock_lock
);
4580 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4581 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4584 } else if (ret
== -EAGAIN
) {
4585 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4589 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4599 *events
= tmp_event
;
4601 DBG2("UST app list events done (%zu events)", count
);
4605 health_code_update();
4610 * Fill events array with all events name of all registered apps.
4612 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4615 size_t nbmem
, count
= 0;
4616 struct lttng_ht_iter iter
;
4617 struct ust_app
*app
;
4618 struct lttng_event_field
*tmp_event
;
4620 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4621 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4622 if (tmp_event
== nullptr) {
4623 PERROR("zmalloc ust app event fields");
4629 lttng::urcu::read_lock_guard read_lock
;
4631 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4632 struct lttng_ust_abi_field_iter uiter
;
4634 health_code_update();
4636 if (!app
->compatible
) {
4638 * TODO: In time, we should notice the caller of this error by
4639 * telling him that this is a version error.
4644 pthread_mutex_lock(&app
->sock_lock
);
4645 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4647 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4648 ERR("UST app list field getting handle failed for app pid %d",
4651 pthread_mutex_unlock(&app
->sock_lock
);
4655 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(
4656 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4657 /* Handle ustctl error. */
4661 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4662 ERR("UST app tp list field failed for app %d with ret %d",
4666 DBG3("UST app tp list field failed. Application is dead");
4672 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4673 pthread_mutex_unlock(&app
->sock_lock
);
4674 if (release_ret
< 0 &&
4675 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4676 release_ret
!= -EPIPE
) {
4677 ERR("Error releasing app handle for app %d with ret %d",
4685 health_code_update();
4686 if (count
>= nbmem
) {
4687 /* In case the realloc fails, we free the memory */
4688 struct lttng_event_field
*new_tmp_event
;
4691 new_nbmem
= nbmem
<< 1;
4692 DBG2("Reallocating event field list from %zu to %zu entries",
4695 new_tmp_event
= (lttng_event_field
*) realloc(
4697 new_nbmem
* sizeof(struct lttng_event_field
));
4698 if (new_tmp_event
== nullptr) {
4701 PERROR("realloc ust app event fields");
4704 release_ret
= lttng_ust_ctl_release_handle(
4706 pthread_mutex_unlock(&app
->sock_lock
);
4708 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4709 release_ret
!= -EPIPE
) {
4710 ERR("Error releasing app handle for app %d with ret %d",
4718 /* Zero the new memory */
4719 memset(new_tmp_event
+ nbmem
,
4721 (new_nbmem
- nbmem
) *
4722 sizeof(struct lttng_event_field
));
4724 tmp_event
= new_tmp_event
;
4727 memcpy(tmp_event
[count
].field_name
,
4729 LTTNG_UST_ABI_SYM_NAME_LEN
);
4730 /* Mapping between these enums matches 1 to 1. */
4731 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4732 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4734 memcpy(tmp_event
[count
].event
.name
,
4736 LTTNG_UST_ABI_SYM_NAME_LEN
);
4737 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4738 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4739 tmp_event
[count
].event
.pid
= app
->pid
;
4740 tmp_event
[count
].event
.enabled
= -1;
4744 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4745 pthread_mutex_unlock(&app
->sock_lock
);
4746 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4747 ERR("Error releasing app handle for app %d with ret %d",
4755 *fields
= tmp_event
;
4757 DBG2("UST app list event fields done (%zu events)", count
);
4761 health_code_update();
4766 * Free and clean all traceable apps of the global list.
4768 void ust_app_clean_list()
4771 struct ust_app
*app
;
4772 struct lttng_ht_iter iter
;
4774 DBG2("UST app cleaning registered apps hash table");
4776 /* Cleanup notify socket hash table */
4777 if (ust_app_ht_by_notify_sock
) {
4778 lttng::urcu::read_lock_guard read_lock
;
4780 cds_lfht_for_each_entry (
4781 ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
, notify_sock_n
.node
) {
4783 * Assert that all notifiers are gone as all triggers
4784 * are unregistered prior to this clean-up.
4786 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4788 ust_app_notify_sock_unregister(app
->notify_sock
);
4793 lttng::urcu::read_lock_guard read_lock
;
4795 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4796 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4798 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4802 /* Cleanup socket hash table */
4803 if (ust_app_ht_by_sock
) {
4804 lttng::urcu::read_lock_guard read_lock
;
4806 cds_lfht_for_each_entry (ust_app_ht_by_sock
->ht
, &iter
.iter
, app
, sock_n
.node
) {
4807 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4812 /* Destroy is done only when the ht is empty */
4814 lttng_ht_destroy(ust_app_ht
);
4816 if (ust_app_ht_by_sock
) {
4817 lttng_ht_destroy(ust_app_ht_by_sock
);
4819 if (ust_app_ht_by_notify_sock
) {
4820 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4825 * Init UST app hash table.
4827 int ust_app_ht_alloc()
4829 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4833 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4834 if (!ust_app_ht_by_sock
) {
4837 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4838 if (!ust_app_ht_by_notify_sock
) {
4845 * For a specific UST session, disable the channel for all registered apps.
4847 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4850 struct lttng_ht_iter iter
;
4851 struct lttng_ht_node_str
*ua_chan_node
;
4852 struct ust_app
*app
;
4853 struct ust_app_session
*ua_sess
;
4854 struct ust_app_channel
*ua_chan
;
4856 LTTNG_ASSERT(usess
->active
);
4857 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4862 lttng::urcu::read_lock_guard read_lock
;
4864 /* For every registered applications */
4865 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4866 struct lttng_ht_iter uiter
;
4867 if (!app
->compatible
) {
4869 * TODO: In time, we should notice the caller of this error by
4870 * telling him that this is a version error.
4874 ua_sess
= lookup_session_by_app(usess
, app
);
4875 if (ua_sess
== nullptr) {
4880 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4881 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4882 /* If the session if found for the app, the channel must be there */
4883 LTTNG_ASSERT(ua_chan_node
);
4885 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4886 /* The channel must not be already disabled */
4887 LTTNG_ASSERT(ua_chan
->enabled
);
4889 /* Disable channel onto application */
4890 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4892 /* XXX: We might want to report this error at some point... */
4902 * For a specific UST session, enable the channel for all registered apps.
4904 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4907 struct lttng_ht_iter iter
;
4908 struct ust_app
*app
;
4909 struct ust_app_session
*ua_sess
;
4911 LTTNG_ASSERT(usess
->active
);
4912 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4917 lttng::urcu::read_lock_guard read_lock
;
4919 /* For every registered applications */
4920 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4921 if (!app
->compatible
) {
4923 * TODO: In time, we should notice the caller of this error by
4924 * telling him that this is a version error.
4928 ua_sess
= lookup_session_by_app(usess
, app
);
4929 if (ua_sess
== nullptr) {
4933 /* Enable channel onto application */
4934 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4936 /* XXX: We might want to report this error at some point... */
4946 * Disable an event in a channel and for a specific session.
4948 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4949 struct ltt_ust_channel
*uchan
,
4950 struct ltt_ust_event
*uevent
)
4953 struct lttng_ht_iter iter
, uiter
;
4954 struct lttng_ht_node_str
*ua_chan_node
;
4955 struct ust_app
*app
;
4956 struct ust_app_session
*ua_sess
;
4957 struct ust_app_channel
*ua_chan
;
4958 struct ust_app_event
*ua_event
;
4960 LTTNG_ASSERT(usess
->active
);
4961 DBG("UST app disabling event %s for all apps in channel "
4962 "%s for session id %" PRIu64
,
4968 lttng::urcu::read_lock_guard read_lock
;
4970 /* For all registered applications */
4971 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4972 if (!app
->compatible
) {
4974 * TODO: In time, we should notice the caller of this error by
4975 * telling him that this is a version error.
4979 ua_sess
= lookup_session_by_app(usess
, app
);
4980 if (ua_sess
== nullptr) {
4985 /* Lookup channel in the ust app session */
4986 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4987 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4988 if (ua_chan_node
== nullptr) {
4989 DBG2("Channel %s not found in session id %" PRIu64
4997 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4999 ua_event
= find_ust_app_event(
5003 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
5004 uevent
->attr
.loglevel
,
5006 if (ua_event
== nullptr) {
5007 DBG2("Event %s not found in channel %s for app pid %d."
5015 ret
= disable_ust_app_event(ua_event
, app
);
5017 /* XXX: Report error someday... */
5026 /* The ua_sess lock must be held by the caller. */
5027 static int ust_app_channel_create(struct ltt_ust_session
*usess
,
5028 struct ust_app_session
*ua_sess
,
5029 struct ltt_ust_channel
*uchan
,
5030 struct ust_app
*app
,
5031 struct ust_app_channel
**_ua_chan
)
5034 struct ust_app_channel
*ua_chan
= nullptr;
5036 LTTNG_ASSERT(ua_sess
);
5037 ASSERT_LOCKED(ua_sess
->lock
);
5039 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
))) {
5040 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
5043 struct ltt_ust_context
*uctx
= nullptr;
5046 * Create channel onto application and synchronize its
5049 ret
= ust_app_channel_allocate(
5050 ua_sess
, uchan
, LTTNG_UST_ABI_CHAN_PER_CPU
, usess
, &ua_chan
);
5055 ret
= ust_app_channel_send(app
, usess
, ua_sess
, ua_chan
);
5061 cds_list_for_each_entry (uctx
, &uchan
->ctx_list
, list
) {
5062 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5074 * The application's socket is not valid. Either a bad socket
5075 * or a timeout on it. We can't inform the caller that for a
5076 * specific app, the session failed so lets continue here.
5078 ret
= 0; /* Not an error. */
5086 if (ret
== 0 && _ua_chan
) {
5088 * Only return the application's channel on success. Note
5089 * that the channel can still be part of the application's
5090 * channel hashtable on error.
5092 *_ua_chan
= ua_chan
;
5098 * Enable event for a specific session and channel on the tracer.
5100 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
5101 struct ltt_ust_channel
*uchan
,
5102 struct ltt_ust_event
*uevent
)
5105 struct lttng_ht_iter iter
, uiter
;
5106 struct lttng_ht_node_str
*ua_chan_node
;
5107 struct ust_app
*app
;
5108 struct ust_app_session
*ua_sess
;
5109 struct ust_app_channel
*ua_chan
;
5110 struct ust_app_event
*ua_event
;
5112 LTTNG_ASSERT(usess
->active
);
5113 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
5118 * NOTE: At this point, this function is called only if the session and
5119 * channel passed are already created for all apps. and enabled on the
5124 lttng::urcu::read_lock_guard read_lock
;
5126 /* For all registered applications */
5127 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5128 if (!app
->compatible
) {
5130 * TODO: In time, we should notice the caller of this error by
5131 * telling him that this is a version error.
5135 ua_sess
= lookup_session_by_app(usess
, app
);
5137 /* The application has problem or is probably dead. */
5141 pthread_mutex_lock(&ua_sess
->lock
);
5143 if (ua_sess
->deleted
) {
5144 pthread_mutex_unlock(&ua_sess
->lock
);
5148 /* Lookup channel in the ust app session */
5149 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5150 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5152 * It is possible that the channel cannot be found is
5153 * the channel/event creation occurs concurrently with
5154 * an application exit.
5156 if (!ua_chan_node
) {
5157 pthread_mutex_unlock(&ua_sess
->lock
);
5161 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5163 /* Get event node */
5164 ua_event
= find_ust_app_event(
5168 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
5169 uevent
->attr
.loglevel
,
5171 if (ua_event
== nullptr) {
5172 DBG3("UST app enable event %s not found for app PID %d."
5179 ret
= enable_ust_app_event(ua_event
, app
);
5181 pthread_mutex_unlock(&ua_sess
->lock
);
5185 pthread_mutex_unlock(&ua_sess
->lock
);
5193 * For a specific existing UST session and UST channel, creates the event for
5194 * all registered apps.
5196 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5197 struct ltt_ust_channel
*uchan
,
5198 struct ltt_ust_event
*uevent
)
5201 struct lttng_ht_iter iter
, uiter
;
5202 struct lttng_ht_node_str
*ua_chan_node
;
5203 struct ust_app
*app
;
5204 struct ust_app_session
*ua_sess
;
5205 struct ust_app_channel
*ua_chan
;
5207 LTTNG_ASSERT(usess
->active
);
5208 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5213 lttng::urcu::read_lock_guard read_lock
;
5215 /* For all registered applications */
5216 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5217 if (!app
->compatible
) {
5219 * TODO: In time, we should notice the caller of this error by
5220 * telling him that this is a version error.
5225 ua_sess
= lookup_session_by_app(usess
, app
);
5227 /* The application has problem or is probably dead. */
5231 pthread_mutex_lock(&ua_sess
->lock
);
5233 if (ua_sess
->deleted
) {
5234 pthread_mutex_unlock(&ua_sess
->lock
);
5238 /* Lookup channel in the ust app session */
5239 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5240 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5241 /* If the channel is not found, there is a code flow error */
5242 LTTNG_ASSERT(ua_chan_node
);
5244 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5246 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5247 pthread_mutex_unlock(&ua_sess
->lock
);
5249 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5250 /* Possible value at this point: -ENOMEM. If so, we stop! */
5254 DBG2("UST app event %s already exist on app PID %d",
5266 * Start tracing for a specific UST session and app.
5268 * Called with UST app session lock held.
5271 static int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5274 struct ust_app_session
*ua_sess
;
5276 DBG("Starting tracing for ust app pid %d", app
->pid
);
5278 lttng::urcu::read_lock_guard read_lock
;
5280 if (!app
->compatible
) {
5284 ua_sess
= lookup_session_by_app(usess
, app
);
5285 if (ua_sess
== nullptr) {
5286 /* The session is in teardown process. Ignore and continue. */
5290 pthread_mutex_lock(&ua_sess
->lock
);
5292 if (ua_sess
->deleted
) {
5293 pthread_mutex_unlock(&ua_sess
->lock
);
5297 if (ua_sess
->enabled
) {
5298 pthread_mutex_unlock(&ua_sess
->lock
);
5302 /* Upon restart, we skip the setup, already done */
5303 if (ua_sess
->started
) {
5307 health_code_update();
5310 /* This starts the UST tracing */
5311 pthread_mutex_lock(&app
->sock_lock
);
5312 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5313 pthread_mutex_unlock(&app
->sock_lock
);
5315 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5316 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5319 pthread_mutex_unlock(&ua_sess
->lock
);
5321 } else if (ret
== -EAGAIN
) {
5322 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5325 pthread_mutex_unlock(&ua_sess
->lock
);
5329 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5337 /* Indicate that the session has been started once */
5338 ua_sess
->started
= true;
5339 ua_sess
->enabled
= true;
5341 pthread_mutex_unlock(&ua_sess
->lock
);
5343 health_code_update();
5345 /* Quiescent wait after starting trace */
5346 pthread_mutex_lock(&app
->sock_lock
);
5347 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5348 pthread_mutex_unlock(&app
->sock_lock
);
5350 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5351 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5354 } else if (ret
== -EAGAIN
) {
5355 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5359 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5367 health_code_update();
5371 pthread_mutex_unlock(&ua_sess
->lock
);
5372 health_code_update();
5377 * Stop tracing for a specific UST session and app.
5379 static int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5382 struct ust_app_session
*ua_sess
;
5384 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5386 lttng::urcu::read_lock_guard read_lock
;
5388 if (!app
->compatible
) {
5389 goto end_no_session
;
5392 ua_sess
= lookup_session_by_app(usess
, app
);
5393 if (ua_sess
== nullptr) {
5394 goto end_no_session
;
5397 pthread_mutex_lock(&ua_sess
->lock
);
5399 if (ua_sess
->deleted
) {
5400 pthread_mutex_unlock(&ua_sess
->lock
);
5401 goto end_no_session
;
5405 * If started = 0, it means that stop trace has been called for a session
5406 * that was never started. It's possible since we can have a fail start
5407 * from either the application manager thread or the command thread. Simply
5408 * indicate that this is a stop error.
5410 if (!ua_sess
->started
) {
5411 goto error_rcu_unlock
;
5414 health_code_update();
5416 /* This inhibits UST tracing */
5417 pthread_mutex_lock(&app
->sock_lock
);
5418 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5419 pthread_mutex_unlock(&app
->sock_lock
);
5421 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5422 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5426 } else if (ret
== -EAGAIN
) {
5427 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5433 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5438 goto error_rcu_unlock
;
5441 health_code_update();
5442 ua_sess
->enabled
= false;
5444 /* Quiescent wait after stopping trace */
5445 pthread_mutex_lock(&app
->sock_lock
);
5446 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5447 pthread_mutex_unlock(&app
->sock_lock
);
5449 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5450 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5453 } else if (ret
== -EAGAIN
) {
5454 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5458 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5465 health_code_update();
5468 auto locked_registry
= get_locked_session_registry(ua_sess
);
5470 /* The UST app session is held registry shall not be null. */
5471 LTTNG_ASSERT(locked_registry
);
5473 /* Push metadata for application before freeing the application. */
5474 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5478 pthread_mutex_unlock(&ua_sess
->lock
);
5480 health_code_update();
5484 pthread_mutex_unlock(&ua_sess
->lock
);
5485 health_code_update();
5489 static int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5491 int ret
, retval
= 0;
5492 struct lttng_ht_iter iter
;
5493 struct ust_app_channel
*ua_chan
;
5494 struct consumer_socket
*socket
;
5496 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5498 if (!app
->compatible
) {
5499 goto end_not_compatible
;
5502 pthread_mutex_lock(&ua_sess
->lock
);
5504 if (ua_sess
->deleted
) {
5508 health_code_update();
5510 /* Flushing buffers */
5511 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5513 /* Flush buffers and push metadata. */
5514 switch (ua_sess
->buffer_type
) {
5515 case LTTNG_BUFFER_PER_PID
:
5517 lttng::urcu::read_lock_guard read_lock
;
5519 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5520 health_code_update();
5521 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5523 ERR("Error flushing consumer channel");
5531 case LTTNG_BUFFER_PER_UID
:
5537 health_code_update();
5540 pthread_mutex_unlock(&ua_sess
->lock
);
5543 health_code_update();
5548 * Flush buffers for all applications for a specific UST session.
5549 * Called with UST session lock held.
5551 static int ust_app_flush_session(struct ltt_ust_session
*usess
)
5556 DBG("Flushing session buffers for all ust apps");
5558 /* Flush buffers and push metadata. */
5559 switch (usess
->buffer_type
) {
5560 case LTTNG_BUFFER_PER_UID
:
5562 struct buffer_reg_uid
*reg
;
5563 struct lttng_ht_iter iter
;
5565 /* Flush all per UID buffers associated to that session. */
5566 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5567 lttng::urcu::read_lock_guard read_lock
;
5568 lsu::registry_session
*ust_session_reg
;
5569 struct buffer_reg_channel
*buf_reg_chan
;
5570 struct consumer_socket
*socket
;
5572 /* Get consumer socket to use to push the metadata.*/
5573 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5576 /* Ignore request if no consumer is found for the session. */
5580 cds_lfht_for_each_entry (
5581 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5583 * The following call will print error values so the return
5584 * code is of little importance because whatever happens, we
5585 * have to try them all.
5587 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5590 ust_session_reg
= reg
->registry
->reg
.ust
;
5591 /* Push metadata. */
5592 auto locked_registry
= ust_session_reg
->lock();
5593 (void) push_metadata(locked_registry
, usess
->consumer
);
5598 case LTTNG_BUFFER_PER_PID
:
5600 struct ust_app_session
*ua_sess
;
5601 struct lttng_ht_iter iter
;
5602 struct ust_app
*app
;
5603 lttng::urcu::read_lock_guard read_lock
;
5605 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5606 ua_sess
= lookup_session_by_app(usess
, app
);
5607 if (ua_sess
== nullptr) {
5611 (void) ust_app_flush_app_session(app
, ua_sess
);
5622 health_code_update();
5626 static int ust_app_clear_quiescent_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5629 struct lttng_ht_iter iter
;
5630 struct ust_app_channel
*ua_chan
;
5631 struct consumer_socket
*socket
;
5633 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5635 lttng::urcu::read_lock_guard read_lock
;
5637 if (!app
->compatible
) {
5638 goto end_not_compatible
;
5641 pthread_mutex_lock(&ua_sess
->lock
);
5643 if (ua_sess
->deleted
) {
5647 health_code_update();
5649 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5651 ERR("Failed to find consumer (%" PRIu32
") socket", app
->abi
.bits_per_long
);
5656 /* Clear quiescent state. */
5657 switch (ua_sess
->buffer_type
) {
5658 case LTTNG_BUFFER_PER_PID
:
5659 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5660 health_code_update();
5661 ret
= consumer_clear_quiescent_channel(socket
, ua_chan
->key
);
5663 ERR("Error clearing quiescent state for consumer channel");
5669 case LTTNG_BUFFER_PER_UID
:
5676 health_code_update();
5679 pthread_mutex_unlock(&ua_sess
->lock
);
5682 health_code_update();
5687 * Clear quiescent state in each stream for all applications for a
5688 * specific UST session.
5689 * Called with UST session lock held.
5691 static int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5696 DBG("Clearing stream quiescent state for all ust apps");
5698 switch (usess
->buffer_type
) {
5699 case LTTNG_BUFFER_PER_UID
:
5701 struct lttng_ht_iter iter
;
5702 struct buffer_reg_uid
*reg
;
5705 * Clear quiescent for all per UID buffers associated to
5708 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5709 struct consumer_socket
*socket
;
5710 struct buffer_reg_channel
*buf_reg_chan
;
5711 lttng::urcu::read_lock_guard read_lock
;
5713 /* Get associated consumer socket.*/
5714 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5718 * Ignore request if no consumer is found for
5724 cds_lfht_for_each_entry (
5725 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5727 * The following call will print error values so
5728 * the return code is of little importance
5729 * because whatever happens, we have to try them
5732 (void) consumer_clear_quiescent_channel(socket
,
5733 buf_reg_chan
->consumer_key
);
5739 case LTTNG_BUFFER_PER_PID
:
5741 struct ust_app_session
*ua_sess
;
5742 struct lttng_ht_iter iter
;
5743 struct ust_app
*app
;
5744 lttng::urcu::read_lock_guard read_lock
;
5746 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5747 ua_sess
= lookup_session_by_app(usess
, app
);
5748 if (ua_sess
== nullptr) {
5751 (void) ust_app_clear_quiescent_app_session(app
, ua_sess
);
5762 health_code_update();
5767 * Destroy a specific UST session in apps.
5769 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5772 struct ust_app_session
*ua_sess
;
5773 struct lttng_ht_iter iter
;
5774 struct lttng_ht_node_u64
*node
;
5776 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5778 lttng::urcu::read_lock_guard read_lock
;
5780 if (!app
->compatible
) {
5784 __lookup_session_by_app(usess
, app
, &iter
);
5785 node
= lttng_ht_iter_get_node_u64(&iter
);
5786 if (node
== nullptr) {
5787 /* Session is being or is deleted. */
5790 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5792 health_code_update();
5793 destroy_app_session(app
, ua_sess
);
5795 health_code_update();
5797 /* Quiescent wait after stopping trace */
5798 pthread_mutex_lock(&app
->sock_lock
);
5799 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5800 pthread_mutex_unlock(&app
->sock_lock
);
5802 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5803 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5806 } else if (ret
== -EAGAIN
) {
5807 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5811 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5818 health_code_update();
5823 * Start tracing for the UST session.
5825 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5827 struct lttng_ht_iter iter
;
5828 struct ust_app
*app
;
5830 DBG("Starting all UST traces");
5833 * Even though the start trace might fail, flag this session active so
5834 * other application coming in are started by default.
5836 usess
->active
= true;
5839 * In a start-stop-start use-case, we need to clear the quiescent state
5840 * of each channel set by the prior stop command, thus ensuring that a
5841 * following stop or destroy is sure to grab a timestamp_end near those
5842 * operations, even if the packet is empty.
5844 (void) ust_app_clear_quiescent_session(usess
);
5847 lttng::urcu::read_lock_guard read_lock
;
5849 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5850 ust_app_global_update(usess
, app
);
5858 * Start tracing for the UST session.
5859 * Called with UST session lock held.
5861 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5864 struct lttng_ht_iter iter
;
5865 struct ust_app
*app
;
5867 DBG("Stopping all UST traces");
5870 * Even though the stop trace might fail, flag this session inactive so
5871 * other application coming in are not started by default.
5873 usess
->active
= false;
5876 lttng::urcu::read_lock_guard read_lock
;
5878 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5879 ret
= ust_app_stop_trace(usess
, app
);
5881 /* Continue to next apps even on error */
5887 (void) ust_app_flush_session(usess
);
5893 * Destroy app UST session.
5895 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5898 struct lttng_ht_iter iter
;
5899 struct ust_app
*app
;
5901 DBG("Destroy all UST traces");
5904 lttng::urcu::read_lock_guard read_lock
;
5906 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5907 ret
= destroy_trace(usess
, app
);
5909 /* Continue to next apps even on error */
5918 /* The ua_sess lock must be held by the caller. */
5919 static int find_or_create_ust_app_channel(struct ltt_ust_session
*usess
,
5920 struct ust_app_session
*ua_sess
,
5921 struct ust_app
*app
,
5922 struct ltt_ust_channel
*uchan
,
5923 struct ust_app_channel
**ua_chan
)
5926 struct lttng_ht_iter iter
;
5927 struct lttng_ht_node_str
*ua_chan_node
;
5929 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5930 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5932 *ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5936 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5944 static int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5945 struct ltt_ust_event
*uevent
,
5946 struct ust_app
*app
)
5949 struct ust_app_event
*ua_event
= nullptr;
5951 ua_event
= find_ust_app_event(ua_chan
->events
,
5954 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
5955 uevent
->attr
.loglevel
,
5958 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5963 if (ua_event
->enabled
!= uevent
->enabled
) {
5964 ret
= uevent
->enabled
? enable_ust_app_event(ua_event
, app
) :
5965 disable_ust_app_event(ua_event
, app
);
5973 /* Called with RCU read-side lock held. */
5974 static void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5977 enum lttng_error_code ret_code
;
5978 enum lttng_trigger_status t_status
;
5979 struct lttng_ht_iter app_trigger_iter
;
5980 struct lttng_triggers
*triggers
= nullptr;
5981 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5982 unsigned int count
, i
;
5984 ASSERT_RCU_READ_LOCKED();
5986 if (!ust_app_supports_notifiers(app
)) {
5991 * Currrently, registering or unregistering a trigger with an
5992 * event rule condition causes a full synchronization of the event
5995 * The first step attempts to add an event notifier for all registered
5996 * triggers that apply to the user space tracers. Then, the
5997 * application's event notifiers rules are all checked against the list
5998 * of registered triggers. Any event notifier that doesn't have a
5999 * matching trigger can be assumed to have been disabled.
6001 * All of this is inefficient, but is put in place to get the feature
6002 * rolling as it is simpler at this moment. It will be optimized Soon™
6003 * to allow the state of enabled
6004 * event notifiers to be synchronized in a piece-wise way.
6007 /* Get all triggers using uid 0 (root) */
6008 ret_code
= notification_thread_command_list_triggers(
6009 the_notification_thread_handle
, 0, &triggers
);
6010 if (ret_code
!= LTTNG_OK
) {
6014 LTTNG_ASSERT(triggers
);
6016 t_status
= lttng_triggers_get_count(triggers
, &count
);
6017 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
6021 for (i
= 0; i
< count
; i
++) {
6022 struct lttng_condition
*condition
;
6023 struct lttng_event_rule
*event_rule
;
6024 struct lttng_trigger
*trigger
;
6025 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
6026 enum lttng_condition_status condition_status
;
6029 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
6030 LTTNG_ASSERT(trigger
);
6032 token
= lttng_trigger_get_tracer_token(trigger
);
6033 condition
= lttng_trigger_get_condition(trigger
);
6035 if (lttng_condition_get_type(condition
) !=
6036 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
6037 /* Does not apply */
6041 condition_status
= lttng_condition_event_rule_matches_borrow_rule_mutable(
6042 condition
, &event_rule
);
6043 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
6045 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
6046 /* Skip kernel related triggers. */
6051 * Find or create the associated token event rule. The caller
6052 * holds the RCU read lock, so this is safe to call without
6053 * explicitly acquiring it here.
6055 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
6056 app
->token_to_event_notifier_rule_ht
, token
);
6057 if (!looked_up_event_notifier_rule
) {
6058 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
6066 lttng::urcu::read_lock_guard read_lock
;
6068 /* Remove all unknown event sources from the app. */
6069 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
6070 &app_trigger_iter
.iter
,
6071 event_notifier_rule
,
6073 const uint64_t app_token
= event_notifier_rule
->token
;
6077 * Check if the app event trigger still exists on the
6078 * notification side.
6080 for (i
= 0; i
< count
; i
++) {
6081 uint64_t notification_thread_token
;
6082 const struct lttng_trigger
*trigger
=
6083 lttng_triggers_get_at_index(triggers
, i
);
6085 LTTNG_ASSERT(trigger
);
6087 notification_thread_token
= lttng_trigger_get_tracer_token(trigger
);
6089 if (notification_thread_token
== app_token
) {
6101 * This trigger was unregistered, disable it on the tracer's
6104 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &app_trigger_iter
);
6105 LTTNG_ASSERT(ret
== 0);
6107 /* Callee logs errors. */
6108 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
6110 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
6115 lttng_triggers_destroy(triggers
);
6120 * RCU read lock must be held by the caller.
6122 static void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
6123 struct ust_app_session
*ua_sess
,
6124 struct ust_app
*app
)
6127 struct cds_lfht_iter uchan_iter
;
6128 struct ltt_ust_channel
*uchan
;
6130 LTTNG_ASSERT(usess
);
6131 LTTNG_ASSERT(ua_sess
);
6133 ASSERT_RCU_READ_LOCKED();
6135 cds_lfht_for_each_entry (usess
->domain_global
.channels
->ht
, &uchan_iter
, uchan
, node
.node
) {
6136 struct ust_app_channel
*ua_chan
;
6137 struct cds_lfht_iter uevent_iter
;
6138 struct ltt_ust_event
*uevent
;
6141 * Search for a matching ust_app_channel. If none is found,
6142 * create it. Creating the channel will cause the ua_chan
6143 * structure to be allocated, the channel buffers to be
6144 * allocated (if necessary) and sent to the application, and
6145 * all enabled contexts will be added to the channel.
6147 ret
= find_or_create_ust_app_channel(usess
, ua_sess
, app
, uchan
, &ua_chan
);
6149 /* Tracer is probably gone or ENOMEM. */
6154 /* ua_chan will be NULL for the metadata channel */
6158 cds_lfht_for_each_entry (uchan
->events
->ht
, &uevent_iter
, uevent
, node
.node
) {
6159 ret
= ust_app_channel_synchronize_event(ua_chan
, uevent
, app
);
6165 if (ua_chan
->enabled
!= uchan
->enabled
) {
6166 ret
= uchan
->enabled
? enable_ust_app_channel(ua_sess
, uchan
, app
) :
6167 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6178 * The caller must ensure that the application is compatible and is tracked
6179 * by the process attribute trackers.
6181 static void ust_app_synchronize(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6184 struct ust_app_session
*ua_sess
= nullptr;
6187 * The application's configuration should only be synchronized for
6190 LTTNG_ASSERT(usess
->active
);
6192 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, nullptr);
6194 /* Tracer is probably gone or ENOMEM. */
6198 LTTNG_ASSERT(ua_sess
);
6200 pthread_mutex_lock(&ua_sess
->lock
);
6201 if (ua_sess
->deleted
) {
6202 goto deleted_session
;
6206 lttng::urcu::read_lock_guard read_lock
;
6208 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6211 * Create the metadata for the application. This returns gracefully if a
6212 * metadata was already set for the session.
6214 * The metadata channel must be created after the data channels as the
6215 * consumer daemon assumes this ordering. When interacting with a relay
6216 * daemon, the consumer will use this assumption to send the
6217 * "STREAMS_SENT" message to the relay daemon.
6219 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6221 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6228 pthread_mutex_unlock(&ua_sess
->lock
);
6233 static void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6235 struct ust_app_session
*ua_sess
;
6237 ua_sess
= lookup_session_by_app(usess
, app
);
6238 if (ua_sess
== nullptr) {
6241 destroy_app_session(app
, ua_sess
);
6245 * Add channels/events from UST global domain to registered apps at sock.
6247 * Called with session lock held.
6248 * Called with RCU read-side lock held.
6250 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6252 LTTNG_ASSERT(usess
);
6253 LTTNG_ASSERT(usess
->active
);
6254 ASSERT_RCU_READ_LOCKED();
6256 DBG2("UST app global update for app sock %d for session id %" PRIu64
, app
->sock
, usess
->id
);
6258 if (!app
->compatible
) {
6261 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, usess
, app
->pid
) &&
6262 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
, usess
, app
->uid
) &&
6263 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
, usess
, app
->gid
)) {
6265 * Synchronize the application's internal tracing configuration
6266 * and start tracing.
6268 ust_app_synchronize(usess
, app
);
6269 ust_app_start_trace(usess
, app
);
6271 ust_app_global_destroy(usess
, app
);
6276 * Add all event notifiers to an application.
6278 * Called with session lock held.
6279 * Called with RCU read-side lock held.
6281 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6283 ASSERT_RCU_READ_LOCKED();
6285 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6289 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6293 if (app
->event_notifier_group
.object
== nullptr) {
6294 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6300 ust_app_synchronize_event_notifier_rules(app
);
6304 * Called with session lock held.
6306 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6308 struct lttng_ht_iter iter
;
6309 struct ust_app
*app
;
6312 lttng::urcu::read_lock_guard read_lock
;
6314 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6315 ust_app_global_update(usess
, app
);
6320 void ust_app_global_update_all_event_notifier_rules()
6322 struct lttng_ht_iter iter
;
6323 struct ust_app
*app
;
6325 lttng::urcu::read_lock_guard read_lock
;
6326 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6327 ust_app_global_update_event_notifier_rules(app
);
6332 * Add context to a specific channel for global UST domain.
6334 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6335 struct ltt_ust_channel
*uchan
,
6336 struct ltt_ust_context
*uctx
)
6339 struct lttng_ht_node_str
*ua_chan_node
;
6340 struct lttng_ht_iter iter
, uiter
;
6341 struct ust_app_channel
*ua_chan
= nullptr;
6342 struct ust_app_session
*ua_sess
;
6343 struct ust_app
*app
;
6345 LTTNG_ASSERT(usess
->active
);
6348 lttng::urcu::read_lock_guard read_lock
;
6349 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6350 if (!app
->compatible
) {
6352 * TODO: In time, we should notice the caller of this error by
6353 * telling him that this is a version error.
6357 ua_sess
= lookup_session_by_app(usess
, app
);
6358 if (ua_sess
== nullptr) {
6362 pthread_mutex_lock(&ua_sess
->lock
);
6364 if (ua_sess
->deleted
) {
6365 pthread_mutex_unlock(&ua_sess
->lock
);
6369 /* Lookup channel in the ust app session */
6370 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6371 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6372 if (ua_chan_node
== nullptr) {
6375 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6376 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6381 pthread_mutex_unlock(&ua_sess
->lock
);
6389 * Receive registration and populate the given msg structure.
6391 * On success return 0 else a negative value returned by the ustctl call.
6393 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6396 uint32_t pid
, ppid
, uid
, gid
;
6400 ret
= lttng_ust_ctl_recv_reg_msg(sock
,
6408 &msg
->bits_per_long
,
6409 &msg
->uint8_t_alignment
,
6410 &msg
->uint16_t_alignment
,
6411 &msg
->uint32_t_alignment
,
6412 &msg
->uint64_t_alignment
,
6413 &msg
->long_alignment
,
6420 case LTTNG_UST_ERR_EXITING
:
6421 DBG3("UST app recv reg message failed. Application died");
6423 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6424 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6427 LTTNG_UST_ABI_MAJOR_VERSION
,
6428 LTTNG_UST_ABI_MINOR_VERSION
);
6431 ERR("UST app recv reg message failed with ret %d", ret
);
6436 msg
->pid
= (pid_t
) pid
;
6437 msg
->ppid
= (pid_t
) ppid
;
6438 msg
->uid
= (uid_t
) uid
;
6439 msg
->gid
= (gid_t
) gid
;
6446 * Return a ust app session object using the application object and the
6447 * session object descriptor has a key. If not found, NULL is returned.
6448 * A RCU read side lock MUST be acquired when calling this function.
6450 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
, int objd
)
6452 struct lttng_ht_node_ulong
*node
;
6453 struct lttng_ht_iter iter
;
6454 struct ust_app_session
*ua_sess
= nullptr;
6457 ASSERT_RCU_READ_LOCKED();
6459 lttng_ht_lookup(app
->ust_sessions_objd
, (void *) ((unsigned long) objd
), &iter
);
6460 node
= lttng_ht_iter_get_node_ulong(&iter
);
6461 if (node
== nullptr) {
6462 DBG2("UST app session find by objd %d not found", objd
);
6466 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6473 * Return a ust app channel object using the application object and the channel
6474 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6475 * lock MUST be acquired before calling this function.
6477 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
, int objd
)
6479 struct lttng_ht_node_ulong
*node
;
6480 struct lttng_ht_iter iter
;
6481 struct ust_app_channel
*ua_chan
= nullptr;
6484 ASSERT_RCU_READ_LOCKED();
6486 lttng_ht_lookup(app
->ust_objd
, (void *) ((unsigned long) objd
), &iter
);
6487 node
= lttng_ht_iter_get_node_ulong(&iter
);
6488 if (node
== nullptr) {
6489 DBG2("UST app channel find by objd %d not found", objd
);
6493 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6500 * Reply to a register channel notification from an application on the notify
6501 * socket. The channel metadata is also created.
6503 * The session UST registry lock is acquired in this function.
6505 * On success 0 is returned else a negative value.
6507 static int handle_app_register_channel_notification(int sock
,
6509 struct lttng_ust_ctl_field
*raw_context_fields
,
6510 size_t context_field_count
)
6512 int ret
, ret_code
= 0;
6514 uint64_t chan_reg_key
;
6515 struct ust_app
*app
;
6516 struct ust_app_channel
*ua_chan
;
6517 struct ust_app_session
*ua_sess
;
6518 auto ust_ctl_context_fields
=
6519 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_context_fields
);
6521 lttng::urcu::read_lock_guard read_lock_guard
;
6523 /* Lookup application. If not found, there is a code flow error. */
6524 app
= find_app_by_notify_sock(sock
);
6526 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6530 /* Lookup channel by UST object descriptor. */
6531 ua_chan
= find_channel_by_objd(app
, cobjd
);
6533 DBG("Application channel is being torn down. Abort event notify");
6537 LTTNG_ASSERT(ua_chan
->session
);
6538 ua_sess
= ua_chan
->session
;
6540 /* Get right session registry depending on the session buffer type. */
6541 auto locked_registry_session
= get_locked_session_registry(ua_sess
);
6542 if (!locked_registry_session
) {
6543 DBG("Application session is being torn down. Abort event notify");
6547 /* Depending on the buffer type, a different channel key is used. */
6548 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6549 chan_reg_key
= ua_chan
->tracing_channel_id
;
6551 chan_reg_key
= ua_chan
->key
;
6554 auto& ust_reg_chan
= locked_registry_session
->channel(chan_reg_key
);
6556 /* Channel id is set during the object creation. */
6557 chan_id
= ust_reg_chan
.id
;
6560 * The application returns the typing information of the channel's
6561 * context fields. In per-PID buffering mode, this is the first and only
6562 * time we get this information. It is our chance to finalize the
6563 * initialiation of the channel and serialize it's layout's description
6564 * to the trace's metadata.
6566 * However, in per-UID buffering mode, every application will provide
6567 * this information (redundantly). The first time will allow us to
6568 * complete the initialization. The following times, we simply validate
6569 * that all apps provide the same typing for the context fields as a
6573 auto app_context_fields
= lsu::create_trace_fields_from_ust_ctl_fields(
6574 *locked_registry_session
,
6575 ust_ctl_context_fields
.get(),
6576 context_field_count
,
6577 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT
,
6578 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
);
6580 if (!ust_reg_chan
.is_registered()) {
6581 lst::type::cuptr event_context
= app_context_fields
.size() ?
6582 lttng::make_unique
<lst::structure_type
>(
6583 0, std::move(app_context_fields
)) :
6586 ust_reg_chan
.event_context(std::move(event_context
));
6589 * Validate that the context fields match between
6590 * registry and newcoming application.
6592 bool context_fields_match
;
6593 const auto *previous_event_context
= ust_reg_chan
.event_context();
6595 if (!previous_event_context
) {
6596 context_fields_match
= app_context_fields
.size() == 0;
6598 const lst::structure_type
app_event_context_struct(
6599 0, std::move(app_context_fields
));
6601 context_fields_match
= *previous_event_context
==
6602 app_event_context_struct
;
6605 if (!context_fields_match
) {
6606 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6613 } catch (const std::exception
& ex
) {
6614 ERR("Failed to handle application context: %s", ex
.what());
6620 DBG3("UST app replying to register channel key %" PRIu64
" with id %u, ret = %d",
6625 ret
= lttng_ust_ctl_reply_register_channel(
6628 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6629 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6630 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6633 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6634 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6637 } else if (ret
== -EAGAIN
) {
6638 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6642 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6651 /* This channel registry's registration is completed. */
6652 ust_reg_chan
.set_as_registered();
6658 * Add event to the UST channel registry. When the event is added to the
6659 * registry, the metadata is also created. Once done, this replies to the
6660 * application with the appropriate error code.
6662 * The session UST registry lock is acquired in the function.
6664 * On success 0 is returned else a negative value.
6666 static int add_event_ust_registry(int sock
,
6670 char *raw_signature
,
6672 struct lttng_ust_ctl_field
*raw_fields
,
6674 char *raw_model_emf_uri
)
6677 uint32_t event_id
= 0;
6678 uint64_t chan_reg_key
;
6679 struct ust_app
*app
;
6680 struct ust_app_channel
*ua_chan
;
6681 struct ust_app_session
*ua_sess
;
6682 lttng::urcu::read_lock_guard rcu_lock
;
6683 auto signature
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_signature
);
6684 auto fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_fields
);
6685 auto model_emf_uri
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_model_emf_uri
);
6687 /* Lookup application. If not found, there is a code flow error. */
6688 app
= find_app_by_notify_sock(sock
);
6690 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6694 /* Lookup channel by UST object descriptor. */
6695 ua_chan
= find_channel_by_objd(app
, cobjd
);
6697 DBG("Application channel is being torn down. Abort event notify");
6701 LTTNG_ASSERT(ua_chan
->session
);
6702 ua_sess
= ua_chan
->session
;
6704 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6705 chan_reg_key
= ua_chan
->tracing_channel_id
;
6707 chan_reg_key
= ua_chan
->key
;
6711 auto locked_registry
= get_locked_session_registry(ua_sess
);
6712 if (locked_registry
) {
6714 * From this point on, this call acquires the ownership of the signature,
6715 * fields and model_emf_uri meaning any free are done inside it if needed.
6716 * These three variables MUST NOT be read/write after this.
6719 auto& channel
= locked_registry
->channel(chan_reg_key
);
6721 /* event_id is set on success. */
6727 lsu::create_trace_fields_from_ust_ctl_fields(
6731 lst::field_location::root::EVENT_RECORD_PAYLOAD
,
6732 lsu::ctl_field_quirks::
6733 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
),
6735 model_emf_uri
.get() ?
6736 nonstd::optional
<std::string
>(model_emf_uri
.get()) :
6738 ua_sess
->buffer_type
,
6742 } catch (const std::exception
& ex
) {
6743 ERR("Failed to add event `%s` to registry session: %s",
6746 /* Inform the application of the error; don't return directly. */
6750 DBG("Application session is being torn down. Abort event notify");
6756 * The return value is returned to ustctl so in case of an error, the
6757 * application can be notified. In case of an error, it's important not to
6758 * return a negative error or else the application will get closed.
6760 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6762 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6763 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6766 } else if (ret
== -EAGAIN
) {
6767 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6771 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6777 * No need to wipe the create event since the application socket will
6778 * get close on error hence cleaning up everything by itself.
6783 DBG3("UST registry event %s with id %" PRId32
" added successfully", name
, event_id
);
6788 * Add enum to the UST session registry. Once done, this replies to the
6789 * application with the appropriate error code.
6791 * The session UST registry lock is acquired within this function.
6793 * On success 0 is returned else a negative value.
6795 static int add_enum_ust_registry(int sock
,
6798 struct lttng_ust_ctl_enum_entry
*raw_entries
,
6802 struct ust_app
*app
;
6803 struct ust_app_session
*ua_sess
;
6804 uint64_t enum_id
= -1ULL;
6805 lttng::urcu::read_lock_guard read_lock_guard
;
6806 auto entries
= lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::free
>(
6809 /* Lookup application. If not found, there is a code flow error. */
6810 app
= find_app_by_notify_sock(sock
);
6812 /* Return an error since this is not an error */
6813 DBG("Application socket %d is being torn down. Aborting enum registration", sock
);
6817 /* Lookup session by UST object descriptor. */
6818 ua_sess
= find_session_by_objd(app
, sobjd
);
6820 /* Return an error since this is not an error */
6821 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6825 auto locked_registry
= get_locked_session_registry(ua_sess
);
6826 if (!locked_registry
) {
6827 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6832 * From this point on, the callee acquires the ownership of
6833 * entries. The variable entries MUST NOT be read/written after
6836 int application_reply_code
;
6838 locked_registry
->create_or_find_enum(
6839 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6840 application_reply_code
= 0;
6841 } catch (const std::exception
& ex
) {
6844 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6849 application_reply_code
= -1;
6853 * The return value is returned to ustctl so in case of an error, the
6854 * application can be notified. In case of an error, it's important not to
6855 * return a negative error or else the application will get closed.
6857 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6859 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6860 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6863 } else if (ret
== -EAGAIN
) {
6864 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6868 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6874 * No need to wipe the create enum since the application socket will
6875 * get close on error hence cleaning up everything by itself.
6880 DBG3("UST registry enum %s added successfully or already found", name
);
6885 * Handle application notification through the given notify socket.
6887 * Return 0 on success or else a negative value.
6889 int ust_app_recv_notify(int sock
)
6892 enum lttng_ust_ctl_notify_cmd cmd
;
6894 DBG3("UST app receiving notify from sock %d", sock
);
6896 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6898 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6899 DBG3("UST app recv notify failed. Application died: sock = %d", sock
);
6900 } else if (ret
== -EAGAIN
) {
6901 WARN("UST app recv notify failed. Communication time out: sock = %d", sock
);
6903 ERR("UST app recv notify failed with ret %d: sock = %d", ret
, sock
);
6909 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6911 int sobjd
, cobjd
, loglevel_value
;
6912 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6914 struct lttng_ust_ctl_field
*fields
;
6916 DBG2("UST app ustctl register event received");
6918 ret
= lttng_ust_ctl_recv_register_event(sock
,
6928 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6929 DBG3("UST app recv event failed. Application died: sock = %d",
6931 } else if (ret
== -EAGAIN
) {
6932 WARN("UST app recv event failed. Communication time out: sock = %d",
6935 ERR("UST app recv event failed with ret %d: sock = %d", ret
, sock
);
6941 lttng::urcu::read_lock_guard rcu_lock
;
6942 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6944 DBG("Application socket %d is being torn down. Abort event notify",
6951 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6952 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6961 * Add event to the UST registry coming from the notify socket. This
6962 * call will free if needed the sig, fields and model_emf_uri. This
6963 * code path loses the ownsership of these variables and transfer them
6964 * to the this function.
6966 ret
= add_event_ust_registry(sock
,
6981 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6985 struct lttng_ust_ctl_field
*context_fields
;
6987 DBG2("UST app ustctl register channel received");
6989 ret
= lttng_ust_ctl_recv_register_channel(
6990 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6992 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6993 DBG3("UST app recv channel failed. Application died: sock = %d",
6995 } else if (ret
== -EAGAIN
) {
6996 WARN("UST app recv channel failed. Communication time out: sock = %d",
6999 ERR("UST app recv channel failed with ret %d: sock = %d",
7007 * The fields ownership are transfered to this function call meaning
7008 * that if needed it will be freed. After this, it's invalid to access
7009 * fields or clean them up.
7011 ret
= handle_app_register_channel_notification(
7012 sock
, cobjd
, context_fields
, field_count
);
7019 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
7022 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
7024 struct lttng_ust_ctl_enum_entry
*entries
;
7026 DBG2("UST app ustctl register enum received");
7028 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
, &entries
, &nr_entries
);
7030 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
7031 DBG3("UST app recv enum failed. Application died: sock = %d", sock
);
7032 } else if (ret
== -EAGAIN
) {
7033 WARN("UST app recv enum failed. Communication time out: sock = %d",
7036 ERR("UST app recv enum failed with ret %d: sock = %d", ret
, sock
);
7041 /* Callee assumes ownership of entries. */
7042 ret
= add_enum_ust_registry(sock
, sobjd
, name
, entries
, nr_entries
);
7050 /* Should NEVER happen. */
7059 * Once the notify socket hangs up, this is called. First, it tries to find the
7060 * corresponding application. On failure, the call_rcu to close the socket is
7061 * executed. If an application is found, it tries to delete it from the notify
7062 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7064 * Note that an object needs to be allocated here so on ENOMEM failure, the
7065 * call RCU is not done but the rest of the cleanup is.
7067 void ust_app_notify_sock_unregister(int sock
)
7070 struct lttng_ht_iter iter
;
7071 struct ust_app
*app
;
7072 struct ust_app_notify_sock_obj
*obj
;
7074 LTTNG_ASSERT(sock
>= 0);
7076 lttng::urcu::read_lock_guard read_lock
;
7078 obj
= zmalloc
<ust_app_notify_sock_obj
>();
7081 * An ENOMEM is kind of uncool. If this strikes we continue the
7082 * procedure but the call_rcu will not be called. In this case, we
7083 * accept the fd leak rather than possibly creating an unsynchronized
7084 * state between threads.
7086 * TODO: The notify object should be created once the notify socket is
7087 * registered and stored independantely from the ust app object. The
7088 * tricky part is to synchronize the teardown of the application and
7089 * this notify object. Let's keep that in mind so we can avoid this
7090 * kind of shenanigans with ENOMEM in the teardown path.
7097 DBG("UST app notify socket unregister %d", sock
);
7100 * Lookup application by notify socket. If this fails, this means that the
7101 * hash table delete has already been done by the application
7102 * unregistration process so we can safely close the notify socket in a
7105 app
= find_app_by_notify_sock(sock
);
7110 iter
.iter
.node
= &app
->notify_sock_n
.node
;
7113 * Whatever happens here either we fail or succeed, in both cases we have
7114 * to close the socket after a grace period to continue to the call RCU
7115 * here. If the deletion is successful, the application is not visible
7116 * anymore by other threads and is it fails it means that it was already
7117 * deleted from the hash table so either way we just have to close the
7120 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
7125 * Close socket after a grace period to avoid for the socket to be reused
7126 * before the application object is freed creating potential race between
7127 * threads trying to add unique in the global hash table.
7130 call_rcu(&obj
->head
, close_notify_sock_rcu
);
7135 * Destroy a ust app data structure and free its memory.
7137 void ust_app_destroy(struct ust_app
*app
)
7143 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
7147 * Take a snapshot for a given UST session. The snapshot is sent to the given
7150 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7152 enum lttng_error_code
ust_app_snapshot_record(const struct ltt_ust_session
*usess
,
7153 const struct consumer_output
*output
,
7154 uint64_t nb_packets_per_stream
)
7157 enum lttng_error_code status
= LTTNG_OK
;
7158 struct lttng_ht_iter iter
;
7159 struct ust_app
*app
;
7160 char *trace_path
= nullptr;
7162 LTTNG_ASSERT(usess
);
7163 LTTNG_ASSERT(output
);
7165 switch (usess
->buffer_type
) {
7166 case LTTNG_BUFFER_PER_UID
:
7168 struct buffer_reg_uid
*reg
;
7170 lttng::urcu::read_lock_guard read_lock
;
7172 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7173 struct buffer_reg_channel
*buf_reg_chan
;
7174 struct consumer_socket
*socket
;
7175 char pathname
[PATH_MAX
];
7176 size_t consumer_path_offset
= 0;
7178 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7179 /* Skip since no metadata is present */
7183 /* Get consumer socket to use to push the metadata.*/
7184 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7187 status
= LTTNG_ERR_INVALID
;
7191 memset(pathname
, 0, sizeof(pathname
));
7192 ret
= snprintf(pathname
,
7194 DEFAULT_UST_TRACE_UID_PATH
,
7196 reg
->bits_per_long
);
7198 PERROR("snprintf snapshot path");
7199 status
= LTTNG_ERR_INVALID
;
7202 /* Free path allowed on previous iteration. */
7204 trace_path
= setup_channel_trace_path(
7205 usess
->consumer
, pathname
, &consumer_path_offset
);
7207 status
= LTTNG_ERR_INVALID
;
7210 /* Add the UST default trace dir to path. */
7211 cds_lfht_for_each_entry (
7212 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7214 consumer_snapshot_channel(socket
,
7215 buf_reg_chan
->consumer_key
,
7218 &trace_path
[consumer_path_offset
],
7219 nb_packets_per_stream
);
7220 if (status
!= LTTNG_OK
) {
7224 status
= consumer_snapshot_channel(socket
,
7225 reg
->registry
->reg
.ust
->_metadata_key
,
7228 &trace_path
[consumer_path_offset
],
7230 if (status
!= LTTNG_OK
) {
7237 case LTTNG_BUFFER_PER_PID
:
7239 lttng::urcu::read_lock_guard read_lock
;
7241 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7242 struct consumer_socket
*socket
;
7243 struct lttng_ht_iter chan_iter
;
7244 struct ust_app_channel
*ua_chan
;
7245 struct ust_app_session
*ua_sess
;
7246 lsu::registry_session
*registry
;
7247 char pathname
[PATH_MAX
];
7248 size_t consumer_path_offset
= 0;
7250 ua_sess
= lookup_session_by_app(usess
, app
);
7252 /* Session not associated with this app. */
7256 /* Get the right consumer socket for the application. */
7257 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, output
);
7259 status
= LTTNG_ERR_INVALID
;
7263 /* Add the UST default trace dir to path. */
7264 memset(pathname
, 0, sizeof(pathname
));
7265 ret
= snprintf(pathname
, sizeof(pathname
), "%s", ua_sess
->path
);
7267 status
= LTTNG_ERR_INVALID
;
7268 PERROR("snprintf snapshot path");
7271 /* Free path allowed on previous iteration. */
7273 trace_path
= setup_channel_trace_path(
7274 usess
->consumer
, pathname
, &consumer_path_offset
);
7276 status
= LTTNG_ERR_INVALID
;
7279 cds_lfht_for_each_entry (
7280 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7282 consumer_snapshot_channel(socket
,
7286 &trace_path
[consumer_path_offset
],
7287 nb_packets_per_stream
);
7291 case LTTNG_ERR_CHAN_NOT_FOUND
:
7298 registry
= get_session_registry(ua_sess
);
7300 DBG("Application session is being torn down. Skip application.");
7303 status
= consumer_snapshot_channel(socket
,
7304 registry
->_metadata_key
,
7307 &trace_path
[consumer_path_offset
],
7312 case LTTNG_ERR_CHAN_NOT_FOUND
:
7331 * Return the size taken by one more packet per stream.
7333 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session
*usess
,
7334 uint64_t cur_nr_packets
)
7336 uint64_t tot_size
= 0;
7337 struct ust_app
*app
;
7338 struct lttng_ht_iter iter
;
7340 LTTNG_ASSERT(usess
);
7342 switch (usess
->buffer_type
) {
7343 case LTTNG_BUFFER_PER_UID
:
7345 struct buffer_reg_uid
*reg
;
7347 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7348 struct buffer_reg_channel
*buf_reg_chan
;
7350 lttng::urcu::read_lock_guard read_lock
;
7352 cds_lfht_for_each_entry (
7353 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7354 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7356 * Don't take channel into account if we
7357 * already grab all its packets.
7361 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7366 case LTTNG_BUFFER_PER_PID
:
7368 lttng::urcu::read_lock_guard read_lock
;
7370 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7371 struct ust_app_channel
*ua_chan
;
7372 struct ust_app_session
*ua_sess
;
7373 struct lttng_ht_iter chan_iter
;
7375 ua_sess
= lookup_session_by_app(usess
, app
);
7377 /* Session not associated with this app. */
7381 cds_lfht_for_each_entry (
7382 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7383 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7385 * Don't take channel into account if we
7386 * already grab all its packets.
7390 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7403 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7404 struct cds_list_head
*buffer_reg_uid_list
,
7405 struct consumer_output
*consumer
,
7408 uint64_t *discarded
,
7412 uint64_t consumer_chan_key
;
7417 ret
= buffer_reg_uid_consumer_channel_key(
7418 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7426 ret
= consumer_get_lost_packets(ust_session_id
, consumer_chan_key
, consumer
, lost
);
7428 ret
= consumer_get_discarded_events(
7429 ust_session_id
, consumer_chan_key
, consumer
, discarded
);
7436 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7437 struct ltt_ust_channel
*uchan
,
7438 struct consumer_output
*consumer
,
7440 uint64_t *discarded
,
7444 struct lttng_ht_iter iter
;
7445 struct lttng_ht_node_str
*ua_chan_node
;
7446 struct ust_app
*app
;
7447 struct ust_app_session
*ua_sess
;
7448 struct ust_app_channel
*ua_chan
;
7454 * Iterate over every registered applications. Sum counters for
7455 * all applications containing requested session and channel.
7457 lttng::urcu::read_lock_guard read_lock
;
7459 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7460 struct lttng_ht_iter uiter
;
7462 ua_sess
= lookup_session_by_app(usess
, app
);
7463 if (ua_sess
== nullptr) {
7468 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7469 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7470 /* If the session is found for the app, the channel must be there */
7471 LTTNG_ASSERT(ua_chan_node
);
7473 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7478 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
, consumer
, &_lost
);
7484 uint64_t _discarded
;
7486 ret
= consumer_get_discarded_events(
7487 usess
->id
, ua_chan
->key
, consumer
, &_discarded
);
7491 (*discarded
) += _discarded
;
7498 static int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7501 struct ust_app_session
*ua_sess
;
7503 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7505 lttng::urcu::read_lock_guard read_lock
;
7507 ua_sess
= lookup_session_by_app(usess
, app
);
7508 if (ua_sess
== nullptr) {
7509 /* The session is in teardown process. Ignore and continue. */
7513 pthread_mutex_lock(&ua_sess
->lock
);
7515 if (ua_sess
->deleted
) {
7519 pthread_mutex_lock(&app
->sock_lock
);
7520 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7521 pthread_mutex_unlock(&app
->sock_lock
);
7524 pthread_mutex_unlock(&ua_sess
->lock
);
7527 health_code_update();
7532 * Regenerate the statedump for each app in the session.
7534 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7537 struct lttng_ht_iter iter
;
7538 struct ust_app
*app
;
7540 DBG("Regenerating the metadata for all UST apps");
7542 lttng::urcu::read_lock_guard read_lock
;
7544 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7545 if (!app
->compatible
) {
7549 ret
= ust_app_regenerate_statedump(usess
, app
);
7551 /* Continue to the next app even on error */
7560 * Rotate all the channels of a session.
7562 * Return LTTNG_OK on success or else an LTTng error code.
7564 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7567 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7568 struct lttng_ht_iter iter
;
7569 struct ust_app
*app
;
7570 struct ltt_ust_session
*usess
= session
->ust_session
;
7572 LTTNG_ASSERT(usess
);
7574 switch (usess
->buffer_type
) {
7575 case LTTNG_BUFFER_PER_UID
:
7577 struct buffer_reg_uid
*reg
;
7579 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7580 struct buffer_reg_channel
*buf_reg_chan
;
7581 struct consumer_socket
*socket
;
7582 lttng::urcu::read_lock_guard read_lock
;
7584 /* Get consumer socket to use to push the metadata.*/
7585 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7588 cmd_ret
= LTTNG_ERR_INVALID
;
7592 /* Rotate the data channels. */
7593 cds_lfht_for_each_entry (
7594 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7595 ret
= consumer_rotate_channel(socket
,
7596 buf_reg_chan
->consumer_key
,
7598 /* is_metadata_channel */ false);
7600 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7606 * The metadata channel might not be present.
7608 * Consumer stream allocation can be done
7609 * asynchronously and can fail on intermediary
7610 * operations (i.e add context) and lead to data
7611 * channels created with no metadata channel.
7613 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7614 /* Skip since no metadata is present. */
7619 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7620 (void) push_metadata(locked_registry
, usess
->consumer
);
7623 ret
= consumer_rotate_channel(socket
,
7624 reg
->registry
->reg
.ust
->_metadata_key
,
7626 /* is_metadata_channel */ true);
7628 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7634 case LTTNG_BUFFER_PER_PID
:
7636 lttng::urcu::read_lock_guard read_lock
;
7638 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7639 struct consumer_socket
*socket
;
7640 struct lttng_ht_iter chan_iter
;
7641 struct ust_app_channel
*ua_chan
;
7642 struct ust_app_session
*ua_sess
;
7643 lsu::registry_session
*registry
;
7645 ua_sess
= lookup_session_by_app(usess
, app
);
7647 /* Session not associated with this app. */
7651 /* Get the right consumer socket for the application. */
7652 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7655 cmd_ret
= LTTNG_ERR_INVALID
;
7659 registry
= get_session_registry(ua_sess
);
7661 DBG("Application session is being torn down. Skip application.");
7665 /* Rotate the data channels. */
7666 cds_lfht_for_each_entry (
7667 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7668 ret
= consumer_rotate_channel(socket
,
7671 /* is_metadata_channel */ false);
7673 /* Per-PID buffer and application going away. */
7674 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7676 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7681 /* Rotate the metadata channel. */
7683 auto locked_registry
= registry
->lock();
7685 (void) push_metadata(locked_registry
, usess
->consumer
);
7687 ret
= consumer_rotate_channel(socket
,
7688 registry
->_metadata_key
,
7690 /* is_metadata_channel */ true);
7692 /* Per-PID buffer and application going away. */
7693 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7695 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7712 enum lttng_error_code
ust_app_create_channel_subdirectories(const struct ltt_ust_session
*usess
)
7714 enum lttng_error_code ret
= LTTNG_OK
;
7715 struct lttng_ht_iter iter
;
7716 enum lttng_trace_chunk_status chunk_status
;
7717 char *pathname_index
;
7720 LTTNG_ASSERT(usess
->current_trace_chunk
);
7722 switch (usess
->buffer_type
) {
7723 case LTTNG_BUFFER_PER_UID
:
7725 struct buffer_reg_uid
*reg
;
7726 lttng::urcu::read_lock_guard read_lock
;
7728 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7729 fmt_ret
= asprintf(&pathname_index
,
7730 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
7731 "/" DEFAULT_INDEX_DIR
,
7733 reg
->bits_per_long
);
7735 ERR("Failed to format channel index directory");
7736 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7741 * Create the index subdirectory which will take care
7742 * of implicitly creating the channel's path.
7744 chunk_status
= lttng_trace_chunk_create_subdirectory(
7745 usess
->current_trace_chunk
, pathname_index
);
7746 free(pathname_index
);
7747 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7748 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7754 case LTTNG_BUFFER_PER_PID
:
7756 struct ust_app
*app
;
7757 lttng::urcu::read_lock_guard read_lock
;
7760 * Create the toplevel ust/ directory in case no apps are running.
7762 chunk_status
= lttng_trace_chunk_create_subdirectory(usess
->current_trace_chunk
,
7763 DEFAULT_UST_TRACE_DIR
);
7764 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7765 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7769 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7770 struct ust_app_session
*ua_sess
;
7771 lsu::registry_session
*registry
;
7773 ua_sess
= lookup_session_by_app(usess
, app
);
7775 /* Session not associated with this app. */
7779 registry
= get_session_registry(ua_sess
);
7781 DBG("Application session is being torn down. Skip application.");
7785 fmt_ret
= asprintf(&pathname_index
,
7786 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7789 ERR("Failed to format channel index directory");
7790 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7794 * Create the index subdirectory which will take care
7795 * of implicitly creating the channel's path.
7797 chunk_status
= lttng_trace_chunk_create_subdirectory(
7798 usess
->current_trace_chunk
, pathname_index
);
7799 free(pathname_index
);
7800 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7801 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7817 * Clear all the channels of a session.
7819 * Return LTTNG_OK on success or else an LTTng error code.
7821 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7824 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7825 struct lttng_ht_iter iter
;
7826 struct ust_app
*app
;
7827 struct ltt_ust_session
*usess
= session
->ust_session
;
7829 LTTNG_ASSERT(usess
);
7831 if (usess
->active
) {
7832 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7833 cmd_ret
= LTTNG_ERR_FATAL
;
7837 switch (usess
->buffer_type
) {
7838 case LTTNG_BUFFER_PER_UID
:
7840 struct buffer_reg_uid
*reg
;
7841 lttng::urcu::read_lock_guard read_lock
;
7843 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7844 struct buffer_reg_channel
*buf_reg_chan
;
7845 struct consumer_socket
*socket
;
7847 /* Get consumer socket to use to push the metadata.*/
7848 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7851 cmd_ret
= LTTNG_ERR_INVALID
;
7855 /* Clear the data channels. */
7856 cds_lfht_for_each_entry (
7857 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7858 ret
= consumer_clear_channel(socket
, buf_reg_chan
->consumer_key
);
7865 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7866 (void) push_metadata(locked_registry
, usess
->consumer
);
7870 * Clear the metadata channel.
7871 * Metadata channel is not cleared per se but we still need to
7872 * perform a rotation operation on it behind the scene.
7874 ret
= consumer_clear_channel(socket
, reg
->registry
->reg
.ust
->_metadata_key
);
7881 case LTTNG_BUFFER_PER_PID
:
7883 lttng::urcu::read_lock_guard read_lock
;
7885 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7886 struct consumer_socket
*socket
;
7887 struct lttng_ht_iter chan_iter
;
7888 struct ust_app_channel
*ua_chan
;
7889 struct ust_app_session
*ua_sess
;
7890 lsu::registry_session
*registry
;
7892 ua_sess
= lookup_session_by_app(usess
, app
);
7894 /* Session not associated with this app. */
7898 /* Get the right consumer socket for the application. */
7899 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7902 cmd_ret
= LTTNG_ERR_INVALID
;
7906 registry
= get_session_registry(ua_sess
);
7908 DBG("Application session is being torn down. Skip application.");
7912 /* Clear the data channels. */
7913 cds_lfht_for_each_entry (
7914 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7915 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7917 /* Per-PID buffer and application going away. */
7918 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7926 auto locked_registry
= registry
->lock();
7927 (void) push_metadata(locked_registry
, usess
->consumer
);
7931 * Clear the metadata channel.
7932 * Metadata channel is not cleared per se but we still need to
7933 * perform rotation operation on it behind the scene.
7935 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7937 /* Per-PID buffer and application going away. */
7938 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7956 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7957 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7960 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7969 * This function skips the metadata channel as the begin/end timestamps of a
7970 * metadata packet are useless.
7972 * Moreover, opening a packet after a "clear" will cause problems for live
7973 * sessions as it will introduce padding that was not part of the first trace
7974 * chunk. The relay daemon expects the content of the metadata stream of
7975 * successive metadata trace chunks to be strict supersets of one another.
7977 * For example, flushing a packet at the beginning of the metadata stream of
7978 * a trace chunk resulting from a "clear" session command will cause the
7979 * size of the metadata stream of the new trace chunk to not match the size of
7980 * the metadata stream of the original chunk. This will confuse the relay
7981 * daemon as the same "offset" in a metadata stream will no longer point
7982 * to the same content.
7984 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7986 enum lttng_error_code ret
= LTTNG_OK
;
7987 struct lttng_ht_iter iter
;
7988 struct ltt_ust_session
*usess
= session
->ust_session
;
7990 LTTNG_ASSERT(usess
);
7992 switch (usess
->buffer_type
) {
7993 case LTTNG_BUFFER_PER_UID
:
7995 struct buffer_reg_uid
*reg
;
7997 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7998 struct buffer_reg_channel
*buf_reg_chan
;
7999 struct consumer_socket
*socket
;
8000 lttng::urcu::read_lock_guard read_lock
;
8002 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
8005 ret
= LTTNG_ERR_FATAL
;
8009 cds_lfht_for_each_entry (
8010 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
8011 const int open_ret
= consumer_open_channel_packets(
8012 socket
, buf_reg_chan
->consumer_key
);
8015 ret
= LTTNG_ERR_UNK
;
8022 case LTTNG_BUFFER_PER_PID
:
8024 struct ust_app
*app
;
8025 lttng::urcu::read_lock_guard read_lock
;
8027 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8028 struct consumer_socket
*socket
;
8029 struct lttng_ht_iter chan_iter
;
8030 struct ust_app_channel
*ua_chan
;
8031 struct ust_app_session
*ua_sess
;
8032 lsu::registry_session
*registry
;
8034 ua_sess
= lookup_session_by_app(usess
, app
);
8036 /* Session not associated with this app. */
8040 /* Get the right consumer socket for the application. */
8041 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
8044 ret
= LTTNG_ERR_FATAL
;
8048 registry
= get_session_registry(ua_sess
);
8050 DBG("Application session is being torn down. Skip application.");
8054 cds_lfht_for_each_entry (
8055 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
8056 const int open_ret
=
8057 consumer_open_channel_packets(socket
, ua_chan
->key
);
8061 * Per-PID buffer and application going
8064 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
8068 ret
= LTTNG_ERR_UNK
;
8084 lsu::ctl_field_quirks
ust_app::ctl_field_quirks() const
8087 * Application contexts are expressed as variants. LTTng-UST announces
8088 * those by registering an enumeration named `..._tag`. It then registers a
8089 * variant as part of the event context that contains the various possible
8092 * Unfortunately, the names used in the enumeration and variant don't
8093 * match: the enumeration names are all prefixed with an underscore while
8094 * the variant type tag fields aren't.
8096 * While the CTF 1.8.3 specification mentions that
8097 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8098 * 1.x (and possibly others) expect a perfect match between the names used
8099 * by tags and variants.
8101 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8102 * the variant's fields are modified to match the mappings of its tag.
8104 * From ABI version >= 10.x, the variant fields and tag mapping names
8105 * correctly match, making this quirk unnecessary.
8107 return v_major
<= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
:
8108 lsu::ctl_field_quirks::NONE
;