2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "buffer-registry.hpp"
12 #include "condition-internal.hpp"
13 #include "event-notifier-error-accounting.hpp"
15 #include "fd-limit.hpp"
17 #include "health-sessiond.hpp"
18 #include "lttng-sessiond.hpp"
19 #include "lttng-ust-ctl.hpp"
20 #include "lttng-ust-error.hpp"
21 #include "notification-thread-commands.hpp"
23 #include "session.hpp"
24 #include "ust-app.hpp"
25 #include "ust-consumer.hpp"
26 #include "ust-field-convert.hpp"
29 #include <common/bytecode/bytecode.hpp>
30 #include <common/common.hpp>
31 #include <common/compat/errno.hpp>
32 #include <common/exception.hpp>
33 #include <common/format.hpp>
34 #include <common/hashtable/utils.hpp>
35 #include <common/make-unique.hpp>
36 #include <common/sessiond-comm/sessiond-comm.hpp>
37 #include <common/urcu.hpp>
39 #include <lttng/condition/condition.h>
40 #include <lttng/condition/event-rule-matches-internal.hpp>
41 #include <lttng/condition/event-rule-matches.h>
42 #include <lttng/event-rule/event-rule-internal.hpp>
43 #include <lttng/event-rule/event-rule.h>
44 #include <lttng/event-rule/user-tracepoint.h>
45 #include <lttng/trigger/trigger-internal.hpp>
57 #include <sys/types.h>
59 #include <urcu/compiler.h>
62 namespace lsu
= lttng::sessiond::ust
;
63 namespace lst
= lttng::sessiond::trace
;
65 struct lttng_ht
*ust_app_ht
;
66 struct lttng_ht
*ust_app_ht_by_sock
;
67 struct lttng_ht
*ust_app_ht_by_notify_sock
;
70 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
72 /* Next available channel key. Access under next_channel_key_lock. */
73 static uint64_t _next_channel_key
;
74 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
76 /* Next available session ID. Access under next_session_id_lock. */
77 static uint64_t _next_session_id
;
78 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
83 * Return the session registry according to the buffer type of the given
86 * A registry per UID object MUST exists before calling this function or else
87 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
89 static lsu::registry_session
*get_session_registry(
90 const struct ust_app_session
*ua_sess
)
92 lsu::registry_session
*registry
= NULL
;
94 LTTNG_ASSERT(ua_sess
);
96 switch (ua_sess
->buffer_type
) {
97 case LTTNG_BUFFER_PER_PID
:
99 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
103 registry
= reg_pid
->registry
->reg
.ust
;
106 case LTTNG_BUFFER_PER_UID
:
108 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
109 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
110 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
114 registry
= reg_uid
->registry
->reg
.ust
;
125 lsu::registry_session::locked_ptr
126 get_locked_session_registry(const struct ust_app_session
*ua_sess
)
128 auto session
= get_session_registry(ua_sess
);
130 pthread_mutex_lock(&session
->_lock
);
133 return lsu::registry_session::locked_ptr
{session
};
138 * Return the incremented value of next_channel_key.
140 static uint64_t get_next_channel_key(void)
144 pthread_mutex_lock(&next_channel_key_lock
);
145 ret
= ++_next_channel_key
;
146 pthread_mutex_unlock(&next_channel_key_lock
);
151 * Return the atomically incremented value of next_session_id.
153 static uint64_t get_next_session_id(void)
157 pthread_mutex_lock(&next_session_id_lock
);
158 ret
= ++_next_session_id
;
159 pthread_mutex_unlock(&next_session_id_lock
);
163 static void copy_channel_attr_to_ustctl(
164 struct lttng_ust_ctl_consumer_channel_attr
*attr
,
165 struct lttng_ust_abi_channel_attr
*uattr
)
167 /* Copy event attributes since the layout is different. */
168 attr
->subbuf_size
= uattr
->subbuf_size
;
169 attr
->num_subbuf
= uattr
->num_subbuf
;
170 attr
->overwrite
= uattr
->overwrite
;
171 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
172 attr
->read_timer_interval
= uattr
->read_timer_interval
;
173 attr
->output
= (lttng_ust_abi_output
) uattr
->output
;
174 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
178 * Match function for the hash table lookup.
180 * It matches an ust app event based on three attributes which are the event
181 * name, the filter bytecode and the loglevel.
183 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
185 struct ust_app_event
*event
;
186 const struct ust_app_ht_key
*key
;
187 int ev_loglevel_value
;
192 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
193 key
= (ust_app_ht_key
*) _key
;
194 ev_loglevel_value
= event
->attr
.loglevel
;
196 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
199 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
203 /* Event loglevel. */
204 if (ev_loglevel_value
!= key
->loglevel_type
) {
205 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
206 && key
->loglevel_type
== 0 &&
207 ev_loglevel_value
== -1) {
209 * Match is accepted. This is because on event creation, the
210 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
211 * -1 are accepted for this loglevel type since 0 is the one set by
212 * the API when receiving an enable event.
219 /* One of the filters is NULL, fail. */
220 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
224 if (key
->filter
&& event
->filter
) {
225 /* Both filters exists, check length followed by the bytecode. */
226 if (event
->filter
->len
!= key
->filter
->len
||
227 memcmp(event
->filter
->data
, key
->filter
->data
,
228 event
->filter
->len
) != 0) {
233 /* One of the exclusions is NULL, fail. */
234 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
238 if (key
->exclusion
&& event
->exclusion
) {
239 /* Both exclusions exists, check count followed by the names. */
240 if (event
->exclusion
->count
!= key
->exclusion
->count
||
241 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
242 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
256 * Unique add of an ust app event in the given ht. This uses the custom
257 * ht_match_ust_app_event match function and the event name as hash.
259 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
260 struct ust_app_event
*event
)
262 struct cds_lfht_node
*node_ptr
;
263 struct ust_app_ht_key key
;
266 LTTNG_ASSERT(ua_chan
);
267 LTTNG_ASSERT(ua_chan
->events
);
270 ht
= ua_chan
->events
;
271 key
.name
= event
->attr
.name
;
272 key
.filter
= event
->filter
;
273 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel
;
274 key
.exclusion
= event
->exclusion
;
276 node_ptr
= cds_lfht_add_unique(ht
->ht
,
277 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
278 ht_match_ust_app_event
, &key
, &event
->node
.node
);
279 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
283 * Close the notify socket from the given RCU head object. This MUST be called
284 * through a call_rcu().
286 static void close_notify_sock_rcu(struct rcu_head
*head
)
289 struct ust_app_notify_sock_obj
*obj
=
290 lttng::utils::container_of(head
, &ust_app_notify_sock_obj::head
);
292 /* Must have a valid fd here. */
293 LTTNG_ASSERT(obj
->fd
>= 0);
295 ret
= close(obj
->fd
);
297 ERR("close notify sock %d RCU", obj
->fd
);
299 lttng_fd_put(LTTNG_FD_APPS
, 1);
305 * Delete ust context safely. RCU read lock must be held before calling
309 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
314 LTTNG_ASSERT(ua_ctx
);
315 ASSERT_RCU_READ_LOCKED();
318 pthread_mutex_lock(&app
->sock_lock
);
319 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
320 pthread_mutex_unlock(&app
->sock_lock
);
322 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
323 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
324 app
->pid
, app
->sock
);
325 } else if (ret
== -EAGAIN
) {
326 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
327 app
->pid
, app
->sock
);
329 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
330 ua_ctx
->obj
->handle
, ret
,
331 app
->pid
, app
->sock
);
340 * Delete ust app event safely. RCU read lock must be held before calling
344 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
349 LTTNG_ASSERT(ua_event
);
350 ASSERT_RCU_READ_LOCKED();
352 free(ua_event
->filter
);
353 if (ua_event
->exclusion
!= NULL
)
354 free(ua_event
->exclusion
);
355 if (ua_event
->obj
!= NULL
) {
356 pthread_mutex_lock(&app
->sock_lock
);
357 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
358 pthread_mutex_unlock(&app
->sock_lock
);
360 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
361 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
362 app
->pid
, app
->sock
);
363 } else if (ret
== -EAGAIN
) {
364 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
365 app
->pid
, app
->sock
);
367 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
368 ret
, app
->pid
, app
->sock
);
377 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
378 * through a call_rcu().
381 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
383 struct ust_app_event_notifier_rule
*obj
= lttng::utils::container_of(
384 head
, &ust_app_event_notifier_rule::rcu_head
);
390 * Delete ust app event notifier rule safely.
392 static void delete_ust_app_event_notifier_rule(int sock
,
393 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
398 LTTNG_ASSERT(ua_event_notifier_rule
);
400 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
401 free(ua_event_notifier_rule
->exclusion
);
404 if (ua_event_notifier_rule
->obj
!= NULL
) {
405 pthread_mutex_lock(&app
->sock_lock
);
406 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
407 pthread_mutex_unlock(&app
->sock_lock
);
409 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
410 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
411 app
->pid
, app
->sock
);
412 } else if (ret
== -EAGAIN
) {
413 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
414 app
->pid
, app
->sock
);
416 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
417 ret
, app
->pid
, app
->sock
);
421 free(ua_event_notifier_rule
->obj
);
424 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
425 call_rcu(&ua_event_notifier_rule
->rcu_head
,
426 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
,
439 LTTNG_ASSERT(stream
);
442 pthread_mutex_lock(&app
->sock_lock
);
443 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
444 pthread_mutex_unlock(&app
->sock_lock
);
446 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
447 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
448 app
->pid
, app
->sock
);
449 } else if (ret
== -EAGAIN
) {
450 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
451 app
->pid
, app
->sock
);
453 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
454 ret
, app
->pid
, app
->sock
);
457 lttng_fd_put(LTTNG_FD_APPS
, 2);
465 * Delete ust app stream safely. RCU read lock must be held before calling
469 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
472 LTTNG_ASSERT(stream
);
473 ASSERT_RCU_READ_LOCKED();
475 (void) release_ust_app_stream(sock
, stream
, app
);
480 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.
498 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
500 uint64_t discarded
= 0, lost
= 0;
501 struct ltt_session
*session
;
502 struct ltt_ust_channel
*uchan
;
504 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
509 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
510 if (!session
|| !session
->ust_session
) {
512 * Not finding the session is not an error because there are
513 * multiple ways the channels can be torn down.
515 * 1) The session daemon can initiate the destruction of the
516 * ust app session after receiving a destroy command or
517 * during its shutdown/teardown.
518 * 2) The application, since we are in per-pid tracing, is
519 * unregistering and tearing down its ust app session.
521 * Both paths are protected by the session list lock which
522 * ensures that the accounting of lost packets and discarded
523 * events is done exactly once. The session is then unpublished
524 * from the session list, resulting in this condition.
529 if (ua_chan
->attr
.overwrite
) {
530 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
531 ua_chan
->key
, session
->ust_session
->consumer
,
534 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
535 ua_chan
->key
, session
->ust_session
->consumer
,
538 uchan
= trace_ust_find_channel_by_name(
539 session
->ust_session
->domain_global
.channels
,
542 ERR("Missing UST channel to store discarded counters");
546 uchan
->per_pid_closed_app_discarded
+= discarded
;
547 uchan
->per_pid_closed_app_lost
+= lost
;
552 session_put(session
);
557 * Delete ust app channel safely. RCU read lock must be held before calling
560 * The session list lock must be held by the caller.
562 static void delete_ust_app_channel(int sock
,
563 struct ust_app_channel
*ua_chan
,
565 const lsu::registry_session::locked_ptr
& locked_registry
)
568 struct lttng_ht_iter iter
;
569 struct ust_app_event
*ua_event
;
570 struct ust_app_ctx
*ua_ctx
;
571 struct ust_app_stream
*stream
, *stmp
;
573 LTTNG_ASSERT(ua_chan
);
574 ASSERT_RCU_READ_LOCKED();
576 DBG3("UST app deleting channel %s", ua_chan
->name
);
579 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
580 cds_list_del(&stream
->list
);
581 delete_ust_app_stream(sock
, stream
, app
);
585 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
586 cds_list_del(&ua_ctx
->list
);
587 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
589 delete_ust_app_ctx(sock
, ua_ctx
, app
);
593 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
595 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
597 delete_ust_app_event(sock
, ua_event
, app
);
600 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
601 /* Wipe and free registry from session registry. */
602 if (locked_registry
) {
604 locked_registry
->remove_channel(ua_chan
->key
, sock
>= 0);
605 } catch (const std::exception
&ex
) {
606 DBG("Could not find channel for removal: %s", ex
.what());
611 * A negative socket can be used by the caller when
612 * cleaning-up a ua_chan in an error path. Skip the
613 * accounting in this case.
616 save_per_pid_lost_discarded_counters(ua_chan
);
620 if (ua_chan
->obj
!= NULL
) {
621 /* Remove channel from application UST object descriptor. */
622 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
623 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
625 pthread_mutex_lock(&app
->sock_lock
);
626 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
627 pthread_mutex_unlock(&app
->sock_lock
);
629 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
630 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
631 ua_chan
->name
, app
->pid
,
633 } else if (ret
== -EAGAIN
) {
634 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
635 ua_chan
->name
, app
->pid
,
638 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
639 ua_chan
->name
, ret
, app
->pid
,
643 lttng_fd_put(LTTNG_FD_APPS
, 1);
646 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
649 int ust_app_register_done(struct ust_app
*app
)
653 pthread_mutex_lock(&app
->sock_lock
);
654 ret
= lttng_ust_ctl_register_done(app
->sock
);
655 pthread_mutex_unlock(&app
->sock_lock
);
659 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
664 pthread_mutex_lock(&app
->sock_lock
);
669 ret
= lttng_ust_ctl_release_object(sock
, data
);
671 pthread_mutex_unlock(&app
->sock_lock
);
677 * Push metadata to consumer socket.
679 * RCU read-side lock must be held to guarantee existence of socket.
680 * Must be called with the ust app session lock held.
681 * Must be called with the registry lock held.
683 * On success, return the len of metadata pushed or else a negative value.
684 * Returning a -EPIPE return value means we could not send the metadata,
685 * but it can be caused by recoverable errors (e.g. the application has
686 * terminated concurrently).
688 ssize_t
ust_app_push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
689 struct consumer_socket
*socket
,
693 char *metadata_str
= NULL
;
694 size_t len
, offset
, new_metadata_len_sent
;
696 uint64_t metadata_key
, metadata_version
;
698 LTTNG_ASSERT(locked_registry
);
699 LTTNG_ASSERT(socket
);
700 ASSERT_RCU_READ_LOCKED();
702 metadata_key
= locked_registry
->_metadata_key
;
705 * Means that no metadata was assigned to the session. This can
706 * happens if no start has been done previously.
712 offset
= locked_registry
->_metadata_len_sent
;
713 len
= locked_registry
->_metadata_len
- locked_registry
->_metadata_len_sent
;
714 new_metadata_len_sent
= locked_registry
->_metadata_len
;
715 metadata_version
= locked_registry
->_metadata_version
;
717 DBG3("No metadata to push for metadata key %" PRIu64
,
718 locked_registry
->_metadata_key
);
720 if (send_zero_data
) {
721 DBG("No metadata to push");
727 /* Allocate only what we have to send. */
728 metadata_str
= calloc
<char>(len
);
730 PERROR("zmalloc ust app metadata string");
734 /* Copy what we haven't sent out. */
735 memcpy(metadata_str
, locked_registry
->_metadata
+ offset
, len
);
738 pthread_mutex_unlock(&locked_registry
->_lock
);
740 * We need to unlock the registry while we push metadata to
741 * break a circular dependency between the consumerd metadata
742 * lock and the sessiond registry lock. Indeed, pushing metadata
743 * to the consumerd awaits that it gets pushed all the way to
744 * relayd, but doing so requires grabbing the metadata lock. If
745 * a concurrent metadata request is being performed by
746 * consumerd, this can try to grab the registry lock on the
747 * sessiond while holding the metadata lock on the consumer
748 * daemon. Those push and pull schemes are performed on two
749 * different bidirectionnal communication sockets.
751 ret
= consumer_push_metadata(socket
, metadata_key
,
752 metadata_str
, len
, offset
, metadata_version
);
753 pthread_mutex_lock(&locked_registry
->_lock
);
756 * There is an acceptable race here between the registry
757 * metadata key assignment and the creation on the
758 * consumer. The session daemon can concurrently push
759 * metadata for this registry while being created on the
760 * consumer since the metadata key of the registry is
761 * assigned *before* it is setup to avoid the consumer
762 * to ask for metadata that could possibly be not found
763 * in the session daemon.
765 * The metadata will get pushed either by the session
766 * being stopped or the consumer requesting metadata if
767 * that race is triggered.
769 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
772 ERR("Error pushing metadata to consumer");
778 * Metadata may have been concurrently pushed, since
779 * we're not holding the registry lock while pushing to
780 * consumer. This is handled by the fact that we send
781 * the metadata content, size, and the offset at which
782 * that metadata belongs. This may arrive out of order
783 * on the consumer side, and the consumer is able to
784 * deal with overlapping fragments. The consumer
785 * supports overlapping fragments, which must be
786 * contiguous starting from offset 0. We keep the
787 * largest metadata_len_sent value of the concurrent
790 locked_registry
->_metadata_len_sent
=
791 std::max(locked_registry
->_metadata_len_sent
,
792 new_metadata_len_sent
);
801 * On error, flag the registry that the metadata is
802 * closed. We were unable to push anything and this
803 * means that either the consumer is not responding or
804 * the metadata cache has been destroyed on the
807 locked_registry
->_metadata_closed
= true;
815 * For a given application and session, push metadata to consumer.
816 * Either sock or consumer is required : if sock is NULL, the default
817 * socket to send the metadata is retrieved from consumer, if sock
818 * is not NULL we use it to send the metadata.
819 * RCU read-side lock must be held while calling this function,
820 * therefore ensuring existence of registry. It also ensures existence
821 * of socket throughout this function.
823 * Return 0 on success else a negative error.
824 * Returning a -EPIPE return value means we could not send the metadata,
825 * but it can be caused by recoverable errors (e.g. the application has
826 * terminated concurrently).
828 static int push_metadata(const lsu::registry_session::locked_ptr
& locked_registry
,
829 struct consumer_output
*consumer
)
833 struct consumer_socket
*socket
;
835 LTTNG_ASSERT(locked_registry
);
836 LTTNG_ASSERT(consumer
);
837 ASSERT_RCU_READ_LOCKED();
839 if (locked_registry
->_metadata_closed
) {
844 /* Get consumer socket to use to push the metadata.*/
845 socket
= consumer_find_socket_by_bitness(locked_registry
->abi
.bits_per_long
,
852 ret
= ust_app_push_metadata(locked_registry
, socket
, 0);
864 * Send to the consumer a close metadata command for the given session. Once
865 * done, the metadata channel is deleted and the session metadata pointer is
866 * nullified. The session lock MUST be held unless the application is
867 * in the destroy path.
869 * Do not hold the registry lock while communicating with the consumerd, because
870 * doing so causes inter-process deadlocks between consumerd and sessiond with
871 * the metadata request notification.
873 * Return 0 on success else a negative value.
875 static int close_metadata(uint64_t metadata_key
, unsigned int consumer_bitness
,
876 struct consumer_output
*consumer
)
879 struct consumer_socket
*socket
;
880 lttng::urcu::read_lock_guard read_lock_guard
;
882 LTTNG_ASSERT(consumer
);
884 /* Get consumer socket to use to push the metadata. */
885 socket
= consumer_find_socket_by_bitness(consumer_bitness
,
892 ret
= consumer_close_metadata(socket
, metadata_key
);
902 void delete_ust_app_session_rcu(struct rcu_head
*head
)
904 struct ust_app_session
*ua_sess
=
905 lttng::utils::container_of(head
, &ust_app_session::rcu_head
);
907 lttng_ht_destroy(ua_sess
->channels
);
912 * Delete ust app session safely. RCU read lock must be held before calling
915 * The session list lock must be held by the caller.
918 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
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
,
942 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
944 delete_ust_app_channel(sock
, ua_chan
, app
, locked_registry
);
947 if (locked_registry
) {
949 * Don't ask to close metadata for global per UID buffers. Close
950 * metadata only on destroy trace session in this case. Also, the
951 * previous push metadata could have flag the metadata registry to
952 * close so don't send a close command if closed.
954 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
955 const auto metadata_key
= locked_registry
->_metadata_key
;
956 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
958 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
959 locked_registry
->_metadata_closed
= true;
962 /* Release lock before communication, see comments in close_metadata(). */
963 locked_registry
.reset();
964 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
968 /* In case of per PID, the registry is kept in the session. */
969 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
970 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
973 * Registry can be null on error path during
976 buffer_reg_pid_remove(reg_pid
);
977 buffer_reg_pid_destroy(reg_pid
);
981 if (ua_sess
->handle
!= -1) {
982 pthread_mutex_lock(&app
->sock_lock
);
983 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
984 pthread_mutex_unlock(&app
->sock_lock
);
986 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
987 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
988 app
->pid
, app
->sock
);
989 } else if (ret
== -EAGAIN
) {
990 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
991 app
->pid
, app
->sock
);
993 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
994 ret
, app
->pid
, app
->sock
);
998 /* Remove session from application UST object descriptor. */
999 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
1000 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
1004 pthread_mutex_unlock(&ua_sess
->lock
);
1006 consumer_output_put(ua_sess
->consumer
);
1008 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1012 * Delete a traceable application structure from the global list. Never call
1013 * this function outside of a call_rcu call.
1016 void delete_ust_app(struct ust_app
*app
)
1019 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1020 struct lttng_ht_iter iter
;
1021 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1022 bool event_notifier_write_fd_is_open
;
1025 * The session list lock must be held during this function to guarantee
1026 * the existence of ua_sess.
1028 session_lock_list();
1029 /* Delete ust app sessions info */
1034 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
1036 /* Free every object in the session and the session. */
1038 delete_ust_app_session(sock
, ua_sess
, app
);
1042 /* Remove the event notifier rules associated with this app. */
1044 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1045 &iter
.iter
, event_notifier_rule
, node
.node
) {
1046 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1049 delete_ust_app_event_notifier_rule(
1050 app
->sock
, event_notifier_rule
, app
);
1055 lttng_ht_destroy(app
->sessions
);
1056 lttng_ht_destroy(app
->ust_sessions_objd
);
1057 lttng_ht_destroy(app
->ust_objd
);
1058 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1061 * This could be NULL if the event notifier setup failed (e.g the app
1062 * was killed or the tracer does not support this feature).
1064 if (app
->event_notifier_group
.object
) {
1065 enum lttng_error_code ret_code
;
1066 enum event_notifier_error_accounting_status status
;
1068 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1069 app
->event_notifier_group
.event_pipe
);
1071 ret_code
= notification_thread_command_remove_tracer_event_source(
1072 the_notification_thread_handle
,
1073 event_notifier_read_fd
);
1074 if (ret_code
!= LTTNG_OK
) {
1075 ERR("Failed to remove application tracer event source from notification thread");
1078 status
= event_notifier_error_accounting_unregister_app(app
);
1079 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1080 ERR("Error unregistering app from event notifier error accounting");
1083 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1084 free(app
->event_notifier_group
.object
);
1087 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1088 app
->event_notifier_group
.event_pipe
);
1089 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1091 * Release the file descriptors reserved for the event notifier pipe.
1092 * The app could be destroyed before the write end of the pipe could be
1093 * passed to the application (and closed). In that case, both file
1094 * descriptors must be released.
1096 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1099 * Wait until we have deleted the application from the sock hash table
1100 * before closing this socket, otherwise an application could re-use the
1101 * socket ID and race with the teardown, using the same hash table entry.
1103 * It's OK to leave the close in call_rcu. We want it to stay unique for
1104 * all RCU readers that could run concurrently with unregister app,
1105 * therefore we _need_ to only close that socket after a grace period. So
1106 * it should stay in this RCU callback.
1108 * This close() is a very important step of the synchronization model so
1109 * every modification to this function must be carefully reviewed.
1115 lttng_fd_put(LTTNG_FD_APPS
, 1);
1117 DBG2("UST app pid %d deleted", app
->pid
);
1119 session_unlock_list();
1123 * URCU intermediate call to delete an UST app.
1126 void delete_ust_app_rcu(struct rcu_head
*head
)
1128 struct lttng_ht_node_ulong
*node
=
1129 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1130 struct ust_app
*app
=
1131 lttng::utils::container_of(node
, &ust_app::pid_n
);
1133 DBG3("Call RCU deleting app PID %d", app
->pid
);
1134 delete_ust_app(app
);
1138 * Delete the session from the application ht and delete the data structure by
1139 * freeing every object inside and releasing them.
1141 * The session list lock must be held by the caller.
1143 static void destroy_app_session(struct ust_app
*app
,
1144 struct ust_app_session
*ua_sess
)
1147 struct lttng_ht_iter iter
;
1150 LTTNG_ASSERT(ua_sess
);
1152 iter
.iter
.node
= &ua_sess
->node
.node
;
1153 ret
= lttng_ht_del(app
->sessions
, &iter
);
1155 /* Already scheduled for teardown. */
1159 /* Once deleted, free the data structure. */
1160 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1167 * Alloc new UST app session.
1170 struct ust_app_session
*alloc_ust_app_session(void)
1172 struct ust_app_session
*ua_sess
;
1174 /* Init most of the default value by allocating and zeroing */
1175 ua_sess
= zmalloc
<ust_app_session
>();
1176 if (ua_sess
== NULL
) {
1181 ua_sess
->handle
= -1;
1182 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1183 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1184 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1193 * Alloc new UST app channel.
1196 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1197 struct ust_app_session
*ua_sess
,
1198 struct lttng_ust_abi_channel_attr
*attr
)
1200 struct ust_app_channel
*ua_chan
;
1202 /* Init most of the default value by allocating and zeroing */
1203 ua_chan
= zmalloc
<ust_app_channel
>();
1204 if (ua_chan
== NULL
) {
1209 /* Setup channel name */
1210 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1211 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1213 ua_chan
->enabled
= 1;
1214 ua_chan
->handle
= -1;
1215 ua_chan
->session
= ua_sess
;
1216 ua_chan
->key
= get_next_channel_key();
1217 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1218 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1219 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1221 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1222 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1224 /* Copy attributes */
1226 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1227 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1228 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1229 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1230 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1231 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1232 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1233 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1235 /* By default, the channel is a per cpu channel. */
1236 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1238 DBG3("UST app channel %s allocated", ua_chan
->name
);
1247 * Allocate and initialize a UST app stream.
1249 * Return newly allocated stream pointer or NULL on error.
1251 struct ust_app_stream
*ust_app_alloc_stream(void)
1253 struct ust_app_stream
*stream
= NULL
;
1255 stream
= zmalloc
<ust_app_stream
>();
1256 if (stream
== NULL
) {
1257 PERROR("zmalloc ust app stream");
1261 /* Zero could be a valid value for a handle so flag it to -1. */
1262 stream
->handle
= -1;
1269 * Alloc new UST app event.
1272 struct ust_app_event
*alloc_ust_app_event(char *name
,
1273 struct lttng_ust_abi_event
*attr
)
1275 struct ust_app_event
*ua_event
;
1277 /* Init most of the default value by allocating and zeroing */
1278 ua_event
= zmalloc
<ust_app_event
>();
1279 if (ua_event
== NULL
) {
1280 PERROR("Failed to allocate ust_app_event structure");
1284 ua_event
->enabled
= 1;
1285 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1286 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1287 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1289 /* Copy attributes */
1291 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1294 DBG3("UST app event %s allocated", ua_event
->name
);
1303 * Allocate a new UST app event notifier rule.
1305 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1306 struct lttng_trigger
*trigger
)
1308 enum lttng_event_rule_generate_exclusions_status
1309 generate_exclusion_status
;
1310 enum lttng_condition_status cond_status
;
1311 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1312 struct lttng_condition
*condition
= NULL
;
1313 const struct lttng_event_rule
*event_rule
= NULL
;
1315 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1316 if (ua_event_notifier_rule
== NULL
) {
1317 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1321 ua_event_notifier_rule
->enabled
= 1;
1322 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1323 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1324 ua_event_notifier_rule
->token
);
1326 condition
= lttng_trigger_get_condition(trigger
);
1327 LTTNG_ASSERT(condition
);
1328 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1329 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1331 cond_status
= lttng_condition_event_rule_matches_get_rule(
1332 condition
, &event_rule
);
1333 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1334 LTTNG_ASSERT(event_rule
);
1336 ua_event_notifier_rule
->error_counter_index
=
1337 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1338 /* Acquire the event notifier's reference to the trigger. */
1339 lttng_trigger_get(trigger
);
1341 ua_event_notifier_rule
->trigger
= trigger
;
1342 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1343 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1344 event_rule
, &ua_event_notifier_rule
->exclusion
);
1345 switch (generate_exclusion_status
) {
1346 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1347 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1350 /* Error occurred. */
1351 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1352 goto error_put_trigger
;
1355 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1356 ua_event_notifier_rule
->token
);
1358 return ua_event_notifier_rule
;
1361 lttng_trigger_put(trigger
);
1363 free(ua_event_notifier_rule
);
1368 * Alloc new UST app context.
1371 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1373 struct ust_app_ctx
*ua_ctx
;
1375 ua_ctx
= zmalloc
<ust_app_ctx
>();
1376 if (ua_ctx
== NULL
) {
1380 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1383 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1384 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1385 char *provider_name
= NULL
, *ctx_name
= NULL
;
1387 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1388 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1389 if (!provider_name
|| !ctx_name
) {
1390 free(provider_name
);
1395 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1396 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1400 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1408 * Create a liblttng-ust filter bytecode from given bytecode.
1410 * Return allocated filter or NULL on error.
1412 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1413 const struct lttng_bytecode
*orig_f
)
1415 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1417 /* Copy filter bytecode. */
1418 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1420 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1424 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1425 sizeof(struct lttng_ust_abi_filter_bytecode
));
1426 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1432 * Create a liblttng-ust capture bytecode from given bytecode.
1434 * Return allocated filter or NULL on error.
1436 static struct lttng_ust_abi_capture_bytecode
*
1437 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1439 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1441 /* Copy capture bytecode. */
1442 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1444 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1448 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1449 sizeof(struct lttng_ust_abi_capture_bytecode
));
1450 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1456 * Find an ust_app using the sock and return it. RCU read side lock must be
1457 * held before calling this helper function.
1459 struct ust_app
*ust_app_find_by_sock(int sock
)
1461 struct lttng_ht_node_ulong
*node
;
1462 struct lttng_ht_iter iter
;
1464 ASSERT_RCU_READ_LOCKED();
1466 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1467 node
= lttng_ht_iter_get_node_ulong(&iter
);
1469 DBG2("UST app find by sock %d not found", sock
);
1473 return lttng::utils::container_of(node
, &ust_app::sock_n
);
1480 * Find an ust_app using the notify sock and return it. RCU read side lock must
1481 * be held before calling this helper function.
1483 static struct ust_app
*find_app_by_notify_sock(int sock
)
1485 struct lttng_ht_node_ulong
*node
;
1486 struct lttng_ht_iter iter
;
1488 ASSERT_RCU_READ_LOCKED();
1490 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1492 node
= lttng_ht_iter_get_node_ulong(&iter
);
1494 DBG2("UST app find by notify sock %d not found", sock
);
1498 return lttng::utils::container_of(node
, &ust_app::notify_sock_n
);
1505 * Lookup for an ust app event based on event name, filter bytecode and the
1508 * Return an ust_app_event object or NULL on error.
1510 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1511 const char *name
, const struct lttng_bytecode
*filter
,
1513 const struct lttng_event_exclusion
*exclusion
)
1515 struct lttng_ht_iter iter
;
1516 struct lttng_ht_node_str
*node
;
1517 struct ust_app_event
*event
= NULL
;
1518 struct ust_app_ht_key key
;
1523 /* Setup key for event lookup. */
1525 key
.filter
= filter
;
1526 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) loglevel_value
;
1527 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1528 key
.exclusion
= exclusion
;
1530 /* Lookup using the event name as hash and a custom match fct. */
1531 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1532 ht_match_ust_app_event
, &key
, &iter
.iter
);
1533 node
= lttng_ht_iter_get_node_str(&iter
);
1538 event
= lttng::utils::container_of(node
, &ust_app_event::node
);
1545 * Look-up an event notifier rule based on its token id.
1547 * Must be called with the RCU read lock held.
1548 * Return an ust_app_event_notifier_rule object or NULL on error.
1550 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1551 struct lttng_ht
*ht
, uint64_t token
)
1553 struct lttng_ht_iter iter
;
1554 struct lttng_ht_node_u64
*node
;
1555 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1558 ASSERT_RCU_READ_LOCKED();
1560 lttng_ht_lookup(ht
, &token
, &iter
);
1561 node
= lttng_ht_iter_get_node_u64(&iter
);
1563 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1568 event_notifier_rule
= lttng::utils::container_of(
1569 node
, &ust_app_event_notifier_rule::node
);
1571 return event_notifier_rule
;
1575 * Create the channel context on the tracer.
1577 * Called with UST app session lock held.
1580 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1581 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1585 health_code_update();
1587 pthread_mutex_lock(&app
->sock_lock
);
1588 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1589 ua_chan
->obj
, &ua_ctx
->obj
);
1590 pthread_mutex_unlock(&app
->sock_lock
);
1592 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1594 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1595 app
->pid
, app
->sock
);
1596 } else if (ret
== -EAGAIN
) {
1598 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1599 app
->pid
, app
->sock
);
1601 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1602 ret
, app
->pid
, app
->sock
);
1607 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1609 DBG2("UST app context handle %d created successfully for channel %s",
1610 ua_ctx
->handle
, ua_chan
->name
);
1613 health_code_update();
1618 * Set the filter on the tracer.
1620 static int set_ust_object_filter(struct ust_app
*app
,
1621 const struct lttng_bytecode
*bytecode
,
1622 struct lttng_ust_abi_object_data
*ust_object
)
1625 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1627 health_code_update();
1629 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1630 if (!ust_bytecode
) {
1631 ret
= -LTTNG_ERR_NOMEM
;
1634 pthread_mutex_lock(&app
->sock_lock
);
1635 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
,
1637 pthread_mutex_unlock(&app
->sock_lock
);
1639 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1641 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1642 app
->pid
, app
->sock
);
1643 } else if (ret
== -EAGAIN
) {
1645 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1646 app
->pid
, app
->sock
);
1648 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1649 ret
, app
->pid
, app
->sock
, ust_object
);
1654 DBG2("UST filter successfully set: object = %p", ust_object
);
1657 health_code_update();
1663 * Set a capture bytecode for the passed object.
1664 * The sequence number enforces the ordering at runtime and on reception of
1665 * the captured payloads.
1667 static int set_ust_capture(struct ust_app
*app
,
1668 const struct lttng_bytecode
*bytecode
,
1669 unsigned int capture_seqnum
,
1670 struct lttng_ust_abi_object_data
*ust_object
)
1673 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1675 health_code_update();
1677 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1678 if (!ust_bytecode
) {
1679 ret
= -LTTNG_ERR_NOMEM
;
1684 * Set the sequence number to ensure the capture of fields is ordered.
1686 ust_bytecode
->seqnum
= capture_seqnum
;
1688 pthread_mutex_lock(&app
->sock_lock
);
1689 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
,
1691 pthread_mutex_unlock(&app
->sock_lock
);
1693 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1695 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1696 app
->pid
, app
->sock
);
1697 } else if (ret
== -EAGAIN
) {
1699 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1700 app
->pid
, app
->sock
);
1702 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1710 DBG2("UST capture successfully set: object = %p", ust_object
);
1713 health_code_update();
1719 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1720 const struct lttng_event_exclusion
*exclusion
)
1722 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1723 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1724 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1726 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1727 if (!ust_exclusion
) {
1732 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1733 sizeof(struct lttng_ust_abi_event_exclusion
));
1734 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1736 return ust_exclusion
;
1740 * Set event exclusions on the tracer.
1742 static int set_ust_object_exclusions(struct ust_app
*app
,
1743 const struct lttng_event_exclusion
*exclusions
,
1744 struct lttng_ust_abi_object_data
*ust_object
)
1747 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1749 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1751 health_code_update();
1753 ust_exclusions
= create_ust_exclusion_from_exclusion(
1755 if (!ust_exclusions
) {
1756 ret
= -LTTNG_ERR_NOMEM
;
1759 pthread_mutex_lock(&app
->sock_lock
);
1760 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1761 pthread_mutex_unlock(&app
->sock_lock
);
1763 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1765 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1766 app
->pid
, app
->sock
);
1767 } else if (ret
== -EAGAIN
) {
1769 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1770 app
->pid
, app
->sock
);
1772 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1773 ret
, app
->pid
, app
->sock
, ust_object
);
1778 DBG2("UST exclusions set successfully for object %p", ust_object
);
1781 health_code_update();
1782 free(ust_exclusions
);
1787 * Disable the specified event on to UST tracer for the UST session.
1789 static int disable_ust_object(struct ust_app
*app
,
1790 struct lttng_ust_abi_object_data
*object
)
1794 health_code_update();
1796 pthread_mutex_lock(&app
->sock_lock
);
1797 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1798 pthread_mutex_unlock(&app
->sock_lock
);
1800 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1802 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1803 app
->pid
, app
->sock
);
1804 } else if (ret
== -EAGAIN
) {
1806 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1807 app
->pid
, app
->sock
);
1809 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1810 ret
, app
->pid
, app
->sock
, object
);
1815 DBG2("UST app object %p disabled successfully for app: pid = %d",
1819 health_code_update();
1824 * Disable the specified channel on to UST tracer for the UST session.
1826 static int disable_ust_channel(struct ust_app
*app
,
1827 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1831 health_code_update();
1833 pthread_mutex_lock(&app
->sock_lock
);
1834 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1835 pthread_mutex_unlock(&app
->sock_lock
);
1837 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1839 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1840 app
->pid
, app
->sock
);
1841 } else if (ret
== -EAGAIN
) {
1843 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1844 app
->pid
, app
->sock
);
1846 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1847 ua_chan
->name
, ua_sess
->handle
, ret
,
1848 app
->pid
, app
->sock
);
1853 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1854 ua_chan
->name
, app
->pid
);
1857 health_code_update();
1862 * Enable the specified channel on to UST tracer for the UST session.
1864 static int enable_ust_channel(struct ust_app
*app
,
1865 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1869 health_code_update();
1871 pthread_mutex_lock(&app
->sock_lock
);
1872 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1873 pthread_mutex_unlock(&app
->sock_lock
);
1875 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1877 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1878 ua_chan
->name
, app
->pid
, app
->sock
);
1879 } else if (ret
== -EAGAIN
) {
1881 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1882 ua_chan
->name
, app
->pid
, app
->sock
);
1884 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1885 ua_chan
->name
, ua_sess
->handle
, ret
,
1886 app
->pid
, app
->sock
);
1891 ua_chan
->enabled
= 1;
1893 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1894 ua_chan
->name
, app
->pid
);
1897 health_code_update();
1902 * Enable the specified event on to UST tracer for the UST session.
1904 static int enable_ust_object(
1905 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1909 health_code_update();
1911 pthread_mutex_lock(&app
->sock_lock
);
1912 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1913 pthread_mutex_unlock(&app
->sock_lock
);
1915 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1917 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1918 app
->pid
, app
->sock
);
1919 } else if (ret
== -EAGAIN
) {
1921 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1922 app
->pid
, app
->sock
);
1924 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1925 ret
, app
->pid
, app
->sock
, ust_object
);
1930 DBG2("UST app object %p enabled successfully for app: pid = %d",
1931 ust_object
, app
->pid
);
1934 health_code_update();
1939 * Send channel and stream buffer to application.
1941 * Return 0 on success. On error, a negative value is returned.
1943 static int send_channel_pid_to_ust(struct ust_app
*app
,
1944 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1947 struct ust_app_stream
*stream
, *stmp
;
1950 LTTNG_ASSERT(ua_sess
);
1951 LTTNG_ASSERT(ua_chan
);
1953 health_code_update();
1955 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1958 /* Send channel to the application. */
1959 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1960 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1961 ret
= -ENOTCONN
; /* Caused by app exiting. */
1963 } else if (ret
== -EAGAIN
) {
1964 /* Caused by timeout. */
1965 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
1966 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
1967 /* Treat this the same way as an application that is exiting. */
1970 } else if (ret
< 0) {
1974 health_code_update();
1976 /* Send all streams to application. */
1977 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1978 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1979 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1980 ret
= -ENOTCONN
; /* Caused by app exiting. */
1982 } else if (ret
== -EAGAIN
) {
1983 /* Caused by timeout. */
1984 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
1985 app
->pid
, stream
->name
, ua_chan
->name
,
1986 ua_sess
->tracing_id
);
1988 * Treat this the same way as an application that is
1992 } else if (ret
< 0) {
1995 /* We don't need the stream anymore once sent to the tracer. */
1996 cds_list_del(&stream
->list
);
1997 delete_ust_app_stream(-1, stream
, app
);
2001 health_code_update();
2006 * Create the specified event onto the UST tracer for a UST session.
2008 * Should be called with session mutex held.
2011 int create_ust_event(struct ust_app
*app
,
2012 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
2016 health_code_update();
2018 /* Create UST event on tracer */
2019 pthread_mutex_lock(&app
->sock_lock
);
2020 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
2022 pthread_mutex_unlock(&app
->sock_lock
);
2024 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2026 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2027 app
->pid
, app
->sock
);
2028 } else if (ret
== -EAGAIN
) {
2030 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2031 app
->pid
, app
->sock
);
2033 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2034 ua_event
->attr
.name
, ret
, app
->pid
,
2040 ua_event
->handle
= ua_event
->obj
->handle
;
2042 DBG2("UST app event %s created successfully for pid:%d object = %p",
2043 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
2045 health_code_update();
2047 /* Set filter if one is present. */
2048 if (ua_event
->filter
) {
2049 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2055 /* Set exclusions for the event */
2056 if (ua_event
->exclusion
) {
2057 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2063 /* If event not enabled, disable it on the tracer */
2064 if (ua_event
->enabled
) {
2066 * We now need to explicitly enable the event, since it
2067 * is now disabled at creation.
2069 ret
= enable_ust_object(app
, ua_event
->obj
);
2072 * If we hit an EPERM, something is wrong with our enable call. If
2073 * we get an EEXIST, there is a problem on the tracer side since we
2077 case -LTTNG_UST_ERR_PERM
:
2078 /* Code flow problem */
2080 case -LTTNG_UST_ERR_EXIST
:
2081 /* It's OK for our use case. */
2092 health_code_update();
2096 static int init_ust_event_notifier_from_event_rule(
2097 const struct lttng_event_rule
*rule
,
2098 struct lttng_ust_abi_event_notifier
*event_notifier
)
2100 enum lttng_event_rule_status status
;
2101 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2102 int loglevel
= -1, ret
= 0;
2103 const char *pattern
;
2106 memset(event_notifier
, 0, sizeof(*event_notifier
));
2108 if (lttng_event_rule_targets_agent_domain(rule
)) {
2110 * Special event for agents
2111 * The actual meat of the event is in the filter that will be
2112 * attached later on.
2113 * Set the default values for the agent event.
2115 pattern
= event_get_default_agent_ust_name(
2116 lttng_event_rule_get_domain_type(rule
));
2118 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2120 const struct lttng_log_level_rule
*log_level_rule
;
2122 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2123 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2125 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2126 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2127 /* At this point, this is a fatal error. */
2131 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(
2132 rule
, &log_level_rule
);
2133 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2134 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2135 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2136 enum lttng_log_level_rule_status llr_status
;
2138 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2139 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2140 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2141 llr_status
= lttng_log_level_rule_exactly_get_level(
2142 log_level_rule
, &loglevel
);
2144 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2145 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2146 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2147 log_level_rule
, &loglevel
);
2153 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2155 /* At this point this is a fatal error. */
2160 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2161 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2162 sizeof(event_notifier
->event
.name
));
2164 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2169 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2170 event_notifier
->event
.loglevel
= loglevel
;
2176 * Create the specified event notifier against the user space tracer of a
2177 * given application.
2179 static int create_ust_event_notifier(struct ust_app
*app
,
2180 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2183 enum lttng_condition_status condition_status
;
2184 const struct lttng_condition
*condition
= NULL
;
2185 struct lttng_ust_abi_event_notifier event_notifier
;
2186 const struct lttng_event_rule
*event_rule
= NULL
;
2187 unsigned int capture_bytecode_count
= 0, i
;
2188 enum lttng_condition_status cond_status
;
2189 enum lttng_event_rule_type event_rule_type
;
2191 health_code_update();
2192 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2194 condition
= lttng_trigger_get_const_condition(
2195 ua_event_notifier_rule
->trigger
);
2196 LTTNG_ASSERT(condition
);
2197 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2198 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2200 condition_status
= lttng_condition_event_rule_matches_get_rule(
2201 condition
, &event_rule
);
2202 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2204 LTTNG_ASSERT(event_rule
);
2206 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2207 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2208 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2210 LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2212 LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2214 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2215 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2216 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2218 /* Create UST event notifier against the tracer. */
2219 pthread_mutex_lock(&app
->sock_lock
);
2220 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
, &event_notifier
,
2221 app
->event_notifier_group
.object
,
2222 &ua_event_notifier_rule
->obj
);
2223 pthread_mutex_unlock(&app
->sock_lock
);
2225 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2227 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2228 app
->pid
, app
->sock
);
2229 } else if (ret
== -EAGAIN
) {
2231 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2232 app
->pid
, app
->sock
);
2234 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2235 event_notifier
.event
.name
, ret
, app
->pid
,
2241 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2243 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2244 event_notifier
.event
.name
, app
->name
, app
->pid
,
2245 ua_event_notifier_rule
->obj
);
2247 health_code_update();
2249 /* Set filter if one is present. */
2250 if (ua_event_notifier_rule
->filter
) {
2251 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2252 ua_event_notifier_rule
->obj
);
2258 /* Set exclusions for the event. */
2259 if (ua_event_notifier_rule
->exclusion
) {
2260 ret
= set_ust_object_exclusions(app
,
2261 ua_event_notifier_rule
->exclusion
,
2262 ua_event_notifier_rule
->obj
);
2268 /* Set the capture bytecodes. */
2269 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2270 condition
, &capture_bytecode_count
);
2271 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2273 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2274 const struct lttng_bytecode
*capture_bytecode
=
2275 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2278 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2279 ua_event_notifier_rule
->obj
);
2286 * We now need to explicitly enable the event, since it
2287 * is disabled at creation.
2289 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2292 * If we hit an EPERM, something is wrong with our enable call.
2293 * If we get an EEXIST, there is a problem on the tracer side
2294 * since we just created it.
2297 case -LTTNG_UST_ERR_PERM
:
2298 /* Code flow problem. */
2300 case -LTTNG_UST_ERR_EXIST
:
2301 /* It's OK for our use case. */
2311 ua_event_notifier_rule
->enabled
= true;
2314 health_code_update();
2319 * Copy data between an UST app event and a LTT event.
2321 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2322 struct ltt_ust_event
*uevent
)
2324 size_t exclusion_alloc_size
;
2326 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2327 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2329 ua_event
->enabled
= uevent
->enabled
;
2331 /* Copy event attributes */
2332 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2334 /* Copy filter bytecode */
2335 if (uevent
->filter
) {
2336 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2337 /* Filter might be NULL here in case of ENONEM. */
2340 /* Copy exclusion data */
2341 if (uevent
->exclusion
) {
2342 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2343 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2344 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2345 if (ua_event
->exclusion
== NULL
) {
2348 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2349 exclusion_alloc_size
);
2355 * Copy data between an UST app channel and a LTT channel.
2357 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2358 struct ltt_ust_channel
*uchan
)
2360 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2362 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2363 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2365 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2366 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2368 /* Copy event attributes since the layout is different. */
2369 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2370 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2371 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2372 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2373 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2374 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2375 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2376 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2379 * Note that the attribute channel type is not set since the channel on the
2380 * tracing registry side does not have this information.
2383 ua_chan
->enabled
= uchan
->enabled
;
2384 ua_chan
->tracing_channel_id
= uchan
->id
;
2386 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2390 * Copy data between a UST app session and a regular LTT session.
2392 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2393 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2395 struct tm
*timeinfo
;
2398 char tmp_shm_path
[PATH_MAX
];
2400 timeinfo
= localtime(&app
->registration_time
);
2401 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2403 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2405 ua_sess
->tracing_id
= usess
->id
;
2406 ua_sess
->id
= get_next_session_id();
2407 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2408 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2409 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2410 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2411 ua_sess
->buffer_type
= usess
->buffer_type
;
2412 ua_sess
->bits_per_long
= app
->abi
.bits_per_long
;
2414 /* There is only one consumer object per session possible. */
2415 consumer_output_get(usess
->consumer
);
2416 ua_sess
->consumer
= usess
->consumer
;
2418 ua_sess
->output_traces
= usess
->output_traces
;
2419 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2420 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2421 &usess
->metadata_attr
);
2423 switch (ua_sess
->buffer_type
) {
2424 case LTTNG_BUFFER_PER_PID
:
2425 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2426 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2429 case LTTNG_BUFFER_PER_UID
:
2430 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2431 DEFAULT_UST_TRACE_UID_PATH
,
2432 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2433 app
->abi
.bits_per_long
);
2440 PERROR("asprintf UST shadow copy session");
2445 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2446 sizeof(ua_sess
->root_shm_path
));
2447 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2448 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2449 sizeof(ua_sess
->shm_path
));
2450 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2451 if (ua_sess
->shm_path
[0]) {
2452 switch (ua_sess
->buffer_type
) {
2453 case LTTNG_BUFFER_PER_PID
:
2454 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2455 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2456 app
->name
, app
->pid
, datetime
);
2458 case LTTNG_BUFFER_PER_UID
:
2459 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2460 "/" DEFAULT_UST_TRACE_UID_PATH
,
2461 app
->uid
, app
->abi
.bits_per_long
);
2468 PERROR("sprintf UST shadow copy session");
2472 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2473 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2474 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2479 consumer_output_put(ua_sess
->consumer
);
2483 * Lookup sesison wrapper.
2486 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2487 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2489 /* Get right UST app session from app */
2490 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2494 * Return ust app session from the app session hashtable using the UST session
2497 static struct ust_app_session
*lookup_session_by_app(
2498 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2500 struct lttng_ht_iter iter
;
2501 struct lttng_ht_node_u64
*node
;
2503 __lookup_session_by_app(usess
, app
, &iter
);
2504 node
= lttng_ht_iter_get_node_u64(&iter
);
2509 return lttng::utils::container_of(node
, &ust_app_session::node
);
2516 * Setup buffer registry per PID for the given session and application. If none
2517 * is found, a new one is created, added to the global registry and
2518 * initialized. If regp is valid, it's set with the newly created object.
2520 * Return 0 on success or else a negative value.
2522 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2523 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2526 struct buffer_reg_pid
*reg_pid
;
2528 LTTNG_ASSERT(ua_sess
);
2533 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2536 * This is the create channel path meaning that if there is NO
2537 * registry available, we have to create one for this session.
2539 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2540 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2548 /* Initialize registry. */
2549 reg_pid
->registry
->reg
.ust
= ust_registry_session_per_pid_create(app
, app
->abi
,
2550 app
->version
.major
, app
->version
.minor
, reg_pid
->root_shm_path
,
2552 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2553 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2554 ua_sess
->tracing_id
);
2555 if (!reg_pid
->registry
->reg
.ust
) {
2557 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2558 * destroy the buffer registry, because it is always expected
2559 * that if the buffer registry can be found, its ust registry is
2562 buffer_reg_pid_destroy(reg_pid
);
2566 buffer_reg_pid_add(reg_pid
);
2568 DBG3("UST app buffer registry per PID created successfully");
2580 * Setup buffer registry per UID for the given session and application. If none
2581 * is found, a new one is created, added to the global registry and
2582 * initialized. If regp is valid, it's set with the newly created object.
2584 * Return 0 on success or else a negative value.
2586 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2587 struct ust_app_session
*ua_sess
,
2588 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2591 struct buffer_reg_uid
*reg_uid
;
2593 LTTNG_ASSERT(usess
);
2598 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
2601 * This is the create channel path meaning that if there is NO
2602 * registry available, we have to create one for this session.
2604 ret
= buffer_reg_uid_create(usess
->id
, app
->abi
.bits_per_long
, app
->uid
,
2605 LTTNG_DOMAIN_UST
, ®_uid
, ua_sess
->root_shm_path
,
2614 /* Initialize registry. */
2615 reg_uid
->registry
->reg
.ust
= ust_registry_session_per_uid_create(app
->abi
,
2616 app
->version
.major
, app
->version
.minor
, reg_uid
->root_shm_path
,
2617 reg_uid
->shm_path
, usess
->uid
, usess
->gid
, ua_sess
->tracing_id
, app
->uid
);
2618 if (!reg_uid
->registry
->reg
.ust
) {
2620 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2621 * destroy the buffer registry, because it is always expected
2622 * that if the buffer registry can be found, its ust registry is
2625 buffer_reg_uid_destroy(reg_uid
, NULL
);
2629 /* Add node to teardown list of the session. */
2630 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2632 buffer_reg_uid_add(reg_uid
);
2634 DBG3("UST app buffer registry per UID created successfully");
2645 * Create a session on the tracer side for the given app.
2647 * On success, ua_sess_ptr is populated with the session pointer or else left
2648 * untouched. If the session was created, is_created is set to 1. On error,
2649 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2652 * Returns 0 on success or else a negative code which is either -ENOMEM or
2653 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2655 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2656 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2659 int ret
, created
= 0;
2660 struct ust_app_session
*ua_sess
;
2662 LTTNG_ASSERT(usess
);
2664 LTTNG_ASSERT(ua_sess_ptr
);
2666 health_code_update();
2668 ua_sess
= lookup_session_by_app(usess
, app
);
2669 if (ua_sess
== NULL
) {
2670 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2671 app
->pid
, usess
->id
);
2672 ua_sess
= alloc_ust_app_session();
2673 if (ua_sess
== NULL
) {
2674 /* Only malloc can failed so something is really wrong */
2678 shadow_copy_session(ua_sess
, usess
, app
);
2682 switch (usess
->buffer_type
) {
2683 case LTTNG_BUFFER_PER_PID
:
2684 /* Init local registry. */
2685 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2687 delete_ust_app_session(-1, ua_sess
, app
);
2691 case LTTNG_BUFFER_PER_UID
:
2692 /* Look for a global registry. If none exists, create one. */
2693 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2695 delete_ust_app_session(-1, ua_sess
, app
);
2705 health_code_update();
2707 if (ua_sess
->handle
== -1) {
2708 pthread_mutex_lock(&app
->sock_lock
);
2709 ret
= lttng_ust_ctl_create_session(app
->sock
);
2710 pthread_mutex_unlock(&app
->sock_lock
);
2712 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2713 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2714 app
->pid
, app
->sock
);
2716 } else if (ret
== -EAGAIN
) {
2717 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2718 app
->pid
, app
->sock
);
2721 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2722 ret
, app
->pid
, app
->sock
);
2724 delete_ust_app_session(-1, ua_sess
, app
);
2725 if (ret
!= -ENOMEM
) {
2727 * Tracer is probably gone or got an internal error so let's
2728 * behave like it will soon unregister or not usable.
2735 ua_sess
->handle
= ret
;
2737 /* Add ust app session to app's HT */
2738 lttng_ht_node_init_u64(&ua_sess
->node
,
2739 ua_sess
->tracing_id
);
2740 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2741 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2742 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2743 &ua_sess
->ust_objd_node
);
2745 DBG2("UST app session created successfully with handle %d", ret
);
2748 *ua_sess_ptr
= ua_sess
;
2750 *is_created
= created
;
2753 /* Everything went well. */
2757 health_code_update();
2762 * Match function for a hash table lookup of ust_app_ctx.
2764 * It matches an ust app context based on the context type and, in the case
2765 * of perf counters, their name.
2767 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2769 struct ust_app_ctx
*ctx
;
2770 const struct lttng_ust_context_attr
*key
;
2775 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2776 key
= (lttng_ust_context_attr
*) _key
;
2779 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2784 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2785 if (strncmp(key
->u
.perf_counter
.name
,
2786 ctx
->ctx
.u
.perf_counter
.name
,
2787 sizeof(key
->u
.perf_counter
.name
))) {
2791 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2792 if (strcmp(key
->u
.app_ctx
.provider_name
,
2793 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2794 strcmp(key
->u
.app_ctx
.ctx_name
,
2795 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2811 * Lookup for an ust app context from an lttng_ust_context.
2813 * Must be called while holding RCU read side lock.
2814 * Return an ust_app_ctx object or NULL on error.
2817 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2818 struct lttng_ust_context_attr
*uctx
)
2820 struct lttng_ht_iter iter
;
2821 struct lttng_ht_node_ulong
*node
;
2822 struct ust_app_ctx
*app_ctx
= NULL
;
2826 ASSERT_RCU_READ_LOCKED();
2828 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2829 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2830 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2831 node
= lttng_ht_iter_get_node_ulong(&iter
);
2836 app_ctx
= lttng::utils::container_of(node
, &ust_app_ctx::node
);
2843 * Create a context for the channel on the tracer.
2845 * Called with UST app session lock held and a RCU read side lock.
2848 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2849 struct lttng_ust_context_attr
*uctx
,
2850 struct ust_app
*app
)
2853 struct ust_app_ctx
*ua_ctx
;
2855 ASSERT_RCU_READ_LOCKED();
2857 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2859 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2865 ua_ctx
= alloc_ust_app_ctx(uctx
);
2866 if (ua_ctx
== NULL
) {
2872 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2873 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2874 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2876 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2886 * Enable on the tracer side a ust app event for the session and channel.
2888 * Called with UST app session lock held.
2891 int enable_ust_app_event(struct ust_app_event
*ua_event
,
2892 struct ust_app
*app
)
2896 ret
= enable_ust_object(app
, ua_event
->obj
);
2901 ua_event
->enabled
= 1;
2908 * Disable on the tracer side a ust app event for the session and channel.
2910 static int disable_ust_app_event(struct ust_app_event
*ua_event
,
2911 struct ust_app
*app
)
2915 ret
= disable_ust_object(app
, ua_event
->obj
);
2920 ua_event
->enabled
= 0;
2927 * Lookup ust app channel for session and disable it on the tracer side.
2930 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2931 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2935 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2940 ua_chan
->enabled
= 0;
2947 * Lookup ust app channel for session and enable it on the tracer side. This
2948 * MUST be called with a RCU read side lock acquired.
2950 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2951 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2954 struct lttng_ht_iter iter
;
2955 struct lttng_ht_node_str
*ua_chan_node
;
2956 struct ust_app_channel
*ua_chan
;
2958 ASSERT_RCU_READ_LOCKED();
2960 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2961 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2962 if (ua_chan_node
== NULL
) {
2963 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2964 uchan
->name
, ua_sess
->tracing_id
);
2968 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
2970 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2980 * Ask the consumer to create a channel and get it if successful.
2982 * Called with UST app session lock held.
2984 * Return 0 on success or else a negative value.
2986 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2987 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2988 int bitness
, lsu::registry_session
*registry
)
2991 unsigned int nb_fd
= 0;
2992 struct consumer_socket
*socket
;
2994 LTTNG_ASSERT(usess
);
2995 LTTNG_ASSERT(ua_sess
);
2996 LTTNG_ASSERT(ua_chan
);
2997 LTTNG_ASSERT(registry
);
3000 health_code_update();
3002 /* Get the right consumer socket for the application. */
3003 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3009 health_code_update();
3011 /* Need one fd for the channel. */
3012 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3014 ERR("Exhausted number of available FD upon create channel");
3019 * Ask consumer to create channel. The consumer will return the number of
3020 * stream we have to expect.
3022 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
3023 registry
, usess
->current_trace_chunk
);
3029 * Compute the number of fd needed before receiving them. It must be 2 per
3030 * stream (2 being the default value here).
3032 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3034 /* Reserve the amount of file descriptor we need. */
3035 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3037 ERR("Exhausted number of available FD upon create channel");
3038 goto error_fd_get_stream
;
3041 health_code_update();
3044 * Now get the channel from the consumer. This call will populate the stream
3045 * list of that channel and set the ust objects.
3047 if (usess
->consumer
->enabled
) {
3048 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3058 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3059 error_fd_get_stream
:
3061 * Initiate a destroy channel on the consumer since we had an error
3062 * handling it on our side. The return value is of no importance since we
3063 * already have a ret value set by the previous error that we need to
3066 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3068 lttng_fd_put(LTTNG_FD_APPS
, 1);
3070 health_code_update();
3076 * Duplicate the ust data object of the ust app stream and save it in the
3077 * buffer registry stream.
3079 * Return 0 on success or else a negative value.
3081 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3082 struct ust_app_stream
*stream
)
3086 LTTNG_ASSERT(reg_stream
);
3087 LTTNG_ASSERT(stream
);
3089 /* Duplicating a stream requires 2 new fds. Reserve them. */
3090 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3092 ERR("Exhausted number of available FD upon duplicate stream");
3096 /* Duplicate object for stream once the original is in the registry. */
3097 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
,
3098 reg_stream
->obj
.ust
);
3100 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3101 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3102 lttng_fd_put(LTTNG_FD_APPS
, 2);
3105 stream
->handle
= stream
->obj
->handle
;
3112 * Duplicate the ust data object of the ust app. channel and save it in the
3113 * buffer registry channel.
3115 * Return 0 on success or else a negative value.
3117 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3118 struct ust_app_channel
*ua_chan
)
3122 LTTNG_ASSERT(buf_reg_chan
);
3123 LTTNG_ASSERT(ua_chan
);
3125 /* Duplicating a channel requires 1 new fd. Reserve it. */
3126 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3128 ERR("Exhausted number of available FD upon duplicate channel");
3132 /* Duplicate object for stream once the original is in the registry. */
3133 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3135 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3136 buf_reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3139 ua_chan
->handle
= ua_chan
->obj
->handle
;
3144 lttng_fd_put(LTTNG_FD_APPS
, 1);
3150 * For a given channel buffer registry, setup all streams of the given ust
3151 * application channel.
3153 * Return 0 on success or else a negative value.
3155 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3156 struct ust_app_channel
*ua_chan
,
3157 struct ust_app
*app
)
3160 struct ust_app_stream
*stream
, *stmp
;
3162 LTTNG_ASSERT(buf_reg_chan
);
3163 LTTNG_ASSERT(ua_chan
);
3165 DBG2("UST app setup buffer registry stream");
3167 /* Send all streams to application. */
3168 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3169 struct buffer_reg_stream
*reg_stream
;
3171 ret
= buffer_reg_stream_create(®_stream
);
3177 * Keep original pointer and nullify it in the stream so the delete
3178 * stream call does not release the object.
3180 reg_stream
->obj
.ust
= stream
->obj
;
3182 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3184 /* We don't need the streams anymore. */
3185 cds_list_del(&stream
->list
);
3186 delete_ust_app_stream(-1, stream
, app
);
3194 * Create a buffer registry channel for the given session registry and
3195 * application channel object. If regp pointer is valid, it's set with the
3196 * created object. Important, the created object is NOT added to the session
3197 * registry hash table.
3199 * Return 0 on success else a negative value.
3201 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3202 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3205 struct buffer_reg_channel
*buf_reg_chan
= NULL
;
3207 LTTNG_ASSERT(reg_sess
);
3208 LTTNG_ASSERT(ua_chan
);
3210 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3212 /* Create buffer registry channel. */
3213 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3217 LTTNG_ASSERT(buf_reg_chan
);
3218 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3219 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3220 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3222 /* Create and add a channel registry to session. */
3224 reg_sess
->reg
.ust
->add_channel(ua_chan
->tracing_channel_id
);
3225 } catch (const std::exception
& ex
) {
3226 ERR("Failed to add a channel registry to userspace registry session: %s", ex
.what());
3231 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3234 *regp
= buf_reg_chan
;
3240 /* Safe because the registry channel object was not added to any HT. */
3241 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3247 * Setup buffer registry channel for the given session registry and application
3248 * channel object. If regp pointer is valid, it's set with the created object.
3250 * Return 0 on success else a negative value.
3252 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3253 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*buf_reg_chan
,
3254 struct ust_app
*app
)
3258 LTTNG_ASSERT(reg_sess
);
3259 LTTNG_ASSERT(buf_reg_chan
);
3260 LTTNG_ASSERT(ua_chan
);
3261 LTTNG_ASSERT(ua_chan
->obj
);
3263 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3265 /* Setup all streams for the registry. */
3266 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3271 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3272 ua_chan
->obj
= NULL
;
3277 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3278 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3283 * Send buffer registry channel to the application.
3285 * Return 0 on success else a negative value.
3287 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3288 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3289 struct ust_app_channel
*ua_chan
)
3292 struct buffer_reg_stream
*reg_stream
;
3294 LTTNG_ASSERT(buf_reg_chan
);
3296 LTTNG_ASSERT(ua_sess
);
3297 LTTNG_ASSERT(ua_chan
);
3299 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3301 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3306 /* Send channel to the application. */
3307 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3308 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3309 ret
= -ENOTCONN
; /* Caused by app exiting. */
3311 } else if (ret
== -EAGAIN
) {
3312 /* Caused by timeout. */
3313 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
3314 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
3315 /* Treat this the same way as an application that is exiting. */
3318 } else if (ret
< 0) {
3322 health_code_update();
3324 /* Send all streams to application. */
3325 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3326 cds_list_for_each_entry(reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3327 struct ust_app_stream stream
= {};
3329 ret
= duplicate_stream_object(reg_stream
, &stream
);
3331 goto error_stream_unlock
;
3334 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3336 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3337 ret
= -ENOTCONN
; /* Caused by app exiting. */
3338 } else if (ret
== -EAGAIN
) {
3340 * Caused by timeout.
3341 * Treat this the same way as an application
3344 WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64
"\".",
3347 ua_sess
->tracing_id
);
3350 (void) release_ust_app_stream(-1, &stream
, app
);
3351 goto error_stream_unlock
;
3355 * The return value is not important here. This function will output an
3358 (void) release_ust_app_stream(-1, &stream
, app
);
3361 error_stream_unlock
:
3362 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3368 * Create and send to the application the created buffers with per UID buffers.
3370 * This MUST be called with a RCU read side lock acquired.
3371 * The session list lock and the session's lock must be acquired.
3373 * Return 0 on success else a negative value.
3375 static int create_channel_per_uid(struct ust_app
*app
,
3376 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3377 struct ust_app_channel
*ua_chan
)
3380 struct buffer_reg_uid
*reg_uid
;
3381 struct buffer_reg_channel
*buf_reg_chan
;
3382 struct ltt_session
*session
= NULL
;
3383 enum lttng_error_code notification_ret
;
3386 LTTNG_ASSERT(usess
);
3387 LTTNG_ASSERT(ua_sess
);
3388 LTTNG_ASSERT(ua_chan
);
3389 ASSERT_RCU_READ_LOCKED();
3391 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3393 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->abi
.bits_per_long
, app
->uid
);
3395 * The session creation handles the creation of this global registry
3396 * object. If none can be find, there is a code flow problem or a
3399 LTTNG_ASSERT(reg_uid
);
3401 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3407 /* Create the buffer registry channel object. */
3408 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3410 ERR("Error creating the UST channel \"%s\" registry instance",
3415 session
= session_find_by_id(ua_sess
->tracing_id
);
3416 LTTNG_ASSERT(session
);
3417 ASSERT_LOCKED(session
->lock
);
3418 ASSERT_SESSION_LIST_LOCKED();
3421 * Create the buffers on the consumer side. This call populates the
3422 * ust app channel object with all streams and data object.
3424 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3425 app
->abi
.bits_per_long
, reg_uid
->registry
->reg
.ust
);
3427 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3431 * Let's remove the previously created buffer registry channel so
3432 * it's not visible anymore in the session registry.
3434 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3436 locked_registry
->remove_channel(ua_chan
->tracing_channel_id
, false);
3437 } catch (const std::exception
&ex
) {
3438 DBG("Could not find channel for removal: %s", ex
.what());
3440 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3441 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3446 * Setup the streams and add it to the session registry.
3448 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3449 ua_chan
, buf_reg_chan
, app
);
3451 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3456 auto locked_registry
= reg_uid
->registry
->reg
.ust
->lock();
3457 auto& ust_reg_chan
= locked_registry
->get_channel(ua_chan
->tracing_channel_id
);
3459 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3462 /* Notify the notification subsystem of the channel's creation. */
3463 notification_ret
= notification_thread_command_add_channel(
3464 the_notification_thread_handle
, session
->name
,
3465 lttng_credentials_get_uid(
3466 &ua_sess
->effective_credentials
),
3467 lttng_credentials_get_gid(
3468 &ua_sess
->effective_credentials
),
3469 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3470 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3471 if (notification_ret
!= LTTNG_OK
) {
3472 ret
= - (int) notification_ret
;
3473 ERR("Failed to add channel to notification thread");
3478 /* Send buffers to the application. */
3479 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3481 if (ret
!= -ENOTCONN
) {
3482 ERR("Error sending channel to application");
3489 session_put(session
);
3495 * Create and send to the application the created buffers with per PID buffers.
3497 * Called with UST app session lock held.
3498 * The session list lock and the session's lock must be acquired.
3500 * Return 0 on success else a negative value.
3502 static int create_channel_per_pid(struct ust_app
*app
,
3503 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3504 struct ust_app_channel
*ua_chan
)
3507 lsu::registry_session
*registry
;
3508 enum lttng_error_code cmd_ret
;
3509 struct ltt_session
*session
= NULL
;
3510 uint64_t chan_reg_key
;
3513 LTTNG_ASSERT(usess
);
3514 LTTNG_ASSERT(ua_sess
);
3515 LTTNG_ASSERT(ua_chan
);
3517 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3521 registry
= get_session_registry(ua_sess
);
3522 /* The UST app session lock is held, registry shall not be null. */
3523 LTTNG_ASSERT(registry
);
3525 /* Create and add a new channel registry to session. */
3527 registry
->add_channel(ua_chan
->key
);
3528 } catch (const std::exception
& ex
) {
3529 ERR("Error creating the UST channel \"%s\" registry instance: %s", ua_chan
->name
,
3535 session
= session_find_by_id(ua_sess
->tracing_id
);
3536 LTTNG_ASSERT(session
);
3537 ASSERT_LOCKED(session
->lock
);
3538 ASSERT_SESSION_LIST_LOCKED();
3540 /* Create and get channel on the consumer side. */
3541 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3542 app
->abi
.bits_per_long
, registry
);
3544 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3546 goto error_remove_from_registry
;
3549 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3551 if (ret
!= -ENOTCONN
) {
3552 ERR("Error sending channel to application");
3554 goto error_remove_from_registry
;
3557 chan_reg_key
= ua_chan
->key
;
3559 auto locked_registry
= registry
->lock();
3561 auto& ust_reg_chan
= locked_registry
->get_channel(chan_reg_key
);
3562 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3565 cmd_ret
= notification_thread_command_add_channel(
3566 the_notification_thread_handle
, session
->name
,
3567 lttng_credentials_get_uid(
3568 &ua_sess
->effective_credentials
),
3569 lttng_credentials_get_gid(
3570 &ua_sess
->effective_credentials
),
3571 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3572 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3573 if (cmd_ret
!= LTTNG_OK
) {
3574 ret
= - (int) cmd_ret
;
3575 ERR("Failed to add channel to notification thread");
3576 goto error_remove_from_registry
;
3579 error_remove_from_registry
:
3582 auto locked_registry
= registry
->lock();
3583 locked_registry
->remove_channel(ua_chan
->key
, false);
3584 } catch (const std::exception
& ex
) {
3585 DBG("Could not find channel for removal: %s", ex
.what());
3591 session_put(session
);
3597 * From an already allocated ust app channel, create the channel buffers if
3598 * needed and send them to the application. This MUST be called with a RCU read
3599 * side lock acquired.
3601 * Called with UST app session lock held.
3603 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3604 * the application exited concurrently.
3606 static int ust_app_channel_send(struct ust_app
*app
,
3607 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3608 struct ust_app_channel
*ua_chan
)
3613 LTTNG_ASSERT(usess
);
3614 LTTNG_ASSERT(usess
->active
);
3615 LTTNG_ASSERT(ua_sess
);
3616 LTTNG_ASSERT(ua_chan
);
3617 ASSERT_RCU_READ_LOCKED();
3619 /* Handle buffer type before sending the channel to the application. */
3620 switch (usess
->buffer_type
) {
3621 case LTTNG_BUFFER_PER_UID
:
3623 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3629 case LTTNG_BUFFER_PER_PID
:
3631 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3643 /* Initialize ust objd object using the received handle and add it. */
3644 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3645 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3647 /* If channel is not enabled, disable it on the tracer */
3648 if (!ua_chan
->enabled
) {
3649 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3660 * Create UST app channel and return it through ua_chanp if not NULL.
3662 * Called with UST app session lock and RCU read-side lock held.
3664 * Return 0 on success or else a negative value.
3666 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3667 struct ltt_ust_channel
*uchan
,
3668 enum lttng_ust_abi_chan_type type
,
3669 struct ltt_ust_session
*usess
__attribute__((unused
)),
3670 struct ust_app_channel
**ua_chanp
)
3673 struct lttng_ht_iter iter
;
3674 struct lttng_ht_node_str
*ua_chan_node
;
3675 struct ust_app_channel
*ua_chan
;
3677 ASSERT_RCU_READ_LOCKED();
3679 /* Lookup channel in the ust app session */
3680 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3681 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3682 if (ua_chan_node
!= NULL
) {
3683 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3687 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3688 if (ua_chan
== NULL
) {
3689 /* Only malloc can fail here */
3693 shadow_copy_channel(ua_chan
, uchan
);
3695 /* Set channel type. */
3696 ua_chan
->attr
.type
= type
;
3698 /* Only add the channel if successful on the tracer side. */
3699 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3702 *ua_chanp
= ua_chan
;
3705 /* Everything went well. */
3713 * Create UST app event and create it on the tracer side.
3715 * Must be called with the RCU read side lock held.
3716 * Called with ust app session mutex held.
3719 int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3720 struct ltt_ust_event
*uevent
,
3721 struct ust_app
*app
)
3724 struct ust_app_event
*ua_event
;
3726 ASSERT_RCU_READ_LOCKED();
3728 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3729 if (ua_event
== NULL
) {
3730 /* Only failure mode of alloc_ust_app_event(). */
3734 shadow_copy_event(ua_event
, uevent
);
3736 /* Create it on the tracer side */
3737 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3740 * Not found previously means that it does not exist on the
3741 * tracer. If the application reports that the event existed,
3742 * it means there is a bug in the sessiond or lttng-ust
3743 * (or corruption, etc.)
3745 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3746 ERR("Tracer for application reported that an event being created already existed: "
3747 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3749 app
->pid
, app
->ppid
, app
->uid
,
3755 add_unique_ust_app_event(ua_chan
, ua_event
);
3757 DBG2("UST app create event completed: app = '%s' pid = %d",
3758 app
->name
, app
->pid
);
3764 /* Valid. Calling here is already in a read side lock */
3765 delete_ust_app_event(-1, ua_event
, app
);
3770 * Create UST app event notifier rule and create it on the tracer side.
3772 * Must be called with the RCU read side lock held.
3773 * Called with ust app session mutex held.
3776 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3777 struct ust_app
*app
)
3780 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3782 ASSERT_RCU_READ_LOCKED();
3784 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3785 if (ua_event_notifier_rule
== NULL
) {
3790 /* Create it on the tracer side. */
3791 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3794 * Not found previously means that it does not exist on the
3795 * tracer. If the application reports that the event existed,
3796 * it means there is a bug in the sessiond or lttng-ust
3797 * (or corruption, etc.)
3799 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3800 ERR("Tracer for application reported that an event notifier being created already exists: "
3801 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3802 lttng_trigger_get_tracer_token(trigger
),
3803 app
->pid
, app
->ppid
, app
->uid
,
3809 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3810 &ua_event_notifier_rule
->node
);
3812 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3813 app
->name
, app
->pid
, lttng_trigger_get_tracer_token(trigger
));
3818 /* The RCU read side lock is already being held by the caller. */
3819 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3825 * Create UST metadata and open it on the tracer side.
3827 * Called with UST app session lock held and RCU read side lock.
3829 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3830 struct ust_app
*app
, struct consumer_output
*consumer
)
3833 struct ust_app_channel
*metadata
;
3834 struct consumer_socket
*socket
;
3835 struct ltt_session
*session
= NULL
;
3837 LTTNG_ASSERT(ua_sess
);
3839 LTTNG_ASSERT(consumer
);
3840 ASSERT_RCU_READ_LOCKED();
3842 auto locked_registry
= get_locked_session_registry(ua_sess
);
3843 /* The UST app session is held registry shall not be null. */
3844 LTTNG_ASSERT(locked_registry
);
3846 /* Metadata already exists for this registry or it was closed previously */
3847 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3852 /* Allocate UST metadata */
3853 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3855 /* malloc() failed */
3860 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3862 /* Need one fd for the channel. */
3863 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3865 ERR("Exhausted number of available FD upon create metadata");
3869 /* Get the right consumer socket for the application. */
3870 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3873 goto error_consumer
;
3877 * Keep metadata key so we can identify it on the consumer side. Assign it
3878 * to the registry *before* we ask the consumer so we avoid the race of the
3879 * consumer requesting the metadata and the ask_channel call on our side
3880 * did not returned yet.
3882 locked_registry
->_metadata_key
= metadata
->key
;
3884 session
= session_find_by_id(ua_sess
->tracing_id
);
3885 LTTNG_ASSERT(session
);
3886 ASSERT_LOCKED(session
->lock
);
3887 ASSERT_SESSION_LIST_LOCKED();
3890 * Ask the metadata channel creation to the consumer. The metadata object
3891 * will be created by the consumer and kept their. However, the stream is
3892 * never added or monitored until we do a first push metadata to the
3895 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3896 locked_registry
.get(), session
->current_trace_chunk
);
3898 /* Nullify the metadata key so we don't try to close it later on. */
3899 locked_registry
->_metadata_key
= 0;
3900 goto error_consumer
;
3904 * The setup command will make the metadata stream be sent to the relayd,
3905 * if applicable, and the thread managing the metadatas. This is important
3906 * because after this point, if an error occurs, the only way the stream
3907 * can be deleted is to be monitored in the consumer.
3909 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3911 /* Nullify the metadata key so we don't try to close it later on. */
3912 locked_registry
->_metadata_key
= 0;
3913 goto error_consumer
;
3916 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3917 metadata
->key
, app
->pid
);
3920 lttng_fd_put(LTTNG_FD_APPS
, 1);
3921 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3924 session_put(session
);
3930 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3931 * acquired before calling this function.
3933 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3935 struct ust_app
*app
= NULL
;
3936 struct lttng_ht_node_ulong
*node
;
3937 struct lttng_ht_iter iter
;
3939 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3940 node
= lttng_ht_iter_get_node_ulong(&iter
);
3942 DBG2("UST app no found with pid %d", pid
);
3946 DBG2("Found UST app by pid %d", pid
);
3948 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
3955 * Allocate and init an UST app object using the registration information and
3956 * the command socket. This is called when the command socket connects to the
3959 * The object is returned on success or else NULL.
3961 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3964 struct ust_app
*lta
= NULL
;
3965 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3968 LTTNG_ASSERT(sock
>= 0);
3970 DBG3("UST app creating application for socket %d", sock
);
3972 if ((msg
->bits_per_long
== 64 &&
3973 (uatomic_read(&the_ust_consumerd64_fd
) ==
3975 (msg
->bits_per_long
== 32 &&
3976 (uatomic_read(&the_ust_consumerd32_fd
) ==
3978 ERR("Registration failed: application \"%s\" (pid: %d) has "
3979 "%d-bit long, but no consumerd for this size is available.\n",
3980 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3985 * Reserve the two file descriptors of the event source pipe. The write
3986 * end will be closed once it is passed to the application, at which
3987 * point a single 'put' will be performed.
3989 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3991 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
3992 msg
->name
, (int) msg
->pid
);
3996 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3997 if (!event_notifier_event_source_pipe
) {
3998 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
3999 msg
->name
, msg
->pid
);
4003 lta
= zmalloc
<ust_app
>();
4006 goto error_free_pipe
;
4009 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4011 lta
->ppid
= msg
->ppid
;
4012 lta
->uid
= msg
->uid
;
4013 lta
->gid
= msg
->gid
;
4016 .bits_per_long
= msg
->bits_per_long
,
4017 .long_alignment
= msg
->long_alignment
,
4018 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4019 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4020 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4021 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4022 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4023 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4024 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4027 lta
->v_major
= msg
->major
;
4028 lta
->v_minor
= msg
->minor
;
4029 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4030 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4031 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4032 lta
->notify_sock
= -1;
4033 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4035 /* Copy name and make sure it's NULL terminated. */
4036 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4037 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4040 * Before this can be called, when receiving the registration information,
4041 * the application compatibility is checked. So, at this point, the
4042 * application can work with this session daemon.
4044 lta
->compatible
= 1;
4046 lta
->pid
= msg
->pid
;
4047 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4049 pthread_mutex_init(<a
->sock_lock
, NULL
);
4050 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4052 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4056 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4057 lttng_fd_put(LTTNG_FD_APPS
, 2);
4063 * For a given application object, add it to every hash table.
4065 void ust_app_add(struct ust_app
*app
)
4068 LTTNG_ASSERT(app
->notify_sock
>= 0);
4070 app
->registration_time
= time(NULL
);
4075 * On a re-registration, we want to kick out the previous registration of
4078 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4081 * The socket _should_ be unique until _we_ call close. So, a add_unique
4082 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4083 * already in the table.
4085 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4087 /* Add application to the notify socket hash table. */
4088 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4089 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4091 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4092 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
4093 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
4100 * Set the application version into the object.
4102 * Return 0 on success else a negative value either an errno code or a
4103 * LTTng-UST error code.
4105 int ust_app_version(struct ust_app
*app
)
4111 pthread_mutex_lock(&app
->sock_lock
);
4112 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4113 pthread_mutex_unlock(&app
->sock_lock
);
4115 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4116 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4117 app
->pid
, app
->sock
);
4118 } else if (ret
== -EAGAIN
) {
4119 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4120 app
->pid
, app
->sock
);
4122 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4123 ret
, app
->pid
, app
->sock
);
4130 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4132 return app
->v_major
>= 9;
4135 bool ust_app_supports_counters(const struct ust_app
*app
)
4137 return app
->v_major
>= 9;
4141 * Setup the base event notifier group.
4143 * Return 0 on success else a negative value either an errno code or a
4144 * LTTng-UST error code.
4146 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4149 int event_pipe_write_fd
;
4150 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4151 enum lttng_error_code lttng_ret
;
4152 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4156 if (!ust_app_supports_notifiers(app
)) {
4161 /* Get the write side of the pipe. */
4162 event_pipe_write_fd
= lttng_pipe_get_writefd(
4163 app
->event_notifier_group
.event_pipe
);
4165 pthread_mutex_lock(&app
->sock_lock
);
4166 ret
= lttng_ust_ctl_create_event_notifier_group(app
->sock
,
4167 event_pipe_write_fd
, &event_notifier_group
);
4168 pthread_mutex_unlock(&app
->sock_lock
);
4170 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4172 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4173 app
->pid
, app
->sock
);
4174 } else if (ret
== -EAGAIN
) {
4176 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4177 app
->pid
, app
->sock
);
4179 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4180 ret
, app
->pid
, app
->sock
, event_pipe_write_fd
);
4185 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4187 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4188 app
->name
, app
->pid
);
4193 * Release the file descriptor that was reserved for the write-end of
4196 lttng_fd_put(LTTNG_FD_APPS
, 1);
4198 lttng_ret
= notification_thread_command_add_tracer_event_source(
4199 the_notification_thread_handle
,
4200 lttng_pipe_get_readfd(
4201 app
->event_notifier_group
.event_pipe
),
4203 if (lttng_ret
!= LTTNG_OK
) {
4204 ERR("Failed to add tracer event source to notification thread");
4209 /* Assign handle only when the complete setup is valid. */
4210 app
->event_notifier_group
.object
= event_notifier_group
;
4212 event_notifier_error_accounting_status
=
4213 event_notifier_error_accounting_register_app(app
);
4214 switch (event_notifier_error_accounting_status
) {
4215 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4217 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4218 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",
4219 app
->sock
, app
->name
, (int) app
->pid
);
4221 goto error_accounting
;
4222 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4223 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4224 app
->sock
, app
->name
, (int) app
->pid
);
4226 goto error_accounting
;
4228 ERR("Failed to setup event notifier error accounting for app");
4230 goto error_accounting
;
4236 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4237 the_notification_thread_handle
,
4238 lttng_pipe_get_readfd(
4239 app
->event_notifier_group
.event_pipe
));
4240 if (lttng_ret
!= LTTNG_OK
) {
4241 ERR("Failed to remove application tracer event source from notification thread");
4245 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4246 free(app
->event_notifier_group
.object
);
4247 app
->event_notifier_group
.object
= NULL
;
4252 * Unregister app by removing it from the global traceable app list and freeing
4255 * The socket is already closed at this point so no close to sock.
4257 void ust_app_unregister(int sock
)
4259 struct ust_app
*lta
;
4260 struct lttng_ht_node_ulong
*node
;
4261 struct lttng_ht_iter ust_app_sock_iter
;
4262 struct lttng_ht_iter iter
;
4263 struct ust_app_session
*ua_sess
;
4268 /* Get the node reference for a call_rcu */
4269 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4270 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4273 lta
= lttng::utils::container_of(node
, &ust_app::sock_n
);
4274 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4277 * For per-PID buffers, perform "push metadata" and flush all
4278 * application streams before removing app from hash tables,
4279 * ensuring proper behavior of data_pending check.
4280 * Remove sessions so they are not visible during deletion.
4282 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4284 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4286 /* The session was already removed so scheduled for teardown. */
4290 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4291 (void) ust_app_flush_app_session(lta
, ua_sess
);
4295 * Add session to list for teardown. This is safe since at this point we
4296 * are the only one using this list.
4298 pthread_mutex_lock(&ua_sess
->lock
);
4300 if (ua_sess
->deleted
) {
4301 pthread_mutex_unlock(&ua_sess
->lock
);
4306 * Normally, this is done in the delete session process which is
4307 * executed in the call rcu below. However, upon registration we can't
4308 * afford to wait for the grace period before pushing data or else the
4309 * data pending feature can race between the unregistration and stop
4310 * command where the data pending command is sent *before* the grace
4313 * The close metadata below nullifies the metadata pointer in the
4314 * session so the delete session will NOT push/close a second time.
4316 auto locked_registry
= get_locked_session_registry(ua_sess
);
4317 if (locked_registry
) {
4318 /* Push metadata for application before freeing the application. */
4319 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4322 * Don't ask to close metadata for global per UID buffers. Close
4323 * metadata only on destroy trace session in this case. Also, the
4324 * previous push metadata could have flag the metadata registry to
4325 * close so don't send a close command if closed.
4327 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4328 const auto metadata_key
= locked_registry
->_metadata_key
;
4329 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4331 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4332 locked_registry
->_metadata_closed
= true;
4335 /* Release lock before communication, see comments in close_metadata(). */
4336 locked_registry
.reset();
4337 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4339 locked_registry
.reset();
4342 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4344 pthread_mutex_unlock(&ua_sess
->lock
);
4347 /* Remove application from PID hash table */
4348 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4352 * Remove application from notify hash table. The thread handling the
4353 * notify socket could have deleted the node so ignore on error because
4354 * either way it's valid. The close of that socket is handled by the
4355 * apps_notify_thread.
4357 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4358 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4361 * Ignore return value since the node might have been removed before by an
4362 * add replace during app registration because the PID can be reassigned by
4365 iter
.iter
.node
= <a
->pid_n
.node
;
4366 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4368 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4373 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4380 * Fill events array with all events name of all registered apps.
4382 int ust_app_list_events(struct lttng_event
**events
)
4385 size_t nbmem
, count
= 0;
4386 struct lttng_ht_iter iter
;
4387 struct ust_app
*app
;
4388 struct lttng_event
*tmp_event
;
4390 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4391 tmp_event
= calloc
<lttng_event
>(nbmem
);
4392 if (tmp_event
== NULL
) {
4393 PERROR("zmalloc ust app events");
4400 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4401 struct lttng_ust_abi_tracepoint_iter uiter
;
4403 health_code_update();
4405 if (!app
->compatible
) {
4407 * TODO: In time, we should notice the caller of this error by
4408 * telling him that this is a version error.
4412 pthread_mutex_lock(&app
->sock_lock
);
4413 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4415 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4416 ERR("UST app list events getting handle failed for app pid %d",
4419 pthread_mutex_unlock(&app
->sock_lock
);
4423 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
,
4424 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4425 /* Handle ustctl error. */
4429 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4430 ERR("UST app tp list get failed for app %d with ret %d",
4433 DBG3("UST app tp list get failed. Application is dead");
4437 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4438 if (release_ret
< 0 &&
4439 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4440 release_ret
!= -EPIPE
) {
4441 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4443 pthread_mutex_unlock(&app
->sock_lock
);
4447 health_code_update();
4448 if (count
>= nbmem
) {
4449 /* In case the realloc fails, we free the memory */
4450 struct lttng_event
*new_tmp_event
;
4453 new_nbmem
= nbmem
<< 1;
4454 DBG2("Reallocating event list from %zu to %zu entries",
4456 new_tmp_event
= (lttng_event
*) realloc(tmp_event
,
4457 new_nbmem
* sizeof(struct lttng_event
));
4458 if (new_tmp_event
== NULL
) {
4461 PERROR("realloc ust app events");
4464 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4465 if (release_ret
< 0 &&
4466 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4467 release_ret
!= -EPIPE
) {
4468 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4470 pthread_mutex_unlock(&app
->sock_lock
);
4473 /* Zero the new memory */
4474 memset(new_tmp_event
+ nbmem
, 0,
4475 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4477 tmp_event
= new_tmp_event
;
4479 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4480 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4481 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4482 tmp_event
[count
].pid
= app
->pid
;
4483 tmp_event
[count
].enabled
= -1;
4486 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4487 pthread_mutex_unlock(&app
->sock_lock
);
4489 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4490 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4491 app
->pid
, app
->sock
);
4492 } else if (ret
== -EAGAIN
) {
4493 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4494 app
->pid
, app
->sock
);
4496 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4497 ret
, app
->pid
, app
->sock
);
4503 *events
= tmp_event
;
4505 DBG2("UST app list events done (%zu events)", count
);
4510 health_code_update();
4515 * Fill events array with all events name of all registered apps.
4517 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4520 size_t nbmem
, count
= 0;
4521 struct lttng_ht_iter iter
;
4522 struct ust_app
*app
;
4523 struct lttng_event_field
*tmp_event
;
4525 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4526 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4527 if (tmp_event
== NULL
) {
4528 PERROR("zmalloc ust app event fields");
4535 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4536 struct lttng_ust_abi_field_iter uiter
;
4538 health_code_update();
4540 if (!app
->compatible
) {
4542 * TODO: In time, we should notice the caller of this error by
4543 * telling him that this is a version error.
4547 pthread_mutex_lock(&app
->sock_lock
);
4548 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4550 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4551 ERR("UST app list field getting handle failed for app pid %d",
4554 pthread_mutex_unlock(&app
->sock_lock
);
4558 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
,
4559 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4560 /* Handle ustctl error. */
4564 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4565 ERR("UST app tp list field failed for app %d with ret %d",
4568 DBG3("UST app tp list field failed. Application is dead");
4572 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4573 pthread_mutex_unlock(&app
->sock_lock
);
4574 if (release_ret
< 0 &&
4575 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4576 release_ret
!= -EPIPE
) {
4577 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4582 health_code_update();
4583 if (count
>= nbmem
) {
4584 /* In case the realloc fails, we free the memory */
4585 struct lttng_event_field
*new_tmp_event
;
4588 new_nbmem
= nbmem
<< 1;
4589 DBG2("Reallocating event field list from %zu to %zu entries",
4591 new_tmp_event
= (lttng_event_field
*) realloc(tmp_event
,
4592 new_nbmem
* sizeof(struct lttng_event_field
));
4593 if (new_tmp_event
== NULL
) {
4596 PERROR("realloc ust app event fields");
4599 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4600 pthread_mutex_unlock(&app
->sock_lock
);
4602 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4603 release_ret
!= -EPIPE
) {
4604 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4608 /* Zero the new memory */
4609 memset(new_tmp_event
+ nbmem
, 0,
4610 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4612 tmp_event
= new_tmp_event
;
4615 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4616 /* Mapping between these enums matches 1 to 1. */
4617 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4618 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4620 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4621 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4622 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4623 tmp_event
[count
].event
.pid
= app
->pid
;
4624 tmp_event
[count
].event
.enabled
= -1;
4627 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4628 pthread_mutex_unlock(&app
->sock_lock
);
4630 ret
!= -LTTNG_UST_ERR_EXITING
&&
4632 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4637 *fields
= tmp_event
;
4639 DBG2("UST app list event fields done (%zu events)", count
);
4644 health_code_update();
4649 * Free and clean all traceable apps of the global list.
4651 void ust_app_clean_list(void)
4654 struct ust_app
*app
;
4655 struct lttng_ht_iter iter
;
4657 DBG2("UST app cleaning registered apps hash table");
4661 /* Cleanup notify socket hash table */
4662 if (ust_app_ht_by_notify_sock
) {
4663 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4664 notify_sock_n
.node
) {
4666 * Assert that all notifiers are gone as all triggers
4667 * are unregistered prior to this clean-up.
4669 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4671 ust_app_notify_sock_unregister(app
->notify_sock
);
4676 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4677 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4679 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4683 /* Cleanup socket hash table */
4684 if (ust_app_ht_by_sock
) {
4685 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4687 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4694 /* Destroy is done only when the ht is empty */
4696 lttng_ht_destroy(ust_app_ht
);
4698 if (ust_app_ht_by_sock
) {
4699 lttng_ht_destroy(ust_app_ht_by_sock
);
4701 if (ust_app_ht_by_notify_sock
) {
4702 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4707 * Init UST app hash table.
4709 int ust_app_ht_alloc(void)
4711 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4715 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4716 if (!ust_app_ht_by_sock
) {
4719 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4720 if (!ust_app_ht_by_notify_sock
) {
4727 * For a specific UST session, disable the channel for all registered apps.
4729 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4730 struct ltt_ust_channel
*uchan
)
4733 struct lttng_ht_iter iter
;
4734 struct lttng_ht_node_str
*ua_chan_node
;
4735 struct ust_app
*app
;
4736 struct ust_app_session
*ua_sess
;
4737 struct ust_app_channel
*ua_chan
;
4739 LTTNG_ASSERT(usess
->active
);
4740 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4741 uchan
->name
, usess
->id
);
4745 /* For every registered applications */
4746 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4747 struct lttng_ht_iter uiter
;
4748 if (!app
->compatible
) {
4750 * TODO: In time, we should notice the caller of this error by
4751 * telling him that this is a version error.
4755 ua_sess
= lookup_session_by_app(usess
, app
);
4756 if (ua_sess
== NULL
) {
4761 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4762 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4763 /* If the session if found for the app, the channel must be there */
4764 LTTNG_ASSERT(ua_chan_node
);
4766 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4767 /* The channel must not be already disabled */
4768 LTTNG_ASSERT(ua_chan
->enabled
== 1);
4770 /* Disable channel onto application */
4771 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4773 /* XXX: We might want to report this error at some point... */
4783 * For a specific UST session, enable the channel for all registered apps.
4785 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4786 struct ltt_ust_channel
*uchan
)
4789 struct lttng_ht_iter iter
;
4790 struct ust_app
*app
;
4791 struct ust_app_session
*ua_sess
;
4793 LTTNG_ASSERT(usess
->active
);
4794 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4795 uchan
->name
, usess
->id
);
4799 /* For every registered applications */
4800 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4801 if (!app
->compatible
) {
4803 * TODO: In time, we should notice the caller of this error by
4804 * telling him that this is a version error.
4808 ua_sess
= lookup_session_by_app(usess
, app
);
4809 if (ua_sess
== NULL
) {
4813 /* Enable channel onto application */
4814 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4816 /* XXX: We might want to report this error at some point... */
4826 * Disable an event in a channel and for a specific session.
4828 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4829 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4832 struct lttng_ht_iter iter
, uiter
;
4833 struct lttng_ht_node_str
*ua_chan_node
;
4834 struct ust_app
*app
;
4835 struct ust_app_session
*ua_sess
;
4836 struct ust_app_channel
*ua_chan
;
4837 struct ust_app_event
*ua_event
;
4839 LTTNG_ASSERT(usess
->active
);
4840 DBG("UST app disabling event %s for all apps in channel "
4841 "%s for session id %" PRIu64
,
4842 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4846 /* For all registered applications */
4847 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4848 if (!app
->compatible
) {
4850 * TODO: In time, we should notice the caller of this error by
4851 * telling him that this is a version error.
4855 ua_sess
= lookup_session_by_app(usess
, app
);
4856 if (ua_sess
== NULL
) {
4861 /* Lookup channel in the ust app session */
4862 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4863 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4864 if (ua_chan_node
== NULL
) {
4865 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4866 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4869 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4871 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4872 uevent
->filter
, uevent
->attr
.loglevel
,
4874 if (ua_event
== NULL
) {
4875 DBG2("Event %s not found in channel %s for app pid %d."
4876 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4880 ret
= disable_ust_app_event(ua_event
, app
);
4882 /* XXX: Report error someday... */
4891 /* The ua_sess lock must be held by the caller. */
4893 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4894 struct ust_app_session
*ua_sess
,
4895 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4896 struct ust_app_channel
**_ua_chan
)
4899 struct ust_app_channel
*ua_chan
= NULL
;
4901 LTTNG_ASSERT(ua_sess
);
4902 ASSERT_LOCKED(ua_sess
->lock
);
4904 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4905 sizeof(uchan
->name
))) {
4906 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4910 struct ltt_ust_context
*uctx
= NULL
;
4913 * Create channel onto application and synchronize its
4916 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4917 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4923 ret
= ust_app_channel_send(app
, usess
,
4930 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4931 ret
= create_ust_app_channel_context(ua_chan
,
4944 * The application's socket is not valid. Either a bad socket
4945 * or a timeout on it. We can't inform the caller that for a
4946 * specific app, the session failed so lets continue here.
4948 ret
= 0; /* Not an error. */
4956 if (ret
== 0 && _ua_chan
) {
4958 * Only return the application's channel on success. Note
4959 * that the channel can still be part of the application's
4960 * channel hashtable on error.
4962 *_ua_chan
= ua_chan
;
4968 * Enable event for a specific session and channel on the tracer.
4970 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4971 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4974 struct lttng_ht_iter iter
, uiter
;
4975 struct lttng_ht_node_str
*ua_chan_node
;
4976 struct ust_app
*app
;
4977 struct ust_app_session
*ua_sess
;
4978 struct ust_app_channel
*ua_chan
;
4979 struct ust_app_event
*ua_event
;
4981 LTTNG_ASSERT(usess
->active
);
4982 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4983 uevent
->attr
.name
, usess
->id
);
4986 * NOTE: At this point, this function is called only if the session and
4987 * channel passed are already created for all apps. and enabled on the
4993 /* For all registered applications */
4994 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4995 if (!app
->compatible
) {
4997 * TODO: In time, we should notice the caller of this error by
4998 * telling him that this is a version error.
5002 ua_sess
= lookup_session_by_app(usess
, app
);
5004 /* The application has problem or is probably dead. */
5008 pthread_mutex_lock(&ua_sess
->lock
);
5010 if (ua_sess
->deleted
) {
5011 pthread_mutex_unlock(&ua_sess
->lock
);
5015 /* Lookup channel in the ust app session */
5016 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5017 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5019 * It is possible that the channel cannot be found is
5020 * the channel/event creation occurs concurrently with
5021 * an application exit.
5023 if (!ua_chan_node
) {
5024 pthread_mutex_unlock(&ua_sess
->lock
);
5028 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5030 /* Get event node */
5031 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5032 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5033 if (ua_event
== NULL
) {
5034 DBG3("UST app enable event %s not found for app PID %d."
5035 "Skipping app", uevent
->attr
.name
, app
->pid
);
5039 ret
= enable_ust_app_event(ua_event
, app
);
5041 pthread_mutex_unlock(&ua_sess
->lock
);
5045 pthread_mutex_unlock(&ua_sess
->lock
);
5054 * For a specific existing UST session and UST channel, creates the event for
5055 * all registered apps.
5057 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5058 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
5061 struct lttng_ht_iter iter
, uiter
;
5062 struct lttng_ht_node_str
*ua_chan_node
;
5063 struct ust_app
*app
;
5064 struct ust_app_session
*ua_sess
;
5065 struct ust_app_channel
*ua_chan
;
5067 LTTNG_ASSERT(usess
->active
);
5068 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5069 uevent
->attr
.name
, usess
->id
);
5073 /* For all registered applications */
5074 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5075 if (!app
->compatible
) {
5077 * TODO: In time, we should notice the caller of this error by
5078 * telling him that this is a version error.
5082 ua_sess
= lookup_session_by_app(usess
, app
);
5084 /* The application has problem or is probably dead. */
5088 pthread_mutex_lock(&ua_sess
->lock
);
5090 if (ua_sess
->deleted
) {
5091 pthread_mutex_unlock(&ua_sess
->lock
);
5095 /* Lookup channel in the ust app session */
5096 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5097 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5098 /* If the channel is not found, there is a code flow error */
5099 LTTNG_ASSERT(ua_chan_node
);
5101 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5103 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5104 pthread_mutex_unlock(&ua_sess
->lock
);
5106 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5107 /* Possible value at this point: -ENOMEM. If so, we stop! */
5110 DBG2("UST app event %s already exist on app PID %d",
5111 uevent
->attr
.name
, app
->pid
);
5121 * Start tracing for a specific UST session and app.
5123 * Called with UST app session lock held.
5127 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5130 struct ust_app_session
*ua_sess
;
5132 DBG("Starting tracing for ust app pid %d", app
->pid
);
5136 if (!app
->compatible
) {
5140 ua_sess
= lookup_session_by_app(usess
, app
);
5141 if (ua_sess
== NULL
) {
5142 /* The session is in teardown process. Ignore and continue. */
5146 pthread_mutex_lock(&ua_sess
->lock
);
5148 if (ua_sess
->deleted
) {
5149 pthread_mutex_unlock(&ua_sess
->lock
);
5153 if (ua_sess
->enabled
) {
5154 pthread_mutex_unlock(&ua_sess
->lock
);
5158 /* Upon restart, we skip the setup, already done */
5159 if (ua_sess
->started
) {
5163 health_code_update();
5166 /* This starts the UST tracing */
5167 pthread_mutex_lock(&app
->sock_lock
);
5168 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5169 pthread_mutex_unlock(&app
->sock_lock
);
5171 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5172 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5173 app
->pid
, app
->sock
);
5174 pthread_mutex_unlock(&ua_sess
->lock
);
5176 } else if (ret
== -EAGAIN
) {
5177 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5178 app
->pid
, app
->sock
);
5179 pthread_mutex_unlock(&ua_sess
->lock
);
5183 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5184 ret
, app
->pid
, app
->sock
);
5189 /* Indicate that the session has been started once */
5190 ua_sess
->started
= 1;
5191 ua_sess
->enabled
= 1;
5193 pthread_mutex_unlock(&ua_sess
->lock
);
5195 health_code_update();
5197 /* Quiescent wait after starting trace */
5198 pthread_mutex_lock(&app
->sock_lock
);
5199 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5200 pthread_mutex_unlock(&app
->sock_lock
);
5202 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5203 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5204 app
->pid
, app
->sock
);
5205 } else if (ret
== -EAGAIN
) {
5206 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5207 app
->pid
, app
->sock
);
5209 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5210 ret
, app
->pid
, app
->sock
);
5216 health_code_update();
5220 pthread_mutex_unlock(&ua_sess
->lock
);
5222 health_code_update();
5227 * Stop tracing for a specific UST session and app.
5230 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5233 struct ust_app_session
*ua_sess
;
5235 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5239 if (!app
->compatible
) {
5240 goto end_no_session
;
5243 ua_sess
= lookup_session_by_app(usess
, app
);
5244 if (ua_sess
== NULL
) {
5245 goto end_no_session
;
5248 pthread_mutex_lock(&ua_sess
->lock
);
5250 if (ua_sess
->deleted
) {
5251 pthread_mutex_unlock(&ua_sess
->lock
);
5252 goto end_no_session
;
5256 * If started = 0, it means that stop trace has been called for a session
5257 * that was never started. It's possible since we can have a fail start
5258 * from either the application manager thread or the command thread. Simply
5259 * indicate that this is a stop error.
5261 if (!ua_sess
->started
) {
5262 goto error_rcu_unlock
;
5265 health_code_update();
5267 /* This inhibits UST tracing */
5268 pthread_mutex_lock(&app
->sock_lock
);
5269 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5270 pthread_mutex_unlock(&app
->sock_lock
);
5272 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5273 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5274 app
->pid
, app
->sock
);
5276 } else if (ret
== -EAGAIN
) {
5277 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5278 app
->pid
, app
->sock
);
5282 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5283 ret
, app
->pid
, app
->sock
);
5285 goto error_rcu_unlock
;
5288 health_code_update();
5289 ua_sess
->enabled
= 0;
5291 /* Quiescent wait after stopping trace */
5292 pthread_mutex_lock(&app
->sock_lock
);
5293 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5294 pthread_mutex_unlock(&app
->sock_lock
);
5296 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5297 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5298 app
->pid
, app
->sock
);
5299 } else if (ret
== -EAGAIN
) {
5300 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5301 app
->pid
, app
->sock
);
5303 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5304 ret
, app
->pid
, app
->sock
);
5308 health_code_update();
5311 auto locked_registry
= get_locked_session_registry(ua_sess
);
5313 /* The UST app session is held registry shall not be null. */
5314 LTTNG_ASSERT(locked_registry
);
5316 /* Push metadata for application before freeing the application. */
5317 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5321 pthread_mutex_unlock(&ua_sess
->lock
);
5324 health_code_update();
5328 pthread_mutex_unlock(&ua_sess
->lock
);
5330 health_code_update();
5335 int ust_app_flush_app_session(struct ust_app
*app
,
5336 struct ust_app_session
*ua_sess
)
5338 int ret
, retval
= 0;
5339 struct lttng_ht_iter iter
;
5340 struct ust_app_channel
*ua_chan
;
5341 struct consumer_socket
*socket
;
5343 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5347 if (!app
->compatible
) {
5348 goto end_not_compatible
;
5351 pthread_mutex_lock(&ua_sess
->lock
);
5353 if (ua_sess
->deleted
) {
5357 health_code_update();
5359 /* Flushing buffers */
5360 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
5363 /* Flush buffers and push metadata. */
5364 switch (ua_sess
->buffer_type
) {
5365 case LTTNG_BUFFER_PER_PID
:
5366 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5368 health_code_update();
5369 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5371 ERR("Error flushing consumer channel");
5377 case LTTNG_BUFFER_PER_UID
:
5383 health_code_update();
5386 pthread_mutex_unlock(&ua_sess
->lock
);
5390 health_code_update();
5395 * Flush buffers for all applications for a specific UST session.
5396 * Called with UST session lock held.
5399 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5404 DBG("Flushing session buffers for all ust apps");
5408 /* Flush buffers and push metadata. */
5409 switch (usess
->buffer_type
) {
5410 case LTTNG_BUFFER_PER_UID
:
5412 struct buffer_reg_uid
*reg
;
5413 struct lttng_ht_iter iter
;
5415 /* Flush all per UID buffers associated to that session. */
5416 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5417 lsu::registry_session
*ust_session_reg
;
5418 struct buffer_reg_channel
*buf_reg_chan
;
5419 struct consumer_socket
*socket
;
5421 /* Get consumer socket to use to push the metadata.*/
5422 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5425 /* Ignore request if no consumer is found for the session. */
5429 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5430 buf_reg_chan
, node
.node
) {
5432 * The following call will print error values so the return
5433 * code is of little importance because whatever happens, we
5434 * have to try them all.
5436 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5439 ust_session_reg
= reg
->registry
->reg
.ust
;
5440 /* Push metadata. */
5441 auto locked_registry
= ust_session_reg
->lock();
5442 (void) push_metadata(locked_registry
, usess
->consumer
);
5446 case LTTNG_BUFFER_PER_PID
:
5448 struct ust_app_session
*ua_sess
;
5449 struct lttng_ht_iter iter
;
5450 struct ust_app
*app
;
5452 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5453 ua_sess
= lookup_session_by_app(usess
, app
);
5454 if (ua_sess
== NULL
) {
5457 (void) ust_app_flush_app_session(app
, ua_sess
);
5468 health_code_update();
5473 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5474 struct ust_app_session
*ua_sess
)
5477 struct lttng_ht_iter iter
;
5478 struct ust_app_channel
*ua_chan
;
5479 struct consumer_socket
*socket
;
5481 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5485 if (!app
->compatible
) {
5486 goto end_not_compatible
;
5489 pthread_mutex_lock(&ua_sess
->lock
);
5491 if (ua_sess
->deleted
) {
5495 health_code_update();
5497 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
5500 ERR("Failed to find consumer (%" PRIu32
") socket",
5501 app
->abi
.bits_per_long
);
5506 /* Clear quiescent state. */
5507 switch (ua_sess
->buffer_type
) {
5508 case LTTNG_BUFFER_PER_PID
:
5509 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5510 ua_chan
, node
.node
) {
5511 health_code_update();
5512 ret
= consumer_clear_quiescent_channel(socket
,
5515 ERR("Error clearing quiescent state for consumer channel");
5521 case LTTNG_BUFFER_PER_UID
:
5528 health_code_update();
5531 pthread_mutex_unlock(&ua_sess
->lock
);
5535 health_code_update();
5540 * Clear quiescent state in each stream for all applications for a
5541 * specific UST session.
5542 * Called with UST session lock held.
5545 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5550 DBG("Clearing stream quiescent state for all ust apps");
5554 switch (usess
->buffer_type
) {
5555 case LTTNG_BUFFER_PER_UID
:
5557 struct lttng_ht_iter iter
;
5558 struct buffer_reg_uid
*reg
;
5561 * Clear quiescent for all per UID buffers associated to
5564 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5565 struct consumer_socket
*socket
;
5566 struct buffer_reg_channel
*buf_reg_chan
;
5568 /* Get associated consumer socket.*/
5569 socket
= consumer_find_socket_by_bitness(
5570 reg
->bits_per_long
, usess
->consumer
);
5573 * Ignore request if no consumer is found for
5579 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5580 &iter
.iter
, buf_reg_chan
, node
.node
) {
5582 * The following call will print error values so
5583 * the return code is of little importance
5584 * because whatever happens, we have to try them
5587 (void) consumer_clear_quiescent_channel(socket
,
5588 buf_reg_chan
->consumer_key
);
5593 case LTTNG_BUFFER_PER_PID
:
5595 struct ust_app_session
*ua_sess
;
5596 struct lttng_ht_iter iter
;
5597 struct ust_app
*app
;
5599 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5601 ua_sess
= lookup_session_by_app(usess
, app
);
5602 if (ua_sess
== NULL
) {
5605 (void) ust_app_clear_quiescent_app_session(app
,
5617 health_code_update();
5622 * Destroy a specific UST session in apps.
5624 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5627 struct ust_app_session
*ua_sess
;
5628 struct lttng_ht_iter iter
;
5629 struct lttng_ht_node_u64
*node
;
5631 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5635 if (!app
->compatible
) {
5639 __lookup_session_by_app(usess
, app
, &iter
);
5640 node
= lttng_ht_iter_get_node_u64(&iter
);
5642 /* Session is being or is deleted. */
5645 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5647 health_code_update();
5648 destroy_app_session(app
, ua_sess
);
5650 health_code_update();
5652 /* Quiescent wait after stopping trace */
5653 pthread_mutex_lock(&app
->sock_lock
);
5654 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5655 pthread_mutex_unlock(&app
->sock_lock
);
5657 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5658 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5659 app
->pid
, app
->sock
);
5660 } else if (ret
== -EAGAIN
) {
5661 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5662 app
->pid
, app
->sock
);
5664 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5665 ret
, app
->pid
, app
->sock
);
5670 health_code_update();
5675 * Start tracing for the UST session.
5677 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5679 struct lttng_ht_iter iter
;
5680 struct ust_app
*app
;
5682 DBG("Starting all UST traces");
5685 * Even though the start trace might fail, flag this session active so
5686 * other application coming in are started by default.
5693 * In a start-stop-start use-case, we need to clear the quiescent state
5694 * of each channel set by the prior stop command, thus ensuring that a
5695 * following stop or destroy is sure to grab a timestamp_end near those
5696 * operations, even if the packet is empty.
5698 (void) ust_app_clear_quiescent_session(usess
);
5700 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5701 ust_app_global_update(usess
, app
);
5710 * Start tracing for the UST session.
5711 * Called with UST session lock held.
5713 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5716 struct lttng_ht_iter iter
;
5717 struct ust_app
*app
;
5719 DBG("Stopping all UST traces");
5722 * Even though the stop trace might fail, flag this session inactive so
5723 * other application coming in are not started by default.
5729 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5730 ret
= ust_app_stop_trace(usess
, app
);
5732 /* Continue to next apps even on error */
5737 (void) ust_app_flush_session(usess
);
5745 * Destroy app UST session.
5747 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5750 struct lttng_ht_iter iter
;
5751 struct ust_app
*app
;
5753 DBG("Destroy all UST traces");
5757 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5758 ret
= destroy_trace(usess
, app
);
5760 /* Continue to next apps even on error */
5770 /* The ua_sess lock must be held by the caller. */
5772 int find_or_create_ust_app_channel(
5773 struct ltt_ust_session
*usess
,
5774 struct ust_app_session
*ua_sess
,
5775 struct ust_app
*app
,
5776 struct ltt_ust_channel
*uchan
,
5777 struct ust_app_channel
**ua_chan
)
5780 struct lttng_ht_iter iter
;
5781 struct lttng_ht_node_str
*ua_chan_node
;
5783 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5784 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5786 *ua_chan
= caa_container_of(ua_chan_node
,
5787 struct ust_app_channel
, node
);
5791 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5800 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5801 struct ltt_ust_event
*uevent
,
5802 struct ust_app
*app
)
5805 struct ust_app_event
*ua_event
= NULL
;
5807 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5808 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5810 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5815 if (ua_event
->enabled
!= uevent
->enabled
) {
5816 ret
= uevent
->enabled
?
5817 enable_ust_app_event(ua_event
, app
) :
5818 disable_ust_app_event(ua_event
, app
);
5826 /* Called with RCU read-side lock held. */
5828 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5831 enum lttng_error_code ret_code
;
5832 enum lttng_trigger_status t_status
;
5833 struct lttng_ht_iter app_trigger_iter
;
5834 struct lttng_triggers
*triggers
= NULL
;
5835 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5836 unsigned int count
, i
;
5838 ASSERT_RCU_READ_LOCKED();
5840 if (!ust_app_supports_notifiers(app
)) {
5845 * Currrently, registering or unregistering a trigger with an
5846 * event rule condition causes a full synchronization of the event
5849 * The first step attempts to add an event notifier for all registered
5850 * triggers that apply to the user space tracers. Then, the
5851 * application's event notifiers rules are all checked against the list
5852 * of registered triggers. Any event notifier that doesn't have a
5853 * matching trigger can be assumed to have been disabled.
5855 * All of this is inefficient, but is put in place to get the feature
5856 * rolling as it is simpler at this moment. It will be optimized Soon™
5857 * to allow the state of enabled
5858 * event notifiers to be synchronized in a piece-wise way.
5861 /* Get all triggers using uid 0 (root) */
5862 ret_code
= notification_thread_command_list_triggers(
5863 the_notification_thread_handle
, 0, &triggers
);
5864 if (ret_code
!= LTTNG_OK
) {
5868 LTTNG_ASSERT(triggers
);
5870 t_status
= lttng_triggers_get_count(triggers
, &count
);
5871 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5875 for (i
= 0; i
< count
; i
++) {
5876 struct lttng_condition
*condition
;
5877 struct lttng_event_rule
*event_rule
;
5878 struct lttng_trigger
*trigger
;
5879 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5880 enum lttng_condition_status condition_status
;
5883 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5884 LTTNG_ASSERT(trigger
);
5886 token
= lttng_trigger_get_tracer_token(trigger
);
5887 condition
= lttng_trigger_get_condition(trigger
);
5889 if (lttng_condition_get_type(condition
) !=
5890 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5891 /* Does not apply */
5896 lttng_condition_event_rule_matches_borrow_rule_mutable(
5897 condition
, &event_rule
);
5898 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5900 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5901 /* Skip kernel related triggers. */
5906 * Find or create the associated token event rule. The caller
5907 * holds the RCU read lock, so this is safe to call without
5908 * explicitly acquiring it here.
5910 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5911 app
->token_to_event_notifier_rule_ht
, token
);
5912 if (!looked_up_event_notifier_rule
) {
5913 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5921 /* Remove all unknown event sources from the app. */
5922 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5923 &app_trigger_iter
.iter
, event_notifier_rule
,
5925 const uint64_t app_token
= event_notifier_rule
->token
;
5929 * Check if the app event trigger still exists on the
5930 * notification side.
5932 for (i
= 0; i
< count
; i
++) {
5933 uint64_t notification_thread_token
;
5934 const struct lttng_trigger
*trigger
=
5935 lttng_triggers_get_at_index(
5938 LTTNG_ASSERT(trigger
);
5940 notification_thread_token
=
5941 lttng_trigger_get_tracer_token(trigger
);
5943 if (notification_thread_token
== app_token
) {
5955 * This trigger was unregistered, disable it on the tracer's
5958 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5960 LTTNG_ASSERT(ret
== 0);
5962 /* Callee logs errors. */
5963 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5965 delete_ust_app_event_notifier_rule(
5966 app
->sock
, event_notifier_rule
, app
);
5972 lttng_triggers_destroy(triggers
);
5977 * RCU read lock must be held by the caller.
5980 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5981 struct ust_app_session
*ua_sess
,
5982 struct ust_app
*app
)
5985 struct cds_lfht_iter uchan_iter
;
5986 struct ltt_ust_channel
*uchan
;
5988 LTTNG_ASSERT(usess
);
5989 LTTNG_ASSERT(ua_sess
);
5991 ASSERT_RCU_READ_LOCKED();
5993 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5995 struct ust_app_channel
*ua_chan
;
5996 struct cds_lfht_iter uevent_iter
;
5997 struct ltt_ust_event
*uevent
;
6000 * Search for a matching ust_app_channel. If none is found,
6001 * create it. Creating the channel will cause the ua_chan
6002 * structure to be allocated, the channel buffers to be
6003 * allocated (if necessary) and sent to the application, and
6004 * all enabled contexts will be added to the channel.
6006 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
6007 app
, uchan
, &ua_chan
);
6009 /* Tracer is probably gone or ENOMEM. */
6014 /* ua_chan will be NULL for the metadata channel */
6018 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
6020 ret
= ust_app_channel_synchronize_event(ua_chan
,
6027 if (ua_chan
->enabled
!= uchan
->enabled
) {
6028 ret
= uchan
->enabled
?
6029 enable_ust_app_channel(ua_sess
, uchan
, app
) :
6030 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6041 * The caller must ensure that the application is compatible and is tracked
6042 * by the process attribute trackers.
6045 void ust_app_synchronize(struct ltt_ust_session
*usess
,
6046 struct ust_app
*app
)
6049 struct ust_app_session
*ua_sess
= NULL
;
6052 * The application's configuration should only be synchronized for
6055 LTTNG_ASSERT(usess
->active
);
6057 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
6059 /* Tracer is probably gone or ENOMEM. */
6061 destroy_app_session(app
, ua_sess
);
6065 LTTNG_ASSERT(ua_sess
);
6067 pthread_mutex_lock(&ua_sess
->lock
);
6068 if (ua_sess
->deleted
) {
6069 goto deleted_session
;
6074 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6077 * Create the metadata for the application. This returns gracefully if a
6078 * metadata was already set for the session.
6080 * The metadata channel must be created after the data channels as the
6081 * consumer daemon assumes this ordering. When interacting with a relay
6082 * daemon, the consumer will use this assumption to send the
6083 * "STREAMS_SENT" message to the relay daemon.
6085 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6087 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6088 app
->sock
, usess
->id
);
6094 pthread_mutex_unlock(&ua_sess
->lock
);
6100 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6102 struct ust_app_session
*ua_sess
;
6104 ua_sess
= lookup_session_by_app(usess
, app
);
6105 if (ua_sess
== NULL
) {
6108 destroy_app_session(app
, ua_sess
);
6112 * Add channels/events from UST global domain to registered apps at sock.
6114 * Called with session lock held.
6115 * Called with RCU read-side lock held.
6117 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6119 LTTNG_ASSERT(usess
);
6120 LTTNG_ASSERT(usess
->active
);
6121 ASSERT_RCU_READ_LOCKED();
6123 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
6124 app
->sock
, usess
->id
);
6126 if (!app
->compatible
) {
6129 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
6131 trace_ust_id_tracker_lookup(
6132 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
6134 trace_ust_id_tracker_lookup(
6135 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
6138 * Synchronize the application's internal tracing configuration
6139 * and start tracing.
6141 ust_app_synchronize(usess
, app
);
6142 ust_app_start_trace(usess
, app
);
6144 ust_app_global_destroy(usess
, app
);
6149 * Add all event notifiers to an application.
6151 * Called with session lock held.
6152 * Called with RCU read-side lock held.
6154 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6156 ASSERT_RCU_READ_LOCKED();
6158 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6159 app
->name
, app
->pid
);
6161 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6165 if (app
->event_notifier_group
.object
== NULL
) {
6166 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6167 app
->name
, app
->pid
);
6171 ust_app_synchronize_event_notifier_rules(app
);
6175 * Called with session lock held.
6177 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6179 struct lttng_ht_iter iter
;
6180 struct ust_app
*app
;
6183 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6184 ust_app_global_update(usess
, app
);
6189 void ust_app_global_update_all_event_notifier_rules(void)
6191 struct lttng_ht_iter iter
;
6192 struct ust_app
*app
;
6195 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6196 ust_app_global_update_event_notifier_rules(app
);
6203 * Add context to a specific channel for global UST domain.
6205 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6206 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6209 struct lttng_ht_node_str
*ua_chan_node
;
6210 struct lttng_ht_iter iter
, uiter
;
6211 struct ust_app_channel
*ua_chan
= NULL
;
6212 struct ust_app_session
*ua_sess
;
6213 struct ust_app
*app
;
6215 LTTNG_ASSERT(usess
->active
);
6218 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6219 if (!app
->compatible
) {
6221 * TODO: In time, we should notice the caller of this error by
6222 * telling him that this is a version error.
6226 ua_sess
= lookup_session_by_app(usess
, app
);
6227 if (ua_sess
== NULL
) {
6231 pthread_mutex_lock(&ua_sess
->lock
);
6233 if (ua_sess
->deleted
) {
6234 pthread_mutex_unlock(&ua_sess
->lock
);
6238 /* Lookup channel in the ust app session */
6239 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6240 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6241 if (ua_chan_node
== NULL
) {
6244 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6246 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6251 pthread_mutex_unlock(&ua_sess
->lock
);
6259 * Receive registration and populate the given msg structure.
6261 * On success return 0 else a negative value returned by the ustctl call.
6263 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6266 uint32_t pid
, ppid
, uid
, gid
;
6270 ret
= lttng_ust_ctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6271 &pid
, &ppid
, &uid
, &gid
,
6272 &msg
->bits_per_long
,
6273 &msg
->uint8_t_alignment
,
6274 &msg
->uint16_t_alignment
,
6275 &msg
->uint32_t_alignment
,
6276 &msg
->uint64_t_alignment
,
6277 &msg
->long_alignment
,
6284 case LTTNG_UST_ERR_EXITING
:
6285 DBG3("UST app recv reg message failed. Application died");
6287 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6288 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6289 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6290 LTTNG_UST_ABI_MINOR_VERSION
);
6293 ERR("UST app recv reg message failed with ret %d", ret
);
6298 msg
->pid
= (pid_t
) pid
;
6299 msg
->ppid
= (pid_t
) ppid
;
6300 msg
->uid
= (uid_t
) uid
;
6301 msg
->gid
= (gid_t
) gid
;
6308 * Return a ust app session object using the application object and the
6309 * session object descriptor has a key. If not found, NULL is returned.
6310 * A RCU read side lock MUST be acquired when calling this function.
6312 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6315 struct lttng_ht_node_ulong
*node
;
6316 struct lttng_ht_iter iter
;
6317 struct ust_app_session
*ua_sess
= NULL
;
6320 ASSERT_RCU_READ_LOCKED();
6322 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6323 node
= lttng_ht_iter_get_node_ulong(&iter
);
6325 DBG2("UST app session find by objd %d not found", objd
);
6329 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6336 * Return a ust app channel object using the application object and the channel
6337 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6338 * lock MUST be acquired before calling this function.
6340 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6343 struct lttng_ht_node_ulong
*node
;
6344 struct lttng_ht_iter iter
;
6345 struct ust_app_channel
*ua_chan
= NULL
;
6348 ASSERT_RCU_READ_LOCKED();
6350 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6351 node
= lttng_ht_iter_get_node_ulong(&iter
);
6353 DBG2("UST app channel find by objd %d not found", objd
);
6357 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6364 * Reply to a register channel notification from an application on the notify
6365 * socket. The channel metadata is also created.
6367 * The session UST registry lock is acquired in this function.
6369 * On success 0 is returned else a negative value.
6371 static int handle_app_register_channel_notification(int sock
,
6373 struct lttng_ust_ctl_field
*raw_context_fields
,
6374 size_t context_field_count
)
6376 int ret
, ret_code
= 0;
6378 uint64_t chan_reg_key
;
6379 struct ust_app
*app
;
6380 struct ust_app_channel
*ua_chan
;
6381 struct ust_app_session
*ua_sess
;
6382 auto ust_ctl_context_fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(
6383 raw_context_fields
);
6385 lttng::urcu::read_lock_guard read_lock_guard
;
6387 /* Lookup application. If not found, there is a code flow error. */
6388 app
= find_app_by_notify_sock(sock
);
6390 DBG("Application socket %d is being torn down. Abort event notify",
6395 /* Lookup channel by UST object descriptor. */
6396 ua_chan
= find_channel_by_objd(app
, cobjd
);
6398 DBG("Application channel is being torn down. Abort event notify");
6402 LTTNG_ASSERT(ua_chan
->session
);
6403 ua_sess
= ua_chan
->session
;
6405 /* Get right session registry depending on the session buffer type. */
6406 auto locked_registry_session
= get_locked_session_registry(ua_sess
);
6407 if (!locked_registry_session
) {
6408 DBG("Application session is being torn down. Abort event notify");
6412 /* Depending on the buffer type, a different channel key is used. */
6413 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6414 chan_reg_key
= ua_chan
->tracing_channel_id
;
6416 chan_reg_key
= ua_chan
->key
;
6419 auto& ust_reg_chan
= locked_registry_session
->get_channel(chan_reg_key
);
6421 /* Channel id is set during the object creation. */
6422 chan_id
= ust_reg_chan
.id
;
6425 * The application returns the typing information of the channel's
6426 * context fields. In per-PID buffering mode, this is the first and only
6427 * time we get this information. It is our chance to finalize the
6428 * initialiation of the channel and serialize it's layout's description
6429 * to the trace's metadata.
6431 * However, in per-UID buffering mode, every application will provide
6432 * this information (redundantly). The first time will allow us to
6433 * complete the initialization. The following times, we simply validate
6434 * that all apps provide the same typing for the context fields as a
6437 lst::type::cuptr context_fields
= lttng::make_unique
<lst::structure_type
>(0,
6438 lsu::create_trace_fields_from_ust_ctl_fields(*locked_registry_session
,
6439 ust_ctl_context_fields
.get(), context_field_count
));
6441 if (!ust_reg_chan
.is_registered()) {
6442 ust_reg_chan
.set_context(std::move(context_fields
));
6445 * Validate that the context fields match between
6446 * registry and newcoming application.
6448 if (ust_reg_chan
.get_context() != *context_fields
) {
6449 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6450 app
->pid
, app
->sock
);
6456 /* Append to metadata */
6457 if (!ust_reg_chan
._metadata_dumped
) {
6458 /*ret_code = ust_metadata_channel_statedump(registry, ust_reg_chan);*/
6460 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6466 DBG3("UST app replying to register channel key %" PRIu64
6467 " with id %u, ret = %d", chan_reg_key
, chan_id
,
6470 ret
= lttng_ust_ctl_reply_register_channel(sock
, chan_id
,
6471 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6472 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6473 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6476 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6477 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6478 app
->pid
, app
->sock
);
6479 } else if (ret
== -EAGAIN
) {
6480 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6481 app
->pid
, app
->sock
);
6483 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6484 ret
, app
->pid
, app
->sock
);
6490 /* This channel registry's registration is completed. */
6491 ust_reg_chan
.set_as_registered();
6497 * Add event to the UST channel registry. When the event is added to the
6498 * registry, the metadata is also created. Once done, this replies to the
6499 * application with the appropriate error code.
6501 * The session UST registry lock is acquired in the function.
6503 * On success 0 is returned else a negative value.
6505 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, const char *name
,
6506 char *raw_signature
, size_t nr_fields
, struct lttng_ust_ctl_field
*raw_fields
,
6507 int loglevel_value
, char *raw_model_emf_uri
)
6510 uint32_t event_id
= 0;
6511 uint64_t chan_reg_key
;
6512 struct ust_app
*app
;
6513 struct ust_app_channel
*ua_chan
;
6514 struct ust_app_session
*ua_sess
;
6515 lttng::urcu::read_lock_guard rcu_lock
;
6516 auto signature
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_signature
);
6517 auto fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_fields
);
6518 auto model_emf_uri
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_model_emf_uri
);
6520 /* Lookup application. If not found, there is a code flow error. */
6521 app
= find_app_by_notify_sock(sock
);
6523 DBG("Application socket %d is being torn down. Abort event notify",
6528 /* Lookup channel by UST object descriptor. */
6529 ua_chan
= find_channel_by_objd(app
, cobjd
);
6531 DBG("Application channel is being torn down. Abort event notify");
6535 LTTNG_ASSERT(ua_chan
->session
);
6536 ua_sess
= ua_chan
->session
;
6538 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6539 chan_reg_key
= ua_chan
->tracing_channel_id
;
6541 chan_reg_key
= ua_chan
->key
;
6545 auto locked_registry
= get_locked_session_registry(ua_sess
);
6546 if (locked_registry
) {
6548 * From this point on, this call acquires the ownership of the signature,
6549 * fields and model_emf_uri meaning any free are done inside it if needed.
6550 * These three variables MUST NOT be read/write after this.
6553 auto& channel
= locked_registry
->get_channel(chan_reg_key
);
6555 /* event_id is set on success. */
6556 channel
.add_event(sobjd
, cobjd
, name
, signature
.get(),
6557 lsu::create_trace_fields_from_ust_ctl_fields(
6558 *locked_registry
, fields
.get(),
6561 model_emf_uri
.get() ?
6562 nonstd::optional
<std::string
>(
6563 model_emf_uri
.get()) :
6565 ua_sess
->buffer_type
, *app
, event_id
);
6567 } catch (const std::exception
& ex
) {
6568 ERR("Failed to add event `%s` to registry session: %s", name
,
6570 /* Inform the application of the error; don't return directly. */
6574 DBG("Application session is being torn down. Abort event notify");
6580 * The return value is returned to ustctl so in case of an error, the
6581 * application can be notified. In case of an error, it's important not to
6582 * return a negative error or else the application will get closed.
6584 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6586 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6587 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6588 app
->pid
, app
->sock
);
6589 } else if (ret
== -EAGAIN
) {
6590 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6591 app
->pid
, app
->sock
);
6593 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6594 ret
, app
->pid
, app
->sock
);
6597 * No need to wipe the create event since the application socket will
6598 * get close on error hence cleaning up everything by itself.
6603 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6609 * Add enum to the UST session registry. Once done, this replies to the
6610 * application with the appropriate error code.
6612 * The session UST registry lock is acquired within this function.
6614 * On success 0 is returned else a negative value.
6616 static int add_enum_ust_registry(int sock
, int sobjd
, const char *name
,
6617 struct lttng_ust_ctl_enum_entry
*raw_entries
, size_t nr_entries
)
6620 struct ust_app
*app
;
6621 struct ust_app_session
*ua_sess
;
6622 uint64_t enum_id
= -1ULL;
6623 lttng::urcu::read_lock_guard read_lock_guard
;
6624 auto entries
= lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::free
>(
6627 /* Lookup application. If not found, there is a code flow error. */
6628 app
= find_app_by_notify_sock(sock
);
6630 /* Return an error since this is not an error */
6631 DBG("Application socket %d is being torn down. Aborting enum registration",
6636 /* Lookup session by UST object descriptor. */
6637 ua_sess
= find_session_by_objd(app
, sobjd
);
6639 /* Return an error since this is not an error */
6640 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6644 auto locked_registry
= get_locked_session_registry(ua_sess
);
6645 if (!locked_registry
) {
6646 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6651 * From this point on, the callee acquires the ownership of
6652 * entries. The variable entries MUST NOT be read/written after
6655 int application_reply_code
;
6657 locked_registry
->create_or_find_enum(
6658 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6659 application_reply_code
= 0;
6660 } catch (const std::exception
& ex
) {
6661 ERR("%s: %s", fmt::format("Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6662 *app
, name
).c_str(), ex
.what());
6663 application_reply_code
= -1;
6667 * The return value is returned to ustctl so in case of an error, the
6668 * application can be notified. In case of an error, it's important not to
6669 * return a negative error or else the application will get closed.
6671 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6673 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6674 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6675 app
->pid
, app
->sock
);
6676 } else if (ret
== -EAGAIN
) {
6677 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6678 app
->pid
, app
->sock
);
6680 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6681 ret
, app
->pid
, app
->sock
);
6684 * No need to wipe the create enum since the application socket will
6685 * get close on error hence cleaning up everything by itself.
6690 DBG3("UST registry enum %s added successfully or already found", name
);
6695 * Handle application notification through the given notify socket.
6697 * Return 0 on success or else a negative value.
6699 int ust_app_recv_notify(int sock
)
6702 enum lttng_ust_ctl_notify_cmd cmd
;
6704 DBG3("UST app receiving notify from sock %d", sock
);
6706 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6708 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6709 DBG3("UST app recv notify failed. Application died: sock = %d",
6711 } else if (ret
== -EAGAIN
) {
6712 WARN("UST app recv notify failed. Communication time out: sock = %d",
6715 ERR("UST app recv notify failed with ret %d: sock = %d",
6722 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6724 int sobjd
, cobjd
, loglevel_value
;
6725 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6727 struct lttng_ust_ctl_field
*fields
;
6729 DBG2("UST app ustctl register event received");
6731 ret
= lttng_ust_ctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel_value
,
6732 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
6734 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6735 DBG3("UST app recv event failed. Application died: sock = %d",
6737 } else if (ret
== -EAGAIN
) {
6738 WARN("UST app recv event failed. Communication time out: sock = %d",
6741 ERR("UST app recv event failed with ret %d: sock = %d",
6748 lttng::urcu::read_lock_guard rcu_lock
;
6749 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6751 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6757 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6758 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6766 * Add event to the UST registry coming from the notify socket. This
6767 * call will free if needed the sig, fields and model_emf_uri. This
6768 * code path loses the ownsership of these variables and transfer them
6769 * to the this function.
6771 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6772 fields
, loglevel_value
, model_emf_uri
);
6779 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6783 struct lttng_ust_ctl_field
*context_fields
;
6785 DBG2("UST app ustctl register channel received");
6787 ret
= lttng_ust_ctl_recv_register_channel(
6788 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6790 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6791 DBG3("UST app recv channel failed. Application died: sock = %d",
6793 } else if (ret
== -EAGAIN
) {
6794 WARN("UST app recv channel failed. Communication time out: sock = %d",
6797 ERR("UST app recv channel failed with ret %d: sock = %d", ret
,
6804 * The fields ownership are transfered to this function call meaning
6805 * that if needed it will be freed. After this, it's invalid to access
6806 * fields or clean them up.
6808 ret
= handle_app_register_channel_notification(sock
, cobjd
, context_fields
, field_count
);
6815 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6818 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6820 struct lttng_ust_ctl_enum_entry
*entries
;
6822 DBG2("UST app ustctl register enum received");
6824 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
,
6825 &entries
, &nr_entries
);
6827 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6828 DBG3("UST app recv enum failed. Application died: sock = %d",
6830 } else if (ret
== -EAGAIN
) {
6831 WARN("UST app recv enum failed. Communication time out: sock = %d",
6834 ERR("UST app recv enum failed with ret %d: sock = %d",
6840 /* Callee assumes ownership of entries. */
6841 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6842 entries
, nr_entries
);
6850 /* Should NEVER happen. */
6859 * Once the notify socket hangs up, this is called. First, it tries to find the
6860 * corresponding application. On failure, the call_rcu to close the socket is
6861 * executed. If an application is found, it tries to delete it from the notify
6862 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6864 * Note that an object needs to be allocated here so on ENOMEM failure, the
6865 * call RCU is not done but the rest of the cleanup is.
6867 void ust_app_notify_sock_unregister(int sock
)
6870 struct lttng_ht_iter iter
;
6871 struct ust_app
*app
;
6872 struct ust_app_notify_sock_obj
*obj
;
6874 LTTNG_ASSERT(sock
>= 0);
6878 obj
= zmalloc
<ust_app_notify_sock_obj
>();
6881 * An ENOMEM is kind of uncool. If this strikes we continue the
6882 * procedure but the call_rcu will not be called. In this case, we
6883 * accept the fd leak rather than possibly creating an unsynchronized
6884 * state between threads.
6886 * TODO: The notify object should be created once the notify socket is
6887 * registered and stored independantely from the ust app object. The
6888 * tricky part is to synchronize the teardown of the application and
6889 * this notify object. Let's keep that in mind so we can avoid this
6890 * kind of shenanigans with ENOMEM in the teardown path.
6897 DBG("UST app notify socket unregister %d", sock
);
6900 * Lookup application by notify socket. If this fails, this means that the
6901 * hash table delete has already been done by the application
6902 * unregistration process so we can safely close the notify socket in a
6905 app
= find_app_by_notify_sock(sock
);
6910 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6913 * Whatever happens here either we fail or succeed, in both cases we have
6914 * to close the socket after a grace period to continue to the call RCU
6915 * here. If the deletion is successful, the application is not visible
6916 * anymore by other threads and is it fails it means that it was already
6917 * deleted from the hash table so either way we just have to close the
6920 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6926 * Close socket after a grace period to avoid for the socket to be reused
6927 * before the application object is freed creating potential race between
6928 * threads trying to add unique in the global hash table.
6931 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6936 * Destroy a ust app data structure and free its memory.
6938 void ust_app_destroy(struct ust_app
*app
)
6944 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6948 * Take a snapshot for a given UST session. The snapshot is sent to the given
6951 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6953 enum lttng_error_code
ust_app_snapshot_record(
6954 const struct ltt_ust_session
*usess
,
6955 const struct consumer_output
*output
,
6956 uint64_t nb_packets_per_stream
)
6959 enum lttng_error_code status
= LTTNG_OK
;
6960 struct lttng_ht_iter iter
;
6961 struct ust_app
*app
;
6962 char *trace_path
= NULL
;
6964 LTTNG_ASSERT(usess
);
6965 LTTNG_ASSERT(output
);
6969 switch (usess
->buffer_type
) {
6970 case LTTNG_BUFFER_PER_UID
:
6972 struct buffer_reg_uid
*reg
;
6974 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6975 struct buffer_reg_channel
*buf_reg_chan
;
6976 struct consumer_socket
*socket
;
6977 char pathname
[PATH_MAX
];
6978 size_t consumer_path_offset
= 0;
6980 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
6981 /* Skip since no metadata is present */
6985 /* Get consumer socket to use to push the metadata.*/
6986 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6989 status
= LTTNG_ERR_INVALID
;
6993 memset(pathname
, 0, sizeof(pathname
));
6994 ret
= snprintf(pathname
, sizeof(pathname
),
6995 DEFAULT_UST_TRACE_UID_PATH
,
6996 reg
->uid
, reg
->bits_per_long
);
6998 PERROR("snprintf snapshot path");
6999 status
= LTTNG_ERR_INVALID
;
7002 /* Free path allowed on previous iteration. */
7004 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7005 &consumer_path_offset
);
7007 status
= LTTNG_ERR_INVALID
;
7010 /* Add the UST default trace dir to path. */
7011 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7012 buf_reg_chan
, node
.node
) {
7013 status
= consumer_snapshot_channel(socket
,
7014 buf_reg_chan
->consumer_key
,
7015 output
, 0, &trace_path
[consumer_path_offset
],
7016 nb_packets_per_stream
);
7017 if (status
!= LTTNG_OK
) {
7021 status
= consumer_snapshot_channel(socket
,
7022 reg
->registry
->reg
.ust
->_metadata_key
, output
, 1,
7023 &trace_path
[consumer_path_offset
], 0);
7024 if (status
!= LTTNG_OK
) {
7030 case LTTNG_BUFFER_PER_PID
:
7032 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7033 struct consumer_socket
*socket
;
7034 struct lttng_ht_iter chan_iter
;
7035 struct ust_app_channel
*ua_chan
;
7036 struct ust_app_session
*ua_sess
;
7037 lsu::registry_session
*registry
;
7038 char pathname
[PATH_MAX
];
7039 size_t consumer_path_offset
= 0;
7041 ua_sess
= lookup_session_by_app(usess
, app
);
7043 /* Session not associated with this app. */
7047 /* Get the right consumer socket for the application. */
7048 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7051 status
= LTTNG_ERR_INVALID
;
7055 /* Add the UST default trace dir to path. */
7056 memset(pathname
, 0, sizeof(pathname
));
7057 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
7060 status
= LTTNG_ERR_INVALID
;
7061 PERROR("snprintf snapshot path");
7064 /* Free path allowed on previous iteration. */
7066 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7067 &consumer_path_offset
);
7069 status
= LTTNG_ERR_INVALID
;
7072 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7073 ua_chan
, node
.node
) {
7074 status
= consumer_snapshot_channel(socket
,
7075 ua_chan
->key
, output
, 0,
7076 &trace_path
[consumer_path_offset
],
7077 nb_packets_per_stream
);
7081 case LTTNG_ERR_CHAN_NOT_FOUND
:
7088 registry
= get_session_registry(ua_sess
);
7090 DBG("Application session is being torn down. Skip application.");
7093 status
= consumer_snapshot_channel(socket
,
7094 registry
->_metadata_key
, output
, 1,
7095 &trace_path
[consumer_path_offset
], 0);
7099 case LTTNG_ERR_CHAN_NOT_FOUND
:
7119 * Return the size taken by one more packet per stream.
7121 uint64_t ust_app_get_size_one_more_packet_per_stream(
7122 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
7124 uint64_t tot_size
= 0;
7125 struct ust_app
*app
;
7126 struct lttng_ht_iter iter
;
7128 LTTNG_ASSERT(usess
);
7130 switch (usess
->buffer_type
) {
7131 case LTTNG_BUFFER_PER_UID
:
7133 struct buffer_reg_uid
*reg
;
7135 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7136 struct buffer_reg_channel
*buf_reg_chan
;
7139 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7140 buf_reg_chan
, node
.node
) {
7141 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7143 * Don't take channel into account if we
7144 * already grab all its packets.
7148 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7154 case LTTNG_BUFFER_PER_PID
:
7157 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7158 struct ust_app_channel
*ua_chan
;
7159 struct ust_app_session
*ua_sess
;
7160 struct lttng_ht_iter chan_iter
;
7162 ua_sess
= lookup_session_by_app(usess
, app
);
7164 /* Session not associated with this app. */
7168 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7169 ua_chan
, node
.node
) {
7170 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7172 * Don't take channel into account if we
7173 * already grab all its packets.
7177 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7191 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7192 struct cds_list_head
*buffer_reg_uid_list
,
7193 struct consumer_output
*consumer
, uint64_t uchan_id
,
7194 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
7197 uint64_t consumer_chan_key
;
7202 ret
= buffer_reg_uid_consumer_channel_key(
7203 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7211 ret
= consumer_get_lost_packets(ust_session_id
,
7212 consumer_chan_key
, consumer
, lost
);
7214 ret
= consumer_get_discarded_events(ust_session_id
,
7215 consumer_chan_key
, consumer
, discarded
);
7222 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7223 struct ltt_ust_channel
*uchan
,
7224 struct consumer_output
*consumer
, int overwrite
,
7225 uint64_t *discarded
, uint64_t *lost
)
7228 struct lttng_ht_iter iter
;
7229 struct lttng_ht_node_str
*ua_chan_node
;
7230 struct ust_app
*app
;
7231 struct ust_app_session
*ua_sess
;
7232 struct ust_app_channel
*ua_chan
;
7239 * Iterate over every registered applications. Sum counters for
7240 * all applications containing requested session and channel.
7242 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7243 struct lttng_ht_iter uiter
;
7245 ua_sess
= lookup_session_by_app(usess
, app
);
7246 if (ua_sess
== NULL
) {
7251 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7252 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7253 /* If the session is found for the app, the channel must be there */
7254 LTTNG_ASSERT(ua_chan_node
);
7256 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7261 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7268 uint64_t _discarded
;
7270 ret
= consumer_get_discarded_events(usess
->id
,
7271 ua_chan
->key
, consumer
, &_discarded
);
7275 (*discarded
) += _discarded
;
7284 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7285 struct ust_app
*app
)
7288 struct ust_app_session
*ua_sess
;
7290 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7294 ua_sess
= lookup_session_by_app(usess
, app
);
7295 if (ua_sess
== NULL
) {
7296 /* The session is in teardown process. Ignore and continue. */
7300 pthread_mutex_lock(&ua_sess
->lock
);
7302 if (ua_sess
->deleted
) {
7306 pthread_mutex_lock(&app
->sock_lock
);
7307 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7308 pthread_mutex_unlock(&app
->sock_lock
);
7311 pthread_mutex_unlock(&ua_sess
->lock
);
7315 health_code_update();
7320 * Regenerate the statedump for each app in the session.
7322 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7325 struct lttng_ht_iter iter
;
7326 struct ust_app
*app
;
7328 DBG("Regenerating the metadata for all UST apps");
7332 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7333 if (!app
->compatible
) {
7337 ret
= ust_app_regenerate_statedump(usess
, app
);
7339 /* Continue to the next app even on error */
7350 * Rotate all the channels of a session.
7352 * Return LTTNG_OK on success or else an LTTng error code.
7354 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7357 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7358 struct lttng_ht_iter iter
;
7359 struct ust_app
*app
;
7360 struct ltt_ust_session
*usess
= session
->ust_session
;
7362 LTTNG_ASSERT(usess
);
7366 switch (usess
->buffer_type
) {
7367 case LTTNG_BUFFER_PER_UID
:
7369 struct buffer_reg_uid
*reg
;
7371 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7372 struct buffer_reg_channel
*buf_reg_chan
;
7373 struct consumer_socket
*socket
;
7375 /* Get consumer socket to use to push the metadata.*/
7376 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7379 cmd_ret
= LTTNG_ERR_INVALID
;
7383 /* Rotate the data channels. */
7384 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7385 buf_reg_chan
, node
.node
) {
7386 ret
= consumer_rotate_channel(socket
,
7387 buf_reg_chan
->consumer_key
,
7389 /* is_metadata_channel */ false);
7391 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7397 * The metadata channel might not be present.
7399 * Consumer stream allocation can be done
7400 * asynchronously and can fail on intermediary
7401 * operations (i.e add context) and lead to data
7402 * channels created with no metadata channel.
7404 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7405 /* Skip since no metadata is present. */
7410 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7411 (void) push_metadata(locked_registry
, usess
->consumer
);
7414 ret
= consumer_rotate_channel(socket
,
7415 reg
->registry
->reg
.ust
->_metadata_key
,
7417 /* is_metadata_channel */ true);
7419 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7425 case LTTNG_BUFFER_PER_PID
:
7427 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7428 struct consumer_socket
*socket
;
7429 struct lttng_ht_iter chan_iter
;
7430 struct ust_app_channel
*ua_chan
;
7431 struct ust_app_session
*ua_sess
;
7432 lsu::registry_session
*registry
;
7434 ua_sess
= lookup_session_by_app(usess
, app
);
7436 /* Session not associated with this app. */
7440 /* Get the right consumer socket for the application. */
7441 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7444 cmd_ret
= LTTNG_ERR_INVALID
;
7448 registry
= get_session_registry(ua_sess
);
7450 DBG("Application session is being torn down. Skip application.");
7454 /* Rotate the data channels. */
7455 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7456 ua_chan
, node
.node
) {
7457 ret
= consumer_rotate_channel(socket
,
7460 /* is_metadata_channel */ false);
7462 /* Per-PID buffer and application going away. */
7463 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7465 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7470 /* Rotate the metadata channel. */
7472 auto locked_registry
= registry
->lock();
7474 (void) push_metadata(locked_registry
, usess
->consumer
);
7476 ret
= consumer_rotate_channel(socket
,
7477 registry
->_metadata_key
,
7479 /* is_metadata_channel */ true);
7481 /* Per-PID buffer and application going away. */
7482 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7484 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7502 enum lttng_error_code
ust_app_create_channel_subdirectories(
7503 const struct ltt_ust_session
*usess
)
7505 enum lttng_error_code ret
= LTTNG_OK
;
7506 struct lttng_ht_iter iter
;
7507 enum lttng_trace_chunk_status chunk_status
;
7508 char *pathname_index
;
7511 LTTNG_ASSERT(usess
->current_trace_chunk
);
7514 switch (usess
->buffer_type
) {
7515 case LTTNG_BUFFER_PER_UID
:
7517 struct buffer_reg_uid
*reg
;
7519 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7520 fmt_ret
= asprintf(&pathname_index
,
7521 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7522 reg
->uid
, reg
->bits_per_long
);
7524 ERR("Failed to format channel index directory");
7525 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7530 * Create the index subdirectory which will take care
7531 * of implicitly creating the channel's path.
7533 chunk_status
= lttng_trace_chunk_create_subdirectory(
7534 usess
->current_trace_chunk
,
7536 free(pathname_index
);
7537 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7538 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7544 case LTTNG_BUFFER_PER_PID
:
7546 struct ust_app
*app
;
7549 * Create the toplevel ust/ directory in case no apps are running.
7551 chunk_status
= lttng_trace_chunk_create_subdirectory(
7552 usess
->current_trace_chunk
,
7553 DEFAULT_UST_TRACE_DIR
);
7554 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7555 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7559 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7561 struct ust_app_session
*ua_sess
;
7562 lsu::registry_session
*registry
;
7564 ua_sess
= lookup_session_by_app(usess
, app
);
7566 /* Session not associated with this app. */
7570 registry
= get_session_registry(ua_sess
);
7572 DBG("Application session is being torn down. Skip application.");
7576 fmt_ret
= asprintf(&pathname_index
,
7577 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7580 ERR("Failed to format channel index directory");
7581 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7585 * Create the index subdirectory which will take care
7586 * of implicitly creating the channel's path.
7588 chunk_status
= lttng_trace_chunk_create_subdirectory(
7589 usess
->current_trace_chunk
,
7591 free(pathname_index
);
7592 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7593 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7610 * Clear all the channels of a session.
7612 * Return LTTNG_OK on success or else an LTTng error code.
7614 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7617 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7618 struct lttng_ht_iter iter
;
7619 struct ust_app
*app
;
7620 struct ltt_ust_session
*usess
= session
->ust_session
;
7622 LTTNG_ASSERT(usess
);
7626 if (usess
->active
) {
7627 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7628 cmd_ret
= LTTNG_ERR_FATAL
;
7632 switch (usess
->buffer_type
) {
7633 case LTTNG_BUFFER_PER_UID
:
7635 struct buffer_reg_uid
*reg
;
7637 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7638 struct buffer_reg_channel
*buf_reg_chan
;
7639 struct consumer_socket
*socket
;
7641 /* Get consumer socket to use to push the metadata.*/
7642 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7645 cmd_ret
= LTTNG_ERR_INVALID
;
7649 /* Clear the data channels. */
7650 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7651 buf_reg_chan
, node
.node
) {
7652 ret
= consumer_clear_channel(socket
,
7653 buf_reg_chan
->consumer_key
);
7660 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7661 (void) push_metadata(locked_registry
, usess
->consumer
);
7665 * Clear the metadata channel.
7666 * Metadata channel is not cleared per se but we still need to
7667 * perform a rotation operation on it behind the scene.
7669 ret
= consumer_clear_channel(socket
,
7670 reg
->registry
->reg
.ust
->_metadata_key
);
7677 case LTTNG_BUFFER_PER_PID
:
7679 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7680 struct consumer_socket
*socket
;
7681 struct lttng_ht_iter chan_iter
;
7682 struct ust_app_channel
*ua_chan
;
7683 struct ust_app_session
*ua_sess
;
7684 lsu::registry_session
*registry
;
7686 ua_sess
= lookup_session_by_app(usess
, app
);
7688 /* Session not associated with this app. */
7692 /* Get the right consumer socket for the application. */
7693 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7696 cmd_ret
= LTTNG_ERR_INVALID
;
7700 registry
= get_session_registry(ua_sess
);
7702 DBG("Application session is being torn down. Skip application.");
7706 /* Clear the data channels. */
7707 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7708 ua_chan
, node
.node
) {
7709 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7711 /* Per-PID buffer and application going away. */
7712 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7720 auto locked_registry
= registry
->lock();
7721 (void) push_metadata(locked_registry
, usess
->consumer
);
7725 * Clear the metadata channel.
7726 * Metadata channel is not cleared per se but we still need to
7727 * perform rotation operation on it behind the scene.
7729 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7731 /* Per-PID buffer and application going away. */
7732 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7750 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7751 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7754 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7764 * This function skips the metadata channel as the begin/end timestamps of a
7765 * metadata packet are useless.
7767 * Moreover, opening a packet after a "clear" will cause problems for live
7768 * sessions as it will introduce padding that was not part of the first trace
7769 * chunk. The relay daemon expects the content of the metadata stream of
7770 * successive metadata trace chunks to be strict supersets of one another.
7772 * For example, flushing a packet at the beginning of the metadata stream of
7773 * a trace chunk resulting from a "clear" session command will cause the
7774 * size of the metadata stream of the new trace chunk to not match the size of
7775 * the metadata stream of the original chunk. This will confuse the relay
7776 * daemon as the same "offset" in a metadata stream will no longer point
7777 * to the same content.
7779 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7781 enum lttng_error_code ret
= LTTNG_OK
;
7782 struct lttng_ht_iter iter
;
7783 struct ltt_ust_session
*usess
= session
->ust_session
;
7785 LTTNG_ASSERT(usess
);
7789 switch (usess
->buffer_type
) {
7790 case LTTNG_BUFFER_PER_UID
:
7792 struct buffer_reg_uid
*reg
;
7794 cds_list_for_each_entry (
7795 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7796 struct buffer_reg_channel
*buf_reg_chan
;
7797 struct consumer_socket
*socket
;
7799 socket
= consumer_find_socket_by_bitness(
7800 reg
->bits_per_long
, usess
->consumer
);
7802 ret
= LTTNG_ERR_FATAL
;
7806 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7807 &iter
.iter
, buf_reg_chan
, node
.node
) {
7808 const int open_ret
=
7809 consumer_open_channel_packets(
7811 buf_reg_chan
->consumer_key
);
7814 ret
= LTTNG_ERR_UNK
;
7821 case LTTNG_BUFFER_PER_PID
:
7823 struct ust_app
*app
;
7825 cds_lfht_for_each_entry (
7826 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7827 struct consumer_socket
*socket
;
7828 struct lttng_ht_iter chan_iter
;
7829 struct ust_app_channel
*ua_chan
;
7830 struct ust_app_session
*ua_sess
;
7831 lsu::registry_session
*registry
;
7833 ua_sess
= lookup_session_by_app(usess
, app
);
7835 /* Session not associated with this app. */
7839 /* Get the right consumer socket for the application. */
7840 socket
= consumer_find_socket_by_bitness(
7841 app
->abi
.bits_per_long
, usess
->consumer
);
7843 ret
= LTTNG_ERR_FATAL
;
7847 registry
= get_session_registry(ua_sess
);
7849 DBG("Application session is being torn down. Skip application.");
7853 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7854 &chan_iter
.iter
, ua_chan
, node
.node
) {
7855 const int open_ret
=
7856 consumer_open_channel_packets(
7862 * Per-PID buffer and application going
7865 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7869 ret
= LTTNG_ERR_UNK
;