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
);
817 /* Push metadata for application before freeing the application. */
818 (void) push_metadata(registry
, ua_sess
->consumer
);
821 * Don't ask to close metadata for global per UID buffers. Close
822 * metadata only on destroy trace session in this case. Also, the
823 * previous push metadata could have flag the metadata registry to
824 * close so don't send a close command if closed.
826 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
827 /* And ask to close it for this session registry. */
828 (void) close_metadata(registry
, ua_sess
->consumer
);
832 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
834 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
836 delete_ust_app_channel(sock
, ua_chan
, app
);
839 /* In case of per PID, the registry is kept in the session. */
840 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
841 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
843 buffer_reg_pid_remove(reg_pid
);
844 buffer_reg_pid_destroy(reg_pid
);
848 if (ua_sess
->handle
!= -1) {
849 pthread_mutex_lock(&app
->sock_lock
);
850 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
851 pthread_mutex_unlock(&app
->sock_lock
);
852 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
853 ERR("UST app sock %d release session handle failed with ret %d",
856 /* Remove session from application UST object descriptor. */
857 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
858 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
862 pthread_mutex_unlock(&ua_sess
->lock
);
864 consumer_output_put(ua_sess
->consumer
);
866 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
870 * Delete a traceable application structure from the global list. Never call
871 * this function outside of a call_rcu call.
873 * RCU read side lock should _NOT_ be held when calling this function.
876 void delete_ust_app(struct ust_app
*app
)
879 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
882 * The session list lock must be held during this function to guarantee
883 * the existence of ua_sess.
886 /* Delete ust app sessions info */
891 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
893 /* Free every object in the session and the session. */
895 delete_ust_app_session(sock
, ua_sess
, app
);
899 ht_cleanup_push(app
->sessions
);
900 ht_cleanup_push(app
->ust_sessions_objd
);
901 ht_cleanup_push(app
->ust_objd
);
904 * Wait until we have deleted the application from the sock hash table
905 * before closing this socket, otherwise an application could re-use the
906 * socket ID and race with the teardown, using the same hash table entry.
908 * It's OK to leave the close in call_rcu. We want it to stay unique for
909 * all RCU readers that could run concurrently with unregister app,
910 * therefore we _need_ to only close that socket after a grace period. So
911 * it should stay in this RCU callback.
913 * This close() is a very important step of the synchronization model so
914 * every modification to this function must be carefully reviewed.
920 lttng_fd_put(LTTNG_FD_APPS
, 1);
922 DBG2("UST app pid %d deleted", app
->pid
);
924 session_unlock_list();
928 * URCU intermediate call to delete an UST app.
931 void delete_ust_app_rcu(struct rcu_head
*head
)
933 struct lttng_ht_node_ulong
*node
=
934 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
935 struct ust_app
*app
=
936 caa_container_of(node
, struct ust_app
, pid_n
);
938 DBG3("Call RCU deleting app PID %d", app
->pid
);
943 * Delete the session from the application ht and delete the data structure by
944 * freeing every object inside and releasing them.
946 * The session list lock must be held by the caller.
948 static void destroy_app_session(struct ust_app
*app
,
949 struct ust_app_session
*ua_sess
)
952 struct lttng_ht_iter iter
;
957 iter
.iter
.node
= &ua_sess
->node
.node
;
958 ret
= lttng_ht_del(app
->sessions
, &iter
);
960 /* Already scheduled for teardown. */
964 /* Once deleted, free the data structure. */
965 delete_ust_app_session(app
->sock
, ua_sess
, app
);
972 * Alloc new UST app session.
975 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
977 struct ust_app_session
*ua_sess
;
979 /* Init most of the default value by allocating and zeroing */
980 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
981 if (ua_sess
== NULL
) {
986 ua_sess
->handle
= -1;
987 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
988 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
989 pthread_mutex_init(&ua_sess
->lock
, NULL
);
998 * Alloc new UST app channel.
1001 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1002 struct ust_app_session
*ua_sess
,
1003 struct lttng_ust_channel_attr
*attr
)
1005 struct ust_app_channel
*ua_chan
;
1007 /* Init most of the default value by allocating and zeroing */
1008 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1009 if (ua_chan
== NULL
) {
1014 /* Setup channel name */
1015 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1016 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1018 ua_chan
->enabled
= 1;
1019 ua_chan
->handle
= -1;
1020 ua_chan
->session
= ua_sess
;
1021 ua_chan
->key
= get_next_channel_key();
1022 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1023 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1024 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1026 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1027 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1029 /* Copy attributes */
1031 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1032 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1033 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1034 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1035 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1036 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1037 ua_chan
->attr
.output
= attr
->output
;
1039 /* By default, the channel is a per cpu channel. */
1040 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1042 DBG3("UST app channel %s allocated", ua_chan
->name
);
1051 * Allocate and initialize a UST app stream.
1053 * Return newly allocated stream pointer or NULL on error.
1055 struct ust_app_stream
*ust_app_alloc_stream(void)
1057 struct ust_app_stream
*stream
= NULL
;
1059 stream
= zmalloc(sizeof(*stream
));
1060 if (stream
== NULL
) {
1061 PERROR("zmalloc ust app stream");
1065 /* Zero could be a valid value for a handle so flag it to -1. */
1066 stream
->handle
= -1;
1073 * Alloc new UST app event.
1076 struct ust_app_event
*alloc_ust_app_event(char *name
,
1077 struct lttng_ust_event
*attr
)
1079 struct ust_app_event
*ua_event
;
1081 /* Init most of the default value by allocating and zeroing */
1082 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1083 if (ua_event
== NULL
) {
1088 ua_event
->enabled
= 1;
1089 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1090 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1091 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1093 /* Copy attributes */
1095 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1098 DBG3("UST app event %s allocated", ua_event
->name
);
1107 * Alloc new UST app context.
1110 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1112 struct ust_app_ctx
*ua_ctx
;
1114 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1115 if (ua_ctx
== NULL
) {
1119 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1122 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1123 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1124 char *provider_name
= NULL
, *ctx_name
= NULL
;
1126 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1127 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1128 if (!provider_name
|| !ctx_name
) {
1129 free(provider_name
);
1134 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1135 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1139 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1147 * Allocate a filter and copy the given original filter.
1149 * Return allocated filter or NULL on error.
1151 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1152 struct lttng_filter_bytecode
*orig_f
)
1154 struct lttng_filter_bytecode
*filter
= NULL
;
1156 /* Copy filter bytecode */
1157 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1159 PERROR("zmalloc alloc filter bytecode");
1163 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1170 * Create a liblttng-ust filter bytecode from given bytecode.
1172 * Return allocated filter or NULL on error.
1174 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1175 struct lttng_filter_bytecode
*orig_f
)
1177 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1179 /* Copy filter bytecode */
1180 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1182 PERROR("zmalloc alloc ust filter bytecode");
1186 assert(sizeof(struct lttng_filter_bytecode
) ==
1187 sizeof(struct lttng_ust_filter_bytecode
));
1188 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1194 * Find an ust_app using the sock and return it. RCU read side lock must be
1195 * held before calling this helper function.
1197 struct ust_app
*ust_app_find_by_sock(int sock
)
1199 struct lttng_ht_node_ulong
*node
;
1200 struct lttng_ht_iter iter
;
1202 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1203 node
= lttng_ht_iter_get_node_ulong(&iter
);
1205 DBG2("UST app find by sock %d not found", sock
);
1209 return caa_container_of(node
, struct ust_app
, sock_n
);
1216 * Find an ust_app using the notify sock and return it. RCU read side lock must
1217 * be held before calling this helper function.
1219 static struct ust_app
*find_app_by_notify_sock(int sock
)
1221 struct lttng_ht_node_ulong
*node
;
1222 struct lttng_ht_iter iter
;
1224 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1226 node
= lttng_ht_iter_get_node_ulong(&iter
);
1228 DBG2("UST app find by notify sock %d not found", sock
);
1232 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1239 * Lookup for an ust app event based on event name, filter bytecode and the
1242 * Return an ust_app_event object or NULL on error.
1244 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1245 char *name
, struct lttng_filter_bytecode
*filter
,
1247 const struct lttng_event_exclusion
*exclusion
)
1249 struct lttng_ht_iter iter
;
1250 struct lttng_ht_node_str
*node
;
1251 struct ust_app_event
*event
= NULL
;
1252 struct ust_app_ht_key key
;
1257 /* Setup key for event lookup. */
1259 key
.filter
= filter
;
1260 key
.loglevel_type
= loglevel_value
;
1261 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1262 key
.exclusion
= exclusion
;
1264 /* Lookup using the event name as hash and a custom match fct. */
1265 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1266 ht_match_ust_app_event
, &key
, &iter
.iter
);
1267 node
= lttng_ht_iter_get_node_str(&iter
);
1272 event
= caa_container_of(node
, struct ust_app_event
, node
);
1279 * Create the channel context on the tracer.
1281 * Called with UST app session lock held.
1284 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1285 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1289 health_code_update();
1291 pthread_mutex_lock(&app
->sock_lock
);
1292 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1293 ua_chan
->obj
, &ua_ctx
->obj
);
1294 pthread_mutex_unlock(&app
->sock_lock
);
1296 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1297 ERR("UST app create channel context failed for app (pid: %d) "
1298 "with ret %d", app
->pid
, ret
);
1301 * This is normal behavior, an application can die during the
1302 * creation process. Don't report an error so the execution can
1303 * continue normally.
1306 DBG3("UST app disable event failed. Application is dead.");
1311 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1313 DBG2("UST app context handle %d created successfully for channel %s",
1314 ua_ctx
->handle
, ua_chan
->name
);
1317 health_code_update();
1322 * Set the filter on the tracer.
1325 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1326 struct ust_app
*app
)
1329 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1331 health_code_update();
1333 if (!ua_event
->filter
) {
1338 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1339 if (!ust_bytecode
) {
1340 ret
= -LTTNG_ERR_NOMEM
;
1343 pthread_mutex_lock(&app
->sock_lock
);
1344 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1346 pthread_mutex_unlock(&app
->sock_lock
);
1348 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1349 ERR("UST app event %s filter failed for app (pid: %d) "
1350 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1353 * This is normal behavior, an application can die during the
1354 * creation process. Don't report an error so the execution can
1355 * continue normally.
1358 DBG3("UST app filter event failed. Application is dead.");
1363 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1366 health_code_update();
1372 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1373 struct lttng_event_exclusion
*exclusion
)
1375 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1376 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1377 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1379 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1380 if (!ust_exclusion
) {
1385 assert(sizeof(struct lttng_event_exclusion
) ==
1386 sizeof(struct lttng_ust_event_exclusion
));
1387 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1389 return ust_exclusion
;
1393 * Set event exclusions on the tracer.
1396 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1397 struct ust_app
*app
)
1400 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1402 health_code_update();
1404 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1409 ust_exclusion
= create_ust_exclusion_from_exclusion(
1410 ua_event
->exclusion
);
1411 if (!ust_exclusion
) {
1412 ret
= -LTTNG_ERR_NOMEM
;
1415 pthread_mutex_lock(&app
->sock_lock
);
1416 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1417 pthread_mutex_unlock(&app
->sock_lock
);
1419 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1420 ERR("UST app event %s exclusions failed for app (pid: %d) "
1421 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1424 * This is normal behavior, an application can die during the
1425 * creation process. Don't report an error so the execution can
1426 * continue normally.
1429 DBG3("UST app event exclusion failed. Application is dead.");
1434 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1437 health_code_update();
1438 free(ust_exclusion
);
1443 * Disable the specified event on to UST tracer for the UST session.
1445 static int disable_ust_event(struct ust_app
*app
,
1446 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1450 health_code_update();
1452 pthread_mutex_lock(&app
->sock_lock
);
1453 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1454 pthread_mutex_unlock(&app
->sock_lock
);
1456 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1457 ERR("UST app event %s disable failed for app (pid: %d) "
1458 "and session handle %d with ret %d",
1459 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1462 * This is normal behavior, an application can die during the
1463 * creation process. Don't report an error so the execution can
1464 * continue normally.
1467 DBG3("UST app disable event failed. Application is dead.");
1472 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1473 ua_event
->attr
.name
, app
->pid
);
1476 health_code_update();
1481 * Disable the specified channel on to UST tracer for the UST session.
1483 static int disable_ust_channel(struct ust_app
*app
,
1484 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1488 health_code_update();
1490 pthread_mutex_lock(&app
->sock_lock
);
1491 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1492 pthread_mutex_unlock(&app
->sock_lock
);
1494 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1495 ERR("UST app channel %s disable failed for app (pid: %d) "
1496 "and session handle %d with ret %d",
1497 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1500 * This is normal behavior, an application can die during the
1501 * creation process. Don't report an error so the execution can
1502 * continue normally.
1505 DBG3("UST app disable channel failed. Application is dead.");
1510 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1511 ua_chan
->name
, app
->pid
);
1514 health_code_update();
1519 * Enable the specified channel on to UST tracer for the UST session.
1521 static int enable_ust_channel(struct ust_app
*app
,
1522 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1526 health_code_update();
1528 pthread_mutex_lock(&app
->sock_lock
);
1529 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1530 pthread_mutex_unlock(&app
->sock_lock
);
1532 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1533 ERR("UST app channel %s enable failed for app (pid: %d) "
1534 "and session handle %d with ret %d",
1535 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1538 * This is normal behavior, an application can die during the
1539 * creation process. Don't report an error so the execution can
1540 * continue normally.
1543 DBG3("UST app enable channel failed. Application is dead.");
1548 ua_chan
->enabled
= 1;
1550 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1551 ua_chan
->name
, app
->pid
);
1554 health_code_update();
1559 * Enable the specified event on to UST tracer for the UST session.
1561 static int enable_ust_event(struct ust_app
*app
,
1562 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1566 health_code_update();
1568 pthread_mutex_lock(&app
->sock_lock
);
1569 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1570 pthread_mutex_unlock(&app
->sock_lock
);
1572 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1573 ERR("UST app event %s enable failed for app (pid: %d) "
1574 "and session handle %d with ret %d",
1575 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1578 * This is normal behavior, an application can die during the
1579 * creation process. Don't report an error so the execution can
1580 * continue normally.
1583 DBG3("UST app enable event failed. Application is dead.");
1588 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1589 ua_event
->attr
.name
, app
->pid
);
1592 health_code_update();
1597 * Send channel and stream buffer to application.
1599 * Return 0 on success. On error, a negative value is returned.
1601 static int send_channel_pid_to_ust(struct ust_app
*app
,
1602 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1605 struct ust_app_stream
*stream
, *stmp
;
1611 health_code_update();
1613 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1616 /* Send channel to the application. */
1617 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1618 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1619 ret
= -ENOTCONN
; /* Caused by app exiting. */
1621 } else if (ret
< 0) {
1625 health_code_update();
1627 /* Send all streams to application. */
1628 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1629 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1630 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1631 ret
= -ENOTCONN
; /* Caused by app exiting. */
1633 } else if (ret
< 0) {
1636 /* We don't need the stream anymore once sent to the tracer. */
1637 cds_list_del(&stream
->list
);
1638 delete_ust_app_stream(-1, stream
, app
);
1640 /* Flag the channel that it is sent to the application. */
1641 ua_chan
->is_sent
= 1;
1644 health_code_update();
1649 * Create the specified event onto the UST tracer for a UST session.
1651 * Should be called with session mutex held.
1654 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1655 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1659 health_code_update();
1661 /* Create UST event on tracer */
1662 pthread_mutex_lock(&app
->sock_lock
);
1663 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1665 pthread_mutex_unlock(&app
->sock_lock
);
1667 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1668 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1669 ua_event
->attr
.name
, app
->pid
, ret
);
1672 * This is normal behavior, an application can die during the
1673 * creation process. Don't report an error so the execution can
1674 * continue normally.
1677 DBG3("UST app create event failed. Application is dead.");
1682 ua_event
->handle
= ua_event
->obj
->handle
;
1684 DBG2("UST app event %s created successfully for pid:%d",
1685 ua_event
->attr
.name
, app
->pid
);
1687 health_code_update();
1689 /* Set filter if one is present. */
1690 if (ua_event
->filter
) {
1691 ret
= set_ust_event_filter(ua_event
, app
);
1697 /* Set exclusions for the event */
1698 if (ua_event
->exclusion
) {
1699 ret
= set_ust_event_exclusion(ua_event
, app
);
1705 /* If event not enabled, disable it on the tracer */
1706 if (ua_event
->enabled
) {
1708 * We now need to explicitly enable the event, since it
1709 * is now disabled at creation.
1711 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1714 * If we hit an EPERM, something is wrong with our enable call. If
1715 * we get an EEXIST, there is a problem on the tracer side since we
1719 case -LTTNG_UST_ERR_PERM
:
1720 /* Code flow problem */
1722 case -LTTNG_UST_ERR_EXIST
:
1723 /* It's OK for our use case. */
1734 health_code_update();
1739 * Copy data between an UST app event and a LTT event.
1741 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1742 struct ltt_ust_event
*uevent
)
1744 size_t exclusion_alloc_size
;
1746 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1747 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1749 ua_event
->enabled
= uevent
->enabled
;
1751 /* Copy event attributes */
1752 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1754 /* Copy filter bytecode */
1755 if (uevent
->filter
) {
1756 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1757 /* Filter might be NULL here in case of ENONEM. */
1760 /* Copy exclusion data */
1761 if (uevent
->exclusion
) {
1762 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1763 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1764 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1765 if (ua_event
->exclusion
== NULL
) {
1768 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1769 exclusion_alloc_size
);
1775 * Copy data between an UST app channel and a LTT channel.
1777 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1778 struct ltt_ust_channel
*uchan
)
1780 struct lttng_ht_iter iter
;
1781 struct ltt_ust_event
*uevent
;
1782 struct ltt_ust_context
*uctx
;
1783 struct ust_app_event
*ua_event
;
1785 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1787 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1788 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1790 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1791 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1793 /* Copy event attributes since the layout is different. */
1794 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1795 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1796 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1797 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1798 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1799 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1800 ua_chan
->attr
.output
= uchan
->attr
.output
;
1802 * Note that the attribute channel type is not set since the channel on the
1803 * tracing registry side does not have this information.
1806 ua_chan
->enabled
= uchan
->enabled
;
1807 ua_chan
->tracing_channel_id
= uchan
->id
;
1809 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1810 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1812 if (ua_ctx
== NULL
) {
1815 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1816 (unsigned long) ua_ctx
->ctx
.ctx
);
1817 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1818 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1821 /* Copy all events from ltt ust channel to ust app channel */
1822 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1823 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1824 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1825 if (ua_event
== NULL
) {
1826 DBG2("UST event %s not found on shadow copy channel",
1828 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1829 if (ua_event
== NULL
) {
1832 shadow_copy_event(ua_event
, uevent
);
1833 add_unique_ust_app_event(ua_chan
, ua_event
);
1837 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1841 * Copy data between a UST app session and a regular LTT session.
1843 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1844 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1846 struct lttng_ht_node_str
*ua_chan_node
;
1847 struct lttng_ht_iter iter
;
1848 struct ltt_ust_channel
*uchan
;
1849 struct ust_app_channel
*ua_chan
;
1851 struct tm
*timeinfo
;
1854 char tmp_shm_path
[PATH_MAX
];
1856 /* Get date and time for unique app path */
1858 timeinfo
= localtime(&rawtime
);
1859 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1861 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1863 ua_sess
->tracing_id
= usess
->id
;
1864 ua_sess
->id
= get_next_session_id();
1865 ua_sess
->uid
= app
->uid
;
1866 ua_sess
->gid
= app
->gid
;
1867 ua_sess
->euid
= usess
->uid
;
1868 ua_sess
->egid
= usess
->gid
;
1869 ua_sess
->buffer_type
= usess
->buffer_type
;
1870 ua_sess
->bits_per_long
= app
->bits_per_long
;
1872 /* There is only one consumer object per session possible. */
1873 consumer_output_get(usess
->consumer
);
1874 ua_sess
->consumer
= usess
->consumer
;
1876 ua_sess
->output_traces
= usess
->output_traces
;
1877 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1878 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1879 &usess
->metadata_attr
);
1881 switch (ua_sess
->buffer_type
) {
1882 case LTTNG_BUFFER_PER_PID
:
1883 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1884 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1887 case LTTNG_BUFFER_PER_UID
:
1888 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1889 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1896 PERROR("asprintf UST shadow copy session");
1901 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1902 sizeof(ua_sess
->root_shm_path
));
1903 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1904 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1905 sizeof(ua_sess
->shm_path
));
1906 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1907 if (ua_sess
->shm_path
[0]) {
1908 switch (ua_sess
->buffer_type
) {
1909 case LTTNG_BUFFER_PER_PID
:
1910 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1911 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1912 app
->name
, app
->pid
, datetime
);
1914 case LTTNG_BUFFER_PER_UID
:
1915 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1916 DEFAULT_UST_TRACE_UID_PATH
,
1917 app
->uid
, app
->bits_per_long
);
1924 PERROR("sprintf UST shadow copy session");
1928 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1929 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1930 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1933 /* Iterate over all channels in global domain. */
1934 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1936 struct lttng_ht_iter uiter
;
1938 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1939 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1940 if (ua_chan_node
!= NULL
) {
1941 /* Session exist. Contiuing. */
1945 DBG2("Channel %s not found on shadow session copy, creating it",
1947 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1949 if (ua_chan
== NULL
) {
1950 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1953 shadow_copy_channel(ua_chan
, uchan
);
1955 * The concept of metadata channel does not exist on the tracing
1956 * registry side of the session daemon so this can only be a per CPU
1957 * channel and not metadata.
1959 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1961 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1966 consumer_output_put(ua_sess
->consumer
);
1970 * Lookup sesison wrapper.
1973 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1974 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1976 /* Get right UST app session from app */
1977 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1981 * Return ust app session from the app session hashtable using the UST session
1984 static struct ust_app_session
*lookup_session_by_app(
1985 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1987 struct lttng_ht_iter iter
;
1988 struct lttng_ht_node_u64
*node
;
1990 __lookup_session_by_app(usess
, app
, &iter
);
1991 node
= lttng_ht_iter_get_node_u64(&iter
);
1996 return caa_container_of(node
, struct ust_app_session
, node
);
2003 * Setup buffer registry per PID for the given session and application. If none
2004 * is found, a new one is created, added to the global registry and
2005 * initialized. If regp is valid, it's set with the newly created object.
2007 * Return 0 on success or else a negative value.
2009 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2010 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2013 struct buffer_reg_pid
*reg_pid
;
2020 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2023 * This is the create channel path meaning that if there is NO
2024 * registry available, we have to create one for this session.
2026 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2027 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2035 /* Initialize registry. */
2036 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2037 app
->bits_per_long
, app
->uint8_t_alignment
,
2038 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2039 app
->uint64_t_alignment
, app
->long_alignment
,
2040 app
->byte_order
, app
->version
.major
,
2041 app
->version
.minor
, reg_pid
->root_shm_path
,
2043 ua_sess
->euid
, ua_sess
->egid
);
2046 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2047 * destroy the buffer registry, because it is always expected
2048 * that if the buffer registry can be found, its ust registry is
2051 buffer_reg_pid_destroy(reg_pid
);
2055 buffer_reg_pid_add(reg_pid
);
2057 DBG3("UST app buffer registry per PID created successfully");
2069 * Setup buffer registry per UID for the given session and application. If none
2070 * is found, a new one is created, added to the global registry and
2071 * initialized. If regp is valid, it's set with the newly created object.
2073 * Return 0 on success or else a negative value.
2075 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2076 struct ust_app_session
*ua_sess
,
2077 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2080 struct buffer_reg_uid
*reg_uid
;
2087 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2090 * This is the create channel path meaning that if there is NO
2091 * registry available, we have to create one for this session.
2093 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2094 LTTNG_DOMAIN_UST
, ®_uid
,
2095 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2103 /* Initialize registry. */
2104 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2105 app
->bits_per_long
, app
->uint8_t_alignment
,
2106 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2107 app
->uint64_t_alignment
, app
->long_alignment
,
2108 app
->byte_order
, app
->version
.major
,
2109 app
->version
.minor
, reg_uid
->root_shm_path
,
2110 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2113 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2114 * destroy the buffer registry, because it is always expected
2115 * that if the buffer registry can be found, its ust registry is
2118 buffer_reg_uid_destroy(reg_uid
, NULL
);
2121 /* Add node to teardown list of the session. */
2122 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2124 buffer_reg_uid_add(reg_uid
);
2126 DBG3("UST app buffer registry per UID created successfully");
2137 * Create a session on the tracer side for the given app.
2139 * On success, ua_sess_ptr is populated with the session pointer or else left
2140 * untouched. If the session was created, is_created is set to 1. On error,
2141 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2144 * Returns 0 on success or else a negative code which is either -ENOMEM or
2145 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2147 static int create_ust_app_session(struct ltt_ust_session
*usess
,
2148 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2151 int ret
, created
= 0;
2152 struct ust_app_session
*ua_sess
;
2156 assert(ua_sess_ptr
);
2158 health_code_update();
2160 ua_sess
= lookup_session_by_app(usess
, app
);
2161 if (ua_sess
== NULL
) {
2162 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2163 app
->pid
, usess
->id
);
2164 ua_sess
= alloc_ust_app_session(app
);
2165 if (ua_sess
== NULL
) {
2166 /* Only malloc can failed so something is really wrong */
2170 shadow_copy_session(ua_sess
, usess
, app
);
2174 switch (usess
->buffer_type
) {
2175 case LTTNG_BUFFER_PER_PID
:
2176 /* Init local registry. */
2177 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2179 delete_ust_app_session(-1, ua_sess
, app
);
2183 case LTTNG_BUFFER_PER_UID
:
2184 /* Look for a global registry. If none exists, create one. */
2185 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2187 delete_ust_app_session(-1, ua_sess
, app
);
2197 health_code_update();
2199 if (ua_sess
->handle
== -1) {
2200 pthread_mutex_lock(&app
->sock_lock
);
2201 ret
= ustctl_create_session(app
->sock
);
2202 pthread_mutex_unlock(&app
->sock_lock
);
2204 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2205 ERR("Creating session for app pid %d with ret %d",
2208 DBG("UST app creating session failed. Application is dead");
2210 * This is normal behavior, an application can die during the
2211 * creation process. Don't report an error so the execution can
2212 * continue normally. This will get flagged ENOTCONN and the
2213 * caller will handle it.
2217 delete_ust_app_session(-1, ua_sess
, app
);
2218 if (ret
!= -ENOMEM
) {
2220 * Tracer is probably gone or got an internal error so let's
2221 * behave like it will soon unregister or not usable.
2228 ua_sess
->handle
= ret
;
2230 /* Add ust app session to app's HT */
2231 lttng_ht_node_init_u64(&ua_sess
->node
,
2232 ua_sess
->tracing_id
);
2233 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2234 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2235 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2236 &ua_sess
->ust_objd_node
);
2238 DBG2("UST app session created successfully with handle %d", ret
);
2241 *ua_sess_ptr
= ua_sess
;
2243 *is_created
= created
;
2246 /* Everything went well. */
2250 health_code_update();
2255 * Match function for a hash table lookup of ust_app_ctx.
2257 * It matches an ust app context based on the context type and, in the case
2258 * of perf counters, their name.
2260 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2262 struct ust_app_ctx
*ctx
;
2263 const struct lttng_ust_context_attr
*key
;
2268 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2272 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2277 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2278 if (strncmp(key
->u
.perf_counter
.name
,
2279 ctx
->ctx
.u
.perf_counter
.name
,
2280 sizeof(key
->u
.perf_counter
.name
))) {
2284 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2285 if (strcmp(key
->u
.app_ctx
.provider_name
,
2286 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2287 strcmp(key
->u
.app_ctx
.ctx_name
,
2288 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2304 * Lookup for an ust app context from an lttng_ust_context.
2306 * Must be called while holding RCU read side lock.
2307 * Return an ust_app_ctx object or NULL on error.
2310 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2311 struct lttng_ust_context_attr
*uctx
)
2313 struct lttng_ht_iter iter
;
2314 struct lttng_ht_node_ulong
*node
;
2315 struct ust_app_ctx
*app_ctx
= NULL
;
2320 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2321 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2322 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2323 node
= lttng_ht_iter_get_node_ulong(&iter
);
2328 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2335 * Create a context for the channel on the tracer.
2337 * Called with UST app session lock held and a RCU read side lock.
2340 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2341 struct ust_app_channel
*ua_chan
,
2342 struct lttng_ust_context_attr
*uctx
,
2343 struct ust_app
*app
)
2346 struct ust_app_ctx
*ua_ctx
;
2348 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2350 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2356 ua_ctx
= alloc_ust_app_ctx(uctx
);
2357 if (ua_ctx
== NULL
) {
2363 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2364 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2365 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2367 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2377 * Enable on the tracer side a ust app event for the session and channel.
2379 * Called with UST app session lock held.
2382 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2383 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2387 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2392 ua_event
->enabled
= 1;
2399 * Disable on the tracer side a ust app event for the session and channel.
2401 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2402 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2406 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2411 ua_event
->enabled
= 0;
2418 * Lookup ust app channel for session and disable it on the tracer side.
2421 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2422 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2426 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2431 ua_chan
->enabled
= 0;
2438 * Lookup ust app channel for session and enable it on the tracer side. This
2439 * MUST be called with a RCU read side lock acquired.
2441 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2442 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2445 struct lttng_ht_iter iter
;
2446 struct lttng_ht_node_str
*ua_chan_node
;
2447 struct ust_app_channel
*ua_chan
;
2449 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2450 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2451 if (ua_chan_node
== NULL
) {
2452 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2453 uchan
->name
, ua_sess
->tracing_id
);
2457 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2459 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2469 * Ask the consumer to create a channel and get it if successful.
2471 * Return 0 on success or else a negative value.
2473 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2474 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2475 int bitness
, struct ust_registry_session
*registry
)
2478 unsigned int nb_fd
= 0;
2479 struct consumer_socket
*socket
;
2487 health_code_update();
2489 /* Get the right consumer socket for the application. */
2490 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2496 health_code_update();
2498 /* Need one fd for the channel. */
2499 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2501 ERR("Exhausted number of available FD upon create channel");
2506 * Ask consumer to create channel. The consumer will return the number of
2507 * stream we have to expect.
2509 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2516 * Compute the number of fd needed before receiving them. It must be 2 per
2517 * stream (2 being the default value here).
2519 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2521 /* Reserve the amount of file descriptor we need. */
2522 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2524 ERR("Exhausted number of available FD upon create channel");
2525 goto error_fd_get_stream
;
2528 health_code_update();
2531 * Now get the channel from the consumer. This call wil populate the stream
2532 * list of that channel and set the ust objects.
2534 if (usess
->consumer
->enabled
) {
2535 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2545 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2546 error_fd_get_stream
:
2548 * Initiate a destroy channel on the consumer since we had an error
2549 * handling it on our side. The return value is of no importance since we
2550 * already have a ret value set by the previous error that we need to
2553 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2555 lttng_fd_put(LTTNG_FD_APPS
, 1);
2557 health_code_update();
2563 * Duplicate the ust data object of the ust app stream and save it in the
2564 * buffer registry stream.
2566 * Return 0 on success or else a negative value.
2568 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2569 struct ust_app_stream
*stream
)
2576 /* Reserve the amount of file descriptor we need. */
2577 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2579 ERR("Exhausted number of available FD upon duplicate stream");
2583 /* Duplicate object for stream once the original is in the registry. */
2584 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2585 reg_stream
->obj
.ust
);
2587 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2588 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2589 lttng_fd_put(LTTNG_FD_APPS
, 2);
2592 stream
->handle
= stream
->obj
->handle
;
2599 * Duplicate the ust data object of the ust app. channel and save it in the
2600 * buffer registry channel.
2602 * Return 0 on success or else a negative value.
2604 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2605 struct ust_app_channel
*ua_chan
)
2612 /* Need two fds for the channel. */
2613 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2615 ERR("Exhausted number of available FD upon duplicate channel");
2619 /* Duplicate object for stream once the original is in the registry. */
2620 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2622 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2623 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2626 ua_chan
->handle
= ua_chan
->obj
->handle
;
2631 lttng_fd_put(LTTNG_FD_APPS
, 1);
2637 * For a given channel buffer registry, setup all streams of the given ust
2638 * application channel.
2640 * Return 0 on success or else a negative value.
2642 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2643 struct ust_app_channel
*ua_chan
,
2644 struct ust_app
*app
)
2647 struct ust_app_stream
*stream
, *stmp
;
2652 DBG2("UST app setup buffer registry stream");
2654 /* Send all streams to application. */
2655 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2656 struct buffer_reg_stream
*reg_stream
;
2658 ret
= buffer_reg_stream_create(®_stream
);
2664 * Keep original pointer and nullify it in the stream so the delete
2665 * stream call does not release the object.
2667 reg_stream
->obj
.ust
= stream
->obj
;
2669 buffer_reg_stream_add(reg_stream
, reg_chan
);
2671 /* We don't need the streams anymore. */
2672 cds_list_del(&stream
->list
);
2673 delete_ust_app_stream(-1, stream
, app
);
2681 * Create a buffer registry channel for the given session registry and
2682 * application channel object. If regp pointer is valid, it's set with the
2683 * created object. Important, the created object is NOT added to the session
2684 * registry hash table.
2686 * Return 0 on success else a negative value.
2688 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2689 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2692 struct buffer_reg_channel
*reg_chan
= NULL
;
2697 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2699 /* Create buffer registry channel. */
2700 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2705 reg_chan
->consumer_key
= ua_chan
->key
;
2706 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2707 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2709 /* Create and add a channel registry to session. */
2710 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2711 ua_chan
->tracing_channel_id
);
2715 buffer_reg_channel_add(reg_sess
, reg_chan
);
2724 /* Safe because the registry channel object was not added to any HT. */
2725 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2731 * Setup buffer registry channel for the given session registry and application
2732 * channel object. If regp pointer is valid, it's set with the created object.
2734 * Return 0 on success else a negative value.
2736 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2737 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2738 struct ust_app
*app
)
2745 assert(ua_chan
->obj
);
2747 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2749 /* Setup all streams for the registry. */
2750 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2755 reg_chan
->obj
.ust
= ua_chan
->obj
;
2756 ua_chan
->obj
= NULL
;
2761 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2762 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2767 * Send buffer registry channel to the application.
2769 * Return 0 on success else a negative value.
2771 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2772 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2773 struct ust_app_channel
*ua_chan
)
2776 struct buffer_reg_stream
*reg_stream
;
2783 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2785 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2790 /* Send channel to the application. */
2791 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2792 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2793 ret
= -ENOTCONN
; /* Caused by app exiting. */
2795 } else if (ret
< 0) {
2799 health_code_update();
2801 /* Send all streams to application. */
2802 pthread_mutex_lock(®_chan
->stream_list_lock
);
2803 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2804 struct ust_app_stream stream
;
2806 ret
= duplicate_stream_object(reg_stream
, &stream
);
2808 goto error_stream_unlock
;
2811 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2813 (void) release_ust_app_stream(-1, &stream
, app
);
2814 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2815 ret
= -ENOTCONN
; /* Caused by app exiting. */
2817 goto error_stream_unlock
;
2821 * The return value is not important here. This function will output an
2824 (void) release_ust_app_stream(-1, &stream
, app
);
2826 ua_chan
->is_sent
= 1;
2828 error_stream_unlock
:
2829 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2835 * Create and send to the application the created buffers with per UID buffers.
2837 * Return 0 on success else a negative value.
2839 static int create_channel_per_uid(struct ust_app
*app
,
2840 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2841 struct ust_app_channel
*ua_chan
)
2844 struct buffer_reg_uid
*reg_uid
;
2845 struct buffer_reg_channel
*reg_chan
;
2846 bool created
= false;
2853 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2855 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2857 * The session creation handles the creation of this global registry
2858 * object. If none can be find, there is a code flow problem or a
2863 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2866 /* Create the buffer registry channel object. */
2867 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2869 ERR("Error creating the UST channel \"%s\" registry instance",
2876 * Create the buffers on the consumer side. This call populates the
2877 * ust app channel object with all streams and data object.
2879 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2880 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2882 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2886 * Let's remove the previously created buffer registry channel so
2887 * it's not visible anymore in the session registry.
2889 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2890 ua_chan
->tracing_channel_id
, false);
2891 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2892 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2897 * Setup the streams and add it to the session registry.
2899 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2900 ua_chan
, reg_chan
, app
);
2902 ERR("Error setting up UST channel \"%s\"",
2909 /* Send buffers to the application. */
2910 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2912 if (ret
!= -ENOTCONN
) {
2913 ERR("Error sending channel to application");
2919 enum lttng_error_code cmd_ret
;
2920 struct ltt_session
*session
;
2921 uint64_t chan_reg_key
;
2922 struct ust_registry_channel
*chan_reg
;
2925 chan_reg_key
= ua_chan
->tracing_channel_id
;
2927 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2928 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2931 chan_reg
->consumer_key
= ua_chan
->key
;
2933 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2935 session
= session_find_by_id(ua_sess
->tracing_id
);
2938 cmd_ret
= notification_thread_command_add_channel(
2939 notification_thread_handle
, session
->name
,
2940 ua_sess
->euid
, ua_sess
->egid
,
2944 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2946 if (cmd_ret
!= LTTNG_OK
) {
2947 ret
= - (int) cmd_ret
;
2948 ERR("Failed to add channel to notification thread");
2958 * Create and send to the application the created buffers with per PID buffers.
2960 * Return 0 on success else a negative value.
2962 static int create_channel_per_pid(struct ust_app
*app
,
2963 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2964 struct ust_app_channel
*ua_chan
)
2967 struct ust_registry_session
*registry
;
2968 enum lttng_error_code cmd_ret
;
2969 struct ltt_session
*session
;
2970 uint64_t chan_reg_key
;
2971 struct ust_registry_channel
*chan_reg
;
2978 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2982 registry
= get_session_registry(ua_sess
);
2985 /* Create and add a new channel registry to session. */
2986 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2988 ERR("Error creating the UST channel \"%s\" registry instance",
2993 /* Create and get channel on the consumer side. */
2994 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2995 app
->bits_per_long
, registry
);
2997 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3002 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3004 if (ret
!= -ENOTCONN
) {
3005 ERR("Error sending channel to application");
3010 session
= session_find_by_id(ua_sess
->tracing_id
);
3013 chan_reg_key
= ua_chan
->key
;
3014 pthread_mutex_lock(®istry
->lock
);
3015 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3017 chan_reg
->consumer_key
= ua_chan
->key
;
3018 pthread_mutex_unlock(®istry
->lock
);
3020 cmd_ret
= notification_thread_command_add_channel(
3021 notification_thread_handle
, session
->name
,
3022 ua_sess
->euid
, ua_sess
->egid
,
3026 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3027 if (cmd_ret
!= LTTNG_OK
) {
3028 ret
= - (int) cmd_ret
;
3029 ERR("Failed to add channel to notification thread");
3039 * From an already allocated ust app channel, create the channel buffers if
3040 * need and send it to the application. This MUST be called with a RCU read
3041 * side lock acquired.
3043 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3044 * the application exited concurrently.
3046 static int do_create_channel(struct ust_app
*app
,
3047 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3048 struct ust_app_channel
*ua_chan
)
3057 /* Handle buffer type before sending the channel to the application. */
3058 switch (usess
->buffer_type
) {
3059 case LTTNG_BUFFER_PER_UID
:
3061 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3067 case LTTNG_BUFFER_PER_PID
:
3069 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3081 /* Initialize ust objd object using the received handle and add it. */
3082 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3083 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3085 /* If channel is not enabled, disable it on the tracer */
3086 if (!ua_chan
->enabled
) {
3087 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3098 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3099 * newly created channel if not NULL.
3101 * Called with UST app session lock and RCU read-side lock held.
3103 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3104 * the application exited concurrently.
3106 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3107 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3108 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3109 struct ust_app_channel
**ua_chanp
)
3112 struct lttng_ht_iter iter
;
3113 struct lttng_ht_node_str
*ua_chan_node
;
3114 struct ust_app_channel
*ua_chan
;
3116 /* Lookup channel in the ust app session */
3117 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3118 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3119 if (ua_chan_node
!= NULL
) {
3120 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3124 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3125 if (ua_chan
== NULL
) {
3126 /* Only malloc can fail here */
3130 shadow_copy_channel(ua_chan
, uchan
);
3132 /* Set channel type. */
3133 ua_chan
->attr
.type
= type
;
3135 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3140 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3143 /* Only add the channel if successful on the tracer side. */
3144 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3147 *ua_chanp
= ua_chan
;
3150 /* Everything went well. */
3154 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3160 * Create UST app event and create it on the tracer side.
3162 * Called with ust app session mutex held.
3165 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3166 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3167 struct ust_app
*app
)
3170 struct ust_app_event
*ua_event
;
3172 /* Get event node */
3173 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3174 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3175 if (ua_event
!= NULL
) {
3180 /* Does not exist so create one */
3181 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3182 if (ua_event
== NULL
) {
3183 /* Only malloc can failed so something is really wrong */
3187 shadow_copy_event(ua_event
, uevent
);
3189 /* Create it on the tracer side */
3190 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3192 /* Not found previously means that it does not exist on the tracer */
3193 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3197 add_unique_ust_app_event(ua_chan
, ua_event
);
3199 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3206 /* Valid. Calling here is already in a read side lock */
3207 delete_ust_app_event(-1, ua_event
, app
);
3212 * Create UST metadata and open it on the tracer side.
3214 * Called with UST app session lock held and RCU read side lock.
3216 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3217 struct ust_app
*app
, struct consumer_output
*consumer
)
3220 struct ust_app_channel
*metadata
;
3221 struct consumer_socket
*socket
;
3222 struct ust_registry_session
*registry
;
3228 registry
= get_session_registry(ua_sess
);
3231 pthread_mutex_lock(®istry
->lock
);
3233 /* Metadata already exists for this registry or it was closed previously */
3234 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3239 /* Allocate UST metadata */
3240 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3242 /* malloc() failed */
3247 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3249 /* Need one fd for the channel. */
3250 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3252 ERR("Exhausted number of available FD upon create metadata");
3256 /* Get the right consumer socket for the application. */
3257 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3260 goto error_consumer
;
3264 * Keep metadata key so we can identify it on the consumer side. Assign it
3265 * to the registry *before* we ask the consumer so we avoid the race of the
3266 * consumer requesting the metadata and the ask_channel call on our side
3267 * did not returned yet.
3269 registry
->metadata_key
= metadata
->key
;
3272 * Ask the metadata channel creation to the consumer. The metadata object
3273 * will be created by the consumer and kept their. However, the stream is
3274 * never added or monitored until we do a first push metadata to the
3277 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3280 /* Nullify the metadata key so we don't try to close it later on. */
3281 registry
->metadata_key
= 0;
3282 goto error_consumer
;
3286 * The setup command will make the metadata stream be sent to the relayd,
3287 * if applicable, and the thread managing the metadatas. This is important
3288 * because after this point, if an error occurs, the only way the stream
3289 * can be deleted is to be monitored in the consumer.
3291 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3293 /* Nullify the metadata key so we don't try to close it later on. */
3294 registry
->metadata_key
= 0;
3295 goto error_consumer
;
3298 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3299 metadata
->key
, app
->pid
);
3302 lttng_fd_put(LTTNG_FD_APPS
, 1);
3303 delete_ust_app_channel(-1, metadata
, app
);
3305 pthread_mutex_unlock(®istry
->lock
);
3310 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3311 * acquired before calling this function.
3313 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3315 struct ust_app
*app
= NULL
;
3316 struct lttng_ht_node_ulong
*node
;
3317 struct lttng_ht_iter iter
;
3319 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3320 node
= lttng_ht_iter_get_node_ulong(&iter
);
3322 DBG2("UST app no found with pid %d", pid
);
3326 DBG2("Found UST app by pid %d", pid
);
3328 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3335 * Allocate and init an UST app object using the registration information and
3336 * the command socket. This is called when the command socket connects to the
3339 * The object is returned on success or else NULL.
3341 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3343 struct ust_app
*lta
= NULL
;
3348 DBG3("UST app creating application for socket %d", sock
);
3350 if ((msg
->bits_per_long
== 64 &&
3351 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3352 || (msg
->bits_per_long
== 32 &&
3353 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3354 ERR("Registration failed: application \"%s\" (pid: %d) has "
3355 "%d-bit long, but no consumerd for this size is available.\n",
3356 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3360 lta
= zmalloc(sizeof(struct ust_app
));
3366 lta
->ppid
= msg
->ppid
;
3367 lta
->uid
= msg
->uid
;
3368 lta
->gid
= msg
->gid
;
3370 lta
->bits_per_long
= msg
->bits_per_long
;
3371 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3372 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3373 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3374 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3375 lta
->long_alignment
= msg
->long_alignment
;
3376 lta
->byte_order
= msg
->byte_order
;
3378 lta
->v_major
= msg
->major
;
3379 lta
->v_minor
= msg
->minor
;
3380 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3381 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3382 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3383 lta
->notify_sock
= -1;
3385 /* Copy name and make sure it's NULL terminated. */
3386 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3387 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3390 * Before this can be called, when receiving the registration information,
3391 * the application compatibility is checked. So, at this point, the
3392 * application can work with this session daemon.
3394 lta
->compatible
= 1;
3396 lta
->pid
= msg
->pid
;
3397 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3399 pthread_mutex_init(<a
->sock_lock
, NULL
);
3400 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3402 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3408 * For a given application object, add it to every hash table.
3410 void ust_app_add(struct ust_app
*app
)
3413 assert(app
->notify_sock
>= 0);
3418 * On a re-registration, we want to kick out the previous registration of
3421 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3424 * The socket _should_ be unique until _we_ call close. So, a add_unique
3425 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3426 * already in the table.
3428 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3430 /* Add application to the notify socket hash table. */
3431 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3432 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3434 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3435 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3436 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3443 * Set the application version into the object.
3445 * Return 0 on success else a negative value either an errno code or a
3446 * LTTng-UST error code.
3448 int ust_app_version(struct ust_app
*app
)
3454 pthread_mutex_lock(&app
->sock_lock
);
3455 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3456 pthread_mutex_unlock(&app
->sock_lock
);
3458 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3459 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3461 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3469 * Unregister app by removing it from the global traceable app list and freeing
3472 * The socket is already closed at this point so no close to sock.
3474 void ust_app_unregister(int sock
)
3476 struct ust_app
*lta
;
3477 struct lttng_ht_node_ulong
*node
;
3478 struct lttng_ht_iter ust_app_sock_iter
;
3479 struct lttng_ht_iter iter
;
3480 struct ust_app_session
*ua_sess
;
3485 /* Get the node reference for a call_rcu */
3486 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3487 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3490 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3491 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3494 * For per-PID buffers, perform "push metadata" and flush all
3495 * application streams before removing app from hash tables,
3496 * ensuring proper behavior of data_pending check.
3497 * Remove sessions so they are not visible during deletion.
3499 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3501 struct ust_registry_session
*registry
;
3503 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3505 /* The session was already removed so scheduled for teardown. */
3509 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3510 (void) ust_app_flush_app_session(lta
, ua_sess
);
3514 * Add session to list for teardown. This is safe since at this point we
3515 * are the only one using this list.
3517 pthread_mutex_lock(&ua_sess
->lock
);
3519 if (ua_sess
->deleted
) {
3520 pthread_mutex_unlock(&ua_sess
->lock
);
3525 * Normally, this is done in the delete session process which is
3526 * executed in the call rcu below. However, upon registration we can't
3527 * afford to wait for the grace period before pushing data or else the
3528 * data pending feature can race between the unregistration and stop
3529 * command where the data pending command is sent *before* the grace
3532 * The close metadata below nullifies the metadata pointer in the
3533 * session so the delete session will NOT push/close a second time.
3535 registry
= get_session_registry(ua_sess
);
3537 /* Push metadata for application before freeing the application. */
3538 (void) push_metadata(registry
, ua_sess
->consumer
);
3541 * Don't ask to close metadata for global per UID buffers. Close
3542 * metadata only on destroy trace session in this case. Also, the
3543 * previous push metadata could have flag the metadata registry to
3544 * close so don't send a close command if closed.
3546 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3547 /* And ask to close it for this session registry. */
3548 (void) close_metadata(registry
, ua_sess
->consumer
);
3551 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3553 pthread_mutex_unlock(&ua_sess
->lock
);
3556 /* Remove application from PID hash table */
3557 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3561 * Remove application from notify hash table. The thread handling the
3562 * notify socket could have deleted the node so ignore on error because
3563 * either way it's valid. The close of that socket is handled by the other
3566 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3567 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3570 * Ignore return value since the node might have been removed before by an
3571 * add replace during app registration because the PID can be reassigned by
3574 iter
.iter
.node
= <a
->pid_n
.node
;
3575 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3577 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3582 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3589 * Fill events array with all events name of all registered apps.
3591 int ust_app_list_events(struct lttng_event
**events
)
3594 size_t nbmem
, count
= 0;
3595 struct lttng_ht_iter iter
;
3596 struct ust_app
*app
;
3597 struct lttng_event
*tmp_event
;
3599 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3600 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3601 if (tmp_event
== NULL
) {
3602 PERROR("zmalloc ust app events");
3609 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3610 struct lttng_ust_tracepoint_iter uiter
;
3612 health_code_update();
3614 if (!app
->compatible
) {
3616 * TODO: In time, we should notice the caller of this error by
3617 * telling him that this is a version error.
3621 pthread_mutex_lock(&app
->sock_lock
);
3622 handle
= ustctl_tracepoint_list(app
->sock
);
3624 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3625 ERR("UST app list events getting handle failed for app pid %d",
3628 pthread_mutex_unlock(&app
->sock_lock
);
3632 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3633 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3634 /* Handle ustctl error. */
3638 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3639 ERR("UST app tp list get failed for app %d with ret %d",
3642 DBG3("UST app tp list get failed. Application is dead");
3644 * This is normal behavior, an application can die during the
3645 * creation process. Don't report an error so the execution can
3646 * continue normally. Continue normal execution.
3651 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3652 if (release_ret
< 0 &&
3653 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3654 release_ret
!= -EPIPE
) {
3655 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3657 pthread_mutex_unlock(&app
->sock_lock
);
3661 health_code_update();
3662 if (count
>= nbmem
) {
3663 /* In case the realloc fails, we free the memory */
3664 struct lttng_event
*new_tmp_event
;
3667 new_nbmem
= nbmem
<< 1;
3668 DBG2("Reallocating event list from %zu to %zu entries",
3670 new_tmp_event
= realloc(tmp_event
,
3671 new_nbmem
* sizeof(struct lttng_event
));
3672 if (new_tmp_event
== NULL
) {
3675 PERROR("realloc ust app events");
3678 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3679 if (release_ret
< 0 &&
3680 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3681 release_ret
!= -EPIPE
) {
3682 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3684 pthread_mutex_unlock(&app
->sock_lock
);
3687 /* Zero the new memory */
3688 memset(new_tmp_event
+ nbmem
, 0,
3689 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3691 tmp_event
= new_tmp_event
;
3693 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3694 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3695 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3696 tmp_event
[count
].pid
= app
->pid
;
3697 tmp_event
[count
].enabled
= -1;
3700 ret
= ustctl_release_handle(app
->sock
, handle
);
3701 pthread_mutex_unlock(&app
->sock_lock
);
3702 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3703 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3708 *events
= tmp_event
;
3710 DBG2("UST app list events done (%zu events)", count
);
3715 health_code_update();
3720 * Fill events array with all events name of all registered apps.