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"
49 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
51 /* Next available channel key. Access under next_channel_key_lock. */
52 static uint64_t _next_channel_key
;
53 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 /* Next available session ID. Access under next_session_id_lock. */
56 static uint64_t _next_session_id
;
57 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
60 * Return the incremented value of next_channel_key.
62 static uint64_t get_next_channel_key(void)
66 pthread_mutex_lock(&next_channel_key_lock
);
67 ret
= ++_next_channel_key
;
68 pthread_mutex_unlock(&next_channel_key_lock
);
73 * Return the atomically incremented value of next_session_id.
75 static uint64_t get_next_session_id(void)
79 pthread_mutex_lock(&next_session_id_lock
);
80 ret
= ++_next_session_id
;
81 pthread_mutex_unlock(&next_session_id_lock
);
85 static void copy_channel_attr_to_ustctl(
86 struct ustctl_consumer_channel_attr
*attr
,
87 struct lttng_ust_channel_attr
*uattr
)
89 /* Copy event attributes since the layout is different. */
90 attr
->subbuf_size
= uattr
->subbuf_size
;
91 attr
->num_subbuf
= uattr
->num_subbuf
;
92 attr
->overwrite
= uattr
->overwrite
;
93 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
94 attr
->read_timer_interval
= uattr
->read_timer_interval
;
95 attr
->output
= uattr
->output
;
96 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
100 * Match function for the hash table lookup.
102 * It matches an ust app event based on three attributes which are the event
103 * name, the filter bytecode and the loglevel.
105 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
107 struct ust_app_event
*event
;
108 const struct ust_app_ht_key
*key
;
109 int ev_loglevel_value
;
114 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
116 ev_loglevel_value
= event
->attr
.loglevel
;
118 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
121 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
125 /* Event loglevel. */
126 if (ev_loglevel_value
!= key
->loglevel_type
) {
127 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
128 && key
->loglevel_type
== 0 &&
129 ev_loglevel_value
== -1) {
131 * Match is accepted. This is because on event creation, the
132 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
133 * -1 are accepted for this loglevel type since 0 is the one set by
134 * the API when receiving an enable event.
141 /* One of the filters is NULL, fail. */
142 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
146 if (key
->filter
&& event
->filter
) {
147 /* Both filters exists, check length followed by the bytecode. */
148 if (event
->filter
->len
!= key
->filter
->len
||
149 memcmp(event
->filter
->data
, key
->filter
->data
,
150 event
->filter
->len
) != 0) {
155 /* One of the exclusions is NULL, fail. */
156 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
160 if (key
->exclusion
&& event
->exclusion
) {
161 /* Both exclusions exists, check count followed by the names. */
162 if (event
->exclusion
->count
!= key
->exclusion
->count
||
163 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
164 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
178 * Unique add of an ust app event in the given ht. This uses the custom
179 * ht_match_ust_app_event match function and the event name as hash.
181 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
182 struct ust_app_event
*event
)
184 struct cds_lfht_node
*node_ptr
;
185 struct ust_app_ht_key key
;
189 assert(ua_chan
->events
);
192 ht
= ua_chan
->events
;
193 key
.name
= event
->attr
.name
;
194 key
.filter
= event
->filter
;
195 key
.loglevel_type
= event
->attr
.loglevel
;
196 key
.exclusion
= event
->exclusion
;
198 node_ptr
= cds_lfht_add_unique(ht
->ht
,
199 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
200 ht_match_ust_app_event
, &key
, &event
->node
.node
);
201 assert(node_ptr
== &event
->node
.node
);
205 * Close the notify socket from the given RCU head object. This MUST be called
206 * through a call_rcu().
208 static void close_notify_sock_rcu(struct rcu_head
*head
)
211 struct ust_app_notify_sock_obj
*obj
=
212 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
214 /* Must have a valid fd here. */
215 assert(obj
->fd
>= 0);
217 ret
= close(obj
->fd
);
219 ERR("close notify sock %d RCU", obj
->fd
);
221 lttng_fd_put(LTTNG_FD_APPS
, 1);
227 * Return the session registry according to the buffer type of the given
230 * A registry per UID object MUST exists before calling this function or else
231 * it assert() if not found. RCU read side lock must be acquired.
233 static struct ust_registry_session
*get_session_registry(
234 struct ust_app_session
*ua_sess
)
236 struct ust_registry_session
*registry
= NULL
;
240 switch (ua_sess
->buffer_type
) {
241 case LTTNG_BUFFER_PER_PID
:
243 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
247 registry
= reg_pid
->registry
->reg
.ust
;
250 case LTTNG_BUFFER_PER_UID
:
252 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
253 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
257 registry
= reg_uid
->registry
->reg
.ust
;
269 * Delete ust context safely. RCU read lock must be held before calling
273 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
281 pthread_mutex_lock(&app
->sock_lock
);
282 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
283 pthread_mutex_unlock(&app
->sock_lock
);
284 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
285 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
286 sock
, ua_ctx
->obj
->handle
, ret
);
294 * Delete ust app event safely. RCU read lock must be held before calling
298 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
305 free(ua_event
->filter
);
306 if (ua_event
->exclusion
!= NULL
)
307 free(ua_event
->exclusion
);
308 if (ua_event
->obj
!= NULL
) {
309 pthread_mutex_lock(&app
->sock_lock
);
310 ret
= ustctl_release_object(sock
, ua_event
->obj
);
311 pthread_mutex_unlock(&app
->sock_lock
);
312 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
313 ERR("UST app sock %d release event obj failed with ret %d",
322 * Release ust data object of the given stream.
324 * Return 0 on success or else a negative value.
326 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
334 pthread_mutex_lock(&app
->sock_lock
);
335 ret
= ustctl_release_object(sock
, stream
->obj
);
336 pthread_mutex_unlock(&app
->sock_lock
);
337 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
338 ERR("UST app sock %d release stream obj failed with ret %d",
341 lttng_fd_put(LTTNG_FD_APPS
, 2);
349 * Delete ust app stream safely. RCU read lock must be held before calling
353 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
358 (void) release_ust_app_stream(sock
, stream
, app
);
363 * We need to execute ht_destroy outside of RCU read-side critical
364 * section and outside of call_rcu thread, so we postpone its execution
365 * using ht_cleanup_push. It is simpler than to change the semantic of
366 * the many callers of delete_ust_app_session().
369 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
371 struct ust_app_channel
*ua_chan
=
372 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
374 ht_cleanup_push(ua_chan
->ctx
);
375 ht_cleanup_push(ua_chan
->events
);
380 * Extract the lost packet or discarded events counter when the channel is
381 * being deleted and store the value in the parent channel so we can
382 * access it from lttng list and at stop/destroy.
384 * The session list lock must be held by the caller.
387 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
389 uint64_t discarded
= 0, lost
= 0;
390 struct ltt_session
*session
;
391 struct ltt_ust_channel
*uchan
;
393 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
398 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
399 if (!session
|| !session
->ust_session
) {
401 * Not finding the session is not an error because there are
402 * multiple ways the channels can be torn down.
404 * 1) The session daemon can initiate the destruction of the
405 * ust app session after receiving a destroy command or
406 * during its shutdown/teardown.
407 * 2) The application, since we are in per-pid tracing, is
408 * unregistering and tearing down its ust app session.
410 * Both paths are protected by the session list lock which
411 * ensures that the accounting of lost packets and discarded
412 * events is done exactly once. The session is then unpublished
413 * from the session list, resulting in this condition.
418 if (ua_chan
->attr
.overwrite
) {
419 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
420 ua_chan
->key
, session
->ust_session
->consumer
,
423 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
424 ua_chan
->key
, session
->ust_session
->consumer
,
427 uchan
= trace_ust_find_channel_by_name(
428 session
->ust_session
->domain_global
.channels
,
431 ERR("Missing UST channel to store discarded counters");
435 uchan
->per_pid_closed_app_discarded
+= discarded
;
436 uchan
->per_pid_closed_app_lost
+= lost
;
443 * Delete ust app channel safely. RCU read lock must be held before calling
446 * The session list lock must be held by the caller.
449 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
453 struct lttng_ht_iter iter
;
454 struct ust_app_event
*ua_event
;
455 struct ust_app_ctx
*ua_ctx
;
456 struct ust_app_stream
*stream
, *stmp
;
457 struct ust_registry_session
*registry
;
461 DBG3("UST app deleting channel %s", ua_chan
->name
);
464 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
465 cds_list_del(&stream
->list
);
466 delete_ust_app_stream(sock
, stream
, app
);
470 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
471 cds_list_del(&ua_ctx
->list
);
472 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
474 delete_ust_app_ctx(sock
, ua_ctx
, app
);
478 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
480 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
482 delete_ust_app_event(sock
, ua_event
, app
);
485 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
486 /* Wipe and free registry from session registry. */
487 registry
= get_session_registry(ua_chan
->session
);
489 ust_registry_channel_del_free(registry
, ua_chan
->key
,
492 save_per_pid_lost_discarded_counters(ua_chan
);
495 if (ua_chan
->obj
!= NULL
) {
496 /* Remove channel from application UST object descriptor. */
497 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
498 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
500 pthread_mutex_lock(&app
->sock_lock
);
501 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
502 pthread_mutex_unlock(&app
->sock_lock
);
503 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
504 ERR("UST app sock %d release channel obj failed with ret %d",
507 lttng_fd_put(LTTNG_FD_APPS
, 1);
510 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
513 int ust_app_register_done(struct ust_app
*app
)
517 pthread_mutex_lock(&app
->sock_lock
);
518 ret
= ustctl_register_done(app
->sock
);
519 pthread_mutex_unlock(&app
->sock_lock
);
523 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
528 pthread_mutex_lock(&app
->sock_lock
);
533 ret
= ustctl_release_object(sock
, data
);
535 pthread_mutex_unlock(&app
->sock_lock
);
541 * Push metadata to consumer socket.
543 * RCU read-side lock must be held to guarantee existance of socket.
544 * Must be called with the ust app session lock held.
545 * Must be called with the registry lock held.
547 * On success, return the len of metadata pushed or else a negative value.
548 * Returning a -EPIPE return value means we could not send the metadata,
549 * but it can be caused by recoverable errors (e.g. the application has
550 * terminated concurrently).
552 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
553 struct consumer_socket
*socket
, int send_zero_data
)
556 char *metadata_str
= NULL
;
557 size_t len
, offset
, new_metadata_len_sent
;
559 uint64_t metadata_key
, metadata_version
;
564 metadata_key
= registry
->metadata_key
;
567 * Means that no metadata was assigned to the session. This can
568 * happens if no start has been done previously.
574 offset
= registry
->metadata_len_sent
;
575 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
576 new_metadata_len_sent
= registry
->metadata_len
;
577 metadata_version
= registry
->metadata_version
;
579 DBG3("No metadata to push for metadata key %" PRIu64
,
580 registry
->metadata_key
);
582 if (send_zero_data
) {
583 DBG("No metadata to push");
589 /* Allocate only what we have to send. */
590 metadata_str
= zmalloc(len
);
592 PERROR("zmalloc ust app metadata string");
596 /* Copy what we haven't sent out. */
597 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
600 pthread_mutex_unlock(®istry
->lock
);
602 * We need to unlock the registry while we push metadata to
603 * break a circular dependency between the consumerd metadata
604 * lock and the sessiond registry lock. Indeed, pushing metadata
605 * to the consumerd awaits that it gets pushed all the way to
606 * relayd, but doing so requires grabbing the metadata lock. If
607 * a concurrent metadata request is being performed by
608 * consumerd, this can try to grab the registry lock on the
609 * sessiond while holding the metadata lock on the consumer
610 * daemon. Those push and pull schemes are performed on two
611 * different bidirectionnal communication sockets.
613 ret
= consumer_push_metadata(socket
, metadata_key
,
614 metadata_str
, len
, offset
, metadata_version
);
615 pthread_mutex_lock(®istry
->lock
);
618 * There is an acceptable race here between the registry
619 * metadata key assignment and the creation on the
620 * consumer. The session daemon can concurrently push
621 * metadata for this registry while being created on the
622 * consumer since the metadata key of the registry is
623 * assigned *before* it is setup to avoid the consumer
624 * to ask for metadata that could possibly be not found
625 * in the session daemon.
627 * The metadata will get pushed either by the session
628 * being stopped or the consumer requesting metadata if
629 * that race is triggered.
631 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
634 ERR("Error pushing metadata to consumer");
640 * Metadata may have been concurrently pushed, since
641 * we're not holding the registry lock while pushing to
642 * consumer. This is handled by the fact that we send
643 * the metadata content, size, and the offset at which
644 * that metadata belongs. This may arrive out of order
645 * on the consumer side, and the consumer is able to
646 * deal with overlapping fragments. The consumer
647 * supports overlapping fragments, which must be
648 * contiguous starting from offset 0. We keep the
649 * largest metadata_len_sent value of the concurrent
652 registry
->metadata_len_sent
=
653 max_t(size_t, registry
->metadata_len_sent
,
654 new_metadata_len_sent
);
663 * On error, flag the registry that the metadata is
664 * closed. We were unable to push anything and this
665 * means that either the consumer is not responding or
666 * the metadata cache has been destroyed on the
669 registry
->metadata_closed
= 1;
677 * For a given application and session, push metadata to consumer.
678 * Either sock or consumer is required : if sock is NULL, the default
679 * socket to send the metadata is retrieved from consumer, if sock
680 * is not NULL we use it to send the metadata.
681 * RCU read-side lock must be held while calling this function,
682 * therefore ensuring existance of registry. It also ensures existance
683 * of socket throughout this function.
685 * Return 0 on success else a negative error.
686 * Returning a -EPIPE return value means we could not send the metadata,
687 * but it can be caused by recoverable errors (e.g. the application has
688 * terminated concurrently).
690 static int push_metadata(struct ust_registry_session
*registry
,
691 struct consumer_output
*consumer
)
695 struct consumer_socket
*socket
;
700 pthread_mutex_lock(®istry
->lock
);
701 if (registry
->metadata_closed
) {
706 /* Get consumer socket to use to push the metadata.*/
707 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
714 ret
= ust_app_push_metadata(registry
, socket
, 0);
719 pthread_mutex_unlock(®istry
->lock
);
723 pthread_mutex_unlock(®istry
->lock
);
728 * Send to the consumer a close metadata command for the given session. Once
729 * done, the metadata channel is deleted and the session metadata pointer is
730 * nullified. The session lock MUST be held unless the application is
731 * in the destroy path.
733 * Return 0 on success else a negative value.
735 static int close_metadata(struct ust_registry_session
*registry
,
736 struct consumer_output
*consumer
)
739 struct consumer_socket
*socket
;
746 pthread_mutex_lock(®istry
->lock
);
748 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
753 /* Get consumer socket to use to push the metadata.*/
754 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
761 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
768 * Metadata closed. Even on error this means that the consumer is not
769 * responding or not found so either way a second close should NOT be emit
772 registry
->metadata_closed
= 1;
774 pthread_mutex_unlock(®istry
->lock
);
780 * We need to execute ht_destroy outside of RCU read-side critical
781 * section and outside of call_rcu thread, so we postpone its execution
782 * using ht_cleanup_push. It is simpler than to change the semantic of
783 * the many callers of delete_ust_app_session().
786 void delete_ust_app_session_rcu(struct rcu_head
*head
)
788 struct ust_app_session
*ua_sess
=
789 caa_container_of(head
, struct ust_app_session
, rcu_head
);
791 ht_cleanup_push(ua_sess
->channels
);
796 * Delete ust app session safely. RCU read lock must be held before calling
799 * The session list lock must be held by the caller.
802 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
806 struct lttng_ht_iter iter
;
807 struct ust_app_channel
*ua_chan
;
808 struct ust_registry_session
*registry
;
812 pthread_mutex_lock(&ua_sess
->lock
);
814 assert(!ua_sess
->deleted
);
815 ua_sess
->deleted
= true;
817 registry
= get_session_registry(ua_sess
);
818 /* Registry can be null on error path during initialization. */
820 /* Push metadata for application before freeing the application. */
821 (void) push_metadata(registry
, ua_sess
->consumer
);
824 * Don't ask to close metadata for global per UID buffers. Close
825 * metadata only on destroy trace session in this case. Also, the
826 * previous push metadata could have flag the metadata registry to
827 * close so don't send a close command if closed.
829 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
830 /* And ask to close it for this session registry. */
831 (void) close_metadata(registry
, ua_sess
->consumer
);
835 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
837 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
839 delete_ust_app_channel(sock
, ua_chan
, app
);
842 /* In case of per PID, the registry is kept in the session. */
843 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
844 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
847 * Registry can be null on error path during
850 buffer_reg_pid_remove(reg_pid
);
851 buffer_reg_pid_destroy(reg_pid
);
855 if (ua_sess
->handle
!= -1) {
856 pthread_mutex_lock(&app
->sock_lock
);
857 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
858 pthread_mutex_unlock(&app
->sock_lock
);
859 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
860 ERR("UST app sock %d release session handle failed with ret %d",
863 /* Remove session from application UST object descriptor. */
864 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
865 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
869 pthread_mutex_unlock(&ua_sess
->lock
);
871 consumer_output_put(ua_sess
->consumer
);
873 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
877 * Delete a traceable application structure from the global list. Never call
878 * this function outside of a call_rcu call.
880 * RCU read side lock should _NOT_ be held when calling this function.
883 void delete_ust_app(struct ust_app
*app
)
886 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
889 * The session list lock must be held during this function to guarantee
890 * the existence of ua_sess.
893 /* Delete ust app sessions info */
898 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
900 /* Free every object in the session and the session. */
902 delete_ust_app_session(sock
, ua_sess
, app
);
906 ht_cleanup_push(app
->sessions
);
907 ht_cleanup_push(app
->ust_sessions_objd
);
908 ht_cleanup_push(app
->ust_objd
);
911 * Wait until we have deleted the application from the sock hash table
912 * before closing this socket, otherwise an application could re-use the
913 * socket ID and race with the teardown, using the same hash table entry.
915 * It's OK to leave the close in call_rcu. We want it to stay unique for
916 * all RCU readers that could run concurrently with unregister app,
917 * therefore we _need_ to only close that socket after a grace period. So
918 * it should stay in this RCU callback.
920 * This close() is a very important step of the synchronization model so
921 * every modification to this function must be carefully reviewed.
927 lttng_fd_put(LTTNG_FD_APPS
, 1);
929 DBG2("UST app pid %d deleted", app
->pid
);
931 session_unlock_list();
935 * URCU intermediate call to delete an UST app.
938 void delete_ust_app_rcu(struct rcu_head
*head
)
940 struct lttng_ht_node_ulong
*node
=
941 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
942 struct ust_app
*app
=
943 caa_container_of(node
, struct ust_app
, pid_n
);
945 DBG3("Call RCU deleting app PID %d", app
->pid
);
950 * Delete the session from the application ht and delete the data structure by
951 * freeing every object inside and releasing them.
953 * The session list lock must be held by the caller.
955 static void destroy_app_session(struct ust_app
*app
,
956 struct ust_app_session
*ua_sess
)
959 struct lttng_ht_iter iter
;
964 iter
.iter
.node
= &ua_sess
->node
.node
;
965 ret
= lttng_ht_del(app
->sessions
, &iter
);
967 /* Already scheduled for teardown. */
971 /* Once deleted, free the data structure. */
972 delete_ust_app_session(app
->sock
, ua_sess
, app
);
979 * Alloc new UST app session.
982 struct ust_app_session
*alloc_ust_app_session(void)
984 struct ust_app_session
*ua_sess
;
986 /* Init most of the default value by allocating and zeroing */
987 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
988 if (ua_sess
== NULL
) {
993 ua_sess
->handle
= -1;
994 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
995 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
996 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1005 * Alloc new UST app channel.
1008 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1009 struct ust_app_session
*ua_sess
,
1010 struct lttng_ust_channel_attr
*attr
)
1012 struct ust_app_channel
*ua_chan
;
1014 /* Init most of the default value by allocating and zeroing */
1015 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1016 if (ua_chan
== NULL
) {
1021 /* Setup channel name */
1022 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1023 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1025 ua_chan
->enabled
= 1;
1026 ua_chan
->handle
= -1;
1027 ua_chan
->session
= ua_sess
;
1028 ua_chan
->key
= get_next_channel_key();
1029 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1030 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1031 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1033 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1034 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1036 /* Copy attributes */
1038 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1039 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1040 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1041 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1042 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1043 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1044 ua_chan
->attr
.output
= attr
->output
;
1045 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1047 /* By default, the channel is a per cpu channel. */
1048 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1050 DBG3("UST app channel %s allocated", ua_chan
->name
);
1059 * Allocate and initialize a UST app stream.
1061 * Return newly allocated stream pointer or NULL on error.
1063 struct ust_app_stream
*ust_app_alloc_stream(void)
1065 struct ust_app_stream
*stream
= NULL
;
1067 stream
= zmalloc(sizeof(*stream
));
1068 if (stream
== NULL
) {
1069 PERROR("zmalloc ust app stream");
1073 /* Zero could be a valid value for a handle so flag it to -1. */
1074 stream
->handle
= -1;
1081 * Alloc new UST app event.
1084 struct ust_app_event
*alloc_ust_app_event(char *name
,
1085 struct lttng_ust_event
*attr
)
1087 struct ust_app_event
*ua_event
;
1089 /* Init most of the default value by allocating and zeroing */
1090 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1091 if (ua_event
== NULL
) {
1096 ua_event
->enabled
= 1;
1097 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1098 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1099 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1101 /* Copy attributes */
1103 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1106 DBG3("UST app event %s allocated", ua_event
->name
);
1115 * Alloc new UST app context.
1118 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1120 struct ust_app_ctx
*ua_ctx
;
1122 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1123 if (ua_ctx
== NULL
) {
1127 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1130 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1131 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1132 char *provider_name
= NULL
, *ctx_name
= NULL
;
1134 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1135 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1136 if (!provider_name
|| !ctx_name
) {
1137 free(provider_name
);
1142 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1143 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1147 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1155 * Allocate a filter and copy the given original filter.
1157 * Return allocated filter or NULL on error.
1159 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1160 struct lttng_filter_bytecode
*orig_f
)
1162 struct lttng_filter_bytecode
*filter
= NULL
;
1164 /* Copy filter bytecode */
1165 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1167 PERROR("zmalloc alloc filter bytecode");
1171 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1178 * Create a liblttng-ust filter bytecode from given bytecode.
1180 * Return allocated filter or NULL on error.
1182 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1183 struct lttng_filter_bytecode
*orig_f
)
1185 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1187 /* Copy filter bytecode */
1188 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1190 PERROR("zmalloc alloc ust filter bytecode");
1194 assert(sizeof(struct lttng_filter_bytecode
) ==
1195 sizeof(struct lttng_ust_filter_bytecode
));
1196 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1202 * Find an ust_app using the sock and return it. RCU read side lock must be
1203 * held before calling this helper function.
1205 struct ust_app
*ust_app_find_by_sock(int sock
)
1207 struct lttng_ht_node_ulong
*node
;
1208 struct lttng_ht_iter iter
;
1210 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1211 node
= lttng_ht_iter_get_node_ulong(&iter
);
1213 DBG2("UST app find by sock %d not found", sock
);
1217 return caa_container_of(node
, struct ust_app
, sock_n
);
1224 * Find an ust_app using the notify sock and return it. RCU read side lock must
1225 * be held before calling this helper function.
1227 static struct ust_app
*find_app_by_notify_sock(int sock
)
1229 struct lttng_ht_node_ulong
*node
;
1230 struct lttng_ht_iter iter
;
1232 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1234 node
= lttng_ht_iter_get_node_ulong(&iter
);
1236 DBG2("UST app find by notify sock %d not found", sock
);
1240 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1247 * Lookup for an ust app event based on event name, filter bytecode and the
1250 * Return an ust_app_event object or NULL on error.
1252 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1253 char *name
, struct lttng_filter_bytecode
*filter
,
1255 const struct lttng_event_exclusion
*exclusion
)
1257 struct lttng_ht_iter iter
;
1258 struct lttng_ht_node_str
*node
;
1259 struct ust_app_event
*event
= NULL
;
1260 struct ust_app_ht_key key
;
1265 /* Setup key for event lookup. */
1267 key
.filter
= filter
;
1268 key
.loglevel_type
= loglevel_value
;
1269 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1270 key
.exclusion
= exclusion
;
1272 /* Lookup using the event name as hash and a custom match fct. */
1273 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1274 ht_match_ust_app_event
, &key
, &iter
.iter
);
1275 node
= lttng_ht_iter_get_node_str(&iter
);
1280 event
= caa_container_of(node
, struct ust_app_event
, node
);
1287 * Create the channel context on the tracer.
1289 * Called with UST app session lock held.
1292 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1293 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1297 health_code_update();
1299 pthread_mutex_lock(&app
->sock_lock
);
1300 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1301 ua_chan
->obj
, &ua_ctx
->obj
);
1302 pthread_mutex_unlock(&app
->sock_lock
);
1304 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1305 ERR("UST app create channel context failed for app (pid: %d) "
1306 "with ret %d", app
->pid
, ret
);
1309 * This is normal behavior, an application can die during the
1310 * creation process. Don't report an error so the execution can
1311 * continue normally.
1314 DBG3("UST app disable event failed. Application is dead.");
1319 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1321 DBG2("UST app context handle %d created successfully for channel %s",
1322 ua_ctx
->handle
, ua_chan
->name
);
1325 health_code_update();
1330 * Set the filter on the tracer.
1333 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1334 struct ust_app
*app
)
1337 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1339 health_code_update();
1341 if (!ua_event
->filter
) {
1346 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1347 if (!ust_bytecode
) {
1348 ret
= -LTTNG_ERR_NOMEM
;
1351 pthread_mutex_lock(&app
->sock_lock
);
1352 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1354 pthread_mutex_unlock(&app
->sock_lock
);
1356 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1357 ERR("UST app event %s filter failed for app (pid: %d) "
1358 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1361 * This is normal behavior, an application can die during the
1362 * creation process. Don't report an error so the execution can
1363 * continue normally.
1366 DBG3("UST app filter event failed. Application is dead.");
1371 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1374 health_code_update();
1380 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1381 struct lttng_event_exclusion
*exclusion
)
1383 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1384 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1385 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1387 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1388 if (!ust_exclusion
) {
1393 assert(sizeof(struct lttng_event_exclusion
) ==
1394 sizeof(struct lttng_ust_event_exclusion
));
1395 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1397 return ust_exclusion
;
1401 * Set event exclusions on the tracer.
1404 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1405 struct ust_app
*app
)
1408 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1410 health_code_update();
1412 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1417 ust_exclusion
= create_ust_exclusion_from_exclusion(
1418 ua_event
->exclusion
);
1419 if (!ust_exclusion
) {
1420 ret
= -LTTNG_ERR_NOMEM
;
1423 pthread_mutex_lock(&app
->sock_lock
);
1424 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1425 pthread_mutex_unlock(&app
->sock_lock
);
1427 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1428 ERR("UST app event %s exclusions failed for app (pid: %d) "
1429 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1432 * This is normal behavior, an application can die during the
1433 * creation process. Don't report an error so the execution can
1434 * continue normally.
1437 DBG3("UST app event exclusion failed. Application is dead.");
1442 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1445 health_code_update();
1446 free(ust_exclusion
);
1451 * Disable the specified event on to UST tracer for the UST session.
1453 static int disable_ust_event(struct ust_app
*app
,
1454 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1458 health_code_update();
1460 pthread_mutex_lock(&app
->sock_lock
);
1461 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1462 pthread_mutex_unlock(&app
->sock_lock
);
1464 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1465 ERR("UST app event %s disable failed for app (pid: %d) "
1466 "and session handle %d with ret %d",
1467 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1470 * This is normal behavior, an application can die during the
1471 * creation process. Don't report an error so the execution can
1472 * continue normally.
1475 DBG3("UST app disable event failed. Application is dead.");
1480 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1481 ua_event
->attr
.name
, app
->pid
);
1484 health_code_update();
1489 * Disable the specified channel on to UST tracer for the UST session.
1491 static int disable_ust_channel(struct ust_app
*app
,
1492 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1496 health_code_update();
1498 pthread_mutex_lock(&app
->sock_lock
);
1499 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1500 pthread_mutex_unlock(&app
->sock_lock
);
1502 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1503 ERR("UST app channel %s disable failed for app (pid: %d) "
1504 "and session handle %d with ret %d",
1505 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1508 * This is normal behavior, an application can die during the
1509 * creation process. Don't report an error so the execution can
1510 * continue normally.
1513 DBG3("UST app disable channel failed. Application is dead.");
1518 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1519 ua_chan
->name
, app
->pid
);
1522 health_code_update();
1527 * Enable the specified channel on to UST tracer for the UST session.
1529 static int enable_ust_channel(struct ust_app
*app
,
1530 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1534 health_code_update();
1536 pthread_mutex_lock(&app
->sock_lock
);
1537 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1538 pthread_mutex_unlock(&app
->sock_lock
);
1540 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1541 ERR("UST app channel %s enable failed for app (pid: %d) "
1542 "and session handle %d with ret %d",
1543 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1546 * This is normal behavior, an application can die during the
1547 * creation process. Don't report an error so the execution can
1548 * continue normally.
1551 DBG3("UST app enable channel failed. Application is dead.");
1556 ua_chan
->enabled
= 1;
1558 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1559 ua_chan
->name
, app
->pid
);
1562 health_code_update();
1567 * Enable the specified event on to UST tracer for the UST session.
1569 static int enable_ust_event(struct ust_app
*app
,
1570 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1574 health_code_update();
1576 pthread_mutex_lock(&app
->sock_lock
);
1577 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1578 pthread_mutex_unlock(&app
->sock_lock
);
1580 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1581 ERR("UST app event %s enable failed for app (pid: %d) "
1582 "and session handle %d with ret %d",
1583 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1586 * This is normal behavior, an application can die during the
1587 * creation process. Don't report an error so the execution can
1588 * continue normally.
1591 DBG3("UST app enable event failed. Application is dead.");
1596 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1597 ua_event
->attr
.name
, app
->pid
);
1600 health_code_update();
1605 * Send channel and stream buffer to application.
1607 * Return 0 on success. On error, a negative value is returned.
1609 static int send_channel_pid_to_ust(struct ust_app
*app
,
1610 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1613 struct ust_app_stream
*stream
, *stmp
;
1619 health_code_update();
1621 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1624 /* Send channel to the application. */
1625 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1626 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1627 ret
= -ENOTCONN
; /* Caused by app exiting. */
1629 } else if (ret
< 0) {
1633 health_code_update();
1635 /* Send all streams to application. */
1636 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1637 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1638 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1639 ret
= -ENOTCONN
; /* Caused by app exiting. */
1641 } else if (ret
< 0) {
1644 /* We don't need the stream anymore once sent to the tracer. */
1645 cds_list_del(&stream
->list
);
1646 delete_ust_app_stream(-1, stream
, app
);
1648 /* Flag the channel that it is sent to the application. */
1649 ua_chan
->is_sent
= 1;
1652 health_code_update();
1657 * Create the specified event onto the UST tracer for a UST session.
1659 * Should be called with session mutex held.
1662 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1663 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1667 health_code_update();
1669 /* Create UST event on tracer */
1670 pthread_mutex_lock(&app
->sock_lock
);
1671 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1673 pthread_mutex_unlock(&app
->sock_lock
);
1675 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1676 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1677 ua_event
->attr
.name
, app
->pid
, ret
);
1680 * This is normal behavior, an application can die during the
1681 * creation process. Don't report an error so the execution can
1682 * continue normally.
1685 DBG3("UST app create event failed. Application is dead.");
1690 ua_event
->handle
= ua_event
->obj
->handle
;
1692 DBG2("UST app event %s created successfully for pid:%d",
1693 ua_event
->attr
.name
, app
->pid
);
1695 health_code_update();
1697 /* Set filter if one is present. */
1698 if (ua_event
->filter
) {
1699 ret
= set_ust_event_filter(ua_event
, app
);
1705 /* Set exclusions for the event */
1706 if (ua_event
->exclusion
) {
1707 ret
= set_ust_event_exclusion(ua_event
, app
);
1713 /* If event not enabled, disable it on the tracer */
1714 if (ua_event
->enabled
) {
1716 * We now need to explicitly enable the event, since it
1717 * is now disabled at creation.
1719 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1722 * If we hit an EPERM, something is wrong with our enable call. If
1723 * we get an EEXIST, there is a problem on the tracer side since we
1727 case -LTTNG_UST_ERR_PERM
:
1728 /* Code flow problem */
1730 case -LTTNG_UST_ERR_EXIST
:
1731 /* It's OK for our use case. */
1742 health_code_update();
1747 * Copy data between an UST app event and a LTT event.
1749 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1750 struct ltt_ust_event
*uevent
)
1752 size_t exclusion_alloc_size
;
1754 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1755 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1757 ua_event
->enabled
= uevent
->enabled
;
1759 /* Copy event attributes */
1760 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1762 /* Copy filter bytecode */
1763 if (uevent
->filter
) {
1764 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1765 /* Filter might be NULL here in case of ENONEM. */
1768 /* Copy exclusion data */
1769 if (uevent
->exclusion
) {
1770 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1771 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1772 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1773 if (ua_event
->exclusion
== NULL
) {
1776 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1777 exclusion_alloc_size
);
1783 * Copy data between an UST app channel and a LTT channel.
1785 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1786 struct ltt_ust_channel
*uchan
)
1788 struct lttng_ht_iter iter
;
1789 struct ltt_ust_event
*uevent
;
1790 struct ltt_ust_context
*uctx
;
1791 struct ust_app_event
*ua_event
;
1793 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1795 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1796 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1798 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1799 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1801 /* Copy event attributes since the layout is different. */
1802 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1803 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1804 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1805 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1806 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1807 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1808 ua_chan
->attr
.output
= uchan
->attr
.output
;
1809 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1812 * Note that the attribute channel type is not set since the channel on the
1813 * tracing registry side does not have this information.
1816 ua_chan
->enabled
= uchan
->enabled
;
1817 ua_chan
->tracing_channel_id
= uchan
->id
;
1819 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1820 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1822 if (ua_ctx
== NULL
) {
1825 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1826 (unsigned long) ua_ctx
->ctx
.ctx
);
1827 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1828 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1831 /* Copy all events from ltt ust channel to ust app channel */
1832 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1833 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1834 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1835 if (ua_event
== NULL
) {
1836 DBG2("UST event %s not found on shadow copy channel",
1838 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1839 if (ua_event
== NULL
) {
1842 shadow_copy_event(ua_event
, uevent
);
1843 add_unique_ust_app_event(ua_chan
, ua_event
);
1847 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1851 * Copy data between a UST app session and a regular LTT session.
1853 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1854 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1856 struct lttng_ht_node_str
*ua_chan_node
;
1857 struct lttng_ht_iter iter
;
1858 struct ltt_ust_channel
*uchan
;
1859 struct ust_app_channel
*ua_chan
;
1861 struct tm
*timeinfo
;
1864 char tmp_shm_path
[PATH_MAX
];
1866 /* Get date and time for unique app path */
1868 timeinfo
= localtime(&rawtime
);
1869 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1871 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1873 ua_sess
->tracing_id
= usess
->id
;
1874 ua_sess
->id
= get_next_session_id();
1875 ua_sess
->uid
= app
->uid
;
1876 ua_sess
->gid
= app
->gid
;
1877 ua_sess
->euid
= usess
->uid
;
1878 ua_sess
->egid
= usess
->gid
;
1879 ua_sess
->buffer_type
= usess
->buffer_type
;
1880 ua_sess
->bits_per_long
= app
->bits_per_long
;
1882 /* There is only one consumer object per session possible. */
1883 consumer_output_get(usess
->consumer
);
1884 ua_sess
->consumer
= usess
->consumer
;
1886 ua_sess
->output_traces
= usess
->output_traces
;
1887 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1888 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1889 &usess
->metadata_attr
);
1891 switch (ua_sess
->buffer_type
) {
1892 case LTTNG_BUFFER_PER_PID
:
1893 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1894 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1897 case LTTNG_BUFFER_PER_UID
:
1898 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1899 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1906 PERROR("asprintf UST shadow copy session");
1911 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1912 sizeof(ua_sess
->root_shm_path
));
1913 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1914 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1915 sizeof(ua_sess
->shm_path
));
1916 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1917 if (ua_sess
->shm_path
[0]) {
1918 switch (ua_sess
->buffer_type
) {
1919 case LTTNG_BUFFER_PER_PID
:
1920 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1921 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1922 app
->name
, app
->pid
, datetime
);
1924 case LTTNG_BUFFER_PER_UID
:
1925 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1926 DEFAULT_UST_TRACE_UID_PATH
,
1927 app
->uid
, app
->bits_per_long
);
1934 PERROR("sprintf UST shadow copy session");
1938 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1939 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1940 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1943 /* Iterate over all channels in global domain. */
1944 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1946 struct lttng_ht_iter uiter
;
1948 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1949 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1950 if (ua_chan_node
!= NULL
) {
1951 /* Session exist. Contiuing. */
1955 DBG2("Channel %s not found on shadow session copy, creating it",
1957 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1959 if (ua_chan
== NULL
) {
1960 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1963 shadow_copy_channel(ua_chan
, uchan
);
1965 * The concept of metadata channel does not exist on the tracing
1966 * registry side of the session daemon so this can only be a per CPU
1967 * channel and not metadata.
1969 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1971 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1976 consumer_output_put(ua_sess
->consumer
);
1980 * Lookup sesison wrapper.
1983 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1984 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1986 /* Get right UST app session from app */
1987 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1991 * Return ust app session from the app session hashtable using the UST session
1994 static struct ust_app_session
*lookup_session_by_app(
1995 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1997 struct lttng_ht_iter iter
;
1998 struct lttng_ht_node_u64
*node
;
2000 __lookup_session_by_app(usess
, app
, &iter
);
2001 node
= lttng_ht_iter_get_node_u64(&iter
);
2006 return caa_container_of(node
, struct ust_app_session
, node
);
2013 * Setup buffer registry per PID for the given session and application. If none
2014 * is found, a new one is created, added to the global registry and
2015 * initialized. If regp is valid, it's set with the newly created object.
2017 * Return 0 on success or else a negative value.
2019 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2020 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2023 struct buffer_reg_pid
*reg_pid
;
2030 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2033 * This is the create channel path meaning that if there is NO
2034 * registry available, we have to create one for this session.
2036 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2037 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2045 /* Initialize registry. */
2046 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2047 app
->bits_per_long
, app
->uint8_t_alignment
,
2048 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2049 app
->uint64_t_alignment
, app
->long_alignment
,
2050 app
->byte_order
, app
->version
.major
,
2051 app
->version
.minor
, reg_pid
->root_shm_path
,
2053 ua_sess
->euid
, ua_sess
->egid
);
2056 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2057 * destroy the buffer registry, because it is always expected
2058 * that if the buffer registry can be found, its ust registry is
2061 buffer_reg_pid_destroy(reg_pid
);
2065 buffer_reg_pid_add(reg_pid
);
2067 DBG3("UST app buffer registry per PID created successfully");
2079 * Setup buffer registry per UID for the given session and application. If none
2080 * is found, a new one is created, added to the global registry and
2081 * initialized. If regp is valid, it's set with the newly created object.
2083 * Return 0 on success or else a negative value.
2085 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2086 struct ust_app_session
*ua_sess
,
2087 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2090 struct buffer_reg_uid
*reg_uid
;
2097 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2100 * This is the create channel path meaning that if there is NO
2101 * registry available, we have to create one for this session.
2103 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2104 LTTNG_DOMAIN_UST
, ®_uid
,
2105 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2113 /* Initialize registry. */
2114 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2115 app
->bits_per_long
, app
->uint8_t_alignment
,
2116 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2117 app
->uint64_t_alignment
, app
->long_alignment
,
2118 app
->byte_order
, app
->version
.major
,
2119 app
->version
.minor
, reg_uid
->root_shm_path
,
2120 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2123 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2124 * destroy the buffer registry, because it is always expected
2125 * that if the buffer registry can be found, its ust registry is
2128 buffer_reg_uid_destroy(reg_uid
, NULL
);
2131 /* Add node to teardown list of the session. */
2132 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2134 buffer_reg_uid_add(reg_uid
);
2136 DBG3("UST app buffer registry per UID created successfully");
2147 * Create a session on the tracer side for the given app.
2149 * On success, ua_sess_ptr is populated with the session pointer or else left
2150 * untouched. If the session was created, is_created is set to 1. On error,
2151 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2154 * Returns 0 on success or else a negative code which is either -ENOMEM or
2155 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2157 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2158 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2161 int ret
, created
= 0;
2162 struct ust_app_session
*ua_sess
;
2166 assert(ua_sess_ptr
);
2168 health_code_update();
2170 ua_sess
= lookup_session_by_app(usess
, app
);
2171 if (ua_sess
== NULL
) {
2172 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2173 app
->pid
, usess
->id
);
2174 ua_sess
= alloc_ust_app_session();
2175 if (ua_sess
== NULL
) {
2176 /* Only malloc can failed so something is really wrong */
2180 shadow_copy_session(ua_sess
, usess
, app
);
2184 switch (usess
->buffer_type
) {
2185 case LTTNG_BUFFER_PER_PID
:
2186 /* Init local registry. */
2187 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2189 delete_ust_app_session(-1, ua_sess
, app
);
2193 case LTTNG_BUFFER_PER_UID
:
2194 /* Look for a global registry. If none exists, create one. */
2195 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2197 delete_ust_app_session(-1, ua_sess
, app
);
2207 health_code_update();
2209 if (ua_sess
->handle
== -1) {
2210 pthread_mutex_lock(&app
->sock_lock
);
2211 ret
= ustctl_create_session(app
->sock
);
2212 pthread_mutex_unlock(&app
->sock_lock
);
2214 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2215 ERR("Creating session for app pid %d with ret %d",
2218 DBG("UST app creating session failed. Application is dead");
2220 * This is normal behavior, an application can die during the
2221 * creation process. Don't report an error so the execution can
2222 * continue normally. This will get flagged ENOTCONN and the
2223 * caller will handle it.
2227 delete_ust_app_session(-1, ua_sess
, app
);
2228 if (ret
!= -ENOMEM
) {
2230 * Tracer is probably gone or got an internal error so let's
2231 * behave like it will soon unregister or not usable.
2238 ua_sess
->handle
= ret
;
2240 /* Add ust app session to app's HT */
2241 lttng_ht_node_init_u64(&ua_sess
->node
,
2242 ua_sess
->tracing_id
);
2243 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2244 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2245 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2246 &ua_sess
->ust_objd_node
);
2248 DBG2("UST app session created successfully with handle %d", ret
);
2251 *ua_sess_ptr
= ua_sess
;
2253 *is_created
= created
;
2256 /* Everything went well. */
2260 health_code_update();
2265 * Match function for a hash table lookup of ust_app_ctx.
2267 * It matches an ust app context based on the context type and, in the case
2268 * of perf counters, their name.
2270 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2272 struct ust_app_ctx
*ctx
;
2273 const struct lttng_ust_context_attr
*key
;
2278 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2282 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2287 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2288 if (strncmp(key
->u
.perf_counter
.name
,
2289 ctx
->ctx
.u
.perf_counter
.name
,
2290 sizeof(key
->u
.perf_counter
.name
))) {
2294 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2295 if (strcmp(key
->u
.app_ctx
.provider_name
,
2296 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2297 strcmp(key
->u
.app_ctx
.ctx_name
,
2298 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2314 * Lookup for an ust app context from an lttng_ust_context.
2316 * Must be called while holding RCU read side lock.
2317 * Return an ust_app_ctx object or NULL on error.
2320 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2321 struct lttng_ust_context_attr
*uctx
)
2323 struct lttng_ht_iter iter
;
2324 struct lttng_ht_node_ulong
*node
;
2325 struct ust_app_ctx
*app_ctx
= NULL
;
2330 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2331 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2332 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2333 node
= lttng_ht_iter_get_node_ulong(&iter
);
2338 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2345 * Create a context for the channel on the tracer.
2347 * Called with UST app session lock held and a RCU read side lock.
2350 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2351 struct lttng_ust_context_attr
*uctx
,
2352 struct ust_app
*app
)
2355 struct ust_app_ctx
*ua_ctx
;
2357 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2359 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2365 ua_ctx
= alloc_ust_app_ctx(uctx
);
2366 if (ua_ctx
== NULL
) {
2372 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2373 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2374 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2376 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2386 * Enable on the tracer side a ust app event for the session and channel.
2388 * Called with UST app session lock held.
2391 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2392 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2396 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2401 ua_event
->enabled
= 1;
2408 * Disable on the tracer side a ust app event for the session and channel.
2410 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2411 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2415 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2420 ua_event
->enabled
= 0;
2427 * Lookup ust app channel for session and disable it on the tracer side.
2430 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2431 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2435 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2440 ua_chan
->enabled
= 0;
2447 * Lookup ust app channel for session and enable it on the tracer side. This
2448 * MUST be called with a RCU read side lock acquired.
2450 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2451 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2454 struct lttng_ht_iter iter
;
2455 struct lttng_ht_node_str
*ua_chan_node
;
2456 struct ust_app_channel
*ua_chan
;
2458 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2459 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2460 if (ua_chan_node
== NULL
) {
2461 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2462 uchan
->name
, ua_sess
->tracing_id
);
2466 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2468 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2478 * Ask the consumer to create a channel and get it if successful.
2480 * Called with UST app session lock held.
2482 * Return 0 on success or else a negative value.
2484 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2485 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2486 int bitness
, struct ust_registry_session
*registry
)
2489 unsigned int nb_fd
= 0;
2490 struct consumer_socket
*socket
;
2498 health_code_update();
2500 /* Get the right consumer socket for the application. */
2501 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2507 health_code_update();
2509 /* Need one fd for the channel. */
2510 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2512 ERR("Exhausted number of available FD upon create channel");
2517 * Ask consumer to create channel. The consumer will return the number of
2518 * stream we have to expect.
2520 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2527 * Compute the number of fd needed before receiving them. It must be 2 per
2528 * stream (2 being the default value here).
2530 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2532 /* Reserve the amount of file descriptor we need. */
2533 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2535 ERR("Exhausted number of available FD upon create channel");
2536 goto error_fd_get_stream
;
2539 health_code_update();
2542 * Now get the channel from the consumer. This call wil populate the stream
2543 * list of that channel and set the ust objects.
2545 if (usess
->consumer
->enabled
) {
2546 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2556 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2557 error_fd_get_stream
:
2559 * Initiate a destroy channel on the consumer since we had an error
2560 * handling it on our side. The return value is of no importance since we
2561 * already have a ret value set by the previous error that we need to
2564 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2566 lttng_fd_put(LTTNG_FD_APPS
, 1);
2568 health_code_update();
2574 * Duplicate the ust data object of the ust app stream and save it in the
2575 * buffer registry stream.
2577 * Return 0 on success or else a negative value.
2579 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2580 struct ust_app_stream
*stream
)
2587 /* Reserve the amount of file descriptor we need. */
2588 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2590 ERR("Exhausted number of available FD upon duplicate stream");
2594 /* Duplicate object for stream once the original is in the registry. */
2595 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2596 reg_stream
->obj
.ust
);
2598 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2599 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2600 lttng_fd_put(LTTNG_FD_APPS
, 2);
2603 stream
->handle
= stream
->obj
->handle
;
2610 * Duplicate the ust data object of the ust app. channel and save it in the
2611 * buffer registry channel.
2613 * Return 0 on success or else a negative value.
2615 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2616 struct ust_app_channel
*ua_chan
)
2623 /* Need two fds for the channel. */
2624 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2626 ERR("Exhausted number of available FD upon duplicate channel");
2630 /* Duplicate object for stream once the original is in the registry. */
2631 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2633 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2634 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2637 ua_chan
->handle
= ua_chan
->obj
->handle
;
2642 lttng_fd_put(LTTNG_FD_APPS
, 1);
2648 * For a given channel buffer registry, setup all streams of the given ust
2649 * application channel.
2651 * Return 0 on success or else a negative value.
2653 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2654 struct ust_app_channel
*ua_chan
,
2655 struct ust_app
*app
)
2658 struct ust_app_stream
*stream
, *stmp
;
2663 DBG2("UST app setup buffer registry stream");
2665 /* Send all streams to application. */
2666 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2667 struct buffer_reg_stream
*reg_stream
;
2669 ret
= buffer_reg_stream_create(®_stream
);
2675 * Keep original pointer and nullify it in the stream so the delete
2676 * stream call does not release the object.
2678 reg_stream
->obj
.ust
= stream
->obj
;
2680 buffer_reg_stream_add(reg_stream
, reg_chan
);
2682 /* We don't need the streams anymore. */
2683 cds_list_del(&stream
->list
);
2684 delete_ust_app_stream(-1, stream
, app
);
2692 * Create a buffer registry channel for the given session registry and
2693 * application channel object. If regp pointer is valid, it's set with the
2694 * created object. Important, the created object is NOT added to the session
2695 * registry hash table.
2697 * Return 0 on success else a negative value.
2699 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2700 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2703 struct buffer_reg_channel
*reg_chan
= NULL
;
2708 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2710 /* Create buffer registry channel. */
2711 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2716 reg_chan
->consumer_key
= ua_chan
->key
;
2717 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2718 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2720 /* Create and add a channel registry to session. */
2721 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2722 ua_chan
->tracing_channel_id
);
2726 buffer_reg_channel_add(reg_sess
, reg_chan
);
2735 /* Safe because the registry channel object was not added to any HT. */
2736 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2742 * Setup buffer registry channel for the given session registry and application
2743 * channel object. If regp pointer is valid, it's set with the created object.
2745 * Return 0 on success else a negative value.
2747 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2748 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2749 struct ust_app
*app
)
2756 assert(ua_chan
->obj
);
2758 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2760 /* Setup all streams for the registry. */
2761 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2766 reg_chan
->obj
.ust
= ua_chan
->obj
;
2767 ua_chan
->obj
= NULL
;
2772 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2773 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2778 * Send buffer registry channel to the application.
2780 * Return 0 on success else a negative value.
2782 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2783 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2784 struct ust_app_channel
*ua_chan
)
2787 struct buffer_reg_stream
*reg_stream
;
2794 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2796 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2801 /* Send channel to the application. */
2802 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2803 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2804 ret
= -ENOTCONN
; /* Caused by app exiting. */
2806 } else if (ret
< 0) {
2810 health_code_update();
2812 /* Send all streams to application. */
2813 pthread_mutex_lock(®_chan
->stream_list_lock
);
2814 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2815 struct ust_app_stream stream
;
2817 ret
= duplicate_stream_object(reg_stream
, &stream
);
2819 goto error_stream_unlock
;
2822 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2824 (void) release_ust_app_stream(-1, &stream
, app
);
2825 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2826 ret
= -ENOTCONN
; /* Caused by app exiting. */
2828 goto error_stream_unlock
;
2832 * The return value is not important here. This function will output an
2835 (void) release_ust_app_stream(-1, &stream
, app
);
2837 ua_chan
->is_sent
= 1;
2839 error_stream_unlock
:
2840 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2846 * Create and send to the application the created buffers with per UID buffers.
2848 * This MUST be called with a RCU read side lock acquired.
2849 * The session list lock and the session's lock must be acquired.
2851 * Return 0 on success else a negative value.
2853 static int create_channel_per_uid(struct ust_app
*app
,
2854 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2855 struct ust_app_channel
*ua_chan
)
2858 struct buffer_reg_uid
*reg_uid
;
2859 struct buffer_reg_channel
*reg_chan
;
2866 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2868 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2870 * The session creation handles the creation of this global registry
2871 * object. If none can be find, there is a code flow problem or a
2876 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2882 /* Create the buffer registry channel object. */
2883 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2885 ERR("Error creating the UST channel \"%s\" registry instance",
2891 * Create the buffers on the consumer side. This call populates the
2892 * ust app channel object with all streams and data object.
2894 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2895 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2897 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2901 * Let's remove the previously created buffer registry channel so
2902 * it's not visible anymore in the session registry.
2904 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2905 ua_chan
->tracing_channel_id
, false);
2906 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2907 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2912 * Setup the streams and add it to the session registry.
2914 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2915 ua_chan
, reg_chan
, app
);
2917 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2922 enum lttng_error_code cmd_ret
;
2923 struct ltt_session
*session
;
2924 uint64_t chan_reg_key
;
2925 struct ust_registry_channel
*chan_reg
;
2927 chan_reg_key
= ua_chan
->tracing_channel_id
;
2929 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2930 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2933 chan_reg
->consumer_key
= ua_chan
->key
;
2935 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2937 session
= session_find_by_id(ua_sess
->tracing_id
);
2940 assert(pthread_mutex_trylock(&session
->lock
));
2941 assert(session_trylock_list());
2942 cmd_ret
= notification_thread_command_add_channel(
2943 notification_thread_handle
, session
->name
,
2944 ua_sess
->euid
, ua_sess
->egid
,
2948 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2949 if (cmd_ret
!= LTTNG_OK
) {
2950 ret
= - (int) cmd_ret
;
2951 ERR("Failed to add channel to notification thread");
2957 /* Send buffers to the application. */
2958 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2960 if (ret
!= -ENOTCONN
) {
2961 ERR("Error sending channel to application");
2971 * Create and send to the application the created buffers with per PID buffers.
2973 * Called with UST app session lock held.
2974 * The session list lock and the session's lock must be acquired.
2976 * Return 0 on success else a negative value.
2978 static int create_channel_per_pid(struct ust_app
*app
,
2979 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2980 struct ust_app_channel
*ua_chan
)
2983 struct ust_registry_session
*registry
;
2984 enum lttng_error_code cmd_ret
;
2985 struct ltt_session
*session
;
2986 uint64_t chan_reg_key
;
2987 struct ust_registry_channel
*chan_reg
;
2994 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2998 registry
= get_session_registry(ua_sess
);
2999 /* The UST app session lock is held, registry shall not be null. */
3002 /* Create and add a new channel registry to session. */
3003 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3005 ERR("Error creating the UST channel \"%s\" registry instance",
3010 /* Create and get channel on the consumer side. */
3011 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3012 app
->bits_per_long
, registry
);
3014 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3019 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3021 if (ret
!= -ENOTCONN
) {
3022 ERR("Error sending channel to application");
3027 session
= session_find_by_id(ua_sess
->tracing_id
);
3030 chan_reg_key
= ua_chan
->key
;
3031 pthread_mutex_lock(®istry
->lock
);
3032 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3034 chan_reg
->consumer_key
= ua_chan
->key
;
3035 pthread_mutex_unlock(®istry
->lock
);
3037 assert(pthread_mutex_trylock(&session
->lock
));
3038 assert(session_trylock_list());
3040 cmd_ret
= notification_thread_command_add_channel(
3041 notification_thread_handle
, session
->name
,
3042 ua_sess
->euid
, ua_sess
->egid
,
3046 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3047 if (cmd_ret
!= LTTNG_OK
) {
3048 ret
= - (int) cmd_ret
;
3049 ERR("Failed to add channel to notification thread");
3059 * From an already allocated ust app channel, create the channel buffers if
3060 * need and send it to the application. This MUST be called with a RCU read
3061 * side lock acquired.
3063 * Called with UST app session lock held.
3065 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3066 * the application exited concurrently.
3068 static int do_create_channel(struct ust_app
*app
,
3069 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3070 struct ust_app_channel
*ua_chan
)
3079 /* Handle buffer type before sending the channel to the application. */
3080 switch (usess
->buffer_type
) {
3081 case LTTNG_BUFFER_PER_UID
:
3083 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3089 case LTTNG_BUFFER_PER_PID
:
3091 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3103 /* Initialize ust objd object using the received handle and add it. */
3104 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3105 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3107 /* If channel is not enabled, disable it on the tracer */
3108 if (!ua_chan
->enabled
) {
3109 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3120 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3121 * newly created channel if not NULL.
3123 * Called with UST app session lock and RCU read-side lock held.
3125 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3126 * the application exited concurrently.
3128 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3129 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3130 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3131 struct ust_app_channel
**ua_chanp
)
3134 struct lttng_ht_iter iter
;
3135 struct lttng_ht_node_str
*ua_chan_node
;
3136 struct ust_app_channel
*ua_chan
;
3138 /* Lookup channel in the ust app session */
3139 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3140 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3141 if (ua_chan_node
!= NULL
) {
3142 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3146 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3147 if (ua_chan
== NULL
) {
3148 /* Only malloc can fail here */
3152 shadow_copy_channel(ua_chan
, uchan
);
3154 /* Set channel type. */
3155 ua_chan
->attr
.type
= type
;
3157 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3162 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3165 /* Only add the channel if successful on the tracer side. */
3166 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3169 *ua_chanp
= ua_chan
;
3172 /* Everything went well. */
3176 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3182 * Create UST app event and create it on the tracer side.
3184 * Called with ust app session mutex held.
3187 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3188 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3189 struct ust_app
*app
)
3192 struct ust_app_event
*ua_event
;
3194 /* Get event node */
3195 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3196 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3197 if (ua_event
!= NULL
) {
3202 /* Does not exist so create one */
3203 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3204 if (ua_event
== NULL
) {
3205 /* Only malloc can failed so something is really wrong */
3209 shadow_copy_event(ua_event
, uevent
);
3211 /* Create it on the tracer side */
3212 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3214 /* Not found previously means that it does not exist on the tracer */
3215 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3219 add_unique_ust_app_event(ua_chan
, ua_event
);
3221 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3228 /* Valid. Calling here is already in a read side lock */
3229 delete_ust_app_event(-1, ua_event
, app
);
3234 * Create UST metadata and open it on the tracer side.
3236 * Called with UST app session lock held and RCU read side lock.
3238 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3239 struct ust_app
*app
, struct consumer_output
*consumer
)
3242 struct ust_app_channel
*metadata
;
3243 struct consumer_socket
*socket
;
3244 struct ust_registry_session
*registry
;
3250 registry
= get_session_registry(ua_sess
);
3251 /* The UST app session is held registry shall not be null. */
3254 pthread_mutex_lock(®istry
->lock
);
3256 /* Metadata already exists for this registry or it was closed previously */
3257 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3262 /* Allocate UST metadata */
3263 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3265 /* malloc() failed */
3270 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3272 /* Need one fd for the channel. */
3273 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3275 ERR("Exhausted number of available FD upon create metadata");
3279 /* Get the right consumer socket for the application. */
3280 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3283 goto error_consumer
;
3287 * Keep metadata key so we can identify it on the consumer side. Assign it
3288 * to the registry *before* we ask the consumer so we avoid the race of the
3289 * consumer requesting the metadata and the ask_channel call on our side
3290 * did not returned yet.
3292 registry
->metadata_key
= metadata
->key
;
3295 * Ask the metadata channel creation to the consumer. The metadata object
3296 * will be created by the consumer and kept their. However, the stream is
3297 * never added or monitored until we do a first push metadata to the
3300 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3303 /* Nullify the metadata key so we don't try to close it later on. */
3304 registry
->metadata_key
= 0;
3305 goto error_consumer
;
3309 * The setup command will make the metadata stream be sent to the relayd,
3310 * if applicable, and the thread managing the metadatas. This is important
3311 * because after this point, if an error occurs, the only way the stream
3312 * can be deleted is to be monitored in the consumer.
3314 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3316 /* Nullify the metadata key so we don't try to close it later on. */
3317 registry
->metadata_key
= 0;
3318 goto error_consumer
;
3321 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3322 metadata
->key
, app
->pid
);
3325 lttng_fd_put(LTTNG_FD_APPS
, 1);
3326 delete_ust_app_channel(-1, metadata
, app
);
3328 pthread_mutex_unlock(®istry
->lock
);
3333 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3334 * acquired before calling this function.
3336 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3338 struct ust_app
*app
= NULL
;
3339 struct lttng_ht_node_ulong
*node
;
3340 struct lttng_ht_iter iter
;
3342 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3343 node
= lttng_ht_iter_get_node_ulong(&iter
);
3345 DBG2("UST app no found with pid %d", pid
);
3349 DBG2("Found UST app by pid %d", pid
);
3351 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3358 * Allocate and init an UST app object using the registration information and
3359 * the command socket. This is called when the command socket connects to the
3362 * The object is returned on success or else NULL.
3364 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3366 struct ust_app
*lta
= NULL
;
3371 DBG3("UST app creating application for socket %d", sock
);
3373 if ((msg
->bits_per_long
== 64 &&
3374 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3375 || (msg
->bits_per_long
== 32 &&
3376 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3377 ERR("Registration failed: application \"%s\" (pid: %d) has "
3378 "%d-bit long, but no consumerd for this size is available.\n",
3379 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3383 lta
= zmalloc(sizeof(struct ust_app
));
3389 lta
->ppid
= msg
->ppid
;
3390 lta
->uid
= msg
->uid
;
3391 lta
->gid
= msg
->gid
;
3393 lta
->bits_per_long
= msg
->bits_per_long
;
3394 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3395 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3396 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3397 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3398 lta
->long_alignment
= msg
->long_alignment
;
3399 lta
->byte_order
= msg
->byte_order
;
3401 lta
->v_major
= msg
->major
;
3402 lta
->v_minor
= msg
->minor
;
3403 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3404 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3405 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3406 lta
->notify_sock
= -1;
3408 /* Copy name and make sure it's NULL terminated. */
3409 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3410 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3413 * Before this can be called, when receiving the registration information,
3414 * the application compatibility is checked. So, at this point, the
3415 * application can work with this session daemon.
3417 lta
->compatible
= 1;
3419 lta
->pid
= msg
->pid
;
3420 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3422 pthread_mutex_init(<a
->sock_lock
, NULL
);
3423 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3425 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3431 * For a given application object, add it to every hash table.
3433 void ust_app_add(struct ust_app
*app
)
3436 assert(app
->notify_sock
>= 0);
3441 * On a re-registration, we want to kick out the previous registration of
3444 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3447 * The socket _should_ be unique until _we_ call close. So, a add_unique
3448 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3449 * already in the table.
3451 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3453 /* Add application to the notify socket hash table. */
3454 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3455 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3457 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3458 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3459 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3466 * Set the application version into the object.
3468 * Return 0 on success else a negative value either an errno code or a
3469 * LTTng-UST error code.
3471 int ust_app_version(struct ust_app
*app
)
3477 pthread_mutex_lock(&app
->sock_lock
);
3478 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3479 pthread_mutex_unlock(&app
->sock_lock
);
3481 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3482 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3484 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3492 * Unregister app by removing it from the global traceable app list and freeing
3495 * The socket is already closed at this point so no close to sock.
3497 void ust_app_unregister(int sock
)
3499 struct ust_app
*lta
;
3500 struct lttng_ht_node_ulong
*node
;
3501 struct lttng_ht_iter ust_app_sock_iter
;
3502 struct lttng_ht_iter iter
;
3503 struct ust_app_session
*ua_sess
;
3508 /* Get the node reference for a call_rcu */
3509 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3510 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3513 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3514 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3517 * For per-PID buffers, perform "push metadata" and flush all
3518 * application streams before removing app from hash tables,
3519 * ensuring proper behavior of data_pending check.
3520 * Remove sessions so they are not visible during deletion.
3522 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3524 struct ust_registry_session
*registry
;
3526 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3528 /* The session was already removed so scheduled for teardown. */
3532 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3533 (void) ust_app_flush_app_session(lta
, ua_sess
);
3537 * Add session to list for teardown. This is safe since at this point we
3538 * are the only one using this list.
3540 pthread_mutex_lock(&ua_sess
->lock
);
3542 if (ua_sess
->deleted
) {
3543 pthread_mutex_unlock(&ua_sess
->lock
);
3548 * Normally, this is done in the delete session process which is
3549 * executed in the call rcu below. However, upon registration we can't
3550 * afford to wait for the grace period before pushing data or else the
3551 * data pending feature can race between the unregistration and stop
3552 * command where the data pending command is sent *before* the grace
3555 * The close metadata below nullifies the metadata pointer in the
3556 * session so the delete session will NOT push/close a second time.
3558 registry
= get_session_registry(ua_sess
);
3560 /* Push metadata for application before freeing the application. */
3561 (void) push_metadata(registry
, ua_sess
->consumer
);
3564 * Don't ask to close metadata for global per UID buffers. Close
3565 * metadata only on destroy trace session in this case. Also, the
3566 * previous push metadata could have flag the metadata registry to
3567 * close so don't send a close command if closed.
3569 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3570 /* And ask to close it for this session registry. */
3571 (void) close_metadata(registry
, ua_sess
->consumer
);
3574 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3576 pthread_mutex_unlock(&ua_sess
->lock
);
3579 /* Remove application from PID hash table */
3580 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3584 * Remove application from notify hash table. The thread handling the
3585 * notify socket could have deleted the node so ignore on error because
3586 * either way it's valid. The close of that socket is handled by the
3587 * apps_notify_thread.
3589 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3590 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3593 * Ignore return value since the node might have been removed before by an
3594 * add replace during app registration because the PID can be reassigned by
3597 iter
.iter
.node
= <a
->pid_n
.node
;
3598 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3600 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3605 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3612 * Fill events array with all events name of all registered apps.
3614 int ust_app_list_events(struct lttng_event
**events
)
3617 size_t nbmem
, count
= 0;
3618 struct lttng_ht_iter iter
;
3619 struct ust_app
*app
;
3620 struct lttng_event
*tmp_event
;
3622 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3623 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3624 if (tmp_event
== NULL
) {
3625 PERROR("zmalloc ust app events");
3632 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3633 struct lttng_ust_tracepoint_iter uiter
;
3635 health_code_update();
3637 if (!app
->compatible
) {
3639 * TODO: In time, we should notice the caller of this error by
3640 * telling him that this is a version error.
3644 pthread_mutex_lock(&app
->sock_lock
);
3645 handle
= ustctl_tracepoint_list(app
->sock
);
3647 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3648 ERR("UST app list events getting handle failed for app pid %d",
3651 pthread_mutex_unlock(&app
->sock_lock
);
3655 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3656 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3657 /* Handle ustctl error. */
3661 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3662 ERR("UST app tp list get failed for app %d with ret %d",
3665 DBG3("UST app tp list get failed. Application is dead");
3667 * This is normal behavior, an application can die during the
3668 * creation process. Don't report an error so the execution can
3669 * continue normally. Continue normal execution.
3674 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3675 if (release_ret
< 0 &&
3676 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3677 release_ret
!= -EPIPE
) {
3678 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3680 pthread_mutex_unlock(&app
->sock_lock
);
3684 health_code_update();
3685 if (count
>= nbmem
) {
3686 /* In case the realloc fails, we free the memory */
3687 struct lttng_event
*new_tmp_event
;
3690 new_nbmem
= nbmem
<< 1;
3691 DBG2("Reallocating event list from %zu to %zu entries",
3693 new_tmp_event
= realloc(tmp_event
,
3694 new_nbmem
* sizeof(struct lttng_event
));
3695 if (new_tmp_event
== NULL
) {
3698 PERROR("realloc ust app events");
3701 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3702 if (release_ret
< 0 &&
3703 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3704 release_ret
!= -EPIPE
) {
3705 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3707 pthread_mutex_unlock(&app
->sock_lock
);
3710 /* Zero the new memory */
3711 memset(new_tmp_event
+ nbmem
, 0,
3712 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3714 tmp_event
= new_tmp_event
;
3716 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3717 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3718 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3719 tmp_event
[count
].pid
= app
->pid
;
3720 tmp_event
[count
].enabled
= -1;
3723 ret
= ustctl_release_handle(app
->sock
, handle
);
3724 pthread_mutex_unlock(&app
->sock_lock
);
3725 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3726 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3731 *events
= tmp_event
;
3733 DBG2("UST app list events done (%zu events)", count
);
3738 health_code_update();
3743 * Fill events array with all events name of all registered apps.
3745 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3748 size_t nbmem
, count
= 0;
3749 struct lttng_ht_iter iter
;
3750 struct ust_app
*app
;
3751 struct lttng_event_field
*tmp_event
;
3753 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3754 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3755 if (tmp_event
== NULL
) {
3756 PERROR("zmalloc ust app event fields");
3763 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3764 struct lttng_ust_field_iter uiter
;
3766 health_code_update();
3768 if (!app
->compatible
) {
3770 * TODO: In time, we should notice the caller of this error by
3771 * telling him that this is a version error.
3775 pthread_mutex_lock(&app
->sock_lock
);
3776 handle
= ustctl_tracepoint_field_list(app
->sock
);
3778 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3779 ERR("UST app list field getting handle failed for app pid %d",
3782 pthread_mutex_unlock(&app
->sock_lock
);
3786 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3787 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3788 /* Handle ustctl error. */
3792 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3793 ERR("UST app tp list field failed for app %d with ret %d",
3796 DBG3("UST app tp list field failed. Application is dead");
3798 * This is normal behavior, an application can die during the
3799 * creation process. Don't report an error so the execution can
3800 * continue normally. Reset list and count for next app.
3805 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3806 pthread_mutex_unlock(&app
->sock_lock
);
3807 if (release_ret
< 0 &&
3808 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3809 release_ret
!= -EPIPE
) {
3810 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3815 health_code_update();
3816 if (count
>= nbmem
) {
3817 /* In case the realloc fails, we free the memory */
3818 struct lttng_event_field
*new_tmp_event
;
3821 new_nbmem
= nbmem
<< 1;
3822 DBG2("Reallocating event field list from %zu to %zu entries",
3824 new_tmp_event
= realloc(tmp_event
,
3825 new_nbmem
* sizeof(struct lttng_event_field
));
3826 if (new_tmp_event
== NULL
) {
3829 PERROR("realloc ust app event fields");
3832 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3833 pthread_mutex_unlock(&app
->sock_lock
);
3835 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3836 release_ret
!= -EPIPE
) {
3837 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3841 /* Zero the new memory */
3842 memset(new_tmp_event
+ nbmem
, 0,
3843 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3845 tmp_event
= new_tmp_event
;
3848 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3849 /* Mapping between these enums matches 1 to 1. */
3850 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3851 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3853 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3854 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3855 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3856 tmp_event
[count
].event
.pid
= app
->pid
;
3857 tmp_event
[count
].event
.enabled
= -1;
3860 ret
= ustctl_release_handle(app
->sock
, handle
);
3861 pthread_mutex_unlock(&app
->sock_lock
);
3863 ret
!= -LTTNG_UST_ERR_EXITING
&&
3865 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3870 *fields
= tmp_event
;
3872 DBG2("UST app list event fields done (%zu events)", count
);
3877 health_code_update();
3882 * Free and clean all traceable apps of the global list.
3884 * Should _NOT_ be called with RCU read-side lock held.
3886 void ust_app_clean_list(void)
3889 struct ust_app
*app
;
3890 struct lttng_ht_iter iter
;
3892 DBG2("UST app cleaning registered apps hash table");
3897 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3898 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3900 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3904 /* Cleanup socket hash table */
3905 if (ust_app_ht_by_sock
) {
3906 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3908 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3913 /* Cleanup notify socket hash table */
3914 if (ust_app_ht_by_notify_sock
) {
3915 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3916 notify_sock_n
.node
) {
3917 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3923 /* Destroy is done only when the ht is empty */
3925 ht_cleanup_push(ust_app_ht
);
3927 if (ust_app_ht_by_sock
) {
3928 ht_cleanup_push(ust_app_ht_by_sock
);
3930 if (ust_app_ht_by_notify_sock
) {
3931 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3936 * Init UST app hash table.
3938 int ust_app_ht_alloc(void)
3940 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3944 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3945 if (!ust_app_ht_by_sock
) {
3948 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3949 if (!ust_app_ht_by_notify_sock
) {
3956 * For a specific UST session, disable the channel for all registered apps.
3958 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3959 struct ltt_ust_channel
*uchan
)
3962 struct lttng_ht_iter iter
;
3963 struct lttng_ht_node_str
*ua_chan_node
;
3964 struct ust_app
*app
;
3965 struct ust_app_session
*ua_sess
;
3966 struct ust_app_channel
*ua_chan
;
3968 if (usess
== NULL
|| uchan
== NULL
) {
3969 ERR("Disabling UST global channel with NULL values");
3974 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3975 uchan
->name
, usess
->id
);
3979 /* For every registered applications */
3980 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3981 struct lttng_ht_iter uiter
;
3982 if (!app
->compatible
) {
3984 * TODO: In time, we should notice the caller of this error by
3985 * telling him that this is a version error.
3989 ua_sess
= lookup_session_by_app(usess
, app
);
3990 if (ua_sess
== NULL
) {
3995 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3996 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3997 /* If the session if found for the app, the channel must be there */
3998 assert(ua_chan_node
);
4000 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4001 /* The channel must not be already disabled */
4002 assert(ua_chan
->enabled
== 1);
4004 /* Disable channel onto application */
4005 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4007 /* XXX: We might want to report this error at some point... */
4019 * For a specific UST session, enable the channel for all registered apps.
4021 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4022 struct ltt_ust_channel
*uchan
)
4025 struct lttng_ht_iter iter
;
4026 struct ust_app
*app
;
4027 struct ust_app_session
*ua_sess
;
4029 if (usess
== NULL
|| uchan
== NULL
) {
4030 ERR("Adding UST global channel to NULL values");
4035 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4036 uchan
->name
, usess
->id
);
4040 /* For every registered applications */
4041 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4042 if (!app
->compatible
) {
4044 * TODO: In time, we should notice the caller of this error by
4045 * telling him that this is a version error.
4049 ua_sess
= lookup_session_by_app(usess
, app
);
4050 if (ua_sess
== NULL
) {
4054 /* Enable channel onto application */
4055 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4057 /* XXX: We might want to report this error at some point... */
4069 * Disable an event in a channel and for a specific session.
4071 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4072 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4075 struct lttng_ht_iter iter
, uiter
;
4076 struct lttng_ht_node_str
*ua_chan_node
;
4077 struct ust_app
*app
;
4078 struct ust_app_session
*ua_sess
;
4079 struct ust_app_channel
*ua_chan
;
4080 struct ust_app_event
*ua_event
;
4082 DBG("UST app disabling event %s for all apps in channel "
4083 "%s for session id %" PRIu64
,
4084 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4088 /* For all registered applications */
4089 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4090 if (!app
->compatible
) {
4092 * TODO: In time, we should notice the caller of this error by
4093 * telling him that this is a version error.
4097 ua_sess
= lookup_session_by_app(usess
, app
);
4098 if (ua_sess
== NULL
) {
4103 /* Lookup channel in the ust app session */
4104 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4105 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4106 if (ua_chan_node
== NULL
) {
4107 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4108 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4111 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4113 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4114 uevent
->filter
, uevent
->attr
.loglevel
,
4116 if (ua_event
== NULL
) {
4117 DBG2("Event %s not found in channel %s for app pid %d."
4118 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4122 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4124 /* XXX: Report error someday... */
4135 * For a specific UST session, create the channel for all registered apps.
4137 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4138 struct ltt_ust_channel
*uchan
)
4140 int ret
= 0, created
;
4141 struct lttng_ht_iter iter
;
4142 struct ust_app
*app
;
4143 struct ust_app_session
*ua_sess
= NULL
;
4145 /* Very wrong code flow */
4149 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4150 uchan
->name
, usess
->id
);
4154 /* For every registered applications */
4155 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4156 if (!app
->compatible
) {
4158 * TODO: In time, we should notice the caller of this error by
4159 * telling him that this is a version error.
4163 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4169 * Create session on the tracer side and add it to app session HT. Note
4170 * that if session exist, it will simply return a pointer to the ust
4173 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, &created
);
4178 * The application's socket is not valid. Either a bad socket
4179 * or a timeout on it. We can't inform the caller that for a
4180 * specific app, the session failed so lets continue here.
4182 ret
= 0; /* Not an error. */
4186 goto error_rcu_unlock
;
4191 pthread_mutex_lock(&ua_sess
->lock
);
4193 if (ua_sess
->deleted
) {
4194 pthread_mutex_unlock(&ua_sess
->lock
);
4198 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4199 sizeof(uchan
->name
))) {
4200 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
4203 /* Create channel onto application. We don't need the chan ref. */
4204 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
4205 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
4207 pthread_mutex_unlock(&ua_sess
->lock
);
4209 /* Cleanup the created session if it's the case. */
4211 destroy_app_session(app
, ua_sess
);
4216 * The application's socket is not valid. Either a bad socket
4217 * or a timeout on it. We can't inform the caller that for a
4218 * specific app, the session failed so lets continue here.
4220 ret
= 0; /* Not an error. */
4224 goto error_rcu_unlock
;
4235 * Enable event for a specific session and channel on the tracer.
4237 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4238 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4241 struct lttng_ht_iter iter
, uiter
;
4242 struct lttng_ht_node_str
*ua_chan_node
;
4243 struct ust_app
*app
;
4244 struct ust_app_session
*ua_sess
;
4245 struct ust_app_channel
*ua_chan
;
4246 struct ust_app_event
*ua_event
;
4248 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4249 uevent
->attr
.name
, usess
->id
);
4252 * NOTE: At this point, this function is called only if the session and
4253 * channel passed are already created for all apps. and enabled on the
4259 /* For all registered applications */
4260 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4261 if (!app
->compatible
) {
4263 * TODO: In time, we should notice the caller of this error by
4264 * telling him that this is a version error.
4268 ua_sess
= lookup_session_by_app(usess
, app
);
4270 /* The application has problem or is probably dead. */
4274 pthread_mutex_lock(&ua_sess
->lock
);
4276 if (ua_sess
->deleted
) {
4277 pthread_mutex_unlock(&ua_sess
->lock
);
4281 /* Lookup channel in the ust app session */
4282 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4283 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4285 * It is possible that the channel cannot be found is
4286 * the channel/event creation occurs concurrently with
4287 * an application exit.
4289 if (!ua_chan_node
) {
4290 pthread_mutex_unlock(&ua_sess
->lock
);
4294 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4296 /* Get event node */
4297 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4298 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4299 if (ua_event
== NULL
) {
4300 DBG3("UST app enable event %s not found for app PID %d."
4301 "Skipping app", uevent
->attr
.name
, app
->pid
);
4305 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4307 pthread_mutex_unlock(&ua_sess
->lock
);
4311 pthread_mutex_unlock(&ua_sess
->lock
);
4320 * For a specific existing UST session and UST channel, creates the event for
4321 * all registered apps.
4323 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4324 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4327 struct lttng_ht_iter iter
, uiter
;
4328 struct lttng_ht_node_str
*ua_chan_node
;
4329 struct ust_app
*app
;
4330 struct ust_app_session
*ua_sess
;
4331 struct ust_app_channel
*ua_chan
;
4333 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4334 uevent
->attr
.name
, usess
->id
);
4338 /* For all registered applications */
4339 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4340 if (!app
->compatible
) {
4342 * TODO: In time, we should notice the caller of this error by
4343 * telling him that this is a version error.
4347 ua_sess
= lookup_session_by_app(usess
, app
);
4349 /* The application has problem or is probably dead. */
4353 pthread_mutex_lock(&ua_sess
->lock
);
4355 if (ua_sess
->deleted
) {
4356 pthread_mutex_unlock(&ua_sess
->lock
);
4360 /* Lookup channel in the ust app session */
4361 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4362 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4363 /* If the channel is not found, there is a code flow error */
4364 assert(ua_chan_node
);
4366 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4368 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4369 pthread_mutex_unlock(&ua_sess
->lock
);
4371 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4372 /* Possible value at this point: -ENOMEM. If so, we stop! */
4375 DBG2("UST app event %s already exist on app PID %d",
4376 uevent
->attr
.name
, app
->pid
);
4387 * Start tracing for a specific UST session and app.
4389 * Called with UST app session lock held.
4393 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4396 struct ust_app_session
*ua_sess
;
4398 DBG("Starting tracing for ust app pid %d", app
->pid
);
4402 if (!app
->compatible
) {
4406 ua_sess
= lookup_session_by_app(usess
, app
);
4407 if (ua_sess
== NULL
) {
4408 /* The session is in teardown process. Ignore and continue. */
4412 pthread_mutex_lock(&ua_sess
->lock
);
4414 if (ua_sess
->deleted
) {
4415 pthread_mutex_unlock(&ua_sess
->lock
);
4419 /* Upon restart, we skip the setup, already done */
4420 if (ua_sess
->started
) {
4424 /* Create directories if consumer is LOCAL and has a path defined. */
4425 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4426 usess
->consumer
->dst
.session_root_path
[0] != '\0') {
4429 tmp_path
= zmalloc(LTTNG_PATH_MAX
);
4431 ERR("Alloc tmp_path");
4434 ret
= snprintf(tmp_path
, LTTNG_PATH_MAX
, "%s%s%s",
4435 usess
->consumer
->dst
.session_root_path
,
4436 usess
->consumer
->chunk_path
,
4437 usess
->consumer
->subdir
);
4438 if (ret
>= LTTNG_PATH_MAX
) {
4439 ERR("Local destination path exceeds the maximal allowed length of %i bytes (needs %i bytes) with path = \"%s%s%s\"",
4440 LTTNG_PATH_MAX
, ret
,
4441 usess
->consumer
->dst
.session_root_path
,
4442 usess
->consumer
->chunk_path
,
4443 usess
->consumer
->subdir
);
4447 DBG("Creating directory path for local tracing: \"%s\"",
4449 ret
= run_as_mkdir_recursive(tmp_path
, S_IRWXU
| S_IRWXG
,
4450 ua_sess
->euid
, ua_sess
->egid
);
4453 if (errno
!= EEXIST
) {
4454 ERR("Trace directory creation error");
4461 * Create the metadata for the application. This returns gracefully if a
4462 * metadata was already set for the session.
4464 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4469 health_code_update();
4472 /* This start the UST tracing */
4473 pthread_mutex_lock(&app
->sock_lock
);
4474 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4475 pthread_mutex_unlock(&app
->sock_lock
);
4477 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4478 ERR("Error starting tracing for app pid: %d (ret: %d)",
4481 DBG("UST app start session failed. Application is dead.");
4483 * This is normal behavior, an application can die during the
4484 * creation process. Don't report an error so the execution can
4485 * continue normally.
4487 pthread_mutex_unlock(&ua_sess
->lock
);
4493 /* Indicate that the session has been started once */
4494 ua_sess
->started
= 1;
4496 pthread_mutex_unlock(&ua_sess
->lock
);
4498 health_code_update();
4500 /* Quiescent wait after starting trace */
4501 pthread_mutex_lock(&app
->sock_lock
);
4502 ret
= ustctl_wait_quiescent(app
->sock
);
4503 pthread_mutex_unlock(&app
->sock_lock
);
4504 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4505 ERR("UST app wait quiescent failed for app pid %d ret %d",
4511 health_code_update();
4515 pthread_mutex_unlock(&ua_sess
->lock
);
4517 health_code_update();
4522 * Stop tracing for a specific UST session and app.
4525 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4528 struct ust_app_session
*ua_sess
;
4529 struct ust_registry_session
*registry
;
4531 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4535 if (!app
->compatible
) {
4536 goto end_no_session
;
4539 ua_sess
= lookup_session_by_app(usess
, app
);
4540 if (ua_sess
== NULL
) {
4541 goto end_no_session
;
4544 pthread_mutex_lock(&ua_sess
->lock
);
4546 if (ua_sess
->deleted
) {
4547 pthread_mutex_unlock(&ua_sess
->lock
);
4548 goto end_no_session
;
4552 * If started = 0, it means that stop trace has been called for a session
4553 * that was never started. It's possible since we can have a fail start
4554 * from either the application manager thread or the command thread. Simply
4555 * indicate that this is a stop error.
4557 if (!ua_sess
->started
) {
4558 goto error_rcu_unlock
;
4561 health_code_update();
4563 /* This inhibits UST tracing */
4564 pthread_mutex_lock(&app
->sock_lock
);
4565 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4566 pthread_mutex_unlock(&app
->sock_lock
);
4568 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4569 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4572 DBG("UST app stop session failed. Application is dead.");
4574 * This is normal behavior, an application can die during the
4575 * creation process. Don't report an error so the execution can
4576 * continue normally.
4580 goto error_rcu_unlock
;
4583 health_code_update();
4585 /* Quiescent wait after stopping trace */
4586 pthread_mutex_lock(&app
->sock_lock
);
4587 ret
= ustctl_wait_quiescent(app
->sock
);
4588 pthread_mutex_unlock(&app
->sock_lock
);
4589 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4590 ERR("UST app wait quiescent failed for app pid %d ret %d",
4594 health_code_update();
4596 registry
= get_session_registry(ua_sess
);
4598 /* The UST app session is held registry shall not be null. */
4601 /* Push metadata for application before freeing the application. */
4602 (void) push_metadata(registry
, ua_sess
->consumer
);
4605 pthread_mutex_unlock(&ua_sess
->lock
);
4608 health_code_update();
4612 pthread_mutex_unlock(&ua_sess
->lock
);
4614 health_code_update();
4619 int ust_app_flush_app_session(struct ust_app
*app
,
4620 struct ust_app_session
*ua_sess
)
4622 int ret
, retval
= 0;
4623 struct lttng_ht_iter iter
;
4624 struct ust_app_channel
*ua_chan
;
4625 struct consumer_socket
*socket
;
4627 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4631 if (!app
->compatible
) {
4632 goto end_not_compatible
;
4635 pthread_mutex_lock(&ua_sess
->lock
);
4637 if (ua_sess
->deleted
) {
4641 health_code_update();
4643 /* Flushing buffers */
4644 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4647 /* Flush buffers and push metadata. */
4648 switch (ua_sess
->buffer_type
) {
4649 case LTTNG_BUFFER_PER_PID
:
4650 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4652 health_code_update();
4653 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4655 ERR("Error flushing consumer channel");
4661 case LTTNG_BUFFER_PER_UID
:
4667 health_code_update();
4670 pthread_mutex_unlock(&ua_sess
->lock
);
4674 health_code_update();
4679 * Flush buffers for all applications for a specific UST session.
4680 * Called with UST session lock held.
4683 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4688 DBG("Flushing session buffers for all ust apps");
4692 /* Flush buffers and push metadata. */
4693 switch (usess
->buffer_type
) {
4694 case LTTNG_BUFFER_PER_UID
:
4696 struct buffer_reg_uid
*reg
;
4697 struct lttng_ht_iter iter
;
4699 /* Flush all per UID buffers associated to that session. */
4700 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4701 struct ust_registry_session
*ust_session_reg
;
4702 struct buffer_reg_channel
*reg_chan
;
4703 struct consumer_socket
*socket
;
4705 /* Get consumer socket to use to push the metadata.*/
4706 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4709 /* Ignore request if no consumer is found for the session. */
4713 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4714 reg_chan
, node
.node
) {
4716 * The following call will print error values so the return
4717 * code is of little importance because whatever happens, we
4718 * have to try them all.
4720 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4723 ust_session_reg
= reg
->registry
->reg
.ust
;
4724 /* Push metadata. */
4725 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4729 case LTTNG_BUFFER_PER_PID
:
4731 struct ust_app_session
*ua_sess
;
4732 struct lttng_ht_iter iter
;
4733 struct ust_app
*app
;
4735 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4736 ua_sess
= lookup_session_by_app(usess
, app
);
4737 if (ua_sess
== NULL
) {
4740 (void) ust_app_flush_app_session(app
, ua_sess
);
4751 health_code_update();
4756 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4757 struct ust_app_session
*ua_sess
)
4760 struct lttng_ht_iter iter
;
4761 struct ust_app_channel
*ua_chan
;
4762 struct consumer_socket
*socket
;
4764 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4768 if (!app
->compatible
) {
4769 goto end_not_compatible
;
4772 pthread_mutex_lock(&ua_sess
->lock
);
4774 if (ua_sess
->deleted
) {
4778 health_code_update();
4780 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4783 ERR("Failed to find consumer (%" PRIu32
") socket",
4784 app
->bits_per_long
);
4789 /* Clear quiescent state. */
4790 switch (ua_sess
->buffer_type
) {
4791 case LTTNG_BUFFER_PER_PID
:
4792 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4793 ua_chan
, node
.node
) {
4794 health_code_update();
4795 ret
= consumer_clear_quiescent_channel(socket
,
4798 ERR("Error clearing quiescent state for consumer channel");
4804 case LTTNG_BUFFER_PER_UID
:
4811 health_code_update();
4814 pthread_mutex_unlock(&ua_sess
->lock
);
4818 health_code_update();
4823 * Clear quiescent state in each stream for all applications for a
4824 * specific UST session.
4825 * Called with UST session lock held.
4828 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4833 DBG("Clearing stream quiescent state for all ust apps");
4837 switch (usess
->buffer_type
) {
4838 case LTTNG_BUFFER_PER_UID
:
4840 struct lttng_ht_iter iter
;
4841 struct buffer_reg_uid
*reg
;
4844 * Clear quiescent for all per UID buffers associated to
4847 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4848 struct consumer_socket
*socket
;
4849 struct buffer_reg_channel
*reg_chan
;
4851 /* Get associated consumer socket.*/
4852 socket
= consumer_find_socket_by_bitness(
4853 reg
->bits_per_long
, usess
->consumer
);
4856 * Ignore request if no consumer is found for
4862 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4863 &iter
.iter
, reg_chan
, node
.node
) {
4865 * The following call will print error values so
4866 * the return code is of little importance
4867 * because whatever happens, we have to try them
4870 (void) consumer_clear_quiescent_channel(socket
,
4871 reg_chan
->consumer_key
);
4876 case LTTNG_BUFFER_PER_PID
:
4878 struct ust_app_session
*ua_sess
;
4879 struct lttng_ht_iter iter
;
4880 struct ust_app
*app
;
4882 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4884 ua_sess
= lookup_session_by_app(usess
, app
);
4885 if (ua_sess
== NULL
) {
4888 (void) ust_app_clear_quiescent_app_session(app
,
4900 health_code_update();
4905 * Destroy a specific UST session in apps.
4907 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4910 struct ust_app_session
*ua_sess
;
4911 struct lttng_ht_iter iter
;
4912 struct lttng_ht_node_u64
*node
;
4914 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4918 if (!app
->compatible
) {
4922 __lookup_session_by_app(usess
, app
, &iter
);
4923 node
= lttng_ht_iter_get_node_u64(&iter
);
4925 /* Session is being or is deleted. */
4928 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4930 health_code_update();
4931 destroy_app_session(app
, ua_sess
);
4933 health_code_update();
4935 /* Quiescent wait after stopping trace */
4936 pthread_mutex_lock(&app
->sock_lock
);
4937 ret
= ustctl_wait_quiescent(app
->sock
);
4938 pthread_mutex_unlock(&app
->sock_lock
);
4939 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4940 ERR("UST app wait quiescent failed for app pid %d ret %d",
4945 health_code_update();
4950 * Start tracing for the UST session.
4952 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4955 struct lttng_ht_iter iter
;
4956 struct ust_app
*app
;
4958 DBG("Starting all UST traces");
4963 * In a start-stop-start use-case, we need to clear the quiescent state
4964 * of each channel set by the prior stop command, thus ensuring that a
4965 * following stop or destroy is sure to grab a timestamp_end near those
4966 * operations, even if the packet is empty.
4968 (void) ust_app_clear_quiescent_session(usess
);
4970 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4971 ret
= ust_app_start_trace(usess
, app
);
4973 /* Continue to next apps even on error */
4984 * Start tracing for the UST session.
4985 * Called with UST session lock held.
4987 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4990 struct lttng_ht_iter iter
;
4991 struct ust_app
*app
;
4993 DBG("Stopping all UST traces");
4997 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4998 ret
= ust_app_stop_trace(usess
, app
);
5000 /* Continue to next apps even on error */
5005 (void) ust_app_flush_session(usess
);
5013 * Destroy app UST session.
5015 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5018 struct lttng_ht_iter iter
;
5019 struct ust_app
*app
;
5021 DBG("Destroy all UST traces");
5025 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5026 ret
= destroy_trace(usess
, app
);
5028 /* Continue to next apps even on error */
5039 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5042 struct lttng_ht_iter iter
, uiter
;
5043 struct ust_app_session
*ua_sess
= NULL
;
5044 struct ust_app_channel
*ua_chan
;
5045 struct ust_app_event
*ua_event
;
5046 struct ust_app_ctx
*ua_ctx
;
5049 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
5051 /* Tracer is probably gone or ENOMEM. */
5055 /* App session already created. */
5060 pthread_mutex_lock(&ua_sess
->lock
);
5062 if (ua_sess
->deleted
) {
5063 pthread_mutex_unlock(&ua_sess
->lock
);
5068 * We can iterate safely here over all UST app session since the create ust
5069 * app session above made a shadow copy of the UST global domain from the
5072 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5074 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
5075 if (ret
< 0 && ret
!= -ENOTCONN
) {
5077 * Stop everything. On error, the application
5078 * failed, no more file descriptor are available
5079 * or ENOMEM so stopping here is the only thing
5080 * we can do for now. The only exception is
5081 * -ENOTCONN, which indicates that the application
5088 * Add context using the list so they are enabled in the same order the
5091 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
5092 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
5099 /* For each events */
5100 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
5102 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
5109 pthread_mutex_unlock(&ua_sess
->lock
);
5111 if (usess
->active
) {
5112 ret
= ust_app_start_trace(usess
, app
);
5117 DBG2("UST trace started for app pid %d", app
->pid
);
5120 /* Everything went well at this point. */
5124 pthread_mutex_unlock(&ua_sess
->lock
);
5127 destroy_app_session(app
, ua_sess
);
5133 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5135 struct ust_app_session
*ua_sess
;
5137 ua_sess
= lookup_session_by_app(usess
, app
);
5138 if (ua_sess
== NULL
) {
5141 destroy_app_session(app
, ua_sess
);
5145 * Add channels/events from UST global domain to registered apps at sock.
5147 * Called with session lock held.
5148 * Called with RCU read-side lock held.
5150 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5154 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5155 app
->sock
, usess
->id
);
5157 if (!app
->compatible
) {
5161 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
5162 ust_app_global_create(usess
, app
);
5164 ust_app_global_destroy(usess
, app
);
5169 * Called with session lock held.
5171 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5173 struct lttng_ht_iter iter
;
5174 struct ust_app
*app
;
5177 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5178 ust_app_global_update(usess
, app
);
5184 * Add context to a specific channel for global UST domain.
5186 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5187 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5190 struct lttng_ht_node_str
*ua_chan_node
;
5191 struct lttng_ht_iter iter
, uiter
;
5192 struct ust_app_channel
*ua_chan
= NULL
;
5193 struct ust_app_session
*ua_sess
;
5194 struct ust_app
*app
;
5198 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5199 if (!app
->compatible
) {
5201 * TODO: In time, we should notice the caller of this error by
5202 * telling him that this is a version error.
5206 ua_sess
= lookup_session_by_app(usess
, app
);
5207 if (ua_sess
== NULL
) {
5211 pthread_mutex_lock(&ua_sess
->lock
);
5213 if (ua_sess
->deleted
) {
5214 pthread_mutex_unlock(&ua_sess
->lock
);
5218 /* Lookup channel in the ust app session */
5219 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5220 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5221 if (ua_chan_node
== NULL
) {
5224 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5226 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5231 pthread_mutex_unlock(&ua_sess
->lock
);
5239 * Enable event for a channel from a UST session for a specific PID.
5241 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
5242 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
5245 struct lttng_ht_iter iter
;
5246 struct lttng_ht_node_str
*ua_chan_node
;
5247 struct ust_app
*app
;
5248 struct ust_app_session
*ua_sess
;
5249 struct ust_app_channel
*ua_chan
;
5250 struct ust_app_event
*ua_event
;
5252 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
5256 app
= ust_app_find_by_pid(pid
);
5258 ERR("UST app enable event per PID %d not found", pid
);
5263 if (!app
->compatible
) {
5268 ua_sess
= lookup_session_by_app(usess
, app
);
5270 /* The application has problem or is probably dead. */
5275 pthread_mutex_lock(&ua_sess
->lock
);
5277 if (ua_sess
->deleted
) {
5282 /* Lookup channel in the ust app session */
5283 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
5284 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5285 /* If the channel is not found, there is a code flow error */
5286 assert(ua_chan_node
);
5288 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5290 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5291 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5292 if (ua_event
== NULL
) {
5293 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5298 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
5305 pthread_mutex_unlock(&ua_sess
->lock
);
5312 * Receive registration and populate the given msg structure.
5314 * On success return 0 else a negative value returned by the ustctl call.
5316 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5319 uint32_t pid
, ppid
, uid
, gid
;
5323 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5324 &pid
, &ppid
, &uid
, &gid
,
5325 &msg
->bits_per_long
,
5326 &msg
->uint8_t_alignment
,
5327 &msg
->uint16_t_alignment
,
5328 &msg
->uint32_t_alignment
,
5329 &msg
->uint64_t_alignment
,
5330 &msg
->long_alignment
,
5337 case LTTNG_UST_ERR_EXITING
:
5338 DBG3("UST app recv reg message failed. Application died");
5340 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5341 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5342 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5343 LTTNG_UST_ABI_MINOR_VERSION
);
5346 ERR("UST app recv reg message failed with ret %d", ret
);
5351 msg
->pid
= (pid_t
) pid
;
5352 msg
->ppid
= (pid_t
) ppid
;
5353 msg
->uid
= (uid_t
) uid
;
5354 msg
->gid
= (gid_t
) gid
;
5361 * Return a ust app session object using the application object and the
5362 * session object descriptor has a key. If not found, NULL is returned.
5363 * A RCU read side lock MUST be acquired when calling this function.
5365 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5368 struct lttng_ht_node_ulong
*node
;
5369 struct lttng_ht_iter iter
;
5370 struct ust_app_session
*ua_sess
= NULL
;
5374 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5375 node
= lttng_ht_iter_get_node_ulong(&iter
);
5377 DBG2("UST app session find by objd %d not found", objd
);
5381 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5388 * Return a ust app channel object using the application object and the channel
5389 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5390 * lock MUST be acquired before calling this function.
5392 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5395 struct lttng_ht_node_ulong
*node
;
5396 struct lttng_ht_iter iter
;
5397 struct ust_app_channel
*ua_chan
= NULL
;
5401 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5402 node
= lttng_ht_iter_get_node_ulong(&iter
);
5404 DBG2("UST app channel find by objd %d not found", objd
);
5408 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5415 * Reply to a register channel notification from an application on the notify
5416 * socket. The channel metadata is also created.
5418 * The session UST registry lock is acquired in this function.
5420 * On success 0 is returned else a negative value.
5422 static int reply_ust_register_channel(int sock
, int cobjd
,
5423 size_t nr_fields
, struct ustctl_field
*fields
)
5425 int ret
, ret_code
= 0;
5426 uint32_t chan_id
, reg_count
;
5427 uint64_t chan_reg_key
;
5428 enum ustctl_channel_header type
;
5429 struct ust_app
*app
;
5430 struct ust_app_channel
*ua_chan
;
5431 struct ust_app_session
*ua_sess
;
5432 struct ust_registry_session
*registry
;
5433 struct ust_registry_channel
*chan_reg
;
5437 /* Lookup application. If not found, there is a code flow error. */
5438 app
= find_app_by_notify_sock(sock
);
5440 DBG("Application socket %d is being torn down. Abort event notify",
5443 goto error_rcu_unlock
;
5446 /* Lookup channel by UST object descriptor. */
5447 ua_chan
= find_channel_by_objd(app
, cobjd
);
5449 DBG("Application channel is being torn down. Abort event notify");
5451 goto error_rcu_unlock
;
5454 assert(ua_chan
->session
);
5455 ua_sess
= ua_chan
->session
;
5457 /* Get right session registry depending on the session buffer type. */
5458 registry
= get_session_registry(ua_sess
);
5460 DBG("Application session is being torn down. Abort event notify");
5462 goto error_rcu_unlock
;
5465 /* Depending on the buffer type, a different channel key is used. */
5466 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5467 chan_reg_key
= ua_chan
->tracing_channel_id
;
5469 chan_reg_key
= ua_chan
->key
;
5472 pthread_mutex_lock(®istry
->lock
);
5474 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5477 if (!chan_reg
->register_done
) {
5478 reg_count
= ust_registry_get_event_count(chan_reg
);
5479 if (reg_count
< 31) {
5480 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
5482 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5485 chan_reg
->nr_ctx_fields
= nr_fields
;
5486 chan_reg
->ctx_fields
= fields
;
5488 chan_reg
->header_type
= type
;
5490 /* Get current already assigned values. */
5491 type
= chan_reg
->header_type
;
5493 /* Channel id is set during the object creation. */
5494 chan_id
= chan_reg
->chan_id
;
5496 /* Append to metadata */
5497 if (!chan_reg
->metadata_dumped
) {
5498 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5500 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5506 DBG3("UST app replying to register channel key %" PRIu64
5507 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5510 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5512 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5513 ERR("UST app reply channel failed with ret %d", ret
);
5515 DBG3("UST app reply channel failed. Application died");
5520 /* This channel registry registration is completed. */
5521 chan_reg
->register_done
= 1;
5524 pthread_mutex_unlock(®istry
->lock
);
5532 * Add event to the UST channel registry. When the event is added to the
5533 * registry, the metadata is also created. Once done, this replies to the
5534 * application with the appropriate error code.
5536 * The session UST registry lock is acquired in the function.
5538 * On success 0 is returned else a negative value.
5540 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5541 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5542 int loglevel_value
, char *model_emf_uri
)
5545 uint32_t event_id
= 0;
5546 uint64_t chan_reg_key
;
5547 struct ust_app
*app
;
5548 struct ust_app_channel
*ua_chan
;
5549 struct ust_app_session
*ua_sess
;
5550 struct ust_registry_session
*registry
;
5554 /* Lookup application. If not found, there is a code flow error. */
5555 app
= find_app_by_notify_sock(sock
);
5557 DBG("Application socket %d is being torn down. Abort event notify",
5560 goto error_rcu_unlock
;
5563 /* Lookup channel by UST object descriptor. */
5564 ua_chan
= find_channel_by_objd(app
, cobjd
);
5566 DBG("Application channel is being torn down. Abort event notify");
5568 goto error_rcu_unlock
;
5571 assert(ua_chan
->session
);
5572 ua_sess
= ua_chan
->session
;
5574 registry
= get_session_registry(ua_sess
);
5576 DBG("Application session is being torn down. Abort event notify");
5578 goto error_rcu_unlock
;
5581 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5582 chan_reg_key
= ua_chan
->tracing_channel_id
;
5584 chan_reg_key
= ua_chan
->key
;
5587 pthread_mutex_lock(®istry
->lock
);
5590 * From this point on, this call acquires the ownership of the sig, fields
5591 * and model_emf_uri meaning any free are done inside it if needed. These
5592 * three variables MUST NOT be read/write after this.
5594 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5595 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5596 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5600 model_emf_uri
= NULL
;
5603 * The return value is returned to ustctl so in case of an error, the
5604 * application can be notified. In case of an error, it's important not to
5605 * return a negative error or else the application will get closed.
5607 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5609 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5610 ERR("UST app reply event failed with ret %d", ret
);
5612 DBG3("UST app reply event failed. Application died");
5615 * No need to wipe the create event since the application socket will
5616 * get close on error hence cleaning up everything by itself.
5621 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5625 pthread_mutex_unlock(®istry
->lock
);
5630 free(model_emf_uri
);
5635 * Add enum to the UST session registry. Once done, this replies to the
5636 * application with the appropriate error code.
5638 * The session UST registry lock is acquired within this function.
5640 * On success 0 is returned else a negative value.
5642 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5643 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5645 int ret
= 0, ret_code
;
5646 struct ust_app
*app
;
5647 struct ust_app_session
*ua_sess
;
5648 struct ust_registry_session
*registry
;
5649 uint64_t enum_id
= -1ULL;
5653 /* Lookup application. If not found, there is a code flow error. */
5654 app
= find_app_by_notify_sock(sock
);
5656 /* Return an error since this is not an error */
5657 DBG("Application socket %d is being torn down. Aborting enum registration",
5660 goto error_rcu_unlock
;
5663 /* Lookup session by UST object descriptor. */
5664 ua_sess
= find_session_by_objd(app
, sobjd
);
5666 /* Return an error since this is not an error */
5667 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5669 goto error_rcu_unlock
;
5672 registry
= get_session_registry(ua_sess
);
5674 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5676 goto error_rcu_unlock
;
5679 pthread_mutex_lock(®istry
->lock
);
5682 * From this point on, the callee acquires the ownership of
5683 * entries. The variable entries MUST NOT be read/written after
5686 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5687 entries
, nr_entries
, &enum_id
);
5691 * The return value is returned to ustctl so in case of an error, the
5692 * application can be notified. In case of an error, it's important not to
5693 * return a negative error or else the application will get closed.
5695 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5697 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5698 ERR("UST app reply enum failed with ret %d", ret
);
5700 DBG3("UST app reply enum failed. Application died");
5703 * No need to wipe the create enum since the application socket will
5704 * get close on error hence cleaning up everything by itself.
5709 DBG3("UST registry enum %s added successfully or already found", name
);
5712 pthread_mutex_unlock(®istry
->lock
);
5719 * Handle application notification through the given notify socket.
5721 * Return 0 on success or else a negative value.
5723 int ust_app_recv_notify(int sock
)
5726 enum ustctl_notify_cmd cmd
;
5728 DBG3("UST app receiving notify from sock %d", sock
);
5730 ret
= ustctl_recv_notify(sock
, &cmd
);
5732 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5733 ERR("UST app recv notify failed with ret %d", ret
);
5735 DBG3("UST app recv notify failed. Application died");
5741 case USTCTL_NOTIFY_CMD_EVENT
:
5743 int sobjd
, cobjd
, loglevel_value
;
5744 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5746 struct ustctl_field
*fields
;
5748 DBG2("UST app ustctl register event received");
5750 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5751 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5754 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5755 ERR("UST app recv event failed with ret %d", ret
);
5757 DBG3("UST app recv event failed. Application died");
5763 * Add event to the UST registry coming from the notify socket. This
5764 * call will free if needed the sig, fields and model_emf_uri. This
5765 * code path loses the ownsership of these variables and transfer them
5766 * to the this function.
5768 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5769 fields
, loglevel_value
, model_emf_uri
);
5776 case USTCTL_NOTIFY_CMD_CHANNEL
:
5780 struct ustctl_field
*fields
;
5782 DBG2("UST app ustctl register channel received");
5784 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5787 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5788 ERR("UST app recv channel failed with ret %d", ret
);
5790 DBG3("UST app recv channel failed. Application died");
5796 * The fields ownership are transfered to this function call meaning
5797 * that if needed it will be freed. After this, it's invalid to access
5798 * fields or clean it up.
5800 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5808 case USTCTL_NOTIFY_CMD_ENUM
:
5811 char name
[LTTNG_UST_SYM_NAME_LEN
];
5813 struct ustctl_enum_entry
*entries
;
5815 DBG2("UST app ustctl register enum received");
5817 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5818 &entries
, &nr_entries
);
5820 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5821 ERR("UST app recv enum failed with ret %d", ret
);
5823 DBG3("UST app recv enum failed. Application died");
5828 /* Callee assumes ownership of entries */
5829 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5830 entries
, nr_entries
);
5838 /* Should NEVER happen. */
5847 * Once the notify socket hangs up, this is called. First, it tries to find the
5848 * corresponding application. On failure, the call_rcu to close the socket is
5849 * executed. If an application is found, it tries to delete it from the notify
5850 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5852 * Note that an object needs to be allocated here so on ENOMEM failure, the
5853 * call RCU is not done but the rest of the cleanup is.
5855 void ust_app_notify_sock_unregister(int sock
)
5858 struct lttng_ht_iter iter
;
5859 struct ust_app
*app
;
5860 struct ust_app_notify_sock_obj
*obj
;
5866 obj
= zmalloc(sizeof(*obj
));
5869 * An ENOMEM is kind of uncool. If this strikes we continue the
5870 * procedure but the call_rcu will not be called. In this case, we
5871 * accept the fd leak rather than possibly creating an unsynchronized
5872 * state between threads.
5874 * TODO: The notify object should be created once the notify socket is
5875 * registered and stored independantely from the ust app object. The
5876 * tricky part is to synchronize the teardown of the application and
5877 * this notify object. Let's keep that in mind so we can avoid this
5878 * kind of shenanigans with ENOMEM in the teardown path.
5885 DBG("UST app notify socket unregister %d", sock
);
5888 * Lookup application by notify socket. If this fails, this means that the
5889 * hash table delete has already been done by the application
5890 * unregistration process so we can safely close the notify socket in a
5893 app
= find_app_by_notify_sock(sock
);
5898 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5901 * Whatever happens here either we fail or succeed, in both cases we have
5902 * to close the socket after a grace period to continue to the call RCU
5903 * here. If the deletion is successful, the application is not visible
5904 * anymore by other threads and is it fails it means that it was already
5905 * deleted from the hash table so either way we just have to close the
5908 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5914 * Close socket after a grace period to avoid for the socket to be reused
5915 * before the application object is freed creating potential race between
5916 * threads trying to add unique in the global hash table.
5919 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5924 * Destroy a ust app data structure and free its memory.
5926 void ust_app_destroy(struct ust_app
*app
)
5932 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5936 * Take a snapshot for a given UST session. The snapshot is sent to the given
5939 * Return 0 on success or else a negative value.
5941 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5942 struct snapshot_output
*output
, int wait
,
5943 uint64_t nb_packets_per_stream
)
5946 struct lttng_ht_iter iter
;
5947 struct ust_app
*app
;
5948 char pathname
[PATH_MAX
];
5955 switch (usess
->buffer_type
) {
5956 case LTTNG_BUFFER_PER_UID
:
5958 struct buffer_reg_uid
*reg
;
5960 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5961 struct buffer_reg_channel
*reg_chan
;
5962 struct consumer_socket
*socket
;
5964 /* Get consumer socket to use to push the metadata.*/
5965 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5972 memset(pathname
, 0, sizeof(pathname
));
5973 ret
= snprintf(pathname
, sizeof(pathname
),
5974 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5975 reg
->uid
, reg
->bits_per_long
);
5977 PERROR("snprintf snapshot path");
5981 /* Add the UST default trace dir to path. */
5982 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5983 reg_chan
, node
.node
) {
5984 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5985 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5986 nb_packets_per_stream
);
5991 ret
= consumer_snapshot_channel(socket
,
5992 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5993 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
6000 case LTTNG_BUFFER_PER_PID
:
6002 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6003 struct consumer_socket
*socket
;
6004 struct lttng_ht_iter chan_iter
;
6005 struct ust_app_channel
*ua_chan
;
6006 struct ust_app_session
*ua_sess
;
6007 struct ust_registry_session
*registry
;
6009 ua_sess
= lookup_session_by_app(usess
, app
);
6011 /* Session not associated with this app. */
6015 /* Get the right consumer socket for the application. */
6016 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6023 /* Add the UST default trace dir to path. */
6024 memset(pathname
, 0, sizeof(pathname
));
6025 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6028 PERROR("snprintf snapshot path");
6032 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6033 ua_chan
, node
.node
) {
6034 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
6035 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
6036 nb_packets_per_stream
);
6042 registry
= get_session_registry(ua_sess
);
6044 DBG("Application session is being torn down. Abort snapshot record.");
6048 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
6049 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
6067 * Return the size taken by one more packet per stream.
6069 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
6070 uint64_t cur_nr_packets
)
6072 uint64_t tot_size
= 0;
6073 struct ust_app
*app
;
6074 struct lttng_ht_iter iter
;
6078 switch (usess
->buffer_type
) {
6079 case LTTNG_BUFFER_PER_UID
:
6081 struct buffer_reg_uid
*reg
;
6083 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6084 struct buffer_reg_channel
*reg_chan
;
6087 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6088 reg_chan
, node
.node
) {
6089 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6091 * Don't take channel into account if we
6092 * already grab all its packets.
6096 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6102 case LTTNG_BUFFER_PER_PID
:
6105 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6106 struct ust_app_channel
*ua_chan
;
6107 struct ust_app_session
*ua_sess
;
6108 struct lttng_ht_iter chan_iter
;
6110 ua_sess
= lookup_session_by_app(usess
, app
);
6112 /* Session not associated with this app. */
6116 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6117 ua_chan
, node
.node
) {
6118 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6120 * Don't take channel into account if we
6121 * already grab all its packets.
6125 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6139 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6140 struct cds_list_head
*buffer_reg_uid_list
,
6141 struct consumer_output
*consumer
, uint64_t uchan_id
,
6142 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6145 uint64_t consumer_chan_key
;
6150 ret
= buffer_reg_uid_consumer_channel_key(
6151 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6159 ret
= consumer_get_lost_packets(ust_session_id
,
6160 consumer_chan_key
, consumer
, lost
);
6162 ret
= consumer_get_discarded_events(ust_session_id
,
6163 consumer_chan_key
, consumer
, discarded
);
6170 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6171 struct ltt_ust_channel
*uchan
,
6172 struct consumer_output
*consumer
, int overwrite
,
6173 uint64_t *discarded
, uint64_t *lost
)
6176 struct lttng_ht_iter iter
;
6177 struct lttng_ht_node_str
*ua_chan_node
;
6178 struct ust_app
*app
;
6179 struct ust_app_session
*ua_sess
;
6180 struct ust_app_channel
*ua_chan
;
6187 * Iterate over every registered applications. Sum counters for
6188 * all applications containing requested session and channel.
6190 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6191 struct lttng_ht_iter uiter
;
6193 ua_sess
= lookup_session_by_app(usess
, app
);
6194 if (ua_sess
== NULL
) {
6199 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6200 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6201 /* If the session is found for the app, the channel must be there */
6202 assert(ua_chan_node
);
6204 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6209 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6216 uint64_t _discarded
;
6218 ret
= consumer_get_discarded_events(usess
->id
,
6219 ua_chan
->key
, consumer
, &_discarded
);
6223 (*discarded
) += _discarded
;
6232 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6233 struct ust_app
*app
)
6236 struct ust_app_session
*ua_sess
;
6238 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6242 ua_sess
= lookup_session_by_app(usess
, app
);
6243 if (ua_sess
== NULL
) {
6244 /* The session is in teardown process. Ignore and continue. */
6248 pthread_mutex_lock(&ua_sess
->lock
);
6250 if (ua_sess
->deleted
) {
6254 pthread_mutex_lock(&app
->sock_lock
);
6255 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6256 pthread_mutex_unlock(&app
->sock_lock
);
6259 pthread_mutex_unlock(&ua_sess
->lock
);
6263 health_code_update();
6268 * Regenerate the statedump for each app in the session.
6270 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6273 struct lttng_ht_iter iter
;
6274 struct ust_app
*app
;
6276 DBG("Regenerating the metadata for all UST apps");
6280 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6281 if (!app
->compatible
) {
6285 ret
= ust_app_regenerate_statedump(usess
, app
);
6287 /* Continue to the next app even on error */
6298 * Rotate all the channels of a session.
6300 * Return 0 on success or else a negative value.
6302 int ust_app_rotate_session(struct ltt_session
*session
, bool *ust_active
)
6305 struct lttng_ht_iter iter
;
6306 struct ust_app
*app
;
6307 struct ltt_ust_session
*usess
= session
->ust_session
;
6308 char pathname
[LTTNG_PATH_MAX
];
6314 switch (usess
->buffer_type
) {
6315 case LTTNG_BUFFER_PER_UID
:
6317 struct buffer_reg_uid
*reg
;
6319 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6320 struct buffer_reg_channel
*reg_chan
;
6321 struct consumer_socket
*socket
;
6323 /* Get consumer socket to use to push the metadata.*/
6324 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6332 * Account the metadata channel first to make sure the
6333 * number of channels waiting for a rotation cannot
6334 * reach 0 before we complete the iteration over all
6337 ret
= rotate_add_channel_pending(
6338 reg
->registry
->reg
.ust
->metadata_key
,
6339 LTTNG_DOMAIN_UST
, session
);
6341 ret
= reg
->bits_per_long
== 32 ?
6342 -LTTNG_ERR_UST_CONSUMER32_FAIL
:
6343 -LTTNG_ERR_UST_CONSUMER64_FAIL
;
6347 ret
= snprintf(pathname
, sizeof(pathname
),
6348 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6349 reg
->uid
, reg
->bits_per_long
);
6350 if (ret
< 0 || ret
== sizeof(pathname
)) {
6351 PERROR("Failed to format rotation path");
6355 /* Rotate the data channels. */
6356 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6357 reg_chan
, node
.node
) {
6358 ret
= rotate_add_channel_pending(
6359 reg_chan
->consumer_key
,
6360 LTTNG_DOMAIN_UST
, session
);
6362 ret
= reg
->bits_per_long
== 32 ?
6363 -LTTNG_ERR_UST_CONSUMER32_FAIL
:
6364 -LTTNG_ERR_UST_CONSUMER64_FAIL
;
6367 ret
= consumer_rotate_channel(socket
,
6368 reg_chan
->consumer_key
,
6369 usess
->uid
, usess
->gid
,
6370 usess
->consumer
, pathname
,
6371 /* is_metadata_channel */ false,
6372 session
->current_archive_id
,
6373 &session
->rotate_pending_relay
);
6379 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6381 ret
= consumer_rotate_channel(socket
,
6382 reg
->registry
->reg
.ust
->metadata_key
,
6383 usess
->uid
, usess
->gid
,
6384 usess
->consumer
, pathname
,
6385 /* is_metadata_channel */ true,
6386 session
->current_archive_id
,
6387 &session
->rotate_pending_relay
);
6395 case LTTNG_BUFFER_PER_PID
:
6397 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6398 struct consumer_socket
*socket
;
6399 struct lttng_ht_iter chan_iter
;
6400 struct ust_app_channel
*ua_chan
;
6401 struct ust_app_session
*ua_sess
;
6402 struct ust_registry_session
*registry
;
6404 ua_sess
= lookup_session_by_app(usess
, app
);
6406 /* Session not associated with this app. */
6409 ret
= snprintf(pathname
, sizeof(pathname
),
6410 DEFAULT_UST_TRACE_DIR
"/%s",
6412 if (ret
< 0 || ret
== sizeof(pathname
)) {
6413 PERROR("Failed to format rotation path");
6417 /* Get the right consumer socket for the application. */
6418 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6425 registry
= get_session_registry(ua_sess
);
6427 DBG("Application session is being torn down. Abort snapshot record.");
6433 * Account the metadata channel first to make sure the
6434 * number of channels waiting for a rotation cannot
6435 * reach 0 before we complete the iteration over all
6438 ret
= rotate_add_channel_pending(registry
->metadata_key
,
6439 LTTNG_DOMAIN_UST
, session
);
6441 ret
= app
->bits_per_long
== 32 ?
6442 -LTTNG_ERR_UST_CONSUMER32_FAIL
:
6443 -LTTNG_ERR_UST_CONSUMER64_FAIL
;
6447 /* Rotate the data channels. */
6448 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6449 ua_chan
, node
.node
) {
6450 ret
= rotate_add_channel_pending(
6451 ua_chan
->key
, LTTNG_DOMAIN_UST
,
6454 ret
= app
->bits_per_long
== 32 ?
6455 -LTTNG_ERR_UST_CONSUMER32_FAIL
:
6456 -LTTNG_ERR_UST_CONSUMER64_FAIL
;
6459 ret
= consumer_rotate_channel(socket
, ua_chan
->key
,
6460 ua_sess
->euid
, ua_sess
->egid
,
6461 ua_sess
->consumer
, pathname
,
6462 /* is_metadata_channel */ false,
6463 session
->current_archive_id
,
6464 &session
->rotate_pending_relay
);
6470 /* Rotate the metadata channel. */
6471 (void) push_metadata(registry
, usess
->consumer
);
6472 ret
= consumer_rotate_channel(socket
, registry
->metadata_key
,
6473 ua_sess
->euid
, ua_sess
->egid
,
6474 ua_sess
->consumer
, pathname
,
6475 /* is_metadata_channel */ true,
6476 session
->current_archive_id
,
6477 &session
->rotate_pending_relay
);