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/pthread-lock.hpp>
36 #include <common/sessiond-comm/sessiond-comm.hpp>
37 #include <common/urcu.hpp>
39 #include <lttng/condition/condition.h>
40 #include <lttng/condition/event-rule-matches-internal.hpp>
41 #include <lttng/condition/event-rule-matches.h>
42 #include <lttng/event-rule/event-rule-internal.hpp>
43 #include <lttng/event-rule/event-rule.h>
44 #include <lttng/event-rule/user-tracepoint.h>
45 #include <lttng/trigger/trigger-internal.hpp>
57 #include <sys/types.h>
59 #include <urcu/compiler.h>
62 namespace lsu
= lttng::sessiond::ust
;
63 namespace lst
= lttng::sessiond::trace
;
65 struct lttng_ht
*ust_app_ht
;
66 struct lttng_ht
*ust_app_ht_by_sock
;
67 struct lttng_ht
*ust_app_ht_by_notify_sock
;
69 static int ust_app_flush_app_session(ust_app
& app
, ust_app_session
& ua_sess
);
71 /* Next available channel key. Access under next_channel_key_lock. */
72 static uint64_t _next_channel_key
;
73 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
75 /* Next available session ID. Access under next_session_id_lock. */
76 static uint64_t _next_session_id
;
77 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
82 * Return the session registry according to the buffer type of the given
85 * A registry per UID object MUST exists before calling this function or else
86 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
88 lsu::registry_session
*get_session_registry(const struct ust_app_session
*ua_sess
)
90 lsu::registry_session
*registry
= nullptr;
92 LTTNG_ASSERT(ua_sess
);
94 switch (ua_sess
->buffer_type
) {
95 case LTTNG_BUFFER_PER_PID
:
97 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
101 registry
= reg_pid
->registry
->reg
.ust
;
104 case LTTNG_BUFFER_PER_UID
:
106 struct buffer_reg_uid
*reg_uid
=
107 buffer_reg_uid_find(ua_sess
->tracing_id
,
108 ua_sess
->bits_per_long
,
109 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
113 registry
= reg_uid
->registry
->reg
.ust
;
124 lsu::registry_session::locked_ref
get_locked_session_registry(const struct ust_app_session
*ua_sess
)
126 auto session
= get_session_registry(ua_sess
);
128 pthread_mutex_lock(&session
->_lock
);
131 return lsu::registry_session::locked_ref
{ session
};
136 * Return the incremented value of next_channel_key.
138 static uint64_t get_next_channel_key()
142 pthread_mutex_lock(&next_channel_key_lock
);
143 ret
= ++_next_channel_key
;
144 pthread_mutex_unlock(&next_channel_key_lock
);
149 * Return the atomically incremented value of next_session_id.
151 static uint64_t get_next_session_id()
155 pthread_mutex_lock(&next_session_id_lock
);
156 ret
= ++_next_session_id
;
157 pthread_mutex_unlock(&next_session_id_lock
);
161 static void copy_channel_attr_to_ustctl(struct lttng_ust_ctl_consumer_channel_attr
*attr
,
162 struct lttng_ust_abi_channel_attr
*uattr
)
164 /* Copy event attributes since the layout is different. */
165 attr
->subbuf_size
= uattr
->subbuf_size
;
166 attr
->num_subbuf
= uattr
->num_subbuf
;
167 attr
->overwrite
= uattr
->overwrite
;
168 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
169 attr
->read_timer_interval
= uattr
->read_timer_interval
;
170 attr
->output
= (lttng_ust_abi_output
) uattr
->output
;
171 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
175 * Match function for the hash table lookup.
177 * It matches an ust app event based on three attributes which are the event
178 * name, the filter bytecode and the loglevel.
180 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
182 struct ust_app_event
*event
;
183 const struct ust_app_ht_key
*key
;
188 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
189 key
= (ust_app_ht_key
*) _key
;
191 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
194 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
198 /* Event loglevel. */
199 if (!loglevels_match(event
->attr
.loglevel_type
,
200 event
->attr
.loglevel
,
203 LTTNG_UST_ABI_LOGLEVEL_ALL
)) {
207 /* One of the filters is NULL, fail. */
208 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
212 if (key
->filter
&& event
->filter
) {
213 /* Both filters exists, check length followed by the bytecode. */
214 if (event
->filter
->len
!= key
->filter
->len
||
215 memcmp(event
->filter
->data
, key
->filter
->data
, event
->filter
->len
) != 0) {
220 /* One of the exclusions is NULL, fail. */
221 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
225 if (key
->exclusion
&& event
->exclusion
) {
226 /* Both exclusions exists, check count followed by the names. */
227 if (event
->exclusion
->count
!= key
->exclusion
->count
||
228 memcmp(event
->exclusion
->names
,
229 key
->exclusion
->names
,
230 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
243 * Unique add of an ust app event in the given ht. This uses the custom
244 * ht_match_ust_app_event match function and the event name as hash.
246 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
, struct ust_app_event
*event
)
248 struct cds_lfht_node
*node_ptr
;
249 struct ust_app_ht_key key
;
252 LTTNG_ASSERT(ua_chan
);
253 LTTNG_ASSERT(ua_chan
->events
);
256 ht
= ua_chan
->events
;
257 key
.name
= event
->attr
.name
;
258 key
.filter
= event
->filter
;
259 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel_type
;
260 key
.loglevel_value
= event
->attr
.loglevel
;
261 key
.exclusion
= event
->exclusion
;
263 node_ptr
= cds_lfht_add_unique(ht
->ht
,
264 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
265 ht_match_ust_app_event
,
268 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
272 * Close the notify socket from the given RCU head object. This MUST be called
273 * through a call_rcu().
275 static void close_notify_sock_rcu(struct rcu_head
*head
)
278 struct ust_app_notify_sock_obj
*obj
=
279 lttng::utils::container_of(head
, &ust_app_notify_sock_obj::head
);
281 /* Must have a valid fd here. */
282 LTTNG_ASSERT(obj
->fd
>= 0);
284 ret
= close(obj
->fd
);
286 ERR("close notify sock %d RCU", obj
->fd
);
288 lttng_fd_put(LTTNG_FD_APPS
, 1);
294 * Delete ust context safely. RCU read lock must be held before calling
297 static void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
301 LTTNG_ASSERT(ua_ctx
);
302 ASSERT_RCU_READ_LOCKED();
305 pthread_mutex_lock(&app
->sock_lock
);
306 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
307 pthread_mutex_unlock(&app
->sock_lock
);
309 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
310 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
313 } else if (ret
== -EAGAIN
) {
314 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
318 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
328 if (ua_ctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
329 free(ua_ctx
->ctx
.u
.app_ctx
.provider_name
);
330 free(ua_ctx
->ctx
.u
.app_ctx
.ctx_name
);
337 * Delete ust app event safely. RCU read lock must be held before calling
340 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
, struct ust_app
*app
)
344 LTTNG_ASSERT(ua_event
);
345 ASSERT_RCU_READ_LOCKED();
347 free(ua_event
->filter
);
348 if (ua_event
->exclusion
!= nullptr)
349 free(ua_event
->exclusion
);
350 if (ua_event
->obj
!= nullptr) {
351 pthread_mutex_lock(&app
->sock_lock
);
352 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
353 pthread_mutex_unlock(&app
->sock_lock
);
355 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
356 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
359 } else if (ret
== -EAGAIN
) {
360 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
364 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
376 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
377 * through a call_rcu().
379 static void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
381 struct ust_app_event_notifier_rule
*obj
=
382 lttng::utils::container_of(head
, &ust_app_event_notifier_rule::rcu_head
);
388 * Delete ust app event notifier rule safely.
390 static void delete_ust_app_event_notifier_rule(
391 int sock
, struct ust_app_event_notifier_rule
*ua_event_notifier_rule
, struct ust_app
*app
)
395 LTTNG_ASSERT(ua_event_notifier_rule
);
397 if (ua_event_notifier_rule
->exclusion
!= nullptr) {
398 free(ua_event_notifier_rule
->exclusion
);
401 if (ua_event_notifier_rule
->obj
!= nullptr) {
402 pthread_mutex_lock(&app
->sock_lock
);
403 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
404 pthread_mutex_unlock(&app
->sock_lock
);
406 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
407 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
410 } else if (ret
== -EAGAIN
) {
411 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
415 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
422 free(ua_event_notifier_rule
->obj
);
425 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
426 call_rcu(&ua_event_notifier_rule
->rcu_head
, free_ust_app_event_notifier_rule_rcu
);
430 * Release ust data object of the given stream.
432 * Return 0 on success or else a negative value.
434 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
438 LTTNG_ASSERT(stream
);
441 pthread_mutex_lock(&app
->sock_lock
);
442 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
443 pthread_mutex_unlock(&app
->sock_lock
);
445 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
446 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
449 } else if (ret
== -EAGAIN
) {
450 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
454 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
460 lttng_fd_put(LTTNG_FD_APPS
, 2);
468 * Delete ust app stream safely. RCU read lock must be held before calling
471 static void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
, struct ust_app
*app
)
473 LTTNG_ASSERT(stream
);
474 ASSERT_RCU_READ_LOCKED();
476 (void) release_ust_app_stream(sock
, stream
, app
);
480 static void delete_ust_app_channel_rcu(struct rcu_head
*head
)
482 struct ust_app_channel
*ua_chan
=
483 lttng::utils::container_of(head
, &ust_app_channel::rcu_head
);
485 lttng_ht_destroy(ua_chan
->ctx
);
486 lttng_ht_destroy(ua_chan
->events
);
491 * Extract the lost packet or discarded events counter when the channel is
492 * being deleted and store the value in the parent channel so we can
493 * access it from lttng list and at stop/destroy.
495 * The session list lock must be held by the caller.
497 static void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
499 uint64_t discarded
= 0, lost
= 0;
500 struct ltt_ust_channel
*uchan
;
502 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
506 lttng::urcu::read_lock_guard read_lock
;
509 const auto session
= ltt_session::find_session(ua_chan
->session
->tracing_id
);
511 if (!session
->ust_session
) {
513 * Not finding the session is not an error because there are
514 * multiple ways the channels can be torn down.
516 * 1) The session daemon can initiate the destruction of the
517 * ust app session after receiving a destroy command or
518 * during its shutdown/teardown.
519 * 2) The application, since we are in per-pid tracing, is
520 * unregistering and tearing down its ust app session.
522 * Both paths are protected by the session list lock which
523 * ensures that the accounting of lost packets and discarded
524 * events is done exactly once. The session is then unpublished
525 * from the session list, resulting in this condition.
530 if (ua_chan
->attr
.overwrite
) {
531 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
533 session
->ust_session
->consumer
,
536 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
538 session
->ust_session
->consumer
,
541 uchan
= trace_ust_find_channel_by_name(session
->ust_session
->domain_global
.channels
,
544 ERR("Missing UST channel to store discarded counters");
547 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
548 DBG_FMT("Failed to save per-pid lost/discarded counters: {}, location='{}'",
554 uchan
->per_pid_closed_app_discarded
+= discarded
;
555 uchan
->per_pid_closed_app_lost
+= lost
;
559 * Delete ust app channel safely. RCU read lock must be held before calling
562 * The session list lock must be held by the caller.
564 static void delete_ust_app_channel(int sock
,
565 struct ust_app_channel
*ua_chan
,
567 const lsu::registry_session::locked_ref
& locked_registry
)
570 struct lttng_ht_iter iter
;
571 struct ust_app_event
*ua_event
;
572 struct ust_app_ctx
*ua_ctx
;
573 struct ust_app_stream
*stream
, *stmp
;
575 LTTNG_ASSERT(ua_chan
);
576 ASSERT_RCU_READ_LOCKED();
578 DBG3("UST app deleting channel %s", ua_chan
->name
);
581 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
582 cds_list_del(&stream
->list
);
583 delete_ust_app_stream(sock
, stream
, app
);
587 cds_lfht_for_each_entry (ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
588 cds_list_del(&ua_ctx
->list
);
589 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
591 delete_ust_app_ctx(sock
, ua_ctx
, app
);
595 cds_lfht_for_each_entry (ua_chan
->events
->ht
, &iter
.iter
, ua_event
, node
.node
) {
596 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
598 delete_ust_app_event(sock
, ua_event
, app
);
601 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
602 /* Wipe and free registry from session registry. */
603 if (locked_registry
) {
605 locked_registry
->remove_channel(ua_chan
->key
, sock
>= 0);
606 } catch (const std::exception
& ex
) {
607 DBG("Could not find channel for removal: %s", ex
.what());
612 * A negative socket can be used by the caller when
613 * cleaning-up a ua_chan in an error path. Skip the
614 * accounting in this case.
617 save_per_pid_lost_discarded_counters(ua_chan
);
621 if (ua_chan
->obj
!= nullptr) {
622 /* Remove channel from application UST object descriptor. */
623 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
624 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
626 pthread_mutex_lock(&app
->sock_lock
);
627 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
628 pthread_mutex_unlock(&app
->sock_lock
);
630 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
631 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
635 } else if (ret
== -EAGAIN
) {
636 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
641 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
648 lttng_fd_put(LTTNG_FD_APPS
, 1);
651 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
654 int ust_app_register_done(struct ust_app
*app
)
658 pthread_mutex_lock(&app
->sock_lock
);
659 ret
= lttng_ust_ctl_register_done(app
->sock
);
660 pthread_mutex_unlock(&app
->sock_lock
);
664 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
669 pthread_mutex_lock(&app
->sock_lock
);
674 ret
= lttng_ust_ctl_release_object(sock
, data
);
676 pthread_mutex_unlock(&app
->sock_lock
);
682 * Push metadata to consumer socket.
684 * RCU read-side lock must be held to guarantee existence of socket.
685 * Must be called with the ust app session lock held.
686 * Must be called with the registry lock held.
688 * On success, return the len of metadata pushed or else a negative value.
689 * Returning a -EPIPE return value means we could not send the metadata,
690 * but it can be caused by recoverable errors (e.g. the application has
691 * terminated concurrently).
693 ssize_t
ust_app_push_metadata(const lsu::registry_session::locked_ref
& locked_registry
,
694 struct consumer_socket
*socket
,
698 char *metadata_str
= nullptr;
699 size_t len
, offset
, new_metadata_len_sent
;
701 uint64_t metadata_key
, metadata_version
;
703 LTTNG_ASSERT(locked_registry
);
704 LTTNG_ASSERT(socket
);
705 ASSERT_RCU_READ_LOCKED();
707 metadata_key
= locked_registry
->_metadata_key
;
710 * Means that no metadata was assigned to the session. This can
711 * happens if no start has been done previously.
717 offset
= locked_registry
->_metadata_len_sent
;
718 len
= locked_registry
->_metadata_len
- locked_registry
->_metadata_len_sent
;
719 new_metadata_len_sent
= locked_registry
->_metadata_len
;
720 metadata_version
= locked_registry
->_metadata_version
;
722 DBG3("No metadata to push for metadata key %" PRIu64
,
723 locked_registry
->_metadata_key
);
725 if (send_zero_data
) {
726 DBG("No metadata to push");
732 /* Allocate only what we have to send. */
733 metadata_str
= calloc
<char>(len
);
735 PERROR("zmalloc ust app metadata string");
739 /* Copy what we haven't sent out. */
740 memcpy(metadata_str
, locked_registry
->_metadata
+ offset
, len
);
743 pthread_mutex_unlock(&locked_registry
->_lock
);
745 * We need to unlock the registry while we push metadata to
746 * break a circular dependency between the consumerd metadata
747 * lock and the sessiond registry lock. Indeed, pushing metadata
748 * to the consumerd awaits that it gets pushed all the way to
749 * relayd, but doing so requires grabbing the metadata lock. If
750 * a concurrent metadata request is being performed by
751 * consumerd, this can try to grab the registry lock on the
752 * sessiond while holding the metadata lock on the consumer
753 * daemon. Those push and pull schemes are performed on two
754 * different bidirectionnal communication sockets.
756 ret
= consumer_push_metadata(
757 socket
, metadata_key
, metadata_str
, len
, offset
, metadata_version
);
758 pthread_mutex_lock(&locked_registry
->_lock
);
761 * There is an acceptable race here between the registry
762 * metadata key assignment and the creation on the
763 * consumer. The session daemon can concurrently push
764 * metadata for this registry while being created on the
765 * consumer since the metadata key of the registry is
766 * assigned *before* it is setup to avoid the consumer
767 * to ask for metadata that could possibly be not found
768 * in the session daemon.
770 * The metadata will get pushed either by the session
771 * being stopped or the consumer requesting metadata if
772 * that race is triggered.
774 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
777 ERR("Error pushing metadata to consumer");
783 * Metadata may have been concurrently pushed, since
784 * we're not holding the registry lock while pushing to
785 * consumer. This is handled by the fact that we send
786 * the metadata content, size, and the offset at which
787 * that metadata belongs. This may arrive out of order
788 * on the consumer side, and the consumer is able to
789 * deal with overlapping fragments. The consumer
790 * supports overlapping fragments, which must be
791 * contiguous starting from offset 0. We keep the
792 * largest metadata_len_sent value of the concurrent
795 locked_registry
->_metadata_len_sent
=
796 std::max(locked_registry
->_metadata_len_sent
, new_metadata_len_sent
);
805 * On error, flag the registry that the metadata is
806 * closed. We were unable to push anything and this
807 * means that either the consumer is not responding or
808 * the metadata cache has been destroyed on the
811 locked_registry
->_metadata_closed
= true;
819 * For a given application and session, push metadata to consumer.
820 * Either sock or consumer is required : if sock is NULL, the default
821 * socket to send the metadata is retrieved from consumer, if sock
822 * is not NULL we use it to send the metadata.
823 * RCU read-side lock must be held while calling this function,
824 * therefore ensuring existence of registry. It also ensures existence
825 * of socket throughout this function.
827 * Return 0 on success else a negative error.
828 * Returning a -EPIPE return value means we could not send the metadata,
829 * but it can be caused by recoverable errors (e.g. the application has
830 * terminated concurrently).
832 static int push_metadata(const lsu::registry_session::locked_ref
& locked_registry
,
833 struct consumer_output
*consumer
)
837 struct consumer_socket
*socket
;
839 LTTNG_ASSERT(locked_registry
);
840 LTTNG_ASSERT(consumer
);
841 ASSERT_RCU_READ_LOCKED();
843 if (locked_registry
->_metadata_closed
) {
848 /* Get consumer socket to use to push the metadata.*/
849 socket
= consumer_find_socket_by_bitness(locked_registry
->abi
.bits_per_long
, consumer
);
855 ret
= ust_app_push_metadata(locked_registry
, socket
, 0);
867 * Send to the consumer a close metadata command for the given session. Once
868 * done, the metadata channel is deleted and the session metadata pointer is
869 * nullified. The session lock MUST be held unless the application is
870 * in the destroy path.
872 * Do not hold the registry lock while communicating with the consumerd, because
873 * doing so causes inter-process deadlocks between consumerd and sessiond with
874 * the metadata request notification.
876 * Return 0 on success else a negative value.
878 static int close_metadata(uint64_t metadata_key
,
879 unsigned int consumer_bitness
,
880 struct consumer_output
*consumer
)
883 struct consumer_socket
*socket
;
884 lttng::urcu::read_lock_guard read_lock_guard
;
886 LTTNG_ASSERT(consumer
);
888 /* Get consumer socket to use to push the metadata. */
889 socket
= consumer_find_socket_by_bitness(consumer_bitness
, consumer
);
895 ret
= consumer_close_metadata(socket
, metadata_key
);
904 static void delete_ust_app_session_rcu(struct rcu_head
*head
)
906 struct ust_app_session
*ua_sess
=
907 lttng::utils::container_of(head
, &ust_app_session::rcu_head
);
909 lttng_ht_destroy(ua_sess
->channels
);
914 * Delete ust app session safely. RCU read lock must be held before calling
917 * The session list lock must be held by the caller.
919 static void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
, struct ust_app
*app
)
922 struct lttng_ht_iter iter
;
923 struct ust_app_channel
*ua_chan
;
925 LTTNG_ASSERT(ua_sess
);
926 ASSERT_RCU_READ_LOCKED();
928 pthread_mutex_lock(&ua_sess
->lock
);
930 LTTNG_ASSERT(!ua_sess
->deleted
);
931 ua_sess
->deleted
= true;
933 auto locked_registry
= get_locked_session_registry(ua_sess
);
934 /* Registry can be null on error path during initialization. */
935 if (locked_registry
) {
936 /* Push metadata for application before freeing the application. */
937 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
940 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
941 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
943 delete_ust_app_channel(sock
, ua_chan
, app
, locked_registry
);
946 if (locked_registry
) {
948 * Don't ask to close metadata for global per UID buffers. Close
949 * metadata only on destroy trace session in this case. Also, the
950 * previous push metadata could have flag the metadata registry to
951 * close so don't send a close command if closed.
953 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
954 const auto metadata_key
= locked_registry
->_metadata_key
;
955 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
957 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
958 locked_registry
->_metadata_closed
= true;
961 /* Release lock before communication, see comments in close_metadata(). */
962 locked_registry
.reset();
963 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
967 /* In case of per PID, the registry is kept in the session. */
968 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
969 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
972 * Registry can be null on error path during
975 buffer_reg_pid_remove(reg_pid
);
976 buffer_reg_pid_destroy(reg_pid
);
980 if (ua_sess
->handle
!= -1) {
981 pthread_mutex_lock(&app
->sock_lock
);
982 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
983 pthread_mutex_unlock(&app
->sock_lock
);
985 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
986 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
989 } else if (ret
== -EAGAIN
) {
990 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
994 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
1001 /* Remove session from application UST object descriptor. */
1002 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
1003 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
1007 pthread_mutex_unlock(&ua_sess
->lock
);
1009 consumer_output_put(ua_sess
->consumer
);
1011 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1015 * Delete a traceable application structure from the global list. Never call
1016 * this function outside of a call_rcu call.
1018 static void delete_ust_app(struct ust_app
*app
)
1021 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1022 struct lttng_ht_iter iter
;
1023 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1024 bool event_notifier_write_fd_is_open
;
1027 * The session list lock must be held during this function to guarantee
1028 * the existence of ua_sess.
1030 const auto list_lock
= lttng::sessiond::lock_session_list();
1031 /* Delete ust app sessions info */
1036 cds_list_for_each_entry_safe (ua_sess
, tmp_ua_sess
, &app
->teardown_head
, teardown_node
) {
1037 /* Free every object in the session and the session. */
1038 lttng::urcu::read_lock_guard read_lock
;
1039 delete_ust_app_session(sock
, ua_sess
, app
);
1042 /* Remove the event notifier rules associated with this app. */
1044 lttng::urcu::read_lock_guard read_lock
;
1046 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1048 event_notifier_rule
,
1050 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1053 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
1057 lttng_ht_destroy(app
->sessions
);
1058 lttng_ht_destroy(app
->ust_sessions_objd
);
1059 lttng_ht_destroy(app
->ust_objd
);
1060 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1063 * This could be NULL if the event notifier setup failed (e.g the app
1064 * was killed or the tracer does not support this feature).
1066 if (app
->event_notifier_group
.object
) {
1067 enum lttng_error_code ret_code
;
1068 enum event_notifier_error_accounting_status status
;
1070 const int event_notifier_read_fd
=
1071 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
);
1073 ret_code
= notification_thread_command_remove_tracer_event_source(
1074 the_notification_thread_handle
, event_notifier_read_fd
);
1075 if (ret_code
!= LTTNG_OK
) {
1076 ERR("Failed to remove application tracer event source from notification thread");
1079 status
= event_notifier_error_accounting_unregister_app(app
);
1080 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1081 ERR("Error unregistering app from event notifier error accounting");
1084 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1085 free(app
->event_notifier_group
.object
);
1088 event_notifier_write_fd_is_open
=
1089 lttng_pipe_is_write_open(app
->event_notifier_group
.event_pipe
);
1090 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1092 * Release the file descriptors reserved for the event notifier pipe.
1093 * The app could be destroyed before the write end of the pipe could be
1094 * passed to the application (and closed). In that case, both file
1095 * descriptors must be released.
1097 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1100 * Wait until we have deleted the application from the sock hash table
1101 * before closing this socket, otherwise an application could re-use the
1102 * socket ID and race with the teardown, using the same hash table entry.
1104 * It's OK to leave the close in call_rcu. We want it to stay unique for
1105 * all RCU readers that could run concurrently with unregister app,
1106 * therefore we _need_ to only close that socket after a grace period. So
1107 * it should stay in this RCU callback.
1109 * This close() is a very important step of the synchronization model so
1110 * every modification to this function must be carefully reviewed.
1116 lttng_fd_put(LTTNG_FD_APPS
, 1);
1118 DBG2("UST app pid %d deleted", app
->pid
);
1123 * URCU intermediate call to delete an UST app.
1125 static void delete_ust_app_rcu(struct rcu_head
*head
)
1127 struct lttng_ht_node_ulong
*node
=
1128 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1129 struct ust_app
*app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
1131 DBG3("Call RCU deleting app PID %d", app
->pid
);
1132 delete_ust_app(app
);
1136 * Delete the session from the application ht and delete the data structure by
1137 * freeing every object inside and releasing them.
1139 * The session list lock must be held by the caller.
1141 static void destroy_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
1144 struct lttng_ht_iter iter
;
1147 LTTNG_ASSERT(ua_sess
);
1149 iter
.iter
.node
= &ua_sess
->node
.node
;
1150 ret
= lttng_ht_del(app
->sessions
, &iter
);
1152 /* Already scheduled for teardown. */
1156 /* Once deleted, free the data structure. */
1157 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1164 * Alloc new UST app session.
1166 static struct ust_app_session
*alloc_ust_app_session()
1168 struct ust_app_session
*ua_sess
;
1170 /* Init most of the default value by allocating and zeroing */
1171 ua_sess
= zmalloc
<ust_app_session
>();
1172 if (ua_sess
== nullptr) {
1177 ua_sess
->handle
= -1;
1178 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1179 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1180 pthread_mutex_init(&ua_sess
->lock
, nullptr);
1189 * Alloc new UST app channel.
1191 static struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1192 struct ust_app_session
*ua_sess
,
1193 struct lttng_ust_abi_channel_attr
*attr
)
1195 struct ust_app_channel
*ua_chan
;
1197 /* Init most of the default value by allocating and zeroing */
1198 ua_chan
= zmalloc
<ust_app_channel
>();
1199 if (ua_chan
== nullptr) {
1204 /* Setup channel name */
1205 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1206 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1208 ua_chan
->enabled
= true;
1209 ua_chan
->handle
= -1;
1210 ua_chan
->session
= ua_sess
;
1211 ua_chan
->key
= get_next_channel_key();
1212 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1213 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1214 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1216 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1217 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1219 /* Copy attributes */
1221 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1222 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1223 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1224 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1225 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1226 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1227 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1228 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1230 /* By default, the channel is a per cpu channel. */
1231 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1233 DBG3("UST app channel %s allocated", ua_chan
->name
);
1242 * Allocate and initialize a UST app stream.
1244 * Return newly allocated stream pointer or NULL on error.
1246 struct ust_app_stream
*ust_app_alloc_stream()
1248 struct ust_app_stream
*stream
= nullptr;
1250 stream
= zmalloc
<ust_app_stream
>();
1251 if (stream
== nullptr) {
1252 PERROR("zmalloc ust app stream");
1256 /* Zero could be a valid value for a handle so flag it to -1. */
1257 stream
->handle
= -1;
1264 * Alloc new UST app event.
1266 static struct ust_app_event
*alloc_ust_app_event(char *name
, struct lttng_ust_abi_event
*attr
)
1268 struct ust_app_event
*ua_event
;
1270 /* Init most of the default value by allocating and zeroing */
1271 ua_event
= zmalloc
<ust_app_event
>();
1272 if (ua_event
== nullptr) {
1273 PERROR("Failed to allocate ust_app_event structure");
1277 ua_event
->enabled
= true;
1278 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1279 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1280 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1282 /* Copy attributes */
1284 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1287 DBG3("UST app event %s allocated", ua_event
->name
);
1296 * Allocate a new UST app event notifier rule.
1298 static struct ust_app_event_notifier_rule
*
1299 alloc_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
)
1301 enum lttng_event_rule_generate_exclusions_status generate_exclusion_status
;
1302 enum lttng_condition_status cond_status
;
1303 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1304 struct lttng_condition
*condition
= nullptr;
1305 const struct lttng_event_rule
*event_rule
= nullptr;
1307 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1308 if (ua_event_notifier_rule
== nullptr) {
1309 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1313 ua_event_notifier_rule
->enabled
= true;
1314 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1315 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, ua_event_notifier_rule
->token
);
1317 condition
= lttng_trigger_get_condition(trigger
);
1318 LTTNG_ASSERT(condition
);
1319 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1320 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1322 cond_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
1323 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1324 LTTNG_ASSERT(event_rule
);
1326 ua_event_notifier_rule
->error_counter_index
=
1327 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1328 /* Acquire the event notifier's reference to the trigger. */
1329 lttng_trigger_get(trigger
);
1331 ua_event_notifier_rule
->trigger
= trigger
;
1332 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1333 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1334 event_rule
, &ua_event_notifier_rule
->exclusion
);
1335 switch (generate_exclusion_status
) {
1336 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1337 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1340 /* Error occurred. */
1341 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1342 goto error_put_trigger
;
1345 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1346 ua_event_notifier_rule
->token
);
1348 return ua_event_notifier_rule
;
1351 lttng_trigger_put(trigger
);
1353 free(ua_event_notifier_rule
);
1358 * Alloc new UST app context.
1360 static struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1362 struct ust_app_ctx
*ua_ctx
;
1364 ua_ctx
= zmalloc
<ust_app_ctx
>();
1365 if (ua_ctx
== nullptr) {
1369 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1372 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1373 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1374 char *provider_name
= nullptr, *ctx_name
= nullptr;
1376 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1377 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1378 if (!provider_name
|| !ctx_name
) {
1379 free(provider_name
);
1384 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1385 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1389 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1397 * Create a liblttng-ust filter bytecode from given bytecode.
1399 * Return allocated filter or NULL on error.
1401 static struct lttng_ust_abi_filter_bytecode
*
1402 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1404 struct lttng_ust_abi_filter_bytecode
*filter
= nullptr;
1406 /* Copy filter bytecode. */
1407 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1409 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
1415 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) == sizeof(struct lttng_ust_abi_filter_bytecode
));
1416 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1422 * Create a liblttng-ust capture bytecode from given bytecode.
1424 * Return allocated filter or NULL on error.
1426 static struct lttng_ust_abi_capture_bytecode
*
1427 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1429 struct lttng_ust_abi_capture_bytecode
*capture
= nullptr;
1431 /* Copy capture bytecode. */
1432 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1434 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
1440 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1441 sizeof(struct lttng_ust_abi_capture_bytecode
));
1442 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1448 * Find an ust_app using the sock and return it. RCU read side lock must be
1449 * held before calling this helper function.
1451 struct ust_app
*ust_app_find_by_sock(int sock
)
1453 struct lttng_ht_node_ulong
*node
;
1454 struct lttng_ht_iter iter
;
1456 ASSERT_RCU_READ_LOCKED();
1458 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
1459 node
= lttng_ht_iter_get_node_ulong(&iter
);
1460 if (node
== nullptr) {
1461 DBG2("UST app find by sock %d not found", sock
);
1465 return lttng::utils::container_of(node
, &ust_app::sock_n
);
1472 * Find an ust_app using the notify sock and return it. RCU read side lock must
1473 * be held before calling this helper function.
1475 static struct ust_app
*find_app_by_notify_sock(int sock
)
1477 struct lttng_ht_node_ulong
*node
;
1478 struct lttng_ht_iter iter
;
1480 ASSERT_RCU_READ_LOCKED();
1482 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *) ((unsigned long) sock
), &iter
);
1483 node
= lttng_ht_iter_get_node_ulong(&iter
);
1484 if (node
== nullptr) {
1485 DBG2("UST app find by notify sock %d not found", sock
);
1489 return lttng::utils::container_of(node
, &ust_app::notify_sock_n
);
1496 * Lookup for an ust app event based on event name, filter bytecode and the
1499 * Return an ust_app_event object or NULL on error.
1501 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1503 const struct lttng_bytecode
*filter
,
1504 lttng_ust_abi_loglevel_type loglevel_type
,
1506 const struct lttng_event_exclusion
*exclusion
)
1508 struct lttng_ht_iter iter
;
1509 struct lttng_ht_node_str
*node
;
1510 struct ust_app_event
*event
= nullptr;
1511 struct ust_app_ht_key key
;
1516 /* Setup key for event lookup. */
1518 key
.filter
= filter
;
1519 key
.loglevel_type
= loglevel_type
;
1520 key
.loglevel_value
= loglevel_value
;
1521 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1522 key
.exclusion
= exclusion
;
1524 /* Lookup using the event name as hash and a custom match fct. */
1525 cds_lfht_lookup(ht
->ht
,
1526 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1527 ht_match_ust_app_event
,
1530 node
= lttng_ht_iter_get_node_str(&iter
);
1531 if (node
== nullptr) {
1535 event
= lttng::utils::container_of(node
, &ust_app_event::node
);
1542 * Look-up an event notifier rule based on its token id.
1544 * Must be called with the RCU read lock held.
1545 * Return an ust_app_event_notifier_rule object or NULL on error.
1547 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(struct lttng_ht
*ht
,
1550 struct lttng_ht_iter iter
;
1551 struct lttng_ht_node_u64
*node
;
1552 struct ust_app_event_notifier_rule
*event_notifier_rule
= nullptr;
1555 ASSERT_RCU_READ_LOCKED();
1557 lttng_ht_lookup(ht
, &token
, &iter
);
1558 node
= lttng_ht_iter_get_node_u64(&iter
);
1559 if (node
== nullptr) {
1560 DBG2("UST app event notifier rule token not found: token = %" PRIu64
, token
);
1564 event_notifier_rule
= lttng::utils::container_of(node
, &ust_app_event_notifier_rule::node
);
1566 return event_notifier_rule
;
1570 * Create the channel context on the tracer.
1572 * Called with UST app session lock held.
1574 static int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1575 struct ust_app_ctx
*ua_ctx
,
1576 struct ust_app
*app
)
1580 health_code_update();
1582 pthread_mutex_lock(&app
->sock_lock
);
1583 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
, ua_chan
->obj
, &ua_ctx
->obj
);
1584 pthread_mutex_unlock(&app
->sock_lock
);
1586 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1588 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1591 } else if (ret
== -EAGAIN
) {
1593 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1597 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1605 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1607 DBG2("UST app context handle %d created successfully for channel %s",
1612 health_code_update();
1617 * Set the filter on the tracer.
1619 static int set_ust_object_filter(struct ust_app
*app
,
1620 const struct lttng_bytecode
*bytecode
,
1621 struct lttng_ust_abi_object_data
*ust_object
)
1624 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= nullptr;
1626 health_code_update();
1628 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1629 if (!ust_bytecode
) {
1630 ret
= -LTTNG_ERR_NOMEM
;
1633 pthread_mutex_lock(&app
->sock_lock
);
1634 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
, ust_object
);
1635 pthread_mutex_unlock(&app
->sock_lock
);
1637 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1639 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1642 } else if (ret
== -EAGAIN
) {
1644 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1648 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1657 DBG2("UST filter successfully set: object = %p", ust_object
);
1660 health_code_update();
1666 * Set a capture bytecode for the passed object.
1667 * The sequence number enforces the ordering at runtime and on reception of
1668 * the captured payloads.
1670 static int set_ust_capture(struct ust_app
*app
,
1671 const struct lttng_bytecode
*bytecode
,
1672 unsigned int capture_seqnum
,
1673 struct lttng_ust_abi_object_data
*ust_object
)
1676 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= nullptr;
1678 health_code_update();
1680 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1681 if (!ust_bytecode
) {
1682 ret
= -LTTNG_ERR_NOMEM
;
1687 * Set the sequence number to ensure the capture of fields is ordered.
1689 ust_bytecode
->seqnum
= capture_seqnum
;
1691 pthread_mutex_lock(&app
->sock_lock
);
1692 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
, ust_object
);
1693 pthread_mutex_unlock(&app
->sock_lock
);
1695 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1697 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1700 } else if (ret
== -EAGAIN
) {
1702 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1706 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1715 DBG2("UST capture successfully set: object = %p", ust_object
);
1718 health_code_update();
1723 static struct lttng_ust_abi_event_exclusion
*
1724 create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion
*exclusion
)
1726 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= nullptr;
1727 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1728 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1730 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1731 if (!ust_exclusion
) {
1736 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1737 sizeof(struct lttng_ust_abi_event_exclusion
));
1738 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1740 return ust_exclusion
;
1744 * Set event exclusions on the tracer.
1746 static int set_ust_object_exclusions(struct ust_app
*app
,
1747 const struct lttng_event_exclusion
*exclusions
,
1748 struct lttng_ust_abi_object_data
*ust_object
)
1751 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= nullptr;
1753 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1755 health_code_update();
1757 ust_exclusions
= create_ust_exclusion_from_exclusion(exclusions
);
1758 if (!ust_exclusions
) {
1759 ret
= -LTTNG_ERR_NOMEM
;
1762 pthread_mutex_lock(&app
->sock_lock
);
1763 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1764 pthread_mutex_unlock(&app
->sock_lock
);
1766 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1768 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1771 } else if (ret
== -EAGAIN
) {
1773 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1777 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1786 DBG2("UST exclusions set successfully for object %p", ust_object
);
1789 health_code_update();
1790 free(ust_exclusions
);
1795 * Disable the specified event on to UST tracer for the UST session.
1797 static int disable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*object
)
1801 health_code_update();
1803 pthread_mutex_lock(&app
->sock_lock
);
1804 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1805 pthread_mutex_unlock(&app
->sock_lock
);
1807 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1809 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1812 } else if (ret
== -EAGAIN
) {
1814 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1818 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1827 DBG2("UST app object %p disabled successfully for app: pid = %d", object
, app
->pid
);
1830 health_code_update();
1835 * Disable the specified channel on to UST tracer for the UST session.
1837 static int disable_ust_channel(struct ust_app
*app
,
1838 struct ust_app_session
*ua_sess
,
1839 struct ust_app_channel
*ua_chan
)
1843 health_code_update();
1845 pthread_mutex_lock(&app
->sock_lock
);
1846 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1847 pthread_mutex_unlock(&app
->sock_lock
);
1849 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1851 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1854 } else if (ret
== -EAGAIN
) {
1856 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1860 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1870 DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1873 health_code_update();
1878 * Enable the specified channel on to UST tracer for the UST session.
1880 static int enable_ust_channel(struct ust_app
*app
,
1881 struct ust_app_session
*ua_sess
,
1882 struct ust_app_channel
*ua_chan
)
1886 health_code_update();
1888 pthread_mutex_lock(&app
->sock_lock
);
1889 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1890 pthread_mutex_unlock(&app
->sock_lock
);
1892 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1894 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1898 } else if (ret
== -EAGAIN
) {
1900 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1905 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1915 ua_chan
->enabled
= true;
1917 DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan
->name
, app
->pid
);
1920 health_code_update();
1925 * Enable the specified event on to UST tracer for the UST session.
1927 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1931 health_code_update();
1933 pthread_mutex_lock(&app
->sock_lock
);
1934 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1935 pthread_mutex_unlock(&app
->sock_lock
);
1937 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1939 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1942 } else if (ret
== -EAGAIN
) {
1944 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1948 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1957 DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object
, app
->pid
);
1960 health_code_update();
1965 * Send channel and stream buffer to application.
1967 * Return 0 on success. On error, a negative value is returned.
1969 static int send_channel_pid_to_ust(struct ust_app
*app
,
1970 struct ust_app_session
*ua_sess
,
1971 struct ust_app_channel
*ua_chan
)
1974 struct ust_app_stream
*stream
, *stmp
;
1977 LTTNG_ASSERT(ua_sess
);
1978 LTTNG_ASSERT(ua_chan
);
1980 health_code_update();
1982 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
, app
->sock
);
1984 /* Send channel to the application. */
1985 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1986 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1987 ret
= -ENOTCONN
; /* Caused by app exiting. */
1989 } else if (ret
== -EAGAIN
) {
1990 /* Caused by timeout. */
1991 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
1995 ua_sess
->tracing_id
);
1996 /* Treat this the same way as an application that is exiting. */
1999 } else if (ret
< 0) {
2003 health_code_update();
2005 /* Send all streams to application. */
2006 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2007 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
2008 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2009 ret
= -ENOTCONN
; /* Caused by app exiting. */
2011 } else if (ret
== -EAGAIN
) {
2012 /* Caused by timeout. */
2013 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
2018 ua_sess
->tracing_id
);
2020 * Treat this the same way as an application that is
2024 } else if (ret
< 0) {
2027 /* We don't need the stream anymore once sent to the tracer. */
2028 cds_list_del(&stream
->list
);
2029 delete_ust_app_stream(-1, stream
, app
);
2033 health_code_update();
2038 * Create the specified event onto the UST tracer for a UST session.
2040 * Should be called with session mutex held.
2042 static int create_ust_event(struct ust_app
*app
,
2043 struct ust_app_channel
*ua_chan
,
2044 struct ust_app_event
*ua_event
)
2048 health_code_update();
2050 /* Create UST event on tracer */
2051 pthread_mutex_lock(&app
->sock_lock
);
2052 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
, &ua_event
->obj
);
2053 pthread_mutex_unlock(&app
->sock_lock
);
2055 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2057 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2060 } else if (ret
== -EAGAIN
) {
2062 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2066 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2067 ua_event
->attr
.name
,
2075 ua_event
->handle
= ua_event
->obj
->handle
;
2077 DBG2("UST app event %s created successfully for pid:%d object = %p",
2078 ua_event
->attr
.name
,
2082 health_code_update();
2084 /* Set filter if one is present. */
2085 if (ua_event
->filter
) {
2086 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2092 /* Set exclusions for the event */
2093 if (ua_event
->exclusion
) {
2094 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2100 /* If event not enabled, disable it on the tracer */
2101 if (ua_event
->enabled
) {
2103 * We now need to explicitly enable the event, since it
2104 * is now disabled at creation.
2106 ret
= enable_ust_object(app
, ua_event
->obj
);
2109 * If we hit an EPERM, something is wrong with our enable call. If
2110 * we get an EEXIST, there is a problem on the tracer side since we
2114 case -LTTNG_UST_ERR_PERM
:
2115 /* Code flow problem */
2117 case -LTTNG_UST_ERR_EXIST
:
2118 /* It's OK for our use case. */
2129 health_code_update();
2134 init_ust_event_notifier_from_event_rule(const struct lttng_event_rule
*rule
,
2135 struct lttng_ust_abi_event_notifier
*event_notifier
)
2137 enum lttng_event_rule_status status
;
2138 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2139 int loglevel
= -1, ret
= 0;
2140 const char *pattern
;
2142 memset(event_notifier
, 0, sizeof(*event_notifier
));
2144 if (lttng_event_rule_targets_agent_domain(rule
)) {
2146 * Special event for agents
2147 * The actual meat of the event is in the filter that will be
2148 * attached later on.
2149 * Set the default values for the agent event.
2151 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
2153 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2155 const struct lttng_log_level_rule
*log_level_rule
;
2157 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2158 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2160 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2161 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2162 /* At this point, this is a fatal error. */
2166 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(rule
, &log_level_rule
);
2167 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2168 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2169 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2170 enum lttng_log_level_rule_status llr_status
;
2172 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2173 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2174 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2175 llr_status
= lttng_log_level_rule_exactly_get_level(log_level_rule
,
2178 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2179 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2180 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2181 log_level_rule
, &loglevel
);
2187 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2189 /* At this point this is a fatal error. */
2194 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2195 ret
= lttng_strncpy(
2196 event_notifier
->event
.name
, pattern
, sizeof(event_notifier
->event
.name
));
2198 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern
);
2202 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2203 event_notifier
->event
.loglevel
= loglevel
;
2209 * Create the specified event notifier against the user space tracer of a
2210 * given application.
2212 static int create_ust_event_notifier(struct ust_app
*app
,
2213 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2216 enum lttng_condition_status condition_status
;
2217 const struct lttng_condition
*condition
= nullptr;
2218 struct lttng_ust_abi_event_notifier event_notifier
;
2219 const struct lttng_event_rule
*event_rule
= nullptr;
2220 unsigned int capture_bytecode_count
= 0, i
;
2221 enum lttng_condition_status cond_status
;
2222 enum lttng_event_rule_type event_rule_type
;
2224 health_code_update();
2225 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2227 condition
= lttng_trigger_get_const_condition(ua_event_notifier_rule
->trigger
);
2228 LTTNG_ASSERT(condition
);
2229 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2230 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2232 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2233 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2235 LTTNG_ASSERT(event_rule
);
2237 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2238 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2239 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2240 event_rule_type
== LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2241 event_rule_type
== LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2243 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2244 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2245 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2247 /* Create UST event notifier against the tracer. */
2248 pthread_mutex_lock(&app
->sock_lock
);
2249 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
,
2251 app
->event_notifier_group
.object
,
2252 &ua_event_notifier_rule
->obj
);
2253 pthread_mutex_unlock(&app
->sock_lock
);
2255 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2257 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2260 } else if (ret
== -EAGAIN
) {
2262 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2266 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2267 event_notifier
.event
.name
,
2275 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2277 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2278 event_notifier
.event
.name
,
2281 ua_event_notifier_rule
->obj
);
2283 health_code_update();
2285 /* Set filter if one is present. */
2286 if (ua_event_notifier_rule
->filter
) {
2287 ret
= set_ust_object_filter(
2288 app
, ua_event_notifier_rule
->filter
, ua_event_notifier_rule
->obj
);
2294 /* Set exclusions for the event. */
2295 if (ua_event_notifier_rule
->exclusion
) {
2296 ret
= set_ust_object_exclusions(
2297 app
, ua_event_notifier_rule
->exclusion
, ua_event_notifier_rule
->obj
);
2303 /* Set the capture bytecodes. */
2304 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2305 condition
, &capture_bytecode_count
);
2306 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2308 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2309 const struct lttng_bytecode
*capture_bytecode
=
2310 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2313 ret
= set_ust_capture(app
, capture_bytecode
, i
, ua_event_notifier_rule
->obj
);
2320 * We now need to explicitly enable the event, since it
2321 * is disabled at creation.
2323 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2326 * If we hit an EPERM, something is wrong with our enable call.
2327 * If we get an EEXIST, there is a problem on the tracer side
2328 * since we just created it.
2331 case -LTTNG_UST_ERR_PERM
:
2332 /* Code flow problem. */
2334 case -LTTNG_UST_ERR_EXIST
:
2335 /* It's OK for our use case. */
2345 ua_event_notifier_rule
->enabled
= true;
2348 health_code_update();
2353 * Copy data between an UST app event and a LTT event.
2355 static void shadow_copy_event(struct ust_app_event
*ua_event
, struct ltt_ust_event
*uevent
)
2357 size_t exclusion_alloc_size
;
2359 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2360 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2362 ua_event
->enabled
= uevent
->enabled
;
2364 /* Copy event attributes */
2365 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2367 /* Copy filter bytecode */
2368 if (uevent
->filter
) {
2369 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2370 /* Filter might be NULL here in case of ENONEM. */
2373 /* Copy exclusion data */
2374 if (uevent
->exclusion
) {
2375 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2376 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2377 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2378 if (ua_event
->exclusion
== nullptr) {
2381 memcpy(ua_event
->exclusion
, uevent
->exclusion
, exclusion_alloc_size
);
2387 * Copy data between an UST app channel and a LTT channel.
2389 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
, struct ltt_ust_channel
*uchan
)
2391 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2393 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2394 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2396 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2397 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2399 /* Copy event attributes since the layout is different. */
2400 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2401 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2402 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2403 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2404 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2405 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2406 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2407 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2410 * Note that the attribute channel type is not set since the channel on the
2411 * tracing registry side does not have this information.
2414 ua_chan
->enabled
= uchan
->enabled
;
2415 ua_chan
->tracing_channel_id
= uchan
->id
;
2417 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2421 * Copy data between a UST app session and a regular LTT session.
2423 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2424 struct ltt_ust_session
*usess
,
2425 struct ust_app
*app
)
2427 struct tm
*timeinfo
;
2430 char tmp_shm_path
[PATH_MAX
];
2432 timeinfo
= localtime(&app
->registration_time
);
2433 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2435 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2437 ua_sess
->tracing_id
= usess
->id
;
2438 ua_sess
->id
= get_next_session_id();
2439 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2440 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2441 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2442 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2443 ua_sess
->buffer_type
= usess
->buffer_type
;
2444 ua_sess
->bits_per_long
= app
->abi
.bits_per_long
;
2446 /* There is only one consumer object per session possible. */
2447 consumer_output_get(usess
->consumer
);
2448 ua_sess
->consumer
= usess
->consumer
;
2450 ua_sess
->output_traces
= usess
->output_traces
;
2451 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2452 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &usess
->metadata_attr
);
2454 switch (ua_sess
->buffer_type
) {
2455 case LTTNG_BUFFER_PER_PID
:
2456 ret
= snprintf(ua_sess
->path
,
2457 sizeof(ua_sess
->path
),
2458 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2463 case LTTNG_BUFFER_PER_UID
:
2464 ret
= snprintf(ua_sess
->path
,
2465 sizeof(ua_sess
->path
),
2466 DEFAULT_UST_TRACE_UID_PATH
,
2467 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2468 app
->abi
.bits_per_long
);
2475 PERROR("asprintf UST shadow copy session");
2480 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
, sizeof(ua_sess
->root_shm_path
));
2481 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2482 strncpy(ua_sess
->shm_path
, usess
->shm_path
, sizeof(ua_sess
->shm_path
));
2483 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2484 if (ua_sess
->shm_path
[0]) {
2485 switch (ua_sess
->buffer_type
) {
2486 case LTTNG_BUFFER_PER_PID
:
2487 ret
= snprintf(tmp_shm_path
,
2488 sizeof(tmp_shm_path
),
2489 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2494 case LTTNG_BUFFER_PER_UID
:
2495 ret
= snprintf(tmp_shm_path
,
2496 sizeof(tmp_shm_path
),
2497 "/" DEFAULT_UST_TRACE_UID_PATH
,
2499 app
->abi
.bits_per_long
);
2506 PERROR("sprintf UST shadow copy session");
2510 strncat(ua_sess
->shm_path
,
2512 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2513 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2518 consumer_output_put(ua_sess
->consumer
);
2522 * Lookup sesison wrapper.
2524 static void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2525 struct ust_app
*app
,
2526 struct lttng_ht_iter
*iter
)
2528 /* Get right UST app session from app */
2529 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2533 * Return ust app session from the app session hashtable using the UST session
2536 static struct ust_app_session
*lookup_session_by_app(const struct ltt_ust_session
*usess
,
2537 struct ust_app
*app
)
2539 struct lttng_ht_iter iter
;
2540 struct lttng_ht_node_u64
*node
;
2542 __lookup_session_by_app(usess
, app
, &iter
);
2543 node
= lttng_ht_iter_get_node_u64(&iter
);
2544 if (node
== nullptr) {
2548 return lttng::utils::container_of(node
, &ust_app_session::node
);
2555 * Setup buffer registry per PID for the given session and application. If none
2556 * is found, a new one is created, added to the global registry and
2557 * initialized. If regp is valid, it's set with the newly created object.
2559 * Return 0 on success or else a negative value.
2561 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2562 struct ust_app
*app
,
2563 struct buffer_reg_pid
**regp
)
2566 struct buffer_reg_pid
*reg_pid
;
2568 LTTNG_ASSERT(ua_sess
);
2571 lttng::urcu::read_lock_guard read_lock
;
2573 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2576 * This is the create channel path meaning that if there is NO
2577 * registry available, we have to create one for this session.
2579 ret
= buffer_reg_pid_create(
2580 ua_sess
->id
, ®_pid
, ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2588 /* Initialize registry. */
2589 reg_pid
->registry
->reg
.ust
= ust_registry_session_per_pid_create(
2594 reg_pid
->root_shm_path
,
2596 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2597 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2598 ua_sess
->tracing_id
);
2599 if (!reg_pid
->registry
->reg
.ust
) {
2601 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2602 * destroy the buffer registry, because it is always expected
2603 * that if the buffer registry can be found, its ust registry is
2606 buffer_reg_pid_destroy(reg_pid
);
2610 buffer_reg_pid_add(reg_pid
);
2612 DBG3("UST app buffer registry per PID created successfully");
2623 * Setup buffer registry per UID for the given session and application. If none
2624 * is found, a new one is created, added to the global registry and
2625 * initialized. If regp is valid, it's set with the newly created object.
2627 * Return 0 on success or else a negative value.
2629 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2630 struct ust_app_session
*ua_sess
,
2631 struct ust_app
*app
,
2632 struct buffer_reg_uid
**regp
)
2635 struct buffer_reg_uid
*reg_uid
;
2637 LTTNG_ASSERT(usess
);
2640 lttng::urcu::read_lock_guard read_lock
;
2642 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
2645 * This is the create channel path meaning that if there is NO
2646 * registry available, we have to create one for this session.
2648 ret
= buffer_reg_uid_create(usess
->id
,
2649 app
->abi
.bits_per_long
,
2653 ua_sess
->root_shm_path
,
2662 /* Initialize registry. */
2663 reg_uid
->registry
->reg
.ust
= ust_registry_session_per_uid_create(app
->abi
,
2666 reg_uid
->root_shm_path
,
2670 ua_sess
->tracing_id
,
2672 if (!reg_uid
->registry
->reg
.ust
) {
2674 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2675 * destroy the buffer registry, because it is always expected
2676 * that if the buffer registry can be found, its ust registry is
2679 buffer_reg_uid_destroy(reg_uid
, nullptr);
2683 /* Add node to teardown list of the session. */
2684 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2686 buffer_reg_uid_add(reg_uid
);
2688 DBG3("UST app buffer registry per UID created successfully");
2698 * Create a session on the tracer side for the given app.
2700 * On success, ua_sess_ptr is populated with the session pointer or else left
2701 * untouched. If the session was created, is_created is set to 1. On error,
2702 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2705 * Returns 0 on success or else a negative code which is either -ENOMEM or
2706 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2708 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2709 struct ust_app
*app
,
2710 struct ust_app_session
**ua_sess_ptr
,
2713 int ret
, created
= 0;
2714 struct ust_app_session
*ua_sess
;
2716 LTTNG_ASSERT(usess
);
2718 LTTNG_ASSERT(ua_sess_ptr
);
2720 health_code_update();
2722 ua_sess
= lookup_session_by_app(usess
, app
);
2723 if (ua_sess
== nullptr) {
2724 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2727 ua_sess
= alloc_ust_app_session();
2728 if (ua_sess
== nullptr) {
2729 /* Only malloc can failed so something is really wrong */
2733 shadow_copy_session(ua_sess
, usess
, app
);
2737 switch (usess
->buffer_type
) {
2738 case LTTNG_BUFFER_PER_PID
:
2739 /* Init local registry. */
2740 ret
= setup_buffer_reg_pid(ua_sess
, app
, nullptr);
2742 delete_ust_app_session(-1, ua_sess
, app
);
2746 case LTTNG_BUFFER_PER_UID
:
2747 /* Look for a global registry. If none exists, create one. */
2748 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, nullptr);
2750 delete_ust_app_session(-1, ua_sess
, app
);
2760 health_code_update();
2762 if (ua_sess
->handle
== -1) {
2763 pthread_mutex_lock(&app
->sock_lock
);
2764 ret
= lttng_ust_ctl_create_session(app
->sock
);
2765 pthread_mutex_unlock(&app
->sock_lock
);
2767 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2768 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2772 } else if (ret
== -EAGAIN
) {
2773 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2778 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2783 delete_ust_app_session(-1, ua_sess
, app
);
2784 if (ret
!= -ENOMEM
) {
2786 * Tracer is probably gone or got an internal error so let's
2787 * behave like it will soon unregister or not usable.
2794 ua_sess
->handle
= ret
;
2796 /* Add ust app session to app's HT */
2797 lttng_ht_node_init_u64(&ua_sess
->node
, ua_sess
->tracing_id
);
2798 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2799 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2800 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
, &ua_sess
->ust_objd_node
);
2802 DBG2("UST app session created successfully with handle %d", ret
);
2805 *ua_sess_ptr
= ua_sess
;
2807 *is_created
= created
;
2810 /* Everything went well. */
2814 health_code_update();
2819 * Match function for a hash table lookup of ust_app_ctx.
2821 * It matches an ust app context based on the context type and, in the case
2822 * of perf counters, their name.
2824 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2826 struct ust_app_ctx
*ctx
;
2827 const struct lttng_ust_context_attr
*key
;
2832 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2833 key
= (lttng_ust_context_attr
*) _key
;
2836 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2841 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2842 if (strncmp(key
->u
.perf_counter
.name
,
2843 ctx
->ctx
.u
.perf_counter
.name
,
2844 sizeof(key
->u
.perf_counter
.name
)) != 0) {
2848 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2849 if (strcmp(key
->u
.app_ctx
.provider_name
, ctx
->ctx
.u
.app_ctx
.provider_name
) != 0 ||
2850 strcmp(key
->u
.app_ctx
.ctx_name
, ctx
->ctx
.u
.app_ctx
.ctx_name
) != 0) {
2866 * Lookup for an ust app context from an lttng_ust_context.
2868 * Must be called while holding RCU read side lock.
2869 * Return an ust_app_ctx object or NULL on error.
2871 static struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2872 struct lttng_ust_context_attr
*uctx
)
2874 struct lttng_ht_iter iter
;
2875 struct lttng_ht_node_ulong
*node
;
2876 struct ust_app_ctx
*app_ctx
= nullptr;
2880 ASSERT_RCU_READ_LOCKED();
2882 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2883 cds_lfht_lookup(ht
->ht
,
2884 ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2885 ht_match_ust_app_ctx
,
2888 node
= lttng_ht_iter_get_node_ulong(&iter
);
2893 app_ctx
= lttng::utils::container_of(node
, &ust_app_ctx::node
);
2900 * Create a context for the channel on the tracer.
2902 * Called with UST app session lock held and a RCU read side lock.
2904 static int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2905 struct lttng_ust_context_attr
*uctx
,
2906 struct ust_app
*app
)
2909 struct ust_app_ctx
*ua_ctx
;
2911 ASSERT_RCU_READ_LOCKED();
2913 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2915 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2921 ua_ctx
= alloc_ust_app_ctx(uctx
);
2922 if (ua_ctx
== nullptr) {
2928 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2929 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2930 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2932 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2942 * Enable on the tracer side a ust app event for the session and channel.
2944 * Called with UST app session lock held.
2946 static int enable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2950 ret
= enable_ust_object(app
, ua_event
->obj
);
2955 ua_event
->enabled
= true;
2962 * Disable on the tracer side a ust app event for the session and channel.
2964 static int disable_ust_app_event(struct ust_app_event
*ua_event
, struct ust_app
*app
)
2968 ret
= disable_ust_object(app
, ua_event
->obj
);
2973 ua_event
->enabled
= false;
2980 * Lookup ust app channel for session and disable it on the tracer side.
2982 static int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2983 struct ust_app_channel
*ua_chan
,
2984 struct ust_app
*app
)
2988 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2993 ua_chan
->enabled
= false;
3000 * Lookup ust app channel for session and enable it on the tracer side. This
3001 * MUST be called with a RCU read side lock acquired.
3003 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
3004 struct ltt_ust_channel
*uchan
,
3005 struct ust_app
*app
)
3008 struct lttng_ht_iter iter
;
3009 struct lttng_ht_node_str
*ua_chan_node
;
3010 struct ust_app_channel
*ua_chan
;
3012 ASSERT_RCU_READ_LOCKED();
3014 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3015 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3016 if (ua_chan_node
== nullptr) {
3017 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
3019 ua_sess
->tracing_id
);
3023 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3025 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
3035 * Ask the consumer to create a channel and get it if successful.
3037 * Called with UST app session lock held.
3039 * Return 0 on success or else a negative value.
3041 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
3042 struct ust_app_session
*ua_sess
,
3043 struct ust_app_channel
*ua_chan
,
3045 lsu::registry_session
*registry
)
3048 unsigned int nb_fd
= 0;
3049 struct consumer_socket
*socket
;
3051 LTTNG_ASSERT(usess
);
3052 LTTNG_ASSERT(ua_sess
);
3053 LTTNG_ASSERT(ua_chan
);
3054 LTTNG_ASSERT(registry
);
3056 lttng::urcu::read_lock_guard read_lock
;
3057 health_code_update();
3059 /* Get the right consumer socket for the application. */
3060 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3066 health_code_update();
3068 /* Need one fd for the channel. */
3069 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3071 ERR("Exhausted number of available FD upon create channel");
3076 * Ask consumer to create channel. The consumer will return the number of
3077 * stream we have to expect.
3079 ret
= ust_consumer_ask_channel(
3080 ua_sess
, ua_chan
, usess
->consumer
, socket
, registry
, usess
->current_trace_chunk
);
3086 * Compute the number of fd needed before receiving them. It must be 2 per
3087 * stream (2 being the default value here).
3089 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3091 /* Reserve the amount of file descriptor we need. */
3092 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3094 ERR("Exhausted number of available FD upon create channel");
3095 goto error_fd_get_stream
;
3098 health_code_update();
3101 * Now get the channel from the consumer. This call will populate the stream
3102 * list of that channel and set the ust objects.
3104 if (usess
->consumer
->enabled
) {
3105 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3114 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3115 error_fd_get_stream
:
3117 * Initiate a destroy channel on the consumer since we had an error
3118 * handling it on our side. The return value is of no importance since we
3119 * already have a ret value set by the previous error that we need to
3122 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3124 lttng_fd_put(LTTNG_FD_APPS
, 1);
3126 health_code_update();
3131 * Duplicate the ust data object of the ust app stream and save it in the
3132 * buffer registry stream.
3134 * Return 0 on success or else a negative value.
3136 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3137 struct ust_app_stream
*stream
)
3141 LTTNG_ASSERT(reg_stream
);
3142 LTTNG_ASSERT(stream
);
3144 /* Duplicating a stream requires 2 new fds. Reserve them. */
3145 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3147 ERR("Exhausted number of available FD upon duplicate stream");
3151 /* Duplicate object for stream once the original is in the registry. */
3152 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
, reg_stream
->obj
.ust
);
3154 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3155 reg_stream
->obj
.ust
,
3158 lttng_fd_put(LTTNG_FD_APPS
, 2);
3161 stream
->handle
= stream
->obj
->handle
;
3168 * Duplicate the ust data object of the ust app. channel and save it in the
3169 * buffer registry channel.
3171 * Return 0 on success or else a negative value.
3173 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3174 struct ust_app_channel
*ua_chan
)
3178 LTTNG_ASSERT(buf_reg_chan
);
3179 LTTNG_ASSERT(ua_chan
);
3181 /* Duplicating a channel requires 1 new fd. Reserve it. */
3182 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3184 ERR("Exhausted number of available FD upon duplicate channel");
3188 /* Duplicate object for stream once the original is in the registry. */
3189 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3191 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3192 buf_reg_chan
->obj
.ust
,
3197 ua_chan
->handle
= ua_chan
->obj
->handle
;
3202 lttng_fd_put(LTTNG_FD_APPS
, 1);
3208 * For a given channel buffer registry, setup all streams of the given ust
3209 * application channel.
3211 * Return 0 on success or else a negative value.
3213 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3214 struct ust_app_channel
*ua_chan
,
3215 struct ust_app
*app
)
3218 struct ust_app_stream
*stream
, *stmp
;
3220 LTTNG_ASSERT(buf_reg_chan
);
3221 LTTNG_ASSERT(ua_chan
);
3223 DBG2("UST app setup buffer registry stream");
3225 /* Send all streams to application. */
3226 cds_list_for_each_entry_safe (stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3227 struct buffer_reg_stream
*reg_stream
;
3229 ret
= buffer_reg_stream_create(®_stream
);
3235 * Keep original pointer and nullify it in the stream so the delete
3236 * stream call does not release the object.
3238 reg_stream
->obj
.ust
= stream
->obj
;
3239 stream
->obj
= nullptr;
3240 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3242 /* We don't need the streams anymore. */
3243 cds_list_del(&stream
->list
);
3244 delete_ust_app_stream(-1, stream
, app
);
3252 * Create a buffer registry channel for the given session registry and
3253 * application channel object. If regp pointer is valid, it's set with the
3254 * created object. Important, the created object is NOT added to the session
3255 * registry hash table.
3257 * Return 0 on success else a negative value.
3259 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3260 struct ust_app_channel
*ua_chan
,
3261 struct buffer_reg_channel
**regp
)
3264 struct buffer_reg_channel
*buf_reg_chan
= nullptr;
3266 LTTNG_ASSERT(reg_sess
);
3267 LTTNG_ASSERT(ua_chan
);
3269 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3271 /* Create buffer registry channel. */
3272 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3276 LTTNG_ASSERT(buf_reg_chan
);
3277 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3278 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3279 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3281 /* Create and add a channel registry to session. */
3283 reg_sess
->reg
.ust
->add_channel(ua_chan
->tracing_channel_id
);
3284 } catch (const std::exception
& ex
) {
3285 ERR("Failed to add a channel registry to userspace registry session: %s",
3291 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3294 *regp
= buf_reg_chan
;
3300 /* Safe because the registry channel object was not added to any HT. */
3301 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3307 * Setup buffer registry channel for the given session registry and application
3308 * channel object. If regp pointer is valid, it's set with the created object.
3310 * Return 0 on success else a negative value.
3312 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3313 struct ust_app_channel
*ua_chan
,
3314 struct buffer_reg_channel
*buf_reg_chan
,
3315 struct ust_app
*app
)
3319 LTTNG_ASSERT(reg_sess
);
3320 LTTNG_ASSERT(buf_reg_chan
);
3321 LTTNG_ASSERT(ua_chan
);
3322 LTTNG_ASSERT(ua_chan
->obj
);
3324 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3326 /* Setup all streams for the registry. */
3327 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3332 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3333 ua_chan
->obj
= nullptr;
3338 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3339 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3344 * Send buffer registry channel to the application.
3346 * Return 0 on success else a negative value.
3348 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3349 struct ust_app
*app
,
3350 struct ust_app_session
*ua_sess
,
3351 struct ust_app_channel
*ua_chan
)
3354 struct buffer_reg_stream
*reg_stream
;
3356 LTTNG_ASSERT(buf_reg_chan
);
3358 LTTNG_ASSERT(ua_sess
);
3359 LTTNG_ASSERT(ua_chan
);
3361 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3363 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3368 /* Send channel to the application. */
3369 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3370 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3371 ret
= -ENOTCONN
; /* Caused by app exiting. */
3373 } else if (ret
== -EAGAIN
) {
3374 /* Caused by timeout. */
3375 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
3379 ua_sess
->tracing_id
);
3380 /* Treat this the same way as an application that is exiting. */
3383 } else if (ret
< 0) {
3387 health_code_update();
3389 /* Send all streams to application. */
3390 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3391 cds_list_for_each_entry (reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3392 struct ust_app_stream stream
= {};
3394 ret
= duplicate_stream_object(reg_stream
, &stream
);
3396 goto error_stream_unlock
;
3399 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3401 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3402 ret
= -ENOTCONN
; /* Caused by app exiting. */
3403 } else if (ret
== -EAGAIN
) {
3405 * Caused by timeout.
3406 * Treat this the same way as an application
3409 WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64
3413 ua_sess
->tracing_id
);
3416 (void) release_ust_app_stream(-1, &stream
, app
);
3417 goto error_stream_unlock
;
3421 * The return value is not important here. This function will output an
3424 (void) release_ust_app_stream(-1, &stream
, app
);
3427 error_stream_unlock
:
3428 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3434 * Create and send to the application the created buffers with per UID buffers.
3436 * This MUST be called with a RCU read side lock acquired.
3437 * The session list lock and the session's lock must be acquired.
3439 * Return 0 on success else a negative value.
3441 static int create_channel_per_uid(struct ust_app
*app
,
3442 struct ltt_ust_session
*usess
,
3443 struct ust_app_session
*ua_sess
,
3444 struct ust_app_channel
*ua_chan
)
3447 struct buffer_reg_uid
*reg_uid
;
3448 struct buffer_reg_channel
*buf_reg_chan
;
3449 enum lttng_error_code notification_ret
;
3452 LTTNG_ASSERT(usess
);
3453 LTTNG_ASSERT(ua_sess
);
3454 LTTNG_ASSERT(ua_chan
);
3455 ASSERT_RCU_READ_LOCKED();
3457 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3459 /* Guaranteed to exist; will not throw. */
3460 const auto session
= ltt_session::find_session(ua_sess
->tracing_id
);
3461 ASSERT_SESSION_LIST_LOCKED();
3463 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
3465 * The session creation handles the creation of this global registry
3466 * object. If none can be find, there is a code flow problem or a
3469 LTTNG_ASSERT(reg_uid
);
3471 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
, reg_uid
);
3476 /* Create the buffer registry channel object. */
3477 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3479 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan
->name
);
3484 * Create the buffers on the consumer side. This call populates the
3485 * ust app channel object with all streams and data object.
3487 ret
= do_consumer_create_channel(
3488 usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, reg_uid
->registry
->reg
.ust
);
3490 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3493 * Let's remove the previously created buffer registry channel so
3494 * it's not visible anymore in the session registry.
3496 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3498 locked_registry
->remove_channel(ua_chan
->tracing_channel_id
, false);
3499 } catch (const std::exception
& ex
) {
3500 DBG("Could not find channel for removal: %s", ex
.what());
3502 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3503 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3508 * Setup the streams and add it to the session registry.
3510 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, buf_reg_chan
, app
);
3512 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3517 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3518 auto& ust_reg_chan
= locked_registry
->channel(ua_chan
->tracing_channel_id
);
3520 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3523 /* Notify the notification subsystem of the channel's creation. */
3524 notification_ret
= notification_thread_command_add_channel(
3525 the_notification_thread_handle
,
3530 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3531 if (notification_ret
!= LTTNG_OK
) {
3532 ret
= -(int) notification_ret
;
3533 ERR("Failed to add channel to notification thread");
3538 /* Send buffers to the application. */
3539 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3541 if (ret
!= -ENOTCONN
) {
3542 ERR("Error sending channel to application");
3552 * Create and send to the application the created buffers with per PID buffers.
3554 * Called with UST app session lock held.
3555 * The session list lock and the session's lock must be acquired.
3557 * Return 0 on success else a negative value.
3559 static int create_channel_per_pid(struct ust_app
*app
,
3560 struct ltt_ust_session
*usess
,
3561 struct ust_app_session
*ua_sess
,
3562 struct ust_app_channel
*ua_chan
)
3565 lsu::registry_session
*registry
;
3566 enum lttng_error_code cmd_ret
;
3567 uint64_t chan_reg_key
;
3570 LTTNG_ASSERT(usess
);
3571 LTTNG_ASSERT(ua_sess
);
3572 LTTNG_ASSERT(ua_chan
);
3574 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3576 lttng::urcu::read_lock_guard read_lock
;
3578 registry
= get_session_registry(ua_sess
);
3579 /* The UST app session lock is held, registry shall not be null. */
3580 LTTNG_ASSERT(registry
);
3582 /* Guaranteed to exist; will not throw. */
3583 const auto session
= ltt_session::find_session(ua_sess
->tracing_id
);
3584 ASSERT_LOCKED(session
->_lock
);
3585 ASSERT_SESSION_LIST_LOCKED();
3587 /* Create and add a new channel registry to session. */
3589 registry
->add_channel(ua_chan
->key
);
3590 } catch (const std::exception
& ex
) {
3591 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3598 /* Create and get channel on the consumer side. */
3599 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
, app
->abi
.bits_per_long
, registry
);
3601 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan
->name
);
3602 goto error_remove_from_registry
;
3605 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3607 if (ret
!= -ENOTCONN
) {
3608 ERR("Error sending channel to application");
3610 goto error_remove_from_registry
;
3613 chan_reg_key
= ua_chan
->key
;
3615 auto locked_registry
= registry
->lock();
3617 auto& ust_reg_chan
= locked_registry
->channel(chan_reg_key
);
3618 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3621 cmd_ret
= notification_thread_command_add_channel(the_notification_thread_handle
,
3626 ua_chan
->attr
.subbuf_size
*
3627 ua_chan
->attr
.num_subbuf
);
3628 if (cmd_ret
!= LTTNG_OK
) {
3629 ret
= -(int) cmd_ret
;
3630 ERR("Failed to add channel to notification thread");
3631 goto error_remove_from_registry
;
3634 error_remove_from_registry
:
3637 auto locked_registry
= registry
->lock();
3638 locked_registry
->remove_channel(ua_chan
->key
, false);
3639 } catch (const std::exception
& ex
) {
3640 DBG("Could not find channel for removal: %s", ex
.what());
3648 * From an already allocated ust app channel, create the channel buffers if
3649 * needed and send them to the application. This MUST be called with a RCU read
3650 * side lock acquired.
3652 * Called with UST app session lock held.
3654 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3655 * the application exited concurrently.
3657 static int ust_app_channel_send(struct ust_app
*app
,
3658 struct ltt_ust_session
*usess
,
3659 struct ust_app_session
*ua_sess
,
3660 struct ust_app_channel
*ua_chan
)
3665 LTTNG_ASSERT(usess
);
3666 LTTNG_ASSERT(usess
->active
);
3667 LTTNG_ASSERT(ua_sess
);
3668 LTTNG_ASSERT(ua_chan
);
3669 ASSERT_RCU_READ_LOCKED();
3671 /* Handle buffer type before sending the channel to the application. */
3672 switch (usess
->buffer_type
) {
3673 case LTTNG_BUFFER_PER_UID
:
3675 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3681 case LTTNG_BUFFER_PER_PID
:
3683 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3695 /* Initialize ust objd object using the received handle and add it. */
3696 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3697 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3699 /* If channel is not enabled, disable it on the tracer */
3700 if (!ua_chan
->enabled
) {
3701 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3712 * Create UST app channel and return it through ua_chanp if not NULL.
3714 * Called with UST app session lock and RCU read-side lock held.
3716 * Return 0 on success or else a negative value.
3718 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3719 struct ltt_ust_channel
*uchan
,
3720 enum lttng_ust_abi_chan_type type
,
3721 struct ltt_ust_session
*usess
__attribute__((unused
)),
3722 struct ust_app_channel
**ua_chanp
)
3725 struct lttng_ht_iter iter
;
3726 struct lttng_ht_node_str
*ua_chan_node
;
3727 struct ust_app_channel
*ua_chan
;
3729 ASSERT_RCU_READ_LOCKED();
3731 /* Lookup channel in the ust app session */
3732 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
3733 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3734 if (ua_chan_node
!= nullptr) {
3735 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3739 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3740 if (ua_chan
== nullptr) {
3741 /* Only malloc can fail here */
3745 shadow_copy_channel(ua_chan
, uchan
);
3747 /* Set channel type. */
3748 ua_chan
->attr
.type
= type
;
3750 /* Only add the channel if successful on the tracer side. */
3751 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3754 *ua_chanp
= ua_chan
;
3757 /* Everything went well. */
3765 * Create UST app event and create it on the tracer side.
3767 * Must be called with the RCU read side lock held.
3768 * Called with ust app session mutex held.
3770 static int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3771 struct ltt_ust_event
*uevent
,
3772 struct ust_app
*app
)
3775 struct ust_app_event
*ua_event
;
3777 ASSERT_RCU_READ_LOCKED();
3779 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3780 if (ua_event
== nullptr) {
3781 /* Only failure mode of alloc_ust_app_event(). */
3785 shadow_copy_event(ua_event
, uevent
);
3787 /* Create it on the tracer side */
3788 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3791 * Not found previously means that it does not exist on the
3792 * tracer. If the application reports that the event existed,
3793 * it means there is a bug in the sessiond or lttng-ust
3794 * (or corruption, etc.)
3796 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3797 ERR("Tracer for application reported that an event being created already existed: "
3798 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3808 add_unique_ust_app_event(ua_chan
, ua_event
);
3810 DBG2("UST app create event completed: app = '%s' pid = %d", app
->name
, app
->pid
);
3816 /* Valid. Calling here is already in a read side lock */
3817 delete_ust_app_event(-1, ua_event
, app
);
3822 * Create UST app event notifier rule and create it on the tracer side.
3824 * Must be called with the RCU read side lock held.
3825 * Called with ust app session mutex held.
3827 static int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
, struct ust_app
*app
)
3830 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3832 ASSERT_RCU_READ_LOCKED();
3834 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3835 if (ua_event_notifier_rule
== nullptr) {
3840 /* Create it on the tracer side. */
3841 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3844 * Not found previously means that it does not exist on the
3845 * tracer. If the application reports that the event existed,
3846 * it means there is a bug in the sessiond or lttng-ust
3847 * (or corruption, etc.)
3849 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3850 ERR("Tracer for application reported that an event notifier being created already exists: "
3851 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3852 lttng_trigger_get_tracer_token(trigger
),
3861 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3862 &ua_event_notifier_rule
->node
);
3864 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3867 lttng_trigger_get_tracer_token(trigger
));
3872 /* The RCU read side lock is already being held by the caller. */
3873 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3879 * Create UST metadata and open it on the tracer side.
3881 * Called with UST app session lock held and RCU read side lock.
3883 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3884 struct ust_app
*app
,
3885 struct consumer_output
*consumer
)
3888 struct ust_app_channel
*metadata
;
3889 struct consumer_socket
*socket
;
3891 LTTNG_ASSERT(ua_sess
);
3893 LTTNG_ASSERT(consumer
);
3894 ASSERT_RCU_READ_LOCKED();
3896 auto locked_registry
= get_locked_session_registry(ua_sess
);
3897 /* The UST app session is held registry shall not be null. */
3898 LTTNG_ASSERT(locked_registry
);
3900 /* Guaranteed to exist; will not throw. */
3901 const auto session
= ltt_session::find_session(ua_sess
->tracing_id
);
3902 ASSERT_LOCKED(session
->_lock
);
3903 ASSERT_SESSION_LIST_LOCKED();
3905 /* Metadata already exists for this registry or it was closed previously */
3906 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3911 /* Allocate UST metadata */
3912 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, nullptr);
3914 /* malloc() failed */
3919 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3921 /* Need one fd for the channel. */
3922 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3924 ERR("Exhausted number of available FD upon create metadata");
3928 /* Get the right consumer socket for the application. */
3929 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3932 goto error_consumer
;
3936 * Keep metadata key so we can identify it on the consumer side. Assign it
3937 * to the registry *before* we ask the consumer so we avoid the race of the
3938 * consumer requesting the metadata and the ask_channel call on our side
3939 * did not returned yet.
3941 locked_registry
->_metadata_key
= metadata
->key
;
3944 * Ask the metadata channel creation to the consumer. The metadata object
3945 * will be created by the consumer and kept their. However, the stream is
3946 * never added or monitored until we do a first push metadata to the
3949 ret
= ust_consumer_ask_channel(ua_sess
,
3953 locked_registry
.get(),
3954 session
->current_trace_chunk
);
3956 /* Nullify the metadata key so we don't try to close it later on. */
3957 locked_registry
->_metadata_key
= 0;
3958 goto error_consumer
;
3962 * The setup command will make the metadata stream be sent to the relayd,
3963 * if applicable, and the thread managing the metadatas. This is important
3964 * because after this point, if an error occurs, the only way the stream
3965 * can be deleted is to be monitored in the consumer.
3967 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3969 /* Nullify the metadata key so we don't try to close it later on. */
3970 locked_registry
->_metadata_key
= 0;
3971 goto error_consumer
;
3974 DBG2("UST metadata with key %" PRIu64
" created for app pid %d", metadata
->key
, app
->pid
);
3977 lttng_fd_put(LTTNG_FD_APPS
, 1);
3978 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3984 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3985 * acquired before calling this function.
3987 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3989 struct ust_app
*app
= nullptr;
3990 struct lttng_ht_node_ulong
*node
;
3991 struct lttng_ht_iter iter
;
3993 lttng_ht_lookup(ust_app_ht
, (void *) ((unsigned long) pid
), &iter
);
3994 node
= lttng_ht_iter_get_node_ulong(&iter
);
3995 if (node
== nullptr) {
3996 DBG2("UST app no found with pid %d", pid
);
4000 DBG2("Found UST app by pid %d", pid
);
4002 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
4009 * Allocate and init an UST app object using the registration information and
4010 * the command socket. This is called when the command socket connects to the
4013 * The object is returned on success or else NULL.
4015 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
4018 struct ust_app
*lta
= nullptr;
4019 struct lttng_pipe
*event_notifier_event_source_pipe
= nullptr;
4022 LTTNG_ASSERT(sock
>= 0);
4024 DBG3("UST app creating application for socket %d", sock
);
4026 if ((msg
->bits_per_long
== 64 && (uatomic_read(&the_ust_consumerd64_fd
) == -EINVAL
)) ||
4027 (msg
->bits_per_long
== 32 && (uatomic_read(&the_ust_consumerd32_fd
) == -EINVAL
))) {
4028 ERR("Registration failed: application \"%s\" (pid: %d) has "
4029 "%d-bit long, but no consumerd for this size is available.\n",
4032 msg
->bits_per_long
);
4037 * Reserve the two file descriptors of the event source pipe. The write
4038 * end will be closed once it is passed to the application, at which
4039 * point a single 'put' will be performed.
4041 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
4043 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4049 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
4050 if (!event_notifier_event_source_pipe
) {
4051 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4057 lta
= zmalloc
<ust_app
>();
4058 if (lta
== nullptr) {
4060 goto error_free_pipe
;
4063 urcu_ref_init(<a
->ref
);
4065 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4067 lta
->ppid
= msg
->ppid
;
4068 lta
->uid
= msg
->uid
;
4069 lta
->gid
= msg
->gid
;
4072 .bits_per_long
= msg
->bits_per_long
,
4073 .long_alignment
= msg
->long_alignment
,
4074 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4075 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4076 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4077 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4078 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4079 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4080 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4083 lta
->v_major
= msg
->major
;
4084 lta
->v_minor
= msg
->minor
;
4085 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4086 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4087 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4088 lta
->notify_sock
= -1;
4089 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4091 /* Copy name and make sure it's NULL terminated. */
4092 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4093 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4096 * Before this can be called, when receiving the registration information,
4097 * the application compatibility is checked. So, at this point, the
4098 * application can work with this session daemon.
4100 lta
->compatible
= 1;
4102 lta
->pid
= msg
->pid
;
4103 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4105 pthread_mutex_init(<a
->sock_lock
, nullptr);
4106 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4108 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4112 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4113 lttng_fd_put(LTTNG_FD_APPS
, 2);
4119 * For a given application object, add it to every hash table.
4121 void ust_app_add(struct ust_app
*app
)
4124 LTTNG_ASSERT(app
->notify_sock
>= 0);
4126 app
->registration_time
= time(nullptr);
4128 lttng::urcu::read_lock_guard read_lock
;
4131 * On a re-registration, we want to kick out the previous registration of
4134 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4137 * The socket _should_ be unique until _we_ call close. So, a add_unique
4138 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4139 * already in the table.
4141 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4143 /* Add application to the notify socket hash table. */
4144 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4145 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4147 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4148 "notify_sock =%d (version %d.%d)",
4161 * Set the application version into the object.
4163 * Return 0 on success else a negative value either an errno code or a
4164 * LTTng-UST error code.
4166 int ust_app_version(struct ust_app
*app
)
4172 pthread_mutex_lock(&app
->sock_lock
);
4173 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4174 pthread_mutex_unlock(&app
->sock_lock
);
4176 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4177 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4180 } else if (ret
== -EAGAIN
) {
4181 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4185 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4195 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4197 return app
->v_major
>= 9;
4200 bool ust_app_supports_counters(const struct ust_app
*app
)
4202 return app
->v_major
>= 9;
4206 * Setup the base event notifier group.
4208 * Return 0 on success else a negative value either an errno code or a
4209 * LTTng-UST error code.
4211 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4214 int event_pipe_write_fd
;
4215 struct lttng_ust_abi_object_data
*event_notifier_group
= nullptr;
4216 enum lttng_error_code lttng_ret
;
4217 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4221 if (!ust_app_supports_notifiers(app
)) {
4226 /* Get the write side of the pipe. */
4227 event_pipe_write_fd
= lttng_pipe_get_writefd(app
->event_notifier_group
.event_pipe
);
4229 pthread_mutex_lock(&app
->sock_lock
);
4230 ret
= lttng_ust_ctl_create_event_notifier_group(
4231 app
->sock
, event_pipe_write_fd
, &event_notifier_group
);
4232 pthread_mutex_unlock(&app
->sock_lock
);
4234 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4236 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4239 } else if (ret
== -EAGAIN
) {
4241 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4245 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4249 event_pipe_write_fd
);
4254 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4256 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4263 * Release the file descriptor that was reserved for the write-end of
4266 lttng_fd_put(LTTNG_FD_APPS
, 1);
4268 lttng_ret
= notification_thread_command_add_tracer_event_source(
4269 the_notification_thread_handle
,
4270 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4272 if (lttng_ret
!= LTTNG_OK
) {
4273 ERR("Failed to add tracer event source to notification thread");
4278 /* Assign handle only when the complete setup is valid. */
4279 app
->event_notifier_group
.object
= event_notifier_group
;
4281 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
4282 switch (event_notifier_error_accounting_status
) {
4283 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4285 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4286 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",
4291 goto error_accounting
;
4292 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4293 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4298 goto error_accounting
;
4300 ERR("Failed to setup event notifier error accounting for app");
4302 goto error_accounting
;
4308 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4309 the_notification_thread_handle
,
4310 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
));
4311 if (lttng_ret
!= LTTNG_OK
) {
4312 ERR("Failed to remove application tracer event source from notification thread");
4316 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4317 free(app
->event_notifier_group
.object
);
4318 app
->event_notifier_group
.object
= nullptr;
4322 static void ust_app_unregister(ust_app
& app
)
4324 struct lttng_ht_iter iter
;
4325 struct ust_app_session
*ua_sess
;
4327 lttng::urcu::read_lock_guard read_lock
;
4330 * For per-PID buffers, perform "push metadata" and flush all
4331 * application streams before removing app from hash tables,
4332 * ensuring proper behavior of data_pending check.
4333 * Remove sessions so they are not visible during deletion.
4335 cds_lfht_for_each_entry (app
.sessions
->ht
, &iter
.iter
, ua_sess
, node
.node
) {
4336 const auto del_ret
= lttng_ht_del(app
.sessions
, &iter
);
4338 /* The session was already removed so scheduled for teardown. */
4342 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4343 (void) ust_app_flush_app_session(app
, *ua_sess
);
4347 * Add session to list for teardown. This is safe since at this point we
4348 * are the only one using this list.
4350 lttng::pthread::lock_guard
ust_app_session_lock(ua_sess
->lock
);
4352 if (ua_sess
->deleted
) {
4357 * Normally, this is done in the delete session process which is
4358 * executed in the call rcu below. However, upon registration we can't
4359 * afford to wait for the grace period before pushing data or else the
4360 * data pending feature can race between the unregistration and stop
4361 * command where the data pending command is sent *before* the grace
4364 * The close metadata below nullifies the metadata pointer in the
4365 * session so the delete session will NOT push/close a second time.
4367 auto locked_registry
= get_locked_session_registry(ua_sess
);
4368 if (locked_registry
) {
4369 /* Push metadata for application before freeing the application. */
4370 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4373 * Don't ask to close metadata for global per UID buffers. Close
4374 * metadata only on destroy trace session in this case. Also, the
4375 * previous push metadata could have flag the metadata registry to
4376 * close so don't send a close command if closed.
4378 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4379 const auto metadata_key
= locked_registry
->_metadata_key
;
4380 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4382 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4383 locked_registry
->_metadata_closed
= true;
4386 /* Release lock before communication, see comments in
4387 * close_metadata(). */
4388 locked_registry
.reset();
4389 (void) close_metadata(
4390 metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4392 locked_registry
.reset();
4396 cds_list_add(&ua_sess
->teardown_node
, &app
.teardown_head
);
4400 * Remove application from notify hash table. The thread handling the
4401 * notify socket could have deleted the node so ignore on error because
4402 * either way it's valid. The close of that socket is handled by the
4403 * apps_notify_thread.
4405 iter
.iter
.node
= &app
.notify_sock_n
.node
;
4406 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4409 * Ignore return value since the node might have been removed before by an
4410 * add replace during app registration because the PID can be reassigned by
4413 iter
.iter
.node
= &app
.pid_n
.node
;
4414 if (lttng_ht_del(ust_app_ht
, &iter
)) {
4415 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", app
.pid
);
4420 * Unregister app by removing it from the global traceable app list and freeing
4423 * The socket is already closed at this point, so there is no need to close it.
4425 void ust_app_unregister_by_socket(int sock_fd
)
4427 struct ust_app
*app
;
4428 struct lttng_ht_node_ulong
*node
;
4429 struct lttng_ht_iter ust_app_sock_iter
;
4432 lttng::urcu::read_lock_guard read_lock
;
4434 /* Get the node reference for a call_rcu */
4435 lttng_ht_lookup(ust_app_ht_by_sock
, (void *) ((unsigned long) sock_fd
), &ust_app_sock_iter
);
4436 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4439 app
= caa_container_of(node
, struct ust_app
, sock_n
);
4441 DBG_FMT("Application unregistering after socket activity: pid={}, socket_fd={}",
4445 /* Remove application from socket hash table */
4446 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4450 * The socket is closed: release its reference to the application
4451 * to trigger its eventual teardown.
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);
4792 ust_app_notify_sock_unregister(app
->notify_sock
);
4796 /* Cleanup socket hash table */
4797 if (ust_app_ht_by_sock
) {
4798 lttng::urcu::read_lock_guard read_lock
;
4800 cds_lfht_for_each_entry (ust_app_ht_by_sock
->ht
, &iter
.iter
, app
, sock_n
.node
) {
4801 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4807 /* Destroy is done only when the ht is empty */
4809 lttng_ht_destroy(ust_app_ht
);
4811 if (ust_app_ht_by_sock
) {
4812 lttng_ht_destroy(ust_app_ht_by_sock
);
4814 if (ust_app_ht_by_notify_sock
) {
4815 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4820 * Init UST app hash table.
4822 int ust_app_ht_alloc()
4824 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4828 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4829 if (!ust_app_ht_by_sock
) {
4832 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4833 if (!ust_app_ht_by_notify_sock
) {
4840 * For a specific UST session, disable the channel for all registered apps.
4842 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4845 struct lttng_ht_iter iter
;
4846 struct lttng_ht_node_str
*ua_chan_node
;
4847 struct ust_app
*app
;
4848 struct ust_app_session
*ua_sess
;
4849 struct ust_app_channel
*ua_chan
;
4851 LTTNG_ASSERT(usess
->active
);
4852 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4857 lttng::urcu::read_lock_guard read_lock
;
4859 /* For every registered applications */
4860 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4861 struct lttng_ht_iter uiter
;
4862 if (!app
->compatible
) {
4864 * TODO: In time, we should notice the caller of this error by
4865 * telling him that this is a version error.
4869 ua_sess
= lookup_session_by_app(usess
, app
);
4870 if (ua_sess
== nullptr) {
4875 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4876 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4877 /* If the session if found for the app, the channel must be there */
4878 LTTNG_ASSERT(ua_chan_node
);
4880 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4881 /* The channel must not be already disabled */
4882 LTTNG_ASSERT(ua_chan
->enabled
);
4884 /* Disable channel onto application */
4885 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4887 /* XXX: We might want to report this error at some point... */
4897 * For a specific UST session, enable the channel for all registered apps.
4899 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
4902 struct lttng_ht_iter iter
;
4903 struct ust_app
*app
;
4904 struct ust_app_session
*ua_sess
;
4906 LTTNG_ASSERT(usess
->active
);
4907 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4912 lttng::urcu::read_lock_guard read_lock
;
4914 /* For every registered applications */
4915 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4916 if (!app
->compatible
) {
4918 * TODO: In time, we should notice the caller of this error by
4919 * telling him that this is a version error.
4923 ua_sess
= lookup_session_by_app(usess
, app
);
4924 if (ua_sess
== nullptr) {
4928 /* Enable channel onto application */
4929 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4931 /* XXX: We might want to report this error at some point... */
4941 * Disable an event in a channel and for a specific session.
4943 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4944 struct ltt_ust_channel
*uchan
,
4945 struct ltt_ust_event
*uevent
)
4948 struct lttng_ht_iter iter
, uiter
;
4949 struct lttng_ht_node_str
*ua_chan_node
;
4950 struct ust_app
*app
;
4951 struct ust_app_session
*ua_sess
;
4952 struct ust_app_channel
*ua_chan
;
4953 struct ust_app_event
*ua_event
;
4955 LTTNG_ASSERT(usess
->active
);
4956 DBG("UST app disabling event %s for all apps in channel "
4957 "%s for session id %" PRIu64
,
4963 lttng::urcu::read_lock_guard read_lock
;
4965 /* For all registered applications */
4966 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4967 if (!app
->compatible
) {
4969 * TODO: In time, we should notice the caller of this error by
4970 * telling him that this is a version error.
4974 ua_sess
= lookup_session_by_app(usess
, app
);
4975 if (ua_sess
== nullptr) {
4980 /* Lookup channel in the ust app session */
4981 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
4982 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4983 if (ua_chan_node
== nullptr) {
4984 DBG2("Channel %s not found in session id %" PRIu64
4992 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4994 ua_event
= find_ust_app_event(
4998 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
4999 uevent
->attr
.loglevel
,
5001 if (ua_event
== nullptr) {
5002 DBG2("Event %s not found in channel %s for app pid %d."
5010 ret
= disable_ust_app_event(ua_event
, app
);
5012 /* XXX: Report error someday... */
5021 /* The ua_sess lock must be held by the caller. */
5022 static int ust_app_channel_create(struct ltt_ust_session
*usess
,
5023 struct ust_app_session
*ua_sess
,
5024 struct ltt_ust_channel
*uchan
,
5025 struct ust_app
*app
,
5026 struct ust_app_channel
**_ua_chan
)
5029 struct ust_app_channel
*ua_chan
= nullptr;
5031 LTTNG_ASSERT(ua_sess
);
5032 ASSERT_LOCKED(ua_sess
->lock
);
5034 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
))) {
5035 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
5038 struct ltt_ust_context
*uctx
= nullptr;
5041 * Create channel onto application and synchronize its
5044 ret
= ust_app_channel_allocate(
5045 ua_sess
, uchan
, LTTNG_UST_ABI_CHAN_PER_CPU
, usess
, &ua_chan
);
5050 ret
= ust_app_channel_send(app
, usess
, ua_sess
, ua_chan
);
5056 cds_list_for_each_entry (uctx
, &uchan
->ctx_list
, list
) {
5057 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5069 * The application's socket is not valid. Either a bad socket
5070 * or a timeout on it. We can't inform the caller that for a
5071 * specific app, the session failed so lets continue here.
5073 ret
= 0; /* Not an error. */
5081 if (ret
== 0 && _ua_chan
) {
5083 * Only return the application's channel on success. Note
5084 * that the channel can still be part of the application's
5085 * channel hashtable on error.
5087 *_ua_chan
= ua_chan
;
5093 * Enable event for a specific session and channel on the tracer.
5095 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
5096 struct ltt_ust_channel
*uchan
,
5097 struct ltt_ust_event
*uevent
)
5100 struct lttng_ht_iter iter
, uiter
;
5101 struct lttng_ht_node_str
*ua_chan_node
;
5102 struct ust_app
*app
;
5103 struct ust_app_session
*ua_sess
;
5104 struct ust_app_channel
*ua_chan
;
5105 struct ust_app_event
*ua_event
;
5107 LTTNG_ASSERT(usess
->active
);
5108 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
5113 * NOTE: At this point, this function is called only if the session and
5114 * channel passed are already created for all apps. and enabled on the
5119 lttng::urcu::read_lock_guard read_lock
;
5121 /* For all registered applications */
5122 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5123 if (!app
->compatible
) {
5125 * TODO: In time, we should notice the caller of this error by
5126 * telling him that this is a version error.
5130 ua_sess
= lookup_session_by_app(usess
, app
);
5132 /* The application has problem or is probably dead. */
5136 pthread_mutex_lock(&ua_sess
->lock
);
5138 if (ua_sess
->deleted
) {
5139 pthread_mutex_unlock(&ua_sess
->lock
);
5143 /* Lookup channel in the ust app session */
5144 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5145 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5147 * It is possible that the channel cannot be found is
5148 * the channel/event creation occurs concurrently with
5149 * an application exit.
5151 if (!ua_chan_node
) {
5152 pthread_mutex_unlock(&ua_sess
->lock
);
5156 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5158 /* Get event node */
5159 ua_event
= find_ust_app_event(
5163 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
5164 uevent
->attr
.loglevel
,
5166 if (ua_event
== nullptr) {
5167 DBG3("UST app enable event %s not found for app PID %d."
5174 ret
= enable_ust_app_event(ua_event
, app
);
5176 pthread_mutex_unlock(&ua_sess
->lock
);
5180 pthread_mutex_unlock(&ua_sess
->lock
);
5188 * For a specific existing UST session and UST channel, creates the event for
5189 * all registered apps.
5191 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5192 struct ltt_ust_channel
*uchan
,
5193 struct ltt_ust_event
*uevent
)
5196 struct lttng_ht_iter iter
, uiter
;
5197 struct lttng_ht_node_str
*ua_chan_node
;
5198 struct ust_app
*app
;
5199 struct ust_app_session
*ua_sess
;
5200 struct ust_app_channel
*ua_chan
;
5202 LTTNG_ASSERT(usess
->active
);
5203 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5208 lttng::urcu::read_lock_guard read_lock
;
5210 /* For all registered applications */
5211 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5212 if (!app
->compatible
) {
5214 * TODO: In time, we should notice the caller of this error by
5215 * telling him that this is a version error.
5220 ua_sess
= lookup_session_by_app(usess
, app
);
5222 /* The application has problem or is probably dead. */
5226 pthread_mutex_lock(&ua_sess
->lock
);
5228 if (ua_sess
->deleted
) {
5229 pthread_mutex_unlock(&ua_sess
->lock
);
5233 /* Lookup channel in the ust app session */
5234 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5235 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5236 /* If the channel is not found, there is a code flow error */
5237 LTTNG_ASSERT(ua_chan_node
);
5239 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5241 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5242 pthread_mutex_unlock(&ua_sess
->lock
);
5244 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5245 /* Possible value at this point: -ENOMEM. If so, we stop! */
5249 DBG2("UST app event %s already exist on app PID %d",
5261 * Start tracing for a specific UST session and app.
5263 * Called with UST app session lock held.
5266 static int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5269 struct ust_app_session
*ua_sess
;
5271 DBG("Starting tracing for ust app pid %d", app
->pid
);
5273 lttng::urcu::read_lock_guard read_lock
;
5275 if (!app
->compatible
) {
5279 ua_sess
= lookup_session_by_app(usess
, app
);
5280 if (ua_sess
== nullptr) {
5281 /* The session is in teardown process. Ignore and continue. */
5285 pthread_mutex_lock(&ua_sess
->lock
);
5287 if (ua_sess
->deleted
) {
5288 pthread_mutex_unlock(&ua_sess
->lock
);
5292 if (ua_sess
->enabled
) {
5293 pthread_mutex_unlock(&ua_sess
->lock
);
5297 /* Upon restart, we skip the setup, already done */
5298 if (ua_sess
->started
) {
5302 health_code_update();
5305 /* This starts the UST tracing */
5306 pthread_mutex_lock(&app
->sock_lock
);
5307 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5308 pthread_mutex_unlock(&app
->sock_lock
);
5310 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5311 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5314 pthread_mutex_unlock(&ua_sess
->lock
);
5316 } else if (ret
== -EAGAIN
) {
5317 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5320 pthread_mutex_unlock(&ua_sess
->lock
);
5324 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5332 /* Indicate that the session has been started once */
5333 ua_sess
->started
= true;
5334 ua_sess
->enabled
= true;
5336 pthread_mutex_unlock(&ua_sess
->lock
);
5338 health_code_update();
5340 /* Quiescent wait after starting trace */
5341 pthread_mutex_lock(&app
->sock_lock
);
5342 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5343 pthread_mutex_unlock(&app
->sock_lock
);
5345 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5346 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5349 } else if (ret
== -EAGAIN
) {
5350 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5354 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5362 health_code_update();
5366 pthread_mutex_unlock(&ua_sess
->lock
);
5367 health_code_update();
5372 * Stop tracing for a specific UST session and app.
5374 static int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5377 struct ust_app_session
*ua_sess
;
5379 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5381 lttng::urcu::read_lock_guard read_lock
;
5383 if (!app
->compatible
) {
5384 goto end_no_session
;
5387 ua_sess
= lookup_session_by_app(usess
, app
);
5388 if (ua_sess
== nullptr) {
5389 goto end_no_session
;
5392 pthread_mutex_lock(&ua_sess
->lock
);
5394 if (ua_sess
->deleted
) {
5395 pthread_mutex_unlock(&ua_sess
->lock
);
5396 goto end_no_session
;
5400 * If started = 0, it means that stop trace has been called for a session
5401 * that was never started. It's possible since we can have a fail start
5402 * from either the application manager thread or the command thread. Simply
5403 * indicate that this is a stop error.
5405 if (!ua_sess
->started
) {
5406 goto error_rcu_unlock
;
5409 health_code_update();
5411 /* This inhibits UST tracing */
5412 pthread_mutex_lock(&app
->sock_lock
);
5413 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5414 pthread_mutex_unlock(&app
->sock_lock
);
5416 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5417 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5421 } else if (ret
== -EAGAIN
) {
5422 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5428 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5433 goto error_rcu_unlock
;
5436 health_code_update();
5437 ua_sess
->enabled
= false;
5439 /* Quiescent wait after stopping trace */
5440 pthread_mutex_lock(&app
->sock_lock
);
5441 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5442 pthread_mutex_unlock(&app
->sock_lock
);
5444 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5445 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5448 } else if (ret
== -EAGAIN
) {
5449 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5453 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5460 health_code_update();
5463 auto locked_registry
= get_locked_session_registry(ua_sess
);
5465 /* The UST app session is held registry shall not be null. */
5466 LTTNG_ASSERT(locked_registry
);
5468 /* Push metadata for application before freeing the application. */
5469 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5473 pthread_mutex_unlock(&ua_sess
->lock
);
5475 health_code_update();
5479 pthread_mutex_unlock(&ua_sess
->lock
);
5480 health_code_update();
5484 static int ust_app_flush_app_session(ust_app
& app
, ust_app_session
& ua_sess
)
5486 int ret
, retval
= 0;
5487 struct lttng_ht_iter iter
;
5488 struct ust_app_channel
*ua_chan
;
5489 struct consumer_socket
*socket
;
5491 DBG("Flushing app session buffers for ust app pid %d", app
.pid
);
5493 if (!app
.compatible
) {
5494 goto end_not_compatible
;
5497 pthread_mutex_lock(&ua_sess
.lock
);
5499 if (ua_sess
.deleted
) {
5503 health_code_update();
5505 /* Flushing buffers */
5506 socket
= consumer_find_socket_by_bitness(app
.abi
.bits_per_long
, ua_sess
.consumer
);
5508 /* Flush buffers and push metadata. */
5509 switch (ua_sess
.buffer_type
) {
5510 case LTTNG_BUFFER_PER_PID
:
5512 lttng::urcu::read_lock_guard read_lock
;
5514 cds_lfht_for_each_entry (ua_sess
.channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5515 health_code_update();
5516 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5518 ERR("Error flushing consumer channel");
5526 case LTTNG_BUFFER_PER_UID
:
5532 health_code_update();
5535 pthread_mutex_unlock(&ua_sess
.lock
);
5538 health_code_update();
5543 * Flush buffers for all applications for a specific UST session.
5544 * Called with UST session lock held.
5546 static int ust_app_flush_session(struct ltt_ust_session
*usess
)
5551 DBG("Flushing session buffers for all ust apps");
5553 /* Flush buffers and push metadata. */
5554 switch (usess
->buffer_type
) {
5555 case LTTNG_BUFFER_PER_UID
:
5557 struct buffer_reg_uid
*reg
;
5558 struct lttng_ht_iter iter
;
5560 /* Flush all per UID buffers associated to that session. */
5561 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5562 lttng::urcu::read_lock_guard read_lock
;
5563 lsu::registry_session
*ust_session_reg
;
5564 struct buffer_reg_channel
*buf_reg_chan
;
5565 struct consumer_socket
*socket
;
5567 /* Get consumer socket to use to push the metadata.*/
5568 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5571 /* Ignore request if no consumer is found for the session. */
5575 cds_lfht_for_each_entry (
5576 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5578 * The following call will print error values so the return
5579 * code is of little importance because whatever happens, we
5580 * have to try them all.
5582 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5585 ust_session_reg
= reg
->registry
->reg
.ust
;
5586 /* Push metadata. */
5587 auto locked_registry
= ust_session_reg
->lock();
5588 (void) push_metadata(locked_registry
, usess
->consumer
);
5593 case LTTNG_BUFFER_PER_PID
:
5595 struct ust_app_session
*ua_sess
;
5596 struct lttng_ht_iter iter
;
5597 struct ust_app
*app
;
5598 lttng::urcu::read_lock_guard read_lock
;
5600 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5601 ua_sess
= lookup_session_by_app(usess
, app
);
5602 if (ua_sess
== nullptr) {
5606 (void) ust_app_flush_app_session(*app
, *ua_sess
);
5617 health_code_update();
5621 static int ust_app_clear_quiescent_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
5624 struct lttng_ht_iter iter
;
5625 struct ust_app_channel
*ua_chan
;
5626 struct consumer_socket
*socket
;
5628 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5630 lttng::urcu::read_lock_guard read_lock
;
5632 if (!app
->compatible
) {
5633 goto end_not_compatible
;
5636 pthread_mutex_lock(&ua_sess
->lock
);
5638 if (ua_sess
->deleted
) {
5642 health_code_update();
5644 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, ua_sess
->consumer
);
5646 ERR("Failed to find consumer (%" PRIu32
") socket", app
->abi
.bits_per_long
);
5651 /* Clear quiescent state. */
5652 switch (ua_sess
->buffer_type
) {
5653 case LTTNG_BUFFER_PER_PID
:
5654 cds_lfht_for_each_entry (ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
, node
.node
) {
5655 health_code_update();
5656 ret
= consumer_clear_quiescent_channel(socket
, ua_chan
->key
);
5658 ERR("Error clearing quiescent state for consumer channel");
5664 case LTTNG_BUFFER_PER_UID
:
5671 health_code_update();
5674 pthread_mutex_unlock(&ua_sess
->lock
);
5677 health_code_update();
5682 * Clear quiescent state in each stream for all applications for a
5683 * specific UST session.
5684 * Called with UST session lock held.
5686 static int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5691 DBG("Clearing stream quiescent state for all ust apps");
5693 switch (usess
->buffer_type
) {
5694 case LTTNG_BUFFER_PER_UID
:
5696 struct lttng_ht_iter iter
;
5697 struct buffer_reg_uid
*reg
;
5700 * Clear quiescent for all per UID buffers associated to
5703 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5704 struct consumer_socket
*socket
;
5705 struct buffer_reg_channel
*buf_reg_chan
;
5706 lttng::urcu::read_lock_guard read_lock
;
5708 /* Get associated consumer socket.*/
5709 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5713 * Ignore request if no consumer is found for
5719 cds_lfht_for_each_entry (
5720 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
5722 * The following call will print error values so
5723 * the return code is of little importance
5724 * because whatever happens, we have to try them
5727 (void) consumer_clear_quiescent_channel(socket
,
5728 buf_reg_chan
->consumer_key
);
5734 case LTTNG_BUFFER_PER_PID
:
5736 struct ust_app_session
*ua_sess
;
5737 struct lttng_ht_iter iter
;
5738 struct ust_app
*app
;
5739 lttng::urcu::read_lock_guard read_lock
;
5741 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5742 ua_sess
= lookup_session_by_app(usess
, app
);
5743 if (ua_sess
== nullptr) {
5746 (void) ust_app_clear_quiescent_app_session(app
, ua_sess
);
5757 health_code_update();
5762 * Destroy a specific UST session in apps.
5764 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5767 struct ust_app_session
*ua_sess
;
5768 struct lttng_ht_iter iter
;
5769 struct lttng_ht_node_u64
*node
;
5771 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5773 lttng::urcu::read_lock_guard read_lock
;
5775 if (!app
->compatible
) {
5779 __lookup_session_by_app(usess
, app
, &iter
);
5780 node
= lttng_ht_iter_get_node_u64(&iter
);
5781 if (node
== nullptr) {
5782 /* Session is being or is deleted. */
5785 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5787 health_code_update();
5788 destroy_app_session(app
, ua_sess
);
5790 health_code_update();
5792 /* Quiescent wait after stopping trace */
5793 pthread_mutex_lock(&app
->sock_lock
);
5794 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5795 pthread_mutex_unlock(&app
->sock_lock
);
5797 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5798 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5801 } else if (ret
== -EAGAIN
) {
5802 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5806 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5813 health_code_update();
5818 * Start tracing for the UST session.
5820 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5822 struct lttng_ht_iter iter
;
5823 struct ust_app
*app
;
5825 DBG("Starting all UST traces");
5828 * Even though the start trace might fail, flag this session active so
5829 * other application coming in are started by default.
5831 usess
->active
= true;
5834 * In a start-stop-start use-case, we need to clear the quiescent state
5835 * of each channel set by the prior stop command, thus ensuring that a
5836 * following stop or destroy is sure to grab a timestamp_end near those
5837 * operations, even if the packet is empty.
5839 (void) ust_app_clear_quiescent_session(usess
);
5842 lttng::urcu::read_lock_guard read_lock
;
5844 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5845 ust_app_global_update(usess
, app
);
5853 * Start tracing for the UST session.
5854 * Called with UST session lock held.
5856 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5859 struct lttng_ht_iter iter
;
5860 struct ust_app
*app
;
5862 DBG("Stopping all UST traces");
5865 * Even though the stop trace might fail, flag this session inactive so
5866 * other application coming in are not started by default.
5868 usess
->active
= false;
5871 lttng::urcu::read_lock_guard read_lock
;
5873 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5874 ret
= ust_app_stop_trace(usess
, app
);
5876 /* Continue to next apps even on error */
5882 (void) ust_app_flush_session(usess
);
5888 * Destroy app UST session.
5890 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5893 struct lttng_ht_iter iter
;
5894 struct ust_app
*app
;
5896 DBG("Destroy all UST traces");
5899 lttng::urcu::read_lock_guard read_lock
;
5901 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5902 ret
= destroy_trace(usess
, app
);
5904 /* Continue to next apps even on error */
5913 /* The ua_sess lock must be held by the caller. */
5914 static int find_or_create_ust_app_channel(struct ltt_ust_session
*usess
,
5915 struct ust_app_session
*ua_sess
,
5916 struct ust_app
*app
,
5917 struct ltt_ust_channel
*uchan
,
5918 struct ust_app_channel
**ua_chan
)
5921 struct lttng_ht_iter iter
;
5922 struct lttng_ht_node_str
*ua_chan_node
;
5924 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5925 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5927 *ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5931 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5939 static int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5940 struct ltt_ust_event
*uevent
,
5941 struct ust_app
*app
)
5944 struct ust_app_event
*ua_event
= nullptr;
5946 ua_event
= find_ust_app_event(ua_chan
->events
,
5949 (enum lttng_ust_abi_loglevel_type
) uevent
->attr
.loglevel_type
,
5950 uevent
->attr
.loglevel
,
5953 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5958 if (ua_event
->enabled
!= uevent
->enabled
) {
5959 ret
= uevent
->enabled
? enable_ust_app_event(ua_event
, app
) :
5960 disable_ust_app_event(ua_event
, app
);
5968 /* Called with RCU read-side lock held. */
5969 static void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5972 enum lttng_error_code ret_code
;
5973 enum lttng_trigger_status t_status
;
5974 struct lttng_ht_iter app_trigger_iter
;
5975 struct lttng_triggers
*triggers
= nullptr;
5976 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5977 unsigned int count
, i
;
5979 ASSERT_RCU_READ_LOCKED();
5981 if (!ust_app_supports_notifiers(app
)) {
5986 * Currrently, registering or unregistering a trigger with an
5987 * event rule condition causes a full synchronization of the event
5990 * The first step attempts to add an event notifier for all registered
5991 * triggers that apply to the user space tracers. Then, the
5992 * application's event notifiers rules are all checked against the list
5993 * of registered triggers. Any event notifier that doesn't have a
5994 * matching trigger can be assumed to have been disabled.
5996 * All of this is inefficient, but is put in place to get the feature
5997 * rolling as it is simpler at this moment. It will be optimized Soon™
5998 * to allow the state of enabled
5999 * event notifiers to be synchronized in a piece-wise way.
6002 /* Get all triggers using uid 0 (root) */
6003 ret_code
= notification_thread_command_list_triggers(
6004 the_notification_thread_handle
, 0, &triggers
);
6005 if (ret_code
!= LTTNG_OK
) {
6009 LTTNG_ASSERT(triggers
);
6011 t_status
= lttng_triggers_get_count(triggers
, &count
);
6012 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
6016 for (i
= 0; i
< count
; i
++) {
6017 const struct lttng_condition
*condition
;
6018 const struct lttng_event_rule
*event_rule
;
6019 struct lttng_trigger
*trigger
;
6020 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
6021 enum lttng_condition_status condition_status
;
6024 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
6025 LTTNG_ASSERT(trigger
);
6027 token
= lttng_trigger_get_tracer_token(trigger
);
6028 condition
= lttng_trigger_get_const_condition(trigger
);
6030 if (lttng_condition_get_type(condition
) !=
6031 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
6032 /* Does not apply */
6037 lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
6038 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
6040 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
6041 /* Skip kernel related triggers. */
6046 * Find or create the associated token event rule. The caller
6047 * holds the RCU read lock, so this is safe to call without
6048 * explicitly acquiring it here.
6050 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
6051 app
->token_to_event_notifier_rule_ht
, token
);
6052 if (!looked_up_event_notifier_rule
) {
6053 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
6061 lttng::urcu::read_lock_guard read_lock
;
6063 /* Remove all unknown event sources from the app. */
6064 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
6065 &app_trigger_iter
.iter
,
6066 event_notifier_rule
,
6068 const uint64_t app_token
= event_notifier_rule
->token
;
6072 * Check if the app event trigger still exists on the
6073 * notification side.
6075 for (i
= 0; i
< count
; i
++) {
6076 uint64_t notification_thread_token
;
6077 const struct lttng_trigger
*trigger
=
6078 lttng_triggers_get_at_index(triggers
, i
);
6080 LTTNG_ASSERT(trigger
);
6082 notification_thread_token
= lttng_trigger_get_tracer_token(trigger
);
6084 if (notification_thread_token
== app_token
) {
6096 * This trigger was unregistered, disable it on the tracer's
6099 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &app_trigger_iter
);
6100 LTTNG_ASSERT(ret
== 0);
6102 /* Callee logs errors. */
6103 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
6105 delete_ust_app_event_notifier_rule(app
->sock
, event_notifier_rule
, app
);
6110 lttng_triggers_destroy(triggers
);
6115 * RCU read lock must be held by the caller.
6117 static void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
6118 struct ust_app_session
*ua_sess
,
6119 struct ust_app
*app
)
6122 struct cds_lfht_iter uchan_iter
;
6123 struct ltt_ust_channel
*uchan
;
6125 LTTNG_ASSERT(usess
);
6126 LTTNG_ASSERT(ua_sess
);
6128 ASSERT_RCU_READ_LOCKED();
6130 cds_lfht_for_each_entry (usess
->domain_global
.channels
->ht
, &uchan_iter
, uchan
, node
.node
) {
6131 struct ust_app_channel
*ua_chan
;
6132 struct cds_lfht_iter uevent_iter
;
6133 struct ltt_ust_event
*uevent
;
6136 * Search for a matching ust_app_channel. If none is found,
6137 * create it. Creating the channel will cause the ua_chan
6138 * structure to be allocated, the channel buffers to be
6139 * allocated (if necessary) and sent to the application, and
6140 * all enabled contexts will be added to the channel.
6142 ret
= find_or_create_ust_app_channel(usess
, ua_sess
, app
, uchan
, &ua_chan
);
6144 /* Tracer is probably gone or ENOMEM. */
6149 /* ua_chan will be NULL for the metadata channel */
6153 cds_lfht_for_each_entry (uchan
->events
->ht
, &uevent_iter
, uevent
, node
.node
) {
6154 ret
= ust_app_channel_synchronize_event(ua_chan
, uevent
, app
);
6160 if (ua_chan
->enabled
!= uchan
->enabled
) {
6161 ret
= uchan
->enabled
? enable_ust_app_channel(ua_sess
, uchan
, app
) :
6162 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6173 * The caller must ensure that the application is compatible and is tracked
6174 * by the process attribute trackers.
6176 static void ust_app_synchronize(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6179 struct ust_app_session
*ua_sess
= nullptr;
6182 * The application's configuration should only be synchronized for
6185 LTTNG_ASSERT(usess
->active
);
6187 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, nullptr);
6189 /* Tracer is probably gone or ENOMEM. */
6193 LTTNG_ASSERT(ua_sess
);
6195 pthread_mutex_lock(&ua_sess
->lock
);
6196 if (ua_sess
->deleted
) {
6197 goto deleted_session
;
6201 lttng::urcu::read_lock_guard read_lock
;
6203 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6206 * Create the metadata for the application. This returns gracefully if a
6207 * metadata was already set for the session.
6209 * The metadata channel must be created after the data channels as the
6210 * consumer daemon assumes this ordering. When interacting with a relay
6211 * daemon, the consumer will use this assumption to send the
6212 * "STREAMS_SENT" message to the relay daemon.
6214 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6216 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6223 pthread_mutex_unlock(&ua_sess
->lock
);
6228 static void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6230 struct ust_app_session
*ua_sess
;
6232 ua_sess
= lookup_session_by_app(usess
, app
);
6233 if (ua_sess
== nullptr) {
6236 destroy_app_session(app
, ua_sess
);
6240 * Add channels/events from UST global domain to registered apps at sock.
6242 * Called with session lock held.
6243 * Called with RCU read-side lock held.
6245 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6247 LTTNG_ASSERT(usess
);
6248 LTTNG_ASSERT(usess
->active
);
6249 ASSERT_RCU_READ_LOCKED();
6251 DBG2("UST app global update for app sock %d for session id %" PRIu64
, app
->sock
, usess
->id
);
6253 if (!app
->compatible
) {
6256 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, usess
, app
->pid
) &&
6257 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
, usess
, app
->uid
) &&
6258 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
, usess
, app
->gid
)) {
6260 * Synchronize the application's internal tracing configuration
6261 * and start tracing.
6263 ust_app_synchronize(usess
, app
);
6264 ust_app_start_trace(usess
, app
);
6266 ust_app_global_destroy(usess
, app
);
6271 * Add all event notifiers to an application.
6273 * Called with session lock held.
6274 * Called with RCU read-side lock held.
6276 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6278 ASSERT_RCU_READ_LOCKED();
6280 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6284 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6288 if (app
->event_notifier_group
.object
== nullptr) {
6289 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6295 ust_app_synchronize_event_notifier_rules(app
);
6299 * Called with session lock held.
6301 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6303 struct lttng_ht_iter iter
;
6304 struct ust_app
*app
;
6307 lttng::urcu::read_lock_guard read_lock
;
6309 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6310 ust_app_global_update(usess
, app
);
6315 void ust_app_global_update_all_event_notifier_rules()
6317 struct lttng_ht_iter iter
;
6318 struct ust_app
*app
;
6320 lttng::urcu::read_lock_guard read_lock
;
6321 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6322 ust_app_global_update_event_notifier_rules(app
);
6327 * Add context to a specific channel for global UST domain.
6329 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6330 struct ltt_ust_channel
*uchan
,
6331 struct ltt_ust_context
*uctx
)
6334 struct lttng_ht_node_str
*ua_chan_node
;
6335 struct lttng_ht_iter iter
, uiter
;
6336 struct ust_app_channel
*ua_chan
= nullptr;
6337 struct ust_app_session
*ua_sess
;
6338 struct ust_app
*app
;
6340 LTTNG_ASSERT(usess
->active
);
6343 lttng::urcu::read_lock_guard read_lock
;
6344 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6345 if (!app
->compatible
) {
6347 * TODO: In time, we should notice the caller of this error by
6348 * telling him that this is a version error.
6352 ua_sess
= lookup_session_by_app(usess
, app
);
6353 if (ua_sess
== nullptr) {
6357 pthread_mutex_lock(&ua_sess
->lock
);
6359 if (ua_sess
->deleted
) {
6360 pthread_mutex_unlock(&ua_sess
->lock
);
6364 /* Lookup channel in the ust app session */
6365 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6366 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6367 if (ua_chan_node
== nullptr) {
6370 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6371 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6376 pthread_mutex_unlock(&ua_sess
->lock
);
6384 * Receive registration and populate the given msg structure.
6386 * On success return 0 else a negative value returned by the ustctl call.
6388 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6391 uint32_t pid
, ppid
, uid
, gid
;
6395 ret
= lttng_ust_ctl_recv_reg_msg(sock
,
6403 &msg
->bits_per_long
,
6404 &msg
->uint8_t_alignment
,
6405 &msg
->uint16_t_alignment
,
6406 &msg
->uint32_t_alignment
,
6407 &msg
->uint64_t_alignment
,
6408 &msg
->long_alignment
,
6415 case LTTNG_UST_ERR_EXITING
:
6416 DBG3("UST app recv reg message failed. Application died");
6418 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6419 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6422 LTTNG_UST_ABI_MAJOR_VERSION
,
6423 LTTNG_UST_ABI_MINOR_VERSION
);
6426 ERR("UST app recv reg message failed with ret %d", ret
);
6431 msg
->pid
= (pid_t
) pid
;
6432 msg
->ppid
= (pid_t
) ppid
;
6433 msg
->uid
= (uid_t
) uid
;
6434 msg
->gid
= (gid_t
) gid
;
6441 * Return a ust app session object using the application object and the
6442 * session object descriptor has a key. If not found, NULL is returned.
6443 * A RCU read side lock MUST be acquired when calling this function.
6445 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
, int objd
)
6447 struct lttng_ht_node_ulong
*node
;
6448 struct lttng_ht_iter iter
;
6449 struct ust_app_session
*ua_sess
= nullptr;
6452 ASSERT_RCU_READ_LOCKED();
6454 lttng_ht_lookup(app
->ust_sessions_objd
, (void *) ((unsigned long) objd
), &iter
);
6455 node
= lttng_ht_iter_get_node_ulong(&iter
);
6456 if (node
== nullptr) {
6457 DBG2("UST app session find by objd %d not found", objd
);
6461 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6468 * Return a ust app channel object using the application object and the channel
6469 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6470 * lock MUST be acquired before calling this function.
6472 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
, int objd
)
6474 struct lttng_ht_node_ulong
*node
;
6475 struct lttng_ht_iter iter
;
6476 struct ust_app_channel
*ua_chan
= nullptr;
6479 ASSERT_RCU_READ_LOCKED();
6481 lttng_ht_lookup(app
->ust_objd
, (void *) ((unsigned long) objd
), &iter
);
6482 node
= lttng_ht_iter_get_node_ulong(&iter
);
6483 if (node
== nullptr) {
6484 DBG2("UST app channel find by objd %d not found", objd
);
6488 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6495 * Reply to a register channel notification from an application on the notify
6496 * socket. The channel metadata is also created.
6498 * The session UST registry lock is acquired in this function.
6500 * On success 0 is returned else a negative value.
6502 static int handle_app_register_channel_notification(int sock
,
6504 struct lttng_ust_ctl_field
*raw_context_fields
,
6505 size_t context_field_count
)
6507 int ret
, ret_code
= 0;
6509 uint64_t chan_reg_key
;
6510 struct ust_app
*app
;
6511 struct ust_app_channel
*ua_chan
;
6512 struct ust_app_session
*ua_sess
;
6513 auto ust_ctl_context_fields
=
6514 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::memory::free
>(
6515 raw_context_fields
);
6517 lttng::urcu::read_lock_guard read_lock_guard
;
6519 /* Lookup application. If not found, there is a code flow error. */
6520 app
= find_app_by_notify_sock(sock
);
6522 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6526 /* Lookup channel by UST object descriptor. */
6527 ua_chan
= find_channel_by_objd(app
, cobjd
);
6529 DBG("Application channel is being torn down. Abort event notify");
6533 LTTNG_ASSERT(ua_chan
->session
);
6534 ua_sess
= ua_chan
->session
;
6536 /* Get right session registry depending on the session buffer type. */
6537 auto locked_registry_session
= get_locked_session_registry(ua_sess
);
6538 if (!locked_registry_session
) {
6539 DBG("Application session is being torn down. Abort event notify");
6543 /* Depending on the buffer type, a different channel key is used. */
6544 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6545 chan_reg_key
= ua_chan
->tracing_channel_id
;
6547 chan_reg_key
= ua_chan
->key
;
6550 auto& ust_reg_chan
= locked_registry_session
->channel(chan_reg_key
);
6552 /* Channel id is set during the object creation. */
6553 chan_id
= ust_reg_chan
.id
;
6556 * The application returns the typing information of the channel's
6557 * context fields. In per-PID buffering mode, this is the first and only
6558 * time we get this information. It is our chance to finalize the
6559 * initialiation of the channel and serialize it's layout's description
6560 * to the trace's metadata.
6562 * However, in per-UID buffering mode, every application will provide
6563 * this information (redundantly). The first time will allow us to
6564 * complete the initialization. The following times, we simply validate
6565 * that all apps provide the same typing for the context fields as a
6569 auto app_context_fields
= lsu::create_trace_fields_from_ust_ctl_fields(
6570 *locked_registry_session
,
6571 ust_ctl_context_fields
.get(),
6572 context_field_count
,
6573 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT
,
6574 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
);
6576 if (!ust_reg_chan
.is_registered()) {
6577 lst::type::cuptr event_context
= app_context_fields
.size() ?
6578 lttng::make_unique
<lst::structure_type
>(
6579 0, std::move(app_context_fields
)) :
6582 ust_reg_chan
.event_context(std::move(event_context
));
6585 * Validate that the context fields match between
6586 * registry and newcoming application.
6588 bool context_fields_match
;
6589 const auto *previous_event_context
= ust_reg_chan
.event_context();
6591 if (!previous_event_context
) {
6592 context_fields_match
= app_context_fields
.size() == 0;
6594 const lst::structure_type
app_event_context_struct(
6595 0, std::move(app_context_fields
));
6597 context_fields_match
= *previous_event_context
==
6598 app_event_context_struct
;
6601 if (!context_fields_match
) {
6602 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6609 } catch (const std::exception
& ex
) {
6610 ERR("Failed to handle application context: %s", ex
.what());
6616 DBG3("UST app replying to register channel key %" PRIu64
" with id %u, ret = %d",
6621 ret
= lttng_ust_ctl_reply_register_channel(
6624 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6625 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6626 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6629 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6630 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6633 } else if (ret
== -EAGAIN
) {
6634 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6638 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6647 /* This channel registry's registration is completed. */
6648 ust_reg_chan
.set_as_registered();
6654 * Add event to the UST channel registry. When the event is added to the
6655 * registry, the metadata is also created. Once done, this replies to the
6656 * application with the appropriate error code.
6658 * The session UST registry lock is acquired in the function.
6660 * On success 0 is returned else a negative value.
6662 static int add_event_ust_registry(int sock
,
6666 char *raw_signature
,
6668 struct lttng_ust_ctl_field
*raw_fields
,
6670 char *raw_model_emf_uri
)
6673 uint32_t event_id
= 0;
6674 uint64_t chan_reg_key
;
6675 struct ust_app
*app
;
6676 struct ust_app_channel
*ua_chan
;
6677 struct ust_app_session
*ua_sess
;
6678 lttng::urcu::read_lock_guard rcu_lock
;
6679 auto signature
= lttng::make_unique_wrapper
<char, lttng::memory::free
>(raw_signature
);
6681 lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::memory::free
>(raw_fields
);
6682 auto model_emf_uri
=
6683 lttng::make_unique_wrapper
<char, lttng::memory::free
>(raw_model_emf_uri
);
6685 /* Lookup application. If not found, there is a code flow error. */
6686 app
= find_app_by_notify_sock(sock
);
6688 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6692 /* Lookup channel by UST object descriptor. */
6693 ua_chan
= find_channel_by_objd(app
, cobjd
);
6695 DBG("Application channel is being torn down. Abort event notify");
6699 LTTNG_ASSERT(ua_chan
->session
);
6700 ua_sess
= ua_chan
->session
;
6702 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6703 chan_reg_key
= ua_chan
->tracing_channel_id
;
6705 chan_reg_key
= ua_chan
->key
;
6709 auto locked_registry
= get_locked_session_registry(ua_sess
);
6710 if (locked_registry
) {
6712 * From this point on, this call acquires the ownership of the signature,
6713 * fields and model_emf_uri meaning any free are done inside it if needed.
6714 * These three variables MUST NOT be read/write after this.
6717 auto& channel
= locked_registry
->channel(chan_reg_key
);
6719 /* event_id is set on success. */
6725 lsu::create_trace_fields_from_ust_ctl_fields(
6729 lst::field_location::root::EVENT_RECORD_PAYLOAD
,
6730 lsu::ctl_field_quirks::
6731 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
),
6733 model_emf_uri
.get() ?
6734 nonstd::optional
<std::string
>(model_emf_uri
.get()) :
6736 ua_sess
->buffer_type
,
6740 } catch (const std::exception
& ex
) {
6741 ERR("Failed to add event `%s` to registry session: %s",
6744 /* Inform the application of the error; don't return directly. */
6748 DBG("Application session is being torn down. Abort event notify");
6754 * The return value is returned to ustctl so in case of an error, the
6755 * application can be notified. In case of an error, it's important not to
6756 * return a negative error or else the application will get closed.
6758 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6760 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6761 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6764 } else if (ret
== -EAGAIN
) {
6765 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6769 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6775 * No need to wipe the create event since the application socket will
6776 * get close on error hence cleaning up everything by itself.
6781 DBG3("UST registry event %s with id %" PRId32
" added successfully", name
, event_id
);
6786 * Add enum to the UST session registry. Once done, this replies to the
6787 * application with the appropriate error code.
6789 * The session UST registry lock is acquired within this function.
6791 * On success 0 is returned else a negative value.
6793 static int add_enum_ust_registry(int sock
,
6796 struct lttng_ust_ctl_enum_entry
*raw_entries
,
6800 struct ust_app
*app
;
6801 struct ust_app_session
*ua_sess
;
6802 uint64_t enum_id
= -1ULL;
6803 lttng::urcu::read_lock_guard read_lock_guard
;
6805 lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::memory::free
>(
6808 /* Lookup application. If not found, there is a code flow error. */
6809 app
= find_app_by_notify_sock(sock
);
6811 /* Return an error since this is not an error */
6812 DBG("Application socket %d is being torn down. Aborting enum registration", sock
);
6816 /* Lookup session by UST object descriptor. */
6817 ua_sess
= find_session_by_objd(app
, sobjd
);
6819 /* Return an error since this is not an error */
6820 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6824 auto locked_registry
= get_locked_session_registry(ua_sess
);
6825 if (!locked_registry
) {
6826 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6831 * From this point on, the callee acquires the ownership of
6832 * entries. The variable entries MUST NOT be read/written after
6835 int application_reply_code
;
6837 locked_registry
->create_or_find_enum(
6838 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6839 application_reply_code
= 0;
6840 } catch (const std::exception
& ex
) {
6843 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6848 application_reply_code
= -1;
6852 * The return value is returned to ustctl so in case of an error, the
6853 * application can be notified. In case of an error, it's important not to
6854 * return a negative error or else the application will get closed.
6856 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6858 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6859 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6862 } else if (ret
== -EAGAIN
) {
6863 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6867 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6873 * No need to wipe the create enum since the application socket will
6874 * get close on error hence cleaning up everything by itself.
6879 DBG3("UST registry enum %s added successfully or already found", name
);
6884 * Handle application notification through the given notify socket.
6886 * Return 0 on success or else a negative value.
6888 int ust_app_recv_notify(int sock
)
6891 enum lttng_ust_ctl_notify_cmd cmd
;
6893 DBG3("UST app receiving notify from sock %d", sock
);
6895 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6897 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6898 DBG3("UST app recv notify failed. Application died: sock = %d", sock
);
6899 } else if (ret
== -EAGAIN
) {
6900 WARN("UST app recv notify failed. Communication time out: sock = %d", sock
);
6902 ERR("UST app recv notify failed with ret %d: sock = %d", ret
, sock
);
6908 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6910 int sobjd
, cobjd
, loglevel_value
;
6911 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6913 struct lttng_ust_ctl_field
*fields
;
6915 DBG2("UST app ustctl register event received");
6917 ret
= lttng_ust_ctl_recv_register_event(sock
,
6927 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6928 DBG3("UST app recv event failed. Application died: sock = %d",
6930 } else if (ret
== -EAGAIN
) {
6931 WARN("UST app recv event failed. Communication time out: sock = %d",
6934 ERR("UST app recv event failed with ret %d: sock = %d", ret
, sock
);
6940 lttng::urcu::read_lock_guard rcu_lock
;
6941 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6943 DBG("Application socket %d is being torn down. Abort event notify",
6950 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6951 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6960 * Add event to the UST registry coming from the notify socket. This
6961 * call will free if needed the sig, fields and model_emf_uri. This
6962 * code path loses the ownsership of these variables and transfer them
6963 * to the this function.
6965 ret
= add_event_ust_registry(sock
,
6980 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6984 struct lttng_ust_ctl_field
*context_fields
;
6986 DBG2("UST app ustctl register channel received");
6988 ret
= lttng_ust_ctl_recv_register_channel(
6989 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6991 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6992 DBG3("UST app recv channel failed. Application died: sock = %d",
6994 } else if (ret
== -EAGAIN
) {
6995 WARN("UST app recv channel failed. Communication time out: sock = %d",
6998 ERR("UST app recv channel failed with ret %d: sock = %d",
7006 * The fields ownership are transfered to this function call meaning
7007 * that if needed it will be freed. After this, it's invalid to access
7008 * fields or clean them up.
7010 ret
= handle_app_register_channel_notification(
7011 sock
, cobjd
, context_fields
, field_count
);
7018 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
7021 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
7023 struct lttng_ust_ctl_enum_entry
*entries
;
7025 DBG2("UST app ustctl register enum received");
7027 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
, &entries
, &nr_entries
);
7029 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
7030 DBG3("UST app recv enum failed. Application died: sock = %d", sock
);
7031 } else if (ret
== -EAGAIN
) {
7032 WARN("UST app recv enum failed. Communication time out: sock = %d",
7035 ERR("UST app recv enum failed with ret %d: sock = %d", ret
, sock
);
7040 /* Callee assumes ownership of entries. */
7041 ret
= add_enum_ust_registry(sock
, sobjd
, name
, entries
, nr_entries
);
7049 /* Should NEVER happen. */
7058 * Once the notify socket hangs up, this is called. First, it tries to find the
7059 * corresponding application. On failure, the call_rcu to close the socket is
7060 * executed. If an application is found, it tries to delete it from the notify
7061 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7063 * Note that an object needs to be allocated here so on ENOMEM failure, the
7064 * call RCU is not done but the rest of the cleanup is.
7066 void ust_app_notify_sock_unregister(int sock
)
7069 struct lttng_ht_iter iter
;
7070 struct ust_app
*app
;
7071 struct ust_app_notify_sock_obj
*obj
;
7073 LTTNG_ASSERT(sock
>= 0);
7075 lttng::urcu::read_lock_guard read_lock
;
7077 obj
= zmalloc
<ust_app_notify_sock_obj
>();
7080 * An ENOMEM is kind of uncool. If this strikes we continue the
7081 * procedure but the call_rcu will not be called. In this case, we
7082 * accept the fd leak rather than possibly creating an unsynchronized
7083 * state between threads.
7085 * TODO: The notify object should be created once the notify socket is
7086 * registered and stored independantely from the ust app object. The
7087 * tricky part is to synchronize the teardown of the application and
7088 * this notify object. Let's keep that in mind so we can avoid this
7089 * kind of shenanigans with ENOMEM in the teardown path.
7096 DBG("UST app notify socket unregister %d", sock
);
7099 * Lookup application by notify socket. If this fails, this means that the
7100 * hash table delete has already been done by the application
7101 * unregistration process so we can safely close the notify socket in a
7104 app
= find_app_by_notify_sock(sock
);
7109 iter
.iter
.node
= &app
->notify_sock_n
.node
;
7112 * Whatever happens here either we fail or succeed, in both cases we have
7113 * to close the socket after a grace period to continue to the call RCU
7114 * here. If the deletion is successful, the application is not visible
7115 * anymore by other threads and is it fails it means that it was already
7116 * deleted from the hash table so either way we just have to close the
7119 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
7124 * Close socket after a grace period to avoid for the socket to be reused
7125 * before the application object is freed creating potential race between
7126 * threads trying to add unique in the global hash table.
7129 call_rcu(&obj
->head
, close_notify_sock_rcu
);
7134 * Destroy a ust app data structure and free its memory.
7136 static void ust_app_destroy(ust_app
& app
)
7138 call_rcu(&app
.pid_n
.head
, delete_ust_app_rcu
);
7142 * Take a snapshot for a given UST session. The snapshot is sent to the given
7145 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7147 enum lttng_error_code
ust_app_snapshot_record(const struct ltt_ust_session
*usess
,
7148 const struct consumer_output
*output
,
7149 uint64_t nb_packets_per_stream
)
7152 enum lttng_error_code status
= LTTNG_OK
;
7153 struct lttng_ht_iter iter
;
7154 struct ust_app
*app
;
7155 char *trace_path
= nullptr;
7157 LTTNG_ASSERT(usess
);
7158 LTTNG_ASSERT(output
);
7160 switch (usess
->buffer_type
) {
7161 case LTTNG_BUFFER_PER_UID
:
7163 struct buffer_reg_uid
*reg
;
7165 lttng::urcu::read_lock_guard read_lock
;
7167 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7168 struct buffer_reg_channel
*buf_reg_chan
;
7169 struct consumer_socket
*socket
;
7170 char pathname
[PATH_MAX
];
7171 size_t consumer_path_offset
= 0;
7173 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7174 /* Skip since no metadata is present */
7178 /* Get consumer socket to use to push the metadata.*/
7179 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7182 status
= LTTNG_ERR_INVALID
;
7186 memset(pathname
, 0, sizeof(pathname
));
7187 ret
= snprintf(pathname
,
7189 DEFAULT_UST_TRACE_UID_PATH
,
7191 reg
->bits_per_long
);
7193 PERROR("snprintf snapshot path");
7194 status
= LTTNG_ERR_INVALID
;
7197 /* Free path allowed on previous iteration. */
7199 trace_path
= setup_channel_trace_path(
7200 usess
->consumer
, pathname
, &consumer_path_offset
);
7202 status
= LTTNG_ERR_INVALID
;
7205 /* Add the UST default trace dir to path. */
7206 cds_lfht_for_each_entry (
7207 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7209 consumer_snapshot_channel(socket
,
7210 buf_reg_chan
->consumer_key
,
7213 &trace_path
[consumer_path_offset
],
7214 nb_packets_per_stream
);
7215 if (status
!= LTTNG_OK
) {
7219 status
= consumer_snapshot_channel(socket
,
7220 reg
->registry
->reg
.ust
->_metadata_key
,
7223 &trace_path
[consumer_path_offset
],
7225 if (status
!= LTTNG_OK
) {
7232 case LTTNG_BUFFER_PER_PID
:
7234 lttng::urcu::read_lock_guard read_lock
;
7236 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7237 struct consumer_socket
*socket
;
7238 struct lttng_ht_iter chan_iter
;
7239 struct ust_app_channel
*ua_chan
;
7240 struct ust_app_session
*ua_sess
;
7241 lsu::registry_session
*registry
;
7242 char pathname
[PATH_MAX
];
7243 size_t consumer_path_offset
= 0;
7245 ua_sess
= lookup_session_by_app(usess
, app
);
7247 /* Session not associated with this app. */
7251 /* Get the right consumer socket for the application. */
7252 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, output
);
7254 status
= LTTNG_ERR_INVALID
;
7258 /* Add the UST default trace dir to path. */
7259 memset(pathname
, 0, sizeof(pathname
));
7260 ret
= snprintf(pathname
, sizeof(pathname
), "%s", ua_sess
->path
);
7262 status
= LTTNG_ERR_INVALID
;
7263 PERROR("snprintf snapshot path");
7266 /* Free path allowed on previous iteration. */
7268 trace_path
= setup_channel_trace_path(
7269 usess
->consumer
, pathname
, &consumer_path_offset
);
7271 status
= LTTNG_ERR_INVALID
;
7274 cds_lfht_for_each_entry (
7275 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7277 consumer_snapshot_channel(socket
,
7281 &trace_path
[consumer_path_offset
],
7282 nb_packets_per_stream
);
7286 case LTTNG_ERR_CHAN_NOT_FOUND
:
7293 registry
= get_session_registry(ua_sess
);
7295 DBG("Application session is being torn down. Skip application.");
7298 status
= consumer_snapshot_channel(socket
,
7299 registry
->_metadata_key
,
7302 &trace_path
[consumer_path_offset
],
7307 case LTTNG_ERR_CHAN_NOT_FOUND
:
7326 * Return the size taken by one more packet per stream.
7328 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session
*usess
,
7329 uint64_t cur_nr_packets
)
7331 uint64_t tot_size
= 0;
7332 struct ust_app
*app
;
7333 struct lttng_ht_iter iter
;
7335 LTTNG_ASSERT(usess
);
7337 switch (usess
->buffer_type
) {
7338 case LTTNG_BUFFER_PER_UID
:
7340 struct buffer_reg_uid
*reg
;
7342 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7343 struct buffer_reg_channel
*buf_reg_chan
;
7345 lttng::urcu::read_lock_guard read_lock
;
7347 cds_lfht_for_each_entry (
7348 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7349 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7351 * Don't take channel into account if we
7352 * already grab all its packets.
7356 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7361 case LTTNG_BUFFER_PER_PID
:
7363 lttng::urcu::read_lock_guard read_lock
;
7365 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7366 struct ust_app_channel
*ua_chan
;
7367 struct ust_app_session
*ua_sess
;
7368 struct lttng_ht_iter chan_iter
;
7370 ua_sess
= lookup_session_by_app(usess
, app
);
7372 /* Session not associated with this app. */
7376 cds_lfht_for_each_entry (
7377 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7378 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7380 * Don't take channel into account if we
7381 * already grab all its packets.
7385 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7398 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7399 struct cds_list_head
*buffer_reg_uid_list
,
7400 struct consumer_output
*consumer
,
7403 uint64_t *discarded
,
7407 uint64_t consumer_chan_key
;
7412 ret
= buffer_reg_uid_consumer_channel_key(
7413 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7421 ret
= consumer_get_lost_packets(ust_session_id
, consumer_chan_key
, consumer
, lost
);
7423 ret
= consumer_get_discarded_events(
7424 ust_session_id
, consumer_chan_key
, consumer
, discarded
);
7431 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7432 struct ltt_ust_channel
*uchan
,
7433 struct consumer_output
*consumer
,
7435 uint64_t *discarded
,
7439 struct lttng_ht_iter iter
;
7440 struct lttng_ht_node_str
*ua_chan_node
;
7441 struct ust_app
*app
;
7442 struct ust_app_session
*ua_sess
;
7443 struct ust_app_channel
*ua_chan
;
7449 * Iterate over every registered applications. Sum counters for
7450 * all applications containing requested session and channel.
7452 lttng::urcu::read_lock_guard read_lock
;
7454 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7455 struct lttng_ht_iter uiter
;
7457 ua_sess
= lookup_session_by_app(usess
, app
);
7458 if (ua_sess
== nullptr) {
7463 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7464 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7465 /* If the session is found for the app, the channel must be there */
7466 LTTNG_ASSERT(ua_chan_node
);
7468 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7473 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
, consumer
, &_lost
);
7479 uint64_t _discarded
;
7481 ret
= consumer_get_discarded_events(
7482 usess
->id
, ua_chan
->key
, consumer
, &_discarded
);
7486 (*discarded
) += _discarded
;
7493 static int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7496 struct ust_app_session
*ua_sess
;
7498 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7500 lttng::urcu::read_lock_guard read_lock
;
7502 ua_sess
= lookup_session_by_app(usess
, app
);
7503 if (ua_sess
== nullptr) {
7504 /* The session is in teardown process. Ignore and continue. */
7508 pthread_mutex_lock(&ua_sess
->lock
);
7510 if (ua_sess
->deleted
) {
7514 pthread_mutex_lock(&app
->sock_lock
);
7515 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7516 pthread_mutex_unlock(&app
->sock_lock
);
7519 pthread_mutex_unlock(&ua_sess
->lock
);
7522 health_code_update();
7527 * Regenerate the statedump for each app in the session.
7529 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7532 struct lttng_ht_iter iter
;
7533 struct ust_app
*app
;
7535 DBG("Regenerating the metadata for all UST apps");
7537 lttng::urcu::read_lock_guard read_lock
;
7539 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7540 if (!app
->compatible
) {
7544 ret
= ust_app_regenerate_statedump(usess
, app
);
7546 /* Continue to the next app even on error */
7555 * Rotate all the channels of a session.
7557 * Return LTTNG_OK on success or else an LTTng error code.
7559 enum lttng_error_code
ust_app_rotate_session(const ltt_session::locked_ref
& session
)
7562 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7563 struct lttng_ht_iter iter
;
7564 struct ltt_ust_session
*usess
= session
->ust_session
;
7566 LTTNG_ASSERT(usess
);
7568 switch (usess
->buffer_type
) {
7569 case LTTNG_BUFFER_PER_UID
:
7571 struct buffer_reg_uid
*reg
;
7573 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7574 struct buffer_reg_channel
*buf_reg_chan
;
7575 struct consumer_socket
*socket
;
7576 lttng::urcu::read_lock_guard read_lock
;
7578 /* Get consumer socket to use to push the metadata.*/
7579 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7582 cmd_ret
= LTTNG_ERR_INVALID
;
7586 /* Rotate the data channels. */
7587 cds_lfht_for_each_entry (
7588 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7589 ret
= consumer_rotate_channel(socket
,
7590 buf_reg_chan
->consumer_key
,
7592 /* is_metadata_channel */ false);
7594 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7600 * The metadata channel might not be present.
7602 * Consumer stream allocation can be done
7603 * asynchronously and can fail on intermediary
7604 * operations (i.e add context) and lead to data
7605 * channels created with no metadata channel.
7607 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7608 /* Skip since no metadata is present. */
7613 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7614 (void) push_metadata(locked_registry
, usess
->consumer
);
7617 ret
= consumer_rotate_channel(socket
,
7618 reg
->registry
->reg
.ust
->_metadata_key
,
7620 /* is_metadata_channel */ true);
7622 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7628 case LTTNG_BUFFER_PER_PID
:
7630 lttng::urcu::read_lock_guard read_lock
;
7633 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, raw_app
, pid_n
.node
) {
7634 struct consumer_socket
*socket
;
7635 struct lttng_ht_iter chan_iter
;
7636 struct ust_app_channel
*ua_chan
;
7637 struct ust_app_session
*ua_sess
;
7638 lsu::registry_session
*registry
;
7639 bool app_reference_taken
;
7641 app_reference_taken
= ust_app_get(*raw_app
);
7642 if (!app_reference_taken
) {
7643 /* Application unregistered concurrently, skip it. */
7644 DBG("Could not get application reference as it is being torn down; skipping application");
7648 ust_app_reference
app(raw_app
);
7651 ua_sess
= lookup_session_by_app(usess
, app
.get());
7653 /* Session not associated with this app. */
7657 /* Get the right consumer socket for the application. */
7658 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7661 cmd_ret
= LTTNG_ERR_INVALID
;
7665 registry
= get_session_registry(ua_sess
);
7666 LTTNG_ASSERT(registry
);
7668 /* Rotate the data channels. */
7669 cds_lfht_for_each_entry (
7670 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7671 ret
= consumer_rotate_channel(socket
,
7674 /* is_metadata_channel */ false);
7676 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7681 /* Rotate the metadata channel. */
7683 auto locked_registry
= registry
->lock();
7685 (void) push_metadata(locked_registry
, usess
->consumer
);
7688 ret
= consumer_rotate_channel(socket
,
7689 registry
->_metadata_key
,
7691 /* is_metadata_channel */ true);
7693 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7711 enum lttng_error_code
ust_app_create_channel_subdirectories(const struct ltt_ust_session
*usess
)
7713 enum lttng_error_code ret
= LTTNG_OK
;
7714 struct lttng_ht_iter iter
;
7715 enum lttng_trace_chunk_status chunk_status
;
7716 char *pathname_index
;
7719 LTTNG_ASSERT(usess
->current_trace_chunk
);
7721 switch (usess
->buffer_type
) {
7722 case LTTNG_BUFFER_PER_UID
:
7724 struct buffer_reg_uid
*reg
;
7725 lttng::urcu::read_lock_guard read_lock
;
7727 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7728 fmt_ret
= asprintf(&pathname_index
,
7729 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
7730 "/" DEFAULT_INDEX_DIR
,
7732 reg
->bits_per_long
);
7734 ERR("Failed to format channel index directory");
7735 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7740 * Create the index subdirectory which will take care
7741 * of implicitly creating the channel's path.
7743 chunk_status
= lttng_trace_chunk_create_subdirectory(
7744 usess
->current_trace_chunk
, pathname_index
);
7745 free(pathname_index
);
7746 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7747 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7753 case LTTNG_BUFFER_PER_PID
:
7755 struct ust_app
*app
;
7756 lttng::urcu::read_lock_guard read_lock
;
7759 * Create the toplevel ust/ directory in case no apps are running.
7761 chunk_status
= lttng_trace_chunk_create_subdirectory(usess
->current_trace_chunk
,
7762 DEFAULT_UST_TRACE_DIR
);
7763 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7764 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7768 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7769 struct ust_app_session
*ua_sess
;
7770 lsu::registry_session
*registry
;
7772 ua_sess
= lookup_session_by_app(usess
, app
);
7774 /* Session not associated with this app. */
7778 registry
= get_session_registry(ua_sess
);
7780 DBG("Application session is being torn down. Skip application.");
7784 fmt_ret
= asprintf(&pathname_index
,
7785 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7788 ERR("Failed to format channel index directory");
7789 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7793 * Create the index subdirectory which will take care
7794 * of implicitly creating the channel's path.
7796 chunk_status
= lttng_trace_chunk_create_subdirectory(
7797 usess
->current_trace_chunk
, pathname_index
);
7798 free(pathname_index
);
7799 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7800 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7816 * Clear all the channels of a session.
7818 * Return LTTNG_OK on success or else an LTTng error code.
7820 enum lttng_error_code
ust_app_clear_session(const ltt_session::locked_ref
& session
)
7823 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7824 struct lttng_ht_iter iter
;
7825 struct ust_app
*app
;
7826 struct ltt_ust_session
*usess
= session
->ust_session
;
7828 LTTNG_ASSERT(usess
);
7830 if (usess
->active
) {
7831 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7832 cmd_ret
= LTTNG_ERR_FATAL
;
7836 switch (usess
->buffer_type
) {
7837 case LTTNG_BUFFER_PER_UID
:
7839 struct buffer_reg_uid
*reg
;
7840 lttng::urcu::read_lock_guard read_lock
;
7842 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7843 struct buffer_reg_channel
*buf_reg_chan
;
7844 struct consumer_socket
*socket
;
7846 /* Get consumer socket to use to push the metadata.*/
7847 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7850 cmd_ret
= LTTNG_ERR_INVALID
;
7854 /* Clear the data channels. */
7855 cds_lfht_for_each_entry (
7856 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
7857 ret
= consumer_clear_channel(socket
, buf_reg_chan
->consumer_key
);
7864 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7865 (void) push_metadata(locked_registry
, usess
->consumer
);
7869 * Clear the metadata channel.
7870 * Metadata channel is not cleared per se but we still need to
7871 * perform a rotation operation on it behind the scene.
7873 ret
= consumer_clear_channel(socket
, reg
->registry
->reg
.ust
->_metadata_key
);
7880 case LTTNG_BUFFER_PER_PID
:
7882 lttng::urcu::read_lock_guard read_lock
;
7884 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7885 struct consumer_socket
*socket
;
7886 struct lttng_ht_iter chan_iter
;
7887 struct ust_app_channel
*ua_chan
;
7888 struct ust_app_session
*ua_sess
;
7889 lsu::registry_session
*registry
;
7891 ua_sess
= lookup_session_by_app(usess
, app
);
7893 /* Session not associated with this app. */
7897 /* Get the right consumer socket for the application. */
7898 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7901 cmd_ret
= LTTNG_ERR_INVALID
;
7905 registry
= get_session_registry(ua_sess
);
7907 DBG("Application session is being torn down. Skip application.");
7911 /* Clear the data channels. */
7912 cds_lfht_for_each_entry (
7913 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
7914 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7916 /* Per-PID buffer and application going away. */
7917 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7925 auto locked_registry
= registry
->lock();
7926 (void) push_metadata(locked_registry
, usess
->consumer
);
7930 * Clear the metadata channel.
7931 * Metadata channel is not cleared per se but we still need to
7932 * perform rotation operation on it behind the scene.
7934 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7936 /* Per-PID buffer and application going away. */
7937 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7955 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7956 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7959 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7968 * This function skips the metadata channel as the begin/end timestamps of a
7969 * metadata packet are useless.
7971 * Moreover, opening a packet after a "clear" will cause problems for live
7972 * sessions as it will introduce padding that was not part of the first trace
7973 * chunk. The relay daemon expects the content of the metadata stream of
7974 * successive metadata trace chunks to be strict supersets of one another.
7976 * For example, flushing a packet at the beginning of the metadata stream of
7977 * a trace chunk resulting from a "clear" session command will cause the
7978 * size of the metadata stream of the new trace chunk to not match the size of
7979 * the metadata stream of the original chunk. This will confuse the relay
7980 * daemon as the same "offset" in a metadata stream will no longer point
7981 * to the same content.
7983 enum lttng_error_code
ust_app_open_packets(const ltt_session::locked_ref
& session
)
7985 enum lttng_error_code ret
= LTTNG_OK
;
7986 struct lttng_ht_iter iter
;
7987 struct ltt_ust_session
*usess
= session
->ust_session
;
7989 LTTNG_ASSERT(usess
);
7991 switch (usess
->buffer_type
) {
7992 case LTTNG_BUFFER_PER_UID
:
7994 struct buffer_reg_uid
*reg
;
7996 cds_list_for_each_entry (reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7997 struct buffer_reg_channel
*buf_reg_chan
;
7998 struct consumer_socket
*socket
;
7999 lttng::urcu::read_lock_guard read_lock
;
8001 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
8004 ret
= LTTNG_ERR_FATAL
;
8008 cds_lfht_for_each_entry (
8009 reg
->registry
->channels
->ht
, &iter
.iter
, buf_reg_chan
, node
.node
) {
8010 const int open_ret
= consumer_open_channel_packets(
8011 socket
, buf_reg_chan
->consumer_key
);
8014 ret
= LTTNG_ERR_UNK
;
8021 case LTTNG_BUFFER_PER_PID
:
8023 struct ust_app
*app
;
8024 lttng::urcu::read_lock_guard read_lock
;
8026 cds_lfht_for_each_entry (ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8027 struct consumer_socket
*socket
;
8028 struct lttng_ht_iter chan_iter
;
8029 struct ust_app_channel
*ua_chan
;
8030 struct ust_app_session
*ua_sess
;
8031 lsu::registry_session
*registry
;
8033 ua_sess
= lookup_session_by_app(usess
, app
);
8035 /* Session not associated with this app. */
8039 /* Get the right consumer socket for the application. */
8040 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
8043 ret
= LTTNG_ERR_FATAL
;
8047 registry
= get_session_registry(ua_sess
);
8049 DBG("Application session is being torn down. Skip application.");
8053 cds_lfht_for_each_entry (
8054 ua_sess
->channels
->ht
, &chan_iter
.iter
, ua_chan
, node
.node
) {
8055 const int open_ret
=
8056 consumer_open_channel_packets(socket
, ua_chan
->key
);
8060 * Per-PID buffer and application going
8063 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
8067 ret
= LTTNG_ERR_UNK
;
8083 lsu::ctl_field_quirks
ust_app::ctl_field_quirks() const
8086 * Application contexts are expressed as variants. LTTng-UST announces
8087 * those by registering an enumeration named `..._tag`. It then registers a
8088 * variant as part of the event context that contains the various possible
8091 * Unfortunately, the names used in the enumeration and variant don't
8092 * match: the enumeration names are all prefixed with an underscore while
8093 * the variant type tag fields aren't.
8095 * While the CTF 1.8.3 specification mentions that
8096 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8097 * 1.x (and possibly others) expect a perfect match between the names used
8098 * by tags and variants.
8100 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8101 * the variant's fields are modified to match the mappings of its tag.
8103 * From ABI version >= 10.x, the variant fields and tag mapping names
8104 * correctly match, making this quirk unnecessary.
8106 return v_major
<= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
:
8107 lsu::ctl_field_quirks::NONE
;
8110 static void ust_app_release(urcu_ref
*ref
)
8112 auto& app
= *lttng::utils::container_of(ref
, &ust_app::ref
);
8114 ust_app_unregister(app
);
8115 ust_app_destroy(app
);
8118 bool ust_app_get(ust_app
& app
)
8120 return urcu_ref_get_unless_zero(&app
.ref
);
8123 void ust_app_put(struct ust_app
*app
)
8129 urcu_ref_put(&app
->ref
, ust_app_release
);