2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
29 #include <urcu/compiler.h>
30 #include <lttng/ust-error.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "buffer-registry.h"
38 #include "health-sessiond.h"
40 #include "ust-consumer.h"
44 #include "lttng-sessiond.h"
45 #include "notification-thread-commands.h"
48 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
50 /* Next available channel key. Access under next_channel_key_lock. */
51 static uint64_t _next_channel_key
;
52 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
54 /* Next available session ID. Access under next_session_id_lock. */
55 static uint64_t _next_session_id
;
56 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 * Return the incremented value of next_channel_key.
61 static uint64_t get_next_channel_key(void)
65 pthread_mutex_lock(&next_channel_key_lock
);
66 ret
= ++_next_channel_key
;
67 pthread_mutex_unlock(&next_channel_key_lock
);
72 * Return the atomically incremented value of next_session_id.
74 static uint64_t get_next_session_id(void)
78 pthread_mutex_lock(&next_session_id_lock
);
79 ret
= ++_next_session_id
;
80 pthread_mutex_unlock(&next_session_id_lock
);
84 static void copy_channel_attr_to_ustctl(
85 struct ustctl_consumer_channel_attr
*attr
,
86 struct lttng_ust_channel_attr
*uattr
)
88 /* Copy event attributes since the layout is different. */
89 attr
->subbuf_size
= uattr
->subbuf_size
;
90 attr
->num_subbuf
= uattr
->num_subbuf
;
91 attr
->overwrite
= uattr
->overwrite
;
92 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
93 attr
->read_timer_interval
= uattr
->read_timer_interval
;
94 attr
->output
= uattr
->output
;
98 * Match function for the hash table lookup.
100 * It matches an ust app event based on three attributes which are the event
101 * name, the filter bytecode and the loglevel.
103 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
105 struct ust_app_event
*event
;
106 const struct ust_app_ht_key
*key
;
107 int ev_loglevel_value
;
112 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
114 ev_loglevel_value
= event
->attr
.loglevel
;
116 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
119 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
123 /* Event loglevel. */
124 if (ev_loglevel_value
!= key
->loglevel_type
) {
125 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
126 && key
->loglevel_type
== 0 &&
127 ev_loglevel_value
== -1) {
129 * Match is accepted. This is because on event creation, the
130 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
131 * -1 are accepted for this loglevel type since 0 is the one set by
132 * the API when receiving an enable event.
139 /* One of the filters is NULL, fail. */
140 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
144 if (key
->filter
&& event
->filter
) {
145 /* Both filters exists, check length followed by the bytecode. */
146 if (event
->filter
->len
!= key
->filter
->len
||
147 memcmp(event
->filter
->data
, key
->filter
->data
,
148 event
->filter
->len
) != 0) {
153 /* One of the exclusions is NULL, fail. */
154 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
158 if (key
->exclusion
&& event
->exclusion
) {
159 /* Both exclusions exists, check count followed by the names. */
160 if (event
->exclusion
->count
!= key
->exclusion
->count
||
161 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
162 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
176 * Unique add of an ust app event in the given ht. This uses the custom
177 * ht_match_ust_app_event match function and the event name as hash.
179 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
180 struct ust_app_event
*event
)
182 struct cds_lfht_node
*node_ptr
;
183 struct ust_app_ht_key key
;
187 assert(ua_chan
->events
);
190 ht
= ua_chan
->events
;
191 key
.name
= event
->attr
.name
;
192 key
.filter
= event
->filter
;
193 key
.loglevel_type
= event
->attr
.loglevel
;
194 key
.exclusion
= event
->exclusion
;
196 node_ptr
= cds_lfht_add_unique(ht
->ht
,
197 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
198 ht_match_ust_app_event
, &key
, &event
->node
.node
);
199 assert(node_ptr
== &event
->node
.node
);
203 * Close the notify socket from the given RCU head object. This MUST be called
204 * through a call_rcu().
206 static void close_notify_sock_rcu(struct rcu_head
*head
)
209 struct ust_app_notify_sock_obj
*obj
=
210 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
212 /* Must have a valid fd here. */
213 assert(obj
->fd
>= 0);
215 ret
= close(obj
->fd
);
217 ERR("close notify sock %d RCU", obj
->fd
);
219 lttng_fd_put(LTTNG_FD_APPS
, 1);
225 * Return the session registry according to the buffer type of the given
228 * A registry per UID object MUST exists before calling this function or else
229 * it assert() if not found. RCU read side lock must be acquired.
231 static struct ust_registry_session
*get_session_registry(
232 struct ust_app_session
*ua_sess
)
234 struct ust_registry_session
*registry
= NULL
;
238 switch (ua_sess
->buffer_type
) {
239 case LTTNG_BUFFER_PER_PID
:
241 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
245 registry
= reg_pid
->registry
->reg
.ust
;
248 case LTTNG_BUFFER_PER_UID
:
250 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
251 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
255 registry
= reg_uid
->registry
->reg
.ust
;
267 * Delete ust context safely. RCU read lock must be held before calling
271 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
279 pthread_mutex_lock(&app
->sock_lock
);
280 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
281 pthread_mutex_unlock(&app
->sock_lock
);
282 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
283 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
284 sock
, ua_ctx
->obj
->handle
, ret
);
292 * Delete ust app event safely. RCU read lock must be held before calling
296 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
303 free(ua_event
->filter
);
304 if (ua_event
->exclusion
!= NULL
)
305 free(ua_event
->exclusion
);
306 if (ua_event
->obj
!= NULL
) {
307 pthread_mutex_lock(&app
->sock_lock
);
308 ret
= ustctl_release_object(sock
, ua_event
->obj
);
309 pthread_mutex_unlock(&app
->sock_lock
);
310 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
311 ERR("UST app sock %d release event obj failed with ret %d",
320 * Release ust data object of the given stream.
322 * Return 0 on success or else a negative value.
324 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
332 pthread_mutex_lock(&app
->sock_lock
);
333 ret
= ustctl_release_object(sock
, stream
->obj
);
334 pthread_mutex_unlock(&app
->sock_lock
);
335 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
336 ERR("UST app sock %d release stream obj failed with ret %d",
339 lttng_fd_put(LTTNG_FD_APPS
, 2);
347 * Delete ust app stream safely. RCU read lock must be held before calling
351 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
356 (void) release_ust_app_stream(sock
, stream
, app
);
361 * We need to execute ht_destroy outside of RCU read-side critical
362 * section and outside of call_rcu thread, so we postpone its execution
363 * using ht_cleanup_push. It is simpler than to change the semantic of
364 * the many callers of delete_ust_app_session().
367 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
369 struct ust_app_channel
*ua_chan
=
370 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
372 ht_cleanup_push(ua_chan
->ctx
);
373 ht_cleanup_push(ua_chan
->events
);
378 * Extract the lost packet or discarded events counter when the channel is
379 * being deleted and store the value in the parent channel so we can
380 * access it from lttng list and at stop/destroy.
382 * The session list lock must be held by the caller.
385 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
387 uint64_t discarded
= 0, lost
= 0;
388 struct ltt_session
*session
;
389 struct ltt_ust_channel
*uchan
;
391 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
396 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
397 if (!session
|| !session
->ust_session
) {
399 * Not finding the session is not an error because there are
400 * multiple ways the channels can be torn down.
402 * 1) The session daemon can initiate the destruction of the
403 * ust app session after receiving a destroy command or
404 * during its shutdown/teardown.
405 * 2) The application, since we are in per-pid tracing, is
406 * unregistering and tearing down its ust app session.
408 * Both paths are protected by the session list lock which
409 * ensures that the accounting of lost packets and discarded
410 * events is done exactly once. The session is then unpublished
411 * from the session list, resulting in this condition.
416 if (ua_chan
->attr
.overwrite
) {
417 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
418 ua_chan
->key
, session
->ust_session
->consumer
,
421 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
422 ua_chan
->key
, session
->ust_session
->consumer
,
425 uchan
= trace_ust_find_channel_by_name(
426 session
->ust_session
->domain_global
.channels
,
429 ERR("Missing UST channel to store discarded counters");
433 uchan
->per_pid_closed_app_discarded
+= discarded
;
434 uchan
->per_pid_closed_app_lost
+= lost
;
441 * Delete ust app channel safely. RCU read lock must be held before calling
444 * The session list lock must be held by the caller.
447 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
451 struct lttng_ht_iter iter
;
452 struct ust_app_event
*ua_event
;
453 struct ust_app_ctx
*ua_ctx
;
454 struct ust_app_stream
*stream
, *stmp
;
455 struct ust_registry_session
*registry
;
459 DBG3("UST app deleting channel %s", ua_chan
->name
);
462 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
463 cds_list_del(&stream
->list
);
464 delete_ust_app_stream(sock
, stream
, app
);
468 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
469 cds_list_del(&ua_ctx
->list
);
470 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
472 delete_ust_app_ctx(sock
, ua_ctx
, app
);
476 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
478 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
480 delete_ust_app_event(sock
, ua_event
, app
);
483 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
484 /* Wipe and free registry from session registry. */
485 registry
= get_session_registry(ua_chan
->session
);
487 ust_registry_channel_del_free(registry
, ua_chan
->key
,
490 save_per_pid_lost_discarded_counters(ua_chan
);
493 if (ua_chan
->obj
!= NULL
) {
494 /* Remove channel from application UST object descriptor. */
495 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
496 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
498 pthread_mutex_lock(&app
->sock_lock
);
499 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
500 pthread_mutex_unlock(&app
->sock_lock
);
501 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
502 ERR("UST app sock %d release channel obj failed with ret %d",
505 lttng_fd_put(LTTNG_FD_APPS
, 1);
508 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
511 int ust_app_register_done(struct ust_app
*app
)
515 pthread_mutex_lock(&app
->sock_lock
);
516 ret
= ustctl_register_done(app
->sock
);
517 pthread_mutex_unlock(&app
->sock_lock
);
521 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
526 pthread_mutex_lock(&app
->sock_lock
);
531 ret
= ustctl_release_object(sock
, data
);
533 pthread_mutex_unlock(&app
->sock_lock
);
539 * Push metadata to consumer socket.
541 * RCU read-side lock must be held to guarantee existance of socket.
542 * Must be called with the ust app session lock held.
543 * Must be called with the registry lock held.
545 * On success, return the len of metadata pushed or else a negative value.
546 * Returning a -EPIPE return value means we could not send the metadata,
547 * but it can be caused by recoverable errors (e.g. the application has
548 * terminated concurrently).
550 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
551 struct consumer_socket
*socket
, int send_zero_data
)
554 char *metadata_str
= NULL
;
555 size_t len
, offset
, new_metadata_len_sent
;
557 uint64_t metadata_key
, metadata_version
;
562 metadata_key
= registry
->metadata_key
;
565 * Means that no metadata was assigned to the session. This can
566 * happens if no start has been done previously.
572 offset
= registry
->metadata_len_sent
;
573 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
574 new_metadata_len_sent
= registry
->metadata_len
;
575 metadata_version
= registry
->metadata_version
;
577 DBG3("No metadata to push for metadata key %" PRIu64
,
578 registry
->metadata_key
);
580 if (send_zero_data
) {
581 DBG("No metadata to push");
587 /* Allocate only what we have to send. */
588 metadata_str
= zmalloc(len
);
590 PERROR("zmalloc ust app metadata string");
594 /* Copy what we haven't sent out. */
595 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
598 pthread_mutex_unlock(®istry
->lock
);
600 * We need to unlock the registry while we push metadata to
601 * break a circular dependency between the consumerd metadata
602 * lock and the sessiond registry lock. Indeed, pushing metadata
603 * to the consumerd awaits that it gets pushed all the way to
604 * relayd, but doing so requires grabbing the metadata lock. If
605 * a concurrent metadata request is being performed by
606 * consumerd, this can try to grab the registry lock on the
607 * sessiond while holding the metadata lock on the consumer
608 * daemon. Those push and pull schemes are performed on two
609 * different bidirectionnal communication sockets.
611 ret
= consumer_push_metadata(socket
, metadata_key
,
612 metadata_str
, len
, offset
, metadata_version
);
613 pthread_mutex_lock(®istry
->lock
);
616 * There is an acceptable race here between the registry
617 * metadata key assignment and the creation on the
618 * consumer. The session daemon can concurrently push
619 * metadata for this registry while being created on the
620 * consumer since the metadata key of the registry is
621 * assigned *before* it is setup to avoid the consumer
622 * to ask for metadata that could possibly be not found
623 * in the session daemon.
625 * The metadata will get pushed either by the session
626 * being stopped or the consumer requesting metadata if
627 * that race is triggered.
629 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
632 ERR("Error pushing metadata to consumer");
638 * Metadata may have been concurrently pushed, since
639 * we're not holding the registry lock while pushing to
640 * consumer. This is handled by the fact that we send
641 * the metadata content, size, and the offset at which
642 * that metadata belongs. This may arrive out of order
643 * on the consumer side, and the consumer is able to
644 * deal with overlapping fragments. The consumer
645 * supports overlapping fragments, which must be
646 * contiguous starting from offset 0. We keep the
647 * largest metadata_len_sent value of the concurrent
650 registry
->metadata_len_sent
=
651 max_t(size_t, registry
->metadata_len_sent
,
652 new_metadata_len_sent
);
661 * On error, flag the registry that the metadata is
662 * closed. We were unable to push anything and this
663 * means that either the consumer is not responding or
664 * the metadata cache has been destroyed on the
667 registry
->metadata_closed
= 1;
675 * For a given application and session, push metadata to consumer.
676 * Either sock or consumer is required : if sock is NULL, the default
677 * socket to send the metadata is retrieved from consumer, if sock
678 * is not NULL we use it to send the metadata.
679 * RCU read-side lock must be held while calling this function,
680 * therefore ensuring existance of registry. It also ensures existance
681 * of socket throughout this function.
683 * Return 0 on success else a negative error.
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 static int push_metadata(struct ust_registry_session
*registry
,
689 struct consumer_output
*consumer
)
693 struct consumer_socket
*socket
;
698 pthread_mutex_lock(®istry
->lock
);
699 if (registry
->metadata_closed
) {
704 /* Get consumer socket to use to push the metadata.*/
705 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
712 ret
= ust_app_push_metadata(registry
, socket
, 0);
717 pthread_mutex_unlock(®istry
->lock
);
721 pthread_mutex_unlock(®istry
->lock
);
726 * Send to the consumer a close metadata command for the given session. Once
727 * done, the metadata channel is deleted and the session metadata pointer is
728 * nullified. The session lock MUST be held unless the application is
729 * in the destroy path.
731 * Return 0 on success else a negative value.
733 static int close_metadata(struct ust_registry_session
*registry
,
734 struct consumer_output
*consumer
)
737 struct consumer_socket
*socket
;
744 pthread_mutex_lock(®istry
->lock
);
746 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
751 /* Get consumer socket to use to push the metadata.*/
752 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
759 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
766 * Metadata closed. Even on error this means that the consumer is not
767 * responding or not found so either way a second close should NOT be emit
770 registry
->metadata_closed
= 1;
772 pthread_mutex_unlock(®istry
->lock
);
778 * We need to execute ht_destroy outside of RCU read-side critical
779 * section and outside of call_rcu thread, so we postpone its execution
780 * using ht_cleanup_push. It is simpler than to change the semantic of
781 * the many callers of delete_ust_app_session().
784 void delete_ust_app_session_rcu(struct rcu_head
*head
)
786 struct ust_app_session
*ua_sess
=
787 caa_container_of(head
, struct ust_app_session
, rcu_head
);
789 ht_cleanup_push(ua_sess
->channels
);
794 * Delete ust app session safely. RCU read lock must be held before calling
797 * The session list lock must be held by the caller.
800 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
804 struct lttng_ht_iter iter
;
805 struct ust_app_channel
*ua_chan
;
806 struct ust_registry_session
*registry
;
810 pthread_mutex_lock(&ua_sess
->lock
);
812 assert(!ua_sess
->deleted
);
813 ua_sess
->deleted
= true;
815 registry
= get_session_registry(ua_sess
);
816 /* Registry can be null on error path during initialization. */
818 /* Push metadata for application before freeing the application. */
819 (void) push_metadata(registry
, ua_sess
->consumer
);
822 * Don't ask to close metadata for global per UID buffers. Close
823 * metadata only on destroy trace session in this case. Also, the
824 * previous push metadata could have flag the metadata registry to
825 * close so don't send a close command if closed.
827 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
828 /* And ask to close it for this session registry. */
829 (void) close_metadata(registry
, ua_sess
->consumer
);
833 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
835 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
837 delete_ust_app_channel(sock
, ua_chan
, app
);
840 /* In case of per PID, the registry is kept in the session. */
841 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
842 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
845 * Registry can be null on error path during
848 buffer_reg_pid_remove(reg_pid
);
849 buffer_reg_pid_destroy(reg_pid
);
853 if (ua_sess
->handle
!= -1) {
854 pthread_mutex_lock(&app
->sock_lock
);
855 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
856 pthread_mutex_unlock(&app
->sock_lock
);
857 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
858 ERR("UST app sock %d release session handle failed with ret %d",
861 /* Remove session from application UST object descriptor. */
862 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
863 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
867 pthread_mutex_unlock(&ua_sess
->lock
);
869 consumer_output_put(ua_sess
->consumer
);
871 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
875 * Delete a traceable application structure from the global list. Never call
876 * this function outside of a call_rcu call.
878 * RCU read side lock should _NOT_ be held when calling this function.
881 void delete_ust_app(struct ust_app
*app
)
884 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
887 * The session list lock must be held during this function to guarantee
888 * the existence of ua_sess.
891 /* Delete ust app sessions info */
896 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
898 /* Free every object in the session and the session. */
900 delete_ust_app_session(sock
, ua_sess
, app
);
904 ht_cleanup_push(app
->sessions
);
905 ht_cleanup_push(app
->ust_sessions_objd
);
906 ht_cleanup_push(app
->ust_objd
);
909 * Wait until we have deleted the application from the sock hash table
910 * before closing this socket, otherwise an application could re-use the
911 * socket ID and race with the teardown, using the same hash table entry.
913 * It's OK to leave the close in call_rcu. We want it to stay unique for
914 * all RCU readers that could run concurrently with unregister app,
915 * therefore we _need_ to only close that socket after a grace period. So
916 * it should stay in this RCU callback.
918 * This close() is a very important step of the synchronization model so
919 * every modification to this function must be carefully reviewed.
925 lttng_fd_put(LTTNG_FD_APPS
, 1);
927 DBG2("UST app pid %d deleted", app
->pid
);
929 session_unlock_list();
933 * URCU intermediate call to delete an UST app.
936 void delete_ust_app_rcu(struct rcu_head
*head
)
938 struct lttng_ht_node_ulong
*node
=
939 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
940 struct ust_app
*app
=
941 caa_container_of(node
, struct ust_app
, pid_n
);
943 DBG3("Call RCU deleting app PID %d", app
->pid
);
948 * Delete the session from the application ht and delete the data structure by
949 * freeing every object inside and releasing them.
951 * The session list lock must be held by the caller.
953 static void destroy_app_session(struct ust_app
*app
,
954 struct ust_app_session
*ua_sess
)
957 struct lttng_ht_iter iter
;
962 iter
.iter
.node
= &ua_sess
->node
.node
;
963 ret
= lttng_ht_del(app
->sessions
, &iter
);
965 /* Already scheduled for teardown. */
969 /* Once deleted, free the data structure. */
970 delete_ust_app_session(app
->sock
, ua_sess
, app
);
977 * Alloc new UST app session.
980 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
982 struct ust_app_session
*ua_sess
;
984 /* Init most of the default value by allocating and zeroing */
985 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
986 if (ua_sess
== NULL
) {
991 ua_sess
->handle
= -1;
992 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
993 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
994 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1003 * Alloc new UST app channel.
1006 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1007 struct ust_app_session
*ua_sess
,
1008 struct lttng_ust_channel_attr
*attr
)
1010 struct ust_app_channel
*ua_chan
;
1012 /* Init most of the default value by allocating and zeroing */
1013 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1014 if (ua_chan
== NULL
) {
1019 /* Setup channel name */
1020 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1021 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1023 ua_chan
->enabled
= 1;
1024 ua_chan
->handle
= -1;
1025 ua_chan
->session
= ua_sess
;
1026 ua_chan
->key
= get_next_channel_key();
1027 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1028 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1029 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1031 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1032 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1034 /* Copy attributes */
1036 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1037 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1038 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1039 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1040 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1041 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1042 ua_chan
->attr
.output
= attr
->output
;
1044 /* By default, the channel is a per cpu channel. */
1045 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1047 DBG3("UST app channel %s allocated", ua_chan
->name
);
1056 * Allocate and initialize a UST app stream.
1058 * Return newly allocated stream pointer or NULL on error.
1060 struct ust_app_stream
*ust_app_alloc_stream(void)
1062 struct ust_app_stream
*stream
= NULL
;
1064 stream
= zmalloc(sizeof(*stream
));
1065 if (stream
== NULL
) {
1066 PERROR("zmalloc ust app stream");
1070 /* Zero could be a valid value for a handle so flag it to -1. */
1071 stream
->handle
= -1;
1078 * Alloc new UST app event.
1081 struct ust_app_event
*alloc_ust_app_event(char *name
,
1082 struct lttng_ust_event
*attr
)
1084 struct ust_app_event
*ua_event
;
1086 /* Init most of the default value by allocating and zeroing */
1087 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1088 if (ua_event
== NULL
) {
1093 ua_event
->enabled
= 1;
1094 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1095 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1096 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1098 /* Copy attributes */
1100 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1103 DBG3("UST app event %s allocated", ua_event
->name
);
1112 * Alloc new UST app context.
1115 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1117 struct ust_app_ctx
*ua_ctx
;
1119 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1120 if (ua_ctx
== NULL
) {
1124 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1127 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1128 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1129 char *provider_name
= NULL
, *ctx_name
= NULL
;
1131 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1132 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1133 if (!provider_name
|| !ctx_name
) {
1134 free(provider_name
);
1139 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1140 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1144 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1152 * Allocate a filter and copy the given original filter.
1154 * Return allocated filter or NULL on error.
1156 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1157 struct lttng_filter_bytecode
*orig_f
)
1159 struct lttng_filter_bytecode
*filter
= NULL
;
1161 /* Copy filter bytecode */
1162 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1164 PERROR("zmalloc alloc filter bytecode");
1168 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1175 * Create a liblttng-ust filter bytecode from given bytecode.
1177 * Return allocated filter or NULL on error.
1179 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1180 struct lttng_filter_bytecode
*orig_f
)
1182 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1184 /* Copy filter bytecode */
1185 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1187 PERROR("zmalloc alloc ust filter bytecode");
1191 assert(sizeof(struct lttng_filter_bytecode
) ==
1192 sizeof(struct lttng_ust_filter_bytecode
));
1193 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1199 * Find an ust_app using the sock and return it. RCU read side lock must be
1200 * held before calling this helper function.
1202 struct ust_app
*ust_app_find_by_sock(int sock
)
1204 struct lttng_ht_node_ulong
*node
;
1205 struct lttng_ht_iter iter
;
1207 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1208 node
= lttng_ht_iter_get_node_ulong(&iter
);
1210 DBG2("UST app find by sock %d not found", sock
);
1214 return caa_container_of(node
, struct ust_app
, sock_n
);
1221 * Find an ust_app using the notify sock and return it. RCU read side lock must
1222 * be held before calling this helper function.
1224 static struct ust_app
*find_app_by_notify_sock(int sock
)
1226 struct lttng_ht_node_ulong
*node
;
1227 struct lttng_ht_iter iter
;
1229 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1231 node
= lttng_ht_iter_get_node_ulong(&iter
);
1233 DBG2("UST app find by notify sock %d not found", sock
);
1237 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1244 * Lookup for an ust app event based on event name, filter bytecode and the
1247 * Return an ust_app_event object or NULL on error.
1249 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1250 char *name
, struct lttng_filter_bytecode
*filter
,
1252 const struct lttng_event_exclusion
*exclusion
)
1254 struct lttng_ht_iter iter
;
1255 struct lttng_ht_node_str
*node
;
1256 struct ust_app_event
*event
= NULL
;
1257 struct ust_app_ht_key key
;
1262 /* Setup key for event lookup. */
1264 key
.filter
= filter
;
1265 key
.loglevel_type
= loglevel_value
;
1266 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1267 key
.exclusion
= exclusion
;
1269 /* Lookup using the event name as hash and a custom match fct. */
1270 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1271 ht_match_ust_app_event
, &key
, &iter
.iter
);
1272 node
= lttng_ht_iter_get_node_str(&iter
);
1277 event
= caa_container_of(node
, struct ust_app_event
, node
);
1284 * Create the channel context on the tracer.
1286 * Called with UST app session lock held.
1289 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1290 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1294 health_code_update();
1296 pthread_mutex_lock(&app
->sock_lock
);
1297 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1298 ua_chan
->obj
, &ua_ctx
->obj
);
1299 pthread_mutex_unlock(&app
->sock_lock
);
1301 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1302 ERR("UST app create channel context failed for app (pid: %d) "
1303 "with ret %d", app
->pid
, ret
);
1306 * This is normal behavior, an application can die during the
1307 * creation process. Don't report an error so the execution can
1308 * continue normally.
1311 DBG3("UST app disable event failed. Application is dead.");
1316 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1318 DBG2("UST app context handle %d created successfully for channel %s",
1319 ua_ctx
->handle
, ua_chan
->name
);
1322 health_code_update();
1327 * Set the filter on the tracer.
1330 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1331 struct ust_app
*app
)
1334 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1336 health_code_update();
1338 if (!ua_event
->filter
) {
1343 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1344 if (!ust_bytecode
) {
1345 ret
= -LTTNG_ERR_NOMEM
;
1348 pthread_mutex_lock(&app
->sock_lock
);
1349 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1351 pthread_mutex_unlock(&app
->sock_lock
);
1353 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1354 ERR("UST app event %s filter failed for app (pid: %d) "
1355 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1358 * This is normal behavior, an application can die during the
1359 * creation process. Don't report an error so the execution can
1360 * continue normally.
1363 DBG3("UST app filter event failed. Application is dead.");
1368 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1371 health_code_update();
1377 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1378 struct lttng_event_exclusion
*exclusion
)
1380 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1381 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1382 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1384 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1385 if (!ust_exclusion
) {
1390 assert(sizeof(struct lttng_event_exclusion
) ==
1391 sizeof(struct lttng_ust_event_exclusion
));
1392 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1394 return ust_exclusion
;
1398 * Set event exclusions on the tracer.
1401 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1402 struct ust_app
*app
)
1405 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1407 health_code_update();
1409 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1414 ust_exclusion
= create_ust_exclusion_from_exclusion(
1415 ua_event
->exclusion
);
1416 if (!ust_exclusion
) {
1417 ret
= -LTTNG_ERR_NOMEM
;
1420 pthread_mutex_lock(&app
->sock_lock
);
1421 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1422 pthread_mutex_unlock(&app
->sock_lock
);
1424 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1425 ERR("UST app event %s exclusions failed for app (pid: %d) "
1426 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1429 * This is normal behavior, an application can die during the
1430 * creation process. Don't report an error so the execution can
1431 * continue normally.
1434 DBG3("UST app event exclusion failed. Application is dead.");
1439 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1442 health_code_update();
1443 free(ust_exclusion
);
1448 * Disable the specified event on to UST tracer for the UST session.
1450 static int disable_ust_event(struct ust_app
*app
,
1451 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1455 health_code_update();
1457 pthread_mutex_lock(&app
->sock_lock
);
1458 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1459 pthread_mutex_unlock(&app
->sock_lock
);
1461 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1462 ERR("UST app event %s disable failed for app (pid: %d) "
1463 "and session handle %d with ret %d",
1464 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1467 * This is normal behavior, an application can die during the
1468 * creation process. Don't report an error so the execution can
1469 * continue normally.
1472 DBG3("UST app disable event failed. Application is dead.");
1477 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1478 ua_event
->attr
.name
, app
->pid
);
1481 health_code_update();
1486 * Disable the specified channel on to UST tracer for the UST session.
1488 static int disable_ust_channel(struct ust_app
*app
,
1489 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1493 health_code_update();
1495 pthread_mutex_lock(&app
->sock_lock
);
1496 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1497 pthread_mutex_unlock(&app
->sock_lock
);
1499 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1500 ERR("UST app channel %s disable failed for app (pid: %d) "
1501 "and session handle %d with ret %d",
1502 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1505 * This is normal behavior, an application can die during the
1506 * creation process. Don't report an error so the execution can
1507 * continue normally.
1510 DBG3("UST app disable channel failed. Application is dead.");
1515 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1516 ua_chan
->name
, app
->pid
);
1519 health_code_update();
1524 * Enable the specified channel on to UST tracer for the UST session.
1526 static int enable_ust_channel(struct ust_app
*app
,
1527 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1531 health_code_update();
1533 pthread_mutex_lock(&app
->sock_lock
);
1534 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1535 pthread_mutex_unlock(&app
->sock_lock
);
1537 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1538 ERR("UST app channel %s enable failed for app (pid: %d) "
1539 "and session handle %d with ret %d",
1540 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1543 * This is normal behavior, an application can die during the
1544 * creation process. Don't report an error so the execution can
1545 * continue normally.
1548 DBG3("UST app enable channel failed. Application is dead.");
1553 ua_chan
->enabled
= 1;
1555 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1556 ua_chan
->name
, app
->pid
);
1559 health_code_update();
1564 * Enable the specified event on to UST tracer for the UST session.
1566 static int enable_ust_event(struct ust_app
*app
,
1567 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1571 health_code_update();
1573 pthread_mutex_lock(&app
->sock_lock
);
1574 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1575 pthread_mutex_unlock(&app
->sock_lock
);
1577 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1578 ERR("UST app event %s enable failed for app (pid: %d) "
1579 "and session handle %d with ret %d",
1580 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1583 * This is normal behavior, an application can die during the
1584 * creation process. Don't report an error so the execution can
1585 * continue normally.
1588 DBG3("UST app enable event failed. Application is dead.");
1593 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1594 ua_event
->attr
.name
, app
->pid
);
1597 health_code_update();
1602 * Send channel and stream buffer to application.
1604 * Return 0 on success. On error, a negative value is returned.
1606 static int send_channel_pid_to_ust(struct ust_app
*app
,
1607 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1610 struct ust_app_stream
*stream
, *stmp
;
1616 health_code_update();
1618 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1621 /* Send channel to the application. */
1622 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1623 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1624 ret
= -ENOTCONN
; /* Caused by app exiting. */
1626 } else if (ret
< 0) {
1630 health_code_update();
1632 /* Send all streams to application. */
1633 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1634 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1635 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1636 ret
= -ENOTCONN
; /* Caused by app exiting. */
1638 } else if (ret
< 0) {
1641 /* We don't need the stream anymore once sent to the tracer. */
1642 cds_list_del(&stream
->list
);
1643 delete_ust_app_stream(-1, stream
, app
);
1645 /* Flag the channel that it is sent to the application. */
1646 ua_chan
->is_sent
= 1;
1649 health_code_update();
1654 * Create the specified event onto the UST tracer for a UST session.
1656 * Should be called with session mutex held.
1659 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1660 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1664 health_code_update();
1666 /* Create UST event on tracer */
1667 pthread_mutex_lock(&app
->sock_lock
);
1668 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1670 pthread_mutex_unlock(&app
->sock_lock
);
1672 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1673 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1674 ua_event
->attr
.name
, app
->pid
, ret
);
1677 * This is normal behavior, an application can die during the
1678 * creation process. Don't report an error so the execution can
1679 * continue normally.
1682 DBG3("UST app create event failed. Application is dead.");
1687 ua_event
->handle
= ua_event
->obj
->handle
;
1689 DBG2("UST app event %s created successfully for pid:%d",
1690 ua_event
->attr
.name
, app
->pid
);
1692 health_code_update();
1694 /* Set filter if one is present. */
1695 if (ua_event
->filter
) {
1696 ret
= set_ust_event_filter(ua_event
, app
);
1702 /* Set exclusions for the event */
1703 if (ua_event
->exclusion
) {
1704 ret
= set_ust_event_exclusion(ua_event
, app
);
1710 /* If event not enabled, disable it on the tracer */
1711 if (ua_event
->enabled
) {
1713 * We now need to explicitly enable the event, since it
1714 * is now disabled at creation.
1716 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1719 * If we hit an EPERM, something is wrong with our enable call. If
1720 * we get an EEXIST, there is a problem on the tracer side since we
1724 case -LTTNG_UST_ERR_PERM
:
1725 /* Code flow problem */
1727 case -LTTNG_UST_ERR_EXIST
:
1728 /* It's OK for our use case. */
1739 health_code_update();
1744 * Copy data between an UST app event and a LTT event.
1746 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1747 struct ltt_ust_event
*uevent
)
1749 size_t exclusion_alloc_size
;
1751 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1752 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1754 ua_event
->enabled
= uevent
->enabled
;
1756 /* Copy event attributes */
1757 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1759 /* Copy filter bytecode */
1760 if (uevent
->filter
) {
1761 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1762 /* Filter might be NULL here in case of ENONEM. */
1765 /* Copy exclusion data */
1766 if (uevent
->exclusion
) {
1767 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1768 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1769 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1770 if (ua_event
->exclusion
== NULL
) {
1773 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1774 exclusion_alloc_size
);
1780 * Copy data between an UST app channel and a LTT channel.
1782 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1783 struct ltt_ust_channel
*uchan
)
1785 struct lttng_ht_iter iter
;
1786 struct ltt_ust_event
*uevent
;
1787 struct ltt_ust_context
*uctx
;
1788 struct ust_app_event
*ua_event
;
1790 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1792 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1793 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1795 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1796 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1798 /* Copy event attributes since the layout is different. */
1799 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1800 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1801 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1802 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1803 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1804 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1805 ua_chan
->attr
.output
= uchan
->attr
.output
;
1807 * Note that the attribute channel type is not set since the channel on the
1808 * tracing registry side does not have this information.
1811 ua_chan
->enabled
= uchan
->enabled
;
1812 ua_chan
->tracing_channel_id
= uchan
->id
;
1814 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1815 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1817 if (ua_ctx
== NULL
) {
1820 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1821 (unsigned long) ua_ctx
->ctx
.ctx
);
1822 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1823 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1826 /* Copy all events from ltt ust channel to ust app channel */
1827 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1828 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1829 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1830 if (ua_event
== NULL
) {
1831 DBG2("UST event %s not found on shadow copy channel",
1833 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1834 if (ua_event
== NULL
) {
1837 shadow_copy_event(ua_event
, uevent
);
1838 add_unique_ust_app_event(ua_chan
, ua_event
);
1842 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1846 * Copy data between a UST app session and a regular LTT session.
1848 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1849 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1851 struct lttng_ht_node_str
*ua_chan_node
;
1852 struct lttng_ht_iter iter
;
1853 struct ltt_ust_channel
*uchan
;
1854 struct ust_app_channel
*ua_chan
;
1856 struct tm
*timeinfo
;
1859 char tmp_shm_path
[PATH_MAX
];
1861 /* Get date and time for unique app path */
1863 timeinfo
= localtime(&rawtime
);
1864 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1866 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1868 ua_sess
->tracing_id
= usess
->id
;
1869 ua_sess
->id
= get_next_session_id();
1870 ua_sess
->uid
= app
->uid
;
1871 ua_sess
->gid
= app
->gid
;
1872 ua_sess
->euid
= usess
->uid
;
1873 ua_sess
->egid
= usess
->gid
;
1874 ua_sess
->buffer_type
= usess
->buffer_type
;
1875 ua_sess
->bits_per_long
= app
->bits_per_long
;
1877 /* There is only one consumer object per session possible. */
1878 consumer_output_get(usess
->consumer
);
1879 ua_sess
->consumer
= usess
->consumer
;
1881 ua_sess
->output_traces
= usess
->output_traces
;
1882 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1883 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1884 &usess
->metadata_attr
);
1886 switch (ua_sess
->buffer_type
) {
1887 case LTTNG_BUFFER_PER_PID
:
1888 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1889 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1892 case LTTNG_BUFFER_PER_UID
:
1893 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1894 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1901 PERROR("asprintf UST shadow copy session");
1906 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1907 sizeof(ua_sess
->root_shm_path
));
1908 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1909 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1910 sizeof(ua_sess
->shm_path
));
1911 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1912 if (ua_sess
->shm_path
[0]) {
1913 switch (ua_sess
->buffer_type
) {
1914 case LTTNG_BUFFER_PER_PID
:
1915 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1916 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1917 app
->name
, app
->pid
, datetime
);
1919 case LTTNG_BUFFER_PER_UID
:
1920 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1921 DEFAULT_UST_TRACE_UID_PATH
,
1922 app
->uid
, app
->bits_per_long
);
1929 PERROR("sprintf UST shadow copy session");
1933 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1934 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1935 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1938 /* Iterate over all channels in global domain. */
1939 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1941 struct lttng_ht_iter uiter
;
1943 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1944 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1945 if (ua_chan_node
!= NULL
) {
1946 /* Session exist. Contiuing. */
1950 DBG2("Channel %s not found on shadow session copy, creating it",
1952 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1954 if (ua_chan
== NULL
) {
1955 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1958 shadow_copy_channel(ua_chan
, uchan
);
1960 * The concept of metadata channel does not exist on the tracing
1961 * registry side of the session daemon so this can only be a per CPU
1962 * channel and not metadata.
1964 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1966 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1971 consumer_output_put(ua_sess
->consumer
);
1975 * Lookup sesison wrapper.
1978 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1979 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1981 /* Get right UST app session from app */
1982 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1986 * Return ust app session from the app session hashtable using the UST session
1989 static struct ust_app_session
*lookup_session_by_app(
1990 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1992 struct lttng_ht_iter iter
;
1993 struct lttng_ht_node_u64
*node
;
1995 __lookup_session_by_app(usess
, app
, &iter
);
1996 node
= lttng_ht_iter_get_node_u64(&iter
);
2001 return caa_container_of(node
, struct ust_app_session
, node
);
2008 * Setup buffer registry per PID for the given session and application. If none
2009 * is found, a new one is created, added to the global registry and
2010 * initialized. If regp is valid, it's set with the newly created object.
2012 * Return 0 on success or else a negative value.
2014 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2015 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2018 struct buffer_reg_pid
*reg_pid
;
2025 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2028 * This is the create channel path meaning that if there is NO
2029 * registry available, we have to create one for this session.
2031 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2032 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2040 /* Initialize registry. */
2041 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2042 app
->bits_per_long
, app
->uint8_t_alignment
,
2043 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2044 app
->uint64_t_alignment
, app
->long_alignment
,
2045 app
->byte_order
, app
->version
.major
,
2046 app
->version
.minor
, reg_pid
->root_shm_path
,
2048 ua_sess
->euid
, ua_sess
->egid
);
2051 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2052 * destroy the buffer registry, because it is always expected
2053 * that if the buffer registry can be found, its ust registry is
2056 buffer_reg_pid_destroy(reg_pid
);
2060 buffer_reg_pid_add(reg_pid
);
2062 DBG3("UST app buffer registry per PID created successfully");
2074 * Setup buffer registry per UID for the given session and application. If none
2075 * is found, a new one is created, added to the global registry and
2076 * initialized. If regp is valid, it's set with the newly created object.
2078 * Return 0 on success or else a negative value.
2080 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2081 struct ust_app_session
*ua_sess
,
2082 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2085 struct buffer_reg_uid
*reg_uid
;
2092 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2095 * This is the create channel path meaning that if there is NO
2096 * registry available, we have to create one for this session.
2098 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2099 LTTNG_DOMAIN_UST
, ®_uid
,
2100 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2108 /* Initialize registry. */
2109 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2110 app
->bits_per_long
, app
->uint8_t_alignment
,
2111 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2112 app
->uint64_t_alignment
, app
->long_alignment
,
2113 app
->byte_order
, app
->version
.major
,
2114 app
->version
.minor
, reg_uid
->root_shm_path
,
2115 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2118 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2119 * destroy the buffer registry, because it is always expected
2120 * that if the buffer registry can be found, its ust registry is
2123 buffer_reg_uid_destroy(reg_uid
, NULL
);
2126 /* Add node to teardown list of the session. */
2127 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2129 buffer_reg_uid_add(reg_uid
);
2131 DBG3("UST app buffer registry per UID created successfully");
2142 * Create a session on the tracer side for the given app.
2144 * On success, ua_sess_ptr is populated with the session pointer or else left
2145 * untouched. If the session was created, is_created is set to 1. On error,
2146 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2149 * Returns 0 on success or else a negative code which is either -ENOMEM or
2150 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2152 static int create_ust_app_session(struct ltt_ust_session
*usess
,
2153 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2156 int ret
, created
= 0;
2157 struct ust_app_session
*ua_sess
;
2161 assert(ua_sess_ptr
);
2163 health_code_update();
2165 ua_sess
= lookup_session_by_app(usess
, app
);
2166 if (ua_sess
== NULL
) {
2167 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2168 app
->pid
, usess
->id
);
2169 ua_sess
= alloc_ust_app_session(app
);
2170 if (ua_sess
== NULL
) {
2171 /* Only malloc can failed so something is really wrong */
2175 shadow_copy_session(ua_sess
, usess
, app
);
2179 switch (usess
->buffer_type
) {
2180 case LTTNG_BUFFER_PER_PID
:
2181 /* Init local registry. */
2182 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2184 delete_ust_app_session(-1, ua_sess
, app
);
2188 case LTTNG_BUFFER_PER_UID
:
2189 /* Look for a global registry. If none exists, create one. */
2190 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2192 delete_ust_app_session(-1, ua_sess
, app
);
2202 health_code_update();
2204 if (ua_sess
->handle
== -1) {
2205 pthread_mutex_lock(&app
->sock_lock
);
2206 ret
= ustctl_create_session(app
->sock
);
2207 pthread_mutex_unlock(&app
->sock_lock
);
2209 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2210 ERR("Creating session for app pid %d with ret %d",
2213 DBG("UST app creating session failed. Application is dead");
2215 * This is normal behavior, an application can die during the
2216 * creation process. Don't report an error so the execution can
2217 * continue normally. This will get flagged ENOTCONN and the
2218 * caller will handle it.
2222 delete_ust_app_session(-1, ua_sess
, app
);
2223 if (ret
!= -ENOMEM
) {
2225 * Tracer is probably gone or got an internal error so let's
2226 * behave like it will soon unregister or not usable.
2233 ua_sess
->handle
= ret
;
2235 /* Add ust app session to app's HT */
2236 lttng_ht_node_init_u64(&ua_sess
->node
,
2237 ua_sess
->tracing_id
);
2238 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2239 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2240 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2241 &ua_sess
->ust_objd_node
);
2243 DBG2("UST app session created successfully with handle %d", ret
);
2246 *ua_sess_ptr
= ua_sess
;
2248 *is_created
= created
;
2251 /* Everything went well. */
2255 health_code_update();
2260 * Match function for a hash table lookup of ust_app_ctx.
2262 * It matches an ust app context based on the context type and, in the case
2263 * of perf counters, their name.
2265 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2267 struct ust_app_ctx
*ctx
;
2268 const struct lttng_ust_context_attr
*key
;
2273 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2277 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2282 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2283 if (strncmp(key
->u
.perf_counter
.name
,
2284 ctx
->ctx
.u
.perf_counter
.name
,
2285 sizeof(key
->u
.perf_counter
.name
))) {
2289 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2290 if (strcmp(key
->u
.app_ctx
.provider_name
,
2291 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2292 strcmp(key
->u
.app_ctx
.ctx_name
,
2293 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2309 * Lookup for an ust app context from an lttng_ust_context.
2311 * Must be called while holding RCU read side lock.
2312 * Return an ust_app_ctx object or NULL on error.
2315 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2316 struct lttng_ust_context_attr
*uctx
)
2318 struct lttng_ht_iter iter
;
2319 struct lttng_ht_node_ulong
*node
;
2320 struct ust_app_ctx
*app_ctx
= NULL
;
2325 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2326 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2327 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2328 node
= lttng_ht_iter_get_node_ulong(&iter
);
2333 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2340 * Create a context for the channel on the tracer.
2342 * Called with UST app session lock held and a RCU read side lock.
2345 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2346 struct ust_app_channel
*ua_chan
,
2347 struct lttng_ust_context_attr
*uctx
,
2348 struct ust_app
*app
)
2351 struct ust_app_ctx
*ua_ctx
;
2353 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2355 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2361 ua_ctx
= alloc_ust_app_ctx(uctx
);
2362 if (ua_ctx
== NULL
) {
2368 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2369 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2370 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2372 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2382 * Enable on the tracer side a ust app event for the session and channel.
2384 * Called with UST app session lock held.
2387 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2388 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2392 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2397 ua_event
->enabled
= 1;
2404 * Disable on the tracer side a ust app event for the session and channel.
2406 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2407 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2411 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2416 ua_event
->enabled
= 0;
2423 * Lookup ust app channel for session and disable it on the tracer side.
2426 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2427 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2431 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2436 ua_chan
->enabled
= 0;
2443 * Lookup ust app channel for session and enable it on the tracer side. This
2444 * MUST be called with a RCU read side lock acquired.
2446 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2447 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2450 struct lttng_ht_iter iter
;
2451 struct lttng_ht_node_str
*ua_chan_node
;
2452 struct ust_app_channel
*ua_chan
;
2454 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2455 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2456 if (ua_chan_node
== NULL
) {
2457 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2458 uchan
->name
, ua_sess
->tracing_id
);
2462 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2464 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2474 * Ask the consumer to create a channel and get it if successful.
2476 * Called with UST app session lock held.
2478 * Return 0 on success or else a negative value.
2480 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2481 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2482 int bitness
, struct ust_registry_session
*registry
)
2485 unsigned int nb_fd
= 0;
2486 struct consumer_socket
*socket
;
2494 health_code_update();
2496 /* Get the right consumer socket for the application. */
2497 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2503 health_code_update();
2505 /* Need one fd for the channel. */
2506 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2508 ERR("Exhausted number of available FD upon create channel");
2513 * Ask consumer to create channel. The consumer will return the number of
2514 * stream we have to expect.
2516 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2523 * Compute the number of fd needed before receiving them. It must be 2 per
2524 * stream (2 being the default value here).
2526 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2528 /* Reserve the amount of file descriptor we need. */
2529 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2531 ERR("Exhausted number of available FD upon create channel");
2532 goto error_fd_get_stream
;
2535 health_code_update();
2538 * Now get the channel from the consumer. This call wil populate the stream
2539 * list of that channel and set the ust objects.
2541 if (usess
->consumer
->enabled
) {
2542 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2552 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2553 error_fd_get_stream
:
2555 * Initiate a destroy channel on the consumer since we had an error
2556 * handling it on our side. The return value is of no importance since we
2557 * already have a ret value set by the previous error that we need to
2560 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2562 lttng_fd_put(LTTNG_FD_APPS
, 1);
2564 health_code_update();
2570 * Duplicate the ust data object of the ust app stream and save it in the
2571 * buffer registry stream.
2573 * Return 0 on success or else a negative value.
2575 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2576 struct ust_app_stream
*stream
)
2583 /* Reserve the amount of file descriptor we need. */
2584 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2586 ERR("Exhausted number of available FD upon duplicate stream");
2590 /* Duplicate object for stream once the original is in the registry. */
2591 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2592 reg_stream
->obj
.ust
);
2594 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2595 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2596 lttng_fd_put(LTTNG_FD_APPS
, 2);
2599 stream
->handle
= stream
->obj
->handle
;
2606 * Duplicate the ust data object of the ust app. channel and save it in the
2607 * buffer registry channel.
2609 * Return 0 on success or else a negative value.
2611 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2612 struct ust_app_channel
*ua_chan
)
2619 /* Need two fds for the channel. */
2620 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2622 ERR("Exhausted number of available FD upon duplicate channel");
2626 /* Duplicate object for stream once the original is in the registry. */
2627 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2629 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2630 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2633 ua_chan
->handle
= ua_chan
->obj
->handle
;
2638 lttng_fd_put(LTTNG_FD_APPS
, 1);
2644 * For a given channel buffer registry, setup all streams of the given ust
2645 * application channel.
2647 * Return 0 on success or else a negative value.
2649 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2650 struct ust_app_channel
*ua_chan
,
2651 struct ust_app
*app
)
2654 struct ust_app_stream
*stream
, *stmp
;
2659 DBG2("UST app setup buffer registry stream");
2661 /* Send all streams to application. */
2662 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2663 struct buffer_reg_stream
*reg_stream
;
2665 ret
= buffer_reg_stream_create(®_stream
);
2671 * Keep original pointer and nullify it in the stream so the delete
2672 * stream call does not release the object.
2674 reg_stream
->obj
.ust
= stream
->obj
;
2676 buffer_reg_stream_add(reg_stream
, reg_chan
);
2678 /* We don't need the streams anymore. */
2679 cds_list_del(&stream
->list
);
2680 delete_ust_app_stream(-1, stream
, app
);
2688 * Create a buffer registry channel for the given session registry and
2689 * application channel object. If regp pointer is valid, it's set with the
2690 * created object. Important, the created object is NOT added to the session
2691 * registry hash table.
2693 * Return 0 on success else a negative value.
2695 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2696 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2699 struct buffer_reg_channel
*reg_chan
= NULL
;
2704 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2706 /* Create buffer registry channel. */
2707 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2712 reg_chan
->consumer_key
= ua_chan
->key
;
2713 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2714 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2716 /* Create and add a channel registry to session. */
2717 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2718 ua_chan
->tracing_channel_id
);
2722 buffer_reg_channel_add(reg_sess
, reg_chan
);
2731 /* Safe because the registry channel object was not added to any HT. */
2732 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2738 * Setup buffer registry channel for the given session registry and application
2739 * channel object. If regp pointer is valid, it's set with the created object.
2741 * Return 0 on success else a negative value.
2743 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2744 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2745 struct ust_app
*app
)
2752 assert(ua_chan
->obj
);
2754 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2756 /* Setup all streams for the registry. */
2757 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2762 reg_chan
->obj
.ust
= ua_chan
->obj
;
2763 ua_chan
->obj
= NULL
;
2768 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2769 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2774 * Send buffer registry channel to the application.
2776 * Return 0 on success else a negative value.
2778 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2779 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2780 struct ust_app_channel
*ua_chan
)
2783 struct buffer_reg_stream
*reg_stream
;
2790 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2792 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2797 /* Send channel to the application. */
2798 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2799 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2800 ret
= -ENOTCONN
; /* Caused by app exiting. */
2802 } else if (ret
< 0) {
2806 health_code_update();
2808 /* Send all streams to application. */
2809 pthread_mutex_lock(®_chan
->stream_list_lock
);
2810 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2811 struct ust_app_stream stream
;
2813 ret
= duplicate_stream_object(reg_stream
, &stream
);
2815 goto error_stream_unlock
;
2818 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2820 (void) release_ust_app_stream(-1, &stream
, app
);
2821 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2822 ret
= -ENOTCONN
; /* Caused by app exiting. */
2824 goto error_stream_unlock
;
2828 * The return value is not important here. This function will output an
2831 (void) release_ust_app_stream(-1, &stream
, app
);
2833 ua_chan
->is_sent
= 1;
2835 error_stream_unlock
:
2836 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2842 * Create and send to the application the created buffers with per UID buffers.
2844 * Return 0 on success else a negative value.
2846 static int create_channel_per_uid(struct ust_app
*app
,
2847 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2848 struct ust_app_channel
*ua_chan
)
2851 struct buffer_reg_uid
*reg_uid
;
2852 struct buffer_reg_channel
*reg_chan
;
2853 bool created
= false;
2860 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2862 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2864 * The session creation handles the creation of this global registry
2865 * object. If none can be find, there is a code flow problem or a
2870 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2873 /* Create the buffer registry channel object. */
2874 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2876 ERR("Error creating the UST channel \"%s\" registry instance",
2883 * Create the buffers on the consumer side. This call populates the
2884 * ust app channel object with all streams and data object.
2886 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2887 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2889 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2893 * Let's remove the previously created buffer registry channel so
2894 * it's not visible anymore in the session registry.
2896 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2897 ua_chan
->tracing_channel_id
, false);
2898 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2899 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2904 * Setup the streams and add it to the session registry.
2906 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2907 ua_chan
, reg_chan
, app
);
2909 ERR("Error setting up UST channel \"%s\"",
2916 /* Send buffers to the application. */
2917 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2919 if (ret
!= -ENOTCONN
) {
2920 ERR("Error sending channel to application");
2926 enum lttng_error_code cmd_ret
;
2927 struct ltt_session
*session
;
2928 uint64_t chan_reg_key
;
2929 struct ust_registry_channel
*chan_reg
;
2932 chan_reg_key
= ua_chan
->tracing_channel_id
;
2934 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2935 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2938 chan_reg
->consumer_key
= ua_chan
->key
;
2940 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2942 session
= session_find_by_id(ua_sess
->tracing_id
);
2945 cmd_ret
= notification_thread_command_add_channel(
2946 notification_thread_handle
, session
->name
,
2947 ua_sess
->euid
, ua_sess
->egid
,
2951 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2953 if (cmd_ret
!= LTTNG_OK
) {
2954 ret
= - (int) cmd_ret
;
2955 ERR("Failed to add channel to notification thread");
2965 * Create and send to the application the created buffers with per PID buffers.
2967 * Called with UST app session lock held.
2969 * Return 0 on success else a negative value.
2971 static int create_channel_per_pid(struct ust_app
*app
,
2972 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2973 struct ust_app_channel
*ua_chan
)
2976 struct ust_registry_session
*registry
;
2977 enum lttng_error_code cmd_ret
;
2978 struct ltt_session
*session
;
2979 uint64_t chan_reg_key
;
2980 struct ust_registry_channel
*chan_reg
;
2987 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2991 registry
= get_session_registry(ua_sess
);
2992 /* The UST app session lock is held, registry shall not be null. */
2995 /* Create and add a new channel registry to session. */
2996 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2998 ERR("Error creating the UST channel \"%s\" registry instance",
3003 /* Create and get channel on the consumer side. */
3004 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3005 app
->bits_per_long
, registry
);
3007 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3012 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3014 if (ret
!= -ENOTCONN
) {
3015 ERR("Error sending channel to application");
3020 session
= session_find_by_id(ua_sess
->tracing_id
);
3023 chan_reg_key
= ua_chan
->key
;
3024 pthread_mutex_lock(®istry
->lock
);
3025 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3027 chan_reg
->consumer_key
= ua_chan
->key
;
3028 pthread_mutex_unlock(®istry
->lock
);
3030 cmd_ret
= notification_thread_command_add_channel(
3031 notification_thread_handle
, session
->name
,
3032 ua_sess
->euid
, ua_sess
->egid
,
3036 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3037 if (cmd_ret
!= LTTNG_OK
) {
3038 ret
= - (int) cmd_ret
;
3039 ERR("Failed to add channel to notification thread");
3049 * From an already allocated ust app channel, create the channel buffers if
3050 * need and send it to the application. This MUST be called with a RCU read
3051 * side lock acquired.
3053 * Called with UST app session lock held.
3055 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3056 * the application exited concurrently.
3058 static int do_create_channel(struct ust_app
*app
,
3059 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3060 struct ust_app_channel
*ua_chan
)
3069 /* Handle buffer type before sending the channel to the application. */
3070 switch (usess
->buffer_type
) {
3071 case LTTNG_BUFFER_PER_UID
:
3073 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3079 case LTTNG_BUFFER_PER_PID
:
3081 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3093 /* Initialize ust objd object using the received handle and add it. */
3094 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3095 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3097 /* If channel is not enabled, disable it on the tracer */
3098 if (!ua_chan
->enabled
) {
3099 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3110 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3111 * newly created channel if not NULL.
3113 * Called with UST app session lock and RCU read-side lock held.
3115 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3116 * the application exited concurrently.
3118 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3119 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3120 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3121 struct ust_app_channel
**ua_chanp
)
3124 struct lttng_ht_iter iter
;
3125 struct lttng_ht_node_str
*ua_chan_node
;
3126 struct ust_app_channel
*ua_chan
;
3128 /* Lookup channel in the ust app session */
3129 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3130 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3131 if (ua_chan_node
!= NULL
) {
3132 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3136 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3137 if (ua_chan
== NULL
) {
3138 /* Only malloc can fail here */
3142 shadow_copy_channel(ua_chan
, uchan
);
3144 /* Set channel type. */
3145 ua_chan
->attr
.type
= type
;
3147 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3152 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3155 /* Only add the channel if successful on the tracer side. */
3156 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3159 *ua_chanp
= ua_chan
;
3162 /* Everything went well. */
3166 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3172 * Create UST app event and create it on the tracer side.
3174 * Called with ust app session mutex held.
3177 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3178 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3179 struct ust_app
*app
)
3182 struct ust_app_event
*ua_event
;
3184 /* Get event node */
3185 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3186 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3187 if (ua_event
!= NULL
) {
3192 /* Does not exist so create one */
3193 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3194 if (ua_event
== NULL
) {
3195 /* Only malloc can failed so something is really wrong */
3199 shadow_copy_event(ua_event
, uevent
);
3201 /* Create it on the tracer side */
3202 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3204 /* Not found previously means that it does not exist on the tracer */
3205 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3209 add_unique_ust_app_event(ua_chan
, ua_event
);
3211 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3218 /* Valid. Calling here is already in a read side lock */
3219 delete_ust_app_event(-1, ua_event
, app
);
3224 * Create UST metadata and open it on the tracer side.
3226 * Called with UST app session lock held and RCU read side lock.
3228 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3229 struct ust_app
*app
, struct consumer_output
*consumer
)
3232 struct ust_app_channel
*metadata
;
3233 struct consumer_socket
*socket
;
3234 struct ust_registry_session
*registry
;
3240 registry
= get_session_registry(ua_sess
);
3241 /* The UST app session is held registry shall not be null. */
3244 pthread_mutex_lock(®istry
->lock
);
3246 /* Metadata already exists for this registry or it was closed previously */
3247 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3252 /* Allocate UST metadata */
3253 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3255 /* malloc() failed */
3260 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3262 /* Need one fd for the channel. */
3263 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3265 ERR("Exhausted number of available FD upon create metadata");
3269 /* Get the right consumer socket for the application. */
3270 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3273 goto error_consumer
;
3277 * Keep metadata key so we can identify it on the consumer side. Assign it
3278 * to the registry *before* we ask the consumer so we avoid the race of the
3279 * consumer requesting the metadata and the ask_channel call on our side
3280 * did not returned yet.
3282 registry
->metadata_key
= metadata
->key
;
3285 * Ask the metadata channel creation to the consumer. The metadata object
3286 * will be created by the consumer and kept their. However, the stream is
3287 * never added or monitored until we do a first push metadata to the
3290 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3293 /* Nullify the metadata key so we don't try to close it later on. */
3294 registry
->metadata_key
= 0;
3295 goto error_consumer
;
3299 * The setup command will make the metadata stream be sent to the relayd,
3300 * if applicable, and the thread managing the metadatas. This is important
3301 * because after this point, if an error occurs, the only way the stream
3302 * can be deleted is to be monitored in the consumer.
3304 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3306 /* Nullify the metadata key so we don't try to close it later on. */
3307 registry
->metadata_key
= 0;
3308 goto error_consumer
;
3311 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3312 metadata
->key
, app
->pid
);
3315 lttng_fd_put(LTTNG_FD_APPS
, 1);
3316 delete_ust_app_channel(-1, metadata
, app
);
3318 pthread_mutex_unlock(®istry
->lock
);
3323 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3324 * acquired before calling this function.
3326 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3328 struct ust_app
*app
= NULL
;
3329 struct lttng_ht_node_ulong
*node
;
3330 struct lttng_ht_iter iter
;
3332 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3333 node
= lttng_ht_iter_get_node_ulong(&iter
);
3335 DBG2("UST app no found with pid %d", pid
);
3339 DBG2("Found UST app by pid %d", pid
);
3341 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3348 * Allocate and init an UST app object using the registration information and
3349 * the command socket. This is called when the command socket connects to the
3352 * The object is returned on success or else NULL.
3354 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3356 struct ust_app
*lta
= NULL
;
3361 DBG3("UST app creating application for socket %d", sock
);
3363 if ((msg
->bits_per_long
== 64 &&
3364 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3365 || (msg
->bits_per_long
== 32 &&
3366 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3367 ERR("Registration failed: application \"%s\" (pid: %d) has "
3368 "%d-bit long, but no consumerd for this size is available.\n",
3369 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3373 lta
= zmalloc(sizeof(struct ust_app
));
3379 lta
->ppid
= msg
->ppid
;
3380 lta
->uid
= msg
->uid
;
3381 lta
->gid
= msg
->gid
;
3383 lta
->bits_per_long
= msg
->bits_per_long
;
3384 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3385 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3386 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3387 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3388 lta
->long_alignment
= msg
->long_alignment
;
3389 lta
->byte_order
= msg
->byte_order
;
3391 lta
->v_major
= msg
->major
;
3392 lta
->v_minor
= msg
->minor
;
3393 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3394 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3395 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3396 lta
->notify_sock
= -1;
3398 /* Copy name and make sure it's NULL terminated. */
3399 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3400 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3403 * Before this can be called, when receiving the registration information,
3404 * the application compatibility is checked. So, at this point, the
3405 * application can work with this session daemon.
3407 lta
->compatible
= 1;
3409 lta
->pid
= msg
->pid
;
3410 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3412 pthread_mutex_init(<a
->sock_lock
, NULL
);
3413 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3415 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3421 * For a given application object, add it to every hash table.
3423 void ust_app_add(struct ust_app
*app
)
3426 assert(app
->notify_sock
>= 0);
3431 * On a re-registration, we want to kick out the previous registration of
3434 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3437 * The socket _should_ be unique until _we_ call close. So, a add_unique
3438 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3439 * already in the table.
3441 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3443 /* Add application to the notify socket hash table. */
3444 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3445 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3447 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3448 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3449 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3456 * Set the application version into the object.
3458 * Return 0 on success else a negative value either an errno code or a
3459 * LTTng-UST error code.
3461 int ust_app_version(struct ust_app
*app
)
3467 pthread_mutex_lock(&app
->sock_lock
);
3468 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3469 pthread_mutex_unlock(&app
->sock_lock
);
3471 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3472 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3474 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3482 * Unregister app by removing it from the global traceable app list and freeing
3485 * The socket is already closed at this point so no close to sock.
3487 void ust_app_unregister(int sock
)
3489 struct ust_app
*lta
;
3490 struct lttng_ht_node_ulong
*node
;
3491 struct lttng_ht_iter ust_app_sock_iter
;
3492 struct lttng_ht_iter iter
;
3493 struct ust_app_session
*ua_sess
;
3498 /* Get the node reference for a call_rcu */
3499 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3500 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3503 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3504 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3507 * For per-PID buffers, perform "push metadata" and flush all
3508 * application streams before removing app from hash tables,
3509 * ensuring proper behavior of data_pending check.
3510 * Remove sessions so they are not visible during deletion.
3512 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3514 struct ust_registry_session
*registry
;
3516 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3518 /* The session was already removed so scheduled for teardown. */
3522 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3523 (void) ust_app_flush_app_session(lta
, ua_sess
);
3527 * Add session to list for teardown. This is safe since at this point we
3528 * are the only one using this list.
3530 pthread_mutex_lock(&ua_sess
->lock
);
3532 if (ua_sess
->deleted
) {
3533 pthread_mutex_unlock(&ua_sess
->lock
);
3538 * Normally, this is done in the delete session process which is
3539 * executed in the call rcu below. However, upon registration we can't
3540 * afford to wait for the grace period before pushing data or else the
3541 * data pending feature can race between the unregistration and stop
3542 * command where the data pending command is sent *before* the grace
3545 * The close metadata below nullifies the metadata pointer in the
3546 * session so the delete session will NOT push/close a second time.
3548 registry
= get_session_registry(ua_sess
);
3550 /* Push metadata for application before freeing the application. */
3551 (void) push_metadata(registry
, ua_sess
->consumer
);
3554 * Don't ask to close metadata for global per UID buffers. Close
3555 * metadata only on destroy trace session in this case. Also, the
3556 * previous push metadata could have flag the metadata registry to
3557 * close so don't send a close command if closed.
3559 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3560 /* And ask to close it for this session registry. */
3561 (void) close_metadata(registry
, ua_sess
->consumer
);
3564 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3566 pthread_mutex_unlock(&ua_sess
->lock
);
3569 /* Remove application from PID hash table */
3570 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3574 * Remove application from notify hash table. The thread handling the
3575 * notify socket could have deleted the node so ignore on error because
3576 * either way it's valid. The close of that socket is handled by the other
3579 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3580 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3583 * Ignore return value since the node might have been removed before by an
3584 * add replace during app registration because the PID can be reassigned by
3587 iter
.iter
.node
= <a
->pid_n
.node
;
3588 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3590 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3595 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3602 * Fill events array with all events name of all registered apps.
3604 int ust_app_list_events(struct lttng_event
**events
)
3607 size_t nbmem
, count
= 0;
3608 struct lttng_ht_iter iter
;
3609 struct ust_app
*app
;
3610 struct lttng_event
*tmp_event
;
3612 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3613 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3614 if (tmp_event
== NULL
) {
3615 PERROR("zmalloc ust app events");
3622 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3623 struct lttng_ust_tracepoint_iter uiter
;
3625 health_code_update();
3627 if (!app
->compatible
) {
3629 * TODO: In time, we should notice the caller of this error by
3630 * telling him that this is a version error.
3634 pthread_mutex_lock(&app
->sock_lock
);
3635 handle
= ustctl_tracepoint_list(app
->sock
);
3637 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3638 ERR("UST app list events getting handle failed for app pid %d",
3641 pthread_mutex_unlock(&app
->sock_lock
);
3645 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3646 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3647 /* Handle ustctl error. */
3651 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3652 ERR("UST app tp list get failed for app %d with ret %d",
3655 DBG3("UST app tp list get failed. Application is dead");
3657 * This is normal behavior, an application can die during the
3658 * creation process. Don't report an error so the execution can
3659 * continue normally. Continue normal execution.
3664 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3665 if (release_ret
< 0 &&
3666 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3667 release_ret
!= -EPIPE
) {
3668 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3670 pthread_mutex_unlock(&app
->sock_lock
);
3674 health_code_update();
3675 if (count
>= nbmem
) {
3676 /* In case the realloc fails, we free the memory */
3677 struct lttng_event
*new_tmp_event
;
3680 new_nbmem
= nbmem
<< 1;
3681 DBG2("Reallocating event list from %zu to %zu entries",
3683 new_tmp_event
= realloc(tmp_event
,
3684 new_nbmem
* sizeof(struct lttng_event
));
3685 if (new_tmp_event
== NULL
) {
3688 PERROR("realloc ust app events");
3691 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3692 if (release_ret
< 0 &&
3693 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3694 release_ret
!= -EPIPE
) {
3695 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3697 pthread_mutex_unlock(&app
->sock_lock
);
3700 /* Zero the new memory */
3701 memset(new_tmp_event
+ nbmem
, 0,
3702 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3704 tmp_event
= new_tmp_event
;
3706 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3707 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3708 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3709 tmp_event
[count
].pid
= app
->pid
;
3710 tmp_event
[count
].enabled
= -1;
3713 ret
= ustctl_release_handle(app
->sock
, handle
);
3714 pthread_mutex_unlock(&app
->sock_lock
);
3715 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3716 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3721 *events
= tmp_event
;