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
;
183 int ev_loglevel_value
;
188 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
189 key
= (ust_app_ht_key
*) _key
;
190 ev_loglevel_value
= event
->attr
.loglevel
;
192 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
195 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
199 /* Event loglevel. */
200 if (ev_loglevel_value
!= key
->loglevel_type
) {
201 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
&&
202 key
->loglevel_type
== 0 && ev_loglevel_value
== -1) {
204 * Match is accepted. This is because on event creation, the
205 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
206 * -1 are accepted for this loglevel type since 0 is the one set by
207 * the API when receiving an enable event.
214 /* One of the filters is NULL, fail. */
215 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
219 if (key
->filter
&& event
->filter
) {
220 /* Both filters exists, check length followed by the bytecode. */
221 if (event
->filter
->len
!= key
->filter
->len
||
222 memcmp(event
->filter
->data
, key
->filter
->data
, event
->filter
->len
) != 0) {
227 /* One of the exclusions is NULL, fail. */
228 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
232 if (key
->exclusion
&& event
->exclusion
) {
233 /* Both exclusions exists, check count followed by the names. */
234 if (event
->exclusion
->count
!= key
->exclusion
->count
||
235 memcmp(event
->exclusion
->names
,
236 key
->exclusion
->names
,
237 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
250 * Unique add of an ust app event in the given ht. This uses the custom
251 * ht_match_ust_app_event match function and the event name as hash.
253 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
, struct ust_app_event
*event
)
255 struct cds_lfht_node
*node_ptr
;
256 struct ust_app_ht_key key
;
259 LTTNG_ASSERT(ua_chan
);
260 LTTNG_ASSERT(ua_chan
->events
);
263 ht
= ua_chan
->events
;
264 key
.name
= event
->attr
.name
;
265 key
.filter
= event
->filter
;
266 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel
;
267 key
.exclusion
= event
->exclusion
;
269 node_ptr
= cds_lfht_add_unique(ht
->ht
,
270 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
271 ht_match_ust_app_event
,
274 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
278 * Close the notify socket from the given RCU head object. This MUST be called
279 * through a call_rcu().
281 static void close_notify_sock_rcu(struct rcu_head
*head
)
284 struct ust_app_notify_sock_obj
*obj
=
285 lttng::utils::container_of(head
, &ust_app_notify_sock_obj::head
);
287 /* Must have a valid fd here. */
288 LTTNG_ASSERT(obj
->fd
>= 0);
290 ret
= close(obj
->fd
);
292 ERR("close notify sock %d RCU", obj
->fd
);
294 lttng_fd_put(LTTNG_FD_APPS
, 1);
300 * Delete ust context safely. RCU read lock must be held before calling
303 static void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
307 LTTNG_ASSERT(ua_ctx
);
308 ASSERT_RCU_READ_LOCKED();
311 pthread_mutex_lock(&app
->sock_lock
);
312 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
313 pthread_mutex_unlock(&app
->sock_lock
);
315 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
316 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
319 } else if (ret
== -EAGAIN
) {
320 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
324 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
337 * Delete ust app event safely. RCU read lock must be held before calling
340 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
, struct ust_app
*app
)
344 LTTNG_ASSERT(ua_event
);
345 ASSERT_RCU_READ_LOCKED();
347 free(ua_event
->filter
);
348 if (ua_event
->exclusion
!= nullptr)
349 free(ua_event
->exclusion
);
350 if (ua_event
->obj
!= nullptr) {
351 pthread_mutex_lock(&app
->sock_lock
);
352 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
353 pthread_mutex_unlock(&app
->sock_lock
);
355 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
356 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
359 } else if (ret
== -EAGAIN
) {
360 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
364 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
376 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
377 * through a call_rcu().
379 static void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
381 struct ust_app_event_notifier_rule
*obj
=
382 lttng::utils::container_of(head
, &ust_app_event_notifier_rule::rcu_head
);
388 * Delete ust app event notifier rule safely.
390 static void delete_ust_app_event_notifier_rule(
391 int sock
, struct ust_app_event_notifier_rule
*ua_event_notifier_rule
, struct ust_app
*app
)
395 LTTNG_ASSERT(ua_event_notifier_rule
);
397 if (ua_event_notifier_rule
->exclusion
!= nullptr) {
398 free(ua_event_notifier_rule
->exclusion
);
401 if (ua_event_notifier_rule
->obj
!= nullptr) {
402 pthread_mutex_lock(&app
->sock_lock
);
403 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
404 pthread_mutex_unlock(&app
->sock_lock
);
406 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
407 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
410 } else if (ret
== -EAGAIN
) {
411 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
415 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
422 free(ua_event_notifier_rule
->obj
);
425 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
426 call_rcu(&ua_event_notifier_rule
->rcu_head
, free_ust_app_event_notifier_rule_rcu
);
430 * Release ust data object of the given stream.
432 * Return 0 on success or else a negative value.
434 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
438 LTTNG_ASSERT(stream
);
441 pthread_mutex_lock(&app
->sock_lock
);
442 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
443 pthread_mutex_unlock(&app
->sock_lock
);
445 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
446 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
449 } else if (ret
== -EAGAIN
) {
450 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
454 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
460 lttng_fd_put(LTTNG_FD_APPS
, 2);
468 * Delete ust app stream safely. RCU read lock must be held before calling
471 static void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
473 LTTNG_ASSERT(stream
);
474 ASSERT_RCU_READ_LOCKED();
476 (void) release_ust_app_stream(sock
, stream
, app
);
480 static void delete_ust_app_channel_rcu(struct rcu_head
*head
)
482 struct ust_app_channel
*ua_chan
=
483 lttng::utils::container_of(head
, &ust_app_channel::rcu_head
);
485 lttng_ht_destroy(ua_chan
->ctx
);
486 lttng_ht_destroy(ua_chan
->events
);
491 * Extract the lost packet or discarded events counter when the channel is
492 * being deleted and store the value in the parent channel so we can
493 * access it from lttng list and at stop/destroy.
495 * The session list lock must be held by the caller.
497 static void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
499 uint64_t discarded
= 0, lost
= 0;
500 struct ltt_session
*session
;
501 struct ltt_ust_channel
*uchan
;
503 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
507 lttng::urcu::read_lock_guard read_lock
;
508 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
509 if (!session
|| !session
->ust_session
) {
511 * Not finding the session is not an error because there are
512 * multiple ways the channels can be torn down.
514 * 1) The session daemon can initiate the destruction of the
515 * ust app session after receiving a destroy command or
516 * during its shutdown/teardown.
517 * 2) The application, since we are in per-pid tracing, is
518 * unregistering and tearing down its ust app session.
520 * Both paths are protected by the session list lock which
521 * ensures that the accounting of lost packets and discarded
522 * events is done exactly once. The session is then unpublished
523 * from the session list, resulting in this condition.
528 if (ua_chan
->attr
.overwrite
) {
529 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
531 session
->ust_session
->consumer
,
534 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
536 session
->ust_session
->consumer
,
539 uchan
= trace_ust_find_channel_by_name(session
->ust_session
->domain_global
.channels
,
542 ERR("Missing UST channel to store discarded counters");
546 uchan
->per_pid_closed_app_discarded
+= discarded
;
547 uchan
->per_pid_closed_app_lost
+= lost
;
551 session_put(session
);
556 * Delete ust app channel safely. RCU read lock must be held before calling
559 * The session list lock must be held by the caller.
561 static void delete_ust_app_channel(int sock
,
562 struct ust_app_channel
*ua_chan
,
564 const lsu::registry_session::locked_ptr
& locked_registry
)
567 struct lttng_ht_iter iter
;
568 struct ust_app_event
*ua_event
;
569 struct ust_app_ctx
*ua_ctx
;
570 struct ust_app_stream
*stream
, *stmp
;
572 LTTNG_ASSERT(ua_chan
);
573 ASSERT_RCU_READ_LOCKED();
575 DBG3("UST app deleting channel %s", ua_chan
->name
);
578 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
579 cds_list_del(&stream
->list
);
580 delete_ust_app_stream(sock
, stream
, app
);
584 cds_lfht_for_each_entry (ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
585 cds_list_del(&ua_ctx
->list
);
586 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
588 delete_ust_app_ctx(sock
, ua_ctx
, app
);
592 cds_lfht_for_each_entry (ua_chan
->events
->ht
, &iter
.iter
, ua_event
, node
.node
) {
593 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
595 delete_ust_app_event(sock
, ua_event
, app
);
598 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
599 /* Wipe and free registry from session registry. */
600 if (locked_registry
) {
602 locked_registry
->remove_channel(ua_chan
->key
, sock
>= 0);
603 } catch (const std::exception
& ex
) {
604 DBG("Could not find channel for removal: %s", ex
.what());
609 * A negative socket can be used by the caller when
610 * cleaning-up a ua_chan in an error path. Skip the
611 * accounting in this case.
614 save_per_pid_lost_discarded_counters(ua_chan
);
618 if (ua_chan
->obj
!= nullptr) {
619 /* Remove channel from application UST object descriptor. */
620 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
621 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
623 pthread_mutex_lock(&app
->sock_lock
);
624 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
625 pthread_mutex_unlock(&app
->sock_lock
);
627 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
628 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
632 } else if (ret
== -EAGAIN
) {
633 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
638 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
645 lttng_fd_put(LTTNG_FD_APPS
, 1);
648 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
651 int ust_app_register_done(struct ust_app
*app
)
655 pthread_mutex_lock(&app
->sock_lock
);
656 ret
= lttng_ust_ctl_register_done(app
->sock
);
657 pthread_mutex_unlock(&app
->sock_lock
);
661 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
666 pthread_mutex_lock(&app
->sock_lock
);
671 ret
= lttng_ust_ctl_release_object(sock
, data
);
673 pthread_mutex_unlock(&app
->sock_lock
);
679 * Push metadata to consumer socket.
681 * RCU read-side lock must be held to guarantee existence of socket.
682 * Must be called with the ust app session lock held.
683 * Must be called with the registry lock held.
685 * On success, return the len of metadata pushed or else a negative value.
686 * Returning a -EPIPE return value means we could not send the metadata,
687 * but it can be caused by recoverable errors (e.g. the application has
688 * terminated concurrently).
690 ssize_t
ust_app_push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
691 struct consumer_socket
*socket
,
695 char *metadata_str
= nullptr;
696 size_t len
, offset
, new_metadata_len_sent
;
698 uint64_t metadata_key
, metadata_version
;
700 LTTNG_ASSERT(locked_registry
);
701 LTTNG_ASSERT(socket
);
702 ASSERT_RCU_READ_LOCKED();
704 metadata_key
= locked_registry
->_metadata_key
;
707 * Means that no metadata was assigned to the session. This can
708 * happens if no start has been done previously.
714 offset
= locked_registry
->_metadata_len_sent
;
715 len
= locked_registry
->_metadata_len
- locked_registry
->_metadata_len_sent
;
716 new_metadata_len_sent
= locked_registry
->_metadata_len
;
717 metadata_version
= locked_registry
->_metadata_version
;
719 DBG3("No metadata to push for metadata key %" PRIu64
,
720 locked_registry
->_metadata_key
);
722 if (send_zero_data
) {
723 DBG("No metadata to push");
729 /* Allocate only what we have to send. */
730 metadata_str
= calloc
<char>(len
);
732 PERROR("zmalloc ust app metadata string");
736 /* Copy what we haven't sent out. */
737 memcpy(metadata_str
, locked_registry
->_metadata
+ offset
, len
);
740 pthread_mutex_unlock(&locked_registry
->_lock
);
742 * We need to unlock the registry while we push metadata to
743 * break a circular dependency between the consumerd metadata
744 * lock and the sessiond registry lock. Indeed, pushing metadata
745 * to the consumerd awaits that it gets pushed all the way to
746 * relayd, but doing so requires grabbing the metadata lock. If
747 * a concurrent metadata request is being performed by
748 * consumerd, this can try to grab the registry lock on the
749 * sessiond while holding the metadata lock on the consumer
750 * daemon. Those push and pull schemes are performed on two
751 * different bidirectionnal communication sockets.
753 ret
= consumer_push_metadata(
754 socket
, metadata_key
, metadata_str
, len
, offset
, metadata_version
);
755 pthread_mutex_lock(&locked_registry
->_lock
);
758 * There is an acceptable race here between the registry
759 * metadata key assignment and the creation on the
760 * consumer. The session daemon can concurrently push
761 * metadata for this registry while being created on the
762 * consumer since the metadata key of the registry is
763 * assigned *before* it is setup to avoid the consumer
764 * to ask for metadata that could possibly be not found
765 * in the session daemon.
767 * The metadata will get pushed either by the session
768 * being stopped or the consumer requesting metadata if
769 * that race is triggered.
771 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
774 ERR("Error pushing metadata to consumer");
780 * Metadata may have been concurrently pushed, since
781 * we're not holding the registry lock while pushing to
782 * consumer. This is handled by the fact that we send
783 * the metadata content, size, and the offset at which
784 * that metadata belongs. This may arrive out of order
785 * on the consumer side, and the consumer is able to
786 * deal with overlapping fragments. The consumer
787 * supports overlapping fragments, which must be
788 * contiguous starting from offset 0. We keep the
789 * largest metadata_len_sent value of the concurrent
792 locked_registry
->_metadata_len_sent
=
793 std::max(locked_registry
->_metadata_len_sent
, new_metadata_len_sent
);
802 * On error, flag the registry that the metadata is
803 * closed. We were unable to push anything and this
804 * means that either the consumer is not responding or
805 * the metadata cache has been destroyed on the
808 locked_registry
->_metadata_closed
= true;
816 * For a given application and session, push metadata to consumer.
817 * Either sock or consumer is required : if sock is NULL, the default
818 * socket to send the metadata is retrieved from consumer, if sock
819 * is not NULL we use it to send the metadata.
820 * RCU read-side lock must be held while calling this function,
821 * therefore ensuring existence of registry. It also ensures existence
822 * of socket throughout this function.
824 * Return 0 on success else a negative error.
825 * Returning a -EPIPE return value means we could not send the metadata,
826 * but it can be caused by recoverable errors (e.g. the application has
827 * terminated concurrently).
829 static int push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
830 struct consumer_output
*consumer
)
834 struct consumer_socket
*socket
;
836 LTTNG_ASSERT(locked_registry
);
837 LTTNG_ASSERT(consumer
);
838 ASSERT_RCU_READ_LOCKED();
840 if (locked_registry
->_metadata_closed
) {
845 /* Get consumer socket to use to push the metadata.*/
846 socket
= consumer_find_socket_by_bitness(locked_registry
->abi
.bits_per_long
, consumer
);
852 ret
= ust_app_push_metadata(locked_registry
, socket
, 0);
864 * Send to the consumer a close metadata command for the given session. Once
865 * done, the metadata channel is deleted and the session metadata pointer is
866 * nullified. The session lock MUST be held unless the application is
867 * in the destroy path.
869 * Do not hold the registry lock while communicating with the consumerd, because
870 * doing so causes inter-process deadlocks between consumerd and sessiond with
871 * the metadata request notification.
873 * Return 0 on success else a negative value.
875 static int close_metadata(uint64_t metadata_key
,
876 unsigned int consumer_bitness
,
877 struct consumer_output
*consumer
)
880 struct consumer_socket
*socket
;
881 lttng::urcu::read_lock_guard read_lock_guard
;
883 LTTNG_ASSERT(consumer
);
885 /* Get consumer socket to use to push the metadata. */
886 socket
= consumer_find_socket_by_bitness(consumer_bitness
, consumer
);
892 ret
= consumer_close_metadata(socket
, metadata_key
);
901 static void delete_ust_app_session_rcu(struct rcu_head
*head
)
903 struct ust_app_session
*ua_sess
=
904 lttng::utils::container_of(head
, &ust_app_session::rcu_head
);
906 lttng_ht_destroy(ua_sess
->channels
);
911 * Delete ust app session safely. RCU read lock must be held before calling
914 * The session list lock must be held by the caller.
916 static void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
, struct ust_app
*app
)
919 struct lttng_ht_iter iter
;
920 struct ust_app_channel
*ua_chan
;
922 LTTNG_ASSERT(ua_sess
);
923 ASSERT_RCU_READ_LOCKED();
925 pthread_mutex_lock(&ua_sess
->lock
);
927 LTTNG_ASSERT(!ua_sess
->deleted
);
928 ua_sess
->deleted
= true;
930 auto locked_registry
= get_locked_session_registry(ua_sess
);
931 /* Registry can be null on error path during initialization. */
932 if (locked_registry
) {
933 /* Push metadata for application before freeing the application. */
934 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
937 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
938 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
940 delete_ust_app_channel(sock
, ua_chan
, app
, locked_registry
);
943 if (locked_registry
) {
945 * Don't ask to close metadata for global per UID buffers. Close
946 * metadata only on destroy trace session in this case. Also, the
947 * previous push metadata could have flag the metadata registry to
948 * close so don't send a close command if closed.
950 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
951 const auto metadata_key
= locked_registry
->_metadata_key
;
952 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
954 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
955 locked_registry
->_metadata_closed
= true;
958 /* Release lock before communication, see comments in close_metadata(). */
959 locked_registry
.reset();
960 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
964 /* In case of per PID, the registry is kept in the session. */
965 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
966 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
969 * Registry can be null on error path during
972 buffer_reg_pid_remove(reg_pid
);
973 buffer_reg_pid_destroy(reg_pid
);
977 if (ua_sess
->handle
!= -1) {
978 pthread_mutex_lock(&app
->sock_lock
);
979 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
980 pthread_mutex_unlock(&app
->sock_lock
);
982 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
983 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
986 } else if (ret
== -EAGAIN
) {
987 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
991 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
998 /* Remove session from application UST object descriptor. */
999 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
1000 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
1004 pthread_mutex_unlock(&ua_sess
->lock
);
1006 consumer_output_put(ua_sess
->consumer
);
1008 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1012 * Delete a traceable application structure from the global list. Never call
1013 * this function outside of a call_rcu call.
1015 static void delete_ust_app(struct ust_app
*app
)
1018 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1019 struct lttng_ht_iter iter
;
1020 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1021 bool event_notifier_write_fd_is_open
;
1024 * The session list lock must be held during this function to guarantee
1025 * the existence of ua_sess.
1027 session_lock_list();
1028 /* Delete ust app sessions info */
1033 cds_list_for_each_entry_safe (ua_sess
, tmp_ua_sess
, &app
->teardown_head
, teardown_node
) {
1034 /* Free every object in the session and the session. */
1035 lttng::urcu::read_lock_guard read_lock
;
1036 delete_ust_app_session(sock
, ua_sess
, app
);
1039 /* Remove the event notifier rules associated with this app. */
1041 lttng::urcu::read_lock_guard read_lock
;
1043 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1045 event_notifier_rule
,
1047 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1050 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
1054 lttng_ht_destroy(app
->sessions
);
1055 lttng_ht_destroy(app
->ust_sessions_objd
);
1056 lttng_ht_destroy(app
->ust_objd
);
1057 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1060 * This could be NULL if the event notifier setup failed (e.g the app
1061 * was killed or the tracer does not support this feature).
1063 if (app
->event_notifier_group
.object
) {
1064 enum lttng_error_code ret_code
;
1065 enum event_notifier_error_accounting_status status
;
1067 const int event_notifier_read_fd
=
1068 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
);
1070 ret_code
= notification_thread_command_remove_tracer_event_source(
1071 the_notification_thread_handle
, event_notifier_read_fd
);
1072 if (ret_code
!= LTTNG_OK
) {
1073 ERR("Failed to remove application tracer event source from notification thread");
1076 status
= event_notifier_error_accounting_unregister_app(app
);
1077 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1078 ERR("Error unregistering app from event notifier error accounting");
1081 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1082 free(app
->event_notifier_group
.object
);
1085 event_notifier_write_fd_is_open
=
1086 lttng_pipe_is_write_open(app
->event_notifier_group
.event_pipe
);
1087 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1089 * Release the file descriptors reserved for the event notifier pipe.
1090 * The app could be destroyed before the write end of the pipe could be
1091 * passed to the application (and closed). In that case, both file
1092 * descriptors must be released.
1094 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1097 * Wait until we have deleted the application from the sock hash table
1098 * before closing this socket, otherwise an application could re-use the
1099 * socket ID and race with the teardown, using the same hash table entry.
1101 * It's OK to leave the close in call_rcu. We want it to stay unique for
1102 * all RCU readers that could run concurrently with unregister app,
1103 * therefore we _need_ to only close that socket after a grace period. So
1104 * it should stay in this RCU callback.
1106 * This close() is a very important step of the synchronization model so
1107 * every modification to this function must be carefully reviewed.
1113 lttng_fd_put(LTTNG_FD_APPS
, 1);
1115 DBG2("UST app pid %d deleted", app
->pid
);
1117 session_unlock_list();
1121 * URCU intermediate call to delete an UST app.
1123 static void delete_ust_app_rcu(struct rcu_head
*head
)
1125 struct lttng_ht_node_ulong
*node
=
1126 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1127 struct ust_app
*app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
1129 DBG3("Call RCU deleting app PID %d", app
->pid
);
1130 delete_ust_app(app
);
1134 * Delete the session from the application ht and delete the data structure by
1135 * freeing every object inside and releasing them.
1137 * The session list lock must be held by the caller.
1139 static void destroy_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
1142 struct lttng_ht_iter iter
;
1145 LTTNG_ASSERT(ua_sess
);
1147 iter
.iter
.node
= &ua_sess
->node
.node
;
1148 ret
= lttng_ht_del(app
->sessions
, &iter
);
1150 /* Already scheduled for teardown. */
1154 /* Once deleted, free the data structure. */
1155 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1162 * Alloc new UST app session.
1164 static struct ust_app_session
*alloc_ust_app_session()
1166 struct ust_app_session
*ua_sess
;
1168 /* Init most of the default value by allocating and zeroing */
1169 ua_sess
= zmalloc
<ust_app_session
>();
1170 if (ua_sess
== nullptr) {
1175 ua_sess
->handle
= -1;
1176 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1177 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1178 pthread_mutex_init(&ua_sess
->lock
, nullptr);
1187 * Alloc new UST app channel.
1189 static struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1190 struct ust_app_session
*ua_sess
,
1191 struct lttng_ust_abi_channel_attr
*attr
)
1193 struct ust_app_channel
*ua_chan
;
1195 /* Init most of the default value by allocating and zeroing */
1196 ua_chan
= zmalloc
<ust_app_channel
>();
1197 if (ua_chan
== nullptr) {
1202 /* Setup channel name */
1203 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1204 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1206 ua_chan
->enabled
= true;
1207 ua_chan
->handle
= -1;
1208 ua_chan
->session
= ua_sess
;
1209 ua_chan
->key
= get_next_channel_key();
1210 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1211 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1212 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1214 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1215 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1217 /* Copy attributes */
1219 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1220 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1221 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1222 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1223 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1224 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1225 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1226 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1228 /* By default, the channel is a per cpu channel. */
1229 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1231 DBG3("UST app channel %s allocated", ua_chan
->name
);
1240 * Allocate and initialize a UST app stream.
1242 * Return newly allocated stream pointer or NULL on error.
1244 struct ust_app_stream
*ust_app_alloc_stream()
1246 struct ust_app_stream
*stream
= nullptr;
1248 stream
= zmalloc
<ust_app_stream
>();
1249 if (stream
== nullptr) {
1250 PERROR("zmalloc ust app stream");
1254 /* Zero could be a valid value for a handle so flag it to -1. */
1255 stream
->handle
= -1;
1262 * Alloc new UST app event.
1264 static struct ust_app_event
*alloc_ust_app_event(char *name
, struct lttng_ust_abi_event
*attr
)
1266 struct ust_app_event
*ua_event
;
1268 /* Init most of the default value by allocating and zeroing */
1269 ua_event
= zmalloc
<ust_app_event
>();
1270 if (ua_event
== nullptr) {
1271 PERROR("Failed to allocate ust_app_event structure");
1275 ua_event
->enabled
= true;
1276 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1277 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1278 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1280 /* Copy attributes */
1282 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1285 DBG3("UST app event %s allocated", ua_event
->name
);
1294 * Allocate a new UST app event notifier rule.
1296 static struct ust_app_event_notifier_rule
*
1297 alloc_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
)
1299 enum lttng_event_rule_generate_exclusions_status generate_exclusion_status
;
1300 enum lttng_condition_status cond_status
;
1301 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1302 struct lttng_condition
*condition
= nullptr;
1303 const struct lttng_event_rule
*event_rule
= nullptr;
1305 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1306 if (ua_event_notifier_rule
== nullptr) {
1307 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1311 ua_event_notifier_rule
->enabled
= true;
1312 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1313 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, ua_event_notifier_rule
->token
);
1315 condition
= lttng_trigger_get_condition(trigger
);
1316 LTTNG_ASSERT(condition
);
1317 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1318 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1320 cond_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
1321 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1322 LTTNG_ASSERT(event_rule
);
1324 ua_event_notifier_rule
->error_counter_index
=
1325 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1326 /* Acquire the event notifier's reference to the trigger. */
1327 lttng_trigger_get(trigger
);
1329 ua_event_notifier_rule
->trigger
= trigger
;
1330 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1331 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1332 event_rule
, &ua_event_notifier_rule
->exclusion
);
1333 switch (generate_exclusion_status
) {
1334 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1335 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1338 /* Error occurred. */
1339 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1340 goto error_put_trigger
;
1343 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1344 ua_event_notifier_rule
->token
);
1346 return ua_event_notifier_rule
;
1349 lttng_trigger_put(trigger
);
1351 free(ua_event_notifier_rule
);
1356 * Alloc new UST app context.
1358 static struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1360 struct ust_app_ctx
*ua_ctx
;
1362 ua_ctx
= zmalloc
<ust_app_ctx
>();
1363 if (ua_ctx
== nullptr) {
1367 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1370 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1371 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1372 char *provider_name
= nullptr, *ctx_name
= nullptr;
1374 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1375 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1376 if (!provider_name
|| !ctx_name
) {
1377 free(provider_name
);
1382 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1383 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1387 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1395 * Create a liblttng-ust filter bytecode from given bytecode.
1397 * Return allocated filter or NULL on error.
1399 static struct lttng_ust_abi_filter_bytecode
*
1400 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1402 struct lttng_ust_abi_filter_bytecode
*filter
= nullptr;
1404 /* Copy filter bytecode. */
1405 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1407 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
1413 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) == sizeof(struct lttng_ust_abi_filter_bytecode
));
1414 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1420 * Create a liblttng-ust capture bytecode from given bytecode.
1422 * Return allocated filter or NULL on error.
1424 static struct lttng_ust_abi_capture_bytecode
*
1425 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1427 struct lttng_ust_abi_capture_bytecode
*capture
= nullptr;
1429 /* Copy capture bytecode. */
1430 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1432 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
1438 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1439 sizeof(struct lttng_ust_abi_capture_bytecode
));
1440 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1446 * Find an ust_app using the sock and return it. RCU read side lock must be
1447 * held before calling this helper function.
1449 struct ust_app
*ust_app_find_by_sock(int sock
)
1451 struct lttng_ht_node_ulong
*node
;
1452 struct lttng_ht_iter iter
;
1454 ASSERT_RCU_READ_LOCKED();
1456 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
1457 node
= lttng_ht_iter_get_node_ulong(&iter
);
1458 if (node
== nullptr) {
1459 DBG2("UST app find by sock %d not found", sock
);
1463 return lttng::utils::container_of(node
, &ust_app::sock_n
);
1470 * Find an ust_app using the notify sock and return it. RCU read side lock must
1471 * be held before calling this helper function.
1473 static struct ust_app
*find_app_by_notify_sock(int sock
)
1475 struct lttng_ht_node_ulong
*node
;
1476 struct lttng_ht_iter iter
;
1478 ASSERT_RCU_READ_LOCKED();
1480 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *) ((unsigned long) sock
), &iter
);
1481 node
= lttng_ht_iter_get_node_ulong(&iter
);
1482 if (node
== nullptr) {
1483 DBG2("UST app find by notify sock %d not found", sock
);
1487 return lttng::utils::container_of(node
, &ust_app::notify_sock_n
);
1494 * Lookup for an ust app event based on event name, filter bytecode and the
1497 * Return an ust_app_event object or NULL on error.
1499 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1501 const struct lttng_bytecode
*filter
,
1503 const struct lttng_event_exclusion
*exclusion
)
1505 struct lttng_ht_iter iter
;
1506 struct lttng_ht_node_str
*node
;
1507 struct ust_app_event
*event
= nullptr;
1508 struct ust_app_ht_key key
;
1513 /* Setup key for event lookup. */
1515 key
.filter
= filter
;
1516 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) loglevel_value
;
1517 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1518 key
.exclusion
= exclusion
;
1520 /* Lookup using the event name as hash and a custom match fct. */
1521 cds_lfht_lookup(ht
->ht
,
1522 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1523 ht_match_ust_app_event
,
1526 node
= lttng_ht_iter_get_node_str(&iter
);
1527 if (node
== nullptr) {
1531 event
= lttng::utils::container_of(node
, &ust_app_event::node
);
1538 * Look-up an event notifier rule based on its token id.
1540 * Must be called with the RCU read lock held.
1541 * Return an ust_app_event_notifier_rule object or NULL on error.
1543 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(struct lttng_ht
*ht
,
1546 struct lttng_ht_iter iter
;
1547 struct lttng_ht_node_u64
*node
;
1548 struct ust_app_event_notifier_rule
*event_notifier_rule
= nullptr;
1551 ASSERT_RCU_READ_LOCKED();
1553 lttng_ht_lookup(ht
, &token
, &iter
);
1554 node
= lttng_ht_iter_get_node_u64(&iter
);
1555 if (node
== nullptr) {
1556 DBG2("UST app event notifier rule token not found: token = %" PRIu64
, token
);
1560 event_notifier_rule
= lttng::utils::container_of(node
, &ust_app_event_notifier_rule::node
);
1562 return event_notifier_rule
;
1566 * Create the channel context on the tracer.
1568 * Called with UST app session lock held.
1570 static int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1571 struct ust_app_ctx
*ua_ctx
,
1572 struct ust_app
*app
)
1576 health_code_update();
1578 pthread_mutex_lock(&app
->sock_lock
);
1579 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
, ua_chan
->obj
, &ua_ctx
->obj
);
1580 pthread_mutex_unlock(&app
->sock_lock
);
1582 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1584 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1587 } else if (ret
== -EAGAIN
) {
1589 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1593 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1601 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1603 DBG2("UST app context handle %d created successfully for channel %s",
1608 health_code_update();
1613 * Set the filter on the tracer.
1615 static int set_ust_object_filter(struct ust_app
*app
,
1616 const struct lttng_bytecode
*bytecode
,
1617 struct lttng_ust_abi_object_data
*ust_object
)
1620 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= nullptr;
1622 health_code_update();
1624 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1625 if (!ust_bytecode
) {
1626 ret
= -LTTNG_ERR_NOMEM
;
1629 pthread_mutex_lock(&app
->sock_lock
);
1630 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
, ust_object
);
1631 pthread_mutex_unlock(&app
->sock_lock
);
1633 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1635 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1638 } else if (ret
== -EAGAIN
) {
1640 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1644 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1653 DBG2("UST filter successfully set: object = %p", ust_object
);
1656 health_code_update();
1662 * Set a capture bytecode for the passed object.
1663 * The sequence number enforces the ordering at runtime and on reception of
1664 * the captured payloads.
1666 static int set_ust_capture(struct ust_app
*app
,
1667 const struct lttng_bytecode
*bytecode
,
1668 unsigned int capture_seqnum
,
1669 struct lttng_ust_abi_object_data
*ust_object
)
1672 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= nullptr;
1674 health_code_update();
1676 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1677 if (!ust_bytecode
) {
1678 ret
= -LTTNG_ERR_NOMEM
;
1683 * Set the sequence number to ensure the capture of fields is ordered.
1685 ust_bytecode
->seqnum
= capture_seqnum
;
1687 pthread_mutex_lock(&app
->sock_lock
);
1688 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
, ust_object
);
1689 pthread_mutex_unlock(&app
->sock_lock
);
1691 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1693 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1696 } else if (ret
== -EAGAIN
) {
1698 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1702 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1711 DBG2("UST capture successfully set: object = %p", ust_object
);
1714 health_code_update();
1719 static struct lttng_ust_abi_event_exclusion
*
1720 create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion
*exclusion
)
1722 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= nullptr;
1723 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1724 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1726 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1727 if (!ust_exclusion
) {
1732 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1733 sizeof(struct lttng_ust_abi_event_exclusion
));
1734 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1736 return ust_exclusion
;
1740 * Set event exclusions on the tracer.
1742 static int set_ust_object_exclusions(struct ust_app
*app
,
1743 const struct lttng_event_exclusion
*exclusions
,
1744 struct lttng_ust_abi_object_data
*ust_object
)
1747 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= nullptr;
1749 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1751 health_code_update();
1753 ust_exclusions
= create_ust_exclusion_from_exclusion(exclusions
);
1754 if (!ust_exclusions
) {
1755 ret
= -LTTNG_ERR_NOMEM
;
1758 pthread_mutex_lock(&app
->sock_lock
);
1759 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1760 pthread_mutex_unlock(&app
->sock_lock
);
1762 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1764 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1767 } else if (ret
== -EAGAIN
) {
1769 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1773 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1782 DBG2("UST exclusions set successfully for object %p", ust_object
);
1785 health_code_update();
1786 free(ust_exclusions
);
1791 * Disable the specified event on to UST tracer for the UST session.
1793 static int disable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*object
)
1797 health_code_update();
1799 pthread_mutex_lock(&app
->sock_lock
);
1800 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1801 pthread_mutex_unlock(&app
->sock_lock
);
1803 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1805 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1808 } else if (ret
== -EAGAIN
) {
1810 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1814 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1823 DBG2("UST app object %p disabled successfully for app: pid = %d", object
, app
->pid
);
1826 health_code_update();
1831 * Disable the specified channel on to UST tracer for the UST session.
1833 static int disable_ust_channel(struct ust_app
*app
,
1834 struct ust_app_session
*ua_sess
,
1835 struct ust_app_channel
*ua_chan
)
1839 health_code_update();
1841 pthread_mutex_lock(&app
->sock_lock
);
1842 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1843 pthread_mutex_unlock(&app
->sock_lock
);
1845 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1847 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1850 } else if (ret
== -EAGAIN
) {
1852 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1856 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1866 DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1869 health_code_update();
1874 * Enable the specified channel on to UST tracer for the UST session.
1876 static int enable_ust_channel(struct ust_app
*app
,
1877 struct ust_app_session
*ua_sess
,
1878 struct ust_app_channel
*ua_chan
)
1882 health_code_update();
1884 pthread_mutex_lock(&app
->sock_lock
);
1885 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1886 pthread_mutex_unlock(&app
->sock_lock
);
1888 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1890 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1894 } else if (ret
== -EAGAIN
) {
1896 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1901 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1911 ua_chan
->enabled
= true;
1913 DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1916 health_code_update();
1921 * Enable the specified event on to UST tracer for the UST session.
1923 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1927 health_code_update();
1929 pthread_mutex_lock(&app
->sock_lock
);
1930 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1931 pthread_mutex_unlock(&app
->sock_lock
);
1933 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1935 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1938 } else if (ret
== -EAGAIN
) {
1940 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1944 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1953 DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object
, app
->pid
);
1956 health_code_update();
1961 * Send channel and stream buffer to application.
1963 * Return 0 on success. On error, a negative value is returned.
1965 static int send_channel_pid_to_ust(struct ust_app
*app
,
1966 struct ust_app_session
*ua_sess
,
1967 struct ust_app_channel
*ua_chan
)
1970 struct ust_app_stream
*stream
, *stmp
;
1973 LTTNG_ASSERT(ua_sess
);
1974 LTTNG_ASSERT(ua_chan
);
1976 health_code_update();
1978 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
, app
->sock
);
1980 /* Send channel to the application. */
1981 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1982 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1983 ret
= -ENOTCONN
; /* Caused by app exiting. */
1985 } else if (ret
== -EAGAIN
) {
1986 /* Caused by timeout. */
1987 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
1991 ua_sess
->tracing_id
);
1992 /* Treat this the same way as an application that is exiting. */
1995 } else if (ret
< 0) {
1999 health_code_update();
2001 /* Send all streams to application. */
2002 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2003 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
2004 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2005 ret
= -ENOTCONN
; /* Caused by app exiting. */
2007 } else if (ret
== -EAGAIN
) {
2008 /* Caused by timeout. */
2009 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
2014 ua_sess
->tracing_id
);
2016 * Treat this the same way as an application that is
2020 } else if (ret
< 0) {
2023 /* We don't need the stream anymore once sent to the tracer. */
2024 cds_list_del(&stream
->list
);
2025 delete_ust_app_stream(-1, stream
, app
);
2029 health_code_update();
2034 * Create the specified event onto the UST tracer for a UST session.
2036 * Should be called with session mutex held.
2038 static int create_ust_event(struct ust_app
*app
,
2039 struct ust_app_channel
*ua_chan
,
2040 struct ust_app_event
*ua_event
)
2044 health_code_update();
2046 /* Create UST event on tracer */
2047 pthread_mutex_lock(&app
->sock_lock
);
2048 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
, &ua_event
->obj
);
2049 pthread_mutex_unlock(&app
->sock_lock
);
2051 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2053 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2056 } else if (ret
== -EAGAIN
) {
2058 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2062 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2063 ua_event
->attr
.name
,
2071 ua_event
->handle
= ua_event
->obj
->handle
;
2073 DBG2("UST app event %s created successfully for pid:%d object = %p",
2074 ua_event
->attr
.name
,
2078 health_code_update();
2080 /* Set filter if one is present. */
2081 if (ua_event
->filter
) {
2082 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2088 /* Set exclusions for the event */
2089 if (ua_event
->exclusion
) {
2090 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2096 /* If event not enabled, disable it on the tracer */
2097 if (ua_event
->enabled
) {
2099 * We now need to explicitly enable the event, since it
2100 * is now disabled at creation.
2102 ret
= enable_ust_object(app
, ua_event
->obj
);
2105 * If we hit an EPERM, something is wrong with our enable call. If
2106 * we get an EEXIST, there is a problem on the tracer side since we
2110 case -LTTNG_UST_ERR_PERM
:
2111 /* Code flow problem */
2113 case -LTTNG_UST_ERR_EXIST
:
2114 /* It's OK for our use case. */
2125 health_code_update();
2130 init_ust_event_notifier_from_event_rule(const struct lttng_event_rule
*rule
,
2131 struct lttng_ust_abi_event_notifier
*event_notifier
)
2133 enum lttng_event_rule_status status
;
2134 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2135 int loglevel
= -1, ret
= 0;
2136 const char *pattern
;
2138 memset(event_notifier
, 0, sizeof(*event_notifier
));
2140 if (lttng_event_rule_targets_agent_domain(rule
)) {
2142 * Special event for agents
2143 * The actual meat of the event is in the filter that will be
2144 * attached later on.
2145 * Set the default values for the agent event.
2147 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
2149 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2151 const struct lttng_log_level_rule
*log_level_rule
;
2153 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2154 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2156 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2157 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2158 /* At this point, this is a fatal error. */
2162 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(rule
, &log_level_rule
);
2163 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2164 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2165 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2166 enum lttng_log_level_rule_status llr_status
;
2168 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2169 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2170 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2171 llr_status
= lttng_log_level_rule_exactly_get_level(log_level_rule
,
2174 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2175 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2176 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2177 log_level_rule
, &loglevel
);
2183 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2185 /* At this point this is a fatal error. */
2190 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2191 ret
= lttng_strncpy(
2192 event_notifier
->event
.name
, pattern
, sizeof(event_notifier
->event
.name
));
2194 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern
);
2198 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2199 event_notifier
->event
.loglevel
= loglevel
;
2205 * Create the specified event notifier against the user space tracer of a
2206 * given application.
2208 static int create_ust_event_notifier(struct ust_app
*app
,
2209 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2212 enum lttng_condition_status condition_status
;
2213 const struct lttng_condition
*condition
= nullptr;
2214 struct lttng_ust_abi_event_notifier event_notifier
;
2215 const struct lttng_event_rule
*event_rule
= nullptr;
2216 unsigned int capture_bytecode_count
= 0, i
;
2217 enum lttng_condition_status cond_status
;
2218 enum lttng_event_rule_type event_rule_type
;
2220 health_code_update();
2221 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2223 condition
= lttng_trigger_get_const_condition(ua_event_notifier_rule
->trigger
);
2224 LTTNG_ASSERT(condition
);
2225 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2226 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2228 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2229 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2231 LTTNG_ASSERT(event_rule
);
2233 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2234 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2235 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2236 event_rule_type
== LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2237 event_rule_type
== LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2239 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2240 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2241 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2243 /* Create UST event notifier against the tracer. */
2244 pthread_mutex_lock(&app
->sock_lock
);
2245 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
,
2247 app
->event_notifier_group
.object
,
2248 &ua_event_notifier_rule
->obj
);
2249 pthread_mutex_unlock(&app
->sock_lock
);
2251 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2253 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2256 } else if (ret
== -EAGAIN
) {
2258 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2262 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2263 event_notifier
.event
.name
,
2271 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2273 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2274 event_notifier
.event
.name
,
2277 ua_event_notifier_rule
->obj
);
2279 health_code_update();
2281 /* Set filter if one is present. */
2282 if (ua_event_notifier_rule
->filter
) {
2283 ret
= set_ust_object_filter(
2284 app
, ua_event_notifier_rule
->filter
, ua_event_notifier_rule
->obj
);
2290 /* Set exclusions for the event. */
2291 if (ua_event_notifier_rule
->exclusion
) {
2292 ret
= set_ust_object_exclusions(
2293 app
, ua_event_notifier_rule
->exclusion
, ua_event_notifier_rule
->obj
);
2299 /* Set the capture bytecodes. */
2300 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2301 condition
, &capture_bytecode_count
);
2302 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2304 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2305 const struct lttng_bytecode
*capture_bytecode
=
2306 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2309 ret
= set_ust_capture(app
, capture_bytecode
, i
, ua_event_notifier_rule
->obj
);
2316 * We now need to explicitly enable the event, since it
2317 * is disabled at creation.
2319 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2322 * If we hit an EPERM, something is wrong with our enable call.
2323 * If we get an EEXIST, there is a problem on the tracer side
2324 * since we just created it.
2327 case -LTTNG_UST_ERR_PERM
:
2328 /* Code flow problem. */
2330 case -LTTNG_UST_ERR_EXIST
:
2331 /* It's OK for our use case. */
2341 ua_event_notifier_rule
->enabled
= true;
2344 health_code_update();
2349 * Copy data between an UST app event and a LTT event.
2351 static void shadow_copy_event(struct ust_app_event
*ua_event
, struct ltt_ust_event
*uevent
)
2353 size_t exclusion_alloc_size
;
2355 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2356 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2358 ua_event
->enabled
= uevent
->enabled
;
2360 /* Copy event attributes */
2361 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2363 /* Copy filter bytecode */
2364 if (uevent
->filter
) {
2365 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2366 /* Filter might be NULL here in case of ENONEM. */
2369 /* Copy exclusion data */
2370 if (uevent
->exclusion
) {
2371 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2372 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2373 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2374 if (ua_event
->exclusion
== nullptr) {
2377 memcpy(ua_event
->exclusion
, uevent
->exclusion
, exclusion_alloc_size
);
2383 * Copy data between an UST app channel and a LTT channel.
2385 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
, struct ltt_ust_channel
*uchan
)
2387 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2389 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2390 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2392 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2393 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2395 /* Copy event attributes since the layout is different. */
2396 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2397 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2398 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2399 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2400 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2401 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2402 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2403 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2406 * Note that the attribute channel type is not set since the channel on the
2407 * tracing registry side does not have this information.
2410 ua_chan
->enabled
= uchan
->enabled
;
2411 ua_chan
->tracing_channel_id
= uchan
->id
;
2413 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2417 * Copy data between a UST app session and a regular LTT session.
2419 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2420 struct ltt_ust_session
*usess
,
2421 struct ust_app
*app
)
2423 struct tm
*timeinfo
;
2426 char tmp_shm_path
[PATH_MAX
];
2428 timeinfo
= localtime(&app
->registration_time
);
2429 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2431 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2433 ua_sess
->tracing_id
= usess
->id
;
2434 ua_sess
->id
= get_next_session_id();
2435 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2436 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2437 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2438 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2439 ua_sess
->buffer_type
= usess
->buffer_type
;
2440 ua_sess
->bits_per_long
= app
->abi
.bits_per_long
;
2442 /* There is only one consumer object per session possible. */
2443 consumer_output_get(usess
->consumer
);
2444 ua_sess
->consumer
= usess
->consumer
;
2446 ua_sess
->output_traces
= usess
->output_traces
;
2447 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2448 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &usess
->metadata_attr
);
2450 switch (ua_sess
->buffer_type
) {
2451 case LTTNG_BUFFER_PER_PID
:
2452 ret
= snprintf(ua_sess
->path
,
2453 sizeof(ua_sess
->path
),
2454 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2459 case LTTNG_BUFFER_PER_UID
:
2460 ret
= snprintf(ua_sess
->path
,
2461 sizeof(ua_sess
->path
),
2462 DEFAULT_UST_TRACE_UID_PATH
,
2463 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2464 app
->abi
.bits_per_long
);
2471 PERROR("asprintf UST shadow copy session");
2476 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
, sizeof(ua_sess
->root_shm_path
));
2477 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2478 strncpy(ua_sess
->shm_path
, usess
->shm_path
, sizeof(ua_sess
->shm_path
));
2479 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2480 if (ua_sess
->shm_path
[0]) {
2481 switch (ua_sess
->buffer_type
) {
2482 case LTTNG_BUFFER_PER_PID
:
2483 ret
= snprintf(tmp_shm_path
,
2484 sizeof(tmp_shm_path
),
2485 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2490 case LTTNG_BUFFER_PER_UID
:
2491 ret
= snprintf(tmp_shm_path
,
2492 sizeof(tmp_shm_path
),
2493 "/" DEFAULT_UST_TRACE_UID_PATH
,
2495 app
->abi
.bits_per_long
);
2502 PERROR("sprintf UST shadow copy session");
2506 strncat(ua_sess
->shm_path
,
2508 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2509 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2514 consumer_output_put(ua_sess
->consumer
);
2518 * Lookup sesison wrapper.
2520 static void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2521 struct ust_app
*app
,
2522 struct lttng_ht_iter
*iter
)
2524 /* Get right UST app session from app */
2525 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2529 * Return ust app session from the app session hashtable using the UST session
2532 static struct ust_app_session
*lookup_session_by_app(const struct ltt_ust_session
*usess
,
2533 struct ust_app
*app
)
2535 struct lttng_ht_iter iter
;
2536 struct lttng_ht_node_u64
*node
;
2538 __lookup_session_by_app(usess
, app
, &iter
);
2539 node
= lttng_ht_iter_get_node_u64(&iter
);
2540 if (node
== nullptr) {
2544 return lttng::utils::container_of(node
, &ust_app_session::node
);
2551 * Setup buffer registry per PID for the given session and application. If none
2552 * is found, a new one is created, added to the global registry and
2553 * initialized. If regp is valid, it's set with the newly created object.
2555 * Return 0 on success or else a negative value.
2557 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2558 struct ust_app
*app
,
2559 struct buffer_reg_pid
**regp
)
2562 struct buffer_reg_pid
*reg_pid
;
2564 LTTNG_ASSERT(ua_sess
);
2567 lttng::urcu::read_lock_guard read_lock
;
2569 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2572 * This is the create channel path meaning that if there is NO
2573 * registry available, we have to create one for this session.
2575 ret
= buffer_reg_pid_create(
2576 ua_sess
->id
, ®_pid
, ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2584 /* Initialize registry. */
2585 reg_pid
->registry
->reg
.ust
= ust_registry_session_per_pid_create(
2590 reg_pid
->root_shm_path
,
2592 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2593 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2594 ua_sess
->tracing_id
);
2595 if (!reg_pid
->registry
->reg
.ust
) {
2597 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2598 * destroy the buffer registry, because it is always expected
2599 * that if the buffer registry can be found, its ust registry is
2602 buffer_reg_pid_destroy(reg_pid
);
2606 buffer_reg_pid_add(reg_pid
);
2608 DBG3("UST app buffer registry per PID created successfully");
2619 * Setup buffer registry per UID for the given session and application. If none
2620 * is found, a new one is created, added to the global registry and
2621 * initialized. If regp is valid, it's set with the newly created object.
2623 * Return 0 on success or else a negative value.
2625 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2626 struct ust_app_session
*ua_sess
,
2627 struct ust_app
*app
,
2628 struct buffer_reg_uid
**regp
)
2631 struct buffer_reg_uid
*reg_uid
;
2633 LTTNG_ASSERT(usess
);
2636 lttng::urcu::read_lock_guard read_lock
;
2638 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
2641 * This is the create channel path meaning that if there is NO
2642 * registry available, we have to create one for this session.
2644 ret
= buffer_reg_uid_create(usess
->id
,
2645 app
->abi
.bits_per_long
,
2649 ua_sess
->root_shm_path
,
2658 /* Initialize registry. */
2659 reg_uid
->registry
->reg
.ust
= ust_registry_session_per_uid_create(app
->abi
,
2662 reg_uid
->root_shm_path
,
2666 ua_sess
->tracing_id
,
2668 if (!reg_uid
->registry
->reg
.ust
) {
2670 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2671 * destroy the buffer registry, because it is always expected
2672 * that if the buffer registry can be found, its ust registry is
2675 buffer_reg_uid_destroy(reg_uid
, nullptr);
2679 /* Add node to teardown list of the session. */
2680 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2682 buffer_reg_uid_add(reg_uid
);
2684 DBG3("UST app buffer registry per UID created successfully");
2694 * Create a session on the tracer side for the given app.
2696 * On success, ua_sess_ptr is populated with the session pointer or else left
2697 * untouched. If the session was created, is_created is set to 1. On error,
2698 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2701 * Returns 0 on success or else a negative code which is either -ENOMEM or
2702 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2704 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2705 struct ust_app
*app
,
2706 struct ust_app_session
**ua_sess_ptr
,
2709 int ret
, created
= 0;
2710 struct ust_app_session
*ua_sess
;
2712 LTTNG_ASSERT(usess
);
2714 LTTNG_ASSERT(ua_sess_ptr
);
2716 health_code_update();
2718 ua_sess
= lookup_session_by_app(usess
, app
);
2719 if (ua_sess
== nullptr) {
2720 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2723 ua_sess
= alloc_ust_app_session();
2724 if (ua_sess
== nullptr) {
2725 /* Only malloc can failed so something is really wrong */
2729 shadow_copy_session(ua_sess
, usess
, app
);
2733 switch (usess
->buffer_type
) {
2734 case LTTNG_BUFFER_PER_PID
:
2735 /* Init local registry. */
2736 ret
= setup_buffer_reg_pid(ua_sess
, app
, nullptr);
2738 delete_ust_app_session(-1, ua_sess
, app
);
2742 case LTTNG_BUFFER_PER_UID
:
2743 /* Look for a global registry. If none exists, create one. */
2744 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, nullptr);
2746 delete_ust_app_session(-1, ua_sess
, app
);
2756 health_code_update();
2758 if (ua_sess
->handle
== -1) {
2759 pthread_mutex_lock(&app
->sock_lock
);
2760 ret
= lttng_ust_ctl_create_session(app
->sock
);
2761 pthread_mutex_unlock(&app
->sock_lock
);
2763 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2764 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2768 } else if (ret
== -EAGAIN
) {
2769 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2774 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2779 delete_ust_app_session(-1, ua_sess
, app
);
2780 if (ret
!= -ENOMEM
) {
2782 * Tracer is probably gone or got an internal error so let's
2783 * behave like it will soon unregister or not usable.
2790 ua_sess
->handle
= ret
;
2792 /* Add ust app session to app's HT */
2793 lttng_ht_node_init_u64(&ua_sess
->node
, ua_sess
->tracing_id
);
2794 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2795 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2796 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
, &ua_sess
->ust_objd_node
);
2798 DBG2("UST app session created successfully with handle %d", ret
);
2801 *ua_sess_ptr
= ua_sess
;
2803 *is_created
= created
;
2806 /* Everything went well. */
2810 health_code_update();
2815 * Match function for a hash table lookup of ust_app_ctx.
2817 * It matches an ust app context based on the context type and, in the case
2818 * of perf counters, their name.
2820 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2822 struct ust_app_ctx
*ctx
;
2823 const struct lttng_ust_context_attr
*key
;
2828 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2829 key
= (lttng_ust_context_attr
*) _key
;
2832 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2837 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2838 if (strncmp(key
->u
.perf_counter
.name
,
2839 ctx
->ctx
.u
.perf_counter
.name
,
2840 sizeof(key
->u
.perf_counter
.name
)) != 0) {
2844 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2845 if (strcmp(key
->u
.app_ctx
.provider_name
, ctx
->ctx
.u
.app_ctx
.provider_name
) != 0 ||
2846 strcmp(key
->u
.app_ctx
.ctx_name
, ctx
->ctx
.u
.app_ctx
.ctx_name
) != 0) {
2862 * Lookup for an ust app context from an lttng_ust_context.
2864 * Must be called while holding RCU read side lock.
2865 * Return an ust_app_ctx object or NULL on error.
2867 static struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2868 struct lttng_ust_context_attr
*uctx
)
2870 struct lttng_ht_iter iter
;
2871 struct lttng_ht_node_ulong
*node
;
2872 struct ust_app_ctx
*app_ctx
= nullptr;
2876 ASSERT_RCU_READ_LOCKED();
2878 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2879 cds_lfht_lookup(ht
->ht
,
2880 ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2881 ht_match_ust_app_ctx
,
2884 node
= lttng_ht_iter_get_node_ulong(&iter
);
2889 app_ctx
= lttng::utils::container_of(node
, &ust_app_ctx::node
);
2896 * Create a context for the channel on the tracer.
2898 * Called with UST app session lock held and a RCU read side lock.
2900 static int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2901 struct lttng_ust_context_attr
*uctx
,
2902 struct ust_app
*app
)
2905 struct ust_app_ctx
*ua_ctx
;
2907 ASSERT_RCU_READ_LOCKED();
2909 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2911 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2917 ua_ctx
= alloc_ust_app_ctx(uctx
);
2918 if (ua_ctx
== nullptr) {
2924 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2925 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2926 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2928 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2938 * Enable on the tracer side a ust app event for the session and channel.
2940 * Called with UST app session lock held.
2942 static int enable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2946 ret
= enable_ust_object(app
, ua_event
->obj
);
2951 ua_event
->enabled
= true;
2958 * Disable on the tracer side a ust app event for the session and channel.
2960 static int disable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2964 ret
= disable_ust_object(app
, ua_event
->obj
);
2969 ua_event
->enabled
= false;
2976 * Lookup ust app channel for session and disable it on the tracer side.
2978 static int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2979 struct ust_app_channel
*ua_chan
,
2980 struct ust_app
*app
)
2984 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2989 ua_chan
->enabled
= false;
2996 * Lookup ust app channel for session and enable it on the tracer side. This
2997 * MUST be called with a RCU read side lock acquired.
2999 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
3000 struct ltt_ust_channel
*uchan
,
3001 struct ust_app
*app
)
3004 struct lttng_ht_iter iter
;
3005 struct lttng_ht_node_str
*ua_chan_node
;
3006 struct ust_app_channel
*ua_chan
;
3008 ASSERT_RCU_READ_LOCKED();
3010 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3011 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3012 if (ua_chan_node
== nullptr) {
3013 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
3015 ua_sess
->tracing_id
);
3019 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3021 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
3031 * Ask the consumer to create a channel and get it if successful.
3033 * Called with UST app session lock held.
3035 * Return 0 on success or else a negative value.
3037 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
3038 struct ust_app_session
*ua_sess
,
3039 struct ust_app_channel
*ua_chan
,
3041 lsu::registry_session
*registry
)
3044 unsigned int nb_fd
= 0;
3045 struct consumer_socket
*socket
;
3047 LTTNG_ASSERT(usess
);
3048 LTTNG_ASSERT(ua_sess
);
3049 LTTNG_ASSERT(ua_chan
);
3050 LTTNG_ASSERT(registry
);
3052 lttng::urcu::read_lock_guard read_lock
;
3053 health_code_update();
3055 /* Get the right consumer socket for the application. */
3056 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3062 health_code_update();
3064 /* Need one fd for the channel. */
3065 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3067 ERR("Exhausted number of available FD upon create channel");
3072 * Ask consumer to create channel. The consumer will return the number of
3073 * stream we have to expect.
3075 ret
= ust_consumer_ask_channel(
3076 ua_sess
, ua_chan
, usess
->consumer
, socket
, registry
, usess
->current_trace_chunk
);
3082 * Compute the number of fd needed before receiving them. It must be 2 per
3083 * stream (2 being the default value here).
3085 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3087 /* Reserve the amount of file descriptor we need. */
3088 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3090 ERR("Exhausted number of available FD upon create channel");
3091 goto error_fd_get_stream
;
3094 health_code_update();
3097 * Now get the channel from the consumer. This call will populate the stream
3098 * list of that channel and set the ust objects.
3100 if (usess
->consumer
->enabled
) {
3101 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3110 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3111 error_fd_get_stream
:
3113 * Initiate a destroy channel on the consumer since we had an error
3114 * handling it on our side. The return value is of no importance since we
3115 * already have a ret value set by the previous error that we need to
3118 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3120 lttng_fd_put(LTTNG_FD_APPS
, 1);
3122 health_code_update();
3127 * Duplicate the ust data object of the ust app stream and save it in the
3128 * buffer registry stream.
3130 * Return 0 on success or else a negative value.
3132 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3133 struct ust_app_stream
*stream
)
3137 LTTNG_ASSERT(reg_stream
);
3138 LTTNG_ASSERT(stream
);
3140 /* Duplicating a stream requires 2 new fds. Reserve them. */
3141 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3143 ERR("Exhausted number of available FD upon duplicate stream");
3147 /* Duplicate object for stream once the original is in the registry. */
3148 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
, reg_stream
->obj
.ust
);
3150 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3151 reg_stream
->obj
.ust
,
3154 lttng_fd_put(LTTNG_FD_APPS
, 2);
3157 stream
->handle
= stream
->obj
->handle
;
3164 * Duplicate the ust data object of the ust app. channel and save it in the
3165 * buffer registry channel.
3167 * Return 0 on success or else a negative value.
3169 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3170 struct ust_app_channel
*ua_chan
)
3174 LTTNG_ASSERT(buf_reg_chan
);
3175 LTTNG_ASSERT(ua_chan
);
3177 /* Duplicating a channel requires 1 new fd. Reserve it. */
3178 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3180 ERR("Exhausted number of available FD upon duplicate channel");
3184 /* Duplicate object for stream once the original is in the registry. */
3185 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3187 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3188 buf_reg_chan
->obj
.ust
,
3193 ua_chan
->handle
= ua_chan
->obj
->handle
;
3198 lttng_fd_put(LTTNG_FD_APPS
, 1);
3204 * For a given channel buffer registry, setup all streams of the given ust
3205 * application channel.
3207 * Return 0 on success or else a negative value.
3209 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3210 struct ust_app_channel
*ua_chan
,
3211 struct ust_app
*app
)
3214 struct ust_app_stream
*stream
, *stmp
;
3216 LTTNG_ASSERT(buf_reg_chan
);
3217 LTTNG_ASSERT(ua_chan
);
3219 DBG2("UST app setup buffer registry stream");
3221 /* Send all streams to application. */
3222 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3223 struct buffer_reg_stream
*reg_stream
;
3225 ret
= buffer_reg_stream_create(®_stream
);
3231 * Keep original pointer and nullify it in the stream so the delete
3232 * stream call does not release the object.
3234 reg_stream
->obj
.ust
= stream
->obj
;
3235 stream
->obj
= nullptr;
3236 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3238 /* We don't need the streams anymore. */
3239 cds_list_del(&stream
->list
);
3240 delete_ust_app_stream(-1, stream
, app
);
3248 * Create a buffer registry channel for the given session registry and
3249 * application channel object. If regp pointer is valid, it's set with the
3250 * created object. Important, the created object is NOT added to the session
3251 * registry hash table.
3253 * Return 0 on success else a negative value.
3255 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3256 struct ust_app_channel
*ua_chan
,
3257 struct buffer_reg_channel
**regp
)
3260 struct buffer_reg_channel
*buf_reg_chan
= nullptr;
3262 LTTNG_ASSERT(reg_sess
);
3263 LTTNG_ASSERT(ua_chan
);
3265 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3267 /* Create buffer registry channel. */
3268 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3272 LTTNG_ASSERT(buf_reg_chan
);
3273 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3274 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3275 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3277 /* Create and add a channel registry to session. */
3279 reg_sess
->reg
.ust
->add_channel(ua_chan
->tracing_channel_id
);
3280 } catch (const std::exception
& ex
) {
3281 ERR("Failed to add a channel registry to userspace registry session: %s",
3287 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3290 *regp
= buf_reg_chan
;
3296 /* Safe because the registry channel object was not added to any HT. */
3297 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3303 * Setup buffer registry channel for the given session registry and application
3304 * channel object. If regp pointer is valid, it's set with the created object.
3306 * Return 0 on success else a negative value.
3308 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3309 struct ust_app_channel
*ua_chan
,
3310 struct buffer_reg_channel
*buf_reg_chan
,
3311 struct ust_app
*app
)
3315 LTTNG_ASSERT(reg_sess
);
3316 LTTNG_ASSERT(buf_reg_chan
);
3317 LTTNG_ASSERT(ua_chan
);
3318 LTTNG_ASSERT(ua_chan
->obj
);
3320 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3322 /* Setup all streams for the registry. */
3323 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3328 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3329 ua_chan
->obj
= nullptr;
3334 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3335 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3340 * Send buffer registry channel to the application.
3342 * Return 0 on success else a negative value.
3344 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3345 struct ust_app
*app
,
3346 struct ust_app_session
*ua_sess
,
3347 struct ust_app_channel
*ua_chan
)
3350 struct buffer_reg_stream
*reg_stream
;
3352 LTTNG_ASSERT(buf_reg_chan
);
3354 LTTNG_ASSERT(ua_sess
);
3355 LTTNG_ASSERT(ua_chan
);
3357 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3359 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3364 /* Send channel to the application. */
3365 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3366 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3367 ret
= -ENOTCONN
; /* Caused by app exiting. */
3369 } else if (ret
== -EAGAIN
) {
3370 /* Caused by timeout. */
3371 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
3375 ua_sess
->tracing_id
);
3376 /* Treat this the same way as an application that is exiting. */
3379 } else if (ret
< 0) {
3383 health_code_update();
3385 /* Send all streams to application. */
3386 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3387 cds_list_for_each_entry (reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3388 struct ust_app_stream stream
= {};
3390 ret
= duplicate_stream_object(reg_stream
, &stream
);
3392 goto error_stream_unlock
;
3395 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3397 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3398 ret
= -ENOTCONN
; /* Caused by app exiting. */
3399 } else if (ret
== -EAGAIN
) {
3401 * Caused by timeout.
3402 * Treat this the same way as an application
3405 WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64
3409 ua_sess
->tracing_id
);
3412 (void) release_ust_app_stream(-1, &stream
, app
);
3413 goto error_stream_unlock
;
3417 * The return value is not important here. This function will output an
3420 (void) release_ust_app_stream(-1, &stream
, app
);
3423 error_stream_unlock
:
3424 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3430 * Create and send to the application the created buffers with per UID buffers.
3432 * This MUST be called with a RCU read side lock acquired.
3433 * The session list lock and the session's lock must be acquired.
3435 * Return 0 on success else a negative value.
3437 static int create_channel_per_uid(struct ust_app
*app
,
3438 struct ltt_ust_session
*usess
,
3439 struct ust_app_session
*ua_sess
,
3440 struct ust_app_channel
*ua_chan
)
3443 struct buffer_reg_uid
*reg_uid
;
3444 struct buffer_reg_channel
*buf_reg_chan
;
3445 struct ltt_session
*session
= nullptr;
3446 enum lttng_error_code notification_ret
;
3449 LTTNG_ASSERT(usess
);
3450 LTTNG_ASSERT(ua_sess
);
3451 LTTNG_ASSERT(ua_chan
);
3452 ASSERT_RCU_READ_LOCKED();
3454 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3456 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
3458 * The session creation handles the creation of this global registry
3459 * object. If none can be find, there is a code flow problem or a
3462 LTTNG_ASSERT(reg_uid
);
3464 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
, reg_uid
);
3469 /* Create the buffer registry channel object. */
3470 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3472 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan
->name
);
3476 session
= session_find_by_id(ua_sess
->tracing_id
);
3477 LTTNG_ASSERT(session
);
3478 ASSERT_LOCKED(session
->lock
);
3479 ASSERT_SESSION_LIST_LOCKED();
3482 * Create the buffers on the consumer side. This call populates the
3483 * ust app channel object with all streams and data object.
3485 ret
= do_consumer_create_channel(
3486 usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, reg_uid
->registry
->reg
.ust
);
3488 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3491 * Let's remove the previously created buffer registry channel so
3492 * it's not visible anymore in the session registry.
3494 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3496 locked_registry
->remove_channel(ua_chan
->tracing_channel_id
, false);
3497 } catch (const std::exception
& ex
) {
3498 DBG("Could not find channel for removal: %s", ex
.what());
3500 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3501 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3506 * Setup the streams and add it to the session registry.
3508 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, buf_reg_chan
, app
);
3510 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3515 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3516 auto& ust_reg_chan
= locked_registry
->channel(ua_chan
->tracing_channel_id
);
3518 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3521 /* Notify the notification subsystem of the channel's creation. */
3522 notification_ret
= notification_thread_command_add_channel(
3523 the_notification_thread_handle
,
3528 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3529 if (notification_ret
!= LTTNG_OK
) {
3530 ret
= -(int) notification_ret
;
3531 ERR("Failed to add channel to notification thread");
3536 /* Send buffers to the application. */
3537 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3539 if (ret
!= -ENOTCONN
) {
3540 ERR("Error sending channel to application");
3547 session_put(session
);
3553 * Create and send to the application the created buffers with per PID buffers.
3555 * Called with UST app session lock held.
3556 * The session list lock and the session's lock must be acquired.
3558 * Return 0 on success else a negative value.
3560 static int create_channel_per_pid(struct ust_app
*app
,
3561 struct ltt_ust_session
*usess
,
3562 struct ust_app_session
*ua_sess
,
3563 struct ust_app_channel
*ua_chan
)
3566 lsu::registry_session
*registry
;
3567 enum lttng_error_code cmd_ret
;
3568 struct ltt_session
*session
= nullptr;
3569 uint64_t chan_reg_key
;
3572 LTTNG_ASSERT(usess
);
3573 LTTNG_ASSERT(ua_sess
);
3574 LTTNG_ASSERT(ua_chan
);
3576 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3578 lttng::urcu::read_lock_guard read_lock
;
3580 registry
= get_session_registry(ua_sess
);
3581 /* The UST app session lock is held, registry shall not be null. */
3582 LTTNG_ASSERT(registry
);
3584 /* Create and add a new channel registry to session. */
3586 registry
->add_channel(ua_chan
->key
);
3587 } catch (const std::exception
& ex
) {
3588 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3595 session
= session_find_by_id(ua_sess
->tracing_id
);
3596 LTTNG_ASSERT(session
);
3597 ASSERT_LOCKED(session
->lock
);
3598 ASSERT_SESSION_LIST_LOCKED();
3600 /* Create and get channel on the consumer side. */
3601 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, registry
);
3603 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3604 goto error_remove_from_registry
;
3607 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3609 if (ret
!= -ENOTCONN
) {
3610 ERR("Error sending channel to application");
3612 goto error_remove_from_registry
;
3615 chan_reg_key
= ua_chan
->key
;
3617 auto locked_registry
= registry
->lock();
3619 auto& ust_reg_chan
= locked_registry
->channel(chan_reg_key
);
3620 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3623 cmd_ret
= notification_thread_command_add_channel(the_notification_thread_handle
,
3628 ua_chan
->attr
.subbuf_size
*
3629 ua_chan
->attr
.num_subbuf
);
3630 if (cmd_ret
!= LTTNG_OK
) {
3631 ret
= -(int) cmd_ret
;
3632 ERR("Failed to add channel to notification thread");
3633 goto error_remove_from_registry
;
3636 error_remove_from_registry
:
3639 auto locked_registry
= registry
->lock();
3640 locked_registry
->remove_channel(ua_chan
->key
, false);
3641 } catch (const std::exception
& ex
) {
3642 DBG("Could not find channel for removal: %s", ex
.what());
3647 session_put(session
);
3653 * From an already allocated ust app channel, create the channel buffers if
3654 * needed and send them to the application. This MUST be called with a RCU read
3655 * side lock acquired.
3657 * Called with UST app session lock held.
3659 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3660 * the application exited concurrently.
3662 static int ust_app_channel_send(struct ust_app
*app
,
3663 struct ltt_ust_session
*usess
,
3664 struct ust_app_session
*ua_sess
,
3665 struct ust_app_channel
*ua_chan
)
3670 LTTNG_ASSERT(usess
);
3671 LTTNG_ASSERT(usess
->active
);
3672 LTTNG_ASSERT(ua_sess
);
3673 LTTNG_ASSERT(ua_chan
);
3674 ASSERT_RCU_READ_LOCKED();
3676 /* Handle buffer type before sending the channel to the application. */
3677 switch (usess
->buffer_type
) {
3678 case LTTNG_BUFFER_PER_UID
:
3680 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3686 case LTTNG_BUFFER_PER_PID
:
3688 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3700 /* Initialize ust objd object using the received handle and add it. */
3701 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3702 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3704 /* If channel is not enabled, disable it on the tracer */
3705 if (!ua_chan
->enabled
) {
3706 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3717 * Create UST app channel and return it through ua_chanp if not NULL.
3719 * Called with UST app session lock and RCU read-side lock held.
3721 * Return 0 on success or else a negative value.
3723 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3724 struct ltt_ust_channel
*uchan
,
3725 enum lttng_ust_abi_chan_type type
,
3726 struct ltt_ust_session
*usess
__attribute__((unused
)),
3727 struct ust_app_channel
**ua_chanp
)
3730 struct lttng_ht_iter iter
;
3731 struct lttng_ht_node_str
*ua_chan_node
;
3732 struct ust_app_channel
*ua_chan
;
3734 ASSERT_RCU_READ_LOCKED();
3736 /* Lookup channel in the ust app session */
3737 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3738 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3739 if (ua_chan_node
!= nullptr) {
3740 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3744 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3745 if (ua_chan
== nullptr) {
3746 /* Only malloc can fail here */
3750 shadow_copy_channel(ua_chan
, uchan
);
3752 /* Set channel type. */
3753 ua_chan
->attr
.type
= type
;
3755 /* Only add the channel if successful on the tracer side. */
3756 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3759 *ua_chanp
= ua_chan
;
3762 /* Everything went well. */
3770 * Create UST app event and create it on the tracer side.
3772 * Must be called with the RCU read side lock held.
3773 * Called with ust app session mutex held.
3775 static int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3776 struct ltt_ust_event
*uevent
,
3777 struct ust_app
*app
)
3780 struct ust_app_event
*ua_event
;
3782 ASSERT_RCU_READ_LOCKED();
3784 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3785 if (ua_event
== nullptr) {
3786 /* Only failure mode of alloc_ust_app_event(). */
3790 shadow_copy_event(ua_event
, uevent
);
3792 /* Create it on the tracer side */
3793 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3796 * Not found previously means that it does not exist on the
3797 * tracer. If the application reports that the event existed,
3798 * it means there is a bug in the sessiond or lttng-ust
3799 * (or corruption, etc.)
3801 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3802 ERR("Tracer for application reported that an event being created already existed: "
3803 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3813 add_unique_ust_app_event(ua_chan
, ua_event
);
3815 DBG2("UST app create event completed: app = '%s' pid = %d", app
->name
, app
->pid
);
3821 /* Valid. Calling here is already in a read side lock */
3822 delete_ust_app_event(-1, ua_event
, app
);
3827 * Create UST app event notifier rule and create it on the tracer side.
3829 * Must be called with the RCU read side lock held.
3830 * Called with ust app session mutex held.
3832 static int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
, struct ust_app
*app
)
3835 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3837 ASSERT_RCU_READ_LOCKED();
3839 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3840 if (ua_event_notifier_rule
== nullptr) {
3845 /* Create it on the tracer side. */
3846 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3849 * Not found previously means that it does not exist on the
3850 * tracer. If the application reports that the event existed,
3851 * it means there is a bug in the sessiond or lttng-ust
3852 * (or corruption, etc.)
3854 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3855 ERR("Tracer for application reported that an event notifier being created already exists: "
3856 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3857 lttng_trigger_get_tracer_token(trigger
),
3866 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3867 &ua_event_notifier_rule
->node
);
3869 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3872 lttng_trigger_get_tracer_token(trigger
));
3877 /* The RCU read side lock is already being held by the caller. */
3878 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3884 * Create UST metadata and open it on the tracer side.
3886 * Called with UST app session lock held and RCU read side lock.
3888 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3889 struct ust_app
*app
,
3890 struct consumer_output
*consumer
)
3893 struct ust_app_channel
*metadata
;
3894 struct consumer_socket
*socket
;
3895 struct ltt_session
*session
= nullptr;
3897 LTTNG_ASSERT(ua_sess
);
3899 LTTNG_ASSERT(consumer
);
3900 ASSERT_RCU_READ_LOCKED();
3902 auto locked_registry
= get_locked_session_registry(ua_sess
);
3903 /* The UST app session is held registry shall not be null. */
3904 LTTNG_ASSERT(locked_registry
);
3906 /* Metadata already exists for this registry or it was closed previously */
3907 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3912 /* Allocate UST metadata */
3913 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, nullptr);
3915 /* malloc() failed */
3920 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3922 /* Need one fd for the channel. */
3923 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3925 ERR("Exhausted number of available FD upon create metadata");
3929 /* Get the right consumer socket for the application. */
3930 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3933 goto error_consumer
;
3937 * Keep metadata key so we can identify it on the consumer side. Assign it
3938 * to the registry *before* we ask the consumer so we avoid the race of the
3939 * consumer requesting the metadata and the ask_channel call on our side
3940 * did not returned yet.
3942 locked_registry
->_metadata_key
= metadata
->key
;
3944 session
= session_find_by_id(ua_sess
->tracing_id
);
3945 LTTNG_ASSERT(session
);
3946 ASSERT_LOCKED(session
->lock
);
3947 ASSERT_SESSION_LIST_LOCKED();
3950 * Ask the metadata channel creation to the consumer. The metadata object
3951 * will be created by the consumer and kept their. However, the stream is
3952 * never added or monitored until we do a first push metadata to the
3955 ret
= ust_consumer_ask_channel(ua_sess
,
3959 locked_registry
.get(),
3960 session
->current_trace_chunk
);
3962 /* Nullify the metadata key so we don't try to close it later on. */
3963 locked_registry
->_metadata_key
= 0;
3964 goto error_consumer
;
3968 * The setup command will make the metadata stream be sent to the relayd,
3969 * if applicable, and the thread managing the metadatas. This is important
3970 * because after this point, if an error occurs, the only way the stream
3971 * can be deleted is to be monitored in the consumer.
3973 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3975 /* Nullify the metadata key so we don't try to close it later on. */
3976 locked_registry
->_metadata_key
= 0;
3977 goto error_consumer
;
3980 DBG2("UST metadata with key %" PRIu64
" created for app pid %d", metadata
->key
, app
->pid
);
3983 lttng_fd_put(LTTNG_FD_APPS
, 1);
3984 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3987 session_put(session
);
3993 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3994 * acquired before calling this function.
3996 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3998 struct ust_app
*app
= nullptr;
3999 struct lttng_ht_node_ulong
*node
;
4000 struct lttng_ht_iter iter
;
4002 lttng_ht_lookup(ust_app_ht
, (void *) ((unsigned long) pid
), &iter
);
4003 node
= lttng_ht_iter_get_node_ulong(&iter
);
4004 if (node
== nullptr) {
4005 DBG2("UST app no found with pid %d", pid
);
4009 DBG2("Found UST app by pid %d", pid
);
4011 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
4018 * Allocate and init an UST app object using the registration information and
4019 * the command socket. This is called when the command socket connects to the
4022 * The object is returned on success or else NULL.
4024 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
4027 struct ust_app
*lta
= nullptr;
4028 struct lttng_pipe
*event_notifier_event_source_pipe
= nullptr;
4031 LTTNG_ASSERT(sock
>= 0);
4033 DBG3("UST app creating application for socket %d", sock
);
4035 if ((msg
->bits_per_long
== 64 && (uatomic_read(&the_ust_consumerd64_fd
) == -EINVAL
)) ||
4036 (msg
->bits_per_long
== 32 && (uatomic_read(&the_ust_consumerd32_fd
) == -EINVAL
))) {
4037 ERR("Registration failed: application \"%s\" (pid: %d) has "
4038 "%d-bit long, but no consumerd for this size is available.\n",
4041 msg
->bits_per_long
);
4046 * Reserve the two file descriptors of the event source pipe. The write
4047 * end will be closed once it is passed to the application, at which
4048 * point a single 'put' will be performed.
4050 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
4052 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4058 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
4059 if (!event_notifier_event_source_pipe
) {
4060 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4066 lta
= zmalloc
<ust_app
>();
4067 if (lta
== nullptr) {
4069 goto error_free_pipe
;
4072 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4074 lta
->ppid
= msg
->ppid
;
4075 lta
->uid
= msg
->uid
;
4076 lta
->gid
= msg
->gid
;
4079 .bits_per_long
= msg
->bits_per_long
,
4080 .long_alignment
= msg
->long_alignment
,
4081 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4082 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4083 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4084 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4085 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4086 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4087 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4090 lta
->v_major
= msg
->major
;
4091 lta
->v_minor
= msg
->minor
;
4092 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4093 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4094 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4095 lta
->notify_sock
= -1;
4096 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4098 /* Copy name and make sure it's NULL terminated. */
4099 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4100 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4103 * Before this can be called, when receiving the registration information,
4104 * the application compatibility is checked. So, at this point, the
4105 * application can work with this session daemon.
4107 lta
->compatible
= 1;
4109 lta
->pid
= msg
->pid
;
4110 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4112 pthread_mutex_init(<a
->sock_lock
, nullptr);
4113 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4115 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4119 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4120 lttng_fd_put(LTTNG_FD_APPS
, 2);
4126 * For a given application object, add it to every hash table.
4128 void ust_app_add(struct ust_app
*app
)
4131 LTTNG_ASSERT(app
->notify_sock
>= 0);
4133 app
->registration_time
= time(nullptr);
4135 lttng::urcu::read_lock_guard read_lock
;
4138 * On a re-registration, we want to kick out the previous registration of
4141 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4144 * The socket _should_ be unique until _we_ call close. So, a add_unique
4145 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4146 * already in the table.
4148 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4150 /* Add application to the notify socket hash table. */
4151 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4152 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4154 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4155 "notify_sock =%d (version %d.%d)",
4168 * Set the application version into the object.
4170 * Return 0 on success else a negative value either an errno code or a
4171 * LTTng-UST error code.
4173 int ust_app_version(struct ust_app
*app
)
4179 pthread_mutex_lock(&app
->sock_lock
);
4180 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4181 pthread_mutex_unlock(&app
->sock_lock
);
4183 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4184 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4187 } else if (ret
== -EAGAIN
) {
4188 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4192 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4202 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4204 return app
->v_major
>= 9;
4207 bool ust_app_supports_counters(const struct ust_app
*app
)
4209 return app
->v_major
>= 9;
4213 * Setup the base event notifier group.
4215 * Return 0 on success else a negative value either an errno code or a
4216 * LTTng-UST error code.
4218 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4221 int event_pipe_write_fd
;
4222 struct lttng_ust_abi_object_data
*event_notifier_group
= nullptr;
4223 enum lttng_error_code lttng_ret
;
4224 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4228 if (!ust_app_supports_notifiers(app
)) {
4233 /* Get the write side of the pipe. */
4234 event_pipe_write_fd
= lttng_pipe_get_writefd(app
->event_notifier_group
.event_pipe
);
4236 pthread_mutex_lock(&app
->sock_lock
);
4237 ret
= lttng_ust_ctl_create_event_notifier_group(
4238 app
->sock
, event_pipe_write_fd
, &event_notifier_group
);
4239 pthread_mutex_unlock(&app
->sock_lock
);
4241 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4243 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4246 } else if (ret
== -EAGAIN
) {
4248 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4252 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4256 event_pipe_write_fd
);
4261 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4263 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4270 * Release the file descriptor that was reserved for the write-end of
4273 lttng_fd_put(LTTNG_FD_APPS
, 1);
4275 lttng_ret
= notification_thread_command_add_tracer_event_source(
4276 the_notification_thread_handle
,
4277 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4279 if (lttng_ret
!= LTTNG_OK
) {
4280 ERR("Failed to add tracer event source to notification thread");
4285 /* Assign handle only when the complete setup is valid. */
4286 app
->event_notifier_group
.object
= event_notifier_group
;
4288 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
4289 switch (event_notifier_error_accounting_status
) {
4290 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4292 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4293 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",
4298 goto error_accounting
;
4299 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4300 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4305 goto error_accounting
;
4307 ERR("Failed to setup event notifier error accounting for app");
4309 goto error_accounting
;
4315 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4316 the_notification_thread_handle
,
4317 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
));
4318 if (lttng_ret
!= LTTNG_OK
) {
4319 ERR("Failed to remove application tracer event source from notification thread");
4323 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4324 free(app
->event_notifier_group
.object
);
4325 app
->event_notifier_group
.object
= nullptr;
4330 * Unregister app by removing it from the global traceable app list and freeing
4333 * The socket is already closed at this point so no close to sock.
4335 void ust_app_unregister(int sock
)
4337 struct ust_app
*lta
;
4338 struct lttng_ht_node_ulong
*node
;
4339 struct lttng_ht_iter ust_app_sock_iter
;
4340 struct lttng_ht_iter iter
;
4341 struct ust_app_session
*ua_sess
;
4344 lttng::urcu::read_lock_guard read_lock
;
4346 /* Get the node reference for a call_rcu */
4347 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &ust_app_sock_iter
);
4348 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4351 lta
= lttng::utils::container_of(node
, &ust_app::sock_n
);
4352 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4355 * For per-PID buffers, perform "push metadata" and flush all
4356 * application streams before removing app from hash tables,
4357 * ensuring proper behavior of data_pending check.
4358 * Remove sessions so they are not visible during deletion.
4360 cds_lfht_for_each_entry (lta
->sessions
->ht
, &iter
.iter
, ua_sess
, node
.node
) {
4361 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4363 /* The session was already removed so scheduled for teardown. */
4367 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4368 (void) ust_app_flush_app_session(lta
, ua_sess
);
4372 * Add session to list for teardown. This is safe since at this point we
4373 * are the only one using this list.
4375 pthread_mutex_lock(&ua_sess
->lock
);
4377 if (ua_sess
->deleted
) {
4378 pthread_mutex_unlock(&ua_sess
->lock
);
4383 * Normally, this is done in the delete session process which is
4384 * executed in the call rcu below. However, upon registration we can't
4385 * afford to wait for the grace period before pushing data or else the
4386 * data pending feature can race between the unregistration and stop
4387 * command where the data pending command is sent *before* the grace
4390 * The close metadata below nullifies the metadata pointer in the
4391 * session so the delete session will NOT push/close a second time.
4393 auto locked_registry
= get_locked_session_registry(ua_sess
);
4394 if (locked_registry
) {
4395 /* Push metadata for application before freeing the application. */
4396 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4399 * Don't ask to close metadata for global per UID buffers. Close
4400 * metadata only on destroy trace session in this case. Also, the
4401 * previous push metadata could have flag the metadata registry to
4402 * close so don't send a close command if closed.
4404 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4405 const auto metadata_key
= locked_registry
->_metadata_key
;
4406 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4408 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4409 locked_registry
->_metadata_closed
= true;
4412 /* Release lock before communication, see comments in
4413 * close_metadata(). */
4414 locked_registry
.reset();
4415 (void) close_metadata(
4416 metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4418 locked_registry
.reset();
4421 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4423 pthread_mutex_unlock(&ua_sess
->lock
);
4426 /* Remove application from PID hash table */
4427 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4431 * Remove application from notify hash table. The thread handling the
4432 * notify socket could have deleted the node so ignore on error because
4433 * either way it's valid. The close of that socket is handled by the
4434 * apps_notify_thread.
4436 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4437 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4440 * Ignore return value since the node might have been removed before by an
4441 * add replace during app registration because the PID can be reassigned by
4444 iter
.iter
.node
= <a
->pid_n
.node
;
4445 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4447 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", lta
->pid
);
4451 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4457 * Fill events array with all events name of all registered apps.
4459 int ust_app_list_events(struct lttng_event
**events
)
4462 size_t nbmem
, count
= 0;
4463 struct lttng_ht_iter iter
;
4464 struct ust_app
*app
;
4465 struct lttng_event
*tmp_event
;
4467 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4468 tmp_event
= calloc
<lttng_event
>(nbmem
);
4469 if (tmp_event
== nullptr) {
4470 PERROR("zmalloc ust app events");
4476 lttng::urcu::read_lock_guard read_lock
;
4478 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4479 struct lttng_ust_abi_tracepoint_iter uiter
;
4481 health_code_update();
4483 if (!app
->compatible
) {
4485 * TODO: In time, we should notice the caller of this error by
4486 * telling him that this is a version error.
4491 pthread_mutex_lock(&app
->sock_lock
);
4492 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4494 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4495 ERR("UST app list events getting handle failed for app pid %d",
4498 pthread_mutex_unlock(&app
->sock_lock
);
4502 while ((ret
= lttng_ust_ctl_tracepoint_list_get(
4503 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4504 /* Handle ustctl error. */
4508 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4509 ERR("UST app tp list get failed for app %d with ret %d",
4513 DBG3("UST app tp list get failed. Application is dead");
4519 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4520 if (release_ret
< 0 &&
4521 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4522 release_ret
!= -EPIPE
) {
4523 ERR("Error releasing app handle for app %d with ret %d",
4528 pthread_mutex_unlock(&app
->sock_lock
);
4532 health_code_update();
4533 if (count
>= nbmem
) {
4534 /* In case the realloc fails, we free the memory */
4535 struct lttng_event
*new_tmp_event
;
4538 new_nbmem
= nbmem
<< 1;
4539 DBG2("Reallocating event list from %zu to %zu entries",
4542 new_tmp_event
= (lttng_event
*) realloc(
4543 tmp_event
, new_nbmem
* sizeof(struct lttng_event
));
4544 if (new_tmp_event
== nullptr) {
4547 PERROR("realloc ust app events");
4550 release_ret
= lttng_ust_ctl_release_handle(
4552 if (release_ret
< 0 &&
4553 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4554 release_ret
!= -EPIPE
) {
4555 ERR("Error releasing app handle for app %d with ret %d",
4560 pthread_mutex_unlock(&app
->sock_lock
);
4563 /* Zero the new memory */
4564 memset(new_tmp_event
+ nbmem
,
4566 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4568 tmp_event
= new_tmp_event
;
4571 memcpy(tmp_event
[count
].name
,
4573 LTTNG_UST_ABI_SYM_NAME_LEN
);
4574 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4575 tmp_event
[count
].type
=
4576 (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4577 tmp_event
[count
].pid
= app
->pid
;
4578 tmp_event
[count
].enabled
= -1;
4582 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4583 pthread_mutex_unlock(&app
->sock_lock
);
4585 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4586 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4589 } else if (ret
== -EAGAIN
) {
4590 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4594 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4604 *events
= tmp_event
;
4606 DBG2("UST app list events done (%zu events)", count
);
4610 health_code_update();
4615 * Fill events array with all events name of all registered apps.
4617 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4620 size_t nbmem
, count
= 0;
4621 struct lttng_ht_iter iter
;
4622 struct ust_app
*app
;
4623 struct lttng_event_field
*tmp_event
;
4625 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4626 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4627 if (tmp_event
== nullptr) {
4628 PERROR("zmalloc ust app event fields");
4634 lttng::urcu::read_lock_guard read_lock
;
4636 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4637 struct lttng_ust_abi_field_iter uiter
;
4639 health_code_update();
4641 if (!app
->compatible
) {
4643 * TODO: In time, we should notice the caller of this error by
4644 * telling him that this is a version error.
4649 pthread_mutex_lock(&app
->sock_lock
);
4650 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4652 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4653 ERR("UST app list field getting handle failed for app pid %d",
4656 pthread_mutex_unlock(&app
->sock_lock
);
4660 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(
4661 app
->sock
, handle
, &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4662 /* Handle ustctl error. */
4666 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4667 ERR("UST app tp list field failed for app %d with ret %d",
4671 DBG3("UST app tp list field failed. Application is dead");
4677 lttng_ust_ctl_release_handle(app
->sock
, handle
);
4678 pthread_mutex_unlock(&app
->sock_lock
);
4679 if (release_ret
< 0 &&
4680 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4681 release_ret
!= -EPIPE
) {
4682 ERR("Error releasing app handle for app %d with ret %d",
4690 health_code_update();
4691 if (count
>= nbmem
) {
4692 /* In case the realloc fails, we free the memory */
4693 struct lttng_event_field
*new_tmp_event
;
4696 new_nbmem
= nbmem
<< 1;
4697 DBG2("Reallocating event field list from %zu to %zu entries",
4700 new_tmp_event
= (lttng_event_field
*) realloc(
4702 new_nbmem
* sizeof(struct lttng_event_field
));
4703 if (new_tmp_event
== nullptr) {
4706 PERROR("realloc ust app event fields");
4709 release_ret
= lttng_ust_ctl_release_handle(
4711 pthread_mutex_unlock(&app
->sock_lock
);
4713 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4714 release_ret
!= -EPIPE
) {
4715 ERR("Error releasing app handle for app %d with ret %d",
4723 /* Zero the new memory */
4724 memset(new_tmp_event
+ nbmem
,
4726 (new_nbmem
- nbmem
) *
4727 sizeof(struct lttng_event_field
));
4729 tmp_event
= new_tmp_event
;
4732 memcpy(tmp_event
[count
].field_name
,
4734 LTTNG_UST_ABI_SYM_NAME_LEN
);
4735 /* Mapping between these enums matches 1 to 1. */
4736 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4737 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4739 memcpy(tmp_event
[count
].event
.name
,
4741 LTTNG_UST_ABI_SYM_NAME_LEN
);
4742 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4743 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4744 tmp_event
[count
].event
.pid
= app
->pid
;
4745 tmp_event
[count
].event
.enabled
= -1;
4749 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4750 pthread_mutex_unlock(&app
->sock_lock
);
4751 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4752 ERR("Error releasing app handle for app %d with ret %d",
4760 *fields
= tmp_event
;
4762 DBG2("UST app list event fields done (%zu events)", count
);
4766 health_code_update();
4771 * Free and clean all traceable apps of the global list.
4773 void ust_app_clean_list()
4776 struct ust_app
*app
;
4777 struct lttng_ht_iter iter
;
4779 DBG2("UST app cleaning registered apps hash table");
4781 /* Cleanup notify socket hash table */
4782 if (ust_app_ht_by_notify_sock
) {
4783 lttng::urcu::read_lock_guard read_lock
;
4785 cds_lfht_for_each_entry (
4786 ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
, notify_sock_n
.node
) {
4788 * Assert that all notifiers are gone as all triggers
4789 * are unregistered prior to this clean-up.
4791 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4793 ust_app_notify_sock_unregister(app
->notify_sock
);
4798 lttng::urcu::read_lock_guard read_lock
;
4800 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4801 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4803 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4807 /* Cleanup socket hash table */
4808 if (ust_app_ht_by_sock
) {
4809 lttng::urcu::read_lock_guard read_lock
;
4811 cds_lfht_for_each_entry (ust_app_ht_by_sock
->ht
, &iter
.iter
, app
, sock_n
.node
) {
4812 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4817 /* Destroy is done only when the ht is empty */
4819 lttng_ht_destroy(ust_app_ht
);
4821 if (ust_app_ht_by_sock
) {
4822 lttng_ht_destroy(ust_app_ht_by_sock
);
4824 if (ust_app_ht_by_notify_sock
) {
4825 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4830 * Init UST app hash table.
4832 int ust_app_ht_alloc()
4834 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4838 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4839 if (!ust_app_ht_by_sock
) {
4842 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4843 if (!ust_app_ht_by_notify_sock
) {
4850 * For a specific UST session, disable the channel for all registered apps.
4852 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4855 struct lttng_ht_iter iter
;
4856 struct lttng_ht_node_str
*ua_chan_node
;
4857 struct ust_app
*app
;
4858 struct ust_app_session
*ua_sess
;
4859 struct ust_app_channel
*ua_chan
;
4861 LTTNG_ASSERT(usess
->active
);
4862 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4867 lttng::urcu::read_lock_guard read_lock
;
4869 /* For every registered applications */
4870 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4871 struct lttng_ht_iter uiter
;
4872 if (!app
->compatible
) {
4874 * TODO: In time, we should notice the caller of this error by
4875 * telling him that this is a version error.
4879 ua_sess
= lookup_session_by_app(usess
, app
);
4880 if (ua_sess
== nullptr) {
4885 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4886 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4887 /* If the session if found for the app, the channel must be there */
4888 LTTNG_ASSERT(ua_chan_node
);
4890 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4891 /* The channel must not be already disabled */
4892 LTTNG_ASSERT(ua_chan
->enabled
);
4894 /* Disable channel onto application */
4895 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4897 /* XXX: We might want to report this error at some point... */
4907 * For a specific UST session, enable the channel for all registered apps.
4909 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4912 struct lttng_ht_iter iter
;
4913 struct ust_app
*app
;
4914 struct ust_app_session
*ua_sess
;
4916 LTTNG_ASSERT(usess
->active
);
4917 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4922 lttng::urcu::read_lock_guard read_lock
;
4924 /* For every registered applications */
4925 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4926 if (!app
->compatible
) {
4928 * TODO: In time, we should notice the caller of this error by
4929 * telling him that this is a version error.
4933 ua_sess
= lookup_session_by_app(usess
, app
);
4934 if (ua_sess
== nullptr) {
4938 /* Enable channel onto application */
4939 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4941 /* XXX: We might want to report this error at some point... */
4951 * Disable an event in a channel and for a specific session.
4953 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4954 struct ltt_ust_channel
*uchan
,
4955 struct ltt_ust_event
*uevent
)
4958 struct lttng_ht_iter iter
, uiter
;
4959 struct lttng_ht_node_str
*ua_chan_node
;
4960 struct ust_app
*app
;
4961 struct ust_app_session
*ua_sess
;
4962 struct ust_app_channel
*ua_chan
;
4963 struct ust_app_event
*ua_event
;
4965 LTTNG_ASSERT(usess
->active
);
4966 DBG("UST app disabling event %s for all apps in channel "
4967 "%s for session id %" PRIu64
,
4973 lttng::urcu::read_lock_guard read_lock
;
4975 /* For all registered applications */
4976 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4977 if (!app
->compatible
) {
4979 * TODO: In time, we should notice the caller of this error by
4980 * telling him that this is a version error.
4984 ua_sess
= lookup_session_by_app(usess
, app
);
4985 if (ua_sess
== nullptr) {
4990 /* Lookup channel in the ust app session */
4991 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4992 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4993 if (ua_chan_node
== nullptr) {
4994 DBG2("Channel %s not found in session id %" PRIu64
5002 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5004 ua_event
= find_ust_app_event(ua_chan
->events
,
5007 uevent
->attr
.loglevel
,
5009 if (ua_event
== nullptr) {
5010 DBG2("Event %s not found in channel %s for app pid %d."
5018 ret
= disable_ust_app_event(ua_event
, app
);
5020 /* XXX: Report error someday... */
5029 /* The ua_sess lock must be held by the caller. */
5030 static int ust_app_channel_create(struct ltt_ust_session
*usess
,
5031 struct ust_app_session
*ua_sess
,
5032 struct ltt_ust_channel
*uchan
,
5033 struct ust_app
*app
,
5034 struct ust_app_channel
**_ua_chan
)
5037 struct ust_app_channel
*ua_chan
= nullptr;
5039 LTTNG_ASSERT(ua_sess
);
5040 ASSERT_LOCKED(ua_sess
->lock
);
5042 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
))) {
5043 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
5046 struct ltt_ust_context
*uctx
= nullptr;
5049 * Create channel onto application and synchronize its
5052 ret
= ust_app_channel_allocate(
5053 ua_sess
, uchan
, LTTNG_UST_ABI_CHAN_PER_CPU
, usess
, &ua_chan
);
5058 ret
= ust_app_channel_send(app
, usess
, ua_sess
, ua_chan
);
5064 cds_list_for_each_entry (uctx
, &uchan
->ctx_list
, list
) {
5065 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5077 * The application's socket is not valid. Either a bad socket
5078 * or a timeout on it. We can't inform the caller that for a
5079 * specific app, the session failed so lets continue here.
5081 ret
= 0; /* Not an error. */
5089 if (ret
== 0 && _ua_chan
) {
5091 * Only return the application's channel on success. Note
5092 * that the channel can still be part of the application's
5093 * channel hashtable on error.
5095 *_ua_chan
= ua_chan
;
5101 * Enable event for a specific session and channel on the tracer.
5103 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
5104 struct ltt_ust_channel
*uchan
,
5105 struct ltt_ust_event
*uevent
)
5108 struct lttng_ht_iter iter
, uiter
;
5109 struct lttng_ht_node_str
*ua_chan_node
;
5110 struct ust_app
*app
;
5111 struct ust_app_session
*ua_sess
;
5112 struct ust_app_channel
*ua_chan
;
5113 struct ust_app_event
*ua_event
;
5115 LTTNG_ASSERT(usess
->active
);
5116 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
5121 * NOTE: At this point, this function is called only if the session and
5122 * channel passed are already created for all apps. and enabled on the
5127 lttng::urcu::read_lock_guard read_lock
;
5129 /* For all registered applications */
5130 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5131 if (!app
->compatible
) {
5133 * TODO: In time, we should notice the caller of this error by
5134 * telling him that this is a version error.
5138 ua_sess
= lookup_session_by_app(usess
, app
);
5140 /* The application has problem or is probably dead. */
5144 pthread_mutex_lock(&ua_sess
->lock
);
5146 if (ua_sess
->deleted
) {
5147 pthread_mutex_unlock(&ua_sess
->lock
);
5151 /* Lookup channel in the ust app session */
5152 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5153 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5155 * It is possible that the channel cannot be found is
5156 * the channel/event creation occurs concurrently with
5157 * an application exit.
5159 if (!ua_chan_node
) {
5160 pthread_mutex_unlock(&ua_sess
->lock
);
5164 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5166 /* Get event node */
5167 ua_event
= find_ust_app_event(ua_chan
->events
,
5170 uevent
->attr
.loglevel
,
5172 if (ua_event
== nullptr) {
5173 DBG3("UST app enable event %s not found for app PID %d."
5180 ret
= enable_ust_app_event(ua_event
, app
);
5182 pthread_mutex_unlock(&ua_sess
->lock
);
5186 pthread_mutex_unlock(&ua_sess
->lock
);
5194 * For a specific existing UST session and UST channel, creates the event for
5195 * all registered apps.
5197 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5198 struct ltt_ust_channel
*uchan
,
5199 struct ltt_ust_event
*uevent
)
5202 struct lttng_ht_iter iter
, uiter
;
5203 struct lttng_ht_node_str
*ua_chan_node
;
5204 struct ust_app
*app
;
5205 struct ust_app_session
*ua_sess
;
5206 struct ust_app_channel
*ua_chan
;
5208 LTTNG_ASSERT(usess
->active
);
5209 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5214 lttng::urcu::read_lock_guard read_lock
;
5216 /* For all registered applications */
5217 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5218 if (!app
->compatible
) {
5220 * TODO: In time, we should notice the caller of this error by
5221 * telling him that this is a version error.
5226 ua_sess
= lookup_session_by_app(usess
, app
);
5228 /* The application has problem or is probably dead. */
5232 pthread_mutex_lock(&ua_sess
->lock
);
5234 if (ua_sess
->deleted
) {
5235 pthread_mutex_unlock(&ua_sess
->lock
);
5239 /* Lookup channel in the ust app session */
5240 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5241 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5242 /* If the channel is not found, there is a code flow error */
5243 LTTNG_ASSERT(ua_chan_node
);
5245 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5247 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5248 pthread_mutex_unlock(&ua_sess
->lock
);
5250 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5251 /* Possible value at this point: -ENOMEM. If so, we stop! */
5255 DBG2("UST app event %s already exist on app PID %d",
5267 * Start tracing for a specific UST session and app.
5269 * Called with UST app session lock held.
5272 static int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5275 struct ust_app_session
*ua_sess
;
5277 DBG("Starting tracing for ust app pid %d", app
->pid
);
5279 lttng::urcu::read_lock_guard read_lock
;
5281 if (!app
->compatible
) {
5285 ua_sess
= lookup_session_by_app(usess
, app
);
5286 if (ua_sess
== nullptr) {
5287 /* The session is in teardown process. Ignore and continue. */
5291 pthread_mutex_lock(&ua_sess
->lock
);
5293 if (ua_sess
->deleted
) {
5294 pthread_mutex_unlock(&ua_sess
->lock
);
5298 if (ua_sess
->enabled
) {
5299 pthread_mutex_unlock(&ua_sess
->lock
);
5303 /* Upon restart, we skip the setup, already done */
5304 if (ua_sess
->started
) {
5308 health_code_update();
5311 /* This starts the UST tracing */
5312 pthread_mutex_lock(&app
->sock_lock
);
5313 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5314 pthread_mutex_unlock(&app
->sock_lock
);
5316 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5317 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5320 pthread_mutex_unlock(&ua_sess
->lock
);
5322 } else if (ret
== -EAGAIN
) {
5323 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5326 pthread_mutex_unlock(&ua_sess
->lock
);
5330 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5338 /* Indicate that the session has been started once */
5339 ua_sess
->started
= true;
5340 ua_sess
->enabled
= true;
5342 pthread_mutex_unlock(&ua_sess
->lock
);
5344 health_code_update();
5346 /* Quiescent wait after starting trace */
5347 pthread_mutex_lock(&app
->sock_lock
);
5348 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5349 pthread_mutex_unlock(&app
->sock_lock
);
5351 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5352 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5355 } else if (ret
== -EAGAIN
) {
5356 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5360 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5368 health_code_update();
5372 pthread_mutex_unlock(&ua_sess
->lock
);
5373 health_code_update();
5378 * Stop tracing for a specific UST session and app.
5380 static int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5383 struct ust_app_session
*ua_sess
;
5385 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5387 lttng::urcu::read_lock_guard read_lock
;
5389 if (!app
->compatible
) {
5390 goto end_no_session
;
5393 ua_sess
= lookup_session_by_app(usess
, app
);
5394 if (ua_sess
== nullptr) {
5395 goto end_no_session
;
5398 pthread_mutex_lock(&ua_sess
->lock
);
5400 if (ua_sess
->deleted
) {
5401 pthread_mutex_unlock(&ua_sess
->lock
);
5402 goto end_no_session
;
5406 * If started = 0, it means that stop trace has been called for a session
5407 * that was never started. It's possible since we can have a fail start
5408 * from either the application manager thread or the command thread. Simply
5409 * indicate that this is a stop error.
5411 if (!ua_sess
->started
) {
5412 goto error_rcu_unlock
;
5415 health_code_update();
5417 /* This inhibits UST tracing */
5418 pthread_mutex_lock(&app
->sock_lock
);
5419 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5420 pthread_mutex_unlock(&app
->sock_lock
);
5422 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5423 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5427 } else if (ret
== -EAGAIN
) {
5428 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5434 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5439 goto error_rcu_unlock
;
5442 health_code_update();
5443 ua_sess
->enabled
= false;
5445 /* Quiescent wait after stopping trace */
5446 pthread_mutex_lock(&app
->sock_lock
);
5447 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5448 pthread_mutex_unlock(&app
->sock_lock
);
5450 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5451 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5454 } else if (ret
== -EAGAIN
) {
5455 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5459 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5466 health_code_update();
5469 auto locked_registry
= get_locked_session_registry(ua_sess
);
5471 /* The UST app session is held registry shall not be null. */
5472 LTTNG_ASSERT(locked_registry
);
5474 /* Push metadata for application before freeing the application. */
5475 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5479 pthread_mutex_unlock(&ua_sess
->lock
);
5481 health_code_update();
5485 pthread_mutex_unlock(&ua_sess
->lock
);
5486 health_code_update();
5490 static int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5492 int ret
, retval
= 0;
5493 struct lttng_ht_iter iter
;
5494 struct ust_app_channel
*ua_chan
;
5495 struct consumer_socket
*socket
;
5497 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5499 if (!app
->compatible
) {
5500 goto end_not_compatible
;
5503 pthread_mutex_lock(&ua_sess
->lock
);
5505 if (ua_sess
->deleted
) {
5509 health_code_update();
5511 /* Flushing buffers */
5512 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5514 /* Flush buffers and push metadata. */
5515 switch (ua_sess
->buffer_type
) {
5516 case LTTNG_BUFFER_PER_PID
:
5518 lttng::urcu::read_lock_guard read_lock
;
5520 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5521 health_code_update();
5522 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5524 ERR("Error flushing consumer channel");
5532 case LTTNG_BUFFER_PER_UID
:
5538 health_code_update();
5541 pthread_mutex_unlock(&ua_sess
->lock
);
5544 health_code_update();
5549 * Flush buffers for all applications for a specific UST session.
5550 * Called with UST session lock held.
5552 static int ust_app_flush_session(struct ltt_ust_session
*usess
)
5557 DBG("Flushing session buffers for all ust apps");
5559 /* Flush buffers and push metadata. */
5560 switch (usess
->buffer_type
) {
5561 case LTTNG_BUFFER_PER_UID
:
5563 struct buffer_reg_uid
*reg
;
5564 struct lttng_ht_iter iter
;
5566 /* Flush all per UID buffers associated to that session. */
5567 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5568 lttng::urcu::read_lock_guard read_lock
;
5569 lsu::registry_session
*ust_session_reg
;
5570 struct buffer_reg_channel
*buf_reg_chan
;
5571 struct consumer_socket
*socket
;
5573 /* Get consumer socket to use to push the metadata.*/
5574 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5577 /* Ignore request if no consumer is found for the session. */
5581 cds_lfht_for_each_entry (
5582 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5584 * The following call will print error values so the return
5585 * code is of little importance because whatever happens, we
5586 * have to try them all.
5588 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5591 ust_session_reg
= reg
->registry
->reg
.ust
;
5592 /* Push metadata. */
5593 auto locked_registry
= ust_session_reg
->lock();
5594 (void) push_metadata(locked_registry
, usess
->consumer
);
5599 case LTTNG_BUFFER_PER_PID
:
5601 struct ust_app_session
*ua_sess
;
5602 struct lttng_ht_iter iter
;
5603 struct ust_app
*app
;
5604 lttng::urcu::read_lock_guard read_lock
;
5606 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5607 ua_sess
= lookup_session_by_app(usess
, app
);
5608 if (ua_sess
== nullptr) {
5612 (void) ust_app_flush_app_session(app
, ua_sess
);
5623 health_code_update();
5627 static int ust_app_clear_quiescent_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5630 struct lttng_ht_iter iter
;
5631 struct ust_app_channel
*ua_chan
;
5632 struct consumer_socket
*socket
;
5634 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5636 lttng::urcu::read_lock_guard read_lock
;
5638 if (!app
->compatible
) {
5639 goto end_not_compatible
;
5642 pthread_mutex_lock(&ua_sess
->lock
);
5644 if (ua_sess
->deleted
) {
5648 health_code_update();
5650 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5652 ERR("Failed to find consumer (%" PRIu32
") socket", app
->abi
.bits_per_long
);
5657 /* Clear quiescent state. */
5658 switch (ua_sess
->buffer_type
) {
5659 case LTTNG_BUFFER_PER_PID
:
5660 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5661 health_code_update();
5662 ret
= consumer_clear_quiescent_channel(socket
, ua_chan
->key
);
5664 ERR("Error clearing quiescent state for consumer channel");
5670 case LTTNG_BUFFER_PER_UID
:
5677 health_code_update();
5680 pthread_mutex_unlock(&ua_sess
->lock
);
5683 health_code_update();
5688 * Clear quiescent state in each stream for all applications for a
5689 * specific UST session.
5690 * Called with UST session lock held.
5692 static int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5697 DBG("Clearing stream quiescent state for all ust apps");
5699 switch (usess
->buffer_type
) {
5700 case LTTNG_BUFFER_PER_UID
:
5702 struct lttng_ht_iter iter
;
5703 struct buffer_reg_uid
*reg
;
5706 * Clear quiescent for all per UID buffers associated to
5709 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5710 struct consumer_socket
*socket
;
5711 struct buffer_reg_channel
*buf_reg_chan
;
5712 lttng::urcu::read_lock_guard read_lock
;
5714 /* Get associated consumer socket.*/
5715 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5719 * Ignore request if no consumer is found for
5725 cds_lfht_for_each_entry (
5726 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5728 * The following call will print error values so
5729 * the return code is of little importance
5730 * because whatever happens, we have to try them
5733 (void) consumer_clear_quiescent_channel(socket
,
5734 buf_reg_chan
->consumer_key
);
5740 case LTTNG_BUFFER_PER_PID
:
5742 struct ust_app_session
*ua_sess
;
5743 struct lttng_ht_iter iter
;
5744 struct ust_app
*app
;
5745 lttng::urcu::read_lock_guard read_lock
;
5747 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5748 ua_sess
= lookup_session_by_app(usess
, app
);
5749 if (ua_sess
== nullptr) {
5752 (void) ust_app_clear_quiescent_app_session(app
, ua_sess
);
5763 health_code_update();
5768 * Destroy a specific UST session in apps.
5770 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5773 struct ust_app_session
*ua_sess
;
5774 struct lttng_ht_iter iter
;
5775 struct lttng_ht_node_u64
*node
;
5777 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5779 lttng::urcu::read_lock_guard read_lock
;
5781 if (!app
->compatible
) {
5785 __lookup_session_by_app(usess
, app
, &iter
);
5786 node
= lttng_ht_iter_get_node_u64(&iter
);
5787 if (node
== nullptr) {
5788 /* Session is being or is deleted. */
5791 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5793 health_code_update();
5794 destroy_app_session(app
, ua_sess
);
5796 health_code_update();
5798 /* Quiescent wait after stopping trace */
5799 pthread_mutex_lock(&app
->sock_lock
);
5800 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5801 pthread_mutex_unlock(&app
->sock_lock
);
5803 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5804 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5807 } else if (ret
== -EAGAIN
) {
5808 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5812 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5819 health_code_update();
5824 * Start tracing for the UST session.
5826 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5828 struct lttng_ht_iter iter
;
5829 struct ust_app
*app
;
5831 DBG("Starting all UST traces");
5834 * Even though the start trace might fail, flag this session active so
5835 * other application coming in are started by default.
5837 usess
->active
= true;
5840 * In a start-stop-start use-case, we need to clear the quiescent state
5841 * of each channel set by the prior stop command, thus ensuring that a
5842 * following stop or destroy is sure to grab a timestamp_end near those
5843 * operations, even if the packet is empty.
5845 (void) ust_app_clear_quiescent_session(usess
);
5848 lttng::urcu::read_lock_guard read_lock
;
5850 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5851 ust_app_global_update(usess
, app
);
5859 * Start tracing for the UST session.
5860 * Called with UST session lock held.
5862 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5865 struct lttng_ht_iter iter
;
5866 struct ust_app
*app
;
5868 DBG("Stopping all UST traces");
5871 * Even though the stop trace might fail, flag this session inactive so
5872 * other application coming in are not started by default.
5874 usess
->active
= false;
5877 lttng::urcu::read_lock_guard read_lock
;
5879 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5880 ret
= ust_app_stop_trace(usess
, app
);
5882 /* Continue to next apps even on error */
5888 (void) ust_app_flush_session(usess
);
5894 * Destroy app UST session.
5896 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5899 struct lttng_ht_iter iter
;
5900 struct ust_app
*app
;
5902 DBG("Destroy all UST traces");
5905 lttng::urcu::read_lock_guard read_lock
;
5907 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5908 ret
= destroy_trace(usess
, app
);
5910 /* Continue to next apps even on error */
5919 /* The ua_sess lock must be held by the caller. */
5920 static int find_or_create_ust_app_channel(struct ltt_ust_session
*usess
,
5921 struct ust_app_session
*ua_sess
,
5922 struct ust_app
*app
,
5923 struct ltt_ust_channel
*uchan
,
5924 struct ust_app_channel
**ua_chan
)
5927 struct lttng_ht_iter iter
;
5928 struct lttng_ht_node_str
*ua_chan_node
;
5930 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5931 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5933 *ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5937 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5945 static int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5946 struct ltt_ust_event
*uevent
,
5947 struct ust_app
*app
)
5950 struct ust_app_event
*ua_event
= nullptr;
5952 ua_event
= find_ust_app_event(ua_chan
->events
,
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. */
6196 destroy_app_session(app
, ua_sess
);
6200 LTTNG_ASSERT(ua_sess
);
6202 pthread_mutex_lock(&ua_sess
->lock
);
6203 if (ua_sess
->deleted
) {
6204 goto deleted_session
;
6208 lttng::urcu::read_lock_guard read_lock
;
6210 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6213 * Create the metadata for the application. This returns gracefully if a
6214 * metadata was already set for the session.
6216 * The metadata channel must be created after the data channels as the
6217 * consumer daemon assumes this ordering. When interacting with a relay
6218 * daemon, the consumer will use this assumption to send the
6219 * "STREAMS_SENT" message to the relay daemon.
6221 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6223 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6230 pthread_mutex_unlock(&ua_sess
->lock
);
6235 static void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6237 struct ust_app_session
*ua_sess
;
6239 ua_sess
= lookup_session_by_app(usess
, app
);
6240 if (ua_sess
== nullptr) {
6243 destroy_app_session(app
, ua_sess
);
6247 * Add channels/events from UST global domain to registered apps at sock.
6249 * Called with session lock held.
6250 * Called with RCU read-side lock held.
6252 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6254 LTTNG_ASSERT(usess
);
6255 LTTNG_ASSERT(usess
->active
);
6256 ASSERT_RCU_READ_LOCKED();
6258 DBG2("UST app global update for app sock %d for session id %" PRIu64
, app
->sock
, usess
->id
);
6260 if (!app
->compatible
) {
6263 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, usess
, app
->pid
) &&
6264 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
, usess
, app
->uid
) &&
6265 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
, usess
, app
->gid
)) {
6267 * Synchronize the application's internal tracing configuration
6268 * and start tracing.
6270 ust_app_synchronize(usess
, app
);
6271 ust_app_start_trace(usess
, app
);
6273 ust_app_global_destroy(usess
, app
);
6278 * Add all event notifiers to an application.
6280 * Called with session lock held.
6281 * Called with RCU read-side lock held.
6283 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6285 ASSERT_RCU_READ_LOCKED();
6287 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6291 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6295 if (app
->event_notifier_group
.object
== nullptr) {
6296 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6302 ust_app_synchronize_event_notifier_rules(app
);
6306 * Called with session lock held.
6308 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6310 struct lttng_ht_iter iter
;
6311 struct ust_app
*app
;
6314 lttng::urcu::read_lock_guard read_lock
;
6316 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6317 ust_app_global_update(usess
, app
);
6322 void ust_app_global_update_all_event_notifier_rules()
6324 struct lttng_ht_iter iter
;
6325 struct ust_app
*app
;
6327 lttng::urcu::read_lock_guard read_lock
;
6328 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6329 ust_app_global_update_event_notifier_rules(app
);
6334 * Add context to a specific channel for global UST domain.
6336 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6337 struct ltt_ust_channel
*uchan
,
6338 struct ltt_ust_context
*uctx
)
6341 struct lttng_ht_node_str
*ua_chan_node
;
6342 struct lttng_ht_iter iter
, uiter
;
6343 struct ust_app_channel
*ua_chan
= nullptr;
6344 struct ust_app_session
*ua_sess
;
6345 struct ust_app
*app
;
6347 LTTNG_ASSERT(usess
->active
);
6350 lttng::urcu::read_lock_guard read_lock
;
6351 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6352 if (!app
->compatible
) {
6354 * TODO: In time, we should notice the caller of this error by
6355 * telling him that this is a version error.
6359 ua_sess
= lookup_session_by_app(usess
, app
);
6360 if (ua_sess
== nullptr) {
6364 pthread_mutex_lock(&ua_sess
->lock
);
6366 if (ua_sess
->deleted
) {
6367 pthread_mutex_unlock(&ua_sess
->lock
);
6371 /* Lookup channel in the ust app session */
6372 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6373 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6374 if (ua_chan_node
== nullptr) {
6377 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6378 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6383 pthread_mutex_unlock(&ua_sess
->lock
);
6391 * Receive registration and populate the given msg structure.
6393 * On success return 0 else a negative value returned by the ustctl call.
6395 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6398 uint32_t pid
, ppid
, uid
, gid
;
6402 ret
= lttng_ust_ctl_recv_reg_msg(sock
,
6410 &msg
->bits_per_long
,
6411 &msg
->uint8_t_alignment
,
6412 &msg
->uint16_t_alignment
,
6413 &msg
->uint32_t_alignment
,
6414 &msg
->uint64_t_alignment
,
6415 &msg
->long_alignment
,
6422 case LTTNG_UST_ERR_EXITING
:
6423 DBG3("UST app recv reg message failed. Application died");
6425 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6426 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6429 LTTNG_UST_ABI_MAJOR_VERSION
,
6430 LTTNG_UST_ABI_MINOR_VERSION
);
6433 ERR("UST app recv reg message failed with ret %d", ret
);
6438 msg
->pid
= (pid_t
) pid
;
6439 msg
->ppid
= (pid_t
) ppid
;
6440 msg
->uid
= (uid_t
) uid
;
6441 msg
->gid
= (gid_t
) gid
;
6448 * Return a ust app session object using the application object and the
6449 * session object descriptor has a key. If not found, NULL is returned.
6450 * A RCU read side lock MUST be acquired when calling this function.
6452 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
, int objd
)
6454 struct lttng_ht_node_ulong
*node
;
6455 struct lttng_ht_iter iter
;
6456 struct ust_app_session
*ua_sess
= nullptr;
6459 ASSERT_RCU_READ_LOCKED();
6461 lttng_ht_lookup(app
->ust_sessions_objd
, (void *) ((unsigned long) objd
), &iter
);
6462 node
= lttng_ht_iter_get_node_ulong(&iter
);
6463 if (node
== nullptr) {
6464 DBG2("UST app session find by objd %d not found", objd
);
6468 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6475 * Return a ust app channel object using the application object and the channel
6476 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6477 * lock MUST be acquired before calling this function.
6479 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
, int objd
)
6481 struct lttng_ht_node_ulong
*node
;
6482 struct lttng_ht_iter iter
;
6483 struct ust_app_channel
*ua_chan
= nullptr;
6486 ASSERT_RCU_READ_LOCKED();
6488 lttng_ht_lookup(app
->ust_objd
, (void *) ((unsigned long) objd
), &iter
);
6489 node
= lttng_ht_iter_get_node_ulong(&iter
);
6490 if (node
== nullptr) {
6491 DBG2("UST app channel find by objd %d not found", objd
);
6495 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6502 * Reply to a register channel notification from an application on the notify
6503 * socket. The channel metadata is also created.
6505 * The session UST registry lock is acquired in this function.
6507 * On success 0 is returned else a negative value.
6509 static int handle_app_register_channel_notification(int sock
,
6511 struct lttng_ust_ctl_field
*raw_context_fields
,
6512 size_t context_field_count
)
6514 int ret
, ret_code
= 0;
6516 uint64_t chan_reg_key
;
6517 struct ust_app
*app
;
6518 struct ust_app_channel
*ua_chan
;
6519 struct ust_app_session
*ua_sess
;
6520 auto ust_ctl_context_fields
=
6521 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_context_fields
);
6523 lttng::urcu::read_lock_guard read_lock_guard
;
6525 /* Lookup application. If not found, there is a code flow error. */
6526 app
= find_app_by_notify_sock(sock
);
6528 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6532 /* Lookup channel by UST object descriptor. */
6533 ua_chan
= find_channel_by_objd(app
, cobjd
);
6535 DBG("Application channel is being torn down. Abort event notify");
6539 LTTNG_ASSERT(ua_chan
->session
);
6540 ua_sess
= ua_chan
->session
;
6542 /* Get right session registry depending on the session buffer type. */
6543 auto locked_registry_session
= get_locked_session_registry(ua_sess
);
6544 if (!locked_registry_session
) {
6545 DBG("Application session is being torn down. Abort event notify");
6549 /* Depending on the buffer type, a different channel key is used. */
6550 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6551 chan_reg_key
= ua_chan
->tracing_channel_id
;
6553 chan_reg_key
= ua_chan
->key
;
6556 auto& ust_reg_chan
= locked_registry_session
->channel(chan_reg_key
);
6558 /* Channel id is set during the object creation. */
6559 chan_id
= ust_reg_chan
.id
;
6562 * The application returns the typing information of the channel's
6563 * context fields. In per-PID buffering mode, this is the first and only
6564 * time we get this information. It is our chance to finalize the
6565 * initialiation of the channel and serialize it's layout's description
6566 * to the trace's metadata.
6568 * However, in per-UID buffering mode, every application will provide
6569 * this information (redundantly). The first time will allow us to
6570 * complete the initialization. The following times, we simply validate
6571 * that all apps provide the same typing for the context fields as a
6575 auto app_context_fields
= lsu::create_trace_fields_from_ust_ctl_fields(
6576 *locked_registry_session
,
6577 ust_ctl_context_fields
.get(),
6578 context_field_count
,
6579 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT
,
6580 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
);
6582 if (!ust_reg_chan
.is_registered()) {
6583 lst::type::cuptr event_context
= app_context_fields
.size() ?
6584 lttng::make_unique
<lst::structure_type
>(
6585 0, std::move(app_context_fields
)) :
6588 ust_reg_chan
.event_context(std::move(event_context
));
6591 * Validate that the context fields match between
6592 * registry and newcoming application.
6594 bool context_fields_match
;
6595 const auto *previous_event_context
= ust_reg_chan
.event_context();
6597 if (!previous_event_context
) {
6598 context_fields_match
= app_context_fields
.size() == 0;
6600 const lst::structure_type
app_event_context_struct(
6601 0, std::move(app_context_fields
));
6603 context_fields_match
= *previous_event_context
==
6604 app_event_context_struct
;
6607 if (!context_fields_match
) {
6608 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6615 } catch (const std::exception
& ex
) {
6616 ERR("Failed to handle application context: %s", ex
.what());
6622 DBG3("UST app replying to register channel key %" PRIu64
" with id %u, ret = %d",
6627 ret
= lttng_ust_ctl_reply_register_channel(
6630 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6631 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6632 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6635 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6636 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6639 } else if (ret
== -EAGAIN
) {
6640 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6644 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6653 /* This channel registry's registration is completed. */
6654 ust_reg_chan
.set_as_registered();
6660 * Add event to the UST channel registry. When the event is added to the
6661 * registry, the metadata is also created. Once done, this replies to the
6662 * application with the appropriate error code.
6664 * The session UST registry lock is acquired in the function.
6666 * On success 0 is returned else a negative value.
6668 static int add_event_ust_registry(int sock
,
6672 char *raw_signature
,
6674 struct lttng_ust_ctl_field
*raw_fields
,
6676 char *raw_model_emf_uri
)
6679 uint32_t event_id
= 0;
6680 uint64_t chan_reg_key
;
6681 struct ust_app
*app
;
6682 struct ust_app_channel
*ua_chan
;
6683 struct ust_app_session
*ua_sess
;
6684 lttng::urcu::read_lock_guard rcu_lock
;
6685 auto signature
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_signature
);
6686 auto fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_fields
);
6687 auto model_emf_uri
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_model_emf_uri
);
6689 /* Lookup application. If not found, there is a code flow error. */
6690 app
= find_app_by_notify_sock(sock
);
6692 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6696 /* Lookup channel by UST object descriptor. */
6697 ua_chan
= find_channel_by_objd(app
, cobjd
);
6699 DBG("Application channel is being torn down. Abort event notify");
6703 LTTNG_ASSERT(ua_chan
->session
);
6704 ua_sess
= ua_chan
->session
;
6706 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6707 chan_reg_key
= ua_chan
->tracing_channel_id
;
6709 chan_reg_key
= ua_chan
->key
;
6713 auto locked_registry
= get_locked_session_registry(ua_sess
);
6714 if (locked_registry
) {
6716 * From this point on, this call acquires the ownership of the signature,
6717 * fields and model_emf_uri meaning any free are done inside it if needed.
6718 * These three variables MUST NOT be read/write after this.
6721 auto& channel
= locked_registry
->channel(chan_reg_key
);
6723 /* event_id is set on success. */
6729 lsu::create_trace_fields_from_ust_ctl_fields(
6733 lst::field_location::root::EVENT_RECORD_PAYLOAD
,
6734 lsu::ctl_field_quirks::
6735 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
),
6737 model_emf_uri
.get() ?
6738 nonstd::optional
<std::string
>(model_emf_uri
.get()) :
6740 ua_sess
->buffer_type
,
6744 } catch (const std::exception
& ex
) {
6745 ERR("Failed to add event `%s` to registry session: %s",
6748 /* Inform the application of the error; don't return directly. */
6752 DBG("Application session is being torn down. Abort event notify");
6758 * The return value is returned to ustctl so in case of an error, the
6759 * application can be notified. In case of an error, it's important not to
6760 * return a negative error or else the application will get closed.
6762 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6764 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6765 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6768 } else if (ret
== -EAGAIN
) {
6769 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6773 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6779 * No need to wipe the create event since the application socket will
6780 * get close on error hence cleaning up everything by itself.
6785 DBG3("UST registry event %s with id %" PRId32
" added successfully", name
, event_id
);
6790 * Add enum to the UST session registry. Once done, this replies to the
6791 * application with the appropriate error code.
6793 * The session UST registry lock is acquired within this function.
6795 * On success 0 is returned else a negative value.
6797 static int add_enum_ust_registry(int sock
,
6800 struct lttng_ust_ctl_enum_entry
*raw_entries
,
6804 struct ust_app
*app
;
6805 struct ust_app_session
*ua_sess
;
6806 uint64_t enum_id
= -1ULL;
6807 lttng::urcu::read_lock_guard read_lock_guard
;
6808 auto entries
= lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::free
>(
6811 /* Lookup application. If not found, there is a code flow error. */
6812 app
= find_app_by_notify_sock(sock
);
6814 /* Return an error since this is not an error */
6815 DBG("Application socket %d is being torn down. Aborting enum registration", sock
);
6819 /* Lookup session by UST object descriptor. */
6820 ua_sess
= find_session_by_objd(app
, sobjd
);
6822 /* Return an error since this is not an error */
6823 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6827 auto locked_registry
= get_locked_session_registry(ua_sess
);
6828 if (!locked_registry
) {
6829 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6834 * From this point on, the callee acquires the ownership of
6835 * entries. The variable entries MUST NOT be read/written after
6838 int application_reply_code
;
6840 locked_registry
->create_or_find_enum(
6841 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6842 application_reply_code
= 0;
6843 } catch (const std::exception
& ex
) {
6846 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6851 application_reply_code
= -1;
6855 * The return value is returned to ustctl so in case of an error, the
6856 * application can be notified. In case of an error, it's important not to
6857 * return a negative error or else the application will get closed.
6859 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6861 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6862 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6865 } else if (ret
== -EAGAIN
) {
6866 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6870 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6876 * No need to wipe the create enum since the application socket will
6877 * get close on error hence cleaning up everything by itself.
6882 DBG3("UST registry enum %s added successfully or already found", name
);
6887 * Handle application notification through the given notify socket.
6889 * Return 0 on success or else a negative value.
6891 int ust_app_recv_notify(int sock
)
6894 enum lttng_ust_ctl_notify_cmd cmd
;
6896 DBG3("UST app receiving notify from sock %d", sock
);
6898 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6900 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6901 DBG3("UST app recv notify failed. Application died: sock = %d", sock
);
6902 } else if (ret
== -EAGAIN
) {
6903 WARN("UST app recv notify failed. Communication time out: sock = %d", sock
);
6905 ERR("UST app recv notify failed with ret %d: sock = %d", ret
, sock
);
6911 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6913 int sobjd
, cobjd
, loglevel_value
;
6914 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6916 struct lttng_ust_ctl_field
*fields
;
6918 DBG2("UST app ustctl register event received");
6920 ret
= lttng_ust_ctl_recv_register_event(sock
,
6930 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6931 DBG3("UST app recv event failed. Application died: sock = %d",
6933 } else if (ret
== -EAGAIN
) {
6934 WARN("UST app recv event failed. Communication time out: sock = %d",
6937 ERR("UST app recv event failed with ret %d: sock = %d", ret
, sock
);
6943 lttng::urcu::read_lock_guard rcu_lock
;
6944 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6946 DBG("Application socket %d is being torn down. Abort event notify",
6953 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6954 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6963 * Add event to the UST registry coming from the notify socket. This
6964 * call will free if needed the sig, fields and model_emf_uri. This
6965 * code path loses the ownsership of these variables and transfer them
6966 * to the this function.
6968 ret
= add_event_ust_registry(sock
,
6983 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6987 struct lttng_ust_ctl_field
*context_fields
;
6989 DBG2("UST app ustctl register channel received");
6991 ret
= lttng_ust_ctl_recv_register_channel(
6992 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6994 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6995 DBG3("UST app recv channel failed. Application died: sock = %d",
6997 } else if (ret
== -EAGAIN
) {
6998 WARN("UST app recv channel failed. Communication time out: sock = %d",
7001 ERR("UST app recv channel failed with ret %d: sock = %d",
7009 * The fields ownership are transfered to this function call meaning
7010 * that if needed it will be freed. After this, it's invalid to access
7011 * fields or clean them up.
7013 ret
= handle_app_register_channel_notification(
7014 sock
, cobjd
, context_fields
, field_count
);
7021 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
7024 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
7026 struct lttng_ust_ctl_enum_entry
*entries
;
7028 DBG2("UST app ustctl register enum received");
7030 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
, &entries
, &nr_entries
);
7032 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
7033 DBG3("UST app recv enum failed. Application died: sock = %d", sock
);
7034 } else if (ret
== -EAGAIN
) {
7035 WARN("UST app recv enum failed. Communication time out: sock = %d",
7038 ERR("UST app recv enum failed with ret %d: sock = %d", ret
, sock
);
7043 /* Callee assumes ownership of entries. */
7044 ret
= add_enum_ust_registry(sock
, sobjd
, name
, entries
, nr_entries
);
7052 /* Should NEVER happen. */
7061 * Once the notify socket hangs up, this is called. First, it tries to find the
7062 * corresponding application. On failure, the call_rcu to close the socket is
7063 * executed. If an application is found, it tries to delete it from the notify
7064 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7066 * Note that an object needs to be allocated here so on ENOMEM failure, the
7067 * call RCU is not done but the rest of the cleanup is.
7069 void ust_app_notify_sock_unregister(int sock
)
7072 struct lttng_ht_iter iter
;
7073 struct ust_app
*app
;
7074 struct ust_app_notify_sock_obj
*obj
;
7076 LTTNG_ASSERT(sock
>= 0);
7078 lttng::urcu::read_lock_guard read_lock
;
7080 obj
= zmalloc
<ust_app_notify_sock_obj
>();
7083 * An ENOMEM is kind of uncool. If this strikes we continue the
7084 * procedure but the call_rcu will not be called. In this case, we
7085 * accept the fd leak rather than possibly creating an unsynchronized
7086 * state between threads.
7088 * TODO: The notify object should be created once the notify socket is
7089 * registered and stored independantely from the ust app object. The
7090 * tricky part is to synchronize the teardown of the application and
7091 * this notify object. Let's keep that in mind so we can avoid this
7092 * kind of shenanigans with ENOMEM in the teardown path.
7099 DBG("UST app notify socket unregister %d", sock
);
7102 * Lookup application by notify socket. If this fails, this means that the
7103 * hash table delete has already been done by the application
7104 * unregistration process so we can safely close the notify socket in a
7107 app
= find_app_by_notify_sock(sock
);
7112 iter
.iter
.node
= &app
->notify_sock_n
.node
;
7115 * Whatever happens here either we fail or succeed, in both cases we have
7116 * to close the socket after a grace period to continue to the call RCU
7117 * here. If the deletion is successful, the application is not visible
7118 * anymore by other threads and is it fails it means that it was already
7119 * deleted from the hash table so either way we just have to close the
7122 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
7127 * Close socket after a grace period to avoid for the socket to be reused
7128 * before the application object is freed creating potential race between
7129 * threads trying to add unique in the global hash table.
7132 call_rcu(&obj
->head
, close_notify_sock_rcu
);
7137 * Destroy a ust app data structure and free its memory.
7139 void ust_app_destroy(struct ust_app
*app
)
7145 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
7149 * Take a snapshot for a given UST session. The snapshot is sent to the given
7152 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7154 enum lttng_error_code
ust_app_snapshot_record(const struct ltt_ust_session
*usess
,
7155 const struct consumer_output
*output
,
7156 uint64_t nb_packets_per_stream
)
7159 enum lttng_error_code status
= LTTNG_OK
;
7160 struct lttng_ht_iter iter
;
7161 struct ust_app
*app
;
7162 char *trace_path
= nullptr;
7164 LTTNG_ASSERT(usess
);
7165 LTTNG_ASSERT(output
);
7167 switch (usess
->buffer_type
) {
7168 case LTTNG_BUFFER_PER_UID
:
7170 struct buffer_reg_uid
*reg
;
7172 lttng::urcu::read_lock_guard read_lock
;
7174 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7175 struct buffer_reg_channel
*buf_reg_chan
;
7176 struct consumer_socket
*socket
;
7177 char pathname
[PATH_MAX
];
7178 size_t consumer_path_offset
= 0;
7180 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7181 /* Skip since no metadata is present */
7185 /* Get consumer socket to use to push the metadata.*/
7186 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7189 status
= LTTNG_ERR_INVALID
;
7193 memset(pathname
, 0, sizeof(pathname
));
7194 ret
= snprintf(pathname
,
7196 DEFAULT_UST_TRACE_UID_PATH
,
7198 reg
->bits_per_long
);
7200 PERROR("snprintf snapshot path");
7201 status
= LTTNG_ERR_INVALID
;
7204 /* Free path allowed on previous iteration. */
7206 trace_path
= setup_channel_trace_path(
7207 usess
->consumer
, pathname
, &consumer_path_offset
);
7209 status
= LTTNG_ERR_INVALID
;
7212 /* Add the UST default trace dir to path. */
7213 cds_lfht_for_each_entry (
7214 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7216 consumer_snapshot_channel(socket
,
7217 buf_reg_chan
->consumer_key
,
7220 &trace_path
[consumer_path_offset
],
7221 nb_packets_per_stream
);
7222 if (status
!= LTTNG_OK
) {
7226 status
= consumer_snapshot_channel(socket
,
7227 reg
->registry
->reg
.ust
->_metadata_key
,
7230 &trace_path
[consumer_path_offset
],
7232 if (status
!= LTTNG_OK
) {
7239 case LTTNG_BUFFER_PER_PID
:
7241 lttng::urcu::read_lock_guard read_lock
;
7243 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7244 struct consumer_socket
*socket
;
7245 struct lttng_ht_iter chan_iter
;
7246 struct ust_app_channel
*ua_chan
;
7247 struct ust_app_session
*ua_sess
;
7248 lsu::registry_session
*registry
;
7249 char pathname
[PATH_MAX
];
7250 size_t consumer_path_offset
= 0;
7252 ua_sess
= lookup_session_by_app(usess
, app
);
7254 /* Session not associated with this app. */
7258 /* Get the right consumer socket for the application. */
7259 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, output
);
7261 status
= LTTNG_ERR_INVALID
;
7265 /* Add the UST default trace dir to path. */
7266 memset(pathname
, 0, sizeof(pathname
));
7267 ret
= snprintf(pathname
, sizeof(pathname
), "%s", ua_sess
->path
);
7269 status
= LTTNG_ERR_INVALID
;
7270 PERROR("snprintf snapshot path");
7273 /* Free path allowed on previous iteration. */
7275 trace_path
= setup_channel_trace_path(
7276 usess
->consumer
, pathname
, &consumer_path_offset
);
7278 status
= LTTNG_ERR_INVALID
;
7281 cds_lfht_for_each_entry (
7282 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7284 consumer_snapshot_channel(socket
,
7288 &trace_path
[consumer_path_offset
],
7289 nb_packets_per_stream
);
7293 case LTTNG_ERR_CHAN_NOT_FOUND
:
7300 registry
= get_session_registry(ua_sess
);
7302 DBG("Application session is being torn down. Skip application.");
7305 status
= consumer_snapshot_channel(socket
,
7306 registry
->_metadata_key
,
7309 &trace_path
[consumer_path_offset
],
7314 case LTTNG_ERR_CHAN_NOT_FOUND
:
7333 * Return the size taken by one more packet per stream.
7335 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session
*usess
,
7336 uint64_t cur_nr_packets
)
7338 uint64_t tot_size
= 0;
7339 struct ust_app
*app
;
7340 struct lttng_ht_iter iter
;
7342 LTTNG_ASSERT(usess
);
7344 switch (usess
->buffer_type
) {
7345 case LTTNG_BUFFER_PER_UID
:
7347 struct buffer_reg_uid
*reg
;
7349 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7350 struct buffer_reg_channel
*buf_reg_chan
;
7352 lttng::urcu::read_lock_guard read_lock
;
7354 cds_lfht_for_each_entry (
7355 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7356 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7358 * Don't take channel into account if we
7359 * already grab all its packets.
7363 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7368 case LTTNG_BUFFER_PER_PID
:
7370 lttng::urcu::read_lock_guard read_lock
;
7372 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7373 struct ust_app_channel
*ua_chan
;
7374 struct ust_app_session
*ua_sess
;
7375 struct lttng_ht_iter chan_iter
;
7377 ua_sess
= lookup_session_by_app(usess
, app
);
7379 /* Session not associated with this app. */
7383 cds_lfht_for_each_entry (
7384 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7385 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7387 * Don't take channel into account if we
7388 * already grab all its packets.
7392 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7405 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7406 struct cds_list_head
*buffer_reg_uid_list
,
7407 struct consumer_output
*consumer
,
7410 uint64_t *discarded
,
7414 uint64_t consumer_chan_key
;
7419 ret
= buffer_reg_uid_consumer_channel_key(
7420 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7428 ret
= consumer_get_lost_packets(ust_session_id
, consumer_chan_key
, consumer
, lost
);
7430 ret
= consumer_get_discarded_events(
7431 ust_session_id
, consumer_chan_key
, consumer
, discarded
);
7438 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7439 struct ltt_ust_channel
*uchan
,
7440 struct consumer_output
*consumer
,
7442 uint64_t *discarded
,
7446 struct lttng_ht_iter iter
;
7447 struct lttng_ht_node_str
*ua_chan_node
;
7448 struct ust_app
*app
;
7449 struct ust_app_session
*ua_sess
;
7450 struct ust_app_channel
*ua_chan
;
7456 * Iterate over every registered applications. Sum counters for
7457 * all applications containing requested session and channel.
7459 lttng::urcu::read_lock_guard read_lock
;
7461 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7462 struct lttng_ht_iter uiter
;
7464 ua_sess
= lookup_session_by_app(usess
, app
);
7465 if (ua_sess
== nullptr) {
7470 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7471 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7472 /* If the session is found for the app, the channel must be there */
7473 LTTNG_ASSERT(ua_chan_node
);
7475 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7480 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
, consumer
, &_lost
);
7486 uint64_t _discarded
;
7488 ret
= consumer_get_discarded_events(
7489 usess
->id
, ua_chan
->key
, consumer
, &_discarded
);
7493 (*discarded
) += _discarded
;
7500 static int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7503 struct ust_app_session
*ua_sess
;
7505 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7507 lttng::urcu::read_lock_guard read_lock
;
7509 ua_sess
= lookup_session_by_app(usess
, app
);
7510 if (ua_sess
== nullptr) {
7511 /* The session is in teardown process. Ignore and continue. */
7515 pthread_mutex_lock(&ua_sess
->lock
);
7517 if (ua_sess
->deleted
) {
7521 pthread_mutex_lock(&app
->sock_lock
);
7522 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7523 pthread_mutex_unlock(&app
->sock_lock
);
7526 pthread_mutex_unlock(&ua_sess
->lock
);
7529 health_code_update();
7534 * Regenerate the statedump for each app in the session.
7536 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7539 struct lttng_ht_iter iter
;
7540 struct ust_app
*app
;
7542 DBG("Regenerating the metadata for all UST apps");
7544 lttng::urcu::read_lock_guard read_lock
;
7546 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7547 if (!app
->compatible
) {
7551 ret
= ust_app_regenerate_statedump(usess
, app
);
7553 /* Continue to the next app even on error */
7562 * Rotate all the channels of a session.
7564 * Return LTTNG_OK on success or else an LTTng error code.
7566 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7569 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7570 struct lttng_ht_iter iter
;
7571 struct ust_app
*app
;
7572 struct ltt_ust_session
*usess
= session
->ust_session
;
7574 LTTNG_ASSERT(usess
);
7576 switch (usess
->buffer_type
) {
7577 case LTTNG_BUFFER_PER_UID
:
7579 struct buffer_reg_uid
*reg
;
7581 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7582 struct buffer_reg_channel
*buf_reg_chan
;
7583 struct consumer_socket
*socket
;
7584 lttng::urcu::read_lock_guard read_lock
;
7586 /* Get consumer socket to use to push the metadata.*/
7587 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7590 cmd_ret
= LTTNG_ERR_INVALID
;
7594 /* Rotate the data channels. */
7595 cds_lfht_for_each_entry (
7596 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7597 ret
= consumer_rotate_channel(socket
,
7598 buf_reg_chan
->consumer_key
,
7600 /* is_metadata_channel */ false);
7602 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7608 * The metadata channel might not be present.
7610 * Consumer stream allocation can be done
7611 * asynchronously and can fail on intermediary
7612 * operations (i.e add context) and lead to data
7613 * channels created with no metadata channel.
7615 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7616 /* Skip since no metadata is present. */
7621 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7622 (void) push_metadata(locked_registry
, usess
->consumer
);
7625 ret
= consumer_rotate_channel(socket
,
7626 reg
->registry
->reg
.ust
->_metadata_key
,
7628 /* is_metadata_channel */ true);
7630 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7636 case LTTNG_BUFFER_PER_PID
:
7638 lttng::urcu::read_lock_guard read_lock
;
7640 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7641 struct consumer_socket
*socket
;
7642 struct lttng_ht_iter chan_iter
;
7643 struct ust_app_channel
*ua_chan
;
7644 struct ust_app_session
*ua_sess
;
7645 lsu::registry_session
*registry
;
7647 ua_sess
= lookup_session_by_app(usess
, app
);
7649 /* Session not associated with this app. */
7653 /* Get the right consumer socket for the application. */
7654 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7657 cmd_ret
= LTTNG_ERR_INVALID
;
7661 registry
= get_session_registry(ua_sess
);
7663 DBG("Application session is being torn down. Skip application.");
7667 /* Rotate the data channels. */
7668 cds_lfht_for_each_entry (
7669 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7670 ret
= consumer_rotate_channel(socket
,
7673 /* is_metadata_channel */ false);
7675 /* Per-PID buffer and application going away. */
7676 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7678 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7683 /* Rotate the metadata channel. */
7685 auto locked_registry
= registry
->lock();
7687 (void) push_metadata(locked_registry
, usess
->consumer
);
7689 ret
= consumer_rotate_channel(socket
,
7690 registry
->_metadata_key
,
7692 /* is_metadata_channel */ true);
7694 /* Per-PID buffer and application going away. */
7695 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7697 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7714 enum lttng_error_code
ust_app_create_channel_subdirectories(const struct ltt_ust_session
*usess
)
7716 enum lttng_error_code ret
= LTTNG_OK
;
7717 struct lttng_ht_iter iter
;
7718 enum lttng_trace_chunk_status chunk_status
;
7719 char *pathname_index
;
7722 LTTNG_ASSERT(usess
->current_trace_chunk
);
7724 switch (usess
->buffer_type
) {
7725 case LTTNG_BUFFER_PER_UID
:
7727 struct buffer_reg_uid
*reg
;
7728 lttng::urcu::read_lock_guard read_lock
;
7730 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7731 fmt_ret
= asprintf(&pathname_index
,
7732 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
7733 "/" DEFAULT_INDEX_DIR
,
7735 reg
->bits_per_long
);
7737 ERR("Failed to format channel index directory");
7738 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7743 * Create the index subdirectory which will take care
7744 * of implicitly creating the channel's path.
7746 chunk_status
= lttng_trace_chunk_create_subdirectory(
7747 usess
->current_trace_chunk
, pathname_index
);
7748 free(pathname_index
);
7749 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7750 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7756 case LTTNG_BUFFER_PER_PID
:
7758 struct ust_app
*app
;
7759 lttng::urcu::read_lock_guard read_lock
;
7762 * Create the toplevel ust/ directory in case no apps are running.
7764 chunk_status
= lttng_trace_chunk_create_subdirectory(usess
->current_trace_chunk
,
7765 DEFAULT_UST_TRACE_DIR
);
7766 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7767 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7771 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7772 struct ust_app_session
*ua_sess
;
7773 lsu::registry_session
*registry
;
7775 ua_sess
= lookup_session_by_app(usess
, app
);
7777 /* Session not associated with this app. */
7781 registry
= get_session_registry(ua_sess
);
7783 DBG("Application session is being torn down. Skip application.");
7787 fmt_ret
= asprintf(&pathname_index
,
7788 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7791 ERR("Failed to format channel index directory");
7792 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7796 * Create the index subdirectory which will take care
7797 * of implicitly creating the channel's path.
7799 chunk_status
= lttng_trace_chunk_create_subdirectory(
7800 usess
->current_trace_chunk
, pathname_index
);
7801 free(pathname_index
);
7802 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7803 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7819 * Clear all the channels of a session.
7821 * Return LTTNG_OK on success or else an LTTng error code.
7823 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7826 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7827 struct lttng_ht_iter iter
;
7828 struct ust_app
*app
;
7829 struct ltt_ust_session
*usess
= session
->ust_session
;
7831 LTTNG_ASSERT(usess
);
7833 if (usess
->active
) {
7834 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7835 cmd_ret
= LTTNG_ERR_FATAL
;
7839 switch (usess
->buffer_type
) {
7840 case LTTNG_BUFFER_PER_UID
:
7842 struct buffer_reg_uid
*reg
;
7843 lttng::urcu::read_lock_guard read_lock
;
7845 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7846 struct buffer_reg_channel
*buf_reg_chan
;
7847 struct consumer_socket
*socket
;
7849 /* Get consumer socket to use to push the metadata.*/
7850 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7853 cmd_ret
= LTTNG_ERR_INVALID
;
7857 /* Clear the data channels. */
7858 cds_lfht_for_each_entry (
7859 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7860 ret
= consumer_clear_channel(socket
, buf_reg_chan
->consumer_key
);
7867 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7868 (void) push_metadata(locked_registry
, usess
->consumer
);
7872 * Clear the metadata channel.
7873 * Metadata channel is not cleared per se but we still need to
7874 * perform a rotation operation on it behind the scene.
7876 ret
= consumer_clear_channel(socket
, reg
->registry
->reg
.ust
->_metadata_key
);
7883 case LTTNG_BUFFER_PER_PID
:
7885 lttng::urcu::read_lock_guard read_lock
;
7887 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7888 struct consumer_socket
*socket
;
7889 struct lttng_ht_iter chan_iter
;
7890 struct ust_app_channel
*ua_chan
;
7891 struct ust_app_session
*ua_sess
;
7892 lsu::registry_session
*registry
;
7894 ua_sess
= lookup_session_by_app(usess
, app
);
7896 /* Session not associated with this app. */
7900 /* Get the right consumer socket for the application. */
7901 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7904 cmd_ret
= LTTNG_ERR_INVALID
;
7908 registry
= get_session_registry(ua_sess
);
7910 DBG("Application session is being torn down. Skip application.");
7914 /* Clear the data channels. */
7915 cds_lfht_for_each_entry (
7916 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7917 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7919 /* Per-PID buffer and application going away. */
7920 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7928 auto locked_registry
= registry
->lock();
7929 (void) push_metadata(locked_registry
, usess
->consumer
);
7933 * Clear the metadata channel.
7934 * Metadata channel is not cleared per se but we still need to
7935 * perform rotation operation on it behind the scene.
7937 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7939 /* Per-PID buffer and application going away. */
7940 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7958 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7959 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7962 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7971 * This function skips the metadata channel as the begin/end timestamps of a
7972 * metadata packet are useless.
7974 * Moreover, opening a packet after a "clear" will cause problems for live
7975 * sessions as it will introduce padding that was not part of the first trace
7976 * chunk. The relay daemon expects the content of the metadata stream of
7977 * successive metadata trace chunks to be strict supersets of one another.
7979 * For example, flushing a packet at the beginning of the metadata stream of
7980 * a trace chunk resulting from a "clear" session command will cause the
7981 * size of the metadata stream of the new trace chunk to not match the size of
7982 * the metadata stream of the original chunk. This will confuse the relay
7983 * daemon as the same "offset" in a metadata stream will no longer point
7984 * to the same content.
7986 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7988 enum lttng_error_code ret
= LTTNG_OK
;
7989 struct lttng_ht_iter iter
;
7990 struct ltt_ust_session
*usess
= session
->ust_session
;
7992 LTTNG_ASSERT(usess
);
7994 switch (usess
->buffer_type
) {
7995 case LTTNG_BUFFER_PER_UID
:
7997 struct buffer_reg_uid
*reg
;
7999 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
8000 struct buffer_reg_channel
*buf_reg_chan
;
8001 struct consumer_socket
*socket
;
8002 lttng::urcu::read_lock_guard read_lock
;
8004 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
8007 ret
= LTTNG_ERR_FATAL
;
8011 cds_lfht_for_each_entry (
8012 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
8013 const int open_ret
= consumer_open_channel_packets(
8014 socket
, buf_reg_chan
->consumer_key
);
8017 ret
= LTTNG_ERR_UNK
;
8024 case LTTNG_BUFFER_PER_PID
:
8026 struct ust_app
*app
;
8027 lttng::urcu::read_lock_guard read_lock
;
8029 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8030 struct consumer_socket
*socket
;
8031 struct lttng_ht_iter chan_iter
;
8032 struct ust_app_channel
*ua_chan
;
8033 struct ust_app_session
*ua_sess
;
8034 lsu::registry_session
*registry
;
8036 ua_sess
= lookup_session_by_app(usess
, app
);
8038 /* Session not associated with this app. */
8042 /* Get the right consumer socket for the application. */
8043 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
8046 ret
= LTTNG_ERR_FATAL
;
8050 registry
= get_session_registry(ua_sess
);
8052 DBG("Application session is being torn down. Skip application.");
8056 cds_lfht_for_each_entry (
8057 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
8058 const int open_ret
=
8059 consumer_open_channel_packets(socket
, ua_chan
->key
);
8063 * Per-PID buffer and application going
8066 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
8070 ret
= LTTNG_ERR_UNK
;
8086 lsu::ctl_field_quirks
ust_app::ctl_field_quirks() const
8089 * Application contexts are expressed as variants. LTTng-UST announces
8090 * those by registering an enumeration named `..._tag`. It then registers a
8091 * variant as part of the event context that contains the various possible
8094 * Unfortunately, the names used in the enumeration and variant don't
8095 * match: the enumeration names are all prefixed with an underscore while
8096 * the variant type tag fields aren't.
8098 * While the CTF 1.8.3 specification mentions that
8099 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8100 * 1.x (and possibly others) expect a perfect match between the names used
8101 * by tags and variants.
8103 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8104 * the variant's fields are modified to match the mappings of its tag.
8106 * From ABI version >= 10.x, the variant fields and tag mapping names
8107 * correctly match, making this quirk unnecessary.
8109 return v_major
<= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
:
8110 lsu::ctl_field_quirks::NONE
;