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
->id
,
3465 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3466 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3467 if (notification_ret
!= LTTNG_OK
) {
3468 ret
= - (int) notification_ret
;
3469 ERR("Failed to add channel to notification thread");
3474 /* Send buffers to the application. */
3475 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3477 if (ret
!= -ENOTCONN
) {
3478 ERR("Error sending channel to application");
3485 session_put(session
);
3491 * Create and send to the application the created buffers with per PID buffers.
3493 * Called with UST app session lock held.
3494 * The session list lock and the session's lock must be acquired.
3496 * Return 0 on success else a negative value.
3498 static int create_channel_per_pid(struct ust_app
*app
,
3499 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3500 struct ust_app_channel
*ua_chan
)
3503 lsu::registry_session
*registry
;
3504 enum lttng_error_code cmd_ret
;
3505 struct ltt_session
*session
= NULL
;
3506 uint64_t chan_reg_key
;
3509 LTTNG_ASSERT(usess
);
3510 LTTNG_ASSERT(ua_sess
);
3511 LTTNG_ASSERT(ua_chan
);
3513 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3517 registry
= get_session_registry(ua_sess
);
3518 /* The UST app session lock is held, registry shall not be null. */
3519 LTTNG_ASSERT(registry
);
3521 /* Create and add a new channel registry to session. */
3523 registry
->add_channel(ua_chan
->key
);
3524 } catch (const std::exception
& ex
) {
3525 ERR("Error creating the UST channel \"%s\" registry instance: %s", ua_chan
->name
,
3531 session
= session_find_by_id(ua_sess
->tracing_id
);
3532 LTTNG_ASSERT(session
);
3533 ASSERT_LOCKED(session
->lock
);
3534 ASSERT_SESSION_LIST_LOCKED();
3536 /* Create and get channel on the consumer side. */
3537 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3538 app
->abi
.bits_per_long
, registry
);
3540 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3542 goto error_remove_from_registry
;
3545 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3547 if (ret
!= -ENOTCONN
) {
3548 ERR("Error sending channel to application");
3550 goto error_remove_from_registry
;
3553 chan_reg_key
= ua_chan
->key
;
3555 auto locked_registry
= registry
->lock();
3557 auto& ust_reg_chan
= locked_registry
->get_channel(chan_reg_key
);
3558 ust_reg_chan
._consumer_key
= ua_chan
->key
;
3561 cmd_ret
= notification_thread_command_add_channel(
3562 the_notification_thread_handle
, session
->id
,
3563 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3564 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3565 if (cmd_ret
!= LTTNG_OK
) {
3566 ret
= - (int) cmd_ret
;
3567 ERR("Failed to add channel to notification thread");
3568 goto error_remove_from_registry
;
3571 error_remove_from_registry
:
3574 auto locked_registry
= registry
->lock();
3575 locked_registry
->remove_channel(ua_chan
->key
, false);
3576 } catch (const std::exception
& ex
) {
3577 DBG("Could not find channel for removal: %s", ex
.what());
3583 session_put(session
);
3589 * From an already allocated ust app channel, create the channel buffers if
3590 * needed and send them to the application. This MUST be called with a RCU read
3591 * side lock acquired.
3593 * Called with UST app session lock held.
3595 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3596 * the application exited concurrently.
3598 static int ust_app_channel_send(struct ust_app
*app
,
3599 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3600 struct ust_app_channel
*ua_chan
)
3605 LTTNG_ASSERT(usess
);
3606 LTTNG_ASSERT(usess
->active
);
3607 LTTNG_ASSERT(ua_sess
);
3608 LTTNG_ASSERT(ua_chan
);
3609 ASSERT_RCU_READ_LOCKED();
3611 /* Handle buffer type before sending the channel to the application. */
3612 switch (usess
->buffer_type
) {
3613 case LTTNG_BUFFER_PER_UID
:
3615 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3621 case LTTNG_BUFFER_PER_PID
:
3623 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3635 /* Initialize ust objd object using the received handle and add it. */
3636 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3637 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3639 /* If channel is not enabled, disable it on the tracer */
3640 if (!ua_chan
->enabled
) {
3641 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3652 * Create UST app channel and return it through ua_chanp if not NULL.
3654 * Called with UST app session lock and RCU read-side lock held.
3656 * Return 0 on success or else a negative value.
3658 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3659 struct ltt_ust_channel
*uchan
,
3660 enum lttng_ust_abi_chan_type type
,
3661 struct ltt_ust_session
*usess
__attribute__((unused
)),
3662 struct ust_app_channel
**ua_chanp
)
3665 struct lttng_ht_iter iter
;
3666 struct lttng_ht_node_str
*ua_chan_node
;
3667 struct ust_app_channel
*ua_chan
;
3669 ASSERT_RCU_READ_LOCKED();
3671 /* Lookup channel in the ust app session */
3672 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3673 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3674 if (ua_chan_node
!= NULL
) {
3675 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
3679 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3680 if (ua_chan
== NULL
) {
3681 /* Only malloc can fail here */
3685 shadow_copy_channel(ua_chan
, uchan
);
3687 /* Set channel type. */
3688 ua_chan
->attr
.type
= type
;
3690 /* Only add the channel if successful on the tracer side. */
3691 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3694 *ua_chanp
= ua_chan
;
3697 /* Everything went well. */
3705 * Create UST app event and create it on the tracer side.
3707 * Must be called with the RCU read side lock held.
3708 * Called with ust app session mutex held.
3711 int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3712 struct ltt_ust_event
*uevent
,
3713 struct ust_app
*app
)
3716 struct ust_app_event
*ua_event
;
3718 ASSERT_RCU_READ_LOCKED();
3720 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3721 if (ua_event
== NULL
) {
3722 /* Only failure mode of alloc_ust_app_event(). */
3726 shadow_copy_event(ua_event
, uevent
);
3728 /* Create it on the tracer side */
3729 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3732 * Not found previously means that it does not exist on the
3733 * tracer. If the application reports that the event existed,
3734 * it means there is a bug in the sessiond or lttng-ust
3735 * (or corruption, etc.)
3737 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3738 ERR("Tracer for application reported that an event being created already existed: "
3739 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3741 app
->pid
, app
->ppid
, app
->uid
,
3747 add_unique_ust_app_event(ua_chan
, ua_event
);
3749 DBG2("UST app create event completed: app = '%s' pid = %d",
3750 app
->name
, app
->pid
);
3756 /* Valid. Calling here is already in a read side lock */
3757 delete_ust_app_event(-1, ua_event
, app
);
3762 * Create UST app event notifier rule and create it on the tracer side.
3764 * Must be called with the RCU read side lock held.
3765 * Called with ust app session mutex held.
3768 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3769 struct ust_app
*app
)
3772 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3774 ASSERT_RCU_READ_LOCKED();
3776 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3777 if (ua_event_notifier_rule
== NULL
) {
3782 /* Create it on the tracer side. */
3783 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3786 * Not found previously means that it does not exist on the
3787 * tracer. If the application reports that the event existed,
3788 * it means there is a bug in the sessiond or lttng-ust
3789 * (or corruption, etc.)
3791 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3792 ERR("Tracer for application reported that an event notifier being created already exists: "
3793 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3794 lttng_trigger_get_tracer_token(trigger
),
3795 app
->pid
, app
->ppid
, app
->uid
,
3801 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3802 &ua_event_notifier_rule
->node
);
3804 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3805 app
->name
, app
->pid
, lttng_trigger_get_tracer_token(trigger
));
3810 /* The RCU read side lock is already being held by the caller. */
3811 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3817 * Create UST metadata and open it on the tracer side.
3819 * Called with UST app session lock held and RCU read side lock.
3821 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3822 struct ust_app
*app
, struct consumer_output
*consumer
)
3825 struct ust_app_channel
*metadata
;
3826 struct consumer_socket
*socket
;
3827 struct ltt_session
*session
= NULL
;
3829 LTTNG_ASSERT(ua_sess
);
3831 LTTNG_ASSERT(consumer
);
3832 ASSERT_RCU_READ_LOCKED();
3834 auto locked_registry
= get_locked_session_registry(ua_sess
);
3835 /* The UST app session is held registry shall not be null. */
3836 LTTNG_ASSERT(locked_registry
);
3838 /* Metadata already exists for this registry or it was closed previously */
3839 if (locked_registry
->_metadata_key
|| locked_registry
->_metadata_closed
) {
3844 /* Allocate UST metadata */
3845 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3847 /* malloc() failed */
3852 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3854 /* Need one fd for the channel. */
3855 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3857 ERR("Exhausted number of available FD upon create metadata");
3861 /* Get the right consumer socket for the application. */
3862 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
, consumer
);
3865 goto error_consumer
;
3869 * Keep metadata key so we can identify it on the consumer side. Assign it
3870 * to the registry *before* we ask the consumer so we avoid the race of the
3871 * consumer requesting the metadata and the ask_channel call on our side
3872 * did not returned yet.
3874 locked_registry
->_metadata_key
= metadata
->key
;
3876 session
= session_find_by_id(ua_sess
->tracing_id
);
3877 LTTNG_ASSERT(session
);
3878 ASSERT_LOCKED(session
->lock
);
3879 ASSERT_SESSION_LIST_LOCKED();
3882 * Ask the metadata channel creation to the consumer. The metadata object
3883 * will be created by the consumer and kept their. However, the stream is
3884 * never added or monitored until we do a first push metadata to the
3887 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3888 locked_registry
.get(), session
->current_trace_chunk
);
3890 /* Nullify the metadata key so we don't try to close it later on. */
3891 locked_registry
->_metadata_key
= 0;
3892 goto error_consumer
;
3896 * The setup command will make the metadata stream be sent to the relayd,
3897 * if applicable, and the thread managing the metadatas. This is important
3898 * because after this point, if an error occurs, the only way the stream
3899 * can be deleted is to be monitored in the consumer.
3901 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3903 /* Nullify the metadata key so we don't try to close it later on. */
3904 locked_registry
->_metadata_key
= 0;
3905 goto error_consumer
;
3908 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3909 metadata
->key
, app
->pid
);
3912 lttng_fd_put(LTTNG_FD_APPS
, 1);
3913 delete_ust_app_channel(-1, metadata
, app
, locked_registry
);
3916 session_put(session
);
3922 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3923 * acquired before calling this function.
3925 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3927 struct ust_app
*app
= NULL
;
3928 struct lttng_ht_node_ulong
*node
;
3929 struct lttng_ht_iter iter
;
3931 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3932 node
= lttng_ht_iter_get_node_ulong(&iter
);
3934 DBG2("UST app no found with pid %d", pid
);
3938 DBG2("Found UST app by pid %d", pid
);
3940 app
= lttng::utils::container_of(node
, &ust_app::pid_n
);
3947 * Allocate and init an UST app object using the registration information and
3948 * the command socket. This is called when the command socket connects to the
3951 * The object is returned on success or else NULL.
3953 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3956 struct ust_app
*lta
= NULL
;
3957 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3960 LTTNG_ASSERT(sock
>= 0);
3962 DBG3("UST app creating application for socket %d", sock
);
3964 if ((msg
->bits_per_long
== 64 &&
3965 (uatomic_read(&the_ust_consumerd64_fd
) ==
3967 (msg
->bits_per_long
== 32 &&
3968 (uatomic_read(&the_ust_consumerd32_fd
) ==
3970 ERR("Registration failed: application \"%s\" (pid: %d) has "
3971 "%d-bit long, but no consumerd for this size is available.\n",
3972 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3977 * Reserve the two file descriptors of the event source pipe. The write
3978 * end will be closed once it is passed to the application, at which
3979 * point a single 'put' will be performed.
3981 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3983 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
3984 msg
->name
, (int) msg
->pid
);
3988 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3989 if (!event_notifier_event_source_pipe
) {
3990 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
3991 msg
->name
, msg
->pid
);
3995 lta
= zmalloc
<ust_app
>();
3998 goto error_free_pipe
;
4001 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
4003 lta
->ppid
= msg
->ppid
;
4004 lta
->uid
= msg
->uid
;
4005 lta
->gid
= msg
->gid
;
4008 .bits_per_long
= msg
->bits_per_long
,
4009 .long_alignment
= msg
->long_alignment
,
4010 .uint8_t_alignment
= msg
->uint8_t_alignment
,
4011 .uint16_t_alignment
= msg
->uint16_t_alignment
,
4012 .uint32_t_alignment
= msg
->uint32_t_alignment
,
4013 .uint64_t_alignment
= msg
->uint64_t_alignment
,
4014 .byte_order
= msg
->byte_order
== LITTLE_ENDIAN
?
4015 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_
:
4016 lttng::sessiond::trace::byte_order::BIG_ENDIAN_
,
4019 lta
->v_major
= msg
->major
;
4020 lta
->v_minor
= msg
->minor
;
4021 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4022 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4023 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4024 lta
->notify_sock
= -1;
4025 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4027 /* Copy name and make sure it's NULL terminated. */
4028 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4029 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4032 * Before this can be called, when receiving the registration information,
4033 * the application compatibility is checked. So, at this point, the
4034 * application can work with this session daemon.
4036 lta
->compatible
= 1;
4038 lta
->pid
= msg
->pid
;
4039 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4041 pthread_mutex_init(<a
->sock_lock
, NULL
);
4042 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4044 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4048 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4049 lttng_fd_put(LTTNG_FD_APPS
, 2);
4055 * For a given application object, add it to every hash table.
4057 void ust_app_add(struct ust_app
*app
)
4060 LTTNG_ASSERT(app
->notify_sock
>= 0);
4062 app
->registration_time
= time(NULL
);
4067 * On a re-registration, we want to kick out the previous registration of
4070 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4073 * The socket _should_ be unique until _we_ call close. So, a add_unique
4074 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4075 * already in the table.
4077 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4079 /* Add application to the notify socket hash table. */
4080 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4081 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4083 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4084 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
4085 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
4092 * Set the application version into the object.
4094 * Return 0 on success else a negative value either an errno code or a
4095 * LTTng-UST error code.
4097 int ust_app_version(struct ust_app
*app
)
4103 pthread_mutex_lock(&app
->sock_lock
);
4104 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4105 pthread_mutex_unlock(&app
->sock_lock
);
4107 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4108 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4109 app
->pid
, app
->sock
);
4110 } else if (ret
== -EAGAIN
) {
4111 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4112 app
->pid
, app
->sock
);
4114 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4115 ret
, app
->pid
, app
->sock
);
4122 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4124 return app
->v_major
>= 9;
4127 bool ust_app_supports_counters(const struct ust_app
*app
)
4129 return app
->v_major
>= 9;
4133 * Setup the base event notifier group.
4135 * Return 0 on success else a negative value either an errno code or a
4136 * LTTng-UST error code.
4138 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4141 int event_pipe_write_fd
;
4142 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4143 enum lttng_error_code lttng_ret
;
4144 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4148 if (!ust_app_supports_notifiers(app
)) {
4153 /* Get the write side of the pipe. */
4154 event_pipe_write_fd
= lttng_pipe_get_writefd(
4155 app
->event_notifier_group
.event_pipe
);
4157 pthread_mutex_lock(&app
->sock_lock
);
4158 ret
= lttng_ust_ctl_create_event_notifier_group(app
->sock
,
4159 event_pipe_write_fd
, &event_notifier_group
);
4160 pthread_mutex_unlock(&app
->sock_lock
);
4162 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4164 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4165 app
->pid
, app
->sock
);
4166 } else if (ret
== -EAGAIN
) {
4168 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4169 app
->pid
, app
->sock
);
4171 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4172 ret
, app
->pid
, app
->sock
, event_pipe_write_fd
);
4177 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4179 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4180 app
->name
, app
->pid
);
4185 * Release the file descriptor that was reserved for the write-end of
4188 lttng_fd_put(LTTNG_FD_APPS
, 1);
4190 lttng_ret
= notification_thread_command_add_tracer_event_source(
4191 the_notification_thread_handle
,
4192 lttng_pipe_get_readfd(
4193 app
->event_notifier_group
.event_pipe
),
4195 if (lttng_ret
!= LTTNG_OK
) {
4196 ERR("Failed to add tracer event source to notification thread");
4201 /* Assign handle only when the complete setup is valid. */
4202 app
->event_notifier_group
.object
= event_notifier_group
;
4204 event_notifier_error_accounting_status
=
4205 event_notifier_error_accounting_register_app(app
);
4206 switch (event_notifier_error_accounting_status
) {
4207 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4209 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4210 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",
4211 app
->sock
, app
->name
, (int) app
->pid
);
4213 goto error_accounting
;
4214 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4215 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4216 app
->sock
, app
->name
, (int) app
->pid
);
4218 goto error_accounting
;
4220 ERR("Failed to setup event notifier error accounting for app");
4222 goto error_accounting
;
4228 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4229 the_notification_thread_handle
,
4230 lttng_pipe_get_readfd(
4231 app
->event_notifier_group
.event_pipe
));
4232 if (lttng_ret
!= LTTNG_OK
) {
4233 ERR("Failed to remove application tracer event source from notification thread");
4237 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4238 free(app
->event_notifier_group
.object
);
4239 app
->event_notifier_group
.object
= NULL
;
4244 * Unregister app by removing it from the global traceable app list and freeing
4247 * The socket is already closed at this point so no close to sock.
4249 void ust_app_unregister(int sock
)
4251 struct ust_app
*lta
;
4252 struct lttng_ht_node_ulong
*node
;
4253 struct lttng_ht_iter ust_app_sock_iter
;
4254 struct lttng_ht_iter iter
;
4255 struct ust_app_session
*ua_sess
;
4260 /* Get the node reference for a call_rcu */
4261 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4262 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4265 lta
= lttng::utils::container_of(node
, &ust_app::sock_n
);
4266 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4269 * For per-PID buffers, perform "push metadata" and flush all
4270 * application streams before removing app from hash tables,
4271 * ensuring proper behavior of data_pending check.
4272 * Remove sessions so they are not visible during deletion.
4274 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4276 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4278 /* The session was already removed so scheduled for teardown. */
4282 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4283 (void) ust_app_flush_app_session(lta
, ua_sess
);
4287 * Add session to list for teardown. This is safe since at this point we
4288 * are the only one using this list.
4290 pthread_mutex_lock(&ua_sess
->lock
);
4292 if (ua_sess
->deleted
) {
4293 pthread_mutex_unlock(&ua_sess
->lock
);
4298 * Normally, this is done in the delete session process which is
4299 * executed in the call rcu below. However, upon registration we can't
4300 * afford to wait for the grace period before pushing data or else the
4301 * data pending feature can race between the unregistration and stop
4302 * command where the data pending command is sent *before* the grace
4305 * The close metadata below nullifies the metadata pointer in the
4306 * session so the delete session will NOT push/close a second time.
4308 auto locked_registry
= get_locked_session_registry(ua_sess
);
4309 if (locked_registry
) {
4310 /* Push metadata for application before freeing the application. */
4311 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
4314 * Don't ask to close metadata for global per UID buffers. Close
4315 * metadata only on destroy trace session in this case. Also, the
4316 * previous push metadata could have flag the metadata registry to
4317 * close so don't send a close command if closed.
4319 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4320 const auto metadata_key
= locked_registry
->_metadata_key
;
4321 const auto consumer_bitness
= locked_registry
->abi
.bits_per_long
;
4323 if (!locked_registry
->_metadata_closed
&& metadata_key
!= 0) {
4324 locked_registry
->_metadata_closed
= true;
4327 /* Release lock before communication, see comments in close_metadata(). */
4328 locked_registry
.reset();
4329 (void) close_metadata(metadata_key
, consumer_bitness
, ua_sess
->consumer
);
4331 locked_registry
.reset();
4334 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4336 pthread_mutex_unlock(&ua_sess
->lock
);
4339 /* Remove application from PID hash table */
4340 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4344 * Remove application from notify hash table. The thread handling the
4345 * notify socket could have deleted the node so ignore on error because
4346 * either way it's valid. The close of that socket is handled by the
4347 * apps_notify_thread.
4349 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4350 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4353 * Ignore return value since the node might have been removed before by an
4354 * add replace during app registration because the PID can be reassigned by
4357 iter
.iter
.node
= <a
->pid_n
.node
;
4358 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4360 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4365 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4372 * Fill events array with all events name of all registered apps.
4374 int ust_app_list_events(struct lttng_event
**events
)
4377 size_t nbmem
, count
= 0;
4378 struct lttng_ht_iter iter
;
4379 struct ust_app
*app
;
4380 struct lttng_event
*tmp_event
;
4382 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4383 tmp_event
= calloc
<lttng_event
>(nbmem
);
4384 if (tmp_event
== NULL
) {
4385 PERROR("zmalloc ust app events");
4392 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4393 struct lttng_ust_abi_tracepoint_iter uiter
;
4395 health_code_update();
4397 if (!app
->compatible
) {
4399 * TODO: In time, we should notice the caller of this error by
4400 * telling him that this is a version error.
4404 pthread_mutex_lock(&app
->sock_lock
);
4405 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4407 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4408 ERR("UST app list events getting handle failed for app pid %d",
4411 pthread_mutex_unlock(&app
->sock_lock
);
4415 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
,
4416 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4417 /* Handle ustctl error. */
4421 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4422 ERR("UST app tp list get failed for app %d with ret %d",
4425 DBG3("UST app tp list get failed. Application is dead");
4429 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4430 if (release_ret
< 0 &&
4431 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4432 release_ret
!= -EPIPE
) {
4433 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4435 pthread_mutex_unlock(&app
->sock_lock
);
4439 health_code_update();
4440 if (count
>= nbmem
) {
4441 /* In case the realloc fails, we free the memory */
4442 struct lttng_event
*new_tmp_event
;
4445 new_nbmem
= nbmem
<< 1;
4446 DBG2("Reallocating event list from %zu to %zu entries",
4448 new_tmp_event
= (lttng_event
*) realloc(tmp_event
,
4449 new_nbmem
* sizeof(struct lttng_event
));
4450 if (new_tmp_event
== NULL
) {
4453 PERROR("realloc ust app events");
4456 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4457 if (release_ret
< 0 &&
4458 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4459 release_ret
!= -EPIPE
) {
4460 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4462 pthread_mutex_unlock(&app
->sock_lock
);
4465 /* Zero the new memory */
4466 memset(new_tmp_event
+ nbmem
, 0,
4467 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4469 tmp_event
= new_tmp_event
;
4471 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4472 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4473 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4474 tmp_event
[count
].pid
= app
->pid
;
4475 tmp_event
[count
].enabled
= -1;
4478 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4479 pthread_mutex_unlock(&app
->sock_lock
);
4481 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4482 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4483 app
->pid
, app
->sock
);
4484 } else if (ret
== -EAGAIN
) {
4485 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4486 app
->pid
, app
->sock
);
4488 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4489 ret
, app
->pid
, app
->sock
);
4495 *events
= tmp_event
;
4497 DBG2("UST app list events done (%zu events)", count
);
4502 health_code_update();
4507 * Fill events array with all events name of all registered apps.
4509 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4512 size_t nbmem
, count
= 0;
4513 struct lttng_ht_iter iter
;
4514 struct ust_app
*app
;
4515 struct lttng_event_field
*tmp_event
;
4517 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4518 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4519 if (tmp_event
== NULL
) {
4520 PERROR("zmalloc ust app event fields");
4527 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4528 struct lttng_ust_abi_field_iter uiter
;
4530 health_code_update();
4532 if (!app
->compatible
) {
4534 * TODO: In time, we should notice the caller of this error by
4535 * telling him that this is a version error.
4539 pthread_mutex_lock(&app
->sock_lock
);
4540 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4542 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4543 ERR("UST app list field getting handle failed for app pid %d",
4546 pthread_mutex_unlock(&app
->sock_lock
);
4550 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
,
4551 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4552 /* Handle ustctl error. */
4556 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4557 ERR("UST app tp list field failed for app %d with ret %d",
4560 DBG3("UST app tp list field failed. Application is dead");
4564 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4565 pthread_mutex_unlock(&app
->sock_lock
);
4566 if (release_ret
< 0 &&
4567 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4568 release_ret
!= -EPIPE
) {
4569 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4574 health_code_update();
4575 if (count
>= nbmem
) {
4576 /* In case the realloc fails, we free the memory */
4577 struct lttng_event_field
*new_tmp_event
;
4580 new_nbmem
= nbmem
<< 1;
4581 DBG2("Reallocating event field list from %zu to %zu entries",
4583 new_tmp_event
= (lttng_event_field
*) realloc(tmp_event
,
4584 new_nbmem
* sizeof(struct lttng_event_field
));
4585 if (new_tmp_event
== NULL
) {
4588 PERROR("realloc ust app event fields");
4591 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4592 pthread_mutex_unlock(&app
->sock_lock
);
4594 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4595 release_ret
!= -EPIPE
) {
4596 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4600 /* Zero the new memory */
4601 memset(new_tmp_event
+ nbmem
, 0,
4602 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4604 tmp_event
= new_tmp_event
;
4607 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4608 /* Mapping between these enums matches 1 to 1. */
4609 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4610 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4612 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4613 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4614 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4615 tmp_event
[count
].event
.pid
= app
->pid
;
4616 tmp_event
[count
].event
.enabled
= -1;
4619 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4620 pthread_mutex_unlock(&app
->sock_lock
);
4622 ret
!= -LTTNG_UST_ERR_EXITING
&&
4624 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4629 *fields
= tmp_event
;
4631 DBG2("UST app list event fields done (%zu events)", count
);
4636 health_code_update();
4641 * Free and clean all traceable apps of the global list.
4643 void ust_app_clean_list(void)
4646 struct ust_app
*app
;
4647 struct lttng_ht_iter iter
;
4649 DBG2("UST app cleaning registered apps hash table");
4653 /* Cleanup notify socket hash table */
4654 if (ust_app_ht_by_notify_sock
) {
4655 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4656 notify_sock_n
.node
) {
4658 * Assert that all notifiers are gone as all triggers
4659 * are unregistered prior to this clean-up.
4661 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4663 ust_app_notify_sock_unregister(app
->notify_sock
);
4668 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4669 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4671 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4675 /* Cleanup socket hash table */
4676 if (ust_app_ht_by_sock
) {
4677 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4679 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4686 /* Destroy is done only when the ht is empty */
4688 lttng_ht_destroy(ust_app_ht
);
4690 if (ust_app_ht_by_sock
) {
4691 lttng_ht_destroy(ust_app_ht_by_sock
);
4693 if (ust_app_ht_by_notify_sock
) {
4694 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4699 * Init UST app hash table.
4701 int ust_app_ht_alloc(void)
4703 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4707 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4708 if (!ust_app_ht_by_sock
) {
4711 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4712 if (!ust_app_ht_by_notify_sock
) {
4719 * For a specific UST session, disable the channel for all registered apps.
4721 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4722 struct ltt_ust_channel
*uchan
)
4725 struct lttng_ht_iter iter
;
4726 struct lttng_ht_node_str
*ua_chan_node
;
4727 struct ust_app
*app
;
4728 struct ust_app_session
*ua_sess
;
4729 struct ust_app_channel
*ua_chan
;
4731 LTTNG_ASSERT(usess
->active
);
4732 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4733 uchan
->name
, usess
->id
);
4737 /* For every registered applications */
4738 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4739 struct lttng_ht_iter uiter
;
4740 if (!app
->compatible
) {
4742 * TODO: In time, we should notice the caller of this error by
4743 * telling him that this is a version error.
4747 ua_sess
= lookup_session_by_app(usess
, app
);
4748 if (ua_sess
== NULL
) {
4753 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4754 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4755 /* If the session if found for the app, the channel must be there */
4756 LTTNG_ASSERT(ua_chan_node
);
4758 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4759 /* The channel must not be already disabled */
4760 LTTNG_ASSERT(ua_chan
->enabled
== 1);
4762 /* Disable channel onto application */
4763 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4765 /* XXX: We might want to report this error at some point... */
4775 * For a specific UST session, enable the channel for all registered apps.
4777 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4778 struct ltt_ust_channel
*uchan
)
4781 struct lttng_ht_iter iter
;
4782 struct ust_app
*app
;
4783 struct ust_app_session
*ua_sess
;
4785 LTTNG_ASSERT(usess
->active
);
4786 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4787 uchan
->name
, usess
->id
);
4791 /* For every registered applications */
4792 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4793 if (!app
->compatible
) {
4795 * TODO: In time, we should notice the caller of this error by
4796 * telling him that this is a version error.
4800 ua_sess
= lookup_session_by_app(usess
, app
);
4801 if (ua_sess
== NULL
) {
4805 /* Enable channel onto application */
4806 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4808 /* XXX: We might want to report this error at some point... */
4818 * Disable an event in a channel and for a specific session.
4820 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4821 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4824 struct lttng_ht_iter iter
, uiter
;
4825 struct lttng_ht_node_str
*ua_chan_node
;
4826 struct ust_app
*app
;
4827 struct ust_app_session
*ua_sess
;
4828 struct ust_app_channel
*ua_chan
;
4829 struct ust_app_event
*ua_event
;
4831 LTTNG_ASSERT(usess
->active
);
4832 DBG("UST app disabling event %s for all apps in channel "
4833 "%s for session id %" PRIu64
,
4834 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4838 /* For all registered applications */
4839 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4840 if (!app
->compatible
) {
4842 * TODO: In time, we should notice the caller of this error by
4843 * telling him that this is a version error.
4847 ua_sess
= lookup_session_by_app(usess
, app
);
4848 if (ua_sess
== NULL
) {
4853 /* Lookup channel in the ust app session */
4854 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4855 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4856 if (ua_chan_node
== NULL
) {
4857 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4858 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4861 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
4863 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4864 uevent
->filter
, uevent
->attr
.loglevel
,
4866 if (ua_event
== NULL
) {
4867 DBG2("Event %s not found in channel %s for app pid %d."
4868 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4872 ret
= disable_ust_app_event(ua_event
, app
);
4874 /* XXX: Report error someday... */
4883 /* The ua_sess lock must be held by the caller. */
4885 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4886 struct ust_app_session
*ua_sess
,
4887 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4888 struct ust_app_channel
**_ua_chan
)
4891 struct ust_app_channel
*ua_chan
= NULL
;
4893 LTTNG_ASSERT(ua_sess
);
4894 ASSERT_LOCKED(ua_sess
->lock
);
4896 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4897 sizeof(uchan
->name
))) {
4898 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4902 struct ltt_ust_context
*uctx
= NULL
;
4905 * Create channel onto application and synchronize its
4908 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4909 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4915 ret
= ust_app_channel_send(app
, usess
,
4922 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4923 ret
= create_ust_app_channel_context(ua_chan
,
4936 * The application's socket is not valid. Either a bad socket
4937 * or a timeout on it. We can't inform the caller that for a
4938 * specific app, the session failed so lets continue here.
4940 ret
= 0; /* Not an error. */
4948 if (ret
== 0 && _ua_chan
) {
4950 * Only return the application's channel on success. Note
4951 * that the channel can still be part of the application's
4952 * channel hashtable on error.
4954 *_ua_chan
= ua_chan
;
4960 * Enable event for a specific session and channel on the tracer.
4962 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4963 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4966 struct lttng_ht_iter iter
, uiter
;
4967 struct lttng_ht_node_str
*ua_chan_node
;
4968 struct ust_app
*app
;
4969 struct ust_app_session
*ua_sess
;
4970 struct ust_app_channel
*ua_chan
;
4971 struct ust_app_event
*ua_event
;
4973 LTTNG_ASSERT(usess
->active
);
4974 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4975 uevent
->attr
.name
, usess
->id
);
4978 * NOTE: At this point, this function is called only if the session and
4979 * channel passed are already created for all apps. and enabled on the
4985 /* For all registered applications */
4986 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4987 if (!app
->compatible
) {
4989 * TODO: In time, we should notice the caller of this error by
4990 * telling him that this is a version error.
4994 ua_sess
= lookup_session_by_app(usess
, app
);
4996 /* The application has problem or is probably dead. */
5000 pthread_mutex_lock(&ua_sess
->lock
);
5002 if (ua_sess
->deleted
) {
5003 pthread_mutex_unlock(&ua_sess
->lock
);
5007 /* Lookup channel in the ust app session */
5008 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5009 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5011 * It is possible that the channel cannot be found is
5012 * the channel/event creation occurs concurrently with
5013 * an application exit.
5015 if (!ua_chan_node
) {
5016 pthread_mutex_unlock(&ua_sess
->lock
);
5020 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5022 /* Get event node */
5023 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5024 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5025 if (ua_event
== NULL
) {
5026 DBG3("UST app enable event %s not found for app PID %d."
5027 "Skipping app", uevent
->attr
.name
, app
->pid
);
5031 ret
= enable_ust_app_event(ua_event
, app
);
5033 pthread_mutex_unlock(&ua_sess
->lock
);
5037 pthread_mutex_unlock(&ua_sess
->lock
);
5046 * For a specific existing UST session and UST channel, creates the event for
5047 * all registered apps.
5049 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5050 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
5053 struct lttng_ht_iter iter
, uiter
;
5054 struct lttng_ht_node_str
*ua_chan_node
;
5055 struct ust_app
*app
;
5056 struct ust_app_session
*ua_sess
;
5057 struct ust_app_channel
*ua_chan
;
5059 LTTNG_ASSERT(usess
->active
);
5060 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5061 uevent
->attr
.name
, usess
->id
);
5065 /* For all registered applications */
5066 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5067 if (!app
->compatible
) {
5069 * TODO: In time, we should notice the caller of this error by
5070 * telling him that this is a version error.
5074 ua_sess
= lookup_session_by_app(usess
, app
);
5076 /* The application has problem or is probably dead. */
5080 pthread_mutex_lock(&ua_sess
->lock
);
5082 if (ua_sess
->deleted
) {
5083 pthread_mutex_unlock(&ua_sess
->lock
);
5087 /* Lookup channel in the ust app session */
5088 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5089 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5090 /* If the channel is not found, there is a code flow error */
5091 LTTNG_ASSERT(ua_chan_node
);
5093 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
5095 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5096 pthread_mutex_unlock(&ua_sess
->lock
);
5098 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5099 /* Possible value at this point: -ENOMEM. If so, we stop! */
5102 DBG2("UST app event %s already exist on app PID %d",
5103 uevent
->attr
.name
, app
->pid
);
5113 * Start tracing for a specific UST session and app.
5115 * Called with UST app session lock held.
5119 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5122 struct ust_app_session
*ua_sess
;
5124 DBG("Starting tracing for ust app pid %d", app
->pid
);
5128 if (!app
->compatible
) {
5132 ua_sess
= lookup_session_by_app(usess
, app
);
5133 if (ua_sess
== NULL
) {
5134 /* The session is in teardown process. Ignore and continue. */
5138 pthread_mutex_lock(&ua_sess
->lock
);
5140 if (ua_sess
->deleted
) {
5141 pthread_mutex_unlock(&ua_sess
->lock
);
5145 if (ua_sess
->enabled
) {
5146 pthread_mutex_unlock(&ua_sess
->lock
);
5150 /* Upon restart, we skip the setup, already done */
5151 if (ua_sess
->started
) {
5155 health_code_update();
5158 /* This starts the UST tracing */
5159 pthread_mutex_lock(&app
->sock_lock
);
5160 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5161 pthread_mutex_unlock(&app
->sock_lock
);
5163 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5164 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5165 app
->pid
, app
->sock
);
5166 pthread_mutex_unlock(&ua_sess
->lock
);
5168 } else if (ret
== -EAGAIN
) {
5169 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5170 app
->pid
, app
->sock
);
5171 pthread_mutex_unlock(&ua_sess
->lock
);
5175 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5176 ret
, app
->pid
, app
->sock
);
5181 /* Indicate that the session has been started once */
5182 ua_sess
->started
= 1;
5183 ua_sess
->enabled
= 1;
5185 pthread_mutex_unlock(&ua_sess
->lock
);
5187 health_code_update();
5189 /* Quiescent wait after starting trace */
5190 pthread_mutex_lock(&app
->sock_lock
);
5191 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5192 pthread_mutex_unlock(&app
->sock_lock
);
5194 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5195 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5196 app
->pid
, app
->sock
);
5197 } else if (ret
== -EAGAIN
) {
5198 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5199 app
->pid
, app
->sock
);
5201 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5202 ret
, app
->pid
, app
->sock
);
5208 health_code_update();
5212 pthread_mutex_unlock(&ua_sess
->lock
);
5214 health_code_update();
5219 * Stop tracing for a specific UST session and app.
5222 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5225 struct ust_app_session
*ua_sess
;
5227 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5231 if (!app
->compatible
) {
5232 goto end_no_session
;
5235 ua_sess
= lookup_session_by_app(usess
, app
);
5236 if (ua_sess
== NULL
) {
5237 goto end_no_session
;
5240 pthread_mutex_lock(&ua_sess
->lock
);
5242 if (ua_sess
->deleted
) {
5243 pthread_mutex_unlock(&ua_sess
->lock
);
5244 goto end_no_session
;
5248 * If started = 0, it means that stop trace has been called for a session
5249 * that was never started. It's possible since we can have a fail start
5250 * from either the application manager thread or the command thread. Simply
5251 * indicate that this is a stop error.
5253 if (!ua_sess
->started
) {
5254 goto error_rcu_unlock
;
5257 health_code_update();
5259 /* This inhibits UST tracing */
5260 pthread_mutex_lock(&app
->sock_lock
);
5261 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5262 pthread_mutex_unlock(&app
->sock_lock
);
5264 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5265 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5266 app
->pid
, app
->sock
);
5268 } else if (ret
== -EAGAIN
) {
5269 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5270 app
->pid
, app
->sock
);
5274 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5275 ret
, app
->pid
, app
->sock
);
5277 goto error_rcu_unlock
;
5280 health_code_update();
5281 ua_sess
->enabled
= 0;
5283 /* Quiescent wait after stopping trace */
5284 pthread_mutex_lock(&app
->sock_lock
);
5285 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5286 pthread_mutex_unlock(&app
->sock_lock
);
5288 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5289 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5290 app
->pid
, app
->sock
);
5291 } else if (ret
== -EAGAIN
) {
5292 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5293 app
->pid
, app
->sock
);
5295 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5296 ret
, app
->pid
, app
->sock
);
5300 health_code_update();
5303 auto locked_registry
= get_locked_session_registry(ua_sess
);
5305 /* The UST app session is held registry shall not be null. */
5306 LTTNG_ASSERT(locked_registry
);
5308 /* Push metadata for application before freeing the application. */
5309 (void) push_metadata(locked_registry
, ua_sess
->consumer
);
5313 pthread_mutex_unlock(&ua_sess
->lock
);
5316 health_code_update();
5320 pthread_mutex_unlock(&ua_sess
->lock
);
5322 health_code_update();
5327 int ust_app_flush_app_session(struct ust_app
*app
,
5328 struct ust_app_session
*ua_sess
)
5330 int ret
, retval
= 0;
5331 struct lttng_ht_iter iter
;
5332 struct ust_app_channel
*ua_chan
;
5333 struct consumer_socket
*socket
;
5335 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5339 if (!app
->compatible
) {
5340 goto end_not_compatible
;
5343 pthread_mutex_lock(&ua_sess
->lock
);
5345 if (ua_sess
->deleted
) {
5349 health_code_update();
5351 /* Flushing buffers */
5352 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
5355 /* Flush buffers and push metadata. */
5356 switch (ua_sess
->buffer_type
) {
5357 case LTTNG_BUFFER_PER_PID
:
5358 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5360 health_code_update();
5361 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5363 ERR("Error flushing consumer channel");
5369 case LTTNG_BUFFER_PER_UID
:
5375 health_code_update();
5378 pthread_mutex_unlock(&ua_sess
->lock
);
5382 health_code_update();
5387 * Flush buffers for all applications for a specific UST session.
5388 * Called with UST session lock held.
5391 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5396 DBG("Flushing session buffers for all ust apps");
5400 /* Flush buffers and push metadata. */
5401 switch (usess
->buffer_type
) {
5402 case LTTNG_BUFFER_PER_UID
:
5404 struct buffer_reg_uid
*reg
;
5405 struct lttng_ht_iter iter
;
5407 /* Flush all per UID buffers associated to that session. */
5408 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5409 lsu::registry_session
*ust_session_reg
;
5410 struct buffer_reg_channel
*buf_reg_chan
;
5411 struct consumer_socket
*socket
;
5413 /* Get consumer socket to use to push the metadata.*/
5414 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5417 /* Ignore request if no consumer is found for the session. */
5421 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5422 buf_reg_chan
, node
.node
) {
5424 * The following call will print error values so the return
5425 * code is of little importance because whatever happens, we
5426 * have to try them all.
5428 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5431 ust_session_reg
= reg
->registry
->reg
.ust
;
5432 /* Push metadata. */
5433 auto locked_registry
= ust_session_reg
->lock();
5434 (void) push_metadata(locked_registry
, usess
->consumer
);
5438 case LTTNG_BUFFER_PER_PID
:
5440 struct ust_app_session
*ua_sess
;
5441 struct lttng_ht_iter iter
;
5442 struct ust_app
*app
;
5444 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5445 ua_sess
= lookup_session_by_app(usess
, app
);
5446 if (ua_sess
== NULL
) {
5449 (void) ust_app_flush_app_session(app
, ua_sess
);
5460 health_code_update();
5465 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5466 struct ust_app_session
*ua_sess
)
5469 struct lttng_ht_iter iter
;
5470 struct ust_app_channel
*ua_chan
;
5471 struct consumer_socket
*socket
;
5473 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5477 if (!app
->compatible
) {
5478 goto end_not_compatible
;
5481 pthread_mutex_lock(&ua_sess
->lock
);
5483 if (ua_sess
->deleted
) {
5487 health_code_update();
5489 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
5492 ERR("Failed to find consumer (%" PRIu32
") socket",
5493 app
->abi
.bits_per_long
);
5498 /* Clear quiescent state. */
5499 switch (ua_sess
->buffer_type
) {
5500 case LTTNG_BUFFER_PER_PID
:
5501 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5502 ua_chan
, node
.node
) {
5503 health_code_update();
5504 ret
= consumer_clear_quiescent_channel(socket
,
5507 ERR("Error clearing quiescent state for consumer channel");
5513 case LTTNG_BUFFER_PER_UID
:
5520 health_code_update();
5523 pthread_mutex_unlock(&ua_sess
->lock
);
5527 health_code_update();
5532 * Clear quiescent state in each stream for all applications for a
5533 * specific UST session.
5534 * Called with UST session lock held.
5537 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5542 DBG("Clearing stream quiescent state for all ust apps");
5546 switch (usess
->buffer_type
) {
5547 case LTTNG_BUFFER_PER_UID
:
5549 struct lttng_ht_iter iter
;
5550 struct buffer_reg_uid
*reg
;
5553 * Clear quiescent for all per UID buffers associated to
5556 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5557 struct consumer_socket
*socket
;
5558 struct buffer_reg_channel
*buf_reg_chan
;
5560 /* Get associated consumer socket.*/
5561 socket
= consumer_find_socket_by_bitness(
5562 reg
->bits_per_long
, usess
->consumer
);
5565 * Ignore request if no consumer is found for
5571 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5572 &iter
.iter
, buf_reg_chan
, node
.node
) {
5574 * The following call will print error values so
5575 * the return code is of little importance
5576 * because whatever happens, we have to try them
5579 (void) consumer_clear_quiescent_channel(socket
,
5580 buf_reg_chan
->consumer_key
);
5585 case LTTNG_BUFFER_PER_PID
:
5587 struct ust_app_session
*ua_sess
;
5588 struct lttng_ht_iter iter
;
5589 struct ust_app
*app
;
5591 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5593 ua_sess
= lookup_session_by_app(usess
, app
);
5594 if (ua_sess
== NULL
) {
5597 (void) ust_app_clear_quiescent_app_session(app
,
5609 health_code_update();
5614 * Destroy a specific UST session in apps.
5616 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5619 struct ust_app_session
*ua_sess
;
5620 struct lttng_ht_iter iter
;
5621 struct lttng_ht_node_u64
*node
;
5623 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5627 if (!app
->compatible
) {
5631 __lookup_session_by_app(usess
, app
, &iter
);
5632 node
= lttng_ht_iter_get_node_u64(&iter
);
5634 /* Session is being or is deleted. */
5637 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::node
);
5639 health_code_update();
5640 destroy_app_session(app
, ua_sess
);
5642 health_code_update();
5644 /* Quiescent wait after stopping trace */
5645 pthread_mutex_lock(&app
->sock_lock
);
5646 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5647 pthread_mutex_unlock(&app
->sock_lock
);
5649 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5650 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5651 app
->pid
, app
->sock
);
5652 } else if (ret
== -EAGAIN
) {
5653 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5654 app
->pid
, app
->sock
);
5656 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5657 ret
, app
->pid
, app
->sock
);
5662 health_code_update();
5667 * Start tracing for the UST session.
5669 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5671 struct lttng_ht_iter iter
;
5672 struct ust_app
*app
;
5674 DBG("Starting all UST traces");
5677 * Even though the start trace might fail, flag this session active so
5678 * other application coming in are started by default.
5685 * In a start-stop-start use-case, we need to clear the quiescent state
5686 * of each channel set by the prior stop command, thus ensuring that a
5687 * following stop or destroy is sure to grab a timestamp_end near those
5688 * operations, even if the packet is empty.
5690 (void) ust_app_clear_quiescent_session(usess
);
5692 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5693 ust_app_global_update(usess
, app
);
5702 * Start tracing for the UST session.
5703 * Called with UST session lock held.
5705 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5708 struct lttng_ht_iter iter
;
5709 struct ust_app
*app
;
5711 DBG("Stopping all UST traces");
5714 * Even though the stop trace might fail, flag this session inactive so
5715 * other application coming in are not started by default.
5721 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5722 ret
= ust_app_stop_trace(usess
, app
);
5724 /* Continue to next apps even on error */
5729 (void) ust_app_flush_session(usess
);
5737 * Destroy app UST session.
5739 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5742 struct lttng_ht_iter iter
;
5743 struct ust_app
*app
;
5745 DBG("Destroy all UST traces");
5749 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5750 ret
= destroy_trace(usess
, app
);
5752 /* Continue to next apps even on error */
5762 /* The ua_sess lock must be held by the caller. */
5764 int find_or_create_ust_app_channel(
5765 struct ltt_ust_session
*usess
,
5766 struct ust_app_session
*ua_sess
,
5767 struct ust_app
*app
,
5768 struct ltt_ust_channel
*uchan
,
5769 struct ust_app_channel
**ua_chan
)
5772 struct lttng_ht_iter iter
;
5773 struct lttng_ht_node_str
*ua_chan_node
;
5775 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5776 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5778 *ua_chan
= caa_container_of(ua_chan_node
,
5779 struct ust_app_channel
, node
);
5783 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5792 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5793 struct ltt_ust_event
*uevent
,
5794 struct ust_app
*app
)
5797 struct ust_app_event
*ua_event
= NULL
;
5799 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5800 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5802 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5807 if (ua_event
->enabled
!= uevent
->enabled
) {
5808 ret
= uevent
->enabled
?
5809 enable_ust_app_event(ua_event
, app
) :
5810 disable_ust_app_event(ua_event
, app
);
5818 /* Called with RCU read-side lock held. */
5820 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5823 enum lttng_error_code ret_code
;
5824 enum lttng_trigger_status t_status
;
5825 struct lttng_ht_iter app_trigger_iter
;
5826 struct lttng_triggers
*triggers
= NULL
;
5827 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5828 unsigned int count
, i
;
5830 ASSERT_RCU_READ_LOCKED();
5832 if (!ust_app_supports_notifiers(app
)) {
5837 * Currrently, registering or unregistering a trigger with an
5838 * event rule condition causes a full synchronization of the event
5841 * The first step attempts to add an event notifier for all registered
5842 * triggers that apply to the user space tracers. Then, the
5843 * application's event notifiers rules are all checked against the list
5844 * of registered triggers. Any event notifier that doesn't have a
5845 * matching trigger can be assumed to have been disabled.
5847 * All of this is inefficient, but is put in place to get the feature
5848 * rolling as it is simpler at this moment. It will be optimized Soon™
5849 * to allow the state of enabled
5850 * event notifiers to be synchronized in a piece-wise way.
5853 /* Get all triggers using uid 0 (root) */
5854 ret_code
= notification_thread_command_list_triggers(
5855 the_notification_thread_handle
, 0, &triggers
);
5856 if (ret_code
!= LTTNG_OK
) {
5860 LTTNG_ASSERT(triggers
);
5862 t_status
= lttng_triggers_get_count(triggers
, &count
);
5863 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5867 for (i
= 0; i
< count
; i
++) {
5868 struct lttng_condition
*condition
;
5869 struct lttng_event_rule
*event_rule
;
5870 struct lttng_trigger
*trigger
;
5871 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5872 enum lttng_condition_status condition_status
;
5875 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5876 LTTNG_ASSERT(trigger
);
5878 token
= lttng_trigger_get_tracer_token(trigger
);
5879 condition
= lttng_trigger_get_condition(trigger
);
5881 if (lttng_condition_get_type(condition
) !=
5882 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5883 /* Does not apply */
5888 lttng_condition_event_rule_matches_borrow_rule_mutable(
5889 condition
, &event_rule
);
5890 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5892 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5893 /* Skip kernel related triggers. */
5898 * Find or create the associated token event rule. The caller
5899 * holds the RCU read lock, so this is safe to call without
5900 * explicitly acquiring it here.
5902 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5903 app
->token_to_event_notifier_rule_ht
, token
);
5904 if (!looked_up_event_notifier_rule
) {
5905 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5913 /* Remove all unknown event sources from the app. */
5914 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5915 &app_trigger_iter
.iter
, event_notifier_rule
,
5917 const uint64_t app_token
= event_notifier_rule
->token
;
5921 * Check if the app event trigger still exists on the
5922 * notification side.
5924 for (i
= 0; i
< count
; i
++) {
5925 uint64_t notification_thread_token
;
5926 const struct lttng_trigger
*trigger
=
5927 lttng_triggers_get_at_index(
5930 LTTNG_ASSERT(trigger
);
5932 notification_thread_token
=
5933 lttng_trigger_get_tracer_token(trigger
);
5935 if (notification_thread_token
== app_token
) {
5947 * This trigger was unregistered, disable it on the tracer's
5950 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5952 LTTNG_ASSERT(ret
== 0);
5954 /* Callee logs errors. */
5955 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5957 delete_ust_app_event_notifier_rule(
5958 app
->sock
, event_notifier_rule
, app
);
5964 lttng_triggers_destroy(triggers
);
5969 * RCU read lock must be held by the caller.
5972 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5973 struct ust_app_session
*ua_sess
,
5974 struct ust_app
*app
)
5977 struct cds_lfht_iter uchan_iter
;
5978 struct ltt_ust_channel
*uchan
;
5980 LTTNG_ASSERT(usess
);
5981 LTTNG_ASSERT(ua_sess
);
5983 ASSERT_RCU_READ_LOCKED();
5985 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5987 struct ust_app_channel
*ua_chan
;
5988 struct cds_lfht_iter uevent_iter
;
5989 struct ltt_ust_event
*uevent
;
5992 * Search for a matching ust_app_channel. If none is found,
5993 * create it. Creating the channel will cause the ua_chan
5994 * structure to be allocated, the channel buffers to be
5995 * allocated (if necessary) and sent to the application, and
5996 * all enabled contexts will be added to the channel.
5998 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5999 app
, uchan
, &ua_chan
);
6001 /* Tracer is probably gone or ENOMEM. */
6006 /* ua_chan will be NULL for the metadata channel */
6010 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
6012 ret
= ust_app_channel_synchronize_event(ua_chan
,
6019 if (ua_chan
->enabled
!= uchan
->enabled
) {
6020 ret
= uchan
->enabled
?
6021 enable_ust_app_channel(ua_sess
, uchan
, app
) :
6022 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6033 * The caller must ensure that the application is compatible and is tracked
6034 * by the process attribute trackers.
6037 void ust_app_synchronize(struct ltt_ust_session
*usess
,
6038 struct ust_app
*app
)
6041 struct ust_app_session
*ua_sess
= NULL
;
6044 * The application's configuration should only be synchronized for
6047 LTTNG_ASSERT(usess
->active
);
6049 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
6051 /* Tracer is probably gone or ENOMEM. */
6053 destroy_app_session(app
, ua_sess
);
6057 LTTNG_ASSERT(ua_sess
);
6059 pthread_mutex_lock(&ua_sess
->lock
);
6060 if (ua_sess
->deleted
) {
6061 goto deleted_session
;
6066 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6069 * Create the metadata for the application. This returns gracefully if a
6070 * metadata was already set for the session.
6072 * The metadata channel must be created after the data channels as the
6073 * consumer daemon assumes this ordering. When interacting with a relay
6074 * daemon, the consumer will use this assumption to send the
6075 * "STREAMS_SENT" message to the relay daemon.
6077 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6079 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6080 app
->sock
, usess
->id
);
6086 pthread_mutex_unlock(&ua_sess
->lock
);
6092 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6094 struct ust_app_session
*ua_sess
;
6096 ua_sess
= lookup_session_by_app(usess
, app
);
6097 if (ua_sess
== NULL
) {
6100 destroy_app_session(app
, ua_sess
);
6104 * Add channels/events from UST global domain to registered apps at sock.
6106 * Called with session lock held.
6107 * Called with RCU read-side lock held.
6109 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6111 LTTNG_ASSERT(usess
);
6112 LTTNG_ASSERT(usess
->active
);
6113 ASSERT_RCU_READ_LOCKED();
6115 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
6116 app
->sock
, usess
->id
);
6118 if (!app
->compatible
) {
6121 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
6123 trace_ust_id_tracker_lookup(
6124 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
6126 trace_ust_id_tracker_lookup(
6127 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
6130 * Synchronize the application's internal tracing configuration
6131 * and start tracing.
6133 ust_app_synchronize(usess
, app
);
6134 ust_app_start_trace(usess
, app
);
6136 ust_app_global_destroy(usess
, app
);
6141 * Add all event notifiers to an application.
6143 * Called with session lock held.
6144 * Called with RCU read-side lock held.
6146 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6148 ASSERT_RCU_READ_LOCKED();
6150 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6151 app
->name
, app
->pid
);
6153 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6157 if (app
->event_notifier_group
.object
== NULL
) {
6158 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6159 app
->name
, app
->pid
);
6163 ust_app_synchronize_event_notifier_rules(app
);
6167 * Called with session lock held.
6169 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6171 struct lttng_ht_iter iter
;
6172 struct ust_app
*app
;
6175 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6176 ust_app_global_update(usess
, app
);
6181 void ust_app_global_update_all_event_notifier_rules(void)
6183 struct lttng_ht_iter iter
;
6184 struct ust_app
*app
;
6187 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6188 ust_app_global_update_event_notifier_rules(app
);
6195 * Add context to a specific channel for global UST domain.
6197 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6198 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6201 struct lttng_ht_node_str
*ua_chan_node
;
6202 struct lttng_ht_iter iter
, uiter
;
6203 struct ust_app_channel
*ua_chan
= NULL
;
6204 struct ust_app_session
*ua_sess
;
6205 struct ust_app
*app
;
6207 LTTNG_ASSERT(usess
->active
);
6210 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6211 if (!app
->compatible
) {
6213 * TODO: In time, we should notice the caller of this error by
6214 * telling him that this is a version error.
6218 ua_sess
= lookup_session_by_app(usess
, app
);
6219 if (ua_sess
== NULL
) {
6223 pthread_mutex_lock(&ua_sess
->lock
);
6225 if (ua_sess
->deleted
) {
6226 pthread_mutex_unlock(&ua_sess
->lock
);
6230 /* Lookup channel in the ust app session */
6231 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6232 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6233 if (ua_chan_node
== NULL
) {
6236 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6238 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6243 pthread_mutex_unlock(&ua_sess
->lock
);
6251 * Receive registration and populate the given msg structure.
6253 * On success return 0 else a negative value returned by the ustctl call.
6255 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6258 uint32_t pid
, ppid
, uid
, gid
;
6262 ret
= lttng_ust_ctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6263 &pid
, &ppid
, &uid
, &gid
,
6264 &msg
->bits_per_long
,
6265 &msg
->uint8_t_alignment
,
6266 &msg
->uint16_t_alignment
,
6267 &msg
->uint32_t_alignment
,
6268 &msg
->uint64_t_alignment
,
6269 &msg
->long_alignment
,
6276 case LTTNG_UST_ERR_EXITING
:
6277 DBG3("UST app recv reg message failed. Application died");
6279 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6280 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6281 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6282 LTTNG_UST_ABI_MINOR_VERSION
);
6285 ERR("UST app recv reg message failed with ret %d", ret
);
6290 msg
->pid
= (pid_t
) pid
;
6291 msg
->ppid
= (pid_t
) ppid
;
6292 msg
->uid
= (uid_t
) uid
;
6293 msg
->gid
= (gid_t
) gid
;
6300 * Return a ust app session object using the application object and the
6301 * session object descriptor has a key. If not found, NULL is returned.
6302 * A RCU read side lock MUST be acquired when calling this function.
6304 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6307 struct lttng_ht_node_ulong
*node
;
6308 struct lttng_ht_iter iter
;
6309 struct ust_app_session
*ua_sess
= NULL
;
6312 ASSERT_RCU_READ_LOCKED();
6314 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6315 node
= lttng_ht_iter_get_node_ulong(&iter
);
6317 DBG2("UST app session find by objd %d not found", objd
);
6321 ua_sess
= lttng::utils::container_of(node
, &ust_app_session::ust_objd_node
);
6328 * Return a ust app channel object using the application object and the channel
6329 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6330 * lock MUST be acquired before calling this function.
6332 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6335 struct lttng_ht_node_ulong
*node
;
6336 struct lttng_ht_iter iter
;
6337 struct ust_app_channel
*ua_chan
= NULL
;
6340 ASSERT_RCU_READ_LOCKED();
6342 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6343 node
= lttng_ht_iter_get_node_ulong(&iter
);
6345 DBG2("UST app channel find by objd %d not found", objd
);
6349 ua_chan
= lttng::utils::container_of(node
, &ust_app_channel::ust_objd_node
);
6356 * Reply to a register channel notification from an application on the notify
6357 * socket. The channel metadata is also created.
6359 * The session UST registry lock is acquired in this function.
6361 * On success 0 is returned else a negative value.
6363 static int handle_app_register_channel_notification(int sock
,
6365 struct lttng_ust_ctl_field
*raw_context_fields
,
6366 size_t context_field_count
)
6368 int ret
, ret_code
= 0;
6370 uint64_t chan_reg_key
;
6371 struct ust_app
*app
;
6372 struct ust_app_channel
*ua_chan
;
6373 struct ust_app_session
*ua_sess
;
6374 auto ust_ctl_context_fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(
6375 raw_context_fields
);
6377 lttng::urcu::read_lock_guard read_lock_guard
;
6379 /* Lookup application. If not found, there is a code flow error. */
6380 app
= find_app_by_notify_sock(sock
);
6382 DBG("Application socket %d is being torn down. Abort event notify",
6387 /* Lookup channel by UST object descriptor. */
6388 ua_chan
= find_channel_by_objd(app
, cobjd
);
6390 DBG("Application channel is being torn down. Abort event notify");
6394 LTTNG_ASSERT(ua_chan
->session
);
6395 ua_sess
= ua_chan
->session
;
6397 /* Get right session registry depending on the session buffer type. */
6398 auto locked_registry_session
= get_locked_session_registry(ua_sess
);
6399 if (!locked_registry_session
) {
6400 DBG("Application session is being torn down. Abort event notify");
6404 /* Depending on the buffer type, a different channel key is used. */
6405 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6406 chan_reg_key
= ua_chan
->tracing_channel_id
;
6408 chan_reg_key
= ua_chan
->key
;
6411 auto& ust_reg_chan
= locked_registry_session
->get_channel(chan_reg_key
);
6413 /* Channel id is set during the object creation. */
6414 chan_id
= ust_reg_chan
.id
;
6417 * The application returns the typing information of the channel's
6418 * context fields. In per-PID buffering mode, this is the first and only
6419 * time we get this information. It is our chance to finalize the
6420 * initialiation of the channel and serialize it's layout's description
6421 * to the trace's metadata.
6423 * However, in per-UID buffering mode, every application will provide
6424 * this information (redundantly). The first time will allow us to
6425 * complete the initialization. The following times, we simply validate
6426 * that all apps provide the same typing for the context fields as a
6430 auto app_context_fields
= lsu::create_trace_fields_from_ust_ctl_fields(
6431 *locked_registry_session
, ust_ctl_context_fields
.get(),
6432 context_field_count
,
6433 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT
,
6434 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
);
6436 if (!ust_reg_chan
.is_registered()) {
6437 lst::type::cuptr event_context
= app_context_fields
.size() ?
6438 lttng::make_unique
<lst::structure_type
>(
6439 0, std::move(app_context_fields
)) :
6442 ust_reg_chan
.set_event_context(std::move(event_context
));
6445 * Validate that the context fields match between
6446 * registry and newcoming application.
6448 bool context_fields_match
;
6449 const auto *previous_event_context
= ust_reg_chan
.get_event_context();
6451 if (!previous_event_context
) {
6452 context_fields_match
= app_context_fields
.size() == 0;
6454 const lst::structure_type
app_event_context_struct(
6455 0, std::move(app_context_fields
));
6457 context_fields_match
= *previous_event_context
==
6458 app_event_context_struct
;
6461 if (!context_fields_match
) {
6462 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6463 app
->pid
, app
->sock
);
6468 } catch (std::exception
& ex
) {
6469 ERR("Failed to handle application context: %s", ex
.what());
6475 DBG3("UST app replying to register channel key %" PRIu64
6476 " with id %u, ret = %d", chan_reg_key
, chan_id
,
6479 ret
= lttng_ust_ctl_reply_register_channel(sock
, chan_id
,
6480 ust_reg_chan
.header_type_
== lst::stream_class::header_type::COMPACT
?
6481 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
:
6482 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
,
6485 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6486 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6487 app
->pid
, app
->sock
);
6488 } else if (ret
== -EAGAIN
) {
6489 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6490 app
->pid
, app
->sock
);
6492 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6493 ret
, app
->pid
, app
->sock
);
6499 /* This channel registry's registration is completed. */
6500 ust_reg_chan
.set_as_registered();
6506 * Add event to the UST channel registry. When the event is added to the
6507 * registry, the metadata is also created. Once done, this replies to the
6508 * application with the appropriate error code.
6510 * The session UST registry lock is acquired in the function.
6512 * On success 0 is returned else a negative value.
6514 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, const char *name
,
6515 char *raw_signature
, size_t nr_fields
, struct lttng_ust_ctl_field
*raw_fields
,
6516 int loglevel_value
, char *raw_model_emf_uri
)
6519 uint32_t event_id
= 0;
6520 uint64_t chan_reg_key
;
6521 struct ust_app
*app
;
6522 struct ust_app_channel
*ua_chan
;
6523 struct ust_app_session
*ua_sess
;
6524 lttng::urcu::read_lock_guard rcu_lock
;
6525 auto signature
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_signature
);
6526 auto fields
= lttng::make_unique_wrapper
<lttng_ust_ctl_field
, lttng::free
>(raw_fields
);
6527 auto model_emf_uri
= lttng::make_unique_wrapper
<char, lttng::free
>(raw_model_emf_uri
);
6529 /* Lookup application. If not found, there is a code flow error. */
6530 app
= find_app_by_notify_sock(sock
);
6532 DBG("Application socket %d is being torn down. Abort event notify",
6537 /* Lookup channel by UST object descriptor. */
6538 ua_chan
= find_channel_by_objd(app
, cobjd
);
6540 DBG("Application channel is being torn down. Abort event notify");
6544 LTTNG_ASSERT(ua_chan
->session
);
6545 ua_sess
= ua_chan
->session
;
6547 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6548 chan_reg_key
= ua_chan
->tracing_channel_id
;
6550 chan_reg_key
= ua_chan
->key
;
6554 auto locked_registry
= get_locked_session_registry(ua_sess
);
6555 if (locked_registry
) {
6557 * From this point on, this call acquires the ownership of the signature,
6558 * fields and model_emf_uri meaning any free are done inside it if needed.
6559 * These three variables MUST NOT be read/write after this.
6562 auto& channel
= locked_registry
->get_channel(chan_reg_key
);
6564 /* event_id is set on success. */
6565 channel
.add_event(sobjd
, cobjd
, name
, signature
.get(),
6566 lsu::create_trace_fields_from_ust_ctl_fields(
6567 *locked_registry
, fields
.get(),
6569 lst::field_location::root::
6570 EVENT_RECORD_PAYLOAD
,
6571 lsu::ctl_field_quirks::
6572 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
),
6574 model_emf_uri
.get() ?
6575 nonstd::optional
<std::string
>(
6576 model_emf_uri
.get()) :
6578 ua_sess
->buffer_type
, *app
, event_id
);
6580 } catch (const std::exception
& ex
) {
6581 ERR("Failed to add event `%s` to registry session: %s", name
,
6583 /* Inform the application of the error; don't return directly. */
6587 DBG("Application session is being torn down. Abort event notify");
6593 * The return value is returned to ustctl so in case of an error, the
6594 * application can be notified. In case of an error, it's important not to
6595 * return a negative error or else the application will get closed.
6597 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6599 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6600 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6601 app
->pid
, app
->sock
);
6602 } else if (ret
== -EAGAIN
) {
6603 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6604 app
->pid
, app
->sock
);
6606 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6607 ret
, app
->pid
, app
->sock
);
6610 * No need to wipe the create event since the application socket will
6611 * get close on error hence cleaning up everything by itself.
6616 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6622 * Add enum to the UST session registry. Once done, this replies to the
6623 * application with the appropriate error code.
6625 * The session UST registry lock is acquired within this function.
6627 * On success 0 is returned else a negative value.
6629 static int add_enum_ust_registry(int sock
, int sobjd
, const char *name
,
6630 struct lttng_ust_ctl_enum_entry
*raw_entries
, size_t nr_entries
)
6633 struct ust_app
*app
;
6634 struct ust_app_session
*ua_sess
;
6635 uint64_t enum_id
= -1ULL;
6636 lttng::urcu::read_lock_guard read_lock_guard
;
6637 auto entries
= lttng::make_unique_wrapper
<struct lttng_ust_ctl_enum_entry
, lttng::free
>(
6640 /* Lookup application. If not found, there is a code flow error. */
6641 app
= find_app_by_notify_sock(sock
);
6643 /* Return an error since this is not an error */
6644 DBG("Application socket %d is being torn down. Aborting enum registration",
6649 /* Lookup session by UST object descriptor. */
6650 ua_sess
= find_session_by_objd(app
, sobjd
);
6652 /* Return an error since this is not an error */
6653 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6657 auto locked_registry
= get_locked_session_registry(ua_sess
);
6658 if (!locked_registry
) {
6659 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6664 * From this point on, the callee acquires the ownership of
6665 * entries. The variable entries MUST NOT be read/written after
6668 int application_reply_code
;
6670 locked_registry
->create_or_find_enum(
6671 sobjd
, name
, entries
.release(), nr_entries
, &enum_id
);
6672 application_reply_code
= 0;
6673 } catch (const std::exception
& ex
) {
6674 ERR("%s: %s", fmt::format("Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6675 *app
, name
).c_str(), ex
.what());
6676 application_reply_code
= -1;
6680 * The return value is returned to ustctl so in case of an error, the
6681 * application can be notified. In case of an error, it's important not to
6682 * return a negative error or else the application will get closed.
6684 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, application_reply_code
);
6686 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6687 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6688 app
->pid
, app
->sock
);
6689 } else if (ret
== -EAGAIN
) {
6690 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6691 app
->pid
, app
->sock
);
6693 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6694 ret
, app
->pid
, app
->sock
);
6697 * No need to wipe the create enum since the application socket will
6698 * get close on error hence cleaning up everything by itself.
6703 DBG3("UST registry enum %s added successfully or already found", name
);
6708 * Handle application notification through the given notify socket.
6710 * Return 0 on success or else a negative value.
6712 int ust_app_recv_notify(int sock
)
6715 enum lttng_ust_ctl_notify_cmd cmd
;
6717 DBG3("UST app receiving notify from sock %d", sock
);
6719 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6721 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6722 DBG3("UST app recv notify failed. Application died: sock = %d",
6724 } else if (ret
== -EAGAIN
) {
6725 WARN("UST app recv notify failed. Communication time out: sock = %d",
6728 ERR("UST app recv notify failed with ret %d: sock = %d",
6735 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6737 int sobjd
, cobjd
, loglevel_value
;
6738 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6740 struct lttng_ust_ctl_field
*fields
;
6742 DBG2("UST app ustctl register event received");
6744 ret
= lttng_ust_ctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel_value
,
6745 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
6747 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6748 DBG3("UST app recv event failed. Application died: sock = %d",
6750 } else if (ret
== -EAGAIN
) {
6751 WARN("UST app recv event failed. Communication time out: sock = %d",
6754 ERR("UST app recv event failed with ret %d: sock = %d",
6761 lttng::urcu::read_lock_guard rcu_lock
;
6762 const struct ust_app
*app
= find_app_by_notify_sock(sock
);
6764 DBG("Application socket %d is being torn down. Abort event notify", sock
);
6770 if ((!fields
&& nr_fields
> 0) || (fields
&& nr_fields
== 0)) {
6771 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6779 * Add event to the UST registry coming from the notify socket. This
6780 * call will free if needed the sig, fields and model_emf_uri. This
6781 * code path loses the ownsership of these variables and transfer them
6782 * to the this function.
6784 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6785 fields
, loglevel_value
, model_emf_uri
);
6792 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6796 struct lttng_ust_ctl_field
*context_fields
;
6798 DBG2("UST app ustctl register channel received");
6800 ret
= lttng_ust_ctl_recv_register_channel(
6801 sock
, &sobjd
, &cobjd
, &field_count
, &context_fields
);
6803 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6804 DBG3("UST app recv channel failed. Application died: sock = %d",
6806 } else if (ret
== -EAGAIN
) {
6807 WARN("UST app recv channel failed. Communication time out: sock = %d",
6810 ERR("UST app recv channel failed with ret %d: sock = %d", ret
,
6817 * The fields ownership are transfered to this function call meaning
6818 * that if needed it will be freed. After this, it's invalid to access
6819 * fields or clean them up.
6821 ret
= handle_app_register_channel_notification(sock
, cobjd
, context_fields
, field_count
);
6828 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6831 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6833 struct lttng_ust_ctl_enum_entry
*entries
;
6835 DBG2("UST app ustctl register enum received");
6837 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
,
6838 &entries
, &nr_entries
);
6840 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6841 DBG3("UST app recv enum failed. Application died: sock = %d",
6843 } else if (ret
== -EAGAIN
) {
6844 WARN("UST app recv enum failed. Communication time out: sock = %d",
6847 ERR("UST app recv enum failed with ret %d: sock = %d",
6853 /* Callee assumes ownership of entries. */
6854 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6855 entries
, nr_entries
);
6863 /* Should NEVER happen. */
6872 * Once the notify socket hangs up, this is called. First, it tries to find the
6873 * corresponding application. On failure, the call_rcu to close the socket is
6874 * executed. If an application is found, it tries to delete it from the notify
6875 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6877 * Note that an object needs to be allocated here so on ENOMEM failure, the
6878 * call RCU is not done but the rest of the cleanup is.
6880 void ust_app_notify_sock_unregister(int sock
)
6883 struct lttng_ht_iter iter
;
6884 struct ust_app
*app
;
6885 struct ust_app_notify_sock_obj
*obj
;
6887 LTTNG_ASSERT(sock
>= 0);
6891 obj
= zmalloc
<ust_app_notify_sock_obj
>();
6894 * An ENOMEM is kind of uncool. If this strikes we continue the
6895 * procedure but the call_rcu will not be called. In this case, we
6896 * accept the fd leak rather than possibly creating an unsynchronized
6897 * state between threads.
6899 * TODO: The notify object should be created once the notify socket is
6900 * registered and stored independantely from the ust app object. The
6901 * tricky part is to synchronize the teardown of the application and
6902 * this notify object. Let's keep that in mind so we can avoid this
6903 * kind of shenanigans with ENOMEM in the teardown path.
6910 DBG("UST app notify socket unregister %d", sock
);
6913 * Lookup application by notify socket. If this fails, this means that the
6914 * hash table delete has already been done by the application
6915 * unregistration process so we can safely close the notify socket in a
6918 app
= find_app_by_notify_sock(sock
);
6923 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6926 * Whatever happens here either we fail or succeed, in both cases we have
6927 * to close the socket after a grace period to continue to the call RCU
6928 * here. If the deletion is successful, the application is not visible
6929 * anymore by other threads and is it fails it means that it was already
6930 * deleted from the hash table so either way we just have to close the
6933 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6939 * Close socket after a grace period to avoid for the socket to be reused
6940 * before the application object is freed creating potential race between
6941 * threads trying to add unique in the global hash table.
6944 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6949 * Destroy a ust app data structure and free its memory.
6951 void ust_app_destroy(struct ust_app
*app
)
6957 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6961 * Take a snapshot for a given UST session. The snapshot is sent to the given
6964 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6966 enum lttng_error_code
ust_app_snapshot_record(
6967 const struct ltt_ust_session
*usess
,
6968 const struct consumer_output
*output
,
6969 uint64_t nb_packets_per_stream
)
6972 enum lttng_error_code status
= LTTNG_OK
;
6973 struct lttng_ht_iter iter
;
6974 struct ust_app
*app
;
6975 char *trace_path
= NULL
;
6977 LTTNG_ASSERT(usess
);
6978 LTTNG_ASSERT(output
);
6982 switch (usess
->buffer_type
) {
6983 case LTTNG_BUFFER_PER_UID
:
6985 struct buffer_reg_uid
*reg
;
6987 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6988 struct buffer_reg_channel
*buf_reg_chan
;
6989 struct consumer_socket
*socket
;
6990 char pathname
[PATH_MAX
];
6991 size_t consumer_path_offset
= 0;
6993 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
6994 /* Skip since no metadata is present */
6998 /* Get consumer socket to use to push the metadata.*/
6999 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7002 status
= LTTNG_ERR_INVALID
;
7006 memset(pathname
, 0, sizeof(pathname
));
7007 ret
= snprintf(pathname
, sizeof(pathname
),
7008 DEFAULT_UST_TRACE_UID_PATH
,
7009 reg
->uid
, reg
->bits_per_long
);
7011 PERROR("snprintf snapshot path");
7012 status
= LTTNG_ERR_INVALID
;
7015 /* Free path allowed on previous iteration. */
7017 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7018 &consumer_path_offset
);
7020 status
= LTTNG_ERR_INVALID
;
7023 /* Add the UST default trace dir to path. */
7024 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7025 buf_reg_chan
, node
.node
) {
7026 status
= consumer_snapshot_channel(socket
,
7027 buf_reg_chan
->consumer_key
,
7028 output
, 0, &trace_path
[consumer_path_offset
],
7029 nb_packets_per_stream
);
7030 if (status
!= LTTNG_OK
) {
7034 status
= consumer_snapshot_channel(socket
,
7035 reg
->registry
->reg
.ust
->_metadata_key
, output
, 1,
7036 &trace_path
[consumer_path_offset
], 0);
7037 if (status
!= LTTNG_OK
) {
7043 case LTTNG_BUFFER_PER_PID
:
7045 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7046 struct consumer_socket
*socket
;
7047 struct lttng_ht_iter chan_iter
;
7048 struct ust_app_channel
*ua_chan
;
7049 struct ust_app_session
*ua_sess
;
7050 lsu::registry_session
*registry
;
7051 char pathname
[PATH_MAX
];
7052 size_t consumer_path_offset
= 0;
7054 ua_sess
= lookup_session_by_app(usess
, app
);
7056 /* Session not associated with this app. */
7060 /* Get the right consumer socket for the application. */
7061 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7064 status
= LTTNG_ERR_INVALID
;
7068 /* Add the UST default trace dir to path. */
7069 memset(pathname
, 0, sizeof(pathname
));
7070 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
7073 status
= LTTNG_ERR_INVALID
;
7074 PERROR("snprintf snapshot path");
7077 /* Free path allowed on previous iteration. */
7079 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7080 &consumer_path_offset
);
7082 status
= LTTNG_ERR_INVALID
;
7085 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7086 ua_chan
, node
.node
) {
7087 status
= consumer_snapshot_channel(socket
,
7088 ua_chan
->key
, output
, 0,
7089 &trace_path
[consumer_path_offset
],
7090 nb_packets_per_stream
);
7094 case LTTNG_ERR_CHAN_NOT_FOUND
:
7101 registry
= get_session_registry(ua_sess
);
7103 DBG("Application session is being torn down. Skip application.");
7106 status
= consumer_snapshot_channel(socket
,
7107 registry
->_metadata_key
, output
, 1,
7108 &trace_path
[consumer_path_offset
], 0);
7112 case LTTNG_ERR_CHAN_NOT_FOUND
:
7132 * Return the size taken by one more packet per stream.
7134 uint64_t ust_app_get_size_one_more_packet_per_stream(
7135 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
7137 uint64_t tot_size
= 0;
7138 struct ust_app
*app
;
7139 struct lttng_ht_iter iter
;
7141 LTTNG_ASSERT(usess
);
7143 switch (usess
->buffer_type
) {
7144 case LTTNG_BUFFER_PER_UID
:
7146 struct buffer_reg_uid
*reg
;
7148 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7149 struct buffer_reg_channel
*buf_reg_chan
;
7152 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7153 buf_reg_chan
, node
.node
) {
7154 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7156 * Don't take channel into account if we
7157 * already grab all its packets.
7161 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7167 case LTTNG_BUFFER_PER_PID
:
7170 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7171 struct ust_app_channel
*ua_chan
;
7172 struct ust_app_session
*ua_sess
;
7173 struct lttng_ht_iter chan_iter
;
7175 ua_sess
= lookup_session_by_app(usess
, app
);
7177 /* Session not associated with this app. */
7181 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7182 ua_chan
, node
.node
) {
7183 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7185 * Don't take channel into account if we
7186 * already grab all its packets.
7190 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7204 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7205 struct cds_list_head
*buffer_reg_uid_list
,
7206 struct consumer_output
*consumer
, uint64_t uchan_id
,
7207 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
7210 uint64_t consumer_chan_key
;
7215 ret
= buffer_reg_uid_consumer_channel_key(
7216 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7224 ret
= consumer_get_lost_packets(ust_session_id
,
7225 consumer_chan_key
, consumer
, lost
);
7227 ret
= consumer_get_discarded_events(ust_session_id
,
7228 consumer_chan_key
, consumer
, discarded
);
7235 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7236 struct ltt_ust_channel
*uchan
,
7237 struct consumer_output
*consumer
, int overwrite
,
7238 uint64_t *discarded
, uint64_t *lost
)
7241 struct lttng_ht_iter iter
;
7242 struct lttng_ht_node_str
*ua_chan_node
;
7243 struct ust_app
*app
;
7244 struct ust_app_session
*ua_sess
;
7245 struct ust_app_channel
*ua_chan
;
7252 * Iterate over every registered applications. Sum counters for
7253 * all applications containing requested session and channel.
7255 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7256 struct lttng_ht_iter uiter
;
7258 ua_sess
= lookup_session_by_app(usess
, app
);
7259 if (ua_sess
== NULL
) {
7264 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7265 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7266 /* If the session is found for the app, the channel must be there */
7267 LTTNG_ASSERT(ua_chan_node
);
7269 ua_chan
= lttng::utils::container_of(ua_chan_node
, &ust_app_channel::node
);
7274 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7281 uint64_t _discarded
;
7283 ret
= consumer_get_discarded_events(usess
->id
,
7284 ua_chan
->key
, consumer
, &_discarded
);
7288 (*discarded
) += _discarded
;
7297 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7298 struct ust_app
*app
)
7301 struct ust_app_session
*ua_sess
;
7303 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7307 ua_sess
= lookup_session_by_app(usess
, app
);
7308 if (ua_sess
== NULL
) {
7309 /* The session is in teardown process. Ignore and continue. */
7313 pthread_mutex_lock(&ua_sess
->lock
);
7315 if (ua_sess
->deleted
) {
7319 pthread_mutex_lock(&app
->sock_lock
);
7320 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7321 pthread_mutex_unlock(&app
->sock_lock
);
7324 pthread_mutex_unlock(&ua_sess
->lock
);
7328 health_code_update();
7333 * Regenerate the statedump for each app in the session.
7335 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7338 struct lttng_ht_iter iter
;
7339 struct ust_app
*app
;
7341 DBG("Regenerating the metadata for all UST apps");
7345 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7346 if (!app
->compatible
) {
7350 ret
= ust_app_regenerate_statedump(usess
, app
);
7352 /* Continue to the next app even on error */
7363 * Rotate all the channels of a session.
7365 * Return LTTNG_OK on success or else an LTTng error code.
7367 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7370 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7371 struct lttng_ht_iter iter
;
7372 struct ust_app
*app
;
7373 struct ltt_ust_session
*usess
= session
->ust_session
;
7375 LTTNG_ASSERT(usess
);
7379 switch (usess
->buffer_type
) {
7380 case LTTNG_BUFFER_PER_UID
:
7382 struct buffer_reg_uid
*reg
;
7384 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7385 struct buffer_reg_channel
*buf_reg_chan
;
7386 struct consumer_socket
*socket
;
7388 /* Get consumer socket to use to push the metadata.*/
7389 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7392 cmd_ret
= LTTNG_ERR_INVALID
;
7396 /* Rotate the data channels. */
7397 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7398 buf_reg_chan
, node
.node
) {
7399 ret
= consumer_rotate_channel(socket
,
7400 buf_reg_chan
->consumer_key
,
7402 /* is_metadata_channel */ false);
7404 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7410 * The metadata channel might not be present.
7412 * Consumer stream allocation can be done
7413 * asynchronously and can fail on intermediary
7414 * operations (i.e add context) and lead to data
7415 * channels created with no metadata channel.
7417 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7418 /* Skip since no metadata is present. */
7423 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7424 (void) push_metadata(locked_registry
, usess
->consumer
);
7427 ret
= consumer_rotate_channel(socket
,
7428 reg
->registry
->reg
.ust
->_metadata_key
,
7430 /* is_metadata_channel */ true);
7432 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7438 case LTTNG_BUFFER_PER_PID
:
7440 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7441 struct consumer_socket
*socket
;
7442 struct lttng_ht_iter chan_iter
;
7443 struct ust_app_channel
*ua_chan
;
7444 struct ust_app_session
*ua_sess
;
7445 lsu::registry_session
*registry
;
7447 ua_sess
= lookup_session_by_app(usess
, app
);
7449 /* Session not associated with this app. */
7453 /* Get the right consumer socket for the application. */
7454 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7457 cmd_ret
= LTTNG_ERR_INVALID
;
7461 registry
= get_session_registry(ua_sess
);
7463 DBG("Application session is being torn down. Skip application.");
7467 /* Rotate the data channels. */
7468 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7469 ua_chan
, node
.node
) {
7470 ret
= consumer_rotate_channel(socket
,
7473 /* is_metadata_channel */ false);
7475 /* Per-PID buffer and application going away. */
7476 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7478 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7483 /* Rotate the metadata channel. */
7485 auto locked_registry
= registry
->lock();
7487 (void) push_metadata(locked_registry
, usess
->consumer
);
7489 ret
= consumer_rotate_channel(socket
,
7490 registry
->_metadata_key
,
7492 /* is_metadata_channel */ true);
7494 /* Per-PID buffer and application going away. */
7495 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7497 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7515 enum lttng_error_code
ust_app_create_channel_subdirectories(
7516 const struct ltt_ust_session
*usess
)
7518 enum lttng_error_code ret
= LTTNG_OK
;
7519 struct lttng_ht_iter iter
;
7520 enum lttng_trace_chunk_status chunk_status
;
7521 char *pathname_index
;
7524 LTTNG_ASSERT(usess
->current_trace_chunk
);
7527 switch (usess
->buffer_type
) {
7528 case LTTNG_BUFFER_PER_UID
:
7530 struct buffer_reg_uid
*reg
;
7532 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7533 fmt_ret
= asprintf(&pathname_index
,
7534 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7535 reg
->uid
, reg
->bits_per_long
);
7537 ERR("Failed to format channel index directory");
7538 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7543 * Create the index subdirectory which will take care
7544 * of implicitly creating the channel's path.
7546 chunk_status
= lttng_trace_chunk_create_subdirectory(
7547 usess
->current_trace_chunk
,
7549 free(pathname_index
);
7550 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7551 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7557 case LTTNG_BUFFER_PER_PID
:
7559 struct ust_app
*app
;
7562 * Create the toplevel ust/ directory in case no apps are running.
7564 chunk_status
= lttng_trace_chunk_create_subdirectory(
7565 usess
->current_trace_chunk
,
7566 DEFAULT_UST_TRACE_DIR
);
7567 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7568 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7572 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7574 struct ust_app_session
*ua_sess
;
7575 lsu::registry_session
*registry
;
7577 ua_sess
= lookup_session_by_app(usess
, app
);
7579 /* Session not associated with this app. */
7583 registry
= get_session_registry(ua_sess
);
7585 DBG("Application session is being torn down. Skip application.");
7589 fmt_ret
= asprintf(&pathname_index
,
7590 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7593 ERR("Failed to format channel index directory");
7594 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7598 * Create the index subdirectory which will take care
7599 * of implicitly creating the channel's path.
7601 chunk_status
= lttng_trace_chunk_create_subdirectory(
7602 usess
->current_trace_chunk
,
7604 free(pathname_index
);
7605 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7606 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7623 * Clear all the channels of a session.
7625 * Return LTTNG_OK on success or else an LTTng error code.
7627 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7630 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7631 struct lttng_ht_iter iter
;
7632 struct ust_app
*app
;
7633 struct ltt_ust_session
*usess
= session
->ust_session
;
7635 LTTNG_ASSERT(usess
);
7639 if (usess
->active
) {
7640 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7641 cmd_ret
= LTTNG_ERR_FATAL
;
7645 switch (usess
->buffer_type
) {
7646 case LTTNG_BUFFER_PER_UID
:
7648 struct buffer_reg_uid
*reg
;
7650 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7651 struct buffer_reg_channel
*buf_reg_chan
;
7652 struct consumer_socket
*socket
;
7654 /* Get consumer socket to use to push the metadata.*/
7655 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7658 cmd_ret
= LTTNG_ERR_INVALID
;
7662 /* Clear the data channels. */
7663 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7664 buf_reg_chan
, node
.node
) {
7665 ret
= consumer_clear_channel(socket
,
7666 buf_reg_chan
->consumer_key
);
7673 auto locked_registry
= reg
->registry
->reg
.ust
->lock();
7674 (void) push_metadata(locked_registry
, usess
->consumer
);
7678 * Clear the metadata channel.
7679 * Metadata channel is not cleared per se but we still need to
7680 * perform a rotation operation on it behind the scene.
7682 ret
= consumer_clear_channel(socket
,
7683 reg
->registry
->reg
.ust
->_metadata_key
);
7690 case LTTNG_BUFFER_PER_PID
:
7692 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7693 struct consumer_socket
*socket
;
7694 struct lttng_ht_iter chan_iter
;
7695 struct ust_app_channel
*ua_chan
;
7696 struct ust_app_session
*ua_sess
;
7697 lsu::registry_session
*registry
;
7699 ua_sess
= lookup_session_by_app(usess
, app
);
7701 /* Session not associated with this app. */
7705 /* Get the right consumer socket for the application. */
7706 socket
= consumer_find_socket_by_bitness(app
->abi
.bits_per_long
,
7709 cmd_ret
= LTTNG_ERR_INVALID
;
7713 registry
= get_session_registry(ua_sess
);
7715 DBG("Application session is being torn down. Skip application.");
7719 /* Clear the data channels. */
7720 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7721 ua_chan
, node
.node
) {
7722 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7724 /* Per-PID buffer and application going away. */
7725 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7733 auto locked_registry
= registry
->lock();
7734 (void) push_metadata(locked_registry
, usess
->consumer
);
7738 * Clear the metadata channel.
7739 * Metadata channel is not cleared per se but we still need to
7740 * perform rotation operation on it behind the scene.
7742 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7744 /* Per-PID buffer and application going away. */
7745 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7763 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7764 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7767 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7777 * This function skips the metadata channel as the begin/end timestamps of a
7778 * metadata packet are useless.
7780 * Moreover, opening a packet after a "clear" will cause problems for live
7781 * sessions as it will introduce padding that was not part of the first trace
7782 * chunk. The relay daemon expects the content of the metadata stream of
7783 * successive metadata trace chunks to be strict supersets of one another.
7785 * For example, flushing a packet at the beginning of the metadata stream of
7786 * a trace chunk resulting from a "clear" session command will cause the
7787 * size of the metadata stream of the new trace chunk to not match the size of
7788 * the metadata stream of the original chunk. This will confuse the relay
7789 * daemon as the same "offset" in a metadata stream will no longer point
7790 * to the same content.
7792 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7794 enum lttng_error_code ret
= LTTNG_OK
;
7795 struct lttng_ht_iter iter
;
7796 struct ltt_ust_session
*usess
= session
->ust_session
;
7798 LTTNG_ASSERT(usess
);
7802 switch (usess
->buffer_type
) {
7803 case LTTNG_BUFFER_PER_UID
:
7805 struct buffer_reg_uid
*reg
;
7807 cds_list_for_each_entry (
7808 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7809 struct buffer_reg_channel
*buf_reg_chan
;
7810 struct consumer_socket
*socket
;
7812 socket
= consumer_find_socket_by_bitness(
7813 reg
->bits_per_long
, usess
->consumer
);
7815 ret
= LTTNG_ERR_FATAL
;
7819 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7820 &iter
.iter
, buf_reg_chan
, node
.node
) {
7821 const int open_ret
=
7822 consumer_open_channel_packets(
7824 buf_reg_chan
->consumer_key
);
7827 ret
= LTTNG_ERR_UNK
;
7834 case LTTNG_BUFFER_PER_PID
:
7836 struct ust_app
*app
;
7838 cds_lfht_for_each_entry (
7839 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7840 struct consumer_socket
*socket
;
7841 struct lttng_ht_iter chan_iter
;
7842 struct ust_app_channel
*ua_chan
;
7843 struct ust_app_session
*ua_sess
;
7844 lsu::registry_session
*registry
;
7846 ua_sess
= lookup_session_by_app(usess
, app
);
7848 /* Session not associated with this app. */
7852 /* Get the right consumer socket for the application. */
7853 socket
= consumer_find_socket_by_bitness(
7854 app
->abi
.bits_per_long
, usess
->consumer
);
7856 ret
= LTTNG_ERR_FATAL
;
7860 registry
= get_session_registry(ua_sess
);
7862 DBG("Application session is being torn down. Skip application.");
7866 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7867 &chan_iter
.iter
, ua_chan
, node
.node
) {
7868 const int open_ret
=
7869 consumer_open_channel_packets(
7875 * Per-PID buffer and application going
7878 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7882 ret
= LTTNG_ERR_UNK
;
7899 lsu::ctl_field_quirks
ust_app::ctl_field_quirks() const
7902 * Application contexts are expressed as variants. LTTng-UST announces
7903 * those by registering an enumeration named `..._tag`. It then registers a
7904 * variant as part of the event context that contains the various possible
7907 * Unfortunately, the names used in the enumeration and variant don't
7908 * match: the enumeration names are all prefixed with an underscore while
7909 * the variant type tag fields aren't.
7911 * While the CTF 1.8.3 specification mentions that
7912 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
7913 * 1.x (and possibly others) expect a perfect match between the names used
7914 * by tags and variants.
7916 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
7917 * the variant's fields are modified to match the mappings of its tag.
7919 * From ABI version >= 10.x, the variant fields and tag mapping names
7920 * correctly match, making this quirk unnecessary.
7922 return v_major
<= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
:
7923 lsu::ctl_field_quirks::NONE
;